1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 Lennart Poettering
6 #include "alloc-util.h"
7 #include "bus-internal.h"
8 #include "bus-introspect.h"
9 #include "bus-message.h"
10 #include "bus-objects.h"
11 #include "bus-signature.h"
16 #include "string-util.h"
19 static int node_vtable_get_userdata(
22 struct node_vtable *c,
24 sd_bus_error *error) {
34 s = container_of(c, sd_bus_slot, node_vtable);
37 bus->current_slot = sd_bus_slot_ref(s);
38 bus->current_userdata = u;
39 r = c->find(bus, path, c->interface, u, &found_u, error);
40 bus->current_userdata = NULL;
41 bus->current_slot = sd_bus_slot_unref(s);
45 if (sd_bus_error_is_set(error))
46 return -sd_bus_error_get_errno(error);
58 static void *vtable_method_convert_userdata(const sd_bus_vtable *p, void *u) {
61 return (uint8_t*) u + p->x.method.offset;
64 static void *vtable_property_convert_userdata(const sd_bus_vtable *p, void *u) {
67 return (uint8_t*) u + p->x.property.offset;
70 static int vtable_property_get_userdata(
73 struct vtable_member *p,
75 sd_bus_error *error) {
85 r = node_vtable_get_userdata(bus, path, p->parent, &u, error);
88 if (bus->nodes_modified)
91 *userdata = vtable_property_convert_userdata(p->vtable, u);
95 static int add_enumerated_to_set(
98 struct node_enumerator *first,
100 sd_bus_error *error) {
102 struct node_enumerator *c;
109 LIST_FOREACH(enumerators, c, first) {
110 char **children = NULL, **k;
113 if (bus->nodes_modified)
116 slot = container_of(c, sd_bus_slot, node_enumerator);
118 bus->current_slot = sd_bus_slot_ref(slot);
119 bus->current_userdata = slot->userdata;
120 r = c->callback(bus, prefix, slot->userdata, &children, error);
121 bus->current_userdata = NULL;
122 bus->current_slot = sd_bus_slot_unref(slot);
126 if (sd_bus_error_is_set(error))
127 return -sd_bus_error_get_errno(error);
129 STRV_FOREACH(k, children) {
135 if (!object_path_is_valid(*k)) {
141 if (!object_path_startswith(*k, prefix)) {
146 r = set_consume(s, *k);
160 /* if set, add_subtree() works recursively */
161 CHILDREN_RECURSIVE = 1 << 0,
162 /* if set, add_subtree() scans object-manager hierarchies recursively */
163 CHILDREN_SUBHIERARCHIES = 1 << 1,
166 static int add_subtree_to_set(
172 sd_bus_error *error) {
182 r = add_enumerated_to_set(bus, prefix, n->enumerators, s, error);
185 if (bus->nodes_modified)
188 LIST_FOREACH(siblings, i, n->child) {
191 if (!object_path_startswith(i->path, prefix))
198 r = set_consume(s, t);
199 if (r < 0 && r != -EEXIST)
202 if ((flags & CHILDREN_RECURSIVE) &&
203 ((flags & CHILDREN_SUBHIERARCHIES) || !i->object_managers)) {
204 r = add_subtree_to_set(bus, prefix, i, flags, s, error);
207 if (bus->nodes_modified)
215 static int get_child_nodes(
221 sd_bus_error *error) {
231 s = set_new(&string_hash_ops);
235 r = add_subtree_to_set(bus, prefix, n, flags, s, error);
245 static int node_callbacks_run(
248 struct node_callback *first,
249 bool require_fallback,
250 bool *found_object) {
252 struct node_callback *c;
257 assert(found_object);
259 LIST_FOREACH(callbacks, c, first) {
260 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
263 if (bus->nodes_modified)
266 if (require_fallback && !c->is_fallback)
269 *found_object = true;
271 if (c->last_iteration == bus->iteration_counter)
274 c->last_iteration = bus->iteration_counter;
276 r = sd_bus_message_rewind(m, true);
280 slot = container_of(c, sd_bus_slot, node_callback);
282 bus->current_slot = sd_bus_slot_ref(slot);
283 bus->current_handler = c->callback;
284 bus->current_userdata = slot->userdata;
285 r = c->callback(m, slot->userdata, &error_buffer);
286 bus->current_userdata = NULL;
287 bus->current_handler = NULL;
288 bus->current_slot = sd_bus_slot_unref(slot);
290 r = bus_maybe_reply_error(m, r, &error_buffer);
298 #define CAPABILITY_SHIFT(x) (((x) >> __builtin_ctzll(_SD_BUS_VTABLE_CAPABILITY_MASK)) & 0xFFFF)
300 static int check_access(sd_bus *bus, sd_bus_message *m, struct vtable_member *c, sd_bus_error *error) {
308 /* If the entire bus is trusted let's grant access */
312 /* If the member is marked UNPRIVILEGED let's grant access */
313 if (c->vtable->flags & SD_BUS_VTABLE_UNPRIVILEGED)
316 /* Check have the caller has the requested capability
317 * set. Note that the flags value contains the capability
318 * number plus one, which we need to subtract here. We do this
319 * so that we have 0 as special value for "default
321 cap = CAPABILITY_SHIFT(c->vtable->flags);
323 cap = CAPABILITY_SHIFT(c->parent->vtable[0].flags);
329 r = sd_bus_query_sender_privilege(m, cap);
335 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Access to %s.%s() not permitted.", c->interface, c->member);
338 static int method_callbacks_run(
341 struct vtable_member *c,
342 bool require_fallback,
343 bool *found_object) {
345 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
346 const char *signature;
353 assert(found_object);
355 if (require_fallback && !c->parent->is_fallback)
358 r = check_access(bus, m, c, &error);
360 return bus_maybe_reply_error(m, r, &error);
362 r = node_vtable_get_userdata(bus, m->path, c->parent, &u, &error);
364 return bus_maybe_reply_error(m, r, &error);
365 if (bus->nodes_modified)
368 u = vtable_method_convert_userdata(c->vtable, u);
370 *found_object = true;
372 if (c->last_iteration == bus->iteration_counter)
375 c->last_iteration = bus->iteration_counter;
377 r = sd_bus_message_rewind(m, true);
381 signature = sd_bus_message_get_signature(m, true);
385 if (!streq(strempty(c->vtable->x.method.signature), signature))
386 return sd_bus_reply_method_errorf(
388 SD_BUS_ERROR_INVALID_ARGS,
389 "Invalid arguments '%s' to call %s.%s(), expecting '%s'.",
390 signature, c->interface, c->member, strempty(c->vtable->x.method.signature));
392 /* Keep track what the signature of the reply to this message
393 * should be, so that this can be enforced when sealing the
395 m->enforced_reply_signature = strempty(c->vtable->x.method.result);
397 if (c->vtable->x.method.handler) {
400 slot = container_of(c->parent, sd_bus_slot, node_vtable);
402 bus->current_slot = sd_bus_slot_ref(slot);
403 bus->current_handler = c->vtable->x.method.handler;
404 bus->current_userdata = u;
405 r = c->vtable->x.method.handler(m, u, &error);
406 bus->current_userdata = NULL;
407 bus->current_handler = NULL;
408 bus->current_slot = sd_bus_slot_unref(slot);
410 return bus_maybe_reply_error(m, r, &error);
413 /* If the method callback is NULL, make this a successful NOP */
414 r = sd_bus_reply_method_return(m, NULL);
421 static int invoke_property_get(
424 const sd_bus_vtable *v,
426 const char *interface,
427 const char *property,
428 sd_bus_message *reply,
430 sd_bus_error *error) {
443 if (v->x.property.get) {
445 bus->current_slot = sd_bus_slot_ref(slot);
446 bus->current_userdata = userdata;
447 r = v->x.property.get(bus, path, interface, property, reply, userdata, error);
448 bus->current_userdata = NULL;
449 bus->current_slot = sd_bus_slot_unref(slot);
453 if (sd_bus_error_is_set(error))
454 return -sd_bus_error_get_errno(error);
458 /* Automatic handling if no callback is defined. */
460 if (streq(v->x.property.signature, "as"))
461 return sd_bus_message_append_strv(reply, *(char***) userdata);
463 assert(signature_is_single(v->x.property.signature, false));
464 assert(bus_type_is_basic(v->x.property.signature[0]));
466 switch (v->x.property.signature[0]) {
468 case SD_BUS_TYPE_STRING:
469 case SD_BUS_TYPE_SIGNATURE:
470 p = strempty(*(char**) userdata);
473 case SD_BUS_TYPE_OBJECT_PATH:
474 p = *(char**) userdata;
483 return sd_bus_message_append_basic(reply, v->x.property.signature[0], p);
486 static int invoke_property_set(
489 const sd_bus_vtable *v,
491 const char *interface,
492 const char *property,
493 sd_bus_message *value,
495 sd_bus_error *error) {
507 if (v->x.property.set) {
509 bus->current_slot = sd_bus_slot_ref(slot);
510 bus->current_userdata = userdata;
511 r = v->x.property.set(bus, path, interface, property, value, userdata, error);
512 bus->current_userdata = NULL;
513 bus->current_slot = sd_bus_slot_unref(slot);
517 if (sd_bus_error_is_set(error))
518 return -sd_bus_error_get_errno(error);
522 /* Automatic handling if no callback is defined. */
524 assert(signature_is_single(v->x.property.signature, false));
525 assert(bus_type_is_basic(v->x.property.signature[0]));
527 switch (v->x.property.signature[0]) {
529 case SD_BUS_TYPE_STRING:
530 case SD_BUS_TYPE_OBJECT_PATH:
531 case SD_BUS_TYPE_SIGNATURE: {
535 r = sd_bus_message_read_basic(value, v->x.property.signature[0], &p);
543 free(*(char**) userdata);
544 *(char**) userdata = n;
550 r = sd_bus_message_read_basic(value, v->x.property.signature[0], userdata);
560 static int property_get_set_callbacks_run(
563 struct vtable_member *c,
564 bool require_fallback,
566 bool *found_object) {
568 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
569 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
577 assert(found_object);
579 if (require_fallback && !c->parent->is_fallback)
582 r = vtable_property_get_userdata(bus, m->path, c, &u, &error);
584 return bus_maybe_reply_error(m, r, &error);
585 if (bus->nodes_modified)
588 slot = container_of(c->parent, sd_bus_slot, node_vtable);
590 *found_object = true;
592 r = sd_bus_message_new_method_return(m, &reply);
597 /* Note that we do not protect against reexecution
598 * here (using the last_iteration check, see below),
599 * should the node tree have changed and we got called
600 * again. We assume that property Get() calls are
601 * ultimately without side-effects or if they aren't
602 * then at least idempotent. */
604 r = sd_bus_message_open_container(reply, 'v', c->vtable->x.property.signature);
608 /* Note that we do not do an access check here. Read
609 * access to properties is always unrestricted, since
610 * PropertiesChanged signals broadcast contents
613 r = invoke_property_get(bus, slot, c->vtable, m->path, c->interface, c->member, reply, u, &error);
615 return bus_maybe_reply_error(m, r, &error);
617 if (bus->nodes_modified)
620 r = sd_bus_message_close_container(reply);
625 const char *signature = NULL;
628 if (c->vtable->type != _SD_BUS_VTABLE_WRITABLE_PROPERTY)
629 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Property '%s' is not writable.", c->member);
631 /* Avoid that we call the set routine more than once
632 * if the processing of this message got restarted
633 * because the node tree changed. */
634 if (c->last_iteration == bus->iteration_counter)
637 c->last_iteration = bus->iteration_counter;
639 r = sd_bus_message_peek_type(m, &type, &signature);
643 if (type != 'v' || !streq(strempty(signature), strempty(c->vtable->x.property.signature)))
644 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));
646 r = sd_bus_message_enter_container(m, 'v', c->vtable->x.property.signature);
650 r = check_access(bus, m, c, &error);
652 return bus_maybe_reply_error(m, r, &error);
654 r = invoke_property_set(bus, slot, c->vtable, m->path, c->interface, c->member, m, u, &error);
656 return bus_maybe_reply_error(m, r, &error);
658 if (bus->nodes_modified)
661 r = sd_bus_message_exit_container(m);
666 r = sd_bus_send(bus, reply, NULL);
673 static int vtable_append_one_property(
675 sd_bus_message *reply,
677 struct node_vtable *c,
678 const sd_bus_vtable *v,
680 sd_bus_error *error) {
691 r = sd_bus_message_open_container(reply, 'e', "sv");
695 r = sd_bus_message_append(reply, "s", v->x.property.member);
699 r = sd_bus_message_open_container(reply, 'v', v->x.property.signature);
703 slot = container_of(c, sd_bus_slot, node_vtable);
705 r = invoke_property_get(bus, slot, v, path, c->interface, v->x.property.member, reply, vtable_property_convert_userdata(v, userdata), error);
708 if (bus->nodes_modified)
711 r = sd_bus_message_close_container(reply);
715 r = sd_bus_message_close_container(reply);
722 static int vtable_append_all_properties(
724 sd_bus_message *reply,
726 struct node_vtable *c,
728 sd_bus_error *error) {
730 const sd_bus_vtable *v;
738 if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN)
741 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
742 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
745 if (v->flags & SD_BUS_VTABLE_HIDDEN)
748 if (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)
751 r = vtable_append_one_property(bus, reply, path, c, v, userdata, error);
754 if (bus->nodes_modified)
761 static int property_get_all_callbacks_run(
764 struct node_vtable *first,
765 bool require_fallback,
767 bool *found_object) {
769 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
770 struct node_vtable *c;
771 bool found_interface;
776 assert(found_object);
778 r = sd_bus_message_new_method_return(m, &reply);
782 r = sd_bus_message_open_container(reply, 'a', "{sv}");
786 found_interface = !iface ||
787 streq(iface, "org.freedesktop.DBus.Properties") ||
788 streq(iface, "org.freedesktop.DBus.Peer") ||
789 streq(iface, "org.freedesktop.DBus.Introspectable");
791 LIST_FOREACH(vtables, c, first) {
792 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
795 if (require_fallback && !c->is_fallback)
798 r = node_vtable_get_userdata(bus, m->path, c, &u, &error);
800 return bus_maybe_reply_error(m, r, &error);
801 if (bus->nodes_modified)
806 *found_object = true;
808 if (iface && !streq(c->interface, iface))
810 found_interface = true;
812 r = vtable_append_all_properties(bus, reply, m->path, c, u, &error);
814 return bus_maybe_reply_error(m, r, &error);
815 if (bus->nodes_modified)
822 if (!found_interface) {
823 r = sd_bus_reply_method_errorf(
825 SD_BUS_ERROR_UNKNOWN_INTERFACE,
826 "Unknown interface '%s'.", iface);
833 r = sd_bus_message_close_container(reply);
837 r = sd_bus_send(bus, reply, NULL);
844 static int bus_node_exists(
848 bool require_fallback) {
850 struct node_vtable *c;
851 struct node_callback *k;
858 /* Tests if there's anything attached directly to this node
859 * for the specified path */
861 if (!require_fallback && (n->enumerators || n->object_managers))
864 LIST_FOREACH(callbacks, k, n->callbacks) {
865 if (require_fallback && !k->is_fallback)
871 LIST_FOREACH(vtables, c, n->vtables) {
872 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
874 if (require_fallback && !c->is_fallback)
877 r = node_vtable_get_userdata(bus, path, c, NULL, &error);
880 if (bus->nodes_modified)
887 static int process_introspect(
891 bool require_fallback,
892 bool *found_object) {
894 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
895 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
896 _cleanup_set_free_free_ Set *s = NULL;
897 const char *previous_interface = NULL;
898 struct introspect intro;
899 struct node_vtable *c;
906 assert(found_object);
908 r = get_child_nodes(bus, m->path, n, 0, &s, &error);
910 return bus_maybe_reply_error(m, r, &error);
911 if (bus->nodes_modified)
914 r = introspect_begin(&intro, bus->trusted);
918 r = introspect_write_default_interfaces(&intro, !require_fallback && n->object_managers);
922 empty = set_isempty(s);
924 LIST_FOREACH(vtables, c, n->vtables) {
925 if (require_fallback && !c->is_fallback)
928 r = node_vtable_get_userdata(bus, m->path, c, NULL, &error);
930 r = bus_maybe_reply_error(m, r, &error);
933 if (bus->nodes_modified) {
942 if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN)
945 if (!streq_ptr(previous_interface, c->interface)) {
947 if (previous_interface)
948 fputs(" </interface>\n", intro.f);
950 fprintf(intro.f, " <interface name=\"%s\">\n", c->interface);
953 r = introspect_write_interface(&intro, c->vtable);
957 previous_interface = c->interface;
960 if (previous_interface)
961 fputs(" </interface>\n", intro.f);
964 /* Nothing?, let's see if we exist at all, and if not
965 * refuse to do anything */
966 r = bus_node_exists(bus, n, m->path, require_fallback);
968 r = bus_maybe_reply_error(m, r, &error);
971 if (bus->nodes_modified) {
977 *found_object = true;
979 r = introspect_write_child_nodes(&intro, s, m->path);
983 r = introspect_finish(&intro, bus, m, &reply);
987 r = sd_bus_send(bus, reply, NULL);
994 introspect_free(&intro);
998 static int object_manager_serialize_path(
1000 sd_bus_message *reply,
1003 bool require_fallback,
1004 sd_bus_error *error) {
1006 const char *previous_interface = NULL;
1007 bool found_something = false;
1008 struct node_vtable *i;
1018 n = hashmap_get(bus->nodes, prefix);
1022 LIST_FOREACH(vtables, i, n->vtables) {
1025 if (require_fallback && !i->is_fallback)
1028 r = node_vtable_get_userdata(bus, path, i, &u, error);
1031 if (bus->nodes_modified)
1036 if (!found_something) {
1038 /* Open the object part */
1040 r = sd_bus_message_open_container(reply, 'e', "oa{sa{sv}}");
1044 r = sd_bus_message_append(reply, "o", path);
1048 r = sd_bus_message_open_container(reply, 'a', "{sa{sv}}");
1052 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
1056 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
1060 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
1064 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
1068 found_something = true;
1071 if (!streq_ptr(previous_interface, i->interface)) {
1073 /* Maybe close the previous interface part */
1075 if (previous_interface) {
1076 r = sd_bus_message_close_container(reply);
1080 r = sd_bus_message_close_container(reply);
1085 /* Open the new interface part */
1087 r = sd_bus_message_open_container(reply, 'e', "sa{sv}");
1091 r = sd_bus_message_append(reply, "s", i->interface);
1095 r = sd_bus_message_open_container(reply, 'a', "{sv}");
1100 r = vtable_append_all_properties(bus, reply, path, i, u, error);
1103 if (bus->nodes_modified)
1106 previous_interface = i->interface;
1109 if (previous_interface) {
1110 r = sd_bus_message_close_container(reply);
1114 r = sd_bus_message_close_container(reply);
1119 if (found_something) {
1120 r = sd_bus_message_close_container(reply);
1124 r = sd_bus_message_close_container(reply);
1132 static int object_manager_serialize_path_and_fallbacks(
1134 sd_bus_message *reply,
1136 sd_bus_error *error) {
1146 /* First, add all vtables registered for this path */
1147 r = object_manager_serialize_path(bus, reply, path, path, false, error);
1150 if (bus->nodes_modified)
1153 /* Second, add fallback vtables registered for any of the prefixes */
1154 prefix = alloca(strlen(path) + 1);
1155 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
1156 r = object_manager_serialize_path(bus, reply, prefix, path, true, error);
1159 if (bus->nodes_modified)
1166 static int process_get_managed_objects(
1170 bool require_fallback,
1171 bool *found_object) {
1173 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1174 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1175 _cleanup_set_free_free_ Set *s = NULL;
1183 assert(found_object);
1185 /* Spec says, GetManagedObjects() is only implemented on the root of a
1186 * sub-tree. Therefore, we require a registered object-manager on
1187 * exactly the queried path, otherwise, we refuse to respond. */
1189 if (require_fallback || !n->object_managers)
1192 r = get_child_nodes(bus, m->path, n, CHILDREN_RECURSIVE, &s, &error);
1194 return bus_maybe_reply_error(m, r, &error);
1195 if (bus->nodes_modified)
1198 r = sd_bus_message_new_method_return(m, &reply);
1202 r = sd_bus_message_open_container(reply, 'a', "{oa{sa{sv}}}");
1206 SET_FOREACH(path, s, i) {
1207 r = object_manager_serialize_path_and_fallbacks(bus, reply, path, &error);
1209 return bus_maybe_reply_error(m, r, &error);
1211 if (bus->nodes_modified)
1215 r = sd_bus_message_close_container(reply);
1219 r = sd_bus_send(bus, reply, NULL);
1226 static int object_find_and_run(
1230 bool require_fallback,
1231 bool *found_object) {
1234 struct vtable_member vtable_key, *v;
1240 assert(found_object);
1242 n = hashmap_get(bus->nodes, p);
1246 /* First, try object callbacks */
1247 r = node_callbacks_run(bus, m, n->callbacks, require_fallback, found_object);
1250 if (bus->nodes_modified)
1253 if (!m->interface || !m->member)
1256 /* Then, look for a known method */
1257 vtable_key.path = (char*) p;
1258 vtable_key.interface = m->interface;
1259 vtable_key.member = m->member;
1261 v = hashmap_get(bus->vtable_methods, &vtable_key);
1263 r = method_callbacks_run(bus, m, v, require_fallback, found_object);
1266 if (bus->nodes_modified)
1270 /* Then, look for a known property */
1271 if (streq(m->interface, "org.freedesktop.DBus.Properties")) {
1274 get = streq(m->member, "Get");
1276 if (get || streq(m->member, "Set")) {
1278 r = sd_bus_message_rewind(m, true);
1282 vtable_key.path = (char*) p;
1284 r = sd_bus_message_read(m, "ss", &vtable_key.interface, &vtable_key.member);
1286 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected interface and member parameters");
1288 v = hashmap_get(bus->vtable_properties, &vtable_key);
1290 r = property_get_set_callbacks_run(bus, m, v, require_fallback, get, found_object);
1295 } else if (streq(m->member, "GetAll")) {
1298 r = sd_bus_message_rewind(m, true);
1302 r = sd_bus_message_read(m, "s", &iface);
1304 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected interface parameter");
1309 r = property_get_all_callbacks_run(bus, m, n->vtables, require_fallback, iface, found_object);
1314 } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1316 if (!isempty(sd_bus_message_get_signature(m, true)))
1317 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected no parameters");
1319 r = process_introspect(bus, m, n, require_fallback, found_object);
1323 } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.ObjectManager", "GetManagedObjects")) {
1325 if (!isempty(sd_bus_message_get_signature(m, true)))
1326 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected no parameters");
1328 r = process_get_managed_objects(bus, m, n, require_fallback, found_object);
1333 if (bus->nodes_modified)
1336 if (!*found_object) {
1337 r = bus_node_exists(bus, n, m->path, require_fallback);
1339 return bus_maybe_reply_error(m, r, NULL);
1340 if (bus->nodes_modified)
1343 *found_object = true;
1349 int bus_process_object(sd_bus *bus, sd_bus_message *m) {
1352 bool found_object = false;
1357 if (bus->is_monitor)
1360 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
1363 if (hashmap_isempty(bus->nodes))
1366 /* Never respond to broadcast messages */
1367 if (bus->bus_client && !m->destination)
1373 pl = strlen(m->path);
1377 bus->nodes_modified = false;
1379 r = object_find_and_run(bus, m, m->path, false, &found_object);
1383 /* Look for fallback prefixes */
1384 OBJECT_PATH_FOREACH_PREFIX(prefix, m->path) {
1386 if (bus->nodes_modified)
1389 r = object_find_and_run(bus, m, prefix, true, &found_object);
1394 } while (bus->nodes_modified);
1399 if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Get") ||
1400 sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Set"))
1401 r = sd_bus_reply_method_errorf(
1403 SD_BUS_ERROR_UNKNOWN_PROPERTY,
1404 "Unknown property or interface.");
1406 r = sd_bus_reply_method_errorf(
1408 SD_BUS_ERROR_UNKNOWN_METHOD,
1409 "Unknown method '%s' or interface '%s'.", m->member, m->interface);
1417 static struct node *bus_node_allocate(sd_bus *bus, const char *path) {
1418 struct node *n, *parent;
1420 _cleanup_free_ char *s = NULL;
1426 assert(path[0] == '/');
1428 n = hashmap_get(bus->nodes, path);
1432 r = hashmap_ensure_allocated(&bus->nodes, &string_hash_ops);
1440 if (streq(path, "/"))
1443 e = strrchr(path, '/');
1446 p = strndupa(path, MAX(1, e - path));
1448 parent = bus_node_allocate(bus, p);
1453 n = new0(struct node, 1);
1458 n->path = TAKE_PTR(s);
1460 r = hashmap_put(bus->nodes, n->path, n);
1467 LIST_PREPEND(siblings, parent->child, n);
1472 void bus_node_gc(sd_bus *b, struct node *n) {
1485 assert_se(hashmap_remove(b->nodes, n->path) == n);
1488 LIST_REMOVE(siblings, n->parent->child, n);
1491 bus_node_gc(b, n->parent);
1495 static int bus_find_parent_object_manager(sd_bus *bus, struct node **out, const char *path) {
1501 n = hashmap_get(bus->nodes, path);
1505 prefix = alloca(strlen(path) + 1);
1506 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
1507 n = hashmap_get(bus->nodes, prefix);
1513 while (n && !n->object_managers)
1521 static int bus_add_object(
1526 sd_bus_message_handler_t callback,
1533 assert_return(bus, -EINVAL);
1534 assert_return(bus = bus_resolve(bus), -ENOPKG);
1535 assert_return(object_path_is_valid(path), -EINVAL);
1536 assert_return(callback, -EINVAL);
1537 assert_return(!bus_pid_changed(bus), -ECHILD);
1539 n = bus_node_allocate(bus, path);
1543 s = bus_slot_allocate(bus, !slot, BUS_NODE_CALLBACK, sizeof(struct node_callback), userdata);
1549 s->node_callback.callback = callback;
1550 s->node_callback.is_fallback = fallback;
1552 s->node_callback.node = n;
1553 LIST_PREPEND(callbacks, n->callbacks, &s->node_callback);
1554 bus->nodes_modified = true;
1562 sd_bus_slot_unref(s);
1563 bus_node_gc(bus, n);
1568 _public_ int sd_bus_add_object(
1572 sd_bus_message_handler_t callback,
1575 return bus_add_object(bus, slot, false, path, callback, userdata);
1578 _public_ int sd_bus_add_fallback(
1582 sd_bus_message_handler_t callback,
1585 return bus_add_object(bus, slot, true, prefix, callback, userdata);
1588 static void vtable_member_hash_func(const void *a, struct siphash *state) {
1589 const struct vtable_member *m = a;
1593 string_hash_func(m->path, state);
1594 string_hash_func(m->interface, state);
1595 string_hash_func(m->member, state);
1598 static int vtable_member_compare_func(const void *a, const void *b) {
1599 const struct vtable_member *x = a, *y = b;
1605 r = strcmp(x->path, y->path);
1609 r = strcmp(x->interface, y->interface);
1613 return strcmp(x->member, y->member);
1616 static const struct hash_ops vtable_member_hash_ops = {
1617 .hash = vtable_member_hash_func,
1618 .compare = vtable_member_compare_func
1621 static int add_object_vtable_internal(
1625 const char *interface,
1626 const sd_bus_vtable *vtable,
1628 sd_bus_object_find_t find,
1631 sd_bus_slot *s = NULL;
1632 struct node_vtable *i, *existing = NULL;
1633 const sd_bus_vtable *v;
1637 assert_return(bus, -EINVAL);
1638 assert_return(bus = bus_resolve(bus), -ENOPKG);
1639 assert_return(object_path_is_valid(path), -EINVAL);
1640 assert_return(interface_name_is_valid(interface), -EINVAL);
1641 assert_return(vtable, -EINVAL);
1642 assert_return(vtable[0].type == _SD_BUS_VTABLE_START, -EINVAL);
1643 assert_return(vtable[0].x.start.element_size == sizeof(struct sd_bus_vtable), -EINVAL);
1644 assert_return(!bus_pid_changed(bus), -ECHILD);
1645 assert_return(!streq(interface, "org.freedesktop.DBus.Properties") &&
1646 !streq(interface, "org.freedesktop.DBus.Introspectable") &&
1647 !streq(interface, "org.freedesktop.DBus.Peer") &&
1648 !streq(interface, "org.freedesktop.DBus.ObjectManager"), -EINVAL);
1650 r = hashmap_ensure_allocated(&bus->vtable_methods, &vtable_member_hash_ops);
1654 r = hashmap_ensure_allocated(&bus->vtable_properties, &vtable_member_hash_ops);
1658 n = bus_node_allocate(bus, path);
1662 LIST_FOREACH(vtables, i, n->vtables) {
1663 if (i->is_fallback != fallback) {
1668 if (streq(i->interface, interface)) {
1670 if (i->vtable == vtable) {
1679 s = bus_slot_allocate(bus, !slot, BUS_NODE_VTABLE, sizeof(struct node_vtable), userdata);
1685 s->node_vtable.is_fallback = fallback;
1686 s->node_vtable.vtable = vtable;
1687 s->node_vtable.find = find;
1689 s->node_vtable.interface = strdup(interface);
1690 if (!s->node_vtable.interface) {
1695 for (v = s->node_vtable.vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
1699 case _SD_BUS_VTABLE_METHOD: {
1700 struct vtable_member *m;
1702 if (!member_name_is_valid(v->x.method.member) ||
1703 !signature_is_valid(strempty(v->x.method.signature), false) ||
1704 !signature_is_valid(strempty(v->x.method.result), false) ||
1705 !(v->x.method.handler || (isempty(v->x.method.signature) && isempty(v->x.method.result))) ||
1706 v->flags & (SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) {
1711 m = new0(struct vtable_member, 1);
1717 m->parent = &s->node_vtable;
1719 m->interface = s->node_vtable.interface;
1720 m->member = v->x.method.member;
1723 r = hashmap_put(bus->vtable_methods, m, m);
1732 case _SD_BUS_VTABLE_WRITABLE_PROPERTY:
1734 if (!(v->x.property.set || bus_type_is_basic(v->x.property.signature[0]))) {
1739 if (v->flags & SD_BUS_VTABLE_PROPERTY_CONST) {
1745 case _SD_BUS_VTABLE_PROPERTY: {
1746 struct vtable_member *m;
1748 if (!member_name_is_valid(v->x.property.member) ||
1749 !signature_is_single(v->x.property.signature, false) ||
1750 !(v->x.property.get || bus_type_is_basic(v->x.property.signature[0]) || streq(v->x.property.signature, "as")) ||
1751 (v->flags & SD_BUS_VTABLE_METHOD_NO_REPLY) ||
1752 (!!(v->flags & SD_BUS_VTABLE_PROPERTY_CONST) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) > 1 ||
1753 ((v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) && (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)) ||
1754 (v->flags & SD_BUS_VTABLE_UNPRIVILEGED && v->type == _SD_BUS_VTABLE_PROPERTY)) {
1759 m = new0(struct vtable_member, 1);
1765 m->parent = &s->node_vtable;
1767 m->interface = s->node_vtable.interface;
1768 m->member = v->x.property.member;
1771 r = hashmap_put(bus->vtable_properties, m, m);
1780 case _SD_BUS_VTABLE_SIGNAL:
1782 if (!member_name_is_valid(v->x.signal.member) ||
1783 !signature_is_valid(strempty(v->x.signal.signature), false) ||
1784 v->flags & SD_BUS_VTABLE_UNPRIVILEGED) {
1797 s->node_vtable.node = n;
1798 LIST_INSERT_AFTER(vtables, n->vtables, existing, &s->node_vtable);
1799 bus->nodes_modified = true;
1807 sd_bus_slot_unref(s);
1808 bus_node_gc(bus, n);
1813 _public_ int sd_bus_add_object_vtable(
1817 const char *interface,
1818 const sd_bus_vtable *vtable,
1821 return add_object_vtable_internal(bus, slot, path, interface, vtable, false, NULL, userdata);
1824 _public_ int sd_bus_add_fallback_vtable(
1828 const char *interface,
1829 const sd_bus_vtable *vtable,
1830 sd_bus_object_find_t find,
1833 return add_object_vtable_internal(bus, slot, prefix, interface, vtable, true, find, userdata);
1836 _public_ int sd_bus_add_node_enumerator(
1840 sd_bus_node_enumerator_t callback,
1847 assert_return(bus, -EINVAL);
1848 assert_return(bus = bus_resolve(bus), -ENOPKG);
1849 assert_return(object_path_is_valid(path), -EINVAL);
1850 assert_return(callback, -EINVAL);
1851 assert_return(!bus_pid_changed(bus), -ECHILD);
1853 n = bus_node_allocate(bus, path);
1857 s = bus_slot_allocate(bus, !slot, BUS_NODE_ENUMERATOR, sizeof(struct node_enumerator), userdata);
1863 s->node_enumerator.callback = callback;
1865 s->node_enumerator.node = n;
1866 LIST_PREPEND(enumerators, n->enumerators, &s->node_enumerator);
1867 bus->nodes_modified = true;
1875 sd_bus_slot_unref(s);
1876 bus_node_gc(bus, n);
1881 static int emit_properties_changed_on_interface(
1885 const char *interface,
1886 bool require_fallback,
1887 bool *found_interface,
1890 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1891 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1892 bool has_invalidating = false, has_changing = false;
1893 struct vtable_member key = {};
1894 struct node_vtable *c;
1904 assert(found_interface);
1906 n = hashmap_get(bus->nodes, prefix);
1910 r = sd_bus_message_new_signal(bus, &m, path, "org.freedesktop.DBus.Properties", "PropertiesChanged");
1914 r = sd_bus_message_append(m, "s", interface);
1918 r = sd_bus_message_open_container(m, 'a', "{sv}");
1923 key.interface = interface;
1925 LIST_FOREACH(vtables, c, n->vtables) {
1926 if (require_fallback && !c->is_fallback)
1929 if (!streq(c->interface, interface))
1932 r = node_vtable_get_userdata(bus, path, c, &u, &error);
1935 if (bus->nodes_modified)
1940 *found_interface = true;
1943 /* If the caller specified a list of
1944 * properties we include exactly those in the
1945 * PropertiesChanged message */
1947 STRV_FOREACH(property, names) {
1948 struct vtable_member *v;
1950 assert_return(member_name_is_valid(*property), -EINVAL);
1952 key.member = *property;
1953 v = hashmap_get(bus->vtable_properties, &key);
1957 /* If there are two vtables for the same
1958 * interface, let's handle this property when
1959 * we come to that vtable. */
1963 assert_return(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE ||
1964 v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION, -EDOM);
1966 assert_return(!(v->vtable->flags & SD_BUS_VTABLE_HIDDEN), -EDOM);
1968 if (v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) {
1969 has_invalidating = true;
1973 has_changing = true;
1975 r = vtable_append_one_property(bus, m, m->path, c, v->vtable, u, &error);
1978 if (bus->nodes_modified)
1982 const sd_bus_vtable *v;
1984 /* If the caller specified no properties list
1985 * we include all properties that are marked
1986 * as changing in the message. */
1988 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
1989 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
1992 if (v->flags & SD_BUS_VTABLE_HIDDEN)
1995 if (v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) {
1996 has_invalidating = true;
2000 if (!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))
2003 has_changing = true;
2005 r = vtable_append_one_property(bus, m, m->path, c, v, u, &error);
2008 if (bus->nodes_modified)
2014 if (!has_invalidating && !has_changing)
2017 r = sd_bus_message_close_container(m);
2021 r = sd_bus_message_open_container(m, 'a', "s");
2025 if (has_invalidating) {
2026 LIST_FOREACH(vtables, c, n->vtables) {
2027 if (require_fallback && !c->is_fallback)
2030 if (!streq(c->interface, interface))
2033 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2036 if (bus->nodes_modified)
2042 STRV_FOREACH(property, names) {
2043 struct vtable_member *v;
2045 key.member = *property;
2046 assert_se(v = hashmap_get(bus->vtable_properties, &key));
2047 assert(c == v->parent);
2049 if (!(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION))
2052 r = sd_bus_message_append(m, "s", *property);
2057 const sd_bus_vtable *v;
2059 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
2060 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
2063 if (v->flags & SD_BUS_VTABLE_HIDDEN)
2066 if (!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION))
2069 r = sd_bus_message_append(m, "s", v->x.property.member);
2077 r = sd_bus_message_close_container(m);
2081 r = sd_bus_send(bus, m, NULL);
2088 _public_ int sd_bus_emit_properties_changed_strv(
2091 const char *interface,
2094 BUS_DONT_DESTROY(bus);
2095 bool found_interface = false;
2099 assert_return(bus, -EINVAL);
2100 assert_return(bus = bus_resolve(bus), -ENOPKG);
2101 assert_return(object_path_is_valid(path), -EINVAL);
2102 assert_return(interface_name_is_valid(interface), -EINVAL);
2103 assert_return(!bus_pid_changed(bus), -ECHILD);
2105 if (!BUS_IS_OPEN(bus->state))
2108 /* A non-NULL but empty names list means nothing needs to be
2109 generated. A NULL list OTOH indicates that all properties
2110 that are set to EMITS_CHANGE or EMITS_INVALIDATION shall be
2111 included in the PropertiesChanged message. */
2112 if (names && names[0] == NULL)
2116 bus->nodes_modified = false;
2118 r = emit_properties_changed_on_interface(bus, path, path, interface, false, &found_interface, names);
2121 if (bus->nodes_modified)
2124 prefix = alloca(strlen(path) + 1);
2125 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2126 r = emit_properties_changed_on_interface(bus, prefix, path, interface, true, &found_interface, names);
2129 if (bus->nodes_modified)
2133 } while (bus->nodes_modified);
2135 return found_interface ? 0 : -ENOENT;
2138 _public_ int sd_bus_emit_properties_changed(
2141 const char *interface,
2142 const char *name, ...) {
2146 assert_return(bus, -EINVAL);
2147 assert_return(bus = bus_resolve(bus), -ENOPKG);
2148 assert_return(object_path_is_valid(path), -EINVAL);
2149 assert_return(interface_name_is_valid(interface), -EINVAL);
2150 assert_return(!bus_pid_changed(bus), -ECHILD);
2152 if (!BUS_IS_OPEN(bus->state))
2158 names = strv_from_stdarg_alloca(name);
2160 return sd_bus_emit_properties_changed_strv(bus, path, interface, names);
2163 static int object_added_append_all_prefix(
2169 bool require_fallback) {
2171 const char *previous_interface = NULL;
2172 struct node_vtable *c;
2182 n = hashmap_get(bus->nodes, prefix);
2186 LIST_FOREACH(vtables, c, n->vtables) {
2187 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2190 if (require_fallback && !c->is_fallback)
2193 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2196 if (bus->nodes_modified)
2201 if (!streq_ptr(c->interface, previous_interface)) {
2202 /* If a child-node already handled this interface, we
2203 * skip it on any of its parents. The child vtables
2204 * always fully override any conflicting vtables of
2205 * any parent node. */
2206 if (set_get(s, c->interface))
2209 r = set_put(s, c->interface);
2213 if (previous_interface) {
2214 r = sd_bus_message_close_container(m);
2217 r = sd_bus_message_close_container(m);
2222 r = sd_bus_message_open_container(m, 'e', "sa{sv}");
2225 r = sd_bus_message_append(m, "s", c->interface);
2228 r = sd_bus_message_open_container(m, 'a', "{sv}");
2232 previous_interface = c->interface;
2235 r = vtable_append_all_properties(bus, m, path, c, u, &error);
2238 if (bus->nodes_modified)
2242 if (previous_interface) {
2243 r = sd_bus_message_close_container(m);
2246 r = sd_bus_message_close_container(m);
2254 static int object_added_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
2255 _cleanup_set_free_ Set *s = NULL;
2264 * This appends all interfaces registered on path @path. We first add
2265 * the builtin interfaces, which are always available and handled by
2266 * sd-bus. Then, we add all interfaces registered on the exact node,
2267 * followed by all fallback interfaces registered on any parent prefix.
2269 * If an interface is registered multiple times on the same node with
2270 * different vtables, we merge all the properties across all vtables.
2271 * However, if a child node has the same interface registered as one of
2272 * its parent nodes has as fallback, we make the child overwrite the
2273 * parent instead of extending it. Therefore, we keep a "Set" of all
2274 * handled interfaces during parent traversal, so we skip interfaces on
2275 * a parent that were overwritten by a child.
2278 s = set_new(&string_hash_ops);
2282 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
2285 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
2288 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
2291 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
2295 r = object_added_append_all_prefix(bus, m, s, path, path, false);
2298 if (bus->nodes_modified)
2301 prefix = alloca(strlen(path) + 1);
2302 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2303 r = object_added_append_all_prefix(bus, m, s, prefix, path, true);
2306 if (bus->nodes_modified)
2313 _public_ int sd_bus_emit_object_added(sd_bus *bus, const char *path) {
2314 BUS_DONT_DESTROY(bus);
2316 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2317 struct node *object_manager;
2321 * This emits an InterfacesAdded signal on the given path, by iterating
2322 * all registered vtables and fallback vtables on the path. All
2323 * properties are queried and included in the signal.
2324 * This call is equivalent to sd_bus_emit_interfaces_added() with an
2325 * explicit list of registered interfaces. However, unlike
2326 * interfaces_added(), this call can figure out the list of supported
2327 * interfaces itself. Furthermore, it properly adds the builtin
2328 * org.freedesktop.DBus.* interfaces.
2331 assert_return(bus, -EINVAL);
2332 assert_return(bus = bus_resolve(bus), -ENOPKG);
2333 assert_return(object_path_is_valid(path), -EINVAL);
2334 assert_return(!bus_pid_changed(bus), -ECHILD);
2336 if (!BUS_IS_OPEN(bus->state))
2339 r = bus_find_parent_object_manager(bus, &object_manager, path);
2346 bus->nodes_modified = false;
2347 m = sd_bus_message_unref(m);
2349 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
2353 r = sd_bus_message_append_basic(m, 'o', path);
2357 r = sd_bus_message_open_container(m, 'a', "{sa{sv}}");
2361 r = object_added_append_all(bus, m, path);
2365 if (bus->nodes_modified)
2368 r = sd_bus_message_close_container(m);
2372 } while (bus->nodes_modified);
2374 return sd_bus_send(bus, m, NULL);
2377 static int object_removed_append_all_prefix(
2383 bool require_fallback) {
2385 const char *previous_interface = NULL;
2386 struct node_vtable *c;
2396 n = hashmap_get(bus->nodes, prefix);
2400 LIST_FOREACH(vtables, c, n->vtables) {
2401 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2404 if (require_fallback && !c->is_fallback)
2406 if (streq_ptr(c->interface, previous_interface))
2409 /* If a child-node already handled this interface, we
2410 * skip it on any of its parents. The child vtables
2411 * always fully override any conflicting vtables of
2412 * any parent node. */
2413 if (set_get(s, c->interface))
2416 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2419 if (bus->nodes_modified)
2424 r = set_put(s, c->interface);
2428 r = sd_bus_message_append(m, "s", c->interface);
2432 previous_interface = c->interface;
2438 static int object_removed_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
2439 _cleanup_set_free_ Set *s = NULL;
2447 /* see sd_bus_emit_object_added() for details */
2449 s = set_new(&string_hash_ops);
2453 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Peer");
2456 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Introspectable");
2459 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Properties");
2462 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.ObjectManager");
2466 r = object_removed_append_all_prefix(bus, m, s, path, path, false);
2469 if (bus->nodes_modified)
2472 prefix = alloca(strlen(path) + 1);
2473 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2474 r = object_removed_append_all_prefix(bus, m, s, prefix, path, true);
2477 if (bus->nodes_modified)
2484 _public_ int sd_bus_emit_object_removed(sd_bus *bus, const char *path) {
2485 BUS_DONT_DESTROY(bus);
2487 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2488 struct node *object_manager;
2492 * This is like sd_bus_emit_object_added(), but emits an
2493 * InterfacesRemoved signal on the given path. This only includes any
2494 * registered interfaces but skips the properties. Note that this will
2495 * call into the find() callbacks of any registered vtable. Therefore,
2496 * you must call this function before destroying/unlinking your object.
2497 * Otherwise, the list of interfaces will be incomplete. However, note
2498 * that this will *NOT* call into any property callback. Therefore, the
2499 * object might be in an "destructed" state, as long as we can find it.
2502 assert_return(bus, -EINVAL);
2503 assert_return(bus = bus_resolve(bus), -ENOPKG);
2504 assert_return(object_path_is_valid(path), -EINVAL);
2505 assert_return(!bus_pid_changed(bus), -ECHILD);
2507 if (!BUS_IS_OPEN(bus->state))
2510 r = bus_find_parent_object_manager(bus, &object_manager, path);
2517 bus->nodes_modified = false;
2518 m = sd_bus_message_unref(m);
2520 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
2524 r = sd_bus_message_append_basic(m, 'o', path);
2528 r = sd_bus_message_open_container(m, 'a', "s");
2532 r = object_removed_append_all(bus, m, path);
2536 if (bus->nodes_modified)
2539 r = sd_bus_message_close_container(m);
2543 } while (bus->nodes_modified);
2545 return sd_bus_send(bus, m, NULL);
2548 static int interfaces_added_append_one_prefix(
2553 const char *interface,
2554 bool require_fallback) {
2556 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2557 bool found_interface = false;
2558 struct node_vtable *c;
2569 n = hashmap_get(bus->nodes, prefix);
2573 LIST_FOREACH(vtables, c, n->vtables) {
2574 if (require_fallback && !c->is_fallback)
2577 if (!streq(c->interface, interface))
2580 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2583 if (bus->nodes_modified)
2588 if (!found_interface) {
2589 r = sd_bus_message_append_basic(m, 's', interface);
2593 r = sd_bus_message_open_container(m, 'a', "{sv}");
2597 found_interface = true;
2600 r = vtable_append_all_properties(bus, m, path, c, u, &error);
2603 if (bus->nodes_modified)
2607 if (found_interface) {
2608 r = sd_bus_message_close_container(m);
2613 return found_interface;
2616 static int interfaces_added_append_one(
2620 const char *interface) {
2630 r = interfaces_added_append_one_prefix(bus, m, path, path, interface, false);
2633 if (bus->nodes_modified)
2636 prefix = alloca(strlen(path) + 1);
2637 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2638 r = interfaces_added_append_one_prefix(bus, m, prefix, path, interface, true);
2641 if (bus->nodes_modified)
2648 _public_ int sd_bus_emit_interfaces_added_strv(sd_bus *bus, const char *path, char **interfaces) {
2649 BUS_DONT_DESTROY(bus);
2651 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2652 struct node *object_manager;
2656 assert_return(bus, -EINVAL);
2657 assert_return(bus = bus_resolve(bus), -ENOPKG);
2658 assert_return(object_path_is_valid(path), -EINVAL);
2659 assert_return(!bus_pid_changed(bus), -ECHILD);
2661 if (!BUS_IS_OPEN(bus->state))
2664 if (strv_isempty(interfaces))
2667 r = bus_find_parent_object_manager(bus, &object_manager, path);
2674 bus->nodes_modified = false;
2675 m = sd_bus_message_unref(m);
2677 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
2681 r = sd_bus_message_append_basic(m, 'o', path);
2685 r = sd_bus_message_open_container(m, 'a', "{sa{sv}}");
2689 STRV_FOREACH(i, interfaces) {
2690 assert_return(interface_name_is_valid(*i), -EINVAL);
2692 r = sd_bus_message_open_container(m, 'e', "sa{sv}");
2696 r = interfaces_added_append_one(bus, m, path, *i);
2700 if (bus->nodes_modified)
2703 r = sd_bus_message_close_container(m);
2708 if (bus->nodes_modified)
2711 r = sd_bus_message_close_container(m);
2715 } while (bus->nodes_modified);
2717 return sd_bus_send(bus, m, NULL);
2720 _public_ int sd_bus_emit_interfaces_added(sd_bus *bus, const char *path, const char *interface, ...) {
2723 assert_return(bus, -EINVAL);
2724 assert_return(bus = bus_resolve(bus), -ENOPKG);
2725 assert_return(object_path_is_valid(path), -EINVAL);
2726 assert_return(!bus_pid_changed(bus), -ECHILD);
2728 if (!BUS_IS_OPEN(bus->state))
2731 interfaces = strv_from_stdarg_alloca(interface);
2733 return sd_bus_emit_interfaces_added_strv(bus, path, interfaces);
2736 _public_ int sd_bus_emit_interfaces_removed_strv(sd_bus *bus, const char *path, char **interfaces) {
2737 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2738 struct node *object_manager;
2741 assert_return(bus, -EINVAL);
2742 assert_return(bus = bus_resolve(bus), -ENOPKG);
2743 assert_return(object_path_is_valid(path), -EINVAL);
2744 assert_return(!bus_pid_changed(bus), -ECHILD);
2746 if (!BUS_IS_OPEN(bus->state))
2749 if (strv_isempty(interfaces))
2752 r = bus_find_parent_object_manager(bus, &object_manager, path);
2758 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
2762 r = sd_bus_message_append_basic(m, 'o', path);
2766 r = sd_bus_message_append_strv(m, interfaces);
2770 return sd_bus_send(bus, m, NULL);
2773 _public_ int sd_bus_emit_interfaces_removed(sd_bus *bus, const char *path, const char *interface, ...) {
2776 assert_return(bus, -EINVAL);
2777 assert_return(bus = bus_resolve(bus), -ENOPKG);
2778 assert_return(object_path_is_valid(path), -EINVAL);
2779 assert_return(!bus_pid_changed(bus), -ECHILD);
2781 if (!BUS_IS_OPEN(bus->state))
2784 interfaces = strv_from_stdarg_alloca(interface);
2786 return sd_bus_emit_interfaces_removed_strv(bus, path, interfaces);
2789 _public_ int sd_bus_add_object_manager(sd_bus *bus, sd_bus_slot **slot, const char *path) {
2794 assert_return(bus, -EINVAL);
2795 assert_return(bus = bus_resolve(bus), -ENOPKG);
2796 assert_return(object_path_is_valid(path), -EINVAL);
2797 assert_return(!bus_pid_changed(bus), -ECHILD);
2799 n = bus_node_allocate(bus, path);
2803 s = bus_slot_allocate(bus, !slot, BUS_NODE_OBJECT_MANAGER, sizeof(struct node_object_manager), NULL);
2809 s->node_object_manager.node = n;
2810 LIST_PREPEND(object_managers, n->object_managers, &s->node_object_manager);
2811 bus->nodes_modified = true;
2819 sd_bus_slot_unref(s);
2820 bus_node_gc(bus, n);