2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
21 #include "bus-internal.h"
22 #include "bus-introspect.h"
23 #include "bus-message.h"
24 #include "bus-objects.h"
25 #include "bus-signature.h"
30 #include "string-util.h"
33 static int node_vtable_get_userdata(
36 struct node_vtable *c,
38 sd_bus_error *error) {
48 s = container_of(c, sd_bus_slot, node_vtable);
51 bus->current_slot = sd_bus_slot_ref(s);
52 bus->current_userdata = u;
53 r = c->find(bus, path, c->interface, u, &found_u, error);
54 bus->current_userdata = NULL;
55 bus->current_slot = sd_bus_slot_unref(s);
59 if (sd_bus_error_is_set(error))
60 return -sd_bus_error_get_errno(error);
72 static void *vtable_method_convert_userdata(const sd_bus_vtable *p, void *u) {
75 return (uint8_t*) u + p->x.method.offset;
78 static void *vtable_property_convert_userdata(const sd_bus_vtable *p, void *u) {
81 return (uint8_t*) u + p->x.property.offset;
84 static int vtable_property_get_userdata(
87 struct vtable_member *p,
89 sd_bus_error *error) {
99 r = node_vtable_get_userdata(bus, path, p->parent, &u, error);
102 if (bus->nodes_modified)
105 *userdata = vtable_property_convert_userdata(p->vtable, u);
109 static int add_enumerated_to_set(
112 struct node_enumerator *first,
114 sd_bus_error *error) {
116 struct node_enumerator *c;
123 LIST_FOREACH(enumerators, c, first) {
124 char **children = NULL, **k;
127 if (bus->nodes_modified)
130 slot = container_of(c, sd_bus_slot, node_enumerator);
132 bus->current_slot = sd_bus_slot_ref(slot);
133 bus->current_userdata = slot->userdata;
134 r = c->callback(bus, prefix, slot->userdata, &children, error);
135 bus->current_userdata = NULL;
136 bus->current_slot = sd_bus_slot_unref(slot);
140 if (sd_bus_error_is_set(error))
141 return -sd_bus_error_get_errno(error);
143 STRV_FOREACH(k, children) {
149 if (!object_path_is_valid(*k)) {
155 if (!object_path_startswith(*k, prefix)) {
160 r = set_consume(s, *k);
174 /* if set, add_subtree() works recursively */
175 CHILDREN_RECURSIVE = (1U << 1),
176 /* if set, add_subtree() scans object-manager hierarchies recursively */
177 CHILDREN_SUBHIERARCHIES = (1U << 0),
180 static int add_subtree_to_set(
186 sd_bus_error *error) {
196 r = add_enumerated_to_set(bus, prefix, n->enumerators, s, error);
199 if (bus->nodes_modified)
202 LIST_FOREACH(siblings, i, n->child) {
205 if (!object_path_startswith(i->path, prefix))
212 r = set_consume(s, t);
213 if (r < 0 && r != -EEXIST)
216 if ((flags & CHILDREN_RECURSIVE) &&
217 ((flags & CHILDREN_SUBHIERARCHIES) || !i->object_managers)) {
218 r = add_subtree_to_set(bus, prefix, i, flags, s, error);
221 if (bus->nodes_modified)
229 static int get_child_nodes(
235 sd_bus_error *error) {
245 s = set_new(&string_hash_ops);
249 r = add_subtree_to_set(bus, prefix, n, flags, s, error);
259 static int node_callbacks_run(
262 struct node_callback *first,
263 bool require_fallback,
264 bool *found_object) {
266 struct node_callback *c;
271 assert(found_object);
273 LIST_FOREACH(callbacks, c, first) {
274 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
277 if (bus->nodes_modified)
280 if (require_fallback && !c->is_fallback)
283 *found_object = true;
285 if (c->last_iteration == bus->iteration_counter)
288 c->last_iteration = bus->iteration_counter;
290 r = sd_bus_message_rewind(m, true);
294 slot = container_of(c, sd_bus_slot, node_callback);
296 bus->current_slot = sd_bus_slot_ref(slot);
297 bus->current_handler = c->callback;
298 bus->current_userdata = slot->userdata;
299 r = c->callback(m, slot->userdata, &error_buffer);
300 bus->current_userdata = NULL;
301 bus->current_handler = NULL;
302 bus->current_slot = sd_bus_slot_unref(slot);
304 r = bus_maybe_reply_error(m, r, &error_buffer);
312 #define CAPABILITY_SHIFT(x) (((x) >> __builtin_ctzll(_SD_BUS_VTABLE_CAPABILITY_MASK)) & 0xFFFF)
314 static int check_access(sd_bus *bus, sd_bus_message *m, struct vtable_member *c, sd_bus_error *error) {
322 /* If the entire bus is trusted let's grant access */
326 /* If the member is marked UNPRIVILEGED let's grant access */
327 if (c->vtable->flags & SD_BUS_VTABLE_UNPRIVILEGED)
330 /* Check have the caller has the requested capability
331 * set. Note that the flags value contains the capability
332 * number plus one, which we need to subtract here. We do this
333 * so that we have 0 as special value for "default
335 cap = CAPABILITY_SHIFT(c->vtable->flags);
337 cap = CAPABILITY_SHIFT(c->parent->vtable[0].flags);
343 r = sd_bus_query_sender_privilege(m, cap);
349 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Access to %s.%s() not permitted.", c->interface, c->member);
352 static int method_callbacks_run(
355 struct vtable_member *c,
356 bool require_fallback,
357 bool *found_object) {
359 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
360 const char *signature;
367 assert(found_object);
369 if (require_fallback && !c->parent->is_fallback)
372 r = check_access(bus, m, c, &error);
374 return bus_maybe_reply_error(m, r, &error);
376 r = node_vtable_get_userdata(bus, m->path, c->parent, &u, &error);
378 return bus_maybe_reply_error(m, r, &error);
379 if (bus->nodes_modified)
382 u = vtable_method_convert_userdata(c->vtable, u);
384 *found_object = true;
386 if (c->last_iteration == bus->iteration_counter)
389 c->last_iteration = bus->iteration_counter;
391 r = sd_bus_message_rewind(m, true);
395 signature = sd_bus_message_get_signature(m, true);
399 if (!streq(strempty(c->vtable->x.method.signature), signature))
400 return sd_bus_reply_method_errorf(
402 SD_BUS_ERROR_INVALID_ARGS,
403 "Invalid arguments '%s' to call %s.%s(), expecting '%s'.",
404 signature, c->interface, c->member, strempty(c->vtable->x.method.signature));
406 /* Keep track what the signature of the reply to this message
407 * should be, so that this can be enforced when sealing the
409 m->enforced_reply_signature = strempty(c->vtable->x.method.result);
411 if (c->vtable->x.method.handler) {
414 slot = container_of(c->parent, sd_bus_slot, node_vtable);
416 bus->current_slot = sd_bus_slot_ref(slot);
417 bus->current_handler = c->vtable->x.method.handler;
418 bus->current_userdata = u;
419 r = c->vtable->x.method.handler(m, u, &error);
420 bus->current_userdata = NULL;
421 bus->current_handler = NULL;
422 bus->current_slot = sd_bus_slot_unref(slot);
424 return bus_maybe_reply_error(m, r, &error);
427 /* If the method callback is NULL, make this a successful NOP */
428 r = sd_bus_reply_method_return(m, NULL);
435 static int invoke_property_get(
438 const sd_bus_vtable *v,
440 const char *interface,
441 const char *property,
442 sd_bus_message *reply,
444 sd_bus_error *error) {
457 if (v->x.property.get) {
459 bus->current_slot = sd_bus_slot_ref(slot);
460 bus->current_userdata = userdata;
461 r = v->x.property.get(bus, path, interface, property, reply, userdata, error);
462 bus->current_userdata = NULL;
463 bus->current_slot = sd_bus_slot_unref(slot);
467 if (sd_bus_error_is_set(error))
468 return -sd_bus_error_get_errno(error);
472 /* Automatic handling if no callback is defined. */
474 if (streq(v->x.property.signature, "as"))
475 return sd_bus_message_append_strv(reply, *(char***) userdata);
477 assert(signature_is_single(v->x.property.signature, false));
478 assert(bus_type_is_basic(v->x.property.signature[0]));
480 switch (v->x.property.signature[0]) {
482 case SD_BUS_TYPE_STRING:
483 case SD_BUS_TYPE_SIGNATURE:
484 p = strempty(*(char**) userdata);
487 case SD_BUS_TYPE_OBJECT_PATH:
488 p = *(char**) userdata;
497 return sd_bus_message_append_basic(reply, v->x.property.signature[0], p);
500 static int invoke_property_set(
503 const sd_bus_vtable *v,
505 const char *interface,
506 const char *property,
507 sd_bus_message *value,
509 sd_bus_error *error) {
521 if (v->x.property.set) {
523 bus->current_slot = sd_bus_slot_ref(slot);
524 bus->current_userdata = userdata;
525 r = v->x.property.set(bus, path, interface, property, value, userdata, error);
526 bus->current_userdata = NULL;
527 bus->current_slot = sd_bus_slot_unref(slot);
531 if (sd_bus_error_is_set(error))
532 return -sd_bus_error_get_errno(error);
536 /* Automatic handling if no callback is defined. */
538 assert(signature_is_single(v->x.property.signature, false));
539 assert(bus_type_is_basic(v->x.property.signature[0]));
541 switch (v->x.property.signature[0]) {
543 case SD_BUS_TYPE_STRING:
544 case SD_BUS_TYPE_OBJECT_PATH:
545 case SD_BUS_TYPE_SIGNATURE: {
549 r = sd_bus_message_read_basic(value, v->x.property.signature[0], &p);
557 free(*(char**) userdata);
558 *(char**) userdata = n;
564 r = sd_bus_message_read_basic(value, v->x.property.signature[0], userdata);
574 static int property_get_set_callbacks_run(
577 struct vtable_member *c,
578 bool require_fallback,
580 bool *found_object) {
582 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
583 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
591 assert(found_object);
593 if (require_fallback && !c->parent->is_fallback)
596 r = vtable_property_get_userdata(bus, m->path, c, &u, &error);
598 return bus_maybe_reply_error(m, r, &error);
599 if (bus->nodes_modified)
602 slot = container_of(c->parent, sd_bus_slot, node_vtable);
604 *found_object = true;
606 r = sd_bus_message_new_method_return(m, &reply);
611 /* Note that we do not protect against reexecution
612 * here (using the last_iteration check, see below),
613 * should the node tree have changed and we got called
614 * again. We assume that property Get() calls are
615 * ultimately without side-effects or if they aren't
616 * then at least idempotent. */
618 r = sd_bus_message_open_container(reply, 'v', c->vtable->x.property.signature);
622 /* Note that we do not do an access check here. Read
623 * access to properties is always unrestricted, since
624 * PropertiesChanged signals broadcast contents
627 r = invoke_property_get(bus, slot, c->vtable, m->path, c->interface, c->member, reply, u, &error);
629 return bus_maybe_reply_error(m, r, &error);
631 if (bus->nodes_modified)
634 r = sd_bus_message_close_container(reply);
639 const char *signature = NULL;
642 if (c->vtable->type != _SD_BUS_VTABLE_WRITABLE_PROPERTY)
643 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Property '%s' is not writable.", c->member);
645 /* Avoid that we call the set routine more than once
646 * if the processing of this message got restarted
647 * because the node tree changed. */
648 if (c->last_iteration == bus->iteration_counter)
651 c->last_iteration = bus->iteration_counter;
653 r = sd_bus_message_peek_type(m, &type, &signature);
657 if (type != 'v' || !streq(strempty(signature), strempty(c->vtable->x.property.signature)))
658 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));
660 r = sd_bus_message_enter_container(m, 'v', c->vtable->x.property.signature);
664 r = check_access(bus, m, c, &error);
666 return bus_maybe_reply_error(m, r, &error);
668 r = invoke_property_set(bus, slot, c->vtable, m->path, c->interface, c->member, m, u, &error);
670 return bus_maybe_reply_error(m, r, &error);
672 if (bus->nodes_modified)
675 r = sd_bus_message_exit_container(m);
680 r = sd_bus_send(bus, reply, NULL);
687 static int vtable_append_one_property(
689 sd_bus_message *reply,
691 struct node_vtable *c,
692 const sd_bus_vtable *v,
694 sd_bus_error *error) {
705 r = sd_bus_message_open_container(reply, 'e', "sv");
709 r = sd_bus_message_append(reply, "s", v->x.property.member);
713 r = sd_bus_message_open_container(reply, 'v', v->x.property.signature);
717 slot = container_of(c, sd_bus_slot, node_vtable);
719 r = invoke_property_get(bus, slot, v, path, c->interface, v->x.property.member, reply, vtable_property_convert_userdata(v, userdata), error);
722 if (bus->nodes_modified)
725 r = sd_bus_message_close_container(reply);
729 r = sd_bus_message_close_container(reply);
736 static int vtable_append_all_properties(
738 sd_bus_message *reply,
740 struct node_vtable *c,
742 sd_bus_error *error) {
744 const sd_bus_vtable *v;
752 if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN)
755 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
756 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
759 if (v->flags & SD_BUS_VTABLE_HIDDEN)
762 if (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)
765 r = vtable_append_one_property(bus, reply, path, c, v, userdata, error);
768 if (bus->nodes_modified)
775 static int property_get_all_callbacks_run(
778 struct node_vtable *first,
779 bool require_fallback,
781 bool *found_object) {
783 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
784 struct node_vtable *c;
785 bool found_interface;
790 assert(found_object);
792 r = sd_bus_message_new_method_return(m, &reply);
796 r = sd_bus_message_open_container(reply, 'a', "{sv}");
800 found_interface = !iface ||
801 streq(iface, "org.freedesktop.DBus.Properties") ||
802 streq(iface, "org.freedesktop.DBus.Peer") ||
803 streq(iface, "org.freedesktop.DBus.Introspectable");
805 LIST_FOREACH(vtables, c, first) {
806 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
809 if (require_fallback && !c->is_fallback)
812 r = node_vtable_get_userdata(bus, m->path, c, &u, &error);
814 return bus_maybe_reply_error(m, r, &error);
815 if (bus->nodes_modified)
820 *found_object = true;
822 if (iface && !streq(c->interface, iface))
824 found_interface = true;
826 r = vtable_append_all_properties(bus, reply, m->path, c, u, &error);
828 return bus_maybe_reply_error(m, r, &error);
829 if (bus->nodes_modified)
836 if (!found_interface) {
837 r = sd_bus_reply_method_errorf(
839 SD_BUS_ERROR_UNKNOWN_INTERFACE,
840 "Unknown interface '%s'.", iface);
847 r = sd_bus_message_close_container(reply);
851 r = sd_bus_send(bus, reply, NULL);
858 static int bus_node_exists(
862 bool require_fallback) {
864 struct node_vtable *c;
865 struct node_callback *k;
872 /* Tests if there's anything attached directly to this node
873 * for the specified path */
875 if (!require_fallback && (n->enumerators || n->object_managers))
878 LIST_FOREACH(callbacks, k, n->callbacks) {
879 if (require_fallback && !k->is_fallback)
885 LIST_FOREACH(vtables, c, n->vtables) {
886 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
888 if (require_fallback && !c->is_fallback)
891 r = node_vtable_get_userdata(bus, path, c, NULL, &error);
894 if (bus->nodes_modified)
901 static int process_introspect(
905 bool require_fallback,
906 bool *found_object) {
908 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
909 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
910 _cleanup_set_free_free_ Set *s = NULL;
911 const char *previous_interface = NULL;
912 struct introspect intro;
913 struct node_vtable *c;
920 assert(found_object);
922 r = get_child_nodes(bus, m->path, n, 0, &s, &error);
924 return bus_maybe_reply_error(m, r, &error);
925 if (bus->nodes_modified)
928 r = introspect_begin(&intro, bus->trusted);
932 r = introspect_write_default_interfaces(&intro, !require_fallback && n->object_managers);
936 empty = set_isempty(s);
938 LIST_FOREACH(vtables, c, n->vtables) {
939 if (require_fallback && !c->is_fallback)
942 r = node_vtable_get_userdata(bus, m->path, c, NULL, &error);
944 r = bus_maybe_reply_error(m, r, &error);
947 if (bus->nodes_modified) {
956 if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN)
959 if (!streq_ptr(previous_interface, c->interface)) {
961 if (previous_interface)
962 fputs_unlocked(" </interface>\n", intro.f);
964 fprintf(intro.f, " <interface name=\"%s\">\n", c->interface);
967 r = introspect_write_interface(&intro, c->vtable);
971 previous_interface = c->interface;
974 if (previous_interface)
975 fputs_unlocked(" </interface>\n", intro.f);
978 /* Nothing?, let's see if we exist at all, and if not
979 * refuse to do anything */
980 r = bus_node_exists(bus, n, m->path, require_fallback);
982 r = bus_maybe_reply_error(m, r, &error);
985 if (bus->nodes_modified) {
991 *found_object = true;
993 r = introspect_write_child_nodes(&intro, s, m->path);
997 r = introspect_finish(&intro, bus, m, &reply);
1001 r = sd_bus_send(bus, reply, NULL);
1008 introspect_free(&intro);
1012 static int object_manager_serialize_path(
1014 sd_bus_message *reply,
1017 bool require_fallback,
1018 sd_bus_error *error) {
1020 const char *previous_interface = NULL;
1021 bool found_something = false;
1022 struct node_vtable *i;
1032 n = hashmap_get(bus->nodes, prefix);
1036 LIST_FOREACH(vtables, i, n->vtables) {
1039 if (require_fallback && !i->is_fallback)
1042 r = node_vtable_get_userdata(bus, path, i, &u, error);
1045 if (bus->nodes_modified)
1050 if (!found_something) {
1052 /* Open the object part */
1054 r = sd_bus_message_open_container(reply, 'e', "oa{sa{sv}}");
1058 r = sd_bus_message_append(reply, "o", path);
1062 r = sd_bus_message_open_container(reply, 'a', "{sa{sv}}");
1066 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
1070 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
1074 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
1078 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
1082 found_something = true;
1085 if (!streq_ptr(previous_interface, i->interface)) {
1087 /* Maybe close the previous interface part */
1089 if (previous_interface) {
1090 r = sd_bus_message_close_container(reply);
1094 r = sd_bus_message_close_container(reply);
1099 /* Open the new interface part */
1101 r = sd_bus_message_open_container(reply, 'e', "sa{sv}");
1105 r = sd_bus_message_append(reply, "s", i->interface);
1109 r = sd_bus_message_open_container(reply, 'a', "{sv}");
1114 r = vtable_append_all_properties(bus, reply, path, i, u, error);
1117 if (bus->nodes_modified)
1120 previous_interface = i->interface;
1123 if (previous_interface) {
1124 r = sd_bus_message_close_container(reply);
1128 r = sd_bus_message_close_container(reply);
1133 if (found_something) {
1134 r = sd_bus_message_close_container(reply);
1138 r = sd_bus_message_close_container(reply);
1146 static int object_manager_serialize_path_and_fallbacks(
1148 sd_bus_message *reply,
1150 sd_bus_error *error) {
1160 /* First, add all vtables registered for this path */
1161 r = object_manager_serialize_path(bus, reply, path, path, false, error);
1164 if (bus->nodes_modified)
1167 /* Second, add fallback vtables registered for any of the prefixes */
1168 prefix = alloca(strlen(path) + 1);
1169 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
1170 r = object_manager_serialize_path(bus, reply, prefix, path, true, error);
1173 if (bus->nodes_modified)
1180 static int process_get_managed_objects(
1184 bool require_fallback,
1185 bool *found_object) {
1187 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1188 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1189 _cleanup_set_free_free_ Set *s = NULL;
1197 assert(found_object);
1199 /* Spec says, GetManagedObjects() is only implemented on the root of a
1200 * sub-tree. Therefore, we require a registered object-manager on
1201 * exactly the queried path, otherwise, we refuse to respond. */
1203 if (require_fallback || !n->object_managers)
1206 r = get_child_nodes(bus, m->path, n, CHILDREN_RECURSIVE, &s, &error);
1208 return bus_maybe_reply_error(m, r, &error);
1209 if (bus->nodes_modified)
1212 r = sd_bus_message_new_method_return(m, &reply);
1216 r = sd_bus_message_open_container(reply, 'a', "{oa{sa{sv}}}");
1220 SET_FOREACH(path, s, i) {
1221 r = object_manager_serialize_path_and_fallbacks(bus, reply, path, &error);
1223 return bus_maybe_reply_error(m, r, &error);
1225 if (bus->nodes_modified)
1229 r = sd_bus_message_close_container(reply);
1233 r = sd_bus_send(bus, reply, NULL);
1240 static int object_find_and_run(
1244 bool require_fallback,
1245 bool *found_object) {
1248 struct vtable_member vtable_key, *v;
1254 assert(found_object);
1256 n = hashmap_get(bus->nodes, p);
1260 /* First, try object callbacks */
1261 r = node_callbacks_run(bus, m, n->callbacks, require_fallback, found_object);
1264 if (bus->nodes_modified)
1267 if (!m->interface || !m->member)
1270 /* Then, look for a known method */
1271 vtable_key.path = (char*) p;
1272 vtable_key.interface = m->interface;
1273 vtable_key.member = m->member;
1275 v = hashmap_get(bus->vtable_methods, &vtable_key);
1277 r = method_callbacks_run(bus, m, v, require_fallback, found_object);
1280 if (bus->nodes_modified)
1284 /* Then, look for a known property */
1285 if (streq(m->interface, "org.freedesktop.DBus.Properties")) {
1288 get = streq(m->member, "Get");
1290 if (get || streq(m->member, "Set")) {
1292 r = sd_bus_message_rewind(m, true);
1296 vtable_key.path = (char*) p;
1298 r = sd_bus_message_read(m, "ss", &vtable_key.interface, &vtable_key.member);
1300 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected interface and member parameters");
1302 v = hashmap_get(bus->vtable_properties, &vtable_key);
1304 r = property_get_set_callbacks_run(bus, m, v, require_fallback, get, found_object);
1309 } else if (streq(m->member, "GetAll")) {
1312 r = sd_bus_message_rewind(m, true);
1316 r = sd_bus_message_read(m, "s", &iface);
1318 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected interface parameter");
1323 r = property_get_all_callbacks_run(bus, m, n->vtables, require_fallback, iface, found_object);
1328 } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1330 if (!isempty(sd_bus_message_get_signature(m, true)))
1331 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected no parameters");
1333 r = process_introspect(bus, m, n, require_fallback, found_object);
1337 } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.ObjectManager", "GetManagedObjects")) {
1339 if (!isempty(sd_bus_message_get_signature(m, true)))
1340 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected no parameters");
1342 r = process_get_managed_objects(bus, m, n, require_fallback, found_object);
1347 if (bus->nodes_modified)
1350 if (!*found_object) {
1351 r = bus_node_exists(bus, n, m->path, require_fallback);
1353 return bus_maybe_reply_error(m, r, NULL);
1354 if (bus->nodes_modified)
1357 *found_object = true;
1363 int bus_process_object(sd_bus *bus, sd_bus_message *m) {
1366 bool found_object = false;
1371 if (bus->hello_flags & KDBUS_HELLO_MONITOR)
1374 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
1377 if (hashmap_isempty(bus->nodes))
1380 /* Never respond to broadcast messages */
1381 if (bus->bus_client && !m->destination)
1387 pl = strlen(m->path);
1391 bus->nodes_modified = false;
1393 r = object_find_and_run(bus, m, m->path, false, &found_object);
1397 /* Look for fallback prefixes */
1398 OBJECT_PATH_FOREACH_PREFIX(prefix, m->path) {
1400 if (bus->nodes_modified)
1403 r = object_find_and_run(bus, m, prefix, true, &found_object);
1408 } while (bus->nodes_modified);
1413 if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Get") ||
1414 sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Set"))
1415 r = sd_bus_reply_method_errorf(
1417 SD_BUS_ERROR_UNKNOWN_PROPERTY,
1418 "Unknown property or interface.");
1420 r = sd_bus_reply_method_errorf(
1422 SD_BUS_ERROR_UNKNOWN_METHOD,
1423 "Unknown method '%s' or interface '%s'.", m->member, m->interface);
1431 static struct node *bus_node_allocate(sd_bus *bus, const char *path) {
1432 struct node *n, *parent;
1434 _cleanup_free_ char *s = NULL;
1440 assert(path[0] == '/');
1442 n = hashmap_get(bus->nodes, path);
1446 r = hashmap_ensure_allocated(&bus->nodes, &string_hash_ops);
1454 if (streq(path, "/"))
1457 e = strrchr(path, '/');
1460 p = strndupa(path, MAX(1, e - path));
1462 parent = bus_node_allocate(bus, p);
1467 n = new0(struct node, 1);
1473 s = NULL; /* do not free */
1475 r = hashmap_put(bus->nodes, n->path, n);
1482 LIST_PREPEND(siblings, parent->child, n);
1487 void bus_node_gc(sd_bus *b, struct node *n) {
1500 assert_se(hashmap_remove(b->nodes, n->path) == n);
1503 LIST_REMOVE(siblings, n->parent->child, n);
1506 bus_node_gc(b, n->parent);
1510 static int bus_find_parent_object_manager(sd_bus *bus, struct node **out, const char *path) {
1516 n = hashmap_get(bus->nodes, path);
1520 prefix = alloca(strlen(path) + 1);
1521 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
1522 n = hashmap_get(bus->nodes, prefix);
1528 while (n && !n->object_managers)
1536 static int bus_add_object(
1541 sd_bus_message_handler_t callback,
1548 assert_return(bus, -EINVAL);
1549 assert_return(object_path_is_valid(path), -EINVAL);
1550 assert_return(callback, -EINVAL);
1551 assert_return(!bus_pid_changed(bus), -ECHILD);
1553 n = bus_node_allocate(bus, path);
1557 s = bus_slot_allocate(bus, !slot, BUS_NODE_CALLBACK, sizeof(struct node_callback), userdata);
1563 s->node_callback.callback = callback;
1564 s->node_callback.is_fallback = fallback;
1566 s->node_callback.node = n;
1567 LIST_PREPEND(callbacks, n->callbacks, &s->node_callback);
1568 bus->nodes_modified = true;
1576 sd_bus_slot_unref(s);
1577 bus_node_gc(bus, n);
1582 _public_ int sd_bus_add_object(
1586 sd_bus_message_handler_t callback,
1589 return bus_add_object(bus, slot, false, path, callback, userdata);
1592 _public_ int sd_bus_add_fallback(
1596 sd_bus_message_handler_t callback,
1599 return bus_add_object(bus, slot, true, prefix, callback, userdata);
1602 static void vtable_member_hash_func(const void *a, struct siphash *state) {
1603 const struct vtable_member *m = a;
1607 string_hash_func(m->path, state);
1608 string_hash_func(m->interface, state);
1609 string_hash_func(m->member, state);
1612 static int vtable_member_compare_func(const void *a, const void *b) {
1613 const struct vtable_member *x = a, *y = b;
1619 r = strcmp(x->path, y->path);
1623 r = strcmp(x->interface, y->interface);
1627 return strcmp(x->member, y->member);
1630 static const struct hash_ops vtable_member_hash_ops = {
1631 .hash = vtable_member_hash_func,
1632 .compare = vtable_member_compare_func
1635 static int add_object_vtable_internal(
1639 const char *interface,
1640 const sd_bus_vtable *vtable,
1642 sd_bus_object_find_t find,
1645 sd_bus_slot *s = NULL;
1646 struct node_vtable *i, *existing = NULL;
1647 const sd_bus_vtable *v;
1651 assert_return(bus, -EINVAL);
1652 assert_return(object_path_is_valid(path), -EINVAL);
1653 assert_return(interface_name_is_valid(interface), -EINVAL);
1654 assert_return(vtable, -EINVAL);
1655 assert_return(vtable[0].type == _SD_BUS_VTABLE_START, -EINVAL);
1656 assert_return(vtable[0].x.start.element_size == sizeof(struct sd_bus_vtable), -EINVAL);
1657 assert_return(!bus_pid_changed(bus), -ECHILD);
1658 assert_return(!streq(interface, "org.freedesktop.DBus.Properties") &&
1659 !streq(interface, "org.freedesktop.DBus.Introspectable") &&
1660 !streq(interface, "org.freedesktop.DBus.Peer") &&
1661 !streq(interface, "org.freedesktop.DBus.ObjectManager"), -EINVAL);
1663 r = hashmap_ensure_allocated(&bus->vtable_methods, &vtable_member_hash_ops);
1667 r = hashmap_ensure_allocated(&bus->vtable_properties, &vtable_member_hash_ops);
1671 n = bus_node_allocate(bus, path);
1675 LIST_FOREACH(vtables, i, n->vtables) {
1676 if (i->is_fallback != fallback) {
1681 if (streq(i->interface, interface)) {
1683 if (i->vtable == vtable) {
1692 s = bus_slot_allocate(bus, !slot, BUS_NODE_VTABLE, sizeof(struct node_vtable), userdata);
1698 s->node_vtable.is_fallback = fallback;
1699 s->node_vtable.vtable = vtable;
1700 s->node_vtable.find = find;
1702 s->node_vtable.interface = strdup(interface);
1703 if (!s->node_vtable.interface) {
1708 for (v = s->node_vtable.vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
1712 case _SD_BUS_VTABLE_METHOD: {
1713 struct vtable_member *m;
1715 if (!member_name_is_valid(v->x.method.member) ||
1716 !signature_is_valid(strempty(v->x.method.signature), false) ||
1717 !signature_is_valid(strempty(v->x.method.result), false) ||
1718 !(v->x.method.handler || (isempty(v->x.method.signature) && isempty(v->x.method.result))) ||
1719 v->flags & (SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) {
1724 m = new0(struct vtable_member, 1);
1730 m->parent = &s->node_vtable;
1732 m->interface = s->node_vtable.interface;
1733 m->member = v->x.method.member;
1736 r = hashmap_put(bus->vtable_methods, m, m);
1745 case _SD_BUS_VTABLE_WRITABLE_PROPERTY:
1747 if (!(v->x.property.set || bus_type_is_basic(v->x.property.signature[0]))) {
1752 if (v->flags & SD_BUS_VTABLE_PROPERTY_CONST) {
1758 case _SD_BUS_VTABLE_PROPERTY: {
1759 struct vtable_member *m;
1761 if (!member_name_is_valid(v->x.property.member) ||
1762 !signature_is_single(v->x.property.signature, false) ||
1763 !(v->x.property.get || bus_type_is_basic(v->x.property.signature[0]) || streq(v->x.property.signature, "as")) ||
1764 (v->flags & SD_BUS_VTABLE_METHOD_NO_REPLY) ||
1765 (!!(v->flags & SD_BUS_VTABLE_PROPERTY_CONST) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) > 1 ||
1766 ((v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) && (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)) ||
1767 (v->flags & SD_BUS_VTABLE_UNPRIVILEGED && v->type == _SD_BUS_VTABLE_PROPERTY)) {
1772 m = new0(struct vtable_member, 1);
1778 m->parent = &s->node_vtable;
1780 m->interface = s->node_vtable.interface;
1781 m->member = v->x.property.member;
1784 r = hashmap_put(bus->vtable_properties, m, m);
1793 case _SD_BUS_VTABLE_SIGNAL:
1795 if (!member_name_is_valid(v->x.signal.member) ||
1796 !signature_is_valid(strempty(v->x.signal.signature), false) ||
1797 v->flags & SD_BUS_VTABLE_UNPRIVILEGED) {
1810 s->node_vtable.node = n;
1811 LIST_INSERT_AFTER(vtables, n->vtables, existing, &s->node_vtable);
1812 bus->nodes_modified = true;
1820 sd_bus_slot_unref(s);
1821 bus_node_gc(bus, n);
1826 _public_ int sd_bus_add_object_vtable(
1830 const char *interface,
1831 const sd_bus_vtable *vtable,
1834 return add_object_vtable_internal(bus, slot, path, interface, vtable, false, NULL, userdata);
1837 _public_ int sd_bus_add_fallback_vtable(
1841 const char *interface,
1842 const sd_bus_vtable *vtable,
1843 sd_bus_object_find_t find,
1846 return add_object_vtable_internal(bus, slot, prefix, interface, vtable, true, find, userdata);
1849 _public_ int sd_bus_add_node_enumerator(
1853 sd_bus_node_enumerator_t callback,
1860 assert_return(bus, -EINVAL);
1861 assert_return(object_path_is_valid(path), -EINVAL);
1862 assert_return(callback, -EINVAL);
1863 assert_return(!bus_pid_changed(bus), -ECHILD);
1865 n = bus_node_allocate(bus, path);
1869 s = bus_slot_allocate(bus, !slot, BUS_NODE_ENUMERATOR, sizeof(struct node_enumerator), userdata);
1875 s->node_enumerator.callback = callback;
1877 s->node_enumerator.node = n;
1878 LIST_PREPEND(enumerators, n->enumerators, &s->node_enumerator);
1879 bus->nodes_modified = true;
1887 sd_bus_slot_unref(s);
1888 bus_node_gc(bus, n);
1893 static int emit_properties_changed_on_interface(
1897 const char *interface,
1898 bool require_fallback,
1899 bool *found_interface,
1902 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1903 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1904 bool has_invalidating = false, has_changing = false;
1905 struct vtable_member key = {};
1906 struct node_vtable *c;
1916 assert(found_interface);
1918 n = hashmap_get(bus->nodes, prefix);
1922 r = sd_bus_message_new_signal(bus, &m, path, "org.freedesktop.DBus.Properties", "PropertiesChanged");
1926 r = sd_bus_message_append(m, "s", interface);
1930 r = sd_bus_message_open_container(m, 'a', "{sv}");
1935 key.interface = interface;
1937 LIST_FOREACH(vtables, c, n->vtables) {
1938 if (require_fallback && !c->is_fallback)
1941 if (!streq(c->interface, interface))
1944 r = node_vtable_get_userdata(bus, path, c, &u, &error);
1947 if (bus->nodes_modified)
1952 *found_interface = true;
1955 /* If the caller specified a list of
1956 * properties we include exactly those in the
1957 * PropertiesChanged message */
1959 STRV_FOREACH(property, names) {
1960 struct vtable_member *v;
1962 assert_return(member_name_is_valid(*property), -EINVAL);
1964 key.member = *property;
1965 v = hashmap_get(bus->vtable_properties, &key);
1969 /* If there are two vtables for the same
1970 * interface, let's handle this property when
1971 * we come to that vtable. */
1975 assert_return(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE ||
1976 v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION, -EDOM);
1978 assert_return(!(v->vtable->flags & SD_BUS_VTABLE_HIDDEN), -EDOM);
1980 if (v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) {
1981 has_invalidating = true;
1985 has_changing = true;
1987 r = vtable_append_one_property(bus, m, m->path, c, v->vtable, u, &error);
1990 if (bus->nodes_modified)
1994 const sd_bus_vtable *v;
1996 /* If the caller specified no properties list
1997 * we include all properties that are marked
1998 * as changing in the message. */
2000 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
2001 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
2004 if (v->flags & SD_BUS_VTABLE_HIDDEN)
2007 if (v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) {
2008 has_invalidating = true;
2012 if (!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))
2015 has_changing = true;
2017 r = vtable_append_one_property(bus, m, m->path, c, v, u, &error);
2020 if (bus->nodes_modified)
2026 if (!has_invalidating && !has_changing)
2029 r = sd_bus_message_close_container(m);
2033 r = sd_bus_message_open_container(m, 'a', "s");
2037 if (has_invalidating) {
2038 LIST_FOREACH(vtables, c, n->vtables) {
2039 if (require_fallback && !c->is_fallback)
2042 if (!streq(c->interface, interface))
2045 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2048 if (bus->nodes_modified)
2054 STRV_FOREACH(property, names) {
2055 struct vtable_member *v;
2057 key.member = *property;
2058 assert_se(v = hashmap_get(bus->vtable_properties, &key));
2059 assert(c == v->parent);
2061 if (!(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION))
2064 r = sd_bus_message_append(m, "s", *property);
2069 const sd_bus_vtable *v;
2071 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
2072 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
2075 if (v->flags & SD_BUS_VTABLE_HIDDEN)
2078 if (!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION))
2081 r = sd_bus_message_append(m, "s", v->x.property.member);
2089 r = sd_bus_message_close_container(m);
2093 r = sd_bus_send(bus, m, NULL);
2100 _public_ int sd_bus_emit_properties_changed_strv(
2103 const char *interface,
2106 BUS_DONT_DESTROY(bus);
2107 bool found_interface = false;
2111 assert_return(bus, -EINVAL);
2112 assert_return(object_path_is_valid(path), -EINVAL);
2113 assert_return(interface_name_is_valid(interface), -EINVAL);
2114 assert_return(!bus_pid_changed(bus), -ECHILD);
2116 if (!BUS_IS_OPEN(bus->state))
2119 /* A non-NULL but empty names list means nothing needs to be
2120 generated. A NULL list OTOH indicates that all properties
2121 that are set to EMITS_CHANGE or EMITS_INVALIDATION shall be
2122 included in the PropertiesChanged message. */
2123 if (names && names[0] == NULL)
2127 bus->nodes_modified = false;
2129 r = emit_properties_changed_on_interface(bus, path, path, interface, false, &found_interface, names);
2132 if (bus->nodes_modified)
2135 prefix = alloca(strlen(path) + 1);
2136 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2137 r = emit_properties_changed_on_interface(bus, prefix, path, interface, true, &found_interface, names);
2140 if (bus->nodes_modified)
2144 } while (bus->nodes_modified);
2146 return found_interface ? 0 : -ENOENT;
2149 _public_ int sd_bus_emit_properties_changed(
2152 const char *interface,
2153 const char *name, ...) {
2157 assert_return(bus, -EINVAL);
2158 assert_return(object_path_is_valid(path), -EINVAL);
2159 assert_return(interface_name_is_valid(interface), -EINVAL);
2160 assert_return(!bus_pid_changed(bus), -ECHILD);
2162 if (!BUS_IS_OPEN(bus->state))
2168 names = strv_from_stdarg_alloca(name);
2170 return sd_bus_emit_properties_changed_strv(bus, path, interface, names);
2173 static int object_added_append_all_prefix(
2179 bool require_fallback) {
2181 const char *previous_interface = NULL;
2182 struct node_vtable *c;
2192 n = hashmap_get(bus->nodes, prefix);
2196 LIST_FOREACH(vtables, c, n->vtables) {
2197 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2200 if (require_fallback && !c->is_fallback)
2203 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2206 if (bus->nodes_modified)
2211 if (!streq_ptr(c->interface, previous_interface)) {
2212 /* If a child-node already handled this interface, we
2213 * skip it on any of its parents. The child vtables
2214 * always fully override any conflicting vtables of
2215 * any parent node. */
2216 if (set_get(s, c->interface))
2219 r = set_put(s, c->interface);
2223 if (previous_interface) {
2224 r = sd_bus_message_close_container(m);
2227 r = sd_bus_message_close_container(m);
2232 r = sd_bus_message_open_container(m, 'e', "sa{sv}");
2235 r = sd_bus_message_append(m, "s", c->interface);
2238 r = sd_bus_message_open_container(m, 'a', "{sv}");
2242 previous_interface = c->interface;
2245 r = vtable_append_all_properties(bus, m, path, c, u, &error);
2248 if (bus->nodes_modified)
2252 if (previous_interface) {
2253 r = sd_bus_message_close_container(m);
2256 r = sd_bus_message_close_container(m);
2264 static int object_added_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
2265 _cleanup_set_free_ Set *s = NULL;
2274 * This appends all interfaces registered on path @path. We first add
2275 * the builtin interfaces, which are always available and handled by
2276 * sd-bus. Then, we add all interfaces registered on the exact node,
2277 * followed by all fallback interfaces registered on any parent prefix.
2279 * If an interface is registered multiple times on the same node with
2280 * different vtables, we merge all the properties across all vtables.
2281 * However, if a child node has the same interface registered as one of
2282 * its parent nodes has as fallback, we make the child overwrite the
2283 * parent instead of extending it. Therefore, we keep a "Set" of all
2284 * handled interfaces during parent traversal, so we skip interfaces on
2285 * a parent that were overwritten by a child.
2288 s = set_new(&string_hash_ops);
2292 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
2295 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
2298 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
2301 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
2305 r = object_added_append_all_prefix(bus, m, s, path, path, false);
2308 if (bus->nodes_modified)
2311 prefix = alloca(strlen(path) + 1);
2312 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2313 r = object_added_append_all_prefix(bus, m, s, prefix, path, true);
2316 if (bus->nodes_modified)
2323 _public_ int sd_bus_emit_object_added(sd_bus *bus, const char *path) {
2324 BUS_DONT_DESTROY(bus);
2326 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2327 struct node *object_manager;
2331 * This emits an InterfacesAdded signal on the given path, by iterating
2332 * all registered vtables and fallback vtables on the path. All
2333 * properties are queried and included in the signal.
2334 * This call is equivalent to sd_bus_emit_interfaces_added() with an
2335 * explicit list of registered interfaces. However, unlike
2336 * interfaces_added(), this call can figure out the list of supported
2337 * interfaces itself. Furthermore, it properly adds the builtin
2338 * org.freedesktop.DBus.* interfaces.
2341 assert_return(bus, -EINVAL);
2342 assert_return(object_path_is_valid(path), -EINVAL);
2343 assert_return(!bus_pid_changed(bus), -ECHILD);
2345 if (!BUS_IS_OPEN(bus->state))
2348 r = bus_find_parent_object_manager(bus, &object_manager, path);
2355 bus->nodes_modified = false;
2356 m = sd_bus_message_unref(m);
2358 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
2362 r = sd_bus_message_append_basic(m, 'o', path);
2366 r = sd_bus_message_open_container(m, 'a', "{sa{sv}}");
2370 r = object_added_append_all(bus, m, path);
2374 if (bus->nodes_modified)
2377 r = sd_bus_message_close_container(m);
2381 } while (bus->nodes_modified);
2383 return sd_bus_send(bus, m, NULL);
2386 static int object_removed_append_all_prefix(
2392 bool require_fallback) {
2394 const char *previous_interface = NULL;
2395 struct node_vtable *c;
2405 n = hashmap_get(bus->nodes, prefix);
2409 LIST_FOREACH(vtables, c, n->vtables) {
2410 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2413 if (require_fallback && !c->is_fallback)
2415 if (streq_ptr(c->interface, previous_interface))
2418 /* If a child-node already handled this interface, we
2419 * skip it on any of its parents. The child vtables
2420 * always fully override any conflicting vtables of
2421 * any parent node. */
2422 if (set_get(s, c->interface))
2425 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2428 if (bus->nodes_modified)
2433 r = set_put(s, c->interface);
2437 r = sd_bus_message_append(m, "s", c->interface);
2441 previous_interface = c->interface;
2447 static int object_removed_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
2448 _cleanup_set_free_ Set *s = NULL;
2456 /* see sd_bus_emit_object_added() for details */
2458 s = set_new(&string_hash_ops);
2462 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Peer");
2465 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Introspectable");
2468 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Properties");
2471 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.ObjectManager");
2475 r = object_removed_append_all_prefix(bus, m, s, path, path, false);
2478 if (bus->nodes_modified)
2481 prefix = alloca(strlen(path) + 1);
2482 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2483 r = object_removed_append_all_prefix(bus, m, s, prefix, path, true);
2486 if (bus->nodes_modified)
2493 _public_ int sd_bus_emit_object_removed(sd_bus *bus, const char *path) {
2494 BUS_DONT_DESTROY(bus);
2496 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2497 struct node *object_manager;
2501 * This is like sd_bus_emit_object_added(), but emits an
2502 * InterfacesRemoved signal on the given path. This only includes any
2503 * registered interfaces but skips the properties. Note that this will
2504 * call into the find() callbacks of any registered vtable. Therefore,
2505 * you must call this function before destroying/unlinking your object.
2506 * Otherwise, the list of interfaces will be incomplete. However, note
2507 * that this will *NOT* call into any property callback. Therefore, the
2508 * object might be in an "destructed" state, as long as we can find it.
2511 assert_return(bus, -EINVAL);
2512 assert_return(object_path_is_valid(path), -EINVAL);
2513 assert_return(!bus_pid_changed(bus), -ECHILD);
2515 if (!BUS_IS_OPEN(bus->state))
2518 r = bus_find_parent_object_manager(bus, &object_manager, path);
2525 bus->nodes_modified = false;
2526 m = sd_bus_message_unref(m);
2528 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
2532 r = sd_bus_message_append_basic(m, 'o', path);
2536 r = sd_bus_message_open_container(m, 'a', "s");
2540 r = object_removed_append_all(bus, m, path);
2544 if (bus->nodes_modified)
2547 r = sd_bus_message_close_container(m);
2551 } while (bus->nodes_modified);
2553 return sd_bus_send(bus, m, NULL);
2556 static int interfaces_added_append_one_prefix(
2561 const char *interface,
2562 bool require_fallback) {
2564 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2565 bool found_interface = false;
2566 struct node_vtable *c;
2577 n = hashmap_get(bus->nodes, prefix);
2581 LIST_FOREACH(vtables, c, n->vtables) {
2582 if (require_fallback && !c->is_fallback)
2585 if (!streq(c->interface, interface))
2588 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2591 if (bus->nodes_modified)
2596 if (!found_interface) {
2597 r = sd_bus_message_append_basic(m, 's', interface);
2601 r = sd_bus_message_open_container(m, 'a', "{sv}");
2605 found_interface = true;
2608 r = vtable_append_all_properties(bus, m, path, c, u, &error);
2611 if (bus->nodes_modified)
2615 if (found_interface) {
2616 r = sd_bus_message_close_container(m);
2621 return found_interface;
2624 static int interfaces_added_append_one(
2628 const char *interface) {
2638 r = interfaces_added_append_one_prefix(bus, m, path, path, interface, false);
2641 if (bus->nodes_modified)
2644 prefix = alloca(strlen(path) + 1);
2645 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2646 r = interfaces_added_append_one_prefix(bus, m, prefix, path, interface, true);
2649 if (bus->nodes_modified)
2656 _public_ int sd_bus_emit_interfaces_added_strv(sd_bus *bus, const char *path, char **interfaces) {
2657 BUS_DONT_DESTROY(bus);
2659 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2660 struct node *object_manager;
2664 assert_return(bus, -EINVAL);
2665 assert_return(object_path_is_valid(path), -EINVAL);
2666 assert_return(!bus_pid_changed(bus), -ECHILD);
2668 if (!BUS_IS_OPEN(bus->state))
2671 if (strv_isempty(interfaces))
2674 r = bus_find_parent_object_manager(bus, &object_manager, path);
2681 bus->nodes_modified = false;
2682 m = sd_bus_message_unref(m);
2684 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
2688 r = sd_bus_message_append_basic(m, 'o', path);
2692 r = sd_bus_message_open_container(m, 'a', "{sa{sv}}");
2696 STRV_FOREACH(i, interfaces) {
2697 assert_return(interface_name_is_valid(*i), -EINVAL);
2699 r = sd_bus_message_open_container(m, 'e', "sa{sv}");
2703 r = interfaces_added_append_one(bus, m, path, *i);
2707 if (bus->nodes_modified)
2710 r = sd_bus_message_close_container(m);
2715 if (bus->nodes_modified)
2718 r = sd_bus_message_close_container(m);
2722 } while (bus->nodes_modified);
2724 return sd_bus_send(bus, m, NULL);
2727 _public_ int sd_bus_emit_interfaces_added(sd_bus *bus, const char *path, const char *interface, ...) {
2730 assert_return(bus, -EINVAL);
2731 assert_return(object_path_is_valid(path), -EINVAL);
2732 assert_return(!bus_pid_changed(bus), -ECHILD);
2734 if (!BUS_IS_OPEN(bus->state))
2737 interfaces = strv_from_stdarg_alloca(interface);
2739 return sd_bus_emit_interfaces_added_strv(bus, path, interfaces);
2742 _public_ int sd_bus_emit_interfaces_removed_strv(sd_bus *bus, const char *path, char **interfaces) {
2743 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2744 struct node *object_manager;
2747 assert_return(bus, -EINVAL);
2748 assert_return(object_path_is_valid(path), -EINVAL);
2749 assert_return(!bus_pid_changed(bus), -ECHILD);
2751 if (!BUS_IS_OPEN(bus->state))
2754 if (strv_isempty(interfaces))
2757 r = bus_find_parent_object_manager(bus, &object_manager, path);
2763 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
2767 r = sd_bus_message_append_basic(m, 'o', path);
2771 r = sd_bus_message_append_strv(m, interfaces);
2775 return sd_bus_send(bus, m, NULL);
2778 _public_ int sd_bus_emit_interfaces_removed(sd_bus *bus, const char *path, const char *interface, ...) {
2781 assert_return(bus, -EINVAL);
2782 assert_return(object_path_is_valid(path), -EINVAL);
2783 assert_return(!bus_pid_changed(bus), -ECHILD);
2785 if (!BUS_IS_OPEN(bus->state))
2788 interfaces = strv_from_stdarg_alloca(interface);
2790 return sd_bus_emit_interfaces_removed_strv(bus, path, interfaces);
2793 _public_ int sd_bus_add_object_manager(sd_bus *bus, sd_bus_slot **slot, const char *path) {
2798 assert_return(bus, -EINVAL);
2799 assert_return(object_path_is_valid(path), -EINVAL);
2800 assert_return(!bus_pid_changed(bus), -ECHILD);
2802 n = bus_node_allocate(bus, path);
2806 s = bus_slot_allocate(bus, !slot, BUS_NODE_OBJECT_MANAGER, sizeof(struct node_object_manager), NULL);
2812 s->node_object_manager.node = n;
2813 LIST_PREPEND(object_managers, n->object_managers, &s->node_object_manager);
2814 bus->nodes_modified = true;
2822 sd_bus_slot_unref(s);
2823 bus_node_gc(bus, n);