module loading: initialize module index to invalid value.
[profile/ivi/pulseaudio.git] / src / pulsecore / module.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2004-2006 Lennart Poettering
5   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7   PulseAudio is free software; you can redistribute it and/or modify
8   it under the terms of the GNU Lesser General Public License as published
9   by the Free Software Foundation; either version 2.1 of the License,
10   or (at your option) any later version.
11
12   PulseAudio is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   General Public License for more details.
16
17   You should have received a copy of the GNU Lesser General Public License
18   along with PulseAudio; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20   USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31
32 #include <pulse/xmalloc.h>
33 #include <pulse/proplist.h>
34
35 #include <pulsecore/core-subscribe.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/macro.h>
39 #include <pulsecore/ltdl-helper.h>
40 #include <pulsecore/modinfo.h>
41
42 #include "module.h"
43
44 #define PA_SYMBOL_INIT "pa__init"
45 #define PA_SYMBOL_DONE "pa__done"
46 #define PA_SYMBOL_LOAD_ONCE "pa__load_once"
47 #define PA_SYMBOL_GET_N_USED "pa__get_n_used"
48 #define PA_SYMBOL_GET_DEPRECATE "pa__get_deprecated"
49
50 pa_module* pa_module_load(pa_core *c, const char *name, const char *argument) {
51     pa_module *m = NULL;
52     pa_bool_t (*load_once)(void);
53     const char* (*get_deprecated)(void);
54     pa_modinfo *mi;
55
56     pa_assert(c);
57     pa_assert(name);
58
59     if (c->disallow_module_loading)
60         goto fail;
61
62     m = pa_xnew(pa_module, 1);
63     m->name = pa_xstrdup(name);
64     m->argument = pa_xstrdup(argument);
65     m->load_once = FALSE;
66     m->proplist = pa_proplist_new();
67     m->index = PA_IDXSET_INVALID;
68
69     if (!(m->dl = lt_dlopenext(name))) {
70         pa_log("Failed to open module \"%s\": %s", name, lt_dlerror());
71         goto fail;
72     }
73
74     if ((load_once = (pa_bool_t (*)(void)) pa_load_sym(m->dl, name, PA_SYMBOL_LOAD_ONCE))) {
75
76         m->load_once = load_once();
77
78         if (m->load_once) {
79             pa_module *i;
80             uint32_t idx;
81             /* OK, the module only wants to be loaded once, let's make sure it is */
82
83             for (i = pa_idxset_first(c->modules, &idx); i; i = pa_idxset_next(c->modules, &idx)) {
84                 if (strcmp(name, i->name) == 0) {
85                     pa_log("Module \"%s\" should be loaded once at most. Refusing to load.", name);
86                     goto fail;
87                 }
88             }
89         }
90     }
91
92     if ((get_deprecated = (const char* (*) (void)) pa_load_sym(m->dl, name, PA_SYMBOL_GET_DEPRECATE))) {
93         const char *t;
94
95         if ((t = get_deprecated()))
96             pa_log_warn("%s is deprecated: %s", name, t);
97     }
98
99     if (!(m->init = (int (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_INIT))) {
100         pa_log("Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT"\" not found.", name);
101         goto fail;
102     }
103
104     m->done = (void (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_DONE);
105     m->get_n_used = (int (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_GET_N_USED);
106     m->userdata = NULL;
107     m->core = c;
108     m->unload_requested = FALSE;
109
110     if (m->init(m) < 0) {
111         pa_log_error("Failed to load module \"%s\" (argument: \"%s\"): initialization failed.", name, argument ? argument : "");
112         goto fail;
113     }
114
115     pa_assert_se(pa_idxset_put(c->modules, m, &m->index) >= 0);
116     pa_assert(m->index != PA_IDXSET_INVALID);
117
118     pa_log_info("Loaded \"%s\" (index: #%u; argument: \"%s\").", m->name, m->index, m->argument ? m->argument : "");
119
120     pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_NEW, m->index);
121
122     if ((mi = pa_modinfo_get_by_handle(m->dl, name))) {
123
124         if (mi->author && !pa_proplist_contains(m->proplist, PA_PROP_MODULE_AUTHOR))
125             pa_proplist_sets(m->proplist, PA_PROP_MODULE_AUTHOR, mi->author);
126
127         if (mi->description && !pa_proplist_contains(m->proplist, PA_PROP_MODULE_DESCRIPTION))
128             pa_proplist_sets(m->proplist, PA_PROP_MODULE_DESCRIPTION, mi->description);
129
130         if (mi->version && !pa_proplist_contains(m->proplist, PA_PROP_MODULE_VERSION))
131             pa_proplist_sets(m->proplist, PA_PROP_MODULE_VERSION, mi->version);
132
133         pa_modinfo_free(mi);
134     }
135
136     return m;
137
138 fail:
139
140     if (m) {
141         if (m->proplist)
142             pa_proplist_free(m->proplist);
143
144         pa_xfree(m->argument);
145         pa_xfree(m->name);
146
147         if (m->dl)
148             lt_dlclose(m->dl);
149
150         pa_xfree(m);
151     }
152
153     return NULL;
154 }
155
156 static void pa_module_free(pa_module *m) {
157     pa_assert(m);
158     pa_assert(m->core);
159
160     pa_log_info("Unloading \"%s\" (index: #%u).", m->name, m->index);
161
162     if (m->done)
163         m->done(m);
164
165     if (m->proplist)
166         pa_proplist_free(m->proplist);
167
168     lt_dlclose(m->dl);
169
170     pa_log_info("Unloaded \"%s\" (index: #%u).", m->name, m->index);
171
172     pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_REMOVE, m->index);
173
174     pa_xfree(m->name);
175     pa_xfree(m->argument);
176     pa_xfree(m);
177 }
178
179 void pa_module_unload(pa_core *c, pa_module *m, pa_bool_t force) {
180     pa_assert(c);
181     pa_assert(m);
182
183     if (m->core->disallow_module_loading && !force)
184         return;
185
186     if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
187         return;
188
189     pa_module_free(m);
190 }
191
192 void pa_module_unload_by_index(pa_core *c, uint32_t idx, pa_bool_t force) {
193     pa_module *m;
194     pa_assert(c);
195     pa_assert(idx != PA_IDXSET_INVALID);
196
197     if (c->disallow_module_loading && !force)
198         return;
199
200     if (!(m = pa_idxset_remove_by_index(c->modules, idx)))
201         return;
202
203     pa_module_free(m);
204 }
205
206 void pa_module_unload_all(pa_core *c) {
207     pa_module *m;
208     pa_assert(c);
209
210     while ((m = pa_idxset_steal_first(c->modules, NULL)))
211         pa_module_free(m);
212
213     if (c->module_defer_unload_event) {
214         c->mainloop->defer_free(c->module_defer_unload_event);
215         c->module_defer_unload_event = NULL;
216     }
217 }
218
219 static void defer_cb(pa_mainloop_api*api, pa_defer_event *e, void *userdata) {
220     void *state = NULL;
221     pa_core *c = PA_CORE(userdata);
222     pa_module *m;
223
224     pa_core_assert_ref(c);
225     api->defer_enable(e, 0);
226
227     while ((m = pa_idxset_iterate(c->modules, &state, NULL)))
228         if (m->unload_requested)
229             pa_module_unload(c, m, TRUE);
230 }
231
232 void pa_module_unload_request(pa_module *m, pa_bool_t force) {
233     pa_assert(m);
234
235     if (m->core->disallow_module_loading && !force)
236         return;
237
238     m->unload_requested = TRUE;
239
240     if (!m->core->module_defer_unload_event)
241         m->core->module_defer_unload_event = m->core->mainloop->defer_new(m->core->mainloop, defer_cb, m->core);
242
243     m->core->mainloop->defer_enable(m->core->module_defer_unload_event, 1);
244 }
245
246 void pa_module_unload_request_by_index(pa_core *c, uint32_t idx, pa_bool_t force) {
247     pa_module *m;
248     pa_assert(c);
249
250     if (!(m = pa_idxset_get_by_index(c->modules, idx)))
251         return;
252
253     pa_module_unload_request(m, force);
254 }
255
256 int pa_module_get_n_used(pa_module*m) {
257     pa_assert(m);
258
259     if (!m->get_n_used)
260         return -1;
261
262     return m->get_n_used(m);
263 }
264
265 void pa_module_update_proplist(pa_module *m, pa_update_mode_t mode, pa_proplist *p) {
266     pa_assert(m);
267
268     if (p)
269         pa_proplist_update(m->proplist, mode, p);
270
271     pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_CHANGE, m->index);
272 }