]> code.delx.au - pulseaudio/blob - src/pulse/context.c
add new "disable-shm" option to client.conf
[pulseaudio] / src / pulse / context.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
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 <stdio.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <errno.h>
34 #include <signal.h>
35 #include <limits.h>
36
37 #ifdef HAVE_SYS_WAIT_H
38 #include <sys/wait.h>
39 #endif
40
41 #ifdef HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
43 #endif
44 #ifdef HAVE_SYS_UN_H
45 #include <sys/un.h>
46 #endif
47 #ifdef HAVE_NETDB_H
48 #include <netdb.h>
49 #endif
50
51 #include "../pulsecore/winsock.h"
52
53 #include <pulsecore/core-error.h>
54 #include <pulse/version.h>
55 #include <pulse/xmalloc.h>
56
57 #include <pulsecore/native-common.h>
58 #include <pulsecore/pdispatch.h>
59 #include <pulsecore/pstream.h>
60 #include <pulsecore/dynarray.h>
61 #include <pulsecore/socket-client.h>
62 #include <pulsecore/pstream-util.h>
63 #include <pulsecore/core-util.h>
64 #include <pulsecore/log.h>
65 #include <pulsecore/socket-util.h>
66 #include <pulsecore/creds.h>
67
68 #include "internal.h"
69
70 #include "client-conf.h"
71
72 #ifdef HAVE_X11
73 #include "client-conf-x11.h"
74 #endif
75
76 #include "context.h"
77
78 #define AUTOSPAWN_LOCK "autospawn.lock"
79
80 static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
81 [PA_COMMAND_REQUEST] = pa_command_request,
82 [PA_COMMAND_OVERFLOW] = pa_command_overflow_or_underflow,
83 [PA_COMMAND_UNDERFLOW] = pa_command_overflow_or_underflow,
84 [PA_COMMAND_PLAYBACK_STREAM_KILLED] = pa_command_stream_killed,
85 [PA_COMMAND_RECORD_STREAM_KILLED] = pa_command_stream_killed,
86 [PA_COMMAND_SUBSCRIBE_EVENT] = pa_command_subscribe_event
87 };
88
89 static void unlock_autospawn_lock_file(pa_context *c) {
90 assert(c);
91
92 if (c->autospawn_lock_fd >= 0) {
93 char lf[PATH_MAX];
94 pa_runtime_path(AUTOSPAWN_LOCK, lf, sizeof(lf));
95
96 pa_unlock_lockfile(lf, c->autospawn_lock_fd);
97 c->autospawn_lock_fd = -1;
98 }
99 }
100
101 pa_context *pa_context_new(pa_mainloop_api *mainloop, const char *name) {
102 pa_context *c;
103
104 assert(mainloop);
105 assert(name);
106
107 c = pa_xnew(pa_context, 1);
108 c->ref = 1;
109 c->name = pa_xstrdup(name);
110 c->mainloop = mainloop;
111 c->client = NULL;
112 c->pstream = NULL;
113 c->pdispatch = NULL;
114 c->playback_streams = pa_dynarray_new();
115 c->record_streams = pa_dynarray_new();
116
117 PA_LLIST_HEAD_INIT(pa_stream, c->streams);
118 PA_LLIST_HEAD_INIT(pa_operation, c->operations);
119
120 c->error = PA_OK;
121 c->state = PA_CONTEXT_UNCONNECTED;
122 c->ctag = 0;
123 c->csyncid = 0;
124
125 c->state_callback = NULL;
126 c->state_userdata = NULL;
127
128 c->subscribe_callback = NULL;
129 c->subscribe_userdata = NULL;
130
131 c->is_local = -1;
132 c->server_list = NULL;
133 c->server = NULL;
134 c->autospawn_lock_fd = -1;
135 memset(&c->spawn_api, 0, sizeof(c->spawn_api));
136 c->do_autospawn = 0;
137
138 #ifndef MSG_NOSIGNAL
139 #ifdef SIGPIPE
140 pa_check_signal_is_blocked(SIGPIPE);
141 #endif
142 #endif
143
144 c->conf = pa_client_conf_new();
145 pa_client_conf_load(c->conf, NULL);
146 #ifdef HAVE_X11
147 pa_client_conf_from_x11(c->conf, NULL);
148 #endif
149 pa_client_conf_env(c->conf);
150
151 c->mempool = pa_mempool_new(!c->conf->disable_shm);
152
153 return c;
154 }
155
156 static void context_free(pa_context *c) {
157 assert(c);
158
159 unlock_autospawn_lock_file(c);
160
161 while (c->operations)
162 pa_operation_cancel(c->operations);
163
164 while (c->streams)
165 pa_stream_set_state(c->streams, PA_STREAM_TERMINATED);
166
167 if (c->client)
168 pa_socket_client_unref(c->client);
169 if (c->pdispatch)
170 pa_pdispatch_unref(c->pdispatch);
171 if (c->pstream) {
172 pa_pstream_close(c->pstream);
173 pa_pstream_unref(c->pstream);
174 }
175
176 if (c->record_streams)
177 pa_dynarray_free(c->record_streams, NULL, NULL);
178 if (c->playback_streams)
179 pa_dynarray_free(c->playback_streams, NULL, NULL);
180
181 pa_mempool_free(c->mempool);
182
183 if (c->conf)
184 pa_client_conf_free(c->conf);
185
186 pa_strlist_free(c->server_list);
187
188 pa_xfree(c->name);
189 pa_xfree(c->server);
190 pa_xfree(c);
191 }
192
193 pa_context* pa_context_ref(pa_context *c) {
194 assert(c);
195 assert(c->ref >= 1);
196
197 c->ref++;
198 return c;
199 }
200
201 void pa_context_unref(pa_context *c) {
202 assert(c);
203 assert(c->ref >= 1);
204
205 if (--c->ref <= 0)
206 context_free(c);
207 }
208
209 void pa_context_set_state(pa_context *c, pa_context_state_t st) {
210 assert(c);
211 assert(c->ref >= 1);
212
213 if (c->state == st)
214 return;
215
216 pa_context_ref(c);
217
218 c->state = st;
219 if (c->state_callback)
220 c->state_callback(c, c->state_userdata);
221
222 if (st == PA_CONTEXT_FAILED || st == PA_CONTEXT_TERMINATED) {
223 pa_stream *s;
224
225 s = c->streams ? pa_stream_ref(c->streams) : NULL;
226 while (s) {
227 pa_stream *n = s->next ? pa_stream_ref(s->next) : NULL;
228 pa_stream_set_state(s, st == PA_CONTEXT_FAILED ? PA_STREAM_FAILED : PA_STREAM_TERMINATED);
229 pa_stream_unref(s);
230 s = n;
231 }
232
233 if (c->pdispatch)
234 pa_pdispatch_unref(c->pdispatch);
235 c->pdispatch = NULL;
236
237 if (c->pstream) {
238 pa_pstream_close(c->pstream);
239 pa_pstream_unref(c->pstream);
240 }
241 c->pstream = NULL;
242
243 if (c->client)
244 pa_socket_client_unref(c->client);
245 c->client = NULL;
246 }
247
248 pa_context_unref(c);
249 }
250
251 void pa_context_fail(pa_context *c, int error) {
252 assert(c);
253 assert(c->ref >= 1);
254
255 pa_context_set_error(c, error);
256 pa_context_set_state(c, PA_CONTEXT_FAILED);
257 }
258
259 int pa_context_set_error(pa_context *c, int error) {
260 assert(error >= 0);
261 assert(error < PA_ERR_MAX);
262
263 if (c)
264 c->error = error;
265
266 return error;
267 }
268
269 static void pstream_die_callback(pa_pstream *p, void *userdata) {
270 pa_context *c = userdata;
271
272 assert(p);
273 assert(c);
274
275 pa_context_fail(c, PA_ERR_CONNECTIONTERMINATED);
276 }
277
278 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const pa_creds *creds, void *userdata) {
279 pa_context *c = userdata;
280
281 assert(p);
282 assert(packet);
283 assert(c);
284
285 pa_context_ref(c);
286
287 if (pa_pdispatch_run(c->pdispatch, packet, creds, c) < 0)
288 pa_context_fail(c, PA_ERR_PROTOCOL);
289
290 pa_context_unref(c);
291 }
292
293 static void pstream_memblock_callback(pa_pstream *p, uint32_t channel, int64_t offset, pa_seek_mode_t seek, const pa_memchunk *chunk, void *userdata) {
294 pa_context *c = userdata;
295 pa_stream *s;
296
297 assert(p);
298 assert(chunk);
299 assert(chunk->memblock);
300 assert(chunk->length);
301 assert(c);
302 assert(c->ref >= 1);
303
304 pa_context_ref(c);
305
306 if ((s = pa_dynarray_get(c->record_streams, channel))) {
307
308 assert(seek == PA_SEEK_RELATIVE && offset == 0);
309
310 pa_memblockq_seek(s->record_memblockq, offset, seek);
311 pa_memblockq_push_align(s->record_memblockq, chunk);
312
313 if (s->read_callback) {
314 size_t l;
315
316 if ((l = pa_memblockq_get_length(s->record_memblockq)) > 0)
317 s->read_callback(s, l, s->read_userdata);
318 }
319 }
320
321 pa_context_unref(c);
322 }
323
324 int pa_context_handle_error(pa_context *c, uint32_t command, pa_tagstruct *t) {
325 assert(c);
326 assert(c->ref >= 1);
327
328 if (command == PA_COMMAND_ERROR) {
329 assert(t);
330
331 if (pa_tagstruct_getu32(t, &c->error) < 0) {
332 pa_context_fail(c, PA_ERR_PROTOCOL);
333 return -1;
334
335 }
336 } else if (command == PA_COMMAND_TIMEOUT)
337 c->error = PA_ERR_TIMEOUT;
338 else {
339 pa_context_fail(c, PA_ERR_PROTOCOL);
340 return -1;
341 }
342
343 return 0;
344 }
345
346 static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
347 pa_context *c = userdata;
348
349 assert(pd);
350 assert(c);
351 assert(c->state == PA_CONTEXT_AUTHORIZING || c->state == PA_CONTEXT_SETTING_NAME);
352
353 pa_context_ref(c);
354
355 if (command != PA_COMMAND_REPLY) {
356
357 if (pa_context_handle_error(c, command, t) < 0)
358 pa_context_fail(c, PA_ERR_PROTOCOL);
359
360 pa_context_fail(c, c->error);
361 goto finish;
362 }
363
364 switch(c->state) {
365 case PA_CONTEXT_AUTHORIZING: {
366 pa_tagstruct *reply;
367
368 if (pa_tagstruct_getu32(t, &c->version) < 0 ||
369 !pa_tagstruct_eof(t)) {
370 pa_context_fail(c, PA_ERR_PROTOCOL);
371 goto finish;
372 }
373
374 /* Minimum supported version */
375 if (c->version < 8) {
376 pa_context_fail(c, PA_ERR_VERSION);
377 goto finish;
378 }
379
380 /* Enable shared memory support if possible */
381 if (c->version >= 10 &&
382 pa_mempool_is_shared(c->mempool) &&
383 c->is_local) {
384
385 /* Only enable SHM if both sides are owned by the same
386 * user. This is a security measure because otherwise
387 * data private to the user might leak. */
388
389 const pa_creds *creds;
390 if ((creds = pa_pdispatch_creds(pd)))
391 if (getuid() == creds->uid)
392 pa_pstream_use_shm(c->pstream, 1);
393 }
394
395 reply = pa_tagstruct_command(c, PA_COMMAND_SET_CLIENT_NAME, &tag);
396 pa_tagstruct_puts(reply, c->name);
397 pa_pstream_send_tagstruct(c->pstream, reply);
398 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c, NULL);
399
400 pa_context_set_state(c, PA_CONTEXT_SETTING_NAME);
401 break;
402 }
403
404 case PA_CONTEXT_SETTING_NAME :
405 pa_context_set_state(c, PA_CONTEXT_READY);
406 break;
407
408 default:
409 assert(0);
410 }
411
412 finish:
413 pa_context_unref(c);
414 }
415
416 static void setup_context(pa_context *c, pa_iochannel *io) {
417 pa_tagstruct *t;
418 uint32_t tag;
419
420 assert(c);
421 assert(io);
422
423 pa_context_ref(c);
424
425 assert(!c->pstream);
426 c->pstream = pa_pstream_new(c->mainloop, io, c->mempool);
427
428 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
429 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
430 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
431
432 assert(!c->pdispatch);
433 c->pdispatch = pa_pdispatch_new(c->mainloop, command_table, PA_COMMAND_MAX);
434
435 if (!c->conf->cookie_valid) {
436 pa_context_fail(c, PA_ERR_AUTHKEY);
437 goto finish;
438 }
439
440 t = pa_tagstruct_command(c, PA_COMMAND_AUTH, &tag);
441 pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION);
442 pa_tagstruct_put_arbitrary(t, c->conf->cookie, sizeof(c->conf->cookie));
443
444 #ifdef HAVE_CREDS
445 {
446 pa_creds ucred;
447
448 if (pa_iochannel_creds_supported(io))
449 pa_iochannel_creds_enable(io);
450
451 ucred.uid = getuid();
452 ucred.gid = getgid();
453
454 pa_pstream_send_tagstruct_with_creds(c->pstream, t, &ucred);
455 }
456 #else
457 pa_pstream_send_tagstruct(c->pstream, t, NULL);
458 #endif
459
460 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c, NULL);
461
462 pa_context_set_state(c, PA_CONTEXT_AUTHORIZING);
463
464 finish:
465
466 pa_context_unref(c);
467 }
468
469 static void on_connection(pa_socket_client *client, pa_iochannel*io, void *userdata);
470
471 #ifndef OS_IS_WIN32
472
473 static int context_connect_spawn(pa_context *c) {
474 pid_t pid;
475 int status, r;
476 int fds[2] = { -1, -1} ;
477 pa_iochannel *io;
478
479 pa_context_ref(c);
480
481 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
482 pa_log("socketpair(): %s", pa_cstrerror(errno));
483 pa_context_fail(c, PA_ERR_INTERNAL);
484 goto fail;
485 }
486
487 pa_fd_set_cloexec(fds[0], 1);
488
489 pa_socket_low_delay(fds[0]);
490 pa_socket_low_delay(fds[1]);
491
492 if (c->spawn_api.prefork)
493 c->spawn_api.prefork();
494
495 if ((pid = fork()) < 0) {
496 pa_log("fork(): %s", pa_cstrerror(errno));
497 pa_context_fail(c, PA_ERR_INTERNAL);
498
499 if (c->spawn_api.postfork)
500 c->spawn_api.postfork();
501
502 goto fail;
503 } else if (!pid) {
504 /* Child */
505
506 char t[128];
507 const char *state = NULL;
508 #define MAX_ARGS 64
509 const char * argv[MAX_ARGS+1];
510 int n;
511
512 /* Not required, since fds[0] has CLOEXEC enabled anyway */
513 close(fds[0]);
514
515 if (c->spawn_api.atfork)
516 c->spawn_api.atfork();
517
518 /* Setup argv */
519
520 n = 0;
521
522 argv[n++] = c->conf->daemon_binary;
523 argv[n++] = "--daemonize=yes";
524
525 snprintf(t, sizeof(t), "-Lmodule-native-protocol-fd fd=%i", fds[1]);
526 argv[n++] = strdup(t);
527
528 while (n < MAX_ARGS) {
529 char *a;
530
531 if (!(a = pa_split_spaces(c->conf->extra_arguments, &state)))
532 break;
533
534 argv[n++] = a;
535 }
536
537 argv[n++] = NULL;
538
539 execv(argv[0], (char * const *) argv);
540 _exit(1);
541 #undef MAX_ARGS
542 }
543
544 /* Parent */
545
546 r = waitpid(pid, &status, 0);
547
548 if (c->spawn_api.postfork)
549 c->spawn_api.postfork();
550
551 if (r < 0) {
552 pa_log("waitpid(): %s", pa_cstrerror(errno));
553 pa_context_fail(c, PA_ERR_INTERNAL);
554 goto fail;
555 } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
556 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
557 goto fail;
558 }
559
560 close(fds[1]);
561
562 c->is_local = 1;
563
564 io = pa_iochannel_new(c->mainloop, fds[0], fds[0]);
565
566 setup_context(c, io);
567 unlock_autospawn_lock_file(c);
568
569 pa_context_unref(c);
570
571 return 0;
572
573 fail:
574 if (fds[0] != -1)
575 close(fds[0]);
576 if (fds[1] != -1)
577 close(fds[1]);
578
579 unlock_autospawn_lock_file(c);
580
581 pa_context_unref(c);
582
583 return -1;
584 }
585
586 #endif /* OS_IS_WIN32 */
587
588 static int try_next_connection(pa_context *c) {
589 char *u = NULL;
590 int r = -1;
591
592 assert(c);
593 assert(!c->client);
594
595 for (;;) {
596 pa_xfree(u);
597 u = NULL;
598
599 c->server_list = pa_strlist_pop(c->server_list, &u);
600
601 if (!u) {
602
603 #ifndef OS_IS_WIN32
604 if (c->do_autospawn) {
605 r = context_connect_spawn(c);
606 goto finish;
607 }
608 #endif
609
610 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
611 goto finish;
612 }
613
614 pa_log_debug("Trying to connect to %s...", u);
615
616 pa_xfree(c->server);
617 c->server = pa_xstrdup(u);
618
619 if (!(c->client = pa_socket_client_new_string(c->mainloop, u, PA_NATIVE_DEFAULT_PORT)))
620 continue;
621
622 c->is_local = pa_socket_client_is_local(c->client);
623 pa_socket_client_set_callback(c->client, on_connection, c);
624 break;
625 }
626
627 r = 0;
628
629 finish:
630 pa_xfree(u);
631
632 return r;
633 }
634
635 static void on_connection(pa_socket_client *client, pa_iochannel*io, void *userdata) {
636 pa_context *c = userdata;
637
638 assert(client);
639 assert(c);
640 assert(c->state == PA_CONTEXT_CONNECTING);
641
642 pa_context_ref(c);
643
644 pa_socket_client_unref(client);
645 c->client = NULL;
646
647 if (!io) {
648 /* Try the item in the list */
649 if (errno == ECONNREFUSED || errno == ETIMEDOUT || errno == EHOSTUNREACH) {
650 try_next_connection(c);
651 goto finish;
652 }
653
654 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
655 goto finish;
656 }
657
658 unlock_autospawn_lock_file(c);
659 setup_context(c, io);
660
661 finish:
662 pa_context_unref(c);
663 }
664
665 int pa_context_connect(
666 pa_context *c,
667 const char *server,
668 pa_context_flags_t flags,
669 const pa_spawn_api *api) {
670
671 int r = -1;
672
673 assert(c);
674 assert(c->ref >= 1);
675
676 PA_CHECK_VALIDITY(c, c->state == PA_CONTEXT_UNCONNECTED, PA_ERR_BADSTATE);
677 PA_CHECK_VALIDITY(c, !(flags & ~PA_CONTEXT_NOAUTOSPAWN), PA_ERR_INVALID);
678 PA_CHECK_VALIDITY(c, !server || *server, PA_ERR_INVALID);
679
680 if (!server)
681 server = c->conf->default_server;
682
683 pa_context_ref(c);
684
685 assert(!c->server_list);
686
687 if (server) {
688 if (!(c->server_list = pa_strlist_parse(server))) {
689 pa_context_fail(c, PA_ERR_INVALIDSERVER);
690 goto finish;
691 }
692 } else {
693 char *d;
694 char ufn[PATH_MAX];
695
696 /* Prepend in reverse order */
697
698 if ((d = getenv("DISPLAY"))) {
699 char *e;
700 d = pa_xstrdup(d);
701 if ((e = strchr(d, ':')))
702 *e = 0;
703
704 if (*d)
705 c->server_list = pa_strlist_prepend(c->server_list, d);
706
707 pa_xfree(d);
708 }
709
710 c->server_list = pa_strlist_prepend(c->server_list, "tcp6:localhost");
711 c->server_list = pa_strlist_prepend(c->server_list, "tcp4:localhost");
712
713 /* The system wide instance */
714 c->server_list = pa_strlist_prepend(c->server_list, PA_SYSTEM_RUNTIME_PATH "/" PA_NATIVE_DEFAULT_UNIX_SOCKET);
715
716 /* The per-user instance */
717 c->server_list = pa_strlist_prepend(c->server_list, pa_runtime_path(PA_NATIVE_DEFAULT_UNIX_SOCKET, ufn, sizeof(ufn)));
718
719 /* Wrap the connection attempts in a single transaction for sane autospawn locking */
720 if (!(flags & PA_CONTEXT_NOAUTOSPAWN) && c->conf->autospawn) {
721 char lf[PATH_MAX];
722
723 pa_runtime_path(AUTOSPAWN_LOCK, lf, sizeof(lf));
724 pa_make_secure_parent_dir(lf, 0700, (uid_t)-1, (gid_t)-1);
725 assert(c->autospawn_lock_fd <= 0);
726 c->autospawn_lock_fd = pa_lock_lockfile(lf);
727
728 if (api)
729 c->spawn_api = *api;
730 c->do_autospawn = 1;
731 }
732
733 }
734
735 pa_context_set_state(c, PA_CONTEXT_CONNECTING);
736 r = try_next_connection(c);
737
738 finish:
739 pa_context_unref(c);
740
741 return r;
742 }
743
744 void pa_context_disconnect(pa_context *c) {
745 assert(c);
746 assert(c->ref >= 1);
747
748 pa_context_set_state(c, PA_CONTEXT_TERMINATED);
749 }
750
751 pa_context_state_t pa_context_get_state(pa_context *c) {
752 assert(c);
753 assert(c->ref >= 1);
754
755 return c->state;
756 }
757
758 int pa_context_errno(pa_context *c) {
759 assert(c);
760 assert(c->ref >= 1);
761
762 return c->error;
763 }
764
765 void pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb, void *userdata) {
766 assert(c);
767 assert(c->ref >= 1);
768
769 c->state_callback = cb;
770 c->state_userdata = userdata;
771 }
772
773 int pa_context_is_pending(pa_context *c) {
774 assert(c);
775 assert(c->ref >= 1);
776
777 PA_CHECK_VALIDITY(c,
778 c->state == PA_CONTEXT_CONNECTING ||
779 c->state == PA_CONTEXT_AUTHORIZING ||
780 c->state == PA_CONTEXT_SETTING_NAME ||
781 c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
782
783 return (c->pstream && pa_pstream_is_pending(c->pstream)) ||
784 (c->pdispatch && pa_pdispatch_is_pending(c->pdispatch)) ||
785 c->client;
786 }
787
788 static void set_dispatch_callbacks(pa_operation *o);
789
790 static void pdispatch_drain_callback(PA_GCC_UNUSED pa_pdispatch*pd, void *userdata) {
791 set_dispatch_callbacks(userdata);
792 }
793
794 static void pstream_drain_callback(PA_GCC_UNUSED pa_pstream *s, void *userdata) {
795 set_dispatch_callbacks(userdata);
796 }
797
798 static void set_dispatch_callbacks(pa_operation *o) {
799 int done = 1;
800
801 assert(o);
802 assert(o->ref >= 1);
803 assert(o->context);
804 assert(o->context->ref >= 1);
805 assert(o->context->state == PA_CONTEXT_READY);
806
807 pa_pstream_set_drain_callback(o->context->pstream, NULL, NULL);
808 pa_pdispatch_set_drain_callback(o->context->pdispatch, NULL, NULL);
809
810 if (pa_pdispatch_is_pending(o->context->pdispatch)) {
811 pa_pdispatch_set_drain_callback(o->context->pdispatch, pdispatch_drain_callback, o);
812 done = 0;
813 }
814
815 if (pa_pstream_is_pending(o->context->pstream)) {
816 pa_pstream_set_drain_callback(o->context->pstream, pstream_drain_callback, o);
817 done = 0;
818 }
819
820 if (done) {
821 if (o->callback) {
822 pa_context_notify_cb_t cb = (pa_context_notify_cb_t) o->callback;
823 cb(o->context, o->userdata);
824 }
825
826 pa_operation_done(o);
827 pa_operation_unref(o);
828 }
829 }
830
831 pa_operation* pa_context_drain(pa_context *c, pa_context_notify_cb_t cb, void *userdata) {
832 pa_operation *o;
833
834 assert(c);
835 assert(c->ref >= 1);
836
837 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
838 PA_CHECK_VALIDITY_RETURN_NULL(c, pa_context_is_pending(c), PA_ERR_BADSTATE);
839
840 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
841 set_dispatch_callbacks(pa_operation_ref(o));
842
843 return o;
844 }
845
846 void pa_context_simple_ack_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
847 pa_operation *o = userdata;
848 int success = 1;
849
850 assert(pd);
851 assert(o);
852 assert(o->ref >= 1);
853
854 if (!o->context)
855 goto finish;
856
857 if (command != PA_COMMAND_REPLY) {
858 if (pa_context_handle_error(o->context, command, t) < 0)
859 goto finish;
860
861 success = 0;
862 } else if (!pa_tagstruct_eof(t)) {
863 pa_context_fail(o->context, PA_ERR_PROTOCOL);
864 goto finish;
865 }
866
867 if (o->callback) {
868 pa_context_success_cb_t cb = (pa_context_success_cb_t) o->callback;
869 cb(o->context, success, o->userdata);
870 }
871
872 finish:
873 pa_operation_done(o);
874 pa_operation_unref(o);
875 }
876
877 pa_operation* pa_context_exit_daemon(pa_context *c, pa_context_success_cb_t cb, void *userdata) {
878 pa_tagstruct *t;
879 pa_operation *o;
880 uint32_t tag;
881
882 assert(c);
883 assert(c->ref >= 1);
884
885 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
886
887 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
888
889 t = pa_tagstruct_command(c, PA_COMMAND_EXIT, &tag);
890 pa_pstream_send_tagstruct(c->pstream, t);
891 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, pa_context_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
892
893 return o;
894 }
895
896 pa_operation* pa_context_send_simple_command(pa_context *c, uint32_t command, pa_pdispatch_cb_t internal_cb, pa_operation_cb_t cb, void *userdata) {
897 pa_tagstruct *t;
898 pa_operation *o;
899 uint32_t tag;
900
901 assert(c);
902 assert(c->ref >= 1);
903
904 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
905
906 o = pa_operation_new(c, NULL, cb, userdata);
907
908 t = pa_tagstruct_command(c, command, &tag);
909 pa_pstream_send_tagstruct(c->pstream, t);
910 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, internal_cb, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
911
912 return o;
913 }
914
915 pa_operation* pa_context_set_default_sink(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
916 pa_tagstruct *t;
917 pa_operation *o;
918 uint32_t tag;
919
920 assert(c);
921 assert(c->ref >= 1);
922
923 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
924
925 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
926
927 t = pa_tagstruct_command(c, PA_COMMAND_SET_DEFAULT_SINK, &tag);
928 pa_tagstruct_puts(t, name);
929 pa_pstream_send_tagstruct(c->pstream, t);
930 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, pa_context_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
931
932 return o;
933 }
934
935 pa_operation* pa_context_set_default_source(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
936 pa_tagstruct *t;
937 pa_operation *o;
938 uint32_t tag;
939
940 assert(c);
941 assert(c->ref >= 1);
942
943 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
944
945 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
946
947 t = pa_tagstruct_command(c, PA_COMMAND_SET_DEFAULT_SOURCE, &tag);
948 pa_tagstruct_puts(t, name);
949 pa_pstream_send_tagstruct(c->pstream, t);
950 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, pa_context_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
951
952 return o;
953 }
954
955 int pa_context_is_local(pa_context *c) {
956 assert(c);
957
958 return c->is_local;
959 }
960
961 pa_operation* pa_context_set_name(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
962 pa_tagstruct *t;
963 pa_operation *o;
964 uint32_t tag;
965
966 assert(c);
967 assert(c->ref >= 1);
968 assert(name);
969
970 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
971
972 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
973
974 t = pa_tagstruct_command(c, PA_COMMAND_SET_CLIENT_NAME, &tag);
975 pa_tagstruct_puts(t, name);
976 pa_pstream_send_tagstruct(c->pstream, t);
977 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, pa_context_simple_ack_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
978
979 return o;
980 }
981
982 const char* pa_get_library_version(void) {
983 return PACKAGE_VERSION;
984 }
985
986 const char* pa_context_get_server(pa_context *c) {
987 assert(c);
988 assert(c->ref >= 1);
989
990 if (!c->server)
991 return NULL;
992
993 if (*c->server == '{') {
994 char *e = strchr(c->server+1, '}');
995 return e ? e+1 : c->server;
996 }
997
998 return c->server;
999 }
1000
1001 uint32_t pa_context_get_protocol_version(PA_GCC_UNUSED pa_context *c) {
1002 return PA_PROTOCOL_VERSION;
1003 }
1004
1005 uint32_t pa_context_get_server_protocol_version(pa_context *c) {
1006 assert(c);
1007 assert(c->ref >= 1);
1008
1009 return c->version;
1010 }
1011
1012 pa_tagstruct *pa_tagstruct_command(pa_context *c, uint32_t command, uint32_t *tag) {
1013 pa_tagstruct *t;
1014
1015 assert(c);
1016 assert(tag);
1017
1018 t = pa_tagstruct_new(NULL, 0);
1019 pa_tagstruct_putu32(t, command);
1020 pa_tagstruct_putu32(t, *tag = c->ctag++);
1021
1022 return t;
1023 }