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/ioctl.h>
48 #include "mainloop-signal.h"
50 #include "cli-command.h"
56 #include "daemon-conf.h"
57 #include "dumpmodules.h"
64 /* Only one instance of these variables */
65 int allow_severity = LOG_INFO;
66 int deny_severity = LOG_WARNING;
69 static void signal_callback(struct pa_mainloop_api*m, struct pa_signal_event *e, int sig, void *userdata) {
70 pa_log_info(__FILE__": Got signal %s.\n", pa_strsignal(sig));
74 pa_module_load(userdata, "module-cli", NULL);
78 pa_module_load(userdata, "module-cli-protocol-unix", NULL);
82 char *c = pa_full_status_string(userdata);
91 pa_log_info(__FILE__": Exiting.\n");
97 static void close_pipe(int p[2]) {
105 int main(int argc, char *argv[]) {
107 struct pa_strbuf *buf = NULL;
108 struct pa_daemon_conf *conf;
109 struct pa_mainloop *mainloop;
112 int r, retval = 1, d = 0;
113 int daemon_pipe[2] = { -1, -1 };
114 gid_t gid = (gid_t) -1;
116 int valid_pid_file = 0;
120 suid_root = getuid() != 0 && geteuid() == 0;
122 if (suid_root && (pa_uid_in_group("realtime", &gid) <= 0 || gid >= 1000)) {
123 pa_log_warn(__FILE__": WARNING: called SUID root, but not in group 'realtime'.\n");
127 LTDL_SET_PRELOADED_SYMBOLS();
132 pa_log_set_ident("polypaudio");
134 conf = pa_daemon_conf_new();
136 if (pa_daemon_conf_load(conf, NULL) < 0)
139 if (pa_daemon_conf_env(conf) < 0)
142 if (pa_cmdline_parse(conf, argc, argv, &d) < 0) {
143 pa_log(__FILE__": failed to parse command line.\n");
147 pa_log_set_maximal_level(conf->log_level);
148 pa_log_set_target(conf->auto_log_target ? PA_LOG_STDERR : conf->log_target, NULL);
150 if (conf->high_priority && conf->cmd == PA_CMD_DAEMON)
158 if (conf->dl_search_path)
159 lt_dlsetsearchpath(conf->dl_search_path);
162 case PA_CMD_DUMP_MODULES:
163 pa_dump_modules(conf, argc-d, argv+d);
167 case PA_CMD_DUMP_CONF: {
168 char *s = pa_daemon_conf_dump(conf);
176 pa_cmdline_help(argv[0]);
180 case PA_CMD_VERSION :
181 printf(PACKAGE_NAME" "PACKAGE_VERSION"\n");
188 if (pa_pid_file_check_running(&pid) < 0) {
189 pa_log_info(__FILE__": daemon not running\n");
191 pa_log_info(__FILE__": daemon running as PID %u\n", pid);
200 if (pa_pid_file_kill(SIGINT, NULL) < 0)
201 pa_log(__FILE__": failed to kill daemon.\n");
208 assert(conf->cmd == PA_CMD_DAEMON);
211 if (conf->daemonize) {
215 if (pa_stdio_acquire() < 0) {
216 pa_log(__FILE__": failed to acquire stdio.\n");
220 if (pipe(daemon_pipe) < 0) {
221 pa_log(__FILE__": failed to create pipe.\n");
225 if ((child = fork()) < 0) {
226 pa_log(__FILE__": fork() failed: %s\n", strerror(errno));
233 close(daemon_pipe[1]);
236 if (pa_loop_read(daemon_pipe[0], &retval, sizeof(retval)) != sizeof(retval)) {
237 pa_log(__FILE__": read() failed: %s\n", strerror(errno));
242 pa_log(__FILE__": daemon startup failed.\n");
244 pa_log_info(__FILE__": daemon startup successful.\n");
249 close(daemon_pipe[0]);
252 if (conf->auto_log_target)
253 pa_log_set_target(PA_LOG_SYSLOG, NULL);
262 open("/dev/null", O_RDONLY);
263 open("/dev/null", O_WRONLY);
264 open("/dev/null", O_WRONLY);
266 signal(SIGTTOU, SIG_IGN);
267 signal(SIGTTIN, SIG_IGN);
268 signal(SIGTSTP, SIG_IGN);
270 if ((tty_fd = open("/dev/tty", O_RDWR)) >= 0) {
271 ioctl(tty_fd, TIOCNOTTY, (char*) 0);
278 if (conf->use_pid_file) {
279 if (pa_pid_file_create() < 0) {
281 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
288 mainloop = pa_mainloop_new();
291 r = pa_signal_init(pa_mainloop_get_api(mainloop));
293 pa_signal_new(SIGINT, signal_callback, c);
294 pa_signal_new(SIGTERM, signal_callback, c);
295 signal(SIGPIPE, SIG_IGN);
297 c = pa_core_new(pa_mainloop_get_api(mainloop));
300 pa_signal_new(SIGUSR1, signal_callback, c);
301 pa_signal_new(SIGUSR2, signal_callback, c);
302 pa_signal_new(SIGHUP, signal_callback, c);
304 r = pa_cpu_limit_init(pa_mainloop_get_api(mainloop));
307 buf = pa_strbuf_new();
309 if (conf->default_script_file)
310 r = pa_cli_command_execute_file(c, conf->default_script_file, buf, &conf->fail);
313 r = pa_cli_command_execute(c, conf->script_commands, buf, &conf->fail);
314 pa_log(s = pa_strbuf_tostring_free(buf));
317 if (r < 0 && conf->fail) {
318 pa_log(__FILE__": failed to initialize daemon.\n");
320 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
321 } else if (!c->modules || pa_idxset_ncontents(c->modules) == 0) {
322 pa_log(__FILE__": daemon startup without any loaded modules, refusing to work.\n");
324 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
329 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
331 c->disallow_module_loading = conf->disallow_module_loading;
332 c->exit_idle_time = conf->exit_idle_time;
333 c->module_idle_time = conf->module_idle_time;
334 c->scache_idle_time = conf->scache_idle_time;
335 c->resample_method = conf->resample_method;
337 if (pa_namereg_get(c, c->default_sink_name, PA_NAMEREG_SINK, 1) == NULL) {
338 pa_log_error("%s : Fatal error. Default sink name (%s) does not exist in name register.\n", __FILE__, c->default_sink_name);
341 pa_log_info(__FILE__": Daemon startup complete.\n");
342 if (pa_mainloop_run(mainloop, &retval) < 0)
344 pa_log_info(__FILE__": Daemon shutdown initiated.\n");
352 pa_mainloop_free(mainloop);
354 pa_log_info(__FILE__": Daemon terminated.\n");
359 pa_daemon_conf_free(conf);
362 pa_pid_file_remove();
364 close_pipe(daemon_pipe);