]> code.delx.au - pulseaudio/blob - polyp/main.c
43a48f6b3627b89ec931697a0811d0b284d1b2ff
[pulseaudio] / polyp / 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 <memblock.h>
36 #include <limits.h>
37 #include <fcntl.h>
38
39 #ifdef HAVE_LIBWRAP
40 #include <syslog.h>
41 #include <tcpd.h>
42 #endif
43
44 #include "core.h"
45 #include "mainloop.h"
46 #include "module.h"
47 #include "mainloop-signal.h"
48 #include "cmdline.h"
49 #include "cli-command.h"
50 #include "util.h"
51 #include "sioman.h"
52 #include "xmalloc.h"
53 #include "cpulimit.h"
54 #include "log.h"
55 #include "daemon-conf.h"
56 #include "dumpmodules.h"
57 #include "caps.h"
58 #include "cli-text.h"
59 #include "pid.h"
60
61 #ifdef HAVE_LIBWRAP
62 /* Only one instance of these variables */
63 int allow_severity = LOG_INFO;
64 int deny_severity = LOG_WARNING;
65 #endif
66
67 static void signal_callback(struct pa_mainloop_api*m, struct pa_signal_event *e, int sig, void *userdata) {
68 pa_log_info(__FILE__": Got signal %s.\n", pa_strsignal(sig));
69
70 switch (sig) {
71 case SIGUSR1:
72 pa_module_load(userdata, "module-cli", NULL);
73 return;
74
75 case SIGUSR2:
76 pa_module_load(userdata, "module-cli-protocol-unix", NULL);
77 return;
78
79 case SIGHUP: {
80 int i;
81
82 for (i = 0;; i++) {
83 char *c;
84 switch (i) {
85 case 0:
86 c = pa_sink_list_to_string(userdata);
87 break;
88 case 1:
89 c = pa_source_list_to_string(userdata);
90 break;
91 case 2:
92 c = pa_sink_input_list_to_string(userdata);
93 break;
94 case 3:
95 c = pa_source_output_list_to_string(userdata);
96 break;
97 case 4:
98 c = pa_client_list_to_string(userdata);
99 break;
100 case 5:
101 c = pa_module_list_to_string(userdata);
102 break;
103 case 6:
104 c = pa_scache_list_to_string(userdata);
105 break;
106 case 7:
107 c = pa_autoload_list_to_string(userdata);
108 break;
109 default:
110 return;
111 }
112 pa_log_notice(c);
113 pa_xfree(c);
114 }
115
116 return;
117 }
118
119 case SIGINT:
120 case SIGTERM:
121 default:
122 pa_log_info(__FILE__": Exiting.\n");
123 m->quit(m, 1);
124 return;
125 }
126 }
127
128 static void close_pipe(int p[2]) {
129 if (p[0] != -1)
130 close(p[0]);
131 if (p[1] != -1)
132 close(p[1]);
133 p[0] = p[1] = -1;
134 }
135
136 int main(int argc, char *argv[]) {
137 struct pa_core *c;
138 struct pa_strbuf *buf = NULL;
139 struct pa_daemon_conf *conf;
140 struct pa_mainloop *mainloop;
141
142 char *s;
143 int r, retval = 1, d = 0;
144 int daemon_pipe[2] = { -1, -1 };
145 gid_t gid = (gid_t) -1;
146 int suid_root;
147 int valid_pid_file = 0;
148
149 pa_limit_caps();
150
151 suid_root = getuid() != 0 && geteuid() == 0;
152
153 if (suid_root && (pa_uid_in_group("realtime", &gid) <= 0 || gid >= 1000)) {
154 pa_log_warn(__FILE__": WARNING: called SUID root, but not in group 'realtime'.\n");
155 pa_drop_root();
156 }
157
158 LTDL_SET_PRELOADED_SYMBOLS();
159
160 r = lt_dlinit();
161 assert(r == 0);
162
163 pa_log_set_ident("polypaudio");
164
165 conf = pa_daemon_conf_new();
166
167 if (pa_daemon_conf_load(conf, NULL) < 0)
168 goto finish;
169
170 if (pa_daemon_conf_env(conf) < 0)
171 goto finish;
172
173 if (pa_cmdline_parse(conf, argc, argv, &d) < 0) {
174 pa_log(__FILE__": failed to parse command line.\n");
175 goto finish;
176 }
177
178 pa_log_set_maximal_level(conf->log_level);
179 pa_log_set_target(conf->auto_log_target ? PA_LOG_STDERR : conf->log_target, NULL);
180
181 if (conf->high_priority && conf->cmd == PA_CMD_DAEMON)
182 pa_raise_priority();
183
184 pa_drop_caps();
185
186 if (suid_root)
187 pa_drop_root();
188
189 if (conf->dl_search_path)
190 lt_dlsetsearchpath(conf->dl_search_path);
191
192 switch (conf->cmd) {
193 case PA_CMD_DUMP_MODULES:
194 pa_dump_modules(conf, argc-d, argv+d);
195 retval = 0;
196 goto finish;
197
198 case PA_CMD_DUMP_CONF: {
199 char *s = pa_daemon_conf_dump(conf);
200 fputs(s, stdout);
201 pa_xfree(s);
202 retval = 0;
203 goto finish;
204 }
205
206 case PA_CMD_HELP :
207 pa_cmdline_help(argv[0]);
208 retval = 0;
209 goto finish;
210
211 case PA_CMD_VERSION :
212 printf(PACKAGE_NAME" "PACKAGE_VERSION"\n");
213 retval = 0;
214 goto finish;
215
216 case PA_CMD_CHECK: {
217 pid_t pid;
218
219 if (pa_pid_file_check_running(&pid) < 0) {
220 pa_log_info(__FILE__": daemon not running\n");
221 } else {
222 pa_log_info(__FILE__": daemon running as PID %u\n", pid);
223 retval = 0;
224 }
225
226 goto finish;
227
228 }
229 case PA_CMD_KILL:
230
231 if (pa_pid_file_kill(SIGINT, NULL) < 0)
232 pa_log(__FILE__": failed to kill daemon.\n");
233 else
234 retval = 0;
235
236 goto finish;
237
238 default:
239 assert(conf->cmd == PA_CMD_DAEMON);
240 }
241
242 if (conf->daemonize) {
243 pid_t child;
244
245 if (pa_stdio_acquire() < 0) {
246 pa_log(__FILE__": failed to acquire stdio.\n");
247 goto finish;
248 }
249
250 if (pipe(daemon_pipe) < 0) {
251 pa_log(__FILE__": failed to create pipe.\n");
252 goto finish;
253 }
254
255 if ((child = fork()) < 0) {
256 pa_log(__FILE__": fork() failed: %s\n", strerror(errno));
257 goto finish;
258 }
259
260 if (child != 0) {
261 /* Father */
262
263 close(daemon_pipe[1]);
264 daemon_pipe[1] = -1;
265
266 if (pa_loop_read(daemon_pipe[0], &retval, sizeof(retval)) != sizeof(retval)) {
267 pa_log(__FILE__": read() failed: %s\n", strerror(errno));
268 retval = 1;
269 }
270
271 if (retval)
272 pa_log(__FILE__": daemon startup failed .\n");
273 else
274 pa_log_info(__FILE__": daemon startup successful.\n");
275
276 goto finish;
277 }
278
279 close(daemon_pipe[0]);
280 daemon_pipe[0] = -1;
281
282 if (conf->auto_log_target)
283 pa_log_set_target(PA_LOG_SYSLOG, NULL);
284
285 setsid();
286 setpgid(0,0);
287
288 close(0);
289 close(1);
290 close(2);
291
292 open("/dev/null", O_RDONLY);
293 open("/dev/null", O_WRONLY);
294 open("/dev/null", O_WRONLY);
295 }
296
297 chdir("/");
298
299 if (conf->use_pid_file) {
300 if (pa_pid_file_create() < 0)
301 goto finish;
302
303 valid_pid_file = 1;
304 }
305
306 mainloop = pa_mainloop_new();
307 assert(mainloop);
308
309 r = pa_signal_init(pa_mainloop_get_api(mainloop));
310 assert(r == 0);
311 pa_signal_new(SIGINT, signal_callback, c);
312 pa_signal_new(SIGTERM, signal_callback, c);
313 signal(SIGPIPE, SIG_IGN);
314
315 c = pa_core_new(pa_mainloop_get_api(mainloop));
316 assert(c);
317
318 pa_signal_new(SIGUSR1, signal_callback, c);
319 pa_signal_new(SIGUSR2, signal_callback, c);
320 pa_signal_new(SIGHUP, signal_callback, c);
321
322 r = pa_cpu_limit_init(pa_mainloop_get_api(mainloop));
323 assert(r == 0);
324
325 buf = pa_strbuf_new();
326 assert(buf);
327 if (conf->default_script_file)
328 r = pa_cli_command_execute_file(c, conf->default_script_file, buf, &conf->fail);
329
330 if (r >= 0)
331 r = pa_cli_command_execute(c, conf->script_commands, buf, &conf->fail);
332 pa_log(s = pa_strbuf_tostring_free(buf));
333 pa_xfree(s);
334
335 if (r < 0 && conf->fail) {
336 pa_log(__FILE__": failed to initialize daemon.\n");
337 if (conf->daemonize)
338 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
339 } else if (!c->modules || pa_idxset_ncontents(c->modules) == 0) {
340 pa_log(__FILE__": daemon startup without any loaded modules, refusing to work.\n");
341 if (conf->daemonize)
342 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
343 } else {
344
345 retval = 0;
346 if (conf->daemonize)
347 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
348
349 c->disallow_module_loading = conf->disallow_module_loading;
350 c->exit_idle_time = conf->exit_idle_time;
351 c->module_idle_time = conf->module_idle_time;
352 c->scache_idle_time = conf->scache_idle_time;
353 c->resample_method = conf->resample_method;
354
355 pa_log_info(__FILE__": Daemon startup complete.\n");
356 if (pa_mainloop_run(mainloop, &retval) < 0)
357 retval = 1;
358 pa_log_info(__FILE__": Daemon shutdown initiated.\n");
359 }
360
361 pa_core_free(c);
362
363 pa_cpu_limit_done();
364 pa_signal_done();
365 pa_mainloop_free(mainloop);
366
367 pa_log_info(__FILE__": Daemon terminated.\n");
368
369 finish:
370
371 if (conf)
372 pa_daemon_conf_free(conf);
373
374 if (valid_pid_file)
375 pa_pid_file_remove();
376
377 close_pipe(daemon_pipe);
378
379 lt_dlexit();
380
381 return retval;
382 }