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/xmalloc.h>
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>
75 #include "daemon-conf.h"
76 #include "dumpmodules.h"
80 /* Only one instance of these variables */
81 int allow_severity
= LOG_INFO
;
82 int deny_severity
= LOG_WARNING
;
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;
94 static void message_cb(pa_mainloop_api
*a
, pa_time_event
*e
, PA_GCC_UNUSED
const struct timeval
*tv
, void *userdata
) {
96 struct timeval tvnext
;
98 while(PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
99 if (msg
.message
== WM_QUIT
)
102 TranslateMessage(&msg
);
103 DispatchMessage(&msg
);
107 pa_timeval_add(pa_gettimeofday(&tvnext
), 100000);
108 a
->time_restart(e
, &tvnext
);
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
));
119 pa_module_load(userdata
, "module-cli", NULL
);
125 pa_module_load(userdata
, "module-cli-protocol-unix", NULL
);
131 char *c
= pa_full_status_string(userdata
);
132 pa_log_notice("%s", c
);
141 pa_log_info(__FILE__
": Exiting.");
147 static void close_pipe(int p
[2]) {
155 #define set_env(key, value) putenv(pa_sprintf_malloc("%s=%s", (key), (value)))
157 static int change_user(void) {
162 if (!(pw
= getpwnam(PA_SYSTEM_USER
))) {
163 pa_log(__FILE__
": Failed to find user '%s'.", PA_SYSTEM_USER
);
167 if (!(gr
= getgrnam(PA_SYSTEM_GROUP
))) {
168 pa_log(__FILE__
": Failed to find group '%s'.", PA_SYSTEM_GROUP
);
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
);
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
);
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
);
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
));
189 if (initgroups(PA_SYSTEM_USER
, gr
->gr_gid
) != 0) {
190 pa_log(__FILE__
": Failed to change group list: %s", pa_cstrerror(errno
));
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
);
202 #error "No API to drop priviliges"
206 pa_log(__FILE__
": Failed to change GID: %s", pa_cstrerror(errno
));
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
);
218 #error "No API to drop priviliges"
222 pa_log(__FILE__
": Failed to change UID: %s", pa_cstrerror(errno
));
226 set_env("USER", PA_SYSTEM_USER
);
227 set_env("LOGNAME", PA_SYSTEM_GROUP
);
228 set_env("HOME", PA_SYSTEM_RUNTIME_PATH
);
230 /* Relevant for pa_runtime_path() */
231 set_env("PULSE_RUNTIME_PATH", PA_SYSTEM_RUNTIME_PATH
);
232 set_env("PULSE_CONFIG_PATH", PA_SYSTEM_RUNTIME_PATH
);
234 pa_log_info(__FILE__
": Successfully dropped root privileges.");
239 static int create_runtime_dir(void) {
242 pa_runtime_path(NULL
, fn
, sizeof(fn
));
244 if (pa_make_secure_dir(fn
, 0700, getuid(), getgid()) < 0) {
245 pa_log(__FILE__
": Failed to create '%s': %s", fn
, pa_cstrerror(errno
));
252 int main(int argc
, char *argv
[]) {
254 pa_strbuf
*buf
= NULL
;
255 pa_daemon_conf
*conf
;
256 pa_mainloop
*mainloop
;
259 int r
, retval
= 1, d
= 0;
260 int daemon_pipe
[2] = { -1, -1 };
262 int valid_pid_file
= 0;
265 gid_t gid
= (gid_t
) -1;
269 pa_time_event
*timer
;
273 setlocale(LC_ALL
, "");
279 suid_root
= getuid() != 0 && geteuid() == 0;
281 if (suid_root
&& (pa_own_uid_in_group(PA_REALTIME_GROUP
, &gid
) <= 0 || gid
>= 1000)) {
282 pa_log_warn(__FILE__
": WARNING: called SUID root, but not in group '"PA_REALTIME_GROUP
"'.");
289 LTDL_SET_PRELOADED_SYMBOLS();
297 WSAStartup(MAKEWORD(2, 0), &data
);
303 pa_log_set_ident("pulseaudio");
305 conf
= pa_daemon_conf_new();
307 if (pa_daemon_conf_load(conf
, NULL
) < 0)
310 if (pa_daemon_conf_env(conf
) < 0)
313 if (pa_cmdline_parse(conf
, argc
, argv
, &d
) < 0) {
314 pa_log(__FILE__
": failed to parse command line.");
318 pa_log_set_maximal_level(conf
->log_level
);
319 pa_log_set_target(conf
->auto_log_target
? PA_LOG_STDERR
: conf
->log_target
, NULL
);
321 if (conf
->high_priority
&& conf
->cmd
== PA_CMD_DAEMON
)
330 if (conf
->dl_search_path
)
331 lt_dlsetsearchpath(conf
->dl_search_path
);
334 case PA_CMD_DUMP_MODULES
:
335 pa_dump_modules(conf
, argc
-d
, argv
+d
);
339 case PA_CMD_DUMP_CONF
: {
340 s
= pa_daemon_conf_dump(conf
);
348 pa_cmdline_help(argv
[0]);
352 case PA_CMD_VERSION
:
353 printf(PACKAGE_NAME
" "PACKAGE_VERSION
"\n");
360 if (pa_pid_file_check_running(&pid
) < 0) {
361 pa_log_info(__FILE__
": daemon not running");
363 pa_log_info(__FILE__
": daemon running as PID %u", pid
);
372 if (pa_pid_file_kill(SIGINT
, NULL
) < 0)
373 pa_log(__FILE__
": failed to kill daemon.");
380 assert(conf
->cmd
== PA_CMD_DAEMON
);
383 if (getuid() == 0 && !conf
->system_instance
) {
384 pa_log(__FILE__
": This program is not intended to be run as root (unless --system is specified).");
386 } else if (getuid() != 0 && conf
->system_instance
) {
387 pa_log(__FILE__
": Root priviliges required.");
391 if (conf
->daemonize
) {
395 if (pa_stdio_acquire() < 0) {
396 pa_log(__FILE__
": failed to acquire stdio.");
401 if (pipe(daemon_pipe
) < 0) {
402 pa_log(__FILE__
": failed to create pipe.");
406 if ((child
= fork()) < 0) {
407 pa_log(__FILE__
": fork() failed: %s", pa_cstrerror(errno
));
414 close(daemon_pipe
[1]);
417 if (pa_loop_read(daemon_pipe
[0], &retval
, sizeof(retval
), NULL
) != sizeof(retval
)) {
418 pa_log(__FILE__
": read() failed: %s", pa_cstrerror(errno
));
423 pa_log(__FILE__
": daemon startup failed.");
425 pa_log_info(__FILE__
": daemon startup successful.");
430 close(daemon_pipe
[0]);
434 if (conf
->auto_log_target
)
435 pa_log_set_target(PA_LOG_SYSLOG
, NULL
);
449 open("/dev/null", O_RDONLY
);
450 open("/dev/null", O_WRONLY
);
451 open("/dev/null", O_WRONLY
);
457 signal(SIGTTOU
, SIG_IGN
);
460 signal(SIGTTIN
, SIG_IGN
);
463 signal(SIGTSTP
, SIG_IGN
);
467 if ((tty_fd
= open("/dev/tty", O_RDWR
)) >= 0) {
468 ioctl(tty_fd
, TIOCNOTTY
, (char*) 0);
477 if (conf
->system_instance
) {
478 if (change_user() < 0)
480 } else if (create_runtime_dir() < 0)
483 if (conf
->use_pid_file
) {
484 if (pa_pid_file_create() < 0) {
485 pa_log(__FILE__
": pa_pid_file_create() failed.");
488 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
), NULL
);
497 signal(SIGPIPE
, SIG_IGN
);
500 mainloop
= pa_mainloop_new();
503 c
= pa_core_new(pa_mainloop_get_api(mainloop
));
505 c
->is_system_instance
= !!conf
->system_instance
;
507 r
= pa_signal_init(pa_mainloop_get_api(mainloop
));
509 pa_signal_new(SIGINT
, signal_callback
, c
);
510 pa_signal_new(SIGTERM
, signal_callback
, c
);
513 pa_signal_new(SIGUSR1
, signal_callback
, c
);
516 pa_signal_new(SIGUSR2
, signal_callback
, c
);
519 pa_signal_new(SIGHUP
, signal_callback
, c
);
523 timer
= pa_mainloop_get_api(mainloop
)->time_new(
524 pa_mainloop_get_api(mainloop
), pa_gettimeofday(&tv
), message_cb
, NULL
);
529 c
->running_as_daemon
= 1;
533 r
= pa_cpu_limit_init(pa_mainloop_get_api(mainloop
));
536 buf
= pa_strbuf_new();
537 if (conf
->default_script_file
)
538 r
= pa_cli_command_execute_file(c
, conf
->default_script_file
, buf
, &conf
->fail
);
541 r
= pa_cli_command_execute(c
, conf
->script_commands
, buf
, &conf
->fail
);
542 pa_log_error("%s", s
= pa_strbuf_tostring_free(buf
));
545 if (r
< 0 && conf
->fail
) {
546 pa_log(__FILE__
": failed to initialize daemon.");
549 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
), NULL
);
551 } else if (!c
->modules
|| pa_idxset_size(c
->modules
) == 0) {
552 pa_log(__FILE__
": daemon startup without any loaded modules, refusing to work.");
555 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
), NULL
);
562 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
), NULL
);
565 c
->disallow_module_loading
= conf
->disallow_module_loading
;
566 c
->exit_idle_time
= conf
->exit_idle_time
;
567 c
->module_idle_time
= conf
->module_idle_time
;
568 c
->scache_idle_time
= conf
->scache_idle_time
;
569 c
->resample_method
= conf
->resample_method
;
571 if (c
->default_sink_name
&&
572 pa_namereg_get(c
, c
->default_sink_name
, PA_NAMEREG_SINK
, 1) == NULL
) {
573 pa_log_error("%s : Fatal error. Default sink name (%s) does not exist in name register.", __FILE__
, c
->default_sink_name
);
576 pa_log_info(__FILE__
": Daemon startup complete.");
577 if (pa_mainloop_run(mainloop
, &retval
) < 0)
579 pa_log_info(__FILE__
": Daemon shutdown initiated.");
584 pa_mainloop_get_api(mainloop
)->time_free(timer
);
591 pa_mainloop_free(mainloop
);
593 pa_log_info(__FILE__
": Daemon terminated.");
598 pa_daemon_conf_free(conf
);
601 pa_pid_file_remove();
603 close_pipe(daemon_pipe
);