]> code.delx.au - pulseaudio/blob - src/daemon/main.c
Make the probe for RNG sources at runtime since the configure script isn't
[pulseaudio] / src / daemon / main.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <signal.h>
32 #include <stddef.h>
33 #include <assert.h>
34 #include <ltdl.h>
35 #include <limits.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <sys/types.h>
39 #include <liboil/liboil.h>
40
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
43 #endif
44
45 #ifdef HAVE_LIBWRAP
46 #include <syslog.h>
47 #include <tcpd.h>
48 #endif
49
50 #include "../polypcore/winsock.h"
51
52 #include <polyp/mainloop.h>
53 #include <polyp/mainloop-signal.h>
54
55 #include <polypcore/core.h>
56 #include <polypcore/memblock.h>
57 #include <polypcore/module.h>
58 #include <polypcore/cli-command.h>
59 #include <polypcore/log.h>
60 #include <polypcore/util.h>
61 #include <polypcore/sioman.h>
62 #include <polypcore/xmalloc.h>
63 #include <polypcore/cli-text.h>
64 #include <polypcore/pid.h>
65 #include <polypcore/namereg.h>
66 #include <polypcore/random.h>
67
68 #include "cmdline.h"
69 #include "cpulimit.h"
70 #include "daemon-conf.h"
71 #include "dumpmodules.h"
72 #include "caps.h"
73
74 #ifdef HAVE_LIBWRAP
75 /* Only one instance of these variables */
76 int allow_severity = LOG_INFO;
77 int deny_severity = LOG_WARNING;
78 #endif
79
80 #ifdef OS_IS_WIN32
81
82 static void message_cb(pa_mainloop_api*a, pa_defer_event *e, void *userdata) {
83 MSG msg;
84
85 while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
86 if (msg.message == WM_QUIT)
87 raise(SIGTERM);
88 else {
89 TranslateMessage(&msg);
90 DispatchMessage(&msg);
91 }
92 }
93 }
94
95 #endif
96
97 static void signal_callback(pa_mainloop_api*m, PA_GCC_UNUSED pa_signal_event *e, int sig, void *userdata) {
98 pa_log_info(__FILE__": Got signal %s.", pa_strsignal(sig));
99
100 switch (sig) {
101 #ifdef SIGUSR1
102 case SIGUSR1:
103 pa_module_load(userdata, "module-cli", NULL);
104 break;
105 #endif
106
107 #ifdef SIGUSR2
108 case SIGUSR2:
109 pa_module_load(userdata, "module-cli-protocol-unix", NULL);
110 break;
111 #endif
112
113 #ifdef SIGHUP
114 case SIGHUP: {
115 char *c = pa_full_status_string(userdata);
116 pa_log_notice("%s", c);
117 pa_xfree(c);
118 return;
119 }
120 #endif
121
122 case SIGINT:
123 case SIGTERM:
124 default:
125 pa_log_info(__FILE__": Exiting.");
126 m->quit(m, 1);
127 break;
128 }
129 }
130
131 static void close_pipe(int p[2]) {
132 if (p[0] != -1)
133 close(p[0]);
134 if (p[1] != -1)
135 close(p[1]);
136 p[0] = p[1] = -1;
137 }
138
139 int main(int argc, char *argv[]) {
140 pa_core *c;
141 pa_strbuf *buf = NULL;
142 pa_daemon_conf *conf;
143 pa_mainloop *mainloop;
144
145 char *s;
146 int r, retval = 1, d = 0;
147 int daemon_pipe[2] = { -1, -1 };
148 int suid_root;
149 int valid_pid_file = 0;
150
151 #ifdef HAVE_GETUID
152 gid_t gid = (gid_t) -1;
153 #endif
154
155 #ifdef OS_IS_WIN32
156 pa_defer_event *defer;
157 #endif
158
159 pa_limit_caps();
160
161 #ifdef HAVE_GETUID
162 suid_root = getuid() != 0 && geteuid() == 0;
163
164 if (suid_root && (pa_own_uid_in_group("realtime", &gid) <= 0 || gid >= 1000)) {
165 pa_log_warn(__FILE__": WARNING: called SUID root, but not in group 'realtime'.");
166 pa_drop_root();
167 }
168 #else
169 suid_root = 0;
170 #endif
171
172 LTDL_SET_PRELOADED_SYMBOLS();
173
174 r = lt_dlinit();
175 assert(r == 0);
176
177 #ifdef OS_IS_WIN32
178 {
179 WSADATA data;
180 WSAStartup(MAKEWORD(2, 0), &data);
181 }
182 #endif
183
184 pa_random_seed();
185
186 pa_log_set_ident("polypaudio");
187
188 conf = pa_daemon_conf_new();
189
190 if (pa_daemon_conf_load(conf, NULL) < 0)
191 goto finish;
192
193 if (pa_daemon_conf_env(conf) < 0)
194 goto finish;
195
196 if (pa_cmdline_parse(conf, argc, argv, &d) < 0) {
197 pa_log(__FILE__": failed to parse command line.");
198 goto finish;
199 }
200
201 pa_log_set_maximal_level(conf->log_level);
202 pa_log_set_target(conf->auto_log_target ? PA_LOG_STDERR : conf->log_target, NULL);
203
204 if (conf->high_priority && conf->cmd == PA_CMD_DAEMON)
205 pa_raise_priority();
206
207 pa_drop_caps();
208
209 if (suid_root)
210 pa_drop_root();
211
212 if (conf->dl_search_path)
213 lt_dlsetsearchpath(conf->dl_search_path);
214
215 switch (conf->cmd) {
216 case PA_CMD_DUMP_MODULES:
217 pa_dump_modules(conf, argc-d, argv+d);
218 retval = 0;
219 goto finish;
220
221 case PA_CMD_DUMP_CONF: {
222 s = pa_daemon_conf_dump(conf);
223 fputs(s, stdout);
224 pa_xfree(s);
225 retval = 0;
226 goto finish;
227 }
228
229 case PA_CMD_HELP :
230 pa_cmdline_help(argv[0]);
231 retval = 0;
232 goto finish;
233
234 case PA_CMD_VERSION :
235 printf(PACKAGE_NAME" "PACKAGE_VERSION"\n");
236 retval = 0;
237 goto finish;
238
239 case PA_CMD_CHECK: {
240 pid_t pid;
241
242 if (pa_pid_file_check_running(&pid) < 0) {
243 pa_log_info(__FILE__": daemon not running");
244 } else {
245 pa_log_info(__FILE__": daemon running as PID %u", pid);
246 retval = 0;
247 }
248
249 goto finish;
250
251 }
252 case PA_CMD_KILL:
253
254 if (pa_pid_file_kill(SIGINT, NULL) < 0)
255 pa_log(__FILE__": failed to kill daemon.");
256 else
257 retval = 0;
258
259 goto finish;
260
261 default:
262 assert(conf->cmd == PA_CMD_DAEMON);
263 }
264
265 if (conf->daemonize) {
266 pid_t child;
267 int tty_fd;
268
269 if (pa_stdio_acquire() < 0) {
270 pa_log(__FILE__": failed to acquire stdio.");
271 goto finish;
272 }
273
274 #ifdef HAVE_FORK
275 if (pipe(daemon_pipe) < 0) {
276 pa_log(__FILE__": failed to create pipe.");
277 goto finish;
278 }
279
280 if ((child = fork()) < 0) {
281 pa_log(__FILE__": fork() failed: %s", strerror(errno));
282 goto finish;
283 }
284
285 if (child != 0) {
286 /* Father */
287
288 close(daemon_pipe[1]);
289 daemon_pipe[1] = -1;
290
291 if (pa_loop_read(daemon_pipe[0], &retval, sizeof(retval)) != sizeof(retval)) {
292 pa_log(__FILE__": read() failed: %s", strerror(errno));
293 retval = 1;
294 }
295
296 if (retval)
297 pa_log(__FILE__": daemon startup failed.");
298 else
299 pa_log_info(__FILE__": daemon startup successful.");
300
301 goto finish;
302 }
303
304 close(daemon_pipe[0]);
305 daemon_pipe[0] = -1;
306 #endif
307
308 if (conf->auto_log_target)
309 pa_log_set_target(PA_LOG_SYSLOG, NULL);
310
311 #ifdef HAVE_SETSID
312 setsid();
313 #endif
314 #ifdef HAVE_SETPGID
315 setpgid(0,0);
316 #endif
317
318 #ifndef OS_IS_WIN32
319 close(0);
320 close(1);
321 close(2);
322
323 open("/dev/null", O_RDONLY);
324 open("/dev/null", O_WRONLY);
325 open("/dev/null", O_WRONLY);
326 #else
327 FreeConsole();
328 #endif
329
330 #ifdef SIGTTOU
331 signal(SIGTTOU, SIG_IGN);
332 #endif
333 #ifdef SIGTTIN
334 signal(SIGTTIN, SIG_IGN);
335 #endif
336 #ifdef SIGTSTP
337 signal(SIGTSTP, SIG_IGN);
338 #endif
339
340 #ifdef TIOCNOTTY
341 if ((tty_fd = open("/dev/tty", O_RDWR)) >= 0) {
342 ioctl(tty_fd, TIOCNOTTY, (char*) 0);
343 close(tty_fd);
344 }
345 #endif
346 }
347
348 chdir("/");
349
350 if (conf->use_pid_file) {
351 if (pa_pid_file_create() < 0) {
352 pa_log(__FILE__": pa_pid_file_create() failed.");
353 #ifdef HAVE_FORK
354 if (conf->daemonize)
355 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
356 #endif
357 goto finish;
358 }
359
360 valid_pid_file = 1;
361 }
362
363 #ifdef SIGPIPE
364 signal(SIGPIPE, SIG_IGN);
365 #endif
366
367 mainloop = pa_mainloop_new();
368 assert(mainloop);
369
370 c = pa_core_new(pa_mainloop_get_api(mainloop));
371 assert(c);
372
373 r = pa_signal_init(pa_mainloop_get_api(mainloop));
374 assert(r == 0);
375 pa_signal_new(SIGINT, signal_callback, c);
376 pa_signal_new(SIGTERM, signal_callback, c);
377
378 #ifdef SIGUSR1
379 pa_signal_new(SIGUSR1, signal_callback, c);
380 #endif
381 #ifdef SIGUSR2
382 pa_signal_new(SIGUSR2, signal_callback, c);
383 #endif
384 #ifdef SIGHUP
385 pa_signal_new(SIGHUP, signal_callback, c);
386 #endif
387
388 #ifdef OS_IS_WIN32
389 defer = pa_mainloop_get_api(mainloop)->defer_new(pa_mainloop_get_api(mainloop), message_cb, NULL);
390 assert(defer);
391 #endif
392
393 if (conf->daemonize)
394 c->running_as_daemon = 1;
395
396 oil_init();
397
398 r = pa_cpu_limit_init(pa_mainloop_get_api(mainloop));
399 assert(r == 0);
400
401 buf = pa_strbuf_new();
402 if (conf->default_script_file)
403 r = pa_cli_command_execute_file(c, conf->default_script_file, buf, &conf->fail);
404
405 if (r >= 0)
406 r = pa_cli_command_execute(c, conf->script_commands, buf, &conf->fail);
407 pa_log_error("%s", s = pa_strbuf_tostring_free(buf));
408 pa_xfree(s);
409
410 if (r < 0 && conf->fail) {
411 pa_log(__FILE__": failed to initialize daemon.");
412 #ifdef HAVE_FORK
413 if (conf->daemonize)
414 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
415 #endif
416 } else if (!c->modules || pa_idxset_size(c->modules) == 0) {
417 pa_log(__FILE__": daemon startup without any loaded modules, refusing to work.");
418 #ifdef HAVE_FORK
419 if (conf->daemonize)
420 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
421 #endif
422 } else {
423
424 retval = 0;
425 #ifdef HAVE_FORK
426 if (conf->daemonize)
427 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
428 #endif
429
430 c->disallow_module_loading = conf->disallow_module_loading;
431 c->exit_idle_time = conf->exit_idle_time;
432 c->module_idle_time = conf->module_idle_time;
433 c->scache_idle_time = conf->scache_idle_time;
434 c->resample_method = conf->resample_method;
435
436 if (c->default_sink_name &&
437 pa_namereg_get(c, c->default_sink_name, PA_NAMEREG_SINK, 1) == NULL) {
438 pa_log_error("%s : Fatal error. Default sink name (%s) does not exist in name register.", __FILE__, c->default_sink_name);
439 retval = 1;
440 } else {
441 pa_log_info(__FILE__": Daemon startup complete.");
442 if (pa_mainloop_run(mainloop, &retval) < 0)
443 retval = 1;
444 pa_log_info(__FILE__": Daemon shutdown initiated.");
445 }
446 }
447
448 #ifdef OS_IS_WIN32
449 pa_mainloop_get_api(mainloop)->defer_free(defer);
450 #endif
451
452 pa_core_free(c);
453
454 pa_cpu_limit_done();
455 pa_signal_done();
456 pa_mainloop_free(mainloop);
457
458 pa_log_info(__FILE__": Daemon terminated.");
459
460 finish:
461
462 if (conf)
463 pa_daemon_conf_free(conf);
464
465 if (valid_pid_file)
466 pa_pid_file_remove();
467
468 close_pipe(daemon_pipe);
469
470 #ifdef OS_IS_WIN32
471 WSACleanup();
472 #endif
473
474 lt_dlexit();
475
476 return retval;
477 }