]> code.delx.au - pulseaudio/blob - polyp/main.c
daemon auto spawn
[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
47 static struct pa_mainloop *mainloop;
48
49 static void drop_root(void) {
50 if (getuid() != 0 && geteuid() == 0) {
51 fprintf(stderr, __FILE__": started SUID root, dropping root rights.\n");
52 setuid(getuid());
53 seteuid(getuid());
54 }
55 }
56
57 static void exit_signal_callback(struct pa_mainloop_api*m, struct pa_signal_event *e, int sig, void *userdata) {
58 m->quit(m, 1);
59 fprintf(stderr, __FILE__": got signal.\n");
60 }
61
62 static void aux_signal_callback(struct pa_mainloop_api*m, struct pa_signal_event *e, int sig, void *userdata) {
63 struct pa_core *c = userdata;
64 assert(c);
65 pa_module_load(c, sig == SIGUSR1 ? "module-cli" : "module-cli-protocol-unix", NULL);
66 }
67
68 static void close_pipe(int p[2]) {
69 if (p[0] != -1)
70 close(p[0]);
71 if (p[1] != -1)
72 close(p[1]);
73 p[0] = p[1] = -1;
74 }
75
76 int main(int argc, char *argv[]) {
77 struct pa_core *c;
78 struct pa_cmdline *cmdline = NULL;
79 struct pa_strbuf *buf = NULL;
80 char *s;
81 int r, retval = 1;
82 int daemon_pipe[2] = { -1, -1 };
83
84 if (!(cmdline = pa_cmdline_parse(argc, argv))) {
85 fprintf(stderr, __FILE__": failed to parse command line.\n");
86 goto finish;
87 }
88
89 if (cmdline->help) {
90 pa_cmdline_help(argv[0]);
91 retval = 0;
92 goto finish;
93 }
94
95 if (cmdline->version) {
96 printf(PACKAGE_NAME" "PACKAGE_VERSION"\n");
97 retval = 0;
98 goto finish;
99 }
100
101 if (cmdline->high_priority)
102 pa_raise_priority();
103
104 if (!cmdline->stay_root)
105 drop_root();
106
107 if (cmdline->daemonize) {
108 pid_t child;
109
110 if (pa_stdio_acquire() < 0) {
111 fprintf(stderr, __FILE__": failed to acquire stdio.\n");
112 goto finish;
113 }
114
115 if (pipe(daemon_pipe) < 0) {
116 fprintf(stderr, __FILE__": failed to create pipe.\n");
117 goto finish;
118 }
119
120 if ((child = fork()) < 0) {
121 fprintf(stderr, __FILE__": fork() failed: %s\n", strerror(errno));
122 goto finish;
123 }
124
125 if (child != 0) {
126 /* Father */
127
128 close(daemon_pipe[1]);
129 daemon_pipe[1] = -1;
130
131 if (pa_loop_read(daemon_pipe[0], &retval, sizeof(retval)) != sizeof(retval)) {
132 fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
133 retval = 1;
134 }
135
136 goto finish;
137 }
138
139 close(daemon_pipe[0]);
140 daemon_pipe[0] = -1;
141
142 setsid();
143 setpgrp();
144
145 close(0);
146 close(1);
147 }
148
149 r = lt_dlinit();
150 assert(r == 0);
151 #ifdef DLSEARCHDIR
152 lt_dladdsearchdir(DLSEARCHDIR);
153 #endif
154
155 mainloop = pa_mainloop_new();
156 assert(mainloop);
157
158 r = pa_signal_init(pa_mainloop_get_api(mainloop));
159 assert(r == 0);
160 pa_signal_new(SIGINT, exit_signal_callback, NULL);
161 pa_signal_new(SIGTERM, exit_signal_callback, NULL);
162 signal(SIGPIPE, SIG_IGN);
163
164 c = pa_core_new(pa_mainloop_get_api(mainloop));
165 assert(c);
166
167 pa_signal_new(SIGUSR1, aux_signal_callback, c);
168 pa_signal_new(SIGUSR2, aux_signal_callback, c);
169
170 buf = pa_strbuf_new();
171 assert(buf);
172 r = pa_cli_command_execute(c, cmdline->cli_commands, buf, &cmdline->fail, &cmdline->verbose);
173 fprintf(stderr, s = pa_strbuf_tostring_free(buf));
174 pa_xfree(s);
175
176 if (r < 0 && cmdline->fail) {
177 fprintf(stderr, __FILE__": failed to initialize daemon.\n");
178 if (cmdline->daemonize)
179 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
180 } else if (!c->modules || pa_idxset_ncontents(c->modules) == 0) {
181 fprintf(stderr, __FILE__": daemon startup without any loaded modules, refusing to work.\n");
182 if (cmdline->daemonize)
183 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
184 } else {
185 retval = 0;
186 if (cmdline->daemonize)
187 pa_loop_write(daemon_pipe[1], &retval, sizeof(retval));
188 fprintf(stderr, __FILE__": mainloop entry.\n");
189 if (pa_mainloop_run(mainloop, &retval) < 0)
190 retval = 1;
191 fprintf(stderr, __FILE__": mainloop exit.\n");
192 }
193
194 pa_core_free(c);
195
196 pa_signal_done();
197 pa_mainloop_free(mainloop);
198
199 lt_dlexit();
200
201 finish:
202
203 if (cmdline)
204 pa_cmdline_free(cmdline);
205
206 close_pipe(daemon_pipe);
207
208 return retval;
209 }