]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/module.c
remap: fix build for non-x86 builds
[pulseaudio] / src / pulsecore / module.c
index 09b15b8b01f917f45fc301d63b6cf3d9f858300d..5bcdd8984085dd7db648216fec4e019a559ff7e9 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /***
   This file is part of PulseAudio.
 
 /***
   This file is part of PulseAudio.
 
@@ -8,7 +6,7 @@
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
-  by the Free Software Foundation; either version 2 of the License,
+  by the Free Software Foundation; either version 2.1 of the License,
   or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
   or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <assert.h>
 #include <string.h>
 #include <errno.h>
 #include <ctype.h>
 
 #include <pulse/timeval.h>
 #include <pulse/xmalloc.h>
 #include <string.h>
 #include <errno.h>
 #include <ctype.h>
 
 #include <pulse/timeval.h>
 #include <pulse/xmalloc.h>
+#include <pulse/proplist.h>
 
 #include <pulsecore/core-subscribe.h>
 #include <pulsecore/log.h>
 #include <pulsecore/core-util.h>
 
 #include <pulsecore/core-subscribe.h>
 #include <pulsecore/log.h>
 #include <pulsecore/core-util.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/ltdl-helper.h>
+#include <pulsecore/modinfo.h>
 
 #include "module.h"
 
 #define PA_SYMBOL_INIT "pa__init"
 #define PA_SYMBOL_DONE "pa__done"
 
 #include "module.h"
 
 #define PA_SYMBOL_INIT "pa__init"
 #define PA_SYMBOL_DONE "pa__done"
-
-#define UNLOAD_POLL_TIME 2
-
-/* lt_dlsym() violates ISO C, so confide the breakage into this function to
- * avoid warnings. */
-typedef void (*fnptr)(void);
-static inline fnptr lt_dlsym_fn(lt_dlhandle handle, const char *symbol) {
-    return (fnptr) (long) lt_dlsym(handle, symbol);
-}
-
-static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
-    pa_core *c = userdata;
-    struct timeval ntv;
-    assert(c && c->mainloop == m && c->module_auto_unload_event == e);
-
-    pa_module_unload_unused(c);
-
-    pa_gettimeofday(&ntv);
-    ntv.tv_sec += UNLOAD_POLL_TIME;
-    m->time_restart(e, &ntv);
-}
-
-static inline fnptr load_sym(lt_dlhandle handle, const char *module, const char *symbol) {
-    char *buffer, *ch;
-    size_t buflen;
-    fnptr res;
-
-    res = lt_dlsym_fn(handle, symbol);
-    if (res)
-        return res;
-
-    /* As the .la files might have been cleansed from the system, we should
-     * try with the ltdl prefix as well. */
-
-    buflen = strlen(symbol) + strlen(module) + strlen("_LTX_") + 1;
-    buffer = pa_xmalloc(buflen);
-    assert(buffer);
-
-    strcpy(buffer, module);
-
-    for (ch = buffer;*ch != '\0';ch++) {
-        if (!isalnum(*ch))
-            *ch = '_';
-    }
-
-    strcat(buffer, "_LTX_");
-    strcat(buffer, symbol);
-
-    res = lt_dlsym_fn(handle, buffer);
-
-    pa_xfree(buffer);
-
-    return res;
-}
+#define PA_SYMBOL_LOAD_ONCE "pa__load_once"
+#define PA_SYMBOL_GET_N_USED "pa__get_n_used"
+#define PA_SYMBOL_GET_DEPRECATE "pa__get_deprecated"
 
 pa_module* pa_module_load(pa_core *c, const char *name, const char *argument) {
     pa_module *m = NULL;
 
 pa_module* pa_module_load(pa_core *c, const char *name, const char *argument) {
     pa_module *m = NULL;
-    int r;
+    pa_bool_t (*load_once)(void);
+    const char* (*get_deprecated)(void);
+    pa_modinfo *mi;
 
 
-    assert(c && name);
+    pa_assert(c);
+    pa_assert(name);
 
     if (c->disallow_module_loading)
         goto fail;
 
 
     if (c->disallow_module_loading)
         goto fail;
 
-    m = pa_xmalloc(sizeof(pa_module));
-
+    m = pa_xnew(pa_module, 1);
     m->name = pa_xstrdup(name);
     m->argument = pa_xstrdup(argument);
     m->name = pa_xstrdup(name);
     m->argument = pa_xstrdup(argument);
+    m->load_once = FALSE;
+    m->proplist = pa_proplist_new();
 
     if (!(m->dl = lt_dlopenext(name))) {
         pa_log("Failed to open module \"%s\": %s", name, lt_dlerror());
         goto fail;
     }
 
 
     if (!(m->dl = lt_dlopenext(name))) {
         pa_log("Failed to open module \"%s\": %s", name, lt_dlerror());
         goto fail;
     }
 
-    if (!(m->init = (int (*)(pa_core *_c, pa_module*_m)) load_sym(m->dl, name, PA_SYMBOL_INIT))) {
-        pa_log("Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT"\" not found.", name);
-        goto fail;
+    if ((load_once = (pa_bool_t (*)(void)) pa_load_sym(m->dl, name, PA_SYMBOL_LOAD_ONCE))) {
+
+        m->load_once = load_once();
+
+        if (m->load_once) {
+            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;
+                }
+            }
+        }
     }
 
     }
 
-    if (!(m->done = (void (*)(pa_core *_c, pa_module*_m)) load_sym(m->dl, name, PA_SYMBOL_DONE))) {
-        pa_log("Failed to load module \"%s\": symbol \""PA_SYMBOL_DONE"\" not found.", name);
+    if ((get_deprecated = (const char* (*) (void)) pa_load_sym(m->dl, name, PA_SYMBOL_GET_DEPRECATE))) {
+        const char *t;
+
+        if ((t = get_deprecated()))
+            pa_log_warn("%s is deprecated: %s", name, t);
+    }
+
+    if (!(m->init = (int (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_INIT))) {
+        pa_log("Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT"\" not found.", name);
         goto fail;
     }
 
         goto fail;
     }
 
+    m->done = (void (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_DONE);
+    m->get_n_used = (int (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_GET_N_USED);
     m->userdata = NULL;
     m->core = c;
     m->userdata = NULL;
     m->core = c;
-    m->n_used = -1;
-    m->auto_unload = 0;
-    m->unload_requested = 0;
+    m->unload_requested = FALSE;
 
 
-    assert(m->init);
-    if (m->init(c, m) < 0) {
+    if (m->init(m) < 0) {
         pa_log_error("Failed to load  module \"%s\" (argument: \"%s\"): initialization failed.", name, argument ? argument : "");
         goto fail;
     }
 
         pa_log_error("Failed to load  module \"%s\" (argument: \"%s\"): initialization failed.", name, argument ? argument : "");
         goto fail;
     }
 
-    if (!c->modules)
-        c->modules = pa_idxset_new(NULL, NULL);
-
-    if (!c->module_auto_unload_event) {
-        struct timeval ntv;
-        pa_gettimeofday(&ntv);
-        ntv.tv_sec += UNLOAD_POLL_TIME;
-        c->module_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
-    }
-    assert(c->module_auto_unload_event);
-
-    assert(c->modules);
-    r = pa_idxset_put(c->modules, m, &m->index);
-    assert(r >= 0 && m->index != PA_IDXSET_INVALID);
+    pa_assert_se(pa_idxset_put(c->modules, m, &m->index) >= 0);
+    pa_assert(m->index != PA_IDXSET_INVALID);
 
     pa_log_info("Loaded \"%s\" (index: #%u; argument: \"%s\").", m->name, m->index, m->argument ? m->argument : "");
 
     pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_NEW, m->index);
 
 
     pa_log_info("Loaded \"%s\" (index: #%u; argument: \"%s\").", m->name, m->index, m->argument ? m->argument : "");
 
     pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_NEW, m->index);
 
+    if ((mi = pa_modinfo_get_by_handle(m->dl, name))) {
+
+        if (mi->author && !pa_proplist_contains(m->proplist, PA_PROP_MODULE_AUTHOR))
+            pa_proplist_sets(m->proplist, PA_PROP_MODULE_AUTHOR, mi->author);
+
+        if (mi->description && !pa_proplist_contains(m->proplist, PA_PROP_MODULE_DESCRIPTION))
+            pa_proplist_sets(m->proplist, PA_PROP_MODULE_DESCRIPTION, mi->description);
+
+        if (mi->version && !pa_proplist_contains(m->proplist, PA_PROP_MODULE_VERSION))
+            pa_proplist_sets(m->proplist, PA_PROP_MODULE_VERSION, mi->version);
+
+        pa_modinfo_free(mi);
+    }
+
     return m;
 
 fail:
 
     if (m) {
     return m;
 
 fail:
 
     if (m) {
+        if (m->proplist)
+            pa_proplist_free(m->proplist);
+
         pa_xfree(m->argument);
         pa_xfree(m->name);
 
         pa_xfree(m->argument);
         pa_xfree(m->name);
 
@@ -178,14 +156,16 @@ fail:
 }
 
 static void pa_module_free(pa_module *m) {
 }
 
 static void pa_module_free(pa_module *m) {
-    assert(m && m->done && m->core);
-
-    if (m->core->disallow_module_loading)
-        return;
+    pa_assert(m);
+    pa_assert(m->core);
 
     pa_log_info("Unloading \"%s\" (index: #%u).", m->name, m->index);
 
 
     pa_log_info("Unloading \"%s\" (index: #%u).", m->name, m->index);
 
-    m->done(m->core, m);
+    if (m->done)
+        m->done(m);
+
+    if (m->proplist)
+        pa_proplist_free(m->proplist);
 
     lt_dlclose(m->dl);
 
 
     lt_dlclose(m->dl);
 
@@ -198,50 +178,39 @@ static void pa_module_free(pa_module *m) {
     pa_xfree(m);
 }
 
     pa_xfree(m);
 }
 
-void pa_module_unload(pa_core *c, pa_module *m) {
-    assert(c && m);
+void pa_module_unload(pa_core *c, pa_module *m, pa_bool_t force) {
+    pa_assert(c);
+    pa_assert(m);
+
+    if (m->core->disallow_module_loading && !force)
+        return;
 
 
-    assert(c->modules);
     if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
         return;
 
     pa_module_free(m);
 }
 
     if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
         return;
 
     pa_module_free(m);
 }
 
-void pa_module_unload_by_index(pa_core *c, uint32_t idx) {
+void pa_module_unload_by_index(pa_core *c, uint32_t idx, pa_bool_t force) {
     pa_module *m;
     pa_module *m;
-    assert(c && idx != PA_IDXSET_INVALID);
+    pa_assert(c);
+    pa_assert(idx != PA_IDXSET_INVALID);
 
 
-    assert(c->modules);
-    if (!(m = pa_idxset_remove_by_index(c->modules, idx)))
+    if (c->disallow_module_loading && !force)
         return;
 
         return;
 
-    pa_module_free(m);
-}
+    if (!(m = pa_idxset_remove_by_index(c->modules, idx)))
+        return;
 
 
-static void free_callback(void *p, PA_GCC_UNUSED void *userdata) {
-    pa_module *m = p;
-    assert(m);
     pa_module_free(m);
 }
 
 void pa_module_unload_all(pa_core *c) {
     pa_module *m;
     pa_module_free(m);
 }
 
 void pa_module_unload_all(pa_core *c) {
     pa_module *m;
-    assert(c);
-
-    if (!c->modules)
-        return;
+    pa_assert(c);
 
 
-    while ((m = pa_idxset_first(c->modules, NULL)))
-        pa_module_unload(c, m);
-
-    pa_idxset_free(c->modules, free_callback, NULL);
-    c->modules = NULL;
-
-    if (c->module_auto_unload_event) {
-        c->mainloop->time_free(c->module_auto_unload_event);
-        c->module_auto_unload_event = NULL;
-    }
+    while ((m = pa_idxset_steal_first(c->modules, NULL)))
+        pa_module_free(m);
 
     if (c->module_defer_unload_event) {
         c->mainloop->defer_free(c->module_defer_unload_event);
 
     if (c->module_defer_unload_event) {
         c->mainloop->defer_free(c->module_defer_unload_event);
@@ -249,57 +218,26 @@ void pa_module_unload_all(pa_core *c) {
     }
 }
 
     }
 }
 
-static int unused_callback(void *p, PA_GCC_UNUSED uint32_t idx, int *del, void *userdata) {
-    pa_module *m = p;
-    time_t *now = userdata;
-    assert(p && del && 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;
-    }
-
-    return 0;
-}
-
-void pa_module_unload_unused(pa_core *c) {
-    time_t now;
-    assert(c);
-
-    if (!c->modules)
-        return;
-
-    time(&now);
-    pa_idxset_foreach(c->modules, unused_callback, &now);
-}
-
-static int unload_callback(void *p, PA_GCC_UNUSED uint32_t idx, int *del, PA_GCC_UNUSED void *userdata) {
-    pa_module *m = p;
-    assert(m);
+static void defer_cb(pa_mainloop_api*api, pa_defer_event *e, void *userdata) {
+    void *state = NULL;
+    pa_core *c = PA_CORE(userdata);
+    pa_module *m;
 
 
-    if (m->unload_requested) {
-        pa_module_free(m);
-        *del = 1;
-    }
+    pa_core_assert_ref(c);
+    api->defer_enable(e, 0);
 
 
-    return 0;
+    while ((m = pa_idxset_iterate(c->modules, &state, NULL)))
+        if (m->unload_requested)
+            pa_module_unload(c, m, TRUE);
 }
 
 }
 
-static void defer_cb(pa_mainloop_api*api, pa_defer_event *e, void *userdata) {
-    pa_core *core = userdata;
-    api->defer_enable(e, 0);
+void pa_module_unload_request(pa_module *m, pa_bool_t force) {
+    pa_assert(m);
 
 
-    if (!core->modules)
+    if (m->core->disallow_module_loading && !force)
         return;
 
         return;
 
-    pa_idxset_foreach(core->modules, unload_callback, NULL);
-
-}
-
-void pa_module_unload_request(pa_module *m) {
-    assert(m);
-
-    m->unload_requested = 1;
+    m->unload_requested = TRUE;
 
     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);
 
     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);
@@ -307,20 +245,21 @@ void pa_module_unload_request(pa_module *m) {
     m->core->mainloop->defer_enable(m->core->module_defer_unload_event, 1);
 }
 
     m->core->mainloop->defer_enable(m->core->module_defer_unload_event, 1);
 }
 
-void pa_module_set_used(pa_module*m, int used) {
-    assert(m);
-
-    if (m->n_used != used)
-        pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_CHANGE, m->index);
+void pa_module_unload_request_by_index(pa_core *c, uint32_t idx, pa_bool_t force) {
+    pa_module *m;
+    pa_assert(c);
 
 
-    if (m->n_used != used && used == 0)
-        time(&m->last_used_time);
+    if (!(m = pa_idxset_get_by_index(c->modules, idx)))
+        return;
 
 
-    m->n_used = used;
+    pa_module_unload_request(m, force);
 }
 
 }
 
-pa_modinfo *pa_module_get_info(pa_module *m) {
-    assert(m);
+int pa_module_get_n_used(pa_module*m) {
+    pa_assert(m);
+
+    if (!m->get_n_used)
+        return -1;
 
 
-    return pa_modinfo_get_by_handle(m->dl);
+    return m->get_n_used(m);
 }
 }