]> code.delx.au - pulseaudio/blob - src/pulse/context.c
Merge commit 'origin/master-tx'
[pulseaudio] / src / pulse / context.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2008 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.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 #include <locale.h>
37
38 #ifdef HAVE_SYS_WAIT_H
39 #include <sys/wait.h>
40 #endif
41
42 #ifdef HAVE_SYS_SOCKET_H
43 #include <sys/socket.h>
44 #endif
45 #ifdef HAVE_SYS_UN_H
46 #include <sys/un.h>
47 #endif
48 #ifdef HAVE_NETDB_H
49 #include <netdb.h>
50 #endif
51
52 #include <pulse/version.h>
53 #include <pulse/xmalloc.h>
54 #include <pulse/utf8.h>
55 #include <pulse/util.h>
56 #include <pulse/i18n.h>
57
58 #include <pulsecore/winsock.h>
59 #include <pulsecore/core-error.h>
60
61 #include <pulsecore/native-common.h>
62 #include <pulsecore/pdispatch.h>
63 #include <pulsecore/pstream.h>
64 #include <pulsecore/dynarray.h>
65 #include <pulsecore/socket-client.h>
66 #include <pulsecore/pstream-util.h>
67 #include <pulsecore/core-util.h>
68 #include <pulsecore/log.h>
69 #include <pulsecore/socket-util.h>
70 #include <pulsecore/creds.h>
71 #include <pulsecore/macro.h>
72 #include <pulsecore/proplist-util.h>
73
74 #include "internal.h"
75
76 #include "client-conf.h"
77
78 #ifdef HAVE_X11
79 #include "client-conf-x11.h"
80 #endif
81
82 #include "context.h"
83
84 void pa_command_extension(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
85
86 static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
87 [PA_COMMAND_REQUEST] = pa_command_request,
88 [PA_COMMAND_OVERFLOW] = pa_command_overflow_or_underflow,
89 [PA_COMMAND_UNDERFLOW] = pa_command_overflow_or_underflow,
90 [PA_COMMAND_PLAYBACK_STREAM_KILLED] = pa_command_stream_killed,
91 [PA_COMMAND_RECORD_STREAM_KILLED] = pa_command_stream_killed,
92 [PA_COMMAND_PLAYBACK_STREAM_MOVED] = pa_command_stream_moved,
93 [PA_COMMAND_RECORD_STREAM_MOVED] = pa_command_stream_moved,
94 [PA_COMMAND_PLAYBACK_STREAM_SUSPENDED] = pa_command_stream_suspended,
95 [PA_COMMAND_RECORD_STREAM_SUSPENDED] = pa_command_stream_suspended,
96 [PA_COMMAND_STARTED] = pa_command_stream_started,
97 [PA_COMMAND_SUBSCRIBE_EVENT] = pa_command_subscribe_event,
98 [PA_COMMAND_EXTENSION] = pa_command_extension
99 };
100 static void context_free(pa_context *c);
101
102 pa_context *pa_context_new(pa_mainloop_api *mainloop, const char *name) {
103 return pa_context_new_with_proplist(mainloop, name, NULL);
104 }
105
106 static void reset_callbacks(pa_context *c) {
107 pa_assert(c);
108
109 c->state_callback = NULL;
110 c->state_userdata = NULL;
111
112 c->subscribe_callback = NULL;
113 c->subscribe_userdata = NULL;
114
115 c->ext_stream_restore.callback = NULL;
116 c->ext_stream_restore.userdata = NULL;
117 }
118
119 pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char *name, pa_proplist *p) {
120 pa_context *c;
121
122 pa_assert(mainloop);
123
124 pa_init_i18n();
125
126 if (!name && !pa_proplist_contains(p, PA_PROP_APPLICATION_NAME))
127 return NULL;
128
129 c = pa_xnew(pa_context, 1);
130 PA_REFCNT_INIT(c);
131
132 c->proplist = p ? pa_proplist_copy(p) : pa_proplist_new();
133
134 if (name)
135 pa_proplist_sets(c->proplist, PA_PROP_APPLICATION_NAME, name);
136
137 c->mainloop = mainloop;
138 c->client = NULL;
139 c->pstream = NULL;
140 c->pdispatch = NULL;
141 c->playback_streams = pa_dynarray_new();
142 c->record_streams = pa_dynarray_new();
143 c->client_index = PA_INVALID_INDEX;
144
145 PA_LLIST_HEAD_INIT(pa_stream, c->streams);
146 PA_LLIST_HEAD_INIT(pa_operation, c->operations);
147
148 c->error = PA_OK;
149 c->state = PA_CONTEXT_UNCONNECTED;
150 c->ctag = 0;
151 c->csyncid = 0;
152
153 reset_callbacks(c);
154
155 c->is_local = FALSE;
156 c->server_list = NULL;
157 c->server = NULL;
158
159 c->do_shm = FALSE;
160
161 c->do_autospawn = FALSE;
162 memset(&c->spawn_api, 0, sizeof(c->spawn_api));
163
164 #ifndef MSG_NOSIGNAL
165 #ifdef SIGPIPE
166 pa_check_signal_is_blocked(SIGPIPE);
167 #endif
168 #endif
169
170 c->conf = pa_client_conf_new();
171 #ifdef HAVE_X11
172 pa_client_conf_from_x11(c->conf, NULL);
173 #endif
174 pa_client_conf_load(c->conf, NULL);
175 pa_client_conf_env(c->conf);
176
177 if (!(c->mempool = pa_mempool_new(!c->conf->disable_shm, c->conf->shm_size))) {
178
179 if (!c->conf->disable_shm)
180 c->mempool = pa_mempool_new(FALSE, c->conf->shm_size);
181
182 if (!c->mempool) {
183 context_free(c);
184 return NULL;
185 }
186 }
187
188 return c;
189 }
190
191 static void context_unlink(pa_context *c) {
192 pa_stream *s;
193
194 pa_assert(c);
195
196 s = c->streams ? pa_stream_ref(c->streams) : NULL;
197 while (s) {
198 pa_stream *n = s->next ? pa_stream_ref(s->next) : NULL;
199 pa_stream_set_state(s, c->state == PA_CONTEXT_FAILED ? PA_STREAM_FAILED : PA_STREAM_TERMINATED);
200 pa_stream_unref(s);
201 s = n;
202 }
203
204 while (c->operations)
205 pa_operation_cancel(c->operations);
206
207 if (c->pdispatch) {
208 pa_pdispatch_unref(c->pdispatch);
209 c->pdispatch = NULL;
210 }
211
212 if (c->pstream) {
213 pa_pstream_unlink(c->pstream);
214 pa_pstream_unref(c->pstream);
215 c->pstream = NULL;
216 }
217
218 if (c->client) {
219 pa_socket_client_unref(c->client);
220 c->client = NULL;
221 }
222
223 reset_callbacks(c);
224 }
225
226 static void context_free(pa_context *c) {
227 pa_assert(c);
228
229 context_unlink(c);
230
231 if (c->record_streams)
232 pa_dynarray_free(c->record_streams, NULL, NULL);
233 if (c->playback_streams)
234 pa_dynarray_free(c->playback_streams, NULL, NULL);
235
236 if (c->mempool)
237 pa_mempool_free(c->mempool);
238
239 if (c->conf)
240 pa_client_conf_free(c->conf);
241
242 pa_strlist_free(c->server_list);
243
244 if (c->proplist)
245 pa_proplist_free(c->proplist);
246
247 pa_xfree(c->server);
248 pa_xfree(c);
249 }
250
251 pa_context* pa_context_ref(pa_context *c) {
252 pa_assert(c);
253 pa_assert(PA_REFCNT_VALUE(c) >= 1);
254
255 PA_REFCNT_INC(c);
256 return c;
257 }
258
259 void pa_context_unref(pa_context *c) {
260 pa_assert(c);
261 pa_assert(PA_REFCNT_VALUE(c) >= 1);
262
263 if (PA_REFCNT_DEC(c) <= 0)
264 context_free(c);
265 }
266
267 void pa_context_set_state(pa_context *c, pa_context_state_t st) {
268 pa_assert(c);
269 pa_assert(PA_REFCNT_VALUE(c) >= 1);
270
271 if (c->state == st)
272 return;
273
274 pa_context_ref(c);
275
276 c->state = st;
277
278 if (c->state_callback)
279 c->state_callback(c, c->state_userdata);
280
281 if (st == PA_CONTEXT_FAILED || st == PA_CONTEXT_TERMINATED)
282 context_unlink(c);
283
284 pa_context_unref(c);
285 }
286
287 int pa_context_set_error(pa_context *c, int error) {
288 pa_assert(error >= 0);
289 pa_assert(error < PA_ERR_MAX);
290
291 if (c)
292 c->error = error;
293
294 return error;
295 }
296
297 void pa_context_fail(pa_context *c, int error) {
298 pa_assert(c);
299 pa_assert(PA_REFCNT_VALUE(c) >= 1);
300
301 pa_context_set_error(c, error);
302 pa_context_set_state(c, PA_CONTEXT_FAILED);
303 }
304
305 static void pstream_die_callback(pa_pstream *p, void *userdata) {
306 pa_context *c = userdata;
307
308 pa_assert(p);
309 pa_assert(c);
310
311 pa_context_fail(c, PA_ERR_CONNECTIONTERMINATED);
312 }
313
314 static void pstream_packet_callback(pa_pstream *p, pa_packet *packet, const pa_creds *creds, void *userdata) {
315 pa_context *c = userdata;
316
317 pa_assert(p);
318 pa_assert(packet);
319 pa_assert(c);
320
321 pa_context_ref(c);
322
323 if (pa_pdispatch_run(c->pdispatch, packet, creds, c) < 0)
324 pa_context_fail(c, PA_ERR_PROTOCOL);
325
326 pa_context_unref(c);
327 }
328
329 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) {
330 pa_context *c = userdata;
331 pa_stream *s;
332
333 pa_assert(p);
334 pa_assert(chunk);
335 pa_assert(chunk->memblock);
336 pa_assert(chunk->length);
337 pa_assert(c);
338 pa_assert(PA_REFCNT_VALUE(c) >= 1);
339
340 pa_context_ref(c);
341
342 if ((s = pa_dynarray_get(c->record_streams, channel))) {
343
344 pa_assert(seek == PA_SEEK_RELATIVE);
345 pa_assert(offset == 0);
346
347 pa_memblockq_seek(s->record_memblockq, offset, seek);
348 pa_memblockq_push_align(s->record_memblockq, chunk);
349
350 if (s->read_callback) {
351 size_t l;
352
353 if ((l = pa_memblockq_get_length(s->record_memblockq)) > 0)
354 s->read_callback(s, l, s->read_userdata);
355 }
356 }
357
358 pa_context_unref(c);
359 }
360
361 int pa_context_handle_error(pa_context *c, uint32_t command, pa_tagstruct *t, pa_bool_t fail) {
362 uint32_t err;
363 pa_assert(c);
364 pa_assert(PA_REFCNT_VALUE(c) >= 1);
365
366 if (command == PA_COMMAND_ERROR) {
367 pa_assert(t);
368
369 if (pa_tagstruct_getu32(t, &err) < 0 ||
370 !pa_tagstruct_eof(t)) {
371 pa_context_fail(c, PA_ERR_PROTOCOL);
372 return -1;
373 }
374
375 } else if (command == PA_COMMAND_TIMEOUT)
376 err = PA_ERR_TIMEOUT;
377 else {
378 pa_context_fail(c, PA_ERR_PROTOCOL);
379 return -1;
380 }
381
382 if (err == PA_OK) {
383 pa_context_fail(c, PA_ERR_PROTOCOL);
384 return -1;
385 }
386
387 if (err >= PA_ERR_MAX)
388 err = PA_ERR_UNKNOWN;
389
390 if (fail) {
391 pa_context_fail(c, (int) err);
392 return -1;
393 }
394
395 pa_context_set_error(c, (int) err);
396
397 return 0;
398 }
399
400 static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
401 pa_context *c = userdata;
402
403 pa_assert(pd);
404 pa_assert(c);
405 pa_assert(c->state == PA_CONTEXT_AUTHORIZING || c->state == PA_CONTEXT_SETTING_NAME);
406
407 pa_context_ref(c);
408
409 if (command != PA_COMMAND_REPLY) {
410 pa_context_handle_error(c, command, t, TRUE);
411 goto finish;
412 }
413
414 switch(c->state) {
415 case PA_CONTEXT_AUTHORIZING: {
416 pa_tagstruct *reply;
417 pa_bool_t shm_on_remote = FALSE;
418
419 if (pa_tagstruct_getu32(t, &c->version) < 0 ||
420 !pa_tagstruct_eof(t)) {
421 pa_context_fail(c, PA_ERR_PROTOCOL);
422 goto finish;
423 }
424
425 /* Minimum supported version */
426 if (c->version < 8) {
427 pa_context_fail(c, PA_ERR_VERSION);
428 goto finish;
429 }
430
431 /* Starting with protocol version 13 the MSB of the version
432 tag reflects if shm is available for this connection or
433 not. */
434 if (c->version >= 13) {
435 shm_on_remote = !!(c->version & 0x80000000U);
436 c->version &= 0x7FFFFFFFU;
437 }
438
439 pa_log_debug("Protocol version: remote %u, local %u", c->version, PA_PROTOCOL_VERSION);
440
441 /* Enable shared memory support if possible */
442 if (c->do_shm)
443 if (c->version < 10 || (c->version >= 13 && !shm_on_remote))
444 c->do_shm = FALSE;
445
446 if (c->do_shm) {
447
448 /* Only enable SHM if both sides are owned by the same
449 * user. This is a security measure because otherwise
450 * data private to the user might leak. */
451
452 #ifdef HAVE_CREDS
453 const pa_creds *creds;
454 if (!(creds = pa_pdispatch_creds(pd)) || getuid() != creds->uid)
455 c->do_shm = FALSE;
456 #endif
457 }
458
459 pa_log_debug("Negotiated SHM: %s", pa_yes_no(c->do_shm));
460 pa_pstream_enable_shm(c->pstream, c->do_shm);
461
462 reply = pa_tagstruct_command(c, PA_COMMAND_SET_CLIENT_NAME, &tag);
463
464 if (c->version >= 13) {
465 pa_init_proplist(c->proplist);
466 pa_tagstruct_put_proplist(reply, c->proplist);
467 } else
468 pa_tagstruct_puts(reply, pa_proplist_gets(c->proplist, PA_PROP_APPLICATION_NAME));
469
470 pa_pstream_send_tagstruct(c->pstream, reply);
471 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c, NULL);
472
473 pa_context_set_state(c, PA_CONTEXT_SETTING_NAME);
474 break;
475 }
476
477 case PA_CONTEXT_SETTING_NAME :
478
479 if ((c->version >= 13 && (pa_tagstruct_getu32(t, &c->client_index) < 0 ||
480 c->client_index == PA_INVALID_INDEX)) ||
481 !pa_tagstruct_eof(t)) {
482 pa_context_fail(c, PA_ERR_PROTOCOL);
483 goto finish;
484 }
485
486 pa_context_set_state(c, PA_CONTEXT_READY);
487 break;
488
489 default:
490 pa_assert_not_reached();
491 }
492
493 finish:
494 pa_context_unref(c);
495 }
496
497 static void setup_context(pa_context *c, pa_iochannel *io) {
498 pa_tagstruct *t;
499 uint32_t tag;
500
501 pa_assert(c);
502 pa_assert(io);
503
504 pa_context_ref(c);
505
506 pa_assert(!c->pstream);
507 c->pstream = pa_pstream_new(c->mainloop, io, c->mempool);
508
509 pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
510 pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
511 pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
512
513 pa_assert(!c->pdispatch);
514 c->pdispatch = pa_pdispatch_new(c->mainloop, command_table, PA_COMMAND_MAX);
515
516 if (!c->conf->cookie_valid)
517 pa_log_info(_("No cookie loaded. Attempting to connect without."));
518
519 t = pa_tagstruct_command(c, PA_COMMAND_AUTH, &tag);
520
521 c->do_shm =
522 pa_mempool_is_shared(c->mempool) &&
523 c->is_local;
524
525 pa_log_debug("SHM possible: %s", pa_yes_no(c->do_shm));
526
527 /* Starting with protocol version 13 we use the MSB of the version
528 * tag for informing the other side if we could do SHM or not */
529 pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION | (c->do_shm ? 0x80000000U : 0));
530 pa_tagstruct_put_arbitrary(t, c->conf->cookie, sizeof(c->conf->cookie));
531
532 #ifdef HAVE_CREDS
533 {
534 pa_creds ucred;
535
536 if (pa_iochannel_creds_supported(io))
537 pa_iochannel_creds_enable(io);
538
539 ucred.uid = getuid();
540 ucred.gid = getgid();
541
542 pa_pstream_send_tagstruct_with_creds(c->pstream, t, &ucred);
543 }
544 #else
545 pa_pstream_send_tagstruct(c->pstream, t);
546 #endif
547
548 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, setup_complete_callback, c, NULL);
549
550 pa_context_set_state(c, PA_CONTEXT_AUTHORIZING);
551
552 pa_context_unref(c);
553 }
554
555 static char *get_old_legacy_runtime_dir(void) {
556 char *p, u[128];
557 struct stat st;
558
559 if (!pa_get_user_name(u, sizeof(u)))
560 return NULL;
561
562 p = pa_sprintf_malloc("/tmp/pulse-%s", u);
563
564 if (stat(p, &st) < 0) {
565 pa_xfree(p);
566 return NULL;
567 }
568
569 if (st.st_uid != getuid()) {
570 pa_xfree(p);
571 return NULL;
572 }
573
574 return p;
575 }
576
577 static char *get_very_old_legacy_runtime_dir(void) {
578 char *p, h[128];
579 struct stat st;
580
581 if (!pa_get_home_dir(h, sizeof(h)))
582 return NULL;
583
584 p = pa_sprintf_malloc("%s/.pulse", h);
585
586 if (stat(p, &st) < 0) {
587 pa_xfree(p);
588 return NULL;
589 }
590
591 if (st.st_uid != getuid()) {
592 pa_xfree(p);
593 return NULL;
594 }
595
596 return p;
597 }
598
599
600 static pa_strlist *prepend_per_user(pa_strlist *l) {
601 char *ufn;
602 static char *legacy_dir;
603
604 /* The very old per-user instance path (< 0.9.11). This is supported only to ease upgrades */
605 if ((legacy_dir = get_very_old_legacy_runtime_dir())) {
606 char *p = pa_sprintf_malloc("%s" PA_PATH_SEP PA_NATIVE_DEFAULT_UNIX_SOCKET, legacy_dir);
607 l = pa_strlist_prepend(l, p);
608 pa_xfree(p);
609 pa_xfree(legacy_dir);
610 }
611
612 /* The old per-user instance path (< 0.9.12). This is supported only to ease upgrades */
613 if ((legacy_dir = get_old_legacy_runtime_dir())) {
614 char *p = pa_sprintf_malloc("%s" PA_PATH_SEP PA_NATIVE_DEFAULT_UNIX_SOCKET, legacy_dir);
615 l = pa_strlist_prepend(l, p);
616 pa_xfree(p);
617 pa_xfree(legacy_dir);
618 }
619
620 /* The per-user instance */
621 if ((ufn = pa_runtime_path(PA_NATIVE_DEFAULT_UNIX_SOCKET))) {
622 l = pa_strlist_prepend(l, ufn);
623 pa_xfree(ufn);
624 }
625
626 return l;
627 }
628
629 #ifndef OS_IS_WIN32
630
631 static int context_autospawn(pa_context *c) {
632 pid_t pid;
633 int status, r;
634
635 pa_log_debug("Trying to autospawn...");
636
637 pa_context_ref(c);
638
639 if (c->spawn_api.prefork)
640 c->spawn_api.prefork();
641
642 if ((pid = fork()) < 0) {
643 pa_log_error(_("fork(): %s"), pa_cstrerror(errno));
644 pa_context_fail(c, PA_ERR_INTERNAL);
645
646 if (c->spawn_api.postfork)
647 c->spawn_api.postfork();
648
649 goto fail;
650 } else if (!pid) {
651 /* Child */
652
653 const char *state = NULL;
654 #define MAX_ARGS 64
655 const char * argv[MAX_ARGS+1];
656 int n;
657
658 if (c->spawn_api.atfork)
659 c->spawn_api.atfork();
660
661 pa_close_all(-1);
662
663 /* Setup argv */
664
665 n = 0;
666
667 argv[n++] = c->conf->daemon_binary;
668 argv[n++] = "--start";
669
670 while (n < MAX_ARGS) {
671 char *a;
672
673 if (!(a = pa_split_spaces(c->conf->extra_arguments, &state)))
674 break;
675
676 argv[n++] = a;
677 }
678
679 argv[n++] = NULL;
680
681 execv(argv[0], (char * const *) argv);
682 _exit(1);
683 #undef MAX_ARGS
684 }
685
686 /* Parent */
687
688 if (c->spawn_api.postfork)
689 c->spawn_api.postfork();
690
691 do {
692 r = waitpid(pid, &status, 0);
693 } while (r < 0 && errno == EINTR);
694
695 if (r < 0) {
696 pa_log(_("waitpid(): %s"), pa_cstrerror(errno));
697 pa_context_fail(c, PA_ERR_INTERNAL);
698 goto fail;
699 } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
700 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
701 goto fail;
702 }
703
704 pa_context_unref(c);
705
706 return 0;
707
708 fail:
709
710 pa_context_unref(c);
711
712 return -1;
713 }
714
715 #endif /* OS_IS_WIN32 */
716
717 static void on_connection(pa_socket_client *client, pa_iochannel*io, void *userdata);
718
719 static int try_next_connection(pa_context *c) {
720 char *u = NULL;
721 int r = -1;
722
723 pa_assert(c);
724 pa_assert(!c->client);
725
726 for (;;) {
727 pa_xfree(u);
728 u = NULL;
729
730 c->server_list = pa_strlist_pop(c->server_list, &u);
731
732 if (!u) {
733
734 #ifndef OS_IS_WIN32
735 if (c->do_autospawn) {
736
737 if ((r = context_autospawn(c)) < 0)
738 goto finish;
739
740 /* Autospawn only once */
741 c->do_autospawn = FALSE;
742
743 /* Connect only to per-user sockets this time */
744 c->server_list = prepend_per_user(c->server_list);
745
746 /* Retry connection */
747 continue;
748 }
749 #endif
750
751 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
752 goto finish;
753 }
754
755 pa_log_debug("Trying to connect to %s...", u);
756
757 pa_xfree(c->server);
758 c->server = pa_xstrdup(u);
759
760 if (!(c->client = pa_socket_client_new_string(c->mainloop, u, PA_NATIVE_DEFAULT_PORT)))
761 continue;
762
763 c->is_local = !!pa_socket_client_is_local(c->client);
764 pa_socket_client_set_callback(c->client, on_connection, c);
765 break;
766 }
767
768 r = 0;
769
770 finish:
771 pa_xfree(u);
772
773 return r;
774 }
775
776 static void on_connection(pa_socket_client *client, pa_iochannel*io, void *userdata) {
777 pa_context *c = userdata;
778 int saved_errno = errno;
779
780 pa_assert(client);
781 pa_assert(c);
782 pa_assert(c->state == PA_CONTEXT_CONNECTING);
783
784 pa_context_ref(c);
785
786 pa_socket_client_unref(client);
787 c->client = NULL;
788
789 if (!io) {
790 /* Try the item in the list */
791 if (saved_errno == ECONNREFUSED ||
792 saved_errno == ETIMEDOUT ||
793 saved_errno == EHOSTUNREACH) {
794 try_next_connection(c);
795 goto finish;
796 }
797
798 pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
799 goto finish;
800 }
801
802 setup_context(c, io);
803
804 finish:
805 pa_context_unref(c);
806 }
807
808 int pa_context_connect(
809 pa_context *c,
810 const char *server,
811 pa_context_flags_t flags,
812 const pa_spawn_api *api) {
813
814 int r = -1;
815
816 pa_assert(c);
817 pa_assert(PA_REFCNT_VALUE(c) >= 1);
818
819 PA_CHECK_VALIDITY(c, c->state == PA_CONTEXT_UNCONNECTED, PA_ERR_BADSTATE);
820 PA_CHECK_VALIDITY(c, !(flags & ~PA_CONTEXT_NOAUTOSPAWN), PA_ERR_INVALID);
821 PA_CHECK_VALIDITY(c, !server || *server, PA_ERR_INVALID);
822
823 if (!server)
824 server = c->conf->default_server;
825
826 pa_context_ref(c);
827
828 pa_assert(!c->server_list);
829
830 if (server) {
831 if (!(c->server_list = pa_strlist_parse(server))) {
832 pa_context_fail(c, PA_ERR_INVALIDSERVER);
833 goto finish;
834 }
835
836 } else {
837 char *d;
838
839 /* Prepend in reverse order */
840
841 /* Follow the X display */
842 if ((d = getenv("DISPLAY"))) {
843 char *e;
844 d = pa_xstrdup(d);
845 if ((e = strchr(d, ':')))
846 *e = 0;
847
848 if (*d)
849 c->server_list = pa_strlist_prepend(c->server_list, d);
850
851 pa_xfree(d);
852 }
853
854 /* Add TCP/IP on the localhost */
855 c->server_list = pa_strlist_prepend(c->server_list, "tcp6:[::1]");
856 c->server_list = pa_strlist_prepend(c->server_list, "tcp4:127.0.0.1");
857
858 /* The system wide instance via PF_LOCAL */
859 c->server_list = pa_strlist_prepend(c->server_list, PA_SYSTEM_RUNTIME_PATH PA_PATH_SEP PA_NATIVE_DEFAULT_UNIX_SOCKET);
860
861 /* The user instance via PF_LOCAL */
862 c->server_list = prepend_per_user(c->server_list);
863
864 /* Set up autospawning */
865 if (!(flags & PA_CONTEXT_NOAUTOSPAWN) && c->conf->autospawn) {
866
867 if (getuid() == 0)
868 pa_log_debug("Not doing autospawn since we are root.");
869 else {
870 c->do_autospawn = TRUE;
871
872 if (api)
873 c->spawn_api = *api;
874 }
875 }
876 }
877
878 pa_context_set_state(c, PA_CONTEXT_CONNECTING);
879 r = try_next_connection(c);
880
881 finish:
882 pa_context_unref(c);
883
884 return r;
885 }
886
887 void pa_context_disconnect(pa_context *c) {
888 pa_assert(c);
889 pa_assert(PA_REFCNT_VALUE(c) >= 1);
890
891 if (PA_CONTEXT_IS_GOOD(c->state))
892 pa_context_set_state(c, PA_CONTEXT_TERMINATED);
893 }
894
895 pa_context_state_t pa_context_get_state(pa_context *c) {
896 pa_assert(c);
897 pa_assert(PA_REFCNT_VALUE(c) >= 1);
898
899 return c->state;
900 }
901
902 int pa_context_errno(pa_context *c) {
903 pa_assert(c);
904 pa_assert(PA_REFCNT_VALUE(c) >= 1);
905
906 return c->error;
907 }
908
909 void pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb, void *userdata) {
910 pa_assert(c);
911 pa_assert(PA_REFCNT_VALUE(c) >= 1);
912
913 if (c->state == PA_CONTEXT_TERMINATED || c->state == PA_CONTEXT_FAILED)
914 return;
915
916 c->state_callback = cb;
917 c->state_userdata = userdata;
918 }
919
920 int pa_context_is_pending(pa_context *c) {
921 pa_assert(c);
922 pa_assert(PA_REFCNT_VALUE(c) >= 1);
923
924 PA_CHECK_VALIDITY(c, PA_CONTEXT_IS_GOOD(c->state), PA_ERR_BADSTATE);
925
926 return (c->pstream && pa_pstream_is_pending(c->pstream)) ||
927 (c->pdispatch && pa_pdispatch_is_pending(c->pdispatch)) ||
928 c->client;
929 }
930
931 static void set_dispatch_callbacks(pa_operation *o);
932
933 static void pdispatch_drain_callback(pa_pdispatch*pd, void *userdata) {
934 set_dispatch_callbacks(userdata);
935 }
936
937 static void pstream_drain_callback(pa_pstream *s, void *userdata) {
938 set_dispatch_callbacks(userdata);
939 }
940
941 static void set_dispatch_callbacks(pa_operation *o) {
942 int done = 1;
943
944 pa_assert(o);
945 pa_assert(PA_REFCNT_VALUE(o) >= 1);
946 pa_assert(o->context);
947 pa_assert(PA_REFCNT_VALUE(o->context) >= 1);
948 pa_assert(o->context->state == PA_CONTEXT_READY);
949
950 pa_pstream_set_drain_callback(o->context->pstream, NULL, NULL);
951 pa_pdispatch_set_drain_callback(o->context->pdispatch, NULL, NULL);
952
953 if (pa_pdispatch_is_pending(o->context->pdispatch)) {
954 pa_pdispatch_set_drain_callback(o->context->pdispatch, pdispatch_drain_callback, o);
955 done = 0;
956 }
957
958 if (pa_pstream_is_pending(o->context->pstream)) {
959 pa_pstream_set_drain_callback(o->context->pstream, pstream_drain_callback, o);
960 done = 0;
961 }
962
963 if (done) {
964 if (o->callback) {
965 pa_context_notify_cb_t cb = (pa_context_notify_cb_t) o->callback;
966 cb(o->context, o->userdata);
967 }
968
969 pa_operation_done(o);
970 pa_operation_unref(o);
971 }
972 }
973
974 pa_operation* pa_context_drain(pa_context *c, pa_context_notify_cb_t cb, void *userdata) {
975 pa_operation *o;
976
977 pa_assert(c);
978 pa_assert(PA_REFCNT_VALUE(c) >= 1);
979
980 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
981 PA_CHECK_VALIDITY_RETURN_NULL(c, pa_context_is_pending(c), PA_ERR_BADSTATE);
982
983 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
984 set_dispatch_callbacks(pa_operation_ref(o));
985
986 return o;
987 }
988
989 void pa_context_simple_ack_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
990 pa_operation *o = userdata;
991 int success = 1;
992
993 pa_assert(pd);
994 pa_assert(o);
995 pa_assert(PA_REFCNT_VALUE(o) >= 1);
996
997 if (!o->context)
998 goto finish;
999
1000 if (command != PA_COMMAND_REPLY) {
1001 if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
1002 goto finish;
1003
1004 success = 0;
1005 } else if (!pa_tagstruct_eof(t)) {
1006 pa_context_fail(o->context, PA_ERR_PROTOCOL);
1007 goto finish;
1008 }
1009
1010 if (o->callback) {
1011 pa_context_success_cb_t cb = (pa_context_success_cb_t) o->callback;
1012 cb(o->context, success, o->userdata);
1013 }
1014
1015 finish:
1016 pa_operation_done(o);
1017 pa_operation_unref(o);
1018 }
1019
1020 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) {
1021 pa_tagstruct *t;
1022 pa_operation *o;
1023 uint32_t tag;
1024
1025 pa_assert(c);
1026 pa_assert(PA_REFCNT_VALUE(c) >= 1);
1027
1028 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
1029
1030 o = pa_operation_new(c, NULL, cb, userdata);
1031
1032 t = pa_tagstruct_command(c, command, &tag);
1033 pa_pstream_send_tagstruct(c->pstream, t);
1034 pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, internal_cb, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
1035
1036 return o;
1037 }
1038
1039 pa_operation* pa_context_exit_daemon(pa_context *c, pa_context_success_cb_t cb, void *userdata) {
1040 pa_assert(c);
1041 pa_assert(PA_REFCNT_VALUE(c) >= 1);
1042
1043 return pa_context_send_simple_command(c, PA_COMMAND_EXIT, pa_context_simple_ack_callback, (pa_operation_cb_t) cb, userdata);
1044 }
1045
1046 pa_operation* pa_context_set_default_sink(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
1047 pa_tagstruct *t;
1048 pa_operation *o;
1049 uint32_t tag;
1050
1051 pa_assert(c);
1052 pa_assert(PA_REFCNT_VALUE(c) >= 1);
1053
1054 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
1055
1056 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
1057 t = pa_tagstruct_command(c, PA_COMMAND_SET_DEFAULT_SINK, &tag);
1058 pa_tagstruct_puts(t, name);
1059 pa_pstream_send_tagstruct(c->pstream, t);
1060 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);
1061
1062 return o;
1063 }
1064
1065 pa_operation* pa_context_set_default_source(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
1066 pa_tagstruct *t;
1067 pa_operation *o;
1068 uint32_t tag;
1069
1070 pa_assert(c);
1071 pa_assert(PA_REFCNT_VALUE(c) >= 1);
1072
1073 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
1074
1075 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
1076 t = pa_tagstruct_command(c, PA_COMMAND_SET_DEFAULT_SOURCE, &tag);
1077 pa_tagstruct_puts(t, name);
1078 pa_pstream_send_tagstruct(c->pstream, t);
1079 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);
1080
1081 return o;
1082 }
1083
1084 int pa_context_is_local(pa_context *c) {
1085 pa_assert(c);
1086 pa_assert(PA_REFCNT_VALUE(c) >= 1);
1087
1088 PA_CHECK_VALIDITY_RETURN_ANY(c, PA_CONTEXT_IS_GOOD(c->state), PA_ERR_BADSTATE, -1);
1089
1090 return !!c->is_local;
1091 }
1092
1093 pa_operation* pa_context_set_name(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata) {
1094 pa_operation *o;
1095
1096 pa_assert(c);
1097 pa_assert(PA_REFCNT_VALUE(c) >= 1);
1098 pa_assert(name);
1099
1100 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
1101
1102 if (c->version >= 13) {
1103 pa_proplist *p = pa_proplist_new();
1104
1105 pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, name);
1106 o = pa_context_proplist_update(c, PA_UPDATE_REPLACE, p, cb, userdata);
1107 pa_proplist_free(p);
1108 } else {
1109 pa_tagstruct *t;
1110 uint32_t tag;
1111
1112 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
1113 t = pa_tagstruct_command(c, PA_COMMAND_SET_CLIENT_NAME, &tag);
1114 pa_tagstruct_puts(t, name);
1115 pa_pstream_send_tagstruct(c->pstream, t);
1116 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);
1117 }
1118
1119 return o;
1120 }
1121
1122 const char* pa_get_library_version(void) {
1123 return PACKAGE_VERSION;
1124 }
1125
1126 const char* pa_context_get_server(pa_context *c) {
1127 pa_assert(c);
1128 pa_assert(PA_REFCNT_VALUE(c) >= 1);
1129
1130 if (!c->server)
1131 return NULL;
1132
1133 if (*c->server == '{') {
1134 char *e = strchr(c->server+1, '}');
1135 return e ? e+1 : c->server;
1136 }
1137
1138 return c->server;
1139 }
1140
1141 uint32_t pa_context_get_protocol_version(pa_context *c) {
1142 return PA_PROTOCOL_VERSION;
1143 }
1144
1145 uint32_t pa_context_get_server_protocol_version(pa_context *c) {
1146 pa_assert(c);
1147 pa_assert(PA_REFCNT_VALUE(c) >= 1);
1148
1149 PA_CHECK_VALIDITY_RETURN_ANY(c, PA_CONTEXT_IS_GOOD(c->state), PA_ERR_BADSTATE, PA_INVALID_INDEX);
1150
1151 return c->version;
1152 }
1153
1154 pa_tagstruct *pa_tagstruct_command(pa_context *c, uint32_t command, uint32_t *tag) {
1155 pa_tagstruct *t;
1156
1157 pa_assert(c);
1158 pa_assert(tag);
1159
1160 t = pa_tagstruct_new(NULL, 0);
1161 pa_tagstruct_putu32(t, command);
1162 pa_tagstruct_putu32(t, *tag = c->ctag++);
1163
1164 return t;
1165 }
1166
1167 uint32_t pa_context_get_index(pa_context *c) {
1168 pa_assert(c);
1169 pa_assert(PA_REFCNT_VALUE(c) >= 1);
1170
1171 PA_CHECK_VALIDITY_RETURN_ANY(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE, PA_INVALID_INDEX);
1172 PA_CHECK_VALIDITY_RETURN_ANY(c, c->version >= 13, PA_ERR_NOTSUPPORTED, PA_INVALID_INDEX);
1173
1174 return c->client_index;
1175 }
1176
1177 pa_operation *pa_context_proplist_update(pa_context *c, pa_update_mode_t mode, pa_proplist *p, pa_context_success_cb_t cb, void *userdata) {
1178 pa_operation *o;
1179 pa_tagstruct *t;
1180 uint32_t tag;
1181
1182 pa_assert(c);
1183 pa_assert(PA_REFCNT_VALUE(c) >= 1);
1184
1185 PA_CHECK_VALIDITY_RETURN_NULL(c, mode == PA_UPDATE_SET || mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE, PA_ERR_INVALID);
1186 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
1187 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 13, PA_ERR_NOTSUPPORTED);
1188
1189 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
1190
1191 t = pa_tagstruct_command(c, PA_COMMAND_UPDATE_CLIENT_PROPLIST, &tag);
1192 pa_tagstruct_putu32(t, (uint32_t) mode);
1193 pa_tagstruct_put_proplist(t, p);
1194
1195 pa_pstream_send_tagstruct(c->pstream, t);
1196 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);
1197
1198 /* Please note that we don't update c->proplist here, because we
1199 * don't export that field */
1200
1201 return o;
1202 }
1203
1204 pa_operation *pa_context_proplist_remove(pa_context *c, const char *const keys[], pa_context_success_cb_t cb, void *userdata) {
1205 pa_operation *o;
1206 pa_tagstruct *t;
1207 uint32_t tag;
1208 const char * const *k;
1209
1210 pa_assert(c);
1211 pa_assert(PA_REFCNT_VALUE(c) >= 1);
1212
1213 PA_CHECK_VALIDITY_RETURN_NULL(c, keys && keys[0], PA_ERR_INVALID);
1214 PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
1215 PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 13, PA_ERR_NOTSUPPORTED);
1216
1217 o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
1218
1219 t = pa_tagstruct_command(c, PA_COMMAND_REMOVE_CLIENT_PROPLIST, &tag);
1220
1221 for (k = keys; *k; k++)
1222 pa_tagstruct_puts(t, *k);
1223
1224 pa_tagstruct_puts(t, NULL);
1225
1226 pa_pstream_send_tagstruct(c->pstream, t);
1227 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);
1228
1229 /* Please note that we don't update c->proplist here, because we
1230 * don't export that field */
1231
1232 return o;
1233 }
1234
1235 void pa_command_extension(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
1236 pa_context *c = userdata;
1237 uint32_t idx;
1238 const char *name;
1239
1240 pa_assert(pd);
1241 pa_assert(command == PA_COMMAND_EXTENSION);
1242 pa_assert(t);
1243 pa_assert(c);
1244 pa_assert(PA_REFCNT_VALUE(c) >= 1);
1245
1246 pa_context_ref(c);
1247
1248 if (pa_tagstruct_getu32(t, &idx) < 0 ||
1249 pa_tagstruct_gets(t, &name) < 0) {
1250 pa_context_fail(c, PA_ERR_PROTOCOL);
1251 goto finish;
1252 }
1253
1254 if (!strcmp(name, "module-stream-restore"))
1255 pa_ext_stream_restore_command(c, tag, t);
1256 else
1257 pa_log(_("Received message for unknown extension '%s'"), name);
1258
1259 finish:
1260 pa_context_unref(c);
1261 }