]> code.delx.au - pulseaudio/blob - src/daemon/ltdl-bind-now.c
really create glitch-free branch
[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 #ifndef HAVE_STRUCT_LT_USER_DLLOADER
38 /* Only used with ltdl 2.2 */
39 #include <string.h>
40 #endif
41
42 #include <ltdl.h>
43
44 #include <pulsecore/macro.h>
45 #include <pulsecore/mutex.h>
46 #include <pulsecore/thread.h>
47 #include <pulsecore/log.h>
48
49 #include "ltdl-bind-now.h"
50
51 #ifdef RTLD_NOW
52 #define PA_BIND_NOW RTLD_NOW
53 #elif defined(DL_NOW)
54 #define PA_BIND_NOW DL_NOW
55 #else
56 #undef PA_BIND_NOW
57 #endif
58
59 static pa_mutex *libtool_mutex = NULL;
60
61 PA_STATIC_TLS_DECLARE_NO_FREE(libtool_tls);
62
63 static void libtool_lock(void) {
64 pa_mutex_lock(libtool_mutex);
65 }
66
67 static void libtool_unlock(void) {
68 pa_mutex_unlock(libtool_mutex);
69 }
70
71 static void libtool_set_error(const char *error) {
72 PA_STATIC_TLS_SET(libtool_tls, (char*) error);
73 }
74
75 static const char *libtool_get_error(void) {
76 return PA_STATIC_TLS_GET(libtool_tls);
77 }
78
79 #ifdef PA_BIND_NOW
80
81 /*
82 To avoid lazy relocations during runtime in our RT threads we add
83 our own shared object loader with uses RTLD_NOW if it is
84 available. The standard ltdl loader prefers RTLD_LAZY.
85
86 Please note that this loader doesn't have any influence on
87 relocations on any libraries that are already loaded into our
88 process, i.e. because the pulseaudio binary links directly to
89 them. To disable lazy relocations for those libraries it is possible
90 to set $LT_BIND_NOW before starting the pulsaudio binary.
91 */
92
93 #ifndef HAVE_LT_DLADVISE
94 static lt_module bind_now_open(lt_user_data d, const char *fname) {
95 #else
96 static lt_module bind_now_open(lt_user_data d, const char *fname, lt_dladvise advise) {
97 #endif
98 lt_module m;
99
100 pa_assert(fname);
101
102 if (!(m = dlopen(fname, PA_BIND_NOW))) {
103 libtool_set_error(dlerror());
104 return NULL;
105 }
106
107 return m;
108 }
109
110 static int bind_now_close(lt_user_data d, lt_module m) {
111
112 pa_assert(m);
113
114 if (dlclose(m) != 0){
115 libtool_set_error(dlerror());
116 return 1;
117 }
118
119 return 0;
120 }
121
122 static lt_ptr bind_now_find_sym(lt_user_data d, lt_module m, const char *symbol) {
123 lt_ptr ptr;
124
125 pa_assert(m);
126 pa_assert(symbol);
127
128 if (!(ptr = dlsym(m, symbol))) {
129 libtool_set_error(dlerror());
130 return NULL;
131 }
132
133 return ptr;
134 }
135
136 #endif
137
138 void pa_ltdl_init(void) {
139
140 #ifdef PA_BIND_NOW
141 # ifdef HAVE_STRUCT_LT_USER_DLLOADER
142 lt_dlloader *place;
143 static const struct lt_user_dlloader loader = {
144 .module_open = bind_now_open,
145 .module_close = bind_now_close,
146 .find_sym = bind_now_find_sym
147 };
148 # else
149 static const lt_dlvtable *dlopen_loader;
150 static lt_dlvtable bindnow_loader;
151 # endif
152 #endif
153
154 pa_assert_se(lt_dlinit() == 0);
155 pa_assert_se(libtool_mutex = pa_mutex_new(TRUE, FALSE));
156 #ifdef HAVE_LT_DLMUTEX_REGISTER
157 pa_assert_se(lt_dlmutex_register(libtool_lock, libtool_unlock, libtool_set_error, libtool_get_error) == 0);
158 #endif
159
160 #ifdef PA_BIND_NOW
161 # ifdef HAVE_STRUCT_LT_USER_DLLOADER
162
163 if (!(place = lt_dlloader_find("dlopen")))
164 place = lt_dlloader_next(NULL);
165
166 /* Add our BIND_NOW loader as the default module loader. */
167 if (lt_dlloader_add(place, &loader, "bind-now-loader") != 0)
168 pa_log_warn("Failed to add bind-now-loader.");
169 # else
170 /* Already initialised */
171 if ( dlopen_loader != NULL ) return;
172
173 if (!(dlopen_loader = lt_dlloader_find("dlopen"))) {
174 pa_log_warn("Failed to find original dlopen loader.");
175 return;
176 }
177
178 memcpy(&bindnow_loader, dlopen_loader, sizeof(bindnow_loader));
179 bindnow_loader.name = "bind-now-loader";
180 bindnow_loader.module_open = bind_now_open;
181 bindnow_loader.module_close = bind_now_close;
182 bindnow_loader.find_sym = bind_now_find_sym;
183 bindnow_loader.priority = LT_DLLOADER_PREPEND;
184
185 /* Add our BIND_NOW loader as the default module loader. */
186 if (lt_dlloader_add(&bindnow_loader) != 0)
187 pa_log_warn("Failed to add bind-now-loader.");
188 # endif
189 #endif
190 }
191
192 void pa_ltdl_done(void) {
193 pa_assert_se(lt_dlexit() == 0);
194 pa_mutex_free(libtool_mutex);
195 libtool_mutex = NULL;
196 }
197