]>
code.delx.au - pulseaudio/blob - polyp/main.c
0837e58df30cfd45330a4ce9f5b545b68a5d8bf7
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"
48 #include "daemon-conf.h"
49 #include "dumpmodules.h"
53 static void signal_callback(struct pa_mainloop_api
*m
, struct pa_signal_event
*e
, int sig
, void *userdata
) {
54 pa_log(__FILE__
": Got signal %s.\n", pa_strsignal(sig
));
58 pa_module_load(userdata
, "module-cli", NULL
);
62 pa_module_load(userdata
, "module-cli-protocol-unix", NULL
);
72 c
= pa_sink_list_to_string(userdata
);
75 c
= pa_source_list_to_string(userdata
);
78 c
= pa_sink_input_list_to_string(userdata
);
81 c
= pa_source_output_list_to_string(userdata
);
84 c
= pa_client_list_to_string(userdata
);
87 c
= pa_module_list_to_string(userdata
);
90 c
= pa_scache_list_to_string(userdata
);
93 c
= pa_autoload_list_to_string(userdata
);
108 pa_log(__FILE__
": Exiting.\n");
115 static void close_pipe(int p
[2]) {
123 int main(int argc
, char *argv
[]) {
125 struct pa_strbuf
*buf
= NULL
;
126 struct pa_daemon_conf
*conf
;
127 struct pa_mainloop
*mainloop
;
130 int r
, retval
= 1, d
= 0;
131 int daemon_pipe
[2] = { -1, -1 };
132 gid_t gid
= (gid_t
) -1;
137 suid_root
= getuid() != 0 && geteuid() == 0;
139 if (suid_root
&& (pa_uid_in_group("realtime", &gid
) <= 0 || gid
>= 1000)) {
140 pa_log(__FILE__
": WARNING: called SUID root, but not in group 'realtime'.\n");
147 pa_log_set_ident("polypaudio");
149 conf
= pa_daemon_conf_new();
151 if (pa_daemon_conf_load(conf
, NULL
) < 0)
154 if (pa_daemon_conf_env(conf
) < 0)
157 if (pa_cmdline_parse(conf
, argc
, argv
, &d
) < 0) {
158 pa_log(__FILE__
": failed to parse command line.\n");
162 pa_log_set_target(conf
->auto_log_target
? PA_LOG_STDERR
: conf
->log_target
, NULL
);
164 if (conf
->high_priority
&& conf
->cmd
== PA_CMD_DAEMON
)
172 if (conf
->dl_search_path
)
173 lt_dlsetsearchpath(conf
->dl_search_path
);
176 case PA_CMD_DUMP_MODULES
:
177 pa_dump_modules(conf
, argc
-d
, argv
+d
);
181 case PA_CMD_DUMP_CONF
: {
182 char *s
= pa_daemon_conf_dump(conf
);
190 pa_cmdline_help(argv
[0]);
194 case PA_CMD_VERSION
:
195 printf(PACKAGE_NAME
" "PACKAGE_VERSION
"\n");
200 assert(conf
->cmd
== PA_CMD_DAEMON
);
203 if (conf
->daemonize
) {
206 if (pa_stdio_acquire() < 0) {
207 pa_log(__FILE__
": failed to acquire stdio.\n");
211 if (pipe(daemon_pipe
) < 0) {
212 pa_log(__FILE__
": failed to create pipe.\n");
216 if ((child
= fork()) < 0) {
217 pa_log(__FILE__
": fork() failed: %s\n", strerror(errno
));
224 close(daemon_pipe
[1]);
227 if (pa_loop_read(daemon_pipe
[0], &retval
, sizeof(retval
)) != sizeof(retval
)) {
228 pa_log(__FILE__
": read() failed: %s\n", strerror(errno
));
233 pa_log(__FILE__
": daemon startup %s.\n", retval
? "failed" : "succeeded");
238 close(daemon_pipe
[0]);
241 if (conf
->auto_log_target
)
242 pa_log_set_target(PA_LOG_SYSLOG
, NULL
);
252 pa_log(__FILE__
": sizeof(pa_usec_t) = %u\n", sizeof(pa_usec_t
));
254 mainloop
= pa_mainloop_new();
257 r
= pa_signal_init(pa_mainloop_get_api(mainloop
));
259 pa_signal_new(SIGINT
, signal_callback
, c
);
260 pa_signal_new(SIGTERM
, signal_callback
, c
);
261 signal(SIGPIPE
, SIG_IGN
);
263 c
= pa_core_new(pa_mainloop_get_api(mainloop
));
266 pa_signal_new(SIGUSR1
, signal_callback
, c
);
267 pa_signal_new(SIGUSR2
, signal_callback
, c
);
268 pa_signal_new(SIGHUP
, signal_callback
, c
);
270 r
= pa_cpu_limit_init(pa_mainloop_get_api(mainloop
));
273 buf
= pa_strbuf_new();
275 if (conf
->default_script_file
)
276 r
= pa_cli_command_execute_file(c
, conf
->default_script_file
, buf
, &conf
->fail
, &conf
->verbose
);
279 r
= pa_cli_command_execute(c
, conf
->script_commands
, buf
, &conf
->fail
, &conf
->verbose
);
280 pa_log(s
= pa_strbuf_tostring_free(buf
));
283 if (r
< 0 && conf
->fail
) {
284 pa_log(__FILE__
": failed to initialize daemon.\n");
286 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
287 } else if (!c
->modules
|| pa_idxset_ncontents(c
->modules
) == 0) {
288 pa_log(__FILE__
": daemon startup without any loaded modules, refusing to work.\n");
290 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
294 pa_loop_write(daemon_pipe
[1], &retval
, sizeof(retval
));
296 c
->disallow_module_loading
= conf
->disallow_module_loading
;
297 c
->exit_idle_time
= conf
->exit_idle_time
;
298 c
->module_idle_time
= conf
->module_idle_time
;
299 c
->scache_idle_time
= conf
->scache_idle_time
;
300 c
->resample_method
= conf
->resample_method
;
302 pa_log(__FILE__
": Daemon startup complete.\n");
303 if (pa_mainloop_run(mainloop
, &retval
) < 0)
305 pa_log(__FILE__
": Daemon shutdown initiated.\n");
312 pa_mainloop_free(mainloop
);
314 pa_log(__FILE__
": Daemon terminated.\n");
319 pa_daemon_conf_free(conf
);
321 close_pipe(daemon_pipe
);