2004-07-16 19:56:36 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2004-07-16 19:56:36 +00:00
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-07-16 19:56:36 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-07-16 19:56:36 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-06-11 17:18:40 +00:00
|
|
|
#include <limits.h>
|
|
|
|
|
#include <stdio.h>
|
2004-06-08 23:54:24 +00:00
|
|
|
#include <stdlib.h>
|
2004-06-10 23:22:16 +00:00
|
|
|
#include <string.h>
|
2004-06-11 21:30:16 +00:00
|
|
|
#include <errno.h>
|
2006-06-01 13:49:10 +00:00
|
|
|
#include <ctype.h>
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/timeval.h>
|
|
|
|
|
#include <pulse/xmalloc.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/core-subscribe.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
|
|
|
|
#include <pulsecore/core-util.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/macro.h>
|
|
|
|
|
#include <pulsecore/ltdl-helper.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
#include "module.h"
|
2004-08-04 16:39:30 +00:00
|
|
|
|
2004-09-11 23:17:38 +00:00
|
|
|
#define PA_SYMBOL_INIT "pa__init"
|
|
|
|
|
#define PA_SYMBOL_DONE "pa__done"
|
2007-11-09 18:25:40 +00:00
|
|
|
#define PA_SYMBOL_LOAD_ONCE "pa__load_once"
|
2004-09-11 23:17:38 +00:00
|
|
|
|
2004-09-13 23:28:30 +00:00
|
|
|
#define UNLOAD_POLL_TIME 2
|
2004-08-04 16:39:30 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_core *c = PA_CORE(userdata);
|
2004-08-04 16:39:30 +00:00
|
|
|
struct timeval ntv;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_core_assert_ref(c);
|
|
|
|
|
pa_assert(c->mainloop == m);
|
|
|
|
|
pa_assert(c->module_auto_unload_event == e);
|
2004-08-04 16:39:30 +00:00
|
|
|
|
|
|
|
|
pa_module_unload_unused(c);
|
|
|
|
|
|
2006-01-10 17:51:06 +00:00
|
|
|
pa_gettimeofday(&ntv);
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_timeval_add(&ntv, UNLOAD_POLL_TIME*1000000);
|
2004-08-05 19:53:57 +00:00
|
|
|
m->time_restart(e, &ntv);
|
2004-08-04 16:39:30 +00:00
|
|
|
}
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_module* pa_module_load(pa_core *c, const char *name, const char *argument) {
|
|
|
|
|
pa_module *m = NULL;
|
2007-11-09 18:25:40 +00:00
|
|
|
pa_bool_t (*load_once)(void);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
|
|
|
|
pa_assert(name);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-09-03 22:44:55 +00:00
|
|
|
if (c->disallow_module_loading)
|
|
|
|
|
goto fail;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
m = pa_xnew(pa_module, 1);
|
2004-08-04 16:39:30 +00:00
|
|
|
m->name = pa_xstrdup(name);
|
|
|
|
|
m->argument = pa_xstrdup(argument);
|
2008-08-03 18:55:42 +02:00
|
|
|
m->load_once = FALSE;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-11 23:17:38 +00:00
|
|
|
if (!(m->dl = lt_dlopenext(name))) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("Failed to open module \"%s\": %s", name, lt_dlerror());
|
2004-06-08 23:54:24 +00:00
|
|
|
goto fail;
|
2004-09-11 23:17:38 +00:00
|
|
|
}
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2007-11-09 18:25:40 +00:00
|
|
|
if ((load_once = (pa_bool_t (*)(void)) pa_load_sym(m->dl, name, PA_SYMBOL_LOAD_ONCE))) {
|
|
|
|
|
|
2008-08-03 18:55:42 +02:00
|
|
|
m->load_once = load_once();
|
|
|
|
|
|
|
|
|
|
if (m->load_once && c->modules) {
|
2007-11-09 18:25:40 +00:00
|
|
|
pa_module *i;
|
|
|
|
|
uint32_t idx;
|
|
|
|
|
/* OK, the module only wants to be loaded once, let's make sure it is */
|
|
|
|
|
|
|
|
|
|
for (i = pa_idxset_first(c->modules, &idx); i; i = pa_idxset_next(c->modules, &idx)) {
|
|
|
|
|
if (strcmp(name, i->name) == 0) {
|
|
|
|
|
pa_log("Module \"%s\" should be loaded once at most. Refusing to load.", name);
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (!(m->init = (int (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_INIT))) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT"\" not found.", name);
|
2004-06-08 23:54:24 +00:00
|
|
|
goto fail;
|
2004-09-11 23:17:38 +00:00
|
|
|
}
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
m->done = (void (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_DONE);
|
2004-06-08 23:54:24 +00:00
|
|
|
m->userdata = NULL;
|
2004-06-10 23:22:16 +00:00
|
|
|
m->core = c;
|
2004-08-04 16:39:30 +00:00
|
|
|
m->n_used = -1;
|
2008-05-15 23:34:41 +00:00
|
|
|
m->auto_unload = FALSE;
|
|
|
|
|
m->unload_requested = FALSE;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (m->init(m) < 0) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_error("Failed to load module \"%s\" (argument: \"%s\"): initialization failed.", name, argument ? argument : "");
|
2004-06-08 23:54:24 +00:00
|
|
|
goto fail;
|
2004-09-11 23:17:38 +00:00
|
|
|
}
|
2004-06-08 23:54:24 +00:00
|
|
|
|
|
|
|
|
if (!c->modules)
|
2004-07-03 23:35:12 +00:00
|
|
|
c->modules = pa_idxset_new(NULL, NULL);
|
2004-08-04 16:39:30 +00:00
|
|
|
|
2008-06-11 16:55:26 +00:00
|
|
|
if (m->auto_unload && !c->module_auto_unload_event) {
|
2004-08-04 16:39:30 +00:00
|
|
|
struct timeval ntv;
|
2006-01-10 17:51:06 +00:00
|
|
|
pa_gettimeofday(&ntv);
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_timeval_add(&ntv, UNLOAD_POLL_TIME*1000000);
|
2004-09-14 23:08:39 +00:00
|
|
|
c->module_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
|
2004-08-04 16:39:30 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert_se(pa_idxset_put(c->modules, m, &m->index) >= 0);
|
|
|
|
|
pa_assert(m->index != PA_IDXSET_INVALID);
|
2004-06-15 15:18:33 +00:00
|
|
|
|
2007-01-04 13:43:45 +00:00
|
|
|
pa_log_info("Loaded \"%s\" (index: #%u; argument: \"%s\").", m->name, m->index, m->argument ? m->argument : "");
|
2004-08-11 00:11:12 +00:00
|
|
|
|
|
|
|
|
pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_NEW, m->index);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
return m;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
fail:
|
2004-09-05 00:03:16 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
if (m) {
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(m->argument);
|
|
|
|
|
pa_xfree(m->name);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
if (m->dl)
|
|
|
|
|
lt_dlclose(m->dl);
|
|
|
|
|
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(m);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void pa_module_free(pa_module *m) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
|
|
|
|
pa_assert(m->core);
|
2004-09-04 00:27:36 +00:00
|
|
|
|
2007-01-04 13:43:45 +00:00
|
|
|
pa_log_info("Unloading \"%s\" (index: #%u).", m->name, m->index);
|
2004-11-07 20:48:46 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (m->done)
|
|
|
|
|
m->done(m);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-06-10 23:22:16 +00:00
|
|
|
lt_dlclose(m->dl);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
|
|
|
|
pa_log_info("Unloaded \"%s\" (index: #%u).", m->name, m->index);
|
2004-06-15 15:18:33 +00:00
|
|
|
|
2004-08-11 00:11:12 +00:00
|
|
|
pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_REMOVE, m->index);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(m->name);
|
|
|
|
|
pa_xfree(m->argument);
|
|
|
|
|
pa_xfree(m);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-05 23:56:25 +02:00
|
|
|
void pa_module_unload(pa_core *c, pa_module *m, pa_bool_t force) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
|
|
|
|
pa_assert(m);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2008-08-05 23:56:25 +02:00
|
|
|
if (m->core->disallow_module_loading && !force)
|
|
|
|
|
return;
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
|
2004-06-08 23:54:24 +00:00
|
|
|
return;
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_module_free(m);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-05 23:56:25 +02:00
|
|
|
void pa_module_unload_by_index(pa_core *c, uint32_t idx, pa_bool_t force) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_module *m;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
|
|
|
|
pa_assert(idx != PA_IDXSET_INVALID);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2008-08-05 23:56:25 +02:00
|
|
|
if (m->core->disallow_module_loading && !force)
|
|
|
|
|
return;
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
if (!(m = pa_idxset_remove_by_index(c->modules, idx)))
|
2004-06-08 23:54:24 +00:00
|
|
|
return;
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_module_free(m);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_module_unload_all(pa_core *c) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2008-06-27 20:34:14 +02:00
|
|
|
if (c->modules) {
|
|
|
|
|
pa_module *m;
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2008-06-27 20:34:14 +02:00
|
|
|
while ((m = pa_idxset_steal_first(c->modules, NULL)))
|
|
|
|
|
pa_module_free(m);
|
2006-07-31 21:53:48 +00:00
|
|
|
|
2008-06-27 20:34:14 +02:00
|
|
|
pa_idxset_free(c->modules, NULL, NULL);
|
|
|
|
|
c->modules = NULL;
|
|
|
|
|
}
|
2004-08-04 16:39:30 +00:00
|
|
|
|
2004-09-14 23:08:39 +00:00
|
|
|
if (c->module_auto_unload_event) {
|
|
|
|
|
c->mainloop->time_free(c->module_auto_unload_event);
|
|
|
|
|
c->module_auto_unload_event = NULL;
|
2004-09-14 20:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-14 23:08:39 +00:00
|
|
|
if (c->module_defer_unload_event) {
|
|
|
|
|
c->mainloop->defer_free(c->module_defer_unload_event);
|
|
|
|
|
c->module_defer_unload_event = NULL;
|
2004-09-14 20:53:25 +00:00
|
|
|
}
|
2004-08-04 16:39:30 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_module_unload_unused(pa_core *c) {
|
2008-06-27 20:34:14 +02:00
|
|
|
void *state = NULL;
|
2004-08-04 16:39:30 +00:00
|
|
|
time_t now;
|
2008-06-27 20:34:14 +02:00
|
|
|
pa_module *m;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
2004-08-04 16:39:30 +00:00
|
|
|
|
|
|
|
|
if (!c->modules)
|
|
|
|
|
return;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-08-04 16:39:30 +00:00
|
|
|
time(&now);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2008-06-27 20:34:14 +02:00
|
|
|
while ((m = pa_idxset_iterate(c->modules, &state, NULL))) {
|
2004-09-14 20:53:25 +00:00
|
|
|
|
2008-06-27 20:34:14 +02:00
|
|
|
if (m->n_used > 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (!m->auto_unload)
|
|
|
|
|
continue;
|
2004-09-14 20:53:25 +00:00
|
|
|
|
2008-06-27 20:34:14 +02:00
|
|
|
if (m->last_used_time + m->core->module_idle_time > now)
|
|
|
|
|
continue;
|
|
|
|
|
|
2008-08-05 23:56:25 +02:00
|
|
|
pa_module_unload(c, m, FALSE);
|
2008-06-27 20:34:14 +02:00
|
|
|
}
|
2004-06-19 01:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void defer_cb(pa_mainloop_api*api, pa_defer_event *e, void *userdata) {
|
2008-06-27 20:34:14 +02:00
|
|
|
void *state = NULL;
|
|
|
|
|
pa_core *c = PA_CORE(userdata);
|
|
|
|
|
pa_module *m;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2008-06-27 20:34:14 +02:00
|
|
|
pa_core_assert_ref(c);
|
2004-09-14 20:53:25 +00:00
|
|
|
api->defer_enable(e, 0);
|
|
|
|
|
|
2008-06-27 20:34:14 +02:00
|
|
|
if (!c->modules)
|
2004-09-14 20:53:25 +00:00
|
|
|
return;
|
|
|
|
|
|
2008-06-27 20:34:14 +02:00
|
|
|
while ((m = pa_idxset_iterate(c->modules, &state, NULL)))
|
|
|
|
|
if (m->unload_requested)
|
2008-08-05 23:56:25 +02:00
|
|
|
pa_module_unload(c, m, TRUE);
|
2004-09-14 20:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-05 23:56:25 +02:00
|
|
|
void pa_module_unload_request(pa_module *m, pa_bool_t force) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
2004-09-14 20:53:25 +00:00
|
|
|
|
2008-08-05 23:56:25 +02:00
|
|
|
if (m->core->disallow_module_loading && !force)
|
|
|
|
|
return;
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
m->unload_requested = TRUE;
|
2004-09-14 20:53:25 +00:00
|
|
|
|
2004-09-14 23:08:39 +00:00
|
|
|
if (!m->core->module_defer_unload_event)
|
|
|
|
|
m->core->module_defer_unload_event = m->core->mainloop->defer_new(m->core->mainloop, defer_cb, m->core);
|
2004-06-19 01:01:09 +00:00
|
|
|
|
2004-09-14 23:08:39 +00:00
|
|
|
m->core->mainloop->defer_enable(m->core->module_defer_unload_event, 1);
|
2004-06-19 01:01:09 +00:00
|
|
|
}
|
2004-08-04 16:39:30 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_module_set_used(pa_module*m, int used) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
2004-08-04 16:39:30 +00:00
|
|
|
|
2004-08-12 23:25:28 +00:00
|
|
|
if (m->n_used != used)
|
|
|
|
|
pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_CHANGE, m->index);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (used == 0 && m->n_used > 0)
|
2004-08-04 16:39:30 +00:00
|
|
|
time(&m->last_used_time);
|
|
|
|
|
|
|
|
|
|
m->n_used = used;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_modinfo *pa_module_get_info(pa_module *m) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
2004-09-11 23:17:38 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return pa_modinfo_get_by_handle(m->dl, m->name);
|
2004-09-11 23:17:38 +00:00
|
|
|
}
|