]>
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 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 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
40 #include "mainloop-signal.h"
42 #include "cli-command.h"
49 #include "dumpmodules.h"
51 static struct pa_mainloop
*mainloop
;
53 static void drop_root(void) {
54 if (getuid() != 0 && geteuid() == 0) {
55 pa_log(__FILE__
": Started SUID root, dropping root rights.\n");
61 static const char* signal_name(int s
) {
63 case SIGINT
: return "SIGINT";
64 case SIGTERM
: return "SIGTERM";
65 case SIGUSR1
: return "SIGUSR1";
66 case SIGUSR2
: return "SIGUSR2";
67 case SIGXCPU
: return "SIGXCPU";
68 case SIGPIPE
: return "SIGPIPE";
69 default: return "UNKNOWN SIGNAL";
73 static void signal_callback(struct pa_mainloop_api
*m
, struct pa_signal_event
*e
, int sig
, void *userdata
) {
74 pa_log(__FILE__
": Got signal %s.\n", signal_name(sig
));
78 pa_module_load(userdata
, "module-cli", NULL
);
82 pa_module_load(userdata
, "module-cli-protocol-unix", NULL
);
88 pa_log(__FILE__
": Exiting.\n");
94 static void close_pipe(int p
[2]) {
102 int main(int argc
, char *argv
[]) {
104 struct pa_strbuf
*buf
= NULL
;
105 struct pa_conf
*conf
;
107 int r
, retval
= 1, d
= 0;
108 int daemon_pipe
[2] = { -1, -1 };
113 pa_log_set_ident("polypaudio");
115 conf
= pa_conf_new();
117 if (pa_conf_load(conf
, NULL
) < 0)
120 if (pa_cmdline_parse(conf
, argc
, argv
, &d
) < 0) {
121 pa_log(__FILE__
": failed to parse command line.\n");
125 pa_log_set_target(conf
->auto_log_target
? PA_LOG_STDERR
: conf
->log_target
, NULL
);
127 if (conf
->dl_search_path
)
128 lt_dlsetsearchpath(conf
->dl_search_path
);
131 lt_dlsetsearchpath(DLSEARCHPATH
);
134 if (conf
->dump_modules
) {
135 pa_dump_modules(conf
, argc
-d
, argv
+d
);
140 if (conf
->dump_conf
) {
141 char *s
= pa_conf_dump(conf
);
149 pa_cmdline_help(argv
[0]);
155 printf(PACKAGE_NAME
" "PACKAGE_VERSION
"\n");
160 if (conf
->high_priority
)
163 if (!conf
->stay_root
)
166 if (conf
->daemonize
) {
169 if (pa_stdio_acquire() < 0) {
170 pa_log(__FILE__
": failed to acquire stdio.\n");
174 if (pipe(daemon_pipe
) < 0) {
175 pa_log(__FILE__
": failed to create pipe.\n");
179 if ((child
= fork()) < 0) {
180 pa_log(__FILE__
": fork() failed: %s\n", strerror(errno
));
187 close(daemon_pipe
[1]);
190 if (pa_loop_read(daemon_pipe
[0], &retval
, sizeof(retval
)) != sizeof(retval
)) {
191 pa_log(__FILE__
": read() failed: %s\n", strerror(errno
));
198 close(daemon_pipe
[0]);
202 if (conf
->auto_log_target
)
203 pa_log_set_target(PA_LOG_SYSLOG
, NULL
);
213 pa_log(__FILE__
": sizeof(pa_usec_t) = %u\n", sizeof(pa_usec_t
));
215 mainloop
= pa_mainloop_new();
218 r
= pa_signal_init(pa_mainloop_get_api(mainloop
));
220 pa_signal_new(SIGINT
, signal_callback
, c
);
221 pa_signal_new(SIGTERM
, signal_callback
, c
);
222 signal(SIGPIPE
, SIG_IGN
);
224 c
= pa_core_new(pa_mainloop_get_api(mainloop
));
227 pa_signal_new(SIGUSR1
, signal_callback
, c
);
228 pa_signal_new(SIGUSR2
, signal_callback
, c
);
230 r
= pa_cpu_limit_init(pa_mainloop_get_api(mainloop
));
233 buf
= pa_strbuf_new();
235 if (conf
->default_script_file
)
236 pa_cli_command_execute_file(c
, conf
->default_script_file
, buf
, &conf
->fail
, &conf
->verbose
);
237 r
= pa_cli_command_execute(c
, conf
->script_commands
, buf
, &conf
->fail
, &conf
->verbose
);
238 pa_log(s
= pa_strbuf_tostring_free(buf
));
241 if (r
< 0 && conf
->fail
) {
242 pa_log(__FILE__
": failed to initialize daemon.\n");
244 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
245 } else if (!c
->modules
|| pa_idxset_ncontents(c
->modules
) == 0) {
246 pa_log(__FILE__
": daemon startup without any loaded modules, refusing to work.\n");
248 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
252 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
254 c
->disallow_module_loading
= conf
->disallow_module_loading
;
255 c
->exit_idle_time
= conf
->exit_idle_time
;
256 c
->module_idle_time
= conf
->module_idle_time
;
258 pa_log(__FILE__
": Daemon startup complete.\n");
259 if (pa_mainloop_run(mainloop
, &retval
) < 0)
261 pa_log(__FILE__
": Daemon shutdown initiated.\n");
268 pa_mainloop_free(mainloop
);
270 pa_log(__FILE__
": Daemon terminated.\n");
277 close_pipe(daemon_pipe
);