4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
37 #include <pulse/timeval.h>
38 #include <pulse/xmalloc.h>
40 #include <pulsecore/core-subscribe.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/core-util.h>
46 #define PA_SYMBOL_INIT "pa__init"
47 #define PA_SYMBOL_DONE "pa__done"
49 #define UNLOAD_POLL_TIME 2
51 /* lt_dlsym() violates ISO C, so confide the breakage into this function to
53 typedef void (*fnptr)(void);
54 static inline fnptr lt_dlsym_fn(lt_dlhandle handle, const char *symbol) {
55 return (fnptr) (long) lt_dlsym(handle, symbol);
58 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
59 pa_core *c = userdata;
61 assert(c && c->mainloop == m && c->module_auto_unload_event == e);
63 pa_module_unload_unused(c);
65 pa_gettimeofday(&ntv);
66 ntv.tv_sec += UNLOAD_POLL_TIME;
67 m->time_restart(e, &ntv);
70 static inline fnptr load_sym(lt_dlhandle handle, const char *module, const char *symbol) {
75 res = lt_dlsym_fn(handle, symbol);
79 /* As the .la files might have been cleansed from the system, we should
80 * try with the ltdl prefix as well. */
82 buflen = strlen(symbol) + strlen(module) + strlen("_LTX_") + 1;
83 buffer = pa_xmalloc(buflen);
86 strcpy(buffer, module);
88 for (ch = buffer;*ch != '\0';ch++) {
93 strcat(buffer, "_LTX_");
94 strcat(buffer, symbol);
96 res = lt_dlsym_fn(handle, buffer);
103 pa_module* pa_module_load(pa_core *c, const char *name, const char *argument) {
109 if (c->disallow_module_loading)
112 m = pa_xmalloc(sizeof(pa_module));
114 m->name = pa_xstrdup(name);
115 m->argument = pa_xstrdup(argument);
117 if (!(m->dl = lt_dlopenext(name))) {
118 pa_log("Failed to open module \"%s\": %s", name, lt_dlerror());
122 if (!(m->init = (int (*)(pa_core *_c, pa_module*_m)) load_sym(m->dl, name, PA_SYMBOL_INIT))) {
123 pa_log("Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT"\" not found.", name);
127 if (!(m->done = (void (*)(pa_core *_c, pa_module*_m)) load_sym(m->dl, name, PA_SYMBOL_DONE))) {
128 pa_log("Failed to load module \"%s\": symbol \""PA_SYMBOL_DONE"\" not found.", name);
136 m->unload_requested = 0;
139 if (m->init(c, m) < 0) {
140 pa_log_error("Failed to load module \"%s\" (argument: \"%s\"): initialization failed.", name, argument ? argument : "");
145 c->modules = pa_idxset_new(NULL, NULL);
147 if (!c->module_auto_unload_event) {
149 pa_gettimeofday(&ntv);
150 ntv.tv_sec += UNLOAD_POLL_TIME;
151 c->module_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
153 assert(c->module_auto_unload_event);
156 r = pa_idxset_put(c->modules, m, &m->index);
157 assert(r >= 0 && m->index != PA_IDXSET_INVALID);
159 pa_log_info("Loaded \"%s\" (index: #%u; argument: \"%s\").", m->name, m->index, m->argument ? m->argument : "");
161 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_NEW, m->index);
168 pa_xfree(m->argument);
180 static void pa_module_free(pa_module *m) {
181 assert(m && m->done && m->core);
183 if (m->core->disallow_module_loading)
186 pa_log_info("Unloading \"%s\" (index: #%u).", m->name, m->index);
192 pa_log_info("Unloaded \"%s\" (index: #%u).", m->name, m->index);
194 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_REMOVE, m->index);
197 pa_xfree(m->argument);
201 void pa_module_unload(pa_core *c, pa_module *m) {
205 if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
211 void pa_module_unload_by_index(pa_core *c, uint32_t idx) {
213 assert(c && idx != PA_IDXSET_INVALID);
216 if (!(m = pa_idxset_remove_by_index(c->modules, idx)))
222 static void free_callback(void *p, PA_GCC_UNUSED void *userdata) {
228 void pa_module_unload_all(pa_core *c) {
235 while ((m = pa_idxset_first(c->modules, NULL)))
236 pa_module_unload(c, m);
238 pa_idxset_free(c->modules, free_callback, NULL);
241 if (c->module_auto_unload_event) {
242 c->mainloop->time_free(c->module_auto_unload_event);
243 c->module_auto_unload_event = NULL;
246 if (c->module_defer_unload_event) {
247 c->mainloop->defer_free(c->module_defer_unload_event);
248 c->module_defer_unload_event = NULL;
252 static int unused_callback(void *p, PA_GCC_UNUSED uint32_t idx, int *del, void *userdata) {
254 time_t *now = userdata;
255 assert(p && del && now);
257 if (m->n_used == 0 && m->auto_unload && m->last_used_time+m->core->module_idle_time <= *now) {
265 void pa_module_unload_unused(pa_core *c) {
273 pa_idxset_foreach(c->modules, unused_callback, &now);
276 static int unload_callback(void *p, PA_GCC_UNUSED uint32_t idx, int *del, PA_GCC_UNUSED void *userdata) {
280 if (m->unload_requested) {
288 static void defer_cb(pa_mainloop_api*api, pa_defer_event *e, void *userdata) {
289 pa_core *core = userdata;
290 api->defer_enable(e, 0);
295 pa_idxset_foreach(core->modules, unload_callback, NULL);
299 void pa_module_unload_request(pa_module *m) {
302 m->unload_requested = 1;
304 if (!m->core->module_defer_unload_event)
305 m->core->module_defer_unload_event = m->core->mainloop->defer_new(m->core->mainloop, defer_cb, m->core);
307 m->core->mainloop->defer_enable(m->core->module_defer_unload_event, 1);
310 void pa_module_set_used(pa_module*m, int used) {
313 if (m->n_used != used)
314 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_CHANGE, m->index);
316 if (m->n_used != used && used == 0)
317 time(&m->last_used_time);
322 pa_modinfo *pa_module_get_info(pa_module *m) {
325 return pa_modinfo_get_by_handle(m->dl);