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