4 This file is part of PulseAudio.
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.
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.
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
39 #include <sys/types.h>
43 #include <liboil/liboil.h>
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
54 #include "../pulsecore/winsock.h"
56 #include <pulse/mainloop.h>
57 #include <pulse/mainloop-signal.h>
58 #include <pulse/timeval.h>
59 #include <pulse/xmalloc.h>
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>
76 #include "daemon-conf.h"
77 #include "dumpmodules.h"
81 /* Only one instance of these variables */
82 int allow_severity
= LOG_INFO
;
83 int deny_severity
= LOG_WARNING
;
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;
95 static void message_cb(pa_mainloop_api
*a
, pa_time_event
*e
, PA_GCC_UNUSED
const struct timeval
*tv
, void *userdata
) {
97 struct timeval tvnext
;
99 while(PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
100 if (msg
.message
== WM_QUIT
)
103 TranslateMessage(&msg
);
104 DispatchMessage(&msg
);
108 pa_timeval_add(pa_gettimeofday(&tvnext
), 100000);
109 a
->time_restart(e
, &tvnext
);
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
));
120 pa_module_load(userdata
, "module-cli", NULL
);
126 pa_module_load(userdata
, "module-cli-protocol-unix", NULL
);
132 char *c
= pa_full_status_string(userdata
);
133 pa_log_notice("%s", c
);
142 pa_log_info(__FILE__
": Exiting.");
148 static void close_pipe(int p
[2]) {
156 #define set_env(key, value) putenv(pa_sprintf_malloc("%s=%s", (key), (value)))
158 static int change_user(void) {
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
167 if (!(pw
= getpwnam(PA_SYSTEM_USER
))) {
168 pa_log(__FILE__
": Failed to find user '%s'.", PA_SYSTEM_USER
);
172 if (!(gr
= getgrnam(PA_SYSTEM_GROUP
))) {
173 pa_log(__FILE__
": Failed to find group '%s'.", PA_SYSTEM_GROUP
);
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
);
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
);
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
);
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
));
194 if (initgroups(PA_SYSTEM_USER
, gr
->gr_gid
) != 0) {
195 pa_log(__FILE__
": Failed to change group list: %s", pa_cstrerror(errno
));
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
);
207 #error "No API to drop priviliges"
211 pa_log(__FILE__
": Failed to change GID: %s", pa_cstrerror(errno
));
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
);
223 #error "No API to drop priviliges"
227 pa_log(__FILE__
": Failed to change UID: %s", pa_cstrerror(errno
));
231 set_env("USER", PA_SYSTEM_USER
);
232 set_env("LOGNAME", PA_SYSTEM_GROUP
);
233 set_env("HOME", PA_SYSTEM_RUNTIME_PATH
);
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
);
239 pa_log_info(__FILE__
": Successfully dropped root privileges.");
244 static int create_runtime_dir(void) {
247 pa_runtime_path(NULL
, fn
, sizeof(fn
));
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 */
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
));
261 int main(int argc
, char *argv
[]) {
263 pa_strbuf
*buf
= NULL
;
264 pa_daemon_conf
*conf
;
265 pa_mainloop
*mainloop
;
268 int r
, retval
= 1, d
= 0;
269 int daemon_pipe
[2] = { -1, -1 };
271 int valid_pid_file
= 0;
274 gid_t gid
= (gid_t
) -1;
278 pa_time_event
*timer
;
282 setlocale(LC_ALL
, "");
288 suid_root
= getuid() != 0 && geteuid() == 0;
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
"'.");
298 LTDL_SET_PRELOADED_SYMBOLS();
306 WSAStartup(MAKEWORD(2, 0), &data
);
312 pa_log_set_ident("pulseaudio");
314 conf
= pa_daemon_conf_new();
316 if (pa_daemon_conf_load(conf
, NULL
) < 0)
319 if (pa_daemon_conf_env(conf
) < 0)
322 if (pa_cmdline_parse(conf
, argc
, argv
, &d
) < 0) {
323 pa_log(__FILE__
": failed to parse command line.");
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
);
330 if (conf
->high_priority
&& conf
->cmd
== PA_CMD_DAEMON
)
339 if (conf
->dl_search_path
)
340 lt_dlsetsearchpath(conf
->dl_search_path
);
343 case PA_CMD_DUMP_MODULES
:
344 pa_dump_modules(conf
, argc
-d
, argv
+d
);
348 case PA_CMD_DUMP_CONF
: {
349 s
= pa_daemon_conf_dump(conf
);
357 pa_cmdline_help(argv
[0]);
361 case PA_CMD_VERSION
:
362 printf(PACKAGE_NAME
" "PACKAGE_VERSION
"\n");
369 if (pa_pid_file_check_running(&pid
) < 0) {
370 pa_log_info(__FILE__
": daemon not running");
372 pa_log_info(__FILE__
": daemon running as PID %u", pid
);
381 if (pa_pid_file_kill(SIGINT
, NULL
) < 0)
382 pa_log(__FILE__
": failed to kill daemon.");
389 assert(conf
->cmd
== PA_CMD_DAEMON
);
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).");
395 } else if (getuid() != 0 && conf
->system_instance
) {
396 pa_log(__FILE__
": Root priviliges required.");
400 if (conf
->daemonize
) {
404 if (pa_stdio_acquire() < 0) {
405 pa_log(__FILE__
": failed to acquire stdio.");
410 if (pipe(daemon_pipe
) < 0) {
411 pa_log(__FILE__
": failed to create pipe.");
415 if ((child
= fork()) < 0) {
416 pa_log(__FILE__
": fork() failed: %s", pa_cstrerror(errno
));
423 close(daemon_pipe
[1]);
426 if (pa_loop_read(daemon_pipe
[0], &retval
, sizeof(retval
), NULL
) != sizeof(retval
)) {
427 pa_log(__FILE__
": read() failed: %s", pa_cstrerror(errno
));
432 pa_log(__FILE__
": daemon startup failed.");
434 pa_log_info(__FILE__
": daemon startup successful.");
439 close(daemon_pipe
[0]);
443 if (conf
->auto_log_target
)
444 pa_log_set_target(PA_LOG_SYSLOG
, NULL
);
458 open("/dev/null", O_RDONLY
);
459 open("/dev/null", O_WRONLY
);
460 open("/dev/null", O_WRONLY
);
466 signal(SIGTTOU
, SIG_IGN
);
469 signal(SIGTTIN
, SIG_IGN
);
472 signal(SIGTSTP
, SIG_IGN
);
476 if ((tty_fd
= open("/dev/tty", O_RDWR
)) >= 0) {
477 ioctl(tty_fd
, TIOCNOTTY
, (char*) 0);
486 if (conf
->system_instance
) {
487 if (change_user() < 0)
489 } else if (create_runtime_dir() < 0)
492 if (conf
->use_pid_file
) {
493 if (pa_pid_file_create() < 0) {
494 pa_log(__FILE__
": pa_pid_file_create() failed.");
497 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
), NULL
);
506 signal(SIGPIPE
, SIG_IGN
);
509 mainloop
= pa_mainloop_new();
512 c
= pa_core_new(pa_mainloop_get_api(mainloop
));
514 c
->is_system_instance
= !!conf
->system_instance
;
516 r
= pa_signal_init(pa_mainloop_get_api(mainloop
));
518 pa_signal_new(SIGINT
, signal_callback
, c
);
519 pa_signal_new(SIGTERM
, signal_callback
, c
);
522 pa_signal_new(SIGUSR1
, signal_callback
, c
);
525 pa_signal_new(SIGUSR2
, signal_callback
, c
);
528 pa_signal_new(SIGHUP
, signal_callback
, c
);
532 timer
= pa_mainloop_get_api(mainloop
)->time_new(
533 pa_mainloop_get_api(mainloop
), pa_gettimeofday(&tv
), message_cb
, NULL
);
538 c
->running_as_daemon
= 1;
542 r
= pa_cpu_limit_init(pa_mainloop_get_api(mainloop
));
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
);
550 r
= pa_cli_command_execute(c
, conf
->script_commands
, buf
, &conf
->fail
);
551 pa_log_error("%s", s
= pa_strbuf_tostring_free(buf
));
554 if (r
< 0 && conf
->fail
) {
555 pa_log(__FILE__
": failed to initialize daemon.");
558 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
), NULL
);
560 } else if (!c
->modules
|| pa_idxset_size(c
->modules
) == 0) {
561 pa_log(__FILE__
": daemon startup without any loaded modules, refusing to work.");
564 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
), NULL
);
571 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
), NULL
);
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
;
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
);
585 pa_log_info(__FILE__
": Daemon startup complete.");
586 if (pa_mainloop_run(mainloop
, &retval
) < 0)
588 pa_log_info(__FILE__
": Daemon shutdown initiated.");
593 pa_mainloop_get_api(mainloop
)->time_free(timer
);
600 pa_mainloop_free(mainloop
);
602 pa_log_info(__FILE__
": Daemon terminated.");
607 pa_daemon_conf_free(conf
);
610 pa_pid_file_remove();
612 close_pipe(daemon_pipe
);