HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/libsystemd/sd-bus/sd-bus.c

140,786 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 1a6eed2baad85e39cff35c47f1771ee148f511c6

Lines 3001–4000 of 4,512

        if (bus->accept_fd)
                return 0;
 
        if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
                return 1; /* just eat it up */
 
        return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INCONSISTENT_MESSAGE,
                                          "Message contains file descriptors, which I cannot accept. Sorry.");
}
 
static int process_message(sd_bus *bus, sd_bus_message *m) {
        _unused_ _cleanup_(log_context_unrefp) LogContext *c = NULL;
        int r;
 
        assert(bus);
        assert(m);
 
        bus->current_message = m;
        bus->iteration_counter++;
 
        if (log_context_enabled())
                c = log_context_new_strv_consume(bus_message_make_log_fields(m));
 
        log_debug_bus_message(m);
 
        r = process_hello(bus, m);
        if (r != 0)
                goto finish;
 
        r = process_reply(bus, m);
        if (r != 0)
                goto finish;
 
        r = process_fd_check(bus, m);
        if (r != 0)
                goto finish;
 
        r = process_filter(bus, m);
        if (r != 0)
                goto finish;
 
        r = process_match(bus, m);
        if (r != 0)
                goto finish;
 
        r = process_builtin(bus, m);
        if (r != 0)
                goto finish;
 
        r = bus_process_object(bus, m);
 
finish:
        bus->current_message = NULL;
        return r;
}
 
static int dispatch_track(sd_bus *bus) {
        assert(bus);
 
        if (!bus->track_queue)
                return 0;
 
        bus_track_dispatch(bus->track_queue);
        return 1;
}
 
static int process_running(sd_bus *bus, sd_bus_message **ret) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
        int r;
 
        assert(bus);
        assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
 
        r = process_timeout(bus);
        if (r != 0)
                goto null_message;
 
        r = dispatch_wqueue(bus);
        if (r != 0)
                goto null_message;
 
        r = dispatch_track(bus);
        if (r != 0)
                goto null_message;
 
        r = dispatch_rqueue(bus, &m);
        if (r < 0)
                return r;
        if (!m)
                goto null_message;
 
        r = process_message(bus, m);
        if (r != 0)
                goto null_message;
 
        if (ret) {
                r = sd_bus_message_rewind(m, true);
                if (r < 0)
                        return r;
 
                *ret = TAKE_PTR(m);
                return 1;
        }
 
        if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
 
                log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s",
                          strna(sd_bus_message_get_sender(m)),
                          strna(sd_bus_message_get_path(m)),
                          strna(sd_bus_message_get_interface(m)),
                          strna(sd_bus_message_get_member(m)));
 
                r = sd_bus_reply_method_errorf(
                                m,
                                SD_BUS_ERROR_UNKNOWN_OBJECT,
                                "Unknown object '%s'.", m->path);
                if (r < 0)
                        return r;
        }
 
        return 1;
 
null_message:
        if (r >= 0 && ret)
                *ret = NULL;
 
        return r;
}
 
static int bus_exit_now(sd_bus *bus, sd_event *event) {
        assert(bus);
 
        /* Exit due to close, if this is requested. If this is bus object is attached to an event source, invokes
         * sd_event_exit(), otherwise invokes libc exit(). */
 
        if (bus->exited) /* did we already exit? */
                return 0;
        if (!bus->exit_triggered) /* was the exit condition triggered? */
                return 0;
        if (!bus->exit_on_disconnect) /* Shall we actually exit on disconnection? */
                return 0;
 
        bus->exited = true; /* never exit more than once */
 
        log_debug("Bus connection disconnected, exiting.");
 
        if (!event)
                event = bus->event;
 
        if (event)
                return sd_event_exit(event, bus->exit_code);
 
        exit(EXIT_FAILURE);
        assert_not_reached();
}
 
static int process_closing_reply_callback(sd_bus *bus, BusReplyCallback *c) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL, error_buffer = SD_BUS_ERROR_NULL;
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
        sd_bus_slot *slot;
        int r;
 
        assert(bus);
        assert(c);
 
        (void) sd_bus_error_set_errnof(
                        &error,
                        bus->exit_code < 0 ? bus->exit_code : -ETIMEDOUT,
                        "Connection terminated");
 
        r = bus_message_new_synthetic_error(bus, c->cookie, &error, &m);
        if (r < 0)
                return r;
 
        m->read_counter = ++bus->read_counter;
 
        r = bus_seal_synthetic_message(bus, m);
        if (r < 0)
                return r;
 
        if (c->timeout_usec != 0) {
                prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
                c->timeout_usec = 0;
        }
 
        ordered_hashmap_remove(bus->reply_callbacks, &c->cookie);
        c->cookie = 0;
 
        slot = container_of(c, sd_bus_slot, reply_callback);
 
        bus->iteration_counter++;
 
        bus->current_message = m;
        bus->current_slot = sd_bus_slot_ref(slot);
        bus->current_handler = c->callback;
        bus->current_userdata = slot->userdata;
        r = c->callback(m, slot->userdata, &error_buffer);
        bus->current_userdata = NULL;
        bus->current_handler = NULL;
        bus->current_slot = NULL;
        bus->current_message = NULL;
 
        if (slot->floating)
                bus_slot_disconnect(slot, true);
 
        sd_bus_slot_unref(slot);
 
        return bus_maybe_reply_error(m, r, &error_buffer);
}
 
static int process_closing(sd_bus *bus, sd_bus_message **ret) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
        BusReplyCallback *c;
        int r;
 
        assert(bus);
        assert(bus->state == BUS_CLOSING);
 
        /* Wait for any still-running fiber method handlers to finish unwinding their cancellation
         * before tearing down the rest of the bus. bus_enter_closing() scheduled the cancel; each
         * fiber resolves asynchronously and bus_fiber_resolved() removes it from the set. Returning
         * 1 here keeps the bus in CLOSING state so the event loop drives the fibers to completion. */
        if (!set_isempty(bus->fiber_futures))
                return 1;
 
        /* Then, fail all outstanding method calls */
        c = ordered_hashmap_first(bus->reply_callbacks);
        if (c)
                return process_closing_reply_callback(bus, c);
 
        /* Then, fake-drop all remaining bus tracking references */
        if (bus->tracks) {
                bus_track_close(bus->tracks);
                return 1;
        }
 
        /* Then, synthesize a Disconnected message */
        r = sd_bus_message_new_signal(
                        bus,
                        &m,
                        "/org/freedesktop/DBus/Local",
                        "org.freedesktop.DBus.Local",
                        "Disconnected");
        if (r < 0)
                return r;
 
        bus_message_set_sender_local(bus, m);
        m->read_counter = ++bus->read_counter;
 
        r = bus_seal_synthetic_message(bus, m);
        if (r < 0)
                return r;
 
        /* sd_bus_close() will deref the event and set bus->event to NULL. But in bus_exit_now() we use
         * bus->event to decide whether to return from the event loop or exit(), but given it's always NULL
         * at that point, it always exit(). Ref it here and pass it through further down to avoid that. */
        event = sd_event_ref(bus->event);
        sd_bus_close(bus);
 
        bus->current_message = m;
        bus->iteration_counter++;
 
        r = process_filter(bus, m);
        if (r != 0)
                goto finish;
 
        r = process_match(bus, m);
        if (r != 0)
                goto finish;
 
        /* Nothing else to do, exit now, if the condition holds */
        bus->exit_triggered = true;
        (void) bus_exit_now(bus, event);
 
        if (ret)
                *ret = TAKE_PTR(m);
 
        r = 1;
 
finish:
        bus->current_message = NULL;
 
        return r;
}
 
static int bus_process_internal(sd_bus *bus, sd_bus_message **ret) {
        int r;
 
        /* Returns 0 when we didn't do anything. This should cause the
         * caller to invoke sd_bus_wait() before returning the next
         * time. Returns > 0 when we did something, which possibly
         * means *ret is filled in with an unprocessed message. */
 
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        /* We don't allow recursively invoking sd_bus_process(). */
        assert_return(!bus->current_message, -EBUSY);
        assert(!bus->current_slot); /* This should be NULL whenever bus->current_message is */
 
        BUS_DONT_DESTROY(bus);
 
        switch (bus->state) {
 
        case BUS_UNSET:
                return -ENOTCONN;
 
        case BUS_CLOSED:
                return -ECONNRESET;
 
        case BUS_WATCH_BIND:
                r = bus_socket_process_watch_bind(bus);
                break;
 
        case BUS_OPENING:
                r = bus_socket_process_opening(bus);
                break;
 
        case BUS_AUTHENTICATING:
                r = bus_socket_process_authenticating(bus);
                break;
 
        case BUS_RUNNING:
        case BUS_HELLO:
                r = process_running(bus, ret);
                if (r >= 0)
                        return r;
 
                /* This branch initializes *ret, hence we don't use the generic error checking below */
                break;
 
        case BUS_CLOSING:
                return process_closing(bus, ret);
 
        default:
                assert_not_reached();
        }
 
        if (ERRNO_IS_NEG_DISCONNECT(r)) {
                bus_enter_closing(bus, r);
                r = 1;
        } else if (r < 0)
                return r;
 
        if (ret)
                *ret = NULL;
 
        return r;
}
 
_public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
        return bus_process_internal(bus, ret);
}
 
_public_ int sd_bus_process_priority(sd_bus *bus, int64_t priority, sd_bus_message **ret) {
        return bus_process_internal(bus, ret);
}
 
static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
        struct pollfd p[2] = {};
        usec_t m = USEC_INFINITY;
        int r, n;
 
        assert(bus);
 
        if (bus->state == BUS_CLOSING)
                return 1;
 
        if (!BUS_IS_OPEN(bus->state))
                return -ENOTCONN;
 
        if (bus->state == BUS_WATCH_BIND) {
                assert(bus->inotify_fd >= 0);
 
                p[0].events = POLLIN;
                p[0].fd = bus->inotify_fd;
                n = 1;
        } else {
                int e;
 
                e = sd_bus_get_events(bus);
                if (e < 0)
                        return e;
 
                if (need_more)
                        /* The caller really needs some more data, they don't
                         * care about what's already read, or any timeouts
                         * except its own. */
                        e |= POLLIN;
                else {
                        usec_t until;
                        /* The caller wants to process if there's something to
                         * process, but doesn't care otherwise */
 
                        r = sd_bus_get_timeout(bus, &until);
                        if (r < 0)
                                return r;
                        if (r > 0)
                                m = usec_sub_unsigned(until, now(CLOCK_MONOTONIC));
                }
 
                p[0].fd = bus->input_fd;
                if (bus->output_fd == bus->input_fd) {
                        p[0].events = e;
                        n = 1;
                } else {
                        p[0].events = e & POLLIN;
                        p[1].fd = bus->output_fd;
                        p[1].events = e & POLLOUT;
                        n = 2;
                }
        }
 
        if (timeout_usec != UINT64_MAX && (m == USEC_INFINITY || timeout_usec < m))
                m = timeout_usec;
 
        r = ppoll_usec(p, n, m);
        if (r <= 0)
                return r;
 
        return 1;
}
 
_public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
        int r;
 
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        if (bus->state == BUS_CLOSING)
                return 0;
 
        if (!BUS_IS_OPEN(bus->state))
                return -ENOTCONN;
 
        if (bus->rqueue_size > 0)
                return 0;
 
        r = bus_poll(bus, false, timeout_usec);
        if (ERRNO_IS_NEG_TRANSIENT(r))
                return 1; /* treat EINTR as success, but let's exit, so that the caller will call back into us soon. */
 
        return r;
}
 
_public_ int sd_bus_flush(sd_bus *bus) {
        int r;
 
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        if (bus->state == BUS_CLOSING)
                return 0;
 
        if (!BUS_IS_OPEN(bus->state))
                return -ENOTCONN;
 
        /* We never were connected? Don't hang in inotify for good, as there's no timeout set for it */
        if (bus->state == BUS_WATCH_BIND)
                return -EUNATCH;
 
        r = bus_ensure_running(bus);
        if (r < 0)
                return r;
 
        if (bus->wqueue_size <= 0)
                return 0;
 
        for (;;) {
                r = dispatch_wqueue(bus);
                if (ERRNO_IS_NEG_DISCONNECT(r)) {
                        bus_enter_closing(bus, r);
                        return -ECONNRESET;
                } else if (r < 0)
                        return r;
 
                if (bus->wqueue_size <= 0)
                        return 0;
 
                r = bus_poll(bus, false, UINT64_MAX);
                if (ERRNO_IS_NEG_TRANSIENT(r))
                        continue;
                if (r < 0)
                        return r;
        }
}
 
_public_ int sd_bus_add_filter(
                sd_bus *bus,
                sd_bus_slot **ret_slot,
                sd_bus_message_handler_t callback,
                void *userdata) {
 
        sd_bus_slot *s;
 
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(callback, -EINVAL);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        s = bus_slot_allocate(bus, !ret_slot, BUS_FILTER_CALLBACK, sizeof(BusFilterCallback), userdata);
        if (!s)
                return -ENOMEM;
 
        s->filter_callback.callback = callback;
 
        bus->filter_callbacks_modified = true;
        LIST_PREPEND(callbacks, bus->filter_callbacks, &s->filter_callback);
 
        if (ret_slot)
                *ret_slot = s;
 
        return 0;
}
 
static int add_match_callback(
                sd_bus_message *m,
                void *userdata,
                sd_bus_error *ret_error) {
 
        sd_bus_slot *match_slot = ASSERT_PTR(userdata);
        bool failed = false;
        int r = 0;
 
        assert(m);
 
        sd_bus_slot_ref(match_slot);
 
        if (sd_bus_message_is_method_error(m, NULL)) {
                const sd_bus_error *e = ASSERT_PTR(sd_bus_message_get_error(m));
                r = sd_bus_error_get_errno(e);
 
                r = log_debug_errno(r,
                                    "Unable to add match %s, failing connection: %s",
                                    match_slot->match_callback.match_string,
                                    bus_error_message(e, r));
 
                failed = true;
        } else
                log_debug("Match %s successfully installed.", match_slot->match_callback.match_string);
 
        if (match_slot->match_callback.install_callback) {
                sd_bus *bus;
 
                bus = sd_bus_message_get_bus(m);
 
                /* This function has been called as slot handler, and we want to call another slot handler. Let's
                 * update the slot callback metadata temporarily with our own data, and then revert back to the old
                 * values. */
 
                assert(bus->current_slot == match_slot->match_callback.install_slot);
                assert(bus->current_handler == add_match_callback);
                assert(bus->current_userdata == userdata);
 
                bus->current_slot = match_slot;
                bus->current_handler = match_slot->match_callback.install_callback;
                bus->current_userdata = match_slot->userdata;
 
                r = match_slot->match_callback.install_callback(m, match_slot->userdata, ret_error);
 
                bus->current_slot = match_slot->match_callback.install_slot;
                bus->current_handler = add_match_callback;
                bus->current_userdata = userdata;
        } else {
                if (failed) /* Generic failure handling: destroy the connection */
                        bus_enter_closing(sd_bus_message_get_bus(m), r);
 
                r = 1;
        }
 
        /* We don't need the install method reply slot anymore, let's free it */
        match_slot->match_callback.install_slot = sd_bus_slot_unref(match_slot->match_callback.install_slot);
 
        if (failed && match_slot->floating)
                bus_slot_disconnect(match_slot, true);
 
        sd_bus_slot_unref(match_slot);
 
        return r;
}
 
int bus_add_match_full(
                sd_bus *bus,
                sd_bus_slot **ret_slot,
                bool asynchronous,
                const char *match,
                sd_bus_message_handler_t callback,
                sd_bus_message_handler_t install_callback,
                void *userdata,
                uint64_t timeout_usec) {
 
        BusMatchComponent *components = NULL;
        size_t n_components = 0;
        _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *s = NULL;
        int r;
 
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(match, -EINVAL);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        CLEANUP_ARRAY(components, n_components, bus_match_parse_free);
 
        r = bus_match_parse(match, &components, &n_components);
        if (r < 0)
                return r;
 
        s = bus_slot_allocate(bus, !ret_slot, BUS_MATCH_CALLBACK, sizeof(BusMatchCallback), userdata);
        if (!s)
                return -ENOMEM;
 
        s->match_callback.callback = callback;
        s->match_callback.install_callback = install_callback;
 
        if (bus->bus_client) {
                BusMatchScope scope;
 
                scope = bus_match_get_scope(components, n_components);
 
                /* Do not install server-side matches for matches against the local service, interface or bus path. */
                if (scope != BUS_MATCH_LOCAL) {
 
                        /* We store the original match string, so that we can use it to remove the match again. */
 
                        s->match_callback.match_string = strdup(match);
                        if (!s->match_callback.match_string)
                                return -ENOMEM;
 
                        if (asynchronous) {
                                r = bus_add_match_internal_async(bus,
                                                                 &s->match_callback.install_slot,
                                                                 s->match_callback.match_string,
                                                                 add_match_callback,
                                                                 s,
                                                                 timeout_usec);
                                if (r < 0)
                                        return r;
 
                                /* Make the slot of the match call floating now. We need the reference, but we don't
                                 * want that this match pins the bus object, hence we first create it non-floating, but
                                 * then make it floating. */
                                r = sd_bus_slot_set_floating(s->match_callback.install_slot, true);
                        } else
                                r = bus_add_match_internal(bus,
                                                s->match_callback.match_string,
                                                timeout_usec,
                                                &s->match_callback.after);
                        if (r < 0)
                                return r;
 
                        s->match_added = true;
                }
        }
 
        bus->match_callbacks_modified = true;
        r = bus_match_add(&bus->match_callbacks, components, n_components, &s->match_callback);
        if (r < 0)
                return r;
 
        if (ret_slot)
                *ret_slot = s;
        s = NULL;
 
        return 0;
}
 
_public_ int sd_bus_add_match(
                sd_bus *bus,
                sd_bus_slot **ret_slot,
                const char *match,
                sd_bus_message_handler_t callback,
                void *userdata) {
 
        return bus_add_match_full(bus, ret_slot, false, match, callback, NULL, userdata, 0);
}
 
_public_ int sd_bus_add_match_async(
                sd_bus *bus,
                sd_bus_slot **ret_slot,
                const char *match,
                sd_bus_message_handler_t callback,
                sd_bus_message_handler_t install_callback,
                void *userdata) {
 
        return bus_add_match_full(bus, ret_slot, true, match, callback, install_callback, userdata, 0);
}
 
static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
        sd_bus *bus = ASSERT_PTR(userdata);
        int r;
 
        /* Note that this is called both on input_fd, output_fd, as well as inotify_fd events */
 
        r = sd_bus_process(bus, NULL);
        if (r < 0) {
                log_debug_errno(r, "Processing of bus failed, closing down: %m");
                bus_enter_closing(bus, r);
        }
 
        return 1;
}
 
static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
        sd_bus *bus = ASSERT_PTR(userdata);
        int r;
 
        r = sd_bus_process(bus, NULL);
        if (r < 0) {
                log_debug_errno(r, "Processing of bus failed, closing down: %m");
                bus_enter_closing(bus, r);
        }
 
        return 1;
}
 
static int prepare_callback(sd_event_source *s, void *userdata) {
        sd_bus *bus = ASSERT_PTR(userdata);
        int r, e;
        usec_t until;
 
        assert(s);
 
        e = sd_bus_get_events(bus);
        if (e < 0) {
                r = e;
                goto fail;
        }
 
        if (bus->output_fd != bus->input_fd) {
 
                r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN);
                if (r < 0)
                        goto fail;
 
                r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT);
        } else
                r = sd_event_source_set_io_events(bus->input_io_event_source, e);
        if (r < 0)
                goto fail;
 
        r = sd_bus_get_timeout(bus, &until);
        if (r < 0)
                goto fail;
        if (r > 0) {
                int j;
 
                j = sd_event_source_set_time(bus->time_event_source, until);
                if (j < 0) {
                        r = j;
                        goto fail;
                }
        }
 
        r = sd_event_source_set_enabled(bus->time_event_source, r > 0 ? SD_EVENT_ONESHOT : SD_EVENT_OFF);
        if (r < 0)
                goto fail;
 
        return 1;
 
fail:
        log_debug_errno(r, "Preparing of bus events failed, closing down: %m");
        bus_enter_closing(bus, r);
 
        return 1;
}
 
static int quit_callback(sd_event_source *event, void *userdata) {
        sd_bus *bus = userdata;
 
        assert(event);
 
        if (bus->close_on_exit) {
                sd_bus_flush(bus);
                sd_bus_close(bus);
        }
 
        return 1;
}
 
int bus_attach_io_events(sd_bus *bus) {
        int r;
 
        assert(bus);
 
        if (bus->input_fd < 0)
                return 0;
 
        if (!bus->event)
                return 0;
 
        if (!bus->input_io_event_source) {
                r = sd_event_add_io(bus->event, &bus->input_io_event_source, bus->input_fd, 0, io_callback, bus);
                if (r < 0)
                        return r;
 
                r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
                if (r < 0)
                        return r;
 
                r = sd_event_source_set_priority(bus->input_io_event_source, bus->event_priority);
                if (r < 0)
                        return r;
 
                r = sd_event_source_set_description(bus->input_io_event_source, "bus-input");
        } else
                r = sd_event_source_set_io_fd(bus->input_io_event_source, bus->input_fd);
 
        if (r < 0)
                return r;
 
        if (bus->output_fd != bus->input_fd) {
                assert(bus->output_fd >= 0);
 
                if (!bus->output_io_event_source) {
                        r = sd_event_add_io(bus->event, &bus->output_io_event_source, bus->output_fd, 0, io_callback, bus);
                        if (r < 0)
                                return r;
 
                        r = sd_event_source_set_priority(bus->output_io_event_source, bus->event_priority);
                        if (r < 0)
                                return r;
 
                        r = sd_event_source_set_description(bus->input_io_event_source, "bus-output");
                } else
                        r = sd_event_source_set_io_fd(bus->output_io_event_source, bus->output_fd);
 
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static void bus_detach_io_events(sd_bus *bus) {
        assert(bus);
 
        bus->input_io_event_source = sd_event_source_disable_unref(bus->input_io_event_source);
        bus->output_io_event_source = sd_event_source_disable_unref(bus->output_io_event_source);
}
 
int bus_attach_inotify_event(sd_bus *bus) {
        int r;
 
        assert(bus);
 
        if (bus->inotify_fd < 0)
                return 0;
 
        if (!bus->event)
                return 0;
 
        if (!bus->inotify_event_source) {
                r = sd_event_add_io(bus->event, &bus->inotify_event_source, bus->inotify_fd, EPOLLIN, io_callback, bus);
                if (r < 0)
                        return r;
 
                r = sd_event_source_set_priority(bus->inotify_event_source, bus->event_priority);
                if (r < 0)
                        return r;
 
                r = sd_event_source_set_description(bus->inotify_event_source, "bus-inotify");
        } else
                r = sd_event_source_set_io_fd(bus->inotify_event_source, bus->inotify_fd);
        if (r < 0)
                return r;
 
        return 0;
}
 
_public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
        int r;
 
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(!bus->event, -EBUSY);
 
        assert(!bus->input_io_event_source);
        assert(!bus->output_io_event_source);
        assert(!bus->time_event_source);
 
        if (event)
                bus->event = sd_event_ref(event);
        else  {
                r = sd_event_default(&bus->event);
                if (r < 0)
                        return r;
        }
 
        bus->event_priority = priority;
 
        r = sd_event_add_time(bus->event, &bus->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, bus);
        if (r < 0)
                goto fail;
 
        r = sd_event_source_set_priority(bus->time_event_source, priority);
        if (r < 0)
                goto fail;
 
        r = sd_event_source_set_description(bus->time_event_source, "bus-time");
        if (r < 0)
                goto fail;
 
        r = sd_event_add_exit(bus->event, &bus->quit_event_source, quit_callback, bus);
        if (r < 0)
                goto fail;
 
        r = sd_event_source_set_description(bus->quit_event_source, "bus-exit");
        if (r < 0)
                goto fail;
 
        r = bus_attach_io_events(bus);
        if (r < 0)
                goto fail;
 
        r = bus_attach_inotify_event(bus);
        if (r < 0)
                goto fail;
 
        return 0;
 
fail:
        sd_bus_detach_event(bus);
        return r;
}
 
_public_ int sd_bus_detach_event(sd_bus *bus) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
 
        if (!bus->event)
                return 0;
 
        bus_detach_io_events(bus);
        bus->inotify_event_source = sd_event_source_disable_unref(bus->inotify_event_source);
        bus->time_event_source = sd_event_source_disable_unref(bus->time_event_source);
        bus->quit_event_source = sd_event_source_disable_unref(bus->quit_event_source);
 
        bus->event = sd_event_unref(bus->event);
        return 1;
}
 
_public_ sd_event* sd_bus_get_event(sd_bus *bus) {
        assert_return(bus = bus_resolve(bus), NULL);
 
        return bus->event;
}
 
_public_ sd_bus_message* sd_bus_get_current_message(sd_bus *bus) {
        assert_return(bus = bus_resolve(bus), NULL);
 
        return bus->current_message;
}
 
_public_ sd_bus_slot* sd_bus_get_current_slot(sd_bus *bus) {
        assert_return(bus = bus_resolve(bus), NULL);
 
        return bus->current_slot;
}
 
_public_ sd_bus_message_handler_t sd_bus_get_current_handler(sd_bus *bus) {
        assert_return(bus = bus_resolve(bus), NULL);
 
        return bus->current_handler;
}
 
_public_ void* sd_bus_get_current_userdata(sd_bus *bus) {
        assert_return(bus = bus_resolve(bus), NULL);
 
        return bus->current_userdata;
}
 
static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
        sd_bus *b = NULL;
        int r;
 
        assert(bus_open);
        assert(default_bus);
 
        if (!ret)
                return !!*default_bus;
 
        if (*default_bus) {
                *ret = sd_bus_ref(*default_bus);
                return 0;
        }
 
        r = bus_open(&b);
        if (r < 0)
                return r;
 
        b->default_bus_ptr = default_bus;
        b->tid = gettid();
        *default_bus = b;
 
        *ret = b;
        return 1;