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