4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
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.
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.
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
34 #include <sys/types.h>
38 #ifdef HAVE_SYS_PRCTL_H
39 #include <sys/prctl.h>
41 #ifdef HAVE_SYS_RESOURCE_H
42 #include <sys/resource.h>
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>
54 #include "module-gconf-symdef.h"
56 PA_MODULE_AUTHOR("Lennart Poettering")
57 PA_MODULE_DESCRIPTION("GConf Adapter")
58 PA_MODULE_VERSION(PACKAGE_VERSION)
61 #define MAX_MODULES 10
64 /* #undef PA_GCONF_HELPER */
65 /* #define PA_GCONF_HELPER "/home/lennart/projects/pulseaudio/src/gconf-helper" */
76 struct module_item items[MAX_MODULES];
84 pa_hashmap *module_infos;
90 pa_io_event *io_event;
96 static int fill_buf(struct userdata *u) {
100 if (u->buf_fill >= BUF_MAX) {
101 pa_log("read buffer overflow");
105 if ((r = pa_read(u->fd, u->buf + u->buf_fill, BUF_MAX - u->buf_fill, &u->fd_type)) <= 0)
112 static int read_byte(struct userdata *u) {
121 assert(u->buf_fill > 0);
123 memmove(u->buf, u->buf+1, u->buf_fill);
127 static char *read_string(struct userdata *u) {
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);
145 static void unload_one_module(struct userdata *u, struct module_info*m, unsigned i) {
148 assert(i < m->n_items);
150 if (m->items[i].index == PA_INVALID_INDEX)
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;
161 static void unload_all_modules(struct userdata *u, struct module_info*m) {
167 for (i = 0; i < m->n_items; i++)
168 unload_one_module(u, m, i);
173 static void load_module(
175 struct module_info *m,
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)
194 unload_one_module(u, m, i);
197 pa_log_debug("Loading module '%s' with args '%s' due to GConf configuration.", name, args);
199 m->items[i].name = pa_xstrdup(name);
200 m->items[i].args = pa_xstrdup(args);
201 m->items[i].index = PA_INVALID_INDEX;
203 if (!(mod = pa_module_load(u->core, name, args))) {
204 pa_log("pa_module_load() failed");
208 m->items[i].index = mod->index;
211 static void module_info_free(void *p, void *userdata) {
212 struct module_info *m = p;
213 struct userdata *u = userdata;
218 unload_all_modules(u, m);
223 static int handle_event(struct userdata *u) {
228 if ((opcode = read_byte(u)) < 0)
233 /* The helper tool is now initialized */
239 struct module_info *m;
242 if (!(name = read_string(u)))
245 if (!(m = pa_hashmap_get(u->module_infos, name))) {
246 m = pa_xnew(struct module_info, 1);
249 pa_hashmap_put(u->module_infos, m->name, m);
254 while (i < MAX_MODULES) {
257 if (!(module = read_string(u))) {
258 if (i > m->n_items) m->n_items = i;
267 if (!(args = read_string(u))) {
270 if (i > m->n_items) m->n_items = i;
274 load_module(u, m, i, module, args, i >= m->n_items);
282 /* Unload all removed modules */
283 for (j = i; j < m->n_items; j++)
284 unload_one_module(u, m, j);
293 struct module_info *m;
295 if (!(name = read_string(u)))
298 if ((m = pa_hashmap_get(u->module_infos, name))) {
299 pa_hashmap_remove(u->module_infos, name);
300 module_info_free(m, u);
308 } while (u->buf_fill > 0 && ret == 0);
313 pa_log("Unable to read or parse data from client.");
317 static void io_event_cb(
321 pa_io_event_flags_t events,
324 struct userdata *u = userdata;
326 if (handle_event(u) < 0) {
329 u->core->mainloop->io_free(u->io_event);
333 pa_module_unload_request(u->module);
337 static int start_client(const char *n, pid_t *pid) {
339 int pipe_fds[2] = { -1, -1 };
341 if (pipe(pipe_fds) < 0) {
342 pa_log("pipe() failed: %s", pa_cstrerror(errno));
346 if ((child = fork()) == (pid_t) -1) {
347 pa_log("fork() failed: %s", pa_cstrerror(errno));
349 } else if (child != 0) {
364 dup2(pipe_fds[1], 1);
366 if (pipe_fds[1] != 1)
370 open("/dev/null", O_RDONLY);
373 open("/dev/null", O_WRONLY);
377 #ifdef HAVE_SYS_RESOURCE_H
380 if (getrlimit(RLIMIT_NOFILE, &r) == 0)
385 for (i = 3; i < max_fd; i++)
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) */
394 prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
398 /* Make sure that SIGPIPE kills the child process */
399 signal(SIGPIPE, SIG_DFL);
407 if (pipe_fds[0] >= 0)
410 if (pipe_fds[1] >= 0)
416 int pa__init(pa_core *c, pa_module*m) {
420 u = pa_xnew(struct userdata, 1);
424 u->module_infos = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
431 if ((u->fd = start_client(PA_GCONF_HELPER, &u->pid)) < 0)
434 u->io_event = c->mainloop->io_new(
442 if ((r = handle_event(u)) < 0)
445 /* Read until the client signalled us that it is ready with
456 void pa__done(pa_core *c, pa_module*m) {
462 if (!(u = m->userdata))
466 c->mainloop->io_free(u->io_event);
471 if (u->pid != (pid_t) -1) {
472 kill(u->pid, SIGTERM);
473 waitpid(u->pid, NULL, 0);
477 pa_hashmap_free(u->module_infos, module_info_free, u);