+dbus_bool_t
+bus_service_swap_owner (BusService *service,
+ DBusConnection *connection,
+ BusTransaction *transaction,
+ DBusError *error)
+{
+ DBusList *swap_link;
+ BusOwner *primary_owner;
+
+ _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+
+ /* We send out notifications before we do any work we
+ * might have to undo if the notification-sending failed
+ */
+
+ /* Send service lost message */
+ primary_owner = bus_service_get_primary_owner (service);
+ if (primary_owner == NULL || primary_owner->conn != connection)
+ _dbus_assert_not_reached ("Tried to swap a non primary owner");
+
+
+ if (!bus_driver_send_service_lost (connection, service->name,
+ transaction, error))
+ return FALSE;
+
+ if (service->owners == NULL)
+ {
+ _dbus_assert_not_reached ("Tried to swap owner of a service that has no owners");
+ }
+ else if (_dbus_list_length_is_one (&service->owners))
+ {
+ _dbus_assert_not_reached ("Tried to swap owner of a service that has no other owners in the queue");
+ }
+ else
+ {
+ DBusList *link;
+ BusOwner *new_owner;
+ DBusConnection *new_owner_conn;
+ link = _dbus_list_get_first_link (&service->owners);
+ _dbus_assert (link != NULL);
+ link = _dbus_list_get_next_link (&service->owners, link);
+ _dbus_assert (link != NULL);
+
+ new_owner = (BusOwner *)link->data;
+ new_owner_conn = new_owner->conn;
+
+ if (!bus_driver_send_service_owner_changed (service->name,
+ bus_connection_get_name (connection),
+ bus_connection_get_name (new_owner_conn),
+ transaction, error))
+ return FALSE;
+
+ /* This will be our new owner */
+ if (!bus_driver_send_service_acquired (new_owner_conn,
+ service->name,
+ transaction,
+ error))
+ return FALSE;
+ }
+
+ if (!add_restore_ownership_to_transaction (transaction, service, primary_owner))
+ {
+ BUS_SET_OOM (error);
+ return FALSE;
+ }
+
+ /* unlink the primary and make it the second link */
+ swap_link = _dbus_list_get_first_link (&service->owners);
+ _dbus_list_unlink (&service->owners, swap_link);
+
+ _dbus_list_insert_after_link (&service->owners,
+ _dbus_list_get_first_link (&service->owners),
+ swap_link);
+
+ return TRUE;
+}
+