4 This file is part of PulseAudio.
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.
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.
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
32 #include <sys/types.h>
36 #ifdef HAVE_SYS_PRCTL_H
37 #include <sys/prctl.h>
39 #ifdef HAVE_SYS_RESOURCE_H
40 #include <sys/resource.h>
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>
52 #include "module-gconf-symdef.h"
54 PA_MODULE_AUTHOR("Lennart Poettering")
55 PA_MODULE_DESCRIPTION("GConf Adapter")
56 PA_MODULE_VERSION(PACKAGE_VERSION
)
59 #define MAX_MODULES 10
62 /* #undef PA_GCONF_HELPER */
63 /* #define PA_GCONF_HELPER "/home/lennart/projects/pulseaudio/src/gconf-helper" */
74 struct module_item items
[MAX_MODULES
];
82 pa_hashmap
*module_infos
;
88 pa_io_event
*io_event
;
94 static int fill_buf(struct userdata
*u
) {
98 if (u
->buf_fill
>= BUF_MAX
) {
99 pa_log(__FILE__
": read buffer overflow");
103 if ((r
= pa_read(u
->fd
, u
->buf
+ u
->buf_fill
, BUF_MAX
- u
->buf_fill
, &u
->fd_type
)) <= 0)
110 static int read_byte(struct userdata
*u
) {
119 assert(u
->buf_fill
> 0);
121 memmove(u
->buf
, u
->buf
+1, u
->buf_fill
);
125 static char *read_string(struct userdata
*u
) {
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
);
143 static void unload_one_module(struct userdata
*u
, struct module_info
*m
, unsigned i
) {
146 assert(i
< m
->n_items
);
148 if (m
->items
[i
].index
== PA_INVALID_INDEX
)
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
;
159 static void unload_all_modules(struct userdata
*u
, struct module_info
*m
) {
165 for (i
= 0; i
< m
->n_items
; i
++)
166 unload_one_module(u
, m
, i
);
171 static void load_module(
173 struct module_info
*m
,
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)
192 unload_one_module(u
, m
, i
);
195 pa_log_debug(__FILE__
": Loading module '%s' with args '%s' due to GConf configuration.", name
, args
);
197 m
->items
[i
].name
= pa_xstrdup(name
);
198 m
->items
[i
].args
= pa_xstrdup(args
);
199 m
->items
[i
].index
= PA_INVALID_INDEX
;
201 if (!(mod
= pa_module_load(u
->core
, name
, args
))) {
202 pa_log(__FILE__
": pa_module_load() failed");
206 m
->items
[i
].index
= mod
->index
;
209 static void module_info_free(void *p
, void *userdata
) {
210 struct module_info
*m
= p
;
211 struct userdata
*u
= userdata
;
216 unload_all_modules(u
, m
);
221 static int handle_event(struct userdata
*u
) {
226 if ((opcode
= read_byte(u
)) < 0)
231 /* The helper tool is now initialized */
237 struct module_info
*m
;
240 if (!(name
= read_string(u
)))
243 if (!(m
= pa_hashmap_get(u
->module_infos
, name
))) {
244 m
= pa_xnew(struct module_info
, 1);
247 pa_hashmap_put(u
->module_infos
, m
->name
, m
);
252 while (i
< MAX_MODULES
) {
255 if (!(module
= read_string(u
))) {
256 if (i
> m
->n_items
) m
->n_items
= i
;
265 if (!(args
= read_string(u
))) {
268 if (i
> m
->n_items
) m
->n_items
= i
;
272 load_module(u
, m
, i
, module
, args
, i
>= m
->n_items
);
280 /* Unload all removed modules */
281 for (j
= i
; j
< m
->n_items
; j
++)
282 unload_one_module(u
, m
, j
);
291 struct module_info
*m
;
293 if (!(name
= read_string(u
)))
296 if ((m
= pa_hashmap_get(u
->module_infos
, name
))) {
297 pa_hashmap_remove(u
->module_infos
, name
);
298 module_info_free(m
, u
);
306 } while (u
->buf_fill
> 0 && ret
== 0);
311 pa_log(__FILE__
": Unable to read or parse data from client.");
315 static void io_event_cb(
319 pa_io_event_flags_t events
,
322 struct userdata
*u
= userdata
;
324 if (handle_event(u
) < 0) {
327 u
->core
->mainloop
->io_free(u
->io_event
);
331 pa_module_unload_request(u
->module
);
335 static int start_client(const char *n
, pid_t
*pid
) {
337 int pipe_fds
[2] = { -1, -1 };
339 if (pipe(pipe_fds
) < 0) {
340 pa_log(__FILE__
": pipe() failed: %s", pa_cstrerror(errno
));
344 if ((child
= fork()) == (pid_t
) -1) {
345 pa_log(__FILE__
": fork() failed: %s", pa_cstrerror(errno
));
347 } else if (child
!= 0) {
362 dup2(pipe_fds
[1], 1);
364 if (pipe_fds
[1] != 1)
368 open("/dev/null", O_RDONLY
);
371 open("/dev/null", O_WRONLY
);
375 #ifdef HAVE_SYS_RESOURCE_H
378 if (getrlimit(RLIMIT_NOFILE
, &r
) == 0)
383 for (i
= 3; i
< max_fd
; i
++)
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) */
392 prctl(PR_SET_PDEATHSIG
, SIGTERM
, 0, 0, 0);
396 /* Make sure that SIGPIPE kills the child process */
397 signal(SIGPIPE
, SIG_DFL
);
405 if (pipe_fds
[0] >= 0)
408 if (pipe_fds
[1] >= 0)
414 int pa__init(pa_core
*c
, pa_module
*m
) {
418 u
= pa_xnew(struct userdata
, 1);
422 u
->module_infos
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
429 if ((u
->fd
= start_client(PA_GCONF_HELPER
, &u
->pid
)) < 0)
432 u
->io_event
= c
->mainloop
->io_new(
440 if ((r
= handle_event(u
)) < 0)
443 /* Read until the client signalled us that it is ready with
454 void pa__done(pa_core
*c
, pa_module
*m
) {
460 if (!(u
= m
->userdata
))
464 c
->mainloop
->io_free(u
->io_event
);
469 if (u
->pid
!= (pid_t
) -1) {
470 kill(u
->pid
, SIGTERM
);
471 waitpid(u
->pid
, NULL
, 0);
475 pa_hashmap_free(u
->module_infos
, module_info_free
, u
);