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