]> code.delx.au - pulseaudio/blob - src/modules/gconf/module-gconf.c
Use pa_hashmap_remove_and_free() where appropriate
[pulseaudio] / src / modules / gconf / module-gconf.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio 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 Lesser General Public License
17 along with PulseAudio; 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 <string.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33
34 #include <pulse/xmalloc.h>
35 #include <pulsecore/module.h>
36 #include <pulsecore/core.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/log.h>
39 #include <pulse/mainloop-api.h>
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/start-child.h>
42
43 #include "module-gconf-symdef.h"
44
45 PA_MODULE_AUTHOR("Lennart Poettering");
46 PA_MODULE_DESCRIPTION("GConf Adapter");
47 PA_MODULE_VERSION(PACKAGE_VERSION);
48 PA_MODULE_LOAD_ONCE(true);
49
50 #define MAX_MODULES 10
51 #define BUF_MAX 2048
52
53 struct userdata;
54
55 struct module_item {
56 char *name;
57 char *args;
58 uint32_t index;
59 };
60
61 struct module_info {
62 struct userdata *userdata;
63 char *name;
64
65 struct module_item items[MAX_MODULES];
66 unsigned n_items;
67 };
68
69 struct userdata {
70 pa_core *core;
71 pa_module *module;
72
73 pa_hashmap *module_infos;
74
75 pid_t pid;
76
77 int fd;
78 int fd_type;
79 pa_io_event *io_event;
80
81 char buf[BUF_MAX];
82 size_t buf_fill;
83 };
84
85 static int fill_buf(struct userdata *u) {
86 ssize_t r;
87 pa_assert(u);
88
89 if (u->buf_fill >= BUF_MAX) {
90 pa_log("read buffer overflow");
91 return -1;
92 }
93
94 if ((r = pa_read(u->fd, u->buf + u->buf_fill, BUF_MAX - u->buf_fill, &u->fd_type)) <= 0)
95 return -1;
96
97 u->buf_fill += (size_t) r;
98 return 0;
99 }
100
101 static int read_byte(struct userdata *u) {
102 int ret;
103 pa_assert(u);
104
105 if (u->buf_fill < 1)
106 if (fill_buf(u) < 0)
107 return -1;
108
109 ret = u->buf[0];
110 pa_assert(u->buf_fill > 0);
111 u->buf_fill--;
112 memmove(u->buf, u->buf+1, u->buf_fill);
113 return ret;
114 }
115
116 static char *read_string(struct userdata *u) {
117 pa_assert(u);
118
119 for (;;) {
120 char *e;
121
122 if ((e = memchr(u->buf, 0, u->buf_fill))) {
123 char *ret = pa_xstrdup(u->buf);
124 u->buf_fill -= (size_t) (e - u->buf +1);
125 memmove(u->buf, e+1, u->buf_fill);
126 return ret;
127 }
128
129 if (fill_buf(u) < 0)
130 return NULL;
131 }
132 }
133
134 static void unload_one_module(struct module_info *m, unsigned i) {
135 struct userdata *u;
136
137 pa_assert(m);
138 pa_assert(i < m->n_items);
139
140 u = m->userdata;
141
142 if (m->items[i].index == PA_INVALID_INDEX)
143 return;
144
145 pa_log_debug("Unloading module #%i", m->items[i].index);
146 pa_module_unload_by_index(u->core, m->items[i].index, true);
147 m->items[i].index = PA_INVALID_INDEX;
148 pa_xfree(m->items[i].name);
149 pa_xfree(m->items[i].args);
150 m->items[i].name = m->items[i].args = NULL;
151 }
152
153 static void unload_all_modules(struct module_info *m) {
154 unsigned i;
155
156 pa_assert(m);
157
158 for (i = 0; i < m->n_items; i++)
159 unload_one_module(m, i);
160
161 m->n_items = 0;
162 }
163
164 static void load_module(
165 struct module_info *m,
166 unsigned i,
167 const char *name,
168 const char *args,
169 bool is_new) {
170
171 struct userdata *u;
172 pa_module *mod;
173
174 pa_assert(m);
175 pa_assert(name);
176 pa_assert(args);
177
178 u = m->userdata;
179
180 if (!is_new) {
181 if (m->items[i].index != PA_INVALID_INDEX &&
182 pa_streq(m->items[i].name, name) &&
183 pa_streq(m->items[i].args, args))
184 return;
185
186 unload_one_module(m, i);
187 }
188
189 pa_log_debug("Loading module '%s' with args '%s' due to GConf configuration.", name, args);
190
191 m->items[i].name = pa_xstrdup(name);
192 m->items[i].args = pa_xstrdup(args);
193 m->items[i].index = PA_INVALID_INDEX;
194
195 if (!(mod = pa_module_load(u->core, name, args))) {
196 pa_log("pa_module_load() failed");
197 return;
198 }
199
200 m->items[i].index = mod->index;
201 }
202
203 static void module_info_free(void *p) {
204 struct module_info *m = p;
205
206 pa_assert(m);
207
208 unload_all_modules(m);
209 pa_xfree(m->name);
210 pa_xfree(m);
211 }
212
213 static int handle_event(struct userdata *u) {
214 int opcode;
215 int ret = 0;
216
217 do {
218 if ((opcode = read_byte(u)) < 0) {
219 if (errno == EINTR || errno == EAGAIN)
220 break;
221 goto fail;
222 }
223
224 switch (opcode) {
225 case '!':
226 /* The helper tool is now initialized */
227 ret = 1;
228 break;
229
230 case '+': {
231 char *name;
232 struct module_info *m;
233 unsigned i, j;
234
235 if (!(name = read_string(u)))
236 goto fail;
237
238 if (!(m = pa_hashmap_get(u->module_infos, name))) {
239 m = pa_xnew(struct module_info, 1);
240 m->userdata = u;
241 m->name = name;
242 m->n_items = 0;
243 pa_hashmap_put(u->module_infos, m->name, m);
244 } else
245 pa_xfree(name);
246
247 i = 0;
248 while (i < MAX_MODULES) {
249 char *module, *args;
250
251 if (!(module = read_string(u))) {
252 if (i > m->n_items) m->n_items = i;
253 goto fail;
254 }
255
256 if (!*module) {
257 pa_xfree(module);
258 break;
259 }
260
261 if (!(args = read_string(u))) {
262 pa_xfree(module);
263
264 if (i > m->n_items) m->n_items = i;
265 goto fail;
266 }
267
268 load_module(m, i, module, args, i >= m->n_items);
269
270 i++;
271
272 pa_xfree(module);
273 pa_xfree(args);
274 }
275
276 /* Unload all removed modules */
277 for (j = i; j < m->n_items; j++)
278 unload_one_module(m, j);
279
280 m->n_items = i;
281
282 break;
283 }
284
285 case '-': {
286 char *name;
287 struct module_info *m;
288
289 if (!(name = read_string(u)))
290 goto fail;
291
292 pa_hashmap_remove_and_free(u->module_infos, name);
293 pa_xfree(name);
294
295 break;
296 }
297 }
298 } while (u->buf_fill > 0 && ret == 0);
299
300 return ret;
301
302 fail:
303 pa_log("Unable to read or parse data from client.");
304 return -1;
305 }
306
307 static void io_event_cb(
308 pa_mainloop_api*a,
309 pa_io_event* e,
310 int fd,
311 pa_io_event_flags_t events,
312 void *userdata) {
313
314 struct userdata *u = userdata;
315
316 if (handle_event(u) < 0) {
317
318 if (u->io_event) {
319 u->core->mainloop->io_free(u->io_event);
320 u->io_event = NULL;
321 }
322
323 pa_module_unload_request(u->module, true);
324 }
325 }
326
327 int pa__init(pa_module*m) {
328 struct userdata *u;
329 int r;
330
331 u = pa_xnew(struct userdata, 1);
332 u->core = m->core;
333 u->module = m;
334 m->userdata = u;
335 u->module_infos = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, (pa_free_cb_t) module_info_free);
336 u->pid = (pid_t) -1;
337 u->fd = -1;
338 u->fd_type = 0;
339 u->io_event = NULL;
340 u->buf_fill = 0;
341
342 if ((u->fd = pa_start_child_for_read(
343 #if defined(__linux__) && !defined(__OPTIMIZE__)
344 pa_run_from_build_tree() ? PA_BUILDDIR "/gconf-helper" :
345 #endif
346 PA_GCONF_HELPER, NULL, &u->pid)) < 0)
347 goto fail;
348
349 u->io_event = m->core->mainloop->io_new(
350 m->core->mainloop,
351 u->fd,
352 PA_IO_EVENT_INPUT,
353 io_event_cb,
354 u);
355
356 do {
357 if ((r = handle_event(u)) < 0)
358 goto fail;
359
360 /* Read until the client signalled us that it is ready with
361 * initialization */
362 } while (r != 1);
363
364 return 0;
365
366 fail:
367 pa__done(m);
368 return -1;
369 }
370
371 void pa__done(pa_module*m) {
372 struct userdata *u;
373
374 pa_assert(m);
375
376 if (!(u = m->userdata))
377 return;
378
379 if (u->pid != (pid_t) -1) {
380 kill(u->pid, SIGTERM);
381
382 for (;;) {
383 if (waitpid(u->pid, NULL, 0) >= 0)
384 break;
385
386 if (errno != EINTR) {
387 pa_log("waitpid() failed: %s", pa_cstrerror(errno));
388 break;
389 }
390 }
391 }
392
393 if (u->io_event)
394 m->core->mainloop->io_free(u->io_event);
395
396 if (u->fd >= 0)
397 pa_close(u->fd);
398
399 if (u->module_infos)
400 pa_hashmap_free(u->module_infos);
401
402 pa_xfree(u);
403 }