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