Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / 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 <assert.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <signal.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <fcntl.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     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     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     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     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     assert(u);
147     assert(m);
148     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     assert(u);
165     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     assert(u);
184     assert(m);
185     assert(name);
186     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     assert(m);
216     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         int max_fd, i;
360         
361         /* child */
362
363         close(pipe_fds[0]);
364         dup2(pipe_fds[1], 1);
365
366         if (pipe_fds[1] != 1)
367             close(pipe_fds[1]);
368
369         close(0);
370         open("/dev/null", O_RDONLY);
371
372         close(2);
373         open("/dev/null", O_WRONLY);
374
375         max_fd = 1024;
376         
377 #ifdef HAVE_SYS_RESOURCE_H
378         {
379             struct rlimit r;
380             if (getrlimit(RLIMIT_NOFILE, &r) == 0)
381                 max_fd = r.rlim_max;
382         }
383 #endif
384                 
385         for (i = 3; i < max_fd; i++)
386             close(i);
387
388 #ifdef PR_SET_PDEATHSIG
389         /* On Linux we can use PR_SET_PDEATHSIG to have the helper
390         process killed when the daemon dies abnormally. On non-Linux
391         machines the client will die as soon as it writes data to
392         stdout again (SIGPIPE) */
393
394         prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
395 #endif
396
397 #ifdef SIGPIPE
398         /* Make sure that SIGPIPE kills the child process */
399         signal(SIGPIPE, SIG_DFL);
400 #endif
401
402         execl(n, n, NULL);
403         _exit(1);
404     }
405     
406 fail:
407     if (pipe_fds[0] >= 0)
408         close(pipe_fds[0]);
409
410     if (pipe_fds[1] >= 0)
411         close(pipe_fds[1]);
412     
413     return -1;
414 }
415
416 int pa__init(pa_core *c, pa_module*m) {
417     struct userdata *u;
418     int r;
419
420     u = pa_xnew(struct userdata, 1);
421     u->core = c;
422     u->module = m;
423     m->userdata = u;
424     u->module_infos = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
425     u->pid = (pid_t) -1;
426     u->fd = -1;
427     u->fd_type = 0;
428     u->io_event = NULL;
429     u->buf_fill = 0;
430     
431     if ((u->fd = start_client(PA_GCONF_HELPER, &u->pid)) < 0)
432         goto fail;
433     
434     u->io_event = c->mainloop->io_new(
435             c->mainloop,
436             u->fd,
437             PA_IO_EVENT_INPUT,
438             io_event_cb,
439             u);
440     
441     do {
442         if ((r = handle_event(u)) < 0)
443             goto fail;
444
445         /* Read until the client signalled us that it is ready with
446          * initialization */
447     } while (r != 1);
448         
449     return 0;
450
451 fail:
452     pa__done(c, m);
453     return -1;
454 }
455
456 void pa__done(pa_core *c, pa_module*m) {
457     struct userdata *u;
458
459     assert(c);
460     assert(m);
461
462     if (!(u = m->userdata))
463         return;
464
465     if (u->io_event)
466         c->mainloop->io_free(u->io_event);
467
468     if (u->fd >= 0)
469         close(u->fd);
470
471     if (u->pid != (pid_t) -1) {
472         kill(u->pid, SIGTERM);
473         waitpid(u->pid, NULL, 0);
474     }
475
476     if (u->module_infos)
477         pa_hashmap_free(u->module_infos, module_info_free, u);
478
479     pa_xfree(u);
480 }
481