-
- HASHMAP_FOREACH(j, m->jobs, i) {
- JobBusClient *cl, *nextcl;
- LIST_FOREACH_SAFE(client, cl, nextcl, j->bus_client_list) {
- if (cl->bus == c) {
- LIST_REMOVE(client, j->bus_client_list, cl);
- free(cl);
- }
- }
- }
-
- set_remove(m->bus_connections, c);
- set_remove(m->bus_connections_for_dispatch, c);
- set_free_free(BUS_CONNECTION_SUBSCRIBED(m, c));
-
- if (m->queued_message_connection == c) {
- m->queued_message_connection = NULL;
-
- if (m->queued_message) {
- dbus_message_unref(m->queued_message);
- m->queued_message = NULL;
- }
- }
-
- dbus_connection_set_dispatch_status_function(c, NULL, NULL, NULL);
- /* system manager cannot afford to block on DBus */
- if (m->running_as != SYSTEMD_SYSTEM)
- dbus_connection_flush(c);
- dbus_connection_close(c);
- dbus_connection_unref(c);
-}
-
-static void bus_done_api(Manager *m) {
- if (!m->api_bus)
- return;
-
- if (m->running_as == SYSTEMD_USER)
- shutdown_connection(m, m->api_bus);
-
- m->api_bus = NULL;
-
- if (m->queued_message) {
- dbus_message_unref(m->queued_message);
- m->queued_message = NULL;
- }
-}
-
-static void bus_done_system(Manager *m) {
- if (!m->system_bus)
- return;
-
- if (m->running_as == SYSTEMD_SYSTEM)
- bus_done_api(m);
-
- shutdown_connection(m, m->system_bus);
- m->system_bus = NULL;
-}
-
-static void bus_done_private(Manager *m) {
- if (!m->private_bus)
- return;
-
- dbus_server_disconnect(m->private_bus);
- dbus_server_unref(m->private_bus);
- m->private_bus = NULL;
-}
-
-void bus_done(Manager *m) {
- DBusConnection *c;
-
- bus_done_api(m);
- bus_done_system(m);
- bus_done_private(m);
-
- while ((c = set_steal_first(m->bus_connections)))
- shutdown_connection(m, c);
-
- while ((c = set_steal_first(m->bus_connections_for_dispatch)))
- shutdown_connection(m, c);
-
- set_free(m->bus_connections);
- set_free(m->bus_connections_for_dispatch);
-
- if (m->name_data_slot >= 0)
- dbus_pending_call_free_data_slot(&m->name_data_slot);
-
- if (m->conn_data_slot >= 0)
- dbus_pending_call_free_data_slot(&m->conn_data_slot);
-
- if (m->subscribed_data_slot >= 0)
- dbus_connection_free_data_slot(&m->subscribed_data_slot);
-}
-
-static void query_pid_pending_cb(DBusPendingCall *pending, void *userdata) {
- Manager *m = userdata;
- DBusMessage *reply;
- DBusError error;
- const char *name;
-
- dbus_error_init(&error);
-
- assert_se(name = BUS_PENDING_CALL_NAME(m, pending));
- assert_se(reply = dbus_pending_call_steal_reply(pending));
-
- switch (dbus_message_get_type(reply)) {
-
- case DBUS_MESSAGE_TYPE_ERROR:
-
- assert_se(dbus_set_error_from_message(&error, reply));
- log_warning("GetConnectionUnixProcessID() failed: %s", bus_error_message(&error));
- break;
-
- case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
- uint32_t r;
-
- if (!dbus_message_get_args(reply,
- &error,
- DBUS_TYPE_UINT32, &r,
- DBUS_TYPE_INVALID)) {
- log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", bus_error_message(&error));
- break;
- }
-
- manager_dispatch_bus_query_pid_done(m, name, (pid_t) r);
- break;
- }
-
- default:
- assert_not_reached("Invalid reply message");
- }
-
- dbus_message_unref(reply);
- dbus_error_free(&error);
-}
-
-int bus_query_pid(Manager *m, const char *name) {
- DBusMessage *message = NULL;
- DBusPendingCall *pending = NULL;
- char *n = NULL;