]> code.delx.au - pulseaudio/blob - src/pulsecore/module.c
16582b32946bd1d709a1f9849806527a019aedf3
[pulseaudio] / src / pulsecore / module.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 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 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31
32 #include <pulse/xmalloc.h>
33 #include <pulse/proplist.h>
34
35 #include <pulsecore/core-subscribe.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/macro.h>
39 #include <pulsecore/ltdl-helper.h>
40 #include <pulsecore/modinfo.h>
41
42 #include "module.h"
43
44 #define PA_SYMBOL_INIT "pa__init"
45 #define PA_SYMBOL_DONE "pa__done"
46 #define PA_SYMBOL_LOAD_ONCE "pa__load_once"
47 #define PA_SYMBOL_GET_N_USED "pa__get_n_used"
48 #define PA_SYMBOL_GET_DEPRECATE "pa__get_deprecated"
49
50 pa_module* pa_module_load(pa_core *c, const char *name, const char *argument) {
51 pa_module *m = NULL;
52 bool (*load_once)(void);
53 const char* (*get_deprecated)(void);
54 pa_modinfo *mi;
55
56 pa_assert(c);
57 pa_assert(name);
58
59 if (c->disallow_module_loading)
60 goto fail;
61
62 m = pa_xnew(pa_module, 1);
63 m->name = pa_xstrdup(name);
64 m->argument = pa_xstrdup(argument);
65 m->load_once = false;
66 m->proplist = pa_proplist_new();
67 m->index = PA_IDXSET_INVALID;
68
69 if (!(m->dl = lt_dlopenext(name))) {
70 /* We used to print the error that is returned by lt_dlerror(), but
71 * lt_dlerror() is useless. It returns pretty much always "file not
72 * found". That's because if there are any problems with loading the
73 * module with normal loaders, libltdl falls back to the "preload"
74 * loader, which never finds anything, and therefore says "file not
75 * found". */
76 pa_log("Failed to open module \"%s\".", name);
77 goto fail;
78 }
79
80 if ((load_once = (bool (*)(void)) pa_load_sym(m->dl, name, PA_SYMBOL_LOAD_ONCE))) {
81
82 m->load_once = load_once();
83
84 if (m->load_once) {
85 pa_module *i;
86 uint32_t idx;
87 /* OK, the module only wants to be loaded once, let's make sure it is */
88
89 PA_IDXSET_FOREACH(i, c->modules, idx) {
90 if (pa_streq(name, i->name)) {
91 pa_log("Module \"%s\" should be loaded once at most. Refusing to load.", name);
92 goto fail;
93 }
94 }
95 }
96 }
97
98 if ((get_deprecated = (const char* (*) (void)) pa_load_sym(m->dl, name, PA_SYMBOL_GET_DEPRECATE))) {
99 const char *t;
100
101 if ((t = get_deprecated()))
102 pa_log_warn("%s is deprecated: %s", name, t);
103 }
104
105 if (!(m->init = (int (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_INIT))) {
106 pa_log("Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT"\" not found.", name);
107 goto fail;
108 }
109
110 m->done = (void (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_DONE);
111 m->get_n_used = (int (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_GET_N_USED);
112 m->userdata = NULL;
113 m->core = c;
114 m->unload_requested = false;
115
116 pa_assert_se(pa_idxset_put(c->modules, m, &m->index) >= 0);
117 pa_assert(m->index != PA_IDXSET_INVALID);
118
119 if (m->init(m) < 0) {
120 pa_log_error("Failed to load module \"%s\" (argument: \"%s\"): initialization failed.", name, argument ? argument : "");
121 goto fail;
122 }
123
124 pa_log_info("Loaded \"%s\" (index: #%u; argument: \"%s\").", m->name, m->index, m->argument ? m->argument : "");
125
126 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_NEW, m->index);
127
128 if ((mi = pa_modinfo_get_by_handle(m->dl, name))) {
129
130 if (mi->author && !pa_proplist_contains(m->proplist, PA_PROP_MODULE_AUTHOR))
131 pa_proplist_sets(m->proplist, PA_PROP_MODULE_AUTHOR, mi->author);
132
133 if (mi->description && !pa_proplist_contains(m->proplist, PA_PROP_MODULE_DESCRIPTION))
134 pa_proplist_sets(m->proplist, PA_PROP_MODULE_DESCRIPTION, mi->description);
135
136 if (mi->version && !pa_proplist_contains(m->proplist, PA_PROP_MODULE_VERSION))
137 pa_proplist_sets(m->proplist, PA_PROP_MODULE_VERSION, mi->version);
138
139 pa_modinfo_free(mi);
140 }
141
142 return m;
143
144 fail:
145
146 if (m) {
147 if (m->index != PA_IDXSET_INVALID)
148 pa_idxset_remove_by_index(c->modules, m->index);
149
150 if (m->proplist)
151 pa_proplist_free(m->proplist);
152
153 pa_xfree(m->argument);
154 pa_xfree(m->name);
155
156 if (m->dl)
157 lt_dlclose(m->dl);
158
159 pa_xfree(m);
160 }
161
162 return NULL;
163 }
164
165 static void pa_module_free(pa_module *m) {
166 pa_assert(m);
167 pa_assert(m->core);
168
169 pa_log_info("Unloading \"%s\" (index: #%u).", m->name, m->index);
170
171 if (m->done)
172 m->done(m);
173
174 if (m->proplist)
175 pa_proplist_free(m->proplist);
176
177 lt_dlclose(m->dl);
178
179 pa_log_info("Unloaded \"%s\" (index: #%u).", m->name, m->index);
180
181 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_REMOVE, m->index);
182
183 pa_xfree(m->name);
184 pa_xfree(m->argument);
185 pa_xfree(m);
186 }
187
188 void pa_module_unload(pa_core *c, pa_module *m, bool force) {
189 pa_assert(c);
190 pa_assert(m);
191
192 if (m->core->disallow_module_loading && !force)
193 return;
194
195 if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
196 return;
197
198 pa_module_free(m);
199 }
200
201 void pa_module_unload_by_index(pa_core *c, uint32_t idx, bool force) {
202 pa_module *m;
203 pa_assert(c);
204 pa_assert(idx != PA_IDXSET_INVALID);
205
206 if (c->disallow_module_loading && !force)
207 return;
208
209 if (!(m = pa_idxset_remove_by_index(c->modules, idx)))
210 return;
211
212 pa_module_free(m);
213 }
214
215 void pa_module_unload_all(pa_core *c) {
216 pa_module *m;
217 uint32_t *indices;
218 uint32_t state;
219 int i;
220
221 pa_assert(c);
222 pa_assert(c->modules);
223
224 if (pa_idxset_isempty(c->modules))
225 return;
226
227 /* Unload modules in reverse order by default */
228 indices = pa_xnew(uint32_t, pa_idxset_size(c->modules));
229 i = 0;
230 PA_IDXSET_FOREACH(m, c->modules, state)
231 indices[i++] = state;
232 pa_assert(i == (int) pa_idxset_size(c->modules));
233 i--;
234 for (; i >= 0; i--) {
235 m = pa_idxset_remove_by_index(c->modules, indices[i]);
236 if (m)
237 pa_module_free(m);
238 }
239 pa_xfree(indices);
240
241 /* Just in case module unloading caused more modules to load */
242 pa_idxset_remove_all(c->modules, (pa_free_cb_t) pa_module_free);
243
244 if (c->module_defer_unload_event) {
245 c->mainloop->defer_free(c->module_defer_unload_event);
246 c->module_defer_unload_event = NULL;
247 }
248 }
249
250 static void defer_cb(pa_mainloop_api*api, pa_defer_event *e, void *userdata) {
251 void *state = NULL;
252 pa_core *c = PA_CORE(userdata);
253 pa_module *m;
254
255 pa_core_assert_ref(c);
256 api->defer_enable(e, 0);
257
258 while ((m = pa_idxset_iterate(c->modules, &state, NULL)))
259 if (m->unload_requested)
260 pa_module_unload(c, m, true);
261 }
262
263 void pa_module_unload_request(pa_module *m, bool force) {
264 pa_assert(m);
265
266 if (m->core->disallow_module_loading && !force)
267 return;
268
269 m->unload_requested = true;
270
271 if (!m->core->module_defer_unload_event)
272 m->core->module_defer_unload_event = m->core->mainloop->defer_new(m->core->mainloop, defer_cb, m->core);
273
274 m->core->mainloop->defer_enable(m->core->module_defer_unload_event, 1);
275 }
276
277 void pa_module_unload_request_by_index(pa_core *c, uint32_t idx, bool force) {
278 pa_module *m;
279 pa_assert(c);
280
281 if (!(m = pa_idxset_get_by_index(c->modules, idx)))
282 return;
283
284 pa_module_unload_request(m, force);
285 }
286
287 int pa_module_get_n_used(pa_module*m) {
288 pa_assert(m);
289
290 if (!m->get_n_used)
291 return -1;
292
293 return m->get_n_used(m);
294 }
295
296 void pa_module_update_proplist(pa_module *m, pa_update_mode_t mode, pa_proplist *p) {
297 pa_assert(m);
298
299 if (p)
300 pa_proplist_update(m->proplist, mode, p);
301
302 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_CHANGE, m->index);
303 }