4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <pulse/timeval.h>
35 #include <pulse/xmalloc.h>
37 #include <pulsecore/core-subscribe.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/core-util.h>
43 #define PA_SYMBOL_INIT "pa__init"
44 #define PA_SYMBOL_DONE "pa__done"
46 #define UNLOAD_POLL_TIME 2
48 /* lt_dlsym() violates ISO C, so confide the breakage into this function to
50 typedef void (*fnptr)(void);
51 static inline fnptr lt_dlsym_fn(lt_dlhandle handle, const char *symbol) {
52 return (fnptr) (long) lt_dlsym(handle, symbol);
55 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
56 pa_core *c = userdata;
58 assert(c && c->mainloop == m && c->module_auto_unload_event == e);
60 pa_module_unload_unused(c);
62 pa_gettimeofday(&ntv);
63 ntv.tv_sec += UNLOAD_POLL_TIME;
64 m->time_restart(e, &ntv);
67 static inline fnptr load_sym(lt_dlhandle handle, const char *module, const char *symbol) {
72 res = lt_dlsym_fn(handle, symbol);
76 /* As the .la files might have been cleansed from the system, we should
77 * try with the ltdl prefix as well. */
79 buflen = strlen(symbol) + strlen(module) + strlen("_LTX_") + 1;
80 buffer = pa_xmalloc(buflen);
83 strcpy(buffer, module);
85 for (ch = buffer;*ch != '\0';ch++) {
90 strcat(buffer, "_LTX_");
91 strcat(buffer, symbol);
93 res = lt_dlsym_fn(handle, buffer);
100 pa_module* pa_module_load(pa_core *c, const char *name, const char *argument) {
106 if (c->disallow_module_loading)
109 m = pa_xmalloc(sizeof(pa_module));
111 m->name = pa_xstrdup(name);
112 m->argument = pa_xstrdup(argument);
114 if (!(m->dl = lt_dlopenext(name))) {
115 pa_log(__FILE__": Failed to open module \"%s\": %s", name, lt_dlerror());
119 if (!(m->init = (int (*)(pa_core *_c, pa_module*_m)) load_sym(m->dl, name, PA_SYMBOL_INIT))) {
120 pa_log(__FILE__": Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT"\" not found.", name);
124 if (!(m->done = (void (*)(pa_core *_c, pa_module*_m)) load_sym(m->dl, name, PA_SYMBOL_DONE))) {
125 pa_log(__FILE__": Failed to load module \"%s\": symbol \""PA_SYMBOL_DONE"\" not found.", name);
133 m->unload_requested = 0;
136 if (m->init(c, m) < 0) {
137 pa_log_error(__FILE__": Failed to load module \"%s\" (argument: \"%s\"): initialization failed.", name, argument ? argument : "");
142 c->modules = pa_idxset_new(NULL, NULL);
144 if (!c->module_auto_unload_event) {
146 pa_gettimeofday(&ntv);
147 ntv.tv_sec += UNLOAD_POLL_TIME;
148 c->module_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
150 assert(c->module_auto_unload_event);
153 r = pa_idxset_put(c->modules, m, &m->index);
154 assert(r >= 0 && m->index != PA_IDXSET_INVALID);
156 pa_log_info(__FILE__": Loaded \"%s\" (index: #%u; argument: \"%s\").", m->name, m->index, m->argument ? m->argument : "");
158 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_NEW, m->index);
165 pa_xfree(m->argument);
177 static void pa_module_free(pa_module *m) {
178 assert(m && m->done && m->core);
180 if (m->core->disallow_module_loading)
183 pa_log_info(__FILE__": Unloading \"%s\" (index: #%u).", m->name, m->index);
189 pa_log_info(__FILE__": Unloaded \"%s\" (index: #%u).", m->name, m->index);
191 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_REMOVE, m->index);
194 pa_xfree(m->argument);
198 void pa_module_unload(pa_core *c, pa_module *m) {
202 if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
208 void pa_module_unload_by_index(pa_core *c, uint32_t idx) {
210 assert(c && idx != PA_IDXSET_INVALID);
213 if (!(m = pa_idxset_remove_by_index(c->modules, idx)))
219 static void free_callback(void *p, PA_GCC_UNUSED void *userdata) {
225 void pa_module_unload_all(pa_core *c) {
232 while ((m = pa_idxset_first(c->modules, NULL)))
233 pa_module_unload(c, m);
235 pa_idxset_free(c->modules, free_callback, NULL);
238 if (c->module_auto_unload_event) {
239 c->mainloop->time_free(c->module_auto_unload_event);
240 c->module_auto_unload_event = NULL;
243 if (c->module_defer_unload_event) {
244 c->mainloop->defer_free(c->module_defer_unload_event);
245 c->module_defer_unload_event = NULL;
249 static int unused_callback(void *p, PA_GCC_UNUSED uint32_t idx, int *del, void *userdata) {
251 time_t *now = userdata;
252 assert(p && del && now);
254 if (m->n_used == 0 && m->auto_unload && m->last_used_time+m->core->module_idle_time <= *now) {
262 void pa_module_unload_unused(pa_core *c) {
270 pa_idxset_foreach(c->modules, unused_callback, &now);
273 static int unload_callback(void *p, PA_GCC_UNUSED uint32_t idx, int *del, PA_GCC_UNUSED void *userdata) {
277 if (m->unload_requested) {
285 static void defer_cb(pa_mainloop_api*api, pa_defer_event *e, void *userdata) {
286 pa_core *core = userdata;
287 api->defer_enable(e, 0);
292 pa_idxset_foreach(core->modules, unload_callback, NULL);
296 void pa_module_unload_request(pa_module *m) {
299 m->unload_requested = 1;
301 if (!m->core->module_defer_unload_event)
302 m->core->module_defer_unload_event = m->core->mainloop->defer_new(m->core->mainloop, defer_cb, m->core);
304 m->core->mainloop->defer_enable(m->core->module_defer_unload_event, 1);
307 void pa_module_set_used(pa_module*m, int used) {
310 if (m->n_used != used)
311 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_CHANGE, m->index);
313 if (m->n_used != used && used == 0)
314 time(&m->last_used_time);
319 pa_modinfo *pa_module_get_info(pa_module *m) {
322 return pa_modinfo_get_by_handle(m->dl);