]> code.delx.au - pulseaudio/blob - src/daemon/main.c
add a few comments
[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 <pwd.h>
41 #include <grp.h>
42
43 #include <liboil/liboil.h>
44
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
47 #endif
48
49 #ifdef HAVE_LIBWRAP
50 #include <syslog.h>
51 #include <tcpd.h>
52 #endif
53
54 #include "../pulsecore/winsock.h"
55
56 #include <pulse/mainloop.h>
57 #include <pulse/mainloop-signal.h>
58 #include <pulse/xmalloc.h>
59
60 #include <pulsecore/core-error.h>
61 #include <pulsecore/core.h>
62 #include <pulsecore/memblock.h>
63 #include <pulsecore/module.h>
64 #include <pulsecore/cli-command.h>
65 #include <pulsecore/log.h>
66 #include <pulsecore/core-util.h>
67 #include <pulsecore/sioman.h>
68 #include <pulsecore/cli-text.h>
69 #include <pulsecore/pid.h>
70 #include <pulsecore/namereg.h>
71 #include <pulsecore/random.h>
72
73 #include "cmdline.h"
74 #include "cpulimit.h"
75 #include "daemon-conf.h"
76 #include "dumpmodules.h"
77 #include "caps.h"
78
79 #ifdef HAVE_LIBWRAP
80 /* Only one instance of these variables */
81 int allow_severity = LOG_INFO;
82 int deny_severity = LOG_WARNING;
83 #endif
84
85 #ifdef HAVE_OSS
86 /* padsp looks for this symbol in the running process and disables
87 * itself if it finds it and it is set to 7 (which is actually a bit
88 * mask). For details see padsp. */
89 int __padsp_disabled__ = 7;
90 #endif
91
92 #ifdef OS_IS_WIN32
93
94 static void message_cb(pa_mainloop_api*a, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
95 MSG msg;
96 struct timeval tvnext;
97
98 while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
99 if (msg.message == WM_QUIT)
100 raise(SIGTERM);
101 else {
102 TranslateMessage(&msg);
103 DispatchMessage(&msg);
104 }
105 }
106
107 pa_timeval_add(pa_gettimeofday(&tvnext), 100000);
108 a->time_restart(e, &tvnext);
109 }
110
111 #endif
112
113 static void signal_callback(pa_mainloop_api*m, PA_GCC_UNUSED pa_signal_event *e, int sig, void *userdata) {
114 pa_log_info(__FILE__": Got signal %s.", pa_strsignal(sig));
115
116 switch (sig) {
117 #ifdef SIGUSR1
118 case SIGUSR1:
119 pa_module_load(userdata, "module-cli", NULL);
120 break;
121 #endif
122
123 #ifdef SIGUSR2
124 case SIGUSR2:
125 pa_module_load(userdata, "module-cli-protocol-unix", NULL);
126 break;
127 #endif
128
129 #ifdef SIGHUP
130 case SIGHUP: {
131 char *c = pa_full_status_string(userdata);
132 pa_log_notice("%s", c);
133 pa_xfree(c);
134 return;
135 }
136 #endif
137
138 case SIGINT:
139 case SIGTERM:
140 default:
141 pa_log_info(__FILE__": Exiting.");
142 m->quit(m, 1);
143 break;
144 }
145 }
146
147 static void close_pipe(int p[2]) {
148 if (p[0] != -1)
149 close(p[0]);
150 if (p[1] != -1)
151 close(p[1]);
152 p[0] = p[1] = -1;
153 }
154
155 #define set_env(key, value) putenv(pa_sprintf_malloc("%s=%s", (key), (value)))
156
157 static int change_user(void) {
158 struct passwd *pw;
159 struct group * gr;
160 int r;
161
162 /* This function is called only in system-wide mode. It creates a
163 * runtime dir in /var/run/ with proper UID/GID and drops privs
164 * afterwards. */
165
166 if (!(pw = getpwnam(PA_SYSTEM_USER))) {
167 pa_log(__FILE__": Failed to find user '%s'.", PA_SYSTEM_USER);
168 return -1;
169 }
170
171 if (!(gr = getgrnam(PA_SYSTEM_GROUP))) {
172 pa_log(__FILE__": Failed to find group '%s'.", PA_SYSTEM_GROUP);
173 return -1;
174 }
175
176 pa_log_info(__FILE__": Found user '%s' (UID %lu) and group '%s' (GID %lu).",
177 PA_SYSTEM_USER, (unsigned long) pw->pw_uid,
178 PA_SYSTEM_GROUP, (unsigned long) gr->gr_gid);
179
180 if (pw->pw_gid != gr->gr_gid) {
181 pa_log(__FILE__": GID of user '%s' and of group '%s' don't match.", PA_SYSTEM_USER, PA_SYSTEM_GROUP);
182 return -1;
183 }
184
185 if (strcmp(pw->pw_dir, PA_SYSTEM_RUNTIME_PATH) != 0)
186 pa_log_warn(__FILE__": Warning: home directory of user '%s' is not '%s', ignoring.", PA_SYSTEM_USER, PA_SYSTEM_RUNTIME_PATH);
187
188 if (pa_make_secure_dir(PA_SYSTEM_RUNTIME_PATH, 0755, pw->pw_uid, gr->gr_gid) < 0) {
189 pa_log(__FILE__": Failed to create '%s': %s", PA_SYSTEM_RUNTIME_PATH, pa_cstrerror(errno));
190 return -1;
191 }
192
193 if (initgroups(PA_SYSTEM_USER, gr->gr_gid) != 0) {
194 pa_log(__FILE__": Failed to change group list: %s", pa_cstrerror(errno));
195 return -1;
196 }
197
198 #if defined(HAVE_SETRESGID)
199 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
200 #elif defined(HAVE_SETEGID)
201 if ((r = setgid(gr->gr_gid)) >= 0)
202 r = setegid(gr->gr_gid);
203 #elif defined(HAVE_SETREGID)
204 r = setregid(gr->gr_gid, gr->gr_gid);
205 #else
206 #error "No API to drop priviliges"
207 #endif
208
209 if (r < 0) {
210 pa_log(__FILE__": Failed to change GID: %s", pa_cstrerror(errno));
211 return -1;
212 }
213
214 #if defined(HAVE_SETRESUID)
215 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
216 #elif defined(HAVE_SETEUID)
217 if ((r = setuid(pw->pw_uid)) >= 0)
218 r = seteuid(pw->pw_uid);
219 #elif defined(HAVE_SETREUID)
220 r = setreuid(pw->pw_uid, pw->pw_uid);
221 #else
222 #error "No API to drop priviliges"
223 #endif
224
225 if (r < 0) {
226 pa_log(__FILE__": Failed to change UID: %s", pa_cstrerror(errno));
227 return -1;
228 }
229
230 set_env("USER", PA_SYSTEM_USER);
231 set_env("LOGNAME", PA_SYSTEM_GROUP);
232 set_env("HOME", PA_SYSTEM_RUNTIME_PATH);
233
234 /* Relevant for pa_runtime_path() */
235 set_env("PULSE_RUNTIME_PATH", PA_SYSTEM_RUNTIME_PATH);
236 set_env("PULSE_CONFIG_PATH", PA_SYSTEM_RUNTIME_PATH);
237
238 pa_log_info(__FILE__": Successfully dropped root privileges.");
239
240 return 0;
241 }
242
243 static int create_runtime_dir(void) {
244 char fn[PATH_MAX];
245
246 pa_runtime_path(NULL, fn, sizeof(fn));
247
248 /* This function is called only when the daemon is started in
249 * per-user mode. We create the runtime directory somewhere in
250 * /tmp/ with the current UID/GID */
251
252 if (pa_make_secure_dir(fn, 0700, getuid(), getgid()) < 0) {
253 pa_log(__FILE__": Failed to create '%s': %s", fn, pa_cstrerror(errno));
254 return -1;
255 }
256
257 return 0;
258 }
259
260 int main(int argc, char *argv[]) {
261 pa_core *c;
262 pa_strbuf *buf = NULL;
263 pa_daemon_conf *conf;
264 pa_mainloop *mainloop;
265
266 char *s;
267 int r, retval = 1, d = 0;
268 int daemon_pipe[2] = { -1, -1 };
269 int suid_root;
270 int valid_pid_file = 0;
271
272 #ifdef HAVE_GETUID
273 gid_t gid = (gid_t) -1;
274 #endif
275
276 #ifdef OS_IS_WIN32
277 pa_time_event *timer;
278 struct timeval tv;
279 #endif
280
281 setlocale(LC_ALL, "");
282
283 if (getuid() != 0)
284 pa_limit_caps();
285
286 #ifdef HAVE_GETUID
287 suid_root = getuid() != 0 && geteuid() == 0;
288
289 if (suid_root && (pa_own_uid_in_group(PA_REALTIME_GROUP, &gid) <= 0 || gid >= 1000)) {
290 pa_log_warn(__FILE__": WARNING: called SUID root, but not in group '"PA_REALTIME_GROUP"'.");
291 pa_drop_root();
292 }
293 #else
294 suid_root = 0;
295 #endif
296
297 LTDL_SET_PRELOADED_SYMBOLS();
298
299 r = lt_dlinit();
300 assert(r == 0);
301
302 #ifdef OS_IS_WIN32
303 {
304 WSADATA data;
305 WSAStartup(MAKEWORD(2, 0), &data);
306 }
307 #endif
308
309 pa_random_seed();
310
311 pa_log_set_ident("pulseaudio");
312
313 conf = pa_daemon_conf_new();
314
315 if (pa_daemon_conf_load(conf, NULL) < 0)
316 goto finish;
317
318 if (pa_daemon_conf_env(conf) < 0)
319 goto finish;
320
321 if (pa_cmdline_parse(conf, argc, argv, &d) < 0) {
322 pa_log(__FILE__": failed to parse command line.");
323 goto finish;
324 }
325
326 pa_log_set_maximal_level(conf->log_level);
327 pa_log_set_target(conf->auto_log_target ? PA_LOG_STDERR : conf->log_target, NULL);
328
329 if (conf->high_priority && conf->cmd == PA_CMD_DAEMON)
330 pa_raise_priority();
331
332 if (getuid() != 0)
333 pa_drop_caps();
334
335 if (suid_root)
336 pa_drop_root();
337
338 if (conf->dl_search_path)
339 lt_dlsetsearchpath(conf->dl_search_path);
340
341 switch (conf->cmd) {
342 case PA_CMD_DUMP_MODULES:
343 pa_dump_modules(conf, argc-d, argv+d);
344 retval = 0;
345 goto finish;
346
347 case PA_CMD_DUMP_CONF: {
348 s = pa_daemon_conf_dump(conf);
349 fputs(s, stdout);
350 pa_xfree(s);
351 retval = 0;
352 goto finish;
353 }
354
355 case PA_CMD_HELP :
356 pa_cmdline_help(argv[0]);
357 retval = 0;
358 goto finish;
359
360 case PA_CMD_VERSION :
361 printf(PACKAGE_NAME" "PACKAGE_VERSION"\n");
362 retval = 0;
363 goto finish;
364
365 case PA_CMD_CHECK: {
366 pid_t pid;
367
368 if (pa_pid_file_check_running(&pid) < 0) {
369 pa_log_info(__FILE__": daemon not running");
370 } else {
371 pa_log_info(__FILE__": daemon running as PID %u", pid);
372 retval = 0;
373 }
374
375 goto finish;
376
377 }
378 case PA_CMD_KILL:
379
380 if (pa_pid_file_kill(SIGINT, NULL) < 0)
381 pa_log(__FILE__": failed to kill daemon.");
382 else
383 retval = 0;
384
385 goto finish;
386
387 default:
388 assert(conf->cmd == PA_CMD_DAEMON);
389 }
390
391 if (getuid() == 0 && !conf->system_instance) {
392 pa_log(__FILE__": This program is not intended to be run as root (unless --system is specified).");
393 goto finish;
394 } else if (getuid() != 0 && conf->system_instance) {
395 pa_log(__FILE__": Root priviliges required.");
396 goto finish;
397 }
398
399 if (conf->daemonize) {
400 pid_t child;
401 int tty_fd;
402
403 if (pa_stdio_acquire() < 0) {
404 pa_log(__FILE__": failed to acquire stdio.");
405 goto finish;
406 }
407
408 #ifdef HAVE_FORK
409 if (pipe(daemon_pipe) < 0) {
410 pa_log(__FILE__": failed to create pipe.");
411 goto finish;
412 }
413
414 if ((child = fork()) < 0) {
415 pa_log(__FILE__": fork() failed: %s", pa_cstrerror(errno));
416 goto finish;
417 }
418
419 if (child != 0) {
420 /* Father */
421
422 close(daemon_pipe[1]);
423 daemon_pipe[1] = -1;
424
425 if (pa_loop_read(daemon_pipe[0], &retval, sizeof(retval), NULL) != sizeof(retval)) {
426 pa_log(__FILE__": read() failed: %s", pa_cstrerror(errno));
427 retval = 1;
428 }
429
430 if (retval)
431 pa_log(__FILE__": daemon startup failed.");
432 else
433 pa_log_info(__FILE__": daemon startup successful.");
434
435 goto finish;
436 }
437
438 close(daemon_pipe[0]);
439 daemon_pipe[0] = -1;
440 #endif
441
442 if (conf->auto_log_target)
443 pa_log_set_target(PA_LOG_SYSLOG, NULL);
444
445 #ifdef HAVE_SETSID
446 setsid();
447 #endif
448 #ifdef HAVE_SETPGID
449 setpgid(0,0);
450 #endif
451
452 #ifndef OS_IS_WIN32
453 close(0);
454 close(1);
455 close(2);
456
457 open("/dev/null", O_RDONLY);
458 open("/dev/null", O_WRONLY);
459 open("/dev/null", O_WRONLY);
460 #else
461 FreeConsole();
462 #endif
463
464 #ifdef SIGTTOU
465 signal(SIGTTOU, SIG_IGN);
466 #endif
467 #ifdef SIGTTIN
468 signal(SIGTTIN, SIG_IGN);
469 #endif
470 #ifdef SIGTSTP
471 signal(SIGTSTP, SIG_IGN);
472 #endif
473
474 #ifdef TIOCNOTTY
475 if ((tty_fd = open("/dev/tty", O_RDWR)) >= 0) {
476 ioctl(tty_fd, TIOCNOTTY, (char*) 0);
477 close(tty_fd);
478 }
479 #endif
480 }
481
482 chdir("/");
483 umask(0022);
484
485 if (conf->system_instance) {
486 if (change_user() < 0)
487 goto finish;
488 } else if (create_runtime_dir() < 0)
489 goto finish;
490
491 if (conf->use_pid_file) {
492 if (pa_pid_file_create() < 0) {
493 pa_log(__FILE__": pa_pid_file_create() failed.");
494 #ifdef HAVE_FORK
495 if (conf->daemonize)
496 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL);
497 #endif
498 goto finish;
499 }
500
501 valid_pid_file = 1;
502 }
503
504 #ifdef SIGPIPE
505 signal(SIGPIPE, SIG_IGN);
506 #endif
507
508 mainloop = pa_mainloop_new();
509 assert(mainloop);
510
511 c = pa_core_new(pa_mainloop_get_api(mainloop));
512 assert(c);
513 c->is_system_instance = !!conf->system_instance;
514
515 r = pa_signal_init(pa_mainloop_get_api(mainloop));
516 assert(r == 0);
517 pa_signal_new(SIGINT, signal_callback, c);
518 pa_signal_new(SIGTERM, signal_callback, c);
519
520 #ifdef SIGUSR1
521 pa_signal_new(SIGUSR1, signal_callback, c);
522 #endif
523 #ifdef SIGUSR2
524 pa_signal_new(SIGUSR2, signal_callback, c);
525 #endif
526 #ifdef SIGHUP
527 pa_signal_new(SIGHUP, signal_callback, c);
528 #endif
529
530 #ifdef OS_IS_WIN32
531 timer = pa_mainloop_get_api(mainloop)->time_new(
532 pa_mainloop_get_api(mainloop), pa_gettimeofday(&tv), message_cb, NULL);
533 assert(timer);
534 #endif
535
536 if (conf->daemonize)
537 c->running_as_daemon = 1;
538
539 oil_init();
540
541 r = pa_cpu_limit_init(pa_mainloop_get_api(mainloop));
542 assert(r == 0);
543
544 buf = pa_strbuf_new();
545 if (conf->default_script_file)
546 r = pa_cli_command_execute_file(c, conf->default_script_file, buf, &conf->fail);
547
548 if (r >= 0)
549 r = pa_cli_command_execute(c, conf->script_commands, buf, &conf->fail);
550 pa_log_error("%s", s = pa_strbuf_tostring_free(buf));
551 pa_xfree(s);
552
553 if (r < 0 && conf->fail) {
554 pa_log(__FILE__": failed to initialize daemon.");
555 #ifdef HAVE_FORK
556 if (conf->daemonize)
557 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL);
558 #endif
559 } else if (!c->modules || pa_idxset_size(c->modules) == 0) {
560 pa_log(__FILE__": daemon startup without any loaded modules, refusing to work.");
561 #ifdef HAVE_FORK
562 if (conf->daemonize)
563 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL);
564 #endif
565 } else {
566
567 retval = 0;
568 #ifdef HAVE_FORK
569 if (conf->daemonize)
570 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL);
571 #endif
572
573 c->disallow_module_loading = conf->disallow_module_loading;
574 c->exit_idle_time = conf->exit_idle_time;
575 c->module_idle_time = conf->module_idle_time;
576 c->scache_idle_time = conf->scache_idle_time;
577 c->resample_method = conf->resample_method;
578
579 if (c->default_sink_name &&
580 pa_namereg_get(c, c->default_sink_name, PA_NAMEREG_SINK, 1) == NULL) {
581 pa_log_error("%s : Fatal error. Default sink name (%s) does not exist in name register.", __FILE__, c->default_sink_name);
582 retval = 1;
583 } else {
584 pa_log_info(__FILE__": Daemon startup complete.");
585 if (pa_mainloop_run(mainloop, &retval) < 0)
586 retval = 1;
587 pa_log_info(__FILE__": Daemon shutdown initiated.");
588 }
589 }
590
591 #ifdef OS_IS_WIN32
592 pa_mainloop_get_api(mainloop)->time_free(timer);
593 #endif
594
595 pa_core_free(c);
596
597 pa_cpu_limit_done();
598 pa_signal_done();
599 pa_mainloop_free(mainloop);
600
601 pa_log_info(__FILE__": Daemon terminated.");
602
603 finish:
604
605 if (conf)
606 pa_daemon_conf_free(conf);
607
608 if (valid_pid_file)
609 pa_pid_file_remove();
610
611 close_pipe(daemon_pipe);
612
613 #ifdef OS_IS_WIN32
614 WSACleanup();
615 #endif
616
617 lt_dlexit();
618
619 return retval;
620 }