l10n: Updates to Russian (ru) translation
[pulseaudio-mirror.git] / src / daemon / ltdl-bind-now.c
blob276b2a06eb1eec4000fe75809edc47ec440ea46a
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2008 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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 ***/
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #ifdef HAVE_DLFCN_H
28 #include <dlfcn.h>
29 #endif
31 #ifdef HAVE_SYS_DL_H
32 #include <sys/dl.h>
33 #endif
35 #include <string.h>
37 #include <ltdl.h>
39 #include <pulse/i18n.h>
41 #include <pulsecore/macro.h>
42 #include <pulsecore/log.h>
44 #include "ltdl-bind-now.h"
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
54 #ifdef PA_BIND_NOW
57 To avoid lazy relocations during runtime in our RT threads we add
58 our own shared object loader with uses RTLD_NOW if it is
59 available. The standard ltdl loader prefers RTLD_LAZY.
61 Please note that this loader doesn't have any influence on
62 relocations on any libraries that are already loaded into our
63 process, i.e. because the pulseaudio binary links directly to
64 them. To disable lazy relocations for those libraries it is possible
65 to set $LT_BIND_NOW before starting the pulsaudio binary.
68 static lt_module bind_now_open(lt_user_data d, const char *fname, lt_dladvise advise) {
69 lt_module m;
71 pa_assert(fname);
73 if (!(m = dlopen(fname, PA_BIND_NOW))) {
74 lt_dlseterror(LT_ERROR_CANNOT_OPEN);
75 return NULL;
78 return m;
81 static int bind_now_close(lt_user_data d, lt_module m) {
83 pa_assert(m);
85 if (dlclose(m) != 0){
86 lt_dlseterror(LT_ERROR_CANNOT_CLOSE);
87 return 1;
90 return 0;
93 static lt_ptr bind_now_find_sym(lt_user_data d, lt_module m, const char *symbol) {
94 lt_ptr ptr;
96 pa_assert(m);
97 pa_assert(symbol);
99 if (!(ptr = dlsym(m, symbol))) {
100 lt_dlseterror(LT_ERROR_SYMBOL_NOT_FOUND);
101 return NULL;
104 return ptr;
107 static lt_dlvtable *bindnow_loader = NULL;
108 #endif
110 void pa_ltdl_init(void) {
112 #ifdef PA_BIND_NOW
113 const lt_dlvtable *dlopen_loader;
114 #endif
116 pa_assert_se(lt_dlinit() == 0);
118 #ifdef PA_BIND_NOW
119 /* Already initialised */
120 if (bindnow_loader)
121 return;
123 if (!(dlopen_loader = lt_dlloader_find((char*) "lt_dlopen"))) {
124 pa_log_warn(_("Failed to find original lt_dlopen loader."));
125 return;
128 if (!(bindnow_loader = malloc(sizeof(lt_dlvtable)))) {
129 pa_log_error(_("Failed to allocate new dl loader."));
130 return;
133 memcpy(bindnow_loader, dlopen_loader, sizeof(*bindnow_loader));
134 bindnow_loader->name = "bind-now-loader";
135 bindnow_loader->module_open = bind_now_open;
136 bindnow_loader->module_close = bind_now_close;
137 bindnow_loader->find_sym = bind_now_find_sym;
138 bindnow_loader->priority = LT_DLLOADER_PREPEND;
140 /* Add our BIND_NOW loader as the default module loader. */
141 if (lt_dlloader_add(bindnow_loader) != 0) {
142 pa_log_warn(_("Failed to add bind-now-loader."));
143 free(bindnow_loader);
144 bindnow_loader = NULL;
146 #endif
149 void pa_ltdl_done(void) {
150 pa_assert_se(lt_dlexit() == 0);
152 #ifdef PA_BIND_NOW
153 /* lt_dlexit() will free our loader vtable, hence reset our
154 * pointer to it here */
155 bindnow_loader = NULL;
156 #endif