]> code.delx.au - pulseaudio/blob - src/modules/gconf/module-gconf.c
merge 'lennart' branch back into trunk.
[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 goto fail;
230
231 switch (opcode) {
232 case '!':
233 /* The helper tool is now initialized */
234 ret = 1;
235 break;
236
237 case '+': {
238 char *name;
239 struct module_info *m;
240 unsigned i, j;
241
242 if (!(name = read_string(u)))
243 goto fail;
244
245 if (!(m = pa_hashmap_get(u->module_infos, name))) {
246 m = pa_xnew(struct module_info, 1);
247 m->name = name;
248 m->n_items = 0;
249 pa_hashmap_put(u->module_infos, m->name, m);
250 } else
251 pa_xfree(name);
252
253 i = 0;
254 while (i < MAX_MODULES) {
255 char *module, *args;
256
257 if (!(module = read_string(u))) {
258 if (i > m->n_items) m->n_items = i;
259 goto fail;
260 }
261
262 if (!*module) {
263 pa_xfree(module);
264 break;
265 }
266
267 if (!(args = read_string(u))) {
268 pa_xfree(module);
269
270 if (i > m->n_items) m->n_items = i;
271 goto fail;
272 }
273
274 load_module(u, m, i, module, args, i >= m->n_items);
275
276 i++;
277
278 pa_xfree(module);
279 pa_xfree(args);
280 }
281
282 /* Unload all removed modules */
283 for (j = i; j < m->n_items; j++)
284 unload_one_module(u, m, j);
285
286 m->n_items = i;
287
288 break;
289 }
290
291 case '-': {
292 char *name;
293 struct module_info *m;
294
295 if (!(name = read_string(u)))
296 goto fail;
297
298 if ((m = pa_hashmap_get(u->module_infos, name))) {
299 pa_hashmap_remove(u->module_infos, name);
300 module_info_free(m, u);
301 }
302
303 pa_xfree(name);
304
305 break;
306 }
307 }
308 } while (u->buf_fill > 0 && ret == 0);
309
310 return ret;
311
312 fail:
313 pa_log("Unable to read or parse data from client.");
314 return -1;
315 }
316
317 static void io_event_cb(
318 pa_mainloop_api*a,
319 pa_io_event* e,
320 int fd,
321 pa_io_event_flags_t events,
322 void *userdata) {
323
324 struct userdata *u = userdata;
325
326 if (handle_event(u) < 0) {
327
328 if (u->io_event) {
329 u->core->mainloop->io_free(u->io_event);
330 u->io_event = NULL;
331 }
332
333 pa_module_unload_request(u->module);
334 }
335 }
336
337 static int start_client(const char *n, pid_t *pid) {
338 pid_t child;
339 int pipe_fds[2] = { -1, -1 };
340
341 if (pipe(pipe_fds) < 0) {
342 pa_log("pipe() failed: %s", pa_cstrerror(errno));
343 goto fail;
344 }
345
346 if ((child = fork()) == (pid_t) -1) {
347 pa_log("fork() failed: %s", pa_cstrerror(errno));
348 goto fail;
349 } else if (child != 0) {
350
351 /* Parent */
352 close(pipe_fds[1]);
353
354 if (pid)
355 *pid = child;
356
357 return pipe_fds[0];
358 } else {
359 #ifdef __linux__
360 DIR* d;
361 #endif
362 int max_fd, i;
363 /* child */
364
365 close(pipe_fds[0]);
366 dup2(pipe_fds[1], 1);
367
368 if (pipe_fds[1] != 1)
369 close(pipe_fds[1]);
370
371 close(0);
372 open("/dev/null", O_RDONLY);
373
374 close(2);
375 open("/dev/null", O_WRONLY);
376
377 #ifdef __linux__
378
379 if ((d = opendir("/proc/self/fd/"))) {
380
381 struct dirent *de;
382
383 while ((de = readdir(d))) {
384 char *e = NULL;
385 int fd;
386
387 if (de->d_name[0] == '.')
388 continue;
389
390 errno = 0;
391 fd = strtol(de->d_name, &e, 10);
392 pa_assert(errno == 0 && e && *e == 0);
393
394 if (fd >= 3 && dirfd(d) != fd)
395 close(fd);
396 }
397
398 closedir(d);
399 } else {
400
401 #endif
402
403 max_fd = 1024;
404
405 #ifdef HAVE_SYS_RESOURCE_H
406 {
407 struct rlimit r;
408 if (getrlimit(RLIMIT_NOFILE, &r) == 0)
409 max_fd = r.rlim_max;
410 }
411 #endif
412
413 for (i = 3; i < max_fd; i++)
414 close(i);
415 #
416 #ifdef __linux__
417 }
418 #endif
419
420 #ifdef PR_SET_PDEATHSIG
421 /* On Linux we can use PR_SET_PDEATHSIG to have the helper
422 process killed when the daemon dies abnormally. On non-Linux
423 machines the client will die as soon as it writes data to
424 stdout again (SIGPIPE) */
425
426 prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
427 #endif
428
429 #ifdef SIGPIPE
430 /* Make sure that SIGPIPE kills the child process */
431 signal(SIGPIPE, SIG_DFL);
432 #endif
433
434 execl(n, n, NULL);
435 _exit(1);
436 }
437
438 fail:
439 if (pipe_fds[0] >= 0)
440 close(pipe_fds[0]);
441
442 if (pipe_fds[1] >= 0)
443 close(pipe_fds[1]);
444
445 return -1;
446 }
447
448 int pa__init(pa_module*m) {
449 struct userdata *u;
450 int r;
451
452 u = pa_xnew(struct userdata, 1);
453 u->core = m->core;
454 u->module = m;
455 m->userdata = u;
456 u->module_infos = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
457 u->pid = (pid_t) -1;
458 u->fd = -1;
459 u->fd_type = 0;
460 u->io_event = NULL;
461 u->buf_fill = 0;
462
463 if ((u->fd = start_client(PA_GCONF_HELPER, &u->pid)) < 0)
464 goto fail;
465
466 u->io_event = m->core->mainloop->io_new(
467 m->core->mainloop,
468 u->fd,
469 PA_IO_EVENT_INPUT,
470 io_event_cb,
471 u);
472
473 do {
474 if ((r = handle_event(u)) < 0)
475 goto fail;
476
477 /* Read until the client signalled us that it is ready with
478 * initialization */
479 } while (r != 1);
480
481 return 0;
482
483 fail:
484 pa__done(m);
485 return -1;
486 }
487
488 void pa__done(pa_module*m) {
489 struct userdata *u;
490
491 pa_assert(m);
492
493 if (!(u = m->userdata))
494 return;
495
496 if (u->io_event)
497 m->core->mainloop->io_free(u->io_event);
498
499 if (u->fd >= 0)
500 close(u->fd);
501
502 if (u->pid != (pid_t) -1) {
503 kill(u->pid, SIGTERM);
504 waitpid(u->pid, NULL, 0);
505 }
506
507 if (u->module_infos)
508 pa_hashmap_free(u->module_infos, module_info_free, u);
509
510 pa_xfree(u);
511 }
512