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