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