make sure we keep a reference of the bus connection during the whole runtime if we manage to acquire the bus name

This commit is contained in:
Lennart Poettering 2009-04-06 02:31:22 +02:00
parent ecba42bd30
commit 90f4fdb071
2 changed files with 30 additions and 11 deletions

View file

@ -334,27 +334,38 @@ static void set_all_rlimits(const pa_daemon_conf *conf) {
#endif #endif
#ifdef HAVE_DBUS #ifdef HAVE_DBUS
static void register_org_pulseaudio(pa_core *c) static pa_dbus_connection *register_dbus(pa_core *c) {
{
DBusError error; DBusError error;
pa_dbus_connection *conn; pa_dbus_connection *conn;
dbus_error_init(&error); dbus_error_init(&error);
if (!(conn = pa_dbus_bus_get(c, pa_in_system_mode() ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) { if (!(conn = pa_dbus_bus_get(c, pa_in_system_mode() ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) {
pa_log_warn("Unable to contact DBUS: %s: %s", error.name, error.message); pa_log_warn("Unable to contact D-Bus: %s: %s", error.name, error.message);
goto finish_dbus; goto fail;
} }
if (dbus_bus_request_name (pa_dbus_connection_get(conn), "org.pulseaudio.Server", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) if (dbus_bus_request_name(pa_dbus_connection_get(conn), "org.pulseaudio.Server", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
pa_log_debug("Got org.pulseaudio.Server!"); pa_log_debug("Got org.pulseaudio.Server!");
else if (dbus_error_is_set(&error)) return conn;
pa_log_warn("Unable to get org.pulseaudio.Server: %s: %s", error.name, error.message); }
if (dbus_error_is_set(&error))
pa_log_warn("Failed to acquire org.pulseaudio.Server: %s: %s", error.name, error.message);
else
pa_log_warn("D-Bus name org.pulseaudio.Server already taken. Weird shit!");
/* PA cannot be started twice by the same user and hence we can
* ignore mostly the case that org.pulseaudio.Server is already
* taken. */
fail:
finish_dbus:
if (conn) if (conn)
pa_dbus_connection_unref(conn); pa_dbus_connection_unref(conn);
dbus_error_free(&error); dbus_error_free(&error);
return NULL;
} }
#endif #endif
@ -380,6 +391,9 @@ int main(int argc, char *argv[]) {
#endif #endif
int autospawn_fd = -1; int autospawn_fd = -1;
pa_bool_t autospawn_locked = FALSE; pa_bool_t autospawn_locked = FALSE;
#ifdef HAVE_DBUS
pa_dbus_connection *dbus = NULL;
#endif
pa_log_set_ident("pulseaudio"); pa_log_set_ident("pulseaudio");
pa_log_set_level(PA_LOG_INFO); pa_log_set_level(PA_LOG_INFO);
@ -1026,7 +1040,7 @@ int main(int argc, char *argv[]) {
#endif #endif
#ifdef HAVE_DBUS #ifdef HAVE_DBUS
register_org_pulseaudio(c); dbus = register_dbus(c);
#endif #endif
pa_log_info(_("Daemon startup complete.")); pa_log_info(_("Daemon startup complete."));
@ -1038,6 +1052,10 @@ int main(int argc, char *argv[]) {
pa_log_info(_("Daemon shutdown initiated.")); pa_log_info(_("Daemon shutdown initiated."));
finish: finish:
#ifdef HAVE_DBUS
if (dbus)
pa_dbus_connection_unref(dbus);
#endif
if (autospawn_fd >= 0) { if (autospawn_fd >= 0) {
if (autospawn_locked) if (autospawn_locked)

View file

@ -244,7 +244,7 @@ static void wakeup_main(void *userdata) {
pa_dbus_wrap_connection* pa_dbus_wrap_connection_new(pa_mainloop_api *m, DBusBusType type, DBusError *error) { pa_dbus_wrap_connection* pa_dbus_wrap_connection_new(pa_mainloop_api *m, DBusBusType type, DBusError *error) {
DBusConnection *conn; DBusConnection *conn;
pa_dbus_wrap_connection *pconn = NULL; pa_dbus_wrap_connection *pconn;
pa_assert(type == DBUS_BUS_SYSTEM || type == DBUS_BUS_SESSION || type == DBUS_BUS_STARTER); pa_assert(type == DBUS_BUS_SYSTEM || type == DBUS_BUS_SESSION || type == DBUS_BUS_STARTER);
@ -273,7 +273,8 @@ void pa_dbus_wrap_connection_free(pa_dbus_wrap_connection* c) {
dbus_connection_close(c->connection); dbus_connection_close(c->connection);
/* must process remaining messages, bit of a kludge to handle /* must process remaining messages, bit of a kludge to handle
* both unload and shutdown */ * both unload and shutdown */
while (dbus_connection_read_write_dispatch(c->connection, -1)); while (dbus_connection_read_write_dispatch(c->connection, -1))
;
} }
c->mainloop->defer_free(c->dispatch_event); c->mainloop->defer_free(c->dispatch_event);