]> code.delx.au - pulseaudio/blob - src/utils/pasuspender.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / utils / pasuspender.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <sys/types.h>
29 #include <sys/wait.h>
30
31 #include <signal.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <assert.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <limits.h>
39 #include <getopt.h>
40
41 #include <sndfile.h>
42
43 #ifdef __linux__
44 #include <sys/prctl.h>
45 #endif
46
47 #include <pulse/pulseaudio.h>
48 #include <pulsecore/macro.h>
49
50 #if PA_API_VERSION < 10
51 #error Invalid PulseAudio API version
52 #endif
53
54 #define BUFSIZE 1024
55
56 static pa_context *context = NULL;
57 static pa_mainloop_api *mainloop_api = NULL;
58 static char **child_argv = NULL;
59 static int child_argc = 0;
60 static pid_t child_pid = (pid_t) -1;
61 static int child_ret = 0;
62 static int dead = 1;
63
64 static void quit(int ret) {
65 pa_assert(mainloop_api);
66 mainloop_api->quit(mainloop_api, ret);
67 }
68
69
70 static void context_drain_complete(pa_context *c, void *userdata) {
71 pa_context_disconnect(c);
72 }
73
74 static void drain(void) {
75 pa_operation *o;
76
77 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
78 pa_context_disconnect(context);
79 else
80 pa_operation_unref(o);
81 }
82
83 static void start_child(void) {
84
85 if ((child_pid = fork()) < 0) {
86
87 fprintf(stderr, "fork(): %s\n", strerror(errno));
88 quit(1);
89
90 } else if (child_pid == 0) {
91 /* Child */
92
93 #ifdef __linux__
94 prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
95 #endif
96
97 if (execvp(child_argv[0], child_argv) < 0)
98 fprintf(stderr, "execvp(): %s\n", strerror(errno));
99
100 _exit(1);
101
102 } else {
103
104 /* parent */
105 dead = 0;
106 }
107 }
108
109 static void suspend_complete(pa_context *c, int success, void *userdata) {
110 static int n = 0;
111
112 n++;
113
114 if (!success) {
115 fprintf(stderr, "Failure to suspend: %s\n", pa_strerror(pa_context_errno(c)));
116 quit(1);
117 return;
118 }
119
120 if (n >= 2)
121 start_child();
122 }
123
124 static void resume_complete(pa_context *c, int success, void *userdata) {
125 static int n = 0;
126
127 n++;
128
129 if (!success) {
130 fprintf(stderr, "Failure to resume: %s\n", pa_strerror(pa_context_errno(c)));
131 quit(1);
132 return;
133 }
134
135 if (n >= 2)
136 drain(); /* drain and quit */
137 }
138
139 static void context_state_callback(pa_context *c, void *userdata) {
140 pa_assert(c);
141
142 switch (pa_context_get_state(c)) {
143 case PA_CONTEXT_CONNECTING:
144 case PA_CONTEXT_AUTHORIZING:
145 case PA_CONTEXT_SETTING_NAME:
146 break;
147
148 case PA_CONTEXT_READY:
149 if (pa_context_is_local(c)) {
150 pa_operation_unref(pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, 1, suspend_complete, NULL));
151 pa_operation_unref(pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, 1, suspend_complete, NULL));
152 } else
153 start_child();
154
155 break;
156
157 case PA_CONTEXT_TERMINATED:
158 quit(0);
159 break;
160
161 case PA_CONTEXT_FAILED:
162 default:
163 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
164
165 pa_context_unref(context);
166 context = NULL;
167
168 if (child_pid == (pid_t) -1)
169 /* not started yet, then we do it now */
170 start_child();
171 else if (dead)
172 /* already started, and dead, so let's quit */
173 quit(1);
174
175 break;
176 }
177 }
178
179 static void sigint_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
180 fprintf(stderr, "Got SIGINT, exiting.\n");
181 quit(0);
182 }
183
184 static void sigchld_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
185 int status = 0;
186 pid_t p;
187
188 p = waitpid(-1, &status, WNOHANG);
189
190 if (p != child_pid)
191 return;
192
193 dead = 1;
194
195 if (WIFEXITED(status))
196 child_ret = WEXITSTATUS(status);
197 else if (WIFSIGNALED(status)) {
198 fprintf(stderr, "WARNING: Child process terminated by signal %u\n", WTERMSIG(status));
199 child_ret = 1;
200 }
201
202 if (context) {
203 if (pa_context_is_local(context)) {
204 /* A context is around, so let's resume */
205 pa_operation_unref(pa_context_suspend_sink_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL));
206 pa_operation_unref(pa_context_suspend_source_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL));
207 } else
208 drain();
209 } else
210 /* Hmm, no context here, so let's terminate right away */
211 quit(0);
212 }
213
214 static void help(const char *argv0) {
215
216 printf("%s [options] ... \n\n"
217 " -h, --help Show this help\n"
218 " --version Show version\n"
219 " -s, --server=SERVER The name of the server to connect to\n\n",
220 argv0);
221 }
222
223 enum {
224 ARG_VERSION = 256
225 };
226
227 int main(int argc, char *argv[]) {
228 pa_mainloop* m = NULL;
229 int c, ret = 1;
230 char *server = NULL, *bn;
231
232 static const struct option long_options[] = {
233 {"server", 1, NULL, 's'},
234 {"version", 0, NULL, ARG_VERSION},
235 {"help", 0, NULL, 'h'},
236 {NULL, 0, NULL, 0}
237 };
238
239 if (!(bn = strrchr(argv[0], '/')))
240 bn = argv[0];
241 else
242 bn++;
243
244 while ((c = getopt_long(argc, argv, "s:h", long_options, NULL)) != -1) {
245 switch (c) {
246 case 'h' :
247 help(bn);
248 ret = 0;
249 goto quit;
250
251 case ARG_VERSION:
252 printf("pasuspender "PACKAGE_VERSION"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
253 ret = 0;
254 goto quit;
255
256 case 's':
257 pa_xfree(server);
258 server = pa_xstrdup(optarg);
259 break;
260
261 default:
262 goto quit;
263 }
264 }
265
266 child_argv = argv + optind;
267 child_argc = argc - optind;
268
269 if (child_argc <= 0) {
270 help(bn);
271 ret = 0;
272 goto quit;
273 }
274
275 if (!(m = pa_mainloop_new())) {
276 fprintf(stderr, "pa_mainloop_new() failed.\n");
277 goto quit;
278 }
279
280 pa_assert_se(mainloop_api = pa_mainloop_get_api(m));
281 pa_assert_se(pa_signal_init(mainloop_api) == 0);
282 pa_signal_new(SIGINT, sigint_callback, NULL);
283 pa_signal_new(SIGCHLD, sigchld_callback, NULL);
284 #ifdef SIGPIPE
285 signal(SIGPIPE, SIG_IGN);
286 #endif
287
288 if (!(context = pa_context_new(mainloop_api, bn))) {
289 fprintf(stderr, "pa_context_new() failed.\n");
290 goto quit;
291 }
292
293 pa_context_set_state_callback(context, context_state_callback, NULL);
294 pa_context_connect(context, server, PA_CONTEXT_NOAUTOSPAWN, NULL);
295
296 if (pa_mainloop_run(m, &ret) < 0) {
297 fprintf(stderr, "pa_mainloop_run() failed.\n");
298 goto quit;
299 }
300
301 quit:
302 if (context)
303 pa_context_unref(context);
304
305 if (m) {
306 pa_signal_done();
307 pa_mainloop_free(m);
308 }
309
310 pa_xfree(server);
311
312 if (!dead)
313 kill(child_pid, SIGTERM);
314
315 return ret == 0 ? child_ret : ret;
316 }