]> code.delx.au - pulseaudio/blob - src/utils/pasuspender.c
warn if the sound server is not local
[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 fprintf(stderr, "WARNING: Sound server is not local, not suspending.\n");
154 start_child();
155 }
156
157 break;
158
159 case PA_CONTEXT_TERMINATED:
160 quit(0);
161 break;
162
163 case PA_CONTEXT_FAILED:
164 default:
165 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
166
167 pa_context_unref(context);
168 context = NULL;
169
170 if (child_pid == (pid_t) -1)
171 /* not started yet, then we do it now */
172 start_child();
173 else if (dead)
174 /* already started, and dead, so let's quit */
175 quit(1);
176
177 break;
178 }
179 }
180
181 static void sigint_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
182 fprintf(stderr, "Got SIGINT, exiting.\n");
183 quit(0);
184 }
185
186 static void sigchld_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
187 int status = 0;
188 pid_t p;
189
190 p = waitpid(-1, &status, WNOHANG);
191
192 if (p != child_pid)
193 return;
194
195 dead = 1;
196
197 if (WIFEXITED(status))
198 child_ret = WEXITSTATUS(status);
199 else if (WIFSIGNALED(status)) {
200 fprintf(stderr, "WARNING: Child process terminated by signal %u\n", WTERMSIG(status));
201 child_ret = 1;
202 }
203
204 if (context) {
205 if (pa_context_is_local(context)) {
206 /* A context is around, so let's resume */
207 pa_operation_unref(pa_context_suspend_sink_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL));
208 pa_operation_unref(pa_context_suspend_source_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL));
209 } else
210 drain();
211 } else
212 /* Hmm, no context here, so let's terminate right away */
213 quit(0);
214 }
215
216 static void help(const char *argv0) {
217
218 printf("%s [options] ... \n\n"
219 " -h, --help Show this help\n"
220 " --version Show version\n"
221 " -s, --server=SERVER The name of the server to connect to\n\n",
222 argv0);
223 }
224
225 enum {
226 ARG_VERSION = 256
227 };
228
229 int main(int argc, char *argv[]) {
230 pa_mainloop* m = NULL;
231 int c, ret = 1;
232 char *server = NULL, *bn;
233
234 static const struct option long_options[] = {
235 {"server", 1, NULL, 's'},
236 {"version", 0, NULL, ARG_VERSION},
237 {"help", 0, NULL, 'h'},
238 {NULL, 0, NULL, 0}
239 };
240
241 if (!(bn = strrchr(argv[0], '/')))
242 bn = argv[0];
243 else
244 bn++;
245
246 while ((c = getopt_long(argc, argv, "s:h", long_options, NULL)) != -1) {
247 switch (c) {
248 case 'h' :
249 help(bn);
250 ret = 0;
251 goto quit;
252
253 case ARG_VERSION:
254 printf("pasuspender "PACKAGE_VERSION"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
255 ret = 0;
256 goto quit;
257
258 case 's':
259 pa_xfree(server);
260 server = pa_xstrdup(optarg);
261 break;
262
263 default:
264 goto quit;
265 }
266 }
267
268 child_argv = argv + optind;
269 child_argc = argc - optind;
270
271 if (child_argc <= 0) {
272 help(bn);
273 ret = 0;
274 goto quit;
275 }
276
277 if (!(m = pa_mainloop_new())) {
278 fprintf(stderr, "pa_mainloop_new() failed.\n");
279 goto quit;
280 }
281
282 pa_assert_se(mainloop_api = pa_mainloop_get_api(m));
283 pa_assert_se(pa_signal_init(mainloop_api) == 0);
284 pa_signal_new(SIGINT, sigint_callback, NULL);
285 pa_signal_new(SIGCHLD, sigchld_callback, NULL);
286 #ifdef SIGPIPE
287 signal(SIGPIPE, SIG_IGN);
288 #endif
289
290 if (!(context = pa_context_new(mainloop_api, bn))) {
291 fprintf(stderr, "pa_context_new() failed.\n");
292 goto quit;
293 }
294
295 pa_context_set_state_callback(context, context_state_callback, NULL);
296 pa_context_connect(context, server, PA_CONTEXT_NOAUTOSPAWN, NULL);
297
298 if (pa_mainloop_run(m, &ret) < 0) {
299 fprintf(stderr, "pa_mainloop_run() failed.\n");
300 goto quit;
301 }
302
303 quit:
304 if (context)
305 pa_context_unref(context);
306
307 if (m) {
308 pa_signal_done();
309 pa_mainloop_free(m);
310 }
311
312 pa_xfree(server);
313
314 if (!dead)
315 kill(child_pid, SIGTERM);
316
317 return ret == 0 ? child_ret : ret;
318 }