]> sourceware.org Git - dm.git/commitdiff
introduced dynamic loading of pthreads lock function
authorHeinz Mauelshagen <heinzm@redhat.com>
Wed, 4 May 2005 15:30:51 +0000 (15:30 +0000)
committerHeinz Mauelshagen <heinzm@redhat.com>
Wed, 4 May 2005 15:30:51 +0000 (15:30 +0000)
multilog/libmultilog.c
multilog/libmultilog.h
multilog/tests/test-multilog.c

index abebb704ad7e820c7d45694838ef6f14c58286f6..3302d81c67e2c7c07158aa860988e5d1e33d8b28 100644 (file)
@@ -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);
index 4c6414808c16a15c1829fc1166968ecfc9858a79..cdd59ad3460c7a9c3fd47226867a9b2667ba4ad5 100644 (file)
@@ -34,6 +34,7 @@ struct file_log {
 struct threaded_syslog_log {
        pthread_t thread;
        void *dlh;
+       void *pthread_dlh;
 };
 
 union log_info {
index 0d7f757507d1c300496ed8e3bc250049a3da9272..badb61c7b925b5403b27b7218e5b570ad57cb6f4 100644 (file)
@@ -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);
This page took 0.025488 seconds and 5 git commands to generate.