#include "module.h"
#include "xmalloc.h"
#include "subscribe.h"
+#include "log.h"
-#define UNLOAD_POLL_TIME 10
+#define PA_SYMBOL_INIT "pa__init"
+#define PA_SYMBOL_DONE "pa__done"
+
+#define UNLOAD_POLL_TIME 2
static void timeout_callback(struct pa_mainloop_api *m, struct pa_time_event*e, const struct timeval *tv, void *userdata) {
struct pa_core *c = userdata;
assert(c && name);
+ if (c->disallow_module_loading)
+ goto fail;
+
m = pa_xmalloc(sizeof(struct pa_module));
m->name = pa_xstrdup(name);
m->argument = pa_xstrdup(argument);
- if (!(m->dl = lt_dlopenext(name)))
+ if (!(m->dl = lt_dlopenext(name))) {
+ pa_log(__FILE__": Failed to open module \"%s\": %s\n", name, lt_dlerror());
goto fail;
+ }
- if (!(m->init = lt_dlsym(m->dl, "pa_module_init")))
+ if (!(m->init = (int (*)(struct pa_core *c, struct pa_module*m)) lt_dlsym(m->dl, PA_SYMBOL_INIT))) {
+ pa_log(__FILE__": Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT"\" not found.\n", name);
goto fail;
+ }
- if (!(m->done = lt_dlsym(m->dl, "pa_module_done")))
+ if (!(m->done = (void (*)(struct pa_core *c, struct pa_module*m)) lt_dlsym(m->dl, PA_SYMBOL_DONE))) {
+ pa_log(__FILE__": Failed to load module \"%s\": symbol \""PA_SYMBOL_DONE"\" not found.\n", name);
goto fail;
+ }
m->userdata = NULL;
m->core = c;
m->auto_unload = 0;
assert(m->init);
- if (m->init(c, m) < 0)
+ if (m->init(c, m) < 0) {
+ pa_log(__FILE__": Failed to load module \"%s\" (argument: \"%s\"): initialization failed.\n", name, argument ? argument : "");
goto fail;
+ }
if (!c->modules)
c->modules = pa_idxset_new(NULL, NULL);
r = pa_idxset_put(c->modules, m, &m->index);
assert(r >= 0 && m->index != PA_IDXSET_INVALID);
- fprintf(stderr, "module: loaded %u \"%s\" with argument \"%s\".\n", m->index, m->name, m->argument);
+ pa_log(__FILE__": Loaded \"%s\" (index: #%u; argument: \"%s\").\n", m->name, m->index, m->argument ? m->argument : "");
pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_NEW, m->index);
return m;
fail:
+
if (m) {
pa_xfree(m->argument);
pa_xfree(m->name);
static void pa_module_free(struct pa_module *m) {
assert(m && m->done && m->core);
+
+ if (m->core->disallow_module_loading)
+ return;
+
m->done(m->core, m);
lt_dlclose(m->dl);
- fprintf(stderr, "module: unloaded %u \"%s\".\n", m->index, m->name);
+ pa_log(__FILE__": Unloaded \"%s\" (index: #%u).\n", m->name, m->index);
pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_REMOVE, m->index);
pa_xfree(m);
}
-
void pa_module_unload(struct pa_core *c, struct pa_module *m) {
assert(c && m);
time_t *now = userdata;
assert(p && del && now);
- if (m->n_used == 0 && m->auto_unload && m->last_used_time+m->core->auto_unload_time <= *now) {
+ if (m->n_used == 0 && m->auto_unload && m->last_used_time+m->core->module_idle_time <= *now) {
pa_module_free(m);
*del = 1;
}
m->n_used = used;
}
+struct pa_modinfo *pa_module_get_info(struct pa_module *m) {
+ assert(m);
+
+ return pa_modinfo_get_by_handle(m->dl);
+}