]>
code.delx.au - pulseaudio/blob - polyp/main.c
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"
63 /* Only one instance of these variables */
64 int allow_severity
= LOG_INFO
;
65 int deny_severity
= LOG_WARNING
;
68 static void signal_callback(struct pa_mainloop_api
*m
, struct pa_signal_event
*e
, int sig
, void *userdata
) {
69 pa_log_info(__FILE__
": Got signal %s.\n", pa_strsignal(sig
));
73 pa_module_load(userdata
, "module-cli", NULL
);
77 pa_module_load(userdata
, "module-cli-protocol-unix", NULL
);
87 c
= pa_sink_list_to_string(userdata
);
90 c
= pa_source_list_to_string(userdata
);
93 c
= pa_sink_input_list_to_string(userdata
);
96 c
= pa_source_output_list_to_string(userdata
);
99 c
= pa_client_list_to_string(userdata
);
102 c
= pa_module_list_to_string(userdata
);
105 c
= pa_scache_list_to_string(userdata
);
108 c
= pa_autoload_list_to_string(userdata
);
123 pa_log_info(__FILE__
": Exiting.\n");
129 static void close_pipe(int p
[2]) {
137 int main(int argc
, char *argv
[]) {
139 struct pa_strbuf
*buf
= NULL
;
140 struct pa_daemon_conf
*conf
;
141 struct pa_mainloop
*mainloop
;
144 int r
, retval
= 1, d
= 0;
145 int daemon_pipe
[2] = { -1, -1 };
146 gid_t gid
= (gid_t
) -1;
148 int valid_pid_file
= 0;
152 suid_root
= getuid() != 0 && geteuid() == 0;
154 if (suid_root
&& (pa_uid_in_group("realtime", &gid
) <= 0 || gid
>= 1000)) {
155 pa_log_warn(__FILE__
": WARNING: called SUID root, but not in group 'realtime'.\n");
159 LTDL_SET_PRELOADED_SYMBOLS();
164 pa_log_set_ident("polypaudio");
166 conf
= pa_daemon_conf_new();
168 if (pa_daemon_conf_load(conf
, NULL
) < 0)
171 if (pa_daemon_conf_env(conf
) < 0)
174 if (pa_cmdline_parse(conf
, argc
, argv
, &d
) < 0) {
175 pa_log(__FILE__
": failed to parse command line.\n");
179 pa_log_set_maximal_level(conf
->log_level
);
180 pa_log_set_target(conf
->auto_log_target
? PA_LOG_STDERR
: conf
->log_target
, NULL
);
182 if (conf
->high_priority
&& conf
->cmd
== PA_CMD_DAEMON
)
190 if (conf
->dl_search_path
)
191 lt_dlsetsearchpath(conf
->dl_search_path
);
194 case PA_CMD_DUMP_MODULES
:
195 pa_dump_modules(conf
, argc
-d
, argv
+d
);
199 case PA_CMD_DUMP_CONF
: {
200 char *s
= pa_daemon_conf_dump(conf
);
208 pa_cmdline_help(argv
[0]);
212 case PA_CMD_VERSION
:
213 printf(PACKAGE_NAME
" "PACKAGE_VERSION
"\n");
220 if (pa_pid_file_check_running(&pid
) < 0) {
221 pa_log_info(__FILE__
": daemon not running\n");
223 pa_log_info(__FILE__
": daemon running as PID %u\n", pid
);
232 if (pa_pid_file_kill(SIGINT
, NULL
) < 0)
233 pa_log(__FILE__
": failed to kill daemon.\n");
240 assert(conf
->cmd
== PA_CMD_DAEMON
);
243 if (conf
->daemonize
) {
247 if (pa_stdio_acquire() < 0) {
248 pa_log(__FILE__
": failed to acquire stdio.\n");
252 if (pipe(daemon_pipe
) < 0) {
253 pa_log(__FILE__
": failed to create pipe.\n");
257 if ((child
= fork()) < 0) {
258 pa_log(__FILE__
": fork() failed: %s\n", strerror(errno
));
265 close(daemon_pipe
[1]);
268 if (pa_loop_read(daemon_pipe
[0], &retval
, sizeof(retval
)) != sizeof(retval
)) {
269 pa_log(__FILE__
": read() failed: %s\n", strerror(errno
));
274 pa_log(__FILE__
": daemon startup failed.\n");
276 pa_log_info(__FILE__
": daemon startup successful.\n");
281 close(daemon_pipe
[0]);
284 if (conf
->auto_log_target
)
285 pa_log_set_target(PA_LOG_SYSLOG
, NULL
);
294 open("/dev/null", O_RDONLY
);
295 open("/dev/null", O_WRONLY
);
296 open("/dev/null", O_WRONLY
);
298 signal(SIGTTOU
, SIG_IGN
);
299 signal(SIGTTIN
, SIG_IGN
);
300 signal(SIGTSTP
, SIG_IGN
);
302 if ((tty_fd
= open("/dev/tty", O_RDWR
)) >= 0) {
303 ioctl(tty_fd
, TIOCNOTTY
, (char*) 0);
310 if (conf
->use_pid_file
) {
311 if (pa_pid_file_create() < 0) {
313 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
320 mainloop
= pa_mainloop_new();
323 r
= pa_signal_init(pa_mainloop_get_api(mainloop
));
325 pa_signal_new(SIGINT
, signal_callback
, c
);
326 pa_signal_new(SIGTERM
, signal_callback
, c
);
327 signal(SIGPIPE
, SIG_IGN
);
329 c
= pa_core_new(pa_mainloop_get_api(mainloop
));
332 pa_signal_new(SIGUSR1
, signal_callback
, c
);
333 pa_signal_new(SIGUSR2
, signal_callback
, c
);
334 pa_signal_new(SIGHUP
, signal_callback
, c
);
336 r
= pa_cpu_limit_init(pa_mainloop_get_api(mainloop
));
339 buf
= pa_strbuf_new();
341 if (conf
->default_script_file
)
342 r
= pa_cli_command_execute_file(c
, conf
->default_script_file
, buf
, &conf
->fail
);
345 r
= pa_cli_command_execute(c
, conf
->script_commands
, buf
, &conf
->fail
);
346 pa_log(s
= pa_strbuf_tostring_free(buf
));
349 if (r
< 0 && conf
->fail
) {
350 pa_log(__FILE__
": failed to initialize daemon.\n");
352 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
353 } else if (!c
->modules
|| pa_idxset_ncontents(c
->modules
) == 0) {
354 pa_log(__FILE__
": daemon startup without any loaded modules, refusing to work.\n");
356 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
361 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
363 c
->disallow_module_loading
= conf
->disallow_module_loading
;
364 c
->exit_idle_time
= conf
->exit_idle_time
;
365 c
->module_idle_time
= conf
->module_idle_time
;
366 c
->scache_idle_time
= conf
->scache_idle_time
;
367 c
->resample_method
= conf
->resample_method
;
369 pa_log_info(__FILE__
": Daemon startup complete.\n");
370 if (pa_mainloop_run(mainloop
, &retval
) < 0)
372 pa_log_info(__FILE__
": Daemon shutdown initiated.\n");
379 pa_mainloop_free(mainloop
);
381 pa_log_info(__FILE__
": Daemon terminated.\n");
386 pa_daemon_conf_free(conf
);
389 pa_pid_file_remove();
391 close_pipe(daemon_pipe
);