]> code.delx.au - pulseaudio/blob - src/pulsecore/module.c
Merge branch 'master' of git://git.0pointer.de/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 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
49 #define UNLOAD_POLL_TIME 2
50
51 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
52 pa_core *c = PA_CORE(userdata);
53 struct timeval ntv;
54
55 pa_core_assert_ref(c);
56 pa_assert(c->mainloop == m);
57 pa_assert(c->module_auto_unload_event == e);
58
59 pa_module_unload_unused(c);
60
61 pa_gettimeofday(&ntv);
62 pa_timeval_add(&ntv, UNLOAD_POLL_TIME*1000000);
63 m->time_restart(e, &ntv);
64 }
65
66 pa_module* pa_module_load(pa_core *c, const char *name, const char *argument) {
67 pa_module *m = NULL;
68 pa_bool_t (*load_once)(void);
69
70 pa_assert(c);
71 pa_assert(name);
72
73 if (c->disallow_module_loading)
74 goto fail;
75
76 m = pa_xnew(pa_module, 1);
77 m->name = pa_xstrdup(name);
78 m->argument = pa_xstrdup(argument);
79 m->load_once = FALSE;
80
81 if (!(m->dl = lt_dlopenext(name))) {
82 pa_log("Failed to open module \"%s\": %s", name, lt_dlerror());
83 goto fail;
84 }
85
86 if ((load_once = (pa_bool_t (*)(void)) pa_load_sym(m->dl, name, PA_SYMBOL_LOAD_ONCE))) {
87
88 m->load_once = load_once();
89
90 if (m->load_once && c->modules) {
91 pa_module *i;
92 uint32_t idx;
93 /* OK, the module only wants to be loaded once, let's make sure it is */
94
95 for (i = pa_idxset_first(c->modules, &idx); i; i = pa_idxset_next(c->modules, &idx)) {
96 if (strcmp(name, i->name) == 0) {
97 pa_log("Module \"%s\" should be loaded once at most. Refusing to load.", name);
98 goto fail;
99 }
100 }
101 }
102 }
103
104 if (!(m->init = (int (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_INIT))) {
105 pa_log("Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT"\" not found.", name);
106 goto fail;
107 }
108
109 m->done = (void (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_DONE);
110 m->userdata = NULL;
111 m->core = c;
112 m->n_used = -1;
113 m->auto_unload = FALSE;
114 m->unload_requested = FALSE;
115
116 if (m->init(m) < 0) {
117 pa_log_error("Failed to load module \"%s\" (argument: \"%s\"): initialization failed.", name, argument ? argument : "");
118 goto fail;
119 }
120
121 if (!c->modules)
122 c->modules = pa_idxset_new(NULL, NULL);
123
124 if (m->auto_unload && !c->module_auto_unload_event) {
125 struct timeval ntv;
126 pa_gettimeofday(&ntv);
127 pa_timeval_add(&ntv, UNLOAD_POLL_TIME*1000000);
128 c->module_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
129 }
130
131 pa_assert_se(pa_idxset_put(c->modules, m, &m->index) >= 0);
132 pa_assert(m->index != PA_IDXSET_INVALID);
133
134 pa_log_info("Loaded \"%s\" (index: #%u; argument: \"%s\").", m->name, m->index, m->argument ? m->argument : "");
135
136 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_NEW, m->index);
137
138 return m;
139
140 fail:
141
142 if (m) {
143 pa_xfree(m->argument);
144 pa_xfree(m->name);
145
146 if (m->dl)
147 lt_dlclose(m->dl);
148
149 pa_xfree(m);
150 }
151
152 return NULL;
153 }
154
155 static void pa_module_free(pa_module *m) {
156 pa_assert(m);
157 pa_assert(m->core);
158
159 if (m->core->disallow_module_loading)
160 return;
161
162 pa_log_info("Unloading \"%s\" (index: #%u).", m->name, m->index);
163
164 if (m->done)
165 m->done(m);
166
167 lt_dlclose(m->dl);
168
169 pa_log_info("Unloaded \"%s\" (index: #%u).", m->name, m->index);
170
171 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_REMOVE, m->index);
172
173 pa_xfree(m->name);
174 pa_xfree(m->argument);
175 pa_xfree(m);
176 }
177
178 void pa_module_unload(pa_core *c, pa_module *m) {
179 pa_assert(c);
180 pa_assert(m);
181
182 pa_assert(c->modules);
183 if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
184 return;
185
186 pa_module_free(m);
187 }
188
189 void pa_module_unload_by_index(pa_core *c, uint32_t idx) {
190 pa_module *m;
191 pa_assert(c);
192 pa_assert(idx != PA_IDXSET_INVALID);
193
194 if (!(m = pa_idxset_remove_by_index(c->modules, idx)))
195 return;
196
197 pa_module_free(m);
198 }
199
200 void pa_module_unload_all(pa_core *c) {
201
202 pa_assert(c);
203
204 if (c->modules) {
205 pa_module *m;
206
207 while ((m = pa_idxset_steal_first(c->modules, NULL)))
208 pa_module_free(m);
209
210 pa_idxset_free(c->modules, NULL, NULL);
211 c->modules = NULL;
212 }
213
214 if (c->module_auto_unload_event) {
215 c->mainloop->time_free(c->module_auto_unload_event);
216 c->module_auto_unload_event = NULL;
217 }
218
219 if (c->module_defer_unload_event) {
220 c->mainloop->defer_free(c->module_defer_unload_event);
221 c->module_defer_unload_event = NULL;
222 }
223 }
224
225 void pa_module_unload_unused(pa_core *c) {
226 void *state = NULL;
227 time_t now;
228 pa_module *m;
229
230 pa_assert(c);
231
232 if (!c->modules)
233 return;
234
235 time(&now);
236
237 while ((m = pa_idxset_iterate(c->modules, &state, NULL))) {
238
239 if (m->n_used > 0)
240 continue;
241
242 if (!m->auto_unload)
243 continue;
244
245 if (m->last_used_time + m->core->module_idle_time > now)
246 continue;
247
248 pa_module_unload(c, m);
249 }
250 }
251
252 static void defer_cb(pa_mainloop_api*api, pa_defer_event *e, void *userdata) {
253 void *state = NULL;
254 pa_core *c = PA_CORE(userdata);
255 pa_module *m;
256
257 pa_core_assert_ref(c);
258 api->defer_enable(e, 0);
259
260 if (!c->modules)
261 return;
262
263 while ((m = pa_idxset_iterate(c->modules, &state, NULL)))
264 if (m->unload_requested)
265 pa_module_unload(c, m);
266 }
267
268 void pa_module_unload_request(pa_module *m) {
269 pa_assert(m);
270
271 m->unload_requested = TRUE;
272
273 if (!m->core->module_defer_unload_event)
274 m->core->module_defer_unload_event = m->core->mainloop->defer_new(m->core->mainloop, defer_cb, m->core);
275
276 m->core->mainloop->defer_enable(m->core->module_defer_unload_event, 1);
277 }
278
279 void pa_module_set_used(pa_module*m, int used) {
280 pa_assert(m);
281
282 if (m->n_used != used)
283 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_CHANGE, m->index);
284
285 if (used == 0 && m->n_used > 0)
286 time(&m->last_used_time);
287
288 m->n_used = used;
289 }
290
291 pa_modinfo *pa_module_get_info(pa_module *m) {
292 pa_assert(m);
293
294 return pa_modinfo_get_by_handle(m->dl, m->name);
295 }