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