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