From a2129f7cccaf0265fffe0da79ca0510b6e01131b Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Sun, 1 Oct 2006 15:36:19 +0000 Subject: 2006-10-01 Havoc Pennington * dbus/dbus-connection.c (_dbus_connection_close_if_only_one_ref): Add a hack to make DBusNewConnectionFunction work right. * dbus/dbus-server-socket.c (handle_new_client_fd_and_unlock): use the hack here. Also, fix the todo about refcount leak. * dbus/dbus-server-debug-pipe.c (_dbus_transport_debug_pipe_new): and use the hack here * dbus/dbus-connection.c: Kill the "shared" flag vs. the "shareable" flag; this was completely broken, since it meant dbus_connection_open() returned a connection of unknown shared-ness. Now, we always hold a ref on anything opened as shareable. Move the call to notify dbus-bus.c into connection_forget_shared_unlocked, so libdbus consistently forgets all its knowledge of a connection at once. This exposed numerous places where things were totally broken if we dropped a ref inside get_dispatch_status_unlocked where connection_forget_shared_unlocked was previously, so move connection_forget_shared_unlocked into _dbus_connection_update_dispatch_status_and_unlock. Also move the exit_on_disconnect here. (shared_connections_shutdown): this assumed weak refs to the shared connections; since we have strong refs now, the assertion was failing and stuff was left in the hash. Fix it to close still-open shared connections. * bus/dispatch.c: fixup to use dbus_connection_open_private on the debug pipe connections * dbus/dbus-connection.c (dbus_connection_dispatch): only notify dbus-bus.c if the closed connection is in fact shared (_dbus_connection_close_possibly_shared): rename from _dbus_connection_close_internal (dbus_connection_close, dbus_connection_open, dbus_connection_open_private): Improve docs to explain the deal with when you should close or unref or both * dbus/dbus-bus.c (_dbus_bus_notify_shared_connection_disconnected_unlocked): rename from _dbus_bus_check_connection_and_unref_unlocked and modify to loop over all connections * test/test-utils.c (test_connection_shutdown): don't try to close shared connections. * test/name-test/test-threads-init.c (main): fix warnings in here * dbus/dbus-sysdeps.c (_dbus_abort): support DBUS_BLOCK_ON_ABORT env variable to cause blocking waiting for gdb; drop DBUS_PRINT_BACKTRACE and just call _dbus_print_backtrace() unconditionally. * configure.in: add -export-dynamic to libtool flags if assertions enabled so _dbus_print_backtrace works. * dbus/dbus-sysdeps-unix.c (_dbus_print_backtrace): use fprintf instead of _dbus_verbose to print the backtrace, and diagnose lack of -rdynamic/-export-dynamic --- dbus/dbus-bus.c | 81 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 24 deletions(-) (limited to 'dbus/dbus-bus.c') diff --git a/dbus/dbus-bus.c b/dbus/dbus-bus.c index a2336786..bc8c107f 100644 --- a/dbus/dbus-bus.c +++ b/dbus/dbus-bus.c @@ -158,6 +158,7 @@ init_connections_unlocked (void) /* Use default system bus address if none set in environment */ bus_connection_addresses[DBUS_BUS_SYSTEM] = _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS); + if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL) return FALSE; @@ -179,7 +180,8 @@ init_connections_unlocked (void) if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL) bus_connection_addresses[DBUS_BUS_SESSION] = _dbus_strdup (DBUS_SESSION_BUS_DEFAULT_ADDRESS); - if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL) + + if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL) return FALSE; _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ? @@ -310,27 +312,32 @@ ensure_bus_data (DBusConnection *connection) return bd; } -/* internal function that checks to see if this - is a shared connection owned by the bus and if it is unref it */ +/** + * Internal function that checks to see if this + * is a shared connection owned by the bus and if it is unref it. + * + * @param connection a connection that has been disconnected. + */ void -_dbus_bus_check_connection_and_unref_unlocked (DBusConnection *connection) +_dbus_bus_notify_shared_connection_disconnected_unlocked (DBusConnection *connection) { + int i; + _DBUS_LOCK (bus); - if (bus_connections[DBUS_BUS_SYSTEM] == connection) - { - bus_connections[DBUS_BUS_SYSTEM] = NULL; - _dbus_connection_unref_unlocked (connection); - } - else if (bus_connections[DBUS_BUS_SESSION] == connection) - { - bus_connections[DBUS_BUS_SESSION] = NULL; - _dbus_connection_unref_unlocked (connection); - } - else if (bus_connections[DBUS_BUS_STARTER] == connection) + /* We are expecting to have the connection saved in only one of these + * slots, but someone could in a pathological case set system and session + * bus to the same bus or something. Or set one of them to the starter + * bus without setting the starter bus type in the env variable. + * So we don't break the loop as soon as we find a match. + */ + for (i = 0; i < N_BUS_TYPES; ++i) { - bus_connections[DBUS_BUS_STARTER] = NULL; - _dbus_connection_unref_unlocked (connection); + if (bus_connections[i] == connection) + { + bus_connections[i] = NULL; + _dbus_connection_unref_unlocked (connection); + } } _DBUS_UNLOCK (bus); @@ -392,7 +399,7 @@ internal_bus_get (DBusBusType type, } if (private) - connection = dbus_connection_open_private(address, error); + connection = dbus_connection_open_private (address, error); else connection = dbus_connection_open (address, error); @@ -412,7 +419,7 @@ internal_bus_get (DBusBusType type, if (!dbus_bus_register (connection, error)) { _DBUS_ASSERT_ERROR_IS_SET (error); - _dbus_connection_close_internal (connection); + _dbus_connection_close_possibly_shared (connection); dbus_connection_unref (connection); _DBUS_UNLOCK (bus); @@ -432,6 +439,8 @@ internal_bus_get (DBusBusType type, bd->is_well_known = TRUE; _DBUS_UNLOCK (bus); + + /* Return a reference to the caller */ return connection; } @@ -446,11 +455,22 @@ internal_bus_get (DBusBusType type, /** * Connects to a bus daemon and registers the client with it. If a * connection to the bus already exists, then that connection is - * returned. Caller owns a reference to the bus. + * returned. The caller of this function owns a reference to the bus. + * + * The caller may NOT call dbus_connection_close() on this connection; + * see dbus_connection_open() and dbus_connection_close() for details + * on that. * + * If this function obtains a new connection object never before + * returned from dbus_bus_get(), it will call + * dbus_connection_set_exit_on_disconnect(), so the application + * will exit if the connection closes. You can undo this + * by calling dbus_connection_set_exit_on_disconnect() yourself + * after you get the connection. + * * @param type bus type * @param error address where an error can be returned. - * @returns a DBusConnection with new ref + * @returns a #DBusConnection with new ref */ DBusConnection * dbus_bus_get (DBusBusType type, @@ -460,10 +480,20 @@ dbus_bus_get (DBusBusType type, } /** - * Connects to a bus daemon and registers the client with it. Unlike - * dbus_bus_get(), always creates a new connection. This connection + * Connects to a bus daemon and registers the client with it as with dbus_bus_register(). + * Unlike dbus_bus_get(), always creates a new connection. This connection * will not be saved or recycled by libdbus. Caller owns a reference - * to the bus. + * to the bus and must either close it or know it to be closed + * prior to releasing this reference. + * + * See dbus_connection_open_private() for more details on when to + * close and unref this connection. + * + * This function calls + * dbus_connection_set_exit_on_disconnect() on the new connection, so the application + * will exit if the connection closes. You can undo this + * by calling dbus_connection_set_exit_on_disconnect() yourself + * after you get the connection. * * @param type bus type * @param error address where an error can be returned. @@ -481,6 +511,9 @@ dbus_bus_get_private (DBusBusType type, * thing an application does when connecting to the message bus. * If registration succeeds, the unique name will be set, * and can be obtained using dbus_bus_get_unique_name(). + * + * If you use dbus_bus_get() or dbus_bus_get_private() this + * function will be called for you. * * @param connection the connection * @param error place to store errors -- cgit