]> code.delx.au - pulseaudio/blob - polyp/main.c
new configuration subsystem
[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 "conf.h"
49 #include "dumpmodules.h"
50
51 static struct pa_mainloop *mainloop;
52
53 static void drop_root(void) {
54 if (getuid() != 0 && geteuid() == 0) {
55 pa_log(__FILE__": Started SUID root, dropping root rights.\n");
56 setuid(getuid());
57 seteuid(getuid());
58 }
59 }
60
61 static const char* signal_name(int s) {
62 switch(s) {
63 case SIGINT: return "SIGINT";
64 case SIGTERM: return "SIGTERM";
65 case SIGUSR1: return "SIGUSR1";
66 case SIGUSR2: return "SIGUSR2";
67 case SIGXCPU: return "SIGXCPU";
68 case SIGPIPE: return "SIGPIPE";
69 default: return "UNKNOWN SIGNAL";
70 }
71 }
72
73 static void signal_callback(struct pa_mainloop_api*m, struct pa_signal_event *e, int sig, void *userdata) {
74 pa_log(__FILE__": Got signal %s.\n", signal_name(sig));
75
76 switch (sig) {
77 case SIGUSR1:
78 pa_module_load(userdata, "module-cli", NULL);
79 return;
80
81 case SIGUSR2:
82 pa_module_load(userdata, "module-cli-protocol-unix", NULL);
83 return;
84
85 case SIGINT:
86 case SIGTERM:
87 default:
88 pa_log(__FILE__": Exiting.\n");
89 m->quit(m, 1);
90 return;
91 }
92 }
93
94 static void close_pipe(int p[2]) {
95 if (p[0] != -1)
96 close(p[0]);
97 if (p[1] != -1)
98 close(p[1]);
99 p[0] = p[1] = -1;
100 }
101
102 int main(int argc, char *argv[]) {
103 struct pa_core *c;
104 struct pa_strbuf *buf = NULL;
105 struct pa_conf *conf;
106 char *s;
107 int r, retval = 1, d = 0;
108 int daemon_pipe[2] = { -1, -1 };
109
110 r = lt_dlinit();
111 assert(r == 0);
112
113 pa_log_set_ident("polypaudio");
114
115 conf = pa_conf_new();
116
117 if (pa_conf_load(conf, NULL) < 0)
118 goto finish;
119
120 if (pa_cmdline_parse(conf, argc, argv, &d) < 0) {
121 pa_log(__FILE__": failed to parse command line.\n");
122 goto finish;
123 }
124
125 pa_log_set_target(conf->auto_log_target ? PA_LOG_STDERR : conf->log_target, NULL);
126
127 if (conf->dl_search_path)
128 lt_dlsetsearchpath(conf->dl_search_path);
129 #ifdef DLSEARCHPATH
130 else
131 lt_dlsetsearchpath(DLSEARCHPATH);
132 #endif
133
134 if (conf->dump_modules) {
135 pa_dump_modules(conf, argc-d, argv+d);
136 retval = 0;
137 goto finish;
138 }
139
140 if (conf->dump_conf) {
141 char *s = pa_conf_dump(conf);
142 fputs(s, stdout);
143 pa_xfree(s);
144 retval = 0;
145 goto finish;
146 }
147
148 if (conf->help) {
149 pa_cmdline_help(argv[0]);
150 retval = 0;
151 goto finish;
152 }
153
154 if (conf->version) {
155 printf(PACKAGE_NAME" "PACKAGE_VERSION"\n");
156 retval = 0;
157 goto finish;
158 }
159
160 if (conf->high_priority)
161 pa_raise_priority();
162
163 if (!conf->stay_root)
164 drop_root();
165
166 if (conf->daemonize) {
167 pid_t child;
168
169 if (pa_stdio_acquire() < 0) {
170 pa_log(__FILE__": failed to acquire stdio.\n");
171 goto finish;
172 }
173
174 if (pipe(daemon_pipe) < 0) {
175 pa_log(__FILE__": failed to create pipe.\n");
176 goto finish;
177 }
178
179 if ((child = fork()) < 0) {
180 pa_log(__FILE__": fork() failed: %s\n", strerror(errno));
181 goto finish;
182 }
183
184 if (child != 0) {
185 /* Father */
186
187 close(daemon_pipe[1]);
188 daemon_pipe[1] = -1;
189
190 if (pa_loop_read(daemon_pipe[0], &retval, sizeof(retval)) != sizeof(retval)) {
191 pa_log(__FILE__": read() failed: %s\n", strerror(errno));
192 retval = 1;
193 }
194
195 goto finish;
196 }
197
198 close(daemon_pipe[0]);
199 daemon_pipe[0] = -1;
200
201
202 if (conf->auto_log_target)
203 pa_log_set_target(PA_LOG_SYSLOG, NULL);
204
205 setsid();
206 setpgrp();
207
208 close(0);
209 close(1);
210 }
211
212
213 pa_log(__FILE__": sizeof(pa_usec_t) = %u\n", sizeof(pa_usec_t));
214
215 mainloop = pa_mainloop_new();
216 assert(mainloop);
217
218 r = pa_signal_init(pa_mainloop_get_api(mainloop));
219 assert(r == 0);
220 pa_signal_new(SIGINT, signal_callback, c);
221 pa_signal_new(SIGTERM, signal_callback, c);
222 signal(SIGPIPE, SIG_IGN);
223
224 c = pa_core_new(pa_mainloop_get_api(mainloop));
225 assert(c);
226
227 pa_signal_new(SIGUSR1, signal_callback, c);
228 pa_signal_new(SIGUSR2, signal_callback, c);
229
230 r = pa_cpu_limit_init(pa_mainloop_get_api(mainloop));
231 assert(r == 0);
232
233 buf = pa_strbuf_new();
234 assert(buf);
235 if (conf->default_script_file)
236 pa_cli_command_execute_file(c, conf->default_script_file, buf, &conf->fail, &conf->verbose);
237 r = pa_cli_command_execute(c, conf->script_commands, buf, &conf->fail, &conf->verbose);
238 pa_log(s = pa_strbuf_tostring_free(buf));
239 pa_xfree(s);
240
241 if (r < 0 && conf->fail) {
242 pa_log(__FILE__": failed to initialize daemon.\n");
243 if (conf->daemonize)
244 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
245 } else if (!c->modules || pa_idxset_ncontents(c->modules) == 0) {
246 pa_log(__FILE__": daemon startup without any loaded modules, refusing to work.\n");
247 if (conf->daemonize)
248 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
249 } else {
250 retval = 0;
251 if (conf->daemonize)
252 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
253
254 c->disallow_module_loading = conf->disallow_module_loading;
255 c->exit_idle_time = conf->exit_idle_time;
256 c->module_idle_time = conf->module_idle_time;
257
258 pa_log(__FILE__": Daemon startup complete.\n");
259 if (pa_mainloop_run(mainloop, &retval) < 0)
260 retval = 1;
261 pa_log(__FILE__": Daemon shutdown initiated.\n");
262 }
263
264 pa_core_free(c);
265
266 pa_cpu_limit_done();
267 pa_signal_done();
268 pa_mainloop_free(mainloop);
269
270 pa_log(__FILE__": Daemon terminated.\n");
271
272 finish:
273
274 if (conf)
275 pa_conf_free(conf);
276
277 close_pipe(daemon_pipe);
278
279 lt_dlexit();
280
281 return retval;
282 }