]> code.delx.au - pulseaudio/blob - src/pulsecore/module.c
Merge branch 'master' of ssh://rootserver/home/lennart/git/public/pulseaudio
[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 <limits.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <ctype.h>
33
34 #include <pulse/timeval.h>
35 #include <pulse/xmalloc.h>
36 #include <pulse/proplist.h>
37
38 #include <pulsecore/core-subscribe.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/macro.h>
42 #include <pulsecore/ltdl-helper.h>
43 #include <pulsecore/modinfo.h>
44
45 #include "module.h"
46
47 #define PA_SYMBOL_INIT "pa__init"
48 #define PA_SYMBOL_DONE "pa__done"
49 #define PA_SYMBOL_LOAD_ONCE "pa__load_once"
50 #define PA_SYMBOL_GET_N_USED "pa__get_n_used"
51 #define PA_SYMBOL_GET_DEPRECATE "pa__get_deprecated"
52
53 pa_module* pa_module_load(pa_core *c, const char *name, const char *argument) {
54 pa_module *m = NULL;
55 pa_bool_t (*load_once)(void);
56 const char* (*get_deprecated)(void);
57 pa_modinfo *mi;
58
59 pa_assert(c);
60 pa_assert(name);
61
62 if (c->disallow_module_loading)
63 goto fail;
64
65 m = pa_xnew(pa_module, 1);
66 m->name = pa_xstrdup(name);
67 m->argument = pa_xstrdup(argument);
68 m->load_once = FALSE;
69 m->proplist = pa_proplist_new();
70
71 if (!(m->dl = lt_dlopenext(name))) {
72 pa_log("Failed to open module \"%s\": %s", name, lt_dlerror());
73 goto fail;
74 }
75
76 if ((load_once = (pa_bool_t (*)(void)) pa_load_sym(m->dl, name, PA_SYMBOL_LOAD_ONCE))) {
77
78 m->load_once = load_once();
79
80 if (m->load_once) {
81 pa_module *i;
82 uint32_t idx;
83 /* OK, the module only wants to be loaded once, let's make sure it is */
84
85 for (i = pa_idxset_first(c->modules, &idx); i; i = pa_idxset_next(c->modules, &idx)) {
86 if (strcmp(name, i->name) == 0) {
87 pa_log("Module \"%s\" should be loaded once at most. Refusing to load.", name);
88 goto fail;
89 }
90 }
91 }
92 }
93
94 if ((get_deprecated = (const char* (*) (void)) pa_load_sym(m->dl, name, PA_SYMBOL_GET_DEPRECATE))) {
95 const char *t;
96
97 if ((t = get_deprecated()))
98 pa_log_warn("%s is deprecated: %s", name, t);
99 }
100
101 if (!(m->init = (int (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_INIT))) {
102 pa_log("Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT"\" not found.", name);
103 goto fail;
104 }
105
106 m->done = (void (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_DONE);
107 m->get_n_used = (int (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_GET_N_USED);
108 m->userdata = NULL;
109 m->core = c;
110 m->unload_requested = FALSE;
111
112 if (m->init(m) < 0) {
113 pa_log_error("Failed to load module \"%s\" (argument: \"%s\"): initialization failed.", name, argument ? argument : "");
114 goto fail;
115 }
116
117 pa_assert_se(pa_idxset_put(c->modules, m, &m->index) >= 0);
118 pa_assert(m->index != PA_IDXSET_INVALID);
119
120 pa_log_info("Loaded \"%s\" (index: #%u; argument: \"%s\").", m->name, m->index, m->argument ? m->argument : "");
121
122 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_NEW, m->index);
123
124 if ((mi = pa_modinfo_get_by_handle(m->dl, name))) {
125
126 if (mi->author && !pa_proplist_contains(m->proplist, PA_PROP_MODULE_AUTHOR))
127 pa_proplist_sets(m->proplist, PA_PROP_MODULE_AUTHOR, mi->author);
128
129 if (mi->description && !pa_proplist_contains(m->proplist, PA_PROP_MODULE_DESCRIPTION))
130 pa_proplist_sets(m->proplist, PA_PROP_MODULE_DESCRIPTION, mi->description);
131
132 if (mi->version && !pa_proplist_contains(m->proplist, PA_PROP_MODULE_VERSION))
133 pa_proplist_sets(m->proplist, PA_PROP_MODULE_VERSION, mi->version);
134
135 pa_modinfo_free(mi);
136 }
137
138 return m;
139
140 fail:
141
142 if (m) {
143 if (m->proplist)
144 pa_proplist_free(m->proplist);
145
146 pa_xfree(m->argument);
147 pa_xfree(m->name);
148
149 if (m->dl)
150 lt_dlclose(m->dl);
151
152 pa_xfree(m);
153 }
154
155 return NULL;
156 }
157
158 static void pa_module_free(pa_module *m) {
159 pa_assert(m);
160 pa_assert(m->core);
161
162 pa_log_info("Unloading \"%s\" (index: #%u).", m->name, m->index);
163
164 if (m->done)
165 m->done(m);
166
167 if (m->proplist)
168 pa_proplist_free(m->proplist);
169
170 lt_dlclose(m->dl);
171
172 pa_log_info("Unloaded \"%s\" (index: #%u).", m->name, m->index);
173
174 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_REMOVE, m->index);
175
176 pa_xfree(m->name);
177 pa_xfree(m->argument);
178 pa_xfree(m);
179 }
180
181 void pa_module_unload(pa_core *c, pa_module *m, pa_bool_t force) {
182 pa_assert(c);
183 pa_assert(m);
184
185 if (m->core->disallow_module_loading && !force)
186 return;
187
188 if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
189 return;
190
191 pa_module_free(m);
192 }
193
194 void pa_module_unload_by_index(pa_core *c, uint32_t idx, pa_bool_t force) {
195 pa_module *m;
196 pa_assert(c);
197 pa_assert(idx != PA_IDXSET_INVALID);
198
199 if (c->disallow_module_loading && !force)
200 return;
201
202 if (!(m = pa_idxset_remove_by_index(c->modules, idx)))
203 return;
204
205 pa_module_free(m);
206 }
207
208 void pa_module_unload_all(pa_core *c) {
209 pa_module *m;
210 pa_assert(c);
211
212 while ((m = pa_idxset_steal_first(c->modules, NULL)))
213 pa_module_free(m);
214
215 if (c->module_defer_unload_event) {
216 c->mainloop->defer_free(c->module_defer_unload_event);
217 c->module_defer_unload_event = NULL;
218 }
219 }
220
221 static void defer_cb(pa_mainloop_api*api, pa_defer_event *e, void *userdata) {
222 void *state = NULL;
223 pa_core *c = PA_CORE(userdata);
224 pa_module *m;
225
226 pa_core_assert_ref(c);
227 api->defer_enable(e, 0);
228
229 while ((m = pa_idxset_iterate(c->modules, &state, NULL)))
230 if (m->unload_requested)
231 pa_module_unload(c, m, TRUE);
232 }
233
234 void pa_module_unload_request(pa_module *m, pa_bool_t force) {
235 pa_assert(m);
236
237 if (m->core->disallow_module_loading && !force)
238 return;
239
240 m->unload_requested = TRUE;
241
242 if (!m->core->module_defer_unload_event)
243 m->core->module_defer_unload_event = m->core->mainloop->defer_new(m->core->mainloop, defer_cb, m->core);
244
245 m->core->mainloop->defer_enable(m->core->module_defer_unload_event, 1);
246 }
247
248 void pa_module_unload_request_by_index(pa_core *c, uint32_t idx, pa_bool_t force) {
249 pa_module *m;
250 pa_assert(c);
251
252 if (!(m = pa_idxset_get_by_index(c->modules, idx)))
253 return;
254
255 pa_module_unload_request(m, force);
256 }
257
258 int pa_module_get_n_used(pa_module*m) {
259 pa_assert(m);
260
261 if (!m->get_n_used)
262 return -1;
263
264 return m->get_n_used(m);
265 }