+static int attach_io_events(sd_bus *b);
+static void detach_io_events(sd_bus *b);
+
+static void bus_close_fds(sd_bus *b) {
+ assert(b);
+
+ detach_io_events(b);
+
+ if (b->input_fd >= 0)
+ close_nointr_nofail(b->input_fd);
+
+ if (b->output_fd >= 0 && b->output_fd != b->input_fd)
+ close_nointr_nofail(b->output_fd);
+
+ b->input_fd = b->output_fd = -1;
+}
+
+static void bus_node_destroy(sd_bus *b, struct node *n) {
+ struct node_callback *c;
+ struct node_vtable *v;
+ struct node_enumerator *e;
+
+ assert(b);
+
+ if (!n)
+ return;
+
+ while (n->child)
+ bus_node_destroy(b, n->child);
+
+ while ((c = n->callbacks)) {
+ LIST_REMOVE(callbacks, n->callbacks, c);
+ free(c);
+ }
+
+ while ((v = n->vtables)) {
+ LIST_REMOVE(vtables, n->vtables, v);
+ free(v->interface);
+ free(v);
+ }
+
+ while ((e = n->enumerators)) {
+ LIST_REMOVE(enumerators, n->enumerators, e);
+ free(e);
+ }
+
+ if (n->parent)
+ LIST_REMOVE(siblings, n->parent->child, n);
+
+ assert_se(hashmap_remove(b->nodes, n->path) == n);
+ free(n->path);
+ free(n);
+}
+
+static void bus_reset_queues(sd_bus *b) {
+ unsigned i;
+
+ assert(b);
+
+ for (i = 0; i < b->rqueue_size; i++)
+ sd_bus_message_unref(b->rqueue[i]);
+ free(b->rqueue);
+
+ for (i = 0; i < b->wqueue_size; i++)
+ sd_bus_message_unref(b->wqueue[i]);
+ free(b->wqueue);
+
+ b->rqueue = b->wqueue = NULL;
+ b->rqueue_size = b->wqueue_size = 0;
+}