]> code.delx.au - pulseaudio/blob - polyp/main.c
* add new module module-mmkbd-evdev
[pulseaudio] / polyp / main.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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.
10
11 polypaudio 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.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <signal.h>
32 #include <stddef.h>
33 #include <assert.h>
34 #include <ltdl.h>
35 #include <memblock.h>
36 #include <limits.h>
37 #include <fcntl.h>
38 #include <sys/ioctl.h>
39
40 #ifdef HAVE_LIBWRAP
41 #include <syslog.h>
42 #include <tcpd.h>
43 #endif
44
45 #include "core.h"
46 #include "mainloop.h"
47 #include "module.h"
48 #include "mainloop-signal.h"
49 #include "cmdline.h"
50 #include "cli-command.h"
51 #include "util.h"
52 #include "sioman.h"
53 #include "xmalloc.h"
54 #include "cpulimit.h"
55 #include "log.h"
56 #include "daemon-conf.h"
57 #include "dumpmodules.h"
58 #include "caps.h"
59 #include "cli-text.h"
60 #include "pid.h"
61
62 #ifdef HAVE_LIBWRAP
63 /* Only one instance of these variables */
64 int allow_severity = LOG_INFO;
65 int deny_severity = LOG_WARNING;
66 #endif
67
68 static void signal_callback(struct pa_mainloop_api*m, struct pa_signal_event *e, int sig, void *userdata) {
69 pa_log_info(__FILE__": Got signal %s.\n", pa_strsignal(sig));
70
71 switch (sig) {
72 case SIGUSR1:
73 pa_module_load(userdata, "module-cli", NULL);
74 return;
75
76 case SIGUSR2:
77 pa_module_load(userdata, "module-cli-protocol-unix", NULL);
78 return;
79
80 case SIGHUP: {
81 int i;
82
83 for (i = 0;; i++) {
84 char *c;
85 switch (i) {
86 case 0:
87 c = pa_sink_list_to_string(userdata);
88 break;
89 case 1:
90 c = pa_source_list_to_string(userdata);
91 break;
92 case 2:
93 c = pa_sink_input_list_to_string(userdata);
94 break;
95 case 3:
96 c = pa_source_output_list_to_string(userdata);
97 break;
98 case 4:
99 c = pa_client_list_to_string(userdata);
100 break;
101 case 5:
102 c = pa_module_list_to_string(userdata);
103 break;
104 case 6:
105 c = pa_scache_list_to_string(userdata);
106 break;
107 case 7:
108 c = pa_autoload_list_to_string(userdata);
109 break;
110 default:
111 return;
112 }
113 pa_log_notice(c);
114 pa_xfree(c);
115 }
116
117 return;
118 }
119
120 case SIGINT:
121 case SIGTERM:
122 default:
123 pa_log_info(__FILE__": Exiting.\n");
124 m->quit(m, 1);
125 return;
126 }
127 }
128
129 static void close_pipe(int p[2]) {
130 if (p[0] != -1)
131 close(p[0]);
132 if (p[1] != -1)
133 close(p[1]);
134 p[0] = p[1] = -1;
135 }
136
137 int main(int argc, char *argv[]) {
138 struct pa_core *c;
139 struct pa_strbuf *buf = NULL;
140 struct pa_daemon_conf *conf;
141 struct pa_mainloop *mainloop;
142
143 char *s;
144 int r, retval = 1, d = 0;
145 int daemon_pipe[2] = { -1, -1 };
146 gid_t gid = (gid_t) -1;
147 int suid_root;
148 int valid_pid_file = 0;
149
150 pa_limit_caps();
151
152 suid_root = getuid() != 0 && geteuid() == 0;
153
154 if (suid_root && (pa_uid_in_group("realtime", &gid) <= 0 || gid >= 1000)) {
155 pa_log_warn(__FILE__": WARNING: called SUID root, but not in group 'realtime'.\n");
156 pa_drop_root();
157 }
158
159 LTDL_SET_PRELOADED_SYMBOLS();
160
161 r = lt_dlinit();
162 assert(r == 0);
163
164 pa_log_set_ident("polypaudio");
165
166 conf = pa_daemon_conf_new();
167
168 if (pa_daemon_conf_load(conf, NULL) < 0)
169 goto finish;
170
171 if (pa_daemon_conf_env(conf) < 0)
172 goto finish;
173
174 if (pa_cmdline_parse(conf, argc, argv, &d) < 0) {
175 pa_log(__FILE__": failed to parse command line.\n");
176 goto finish;
177 }
178
179 pa_log_set_maximal_level(conf->log_level);
180 pa_log_set_target(conf->auto_log_target ? PA_LOG_STDERR : conf->log_target, NULL);
181
182 if (conf->high_priority && conf->cmd == PA_CMD_DAEMON)
183 pa_raise_priority();
184
185 pa_drop_caps();
186
187 if (suid_root)
188 pa_drop_root();
189
190 if (conf->dl_search_path)
191 lt_dlsetsearchpath(conf->dl_search_path);
192
193 switch (conf->cmd) {
194 case PA_CMD_DUMP_MODULES:
195 pa_dump_modules(conf, argc-d, argv+d);
196 retval = 0;
197 goto finish;
198
199 case PA_CMD_DUMP_CONF: {
200 char *s = pa_daemon_conf_dump(conf);
201 fputs(s, stdout);
202 pa_xfree(s);
203 retval = 0;
204 goto finish;
205 }
206
207 case PA_CMD_HELP :
208 pa_cmdline_help(argv[0]);
209 retval = 0;
210 goto finish;
211
212 case PA_CMD_VERSION :
213 printf(PACKAGE_NAME" "PACKAGE_VERSION"\n");
214 retval = 0;
215 goto finish;
216
217 case PA_CMD_CHECK: {
218 pid_t pid;
219
220 if (pa_pid_file_check_running(&pid) < 0) {
221 pa_log_info(__FILE__": daemon not running\n");
222 } else {
223 pa_log_info(__FILE__": daemon running as PID %u\n", pid);
224 retval = 0;
225 }
226
227 goto finish;
228
229 }
230 case PA_CMD_KILL:
231
232 if (pa_pid_file_kill(SIGINT, NULL) < 0)
233 pa_log(__FILE__": failed to kill daemon.\n");
234 else
235 retval = 0;
236
237 goto finish;
238
239 default:
240 assert(conf->cmd == PA_CMD_DAEMON);
241 }
242
243 if (conf->daemonize) {
244 pid_t child;
245 int tty_fd;
246
247 if (pa_stdio_acquire() < 0) {
248 pa_log(__FILE__": failed to acquire stdio.\n");
249 goto finish;
250 }
251
252 if (pipe(daemon_pipe) < 0) {
253 pa_log(__FILE__": failed to create pipe.\n");
254 goto finish;
255 }
256
257 if ((child = fork()) < 0) {
258 pa_log(__FILE__": fork() failed: %s\n", strerror(errno));
259 goto finish;
260 }
261
262 if (child != 0) {
263 /* Father */
264
265 close(daemon_pipe[1]);
266 daemon_pipe[1] = -1;
267
268 if (pa_loop_read(daemon_pipe[0], &retval, sizeof(retval)) != sizeof(retval)) {
269 pa_log(__FILE__": read() failed: %s\n", strerror(errno));
270 retval = 1;
271 }
272
273 if (retval)
274 pa_log(__FILE__": daemon startup failed.\n");
275 else
276 pa_log_info(__FILE__": daemon startup successful.\n");
277
278 goto finish;
279 }
280
281 close(daemon_pipe[0]);
282 daemon_pipe[0] = -1;
283
284 if (conf->auto_log_target)
285 pa_log_set_target(PA_LOG_SYSLOG, NULL);
286
287 setsid();
288 setpgid(0,0);
289
290 close(0);
291 close(1);
292 close(2);
293
294 open("/dev/null", O_RDONLY);
295 open("/dev/null", O_WRONLY);
296 open("/dev/null", O_WRONLY);
297
298 signal(SIGTTOU, SIG_IGN);
299 signal(SIGTTIN, SIG_IGN);
300 signal(SIGTSTP, SIG_IGN);
301
302 if ((tty_fd = open("/dev/tty", O_RDWR)) >= 0) {
303 ioctl(tty_fd, TIOCNOTTY, (char*) 0);
304 close(tty_fd);
305 }
306 }
307
308 chdir("/");
309
310 if (conf->use_pid_file) {
311 if (pa_pid_file_create() < 0) {
312 if (conf->daemonize)
313 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
314 goto finish;
315 }
316
317 valid_pid_file = 1;
318 }
319
320 mainloop = pa_mainloop_new();
321 assert(mainloop);
322
323 r = pa_signal_init(pa_mainloop_get_api(mainloop));
324 assert(r == 0);
325 pa_signal_new(SIGINT, signal_callback, c);
326 pa_signal_new(SIGTERM, signal_callback, c);
327 signal(SIGPIPE, SIG_IGN);
328
329 c = pa_core_new(pa_mainloop_get_api(mainloop));
330 assert(c);
331
332 pa_signal_new(SIGUSR1, signal_callback, c);
333 pa_signal_new(SIGUSR2, signal_callback, c);
334 pa_signal_new(SIGHUP, signal_callback, c);
335
336 r = pa_cpu_limit_init(pa_mainloop_get_api(mainloop));
337 assert(r == 0);
338
339 buf = pa_strbuf_new();
340 assert(buf);
341 if (conf->default_script_file)
342 r = pa_cli_command_execute_file(c, conf->default_script_file, buf, &conf->fail);
343
344 if (r >= 0)
345 r = pa_cli_command_execute(c, conf->script_commands, buf, &conf->fail);
346 pa_log(s = pa_strbuf_tostring_free(buf));
347 pa_xfree(s);
348
349 if (r < 0 && conf->fail) {
350 pa_log(__FILE__": failed to initialize daemon.\n");
351 if (conf->daemonize)
352 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
353 } else if (!c->modules || pa_idxset_ncontents(c->modules) == 0) {
354 pa_log(__FILE__": daemon startup without any loaded modules, refusing to work.\n");
355 if (conf->daemonize)
356 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
357 } else {
358
359 retval = 0;
360 if (conf->daemonize)
361 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
362
363 c->disallow_module_loading = conf->disallow_module_loading;
364 c->exit_idle_time = conf->exit_idle_time;
365 c->module_idle_time = conf->module_idle_time;
366 c->scache_idle_time = conf->scache_idle_time;
367 c->resample_method = conf->resample_method;
368
369 pa_log_info(__FILE__": Daemon startup complete.\n");
370 if (pa_mainloop_run(mainloop, &retval) < 0)
371 retval = 1;
372 pa_log_info(__FILE__": Daemon shutdown initiated.\n");
373 }
374
375 pa_core_free(c);
376
377 pa_cpu_limit_done();
378 pa_signal_done();
379 pa_mainloop_free(mainloop);
380
381 pa_log_info(__FILE__": Daemon terminated.\n");
382
383 finish:
384
385 if (conf)
386 pa_daemon_conf_free(conf);
387
388 if (valid_pid_file)
389 pa_pid_file_remove();
390
391 close_pipe(daemon_pipe);
392
393 lt_dlexit();
394
395 return retval;
396 }