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