From 0e5ea7b4a90f2d204b03dc8b15897576fbbd6575 Mon Sep 17 00:00:00 2001 From: Heinz Mauelshagen Date: Wed, 4 May 2005 14:10:06 +0000 Subject: [PATCH] dummy lock_mutex/unlock_mutex calls for discussion --- multilog/libmultilog.c | 76 ++++++++++++++++++++++++---------- multilog/tests/test-multilog.c | 2 +- 2 files changed, 56 insertions(+), 22 deletions(-) diff --git a/multilog/libmultilog.c b/multilog/libmultilog.c index 6c518e6..93ee5de 100644 --- a/multilog/libmultilog.c +++ b/multilog/libmultilog.c @@ -39,6 +39,16 @@ struct log_list { /* FIXME: probably shouldn't do it this way, but... */ static LIST_INIT(logs); +/* Mutext for logs accesses. */ +static void *mutex = NULL; +static void lock_mutex(void) +{ +} + +static void unlock_mutex(void) +{ +} + /* Noop logging until the custom log fxn gets registered */ static void nop_log(void *data, int priority, const char *file, int line, const char *string) @@ -60,11 +70,8 @@ static void standard_log(void *data, int priority, const char *file, int line, switch (ldata->verbose_level) { case _LOG_DEBUG: - if (!strcmp("", string) && - ldata->verbose_level <= _LOG_DEBUG) - break; - - if (ldata->verbose_level >= _LOG_DEBUG) + if (strcmp("", string) && + ldata->verbose_level >= _LOG_DEBUG) fprintf(stderr, "%s%s\n", locn, string); break; @@ -123,21 +130,30 @@ static int start_threaded_syslog(struct log_list *logl, /* FIXME: Can currently add multiple logging types. */ int multilog_add_type(enum log_type type, struct log_data *data) { - struct log_list *logl; + struct log_list *logl, *ll; + + /* + * Preallocate because we don't want to sleep holding a lock. + */ + if (!(logl = malloc(sizeof(*logl))) || + !(memset(logl, 0, sizeof(*logl)))) + return 0; /* * If the type has already been registered, * it doesn't need to be registered again. */ - list_iterate_items(logl, &logs) { - if (logl->type == type) + lock_mutex(); + + list_iterate_items(ll, &logs) { + if (ll->type == type) { + unlock_mutex(); + free(logl); + return 1; + } } - if (!(logl = malloc(sizeof(*logl))) || - !(memset(logl, 0, sizeof(*logl)))) - return 0; - list_init(&logl->list); /* Superfluous but safe ;) */ logl->type = type; logl->data = data; @@ -167,11 +183,13 @@ int multilog_add_type(enum log_type type, struct log_data *data) } list_add(&logs, &logl->list); + unlock_mutex(); return 1; } /* Resets the logging handle to no logging */ +/* FIXME: how does this stop the logging threads ? */ void multilog_clear_logging(void) { struct list *tmp, *next; @@ -191,25 +209,33 @@ void multilog_clear_logging(void) void multilog_del_type(enum log_type type, struct log_data *data) { struct list *tmp, *next; - struct log_list *logl; + struct log_list *logl, *ll = NULL; + + /* First delete type from list safely. */ + lock_mutex(); list_iterate_safe(tmp, next, &logs) { logl = list_item(tmp, struct log_list); if (logl->type == type) { - if (logl->type == threaded_syslog) { - int (*stop_syslog) (struct log_data *log); - - if ((stop_syslog = dlsym(data->info.threaded_syslog.dlh, "stop_syslog_thread"))) - stop_syslog(data); - } - + ll = logl; list_del(tmp); - free(logl); break; } } + unlock_mutex(); + + if (ll) { + if (ll->type == threaded_syslog) { + int (*stop_syslog) (struct log_data *log); + + if ((stop_syslog = dlsym(data->info.threaded_syslog.dlh, "stop_syslog_thread"))) + stop_syslog(data); + } + + free(ll); + } } void multilog_custom(multilog_fn fn) @@ -220,10 +246,14 @@ void multilog_custom(multilog_fn fn) * FIXME: Should we present an error if * we can't find a suitable target? */ + lock_mutex(); + list_iterate_items(logl, &logs) { if (logl->type == custom && logl->log == nop_log) logl->log = fn; } + + unlock_mutex(); } @@ -239,6 +269,10 @@ void multilog(int priority, const char *file, int line, const char *format, ...) vsnprintf(buf, 4096, format, args); va_end(args); + lock_mutex(); + list_iterate_items(logl, &logs) logl->log(logl->data, priority, file, line, buf); + + unlock_mutex(); } diff --git a/multilog/tests/test-multilog.c b/multilog/tests/test-multilog.c index 42295e3..0d7f757 100644 --- a/multilog/tests/test-multilog.c +++ b/multilog/tests/test-multilog.c @@ -36,7 +36,7 @@ int main(int argc, char **argv) * FIXME: locking on libmultilog bytes here, because the * threaded log is still active. */ - // multilog_add_type(standard, NULL); + multilog_add_type(standard, NULL); log_err("Test of errors5"); log_err("Test of errors6"); -- 2.43.5