4 This file is part of polypaudio.
6 polypaudio 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 polypaudio 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 polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
38 #include <sys/types.h>
39 #include <liboil/liboil.h>
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
50 #include "../polypcore/winsock.h"
52 #include <polyp/mainloop.h>
53 #include <polyp/mainloop-signal.h>
55 #include <polypcore/core.h>
56 #include <polypcore/memblock.h>
57 #include <polypcore/module.h>
58 #include <polypcore/cli-command.h>
59 #include <polypcore/log.h>
60 #include <polypcore/util.h>
61 #include <polypcore/sioman.h>
62 #include <polypcore/xmalloc.h>
63 #include <polypcore/cli-text.h>
64 #include <polypcore/pid.h>
65 #include <polypcore/namereg.h>
69 #include "daemon-conf.h"
70 #include "dumpmodules.h"
74 /* Only one instance of these variables */
75 int allow_severity
= LOG_INFO
;
76 int deny_severity
= LOG_WARNING
;
81 static void message_cb(pa_mainloop_api
*a
, pa_defer_event
*e
, void *userdata
) {
84 while(PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
85 if (msg
.message
== WM_QUIT
)
88 TranslateMessage(&msg
);
89 DispatchMessage(&msg
);
96 static void signal_callback(pa_mainloop_api
*m
, PA_GCC_UNUSED pa_signal_event
*e
, int sig
, void *userdata
) {
97 pa_log_info(__FILE__
": Got signal %s.", pa_strsignal(sig
));
102 pa_module_load(userdata
, "module-cli", NULL
);
108 pa_module_load(userdata
, "module-cli-protocol-unix", NULL
);
114 char *c
= pa_full_status_string(userdata
);
115 pa_log_notice("%s", c
);
124 pa_log_info(__FILE__
": Exiting.");
130 static void close_pipe(int p
[2]) {
138 static void set_random_seed(void) {
139 unsigned int seed
= 0;
144 if ((fd
= open(RANDOM_DEVICE
, O_RDONLY
)) >= 0) {
147 if ((r
= pa_loop_read(fd
, &seed
, sizeof(seed
))) < 0 || (size_t) r
!= sizeof(seed
)) {
148 pa_log_error(__FILE__
": failed to read entropy from '"RANDOM_DEVICE
"'");
149 seed
+= (unsigned int) time(NULL
);
155 seed
= (unsigned int) time(NULL
);
161 int main(int argc
, char *argv
[]) {
163 pa_strbuf
*buf
= NULL
;
164 pa_daemon_conf
*conf
;
165 pa_mainloop
*mainloop
;
168 int r
, retval
= 1, d
= 0;
169 int daemon_pipe
[2] = { -1, -1 };
171 int valid_pid_file
= 0;
174 gid_t gid
= (gid_t
) -1;
178 pa_defer_event
*defer
;
184 suid_root
= getuid() != 0 && geteuid() == 0;
186 if (suid_root
&& (pa_own_uid_in_group("realtime", &gid
) <= 0 || gid
>= 1000)) {
187 pa_log_warn(__FILE__
": WARNING: called SUID root, but not in group 'realtime'.");
194 LTDL_SET_PRELOADED_SYMBOLS();
202 WSAStartup(MAKEWORD(2, 0), &data
);
208 pa_log_set_ident("polypaudio");
210 conf
= pa_daemon_conf_new();
212 if (pa_daemon_conf_load(conf
, NULL
) < 0)
215 if (pa_daemon_conf_env(conf
) < 0)
218 if (pa_cmdline_parse(conf
, argc
, argv
, &d
) < 0) {
219 pa_log(__FILE__
": failed to parse command line.");
223 pa_log_set_maximal_level(conf
->log_level
);
224 pa_log_set_target(conf
->auto_log_target
? PA_LOG_STDERR
: conf
->log_target
, NULL
);
226 if (conf
->high_priority
&& conf
->cmd
== PA_CMD_DAEMON
)
234 if (conf
->dl_search_path
)
235 lt_dlsetsearchpath(conf
->dl_search_path
);
238 case PA_CMD_DUMP_MODULES
:
239 pa_dump_modules(conf
, argc
-d
, argv
+d
);
243 case PA_CMD_DUMP_CONF
: {
244 s
= pa_daemon_conf_dump(conf
);
252 pa_cmdline_help(argv
[0]);
256 case PA_CMD_VERSION
:
257 printf(PACKAGE_NAME
" "PACKAGE_VERSION
"\n");
264 if (pa_pid_file_check_running(&pid
) < 0) {
265 pa_log_info(__FILE__
": daemon not running");
267 pa_log_info(__FILE__
": daemon running as PID %u", pid
);
276 if (pa_pid_file_kill(SIGINT
, NULL
) < 0)
277 pa_log(__FILE__
": failed to kill daemon.");
284 assert(conf
->cmd
== PA_CMD_DAEMON
);
287 if (conf
->daemonize
) {
291 if (pa_stdio_acquire() < 0) {
292 pa_log(__FILE__
": failed to acquire stdio.");
297 if (pipe(daemon_pipe
) < 0) {
298 pa_log(__FILE__
": failed to create pipe.");
302 if ((child
= fork()) < 0) {
303 pa_log(__FILE__
": fork() failed: %s", strerror(errno
));
310 close(daemon_pipe
[1]);
313 if (pa_loop_read(daemon_pipe
[0], &retval
, sizeof(retval
)) != sizeof(retval
)) {
314 pa_log(__FILE__
": read() failed: %s", strerror(errno
));
319 pa_log(__FILE__
": daemon startup failed.");
321 pa_log_info(__FILE__
": daemon startup successful.");
326 close(daemon_pipe
[0]);
330 if (conf
->auto_log_target
)
331 pa_log_set_target(PA_LOG_SYSLOG
, NULL
);
345 open("/dev/null", O_RDONLY
);
346 open("/dev/null", O_WRONLY
);
347 open("/dev/null", O_WRONLY
);
353 signal(SIGTTOU
, SIG_IGN
);
356 signal(SIGTTIN
, SIG_IGN
);
359 signal(SIGTSTP
, SIG_IGN
);
363 if ((tty_fd
= open("/dev/tty", O_RDWR
)) >= 0) {
364 ioctl(tty_fd
, TIOCNOTTY
, (char*) 0);
372 if (conf
->use_pid_file
) {
373 if (pa_pid_file_create() < 0) {
374 pa_log(__FILE__
": pa_pid_file_create() failed.");
377 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
386 signal(SIGPIPE
, SIG_IGN
);
389 mainloop
= pa_mainloop_new();
392 c
= pa_core_new(pa_mainloop_get_api(mainloop
));
395 r
= pa_signal_init(pa_mainloop_get_api(mainloop
));
397 pa_signal_new(SIGINT
, signal_callback
, c
);
398 pa_signal_new(SIGTERM
, signal_callback
, c
);
401 pa_signal_new(SIGUSR1
, signal_callback
, c
);
404 pa_signal_new(SIGUSR2
, signal_callback
, c
);
407 pa_signal_new(SIGHUP
, signal_callback
, c
);
411 defer
= pa_mainloop_get_api(mainloop
)->defer_new(pa_mainloop_get_api(mainloop
), message_cb
, NULL
);
416 c
->running_as_daemon
= 1;
420 r
= pa_cpu_limit_init(pa_mainloop_get_api(mainloop
));
423 buf
= pa_strbuf_new();
424 if (conf
->default_script_file
)
425 r
= pa_cli_command_execute_file(c
, conf
->default_script_file
, buf
, &conf
->fail
);
428 r
= pa_cli_command_execute(c
, conf
->script_commands
, buf
, &conf
->fail
);
429 pa_log_error("%s", s
= pa_strbuf_tostring_free(buf
));
432 if (r
< 0 && conf
->fail
) {
433 pa_log(__FILE__
": failed to initialize daemon.");
436 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
438 } else if (!c
->modules
|| pa_idxset_size(c
->modules
) == 0) {
439 pa_log(__FILE__
": daemon startup without any loaded modules, refusing to work.");
442 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
449 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
452 c
->disallow_module_loading
= conf
->disallow_module_loading
;
453 c
->exit_idle_time
= conf
->exit_idle_time
;
454 c
->module_idle_time
= conf
->module_idle_time
;
455 c
->scache_idle_time
= conf
->scache_idle_time
;
456 c
->resample_method
= conf
->resample_method
;
458 if (c
->default_sink_name
&&
459 pa_namereg_get(c
, c
->default_sink_name
, PA_NAMEREG_SINK
, 1) == NULL
) {
460 pa_log_error("%s : Fatal error. Default sink name (%s) does not exist in name register.", __FILE__
, c
->default_sink_name
);
463 pa_log_info(__FILE__
": Daemon startup complete.");
464 if (pa_mainloop_run(mainloop
, &retval
) < 0)
466 pa_log_info(__FILE__
": Daemon shutdown initiated.");
471 pa_mainloop_get_api(mainloop
)->defer_free(defer
);
478 pa_mainloop_free(mainloop
);
480 pa_log_info(__FILE__
": Daemon terminated.");
485 pa_daemon_conf_free(conf
);
488 pa_pid_file_remove();
490 close_pipe(daemon_pipe
);