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