]> code.delx.au - pulseaudio/blob - src/modules/gconf/module-gconf.c
fix module-gconf initialization
[pulseaudio] / src / modules / gconf / module-gconf.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
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 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 <assert.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <signal.h>
32 #include <sys/types.h>
33 #include <sys/wait.h>
34 #include <fcntl.h>
35
36 #ifdef HAVE_SYS_PRCTL_H
37 #include <sys/prctl.h>
38 #endif
39 #ifdef HAVE_SYS_RESOURCE_H
40 #include <sys/resource.h>
41 #endif
42
43 #include <pulsecore/module.h>
44 #include <pulsecore/core.h>
45 #include <pulsecore/llist.h>
46 #include <pulsecore/core-util.h>
47 #include <pulsecore/log.h>
48 #include <pulse/mainloop-api.h>
49 #include <pulse/xmalloc.h>
50 #include <pulsecore/core-error.h>
51
52 #include "module-gconf-symdef.h"
53
54 PA_MODULE_AUTHOR("Lennart Poettering")
55 PA_MODULE_DESCRIPTION("GConf Adapter")
56 PA_MODULE_VERSION(PACKAGE_VERSION)
57 PA_MODULE_USAGE("")
58
59 #define MAX_MODULES 10
60 #define BUF_MAX 2048
61
62 /* #undef PA_GCONF_HELPER */
63 /* #define PA_GCONF_HELPER "/home/lennart/projects/pulseaudio/src/gconf-helper" */
64
65 struct module_item {
66 char *name;
67 char *args;
68 uint32_t index;
69 };
70
71 struct module_info {
72 char *name;
73
74 struct module_item items[MAX_MODULES];
75 unsigned n_items;
76 };
77
78 struct userdata {
79 pa_core *core;
80 pa_module *module;
81
82 pa_hashmap *module_infos;
83
84 pid_t pid;
85
86 int fd;
87 int fd_type;
88 pa_io_event *io_event;
89
90 char buf[BUF_MAX];
91 size_t buf_fill;
92 };
93
94 static int fill_buf(struct userdata *u) {
95 ssize_t r;
96 assert(u);
97
98 if (u->buf_fill >= BUF_MAX) {
99 pa_log(__FILE__": read buffer overflow");
100 return -1;
101 }
102
103 if ((r = pa_read(u->fd, u->buf + u->buf_fill, BUF_MAX - u->buf_fill, &u->fd_type)) <= 0)
104 return -1;
105
106 u->buf_fill += r;
107 return 0;
108 }
109
110 static int read_byte(struct userdata *u) {
111 int ret;
112 assert(u);
113
114 if (u->buf_fill < 1)
115 if (fill_buf(u) < 0)
116 return -1;
117
118 ret = u->buf[0];
119 assert(u->buf_fill > 0);
120 u->buf_fill--;
121 memmove(u->buf, u->buf+1, u->buf_fill);
122 return ret;
123 }
124
125 static char *read_string(struct userdata *u) {
126 assert(u);
127
128 for (;;) {
129 char *e;
130
131 if ((e = memchr(u->buf, 0, u->buf_fill))) {
132 char *ret = pa_xstrdup(u->buf);
133 u->buf_fill -= e - u->buf +1;
134 memmove(u->buf, e+1, u->buf_fill);
135 return ret;
136 }
137
138 if (fill_buf(u) < 0)
139 return NULL;
140 }
141 }
142
143 static void unload_one_module(struct userdata *u, struct module_info*m, unsigned i) {
144 assert(u);
145 assert(m);
146 assert(i < m->n_items);
147
148 if (m->items[i].index == PA_INVALID_INDEX)
149 return;
150
151 pa_log_debug(__FILE__": Unloading module #%i", m->items[i].index);
152 pa_module_unload_by_index(u->core, m->items[i].index);
153 m->items[i].index = PA_INVALID_INDEX;
154 pa_xfree(m->items[i].name);
155 pa_xfree(m->items[i].args);
156 m->items[i].name = m->items[i].args = NULL;
157 }
158
159 static void unload_all_modules(struct userdata *u, struct module_info*m) {
160 unsigned i;
161
162 assert(u);
163 assert(m);
164
165 for (i = 0; i < m->n_items; i++)
166 unload_one_module(u, m, i);
167
168 m->n_items = 0;
169 }
170
171 static void load_module(
172 struct userdata *u,
173 struct module_info *m,
174 int i,
175 const char *name,
176 const char *args,
177 int is_new) {
178
179 pa_module *mod;
180
181 assert(u);
182 assert(m);
183 assert(name);
184 assert(args);
185
186 if (!is_new) {
187 if (m->items[i].index != PA_INVALID_INDEX &&
188 strcmp(m->items[i].name, name) == 0 &&
189 strcmp(m->items[i].args, args) == 0)
190 return;
191
192 unload_one_module(u, m, i);
193 }
194
195 pa_log_debug(__FILE__": Loading module '%s' with args '%s' due to GConf configuration.", name, args);
196
197 m->items[i].name = pa_xstrdup(name);
198 m->items[i].args = pa_xstrdup(args);
199 m->items[i].index = PA_INVALID_INDEX;
200
201 if (!(mod = pa_module_load(u->core, name, args))) {
202 pa_log(__FILE__": pa_module_load() failed");
203 return;
204 }
205
206 m->items[i].index = mod->index;
207 }
208
209 static void module_info_free(void *p, void *userdata) {
210 struct module_info *m = p;
211 struct userdata *u = userdata;
212
213 assert(m);
214 assert(u);
215
216 unload_all_modules(u, m);
217 pa_xfree(m->name);
218 pa_xfree(m);
219 }
220
221 static int handle_event(struct userdata *u) {
222 int opcode;
223 int ret = 0;
224
225 do {
226 if ((opcode = read_byte(u)) < 0)
227 goto fail;
228
229 switch (opcode) {
230 case '!':
231 /* The helper tool is now initialized */
232 ret = 1;
233 break;
234
235 case '+': {
236 char *name;
237 struct module_info *m;
238 unsigned i, j;
239
240 if (!(name = read_string(u)))
241 goto fail;
242
243 if (!(m = pa_hashmap_get(u->module_infos, name))) {
244 m = pa_xnew(struct module_info, 1);
245 m->name = name;
246 m->n_items = 0;
247 pa_hashmap_put(u->module_infos, m->name, m);
248 } else
249 pa_xfree(name);
250
251 i = 0;
252 while (i < MAX_MODULES) {
253 char *module, *args;
254
255 if (!(module = read_string(u))) {
256 if (i > m->n_items) m->n_items = i;
257 goto fail;
258 }
259
260 if (!*module) {
261 pa_xfree(module);
262 break;
263 }
264
265 if (!(args = read_string(u))) {
266 pa_xfree(module);
267
268 if (i > m->n_items) m->n_items = i;
269 goto fail;
270 }
271
272 load_module(u, m, i, module, args, i >= m->n_items);
273
274 i++;
275
276 pa_xfree(module);
277 pa_xfree(args);
278 }
279
280 /* Unload all removed modules */
281 for (j = i; j < m->n_items; j++)
282 unload_one_module(u, m, j);
283
284 m->n_items = i;
285
286 break;
287 }
288
289 case '-': {
290 char *name;
291 struct module_info *m;
292
293 if (!(name = read_string(u)))
294 goto fail;
295
296 if ((m = pa_hashmap_get(u->module_infos, name))) {
297 pa_hashmap_remove(u->module_infos, name);
298 module_info_free(m, u);
299 }
300
301 pa_xfree(name);
302
303 break;
304 }
305 }
306 } while (u->buf_fill > 0 && ret == 0);
307
308 return ret;
309
310 fail:
311 pa_log(__FILE__": Unable to read or parse data from client.");
312 return -1;
313 }
314
315 static void io_event_cb(
316 pa_mainloop_api*a,
317 pa_io_event* e,
318 int fd,
319 pa_io_event_flags_t events,
320 void *userdata) {
321
322 struct userdata *u = userdata;
323
324 if (handle_event(u) < 0) {
325
326 if (u->io_event) {
327 u->core->mainloop->io_free(u->io_event);
328 u->io_event = NULL;
329 }
330
331 pa_module_unload_request(u->module);
332 }
333 }
334
335 static int start_client(const char *n, pid_t *pid) {
336 pid_t child;
337 int pipe_fds[2] = { -1, -1 };
338
339 if (pipe(pipe_fds) < 0) {
340 pa_log(__FILE__": pipe() failed: %s", pa_cstrerror(errno));
341 goto fail;
342 }
343
344 if ((child = fork()) == (pid_t) -1) {
345 pa_log(__FILE__": fork() failed: %s", pa_cstrerror(errno));
346 goto fail;
347 } else if (child != 0) {
348
349 /* Parent */
350 close(pipe_fds[1]);
351
352 if (pid)
353 *pid = child;
354
355 return pipe_fds[0];
356 } else {
357 int max_fd, i;
358
359 /* child */
360
361 close(pipe_fds[0]);
362 dup2(pipe_fds[1], 1);
363
364 if (pipe_fds[1] != 1)
365 close(pipe_fds[1]);
366
367 close(0);
368 open("/dev/null", O_RDONLY);
369
370 close(2);
371 open("/dev/null", O_WRONLY);
372
373 max_fd = 1024;
374
375 #ifdef HAVE_SYS_RESOURCE_H
376 {
377 struct rlimit r;
378 if (getrlimit(RLIMIT_NOFILE, &r) == 0)
379 max_fd = r.rlim_max;
380 }
381 #endif
382
383 for (i = 3; i < max_fd; i++)
384 close(i);
385
386 #ifdef PR_SET_PDEATHSIG
387 /* On Linux we can use PR_SET_PDEATHSIG to have the helper
388 process killed when the daemon dies abnormally. On non-Linux
389 machines the client will die as soon as it writes data to
390 stdout again (SIGPIPE) */
391
392 prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
393 #endif
394
395 #ifdef SIGPIPE
396 /* Make sure that SIGPIPE kills the child process */
397 signal(SIGPIPE, SIG_DFL);
398 #endif
399
400 execl(n, n, NULL);
401 _exit(1);
402 }
403
404 fail:
405 if (pipe_fds[0] >= 0)
406 close(pipe_fds[0]);
407
408 if (pipe_fds[1] >= 0)
409 close(pipe_fds[1]);
410
411 return -1;
412 }
413
414 int pa__init(pa_core *c, pa_module*m) {
415 struct userdata *u;
416 int r;
417
418 u = pa_xnew(struct userdata, 1);
419 u->core = c;
420 u->module = m;
421 m->userdata = u;
422 u->module_infos = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
423 u->pid = (pid_t) -1;
424 u->fd = -1;
425 u->fd_type = 0;
426 u->io_event = NULL;
427 u->buf_fill = 0;
428
429 if ((u->fd = start_client(PA_GCONF_HELPER, &u->pid)) < 0)
430 goto fail;
431
432 u->io_event = c->mainloop->io_new(
433 c->mainloop,
434 u->fd,
435 PA_IO_EVENT_INPUT,
436 io_event_cb,
437 u);
438
439 do {
440 if ((r = handle_event(u)) < 0)
441 goto fail;
442
443 /* Read until the client signalled us that it is ready with
444 * initialization */
445 } while (r != 1);
446
447 return 0;
448
449 fail:
450 pa__done(c, m);
451 return -1;
452 }
453
454 void pa__done(pa_core *c, pa_module*m) {
455 struct userdata *u;
456
457 assert(c);
458 assert(m);
459
460 if (!(u = m->userdata))
461 return;
462
463 if (u->io_event)
464 c->mainloop->io_free(u->io_event);
465
466 if (u->fd >= 0)
467 close(u->fd);
468
469 if (u->pid != (pid_t) -1) {
470 kill(u->pid, SIGTERM);
471 waitpid(u->pid, NULL, 0);
472 }
473
474 if (u->module_infos)
475 pa_hashmap_free(u->module_infos, module_info_free, u);
476
477 pa_xfree(u);
478 }
479