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