]> code.delx.au - pulseaudio/blob - src/utils/pasuspender.c
don't fail if no pa is srunning
[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/prctl.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31
32 #include <signal.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <assert.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <limits.h>
40 #include <getopt.h>
41
42 #include <sndfile.h>
43
44 #include <pulse/pulseaudio.h>
45 #include <pulsecore/macro.h>
46
47 #if PA_API_VERSION < 10
48 #error Invalid PulseAudio API version
49 #endif
50
51 #define BUFSIZE 1024
52
53 static pa_context *context = NULL;
54 static pa_mainloop_api *mainloop_api = NULL;
55 static char **child_argv = NULL;
56 static int child_argc = 0;
57 static pid_t child_pid = (pid_t) -1;
58 static int child_ret = 0;
59 static int dead = 1;
60
61 static void quit(int ret) {
62 pa_assert(mainloop_api);
63 mainloop_api->quit(mainloop_api, ret);
64 }
65
66
67 static void context_drain_complete(pa_context *c, void *userdata) {
68 pa_context_disconnect(c);
69 }
70
71 static void drain(void) {
72 pa_operation *o;
73
74 if (!(o = pa_context_drain(context, context_drain_complete, NULL)))
75 pa_context_disconnect(context);
76 else
77 pa_operation_unref(o);
78 }
79
80 static void start_child(void) {
81
82 if ((child_pid = fork()) < 0) {
83
84 fprintf(stderr, "fork(): %s\n", strerror(errno));
85 quit(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
106 static void suspend_complete(pa_context *c, int success, void *userdata) {
107 static int n = 0;
108
109 n++;
110
111 if (!success) {
112 fprintf(stderr, "Failure to suspend: %s\n", pa_strerror(pa_context_errno(c)));
113 quit(1);
114 return;
115 }
116
117 if (n >= 2)
118 start_child();
119 }
120
121 static void resume_complete(pa_context *c, int success, void *userdata) {
122 static int n = 0;
123
124 n++;
125
126 if (!success) {
127 fprintf(stderr, "Failure to resume: %s\n", pa_strerror(pa_context_errno(c)));
128 quit(1);
129 return;
130 }
131
132 if (n >= 2)
133 drain(); /* drain and quit */
134 }
135
136 static void context_state_callback(pa_context *c, void *userdata) {
137 pa_assert(c);
138
139 switch (pa_context_get_state(c)) {
140 case PA_CONTEXT_CONNECTING:
141 case PA_CONTEXT_AUTHORIZING:
142 case PA_CONTEXT_SETTING_NAME:
143 break;
144
145 case PA_CONTEXT_READY:
146 pa_operation_unref(pa_context_suspend_sink_by_index(c, PA_INVALID_INDEX, 1, suspend_complete, NULL));
147 pa_operation_unref(pa_context_suspend_source_by_index(c, PA_INVALID_INDEX, 1, suspend_complete, NULL));
148 break;
149
150 case PA_CONTEXT_TERMINATED:
151 quit(0);
152 break;
153
154 case PA_CONTEXT_FAILED:
155 default:
156 fprintf(stderr, "Connection failure: %s\n", pa_strerror(pa_context_errno(c)));
157
158 pa_context_unref(context);
159 context = NULL;
160
161 if (child_pid == (pid_t) -1)
162 /* not started yet, then we do it now */
163 start_child();
164 else if (dead)
165 /* already started, and dead, so let's quit */
166 quit(1);
167
168 break;
169 }
170 }
171
172 static void sigint_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
173 fprintf(stderr, "Got SIGINT, exiting.\n");
174 quit(0);
175 }
176
177 static void sigchld_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) {
178 int status = 0;
179 pid_t p;
180
181 p = waitpid(-1, &status, WNOHANG);
182
183 if (p != child_pid)
184 return;
185
186 dead = 1;
187
188 if (WIFEXITED(status))
189 child_ret = WEXITSTATUS(status);
190 else if (WIFSIGNALED(status)) {
191 fprintf(stderr, "WARNING: Child process terminated by signal %u\n", WTERMSIG(status));
192 child_ret = 1;
193 }
194
195 if (context) {
196 /* A context is around, so let's resume */
197 pa_operation_unref(pa_context_suspend_sink_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL));
198 pa_operation_unref(pa_context_suspend_source_by_index(context, PA_INVALID_INDEX, 0, resume_complete, NULL));
199 } else
200 /* Hmm, no context here, so let's terminate right away */
201 quit(0);
202 }
203
204 static void help(const char *argv0) {
205
206 printf("%s [options] ... \n\n"
207 " -h, --help Show this help\n"
208 " --version Show version\n"
209 " -s, --server=SERVER The name of the server to connect to\n\n",
210 argv0);
211 }
212
213 enum {
214 ARG_VERSION = 256
215 };
216
217 int main(int argc, char *argv[]) {
218 pa_mainloop* m = NULL;
219 int c, ret = 1;
220 char *server = NULL, *bn;
221
222 static const struct option long_options[] = {
223 {"server", 1, NULL, 's'},
224 {"version", 0, NULL, ARG_VERSION},
225 {"help", 0, NULL, 'h'},
226 {NULL, 0, NULL, 0}
227 };
228
229 if (!(bn = strrchr(argv[0], '/')))
230 bn = argv[0];
231 else
232 bn++;
233
234 while ((c = getopt_long(argc, argv, "s:h", long_options, NULL)) != -1) {
235 switch (c) {
236 case 'h' :
237 help(bn);
238 ret = 0;
239 goto quit;
240
241 case ARG_VERSION:
242 printf("pasuspender "PACKAGE_VERSION"\nCompiled with libpulse %s\nLinked with libpulse %s\n", pa_get_headers_version(), pa_get_library_version());
243 ret = 0;
244 goto quit;
245
246 case 's':
247 pa_xfree(server);
248 server = pa_xstrdup(optarg);
249 break;
250
251 default:
252 goto quit;
253 }
254 }
255
256 child_argv = argv + optind;
257 child_argc = argc - optind;
258
259 if (child_argc <= 0) {
260 help(bn);
261 ret = 0;
262 goto quit;
263 }
264
265 if (!(m = pa_mainloop_new())) {
266 fprintf(stderr, "pa_mainloop_new() failed.\n");
267 goto quit;
268 }
269
270 pa_assert_se(mainloop_api = pa_mainloop_get_api(m));
271 pa_assert_se(pa_signal_init(mainloop_api) == 0);
272 pa_signal_new(SIGINT, sigint_callback, NULL);
273 pa_signal_new(SIGCHLD, sigchld_callback, NULL);
274 #ifdef SIGPIPE
275 signal(SIGPIPE, SIG_IGN);
276 #endif
277
278 if (!(context = pa_context_new(mainloop_api, bn))) {
279 fprintf(stderr, "pa_context_new() failed.\n");
280 goto quit;
281 }
282
283 pa_context_set_state_callback(context, context_state_callback, NULL);
284 pa_context_connect(context, server, PA_CONTEXT_NOAUTOSPAWN, NULL);
285
286 if (pa_mainloop_run(m, &ret) < 0) {
287 fprintf(stderr, "pa_mainloop_run() failed.\n");
288 goto quit;
289 }
290
291 quit:
292 if (context)
293 pa_context_unref(context);
294
295 if (m) {
296 pa_signal_done();
297 pa_mainloop_free(m);
298 }
299
300 pa_xfree(server);
301
302 if (!dead)
303 kill(child_pid, SIGTERM);
304
305 return ret == 0 ? child_ret : ret;
306 }