HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

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

106,749 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: aa371cceca8fed558554de425a5b7afabbd09d5b

Lines 2001–3000 of 3,204

                        m->path = n->path;
                        m->interface = s->node_vtable.interface;
                        m->member = v->x.method.member;
                        m->vtable = v;
 
                        r = set_ensure_put(&bus->vtable_methods, &vtable_member_hash_ops, m);
                        if (r == 0)
                                r = -EEXIST;
                        if (r < 0) {
                                free(m);
                                goto fail;
                        }
 
                        break;
                }
 
                case _SD_BUS_VTABLE_WRITABLE_PROPERTY:
 
                        if (!(v->x.property.set || bus_type_is_basic(v->x.property.signature[0]))) {
                                r = -EINVAL;
                                goto fail;
                        }
 
                        if (v->flags & SD_BUS_VTABLE_PROPERTY_CONST) {
                                r = -EINVAL;
                                goto fail;
                        }
 
                        _fallthrough_;
                case _SD_BUS_VTABLE_PROPERTY: {
                        BusVTableMember *m;
 
                        if (!member_name_is_valid(v->x.property.member) ||
                            !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_CONST) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) > 1 ||
                            ((v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) && (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)) ||
                            (v->flags & SD_BUS_VTABLE_UNPRIVILEGED && v->type == _SD_BUS_VTABLE_PROPERTY)) {
                                r = -EINVAL;
                                goto fail;
                        }
 
                        m = new0(BusVTableMember, 1);
                        if (!m) {
                                r = -ENOMEM;
                                goto fail;
                        }
 
                        m->parent = &s->node_vtable;
                        m->path = n->path;
                        m->interface = s->node_vtable.interface;
                        m->member = v->x.property.member;
                        m->vtable = v;
 
                        r = set_ensure_put(&bus->vtable_properties, &vtable_member_hash_ops, m);
                        if (r == 0)
                                r = -EEXIST;
                        if (r < 0) {
                                free(m);
                                goto fail;
                        }
 
                        break;
                }
 
                case _SD_BUS_VTABLE_SIGNAL:
                        nf = NAMES_SINGLE_PART;
 
                        if (bus_vtable_has_names(vtable))
                                names = strempty(v->x.signal.names);
 
                        if (!member_name_is_valid(v->x.signal.member) ||
                            !signature_is_valid(strempty(v->x.signal.signature), false) ||
                            !names_are_valid(strempty(v->x.signal.signature), &names, &nf) ||
                            v->flags & SD_BUS_VTABLE_UNPRIVILEGED) {
                                r = -EINVAL;
                                goto fail;
                        }
 
                        break;
 
                default:
                        r = -EINVAL;
                        goto fail;
                }
        }
 
        if (!existing)
                existing = LIST_FIND_TAIL(vtables, n->vtables);
 
        s->node_vtable.node = n;
        LIST_INSERT_AFTER(vtables, n->vtables, existing, &s->node_vtable);
        bus->nodes_modified = true;
 
        if (ret_slot)
                *ret_slot = s;
 
        return 0;
 
fail:
        /* Remove members we already inserted into the global tables before failing (identified by
         * parent), so they don't dangle once the slot and node are freed below. */
        if (s && s->node_vtable.interface)
                for (v = bus_vtable_next(vtable, vtable); v->type != _SD_BUS_VTABLE_END; v = bus_vtable_next(vtable, v)) {
                        BusVTableMember key, *x;
                        Set *set;
 
                        if (v->type == _SD_BUS_VTABLE_METHOD) {
                                set = bus->vtable_methods;
                                key.member = v->x.method.member;
                        } else if (IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY)) {
                                set = bus->vtable_properties;
                                key.member = v->x.property.member;
                        } else
                                continue;
 
                        /* The entry that failed validation may carry a NULL member, which was never
                         * inserted and would crash the hashmap lookup below. Skip it. */
                        if (isempty(key.member))
                                continue;
 
                        key.path = n->path;
                        key.interface = s->node_vtable.interface;
 
                        x = set_get(set, &key);
                        if (x && x->parent == &s->node_vtable) {
                                assert_se(set_remove(set, &key) == x);
                                free(x);
                        }
                }
 
        sd_bus_slot_unref(s);
        bus_node_gc(bus, n);
 
        return r;
}
 
/* This symbol exists solely to tell the linker that the "new" vtable format is used. */
_public_ const unsigned sd_bus_object_vtable_format = 242;
 
_public_ int sd_bus_add_object_vtable(
                sd_bus *bus,
                sd_bus_slot **ret_slot,
                const char *path,
                const char *interface,
                const sd_bus_vtable *vtable,
                void *userdata) {
 
        return add_object_vtable_internal(bus, ret_slot, path, interface, vtable, false, NULL, userdata);
}
 
_public_ int sd_bus_add_fallback_vtable(
                sd_bus *bus,
                sd_bus_slot **ret_slot,
                const char *prefix,
                const char *interface,
                const sd_bus_vtable *vtable,
                sd_bus_object_find_t find,
                void *userdata) {
 
        return add_object_vtable_internal(bus, ret_slot, prefix, interface, vtable, true, find, userdata);
}
 
_public_ int sd_bus_add_node_enumerator(
                sd_bus *bus,
                sd_bus_slot **ret_slot,
                const char *path,
                sd_bus_node_enumerator_t callback,
                void *userdata) {
 
        sd_bus_slot *s;
        BusNode *n;
        int r;
 
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(object_path_is_valid(path), -EINVAL);
        assert_return(callback, -EINVAL);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        n = bus_node_allocate(bus, path);
        if (!n)
                return -ENOMEM;
 
        s = bus_slot_allocate(bus, !ret_slot, BUS_NODE_ENUMERATOR, sizeof(BusNodeEnumerator), userdata);
        if (!s) {
                r = -ENOMEM;
                goto fail;
        }
 
        s->node_enumerator.callback = callback;
 
        s->node_enumerator.node = n;
        LIST_PREPEND(enumerators, n->enumerators, &s->node_enumerator);
        bus->nodes_modified = true;
 
        if (ret_slot)
                *ret_slot = s;
 
        return 0;
 
fail:
        sd_bus_slot_unref(s);
        bus_node_gc(bus, n);
 
        return r;
}
 
static int emit_properties_changed_on_interface(
                sd_bus *bus,
                const char *prefix,
                const char *path,
                const char *interface,
                bool require_fallback,
                bool *found_interface,
                char **names) {
 
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
        bool has_invalidating = false, has_changing = false;
        BusVTableMember key = {};
        BusNode *n;
        void *u = NULL;
        int r;
 
        assert(bus);
        assert(prefix);
        assert(path);
        assert(interface);
        assert(found_interface);
 
        n = hashmap_get(bus->nodes, prefix);
        if (!n)
                return 0;
 
        r = sd_bus_message_new_signal(bus, &m, path, "org.freedesktop.DBus.Properties", "PropertiesChanged");
        if (r < 0)
                return r;
 
        r = sd_bus_message_append(m, "s", interface);
        if (r < 0)
                return r;
 
        r = sd_bus_message_open_container(m, 'a', "{sv}");
        if (r < 0)
                return r;
 
        key.path = prefix;
        key.interface = interface;
 
        LIST_FOREACH(vtables, c, n->vtables) {
                if (require_fallback && !c->is_fallback)
                        continue;
 
                if (!streq(c->interface, interface))
                        continue;
 
                r = node_vtable_get_userdata(bus, path, c, &u, &error);
                if (r < 0)
                        return r;
                if (bus->nodes_modified)
                        return 0;
                if (r == 0)
                        continue;
 
                *found_interface = true;
 
                if (names) {
                        /* If the caller specified a list of
                         * properties we include exactly those in the
                         * PropertiesChanged message */
 
                        STRV_FOREACH(property, names) {
                                BusVTableMember *v;
 
                                assert_return(member_name_is_valid(*property), -EINVAL);
 
                                key.member = *property;
                                v = set_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 ||
                                              v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION, -EDOM);
 
                                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;
 
                        /* If the caller specified no properties list
                         * we include all properties that are marked
                         * as changing in the message. */
 
                        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;
 
                                if (v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) {
                                        has_invalidating = true;
                                        continue;
                                }
 
                                if (!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))
                                        continue;
 
                                has_changing = true;
 
                                r = vtable_append_one_property(bus, m, m->path, c, v, u, &error);
                                if (r < 0)
                                        return r;
                                if (bus->nodes_modified)
                                        return 0;
                        }
                }
        }
 
        if (!has_invalidating && !has_changing)
                return 0;
 
        r = sd_bus_message_close_container(m);
        if (r < 0)
                return r;
 
        r = sd_bus_message_open_container(m, 'a', "s");
        if (r < 0)
                return r;
 
        if (has_invalidating) {
                LIST_FOREACH(vtables, c, n->vtables) {
                        if (require_fallback && !c->is_fallback)
                                continue;
 
                        if (!streq(c->interface, interface))
                                continue;
 
                        r = node_vtable_get_userdata(bus, path, c, &u, &error);
                        if (r < 0)
                                return r;
                        if (bus->nodes_modified)
                                return 0;
                        if (r == 0)
                                continue;
 
                        if (names) {
                                STRV_FOREACH(property, names) {
                                        BusVTableMember *v;
 
                                        key.member = *property;
                                        assert_se(v = set_get(bus->vtable_properties, &key));
                                        assert(c == v->parent);
 
                                        if (!(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION))
                                                continue;
 
                                        r = sd_bus_message_append(m, "s", *property);
                                        if (r < 0)
                                                return r;
                                }
                        } else {
                                const sd_bus_vtable *v;
 
                                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;
 
                                        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;
                                }
                        }
                }
        }
 
        r = sd_bus_message_close_container(m);
        if (r < 0)
                return r;
 
        r = sd_bus_send(bus, m, NULL);
        if (r < 0)
                return r;
 
        return 1;
}
 
_public_ int sd_bus_emit_properties_changed_strv(
                sd_bus *bus,
                const char *path,
                const char *interface,
                char **names) {
 
        _cleanup_free_ char *prefix = NULL;
        bool found_interface = false;
        size_t pl;
        int r;
 
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(object_path_is_valid(path), -EINVAL);
        assert_return(interface_name_is_valid(interface), -EINVAL);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        if (!BUS_IS_OPEN(bus->state))
                return -ENOTCONN;
 
        /* 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;
 
        BUS_DONT_DESTROY(bus);
 
        pl = strlen(path);
        assert(pl <= BUS_PATH_SIZE_MAX);
        prefix = new(char, pl + 1);
        if (!prefix)
                return -ENOMEM;
 
        do {
                bus->nodes_modified = false;
 
                r = emit_properties_changed_on_interface(bus, path, path, interface, false, &found_interface, names);
                if (r != 0)
                        return r;
                if (bus->nodes_modified)
                        continue;
 
                OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
                        r = emit_properties_changed_on_interface(bus, prefix, path, interface, true, &found_interface, names);
                        if (r != 0)
                                return r;
                        if (bus->nodes_modified)
                                break;
                }
 
        } while (bus->nodes_modified);
 
        return found_interface ? 0 : -ENOENT;
}
 
_public_ int sd_bus_emit_properties_changed(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *name, ...)  {
 
        _cleanup_strv_free_ char **names = NULL;
 
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(object_path_is_valid(path), -EINVAL);
        assert_return(interface_name_is_valid(interface), -EINVAL);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        if (!BUS_IS_OPEN(bus->state))
                return -ENOTCONN;
 
        if (!name)
                return 0;
 
        va_list ap;
 
        va_start(ap, name);
        names = strv_new_ap(name, ap);
        va_end(ap);
 
        if (!names)
                return -ENOMEM;
 
        return sd_bus_emit_properties_changed_strv(bus, path, interface, names);
}
 
static int object_added_append_all_prefix(
                sd_bus *bus,
                sd_bus_message *m,
                OrderedSet *s,
                const char *prefix,
                const char *path,
                bool require_fallback) {
 
        const char *previous_interface = NULL;
        BusNode *n;
        int r;
 
        assert(bus);
        assert(m);
        assert(s);
        assert(prefix);
        assert(path);
 
        n = hashmap_get(bus->nodes, prefix);
        if (!n)
                return 0;
 
        LIST_FOREACH(vtables, c, n->vtables) {
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
                void *u = NULL;
 
                if (require_fallback && !c->is_fallback)
                        continue;
 
                r = node_vtable_get_userdata(bus, path, c, &u, &error);
                if (r < 0)
                        return r;
                if (bus->nodes_modified)
                        return 0;
                if (r == 0)
                        continue;
 
                if (!streq_ptr(c->interface, previous_interface)) {
                        /* If a child-node already handled this interface, we
                         * skip it on any of its parents. The child vtables
                         * always fully override any conflicting vtables of
                         * any parent node. */
                        if (ordered_set_contains(s, c->interface))
                                continue;
 
                        r = ordered_set_put(s, c->interface);
                        if (r < 0)
                                return r;
 
                        if (previous_interface) {
                                r = sd_bus_message_close_container(m);
                                if (r < 0)
                                        return r;
                                r = sd_bus_message_close_container(m);
                                if (r < 0)
                                        return r;
                        }
 
                        r = sd_bus_message_open_container(m, 'e', "sa{sv}");
                        if (r < 0)
                                return r;
                        r = sd_bus_message_append(m, "s", c->interface);
                        if (r < 0)
                                return r;
                        r = sd_bus_message_open_container(m, 'a', "{sv}");
                        if (r < 0)
                                return r;
 
                        previous_interface = c->interface;
                }
 
                r = vtable_append_all_properties(bus, m, path, c, u, &error);
                if (r < 0)
                        return r;
                if (bus->nodes_modified)
                        return 0;
        }
 
        if (previous_interface) {
                r = sd_bus_message_close_container(m);
                if (r < 0)
                        return r;
                r = sd_bus_message_close_container(m);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static int object_added_append_all(sd_bus *bus, sd_bus_message *m, const char *path, bool path_has_object_manager) {
        _cleanup_ordered_set_free_ OrderedSet *s = NULL;
        _cleanup_free_ char *prefix = NULL;
        size_t pl;
        int r;
 
        assert(bus);
        assert(m);
        assert(path);
 
        /*
         * This appends all interfaces registered on path @path. We first add
         * the builtin interfaces, which are always available and handled by
         * sd-bus. Then, we add all interfaces registered on the exact node,
         * followed by all fallback interfaces registered on any parent prefix.
         *
         * If an interface is registered multiple times on the same node with
         * different vtables, we merge all the properties across all vtables.
         * However, if a child node has the same interface registered as one of
         * its parent nodes has as fallback, we make the child overwrite the
         * parent instead of extending it. Therefore, we keep a "Set" of all
         * handled interfaces during parent traversal, so we skip interfaces on
         * a parent that were overwritten by a child.
         */
 
        s = ordered_set_new(&string_hash_ops);
        if (!s)
                return -ENOMEM;
 
        r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
        if (r < 0)
                return r;
        r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
        if (r < 0)
                return r;
        r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
        if (r < 0)
                return r;
        if (path_has_object_manager){
                r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
                if (r < 0)
                        return r;
        }
 
        r = object_added_append_all_prefix(bus, m, s, path, path, false);
        if (r < 0)
                return r;
        if (bus->nodes_modified)
                return 0;
 
        pl = strlen(path);
        assert(pl <= BUS_PATH_SIZE_MAX);
        prefix = new(char, pl + 1);
        if (!prefix)
                return -ENOMEM;
 
        OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
                r = object_added_append_all_prefix(bus, m, s, prefix, path, true);
                if (r < 0)
                        return r;
                if (bus->nodes_modified)
                        return 0;
        }
 
        return 0;
}
 
_public_ int sd_bus_emit_object_added(sd_bus *bus, const char *path) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
        BusNode *object_manager;
        int r;
 
        /*
         * This emits an InterfacesAdded signal on the given path, by iterating
         * all registered vtables and fallback vtables on the path. All
         * properties are queried and included in the signal.
         * This call is equivalent to sd_bus_emit_interfaces_added() with an
         * explicit list of registered interfaces. However, unlike
         * interfaces_added(), this call can figure out the list of supported
         * interfaces itself. Furthermore, it properly adds the builtin
         * org.freedesktop.DBus.* interfaces.
         */
 
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(object_path_is_valid(path), -EINVAL);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        if (!BUS_IS_OPEN(bus->state))
                return -ENOTCONN;
 
        bool path_has_object_manager = false;
        r = bus_find_parent_object_manager(bus, &object_manager, path, &path_has_object_manager);
        if (r < 0)
                return r;
        if (r == 0)
                return -ESRCH;
 
        BUS_DONT_DESTROY(bus);
 
        do {
                bus->nodes_modified = false;
                m = sd_bus_message_unref(m);
 
                r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
                if (r < 0)
                        return r;
 
                r = sd_bus_message_append_basic(m, 'o', path);
                if (r < 0)
                        return r;
 
                r = sd_bus_message_open_container(m, 'a', "{sa{sv}}");
                if (r < 0)
                        return r;
 
                r = object_added_append_all(bus, m, path, path_has_object_manager);
                if (r < 0)
                        return r;
 
                if (bus->nodes_modified)
                        continue;
 
                r = sd_bus_message_close_container(m);
                if (r < 0)
                        return r;
 
        } while (bus->nodes_modified);
 
        return sd_bus_send(bus, m, NULL);
}
 
static int object_removed_append_all_prefix(
                sd_bus *bus,
                sd_bus_message *m,
                OrderedSet *s,
                const char *prefix,
                const char *path,
                bool require_fallback) {
 
        const char *previous_interface = NULL;
        BusNode *n;
        int r;
 
        assert(bus);
        assert(m);
        assert(s);
        assert(prefix);
        assert(path);
 
        n = hashmap_get(bus->nodes, prefix);
        if (!n)
                return 0;
 
        LIST_FOREACH(vtables, c, n->vtables) {
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
                void *u = NULL;
 
                if (require_fallback && !c->is_fallback)
                        continue;
                if (streq_ptr(c->interface, previous_interface))
                        continue;
 
                /* If a child-node already handled this interface, we
                 * skip it on any of its parents. The child vtables
                 * always fully override any conflicting vtables of
                 * any parent node. */
                if (ordered_set_contains(s, c->interface))
                        continue;
 
                r = node_vtable_get_userdata(bus, path, c, &u, &error);
                if (r < 0)
                        return r;
                if (bus->nodes_modified)
                        return 0;
                if (r == 0)
                        continue;
 
                r = ordered_set_put(s, c->interface);
                if (r < 0)
                        return r;
 
                r = sd_bus_message_append(m, "s", c->interface);
                if (r < 0)
                        return r;
 
                previous_interface = c->interface;
        }
 
        return 0;
}
 
static int object_removed_append_all(sd_bus *bus, sd_bus_message *m, const char *path, bool path_has_object_manager) {
        _cleanup_ordered_set_free_ OrderedSet *s = NULL;
        _cleanup_free_ char *prefix = NULL;
        size_t pl;
        int r;
 
        assert(bus);
        assert(m);
        assert(path);
 
        /* see sd_bus_emit_object_added() for details */
 
        s = ordered_set_new(&string_hash_ops);
        if (!s)
                return -ENOMEM;
 
        r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Peer");
        if (r < 0)
                return r;
        r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Introspectable");
        if (r < 0)
                return r;
        r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Properties");
        if (r < 0)
                return r;
 
        if (path_has_object_manager){
                r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.ObjectManager");
                if (r < 0)
                        return r;
        }
 
        r = object_removed_append_all_prefix(bus, m, s, path, path, false);
        if (r < 0)
                return r;
        if (bus->nodes_modified)
                return 0;
 
        pl = strlen(path);
        assert(pl <= BUS_PATH_SIZE_MAX);
        prefix = new(char, pl + 1);
        if (!prefix)
                return -ENOMEM;
 
        OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
                r = object_removed_append_all_prefix(bus, m, s, prefix, path, true);
                if (r < 0)
                        return r;
                if (bus->nodes_modified)
                        return 0;
        }
 
        return 0;
}
 
_public_ int sd_bus_emit_object_removed(sd_bus *bus, const char *path) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
        BusNode *object_manager;
        int r;
 
        /*
         * This is like sd_bus_emit_object_added(), but emits an
         * InterfacesRemoved signal on the given path. This only includes any
         * registered interfaces but skips the properties. Note that this will
         * call into the find() callbacks of any registered vtable. Therefore,
         * you must call this function before destroying/unlinking your object.
         * Otherwise, the list of interfaces will be incomplete. However, note
         * that this will *NOT* call into any property callback. Therefore, the
         * object might be in an "destructed" state, as long as we can find it.
         */
 
        assert_return(bus, -EINVAL);
        assert_return(bus = bus_resolve(bus), -ENOPKG);
        assert_return(object_path_is_valid(path), -EINVAL);
        assert_return(!bus_origin_changed(bus), -ECHILD);
 
        if (!BUS_IS_OPEN(bus->state))
                return -ENOTCONN;
 
        bool path_has_object_manager = false;
        r = bus_find_parent_object_manager(bus, &object_manager, path, &path_has_object_manager);
        if (r < 0)
                return r;
        if (r == 0)
                return -ESRCH;
 
        BUS_DONT_DESTROY(bus);
 
        do {
                bus->nodes_modified = false;
                m = sd_bus_message_unref(m);
 
                r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
                if (r < 0)
                        return r;
 
                r = sd_bus_message_append_basic(m, 'o', path);
                if (r < 0)
                        return r;
 
                r = sd_bus_message_open_container(m, 'a', "s");
                if (r < 0)
                        return r;
 
                r = object_removed_append_all(bus, m, path, path_has_object_manager);
                if (r < 0)
                        return r;
 
                if (bus->nodes_modified)
                        continue;
 
                r = sd_bus_message_close_container(m);
                if (r < 0)
                        return r;
 
        } while (bus->nodes_modified);
 
        return sd_bus_send(bus, m, NULL);
}
 
static int interfaces_added_append_one_prefix(
                sd_bus *bus,
                sd_bus_message *m,
                const char *prefix,
                const char *path,
                const char *interface,
                bool require_fallback) {
 
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        bool found_interface = false;
        BusNode *n;
        void *u = NULL;
        int r;
 
        assert(bus);
        assert(m);
        assert(prefix);
        assert(path);
        assert(interface);
 
        n = hashmap_get(bus->nodes, prefix);
        if (!n)
                return 0;
 
        LIST_FOREACH(vtables, c, n->vtables) {
                if (require_fallback && !c->is_fallback)
                        continue;
 
                if (!streq(c->interface, interface))
                        continue;
 
                r = node_vtable_get_userdata(bus, path, c, &u, &error);
                if (r < 0)
                        return r;
                if (bus->nodes_modified)
                        return 0;
                if (r == 0)
                        continue;
 
                if (!found_interface) {
                        r = sd_bus_message_append_basic(m, 's', interface);
                        if (r < 0)
                                return r;
 
                        r = sd_bus_message_open_container(m, 'a', "{sv}");
                        if (r < 0)
                                return r;
 
                        found_interface = true;
                }
 
                r = vtable_append_all_properties(bus, m, path, c, u, &error);
                if (r < 0)
                        return r;
                if (bus->nodes_modified)
                        return 0;
        }
 
        if (found_interface) {
                r = sd_bus_message_close_container(m);
                if (r < 0)
                        return r;
        }
 
        return found_interface;
}
 
static int interfaces_added_append_one(
                sd_bus *bus,
                sd_bus_message *m,
                const char *path,
                const char *interface) {
 
        _cleanup_free_ char *prefix = NULL;
        size_t pl;
        int r;
 
        assert(bus);
        assert(m);
        assert(path);
        assert(interface);
 
        r = interfaces_added_append_one_prefix(bus, m, path, path, interface, false);
        if (r != 0)
                return r;
        if (bus->nodes_modified)
                return 0;
 
        pl = strlen(path);
        assert(pl <= BUS_PATH_SIZE_MAX);
        prefix = new(char, pl + 1);
        if (!prefix)
                return -ENOMEM;