]> code.delx.au - pulseaudio/blob - polyp/module.c
add option to disallow module loading after startup
[pulseaudio] / polyp / module.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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.
10
11 polypaudio 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.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #include "module.h"
34 #include "xmalloc.h"
35 #include "subscribe.h"
36
37 #define UNLOAD_POLL_TIME 10
38
39 static void timeout_callback(struct pa_mainloop_api *m, struct pa_time_event*e, const struct timeval *tv, void *userdata) {
40 struct pa_core *c = userdata;
41 struct timeval ntv;
42 assert(c && c->mainloop == m && c->auto_unload_event == e);
43
44 pa_module_unload_unused(c);
45
46 gettimeofday(&ntv, NULL);
47 ntv.tv_sec += UNLOAD_POLL_TIME;
48 m->time_restart(e, &ntv);
49 }
50
51 struct pa_module* pa_module_load(struct pa_core *c, const char *name, const char *argument) {
52 struct pa_module *m = NULL;
53 int r;
54
55 assert(c && name);
56
57 if (c->disallow_module_loading)
58 goto fail;
59
60 m = pa_xmalloc(sizeof(struct pa_module));
61
62 m->name = pa_xstrdup(name);
63 m->argument = pa_xstrdup(argument);
64
65 if (!(m->dl = lt_dlopenext(name)))
66 goto fail;
67
68 if (!(m->init = (int (*)(struct pa_core *c, struct pa_module*m)) lt_dlsym(m->dl, "pa_module_init")))
69 goto fail;
70
71 if (!(m->done = (void (*)(struct pa_core *c, struct pa_module*m)) lt_dlsym(m->dl, "pa_module_done")))
72 goto fail;
73
74 m->userdata = NULL;
75 m->core = c;
76 m->n_used = -1;
77 m->auto_unload = 0;
78
79 assert(m->init);
80 if (m->init(c, m) < 0)
81 goto fail;
82
83 if (!c->modules)
84 c->modules = pa_idxset_new(NULL, NULL);
85
86 if (!c->auto_unload_event) {
87 struct timeval ntv;
88 gettimeofday(&ntv, NULL);
89 ntv.tv_sec += UNLOAD_POLL_TIME;
90 c->auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
91 }
92 assert(c->auto_unload_event);
93
94 assert(c->modules);
95 r = pa_idxset_put(c->modules, m, &m->index);
96 assert(r >= 0 && m->index != PA_IDXSET_INVALID);
97
98 fprintf(stderr, "module: loaded %u \"%s\" with argument \"%s\".\n", m->index, m->name, m->argument);
99
100 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_NEW, m->index);
101
102 return m;
103
104 fail:
105 if (m) {
106 pa_xfree(m->argument);
107 pa_xfree(m->name);
108
109 if (m->dl)
110 lt_dlclose(m->dl);
111
112 pa_xfree(m);
113 }
114
115 return NULL;
116 }
117
118 static void pa_module_free(struct pa_module *m) {
119 assert(m && m->done && m->core);
120 m->done(m->core, m);
121
122 lt_dlclose(m->dl);
123
124 fprintf(stderr, "module: unloaded %u \"%s\".\n", m->index, m->name);
125
126 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_REMOVE, m->index);
127
128 pa_xfree(m->name);
129 pa_xfree(m->argument);
130 pa_xfree(m);
131 }
132
133
134 void pa_module_unload(struct pa_core *c, struct pa_module *m) {
135 assert(c && m);
136
137 assert(c->modules);
138 if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
139 return;
140
141 pa_module_free(m);
142 }
143
144 void pa_module_unload_by_index(struct pa_core *c, uint32_t index) {
145 struct pa_module *m;
146 assert(c && index != PA_IDXSET_INVALID);
147
148 assert(c->modules);
149 if (!(m = pa_idxset_remove_by_index(c->modules, index)))
150 return;
151
152 pa_module_free(m);
153 }
154
155 static void free_callback(void *p, void *userdata) {
156 struct pa_module *m = p;
157 assert(m);
158 pa_module_free(m);
159 }
160
161 void pa_module_unload_all(struct pa_core *c) {
162 assert(c);
163
164 if (!c->modules)
165 return;
166
167 pa_idxset_free(c->modules, free_callback, NULL);
168 c->modules = NULL;
169
170 if (c->auto_unload_event)
171 c->mainloop->time_free(c->auto_unload_event);
172 c->auto_unload_event = NULL;
173 }
174
175 static int unused_callback(void *p, uint32_t index, int *del, void *userdata) {
176 struct pa_module *m = p;
177 time_t *now = userdata;
178 assert(p && del && now);
179
180 if (m->n_used == 0 && m->auto_unload && m->last_used_time+m->core->auto_unload_time <= *now) {
181 pa_module_free(m);
182 *del = 1;
183 }
184
185 return 0;
186 }
187
188 void pa_module_unload_unused(struct pa_core *c) {
189 time_t now;
190 assert(c);
191
192 if (!c->modules)
193 return;
194
195 time(&now);
196 pa_idxset_foreach(c->modules, unused_callback, &now);
197 }
198
199 struct once_info {
200 struct pa_core *core;
201 uint32_t index;
202 };
203
204 static void module_unload_once_callback(struct pa_mainloop_api *m, void *userdata) {
205 struct once_info *i = userdata;
206 assert(i);
207 pa_module_unload_by_index(i->core, i->index);
208 pa_xfree(i);
209 }
210
211 void pa_module_unload_request(struct pa_core *c, struct pa_module *m) {
212 struct once_info *i;
213 assert(c && m);
214
215 i = pa_xmalloc(sizeof(struct once_info));
216 i->core = c;
217 i->index = m->index;
218 pa_mainloop_api_once(c->mainloop, module_unload_once_callback, i);
219 }
220
221 void pa_module_set_used(struct pa_module*m, int used) {
222 assert(m);
223
224 if (m->n_used != used)
225 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_CHANGE, m->index);
226
227 if (m->n_used != used && used == 0)
228 time(&m->last_used_time);
229
230 m->n_used = used;
231 }
232