]> code.delx.au - pulseaudio/blob - polyp/main.c
make module-combine autoloadable
[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->high_priority && conf->cmd == PA_CMD_DAEMON)
128 pa_raise_priority();
129
130 drop_root();
131
132 if (conf->dl_search_path)
133 lt_dlsetsearchpath(conf->dl_search_path);
134 #ifdef DLSEARCHPATH
135 else
136 lt_dlsetsearchpath(DLSEARCHPATH);
137 #endif
138
139 switch (conf->cmd) {
140 case PA_CMD_DUMP_MODULES:
141 pa_dump_modules(conf, argc-d, argv+d);
142 retval = 0;
143 goto finish;
144
145 case PA_CMD_DUMP_CONF: {
146 char *s = pa_conf_dump(conf);
147 fputs(s, stdout);
148 pa_xfree(s);
149 retval = 0;
150 goto finish;
151 }
152
153 case PA_CMD_HELP :
154 pa_cmdline_help(argv[0]);
155 retval = 0;
156 goto finish;
157
158 case PA_CMD_VERSION :
159 printf(PACKAGE_NAME" "PACKAGE_VERSION"\n");
160 retval = 0;
161 goto finish;
162
163 default:
164 assert(conf->cmd == PA_CMD_DAEMON);
165 }
166
167 if (conf->daemonize) {
168 pid_t child;
169
170 if (pa_stdio_acquire() < 0) {
171 pa_log(__FILE__": failed to acquire stdio.\n");
172 goto finish;
173 }
174
175 if (pipe(daemon_pipe) < 0) {
176 pa_log(__FILE__": failed to create pipe.\n");
177 goto finish;
178 }
179
180 if ((child = fork()) < 0) {
181 pa_log(__FILE__": fork() failed: %s\n", strerror(errno));
182 goto finish;
183 }
184
185 if (child != 0) {
186 /* Father */
187
188 close(daemon_pipe[1]);
189 daemon_pipe[1] = -1;
190
191 if (pa_loop_read(daemon_pipe[0], &retval, sizeof(retval)) != sizeof(retval)) {
192 pa_log(__FILE__": read() failed: %s\n", strerror(errno));
193 retval = 1;
194 }
195
196 goto finish;
197 }
198
199 close(daemon_pipe[0]);
200 daemon_pipe[0] = -1;
201
202
203 if (conf->auto_log_target)
204 pa_log_set_target(PA_LOG_SYSLOG, NULL);
205
206 setsid();
207 setpgrp();
208
209 close(0);
210 close(1);
211 }
212
213
214 pa_log(__FILE__": sizeof(pa_usec_t) = %u\n", sizeof(pa_usec_t));
215
216 mainloop = pa_mainloop_new();
217 assert(mainloop);
218
219 r = pa_signal_init(pa_mainloop_get_api(mainloop));
220 assert(r == 0);
221 pa_signal_new(SIGINT, signal_callback, c);
222 pa_signal_new(SIGTERM, signal_callback, c);
223 signal(SIGPIPE, SIG_IGN);
224
225 c = pa_core_new(pa_mainloop_get_api(mainloop));
226 assert(c);
227
228 pa_signal_new(SIGUSR1, signal_callback, c);
229 pa_signal_new(SIGUSR2, signal_callback, c);
230
231 r = pa_cpu_limit_init(pa_mainloop_get_api(mainloop));
232 assert(r == 0);
233
234 buf = pa_strbuf_new();
235 assert(buf);
236 if (conf->default_script_file)
237 r = pa_cli_command_execute_file(c, conf->default_script_file, buf, &conf->fail, &conf->verbose);
238
239 if (r >= 0)
240 r = pa_cli_command_execute(c, conf->script_commands, buf, &conf->fail, &conf->verbose);
241 pa_log(s = pa_strbuf_tostring_free(buf));
242 pa_xfree(s);
243
244 if (r < 0 && conf->fail) {
245 pa_log(__FILE__": failed to initialize daemon.\n");
246 if (conf->daemonize)
247 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
248 } else if (!c->modules || pa_idxset_ncontents(c->modules) == 0) {
249 pa_log(__FILE__": daemon startup without any loaded modules, refusing to work.\n");
250 if (conf->daemonize)
251 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
252 } else {
253 retval = 0;
254 if (conf->daemonize)
255 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
256
257 c->disallow_module_loading = conf->disallow_module_loading;
258 c->exit_idle_time = conf->exit_idle_time;
259 c->module_idle_time = conf->module_idle_time;
260
261 pa_log(__FILE__": Daemon startup complete.\n");
262 if (pa_mainloop_run(mainloop, &retval) < 0)
263 retval = 1;
264 pa_log(__FILE__": Daemon shutdown initiated.\n");
265 }
266
267 pa_core_free(c);
268
269 pa_cpu_limit_done();
270 pa_signal_done();
271 pa_mainloop_free(mainloop);
272
273 pa_log(__FILE__": Daemon terminated.\n");
274
275 finish:
276
277 if (conf)
278 pa_conf_free(conf);
279
280 close_pipe(daemon_pipe);
281
282 lt_dlexit();
283
284 return retval;
285 }