X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flibsystemd-bus%2Fbus-objects.c;h=b116a5dd10146ddc729abade07fd4bf7d11b3c05;hb=ac6b760ceedd4b21921b6a682cf1479af3d3024f;hp=8413023d6f979a0bb0f0c27b3676e898cbfd050b;hpb=ebcf1f97de4f6b1580ae55eb56b1a3939fe6b602;p=elogind.git diff --git a/src/libsystemd-bus/bus-objects.c b/src/libsystemd-bus/bus-objects.c index 8413023d6..b116a5dd1 100644 --- a/src/libsystemd-bus/bus-objects.c +++ b/src/libsystemd-bus/bus-objects.c @@ -19,6 +19,8 @@ along with systemd; If not, see . ***/ +#include + #include "strv.h" #include "set.h" #include "bus-internal.h" @@ -33,7 +35,8 @@ static int node_vtable_get_userdata( sd_bus *bus, const char *path, struct node_vtable *c, - void **userdata) { + void **userdata, + sd_bus_error *error) { void *u; int r; @@ -44,8 +47,12 @@ static int node_vtable_get_userdata( u = c->userdata; if (c->find) { - r = c->find(bus, path, c->interface, &u, u); - if (r <= 0) + r = c->find(bus, path, c->interface, u, &u, error); + if (r < 0) + return r; + if (sd_bus_error_is_set(error)) + return -sd_bus_error_get_errno(error); + if (r == 0) return r; } @@ -65,7 +72,8 @@ static int vtable_property_get_userdata( sd_bus *bus, const char *path, struct vtable_member *p, - void **userdata) { + void **userdata, + sd_bus_error *error) { void *u; int r; @@ -75,7 +83,7 @@ static int vtable_property_get_userdata( assert(p); assert(userdata); - r = node_vtable_get_userdata(bus, path, p->parent, &u); + r = node_vtable_get_userdata(bus, path, p->parent, &u, error); if (r <= 0) return r; if (bus->nodes_modified) @@ -89,7 +97,8 @@ static int add_enumerated_to_set( sd_bus *bus, const char *prefix, struct node_enumerator *first, - Set *s) { + Set *s, + sd_bus_error *error) { struct node_enumerator *c; int r; @@ -104,9 +113,11 @@ static int add_enumerated_to_set( if (bus->nodes_modified) return 0; - r = c->callback(bus, prefix, &children, c->userdata); + r = c->callback(bus, prefix, c->userdata, &children, error); if (r < 0) return r; + if (sd_bus_error_is_set(error)) + return -sd_bus_error_get_errno(error); STRV_FOREACH(k, children) { if (r < 0) { @@ -126,6 +137,8 @@ static int add_enumerated_to_set( } r = set_consume(s, *k); + if (r == -EEXIST) + r = 0; } free(children); @@ -140,7 +153,8 @@ static int add_subtree_to_set( sd_bus *bus, const char *prefix, struct node *n, - Set *s) { + Set *s, + sd_bus_error *error) { struct node *i; int r; @@ -150,7 +164,7 @@ static int add_subtree_to_set( assert(n); assert(s); - r = add_enumerated_to_set(bus, prefix, n->enumerators, s); + r = add_enumerated_to_set(bus, prefix, n->enumerators, s, error); if (r < 0) return r; if (bus->nodes_modified) @@ -170,7 +184,7 @@ static int add_subtree_to_set( if (r < 0 && r != -EEXIST) return r; - r = add_subtree_to_set(bus, prefix, i, s); + r = add_subtree_to_set(bus, prefix, i, s, error); if (r < 0) return r; if (bus->nodes_modified) @@ -184,7 +198,8 @@ static int get_child_nodes( sd_bus *bus, const char *prefix, struct node *n, - Set **_s) { + Set **_s, + sd_bus_error *error) { Set *s = NULL; int r; @@ -198,7 +213,7 @@ static int get_child_nodes( if (!s) return -ENOMEM; - r = add_subtree_to_set(bus, prefix, n, s); + r = add_subtree_to_set(bus, prefix, n, s, error); if (r < 0) { set_free_free(s); return r; @@ -251,6 +266,64 @@ static int node_callbacks_run( 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, struct vtable_member *c, sd_bus_error *error) { + _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; + uint64_t cap; + uid_t uid; + 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; + + /* If we are not connected to kdbus we cannot retrieve the + * effective capability set without race. Since we need this + * for a security decision we cannot use racy data, hence + * don't request it. */ + if (bus->is_kernel) + r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_UID|SD_BUS_CREDS_EFFECTIVE_CAPS, &creds); + else + r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_UID, &creds); + if (r < 0) + return r; + + /* Check have 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 "default + * capability". */ + 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_creds_has_effective_cap(creds, cap); + if (r > 0) + return 1; + + /* Caller has same UID as us, then let's grant access */ + r = sd_bus_creds_get_uid(creds, &uid); + if (r >= 0) { + if (uid == getuid()) + return 1; + } + + return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Access to %s.%s() not permitted.", c->interface, c->member); +} + static int method_callbacks_run( sd_bus *bus, sd_bus_message *m, @@ -258,6 +331,7 @@ static int method_callbacks_run( bool require_fallback, bool *found_object) { + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; const char *signature; void *u; int r; @@ -270,9 +344,13 @@ static int method_callbacks_run( if (require_fallback && !c->parent->is_fallback) return 0; - r = node_vtable_get_userdata(bus, m->path, c->parent, &u); + 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 r; + return bus_maybe_reply_error(m, r, &error); if (bus->nodes_modified) return 0; @@ -291,23 +369,21 @@ static int method_callbacks_run( if (!signature) return -EINVAL; - if (!streq(strempty(c->vtable->x.method.signature), signature)) { - r = 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)); - if (r < 0) - return r; + 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)); - return 1; - } + /* 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) { - _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL; - - r = c->vtable->x.method.handler(bus, m, u, &error_buffer); - - return bus_maybe_reply_error(m, r, &error_buffer); + r = c->vtable->x.method.handler(bus, m, u, &error); + return bus_maybe_reply_error(m, r, &error); } /* If the method callback is NULL, make this a successful NOP */ @@ -329,6 +405,7 @@ static int invoke_property_get( sd_bus_error *error) { const void *p; + int r; assert(bus); assert(v); @@ -337,8 +414,14 @@ static int invoke_property_get( assert(property); assert(reply); - if (v->x.property.get) - return v->x.property.get(bus, path, interface, property, reply, userdata, error); + if (v->x.property.get) { + r = v->x.property.get(bus, path, interface, property, reply, userdata, error); + if (r < 0) + return r; + if (sd_bus_error_is_set(error)) + return -sd_bus_error_get_errno(error); + return r; + } /* Automatic handling if no callback is defined. */ @@ -387,8 +470,14 @@ static int invoke_property_set( assert(property); assert(value); - if (v->x.property.set) - return v->x.property.set(bus, path, interface, property, value, userdata, error); + if (v->x.property.set) { + r = v->x.property.set(bus, path, interface, property, value, userdata, error); + if (r < 0) + return r; + if (sd_bus_error_is_set(error)) + return -sd_bus_error_get_errno(error); + return r; + } /* Automatic handling if no callback is defined. */ @@ -449,9 +538,9 @@ static int property_get_set_callbacks_run( if (require_fallback && !c->parent->is_fallback) return 0; - r = vtable_property_get_userdata(bus, m->path, c, &u); + r = vtable_property_get_userdata(bus, m->path, c, &u, &error); if (r <= 0) - return r; + return bus_maybe_reply_error(m, r, &error); if (bus->nodes_modified) return 0; @@ -473,11 +562,14 @@ static int property_get_set_callbacks_run( 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, c->vtable, m->path, c->interface, c->member, reply, u, &error); if (r < 0) - return sd_bus_reply_method_errno(m, r, &error); - if (sd_bus_error_is_set(&error)) - return sd_bus_reply_method_error(m, &error); + return bus_maybe_reply_error(m, r, &error); if (bus->nodes_modified) return 0; @@ -502,11 +594,13 @@ static int property_get_set_callbacks_run( 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, c->vtable, m->path, c->interface, c->member, m, u, &error); if (r < 0) - return sd_bus_reply_method_errno(m, r, &error); - if (sd_bus_error_is_set(&error)) - return sd_bus_reply_method_error(m, &error); + return bus_maybe_reply_error(m, r, &error); if (bus->nodes_modified) return 0; @@ -523,6 +617,52 @@ static int property_get_set_callbacks_run( return 1; } +static int vtable_append_one_property( + sd_bus *bus, + sd_bus_message *reply, + const char *path, + struct node_vtable *c, + const sd_bus_vtable *v, + void *userdata, + sd_bus_error *error) { + + int r; + + assert(bus); + assert(reply); + assert(path); + assert(c); + assert(v); + + 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; + + r = invoke_property_get(bus, v, path, c->interface, v->x.property.member, reply, vtable_property_convert_userdata(v, userdata), 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, @@ -539,39 +679,21 @@ static int vtable_append_all_properties( assert(path); assert(c); + if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN) + return 1; + for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) { if (v->type != _SD_BUS_VTABLE_PROPERTY && v->type != _SD_BUS_VTABLE_WRITABLE_PROPERTY) continue; - 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; + if (v->flags & SD_BUS_VTABLE_HIDDEN) + continue; - r = sd_bus_message_open_container(reply, 'v', v->x.property.signature); + r = vtable_append_one_property(bus, reply, path, c, v, userdata, error); if (r < 0) return r; - - r = invoke_property_get(bus, v, path, c->interface, v->x.property.member, reply, vtable_property_convert_userdata(v, userdata), error); - if (sd_bus_error_is_set(error)) - return 0; - if (r < 0) { - sd_bus_error_set_errno(error, r); - return 0; - } 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 1; @@ -614,9 +736,9 @@ static int property_get_all_callbacks_run( if (require_fallback && !c->is_fallback) continue; - r = node_vtable_get_userdata(bus, m->path, c, &u); + r = node_vtable_get_userdata(bus, m->path, c, &u, &error); if (r < 0) - return r; + return bus_maybe_reply_error(m, r, &error); if (bus->nodes_modified) return 0; if (r == 0) @@ -630,15 +752,7 @@ static int property_get_all_callbacks_run( r = vtable_append_all_properties(bus, reply, m->path, c, u, &error); if (r < 0) - return r; - - if (sd_bus_error_is_set(&error)) { - r = sd_bus_reply_method_error(m, &error); - if (r < 0) - return r; - - return 1; - } + return bus_maybe_reply_error(m, r, &error); if (bus->nodes_modified) return 0; } @@ -702,11 +816,12 @@ static bool bus_node_exists( } LIST_FOREACH(vtables, c, n->vtables) { + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; if (require_fallback && !c->is_fallback) continue; - if (node_vtable_get_userdata(bus, path, c, NULL) > 0) + if (node_vtable_get_userdata(bus, path, c, NULL, &error) > 0) return true; if (bus->nodes_modified) return false; @@ -722,6 +837,7 @@ static int process_introspect( bool require_fallback, bool *found_object) { + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; _cleanup_set_free_free_ Set *s = NULL; const char *previous_interface = NULL; @@ -735,13 +851,13 @@ static int process_introspect( assert(n); assert(found_object); - r = get_child_nodes(bus, m->path, n, &s); + r = get_child_nodes(bus, m->path, n, &s, &error); if (r < 0) - return r; + return bus_maybe_reply_error(m, r, &error); if (bus->nodes_modified) return 0; - r = introspect_begin(&intro); + r = introspect_begin(&intro, bus->trusted); if (r < 0) return r; @@ -755,16 +871,23 @@ static int process_introspect( if (require_fallback && !c->is_fallback) continue; - r = node_vtable_get_userdata(bus, m->path, c, NULL); - if (r < 0) - return r; - if (bus->nodes_modified) - return 0; + r = node_vtable_get_userdata(bus, m->path, c, NULL, &error); + if (r < 0) { + r = bus_maybe_reply_error(m, r, &error); + goto finish; + } + if (bus->nodes_modified) { + r = 0; + goto finish; + } if (r == 0) continue; empty = false; + if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN) + continue; + if (!streq_ptr(previous_interface, c->interface)) { if (previous_interface) @@ -846,7 +969,7 @@ static int object_manager_serialize_path( if (require_fallback && !i->is_fallback) continue; - r = node_vtable_get_userdata(bus, path, i, &u); + r = node_vtable_get_userdata(bus, path, i, &u, error); if (r < 0) return r; if (bus->nodes_modified) @@ -905,8 +1028,6 @@ static int object_manager_serialize_path( r = vtable_append_all_properties(bus, reply, path, i, u, error); if (r < 0) return r; - if (sd_bus_error_is_set(error)) - return 0; if (bus->nodes_modified) return 0; @@ -954,8 +1075,6 @@ static int object_manager_serialize_path_and_fallbacks( r = object_manager_serialize_path(bus, reply, path, path, false, error); if (r < 0) return r; - if (sd_bus_error_is_set(error)) - return 0; if (bus->nodes_modified) return 0; @@ -965,8 +1084,6 @@ static int object_manager_serialize_path_and_fallbacks( r = object_manager_serialize_path(bus, reply, prefix, path, true, error); if (r < 0) return r; - if (sd_bus_error_is_set(error)) - return 0; if (bus->nodes_modified) return 0; } @@ -981,6 +1098,7 @@ static int process_get_managed_objects( bool require_fallback, bool *found_object) { + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; _cleanup_set_free_free_ Set *s = NULL; bool empty; @@ -994,7 +1112,7 @@ static int process_get_managed_objects( if (!bus_node_with_object_manager(bus, n)) return 0; - r = get_child_nodes(bus, m->path, n, &s); + r = get_child_nodes(bus, m->path, n, &s, &error); if (r < 0) return r; if (bus->nodes_modified) @@ -1037,19 +1155,9 @@ static int process_get_managed_objects( char *path; SET_FOREACH(path, s, i) { - _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - r = object_manager_serialize_path_and_fallbacks(bus, reply, path, &error); if (r < 0) - return -ENOMEM; - - if (sd_bus_error_is_set(&error)) { - r = sd_bus_reply_method_error(m, &error); - if (r < 0) - return r; - - return 1; - } + return r; if (bus->nodes_modified) return 0; @@ -1127,7 +1235,7 @@ static int object_find_and_run( r = sd_bus_message_read(m, "ss", &vtable_key.interface, &vtable_key.member); if (r < 0) - return r; + return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected interface and member parameters"); v = hashmap_get(bus->vtable_properties, &vtable_key); if (v) { @@ -1145,7 +1253,7 @@ static int object_find_and_run( r = sd_bus_message_read(m, "s", &iface); if (r < 0) - return r; + return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected interface parameter"); if (iface[0] == 0) iface = NULL; @@ -1157,12 +1265,18 @@ static int object_find_and_run( } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) { + if (!isempty(sd_bus_message_get_signature(m, true))) + return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected no parameters"); + r = process_introspect(bus, m, n, require_fallback, found_object); if (r != 0) return r; } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.ObjectManager", "GetManagedObjects")) { + if (!isempty(sd_bus_message_get_signature(m, true))) + return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected no parameters"); + r = process_get_managed_objects(bus, m, n, require_fallback, found_object); if (r != 0) return r; @@ -1193,12 +1307,16 @@ int bus_process_object(sd_bus *bus, sd_bus_message *m) { if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL) return 0; - if (!m->path) + if (hashmap_isempty(bus->nodes)) return 0; - if (hashmap_isempty(bus->nodes)) + /* Never respond to broadcast messages */ + if (bus->bus_client && !m->destination) return 0; + assert(m->path); + assert(m->member); + pl = strlen(m->path); do { char prefix[pl+1]; @@ -1246,7 +1364,8 @@ int bus_process_object(sd_bus *bus, sd_bus_message *m) { static struct node *bus_node_allocate(sd_bus *bus, const char *path) { struct node *n, *parent; const char *e; - char *s, *p; + _cleanup_free_ char *s = NULL; + char *p; int r; assert(bus); @@ -1274,10 +1393,8 @@ static struct node *bus_node_allocate(sd_bus *bus, const char *path) { p = strndupa(path, MAX(1, path - e)); parent = bus_node_allocate(bus, p); - if (!parent) { - free(s); + if (!parent) return NULL; - } } n = new0(struct node, 1); @@ -1286,10 +1403,11 @@ static struct node *bus_node_allocate(sd_bus *bus, const char *path) { n->parent = parent; n->path = s; + s = NULL; /* do not free */ - r = hashmap_put(bus->nodes, s, n); + r = hashmap_put(bus->nodes, n->path, n); if (r < 0) { - free(s); + free(n->path); free(n); return NULL; } @@ -1475,15 +1593,25 @@ static void free_node_vtable(sd_bus *bus, struct node_vtable *w) { free(w); } -static unsigned vtable_member_hash_func(const void *a) { +static unsigned long vtable_member_hash_func(const void *a, const uint8_t hash_key[HASH_KEY_SIZE]) { const struct vtable_member *m = a; + uint8_t hash_key2[HASH_KEY_SIZE]; + unsigned long ret; assert(m); - return - string_hash_func(m->path) ^ - string_hash_func(m->interface) ^ - string_hash_func(m->member); + ret = string_hash_func(m->path, hash_key); + + /* Use a slightly different hash key for the interface */ + memcpy(hash_key2, hash_key, HASH_KEY_SIZE); + hash_key2[0]++; + ret ^= string_hash_func(m->interface, hash_key2); + + /* And an even different one for the member */ + hash_key2[0]++; + ret ^= string_hash_func(m->member, hash_key2); + + return ret; } static int vtable_member_compare_func(const void *a, const void *b) { @@ -1588,7 +1716,7 @@ static int add_object_vtable_internal( !signature_is_valid(strempty(v->x.method.signature), false) || !signature_is_valid(strempty(v->x.method.result), false) || !(v->x.method.handler || (isempty(v->x.method.signature) && isempty(v->x.method.result))) || - v->flags & (SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE|SD_BUS_VTABLE_PROPERTY_INVALIDATE_ONLY)) { + v->flags & (SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) { r = -EINVAL; goto fail; } @@ -1630,12 +1758,12 @@ static int add_object_vtable_internal( !signature_is_single(v->x.property.signature, false) || !(v->x.property.get || bus_type_is_basic(v->x.property.signature[0]) || streq(v->x.property.signature, "as")) || v->flags & SD_BUS_VTABLE_METHOD_NO_REPLY || - (v->flags & SD_BUS_VTABLE_PROPERTY_INVALIDATE_ONLY && !(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))) { + (!!(v->flags & SD_BUS_VTABLE_PROPERTY_CONST) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) > 1 || + (v->flags & SD_BUS_VTABLE_UNPRIVILEGED && v->type == _SD_BUS_VTABLE_PROPERTY)) { r = -EINVAL; goto fail; } - m = new0(struct vtable_member, 1); if (!m) { r = -ENOMEM; @@ -1660,7 +1788,8 @@ static int add_object_vtable_internal( case _SD_BUS_VTABLE_SIGNAL: if (!member_name_is_valid(v->x.signal.member) || - !signature_is_valid(strempty(v->x.signal.signature), false)) { + !signature_is_valid(strempty(v->x.signal.signature), false) || + v->flags & SD_BUS_VTABLE_UNPRIVILEGED) { r = -EINVAL; goto fail; } @@ -1852,8 +1981,10 @@ static int emit_properties_changed_on_interface( const char *path, const char *interface, bool require_fallback, + bool *found_interface, char **names) { + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_bus_message_unref_ sd_bus_message *m = NULL; bool has_invalidating = false, has_changing = false; struct vtable_member key = {}; @@ -1867,6 +1998,7 @@ static int emit_properties_changed_on_interface( assert(prefix); assert(path); assert(interface); + assert(found_interface); n = hashmap_get(bus->nodes, prefix); if (!n) @@ -1894,7 +2026,7 @@ static int emit_properties_changed_on_interface( if (!streq(c->interface, interface)) continue; - r = node_vtable_get_userdata(bus, path, c, &u); + r = node_vtable_get_userdata(bus, path, c, &u, &error); if (r < 0) return r; if (bus->nodes_modified) @@ -1902,59 +2034,77 @@ static int emit_properties_changed_on_interface( if (r == 0) continue; - STRV_FOREACH(property, names) { - _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; - struct vtable_member *v; + *found_interface = true; + + if (names) { + /* If the caller specified a list of + * properties we include exactly those in the + * PropertiesChanged message */ - assert_return(member_name_is_valid(*property), -EINVAL); + STRV_FOREACH(property, names) { + struct vtable_member *v; - key.member = *property; - v = hashmap_get(bus->vtable_properties, &key); - if (!v) - return -ENOENT; + assert_return(member_name_is_valid(*property), -EINVAL); - /* If there are two vtables for the same - * interface, let's handle this property when - * we come to that vtable. */ - if (c != v->parent) - continue; + key.member = *property; + v = hashmap_get(bus->vtable_properties, &key); + if (!v) + return -ENOENT; + + /* If there are two vtables for the same + * interface, let's handle this property when + * we come to that vtable. */ + if (c != v->parent) + continue; - assert_return(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE, -EDOM); + assert_return(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE || + v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION, -EDOM); - if (v->vtable->flags & SD_BUS_VTABLE_PROPERTY_INVALIDATE_ONLY) { - has_invalidating = true; - continue; + assert_return(!(v->vtable->flags & SD_BUS_VTABLE_HIDDEN), -EDOM); + + if (v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) { + has_invalidating = true; + continue; + } + + has_changing = true; + + r = vtable_append_one_property(bus, m, m->path, c, v->vtable, u, &error); + if (r < 0) + return r; + if (bus->nodes_modified) + return 0; } + } else { + const sd_bus_vtable *v; - has_changing = true; + /* If the caller specified no properties list + * we include all properties that are marked + * as changing in the message. */ - r = sd_bus_message_open_container(m, 'e', "sv"); - if (r < 0) - return r; + for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) { + if (v->type != _SD_BUS_VTABLE_PROPERTY && v->type != _SD_BUS_VTABLE_WRITABLE_PROPERTY) + continue; - r = sd_bus_message_append(m, "s", *property); - if (r < 0) - return r; + if (v->flags & SD_BUS_VTABLE_HIDDEN) + continue; - r = sd_bus_message_open_container(m, 'v', v->vtable->x.property.signature); - if (r < 0) - return r; + if (v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) { + has_invalidating = true; + continue; + } - r = invoke_property_get(bus, v->vtable, m->path, interface, *property, m, vtable_property_convert_userdata(v->vtable, u), &error); - if (r < 0) - return r; - if (sd_bus_error_is_set(&error)) - return sd_bus_error_get_errno(&error); - if (bus->nodes_modified) - return 0; + if (!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE)) + continue; - r = sd_bus_message_close_container(m); - if (r < 0) - return r; + has_changing = true; - r = sd_bus_message_close_container(m); - if (r < 0) - return r; + r = vtable_append_one_property(bus, m, m->path, c, v, u, &error); + if (r < 0) + return r; + if (bus->nodes_modified) + return 0; + } } } @@ -1977,7 +2127,7 @@ static int emit_properties_changed_on_interface( if (!streq(c->interface, interface)) continue; - r = node_vtable_get_userdata(bus, path, c, &u); + r = node_vtable_get_userdata(bus, path, c, &u, &error); if (r < 0) return r; if (bus->nodes_modified) @@ -1985,19 +2135,38 @@ static int emit_properties_changed_on_interface( if (r == 0) continue; - STRV_FOREACH(property, names) { - struct vtable_member *v; + if (names) { + STRV_FOREACH(property, names) { + struct vtable_member *v; - key.member = *property; - assert_se(v = hashmap_get(bus->vtable_properties, &key)); - assert(c == v->parent); + key.member = *property; + assert_se(v = hashmap_get(bus->vtable_properties, &key)); + assert(c == v->parent); - if (!(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_INVALIDATE_ONLY)) - continue; + if (!(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) + continue; - r = sd_bus_message_append(m, "s", *property); - if (r < 0) - return r; + r = sd_bus_message_append(m, "s", *property); + if (r < 0) + return r; + } + } else { + const sd_bus_vtable *v; + + for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) { + if (v->type != _SD_BUS_VTABLE_PROPERTY && v->type != _SD_BUS_VTABLE_WRITABLE_PROPERTY) + continue; + + if (v->flags & SD_BUS_VTABLE_HIDDEN) + continue; + + if (!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) + continue; + + r = sd_bus_message_append(m, "s", v->x.property.member); + if (r < 0) + return r; + } } } } @@ -2020,6 +2189,7 @@ _public_ int sd_bus_emit_properties_changed_strv( char **names) { BUS_DONT_DESTROY(bus); + bool found_interface = false; char *prefix; int r; @@ -2029,13 +2199,18 @@ _public_ int sd_bus_emit_properties_changed_strv( assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN); assert_return(!bus_pid_changed(bus), -ECHILD); - if (strv_isempty(names)) + + /* A non-NULL but empty names list means nothing needs to be + generated. A NULL list OTOH indicates that all properties + that are set to EMITS_CHANGE or EMITS_INVALIDATION shall be + included in the PropertiesChanged message. */ + if (names && names[0] == NULL) return 0; do { bus->nodes_modified = false; - r = emit_properties_changed_on_interface(bus, path, path, interface, false, names); + r = emit_properties_changed_on_interface(bus, path, path, interface, false, &found_interface, names); if (r != 0) return r; if (bus->nodes_modified) @@ -2043,7 +2218,7 @@ _public_ int sd_bus_emit_properties_changed_strv( prefix = alloca(strlen(path) + 1); OBJECT_PATH_FOREACH_PREFIX(prefix, path) { - r = emit_properties_changed_on_interface(bus, prefix, path, interface, true, names); + r = emit_properties_changed_on_interface(bus, prefix, path, interface, true, &found_interface, names); if (r != 0) return r; if (bus->nodes_modified) @@ -2052,7 +2227,7 @@ _public_ int sd_bus_emit_properties_changed_strv( } while (bus->nodes_modified); - return -ENOENT; + return found_interface ? 0 : -ENOENT; } _public_ int sd_bus_emit_properties_changed( @@ -2109,7 +2284,7 @@ static int interfaces_added_append_one_prefix( if (!streq(c->interface, interface)) continue; - r = node_vtable_get_userdata(bus, path, c, &u); + r = node_vtable_get_userdata(bus, path, c, &u, &error); if (r < 0) return r; if (bus->nodes_modified)