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