]> code.delx.au - pulseaudio/blob - src/daemon/main.c
* add new --system command line parameter to the daemon for running PulseAudio as...
[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 if (!(pw = getpwnam(PA_SYSTEM_USER))) {
163 pa_log(__FILE__": Failed to find user '%s'.", PA_SYSTEM_USER);
164 return -1;
165 }
166
167 if (!(gr = getgrnam(PA_SYSTEM_GROUP))) {
168 pa_log(__FILE__": Failed to find group '%s'.", PA_SYSTEM_GROUP);
169 return -1;
170 }
171
172 pa_log_info(__FILE__": Found user '%s' (UID %lu) and group '%s' (GID %lu).",
173 PA_SYSTEM_USER, (unsigned long) pw->pw_uid,
174 PA_SYSTEM_GROUP, (unsigned long) gr->gr_gid);
175
176 if (pw->pw_gid != gr->gr_gid) {
177 pa_log(__FILE__": GID of user '%s' and of group '%s' don't match.", PA_SYSTEM_USER, PA_SYSTEM_GROUP);
178 return -1;
179 }
180
181 if (strcmp(pw->pw_dir, PA_SYSTEM_RUNTIME_PATH) != 0)
182 pa_log_warn(__FILE__": Warning: home directory of user '%s' is not '%s', ignoring.", PA_SYSTEM_USER, PA_SYSTEM_RUNTIME_PATH);
183
184 if (pa_make_secure_dir(PA_SYSTEM_RUNTIME_PATH, 0755, pw->pw_uid, gr->gr_gid) < 0) {
185 pa_log(__FILE__": Failed to create '%s': %s", PA_SYSTEM_RUNTIME_PATH, pa_cstrerror(errno));
186 return -1;
187 }
188
189 if (initgroups(PA_SYSTEM_USER, gr->gr_gid) != 0) {
190 pa_log(__FILE__": Failed to change group list: %s", pa_cstrerror(errno));
191 return -1;
192 }
193
194 #if defined(HAVE_SETRESGID)
195 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
196 #elif defined(HAVE_SETEGID)
197 if ((r = setgid(gr->gr_gid)) >= 0)
198 r = setegid(gr->gr_gid);
199 #elif defined(HAVE_SETREGID)
200 r = setregid(gr->gr_gid, gr->gr_gid);
201 #else
202 #error "No API to drop priviliges"
203 #endif
204
205 if (r < 0) {
206 pa_log(__FILE__": Failed to change GID: %s", pa_cstrerror(errno));
207 return -1;
208 }
209
210 #if defined(HAVE_SETRESUID)
211 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
212 #elif defined(HAVE_SETEUID)
213 if ((r = setuid(pw->pw_uid)) >= 0)
214 r = seteuid(pw->pw_uid);
215 #elif defined(HAVE_SETREUID)
216 r = setreuid(pw->pw_uid, pw->pw_uid);
217 #else
218 #error "No API to drop priviliges"
219 #endif
220
221 if (r < 0) {
222 pa_log(__FILE__": Failed to change UID: %s", pa_cstrerror(errno));
223 return -1;
224 }
225
226 set_env("USER", PA_SYSTEM_USER);
227 set_env("LOGNAME", PA_SYSTEM_GROUP);
228 set_env("HOME", PA_SYSTEM_RUNTIME_PATH);
229
230 /* Relevant for pa_runtime_path() */
231 set_env("PULSE_RUNTIME_PATH", PA_SYSTEM_RUNTIME_PATH);
232
233 pa_log_info(__FILE__": Successfully dropped root privileges.");
234
235 return 0;
236 }
237
238 static int create_runtime_dir(void) {
239 char fn[PATH_MAX];
240
241 pa_runtime_path(NULL, fn, sizeof(fn));
242
243 if (pa_make_secure_dir(fn, 0700, getuid(), getgid()) < 0) {
244 pa_log(__FILE__": Failed to create '%s': %s", fn, pa_cstrerror(errno));
245 return -1;
246 }
247
248 /* Relevant for pa_runtime_path() later on */
249 set_env("PULSE_RUNTIME_PATH", fn);
250 return 0;
251 }
252
253 int main(int argc, char *argv[]) {
254 pa_core *c;
255 pa_strbuf *buf = NULL;
256 pa_daemon_conf *conf;
257 pa_mainloop *mainloop;
258
259 char *s;
260 int r, retval = 1, d = 0;
261 int daemon_pipe[2] = { -1, -1 };
262 int suid_root;
263 int valid_pid_file = 0;
264
265 #ifdef HAVE_GETUID
266 gid_t gid = (gid_t) -1;
267 #endif
268
269 #ifdef OS_IS_WIN32
270 pa_time_event *timer;
271 struct timeval tv;
272 #endif
273
274 setlocale(LC_ALL, "");
275
276 if (getuid() != 0)
277 pa_limit_caps();
278
279 #ifdef HAVE_GETUID
280 suid_root = getuid() != 0 && geteuid() == 0;
281
282 if (suid_root && (pa_own_uid_in_group(PA_REALTIME_GROUP, &gid) <= 0 || gid >= 1000)) {
283 pa_log_warn(__FILE__": WARNING: called SUID root, but not in group '"PA_REALTIME_GROUP"'.");
284 pa_drop_root();
285 }
286 #else
287 suid_root = 0;
288 #endif
289
290 LTDL_SET_PRELOADED_SYMBOLS();
291
292 r = lt_dlinit();
293 assert(r == 0);
294
295 #ifdef OS_IS_WIN32
296 {
297 WSADATA data;
298 WSAStartup(MAKEWORD(2, 0), &data);
299 }
300 #endif
301
302 pa_random_seed();
303
304 pa_log_set_ident("pulseaudio");
305
306 conf = pa_daemon_conf_new();
307
308 if (pa_daemon_conf_load(conf, NULL) < 0)
309 goto finish;
310
311 if (pa_daemon_conf_env(conf) < 0)
312 goto finish;
313
314 if (pa_cmdline_parse(conf, argc, argv, &d) < 0) {
315 pa_log(__FILE__": failed to parse command line.");
316 goto finish;
317 }
318
319 pa_log_set_maximal_level(conf->log_level);
320 pa_log_set_target(conf->auto_log_target ? PA_LOG_STDERR : conf->log_target, NULL);
321
322 if (conf->high_priority && conf->cmd == PA_CMD_DAEMON)
323 pa_raise_priority();
324
325 if (getuid() != 0)
326 pa_drop_caps();
327
328 if (suid_root)
329 pa_drop_root();
330
331 if (conf->dl_search_path)
332 lt_dlsetsearchpath(conf->dl_search_path);
333
334 switch (conf->cmd) {
335 case PA_CMD_DUMP_MODULES:
336 pa_dump_modules(conf, argc-d, argv+d);
337 retval = 0;
338 goto finish;
339
340 case PA_CMD_DUMP_CONF: {
341 s = pa_daemon_conf_dump(conf);
342 fputs(s, stdout);
343 pa_xfree(s);
344 retval = 0;
345 goto finish;
346 }
347
348 case PA_CMD_HELP :
349 pa_cmdline_help(argv[0]);
350 retval = 0;
351 goto finish;
352
353 case PA_CMD_VERSION :
354 printf(PACKAGE_NAME" "PACKAGE_VERSION"\n");
355 retval = 0;
356 goto finish;
357
358 case PA_CMD_CHECK: {
359 pid_t pid;
360
361 if (pa_pid_file_check_running(&pid) < 0) {
362 pa_log_info(__FILE__": daemon not running");
363 } else {
364 pa_log_info(__FILE__": daemon running as PID %u", pid);
365 retval = 0;
366 }
367
368 goto finish;
369
370 }
371 case PA_CMD_KILL:
372
373 if (pa_pid_file_kill(SIGINT, NULL) < 0)
374 pa_log(__FILE__": failed to kill daemon.");
375 else
376 retval = 0;
377
378 goto finish;
379
380 default:
381 assert(conf->cmd == PA_CMD_DAEMON);
382 }
383
384 if (getuid() == 0 && !conf->system_instance) {
385 pa_log(__FILE__": This program is not intended to be run as root (unless --system is specified).");
386 goto finish;
387 } else if (getuid() != 0 && conf->system_instance) {
388 pa_log(__FILE__": Root priviliges required.");
389 goto finish;
390 }
391
392 if (conf->daemonize) {
393 pid_t child;
394 int tty_fd;
395
396 if (pa_stdio_acquire() < 0) {
397 pa_log(__FILE__": failed to acquire stdio.");
398 goto finish;
399 }
400
401 #ifdef HAVE_FORK
402 if (pipe(daemon_pipe) < 0) {
403 pa_log(__FILE__": failed to create pipe.");
404 goto finish;
405 }
406
407 if ((child = fork()) < 0) {
408 pa_log(__FILE__": fork() failed: %s", pa_cstrerror(errno));
409 goto finish;
410 }
411
412 if (child != 0) {
413 /* Father */
414
415 close(daemon_pipe[1]);
416 daemon_pipe[1] = -1;
417
418 if (pa_loop_read(daemon_pipe[0], &retval, sizeof(retval), NULL) != sizeof(retval)) {
419 pa_log(__FILE__": read() failed: %s", pa_cstrerror(errno));
420 retval = 1;
421 }
422
423 if (retval)
424 pa_log(__FILE__": daemon startup failed.");
425 else
426 pa_log_info(__FILE__": daemon startup successful.");
427
428 goto finish;
429 }
430
431 close(daemon_pipe[0]);
432 daemon_pipe[0] = -1;
433 #endif
434
435 if (conf->auto_log_target)
436 pa_log_set_target(PA_LOG_SYSLOG, NULL);
437
438 #ifdef HAVE_SETSID
439 setsid();
440 #endif
441 #ifdef HAVE_SETPGID
442 setpgid(0,0);
443 #endif
444
445 #ifndef OS_IS_WIN32
446 close(0);
447 close(1);
448 close(2);
449
450 open("/dev/null", O_RDONLY);
451 open("/dev/null", O_WRONLY);
452 open("/dev/null", O_WRONLY);
453 #else
454 FreeConsole();
455 #endif
456
457 #ifdef SIGTTOU
458 signal(SIGTTOU, SIG_IGN);
459 #endif
460 #ifdef SIGTTIN
461 signal(SIGTTIN, SIG_IGN);
462 #endif
463 #ifdef SIGTSTP
464 signal(SIGTSTP, SIG_IGN);
465 #endif
466
467 #ifdef TIOCNOTTY
468 if ((tty_fd = open("/dev/tty", O_RDWR)) >= 0) {
469 ioctl(tty_fd, TIOCNOTTY, (char*) 0);
470 close(tty_fd);
471 }
472 #endif
473 }
474
475 chdir("/");
476 umask(0022);
477
478 if (conf->system_instance) {
479 if (change_user() < 0)
480 goto finish;
481 } else if (create_runtime_dir() < 0)
482 goto finish;
483
484 if (conf->use_pid_file) {
485 if (pa_pid_file_create() < 0) {
486 pa_log(__FILE__": pa_pid_file_create() failed.");
487 #ifdef HAVE_FORK
488 if (conf->daemonize)
489 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL);
490 #endif
491 goto finish;
492 }
493
494 valid_pid_file = 1;
495 }
496
497 #ifdef SIGPIPE
498 signal(SIGPIPE, SIG_IGN);
499 #endif
500
501 mainloop = pa_mainloop_new();
502 assert(mainloop);
503
504 c = pa_core_new(pa_mainloop_get_api(mainloop));
505 assert(c);
506 c->is_system_instance = !!conf->system_instance;
507
508 r = pa_signal_init(pa_mainloop_get_api(mainloop));
509 assert(r == 0);
510 pa_signal_new(SIGINT, signal_callback, c);
511 pa_signal_new(SIGTERM, signal_callback, c);
512
513 #ifdef SIGUSR1
514 pa_signal_new(SIGUSR1, signal_callback, c);
515 #endif
516 #ifdef SIGUSR2
517 pa_signal_new(SIGUSR2, signal_callback, c);
518 #endif
519 #ifdef SIGHUP
520 pa_signal_new(SIGHUP, signal_callback, c);
521 #endif
522
523 #ifdef OS_IS_WIN32
524 timer = pa_mainloop_get_api(mainloop)->time_new(
525 pa_mainloop_get_api(mainloop), pa_gettimeofday(&tv), message_cb, NULL);
526 assert(timer);
527 #endif
528
529 if (conf->daemonize)
530 c->running_as_daemon = 1;
531
532 oil_init();
533
534 r = pa_cpu_limit_init(pa_mainloop_get_api(mainloop));
535 assert(r == 0);
536
537 buf = pa_strbuf_new();
538 if (conf->default_script_file)
539 r = pa_cli_command_execute_file(c, conf->default_script_file, buf, &conf->fail);
540
541 if (r >= 0)
542 r = pa_cli_command_execute(c, conf->script_commands, buf, &conf->fail);
543 pa_log_error("%s", s = pa_strbuf_tostring_free(buf));
544 pa_xfree(s);
545
546 if (r < 0 && conf->fail) {
547 pa_log(__FILE__": failed to initialize daemon.");
548 #ifdef HAVE_FORK
549 if (conf->daemonize)
550 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL);
551 #endif
552 } else if (!c->modules || pa_idxset_size(c->modules) == 0) {
553 pa_log(__FILE__": daemon startup without any loaded modules, refusing to work.");
554 #ifdef HAVE_FORK
555 if (conf->daemonize)
556 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL);
557 #endif
558 } else {
559
560 retval = 0;
561 #ifdef HAVE_FORK
562 if (conf->daemonize)
563 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval), NULL);
564 #endif
565
566 c->disallow_module_loading = conf->disallow_module_loading;
567 c->exit_idle_time = conf->exit_idle_time;
568 c->module_idle_time = conf->module_idle_time;
569 c->scache_idle_time = conf->scache_idle_time;
570 c->resample_method = conf->resample_method;
571
572 if (c->default_sink_name &&
573 pa_namereg_get(c, c->default_sink_name, PA_NAMEREG_SINK, 1) == NULL) {
574 pa_log_error("%s : Fatal error. Default sink name (%s) does not exist in name register.", __FILE__, c->default_sink_name);
575 retval = 1;
576 } else {
577 pa_log_info(__FILE__": Daemon startup complete.");
578 if (pa_mainloop_run(mainloop, &retval) < 0)
579 retval = 1;
580 pa_log_info(__FILE__": Daemon shutdown initiated.");
581 }
582 }
583
584 #ifdef OS_IS_WIN32
585 pa_mainloop_get_api(mainloop)->time_free(timer);
586 #endif
587
588 pa_core_free(c);
589
590 pa_cpu_limit_done();
591 pa_signal_done();
592 pa_mainloop_free(mainloop);
593
594 pa_log_info(__FILE__": Daemon terminated.");
595
596 finish:
597
598 if (conf)
599 pa_daemon_conf_free(conf);
600
601 if (valid_pid_file)
602 pa_pid_file_remove();
603
604 close_pipe(daemon_pipe);
605
606 #ifdef OS_IS_WIN32
607 WSACleanup();
608 #endif
609
610 lt_dlexit();
611
612 return retval;
613 }