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