1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "bus-internal.h"
7 #include "bus-introspect.h"
8 #include "bus-message.h"
9 #include "bus-objects.h"
10 #include "bus-signature.h"
15 #include "string-util.h"
18 static int node_vtable_get_userdata(
21 struct node_vtable *c,
23 sd_bus_error *error) {
33 s = container_of(c, sd_bus_slot, node_vtable);
36 bus->current_slot = sd_bus_slot_ref(s);
37 bus->current_userdata = u;
38 r = c->find(bus, path, c->interface, u, &found_u, error);
39 bus->current_userdata = NULL;
40 bus->current_slot = sd_bus_slot_unref(s);
44 if (sd_bus_error_is_set(error))
45 return -sd_bus_error_get_errno(error);
57 static void *vtable_method_convert_userdata(const sd_bus_vtable *p, void *u) {
60 return (uint8_t*) u + p->x.method.offset;
63 static void *vtable_property_convert_userdata(const sd_bus_vtable *p, void *u) {
66 return (uint8_t*) u + p->x.property.offset;
69 static int vtable_property_get_userdata(
72 struct vtable_member *p,
74 sd_bus_error *error) {
84 r = node_vtable_get_userdata(bus, path, p->parent, &u, error);
87 if (bus->nodes_modified)
90 *userdata = vtable_property_convert_userdata(p->vtable, u);
94 static int add_enumerated_to_set(
97 struct node_enumerator *first,
99 sd_bus_error *error) {
101 struct node_enumerator *c;
108 LIST_FOREACH(enumerators, c, first) {
109 char **children = NULL, **k;
112 if (bus->nodes_modified)
115 slot = container_of(c, sd_bus_slot, node_enumerator);
117 bus->current_slot = sd_bus_slot_ref(slot);
118 bus->current_userdata = slot->userdata;
119 r = c->callback(bus, prefix, slot->userdata, &children, error);
120 bus->current_userdata = NULL;
121 bus->current_slot = sd_bus_slot_unref(slot);
125 if (sd_bus_error_is_set(error))
126 return -sd_bus_error_get_errno(error);
128 STRV_FOREACH(k, children) {
134 if (!object_path_is_valid(*k)) {
140 if (!object_path_startswith(*k, prefix)) {
145 r = set_consume(s, *k);
159 /* if set, add_subtree() works recursively */
160 CHILDREN_RECURSIVE = 1 << 0,
161 /* if set, add_subtree() scans object-manager hierarchies recursively */
162 CHILDREN_SUBHIERARCHIES = 1 << 1,
165 static int add_subtree_to_set(
171 sd_bus_error *error) {
181 r = add_enumerated_to_set(bus, prefix, n->enumerators, s, error);
184 if (bus->nodes_modified)
187 LIST_FOREACH(siblings, i, n->child) {
190 if (!object_path_startswith(i->path, prefix))
197 r = set_consume(s, t);
198 if (r < 0 && r != -EEXIST)
201 if ((flags & CHILDREN_RECURSIVE) &&
202 ((flags & CHILDREN_SUBHIERARCHIES) || !i->object_managers)) {
203 r = add_subtree_to_set(bus, prefix, i, flags, s, error);
206 if (bus->nodes_modified)
214 static int get_child_nodes(
220 sd_bus_error *error) {
230 s = set_new(&string_hash_ops);
234 r = add_subtree_to_set(bus, prefix, n, flags, s, error);
244 static int node_callbacks_run(
247 struct node_callback *first,
248 bool require_fallback,
249 bool *found_object) {
251 struct node_callback *c;
256 assert(found_object);
258 LIST_FOREACH(callbacks, c, first) {
259 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
262 if (bus->nodes_modified)
265 if (require_fallback && !c->is_fallback)
268 *found_object = true;
270 if (c->last_iteration == bus->iteration_counter)
273 c->last_iteration = bus->iteration_counter;
275 r = sd_bus_message_rewind(m, true);
279 slot = container_of(c, sd_bus_slot, node_callback);
281 bus->current_slot = sd_bus_slot_ref(slot);
282 bus->current_handler = c->callback;
283 bus->current_userdata = slot->userdata;
284 r = c->callback(m, slot->userdata, &error_buffer);
285 bus->current_userdata = NULL;
286 bus->current_handler = NULL;
287 bus->current_slot = sd_bus_slot_unref(slot);
289 r = bus_maybe_reply_error(m, r, &error_buffer);
297 #define CAPABILITY_SHIFT(x) (((x) >> __builtin_ctzll(_SD_BUS_VTABLE_CAPABILITY_MASK)) & 0xFFFF)
299 static int check_access(sd_bus *bus, sd_bus_message *m, struct vtable_member *c, sd_bus_error *error) {
307 /* If the entire bus is trusted let's grant access */
311 /* If the member is marked UNPRIVILEGED let's grant access */
312 if (c->vtable->flags & SD_BUS_VTABLE_UNPRIVILEGED)
315 /* Check have the caller has the requested capability
316 * set. Note that the flags value contains the capability
317 * number plus one, which we need to subtract here. We do this
318 * so that we have 0 as special value for "default
320 cap = CAPABILITY_SHIFT(c->vtable->flags);
322 cap = CAPABILITY_SHIFT(c->parent->vtable[0].flags);
328 r = sd_bus_query_sender_privilege(m, cap);
334 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Access to %s.%s() not permitted.", c->interface, c->member);
337 static int method_callbacks_run(
340 struct vtable_member *c,
341 bool require_fallback,
342 bool *found_object) {
344 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
345 const char *signature;
352 assert(found_object);
354 if (require_fallback && !c->parent->is_fallback)
357 r = check_access(bus, m, c, &error);
359 return bus_maybe_reply_error(m, r, &error);
361 r = node_vtable_get_userdata(bus, m->path, c->parent, &u, &error);
363 return bus_maybe_reply_error(m, r, &error);
364 if (bus->nodes_modified)
367 u = vtable_method_convert_userdata(c->vtable, u);
369 *found_object = true;
371 if (c->last_iteration == bus->iteration_counter)
374 c->last_iteration = bus->iteration_counter;
376 r = sd_bus_message_rewind(m, true);
380 signature = sd_bus_message_get_signature(m, true);
384 if (!streq(strempty(c->vtable->x.method.signature), signature))
385 return sd_bus_reply_method_errorf(
387 SD_BUS_ERROR_INVALID_ARGS,
388 "Invalid arguments '%s' to call %s.%s(), expecting '%s'.",
389 signature, c->interface, c->member, strempty(c->vtable->x.method.signature));
391 /* Keep track what the signature of the reply to this message
392 * should be, so that this can be enforced when sealing the
394 m->enforced_reply_signature = strempty(c->vtable->x.method.result);
396 if (c->vtable->x.method.handler) {
399 slot = container_of(c->parent, sd_bus_slot, node_vtable);
401 bus->current_slot = sd_bus_slot_ref(slot);
402 bus->current_handler = c->vtable->x.method.handler;
403 bus->current_userdata = u;
404 r = c->vtable->x.method.handler(m, u, &error);
405 bus->current_userdata = NULL;
406 bus->current_handler = NULL;
407 bus->current_slot = sd_bus_slot_unref(slot);
409 return bus_maybe_reply_error(m, r, &error);
412 /* If the method callback is NULL, make this a successful NOP */
413 r = sd_bus_reply_method_return(m, NULL);
420 static int invoke_property_get(
423 const sd_bus_vtable *v,
425 const char *interface,
426 const char *property,
427 sd_bus_message *reply,
429 sd_bus_error *error) {
442 if (v->x.property.get) {
444 bus->current_slot = sd_bus_slot_ref(slot);
445 bus->current_userdata = userdata;
446 r = v->x.property.get(bus, path, interface, property, reply, userdata, error);
447 bus->current_userdata = NULL;
448 bus->current_slot = sd_bus_slot_unref(slot);
452 if (sd_bus_error_is_set(error))
453 return -sd_bus_error_get_errno(error);
457 /* Automatic handling if no callback is defined. */
459 if (streq(v->x.property.signature, "as"))
460 return sd_bus_message_append_strv(reply, *(char***) userdata);
462 assert(signature_is_single(v->x.property.signature, false));
463 assert(bus_type_is_basic(v->x.property.signature[0]));
465 switch (v->x.property.signature[0]) {
467 case SD_BUS_TYPE_STRING:
468 case SD_BUS_TYPE_SIGNATURE:
469 p = strempty(*(char**) userdata);
472 case SD_BUS_TYPE_OBJECT_PATH:
473 p = *(char**) userdata;
482 return sd_bus_message_append_basic(reply, v->x.property.signature[0], p);
485 static int invoke_property_set(
488 const sd_bus_vtable *v,
490 const char *interface,
491 const char *property,
492 sd_bus_message *value,
494 sd_bus_error *error) {
506 if (v->x.property.set) {
508 bus->current_slot = sd_bus_slot_ref(slot);
509 bus->current_userdata = userdata;
510 r = v->x.property.set(bus, path, interface, property, value, userdata, error);
511 bus->current_userdata = NULL;
512 bus->current_slot = sd_bus_slot_unref(slot);
516 if (sd_bus_error_is_set(error))
517 return -sd_bus_error_get_errno(error);
521 /* Automatic handling if no callback is defined. */
523 assert(signature_is_single(v->x.property.signature, false));
524 assert(bus_type_is_basic(v->x.property.signature[0]));
526 switch (v->x.property.signature[0]) {
528 case SD_BUS_TYPE_STRING:
529 case SD_BUS_TYPE_OBJECT_PATH:
530 case SD_BUS_TYPE_SIGNATURE: {
534 r = sd_bus_message_read_basic(value, v->x.property.signature[0], &p);
542 free(*(char**) userdata);
543 *(char**) userdata = n;
549 r = sd_bus_message_read_basic(value, v->x.property.signature[0], userdata);
559 static int property_get_set_callbacks_run(
562 struct vtable_member *c,
563 bool require_fallback,
565 bool *found_object) {
567 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
568 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
576 assert(found_object);
578 if (require_fallback && !c->parent->is_fallback)
581 r = vtable_property_get_userdata(bus, m->path, c, &u, &error);
583 return bus_maybe_reply_error(m, r, &error);
584 if (bus->nodes_modified)
587 slot = container_of(c->parent, sd_bus_slot, node_vtable);
589 *found_object = true;
591 r = sd_bus_message_new_method_return(m, &reply);
596 /* Note that we do not protect against reexecution
597 * here (using the last_iteration check, see below),
598 * should the node tree have changed and we got called
599 * again. We assume that property Get() calls are
600 * ultimately without side-effects or if they aren't
601 * then at least idempotent. */
603 r = sd_bus_message_open_container(reply, 'v', c->vtable->x.property.signature);
607 /* Note that we do not do an access check here. Read
608 * access to properties is always unrestricted, since
609 * PropertiesChanged signals broadcast contents
612 r = invoke_property_get(bus, slot, c->vtable, m->path, c->interface, c->member, reply, u, &error);
614 return bus_maybe_reply_error(m, r, &error);
616 if (bus->nodes_modified)
619 r = sd_bus_message_close_container(reply);
624 const char *signature = NULL;
627 if (c->vtable->type != _SD_BUS_VTABLE_WRITABLE_PROPERTY)
628 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Property '%s' is not writable.", c->member);
630 /* Avoid that we call the set routine more than once
631 * if the processing of this message got restarted
632 * because the node tree changed. */
633 if (c->last_iteration == bus->iteration_counter)
636 c->last_iteration = bus->iteration_counter;
638 r = sd_bus_message_peek_type(m, &type, &signature);
642 if (type != 'v' || !streq(strempty(signature), strempty(c->vtable->x.property.signature)))
643 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));
645 r = sd_bus_message_enter_container(m, 'v', c->vtable->x.property.signature);
649 r = check_access(bus, m, c, &error);
651 return bus_maybe_reply_error(m, r, &error);
653 r = invoke_property_set(bus, slot, c->vtable, m->path, c->interface, c->member, m, u, &error);
655 return bus_maybe_reply_error(m, r, &error);
657 if (bus->nodes_modified)
660 r = sd_bus_message_exit_container(m);
665 r = sd_bus_send(bus, reply, NULL);
672 static int vtable_append_one_property(
674 sd_bus_message *reply,
676 struct node_vtable *c,
677 const sd_bus_vtable *v,
679 sd_bus_error *error) {
690 r = sd_bus_message_open_container(reply, 'e', "sv");
694 r = sd_bus_message_append(reply, "s", v->x.property.member);
698 r = sd_bus_message_open_container(reply, 'v', v->x.property.signature);
702 slot = container_of(c, sd_bus_slot, node_vtable);
704 r = invoke_property_get(bus, slot, v, path, c->interface, v->x.property.member, reply, vtable_property_convert_userdata(v, userdata), error);
707 if (bus->nodes_modified)
710 r = sd_bus_message_close_container(reply);
714 r = sd_bus_message_close_container(reply);
721 static int vtable_append_all_properties(
723 sd_bus_message *reply,
725 struct node_vtable *c,
727 sd_bus_error *error) {
729 const sd_bus_vtable *v;
737 if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN)
740 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
741 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
744 if (v->flags & SD_BUS_VTABLE_HIDDEN)
747 if (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)
750 r = vtable_append_one_property(bus, reply, path, c, v, userdata, error);
753 if (bus->nodes_modified)
760 static int property_get_all_callbacks_run(
763 struct node_vtable *first,
764 bool require_fallback,
766 bool *found_object) {
768 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
769 struct node_vtable *c;
770 bool found_interface;
775 assert(found_object);
777 r = sd_bus_message_new_method_return(m, &reply);
781 r = sd_bus_message_open_container(reply, 'a', "{sv}");
785 found_interface = !iface ||
786 streq(iface, "org.freedesktop.DBus.Properties") ||
787 streq(iface, "org.freedesktop.DBus.Peer") ||
788 streq(iface, "org.freedesktop.DBus.Introspectable");
790 LIST_FOREACH(vtables, c, first) {
791 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
794 if (require_fallback && !c->is_fallback)
797 r = node_vtable_get_userdata(bus, m->path, c, &u, &error);
799 return bus_maybe_reply_error(m, r, &error);
800 if (bus->nodes_modified)
805 *found_object = true;
807 if (iface && !streq(c->interface, iface))
809 found_interface = true;
811 r = vtable_append_all_properties(bus, reply, m->path, c, u, &error);
813 return bus_maybe_reply_error(m, r, &error);
814 if (bus->nodes_modified)
821 if (!found_interface) {
822 r = sd_bus_reply_method_errorf(
824 SD_BUS_ERROR_UNKNOWN_INTERFACE,
825 "Unknown interface '%s'.", iface);
832 r = sd_bus_message_close_container(reply);
836 r = sd_bus_send(bus, reply, NULL);
843 static int bus_node_exists(
847 bool require_fallback) {
849 struct node_vtable *c;
850 struct node_callback *k;
857 /* Tests if there's anything attached directly to this node
858 * for the specified path */
860 if (!require_fallback && (n->enumerators || n->object_managers))
863 LIST_FOREACH(callbacks, k, n->callbacks) {
864 if (require_fallback && !k->is_fallback)
870 LIST_FOREACH(vtables, c, n->vtables) {
871 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
873 if (require_fallback && !c->is_fallback)
876 r = node_vtable_get_userdata(bus, path, c, NULL, &error);
879 if (bus->nodes_modified)
886 static int process_introspect(
890 bool require_fallback,
891 bool *found_object) {
893 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
894 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
895 _cleanup_set_free_free_ Set *s = NULL;
896 const char *previous_interface = NULL;
897 struct introspect intro;
898 struct node_vtable *c;
905 assert(found_object);
907 r = get_child_nodes(bus, m->path, n, 0, &s, &error);
909 return bus_maybe_reply_error(m, r, &error);
910 if (bus->nodes_modified)
913 r = introspect_begin(&intro, bus->trusted);
917 r = introspect_write_default_interfaces(&intro, !require_fallback && n->object_managers);
921 empty = set_isempty(s);
923 LIST_FOREACH(vtables, c, n->vtables) {
924 if (require_fallback && !c->is_fallback)
927 r = node_vtable_get_userdata(bus, m->path, c, NULL, &error);
929 r = bus_maybe_reply_error(m, r, &error);
932 if (bus->nodes_modified) {
941 if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN)
944 if (!streq_ptr(previous_interface, c->interface)) {
946 if (previous_interface)
947 fputs(" </interface>\n", intro.f);
949 fprintf(intro.f, " <interface name=\"%s\">\n", c->interface);
952 r = introspect_write_interface(&intro, c->vtable);
956 previous_interface = c->interface;
959 if (previous_interface)
960 fputs(" </interface>\n", intro.f);
963 /* Nothing?, let's see if we exist at all, and if not
964 * refuse to do anything */
965 r = bus_node_exists(bus, n, m->path, require_fallback);
967 r = bus_maybe_reply_error(m, r, &error);
970 if (bus->nodes_modified) {
976 *found_object = true;
978 r = introspect_write_child_nodes(&intro, s, m->path);
982 r = introspect_finish(&intro, bus, m, &reply);
986 r = sd_bus_send(bus, reply, NULL);
993 introspect_free(&intro);
997 static int object_manager_serialize_path(
999 sd_bus_message *reply,
1002 bool require_fallback,
1003 sd_bus_error *error) {
1005 const char *previous_interface = NULL;
1006 bool found_something = false;
1007 struct node_vtable *i;
1017 n = hashmap_get(bus->nodes, prefix);
1021 LIST_FOREACH(vtables, i, n->vtables) {
1024 if (require_fallback && !i->is_fallback)
1027 r = node_vtable_get_userdata(bus, path, i, &u, error);
1030 if (bus->nodes_modified)
1035 if (!found_something) {
1037 /* Open the object part */
1039 r = sd_bus_message_open_container(reply, 'e', "oa{sa{sv}}");
1043 r = sd_bus_message_append(reply, "o", path);
1047 r = sd_bus_message_open_container(reply, 'a', "{sa{sv}}");
1051 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
1055 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
1059 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
1063 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
1067 found_something = true;
1070 if (!streq_ptr(previous_interface, i->interface)) {
1072 /* Maybe close the previous interface part */
1074 if (previous_interface) {
1075 r = sd_bus_message_close_container(reply);
1079 r = sd_bus_message_close_container(reply);
1084 /* Open the new interface part */
1086 r = sd_bus_message_open_container(reply, 'e', "sa{sv}");
1090 r = sd_bus_message_append(reply, "s", i->interface);
1094 r = sd_bus_message_open_container(reply, 'a', "{sv}");
1099 r = vtable_append_all_properties(bus, reply, path, i, u, error);
1102 if (bus->nodes_modified)
1105 previous_interface = i->interface;
1108 if (previous_interface) {
1109 r = sd_bus_message_close_container(reply);
1113 r = sd_bus_message_close_container(reply);
1118 if (found_something) {
1119 r = sd_bus_message_close_container(reply);
1123 r = sd_bus_message_close_container(reply);
1131 static int object_manager_serialize_path_and_fallbacks(
1133 sd_bus_message *reply,
1135 sd_bus_error *error) {
1145 /* First, add all vtables registered for this path */
1146 r = object_manager_serialize_path(bus, reply, path, path, false, error);
1149 if (bus->nodes_modified)
1152 /* Second, add fallback vtables registered for any of the prefixes */
1153 prefix = alloca(strlen(path) + 1);
1154 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
1155 r = object_manager_serialize_path(bus, reply, prefix, path, true, error);
1158 if (bus->nodes_modified)
1165 static int process_get_managed_objects(
1169 bool require_fallback,
1170 bool *found_object) {
1172 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1173 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1174 _cleanup_set_free_free_ Set *s = NULL;
1182 assert(found_object);
1184 /* Spec says, GetManagedObjects() is only implemented on the root of a
1185 * sub-tree. Therefore, we require a registered object-manager on
1186 * exactly the queried path, otherwise, we refuse to respond. */
1188 if (require_fallback || !n->object_managers)
1191 r = get_child_nodes(bus, m->path, n, CHILDREN_RECURSIVE, &s, &error);
1193 return bus_maybe_reply_error(m, r, &error);
1194 if (bus->nodes_modified)
1197 r = sd_bus_message_new_method_return(m, &reply);
1201 r = sd_bus_message_open_container(reply, 'a', "{oa{sa{sv}}}");
1205 SET_FOREACH(path, s, i) {
1206 r = object_manager_serialize_path_and_fallbacks(bus, reply, path, &error);
1208 return bus_maybe_reply_error(m, r, &error);
1210 if (bus->nodes_modified)
1214 r = sd_bus_message_close_container(reply);
1218 r = sd_bus_send(bus, reply, NULL);
1225 static int object_find_and_run(
1229 bool require_fallback,
1230 bool *found_object) {
1233 struct vtable_member vtable_key, *v;
1239 assert(found_object);
1241 n = hashmap_get(bus->nodes, p);
1245 /* First, try object callbacks */
1246 r = node_callbacks_run(bus, m, n->callbacks, require_fallback, found_object);
1249 if (bus->nodes_modified)
1252 if (!m->interface || !m->member)
1255 /* Then, look for a known method */
1256 vtable_key.path = (char*) p;
1257 vtable_key.interface = m->interface;
1258 vtable_key.member = m->member;
1260 v = hashmap_get(bus->vtable_methods, &vtable_key);
1262 r = method_callbacks_run(bus, m, v, require_fallback, found_object);
1265 if (bus->nodes_modified)
1269 /* Then, look for a known property */
1270 if (streq(m->interface, "org.freedesktop.DBus.Properties")) {
1273 get = streq(m->member, "Get");
1275 if (get || streq(m->member, "Set")) {
1277 r = sd_bus_message_rewind(m, true);
1281 vtable_key.path = (char*) p;
1283 r = sd_bus_message_read(m, "ss", &vtable_key.interface, &vtable_key.member);
1285 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected interface and member parameters");
1287 v = hashmap_get(bus->vtable_properties, &vtable_key);
1289 r = property_get_set_callbacks_run(bus, m, v, require_fallback, get, found_object);
1294 } else if (streq(m->member, "GetAll")) {
1297 r = sd_bus_message_rewind(m, true);
1301 r = sd_bus_message_read(m, "s", &iface);
1303 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected interface parameter");
1308 r = property_get_all_callbacks_run(bus, m, n->vtables, require_fallback, iface, found_object);
1313 } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1315 if (!isempty(sd_bus_message_get_signature(m, true)))
1316 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected no parameters");
1318 r = process_introspect(bus, m, n, require_fallback, found_object);
1322 } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.ObjectManager", "GetManagedObjects")) {
1324 if (!isempty(sd_bus_message_get_signature(m, true)))
1325 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected no parameters");
1327 r = process_get_managed_objects(bus, m, n, require_fallback, found_object);
1332 if (bus->nodes_modified)
1335 if (!*found_object) {
1336 r = bus_node_exists(bus, n, m->path, require_fallback);
1338 return bus_maybe_reply_error(m, r, NULL);
1339 if (bus->nodes_modified)
1342 *found_object = true;
1348 int bus_process_object(sd_bus *bus, sd_bus_message *m) {
1351 bool found_object = false;
1356 if (bus->is_monitor)
1359 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
1362 if (hashmap_isempty(bus->nodes))
1365 /* Never respond to broadcast messages */
1366 if (bus->bus_client && !m->destination)
1372 pl = strlen(m->path);
1376 bus->nodes_modified = false;
1378 r = object_find_and_run(bus, m, m->path, false, &found_object);
1382 /* Look for fallback prefixes */
1383 OBJECT_PATH_FOREACH_PREFIX(prefix, m->path) {
1385 if (bus->nodes_modified)
1388 r = object_find_and_run(bus, m, prefix, true, &found_object);
1393 } while (bus->nodes_modified);
1398 if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Get") ||
1399 sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Set"))
1400 r = sd_bus_reply_method_errorf(
1402 SD_BUS_ERROR_UNKNOWN_PROPERTY,
1403 "Unknown property or interface.");
1405 r = sd_bus_reply_method_errorf(
1407 SD_BUS_ERROR_UNKNOWN_METHOD,
1408 "Unknown method '%s' or interface '%s'.", m->member, m->interface);
1416 static struct node *bus_node_allocate(sd_bus *bus, const char *path) {
1417 struct node *n, *parent;
1419 _cleanup_free_ char *s = NULL;
1425 assert(path[0] == '/');
1427 n = hashmap_get(bus->nodes, path);
1431 r = hashmap_ensure_allocated(&bus->nodes, &string_hash_ops);
1439 if (streq(path, "/"))
1442 e = strrchr(path, '/');
1445 p = strndupa(path, MAX(1, e - path));
1447 parent = bus_node_allocate(bus, p);
1452 n = new0(struct node, 1);
1457 n->path = TAKE_PTR(s);
1459 r = hashmap_put(bus->nodes, n->path, n);
1466 LIST_PREPEND(siblings, parent->child, n);
1471 void bus_node_gc(sd_bus *b, struct node *n) {
1484 assert_se(hashmap_remove(b->nodes, n->path) == n);
1487 LIST_REMOVE(siblings, n->parent->child, n);
1490 bus_node_gc(b, n->parent);
1494 static int bus_find_parent_object_manager(sd_bus *bus, struct node **out, const char *path) {
1500 n = hashmap_get(bus->nodes, path);
1504 prefix = alloca(strlen(path) + 1);
1505 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
1506 n = hashmap_get(bus->nodes, prefix);
1512 while (n && !n->object_managers)
1520 static int bus_add_object(
1525 sd_bus_message_handler_t callback,
1532 assert_return(bus, -EINVAL);
1533 assert_return(bus = bus_resolve(bus), -ENOPKG);
1534 assert_return(object_path_is_valid(path), -EINVAL);
1535 assert_return(callback, -EINVAL);
1536 assert_return(!bus_pid_changed(bus), -ECHILD);
1538 n = bus_node_allocate(bus, path);
1542 s = bus_slot_allocate(bus, !slot, BUS_NODE_CALLBACK, sizeof(struct node_callback), userdata);
1548 s->node_callback.callback = callback;
1549 s->node_callback.is_fallback = fallback;
1551 s->node_callback.node = n;
1552 LIST_PREPEND(callbacks, n->callbacks, &s->node_callback);
1553 bus->nodes_modified = true;
1561 sd_bus_slot_unref(s);
1562 bus_node_gc(bus, n);
1567 _public_ int sd_bus_add_object(
1571 sd_bus_message_handler_t callback,
1574 return bus_add_object(bus, slot, false, path, callback, userdata);
1577 _public_ int sd_bus_add_fallback(
1581 sd_bus_message_handler_t callback,
1584 return bus_add_object(bus, slot, true, prefix, callback, userdata);
1587 static void vtable_member_hash_func(const void *a, struct siphash *state) {
1588 const struct vtable_member *m = a;
1592 string_hash_func(m->path, state);
1593 string_hash_func(m->interface, state);
1594 string_hash_func(m->member, state);
1597 static int vtable_member_compare_func(const void *a, const void *b) {
1598 const struct vtable_member *x = a, *y = b;
1604 r = strcmp(x->path, y->path);
1608 r = strcmp(x->interface, y->interface);
1612 return strcmp(x->member, y->member);
1615 static const struct hash_ops vtable_member_hash_ops = {
1616 .hash = vtable_member_hash_func,
1617 .compare = vtable_member_compare_func
1620 static int add_object_vtable_internal(
1624 const char *interface,
1625 const sd_bus_vtable *vtable,
1627 sd_bus_object_find_t find,
1630 sd_bus_slot *s = NULL;
1631 struct node_vtable *i, *existing = NULL;
1632 const sd_bus_vtable *v;
1636 assert_return(bus, -EINVAL);
1637 assert_return(bus = bus_resolve(bus), -ENOPKG);
1638 assert_return(object_path_is_valid(path), -EINVAL);
1639 assert_return(interface_name_is_valid(interface), -EINVAL);
1640 assert_return(vtable, -EINVAL);
1641 assert_return(vtable[0].type == _SD_BUS_VTABLE_START, -EINVAL);
1642 assert_return(vtable[0].x.start.element_size == sizeof(struct sd_bus_vtable), -EINVAL);
1643 assert_return(!bus_pid_changed(bus), -ECHILD);
1644 assert_return(!streq(interface, "org.freedesktop.DBus.Properties") &&
1645 !streq(interface, "org.freedesktop.DBus.Introspectable") &&
1646 !streq(interface, "org.freedesktop.DBus.Peer") &&
1647 !streq(interface, "org.freedesktop.DBus.ObjectManager"), -EINVAL);
1649 r = hashmap_ensure_allocated(&bus->vtable_methods, &vtable_member_hash_ops);
1653 r = hashmap_ensure_allocated(&bus->vtable_properties, &vtable_member_hash_ops);
1657 n = bus_node_allocate(bus, path);
1661 LIST_FOREACH(vtables, i, n->vtables) {
1662 if (i->is_fallback != fallback) {
1667 if (streq(i->interface, interface)) {
1669 if (i->vtable == vtable) {
1678 s = bus_slot_allocate(bus, !slot, BUS_NODE_VTABLE, sizeof(struct node_vtable), userdata);
1684 s->node_vtable.is_fallback = fallback;
1685 s->node_vtable.vtable = vtable;
1686 s->node_vtable.find = find;
1688 s->node_vtable.interface = strdup(interface);
1689 if (!s->node_vtable.interface) {
1694 for (v = s->node_vtable.vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
1698 case _SD_BUS_VTABLE_METHOD: {
1699 struct vtable_member *m;
1701 if (!member_name_is_valid(v->x.method.member) ||
1702 !signature_is_valid(strempty(v->x.method.signature), false) ||
1703 !signature_is_valid(strempty(v->x.method.result), false) ||
1704 !(v->x.method.handler || (isempty(v->x.method.signature) && isempty(v->x.method.result))) ||
1705 v->flags & (SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) {
1710 m = new0(struct vtable_member, 1);
1716 m->parent = &s->node_vtable;
1718 m->interface = s->node_vtable.interface;
1719 m->member = v->x.method.member;
1722 r = hashmap_put(bus->vtable_methods, m, m);
1731 case _SD_BUS_VTABLE_WRITABLE_PROPERTY:
1733 if (!(v->x.property.set || bus_type_is_basic(v->x.property.signature[0]))) {
1738 if (v->flags & SD_BUS_VTABLE_PROPERTY_CONST) {
1744 case _SD_BUS_VTABLE_PROPERTY: {
1745 struct vtable_member *m;
1747 if (!member_name_is_valid(v->x.property.member) ||
1748 !signature_is_single(v->x.property.signature, false) ||
1749 !(v->x.property.get || bus_type_is_basic(v->x.property.signature[0]) || streq(v->x.property.signature, "as")) ||
1750 (v->flags & SD_BUS_VTABLE_METHOD_NO_REPLY) ||
1751 (!!(v->flags & SD_BUS_VTABLE_PROPERTY_CONST) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) > 1 ||
1752 ((v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) && (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)) ||
1753 (v->flags & SD_BUS_VTABLE_UNPRIVILEGED && v->type == _SD_BUS_VTABLE_PROPERTY)) {
1758 m = new0(struct vtable_member, 1);
1764 m->parent = &s->node_vtable;
1766 m->interface = s->node_vtable.interface;
1767 m->member = v->x.property.member;
1770 r = hashmap_put(bus->vtable_properties, m, m);
1779 case _SD_BUS_VTABLE_SIGNAL:
1781 if (!member_name_is_valid(v->x.signal.member) ||
1782 !signature_is_valid(strempty(v->x.signal.signature), false) ||
1783 v->flags & SD_BUS_VTABLE_UNPRIVILEGED) {
1796 s->node_vtable.node = n;
1797 LIST_INSERT_AFTER(vtables, n->vtables, existing, &s->node_vtable);
1798 bus->nodes_modified = true;
1806 sd_bus_slot_unref(s);
1807 bus_node_gc(bus, n);
1812 _public_ int sd_bus_add_object_vtable(
1816 const char *interface,
1817 const sd_bus_vtable *vtable,
1820 return add_object_vtable_internal(bus, slot, path, interface, vtable, false, NULL, userdata);
1823 _public_ int sd_bus_add_fallback_vtable(
1827 const char *interface,
1828 const sd_bus_vtable *vtable,
1829 sd_bus_object_find_t find,
1832 return add_object_vtable_internal(bus, slot, prefix, interface, vtable, true, find, userdata);
1835 _public_ int sd_bus_add_node_enumerator(
1839 sd_bus_node_enumerator_t callback,
1846 assert_return(bus, -EINVAL);
1847 assert_return(bus = bus_resolve(bus), -ENOPKG);
1848 assert_return(object_path_is_valid(path), -EINVAL);
1849 assert_return(callback, -EINVAL);
1850 assert_return(!bus_pid_changed(bus), -ECHILD);
1852 n = bus_node_allocate(bus, path);
1856 s = bus_slot_allocate(bus, !slot, BUS_NODE_ENUMERATOR, sizeof(struct node_enumerator), userdata);
1862 s->node_enumerator.callback = callback;
1864 s->node_enumerator.node = n;
1865 LIST_PREPEND(enumerators, n->enumerators, &s->node_enumerator);
1866 bus->nodes_modified = true;
1874 sd_bus_slot_unref(s);
1875 bus_node_gc(bus, n);
1880 static int emit_properties_changed_on_interface(
1884 const char *interface,
1885 bool require_fallback,
1886 bool *found_interface,
1889 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1890 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1891 bool has_invalidating = false, has_changing = false;
1892 struct vtable_member key = {};
1893 struct node_vtable *c;
1903 assert(found_interface);
1905 n = hashmap_get(bus->nodes, prefix);
1909 r = sd_bus_message_new_signal(bus, &m, path, "org.freedesktop.DBus.Properties", "PropertiesChanged");
1913 r = sd_bus_message_append(m, "s", interface);
1917 r = sd_bus_message_open_container(m, 'a', "{sv}");
1922 key.interface = interface;
1924 LIST_FOREACH(vtables, c, n->vtables) {
1925 if (require_fallback && !c->is_fallback)
1928 if (!streq(c->interface, interface))
1931 r = node_vtable_get_userdata(bus, path, c, &u, &error);
1934 if (bus->nodes_modified)
1939 *found_interface = true;
1942 /* If the caller specified a list of
1943 * properties we include exactly those in the
1944 * PropertiesChanged message */
1946 STRV_FOREACH(property, names) {
1947 struct vtable_member *v;
1949 assert_return(member_name_is_valid(*property), -EINVAL);
1951 key.member = *property;
1952 v = hashmap_get(bus->vtable_properties, &key);
1956 /* If there are two vtables for the same
1957 * interface, let's handle this property when
1958 * we come to that vtable. */
1962 assert_return(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE ||
1963 v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION, -EDOM);
1965 assert_return(!(v->vtable->flags & SD_BUS_VTABLE_HIDDEN), -EDOM);
1967 if (v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) {
1968 has_invalidating = true;
1972 has_changing = true;
1974 r = vtable_append_one_property(bus, m, m->path, c, v->vtable, u, &error);
1977 if (bus->nodes_modified)
1981 const sd_bus_vtable *v;
1983 /* If the caller specified no properties list
1984 * we include all properties that are marked
1985 * as changing in the message. */
1987 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
1988 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
1991 if (v->flags & SD_BUS_VTABLE_HIDDEN)
1994 if (v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) {
1995 has_invalidating = true;
1999 if (!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))
2002 has_changing = true;
2004 r = vtable_append_one_property(bus, m, m->path, c, v, u, &error);
2007 if (bus->nodes_modified)
2013 if (!has_invalidating && !has_changing)
2016 r = sd_bus_message_close_container(m);
2020 r = sd_bus_message_open_container(m, 'a', "s");
2024 if (has_invalidating) {
2025 LIST_FOREACH(vtables, c, n->vtables) {
2026 if (require_fallback && !c->is_fallback)
2029 if (!streq(c->interface, interface))
2032 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2035 if (bus->nodes_modified)
2041 STRV_FOREACH(property, names) {
2042 struct vtable_member *v;
2044 key.member = *property;
2045 assert_se(v = hashmap_get(bus->vtable_properties, &key));
2046 assert(c == v->parent);
2048 if (!(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION))
2051 r = sd_bus_message_append(m, "s", *property);
2056 const sd_bus_vtable *v;
2058 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
2059 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
2062 if (v->flags & SD_BUS_VTABLE_HIDDEN)
2065 if (!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION))
2068 r = sd_bus_message_append(m, "s", v->x.property.member);
2076 r = sd_bus_message_close_container(m);
2080 r = sd_bus_send(bus, m, NULL);
2087 _public_ int sd_bus_emit_properties_changed_strv(
2090 const char *interface,
2093 BUS_DONT_DESTROY(bus);
2094 bool found_interface = false;
2098 assert_return(bus, -EINVAL);
2099 assert_return(bus = bus_resolve(bus), -ENOPKG);
2100 assert_return(object_path_is_valid(path), -EINVAL);
2101 assert_return(interface_name_is_valid(interface), -EINVAL);
2102 assert_return(!bus_pid_changed(bus), -ECHILD);
2104 if (!BUS_IS_OPEN(bus->state))
2107 /* A non-NULL but empty names list means nothing needs to be
2108 generated. A NULL list OTOH indicates that all properties
2109 that are set to EMITS_CHANGE or EMITS_INVALIDATION shall be
2110 included in the PropertiesChanged message. */
2111 if (names && names[0] == NULL)
2115 bus->nodes_modified = false;
2117 r = emit_properties_changed_on_interface(bus, path, path, interface, false, &found_interface, names);
2120 if (bus->nodes_modified)
2123 prefix = alloca(strlen(path) + 1);
2124 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2125 r = emit_properties_changed_on_interface(bus, prefix, path, interface, true, &found_interface, names);
2128 if (bus->nodes_modified)
2132 } while (bus->nodes_modified);
2134 return found_interface ? 0 : -ENOENT;
2137 _public_ int sd_bus_emit_properties_changed(
2140 const char *interface,
2141 const char *name, ...) {
2145 assert_return(bus, -EINVAL);
2146 assert_return(bus = bus_resolve(bus), -ENOPKG);
2147 assert_return(object_path_is_valid(path), -EINVAL);
2148 assert_return(interface_name_is_valid(interface), -EINVAL);
2149 assert_return(!bus_pid_changed(bus), -ECHILD);
2151 if (!BUS_IS_OPEN(bus->state))
2157 names = strv_from_stdarg_alloca(name);
2159 return sd_bus_emit_properties_changed_strv(bus, path, interface, names);
2162 static int object_added_append_all_prefix(
2168 bool require_fallback) {
2170 const char *previous_interface = NULL;
2171 struct node_vtable *c;
2181 n = hashmap_get(bus->nodes, prefix);
2185 LIST_FOREACH(vtables, c, n->vtables) {
2186 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2189 if (require_fallback && !c->is_fallback)
2192 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2195 if (bus->nodes_modified)
2200 if (!streq_ptr(c->interface, previous_interface)) {
2201 /* If a child-node already handled this interface, we
2202 * skip it on any of its parents. The child vtables
2203 * always fully override any conflicting vtables of
2204 * any parent node. */
2205 if (set_get(s, c->interface))
2208 r = set_put(s, c->interface);
2212 if (previous_interface) {
2213 r = sd_bus_message_close_container(m);
2216 r = sd_bus_message_close_container(m);
2221 r = sd_bus_message_open_container(m, 'e', "sa{sv}");
2224 r = sd_bus_message_append(m, "s", c->interface);
2227 r = sd_bus_message_open_container(m, 'a', "{sv}");
2231 previous_interface = c->interface;
2234 r = vtable_append_all_properties(bus, m, path, c, u, &error);
2237 if (bus->nodes_modified)
2241 if (previous_interface) {
2242 r = sd_bus_message_close_container(m);
2245 r = sd_bus_message_close_container(m);
2253 static int object_added_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
2254 _cleanup_set_free_ Set *s = NULL;
2263 * This appends all interfaces registered on path @path. We first add
2264 * the builtin interfaces, which are always available and handled by
2265 * sd-bus. Then, we add all interfaces registered on the exact node,
2266 * followed by all fallback interfaces registered on any parent prefix.
2268 * If an interface is registered multiple times on the same node with
2269 * different vtables, we merge all the properties across all vtables.
2270 * However, if a child node has the same interface registered as one of
2271 * its parent nodes has as fallback, we make the child overwrite the
2272 * parent instead of extending it. Therefore, we keep a "Set" of all
2273 * handled interfaces during parent traversal, so we skip interfaces on
2274 * a parent that were overwritten by a child.
2277 s = set_new(&string_hash_ops);
2281 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
2284 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
2287 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
2290 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
2294 r = object_added_append_all_prefix(bus, m, s, path, path, false);
2297 if (bus->nodes_modified)
2300 prefix = alloca(strlen(path) + 1);
2301 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2302 r = object_added_append_all_prefix(bus, m, s, prefix, path, true);
2305 if (bus->nodes_modified)
2312 _public_ int sd_bus_emit_object_added(sd_bus *bus, const char *path) {
2313 BUS_DONT_DESTROY(bus);
2315 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2316 struct node *object_manager;
2320 * This emits an InterfacesAdded signal on the given path, by iterating
2321 * all registered vtables and fallback vtables on the path. All
2322 * properties are queried and included in the signal.
2323 * This call is equivalent to sd_bus_emit_interfaces_added() with an
2324 * explicit list of registered interfaces. However, unlike
2325 * interfaces_added(), this call can figure out the list of supported
2326 * interfaces itself. Furthermore, it properly adds the builtin
2327 * org.freedesktop.DBus.* interfaces.
2330 assert_return(bus, -EINVAL);
2331 assert_return(bus = bus_resolve(bus), -ENOPKG);
2332 assert_return(object_path_is_valid(path), -EINVAL);
2333 assert_return(!bus_pid_changed(bus), -ECHILD);
2335 if (!BUS_IS_OPEN(bus->state))
2338 r = bus_find_parent_object_manager(bus, &object_manager, path);
2345 bus->nodes_modified = false;
2346 m = sd_bus_message_unref(m);
2348 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
2352 r = sd_bus_message_append_basic(m, 'o', path);
2356 r = sd_bus_message_open_container(m, 'a', "{sa{sv}}");
2360 r = object_added_append_all(bus, m, path);
2364 if (bus->nodes_modified)
2367 r = sd_bus_message_close_container(m);
2371 } while (bus->nodes_modified);
2373 return sd_bus_send(bus, m, NULL);
2376 static int object_removed_append_all_prefix(
2382 bool require_fallback) {
2384 const char *previous_interface = NULL;
2385 struct node_vtable *c;
2395 n = hashmap_get(bus->nodes, prefix);
2399 LIST_FOREACH(vtables, c, n->vtables) {
2400 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2403 if (require_fallback && !c->is_fallback)
2405 if (streq_ptr(c->interface, previous_interface))
2408 /* If a child-node already handled this interface, we
2409 * skip it on any of its parents. The child vtables
2410 * always fully override any conflicting vtables of
2411 * any parent node. */
2412 if (set_get(s, c->interface))
2415 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2418 if (bus->nodes_modified)
2423 r = set_put(s, c->interface);
2427 r = sd_bus_message_append(m, "s", c->interface);
2431 previous_interface = c->interface;
2437 static int object_removed_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
2438 _cleanup_set_free_ Set *s = NULL;
2446 /* see sd_bus_emit_object_added() for details */
2448 s = set_new(&string_hash_ops);
2452 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Peer");
2455 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Introspectable");
2458 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Properties");
2461 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.ObjectManager");
2465 r = object_removed_append_all_prefix(bus, m, s, path, path, false);
2468 if (bus->nodes_modified)
2471 prefix = alloca(strlen(path) + 1);
2472 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2473 r = object_removed_append_all_prefix(bus, m, s, prefix, path, true);
2476 if (bus->nodes_modified)
2483 _public_ int sd_bus_emit_object_removed(sd_bus *bus, const char *path) {
2484 BUS_DONT_DESTROY(bus);
2486 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2487 struct node *object_manager;
2491 * This is like sd_bus_emit_object_added(), but emits an
2492 * InterfacesRemoved signal on the given path. This only includes any
2493 * registered interfaces but skips the properties. Note that this will
2494 * call into the find() callbacks of any registered vtable. Therefore,
2495 * you must call this function before destroying/unlinking your object.
2496 * Otherwise, the list of interfaces will be incomplete. However, note
2497 * that this will *NOT* call into any property callback. Therefore, the
2498 * object might be in an "destructed" state, as long as we can find it.
2501 assert_return(bus, -EINVAL);
2502 assert_return(bus = bus_resolve(bus), -ENOPKG);
2503 assert_return(object_path_is_valid(path), -EINVAL);
2504 assert_return(!bus_pid_changed(bus), -ECHILD);
2506 if (!BUS_IS_OPEN(bus->state))
2509 r = bus_find_parent_object_manager(bus, &object_manager, path);
2516 bus->nodes_modified = false;
2517 m = sd_bus_message_unref(m);
2519 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
2523 r = sd_bus_message_append_basic(m, 'o', path);
2527 r = sd_bus_message_open_container(m, 'a', "s");
2531 r = object_removed_append_all(bus, m, path);
2535 if (bus->nodes_modified)
2538 r = sd_bus_message_close_container(m);
2542 } while (bus->nodes_modified);
2544 return sd_bus_send(bus, m, NULL);
2547 static int interfaces_added_append_one_prefix(
2552 const char *interface,
2553 bool require_fallback) {
2555 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2556 bool found_interface = false;
2557 struct node_vtable *c;
2568 n = hashmap_get(bus->nodes, prefix);
2572 LIST_FOREACH(vtables, c, n->vtables) {
2573 if (require_fallback && !c->is_fallback)
2576 if (!streq(c->interface, interface))
2579 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2582 if (bus->nodes_modified)
2587 if (!found_interface) {
2588 r = sd_bus_message_append_basic(m, 's', interface);
2592 r = sd_bus_message_open_container(m, 'a', "{sv}");
2596 found_interface = true;
2599 r = vtable_append_all_properties(bus, m, path, c, u, &error);
2602 if (bus->nodes_modified)
2606 if (found_interface) {
2607 r = sd_bus_message_close_container(m);
2612 return found_interface;
2615 static int interfaces_added_append_one(
2619 const char *interface) {
2629 r = interfaces_added_append_one_prefix(bus, m, path, path, interface, false);
2632 if (bus->nodes_modified)
2635 prefix = alloca(strlen(path) + 1);
2636 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2637 r = interfaces_added_append_one_prefix(bus, m, prefix, path, interface, true);
2640 if (bus->nodes_modified)
2647 _public_ int sd_bus_emit_interfaces_added_strv(sd_bus *bus, const char *path, char **interfaces) {
2648 BUS_DONT_DESTROY(bus);
2650 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2651 struct node *object_manager;
2655 assert_return(bus, -EINVAL);
2656 assert_return(bus = bus_resolve(bus), -ENOPKG);
2657 assert_return(object_path_is_valid(path), -EINVAL);
2658 assert_return(!bus_pid_changed(bus), -ECHILD);
2660 if (!BUS_IS_OPEN(bus->state))
2663 if (strv_isempty(interfaces))
2666 r = bus_find_parent_object_manager(bus, &object_manager, path);
2673 bus->nodes_modified = false;
2674 m = sd_bus_message_unref(m);
2676 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
2680 r = sd_bus_message_append_basic(m, 'o', path);
2684 r = sd_bus_message_open_container(m, 'a', "{sa{sv}}");
2688 STRV_FOREACH(i, interfaces) {
2689 assert_return(interface_name_is_valid(*i), -EINVAL);
2691 r = sd_bus_message_open_container(m, 'e', "sa{sv}");
2695 r = interfaces_added_append_one(bus, m, path, *i);
2699 if (bus->nodes_modified)
2702 r = sd_bus_message_close_container(m);
2707 if (bus->nodes_modified)
2710 r = sd_bus_message_close_container(m);
2714 } while (bus->nodes_modified);
2716 return sd_bus_send(bus, m, NULL);
2719 _public_ int sd_bus_emit_interfaces_added(sd_bus *bus, const char *path, const char *interface, ...) {
2722 assert_return(bus, -EINVAL);
2723 assert_return(bus = bus_resolve(bus), -ENOPKG);
2724 assert_return(object_path_is_valid(path), -EINVAL);
2725 assert_return(!bus_pid_changed(bus), -ECHILD);
2727 if (!BUS_IS_OPEN(bus->state))
2730 interfaces = strv_from_stdarg_alloca(interface);
2732 return sd_bus_emit_interfaces_added_strv(bus, path, interfaces);
2735 _public_ int sd_bus_emit_interfaces_removed_strv(sd_bus *bus, const char *path, char **interfaces) {
2736 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2737 struct node *object_manager;
2740 assert_return(bus, -EINVAL);
2741 assert_return(bus = bus_resolve(bus), -ENOPKG);
2742 assert_return(object_path_is_valid(path), -EINVAL);
2743 assert_return(!bus_pid_changed(bus), -ECHILD);
2745 if (!BUS_IS_OPEN(bus->state))
2748 if (strv_isempty(interfaces))
2751 r = bus_find_parent_object_manager(bus, &object_manager, path);
2757 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
2761 r = sd_bus_message_append_basic(m, 'o', path);
2765 r = sd_bus_message_append_strv(m, interfaces);
2769 return sd_bus_send(bus, m, NULL);
2772 _public_ int sd_bus_emit_interfaces_removed(sd_bus *bus, const char *path, const char *interface, ...) {
2775 assert_return(bus, -EINVAL);
2776 assert_return(bus = bus_resolve(bus), -ENOPKG);
2777 assert_return(object_path_is_valid(path), -EINVAL);
2778 assert_return(!bus_pid_changed(bus), -ECHILD);
2780 if (!BUS_IS_OPEN(bus->state))
2783 interfaces = strv_from_stdarg_alloca(interface);
2785 return sd_bus_emit_interfaces_removed_strv(bus, path, interfaces);
2788 _public_ int sd_bus_add_object_manager(sd_bus *bus, sd_bus_slot **slot, const char *path) {
2793 assert_return(bus, -EINVAL);
2794 assert_return(bus = bus_resolve(bus), -ENOPKG);
2795 assert_return(object_path_is_valid(path), -EINVAL);
2796 assert_return(!bus_pid_changed(bus), -ECHILD);
2798 n = bus_node_allocate(bus, path);
2802 s = bus_slot_allocate(bus, !slot, BUS_NODE_OBJECT_MANAGER, sizeof(struct node_object_manager), NULL);
2808 s->node_object_manager.node = n;
2809 LIST_PREPEND(object_managers, n->object_managers, &s->node_object_manager);
2810 bus->nodes_modified = true;
2818 sd_bus_slot_unref(s);
2819 bus_node_gc(bus, n);