]>
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"
47 static struct pa_mainloop
*mainloop
;
49 static void drop_root(void) {
50 if (getuid() != 0 && geteuid() == 0) {
51 fprintf(stderr
, __FILE__
": started SUID root, dropping root rights.\n");
57 static void exit_signal_callback(struct pa_mainloop_api
*m
, struct pa_signal_event
*e
, int sig
, void *userdata
) {
59 fprintf(stderr
, __FILE__
": got signal.\n");
62 static void aux_signal_callback(struct pa_mainloop_api
*m
, struct pa_signal_event
*e
, int sig
, void *userdata
) {
63 struct pa_core
*c
= userdata
;
65 pa_module_load(c
, sig
== SIGUSR1
? "module-cli" : "module-cli-protocol-unix", NULL
);
68 static void close_pipe(int p
[2]) {
76 int main(int argc
, char *argv
[]) {
78 struct pa_cmdline
*cmdline
= NULL
;
79 struct pa_strbuf
*buf
= NULL
;
82 int daemon_pipe
[2] = { -1, -1 };
84 if (!(cmdline
= pa_cmdline_parse(argc
, argv
))) {
85 fprintf(stderr
, __FILE__
": failed to parse command line.\n");
90 pa_cmdline_help(argv
[0]);
95 if (cmdline
->version
) {
96 printf(PACKAGE_NAME
" "PACKAGE_VERSION
"\n");
101 if (cmdline
->high_priority
)
104 if (!cmdline
->stay_root
)
107 if (cmdline
->daemonize
) {
110 if (pa_stdio_acquire() < 0) {
111 fprintf(stderr
, __FILE__
": failed to acquire stdio.\n");
115 if (pipe(daemon_pipe
) < 0) {
116 fprintf(stderr
, __FILE__
": failed to create pipe.\n");
120 if ((child
= fork()) < 0) {
121 fprintf(stderr
, __FILE__
": fork() failed: %s\n", strerror(errno
));
128 close(daemon_pipe
[1]);
131 if (pa_loop_read(daemon_pipe
[0], &retval
, sizeof(retval
)) != sizeof(retval
)) {
132 fprintf(stderr
, __FILE__
": read() failed: %s\n", strerror(errno
));
139 close(daemon_pipe
[0]);
148 #ifdef PA_DLSEARCHDIR
149 lt_dladdsearchdir(PA_DLSEARCHDIR
);
152 mainloop
= pa_mainloop_new();
155 r
= pa_signal_init(pa_mainloop_get_api(mainloop
));
157 pa_signal_new(SIGINT
, exit_signal_callback
, NULL
);
158 signal(SIGPIPE
, SIG_IGN
);
160 c
= pa_core_new(pa_mainloop_get_api(mainloop
));
163 pa_signal_new(SIGUSR1
, aux_signal_callback
, c
);
164 pa_signal_new(SIGUSR2
, aux_signal_callback
, c
);
166 buf
= pa_strbuf_new();
168 r
= pa_cli_command_execute(c
, cmdline
->cli_commands
, buf
, &cmdline
->fail
, &cmdline
->verbose
);
169 fprintf(stderr
, s
= pa_strbuf_tostring_free(buf
));
172 if (r
< 0 && cmdline
->fail
) {
173 fprintf(stderr
, __FILE__
": failed to initialize daemon.\n");
174 if (cmdline
->daemonize
)
175 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
176 } else if (!c
->modules
|| pa_idxset_ncontents(c
->modules
) == 0) {
177 fprintf(stderr
, __FILE__
": daemon startup without any loaded modules, refusing to work.\n");
178 if (cmdline
->daemonize
)
179 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
182 if (cmdline
->daemonize
)
183 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
184 fprintf(stderr
, __FILE__
": mainloop entry.\n");
185 if (pa_mainloop_run(mainloop
, &retval
) < 0)
187 fprintf(stderr
, __FILE__
": mainloop exit.\n");
193 pa_mainloop_free(mainloop
);
200 pa_cmdline_free(cmdline
);
202 close_pipe(daemon_pipe
);