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