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 static struct pa_mainloop
*mainloop
;
51 static void drop_root(void) {
52 if (getuid() != 0 && geteuid() == 0) {
53 pa_log(__FILE__
": Started SUID root, dropping root rights.\n");
59 static const char* signal_name(int s
) {
61 case SIGINT
: return "SIGINT";
62 case SIGTERM
: return "SIGTERM";
63 case SIGUSR1
: return "SIGUSR1";
64 case SIGUSR2
: return "SIGUSR2";
65 case SIGXCPU
: return "SIGXCPU";
66 case SIGPIPE
: return "SIGPIPE";
67 default: return "UNKNOWN SIGNAL";
71 static void signal_callback(struct pa_mainloop_api
*m
, struct pa_signal_event
*e
, int sig
, void *userdata
) {
72 pa_log(__FILE__
": Got signal %s.\n", signal_name(sig
));
76 pa_module_load(userdata
, "module-cli", NULL
);
80 pa_module_load(userdata
, "module-cli-protocol-unix", NULL
);
86 pa_log(__FILE__
": Exiting.\n");
92 static void close_pipe(int p
[2]) {
100 int main(int argc
, char *argv
[]) {
102 struct pa_cmdline
*cmdline
= NULL
;
103 struct pa_strbuf
*buf
= NULL
;
106 int daemon_pipe
[2] = { -1, -1 };
108 pa_log_set_ident("polypaudio");
110 if (!(cmdline
= pa_cmdline_parse(argc
, argv
))) {
111 pa_log(__FILE__
": failed to parse command line.\n");
115 pa_log_set_target(cmdline
->auto_log_target
? PA_LOG_STDERR
: cmdline
->log_target
, NULL
);
118 pa_cmdline_help(argv
[0]);
123 if (cmdline
->version
) {
124 printf(PACKAGE_NAME
" "PACKAGE_VERSION
"\n");
129 if (cmdline
->high_priority
)
132 if (!cmdline
->stay_root
)
135 if (cmdline
->daemonize
) {
138 if (pa_stdio_acquire() < 0) {
139 pa_log(__FILE__
": failed to acquire stdio.\n");
143 if (pipe(daemon_pipe
) < 0) {
144 pa_log(__FILE__
": failed to create pipe.\n");
148 if ((child
= fork()) < 0) {
149 pa_log(__FILE__
": fork() failed: %s\n", strerror(errno
));
156 close(daemon_pipe
[1]);
159 if (pa_loop_read(daemon_pipe
[0], &retval
, sizeof(retval
)) != sizeof(retval
)) {
160 pa_log(__FILE__
": read() failed: %s\n", strerror(errno
));
167 close(daemon_pipe
[0]);
171 if (cmdline
->auto_log_target
)
172 pa_log_set_target(PA_LOG_SYSLOG
, NULL
);
184 if (cmdline
->dl_searchdir
)
185 lt_dladdsearchdir(cmdline
->dl_searchdir
);
188 lt_dladdsearchdir(DLSEARCHDIR
);
191 mainloop
= pa_mainloop_new();
194 r
= pa_signal_init(pa_mainloop_get_api(mainloop
));
196 pa_signal_new(SIGINT
, signal_callback
, c
);
197 pa_signal_new(SIGTERM
, signal_callback
, c
);
198 signal(SIGPIPE
, SIG_IGN
);
200 c
= pa_core_new(pa_mainloop_get_api(mainloop
));
203 pa_signal_new(SIGUSR1
, signal_callback
, c
);
204 pa_signal_new(SIGUSR2
, signal_callback
, c
);
206 r
= pa_cpu_limit_init(pa_mainloop_get_api(mainloop
));
209 buf
= pa_strbuf_new();
211 r
= pa_cli_command_execute(c
, cmdline
->cli_commands
, buf
, &cmdline
->fail
, &cmdline
->verbose
);
212 pa_log(s
= pa_strbuf_tostring_free(buf
));
215 if (r
< 0 && cmdline
->fail
) {
216 pa_log(__FILE__
": failed to initialize daemon.\n");
217 if (cmdline
->daemonize
)
218 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
219 } else if (!c
->modules
|| pa_idxset_ncontents(c
->modules
) == 0) {
220 pa_log(__FILE__
": daemon startup without any loaded modules, refusing to work.\n");
221 if (cmdline
->daemonize
)
222 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
225 if (cmdline
->daemonize
)
226 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
228 c
->disallow_module_loading
= cmdline
->disallow_module_loading
;
229 c
->quit_after_last_client_time
= cmdline
->quit_after_last_client_time
;
231 pa_log(__FILE__
": Daemon startup complete.\n");
232 if (pa_mainloop_run(mainloop
, &retval
) < 0)
234 pa_log(__FILE__
": Daemon shutdown initiated.\n");
241 pa_mainloop_free(mainloop
);
245 pa_log(__FILE__
": Daemon terminated.\n");
250 pa_cmdline_free(cmdline
);
252 close_pipe(daemon_pipe
);