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 2001–3000 of 4,512

        if (!m->sender && b->patch_sender) {
                r = sd_bus_message_set_sender(m, b->patch_sender);
                if (r < 0)
                        return r;
        }
 
        r = next_cookie(b);
        if (r < 0)
                return r;
 
        return sd_bus_message_seal(m, b->cookie, timeout);
}
 
static int bus_remarshal_message(sd_bus *b, sd_bus_message **m) {
        bool remarshal = false;
 
        assert(b);
 
        /* wrong packet version */
        if (b->message_version != 0 && b->message_version != (*m)->header->version)
                remarshal = true;
 
        /* wrong packet endianness */
        if (b->message_endian != 0 && b->message_endian != (*m)->header->endian)
                remarshal = true;
 
        return remarshal ? bus_message_remarshal(b, m) : 0;
}
 
int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
        assert(b);
        assert(m);
 
        /* Fake some timestamps, if they were requested, and not
         * already initialized */
        if (b->attach_timestamp) {
                if (m->realtime <= 0)
                        m->realtime = now(CLOCK_REALTIME);
 
                if (m->monotonic <= 0)
                        m->monotonic = now(CLOCK_MONOTONIC);
        }
 
        /* The bus specification says the serial number cannot be 0,
         * hence let's fill something in for synthetic messages. Since
         * synthetic messages might have a fake sender and we don't
         * want to interfere with the real sender's serial numbers we
         * pick a fixed, artificial one. */
        return sd_bus_message_seal(m, UINT32_MAX, 0);
}
 
static int bus_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx) {
        int r;
 
        assert(bus);
        assert(m);
        assert(idx);
 
        r = bus_socket_write_message(bus, m, idx);
        if (r <= 0)
                return r;
 
        if (*idx >= BUS_MESSAGE_SIZE(m))
                log_debug("Sent message type=%s sender=%s destination=%s path=%s interface=%s member=%s"
                          " cookie=%" PRIu64 " reply_cookie=%" PRIu64
                          " signature=%s error-name=%s error-message=%s",
                          bus_message_type_to_string(m->header->type),
                          strna(sd_bus_message_get_sender(m)),
                          strna(sd_bus_message_get_destination(m)),
                          strna(sd_bus_message_get_path(m)),
                          strna(sd_bus_message_get_interface(m)),
                          strna(sd_bus_message_get_member(m)),
                          BUS_MESSAGE_COOKIE(m),
                          m->reply_cookie,
                          strna(m->root_container.signature),
                          strna(m->error.name),
                          strna(m->error.message));
 
        return r;
}
 
static int dispatch_wqueue(sd_bus *bus) {
        int r, ret = 0;
 
        assert(bus);
        assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
 
        while (bus->wqueue_size > 0) {
 
                r = bus_write_message(bus, bus->wqueue[0], &bus->windex);
                if (r < 0)
                        return r;
                if (r == 0)
                        /* Didn't do anything this time */
                        return ret;
                if (bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
                        /* Fully written. Let's drop the entry from
                         * the queue.
                         *
                         * This isn't particularly optimized, but
                         * well, this is supposed to be our worst-case
                         * buffer only, and the socket buffer is
                         * supposed to be our primary buffer, and if
                         * it got full, then all bets are off
                         * anyway. */
 
                        bus->wqueue_size--;
                        bus_message_unref_queued(bus->wqueue[0], bus);
                        memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
                        bus->windex = 0;
 
                        ret = 1;
                }
        }
 
        return ret;
}
 
static int bus_read_message(sd_bus *bus) {
        assert(bus);
 
        return bus_socket_read_message(bus);
}
 
int bus_rqueue_make_room(sd_bus *bus) {
        assert(bus);
 
        if (bus->rqueue_size >= BUS_RQUEUE_MAX)
                return -ENOBUFS;
 
        if (!GREEDY_REALLOC(bus->rqueue, bus->rqueue_size + 1))
                return -ENOMEM;
 
        return 0;
}
 
static void rqueue_drop_one(sd_bus *bus, size_t i) {
        assert(bus);
        assert(i < bus->rqueue_size);
 
        bus_message_unref_queued(bus->rqueue[i], bus);
        memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
        bus->rqueue_size--;
}
 
static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
        int r, ret = 0;
 
        assert(bus);
        assert(m);
        assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
 
        for (;;) {
                if (bus->rqueue_size > 0) {
                        /* Dispatch a queued message */
                        *m = sd_bus_message_ref(bus->rqueue[0]);
                        rqueue_drop_one(bus, 0);
                        return 1;
                }
 
                /* Try to read a new message */
                r = bus_read_message(bus);
                if (r < 0)
                        return r;
                if (r == 0) {
                        *m = NULL;
                        return ret;
                }
 
                ret = 1;
        }
}
 
_public_ int sd_bus_send(sd_bus *bus, sd_bus_message *_m, uint64_t *ret_cookie) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = sd_bus_message_ref(_m);
        int r;
 
        assert_return(m, -EINVAL);
 
        if (bus)
                assert_return(bus = bus_resolve(bus), -ENOPKG);
        else
                assert_return(bus = m->bus, -ENOTCONN);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        if (!BUS_IS_OPEN(bus->state))
                return -ENOTCONN;
 
        if (m->n_fds > 0) {
                r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
                if (r < 0)
                        return r;
                if (r == 0)
                        return -EOPNOTSUPP;
        }
 
        /* If the cookie number isn't kept, then we know that no reply
         * is expected */
        if (!ret_cookie && !m->sealed)
                m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
 
        r = bus_seal_message(bus, m, 0);
        if (r < 0)
                return r;
 
        /* Remarshall if we have to. This will possibly unref the
         * message and place a replacement in m */
        r = bus_remarshal_message(bus, &m);
        if (r < 0)
                return r;
 
        /* If this is a reply and no reply was requested, then let's
         * suppress this, if we can */
        if (m->dont_send)
                goto finish;
 
        if (IN_SET(bus->state, BUS_RUNNING, BUS_HELLO) && bus->wqueue_size <= 0) {
                size_t idx = 0;
 
                r = bus_write_message(bus, m, &idx);
                if (ERRNO_IS_NEG_DISCONNECT(r)) {
                        bus_enter_closing(bus, r);
                        return -ECONNRESET;
                }
                if (r < 0)
                        return r;
 
                if (idx < BUS_MESSAGE_SIZE(m))  {
                        /* Wasn't fully written. So let's remember how
                         * much was written. Note that the first entry
                         * of the wqueue array is always allocated so
                         * that we always can remember how much was
                         * written. */
                        bus->wqueue[0] = bus_message_ref_queued(m, bus);
                        bus->wqueue_size = 1;
                        bus->windex = idx;
                }
 
        } else {
                /* Just append it to the queue. */
 
                if (bus->wqueue_size >= BUS_WQUEUE_MAX)
                        return -ENOBUFS;
 
                if (!GREEDY_REALLOC(bus->wqueue, bus->wqueue_size + 1))
                        return -ENOMEM;
 
                bus->wqueue[bus->wqueue_size++] = bus_message_ref_queued(m, bus);
        }
 
finish:
        if (ret_cookie)
                *ret_cookie = BUS_MESSAGE_COOKIE(m);
 
        return 1;
}
 
_public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *ret_cookie) {
        int r;
 
        assert_return(m, -EINVAL);
 
        if (bus)
                assert_return(bus = bus_resolve(bus), -ENOPKG);
        else
                assert_return(bus = m->bus, -ENOTCONN);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        if (!BUS_IS_OPEN(bus->state))
                return -ENOTCONN;
 
        if (!streq_ptr(m->destination, destination)) {
 
                if (!destination)
                        return -EEXIST;
 
                r = sd_bus_message_set_destination(m, destination);
                if (r < 0)
                        return r;
        }
 
        return sd_bus_send(bus, m, ret_cookie);
}
 
static usec_t calc_elapse(sd_bus *bus, uint64_t usec) {
        assert(bus);
 
        assert_cc(sizeof(usec_t) == sizeof(uint64_t));
 
        if (usec == USEC_INFINITY)
                return 0;
 
        /* We start all timeouts the instant we enter BUS_HELLO/BUS_RUNNING state, so that the don't run in parallel
         * with any connection setup states. Hence, if a method callback is started earlier than that we just store the
         * relative timestamp, and afterwards the absolute one. */
 
        if (IN_SET(bus->state, BUS_WATCH_BIND, BUS_OPENING, BUS_AUTHENTICATING))
                return usec;
        else
                return usec_add(now(CLOCK_MONOTONIC), usec);
}
 
static int timeout_compare(const void *a, const void *b) {
        const BusReplyCallback *x = a, *y = b;
 
        if (x->timeout_usec != 0 && y->timeout_usec == 0)
                return -1;
 
        if (x->timeout_usec == 0 && y->timeout_usec != 0)
                return 1;
 
        return CMP(x->timeout_usec, y->timeout_usec);
}
 
_public_ int sd_bus_call_async(
                sd_bus *bus,
                sd_bus_slot **ret_slot,
                sd_bus_message *m,
                sd_bus_message_handler_t callback,
                void *userdata,
                uint64_t usec) {
 
        _unused_ _cleanup_(sd_bus_message_unrefp) sd_bus_message *m_unref = sd_bus_message_ref(m);
        _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *s = NULL;
        int r;
 
        assert_return(m, -EINVAL);
        assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
        assert_return(!m->sealed || (!!callback == !(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)), -EINVAL);
 
        if (bus)
                assert_return(bus = bus_resolve(bus), -ENOPKG);
        else
                assert_return(bus = m->bus, -ENOTCONN);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        if (!BUS_IS_OPEN(bus->state))
                return -ENOTCONN;
 
        /* If no callback is specified and there's no interest in a slot, then there's no reason to ask for a reply */
        if (!callback && !ret_slot && !m->sealed)
                m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
 
        r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
        if (r < 0)
                return r;
 
        r = bus_seal_message(bus, m, usec);
        if (r < 0)
                return r;
 
        r = bus_remarshal_message(bus, &m);
        if (r < 0)
                return r;
 
        if (ret_slot || callback) {
                s = bus_slot_allocate(bus, !ret_slot, BUS_REPLY_CALLBACK, sizeof(BusReplyCallback), userdata);
                if (!s)
                        return -ENOMEM;
 
                s->reply_callback.callback = callback;
 
                s->reply_callback.cookie = BUS_MESSAGE_COOKIE(m);
                r = ordered_hashmap_ensure_put(&bus->reply_callbacks, &uint64_hash_ops_value_free, &s->reply_callback.cookie, &s->reply_callback);
                if (r < 0) {
                        s->reply_callback.cookie = 0;
                        return r;
                }
 
                s->reply_callback.timeout_usec = calc_elapse(bus, m->timeout);
                if (s->reply_callback.timeout_usec != 0) {
                        r = prioq_put(bus->reply_callbacks_prioq, &s->reply_callback, &s->reply_callback.prioq_idx);
                        if (r < 0) {
                                s->reply_callback.timeout_usec = 0;
                                return r;
                        }
                }
        }
 
        r = sd_bus_send(bus, m, s ? &s->reply_callback.cookie : NULL);
        if (r < 0)
                return r;
 
        if (ret_slot)
                *ret_slot = s;
        s = NULL;
 
        return r;
}
 
int bus_ensure_running(sd_bus *bus) {
        int r;
 
        assert(bus);
 
        if (bus->state == BUS_RUNNING)
                return 1;
 
        for (;;) {
                if (IN_SET(bus->state, BUS_UNSET, BUS_CLOSED, BUS_CLOSING))
                        return -ENOTCONN;
 
                r = sd_bus_process(bus, NULL);
                if (r < 0)
                        return r;
                if (bus->state == BUS_RUNNING)
                        return 1;
                if (r > 0)
                        continue;
 
                r = sd_bus_wait(bus, UINT64_MAX);
                if (r < 0)
                        return r;
        }
}
 
_public_ int sd_bus_call(
                sd_bus *bus,
                sd_bus_message *_m,
                uint64_t usec,
                sd_bus_error *reterr_error,
                sd_bus_message **ret_reply) {
 
        usec_t timeout;
        uint64_t cookie;
        size_t i;
        int r;
 
        bus_assert_return(_m, -EINVAL, reterr_error);
        bus_assert_return(_m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL, reterr_error);
        bus_assert_return(!(_m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL, reterr_error);
        bus_assert_return(!bus_error_is_dirty(reterr_error), -EINVAL, reterr_error);
 
        if (bus)
                assert_return(bus = bus_resolve(bus), -ENOPKG);
        else
                assert_return(bus = _m->bus, -ENOTCONN);
        bus_assert_return(!bus_origin_changed(bus), -ECHILD, reterr_error);
 
        /* If the current fiber and the bus share their event loop, we can use sd_bus_call_suspend()
         * instead which does an async method call. This allows multiple invocations of sd_bus_call() to
         * happen across multiple fibers at once. */
        if (sd_fiber_is_running() && bus->event == sd_fiber_get_event())
                return bus_call_suspend(bus, _m, usec, reterr_error, ret_reply);
 
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = sd_bus_message_ref(_m);
 
        if (!BUS_IS_OPEN(bus->state)) {
                r = -ENOTCONN;
                goto fail;
        }
 
        r = bus_ensure_running(bus);
        if (r < 0)
                goto fail;
 
        i = bus->rqueue_size;
 
        r = bus_seal_message(bus, m, usec);
        if (r < 0)
                goto fail;
 
        r = bus_remarshal_message(bus, &m);
        if (r < 0)
                goto fail;
 
        r = sd_bus_send(bus, m, &cookie);
        if (r < 0)
                goto fail;
 
        timeout = calc_elapse(bus, m->timeout);
 
        for (;;) {
                usec_t left;
 
                while (i < bus->rqueue_size) {
                        _cleanup_(sd_bus_message_unrefp) sd_bus_message *incoming = NULL;
 
                        incoming = sd_bus_message_ref(bus->rqueue[i]);
 
                        if (incoming->reply_cookie == cookie) {
                                /* Found a match! */
 
                                rqueue_drop_one(bus, i);
                                log_debug_bus_message(incoming);
 
                                if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
 
                                        if (incoming->n_fds <= 0 || bus->accept_fd) {
                                                if (ret_reply)
                                                        *ret_reply = TAKE_PTR(incoming);
 
                                                return 1;
                                        }
 
                                        return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INCONSISTENT_MESSAGE,
                                                                "Reply message contained file descriptors which I couldn't accept. Sorry.");
 
                                } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
                                        return sd_bus_error_copy(reterr_error, &incoming->error);
                                else {
                                        r = -EIO;
                                        goto fail;
                                }
 
                        } else if (BUS_MESSAGE_COOKIE(incoming) == cookie &&
                                   bus->unique_name &&
                                   incoming->sender &&
                                   streq(bus->unique_name, incoming->sender)) {
 
                                rqueue_drop_one(bus, i);
 
                                /* Our own message? Somebody is trying to send its own client a message,
                                 * let's not dead-lock, let's fail immediately. */
 
                                r = -ELOOP;
                                goto fail;
                        }
 
                        /* Try to read more, right-away */
                        i++;
                }
 
                r = bus_read_message(bus);
                if (r < 0) {
                        if (ERRNO_IS_DISCONNECT(r)) {
                                bus_enter_closing(bus, r);
                                r = -ECONNRESET;
                        }
 
                        goto fail;
                }
                if (r > 0)
                        continue;
 
                if (timeout > 0) {
                        usec_t n;
 
                        n = now(CLOCK_MONOTONIC);
                        if (n >= timeout) {
                                r = -ETIMEDOUT;
                                goto fail;
                        }
 
                        left = timeout - n;
                } else
                        left = UINT64_MAX;
 
                r = bus_poll(bus, true, left);
                if (ERRNO_IS_NEG_TRANSIENT(r))
                        continue;
                if (r < 0)
                        goto fail;
                if (r == 0) {
                        r = -ETIMEDOUT;
                        goto fail;
                }
 
                r = dispatch_wqueue(bus);
                if (r < 0) {
                        if (ERRNO_IS_DISCONNECT(r)) {
                                bus_enter_closing(bus, r);
                                r = -ECONNRESET;
                        }
 
                        goto fail;
                }
        }
 
fail:
        return sd_bus_error_set_errno(reterr_error, r);
}
 
_public_ int sd_bus_get_fd(sd_bus *bus) {
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(bus->input_fd == bus->output_fd, -EPERM);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        if (bus->state == BUS_CLOSED)
                return -ENOTCONN;
 
        if (bus->inotify_fd >= 0)
                return bus->inotify_fd;
 
        if (bus->input_fd >= 0)
                return bus->input_fd;
 
        return -ENOTCONN;
}
 
_public_ int sd_bus_get_events(sd_bus *bus) {
        int flags = 0;
 
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        switch (bus->state) {
 
        case BUS_UNSET:
        case BUS_CLOSED:
                return -ENOTCONN;
 
        case BUS_WATCH_BIND:
                flags |= POLLIN;
                break;
 
        case BUS_OPENING:
                flags |= POLLOUT;
                break;
 
        case BUS_AUTHENTICATING:
                if (bus_socket_auth_needs_write(bus))
                        flags |= POLLOUT;
 
                flags |= POLLIN;
                break;
 
        case BUS_RUNNING:
        case BUS_HELLO:
                if (bus->rqueue_size <= 0)
                        flags |= POLLIN;
                if (bus->wqueue_size > 0)
                        flags |= POLLOUT;
                break;
 
        case BUS_CLOSING:
                break;
 
        default:
                assert_not_reached();
        }
 
        return flags;
}
 
_public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *ret) {
        BusReplyCallback *c;
 
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(ret, -EINVAL);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        if (!BUS_IS_OPEN(bus->state) && bus->state != BUS_CLOSING)
                return -ENOTCONN;
 
        if (bus->track_queue) {
                *ret = 0;
                return 1;
        }
 
        switch (bus->state) {
 
        case BUS_AUTHENTICATING:
                *ret = bus->auth_timeout;
                return 1;
 
        case BUS_RUNNING:
        case BUS_HELLO:
                if (bus->rqueue_size > 0) {
                        *ret = 0;
                        return 1;
                }
 
                c = prioq_peek(bus->reply_callbacks_prioq);
                if (!c) {
                        *ret = UINT64_MAX;
                        return 0;
                }
 
                if (c->timeout_usec == 0) {
                        *ret = UINT64_MAX;
                        return 0;
                }
 
                *ret = c->timeout_usec;
                return 1;
 
        case BUS_CLOSING:
                *ret = 0;
                return 1;
 
        case BUS_WATCH_BIND:
        case BUS_OPENING:
                *ret = UINT64_MAX;
                return 0;
 
        default:
                assert_not_reached();
        }
}
 
static int process_timeout(sd_bus *bus) {
        _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
        _cleanup_(sd_bus_message_unrefp) sd_bus_message* m = NULL;
        BusReplyCallback *c;
        sd_bus_slot *slot;
        bool is_hello;
        usec_t n;
        int r;
 
        assert(bus);
        assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
 
        c = prioq_peek(bus->reply_callbacks_prioq);
        if (!c)
                return 0;
 
        n = now(CLOCK_MONOTONIC);
        if (c->timeout_usec > n)
                return 0;
 
        r = bus_message_new_synthetic_error(
                        bus,
                        c->cookie,
                        &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
                        &m);
        if (r < 0)
                return r;
 
        m->read_counter = ++bus->read_counter;
 
        r = bus_seal_synthetic_message(bus, m);
        if (r < 0)
                return r;
 
        assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
        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++;
 
        is_hello = bus->state == BUS_HELLO && c->callback == hello_callback;
 
        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);
 
        /* When this is the hello message and it timed out, then make sure to propagate the error up, don't just log
         * and ignore the callback handler's return value. */
        if (is_hello)
                return r;
 
        return bus_maybe_reply_error(m, r, &error_buffer);
}
 
static int process_hello(sd_bus *bus, sd_bus_message *m) {
        assert(bus);
        assert(m);
 
        if (bus->state != BUS_HELLO)
                return 0;
 
        /* Let's make sure the first message on the bus is the HELLO
         * reply. But note that we don't actually parse the message
         * here (we leave that to the usual handling), we just verify
         * we don't let any earlier msg through. */
 
        if (!IN_SET(m->header->type, SD_BUS_MESSAGE_METHOD_RETURN, SD_BUS_MESSAGE_METHOD_ERROR))
                return -EIO;
 
        if (m->reply_cookie != 1)
                return -EIO;
 
        return 0;
}
 
static int process_reply(sd_bus *bus, sd_bus_message *m) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *synthetic_reply = NULL;
        _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
        BusReplyCallback *c;
        sd_bus_slot *slot;
        bool is_hello;
        int r;
 
        assert(bus);
        assert(m);
 
        if (!IN_SET(m->header->type, SD_BUS_MESSAGE_METHOD_RETURN, SD_BUS_MESSAGE_METHOD_ERROR))
                return 0;
 
        if (m->destination && bus->unique_name && !streq_ptr(m->destination, bus->unique_name))
                return 0;
 
        c = ordered_hashmap_remove(bus->reply_callbacks, &m->reply_cookie);
        if (!c)
                return 0;
 
        c->cookie = 0;
 
        slot = container_of(c, sd_bus_slot, reply_callback);
 
        if (m->n_fds > 0 && !bus->accept_fd) {
 
                /* If the reply contained a file descriptor which we
                 * didn't want we pass an error instead. */
 
                r = bus_message_new_synthetic_error(
                                bus,
                                m->reply_cookie,
                                &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptor"),
                                &synthetic_reply);
                if (r < 0)
                        return r;
 
                /* Copy over original timestamp */
                synthetic_reply->realtime = m->realtime;
                synthetic_reply->monotonic = m->monotonic;
                synthetic_reply->seqnum = m->seqnum;
                synthetic_reply->read_counter = m->read_counter;
 
                r = bus_seal_synthetic_message(bus, synthetic_reply);
                if (r < 0)
                        return r;
 
                m = synthetic_reply;
        } else {
                r = sd_bus_message_rewind(m, true);
                if (r < 0)
                        return r;
        }
 
        if (c->timeout_usec != 0) {
                prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
                c->timeout_usec = 0;
        }
 
        is_hello = bus->state == BUS_HELLO && c->callback == hello_callback;
 
        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;
 
        if (slot->floating)
                bus_slot_disconnect(slot, true);
 
        sd_bus_slot_unref(slot);
 
        /* When this is the hello message and it failed, then make sure to propagate the error up, don't just log and
         * ignore the callback handler's return value. */
        if (is_hello)
                return r;
 
        return bus_maybe_reply_error(m, r, &error_buffer);
}
 
static int process_filter(sd_bus *bus, sd_bus_message *m) {
        _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
        int r;
 
        assert(bus);
        assert(m);
 
        do {
                bus->filter_callbacks_modified = false;
 
                LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
                        sd_bus_slot *slot;
 
                        if (bus->filter_callbacks_modified)
                                break;
 
                        /* Don't run this more than once per iteration */
                        if (l->last_iteration == bus->iteration_counter)
                                continue;
 
                        l->last_iteration = bus->iteration_counter;
 
                        r = sd_bus_message_rewind(m, true);
                        if (r < 0)
                                return r;
 
                        slot = container_of(l, sd_bus_slot, filter_callback);
 
                        bus->current_slot = sd_bus_slot_ref(slot);
                        bus->current_handler = l->callback;
                        bus->current_userdata = slot->userdata;
                        r = l->callback(m, slot->userdata, &error_buffer);
                        bus->current_userdata = NULL;
                        bus->current_handler = NULL;
                        bus->current_slot = sd_bus_slot_unref(slot);
 
                        r = bus_maybe_reply_error(m, r, &error_buffer);
                        if (r != 0)
                                return r;
 
                }
 
        } while (bus->filter_callbacks_modified);
 
        return 0;
}
 
static int process_match(sd_bus *bus, sd_bus_message *m) {
        int r;
 
        assert(bus);
        assert(m);
 
        do {
                bus->match_callbacks_modified = false;
 
                r = bus_match_run(bus, &bus->match_callbacks, m);
                if (r != 0)
                        return r;
 
        } while (bus->match_callbacks_modified);
 
        return 0;
}
 
static int process_builtin(sd_bus *bus, sd_bus_message *m) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        int r;
 
        assert(bus);
        assert(m);
 
        if (bus->is_monitor)
                return 0;
 
        if (bus->manual_peer_interface)
                return 0;
 
        if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
                return 0;
 
        if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
                return 0;
 
        if (m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
                return 1;
 
        if (streq_ptr(m->member, "Ping"))
                r = sd_bus_message_new_method_return(m, &reply);
        else if (streq_ptr(m->member, "GetMachineId")) {
                sd_id128_t id;
 
                r = sd_id128_get_machine(&id);
                if (r < 0)
                        return r;
 
                r = sd_bus_message_new_method_return(m, &reply);
                if (r < 0)
                        return r;
 
                r = sd_bus_message_append(reply, "s", SD_ID128_TO_STRING(id));
        } else
                r = sd_bus_message_new_method_errorf(
                                m, &reply,
                                SD_BUS_ERROR_UNKNOWN_METHOD,
                                 "Unknown method '%s' on interface '%s'.", m->member, m->interface);
        if (r < 0)
                return r;
 
        r = sd_bus_send(bus, reply, NULL);
        if (r < 0)
                return r;
 
        return 1;
}
 
static int process_fd_check(sd_bus *bus, sd_bus_message *m) {
        assert(bus);
        assert(m);
 
        /* If we got a message with a file descriptor which we didn't
         * want to accept, then let's drop it. How can this even
         * happen? For example, when the kernel queues a message into
         * an activatable names's queue which allows fds, and then is
         * delivered to us later even though we ourselves did not
         * negotiate it. */
 
        if (bus->is_monitor)
                return 0;
 
        if (m->n_fds <= 0)
                return 0;