]> code.delx.au - pulseaudio/blobdiff - src/daemon/ltdl-bind-now.c
adjust various data/library paths automatically if we are run from a build tree
[pulseaudio] / src / daemon / ltdl-bind-now.c
index b17706745fbcfac577c34e16048376061596601c..276b2a06eb1eec4000fe75809edc47ec440ea46a 100644 (file)
@@ -1,12 +1,12 @@
 /***
   This file is part of PulseAudio.
 
 /***
   This file is part of PulseAudio.
 
-  Copyright 2004-2006 Lennart Poettering
+  Copyright 2004-2008 Lennart Poettering
   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
-  by the Free Software Foundation; either version 2 of the License,
+  by the Free Software Foundation; either version 2.1 of the License,
   or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
   or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
 #include <config.h>
 #endif
 
 #include <config.h>
 #endif
 
-#if HAVE_DLFCN_H
+#ifdef HAVE_DLFCN_H
 #include <dlfcn.h>
 #endif
 
 #include <dlfcn.h>
 #endif
 
-#if HAVE_SYS_DL_H
+#ifdef HAVE_SYS_DL_H
 #include <sys/dl.h>
 #endif
 
 #include <sys/dl.h>
 #endif
 
-#ifndef HAVE_STRUCT_LT_USER_DLLOADER
-/* Only used with ltdl 2.2 */
 #include <string.h>
 #include <string.h>
-#endif
 
 #include <ltdl.h>
 
 
 #include <ltdl.h>
 
+#include <pulse/i18n.h>
+
 #include <pulsecore/macro.h>
 #include <pulsecore/macro.h>
-#include <pulsecore/mutex.h>
-#include <pulsecore/thread.h>
 #include <pulsecore/log.h>
 
 #include "ltdl-bind-now.h"
 #include <pulsecore/log.h>
 
 #include "ltdl-bind-now.h"
 #undef PA_BIND_NOW
 #endif
 
 #undef PA_BIND_NOW
 #endif
 
-static pa_mutex *libtool_mutex = NULL;
-
-PA_STATIC_TLS_DECLARE_NO_FREE(libtool_tls);
-
-static void libtool_lock(void) {
-    pa_mutex_lock(libtool_mutex);
-}
-
-static void libtool_unlock(void) {
-    pa_mutex_unlock(libtool_mutex);
-}
-
-static void libtool_set_error(const char *error) {
-    PA_STATIC_TLS_SET(libtool_tls, (char*) error);
-}
-
-static const char *libtool_get_error(void) {
-    return PA_STATIC_TLS_GET(libtool_tls);
-}
-
 #ifdef PA_BIND_NOW
 
 /*
 #ifdef PA_BIND_NOW
 
 /*
@@ -88,17 +65,13 @@ static const char *libtool_get_error(void) {
   to set $LT_BIND_NOW before starting the pulsaudio binary.
 */
 
   to set $LT_BIND_NOW before starting the pulsaudio binary.
 */
 
-#ifndef HAVE_LT_DLADVISE
-static lt_module bind_now_open(lt_user_data d, const char *fname) {
-#else
-  static lt_module bind_now_open(lt_user_data d, const char *fname, lt_dladvise advise) {
-#endif
+static lt_module bind_now_open(lt_user_data d, const char *fname, lt_dladvise advise) {
     lt_module m;
 
     pa_assert(fname);
 
     if (!(m = dlopen(fname, PA_BIND_NOW))) {
     lt_module m;
 
     pa_assert(fname);
 
     if (!(m = dlopen(fname, PA_BIND_NOW))) {
-        libtool_set_error(dlerror());
+        lt_dlseterror(LT_ERROR_CANNOT_OPEN);
         return NULL;
     }
 
         return NULL;
     }
 
@@ -110,7 +83,7 @@ static int bind_now_close(lt_user_data d, lt_module m) {
     pa_assert(m);
 
     if (dlclose(m) != 0){
     pa_assert(m);
 
     if (dlclose(m) != 0){
-        libtool_set_error(dlerror());
+        lt_dlseterror(LT_ERROR_CANNOT_CLOSE);
         return 1;
     }
 
         return 1;
     }
 
@@ -124,72 +97,61 @@ static lt_ptr bind_now_find_sym(lt_user_data d, lt_module m, const char *symbol)
     pa_assert(symbol);
 
     if (!(ptr = dlsym(m, symbol))) {
     pa_assert(symbol);
 
     if (!(ptr = dlsym(m, symbol))) {
-        libtool_set_error(dlerror());
+        lt_dlseterror(LT_ERROR_SYMBOL_NOT_FOUND);
         return NULL;
     }
 
     return ptr;
 }
 
         return NULL;
     }
 
     return ptr;
 }
 
+static lt_dlvtable *bindnow_loader = NULL;
 #endif
 
 void pa_ltdl_init(void) {
 
 #ifdef PA_BIND_NOW
 #endif
 
 void pa_ltdl_init(void) {
 
 #ifdef PA_BIND_NOW
-# ifdef HAVE_STRUCT_LT_USER_DLLOADER
-    lt_dlloader *place;
-    static const struct lt_user_dlloader loader = {
-        .module_open = bind_now_open,
-        .module_close = bind_now_close,
-        .find_sym = bind_now_find_sym
-    };
-# else
-    static const lt_dlvtable *dlopen_loader;
-    static lt_dlvtable bindnow_loader;
-# endif
+    const lt_dlvtable *dlopen_loader;
 #endif
 
     pa_assert_se(lt_dlinit() == 0);
 #endif
 
     pa_assert_se(lt_dlinit() == 0);
-    pa_assert_se(libtool_mutex = pa_mutex_new(TRUE, FALSE));
-#ifdef HAVE_LT_DLMUTEX_REGISTER
-    pa_assert_se(lt_dlmutex_register(libtool_lock, libtool_unlock, libtool_set_error, libtool_get_error) == 0);
-#endif
 
 #ifdef PA_BIND_NOW
 
 #ifdef PA_BIND_NOW
-# ifdef HAVE_STRUCT_LT_USER_DLLOADER
-
-    if (!(place = lt_dlloader_find("dlopen")))
-        place = lt_dlloader_next(NULL);
-
-    /* Add our BIND_NOW loader as the default module loader. */
-    if (lt_dlloader_add(place, &loader, "bind-now-loader") != 0)
-        pa_log_warn("Failed to add bind-now-loader.");
-# else
     /* Already initialised */
     /* Already initialised */
-    if ( dlopen_loader != NULL ) return;
+    if (bindnow_loader)
+        return;
+
+    if (!(dlopen_loader = lt_dlloader_find((char*) "lt_dlopen"))) {
+        pa_log_warn(_("Failed to find original lt_dlopen loader."));
+        return;
+    }
 
 
-    if (!(dlopen_loader = lt_dlloader_find("dlopen"))) {
-      pa_log_warn("Failed to find original dlopen loader.");
-      return;
+    if (!(bindnow_loader = malloc(sizeof(lt_dlvtable)))) {
+        pa_log_error(_("Failed to allocate new dl loader."));
+        return;
     }
 
     }
 
-    memcpy(&bindnow_loader, dlopen_loader, sizeof(bindnow_loader));
-    bindnow_loader.name = "bind-now-loader";
-    bindnow_loader.module_open = bind_now_open;
-    bindnow_loader.module_close = bind_now_close;
-    bindnow_loader.find_sym = bind_now_find_sym;
-    bindnow_loader.priority = LT_DLLOADER_PREPEND;
+    memcpy(bindnow_loader, dlopen_loader, sizeof(*bindnow_loader));
+    bindnow_loader->name = "bind-now-loader";
+    bindnow_loader->module_open = bind_now_open;
+    bindnow_loader->module_close = bind_now_close;
+    bindnow_loader->find_sym = bind_now_find_sym;
+    bindnow_loader->priority = LT_DLLOADER_PREPEND;
 
     /* Add our BIND_NOW loader as the default module loader. */
 
     /* Add our BIND_NOW loader as the default module loader. */
-    if (lt_dlloader_add(&bindnow_loader) != 0)
-        pa_log_warn("Failed to add bind-now-loader.");
-# endif
+    if (lt_dlloader_add(bindnow_loader) != 0) {
+        pa_log_warn(_("Failed to add bind-now-loader."));
+        free(bindnow_loader);
+        bindnow_loader = NULL;
+    }
 #endif
 }
 
 void pa_ltdl_done(void) {
     pa_assert_se(lt_dlexit() == 0);
 #endif
 }
 
 void pa_ltdl_done(void) {
     pa_assert_se(lt_dlexit() == 0);
-    pa_mutex_free(libtool_mutex);
-    libtool_mutex = NULL;
-}
 
 
+#ifdef PA_BIND_NOW
+    /* lt_dlexit() will free our loader vtable, hence reset our
+     * pointer to it here */
+    bindnow_loader = NULL;
+#endif
+}