]> code.delx.au - pulseaudio/blob - src/daemon/main.c
big s/polyp/pulse/g
[pulseaudio] / src / daemon / main.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
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.
10
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.
15
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
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 <limits.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <locale.h>
39 #include <sys/types.h>
40 #include <liboil/liboil.h>
41
42 #ifdef HAVE_SYS_IOCTL_H
43 #include <sys/ioctl.h>
44 #endif
45
46 #ifdef HAVE_LIBWRAP
47 #include <syslog.h>
48 #include <tcpd.h>
49 #endif
50
51 #include "../pulsecore/winsock.h"
52
53 #include <pulse/mainloop.h>
54 #include <pulse/mainloop-signal.h>
55 #include <pulse/xmalloc.h>
56
57 #include <pulsecore/core-error.h>
58 #include <pulsecore/core.h>
59 #include <pulsecore/memblock.h>
60 #include <pulsecore/module.h>
61 #include <pulsecore/cli-command.h>
62 #include <pulsecore/log.h>
63 #include <pulsecore/core-util.h>
64 #include <pulsecore/sioman.h>
65 #include <pulsecore/cli-text.h>
66 #include <pulsecore/pid.h>
67 #include <pulsecore/namereg.h>
68 #include <pulsecore/random.h>
69
70 #include "cmdline.h"
71 #include "cpulimit.h"
72 #include "daemon-conf.h"
73 #include "dumpmodules.h"
74 #include "caps.h"
75
76 #ifdef HAVE_LIBWRAP
77 /* Only one instance of these variables */
78 int allow_severity = LOG_INFO;
79 int deny_severity = LOG_WARNING;
80 #endif
81
82 #ifdef HAVE_OSS
83 /* padsp looks for this symbol in the running process and disables
84 * itself if it finds it and it is set to 7 (which is actually a bit
85 * mask). For details see padsp. */
86 int __padsp_disabled__ = 7;
87 #endif
88
89 #ifdef OS_IS_WIN32
90
91 static void message_cb(pa_mainloop_api*a, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
92 MSG msg;
93 struct timeval tvnext;
94
95 while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
96 if (msg.message == WM_QUIT)
97 raise(SIGTERM);
98 else {
99 TranslateMessage(&msg);
100 DispatchMessage(&msg);
101 }
102 }
103
104 pa_timeval_add(pa_gettimeofday(&tvnext), 100000);
105 a->time_restart(e, &tvnext);
106 }
107
108 #endif
109
110 static void signal_callback(pa_mainloop_api*m, PA_GCC_UNUSED pa_signal_event *e, int sig, void *userdata) {
111 pa_log_info(__FILE__": Got signal %s.", pa_strsignal(sig));
112
113 switch (sig) {
114 #ifdef SIGUSR1
115 case SIGUSR1:
116 pa_module_load(userdata, "module-cli", NULL);
117 break;
118 #endif
119
120 #ifdef SIGUSR2
121 case SIGUSR2:
122 pa_module_load(userdata, "module-cli-protocol-unix", NULL);
123 break;
124 #endif
125
126 #ifdef SIGHUP
127 case SIGHUP: {
128 char *c = pa_full_status_string(userdata);
129 pa_log_notice("%s", c);
130 pa_xfree(c);
131 return;
132 }
133 #endif
134
135 case SIGINT:
136 case SIGTERM:
137 default:
138 pa_log_info(__FILE__": Exiting.");
139 m->quit(m, 1);
140 break;
141 }
142 }
143
144 static void close_pipe(int p[2]) {
145 if (p[0] != -1)
146 close(p[0]);
147 if (p[1] != -1)
148 close(p[1]);
149 p[0] = p[1] = -1;
150 }
151
152 int main(int argc, char *argv[]) {
153 pa_core *c;
154 pa_strbuf *buf = NULL;
155 pa_daemon_conf *conf;
156 pa_mainloop *mainloop;
157
158 char *s;
159 int r, retval = 1, d = 0;
160 int daemon_pipe[2] = { -1, -1 };
161 int suid_root;
162 int valid_pid_file = 0;
163
164 #ifdef HAVE_GETUID
165 gid_t gid = (gid_t) -1;
166 #endif
167
168 #ifdef OS_IS_WIN32
169 pa_time_event *timer;
170 struct timeval tv;
171 #endif
172
173 setlocale(LC_ALL, "");
174
175 pa_limit_caps();
176
177 #ifdef HAVE_GETUID
178 suid_root = getuid() != 0 && geteuid() == 0;
179
180 if (suid_root && (pa_own_uid_in_group("realtime", &gid) <= 0 || gid >= 1000)) {
181 pa_log_warn(__FILE__": WARNING: called SUID root, but not in group 'realtime'.");
182 pa_drop_root();
183 }
184 #else
185 suid_root = 0;
186 #endif
187
188 LTDL_SET_PRELOADED_SYMBOLS();
189
190 r = lt_dlinit();
191 assert(r == 0);
192
193 #ifdef OS_IS_WIN32
194 {
195 WSADATA data;
196 WSAStartup(MAKEWORD(2, 0), &data);
197 }
198 #endif
199
200 pa_random_seed();
201
202 pa_log_set_ident("pulseaudio");
203
204 conf = pa_daemon_conf_new();
205
206 if (pa_daemon_conf_load(conf, NULL) < 0)
207 goto finish;
208
209 if (pa_daemon_conf_env(conf) < 0)
210 goto finish;
211
212 if (pa_cmdline_parse(conf, argc, argv, &d) < 0) {
213 pa_log(__FILE__": failed to parse command line.");
214 goto finish;
215 }
216
217 pa_log_set_maximal_level(conf->log_level);
218 pa_log_set_target(conf->auto_log_target ? PA_LOG_STDERR : conf->log_target, NULL);
219
220 if (conf->high_priority && conf->cmd == PA_CMD_DAEMON)
221 pa_raise_priority();
222
223 pa_drop_caps();
224
225 if (suid_root)
226 pa_drop_root();
227
228 if (conf->dl_search_path)
229 lt_dlsetsearchpath(conf->dl_search_path);
230
231 switch (conf->cmd) {
232 case PA_CMD_DUMP_MODULES:
233 pa_dump_modules(conf, argc-d, argv+d);
234 retval = 0;
235 goto finish;
236
237 case PA_CMD_DUMP_CONF: {
238 s = pa_daemon_conf_dump(conf);
239 fputs(s, stdout);
240 pa_xfree(s);
241 retval = 0;
242 goto finish;
243 }
244
245 case PA_CMD_HELP :
246 pa_cmdline_help(argv[0]);
247 retval = 0;
248 goto finish;
249
250 case PA_CMD_VERSION :
251 printf(PACKAGE_NAME" "PACKAGE_VERSION"\n");
252 retval = 0;
253 goto finish;
254
255 case PA_CMD_CHECK: {
256 pid_t pid;
257
258 if (pa_pid_file_check_running(&pid) < 0) {
259 pa_log_info(__FILE__": daemon not running");
260 } else {
261 pa_log_info(__FILE__": daemon running as PID %u", pid);
262 retval = 0;
263 }
264
265 goto finish;
266
267 }
268 case PA_CMD_KILL:
269
270 if (pa_pid_file_kill(SIGINT, NULL) < 0)
271 pa_log(__FILE__": failed to kill daemon.");
272 else
273 retval = 0;
274
275 goto finish;
276
277 default:
278 assert(conf->cmd == PA_CMD_DAEMON);
279 }
280
281 if (conf->daemonize) {
282 pid_t child;
283 int tty_fd;
284
285 if (pa_stdio_acquire() < 0) {
286 pa_log(__FILE__": failed to acquire stdio.");
287 goto finish;
288 }
289
290 #ifdef HAVE_FORK
291 if (pipe(daemon_pipe) < 0) {
292 pa_log(__FILE__": failed to create pipe.");
293 goto finish;
294 }
295
296 if ((child = fork()) < 0) {
297 pa_log(__FILE__": fork() failed: %s", pa_cstrerror(errno));
298 goto finish;
299 }
300
301 if (child != 0) {
302 /* Father */
303
304 close(daemon_pipe[1]);
305 daemon_pipe[1] = -1;
306
307 if (pa_loop_read(daemon_pipe[0], &retval, sizeof(retval)) != sizeof(retval)) {
308 pa_log(__FILE__": read() failed: %s", pa_cstrerror(errno));
309 retval = 1;
310 }
311
312 if (retval)
313 pa_log(__FILE__": daemon startup failed.");
314 else
315 pa_log_info(__FILE__": daemon startup successful.");
316
317 goto finish;
318 }
319
320 close(daemon_pipe[0]);
321 daemon_pipe[0] = -1;
322 #endif
323
324 if (conf->auto_log_target)
325 pa_log_set_target(PA_LOG_SYSLOG, NULL);
326
327 #ifdef HAVE_SETSID
328 setsid();
329 #endif
330 #ifdef HAVE_SETPGID
331 setpgid(0,0);
332 #endif
333
334 #ifndef OS_IS_WIN32
335 close(0);
336 close(1);
337 close(2);
338
339 open("/dev/null", O_RDONLY);
340 open("/dev/null", O_WRONLY);
341 open("/dev/null", O_WRONLY);
342 #else
343 FreeConsole();
344 #endif
345
346 #ifdef SIGTTOU
347 signal(SIGTTOU, SIG_IGN);
348 #endif
349 #ifdef SIGTTIN
350 signal(SIGTTIN, SIG_IGN);
351 #endif
352 #ifdef SIGTSTP
353 signal(SIGTSTP, SIG_IGN);
354 #endif
355
356 #ifdef TIOCNOTTY
357 if ((tty_fd = open("/dev/tty", O_RDWR)) >= 0) {
358 ioctl(tty_fd, TIOCNOTTY, (char*) 0);
359 close(tty_fd);
360 }
361 #endif
362 }
363
364 chdir("/");
365
366 if (conf->use_pid_file) {
367 if (pa_pid_file_create() < 0) {
368 pa_log(__FILE__": pa_pid_file_create() failed.");
369 #ifdef HAVE_FORK
370 if (conf->daemonize)
371 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
372 #endif
373 goto finish;
374 }
375
376 valid_pid_file = 1;
377 }
378
379 #ifdef SIGPIPE
380 signal(SIGPIPE, SIG_IGN);
381 #endif
382
383 mainloop = pa_mainloop_new();
384 assert(mainloop);
385
386 c = pa_core_new(pa_mainloop_get_api(mainloop));
387 assert(c);
388
389 r = pa_signal_init(pa_mainloop_get_api(mainloop));
390 assert(r == 0);
391 pa_signal_new(SIGINT, signal_callback, c);
392 pa_signal_new(SIGTERM, signal_callback, c);
393
394 #ifdef SIGUSR1
395 pa_signal_new(SIGUSR1, signal_callback, c);
396 #endif
397 #ifdef SIGUSR2
398 pa_signal_new(SIGUSR2, signal_callback, c);
399 #endif
400 #ifdef SIGHUP
401 pa_signal_new(SIGHUP, signal_callback, c);
402 #endif
403
404 #ifdef OS_IS_WIN32
405 timer = pa_mainloop_get_api(mainloop)->time_new(
406 pa_mainloop_get_api(mainloop), pa_gettimeofday(&tv), message_cb, NULL);
407 assert(timer);
408 #endif
409
410 if (conf->daemonize)
411 c->running_as_daemon = 1;
412
413 oil_init();
414
415 r = pa_cpu_limit_init(pa_mainloop_get_api(mainloop));
416 assert(r == 0);
417
418 buf = pa_strbuf_new();
419 if (conf->default_script_file)
420 r = pa_cli_command_execute_file(c, conf->default_script_file, buf, &conf->fail);
421
422 if (r >= 0)
423 r = pa_cli_command_execute(c, conf->script_commands, buf, &conf->fail);
424 pa_log_error("%s", s = pa_strbuf_tostring_free(buf));
425 pa_xfree(s);
426
427 if (r < 0 && conf->fail) {
428 pa_log(__FILE__": failed to initialize daemon.");
429 #ifdef HAVE_FORK
430 if (conf->daemonize)
431 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
432 #endif
433 } else if (!c->modules || pa_idxset_size(c->modules) == 0) {
434 pa_log(__FILE__": daemon startup without any loaded modules, refusing to work.");
435 #ifdef HAVE_FORK
436 if (conf->daemonize)
437 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
438 #endif
439 } else {
440
441 retval = 0;
442 #ifdef HAVE_FORK
443 if (conf->daemonize)
444 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
445 #endif
446
447 c->disallow_module_loading = conf->disallow_module_loading;
448 c->exit_idle_time = conf->exit_idle_time;
449 c->module_idle_time = conf->module_idle_time;
450 c->scache_idle_time = conf->scache_idle_time;
451 c->resample_method = conf->resample_method;
452
453 if (c->default_sink_name &&
454 pa_namereg_get(c, c->default_sink_name, PA_NAMEREG_SINK, 1) == NULL) {
455 pa_log_error("%s : Fatal error. Default sink name (%s) does not exist in name register.", __FILE__, c->default_sink_name);
456 retval = 1;
457 } else {
458 pa_log_info(__FILE__": Daemon startup complete.");
459 if (pa_mainloop_run(mainloop, &retval) < 0)
460 retval = 1;
461 pa_log_info(__FILE__": Daemon shutdown initiated.");
462 }
463 }
464
465 #ifdef OS_IS_WIN32
466 pa_mainloop_get_api(mainloop)->time_free(timer);
467 #endif
468
469 pa_core_free(c);
470
471 pa_cpu_limit_done();
472 pa_signal_done();
473 pa_mainloop_free(mainloop);
474
475 pa_log_info(__FILE__": Daemon terminated.");
476
477 finish:
478
479 if (conf)
480 pa_daemon_conf_free(conf);
481
482 if (valid_pid_file)
483 pa_pid_file_remove();
484
485 close_pipe(daemon_pipe);
486
487 #ifdef OS_IS_WIN32
488 WSACleanup();
489 #endif
490
491 lt_dlexit();
492
493 return retval;
494 }