X-Git-Url: https://code.delx.au/pulseaudio/blobdiff_plain/f753ef2a23c0c42e5ad35a36327a9fda2e4f2d98..0a583fe0c330d167f52784585afffdd8065a92cd:/src/pulse/context.c diff --git a/src/pulse/context.c b/src/pulse/context.c index 505e758a..761d13c8 100644 --- a/src/pulse/context.c +++ b/src/pulse/context.c @@ -32,57 +32,43 @@ #include #include #include -#include -#include #ifdef HAVE_SYS_WAIT_H #include #endif -#ifdef HAVE_SYS_SOCKET_H -#include -#endif -#ifdef HAVE_SYS_UN_H -#include -#endif #ifdef HAVE_NETDB_H #include #endif #include #include -#include #include -#include #include #include +#include +#include +#ifdef HAVE_X11 +#include +#endif -#include #include - +#include #include #include #include -#include +#include #include #include #include #include #include -#include +#include #include #include #include #include "internal.h" - -#include "client-conf.h" -#include "fork-detect.h" - -#ifdef HAVE_X11 -#include "client-conf-x11.h" -#endif - #include "context.h" void pa_command_extension(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata); @@ -128,6 +114,12 @@ static void reset_callbacks(pa_context *c) { 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; } @@ -142,7 +134,7 @@ pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char * pa_init_i18n(); - c = pa_xnew(pa_context, 1); + c = pa_xnew0(pa_context, 1); PA_REFCNT_INIT(c); c->proplist = p ? pa_proplist_copy(p) : pa_proplist_new(); @@ -154,11 +146,8 @@ pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char * 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); @@ -167,22 +156,9 @@ pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char * 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->do_shm = FALSE; - - c->server_specified = FALSE; - c->no_fail = FALSE; - c->do_autospawn = FALSE; - memset(&c->spawn_api, 0, sizeof(c->spawn_api)); - #ifndef MSG_NOSIGNAL #ifdef SIGPIPE pa_check_signal_is_blocked(SIGPIPE); @@ -190,16 +166,16 @@ pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char * #endif c->conf = pa_client_conf_new(); - pa_client_conf_load(c->conf, NULL); + pa_client_conf_load(c->conf); #ifdef HAVE_X11 - pa_client_conf_from_x11(c->conf, NULL); + pa_client_conf_from_x11(c->conf); #endif pa_client_conf_env(c->conf); if (!(c->mempool = pa_mempool_new(!c->conf->disable_shm, c->conf->shm_size))) { if (!c->conf->disable_shm) - c->mempool = pa_mempool_new(FALSE, c->conf->shm_size); + c->mempool = pa_mempool_new(false, c->conf->shm_size); if (!c->mempool) { context_free(c); @@ -252,20 +228,22 @@ static void context_free(pa_context *c) { #ifdef HAVE_DBUS if (c->system_bus) { - dbus_connection_remove_filter(pa_dbus_wrap_connection_get(c->system_bus), filter_cb, c); + 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) { - dbus_connection_remove_filter(pa_dbus_wrap_connection_get(c->session_bus), filter_cb, c); + 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); @@ -372,13 +350,13 @@ static void pstream_memblock_callback(pa_pstream *p, uint32_t channel, int64_t o pa_context_ref(c); - if ((s = pa_dynarray_get(c->record_streams, channel))) { + if ((s = pa_hashmap_get(c->record_streams, PA_UINT32_TO_PTR(channel)))) { if (chunk->memblock) { - pa_memblockq_seek(s->record_memblockq, offset, seek, TRUE); + 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); + pa_memblockq_seek(s->record_memblockq, offset+chunk->length, seek, true); if (s->read_callback) { size_t l; @@ -391,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); @@ -440,14 +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; - pa_bool_t shm_on_remote = FALSE; + bool shm_on_remote = false; if (pa_tagstruct_getu32(t, &c->version) < 0 || !pa_tagstruct_eof(t)) { @@ -474,7 +452,7 @@ static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t /* Enable shared memory support if possible */ if (c->do_shm) if (c->version < 10 || (c->version >= 13 && !shm_on_remote)) - c->do_shm = FALSE; + c->do_shm = false; if (c->do_shm) { @@ -485,7 +463,7 @@ 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)) || getuid() != creds->uid) - c->do_shm = FALSE; + c->do_shm = false; #endif } @@ -540,8 +518,8 @@ 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, c->use_rtclock, command_table, PA_COMMAND_MAX); @@ -585,75 +563,9 @@ static void setup_context(pa_context *c, pa_iochannel *io) { pa_context_unref(c); } -#ifdef ENABLE_LEGACY_RUNTIME_DIR -static char *get_old_legacy_runtime_dir(void) { - char *p, u[128]; - struct stat st; - - if (!pa_get_user_name(u, sizeof(u))) - return NULL; - - p = pa_sprintf_malloc("/tmp/pulse-%s", u); - - if (stat(p, &st) < 0) { - pa_xfree(p); - return NULL; - } - - if (st.st_uid != getuid()) { - pa_xfree(p); - return NULL; - } - - return p; -} - -static char *get_very_old_legacy_runtime_dir(void) { - char *p, h[128]; - struct stat st; - - if (!pa_get_home_dir(h, sizeof(h))) - return NULL; - - p = pa_sprintf_malloc("%s/.pulse", h); - - if (stat(p, &st) < 0) { - pa_xfree(p); - return NULL; - } - - if (st.st_uid != getuid()) { - pa_xfree(p); - return NULL; - } - - return p; -} -#endif - static pa_strlist *prepend_per_user(pa_strlist *l) { char *ufn; -#ifdef ENABLE_LEGACY_RUNTIME_DIR - static char *legacy_dir; - - /* The very old per-user instance path (< 0.9.11). This is supported only to ease upgrades */ - if ((legacy_dir = get_very_old_legacy_runtime_dir())) { - char *p = pa_sprintf_malloc("%s" PA_PATH_SEP PA_NATIVE_DEFAULT_UNIX_SOCKET, legacy_dir); - l = pa_strlist_prepend(l, p); - pa_xfree(p); - pa_xfree(legacy_dir); - } - - /* The old per-user instance path (< 0.9.12). This is supported only to ease upgrades */ - if ((legacy_dir = get_old_legacy_runtime_dir())) { - char *p = pa_sprintf_malloc("%s" PA_PATH_SEP PA_NATIVE_DEFAULT_UNIX_SOCKET, legacy_dir); - l = pa_strlist_prepend(l, p); - pa_xfree(p); - pa_xfree(legacy_dir); - } -#endif - /* The per-user instance */ if ((ufn = pa_runtime_path(PA_NATIVE_DEFAULT_UNIX_SOCKET))) { l = pa_strlist_prepend(l, ufn); @@ -668,11 +580,28 @@ static pa_strlist *prepend_per_user(pa_strlist *l) { static int context_autospawn(pa_context *c) { pid_t pid; int status, r; - - pa_log_debug("Trying to autospawn..."); + struct sigaction sa; pa_context_ref(c); + if (sigaction(SIGCHLD, NULL, &sa) < 0) { + pa_log_debug("sigaction() failed: %s", pa_cstrerror(errno)); + pa_context_fail(c, PA_ERR_INTERNAL); + goto fail; + } + +#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_log_debug("Trying to autospawn..."); + if (c->spawn_api.prefork) c->spawn_api.prefork(); @@ -688,23 +617,23 @@ static int context_autospawn(pa_context *c) { /* Child */ const char *state = NULL; -#define MAX_ARGS 64 - const char * argv[MAX_ARGS+1]; - int n; + const char * argv[32]; + unsigned n = 0; if (c->spawn_api.atfork) c->spawn_api.atfork(); + /* 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 */ - - n = 0; - argv[n++] = c->conf->daemon_binary; 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))) @@ -714,10 +643,10 @@ static int context_autospawn(pa_context *c) { } argv[n++] = NULL; + pa_assert(n <= PA_ELEMENTSOF(argv)); execv(argv[0], (char * const *) argv); _exit(1); -#undef MAX_ARGS } /* Parent */ @@ -730,9 +659,16 @@ static int context_autospawn(pa_context *c) { } 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; @@ -761,22 +697,34 @@ static void track_pulseaudio_on_dbus(pa_context *c, DBusBusType type, pa_dbus_wr 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 finish; + 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 finish; + 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) + "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; + } - finish: dbus_error_free(&error); } #endif @@ -803,7 +751,7 @@ static int try_next_connection(pa_context *c) { goto finish; /* Autospawn only once */ - c->do_autospawn = FALSE; + c->do_autospawn = false; /* Connect only to per-user sockets this time */ c->server_list = prepend_per_user(c->server_list); @@ -861,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) { @@ -882,7 +830,7 @@ finish: #ifdef HAVE_DBUS static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, void *userdata) { pa_context *c = userdata; - pa_bool_t is_session; + bool is_session; pa_assert(bus); pa_assert(message); @@ -897,7 +845,7 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, vo /* FIXME: We probably should check if this is actually the NameOwnerChanged we were looking for */ is_session = c->session_bus && bus == pa_dbus_wrap_connection_get(c->session_bus); - pa_log_debug("Rock!! PulseAudio is back on %s bus", is_session ? "session" : "system"); + pa_log_debug("Rock!! PulseAudio might be back on %s bus", is_session ? "session" : "system"); if (is_session) /* The user instance via PF_LOCAL */ @@ -931,13 +879,13 @@ int pa_context_connect( PA_CHECK_VALIDITY(c, !server || *server, PA_ERR_INVALID); if (server) - c->conf->autospawn = FALSE; + c->conf->autospawn = false; else server = c->conf->default_server; pa_context_ref(c); - c->no_fail = flags & PA_CONTEXT_NOFAIL; + c->no_fail = !!(flags & PA_CONTEXT_NOFAIL); c->server_specified = !!server; pa_assert(!c->server_list); @@ -953,21 +901,22 @@ int pa_context_connect( /* Prepend in reverse order */ /* Follow the X display */ - if ((d = getenv("DISPLAY"))) { - char *e; - d = pa_xstrdup(d); - if ((e = strchr(d, ':'))) - *e = 0; + 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); + } } /* Add TCP/IP on the 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"); + 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 via PF_LOCAL */ c->server_list = pa_strlist_prepend(c->server_list, PA_SYSTEM_RUNTIME_PATH PA_PATH_SEP PA_NATIVE_DEFAULT_UNIX_SOCKET); @@ -979,14 +928,16 @@ int pa_context_connect( /* Set up autospawning */ if (!(flags & PA_CONTEXT_NOAUTOSPAWN) && c->conf->autospawn) { +#ifdef HAVE_GETUID if (getuid() == 0) pa_log_debug("Not doing autospawn since we are root."); else { - c->do_autospawn = TRUE; + c->do_autospawn = true; if (api) c->spawn_api = *api; } +#endif } pa_context_set_state(c, PA_CONTEXT_CONNECTING); @@ -1017,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; @@ -1134,7 +1088,7 @@ void pa_context_simple_ack_callback(pa_pdispatch *pd, uint32_t command, uint32_t 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; @@ -1261,7 +1215,7 @@ 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) { @@ -1401,7 +1355,11 @@ void pa_command_extension(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_t goto finish; } - if (!strcmp(name, "module-stream-restore")) + 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); @@ -1410,7 +1368,6 @@ finish: pa_context_unref(c); } - 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; @@ -1452,6 +1409,7 @@ pa_time_event* pa_context_rttime_new(pa_context *c, pa_usec_t usec, pa_time_even struct timeval tv; pa_assert(c); + pa_assert(PA_REFCNT_VALUE(c) >= 1); pa_assert(c->mainloop); if (usec == PA_USEC_INVALID) @@ -1466,6 +1424,7 @@ 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) @@ -1475,3 +1434,28 @@ void pa_context_rttime_restart(pa_context *c, pa_time_event *e, pa_usec_t usec) 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); +}