HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/libsystemd/sd-bus/bus-objects.c
Git blob: aa371cceca8fed558554de425a5b7afabbd09d5b
Lines 1–1000 of 3,204
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <linux/capability.h>
#include "sd-bus.h"
#include "sd-future.h"
#include "alloc-util.h"
#include "bus-internal.h"
#include "bus-introspect.h"
#include "bus-message.h"
#include "bus-objects.h"
#include "bus-signature.h"
#include "bus-slot.h"
#include "bus-type.h"
#include "ordered-set.h"
#include "set.h"
#include "string-util.h"
#include "strv.h"
static int node_vtable_get_userdata(
sd_bus *bus,
const char *path,
BusNodeVTable *c,
void **userdata,
sd_bus_error *reterr_error) {
sd_bus_slot *s;
void *u, *found_u = NULL;
int r;
assert(bus);
assert(path);
assert(c);
s = container_of(c, sd_bus_slot, node_vtable);
u = s->userdata;
if (c->find) {
bus->current_slot = sd_bus_slot_ref(s);
bus->current_userdata = u;
r = c->find(bus, path, c->interface, u, &found_u, reterr_error);
bus->current_userdata = NULL;
bus->current_slot = sd_bus_slot_unref(s);
if (r < 0)
return r;
if (sd_bus_error_is_set(reterr_error))
return -sd_bus_error_get_errno(reterr_error);
if (r == 0)
return r;
} else
found_u = u;
if (userdata)
*userdata = found_u;
return 1;
}
static void* vtable_method_convert_userdata(const sd_bus_vtable *p, void *u) {
assert(p);
if (!u || FLAGS_SET(p->flags, SD_BUS_VTABLE_ABSOLUTE_OFFSET))
return SIZE_TO_PTR(p->x.method.offset); /* don't add offset on NULL, to make ubsan happy */
return (uint8_t*) u + p->x.method.offset;
}
static void* vtable_property_convert_userdata(const sd_bus_vtable *p, void *u) {
assert(p);
if (!u || FLAGS_SET(p->flags, SD_BUS_VTABLE_ABSOLUTE_OFFSET))
return SIZE_TO_PTR(p->x.property.offset); /* as above */
return (uint8_t*) u + p->x.property.offset;
}
static int vtable_property_get_userdata(
sd_bus *bus,
const char *path,
BusVTableMember *p,
void **userdata,
sd_bus_error *reterr_error) {
void *u;
int r;
assert(bus);
assert(path);
assert(p);
assert(userdata);
r = node_vtable_get_userdata(bus, path, p->parent, &u, reterr_error);
if (r <= 0)
return r;
if (bus->nodes_modified)
return 0;
*userdata = vtable_property_convert_userdata(p->vtable, u);
return 1;
}
static int add_enumerated_to_set(
sd_bus *bus,
const char *prefix,
BusNodeEnumerator *first,
OrderedSet *s,
sd_bus_error *reterr_error) {
int r;
assert(bus);
assert(prefix);
assert(s);
LIST_FOREACH(enumerators, c, first) {
char **children = NULL;
sd_bus_slot *slot;
if (bus->nodes_modified)
return 0;
slot = container_of(c, sd_bus_slot, node_enumerator);
bus->current_slot = sd_bus_slot_ref(slot);
bus->current_userdata = slot->userdata;
r = c->callback(bus, prefix, slot->userdata, &children, reterr_error);
bus->current_userdata = NULL;
bus->current_slot = sd_bus_slot_unref(slot);
if (r < 0)
return r;
if (sd_bus_error_is_set(reterr_error))
return -sd_bus_error_get_errno(reterr_error);
strv_sort(children);
STRV_FOREACH(k, children) {
if (r < 0) {
free(*k);
continue;
}
if (!object_path_is_valid(*k)) {
free(*k);
r = -EINVAL;
continue;
}
if (!object_path_startswith(*k, prefix)) {
free(*k);
continue;
}
r = ordered_set_consume(s, *k);
if (r == -EEXIST)
r = 0;
}
free(children);
if (r < 0)
return r;
}
return 0;
}
enum {
/* if set, add_subtree() works recursively */
CHILDREN_RECURSIVE = 1 << 0,
/* if set, add_subtree() scans object-manager hierarchies recursively */
CHILDREN_SUBHIERARCHIES = 1 << 1,
};
static int add_subtree_to_set(
sd_bus *bus,
const char *prefix,
BusNode *n,
unsigned flags,
OrderedSet *s,
sd_bus_error *reterr_error) {
int r;
assert(bus);
assert(prefix);
assert(n);
assert(s);
r = add_enumerated_to_set(bus, prefix, n->enumerators, s, reterr_error);
if (r < 0)
return r;
if (bus->nodes_modified)
return 0;
LIST_FOREACH(siblings, i, n->child) {
char *t;
if (!object_path_startswith(i->path, prefix))
continue;
t = strdup(i->path);
if (!t)
return -ENOMEM;
r = ordered_set_consume(s, t);
if (r < 0 && r != -EEXIST)
return r;
if ((flags & CHILDREN_RECURSIVE) &&
((flags & CHILDREN_SUBHIERARCHIES) || !i->object_managers)) {
r = add_subtree_to_set(bus, prefix, i, flags, s, reterr_error);
if (r < 0)
return r;
if (bus->nodes_modified)
return 0;
}
}
return 0;
}
static int get_child_nodes(
sd_bus *bus,
const char *prefix,
BusNode *n,
unsigned flags,
OrderedSet **ret,
sd_bus_error *reterr_error) {
_cleanup_ordered_set_free_ OrderedSet *s = NULL;
int r;
assert(bus);
assert(prefix);
assert(n);
assert(ret);
s = ordered_set_new(&string_hash_ops_free);
if (!s)
return -ENOMEM;
r = add_subtree_to_set(bus, prefix, n, flags, s, reterr_error);
if (r < 0)
return r;
*ret = TAKE_PTR(s);
return 0;
}
static int node_callbacks_run(
sd_bus *bus,
sd_bus_message *m,
BusNodeCallback *first,
bool require_fallback,
bool *found_object) {
int r;
assert(bus);
assert(m);
assert(found_object);
LIST_FOREACH(callbacks, c, first) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_slot *slot;
if (bus->nodes_modified)
return 0;
if (require_fallback && !c->is_fallback)
continue;
*found_object = true;
if (c->last_iteration == bus->iteration_counter)
continue;
c->last_iteration = bus->iteration_counter;
r = sd_bus_message_rewind(m, true);
if (r < 0)
return r;
slot = container_of(c, sd_bus_slot, node_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);
bus->current_userdata = NULL;
bus->current_handler = NULL;
bus->current_slot = sd_bus_slot_unref(slot);
r = bus_maybe_reply_error(m, r, &error);
if (r != 0)
return r;
}
return 0;
}
#define CAPABILITY_SHIFT(x) (((x) >> __builtin_ctzll(_SD_BUS_VTABLE_CAPABILITY_MASK)) & 0xFFFF)
static int check_access(sd_bus *bus, sd_bus_message *m, BusVTableMember *c, sd_bus_error *reterr_error) {
uint64_t cap;
int r;
assert(bus);
assert(m);
assert(c);
/* If the entire bus is trusted let's grant access */
if (bus->trusted)
return 0;
/* If the member is marked UNPRIVILEGED let's grant access */
if (c->vtable->flags & SD_BUS_VTABLE_UNPRIVILEGED)
return 0;
/* Check that the caller has the requested capability set. Note that the flags value contains the
* capability number plus one, which we need to subtract here. We do this so that we have 0 as
* special value for the default. */
cap = CAPABILITY_SHIFT(c->vtable->flags);
if (cap == 0)
cap = CAPABILITY_SHIFT(c->parent->vtable[0].flags);
if (cap == 0)
cap = CAP_SYS_ADMIN;
else
cap--;
r = sd_bus_query_sender_privilege(m, cap);
if (r < 0)
return r;
if (r > 0)
return 0;
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_ACCESS_DENIED, "Access to %s.%s() not permitted.", c->interface, c->member);
}
typedef struct BusFiberData {
sd_bus *bus;
sd_bus_message *message;
sd_bus_slot *slot;
sd_bus_message_handler_t handler;
void *userdata;
} BusFiberData;
static BusFiberData* bus_fiber_data_free(BusFiberData *d) {
if (!d)
return NULL;
sd_bus_slot_unref(d->slot);
sd_bus_message_unref(d->message);
sd_bus_unref(d->bus);
return mfree(d);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(BusFiberData*, bus_fiber_data_free);
static void bus_fiber_data_destroy(void *userdata) {
bus_fiber_data_free(userdata);
}
static void bus_fiber_future_unref(void *p) {
sd_future_unref(p);
}
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
bus_fiber_future_hash_ops,
void,
trivial_hash_func,
trivial_compare_func,
bus_fiber_future_unref);
static int bus_fiber_resolved(sd_future *f) {
sd_bus *bus = ASSERT_PTR(sd_future_get_userdata(f));
assert_se(set_remove(bus->fiber_futures, f) == f);
sd_future_unref(f);
return 0;
}
static int bus_fiber_entry(void *userdata) {
BusFiberData *d = ASSERT_PTR(userdata);
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
/* Note: unlike the synchronous dispatch path, we deliberately do NOT set
* bus->current_slot/handler/userdata around the callback. Those fields track the slot of the
* message currently being dispatched inline and must be NULL at each entry into
* bus_process_internal(). Because a fiber handler can yield and let the event loop dispatch
* other messages before it resumes, leaving current_slot non-NULL across yields would trip
* that invariant. Fiber handlers receive their slot's userdata via the handler argument, so
* sd_bus_get_current_slot()/handler()/userdata() simply aren't meaningful inside them — the
* handler should use the message/userdata parameters directly instead. */
r = d->handler(d->message, d->userdata, &error);
return bus_maybe_reply_error(d->message, r, &error);
}
static int method_callbacks_run(
sd_bus *bus,
sd_bus_message *m,
BusVTableMember *c,
bool require_fallback,
bool *found_object) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
const char *signature;
void *u;
int r;
assert(bus);
assert(m);
assert(c);
assert(found_object);
if (require_fallback && !c->parent->is_fallback)
return 0;
if (FLAGS_SET(c->vtable->flags, SD_BUS_VTABLE_SENSITIVE)) {
r = sd_bus_message_sensitive(m);
if (r < 0)
return r;
}
r = check_access(bus, m, c, &error);
if (r < 0)
return bus_maybe_reply_error(m, r, &error);
r = node_vtable_get_userdata(bus, m->path, c->parent, &u, &error);
if (r <= 0)
return bus_maybe_reply_error(m, r, &error);
if (bus->nodes_modified)
return 0;
u = vtable_method_convert_userdata(c->vtable, u);
*found_object = true;
if (c->last_iteration == bus->iteration_counter)
return 0;
c->last_iteration = bus->iteration_counter;
r = sd_bus_message_rewind(m, true);
if (r < 0)
return r;
signature = sd_bus_message_get_signature(m, true);
if (!signature)
return -EINVAL;
if (!streq(strempty(c->vtable->x.method.signature), signature))
return sd_bus_reply_method_errorf(
m,
SD_BUS_ERROR_INVALID_ARGS,
"Invalid arguments '%s' to call %s.%s(), expecting '%s'.",
signature, c->interface, c->member, strempty(c->vtable->x.method.signature));
/* Keep track what the signature of the reply to this message
* should be, so that this can be enforced when sealing the
* reply. */
m->enforced_reply_signature = strempty(c->vtable->x.method.result);
if (c->vtable->x.method.handler) {
sd_bus_slot *slot;
slot = container_of(c->parent, sd_bus_slot, node_vtable);
if (FLAGS_SET(c->vtable->flags, SD_BUS_VTABLE_METHOD_FIBER)) {
/* A fiber-dispatched method requires an event loop to spawn the fiber on.
* By the time a method call actually arrives the bus is running, so the
* event loop should already be attached — if not, the caller set up the bus
* wrong and there's no meaningful recovery. */
assert(bus->event);
_cleanup_(bus_fiber_data_freep) BusFiberData *d = new(BusFiberData, 1);
if (!d)
return -ENOMEM;
*d = (BusFiberData) {
.bus = sd_bus_ref(bus),
.message = sd_bus_message_ref(m),
.slot = sd_bus_slot_ref(slot),
.handler = c->vtable->x.method.handler,
.userdata = u,
};
_cleanup_(sd_future_unrefp) sd_future *f = NULL;
r = sd_fiber_new(bus->event, c->member, bus_fiber_entry, d, bus_fiber_data_destroy, &f);
if (r < 0)
return bus_maybe_reply_error(m, r, NULL);
/* The fiber now owns d via bus_fiber_data_destroy. Drop our cleanup before any
* further fallible calls, so a later failure unwinding f doesn't double-free d. */
TAKE_PTR(d);
r = set_ensure_put(&bus->fiber_futures, &bus_fiber_future_hash_ops, f);
if (r < 0)
return bus_maybe_reply_error(m, r, NULL);
assert(r > 0);
/* Track the future on the bus so shutdown can cancel it and wait for it. */
r = sd_future_set_callback(f, bus_fiber_resolved, bus);
if (r < 0) {
/* TAKE_PTR(f) hasn't run yet, so our cleanup attribute still owns the
* ref; set_remove() returns the raw pointer without firing the hash_ops
* destructor, and the cleanup will unref f on return. */
assert_se(set_remove(bus->fiber_futures, f) == f);
return bus_maybe_reply_error(m, r, NULL);
}
TAKE_PTR(f);
return 1;
}
bus->current_slot = sd_bus_slot_ref(slot);
bus->current_handler = c->vtable->x.method.handler;
bus->current_userdata = u;
r = c->vtable->x.method.handler(m, u, &error);
bus->current_userdata = NULL;
bus->current_handler = NULL;
bus->current_slot = sd_bus_slot_unref(slot);
return bus_maybe_reply_error(m, r, &error);
}
/* If the method callback is NULL, make this a successful NOP */
r = sd_bus_reply_method_return(m, NULL);
if (r < 0)
return r;
return 1;
}
static int invoke_property_get(
sd_bus *bus,
sd_bus_slot *slot,
const sd_bus_vtable *v,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *reterr_error) {
const void *p;
int r;
assert(bus);
assert(slot);
assert(v);
assert(path);
assert(interface);
assert(property);
assert(reply);
if (v->x.property.get) {
bus->current_slot = sd_bus_slot_ref(slot);
bus->current_userdata = userdata;
r = v->x.property.get(bus, path, interface, property, reply, userdata, reterr_error);
bus->current_userdata = NULL;
bus->current_slot = sd_bus_slot_unref(slot);
if (r < 0)
return r;
if (sd_bus_error_is_set(reterr_error))
return -sd_bus_error_get_errno(reterr_error);
return r;
}
/* Automatic handling if no callback is defined. */
if (streq(v->x.property.signature, "as"))
return sd_bus_message_append_strv(reply, *(char***) userdata);
assert(signature_is_single(v->x.property.signature, false));
assert(bus_type_is_basic(v->x.property.signature[0]));
switch (v->x.property.signature[0]) {
case SD_BUS_TYPE_STRING:
case SD_BUS_TYPE_SIGNATURE:
p = strempty(*(char**) userdata);
break;
case SD_BUS_TYPE_OBJECT_PATH:
p = *(char**) userdata;
assert(p);
break;
default:
p = userdata;
}
return sd_bus_message_append_basic(reply, v->x.property.signature[0], p);
}
static int invoke_property_set(
sd_bus *bus,
sd_bus_slot *slot,
const sd_bus_vtable *v,
const char *path,
const char *interface,
const char *property,
sd_bus_message *value,
void *userdata,
sd_bus_error *reterr_error) {
int r;
assert(bus);
assert(slot);
assert(v);
assert(path);
assert(interface);
assert(property);
assert(value);
if (v->x.property.set) {
bus->current_slot = sd_bus_slot_ref(slot);
bus->current_userdata = userdata;
r = v->x.property.set(bus, path, interface, property, value, userdata, reterr_error);
bus->current_userdata = NULL;
bus->current_slot = sd_bus_slot_unref(slot);
if (r < 0)
return r;
if (sd_bus_error_is_set(reterr_error))
return -sd_bus_error_get_errno(reterr_error);
return r;
}
/* Automatic handling if no callback is defined. */
assert(signature_is_single(v->x.property.signature, false));
assert(bus_type_is_basic(v->x.property.signature[0]));
switch (v->x.property.signature[0]) {
case SD_BUS_TYPE_STRING:
case SD_BUS_TYPE_OBJECT_PATH:
case SD_BUS_TYPE_SIGNATURE: {
const char *p;
char *n;
r = sd_bus_message_read_basic(value, v->x.property.signature[0], &p);
if (r < 0)
return r;
n = strdup(p);
if (!n)
return -ENOMEM;
free(*(char**) userdata);
*(char**) userdata = n;
break;
}
default:
r = sd_bus_message_read_basic(value, v->x.property.signature[0], userdata);
if (r < 0)
return r;
}
return 1;
}
static int property_get_set_callbacks_run(
sd_bus *bus,
sd_bus_message *m,
BusVTableMember *c,
bool require_fallback,
bool is_get,
bool *found_object) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
sd_bus_slot *slot;
void *u = NULL;
int r;
assert(bus);
assert(m);
assert(c);
assert(found_object);
if (require_fallback && !c->parent->is_fallback)
return 0;
if (FLAGS_SET(c->vtable->flags, SD_BUS_VTABLE_SENSITIVE)) {
r = sd_bus_message_sensitive(m);
if (r < 0)
return r;
}
r = vtable_property_get_userdata(bus, m->path, c, &u, &error);
if (r <= 0)
return bus_maybe_reply_error(m, r, &error);
if (bus->nodes_modified)
return 0;
slot = container_of(c->parent, sd_bus_slot, node_vtable);
*found_object = true;
r = sd_bus_message_new_method_return(m, &reply);
if (r < 0)
return r;
if (FLAGS_SET(c->vtable->flags, SD_BUS_VTABLE_SENSITIVE)) {
r = sd_bus_message_sensitive(reply);
if (r < 0)
return r;
}
if (is_get) {
/* Note that we do not protect against reexecution
* here (using the last_iteration check, see below),
* should the node tree have changed and we got called
* again. We assume that property Get() calls are
* ultimately without side-effects or if they aren't
* then at least idempotent. */
r = sd_bus_message_open_container(reply, 'v', c->vtable->x.property.signature);
if (r < 0)
return r;
/* Note that we do not do an access check here. Read
* access to properties is always unrestricted, since
* PropertiesChanged signals broadcast contents
* anyway. */
r = invoke_property_get(bus, slot, c->vtable, m->path, c->interface, c->member, reply, u, &error);
if (r < 0)
return bus_maybe_reply_error(m, r, &error);
if (bus->nodes_modified)
return 0;
r = sd_bus_message_close_container(reply);
if (r < 0)
return r;
} else {
const char *signature = NULL;
char type = 0;
if (c->vtable->type != _SD_BUS_VTABLE_WRITABLE_PROPERTY)
return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Property '%s' is not writable.", c->member);
/* Avoid that we call the set routine more than once
* if the processing of this message got restarted
* because the node tree changed. */
if (c->last_iteration == bus->iteration_counter)
return 0;
c->last_iteration = bus->iteration_counter;
r = sd_bus_message_peek_type(m, &type, &signature);
if (r < 0)
return r;
if (type != 'v')
return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_SIGNATURE,
"Incorrect signature when setting property '%s', expected 'v', got '%c'.",
c->member, type);
if (!streq(strempty(signature), strempty(c->vtable->x.property.signature)))
return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS,
"Incorrect parameters for property '%s', expected '%s', got '%s'.",
c->member, strempty(c->vtable->x.property.signature), strempty(signature));
r = sd_bus_message_enter_container(m, 'v', c->vtable->x.property.signature);
if (r < 0)
return r;
r = check_access(bus, m, c, &error);
if (r < 0)
return bus_maybe_reply_error(m, r, &error);
r = invoke_property_set(bus, slot, c->vtable, m->path, c->interface, c->member, m, u, &error);
if (r < 0)
return bus_maybe_reply_error(m, r, &error);
if (bus->nodes_modified)
return 0;
r = sd_bus_message_exit_container(m);
if (r < 0)
return r;
}
r = sd_bus_send(bus, reply, NULL);
if (r < 0)
return r;
return 1;
}
static int vtable_append_one_property(
sd_bus *bus,
sd_bus_message *reply,
const char *path,
BusNodeVTable *c,
const sd_bus_vtable *v,
void *userdata,
sd_bus_error *reterr_error) {
sd_bus_slot *slot;
int r;
assert(bus);
assert(reply);
assert(path);
assert(c);
assert(v);
if (FLAGS_SET(c->vtable->flags, SD_BUS_VTABLE_SENSITIVE)) {
r = sd_bus_message_sensitive(reply);
if (r < 0)
return r;
}
r = sd_bus_message_open_container(reply, 'e', "sv");
if (r < 0)
return r;
r = sd_bus_message_append(reply, "s", v->x.property.member);
if (r < 0)
return r;
r = sd_bus_message_open_container(reply, 'v', v->x.property.signature);
if (r < 0)
return r;
slot = container_of(c, sd_bus_slot, node_vtable);
r = invoke_property_get(bus, slot, v, path, c->interface, v->x.property.member, reply, vtable_property_convert_userdata(v, userdata), reterr_error);
if (r < 0)
return r;
if (bus->nodes_modified)
return 0;
r = sd_bus_message_close_container(reply);
if (r < 0)
return r;
r = sd_bus_message_close_container(reply);
if (r < 0)
return r;
return 0;
}
static int vtable_append_all_properties(
sd_bus *bus,
sd_bus_message *reply,
const char *path,
BusNodeVTable *c,
void *userdata,
sd_bus_error *reterr_error) {
const sd_bus_vtable *v;
int r;
assert(bus);
assert(reply);
assert(path);
assert(c);
if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN)
return 1;
v = c->vtable;
for (v = bus_vtable_next(c->vtable, v); v->type != _SD_BUS_VTABLE_END; v = bus_vtable_next(c->vtable, v)) {
if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
continue;
if (v->flags & SD_BUS_VTABLE_HIDDEN)
continue;
/* Let's not include properties marked as "explicit" in any message that contains a generic
* dump of properties, but only in those generated as a response to an explicit request. */
if (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)
continue;
/* Let's not include properties marked only for invalidation on change (i.e. in contrast to
* those whose new values are included in PropertiesChanges message) in any signals. This is
* useful to ensure they aren't included in InterfacesAdded messages. */
if (reply->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
FLAGS_SET(v->flags, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION))
continue;
r = vtable_append_one_property(bus, reply, path, c, v, userdata, reterr_error);
if (r < 0)
return r;
if (bus->nodes_modified)
return 0;
}
return 1;
}
static int property_get_all_callbacks_run(
sd_bus *bus,
sd_bus_message *m,
BusNodeVTable *first,
bool require_fallback,
const char *iface,
bool *found_object) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
bool found_interface;
int r;
assert(bus);
assert(m);
assert(found_object);
r = sd_bus_message_new_method_return(m, &reply);
if (r < 0)
return r;
r = sd_bus_message_open_container(reply, 'a', "{sv}");
if (r < 0)
return r;
found_interface = !iface || STR_IN_SET(iface,
"org.freedesktop.DBus.Properties",
"org.freedesktop.DBus.Peer",
"org.freedesktop.DBus.Introspectable");
LIST_FOREACH(vtables, c, first) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
void *u;
if (require_fallback && !c->is_fallback)
continue;
r = node_vtable_get_userdata(bus, m->path, c, &u, &error);
if (r < 0)
return bus_maybe_reply_error(m, r, &error);
if (bus->nodes_modified)
return 0;
if (r == 0)
continue;
*found_object = true;
if (iface && !streq(c->interface, iface))
continue;
found_interface = true;
r = vtable_append_all_properties(bus, reply, m->path, c, u, &error);
if (r < 0)
return bus_maybe_reply_error(m, r, &error);
if (bus->nodes_modified)
return 0;
}
if (!*found_object)
return 0;
if (!found_interface) {
r = sd_bus_reply_method_errorf(
m,
SD_BUS_ERROR_UNKNOWN_INTERFACE,
"Unknown interface '%s'.", iface);
if (r < 0)
return r;
return 1;
}
r = sd_bus_message_close_container(reply);
if (r < 0)
return r;
r = sd_bus_send(bus, reply, NULL);
if (r < 0)
return r;
return 1;
}
static int bus_node_exists(
sd_bus *bus,
BusNode *n,
const char *path,
bool require_fallback) {
int r;
assert(bus);
assert(n);