From ddef591c779ab1b3a2012f1241a49cc87c58c776 Mon Sep 17 00:00:00 2001 From: Heinz Mauelshagen Date: Wed, 4 May 2005 15:30:51 +0000 Subject: [PATCH] introduced dynamic loading of pthreads lock function --- multilog/libmultilog.c | 35 ++++++++++++++++++++++++++++++++-- multilog/libmultilog.h | 1 + multilog/tests/test-multilog.c | 5 +++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/multilog/libmultilog.c b/multilog/libmultilog.c index abebb70..3302d81 100644 --- a/multilog/libmultilog.c +++ b/multilog/libmultilog.c @@ -40,15 +40,38 @@ struct log_list { static LIST_INIT(logs); /* Mutext for logs accesses. */ -static void *mutex = NULL; +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +static int (*lock_fn)(pthread_mutex_t *) = NULL; +static int (*unlock_fn)(pthread_mutex_t *) = NULL; + static void lock_mutex(void) { + if (lock_fn) + (*lock_fn)(&mutex); } static void unlock_mutex(void) { + if (unlock_fn) + (*unlock_fn)(&mutex); } +static int load_pthread_syms(struct log_data *data) +{ + void *dlh; + + if (!(dlh = dlopen("libpthread.so", RTLD_NOW))) { + fprintf(stderr, "%s\n", dlerror()); + return 0; + } + + data->info.threaded_syslog.pthread_dlh = dlh; + + return (lock_fn = dlsym(dlh, "pthread_mutex_lock")) && + (unlock_fn = dlsym(dlh, "pthread_mutex_unlock")); +} + + /* 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) @@ -119,6 +142,12 @@ static int start_threaded_syslog(struct log_list *logl, log_fxn = dlsym(logdata->info.threaded_syslog.dlh, "write_to_buf"); start_syslog = dlsym(logdata->info.threaded_syslog.dlh, "start_syslog_thread"); + if (!log_fxn || !start_syslog || + !load_pthread_syms(logdata)) { + dlclose(logdata->info.threaded_syslog.dlh); + return 0; + } + /* FIXME: the timeout here probably can be tweaked */ /* FIXME: Probably want to do something if this fails */ if (start_syslog(&(logdata->info.threaded_syslog.thread), 100000)) @@ -154,7 +183,6 @@ int multilog_add_type(enum log_type type, struct log_data *data) } } - list_init(&logl->list); /* Superfluous but safe ;) */ logl->type = type; logl->data = data; @@ -249,6 +277,9 @@ void multilog_del_type(enum log_type type, struct log_data *data) if ((stop_syslog = dlsym(data->info.threaded_syslog.dlh, "stop_syslog_thread"))) stop_syslog(data); + + dlclose(data->info.threaded_syslog.dlh); + dlclose(data->info.threaded_syslog.pthread_dlh); } free(ll); diff --git a/multilog/libmultilog.h b/multilog/libmultilog.h index 4c64148..cdd59ad 100644 --- a/multilog/libmultilog.h +++ b/multilog/libmultilog.h @@ -34,6 +34,7 @@ struct file_log { struct threaded_syslog_log { pthread_t thread; void *dlh; + void *pthread_dlh; }; union log_info { diff --git a/multilog/tests/test-multilog.c b/multilog/tests/test-multilog.c index 0d7f757..badb61c 100644 --- a/multilog/tests/test-multilog.c +++ b/multilog/tests/test-multilog.c @@ -16,7 +16,7 @@ int main(int argc, char **argv) multilog_add_type(standard, &logdata); - for (i = 0; i < 100; i++) { + for (i = 0; i < 50; i++) { log_err("Testing really long strings so that we can " "fill the buffer up and show skips %d", i); @@ -36,11 +36,12 @@ 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, &logdata); log_err("Test of errors5"); log_err("Test of errors6"); + multilog_del_type(standard, NULL); multilog_del_type(threaded_syslog, &logdata); exit(EXIT_SUCCESS); -- 2.43.5