]> code.delx.au - pulseaudio/blobdiff - src/pulse/context.c
core: Fix uninit pointer read in protocol-native
[pulseaudio] / src / pulse / context.c
index 0780e2f311f854c1d4db228feb9c82040e359f2c..b78df27c272eb6466ec8f7914c57abaf6ea80cef 100644 (file)
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /***
   This file is part of PulseAudio.
 
@@ -8,7 +6,7 @@
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
-  by the Free Software Foundation; either version 2 of the License,
+  by the Free Software Foundation; either version 2.1 of the License,
   or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
 #include <sys/stat.h>
 #include <errno.h>
 #include <signal.h>
-#include <limits.h>
-#include <locale.h>
 
 #ifdef HAVE_SYS_WAIT_H
 #include <sys/wait.h>
 #endif
 
-#ifdef HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
-#ifdef HAVE_SYS_UN_H
-#include <sys/un.h>
-#endif
 #ifdef HAVE_NETDB_H
 #include <netdb.h>
 #endif
 
 #include <pulse/version.h>
 #include <pulse/xmalloc.h>
-#include <pulse/utf8.h>
 #include <pulse/util.h>
+#include <pulse/mainloop.h>
+#include <pulse/timeval.h>
+#include <pulse/fork-detect.h>
+#include <pulse/client-conf.h>
+#ifdef HAVE_X11
+#include <pulse/client-conf-x11.h>
+#endif
 
-#include <pulsecore/winsock.h>
 #include <pulsecore/core-error.h>
-
+#include <pulsecore/i18n.h>
 #include <pulsecore/native-common.h>
 #include <pulsecore/pdispatch.h>
 #include <pulsecore/pstream.h>
-#include <pulsecore/dynarray.h>
+#include <pulsecore/hashmap.h>
 #include <pulsecore/socket-client.h>
 #include <pulsecore/pstream-util.h>
+#include <pulsecore/core-rtclock.h>
 #include <pulsecore/core-util.h>
 #include <pulsecore/log.h>
-#include <pulsecore/socket-util.h>
+#include <pulsecore/socket.h>
 #include <pulsecore/creds.h>
 #include <pulsecore/macro.h>
+#include <pulsecore/proplist-util.h>
 
 #include "internal.h"
-
-#include "client-conf.h"
-
-#ifdef HAVE_X11
-#include "client-conf-x11.h"
-#endif
-
 #include "context.h"
 
-#define AUTOSPAWN_LOCK "autospawn.lock"
+void pa_command_extension(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata);
 
 static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
     [PA_COMMAND_REQUEST] = pa_command_request,
@@ -94,25 +84,20 @@ static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = {
     [PA_COMMAND_PLAYBACK_STREAM_SUSPENDED] = pa_command_stream_suspended,
     [PA_COMMAND_RECORD_STREAM_SUSPENDED] = pa_command_stream_suspended,
     [PA_COMMAND_STARTED] = pa_command_stream_started,
-    [PA_COMMAND_SUBSCRIBE_EVENT] = pa_command_subscribe_event
+    [PA_COMMAND_SUBSCRIBE_EVENT] = pa_command_subscribe_event,
+    [PA_COMMAND_EXTENSION] = pa_command_extension,
+    [PA_COMMAND_PLAYBACK_STREAM_EVENT] = pa_command_stream_event,
+    [PA_COMMAND_RECORD_STREAM_EVENT] = pa_command_stream_event,
+    [PA_COMMAND_CLIENT_EVENT] = pa_command_client_event,
+    [PA_COMMAND_PLAYBACK_BUFFER_ATTR_CHANGED] = pa_command_stream_buffer_attr,
+    [PA_COMMAND_RECORD_BUFFER_ATTR_CHANGED] = pa_command_stream_buffer_attr
 };
-
-static void unlock_autospawn_lock_file(pa_context *c) {
-    pa_assert(c);
-
-    if (c->autospawn_lock_fd >= 0) {
-        char *lf;
-
-        lf = pa_runtime_path(AUTOSPAWN_LOCK);
-        pa_unlock_lockfile(lf, c->autospawn_lock_fd);
-        pa_xfree(lf);
-
-        c->autospawn_lock_fd = -1;
-    }
-}
-
 static void context_free(pa_context *c);
 
+#ifdef HAVE_DBUS
+static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, void *userdata);
+#endif
+
 pa_context *pa_context_new(pa_mainloop_api *mainloop, const char *name) {
     return pa_context_new_with_proplist(mainloop, name, NULL);
 }
@@ -125,6 +110,18 @@ static void reset_callbacks(pa_context *c) {
 
     c->subscribe_callback = NULL;
     c->subscribe_userdata = NULL;
+
+    c->event_callback = NULL;
+    c->event_userdata = NULL;
+
+    c->ext_device_manager.callback = NULL;
+    c->ext_device_manager.userdata = NULL;
+
+    c->ext_device_restore.callback = NULL;
+    c->ext_device_restore.userdata = NULL;
+
+    c->ext_stream_restore.callback = NULL;
+    c->ext_stream_restore.userdata = NULL;
 }
 
 pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char *name, pa_proplist *p) {
@@ -132,10 +129,12 @@ pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char *
 
     pa_assert(mainloop);
 
-    if (!name && !pa_proplist_contains(p, PA_PROP_APPLICATION_NAME))
+    if (pa_detect_fork())
         return NULL;
 
-    c = pa_xnew(pa_context, 1);
+    pa_init_i18n();
+
+    c = pa_xnew0(pa_context, 1);
     PA_REFCNT_INIT(c);
 
     c->proplist = p ? pa_proplist_copy(p) : pa_proplist_new();
@@ -143,31 +142,23 @@ pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char *
     if (name)
         pa_proplist_sets(c->proplist, PA_PROP_APPLICATION_NAME, name);
 
+#ifdef HAVE_DBUS
+    c->system_bus = c->session_bus = NULL;
+#endif
     c->mainloop = mainloop;
-    c->client = NULL;
-    c->pstream = NULL;
-    c->pdispatch = NULL;
-    c->playback_streams = pa_dynarray_new();
-    c->record_streams = pa_dynarray_new();
+    c->playback_streams = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+    c->record_streams = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
     c->client_index = PA_INVALID_INDEX;
+    c->use_rtclock = pa_mainloop_is_our_api(mainloop);
 
     PA_LLIST_HEAD_INIT(pa_stream, c->streams);
     PA_LLIST_HEAD_INIT(pa_operation, c->operations);
 
     c->error = PA_OK;
     c->state = PA_CONTEXT_UNCONNECTED;
-    c->ctag = 0;
-    c->csyncid = 0;
 
     reset_callbacks(c);
 
-    c->is_local = FALSE;
-    c->server_list = NULL;
-    c->server = NULL;
-    c->autospawn_lock_fd = -1;
-    memset(&c->spawn_api, 0, sizeof(c->spawn_api));
-    c->do_autospawn = FALSE;
-
 #ifndef MSG_NOSIGNAL
 #ifdef SIGPIPE
     pa_check_signal_is_blocked(SIGPIPE);
@@ -181,10 +172,10 @@ pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char *
 #endif
     pa_client_conf_env(c->conf);
 
-    if (!(c->mempool = pa_mempool_new(!c->conf->disable_shm))) {
+    if (!(c->mempool = pa_mempool_new(!c->conf->disable_shm, c->conf->shm_size))) {
 
         if (!c->conf->disable_shm)
-            c->mempool = pa_mempool_new(0);
+            c->mempool = pa_mempool_new(false, c->conf->shm_size);
 
         if (!c->mempool) {
             context_free(c);
@@ -235,12 +226,24 @@ static void context_free(pa_context *c) {
 
     context_unlink(c);
 
-    unlock_autospawn_lock_file(c);
+#ifdef HAVE_DBUS
+    if (c->system_bus) {
+        if (c->filter_added)
+            dbus_connection_remove_filter(pa_dbus_wrap_connection_get(c->system_bus), filter_cb, c);
+        pa_dbus_wrap_connection_free(c->system_bus);
+    }
+
+    if (c->session_bus) {
+        if (c->filter_added)
+            dbus_connection_remove_filter(pa_dbus_wrap_connection_get(c->session_bus), filter_cb, c);
+        pa_dbus_wrap_connection_free(c->session_bus);
+    }
+#endif
 
     if (c->record_streams)
-        pa_dynarray_free(c->record_streams, NULL, NULL);
+        pa_hashmap_free(c->record_streams);
     if (c->playback_streams)
-        pa_dynarray_free(c->playback_streams, NULL, NULL);
+        pa_hashmap_free(c->playback_streams);
 
     if (c->mempool)
         pa_mempool_free(c->mempool);
@@ -341,20 +344,19 @@ static void pstream_memblock_callback(pa_pstream *p, uint32_t channel, int64_t o
 
     pa_assert(p);
     pa_assert(chunk);
-    pa_assert(chunk->memblock);
-    pa_assert(chunk->length);
+    pa_assert(chunk->length > 0);
     pa_assert(c);
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
     pa_context_ref(c);
 
-    if ((s = pa_dynarray_get(c->record_streams, channel))) {
-
-        pa_assert(seek == PA_SEEK_RELATIVE);
-        pa_assert(offset == 0);
+    if ((s = pa_hashmap_get(c->record_streams, PA_UINT32_TO_PTR(channel)))) {
 
-        pa_memblockq_seek(s->record_memblockq, offset, seek);
-        pa_memblockq_push_align(s->record_memblockq, chunk);
+        if (chunk->memblock) {
+            pa_memblockq_seek(s->record_memblockq, offset, seek, true);
+            pa_memblockq_push_align(s->record_memblockq, chunk);
+        } else
+            pa_memblockq_seek(s->record_memblockq, offset+chunk->length, seek, true);
 
         if (s->read_callback) {
             size_t l;
@@ -367,7 +369,7 @@ static void pstream_memblock_callback(pa_pstream *p, uint32_t channel, int64_t o
     pa_context_unref(c);
 }
 
-int pa_context_handle_error(pa_context *c, uint32_t command, pa_tagstruct *t, pa_bool_t fail) {
+int pa_context_handle_error(pa_context *c, uint32_t command, pa_tagstruct *t, bool fail) {
     uint32_t err;
     pa_assert(c);
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
@@ -375,7 +377,8 @@ int pa_context_handle_error(pa_context *c, uint32_t command, pa_tagstruct *t, pa
     if (command == PA_COMMAND_ERROR) {
         pa_assert(t);
 
-        if (pa_tagstruct_getu32(t, &err) < 0) {
+        if (pa_tagstruct_getu32(t, &err) < 0 ||
+            !pa_tagstruct_eof(t)) {
             pa_context_fail(c, PA_ERR_PROTOCOL);
             return -1;
         }
@@ -396,11 +399,11 @@ int pa_context_handle_error(pa_context *c, uint32_t command, pa_tagstruct *t, pa
         err = PA_ERR_UNKNOWN;
 
     if (fail) {
-        pa_context_fail(c, err);
+        pa_context_fail(c, (int) err);
         return -1;
     }
 
-    pa_context_set_error(c, err);
+    pa_context_set_error(c, (int) err);
 
     return 0;
 }
@@ -415,13 +418,14 @@ static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t
     pa_context_ref(c);
 
     if (command != PA_COMMAND_REPLY) {
-        pa_context_handle_error(c, command, t, TRUE);
+        pa_context_handle_error(c, command, t, true);
         goto finish;
     }
 
     switch(c->state) {
         case PA_CONTEXT_AUTHORIZING: {
             pa_tagstruct *reply;
+            bool shm_on_remote = false;
 
             if (pa_tagstruct_getu32(t, &c->version) < 0 ||
                 !pa_tagstruct_eof(t)) {
@@ -435,10 +439,22 @@ static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t
                 goto finish;
             }
 
+            /* Starting with protocol version 13 the MSB of the version
+               tag reflects if shm is available for this connection or
+               not. */
+            if (c->version >= 13) {
+                shm_on_remote = !!(c->version & 0x80000000U);
+                c->version &= 0x7FFFFFFFU;
+            }
+
+            pa_log_debug("Protocol version: remote %u, local %u", c->version, PA_PROTOCOL_VERSION);
+
             /* Enable shared memory support if possible */
-            if (c->version >= 10 &&
-                pa_mempool_is_shared(c->mempool) &&
-                c->is_local) {
+            if (c->do_shm)
+                if (c->version < 10 || (c->version >= 13 && !shm_on_remote))
+                    c->do_shm = false;
+
+            if (c->do_shm) {
 
                 /* Only enable SHM if both sides are owned by the same
                  * user. This is a security measure because otherwise
@@ -446,12 +462,14 @@ static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t
 
 #ifdef HAVE_CREDS
                 const pa_creds *creds;
-                if ((creds = pa_pdispatch_creds(pd)))
-                    if (getuid() == creds->uid)
-                        pa_pstream_enable_shm(c->pstream, TRUE);
+                if (!(creds = pa_pdispatch_creds(pd)) || getuid() != creds->uid)
+                    c->do_shm = false;
 #endif
             }
 
+            pa_log_debug("Negotiated SHM: %s", pa_yes_no(c->do_shm));
+            pa_pstream_enable_shm(c->pstream, c->do_shm);
+
             reply = pa_tagstruct_command(c, PA_COMMAND_SET_CLIENT_NAME, &tag);
 
             if (c->version >= 13) {
@@ -500,17 +518,26 @@ static void setup_context(pa_context *c, pa_iochannel *io) {
     c->pstream = pa_pstream_new(c->mainloop, io, c->mempool);
 
     pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
-    pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
-    pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
+    pa_pstream_set_receive_packet_callback(c->pstream, pstream_packet_callback, c);
+    pa_pstream_set_receive_memblock_callback(c->pstream, pstream_memblock_callback, c);
 
     pa_assert(!c->pdispatch);
-    c->pdispatch = pa_pdispatch_new(c->mainloop, command_table, PA_COMMAND_MAX);
+    c->pdispatch = pa_pdispatch_new(c->mainloop, c->use_rtclock, command_table, PA_COMMAND_MAX);
 
     if (!c->conf->cookie_valid)
-        pa_log_info("No cookie loaded. Attempting to connect without.");
+        pa_log_info(_("No cookie loaded. Attempting to connect without."));
 
     t = pa_tagstruct_command(c, PA_COMMAND_AUTH, &tag);
-    pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION);
+
+    c->do_shm =
+        pa_mempool_is_shared(c->mempool) &&
+        c->is_local;
+
+    pa_log_debug("SHM possible: %s", pa_yes_no(c->do_shm));
+
+    /* Starting with protocol version 13 we use the MSB of the version
+     * tag for informing the other side if we could do SHM or not */
+    pa_tagstruct_putu32(t, PA_PROTOCOL_VERSION | (c->do_shm ? 0x80000000U : 0));
     pa_tagstruct_put_arbitrary(t, c->conf->cookie, sizeof(c->conf->cookie));
 
 #ifdef HAVE_CREDS
@@ -536,37 +563,50 @@ static void setup_context(pa_context *c, pa_iochannel *io) {
     pa_context_unref(c);
 }
 
-static void on_connection(pa_socket_client *client, pa_iochannel*io, void *userdata);
+static pa_strlist *prepend_per_user(pa_strlist *l) {
+    char *ufn;
+
+    /* The per-user instance */
+    if ((ufn = pa_runtime_path(PA_NATIVE_DEFAULT_UNIX_SOCKET))) {
+        l = pa_strlist_prepend(l, ufn);
+        pa_xfree(ufn);
+    }
+
+    return l;
+}
 
 #ifndef OS_IS_WIN32
 
-static int context_connect_spawn(pa_context *c) {
+static int context_autospawn(pa_context *c) {
     pid_t pid;
     int status, r;
-    int fds[2] = { -1, -1} ;
-    pa_iochannel *io;
-
-    if (getuid() == 0)
-        return -1;
+    struct sigaction sa;
 
     pa_context_ref(c);
 
-    if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
-        pa_log_error("socketpair(): %s", pa_cstrerror(errno));
+    if (sigaction(SIGCHLD, NULL, &sa) < 0) {
+        pa_log_debug("sigaction() failed: %s", pa_cstrerror(errno));
         pa_context_fail(c, PA_ERR_INTERNAL);
         goto fail;
     }
 
-    pa_make_fd_cloexec(fds[0]);
+#ifdef SA_NOCLDWAIT
+    if ((sa.sa_flags & SA_NOCLDWAIT) || sa.sa_handler == SIG_IGN) {
+#else
+    if (sa.sa_handler == SIG_IGN) {
+#endif
+        pa_log_debug("Process disabled waitpid(), cannot autospawn.");
+        pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
+        goto fail;
+    }
 
-    pa_make_socket_low_delay(fds[0]);
-    pa_make_socket_low_delay(fds[1]);
+    pa_log_debug("Trying to autospawn...");
 
     if (c->spawn_api.prefork)
         c->spawn_api.prefork();
 
     if ((pid = fork()) < 0) {
-        pa_log_error("fork(): %s", pa_cstrerror(errno));
+        pa_log_error(_("fork(): %s"), pa_cstrerror(errno));
         pa_context_fail(c, PA_ERR_INTERNAL);
 
         if (c->spawn_api.postfork)
@@ -576,33 +616,24 @@ static int context_connect_spawn(pa_context *c) {
     } else if (!pid) {
         /* Child */
 
-        char t[128];
         const char *state = NULL;
-#define MAX_ARGS 64
-        const char * argv[MAX_ARGS+1];
-        int n;
-        char *f;
-
-        pa_close_all(fds[1], -1);
-
-        f = pa_sprintf_malloc("%i", fds[1]);
-        pa_set_env("PULSE_PASSED_FD", f);
-        pa_xfree(f);
+        const char * argv[32];
+        unsigned n = 0;
 
         if (c->spawn_api.atfork)
             c->spawn_api.atfork();
 
-        /* Setup argv */
-
-        n = 0;
+        /* We leave most of the cleaning up of the process environment
+         * to the executable. We only clean up the file descriptors to
+         * make sure the executable can actually be loaded
+         * correctly. */
+        pa_close_all(-1);
 
+        /* Setup argv */
         argv[n++] = c->conf->daemon_binary;
-        argv[n++] = "--daemonize=yes";
-
-        pa_snprintf(t, sizeof(t), "-Lmodule-native-protocol-fd fd=%i", fds[1]);
-        argv[n++] = strdup(t);
+        argv[n++] = "--start";
 
-        while (n < MAX_ARGS) {
+        while (n < PA_ELEMENTSOF(argv)-1) {
             char *a;
 
             if (!(a = pa_split_spaces(c->conf->extra_arguments, &state)))
@@ -612,46 +643,42 @@ static int context_connect_spawn(pa_context *c) {
         }
 
         argv[n++] = NULL;
+        pa_assert(n <= PA_ELEMENTSOF(argv));
 
         execv(argv[0], (char * const *) argv);
         _exit(1);
-#undef MAX_ARGS
     }
 
     /* Parent */
 
-    pa_assert_se(pa_close(fds[1]) == 0);
-    fds[1] = -1;
-
-    r = waitpid(pid, &status, 0);
-
     if (c->spawn_api.postfork)
         c->spawn_api.postfork();
 
+    do {
+        r = waitpid(pid, &status, 0);
+    } while (r < 0 && errno == EINTR);
+
     if (r < 0) {
-        pa_log("waitpid(): %s", pa_cstrerror(errno));
-        pa_context_fail(c, PA_ERR_INTERNAL);
-        goto fail;
+
+        if (errno != ESRCH) {
+            pa_log(_("waitpid(): %s"), pa_cstrerror(errno));
+            pa_context_fail(c, PA_ERR_INTERNAL);
+            goto fail;
+        }
+
+        /* hmm, something already reaped our child, so we assume
+         * startup worked, even if we cannot know */
+
     } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
         pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
         goto fail;
     }
 
-    c->is_local = TRUE;
-
-    unlock_autospawn_lock_file(c);
-
-    io = pa_iochannel_new(c->mainloop, fds[0], fds[0]);
-    setup_context(c, io);
-
     pa_context_unref(c);
 
     return 0;
 
 fail:
-    pa_close_pipe(fds);
-
-    unlock_autospawn_lock_file(c);
 
     pa_context_unref(c);
 
@@ -660,6 +687,48 @@ fail:
 
 #endif /* OS_IS_WIN32 */
 
+static void on_connection(pa_socket_client *client, pa_iochannel*io, void *userdata);
+
+#ifdef HAVE_DBUS
+static void track_pulseaudio_on_dbus(pa_context *c, DBusBusType type, pa_dbus_wrap_connection **conn) {
+    DBusError error;
+
+    pa_assert(c);
+    pa_assert(conn);
+
+    dbus_error_init(&error);
+
+    if (!(*conn = pa_dbus_wrap_connection_new(c->mainloop, c->use_rtclock, type, &error)) || dbus_error_is_set(&error)) {
+        pa_log_warn("Unable to contact DBUS: %s: %s", error.name, error.message);
+        goto fail;
+    }
+
+    if (!dbus_connection_add_filter(pa_dbus_wrap_connection_get(*conn), filter_cb, c, NULL)) {
+        pa_log_warn("Failed to add filter function");
+        goto fail;
+    }
+    c->filter_added = true;
+
+    if (pa_dbus_add_matches(
+                pa_dbus_wrap_connection_get(*conn), &error,
+                "type='signal',sender='" DBUS_SERVICE_DBUS "',interface='" DBUS_INTERFACE_DBUS "',member='NameOwnerChanged',arg0='org.pulseaudio.Server',arg1=''", NULL) < 0) {
+
+        pa_log_warn("Unable to track org.pulseaudio.Server: %s: %s", error.name, error.message);
+        goto fail;
+    }
+
+    return;
+
+fail:
+    if (*conn) {
+        pa_dbus_wrap_connection_free(*conn);
+        *conn = NULL;
+    }
+
+    dbus_error_free(&error);
+}
+#endif
+
 static int try_next_connection(pa_context *c) {
     char *u = NULL;
     int r = -1;
@@ -677,12 +746,31 @@ static int try_next_connection(pa_context *c) {
 
 #ifndef OS_IS_WIN32
             if (c->do_autospawn) {
-                r = context_connect_spawn(c);
-                goto finish;
+
+                if ((r = context_autospawn(c)) < 0)
+                    goto finish;
+
+                /* Autospawn only once */
+                c->do_autospawn = false;
+
+                /* Connect only to per-user sockets this time */
+                c->server_list = prepend_per_user(c->server_list);
+
+                /* Retry connection */
+                continue;
             }
 #endif
 
-            pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
+#ifdef HAVE_DBUS
+            if (c->no_fail && !c->server_specified) {
+                if (!c->session_bus)
+                    track_pulseaudio_on_dbus(c, DBUS_BUS_SESSION, &c->session_bus);
+                if (!c->system_bus)
+                    track_pulseaudio_on_dbus(c, DBUS_BUS_SYSTEM, &c->system_bus);
+            } else
+#endif
+                pa_context_fail(c, PA_ERR_CONNECTIONREFUSED);
+
             goto finish;
         }
 
@@ -691,7 +779,7 @@ static int try_next_connection(pa_context *c) {
         pa_xfree(c->server);
         c->server = pa_xstrdup(u);
 
-        if (!(c->client = pa_socket_client_new_string(c->mainloop, u, PA_NATIVE_DEFAULT_PORT)))
+        if (!(c->client = pa_socket_client_new_string(c->mainloop, c->use_rtclock, u, PA_NATIVE_DEFAULT_PORT)))
             continue;
 
         c->is_local = !!pa_socket_client_is_local(c->client);
@@ -721,7 +809,7 @@ static void on_connection(pa_socket_client *client, pa_iochannel*io, void *userd
     c->client = NULL;
 
     if (!io) {
-        /* Try the item in the list */
+        /* Try the next item in the list */
         if (saved_errno == ECONNREFUSED ||
             saved_errno == ETIMEDOUT ||
             saved_errno == EHOSTUNREACH) {
@@ -733,31 +821,46 @@ static void on_connection(pa_socket_client *client, pa_iochannel*io, void *userd
         goto finish;
     }
 
-    unlock_autospawn_lock_file(c);
     setup_context(c, io);
 
 finish:
     pa_context_unref(c);
 }
 
+#ifdef HAVE_DBUS
+static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, void *userdata) {
+    pa_context *c = userdata;
+    bool is_session;
 
-static char *get_legacy_runtime_dir(void) {
-    char *p, u[128];
-    struct stat st;
+    pa_assert(bus);
+    pa_assert(message);
+    pa_assert(c);
 
-    if (!pa_get_user_name(u, sizeof(u)))
-        return NULL;
+    if (c->state != PA_CONTEXT_CONNECTING)
+        goto finish;
+
+    if (!c->no_fail)
+        goto finish;
 
-    p = pa_sprintf_malloc("/tmp/pulse-%s", u);
+    /* FIXME: We probably should check if this is actually the NameOwnerChanged we were looking for */
 
-    if (stat(p, &st) < 0)
-        return NULL;
+    is_session = c->session_bus && bus == pa_dbus_wrap_connection_get(c->session_bus);
+    pa_log_debug("Rock!! PulseAudio might be back on %s bus", is_session ? "session" : "system");
 
-    if (st.st_uid != getuid())
-        return NULL;
+    if (is_session)
+        /* The user instance via PF_LOCAL */
+        c->server_list = prepend_per_user(c->server_list);
+    else
+        /* The system wide instance via PF_LOCAL */
+        c->server_list = pa_strlist_prepend(c->server_list, PA_SYSTEM_RUNTIME_PATH PA_PATH_SEP PA_NATIVE_DEFAULT_UNIX_SOCKET);
 
-    return p;
+    if (!c->client)
+        try_next_connection(c);
+
+finish:
+    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
+#endif
 
 int pa_context_connect(
         pa_context *c,
@@ -770,15 +873,20 @@ int pa_context_connect(
     pa_assert(c);
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
+    PA_CHECK_VALIDITY(c, !pa_detect_fork(), PA_ERR_FORKED);
     PA_CHECK_VALIDITY(c, c->state == PA_CONTEXT_UNCONNECTED, PA_ERR_BADSTATE);
-    PA_CHECK_VALIDITY(c, !(flags & ~PA_CONTEXT_NOAUTOSPAWN), PA_ERR_INVALID);
+    PA_CHECK_VALIDITY(c, !(flags & ~(PA_CONTEXT_NOAUTOSPAWN|PA_CONTEXT_NOFAIL)), PA_ERR_INVALID);
     PA_CHECK_VALIDITY(c, !server || *server, PA_ERR_INVALID);
 
-    if (!server)
+    if (server)
+        c->conf->autospawn = false;
+    else
         server = c->conf->default_server;
 
     pa_context_ref(c);
 
+    c->no_fail = !!(flags & PA_CONTEXT_NOFAIL);
+    c->server_specified = !!server;
     pa_assert(!c->server_list);
 
     if (server) {
@@ -786,56 +894,50 @@ int pa_context_connect(
             pa_context_fail(c, PA_ERR_INVALIDSERVER);
             goto finish;
         }
+
     } else {
-        char *d, *ufn;
-        static char *legacy_dir;
+        char *d;
 
         /* Prepend in reverse order */
 
-        if ((d = getenv("DISPLAY"))) {
-            char *e;
-            d = pa_xstrdup(d);
-            if ((e = strchr(d, ':')))
-                *e = 0;
+        /* Follow the X display */
+        if (c->conf->auto_connect_display) {
+            if ((d = getenv("DISPLAY"))) {
+                d = pa_xstrndup(d, strcspn(d, ":"));
 
-            if (*d)
-                c->server_list = pa_strlist_prepend(c->server_list, d);
+                if (*d)
+                    c->server_list = pa_strlist_prepend(c->server_list, d);
 
-            pa_xfree(d);
+                pa_xfree(d);
+            }
         }
 
-        c->server_list = pa_strlist_prepend(c->server_list, "tcp6:localhost");
-        c->server_list = pa_strlist_prepend(c->server_list, "tcp4:localhost");
+        /* Add TCP/IP on the localhost */
+        if (c->conf->auto_connect_localhost) {
+            c->server_list = pa_strlist_prepend(c->server_list, "tcp6:[::1]");
+            c->server_list = pa_strlist_prepend(c->server_list, "tcp4:127.0.0.1");
+        }
 
-        /* The system wide instance */
+        /* The system wide instance via PF_LOCAL */
         c->server_list = pa_strlist_prepend(c->server_list, PA_SYSTEM_RUNTIME_PATH PA_PATH_SEP PA_NATIVE_DEFAULT_UNIX_SOCKET);
 
-        /* The old per-user instance path. This is supported only to easy upgrades */
-        if ((legacy_dir = get_legacy_runtime_dir())) {
-            char *p = pa_sprintf_malloc("%s" PA_PATH_SEP PA_NATIVE_DEFAULT_UNIX_SOCKET, legacy_dir);
-            c->server_list = pa_strlist_prepend(c->server_list, p);
-            pa_xfree(p);
-            pa_xfree(legacy_dir);
-        }
-
-        /* The per-user instance */
-        c->server_list = pa_strlist_prepend(c->server_list, ufn = pa_runtime_path(PA_NATIVE_DEFAULT_UNIX_SOCKET));
-        pa_xfree(ufn);
+        /* The user instance via PF_LOCAL */
+        c->server_list = prepend_per_user(c->server_list);
+    }
 
-        /* Wrap the connection attempts in a single transaction for sane autospawn locking */
-        if (!(flags & PA_CONTEXT_NOAUTOSPAWN) && c->conf->autospawn) {
-            char *lf;
+    /* Set up autospawning */
+    if (!(flags & PA_CONTEXT_NOAUTOSPAWN) && c->conf->autospawn) {
 
-            lf = pa_runtime_path(AUTOSPAWN_LOCK);
-            pa_assert(c->autospawn_lock_fd <= 0);
-            c->autospawn_lock_fd = pa_lock_lockfile(lf);
-            pa_xfree(lf);
+#ifdef HAVE_GETUID
+        if (getuid() == 0)
+            pa_log_debug("Not doing autospawn since we are root.");
+        else {
+            c->do_autospawn = true;
 
             if (api)
                 c->spawn_api = *api;
-
-            c->do_autospawn = TRUE;
         }
+#endif
     }
 
     pa_context_set_state(c, PA_CONTEXT_CONNECTING);
@@ -851,6 +953,9 @@ void pa_context_disconnect(pa_context *c) {
     pa_assert(c);
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
+    if (pa_detect_fork())
+        return;
+
     if (PA_CONTEXT_IS_GOOD(c->state))
         pa_context_set_state(c, PA_CONTEXT_TERMINATED);
 }
@@ -863,7 +968,10 @@ pa_context_state_t pa_context_get_state(pa_context *c) {
 }
 
 int pa_context_errno(pa_context *c) {
-    pa_assert(c);
+
+    if (!c)
+        return PA_ERR_INVALID;
+
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
     return c->error;
@@ -873,6 +981,9 @@ void pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb, voi
     pa_assert(c);
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
+    if (pa_detect_fork())
+        return;
+
     if (c->state == PA_CONTEXT_TERMINATED || c->state == PA_CONTEXT_FAILED)
         return;
 
@@ -880,10 +991,25 @@ void pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb, voi
     c->state_userdata = userdata;
 }
 
+void pa_context_set_event_callback(pa_context *c, pa_context_event_cb_t cb, void *userdata) {
+    pa_assert(c);
+    pa_assert(PA_REFCNT_VALUE(c) >= 1);
+
+    if (pa_detect_fork())
+        return;
+
+    if (c->state == PA_CONTEXT_TERMINATED || c->state == PA_CONTEXT_FAILED)
+        return;
+
+    c->event_callback = cb;
+    c->event_userdata = userdata;
+}
+
 int pa_context_is_pending(pa_context *c) {
     pa_assert(c);
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
+    PA_CHECK_VALIDITY(c, !pa_detect_fork(), PA_ERR_FORKED);
     PA_CHECK_VALIDITY(c, PA_CONTEXT_IS_GOOD(c->state), PA_ERR_BADSTATE);
 
     return (c->pstream && pa_pstream_is_pending(c->pstream)) ||
@@ -893,11 +1019,11 @@ int pa_context_is_pending(pa_context *c) {
 
 static void set_dispatch_callbacks(pa_operation *o);
 
-static void pdispatch_drain_callback(PA_GCC_UNUSED pa_pdispatch*pd, void *userdata) {
+static void pdispatch_drain_callback(pa_pdispatch*pd, void *userdata) {
     set_dispatch_callbacks(userdata);
 }
 
-static void pstream_drain_callback(PA_GCC_UNUSED pa_pstream *s, void *userdata) {
+static void pstream_drain_callback(pa_pstream *s, void *userdata) {
     set_dispatch_callbacks(userdata);
 }
 
@@ -940,6 +1066,7 @@ pa_operation* pa_context_drain(pa_context *c, pa_context_notify_cb_t cb, void *u
     pa_assert(c);
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
+    PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED);
     PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
     PA_CHECK_VALIDITY_RETURN_NULL(c, pa_context_is_pending(c), PA_ERR_BADSTATE);
 
@@ -949,7 +1076,7 @@ pa_operation* pa_context_drain(pa_context *c, pa_context_notify_cb_t cb, void *u
     return o;
 }
 
-void pa_context_simple_ack_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_UNUSED uint32_t tag, pa_tagstruct *t, void *userdata) {
+void pa_context_simple_ack_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
     pa_operation *o = userdata;
     int success = 1;
 
@@ -961,7 +1088,7 @@ void pa_context_simple_ack_callback(pa_pdispatch *pd, uint32_t command, PA_GCC_U
         goto finish;
 
     if (command != PA_COMMAND_REPLY) {
-        if (pa_context_handle_error(o->context, command, t, FALSE) < 0)
+        if (pa_context_handle_error(o->context, command, t, false) < 0)
             goto finish;
 
         success = 0;
@@ -988,6 +1115,7 @@ pa_operation* pa_context_send_simple_command(pa_context *c, uint32_t command, pa
     pa_assert(c);
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
+    PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED);
     PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
 
     o = pa_operation_new(c, NULL, cb, userdata);
@@ -1014,6 +1142,7 @@ pa_operation* pa_context_set_default_sink(pa_context *c, const char *name, pa_co
     pa_assert(c);
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
+    PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED);
     PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
 
     o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
@@ -1033,6 +1162,7 @@ pa_operation* pa_context_set_default_source(pa_context *c, const char *name, pa_
     pa_assert(c);
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
+    PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED);
     PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
 
     o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
@@ -1048,6 +1178,7 @@ int pa_context_is_local(pa_context *c) {
     pa_assert(c);
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
+    PA_CHECK_VALIDITY_RETURN_ANY(c, !pa_detect_fork(), PA_ERR_FORKED, -1);
     PA_CHECK_VALIDITY_RETURN_ANY(c, PA_CONTEXT_IS_GOOD(c->state), PA_ERR_BADSTATE, -1);
 
     return !!c->is_local;
@@ -1060,6 +1191,7 @@ pa_operation* pa_context_set_name(pa_context *c, const char *name, pa_context_su
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
     pa_assert(name);
 
+    PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED);
     PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
 
     if (c->version >= 13) {
@@ -1083,15 +1215,15 @@ pa_operation* pa_context_set_name(pa_context *c, const char *name, pa_context_su
 }
 
 const char* pa_get_library_version(void) {
-    return PACKAGE_VERSION;
+    return pa_get_headers_version();
 }
 
 const char* pa_context_get_server(pa_context *c) {
     pa_assert(c);
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
-    if (!c->server)
-        return NULL;
+    PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED);
+    PA_CHECK_VALIDITY_RETURN_NULL(c, c->server, PA_ERR_NOENTITY);
 
     if (*c->server == '{') {
         char *e = strchr(c->server+1, '}');
@@ -1101,7 +1233,7 @@ const char* pa_context_get_server(pa_context *c) {
     return c->server;
 }
 
-uint32_t pa_context_get_protocol_version(PA_GCC_UNUSED pa_context *c) {
+uint32_t pa_context_get_protocol_version(pa_context *c) {
     return PA_PROTOCOL_VERSION;
 }
 
@@ -1109,6 +1241,7 @@ uint32_t pa_context_get_server_protocol_version(pa_context *c) {
     pa_assert(c);
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
+    PA_CHECK_VALIDITY_RETURN_ANY(c, !pa_detect_fork(), PA_ERR_FORKED, PA_INVALID_INDEX);
     PA_CHECK_VALIDITY_RETURN_ANY(c, PA_CONTEXT_IS_GOOD(c->state), PA_ERR_BADSTATE, PA_INVALID_INDEX);
 
     return c->version;
@@ -1131,6 +1264,7 @@ uint32_t pa_context_get_index(pa_context *c) {
     pa_assert(c);
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
+    PA_CHECK_VALIDITY_RETURN_ANY(c, !pa_detect_fork(), PA_ERR_FORKED, PA_INVALID_INDEX);
     PA_CHECK_VALIDITY_RETURN_ANY(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE, PA_INVALID_INDEX);
     PA_CHECK_VALIDITY_RETURN_ANY(c, c->version >= 13, PA_ERR_NOTSUPPORTED, PA_INVALID_INDEX);
 
@@ -1145,6 +1279,7 @@ pa_operation *pa_context_proplist_update(pa_context *c, pa_update_mode_t mode, p
     pa_assert(c);
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
+    PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED);
     PA_CHECK_VALIDITY_RETURN_NULL(c, mode == PA_UPDATE_SET || mode == PA_UPDATE_MERGE || mode == PA_UPDATE_REPLACE, PA_ERR_INVALID);
     PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
     PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 13, PA_ERR_NOTSUPPORTED);
@@ -1173,6 +1308,7 @@ pa_operation *pa_context_proplist_remove(pa_context *c, const char *const keys[]
     pa_assert(c);
     pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
+    PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED);
     PA_CHECK_VALIDITY_RETURN_NULL(c, keys && keys[0], PA_ERR_INVALID);
     PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
     PA_CHECK_VALIDITY_RETURN_NULL(c, c->version >= 13, PA_ERR_NOTSUPPORTED);
@@ -1195,84 +1331,131 @@ pa_operation *pa_context_proplist_remove(pa_context *c, const char *const keys[]
     return o;
 }
 
-void pa_init_proplist(pa_proplist *p) {
-    int a, b;
-#ifndef HAVE_DECL_ENVIRON
-    extern char **environ;
-#endif
-    char **e;
+void pa_command_extension(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
+    pa_context *c = userdata;
+    uint32_t idx;
+    const char *name;
 
-    pa_assert(p);
+    pa_assert(pd);
+    pa_assert(command == PA_COMMAND_EXTENSION);
+    pa_assert(t);
+    pa_assert(c);
+    pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
-    for (e = environ; *e; e++) {
+    pa_context_ref(c);
 
-        if (pa_startswith(*e, "PULSE_PROP_")) {
-            size_t kl = strcspn(*e+11, "=");
-            char *k;
+    if (c->version < 15) {
+        pa_context_fail(c, PA_ERR_PROTOCOL);
+        goto finish;
+    }
 
-            if ((*e)[11+kl] != '=')
-                continue;
+    if (pa_tagstruct_getu32(t, &idx) < 0 ||
+        pa_tagstruct_gets(t, &name) < 0) {
+        pa_context_fail(c, PA_ERR_PROTOCOL);
+        goto finish;
+    }
 
-            if (!pa_utf8_valid(*e+11+kl+1))
-                continue;
+    if (pa_streq(name, "module-device-manager"))
+        pa_ext_device_manager_command(c, tag, t);
+    else if (pa_streq(name, "module-device-restore"))
+        pa_ext_device_restore_command(c, tag, t);
+    else if (pa_streq(name, "module-stream-restore"))
+        pa_ext_stream_restore_command(c, tag, t);
+    else
+        pa_log(_("Received message for unknown extension '%s'"), name);
 
-            k = pa_xstrndup(*e+11, kl);
+finish:
+    pa_context_unref(c);
+}
 
-            if (pa_proplist_contains(p, k)) {
-                pa_xfree(k);
-                continue;
-            }
+void pa_command_client_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
+    pa_context *c = userdata;
+    pa_proplist *pl = NULL;
+    const char *event;
 
-            pa_proplist_sets(p, k, *e+11+kl+1);
-            pa_xfree(k);
-        }
-    }
+    pa_assert(pd);
+    pa_assert(command == PA_COMMAND_CLIENT_EVENT);
+    pa_assert(t);
+    pa_assert(c);
+    pa_assert(PA_REFCNT_VALUE(c) >= 1);
 
-    if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_ID)) {
-        char t[32];
-        pa_snprintf(t, sizeof(t), "%lu", (unsigned long) getpid());
-        pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_ID, t);
-    }
+    pa_context_ref(c);
 
-    if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_USER)) {
-        char t[64];
-        if (pa_get_user_name(t, sizeof(t))) {
-            char *c = pa_utf8_filter(t);
-            pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_USER, c);
-            pa_xfree(c);
-        }
+    if (c->version < 15) {
+        pa_context_fail(c, PA_ERR_PROTOCOL);
+        goto finish;
     }
 
-    if (!pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_HOST)) {
-        char t[64];
-        if (pa_get_host_name(t, sizeof(t))) {
-            char *c = pa_utf8_filter(t);
-            pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_HOST, c);
-            pa_xfree(c);
-        }
+    pl = pa_proplist_new();
+
+    if (pa_tagstruct_gets(t, &event) < 0 ||
+        pa_tagstruct_get_proplist(t, pl) < 0 ||
+        !pa_tagstruct_eof(t) || !event) {
+        pa_context_fail(c, PA_ERR_PROTOCOL);
+        goto finish;
     }
 
-    a = pa_proplist_contains(p, PA_PROP_APPLICATION_PROCESS_BINARY);
-    b = pa_proplist_contains(p, PA_PROP_APPLICATION_NAME);
+    if (c->event_callback)
+        c->event_callback(c, event, pl, c->event_userdata);
 
-    if (!a || !b) {
-        char t[PATH_MAX];
-        if (pa_get_binary_name(t, sizeof(t))) {
-            char *c = pa_utf8_filter(t);
+finish:
+    pa_context_unref(c);
 
-            if (!a)
-                pa_proplist_sets(p, PA_PROP_APPLICATION_PROCESS_BINARY, c);
-            if (!b)
-                pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, c);
+    if (pl)
+        pa_proplist_free(pl);
+}
 
-            pa_xfree(c);
-        }
-    }
+pa_time_event* pa_context_rttime_new(pa_context *c, pa_usec_t usec, pa_time_event_cb_t cb, void *userdata) {
+    struct timeval tv;
+
+    pa_assert(c);
+    pa_assert(PA_REFCNT_VALUE(c) >= 1);
+    pa_assert(c->mainloop);
 
-    if (!pa_proplist_contains(p, PA_PROP_APPLICATION_LANGUAGE)) {
-        const char *l;
+    if (usec == PA_USEC_INVALID)
+        return c->mainloop->time_new(c->mainloop, NULL, cb, userdata);
+
+    pa_timeval_rtstore(&tv, usec, c->use_rtclock);
+
+    return c->mainloop->time_new(c->mainloop, &tv, cb, userdata);
+}
 
-        if ((l = setlocale(LC_MESSAGES, NULL)))
-            pa_proplist_sets(p, PA_PROP_APPLICATION_LANGUAGE, l);
+void pa_context_rttime_restart(pa_context *c, pa_time_event *e, pa_usec_t usec) {
+    struct timeval tv;
+
+    pa_assert(c);
+    pa_assert(PA_REFCNT_VALUE(c) >= 1);
+    pa_assert(c->mainloop);
+
+    if (usec == PA_USEC_INVALID)
+        c->mainloop->time_restart(e, NULL);
+    else {
+        pa_timeval_rtstore(&tv, usec, c->use_rtclock);
+        c->mainloop->time_restart(e, &tv);
     }
 }
+
+size_t pa_context_get_tile_size(pa_context *c, const pa_sample_spec *ss) {
+    size_t fs, mbs;
+
+    pa_assert(c);
+    pa_assert(PA_REFCNT_VALUE(c) >= 1);
+
+    PA_CHECK_VALIDITY_RETURN_ANY(c, !pa_detect_fork(), PA_ERR_FORKED, (size_t) -1);
+    PA_CHECK_VALIDITY_RETURN_ANY(c, !ss || pa_sample_spec_valid(ss), PA_ERR_INVALID, (size_t) -1);
+
+    fs = ss ? pa_frame_size(ss) : 1;
+    mbs = PA_ROUND_DOWN(pa_mempool_block_size_max(c->mempool), fs);
+    return PA_MAX(mbs, fs);
+}
+
+int pa_context_load_cookie_from_file(pa_context *c, const char *cookie_file_path) {
+    pa_assert(c);
+    pa_assert(cookie_file_path);
+    pa_assert(PA_REFCNT_VALUE(c) >= 1);
+
+    PA_CHECK_VALIDITY(c, !pa_detect_fork(), PA_ERR_FORKED);
+    PA_CHECK_VALIDITY(c, c->state == PA_CONTEXT_UNCONNECTED, PA_ERR_BADSTATE);
+
+    return pa_client_conf_load_cookie_from_file(c->conf, cookie_file_path);
+}