]> code.delx.au - pulseaudio/blob - src/utils/pasuspender.c
Merge dead branch 'lennart'
[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 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 <limits.h>
37 #include <getopt.h>
38
39 #include <sndfile.h>
40
41 #ifdef __linux__
42 #include <sys/prctl.h>
43 #endif
44
45 #include <pulse/pulseaudio.h>
46 #include <pulsecore/macro.h>
47
48 #if PA_API_VERSION < 10
49 #error Invalid PulseAudio API version
50 #endif
51
52 #define BUFSIZE 1024
53
54 static pa_context *context = NULL;
55 static pa_mainloop_api *mainloop_api = NULL;
56 static char **child_argv = NULL;
57 static int child_argc = 0;
58 static pid_t child_pid = (pid_t) -1;
59 static int child_ret = 0;
60 static int dead = 1;
61
62 static void quit(int ret) {
63 pa_assert(mainloop_api);
64 mainloop_api->quit(mainloop_api, ret);
65 }
66
67
68 static void context_drain_complete(pa_context *c, void *userdata) {
69 pa_context_disconnect(c);
70 }
71
72 static void drain(void) {
73 pa_operation *o;
74
75 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
76 pa_context_disconnect(context);
77 else
78 pa_operation_unref(o);
79 }
80
81 static void start_child(void) {
82
83 if ((child_pid = fork()) < 0) {
84
85 fprintf(stderr, "fork(): %s\n", strerror(errno));
86 quit(1);
87
88 } else if (child_pid == 0) {
89 /* Child */
90
91 #ifdef __linux__
92 prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0);
93 #endif
94
95 if (execvp(child_argv[0], child_argv) < 0)
96 fprintf(stderr, "execvp(): %s\n", strerror(errno));
97
98 _exit(1);
99
100 } else {
101
102 /* parent */
103 dead = 0;
104 }
105 }
106
107 static void suspend_complete(pa_context *c, int success, void *userdata) {
108 static int n = 0;
109
110 n++;
111
112 if (!success) {
113 fprintf(stderr, "Failure to suspend: %s\n", pa_strerror(pa_context_errno(c)));
114 quit(1);
115 return;
116 }
117
118 if (n >= 2)
119 start_child();
120 }
121
122 static void resume_complete(pa_context *c, int success, void *userdata) {
123 static int n = 0;
124
125 n++;
126
127 if (!success) {
128 fprintf(stderr, "Failure to resume: %s\n", pa_strerror(pa_context_errno(c)));
129 quit(1);
130 return;
131 }
132
133 if (n >= 2)
134 drain(); /* drain and quit */
135 }
136
137 static void context_state_callback(pa_context *c, void *userdata) {
138 pa_assert(c);
139
140 switch (pa_context_get_state(c)) {
141 case PA_CONTEXT_CONNECTING:
142 case PA_CONTEXT_AUTHORIZING:
143 case PA_CONTEXT_SETTING_NAME:
144 break;
145
146 case PA_CONTEXT_READY:
147 if (pa_context_is_local(c)) {
148 pa_operation_unref(pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, 1, suspend_complete, NULL));
149 pa_operation_unref(pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, 1, suspend_complete, NULL));
150 } else {
151 fprintf(stderr, "WARNING: Sound server is not local, not suspending.\n");
152 start_child();
153 }
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 }