1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
8 #include "alloc-util.h"
9 #include "bus-internal.h"
10 #include "bus-introspect.h"
11 #include "bus-message.h"
12 #include "bus-objects.h"
13 #include "bus-signature.h"
18 #include "string-util.h"
21 static int node_vtable_get_userdata(
24 struct node_vtable *c,
26 sd_bus_error *error) {
36 s = container_of(c, sd_bus_slot, node_vtable);
39 bus->current_slot = sd_bus_slot_ref(s);
40 bus->current_userdata = u;
41 r = c->find(bus, path, c->interface, u, &found_u, error);
42 bus->current_userdata = NULL;
43 bus->current_slot = sd_bus_slot_unref(s);
47 if (sd_bus_error_is_set(error))
48 return -sd_bus_error_get_errno(error);
60 static void *vtable_method_convert_userdata(const sd_bus_vtable *p, void *u) {
63 return (uint8_t*) u + p->x.method.offset;
66 static void *vtable_property_convert_userdata(const sd_bus_vtable *p, void *u) {
69 return (uint8_t*) u + p->x.property.offset;
72 static int vtable_property_get_userdata(
75 struct vtable_member *p,
77 sd_bus_error *error) {
87 r = node_vtable_get_userdata(bus, path, p->parent, &u, error);
90 if (bus->nodes_modified)
93 *userdata = vtable_property_convert_userdata(p->vtable, u);
97 static int add_enumerated_to_set(
100 struct node_enumerator *first,
102 sd_bus_error *error) {
104 struct node_enumerator *c;
111 LIST_FOREACH(enumerators, c, first) {
112 char **children = NULL, **k;
115 if (bus->nodes_modified)
118 slot = container_of(c, sd_bus_slot, node_enumerator);
120 bus->current_slot = sd_bus_slot_ref(slot);
121 bus->current_userdata = slot->userdata;
122 r = c->callback(bus, prefix, slot->userdata, &children, error);
123 bus->current_userdata = NULL;
124 bus->current_slot = sd_bus_slot_unref(slot);
128 if (sd_bus_error_is_set(error))
129 return -sd_bus_error_get_errno(error);
131 STRV_FOREACH(k, children) {
137 if (!object_path_is_valid(*k)) {
143 if (!object_path_startswith(*k, prefix)) {
148 r = set_consume(s, *k);
162 /* if set, add_subtree() works recursively */
163 CHILDREN_RECURSIVE = (1U << 1),
164 /* if set, add_subtree() scans object-manager hierarchies recursively */
165 CHILDREN_SUBHIERARCHIES = (1U << 0),
168 static int add_subtree_to_set(
174 sd_bus_error *error) {
184 r = add_enumerated_to_set(bus, prefix, n->enumerators, s, error);
187 if (bus->nodes_modified)
190 LIST_FOREACH(siblings, i, n->child) {
193 if (!object_path_startswith(i->path, prefix))
200 r = set_consume(s, t);
201 if (r < 0 && r != -EEXIST)
204 if ((flags & CHILDREN_RECURSIVE) &&
205 ((flags & CHILDREN_SUBHIERARCHIES) || !i->object_managers)) {
206 r = add_subtree_to_set(bus, prefix, i, flags, s, error);
209 if (bus->nodes_modified)
217 static int get_child_nodes(
223 sd_bus_error *error) {
233 s = set_new(&string_hash_ops);
237 r = add_subtree_to_set(bus, prefix, n, flags, s, error);
247 static int node_callbacks_run(
250 struct node_callback *first,
251 bool require_fallback,
252 bool *found_object) {
254 struct node_callback *c;
259 assert(found_object);
261 LIST_FOREACH(callbacks, c, first) {
262 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
265 if (bus->nodes_modified)
268 if (require_fallback && !c->is_fallback)
271 *found_object = true;
273 if (c->last_iteration == bus->iteration_counter)
276 c->last_iteration = bus->iteration_counter;
278 r = sd_bus_message_rewind(m, true);
282 slot = container_of(c, sd_bus_slot, node_callback);
284 bus->current_slot = sd_bus_slot_ref(slot);
285 bus->current_handler = c->callback;
286 bus->current_userdata = slot->userdata;
287 r = c->callback(m, slot->userdata, &error_buffer);
288 bus->current_userdata = NULL;
289 bus->current_handler = NULL;
290 bus->current_slot = sd_bus_slot_unref(slot);
292 r = bus_maybe_reply_error(m, r, &error_buffer);
300 #define CAPABILITY_SHIFT(x) (((x) >> __builtin_ctzll(_SD_BUS_VTABLE_CAPABILITY_MASK)) & 0xFFFF)
302 static int check_access(sd_bus *bus, sd_bus_message *m, struct vtable_member *c, sd_bus_error *error) {
310 /* If the entire bus is trusted let's grant access */
314 /* If the member is marked UNPRIVILEGED let's grant access */
315 if (c->vtable->flags & SD_BUS_VTABLE_UNPRIVILEGED)
318 /* Check have the caller has the requested capability
319 * set. Note that the flags value contains the capability
320 * number plus one, which we need to subtract here. We do this
321 * so that we have 0 as special value for "default
323 cap = CAPABILITY_SHIFT(c->vtable->flags);
325 cap = CAPABILITY_SHIFT(c->parent->vtable[0].flags);
331 r = sd_bus_query_sender_privilege(m, cap);
337 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Access to %s.%s() not permitted.", c->interface, c->member);
340 static int method_callbacks_run(
343 struct vtable_member *c,
344 bool require_fallback,
345 bool *found_object) {
347 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
348 const char *signature;
355 assert(found_object);
357 if (require_fallback && !c->parent->is_fallback)
360 r = check_access(bus, m, c, &error);
362 return bus_maybe_reply_error(m, r, &error);
364 r = node_vtable_get_userdata(bus, m->path, c->parent, &u, &error);
366 return bus_maybe_reply_error(m, r, &error);
367 if (bus->nodes_modified)
370 u = vtable_method_convert_userdata(c->vtable, u);
372 *found_object = true;
374 if (c->last_iteration == bus->iteration_counter)
377 c->last_iteration = bus->iteration_counter;
379 r = sd_bus_message_rewind(m, true);
383 signature = sd_bus_message_get_signature(m, true);
387 if (!streq(strempty(c->vtable->x.method.signature), signature))
388 return sd_bus_reply_method_errorf(
390 SD_BUS_ERROR_INVALID_ARGS,
391 "Invalid arguments '%s' to call %s.%s(), expecting '%s'.",
392 signature, c->interface, c->member, strempty(c->vtable->x.method.signature));
394 /* Keep track what the signature of the reply to this message
395 * should be, so that this can be enforced when sealing the
397 m->enforced_reply_signature = strempty(c->vtable->x.method.result);
399 if (c->vtable->x.method.handler) {
402 slot = container_of(c->parent, sd_bus_slot, node_vtable);
404 bus->current_slot = sd_bus_slot_ref(slot);
405 bus->current_handler = c->vtable->x.method.handler;
406 bus->current_userdata = u;
407 r = c->vtable->x.method.handler(m, u, &error);
408 bus->current_userdata = NULL;
409 bus->current_handler = NULL;
410 bus->current_slot = sd_bus_slot_unref(slot);
412 return bus_maybe_reply_error(m, r, &error);
415 /* If the method callback is NULL, make this a successful NOP */
416 r = sd_bus_reply_method_return(m, NULL);
423 static int invoke_property_get(
426 const sd_bus_vtable *v,
428 const char *interface,
429 const char *property,
430 sd_bus_message *reply,
432 sd_bus_error *error) {
445 if (v->x.property.get) {
447 bus->current_slot = sd_bus_slot_ref(slot);
448 bus->current_userdata = userdata;
449 r = v->x.property.get(bus, path, interface, property, reply, userdata, error);
450 bus->current_userdata = NULL;
451 bus->current_slot = sd_bus_slot_unref(slot);
455 if (sd_bus_error_is_set(error))
456 return -sd_bus_error_get_errno(error);
460 /* Automatic handling if no callback is defined. */
462 if (streq(v->x.property.signature, "as"))
463 return sd_bus_message_append_strv(reply, *(char***) userdata);
465 assert(signature_is_single(v->x.property.signature, false));
466 assert(bus_type_is_basic(v->x.property.signature[0]));
468 switch (v->x.property.signature[0]) {
470 case SD_BUS_TYPE_STRING:
471 case SD_BUS_TYPE_SIGNATURE:
472 p = strempty(*(char**) userdata);
475 case SD_BUS_TYPE_OBJECT_PATH:
476 p = *(char**) userdata;
485 return sd_bus_message_append_basic(reply, v->x.property.signature[0], p);
488 static int invoke_property_set(
491 const sd_bus_vtable *v,
493 const char *interface,
494 const char *property,
495 sd_bus_message *value,
497 sd_bus_error *error) {
509 if (v->x.property.set) {
511 bus->current_slot = sd_bus_slot_ref(slot);
512 bus->current_userdata = userdata;
513 r = v->x.property.set(bus, path, interface, property, value, userdata, error);
514 bus->current_userdata = NULL;
515 bus->current_slot = sd_bus_slot_unref(slot);
519 if (sd_bus_error_is_set(error))
520 return -sd_bus_error_get_errno(error);
524 /* Automatic handling if no callback is defined. */
526 assert(signature_is_single(v->x.property.signature, false));
527 assert(bus_type_is_basic(v->x.property.signature[0]));
529 switch (v->x.property.signature[0]) {
531 case SD_BUS_TYPE_STRING:
532 case SD_BUS_TYPE_OBJECT_PATH:
533 case SD_BUS_TYPE_SIGNATURE: {
537 r = sd_bus_message_read_basic(value, v->x.property.signature[0], &p);
545 free(*(char**) userdata);
546 *(char**) userdata = n;
552 r = sd_bus_message_read_basic(value, v->x.property.signature[0], userdata);
562 static int property_get_set_callbacks_run(
565 struct vtable_member *c,
566 bool require_fallback,
568 bool *found_object) {
570 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
571 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
579 assert(found_object);
581 if (require_fallback && !c->parent->is_fallback)
584 r = vtable_property_get_userdata(bus, m->path, c, &u, &error);
586 return bus_maybe_reply_error(m, r, &error);
587 if (bus->nodes_modified)
590 slot = container_of(c->parent, sd_bus_slot, node_vtable);
592 *found_object = true;
594 r = sd_bus_message_new_method_return(m, &reply);
599 /* Note that we do not protect against reexecution
600 * here (using the last_iteration check, see below),
601 * should the node tree have changed and we got called
602 * again. We assume that property Get() calls are
603 * ultimately without side-effects or if they aren't
604 * then at least idempotent. */
606 r = sd_bus_message_open_container(reply, 'v', c->vtable->x.property.signature);
610 /* Note that we do not do an access check here. Read
611 * access to properties is always unrestricted, since
612 * PropertiesChanged signals broadcast contents
615 r = invoke_property_get(bus, slot, c->vtable, m->path, c->interface, c->member, reply, u, &error);
617 return bus_maybe_reply_error(m, r, &error);
619 if (bus->nodes_modified)
622 r = sd_bus_message_close_container(reply);
627 const char *signature = NULL;
630 if (c->vtable->type != _SD_BUS_VTABLE_WRITABLE_PROPERTY)
631 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Property '%s' is not writable.", c->member);
633 /* Avoid that we call the set routine more than once
634 * if the processing of this message got restarted
635 * because the node tree changed. */
636 if (c->last_iteration == bus->iteration_counter)
639 c->last_iteration = bus->iteration_counter;
641 r = sd_bus_message_peek_type(m, &type, &signature);
645 if (type != 'v' || !streq(strempty(signature), strempty(c->vtable->x.property.signature)))
646 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));
648 r = sd_bus_message_enter_container(m, 'v', c->vtable->x.property.signature);
652 r = check_access(bus, m, c, &error);
654 return bus_maybe_reply_error(m, r, &error);
656 r = invoke_property_set(bus, slot, c->vtable, m->path, c->interface, c->member, m, u, &error);
658 return bus_maybe_reply_error(m, r, &error);
660 if (bus->nodes_modified)
663 r = sd_bus_message_exit_container(m);
668 r = sd_bus_send(bus, reply, NULL);
675 static int vtable_append_one_property(
677 sd_bus_message *reply,
679 struct node_vtable *c,
680 const sd_bus_vtable *v,
682 sd_bus_error *error) {
693 r = sd_bus_message_open_container(reply, 'e', "sv");
697 r = sd_bus_message_append(reply, "s", v->x.property.member);
701 r = sd_bus_message_open_container(reply, 'v', v->x.property.signature);
705 slot = container_of(c, sd_bus_slot, node_vtable);
707 r = invoke_property_get(bus, slot, v, path, c->interface, v->x.property.member, reply, vtable_property_convert_userdata(v, userdata), error);
710 if (bus->nodes_modified)
713 r = sd_bus_message_close_container(reply);
717 r = sd_bus_message_close_container(reply);
724 static int vtable_append_all_properties(
726 sd_bus_message *reply,
728 struct node_vtable *c,
730 sd_bus_error *error) {
732 const sd_bus_vtable *v;
740 if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN)
743 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
744 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
747 if (v->flags & SD_BUS_VTABLE_HIDDEN)
750 if (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)
753 r = vtable_append_one_property(bus, reply, path, c, v, userdata, error);
756 if (bus->nodes_modified)
763 static int property_get_all_callbacks_run(
766 struct node_vtable *first,
767 bool require_fallback,
769 bool *found_object) {
771 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
772 struct node_vtable *c;
773 bool found_interface;
778 assert(found_object);
780 r = sd_bus_message_new_method_return(m, &reply);
784 r = sd_bus_message_open_container(reply, 'a', "{sv}");
788 found_interface = !iface ||
789 streq(iface, "org.freedesktop.DBus.Properties") ||
790 streq(iface, "org.freedesktop.DBus.Peer") ||
791 streq(iface, "org.freedesktop.DBus.Introspectable");
793 LIST_FOREACH(vtables, c, first) {
794 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
797 if (require_fallback && !c->is_fallback)
800 r = node_vtable_get_userdata(bus, m->path, c, &u, &error);
802 return bus_maybe_reply_error(m, r, &error);
803 if (bus->nodes_modified)
808 *found_object = true;
810 if (iface && !streq(c->interface, iface))
812 found_interface = true;
814 r = vtable_append_all_properties(bus, reply, m->path, c, u, &error);
816 return bus_maybe_reply_error(m, r, &error);
817 if (bus->nodes_modified)
824 if (!found_interface) {
825 r = sd_bus_reply_method_errorf(
827 SD_BUS_ERROR_UNKNOWN_INTERFACE,
828 "Unknown interface '%s'.", iface);
835 r = sd_bus_message_close_container(reply);
839 r = sd_bus_send(bus, reply, NULL);
846 static int bus_node_exists(
850 bool require_fallback) {
852 struct node_vtable *c;
853 struct node_callback *k;
860 /* Tests if there's anything attached directly to this node
861 * for the specified path */
863 if (!require_fallback && (n->enumerators || n->object_managers))
866 LIST_FOREACH(callbacks, k, n->callbacks) {
867 if (require_fallback && !k->is_fallback)
873 LIST_FOREACH(vtables, c, n->vtables) {
874 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
876 if (require_fallback && !c->is_fallback)
879 r = node_vtable_get_userdata(bus, path, c, NULL, &error);
882 if (bus->nodes_modified)
889 static int process_introspect(
893 bool require_fallback,
894 bool *found_object) {
896 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
897 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
898 _cleanup_set_free_free_ Set *s = NULL;
899 const char *previous_interface = NULL;
900 struct introspect intro;
901 struct node_vtable *c;
908 assert(found_object);
910 r = get_child_nodes(bus, m->path, n, 0, &s, &error);
912 return bus_maybe_reply_error(m, r, &error);
913 if (bus->nodes_modified)
916 r = introspect_begin(&intro, bus->trusted);
920 r = introspect_write_default_interfaces(&intro, !require_fallback && n->object_managers);
924 empty = set_isempty(s);
926 LIST_FOREACH(vtables, c, n->vtables) {
927 if (require_fallback && !c->is_fallback)
930 r = node_vtable_get_userdata(bus, m->path, c, NULL, &error);
932 r = bus_maybe_reply_error(m, r, &error);
935 if (bus->nodes_modified) {
944 if (c->vtable[0].flags & SD_BUS_VTABLE_HIDDEN)
947 if (!streq_ptr(previous_interface, c->interface)) {
949 if (previous_interface)
950 fputs(" </interface>\n", intro.f);
952 fprintf(intro.f, " <interface name=\"%s\">\n", c->interface);
955 r = introspect_write_interface(&intro, c->vtable);
959 previous_interface = c->interface;
962 if (previous_interface)
963 fputs(" </interface>\n", intro.f);
966 /* Nothing?, let's see if we exist at all, and if not
967 * refuse to do anything */
968 r = bus_node_exists(bus, n, m->path, require_fallback);
970 r = bus_maybe_reply_error(m, r, &error);
973 if (bus->nodes_modified) {
979 *found_object = true;
981 r = introspect_write_child_nodes(&intro, s, m->path);
985 r = introspect_finish(&intro, bus, m, &reply);
989 r = sd_bus_send(bus, reply, NULL);
996 introspect_free(&intro);
1000 static int object_manager_serialize_path(
1002 sd_bus_message *reply,
1005 bool require_fallback,
1006 sd_bus_error *error) {
1008 const char *previous_interface = NULL;
1009 bool found_something = false;
1010 struct node_vtable *i;
1020 n = hashmap_get(bus->nodes, prefix);
1024 LIST_FOREACH(vtables, i, n->vtables) {
1027 if (require_fallback && !i->is_fallback)
1030 r = node_vtable_get_userdata(bus, path, i, &u, error);
1033 if (bus->nodes_modified)
1038 if (!found_something) {
1040 /* Open the object part */
1042 r = sd_bus_message_open_container(reply, 'e', "oa{sa{sv}}");
1046 r = sd_bus_message_append(reply, "o", path);
1050 r = sd_bus_message_open_container(reply, 'a', "{sa{sv}}");
1054 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
1058 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
1062 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
1066 r = sd_bus_message_append(reply, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
1070 found_something = true;
1073 if (!streq_ptr(previous_interface, i->interface)) {
1075 /* Maybe close the previous interface part */
1077 if (previous_interface) {
1078 r = sd_bus_message_close_container(reply);
1082 r = sd_bus_message_close_container(reply);
1087 /* Open the new interface part */
1089 r = sd_bus_message_open_container(reply, 'e', "sa{sv}");
1093 r = sd_bus_message_append(reply, "s", i->interface);
1097 r = sd_bus_message_open_container(reply, 'a', "{sv}");
1102 r = vtable_append_all_properties(bus, reply, path, i, u, error);
1105 if (bus->nodes_modified)
1108 previous_interface = i->interface;
1111 if (previous_interface) {
1112 r = sd_bus_message_close_container(reply);
1116 r = sd_bus_message_close_container(reply);
1121 if (found_something) {
1122 r = sd_bus_message_close_container(reply);
1126 r = sd_bus_message_close_container(reply);
1134 static int object_manager_serialize_path_and_fallbacks(
1136 sd_bus_message *reply,
1138 sd_bus_error *error) {
1148 /* First, add all vtables registered for this path */
1149 r = object_manager_serialize_path(bus, reply, path, path, false, error);
1152 if (bus->nodes_modified)
1155 /* Second, add fallback vtables registered for any of the prefixes */
1156 prefix = alloca(strlen(path) + 1);
1157 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
1158 r = object_manager_serialize_path(bus, reply, prefix, path, true, error);
1161 if (bus->nodes_modified)
1168 static int process_get_managed_objects(
1172 bool require_fallback,
1173 bool *found_object) {
1175 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1176 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1177 _cleanup_set_free_free_ Set *s = NULL;
1185 assert(found_object);
1187 /* Spec says, GetManagedObjects() is only implemented on the root of a
1188 * sub-tree. Therefore, we require a registered object-manager on
1189 * exactly the queried path, otherwise, we refuse to respond. */
1191 if (require_fallback || !n->object_managers)
1194 r = get_child_nodes(bus, m->path, n, CHILDREN_RECURSIVE, &s, &error);
1196 return bus_maybe_reply_error(m, r, &error);
1197 if (bus->nodes_modified)
1200 r = sd_bus_message_new_method_return(m, &reply);
1204 r = sd_bus_message_open_container(reply, 'a', "{oa{sa{sv}}}");
1208 SET_FOREACH(path, s, i) {
1209 r = object_manager_serialize_path_and_fallbacks(bus, reply, path, &error);
1211 return bus_maybe_reply_error(m, r, &error);
1213 if (bus->nodes_modified)
1217 r = sd_bus_message_close_container(reply);
1221 r = sd_bus_send(bus, reply, NULL);
1228 static int object_find_and_run(
1232 bool require_fallback,
1233 bool *found_object) {
1236 struct vtable_member vtable_key, *v;
1242 assert(found_object);
1244 n = hashmap_get(bus->nodes, p);
1248 /* First, try object callbacks */
1249 r = node_callbacks_run(bus, m, n->callbacks, require_fallback, found_object);
1252 if (bus->nodes_modified)
1255 if (!m->interface || !m->member)
1258 /* Then, look for a known method */
1259 vtable_key.path = (char*) p;
1260 vtable_key.interface = m->interface;
1261 vtable_key.member = m->member;
1263 v = hashmap_get(bus->vtable_methods, &vtable_key);
1265 r = method_callbacks_run(bus, m, v, require_fallback, found_object);
1268 if (bus->nodes_modified)
1272 /* Then, look for a known property */
1273 if (streq(m->interface, "org.freedesktop.DBus.Properties")) {
1276 get = streq(m->member, "Get");
1278 if (get || streq(m->member, "Set")) {
1280 r = sd_bus_message_rewind(m, true);
1284 vtable_key.path = (char*) p;
1286 r = sd_bus_message_read(m, "ss", &vtable_key.interface, &vtable_key.member);
1288 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected interface and member parameters");
1290 v = hashmap_get(bus->vtable_properties, &vtable_key);
1292 r = property_get_set_callbacks_run(bus, m, v, require_fallback, get, found_object);
1297 } else if (streq(m->member, "GetAll")) {
1300 r = sd_bus_message_rewind(m, true);
1304 r = sd_bus_message_read(m, "s", &iface);
1306 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected interface parameter");
1311 r = property_get_all_callbacks_run(bus, m, n->vtables, require_fallback, iface, found_object);
1316 } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1318 if (!isempty(sd_bus_message_get_signature(m, true)))
1319 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected no parameters");
1321 r = process_introspect(bus, m, n, require_fallback, found_object);
1325 } else if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.ObjectManager", "GetManagedObjects")) {
1327 if (!isempty(sd_bus_message_get_signature(m, true)))
1328 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INVALID_ARGS, "Expected no parameters");
1330 r = process_get_managed_objects(bus, m, n, require_fallback, found_object);
1335 if (bus->nodes_modified)
1338 if (!*found_object) {
1339 r = bus_node_exists(bus, n, m->path, require_fallback);
1341 return bus_maybe_reply_error(m, r, NULL);
1342 if (bus->nodes_modified)
1345 *found_object = true;
1351 int bus_process_object(sd_bus *bus, sd_bus_message *m) {
1354 bool found_object = false;
1359 if (bus->is_monitor)
1362 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
1365 if (hashmap_isempty(bus->nodes))
1368 /* Never respond to broadcast messages */
1369 if (bus->bus_client && !m->destination)
1375 pl = strlen(m->path);
1379 bus->nodes_modified = false;
1381 r = object_find_and_run(bus, m, m->path, false, &found_object);
1385 /* Look for fallback prefixes */
1386 OBJECT_PATH_FOREACH_PREFIX(prefix, m->path) {
1388 if (bus->nodes_modified)
1391 r = object_find_and_run(bus, m, prefix, true, &found_object);
1396 } while (bus->nodes_modified);
1401 if (sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Get") ||
1402 sd_bus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Set"))
1403 r = sd_bus_reply_method_errorf(
1405 SD_BUS_ERROR_UNKNOWN_PROPERTY,
1406 "Unknown property or interface.");
1408 r = sd_bus_reply_method_errorf(
1410 SD_BUS_ERROR_UNKNOWN_METHOD,
1411 "Unknown method '%s' or interface '%s'.", m->member, m->interface);
1419 static struct node *bus_node_allocate(sd_bus *bus, const char *path) {
1420 struct node *n, *parent;
1422 _cleanup_free_ char *s = NULL;
1428 assert(path[0] == '/');
1430 n = hashmap_get(bus->nodes, path);
1434 r = hashmap_ensure_allocated(&bus->nodes, &string_hash_ops);
1442 if (streq(path, "/"))
1445 e = strrchr(path, '/');
1448 p = strndupa(path, MAX(1, e - path));
1450 parent = bus_node_allocate(bus, p);
1455 n = new0(struct node, 1);
1460 n->path = TAKE_PTR(s);
1462 r = hashmap_put(bus->nodes, n->path, n);
1469 LIST_PREPEND(siblings, parent->child, n);
1474 void bus_node_gc(sd_bus *b, struct node *n) {
1487 assert_se(hashmap_remove(b->nodes, n->path) == n);
1490 LIST_REMOVE(siblings, n->parent->child, n);
1493 bus_node_gc(b, n->parent);
1497 static int bus_find_parent_object_manager(sd_bus *bus, struct node **out, const char *path) {
1503 n = hashmap_get(bus->nodes, path);
1507 prefix = alloca(strlen(path) + 1);
1508 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
1509 n = hashmap_get(bus->nodes, prefix);
1515 while (n && !n->object_managers)
1523 static int bus_add_object(
1528 sd_bus_message_handler_t callback,
1535 assert_return(bus, -EINVAL);
1536 assert_return(bus = bus_resolve(bus), -ENOPKG);
1537 assert_return(object_path_is_valid(path), -EINVAL);
1538 assert_return(callback, -EINVAL);
1539 assert_return(!bus_pid_changed(bus), -ECHILD);
1541 n = bus_node_allocate(bus, path);
1545 s = bus_slot_allocate(bus, !slot, BUS_NODE_CALLBACK, sizeof(struct node_callback), userdata);
1551 s->node_callback.callback = callback;
1552 s->node_callback.is_fallback = fallback;
1554 s->node_callback.node = n;
1555 LIST_PREPEND(callbacks, n->callbacks, &s->node_callback);
1556 bus->nodes_modified = true;
1564 sd_bus_slot_unref(s);
1565 bus_node_gc(bus, n);
1570 _public_ int sd_bus_add_object(
1574 sd_bus_message_handler_t callback,
1577 return bus_add_object(bus, slot, false, path, callback, userdata);
1580 _public_ int sd_bus_add_fallback(
1584 sd_bus_message_handler_t callback,
1587 return bus_add_object(bus, slot, true, prefix, callback, userdata);
1590 static void vtable_member_hash_func(const void *a, struct siphash *state) {
1591 const struct vtable_member *m = a;
1595 string_hash_func(m->path, state);
1596 string_hash_func(m->interface, state);
1597 string_hash_func(m->member, state);
1600 static int vtable_member_compare_func(const void *a, const void *b) {
1601 const struct vtable_member *x = a, *y = b;
1607 r = strcmp(x->path, y->path);
1611 r = strcmp(x->interface, y->interface);
1615 return strcmp(x->member, y->member);
1618 static const struct hash_ops vtable_member_hash_ops = {
1619 .hash = vtable_member_hash_func,
1620 .compare = vtable_member_compare_func
1623 static int add_object_vtable_internal(
1627 const char *interface,
1628 const sd_bus_vtable *vtable,
1630 sd_bus_object_find_t find,
1633 sd_bus_slot *s = NULL;
1634 struct node_vtable *i, *existing = NULL;
1635 const sd_bus_vtable *v;
1639 assert_return(bus, -EINVAL);
1640 assert_return(bus = bus_resolve(bus), -ENOPKG);
1641 assert_return(object_path_is_valid(path), -EINVAL);
1642 assert_return(interface_name_is_valid(interface), -EINVAL);
1643 assert_return(vtable, -EINVAL);
1644 assert_return(vtable[0].type == _SD_BUS_VTABLE_START, -EINVAL);
1645 assert_return(vtable[0].x.start.element_size == sizeof(struct sd_bus_vtable), -EINVAL);
1646 assert_return(!bus_pid_changed(bus), -ECHILD);
1647 assert_return(!streq(interface, "org.freedesktop.DBus.Properties") &&
1648 !streq(interface, "org.freedesktop.DBus.Introspectable") &&
1649 !streq(interface, "org.freedesktop.DBus.Peer") &&
1650 !streq(interface, "org.freedesktop.DBus.ObjectManager"), -EINVAL);
1652 r = hashmap_ensure_allocated(&bus->vtable_methods, &vtable_member_hash_ops);
1656 r = hashmap_ensure_allocated(&bus->vtable_properties, &vtable_member_hash_ops);
1660 n = bus_node_allocate(bus, path);
1664 LIST_FOREACH(vtables, i, n->vtables) {
1665 if (i->is_fallback != fallback) {
1670 if (streq(i->interface, interface)) {
1672 if (i->vtable == vtable) {
1681 s = bus_slot_allocate(bus, !slot, BUS_NODE_VTABLE, sizeof(struct node_vtable), userdata);
1687 s->node_vtable.is_fallback = fallback;
1688 s->node_vtable.vtable = vtable;
1689 s->node_vtable.find = find;
1691 s->node_vtable.interface = strdup(interface);
1692 if (!s->node_vtable.interface) {
1697 for (v = s->node_vtable.vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
1701 case _SD_BUS_VTABLE_METHOD: {
1702 struct vtable_member *m;
1704 if (!member_name_is_valid(v->x.method.member) ||
1705 !signature_is_valid(strempty(v->x.method.signature), false) ||
1706 !signature_is_valid(strempty(v->x.method.result), false) ||
1707 !(v->x.method.handler || (isempty(v->x.method.signature) && isempty(v->x.method.result))) ||
1708 v->flags & (SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) {
1713 m = new0(struct vtable_member, 1);
1719 m->parent = &s->node_vtable;
1721 m->interface = s->node_vtable.interface;
1722 m->member = v->x.method.member;
1725 r = hashmap_put(bus->vtable_methods, m, m);
1734 case _SD_BUS_VTABLE_WRITABLE_PROPERTY:
1736 if (!(v->x.property.set || bus_type_is_basic(v->x.property.signature[0]))) {
1741 if (v->flags & SD_BUS_VTABLE_PROPERTY_CONST) {
1747 case _SD_BUS_VTABLE_PROPERTY: {
1748 struct vtable_member *m;
1750 if (!member_name_is_valid(v->x.property.member) ||
1751 !signature_is_single(v->x.property.signature, false) ||
1752 !(v->x.property.get || bus_type_is_basic(v->x.property.signature[0]) || streq(v->x.property.signature, "as")) ||
1753 (v->flags & SD_BUS_VTABLE_METHOD_NO_REPLY) ||
1754 (!!(v->flags & SD_BUS_VTABLE_PROPERTY_CONST) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) + !!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)) > 1 ||
1755 ((v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE) && (v->flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)) ||
1756 (v->flags & SD_BUS_VTABLE_UNPRIVILEGED && v->type == _SD_BUS_VTABLE_PROPERTY)) {
1761 m = new0(struct vtable_member, 1);
1767 m->parent = &s->node_vtable;
1769 m->interface = s->node_vtable.interface;
1770 m->member = v->x.property.member;
1773 r = hashmap_put(bus->vtable_properties, m, m);
1782 case _SD_BUS_VTABLE_SIGNAL:
1784 if (!member_name_is_valid(v->x.signal.member) ||
1785 !signature_is_valid(strempty(v->x.signal.signature), false) ||
1786 v->flags & SD_BUS_VTABLE_UNPRIVILEGED) {
1799 s->node_vtable.node = n;
1800 LIST_INSERT_AFTER(vtables, n->vtables, existing, &s->node_vtable);
1801 bus->nodes_modified = true;
1809 sd_bus_slot_unref(s);
1810 bus_node_gc(bus, n);
1815 _public_ int sd_bus_add_object_vtable(
1819 const char *interface,
1820 const sd_bus_vtable *vtable,
1823 return add_object_vtable_internal(bus, slot, path, interface, vtable, false, NULL, userdata);
1826 _public_ int sd_bus_add_fallback_vtable(
1830 const char *interface,
1831 const sd_bus_vtable *vtable,
1832 sd_bus_object_find_t find,
1835 return add_object_vtable_internal(bus, slot, prefix, interface, vtable, true, find, userdata);
1838 _public_ int sd_bus_add_node_enumerator(
1842 sd_bus_node_enumerator_t callback,
1849 assert_return(bus, -EINVAL);
1850 assert_return(bus = bus_resolve(bus), -ENOPKG);
1851 assert_return(object_path_is_valid(path), -EINVAL);
1852 assert_return(callback, -EINVAL);
1853 assert_return(!bus_pid_changed(bus), -ECHILD);
1855 n = bus_node_allocate(bus, path);
1859 s = bus_slot_allocate(bus, !slot, BUS_NODE_ENUMERATOR, sizeof(struct node_enumerator), userdata);
1865 s->node_enumerator.callback = callback;
1867 s->node_enumerator.node = n;
1868 LIST_PREPEND(enumerators, n->enumerators, &s->node_enumerator);
1869 bus->nodes_modified = true;
1877 sd_bus_slot_unref(s);
1878 bus_node_gc(bus, n);
1883 static int emit_properties_changed_on_interface(
1887 const char *interface,
1888 bool require_fallback,
1889 bool *found_interface,
1892 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1893 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1894 bool has_invalidating = false, has_changing = false;
1895 struct vtable_member key = {};
1896 struct node_vtable *c;
1906 assert(found_interface);
1908 n = hashmap_get(bus->nodes, prefix);
1912 r = sd_bus_message_new_signal(bus, &m, path, "org.freedesktop.DBus.Properties", "PropertiesChanged");
1916 r = sd_bus_message_append(m, "s", interface);
1920 r = sd_bus_message_open_container(m, 'a', "{sv}");
1925 key.interface = interface;
1927 LIST_FOREACH(vtables, c, n->vtables) {
1928 if (require_fallback && !c->is_fallback)
1931 if (!streq(c->interface, interface))
1934 r = node_vtable_get_userdata(bus, path, c, &u, &error);
1937 if (bus->nodes_modified)
1942 *found_interface = true;
1945 /* If the caller specified a list of
1946 * properties we include exactly those in the
1947 * PropertiesChanged message */
1949 STRV_FOREACH(property, names) {
1950 struct vtable_member *v;
1952 assert_return(member_name_is_valid(*property), -EINVAL);
1954 key.member = *property;
1955 v = hashmap_get(bus->vtable_properties, &key);
1959 /* If there are two vtables for the same
1960 * interface, let's handle this property when
1961 * we come to that vtable. */
1965 assert_return(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE ||
1966 v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION, -EDOM);
1968 assert_return(!(v->vtable->flags & SD_BUS_VTABLE_HIDDEN), -EDOM);
1970 if (v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) {
1971 has_invalidating = true;
1975 has_changing = true;
1977 r = vtable_append_one_property(bus, m, m->path, c, v->vtable, u, &error);
1980 if (bus->nodes_modified)
1984 const sd_bus_vtable *v;
1986 /* If the caller specified no properties list
1987 * we include all properties that are marked
1988 * as changing in the message. */
1990 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
1991 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
1994 if (v->flags & SD_BUS_VTABLE_HIDDEN)
1997 if (v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION) {
1998 has_invalidating = true;
2002 if (!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))
2005 has_changing = true;
2007 r = vtable_append_one_property(bus, m, m->path, c, v, u, &error);
2010 if (bus->nodes_modified)
2016 if (!has_invalidating && !has_changing)
2019 r = sd_bus_message_close_container(m);
2023 r = sd_bus_message_open_container(m, 'a', "s");
2027 if (has_invalidating) {
2028 LIST_FOREACH(vtables, c, n->vtables) {
2029 if (require_fallback && !c->is_fallback)
2032 if (!streq(c->interface, interface))
2035 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2038 if (bus->nodes_modified)
2044 STRV_FOREACH(property, names) {
2045 struct vtable_member *v;
2047 key.member = *property;
2048 assert_se(v = hashmap_get(bus->vtable_properties, &key));
2049 assert(c == v->parent);
2051 if (!(v->vtable->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION))
2054 r = sd_bus_message_append(m, "s", *property);
2059 const sd_bus_vtable *v;
2061 for (v = c->vtable+1; v->type != _SD_BUS_VTABLE_END; v++) {
2062 if (!IN_SET(v->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY))
2065 if (v->flags & SD_BUS_VTABLE_HIDDEN)
2068 if (!(v->flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION))
2071 r = sd_bus_message_append(m, "s", v->x.property.member);
2079 r = sd_bus_message_close_container(m);
2083 r = sd_bus_send(bus, m, NULL);
2090 _public_ int sd_bus_emit_properties_changed_strv(
2093 const char *interface,
2096 BUS_DONT_DESTROY(bus);
2097 bool found_interface = false;
2101 assert_return(bus, -EINVAL);
2102 assert_return(bus = bus_resolve(bus), -ENOPKG);
2103 assert_return(object_path_is_valid(path), -EINVAL);
2104 assert_return(interface_name_is_valid(interface), -EINVAL);
2105 assert_return(!bus_pid_changed(bus), -ECHILD);
2107 if (!BUS_IS_OPEN(bus->state))
2110 /* A non-NULL but empty names list means nothing needs to be
2111 generated. A NULL list OTOH indicates that all properties
2112 that are set to EMITS_CHANGE or EMITS_INVALIDATION shall be
2113 included in the PropertiesChanged message. */
2114 if (names && names[0] == NULL)
2118 bus->nodes_modified = false;
2120 r = emit_properties_changed_on_interface(bus, path, path, interface, false, &found_interface, names);
2123 if (bus->nodes_modified)
2126 prefix = alloca(strlen(path) + 1);
2127 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2128 r = emit_properties_changed_on_interface(bus, prefix, path, interface, true, &found_interface, names);
2131 if (bus->nodes_modified)
2135 } while (bus->nodes_modified);
2137 return found_interface ? 0 : -ENOENT;
2140 _public_ int sd_bus_emit_properties_changed(
2143 const char *interface,
2144 const char *name, ...) {
2148 assert_return(bus, -EINVAL);
2149 assert_return(bus = bus_resolve(bus), -ENOPKG);
2150 assert_return(object_path_is_valid(path), -EINVAL);
2151 assert_return(interface_name_is_valid(interface), -EINVAL);
2152 assert_return(!bus_pid_changed(bus), -ECHILD);
2154 if (!BUS_IS_OPEN(bus->state))
2160 names = strv_from_stdarg_alloca(name);
2162 return sd_bus_emit_properties_changed_strv(bus, path, interface, names);
2165 static int object_added_append_all_prefix(
2171 bool require_fallback) {
2173 const char *previous_interface = NULL;
2174 struct node_vtable *c;
2184 n = hashmap_get(bus->nodes, prefix);
2188 LIST_FOREACH(vtables, c, n->vtables) {
2189 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2192 if (require_fallback && !c->is_fallback)
2195 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2198 if (bus->nodes_modified)
2203 if (!streq_ptr(c->interface, previous_interface)) {
2204 /* If a child-node already handled this interface, we
2205 * skip it on any of its parents. The child vtables
2206 * always fully override any conflicting vtables of
2207 * any parent node. */
2208 if (set_get(s, c->interface))
2211 r = set_put(s, c->interface);
2215 if (previous_interface) {
2216 r = sd_bus_message_close_container(m);
2219 r = sd_bus_message_close_container(m);
2224 r = sd_bus_message_open_container(m, 'e', "sa{sv}");
2227 r = sd_bus_message_append(m, "s", c->interface);
2230 r = sd_bus_message_open_container(m, 'a', "{sv}");
2234 previous_interface = c->interface;
2237 r = vtable_append_all_properties(bus, m, path, c, u, &error);
2240 if (bus->nodes_modified)
2244 if (previous_interface) {
2245 r = sd_bus_message_close_container(m);
2248 r = sd_bus_message_close_container(m);
2256 static int object_added_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
2257 _cleanup_set_free_ Set *s = NULL;
2266 * This appends all interfaces registered on path @path. We first add
2267 * the builtin interfaces, which are always available and handled by
2268 * sd-bus. Then, we add all interfaces registered on the exact node,
2269 * followed by all fallback interfaces registered on any parent prefix.
2271 * If an interface is registered multiple times on the same node with
2272 * different vtables, we merge all the properties across all vtables.
2273 * However, if a child node has the same interface registered as one of
2274 * its parent nodes has as fallback, we make the child overwrite the
2275 * parent instead of extending it. Therefore, we keep a "Set" of all
2276 * handled interfaces during parent traversal, so we skip interfaces on
2277 * a parent that were overwritten by a child.
2280 s = set_new(&string_hash_ops);
2284 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
2287 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
2290 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
2293 r = sd_bus_message_append(m, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
2297 r = object_added_append_all_prefix(bus, m, s, path, path, false);
2300 if (bus->nodes_modified)
2303 prefix = alloca(strlen(path) + 1);
2304 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2305 r = object_added_append_all_prefix(bus, m, s, prefix, path, true);
2308 if (bus->nodes_modified)
2315 _public_ int sd_bus_emit_object_added(sd_bus *bus, const char *path) {
2316 BUS_DONT_DESTROY(bus);
2318 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2319 struct node *object_manager;
2323 * This emits an InterfacesAdded signal on the given path, by iterating
2324 * all registered vtables and fallback vtables on the path. All
2325 * properties are queried and included in the signal.
2326 * This call is equivalent to sd_bus_emit_interfaces_added() with an
2327 * explicit list of registered interfaces. However, unlike
2328 * interfaces_added(), this call can figure out the list of supported
2329 * interfaces itself. Furthermore, it properly adds the builtin
2330 * org.freedesktop.DBus.* interfaces.
2333 assert_return(bus, -EINVAL);
2334 assert_return(bus = bus_resolve(bus), -ENOPKG);
2335 assert_return(object_path_is_valid(path), -EINVAL);
2336 assert_return(!bus_pid_changed(bus), -ECHILD);
2338 if (!BUS_IS_OPEN(bus->state))
2341 r = bus_find_parent_object_manager(bus, &object_manager, path);
2348 bus->nodes_modified = false;
2349 m = sd_bus_message_unref(m);
2351 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
2355 r = sd_bus_message_append_basic(m, 'o', path);
2359 r = sd_bus_message_open_container(m, 'a', "{sa{sv}}");
2363 r = object_added_append_all(bus, m, path);
2367 if (bus->nodes_modified)
2370 r = sd_bus_message_close_container(m);
2374 } while (bus->nodes_modified);
2376 return sd_bus_send(bus, m, NULL);
2379 static int object_removed_append_all_prefix(
2385 bool require_fallback) {
2387 const char *previous_interface = NULL;
2388 struct node_vtable *c;
2398 n = hashmap_get(bus->nodes, prefix);
2402 LIST_FOREACH(vtables, c, n->vtables) {
2403 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2406 if (require_fallback && !c->is_fallback)
2408 if (streq_ptr(c->interface, previous_interface))
2411 /* If a child-node already handled this interface, we
2412 * skip it on any of its parents. The child vtables
2413 * always fully override any conflicting vtables of
2414 * any parent node. */
2415 if (set_get(s, c->interface))
2418 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2421 if (bus->nodes_modified)
2426 r = set_put(s, c->interface);
2430 r = sd_bus_message_append(m, "s", c->interface);
2434 previous_interface = c->interface;
2440 static int object_removed_append_all(sd_bus *bus, sd_bus_message *m, const char *path) {
2441 _cleanup_set_free_ Set *s = NULL;
2449 /* see sd_bus_emit_object_added() for details */
2451 s = set_new(&string_hash_ops);
2455 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Peer");
2458 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Introspectable");
2461 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.Properties");
2464 r = sd_bus_message_append(m, "s", "org.freedesktop.DBus.ObjectManager");
2468 r = object_removed_append_all_prefix(bus, m, s, path, path, false);
2471 if (bus->nodes_modified)
2474 prefix = alloca(strlen(path) + 1);
2475 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2476 r = object_removed_append_all_prefix(bus, m, s, prefix, path, true);
2479 if (bus->nodes_modified)
2486 _public_ int sd_bus_emit_object_removed(sd_bus *bus, const char *path) {
2487 BUS_DONT_DESTROY(bus);
2489 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2490 struct node *object_manager;
2494 * This is like sd_bus_emit_object_added(), but emits an
2495 * InterfacesRemoved signal on the given path. This only includes any
2496 * registered interfaces but skips the properties. Note that this will
2497 * call into the find() callbacks of any registered vtable. Therefore,
2498 * you must call this function before destroying/unlinking your object.
2499 * Otherwise, the list of interfaces will be incomplete. However, note
2500 * that this will *NOT* call into any property callback. Therefore, the
2501 * object might be in an "destructed" state, as long as we can find it.
2504 assert_return(bus, -EINVAL);
2505 assert_return(bus = bus_resolve(bus), -ENOPKG);
2506 assert_return(object_path_is_valid(path), -EINVAL);
2507 assert_return(!bus_pid_changed(bus), -ECHILD);
2509 if (!BUS_IS_OPEN(bus->state))
2512 r = bus_find_parent_object_manager(bus, &object_manager, path);
2519 bus->nodes_modified = false;
2520 m = sd_bus_message_unref(m);
2522 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
2526 r = sd_bus_message_append_basic(m, 'o', path);
2530 r = sd_bus_message_open_container(m, 'a', "s");
2534 r = object_removed_append_all(bus, m, path);
2538 if (bus->nodes_modified)
2541 r = sd_bus_message_close_container(m);
2545 } while (bus->nodes_modified);
2547 return sd_bus_send(bus, m, NULL);
2550 static int interfaces_added_append_one_prefix(
2555 const char *interface,
2556 bool require_fallback) {
2558 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2559 bool found_interface = false;
2560 struct node_vtable *c;
2571 n = hashmap_get(bus->nodes, prefix);
2575 LIST_FOREACH(vtables, c, n->vtables) {
2576 if (require_fallback && !c->is_fallback)
2579 if (!streq(c->interface, interface))
2582 r = node_vtable_get_userdata(bus, path, c, &u, &error);
2585 if (bus->nodes_modified)
2590 if (!found_interface) {
2591 r = sd_bus_message_append_basic(m, 's', interface);
2595 r = sd_bus_message_open_container(m, 'a', "{sv}");
2599 found_interface = true;
2602 r = vtable_append_all_properties(bus, m, path, c, u, &error);
2605 if (bus->nodes_modified)
2609 if (found_interface) {
2610 r = sd_bus_message_close_container(m);
2615 return found_interface;
2618 static int interfaces_added_append_one(
2622 const char *interface) {
2632 r = interfaces_added_append_one_prefix(bus, m, path, path, interface, false);
2635 if (bus->nodes_modified)
2638 prefix = alloca(strlen(path) + 1);
2639 OBJECT_PATH_FOREACH_PREFIX(prefix, path) {
2640 r = interfaces_added_append_one_prefix(bus, m, prefix, path, interface, true);
2643 if (bus->nodes_modified)
2650 _public_ int sd_bus_emit_interfaces_added_strv(sd_bus *bus, const char *path, char **interfaces) {
2651 BUS_DONT_DESTROY(bus);
2653 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2654 struct node *object_manager;
2658 assert_return(bus, -EINVAL);
2659 assert_return(bus = bus_resolve(bus), -ENOPKG);
2660 assert_return(object_path_is_valid(path), -EINVAL);
2661 assert_return(!bus_pid_changed(bus), -ECHILD);
2663 if (!BUS_IS_OPEN(bus->state))
2666 if (strv_isempty(interfaces))
2669 r = bus_find_parent_object_manager(bus, &object_manager, path);
2676 bus->nodes_modified = false;
2677 m = sd_bus_message_unref(m);
2679 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
2683 r = sd_bus_message_append_basic(m, 'o', path);
2687 r = sd_bus_message_open_container(m, 'a', "{sa{sv}}");
2691 STRV_FOREACH(i, interfaces) {
2692 assert_return(interface_name_is_valid(*i), -EINVAL);
2694 r = sd_bus_message_open_container(m, 'e', "sa{sv}");
2698 r = interfaces_added_append_one(bus, m, path, *i);
2702 if (bus->nodes_modified)
2705 r = sd_bus_message_close_container(m);
2710 if (bus->nodes_modified)
2713 r = sd_bus_message_close_container(m);
2717 } while (bus->nodes_modified);
2719 return sd_bus_send(bus, m, NULL);
2722 _public_ int sd_bus_emit_interfaces_added(sd_bus *bus, const char *path, const char *interface, ...) {
2725 assert_return(bus, -EINVAL);
2726 assert_return(bus = bus_resolve(bus), -ENOPKG);
2727 assert_return(object_path_is_valid(path), -EINVAL);
2728 assert_return(!bus_pid_changed(bus), -ECHILD);
2730 if (!BUS_IS_OPEN(bus->state))
2733 interfaces = strv_from_stdarg_alloca(interface);
2735 return sd_bus_emit_interfaces_added_strv(bus, path, interfaces);
2738 _public_ int sd_bus_emit_interfaces_removed_strv(sd_bus *bus, const char *path, char **interfaces) {
2739 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2740 struct node *object_manager;
2743 assert_return(bus, -EINVAL);
2744 assert_return(bus = bus_resolve(bus), -ENOPKG);
2745 assert_return(object_path_is_valid(path), -EINVAL);
2746 assert_return(!bus_pid_changed(bus), -ECHILD);
2748 if (!BUS_IS_OPEN(bus->state))
2751 if (strv_isempty(interfaces))
2754 r = bus_find_parent_object_manager(bus, &object_manager, path);
2760 r = sd_bus_message_new_signal(bus, &m, object_manager->path, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
2764 r = sd_bus_message_append_basic(m, 'o', path);
2768 r = sd_bus_message_append_strv(m, interfaces);
2772 return sd_bus_send(bus, m, NULL);
2775 _public_ int sd_bus_emit_interfaces_removed(sd_bus *bus, const char *path, const char *interface, ...) {
2778 assert_return(bus, -EINVAL);
2779 assert_return(bus = bus_resolve(bus), -ENOPKG);
2780 assert_return(object_path_is_valid(path), -EINVAL);
2781 assert_return(!bus_pid_changed(bus), -ECHILD);
2783 if (!BUS_IS_OPEN(bus->state))
2786 interfaces = strv_from_stdarg_alloca(interface);
2788 return sd_bus_emit_interfaces_removed_strv(bus, path, interfaces);
2791 _public_ int sd_bus_add_object_manager(sd_bus *bus, sd_bus_slot **slot, const char *path) {
2796 assert_return(bus, -EINVAL);
2797 assert_return(bus = bus_resolve(bus), -ENOPKG);
2798 assert_return(object_path_is_valid(path), -EINVAL);
2799 assert_return(!bus_pid_changed(bus), -ECHILD);
2801 n = bus_node_allocate(bus, path);
2805 s = bus_slot_allocate(bus, !slot, BUS_NODE_OBJECT_MANAGER, sizeof(struct node_object_manager), NULL);
2811 s->node_object_manager.node = n;
2812 LIST_PREPEND(object_managers, n->object_managers, &s->node_object_manager);
2813 bus->nodes_modified = true;
2821 sd_bus_slot_unref(s);
2822 bus_node_gc(bus, n);