]> code.delx.au - pulseaudio/blob - src/daemon/ltdl-bind-now.c
drop a couple of WARNING prefixes in log messages, since we have pa_log_warn anyway...
[pulseaudio] / src / daemon / ltdl-bind-now.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #if HAVE_DLFCN_H
30 #include <dlfcn.h>
31 #endif
32
33 #if HAVE_SYS_DL_H
34 #include <sys/dl.h>
35 #endif
36
37 #include <ltdl.h>
38
39 #include <pulsecore/macro.h>
40 #include <pulsecore/mutex.h>
41 #include <pulsecore/thread.h>
42 #include <pulsecore/log.h>
43
44 #include "ltdl-bind-now.h"
45
46 #ifdef RTLD_NOW
47 #define PA_BIND_NOW RTLD_NOW
48 #elif defined(DL_NOW)
49 #define PA_BIND_NOW DL_NOW
50 #else
51 #undef PA_BIND_NOW
52 #endif
53
54 static pa_mutex *libtool_mutex = NULL;
55
56 PA_STATIC_TLS_DECLARE_NO_FREE(libtool_tls);
57
58 static void libtool_lock(void) {
59 pa_mutex_lock(libtool_mutex);
60 }
61
62 static void libtool_unlock(void) {
63 pa_mutex_unlock(libtool_mutex);
64 }
65
66 static void libtool_set_error(const char *error) {
67 PA_STATIC_TLS_SET(libtool_tls, (char*) error);
68 }
69
70 static const char *libtool_get_error(void) {
71 return PA_STATIC_TLS_GET(libtool_tls);
72 }
73
74 #ifdef PA_BIND_NOW
75
76 /*
77 To avoid lazy relocations during runtime in our RT threads we add
78 our own shared object loader with uses RTLD_NOW if it is
79 available. The standard ltdl loader prefers RTLD_LAZY.
80
81 Please note that this loader doesn't have any influence on
82 relocations on any libraries that are already loaded into our
83 process, i.e. because the pulseaudio binary links directly to
84 them. To disable lazy relocations for those libraries it is possible
85 to set $LT_BIND_NOW before starting the pulsaudio binary.
86 */
87
88 static lt_module bind_now_open(lt_user_data d, const char *fname) {
89 lt_module m;
90
91 pa_assert(fname);
92
93 if (!(m = dlopen(fname, PA_BIND_NOW))) {
94 libtool_set_error(dlerror());
95 return NULL;
96 }
97
98 return m;
99 }
100
101 static int bind_now_close(lt_user_data d, lt_module m) {
102
103 pa_assert(m);
104
105 if (dlclose(m) != 0){
106 libtool_set_error(dlerror());
107 return 1;
108 }
109
110 return 0;
111 }
112
113 static lt_ptr bind_now_find_sym(lt_user_data d, lt_module m, const char *symbol) {
114 lt_ptr ptr;
115
116 pa_assert(m);
117 pa_assert(symbol);
118
119 if (!(ptr = dlsym(m, symbol))) {
120 libtool_set_error(dlerror());
121 return NULL;
122 }
123
124 return ptr;
125 }
126
127 #endif
128
129 void pa_ltdl_init(void) {
130
131 #ifdef PA_BIND_NOW
132 lt_dlloader *place;
133 static const struct lt_user_dlloader loader = {
134 .module_open = bind_now_open,
135 .module_close = bind_now_close,
136 .find_sym = bind_now_find_sym
137 };
138 #endif
139
140 pa_assert_se(lt_dlinit() == 0);
141 pa_assert_se(libtool_mutex = pa_mutex_new(TRUE, FALSE));
142 pa_assert_se(lt_dlmutex_register(libtool_lock, libtool_unlock, libtool_set_error, libtool_get_error) == 0);
143
144 #ifdef PA_BIND_NOW
145
146 if (!(place = lt_dlloader_find("dlopen")))
147 place = lt_dlloader_next(NULL);
148
149 /* Add our BIND_NOW loader as the default module loader. */
150 if (lt_dlloader_add(place, &loader, "bind-now-loader") != 0)
151 pa_log_warn("Failed to add bind-now-loader.");
152 #endif
153 }
154
155 void pa_ltdl_done(void) {
156 pa_assert_se(lt_dlexit() == 0);
157 pa_mutex_free(libtool_mutex);
158 libtool_mutex = NULL;
159 }
160