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