1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
4 #include "bus-internal.h"
5 #include "bus-introspect.h"
6 #include "bus-message.h"
7 #include "bus-objects.h"
8 #include "bus-signature.h"
13 #include "string-util.h"
16 static int node_vtable_get_userdata(
19 struct node_vtable
*c
,
21 sd_bus_error
*error
) {
31 s
= container_of(c
, sd_bus_slot
, node_vtable
);
34 bus
->current_slot
= sd_bus_slot_ref(s
);
35 bus
->current_userdata
= u
;
36 r
= c
->find(bus
, path
, c
->interface
, u
, &found_u
, error
);
37 bus
->current_userdata
= NULL
;
38 bus
->current_slot
= sd_bus_slot_unref(s
);
42 if (sd_bus_error_is_set(error
))
43 return -sd_bus_error_get_errno(error
);
55 static void *vtable_method_convert_userdata(const sd_bus_vtable
*p
, void *u
) {
58 return (uint8_t*) u
+ p
->x
.method
.offset
;
61 static void *vtable_property_convert_userdata(const sd_bus_vtable
*p
, void *u
) {
64 return (uint8_t*) u
+ p
->x
.property
.offset
;
67 static int vtable_property_get_userdata(
70 struct vtable_member
*p
,
72 sd_bus_error
*error
) {
82 r
= node_vtable_get_userdata(bus
, path
, p
->parent
, &u
, error
);
85 if (bus
->nodes_modified
)
88 *userdata
= vtable_property_convert_userdata(p
->vtable
, u
);
92 static int add_enumerated_to_set(
95 struct node_enumerator
*first
,
97 sd_bus_error
*error
) {
99 struct node_enumerator
*c
;
106 LIST_FOREACH(enumerators
, c
, first
) {
107 char **children
= NULL
, **k
;
110 if (bus
->nodes_modified
)
113 slot
= container_of(c
, sd_bus_slot
, node_enumerator
);
115 bus
->current_slot
= sd_bus_slot_ref(slot
);
116 bus
->current_userdata
= slot
->userdata
;
117 r
= c
->callback(bus
, prefix
, slot
->userdata
, &children
, error
);
118 bus
->current_userdata
= NULL
;
119 bus
->current_slot
= sd_bus_slot_unref(slot
);
123 if (sd_bus_error_is_set(error
))
124 return -sd_bus_error_get_errno(error
);
126 STRV_FOREACH(k
, children
) {
132 if (!object_path_is_valid(*k
)) {
138 if (!object_path_startswith(*k
, prefix
)) {
143 r
= set_consume(s
, *k
);
157 /* if set, add_subtree() works recursively */
158 CHILDREN_RECURSIVE
= 1 << 0,
159 /* if set, add_subtree() scans object-manager hierarchies recursively */
160 CHILDREN_SUBHIERARCHIES
= 1 << 1,
163 static int add_subtree_to_set(
169 sd_bus_error
*error
) {
179 r
= add_enumerated_to_set(bus
, prefix
, n
->enumerators
, s
, error
);
182 if (bus
->nodes_modified
)
185 LIST_FOREACH(siblings
, i
, n
->child
) {
188 if (!object_path_startswith(i
->path
, prefix
))
195 r
= set_consume(s
, t
);
196 if (r
< 0 && r
!= -EEXIST
)
199 if ((flags
& CHILDREN_RECURSIVE
) &&
200 ((flags
& CHILDREN_SUBHIERARCHIES
) || !i
->object_managers
)) {
201 r
= add_subtree_to_set(bus
, prefix
, i
, flags
, s
, error
);
204 if (bus
->nodes_modified
)
212 static int get_child_nodes(
218 sd_bus_error
*error
) {
228 s
= set_new(&string_hash_ops
);
232 r
= add_subtree_to_set(bus
, prefix
, n
, flags
, s
, error
);
242 static int node_callbacks_run(
245 struct node_callback
*first
,
246 bool require_fallback
,
247 bool *found_object
) {
249 struct node_callback
*c
;
254 assert(found_object
);
256 LIST_FOREACH(callbacks
, c
, first
) {
257 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
260 if (bus
->nodes_modified
)
263 if (require_fallback
&& !c
->is_fallback
)
266 *found_object
= true;
268 if (c
->last_iteration
== bus
->iteration_counter
)
271 c
->last_iteration
= bus
->iteration_counter
;
273 r
= sd_bus_message_rewind(m
, true);
277 slot
= container_of(c
, sd_bus_slot
, node_callback
);
279 bus
->current_slot
= sd_bus_slot_ref(slot
);
280 bus
->current_handler
= c
->callback
;
281 bus
->current_userdata
= slot
->userdata
;
282 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
283 bus
->current_userdata
= NULL
;
284 bus
->current_handler
= NULL
;
285 bus
->current_slot
= sd_bus_slot_unref(slot
);
287 r
= bus_maybe_reply_error(m
, r
, &error_buffer
);
295 #define CAPABILITY_SHIFT(x) (((x) >> __builtin_ctzll(_SD_BUS_VTABLE_CAPABILITY_MASK)) & 0xFFFF)
297 static int check_access(sd_bus
*bus
, sd_bus_message
*m
, struct vtable_member
*c
, sd_bus_error
*error
) {
305 /* If the entire bus is trusted let's grant access */
309 /* If the member is marked UNPRIVILEGED let's grant access */
310 if (c
->vtable
->flags
& SD_BUS_VTABLE_UNPRIVILEGED
)
313 /* Check have the caller has the requested capability
314 * set. Note that the flags value contains the capability
315 * number plus one, which we need to subtract here. We do this
316 * so that we have 0 as special value for "default
318 cap
= CAPABILITY_SHIFT(c
->vtable
->flags
);
320 cap
= CAPABILITY_SHIFT(c
->parent
->vtable
[0].flags
);
326 r
= sd_bus_query_sender_privilege(m
, cap
);
332 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Access to %s.%s() not permitted.", c
->interface
, c
->member
);
335 static int method_callbacks_run(
338 struct vtable_member
*c
,
339 bool require_fallback
,
340 bool *found_object
) {
342 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
343 const char *signature
;
350 assert(found_object
);
352 if (require_fallback
&& !c
->parent
->is_fallback
)
355 r
= check_access(bus
, m
, c
, &error
);
357 return bus_maybe_reply_error(m
, r
, &error
);
359 r
= node_vtable_get_userdata(bus
, m
->path
, c
->parent
, &u
, &error
);
361 return bus_maybe_reply_error(m
, r
, &error
);
362 if (bus
->nodes_modified
)
365 u
= vtable_method_convert_userdata(c
->vtable
, u
);
367 *found_object
= true;
369 if (c
->last_iteration
== bus
->iteration_counter
)
372 c
->last_iteration
= bus
->iteration_counter
;
374 r
= sd_bus_message_rewind(m
, true);
378 signature
= sd_bus_message_get_signature(m
, true);
382 if (!streq(strempty(c
->vtable
->x
.method
.signature
), signature
))
383 return sd_bus_reply_method_errorf(
385 SD_BUS_ERROR_INVALID_ARGS
,
386 "Invalid arguments '%s' to call %s.%s(), expecting '%s'.",
387 signature
, c
->interface
, c
->member
, strempty(c
->vtable
->x
.method
.signature
));
389 /* Keep track what the signature of the reply to this message
390 * should be, so that this can be enforced when sealing the
392 m
->enforced_reply_signature
= strempty(c
->vtable
->x
.method
.result
);
394 if (c
->vtable
->x
.method
.handler
) {
397 slot
= container_of(c
->parent
, sd_bus_slot
, node_vtable
);
399 bus
->current_slot
= sd_bus_slot_ref(slot
);
400 bus
->current_handler
= c
->vtable
->x
.method
.handler
;
401 bus
->current_userdata
= u
;
402 r
= c
->vtable
->x
.method
.handler(m
, u
, &error
);
403 bus
->current_userdata
= NULL
;
404 bus
->current_handler
= NULL
;
405 bus
->current_slot
= sd_bus_slot_unref(slot
);
407 return bus_maybe_reply_error(m
, r
, &error
);
410 /* If the method callback is NULL, make this a successful NOP */
411 r
= sd_bus_reply_method_return(m
, NULL
);
418 static int invoke_property_get(
421 const sd_bus_vtable
*v
,
423 const char *interface
,
424 const char *property
,
425 sd_bus_message
*reply
,
427 sd_bus_error
*error
) {
440 if (v
->x
.property
.get
) {
442 bus
->current_slot
= sd_bus_slot_ref(slot
);
443 bus
->current_userdata
= userdata
;
444 r
= v
->x
.property
.get(bus
, path
, interface
, property
, reply
, userdata
, error
);
445 bus
->current_userdata
= NULL
;
446 bus
->current_slot
= sd_bus_slot_unref(slot
);
450 if (sd_bus_error_is_set(error
))
451 return -sd_bus_error_get_errno(error
);
455 /* Automatic handling if no callback is defined. */
457 if (streq(v
->x
.property
.signature
, "as"))
458 return sd_bus_message_append_strv(reply
, *(char***) userdata
);
460 assert(signature_is_single(v
->x
.property
.signature
, false));
461 assert(bus_type_is_basic(v
->x
.property
.signature
[0]));
463 switch (v
->x
.property
.signature
[0]) {
465 case SD_BUS_TYPE_STRING
:
466 case SD_BUS_TYPE_SIGNATURE
:
467 p
= strempty(*(char**) userdata
);
470 case SD_BUS_TYPE_OBJECT_PATH
:
471 p
= *(char**) userdata
;
480 return sd_bus_message_append_basic(reply
, v
->x
.property
.signature
[0], p
);
483 static int invoke_property_set(
486 const sd_bus_vtable
*v
,
488 const char *interface
,
489 const char *property
,
490 sd_bus_message
*value
,
492 sd_bus_error
*error
) {
504 if (v
->x
.property
.set
) {
506 bus
->current_slot
= sd_bus_slot_ref(slot
);
507 bus
->current_userdata
= userdata
;
508 r
= v
->x
.property
.set(bus
, path
, interface
, property
, value
, userdata
, error
);
509 bus
->current_userdata
= NULL
;
510 bus
->current_slot
= sd_bus_slot_unref(slot
);
514 if (sd_bus_error_is_set(error
))
515 return -sd_bus_error_get_errno(error
);
519 /* Automatic handling if no callback is defined. */
521 assert(signature_is_single(v
->x
.property
.signature
, false));
522 assert(bus_type_is_basic(v
->x
.property
.signature
[0]));
524 switch (v
->x
.property
.signature
[0]) {
526 case SD_BUS_TYPE_STRING
:
527 case SD_BUS_TYPE_OBJECT_PATH
:
528 case SD_BUS_TYPE_SIGNATURE
: {
532 r
= sd_bus_message_read_basic(value
, v
->x
.property
.signature
[0], &p
);
540 free(*(char**) userdata
);
541 *(char**) userdata
= n
;
547 r
= sd_bus_message_read_basic(value
, v
->x
.property
.signature
[0], userdata
);
557 static int property_get_set_callbacks_run(
560 struct vtable_member
*c
,
561 bool require_fallback
,
563 bool *found_object
) {
565 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
566 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
574 assert(found_object
);
576 if (require_fallback
&& !c
->parent
->is_fallback
)
579 r
= vtable_property_get_userdata(bus
, m
->path
, c
, &u
, &error
);
581 return bus_maybe_reply_error(m
, r
, &error
);
582 if (bus
->nodes_modified
)
585 slot
= container_of(c
->parent
, sd_bus_slot
, node_vtable
);
587 *found_object
= true;
589 r
= sd_bus_message_new_method_return(m
, &reply
);
594 /* Note that we do not protect against reexecution
595 * here (using the last_iteration check, see below),
596 * should the node tree have changed and we got called
597 * again. We assume that property Get() calls are
598 * ultimately without side-effects or if they aren't
599 * then at least idempotent. */
601 r
= sd_bus_message_open_container(reply
, 'v', c
->vtable
->x
.property
.signature
);
605 /* Note that we do not do an access check here. Read
606 * access to properties is always unrestricted, since
607 * PropertiesChanged signals broadcast contents
610 r
= invoke_property_get(bus
, slot
, c
->vtable
, m
->path
, c
->interface
, c
->member
, reply
, u
, &error
);
612 return bus_maybe_reply_error(m
, r
, &error
);
614 if (bus
->nodes_modified
)
617 r
= sd_bus_message_close_container(reply
);
622 const char *signature
= NULL
;
625 if (c
->vtable
->type
!= _SD_BUS_VTABLE_WRITABLE_PROPERTY
)
626 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Property '%s' is not writable.", c
->member
);
628 /* Avoid that we call the set routine more than once
629 * if the processing of this message got restarted
630 * because the node tree changed. */
631 if (c
->last_iteration
== bus
->iteration_counter
)
634 c
->last_iteration
= bus
->iteration_counter
;
636 r
= sd_bus_message_peek_type(m
, &type
, &signature
);
640 if (type
!= 'v' || !streq(strempty(signature
), strempty(c
->vtable
->x
.property
.signature
)))
641 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
));
643 r
= sd_bus_message_enter_container(m
, 'v', c
->vtable
->x
.property
.signature
);
647 r
= check_access(bus
, m
, c
, &error
);
649 return bus_maybe_reply_error(m
, r
, &error
);
651 r
= invoke_property_set(bus
, slot
, c
->vtable
, m
->path
, c
->interface
, c
->member
, m
, u
, &error
);
653 return bus_maybe_reply_error(m
, r
, &error
);
655 if (bus
->nodes_modified
)
658 r
= sd_bus_message_exit_container(m
);
663 r
= sd_bus_send(bus
, reply
, NULL
);
670 static int vtable_append_one_property(
672 sd_bus_message
*reply
,
674 struct node_vtable
*c
,
675 const sd_bus_vtable
*v
,
677 sd_bus_error
*error
) {
688 r
= sd_bus_message_open_container(reply
, 'e', "sv");
692 r
= sd_bus_message_append(reply
, "s", v
->x
.property
.member
);
696 r
= sd_bus_message_open_container(reply
, 'v', v
->x
.property
.signature
);
700 slot
= container_of(c
, sd_bus_slot
, node_vtable
);
702 r
= invoke_property_get(bus
, slot
, v
, path
, c
->interface
, v
->x
.property
.member
, reply
, vtable_property_convert_userdata(v
, userdata
), error
);
705 if (bus
->nodes_modified
)
708 r
= sd_bus_message_close_container(reply
);
712 r
= sd_bus_message_close_container(reply
);
719 static int vtable_append_all_properties(
721 sd_bus_message
*reply
,
723 struct node_vtable
*c
,
725 sd_bus_error
*error
) {
727 const sd_bus_vtable
*v
;
735 if (c
->vtable
[0].flags
& SD_BUS_VTABLE_HIDDEN
)
738 for (v
= c
->vtable
+1; v
->type
!= _SD_BUS_VTABLE_END
; v
++) {
739 if (!IN_SET(v
->type
, _SD_BUS_VTABLE_PROPERTY
, _SD_BUS_VTABLE_WRITABLE_PROPERTY
))
742 if (v
->flags
& SD_BUS_VTABLE_HIDDEN
)
745 if (v
->flags
& SD_BUS_VTABLE_PROPERTY_EXPLICIT
)
748 r
= vtable_append_one_property(bus
, reply
, path
, c
, v
, userdata
, error
);
751 if (bus
->nodes_modified
)
758 static int property_get_all_callbacks_run(
761 struct node_vtable
*first
,
762 bool require_fallback
,
764 bool *found_object
) {
766 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
767 struct node_vtable
*c
;
768 bool found_interface
;
773 assert(found_object
);
775 r
= sd_bus_message_new_method_return(m
, &reply
);
779 r
= sd_bus_message_open_container(reply
, 'a', "{sv}");
783 found_interface
= !iface
||
784 streq(iface
, "org.freedesktop.DBus.Properties") ||
785 streq(iface
, "org.freedesktop.DBus.Peer") ||
786 streq(iface
, "org.freedesktop.DBus.Introspectable");
788 LIST_FOREACH(vtables
, c
, first
) {
789 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
792 if (require_fallback
&& !c
->is_fallback
)
795 r
= node_vtable_get_userdata(bus
, m
->path
, c
, &u
, &error
);
797 return bus_maybe_reply_error(m
, r
, &error
);
798 if (bus
->nodes_modified
)
803 *found_object
= true;
805 if (iface
&& !streq(c
->interface
, iface
))
807 found_interface
= true;
809 r
= vtable_append_all_properties(bus
, reply
, m
->path
, c
, u
, &error
);
811 return bus_maybe_reply_error(m
, r
, &error
);
812 if (bus
->nodes_modified
)
819 if (!found_interface
) {
820 r
= sd_bus_reply_method_errorf(
822 SD_BUS_ERROR_UNKNOWN_INTERFACE
,
823 "Unknown interface '%s'.", iface
);
830 r
= sd_bus_message_close_container(reply
);
834 r
= sd_bus_send(bus
, reply
, NULL
);
841 static int bus_node_exists(
845 bool require_fallback
) {
847 struct node_vtable
*c
;
848 struct node_callback
*k
;
855 /* Tests if there's anything attached directly to this node
856 * for the specified path */
858 if (!require_fallback
&& (n
->enumerators
|| n
->object_managers
))
861 LIST_FOREACH(callbacks
, k
, n
->callbacks
) {
862 if (require_fallback
&& !k
->is_fallback
)
868 LIST_FOREACH(vtables
, c
, n
->vtables
) {
869 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
871 if (require_fallback
&& !c
->is_fallback
)
874 r
= node_vtable_get_userdata(bus
, path
, c
, NULL
, &error
);
877 if (bus
->nodes_modified
)
884 static int process_introspect(
888 bool require_fallback
,
889 bool *found_object
) {
891 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
892 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
893 _cleanup_set_free_free_ Set
*s
= NULL
;
894 const char *previous_interface
= NULL
;
895 struct introspect intro
;
896 struct node_vtable
*c
;
903 assert(found_object
);
905 r
= get_child_nodes(bus
, m
->path
, n
, 0, &s
, &error
);
907 return bus_maybe_reply_error(m
, r
, &error
);
908 if (bus
->nodes_modified
)
911 r
= introspect_begin(&intro
, bus
->trusted
);
915 r
= introspect_write_default_interfaces(&intro
, !require_fallback
&& n
->object_managers
);
919 empty
= set_isempty(s
);
921 LIST_FOREACH(vtables
, c
, n
->vtables
) {
922 if (require_fallback
&& !c
->is_fallback
)
925 r
= node_vtable_get_userdata(bus
, m
->path
, c
, NULL
, &error
);
927 r
= bus_maybe_reply_error(m
, r
, &error
);
930 if (bus
->nodes_modified
) {
939 if (c
->vtable
[0].flags
& SD_BUS_VTABLE_HIDDEN
)
942 if (!streq_ptr(previous_interface
, c
->interface
)) {
944 if (previous_interface
)
945 fputs(" </interface>\n", intro
.f
);
947 fprintf(intro
.f
, " <interface name=\"%s\">\n", c
->interface
);
950 r
= introspect_write_interface(&intro
, c
->vtable
);
954 previous_interface
= c
->interface
;
957 if (previous_interface
)
958 fputs(" </interface>\n", intro
.f
);
961 /* Nothing?, let's see if we exist at all, and if not
962 * refuse to do anything */
963 r
= bus_node_exists(bus
, n
, m
->path
, require_fallback
);
965 r
= bus_maybe_reply_error(m
, r
, &error
);
968 if (bus
->nodes_modified
) {
974 *found_object
= true;
976 r
= introspect_write_child_nodes(&intro
, s
, m
->path
);
980 r
= introspect_finish(&intro
, bus
, m
, &reply
);
984 r
= sd_bus_send(bus
, reply
, NULL
);
991 introspect_free(&intro
);
995 static int object_manager_serialize_path(
997 sd_bus_message
*reply
,
1000 bool require_fallback
,
1001 sd_bus_error
*error
) {
1003 const char *previous_interface
= NULL
;
1004 bool found_something
= false;
1005 struct node_vtable
*i
;
1015 n
= hashmap_get(bus
->nodes
, prefix
);
1019 LIST_FOREACH(vtables
, i
, n
->vtables
) {
1022 if (require_fallback
&& !i
->is_fallback
)
1025 r
= node_vtable_get_userdata(bus
, path
, i
, &u
, error
);
1028 if (bus
->nodes_modified
)
1033 if (!found_something
) {
1035 /* Open the object part */
1037 r
= sd_bus_message_open_container(reply
, 'e', "oa{sa{sv}}");
1041 r
= sd_bus_message_append(reply
, "o", path
);
1045 r
= sd_bus_message_open_container(reply
, 'a', "{sa{sv}}");
1049 r
= sd_bus_message_append(reply
, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
1053 r
= sd_bus_message_append(reply
, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
1057 r
= sd_bus_message_append(reply
, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
1061 r
= sd_bus_message_append(reply
, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
1065 found_something
= true;
1068 if (!streq_ptr(previous_interface
, i
->interface
)) {
1070 /* Maybe close the previous interface part */
1072 if (previous_interface
) {
1073 r
= sd_bus_message_close_container(reply
);
1077 r
= sd_bus_message_close_container(reply
);
1082 /* Open the new interface part */
1084 r
= sd_bus_message_open_container(reply
, 'e', "sa{sv}");
1088 r
= sd_bus_message_append(reply
, "s", i
->interface
);
1092 r
= sd_bus_message_open_container(reply
, 'a', "{sv}");
1097 r
= vtable_append_all_properties(bus
, reply
, path
, i
, u
, error
);
1100 if (bus
->nodes_modified
)
1103 previous_interface
= i
->interface
;
1106 if (previous_interface
) {
1107 r
= sd_bus_message_close_container(reply
);
1111 r
= sd_bus_message_close_container(reply
);
1116 if (found_something
) {
1117 r
= sd_bus_message_close_container(reply
);
1121 r
= sd_bus_message_close_container(reply
);
1129 static int object_manager_serialize_path_and_fallbacks(
1131 sd_bus_message
*reply
,
1133 sd_bus_error
*error
) {
1143 /* First, add all vtables registered for this path */
1144 r
= object_manager_serialize_path(bus
, reply
, path
, path
, false, error
);
1147 if (bus
->nodes_modified
)
1150 /* Second, add fallback vtables registered for any of the prefixes */
1151 prefix
= alloca(strlen(path
) + 1);
1152 OBJECT_PATH_FOREACH_PREFIX(prefix
, path
) {
1153 r
= object_manager_serialize_path(bus
, reply
, prefix
, path
, true, error
);
1156 if (bus
->nodes_modified
)
1163 static int process_get_managed_objects(
1167 bool require_fallback
,
1168 bool *found_object
) {
1170 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1171 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1172 _cleanup_set_free_free_ Set
*s
= NULL
;
1180 assert(found_object
);
1182 /* Spec says, GetManagedObjects() is only implemented on the root of a
1183 * sub-tree. Therefore, we require a registered object-manager on
1184 * exactly the queried path, otherwise, we refuse to respond. */
1186 if (require_fallback
|| !n
->object_managers
)
1189 r
= get_child_nodes(bus
, m
->path
, n
, CHILDREN_RECURSIVE
, &s
, &error
);
1191 return bus_maybe_reply_error(m
, r
, &error
);
1192 if (bus
->nodes_modified
)
1195 r
= sd_bus_message_new_method_return(m
, &reply
);
1199 r
= sd_bus_message_open_container(reply
, 'a', "{oa{sa{sv}}}");
1203 SET_FOREACH(path
, s
, i
) {
1204 r
= object_manager_serialize_path_and_fallbacks(bus
, reply
, path
, &error
);
1206 return bus_maybe_reply_error(m
, r
, &error
);
1208 if (bus
->nodes_modified
)
1212 r
= sd_bus_message_close_container(reply
);
1216 r
= sd_bus_send(bus
, reply
, NULL
);
1223 static int object_find_and_run(
1227 bool require_fallback
,
1228 bool *found_object
) {
1231 struct vtable_member vtable_key
, *v
;
1237 assert(found_object
);
1239 n
= hashmap_get(bus
->nodes
, p
);
1243 /* First, try object callbacks */
1244 r
= node_callbacks_run(bus
, m
, n
->callbacks
, require_fallback
, found_object
);
1247 if (bus
->nodes_modified
)
1250 if (!m
->interface
|| !m
->member
)
1253 /* Then, look for a known method */
1254 vtable_key
.path
= (char*) p
;
1255 vtable_key
.interface
= m
->interface
;
1256 vtable_key
.member
= m
->member
;
1258 v
= hashmap_get(bus
->vtable_methods
, &vtable_key
);
1260 r
= method_callbacks_run(bus
, m
, v
, require_fallback
, found_object
);
1263 if (bus
->nodes_modified
)
1267 /* Then, look for a known property */
1268 if (streq(m
->interface
, "org.freedesktop.DBus.Properties")) {
1271 get
= streq(m
->member
, "Get");
1273 if (get
|| streq(m
->member
, "Set")) {
1275 r
= sd_bus_message_rewind(m
, true);
1279 vtable_key
.path
= (char*) p
;
1281 r
= sd_bus_message_read(m
, "ss", &vtable_key
.interface
, &vtable_key
.member
);
1283 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_INVALID_ARGS
, "Expected interface and member parameters");
1285 v
= hashmap_get(bus
->vtable_properties
, &vtable_key
);
1287 r
= property_get_set_callbacks_run(bus
, m
, v
, require_fallback
, get
, found_object
);
1292 } else if (streq(m
->member
, "GetAll")) {
1295 r
= sd_bus_message_rewind(m
, true);
1299 r
= sd_bus_message_read(m
, "s", &iface
);
1301 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_INVALID_ARGS
, "Expected interface parameter");
1306 r
= property_get_all_callbacks_run(bus
, m
, n
->vtables
, require_fallback
, iface
, found_object
);
1311 } else if (sd_bus_message_is_method_call(m
, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1313 if (!isempty(sd_bus_message_get_signature(m
, true)))
1314 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_INVALID_ARGS
, "Expected no parameters");
1316 r
= process_introspect(bus
, m
, n
, require_fallback
, found_object
);
1320 } else if (sd_bus_message_is_method_call(m
, "org.freedesktop.DBus.ObjectManager", "GetManagedObjects")) {
1322 if (!isempty(sd_bus_message_get_signature(m
, true)))
1323 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_INVALID_ARGS
, "Expected no parameters");
1325 r
= process_get_managed_objects(bus
, m
, n
, require_fallback
, found_object
);
1330 if (bus
->nodes_modified
)
1333 if (!*found_object
) {
1334 r
= bus_node_exists(bus
, n
, m
->path
, require_fallback
);
1336 return bus_maybe_reply_error(m
, r
, NULL
);
1337 if (bus
->nodes_modified
)
1340 *found_object
= true;
1346 int bus_process_object(sd_bus
*bus
, sd_bus_message
*m
) {
1349 bool found_object
= false;
1354 if (bus
->is_monitor
)
1357 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_CALL
)
1360 if (hashmap_isempty(bus
->nodes
))
1363 /* Never respond to broadcast messages */
1364 if (bus
->bus_client
&& !m
->destination
)
1370 pl
= strlen(m
->path
);
1374 bus
->nodes_modified
= false;
1376 r
= object_find_and_run(bus
, m
, m
->path
, false, &found_object
);
1380 /* Look for fallback prefixes */
1381 OBJECT_PATH_FOREACH_PREFIX(prefix
, m
->path
) {
1383 if (bus
->nodes_modified
)
1386 r
= object_find_and_run(bus
, m
, prefix
, true, &found_object
);
1391 } while (bus
->nodes_modified
);
1396 if (sd_bus_message_is_method_call(m
, "org.freedesktop.DBus.Properties", "Get") ||
1397 sd_bus_message_is_method_call(m
, "org.freedesktop.DBus.Properties", "Set"))
1398 r
= sd_bus_reply_method_errorf(
1400 SD_BUS_ERROR_UNKNOWN_PROPERTY
,
1401 "Unknown property or interface.");
1403 r
= sd_bus_reply_method_errorf(
1405 SD_BUS_ERROR_UNKNOWN_METHOD
,
1406 "Unknown method '%s' or interface '%s'.", m
->member
, m
->interface
);
1414 static struct node
*bus_node_allocate(sd_bus
*bus
, const char *path
) {
1415 struct node
*n
, *parent
;
1417 _cleanup_free_
char *s
= NULL
;
1423 assert(path
[0] == '/');
1425 n
= hashmap_get(bus
->nodes
, path
);
1429 r
= hashmap_ensure_allocated(&bus
->nodes
, &string_hash_ops
);
1437 if (streq(path
, "/"))
1440 e
= strrchr(path
, '/');
1443 p
= strndupa(path
, MAX(1, e
- path
));
1445 parent
= bus_node_allocate(bus
, p
);
1450 n
= new0(struct node
, 1);
1455 n
->path
= TAKE_PTR(s
);
1457 r
= hashmap_put(bus
->nodes
, n
->path
, n
);
1464 LIST_PREPEND(siblings
, parent
->child
, n
);
1469 void bus_node_gc(sd_bus
*b
, struct node
*n
) {
1482 assert_se(hashmap_remove(b
->nodes
, n
->path
) == n
);
1485 LIST_REMOVE(siblings
, n
->parent
->child
, n
);
1488 bus_node_gc(b
, n
->parent
);
1492 static int bus_find_parent_object_manager(sd_bus
*bus
, struct node
**out
, const char *path
) {
1498 n
= hashmap_get(bus
->nodes
, path
);
1502 prefix
= alloca(strlen(path
) + 1);
1503 OBJECT_PATH_FOREACH_PREFIX(prefix
, path
) {
1504 n
= hashmap_get(bus
->nodes
, prefix
);
1510 while (n
&& !n
->object_managers
)
1518 static int bus_add_object(
1523 sd_bus_message_handler_t callback
,
1530 assert_return(bus
, -EINVAL
);
1531 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1532 assert_return(object_path_is_valid(path
), -EINVAL
);
1533 assert_return(callback
, -EINVAL
);
1534 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1536 n
= bus_node_allocate(bus
, path
);
1540 s
= bus_slot_allocate(bus
, !slot
, BUS_NODE_CALLBACK
, sizeof(struct node_callback
), userdata
);
1546 s
->node_callback
.callback
= callback
;
1547 s
->node_callback
.is_fallback
= fallback
;
1549 s
->node_callback
.node
= n
;
1550 LIST_PREPEND(callbacks
, n
->callbacks
, &s
->node_callback
);
1551 bus
->nodes_modified
= true;
1559 sd_bus_slot_unref(s
);
1560 bus_node_gc(bus
, n
);
1565 _public_
int sd_bus_add_object(
1569 sd_bus_message_handler_t callback
,
1572 return bus_add_object(bus
, slot
, false, path
, callback
, userdata
);
1575 _public_
int sd_bus_add_fallback(
1579 sd_bus_message_handler_t callback
,
1582 return bus_add_object(bus
, slot
, true, prefix
, callback
, userdata
);
1585 static void vtable_member_hash_func(const void *a
, struct siphash
*state
) {
1586 const struct vtable_member
*m
= a
;
1590 string_hash_func(m
->path
, state
);
1591 string_hash_func(m
->interface
, state
);
1592 string_hash_func(m
->member
, state
);
1595 static int vtable_member_compare_func(const void *a
, const void *b
) {
1596 const struct vtable_member
*x
= a
, *y
= b
;
1602 r
= strcmp(x
->path
, y
->path
);
1606 r
= strcmp(x
->interface
, y
->interface
);
1610 return strcmp(x
->member
, y
->member
);
1613 static const struct hash_ops vtable_member_hash_ops
= {
1614 .hash
= vtable_member_hash_func
,
1615 .compare
= vtable_member_compare_func
1618 static int add_object_vtable_internal(
1622 const char *interface
,
1623 const sd_bus_vtable
*vtable
,
1625 sd_bus_object_find_t find
,
1628 sd_bus_slot
*s
= NULL
;
1629 struct node_vtable
*i
, *existing
= NULL
;
1630 const sd_bus_vtable
*v
;
1634 assert_return(bus
, -EINVAL
);
1635 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1636 assert_return(object_path_is_valid(path
), -EINVAL
);
1637 assert_return(interface_name_is_valid(interface
), -EINVAL
);
1638 assert_return(vtable
, -EINVAL
);
1639 assert_return(vtable
[0].type
== _SD_BUS_VTABLE_START
, -EINVAL
);
1640 assert_return(vtable
[0].x
.start
.element_size
== sizeof(struct sd_bus_vtable
), -EINVAL
);
1641 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1642 assert_return(!streq(interface
, "org.freedesktop.DBus.Properties") &&
1643 !streq(interface
, "org.freedesktop.DBus.Introspectable") &&
1644 !streq(interface
, "org.freedesktop.DBus.Peer") &&
1645 !streq(interface
, "org.freedesktop.DBus.ObjectManager"), -EINVAL
);
1647 r
= hashmap_ensure_allocated(&bus
->vtable_methods
, &vtable_member_hash_ops
);
1651 r
= hashmap_ensure_allocated(&bus
->vtable_properties
, &vtable_member_hash_ops
);
1655 n
= bus_node_allocate(bus
, path
);
1659 LIST_FOREACH(vtables
, i
, n
->vtables
) {
1660 if (i
->is_fallback
!= fallback
) {
1665 if (streq(i
->interface
, interface
)) {
1667 if (i
->vtable
== vtable
) {
1676 s
= bus_slot_allocate(bus
, !slot
, BUS_NODE_VTABLE
, sizeof(struct node_vtable
), userdata
);
1682 s
->node_vtable
.is_fallback
= fallback
;
1683 s
->node_vtable
.vtable
= vtable
;
1684 s
->node_vtable
.find
= find
;
1686 s
->node_vtable
.interface
= strdup(interface
);
1687 if (!s
->node_vtable
.interface
) {
1692 for (v
= s
->node_vtable
.vtable
+1; v
->type
!= _SD_BUS_VTABLE_END
; v
++) {
1696 case _SD_BUS_VTABLE_METHOD
: {
1697 struct vtable_member
*m
;
1699 if (!member_name_is_valid(v
->x
.method
.member
) ||
1700 !signature_is_valid(strempty(v
->x
.method
.signature
), false) ||
1701 !signature_is_valid(strempty(v
->x
.method
.result
), false) ||
1702 !(v
->x
.method
.handler
|| (isempty(v
->x
.method
.signature
) && isempty(v
->x
.method
.result
))) ||
1703 v
->flags
& (SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
)) {
1708 m
= new0(struct vtable_member
, 1);
1714 m
->parent
= &s
->node_vtable
;
1716 m
->interface
= s
->node_vtable
.interface
;
1717 m
->member
= v
->x
.method
.member
;
1720 r
= hashmap_put(bus
->vtable_methods
, m
, m
);
1729 case _SD_BUS_VTABLE_WRITABLE_PROPERTY
:
1731 if (!(v
->x
.property
.set
|| bus_type_is_basic(v
->x
.property
.signature
[0]))) {
1736 if (v
->flags
& SD_BUS_VTABLE_PROPERTY_CONST
) {
1742 case _SD_BUS_VTABLE_PROPERTY
: {
1743 struct vtable_member
*m
;
1745 if (!member_name_is_valid(v
->x
.property
.member
) ||
1746 !signature_is_single(v
->x
.property
.signature
, false) ||
1747 !(v
->x
.property
.get
|| bus_type_is_basic(v
->x
.property
.signature
[0]) || streq(v
->x
.property
.signature
, "as")) ||
1748 (v
->flags
& SD_BUS_VTABLE_METHOD_NO_REPLY
) ||
1749 (!!(v
->flags
& SD_BUS_VTABLE_PROPERTY_CONST
) + !!(v
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
) + !!(v
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
)) > 1 ||
1750 ((v
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
) && (v
->flags
& SD_BUS_VTABLE_PROPERTY_EXPLICIT
)) ||
1751 (v
->flags
& SD_BUS_VTABLE_UNPRIVILEGED
&& v
->type
== _SD_BUS_VTABLE_PROPERTY
)) {
1756 m
= new0(struct vtable_member
, 1);
1762 m
->parent
= &s
->node_vtable
;
1764 m
->interface
= s
->node_vtable
.interface
;
1765 m
->member
= v
->x
.property
.member
;
1768 r
= hashmap_put(bus
->vtable_properties
, m
, m
);
1777 case _SD_BUS_VTABLE_SIGNAL
:
1779 if (!member_name_is_valid(v
->x
.signal
.member
) ||
1780 !signature_is_valid(strempty(v
->x
.signal
.signature
), false) ||
1781 v
->flags
& SD_BUS_VTABLE_UNPRIVILEGED
) {
1794 s
->node_vtable
.node
= n
;
1795 LIST_INSERT_AFTER(vtables
, n
->vtables
, existing
, &s
->node_vtable
);
1796 bus
->nodes_modified
= true;
1804 sd_bus_slot_unref(s
);
1805 bus_node_gc(bus
, n
);
1810 _public_
int sd_bus_add_object_vtable(
1814 const char *interface
,
1815 const sd_bus_vtable
*vtable
,
1818 return add_object_vtable_internal(bus
, slot
, path
, interface
, vtable
, false, NULL
, userdata
);
1821 _public_
int sd_bus_add_fallback_vtable(
1825 const char *interface
,
1826 const sd_bus_vtable
*vtable
,
1827 sd_bus_object_find_t find
,
1830 return add_object_vtable_internal(bus
, slot
, prefix
, interface
, vtable
, true, find
, userdata
);
1833 _public_
int sd_bus_add_node_enumerator(
1837 sd_bus_node_enumerator_t callback
,
1844 assert_return(bus
, -EINVAL
);
1845 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1846 assert_return(object_path_is_valid(path
), -EINVAL
);
1847 assert_return(callback
, -EINVAL
);
1848 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1850 n
= bus_node_allocate(bus
, path
);
1854 s
= bus_slot_allocate(bus
, !slot
, BUS_NODE_ENUMERATOR
, sizeof(struct node_enumerator
), userdata
);
1860 s
->node_enumerator
.callback
= callback
;
1862 s
->node_enumerator
.node
= n
;
1863 LIST_PREPEND(enumerators
, n
->enumerators
, &s
->node_enumerator
);
1864 bus
->nodes_modified
= true;
1872 sd_bus_slot_unref(s
);
1873 bus_node_gc(bus
, n
);
1878 static int emit_properties_changed_on_interface(
1882 const char *interface
,
1883 bool require_fallback
,
1884 bool *found_interface
,
1887 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1888 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1889 bool has_invalidating
= false, has_changing
= false;
1890 struct vtable_member key
= {};
1891 struct node_vtable
*c
;
1901 assert(found_interface
);
1903 n
= hashmap_get(bus
->nodes
, prefix
);
1907 r
= sd_bus_message_new_signal(bus
, &m
, path
, "org.freedesktop.DBus.Properties", "PropertiesChanged");
1911 r
= sd_bus_message_append(m
, "s", interface
);
1915 r
= sd_bus_message_open_container(m
, 'a', "{sv}");
1920 key
.interface
= interface
;
1922 LIST_FOREACH(vtables
, c
, n
->vtables
) {
1923 if (require_fallback
&& !c
->is_fallback
)
1926 if (!streq(c
->interface
, interface
))
1929 r
= node_vtable_get_userdata(bus
, path
, c
, &u
, &error
);
1932 if (bus
->nodes_modified
)
1937 *found_interface
= true;
1940 /* If the caller specified a list of
1941 * properties we include exactly those in the
1942 * PropertiesChanged message */
1944 STRV_FOREACH(property
, names
) {
1945 struct vtable_member
*v
;
1947 assert_return(member_name_is_valid(*property
), -EINVAL
);
1949 key
.member
= *property
;
1950 v
= hashmap_get(bus
->vtable_properties
, &key
);
1954 /* If there are two vtables for the same
1955 * interface, let's handle this property when
1956 * we come to that vtable. */
1960 assert_return(v
->vtable
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
||
1961 v
->vtable
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
, -EDOM
);
1963 assert_return(!(v
->vtable
->flags
& SD_BUS_VTABLE_HIDDEN
), -EDOM
);
1965 if (v
->vtable
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
) {
1966 has_invalidating
= true;
1970 has_changing
= true;
1972 r
= vtable_append_one_property(bus
, m
, m
->path
, c
, v
->vtable
, u
, &error
);
1975 if (bus
->nodes_modified
)
1979 const sd_bus_vtable
*v
;
1981 /* If the caller specified no properties list
1982 * we include all properties that are marked
1983 * as changing in the message. */
1985 for (v
= c
->vtable
+1; v
->type
!= _SD_BUS_VTABLE_END
; v
++) {
1986 if (!IN_SET(v
->type
, _SD_BUS_VTABLE_PROPERTY
, _SD_BUS_VTABLE_WRITABLE_PROPERTY
))
1989 if (v
->flags
& SD_BUS_VTABLE_HIDDEN
)
1992 if (v
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
) {
1993 has_invalidating
= true;
1997 if (!(v
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
))
2000 has_changing
= true;
2002 r
= vtable_append_one_property(bus
, m
, m
->path
, c
, v
, u
, &error
);
2005 if (bus
->nodes_modified
)
2011 if (!has_invalidating
&& !has_changing
)
2014 r
= sd_bus_message_close_container(m
);
2018 r
= sd_bus_message_open_container(m
, 'a', "s");
2022 if (has_invalidating
) {
2023 LIST_FOREACH(vtables
, c
, n
->vtables
) {
2024 if (require_fallback
&& !c
->is_fallback
)
2027 if (!streq(c
->interface
, interface
))
2030 r
= node_vtable_get_userdata(bus
, path
, c
, &u
, &error
);
2033 if (bus
->nodes_modified
)
2039 STRV_FOREACH(property
, names
) {
2040 struct vtable_member
*v
;
2042 key
.member
= *property
;
2043 assert_se(v
= hashmap_get(bus
->vtable_properties
, &key
));
2044 assert(c
== v
->parent
);
2046 if (!(v
->vtable
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
))
2049 r
= sd_bus_message_append(m
, "s", *property
);
2054 const sd_bus_vtable
*v
;
2056 for (v
= c
->vtable
+1; v
->type
!= _SD_BUS_VTABLE_END
; v
++) {
2057 if (!IN_SET(v
->type
, _SD_BUS_VTABLE_PROPERTY
, _SD_BUS_VTABLE_WRITABLE_PROPERTY
))
2060 if (v
->flags
& SD_BUS_VTABLE_HIDDEN
)
2063 if (!(v
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
))
2066 r
= sd_bus_message_append(m
, "s", v
->x
.property
.member
);
2074 r
= sd_bus_message_close_container(m
);
2078 r
= sd_bus_send(bus
, m
, NULL
);
2085 _public_
int sd_bus_emit_properties_changed_strv(
2088 const char *interface
,
2091 bool found_interface
= false;
2095 assert_return(bus
, -EINVAL
);
2096 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2097 assert_return(object_path_is_valid(path
), -EINVAL
);
2098 assert_return(interface_name_is_valid(interface
), -EINVAL
);
2099 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2101 if (!BUS_IS_OPEN(bus
->state
))
2104 /* A non-NULL but empty names list means nothing needs to be
2105 generated. A NULL list OTOH indicates that all properties
2106 that are set to EMITS_CHANGE or EMITS_INVALIDATION shall be
2107 included in the PropertiesChanged message. */
2108 if (names
&& names
[0] == NULL
)
2111 BUS_DONT_DESTROY(bus
);
2114 bus
->nodes_modified
= false;
2116 r
= emit_properties_changed_on_interface(bus
, path
, path
, interface
, false, &found_interface
, names
);
2119 if (bus
->nodes_modified
)
2122 prefix
= alloca(strlen(path
) + 1);
2123 OBJECT_PATH_FOREACH_PREFIX(prefix
, path
) {
2124 r
= emit_properties_changed_on_interface(bus
, prefix
, path
, interface
, true, &found_interface
, names
);
2127 if (bus
->nodes_modified
)
2131 } while (bus
->nodes_modified
);
2133 return found_interface
? 0 : -ENOENT
;
2136 _public_
int sd_bus_emit_properties_changed(
2139 const char *interface
,
2140 const char *name
, ...) {
2144 assert_return(bus
, -EINVAL
);
2145 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2146 assert_return(object_path_is_valid(path
), -EINVAL
);
2147 assert_return(interface_name_is_valid(interface
), -EINVAL
);
2148 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2150 if (!BUS_IS_OPEN(bus
->state
))
2156 names
= strv_from_stdarg_alloca(name
);
2158 return sd_bus_emit_properties_changed_strv(bus
, path
, interface
, names
);
2161 static int object_added_append_all_prefix(
2167 bool require_fallback
) {
2169 const char *previous_interface
= NULL
;
2170 struct node_vtable
*c
;
2180 n
= hashmap_get(bus
->nodes
, prefix
);
2184 LIST_FOREACH(vtables
, c
, n
->vtables
) {
2185 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2188 if (require_fallback
&& !c
->is_fallback
)
2191 r
= node_vtable_get_userdata(bus
, path
, c
, &u
, &error
);
2194 if (bus
->nodes_modified
)
2199 if (!streq_ptr(c
->interface
, previous_interface
)) {
2200 /* If a child-node already handled this interface, we
2201 * skip it on any of its parents. The child vtables
2202 * always fully override any conflicting vtables of
2203 * any parent node. */
2204 if (set_get(s
, c
->interface
))
2207 r
= set_put(s
, c
->interface
);
2211 if (previous_interface
) {
2212 r
= sd_bus_message_close_container(m
);
2215 r
= sd_bus_message_close_container(m
);
2220 r
= sd_bus_message_open_container(m
, 'e', "sa{sv}");
2223 r
= sd_bus_message_append(m
, "s", c
->interface
);
2226 r
= sd_bus_message_open_container(m
, 'a', "{sv}");
2230 previous_interface
= c
->interface
;
2233 r
= vtable_append_all_properties(bus
, m
, path
, c
, u
, &error
);
2236 if (bus
->nodes_modified
)
2240 if (previous_interface
) {
2241 r
= sd_bus_message_close_container(m
);
2244 r
= sd_bus_message_close_container(m
);
2252 static int object_added_append_all(sd_bus
*bus
, sd_bus_message
*m
, const char *path
) {
2253 _cleanup_set_free_ Set
*s
= NULL
;
2262 * This appends all interfaces registered on path @path. We first add
2263 * the builtin interfaces, which are always available and handled by
2264 * sd-bus. Then, we add all interfaces registered on the exact node,
2265 * followed by all fallback interfaces registered on any parent prefix.
2267 * If an interface is registered multiple times on the same node with
2268 * different vtables, we merge all the properties across all vtables.
2269 * However, if a child node has the same interface registered as one of
2270 * its parent nodes has as fallback, we make the child overwrite the
2271 * parent instead of extending it. Therefore, we keep a "Set" of all
2272 * handled interfaces during parent traversal, so we skip interfaces on
2273 * a parent that were overwritten by a child.
2276 s
= set_new(&string_hash_ops
);
2280 r
= sd_bus_message_append(m
, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
2283 r
= sd_bus_message_append(m
, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
2286 r
= sd_bus_message_append(m
, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
2289 r
= sd_bus_message_append(m
, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
2293 r
= object_added_append_all_prefix(bus
, m
, s
, path
, path
, false);
2296 if (bus
->nodes_modified
)
2299 prefix
= alloca(strlen(path
) + 1);
2300 OBJECT_PATH_FOREACH_PREFIX(prefix
, path
) {
2301 r
= object_added_append_all_prefix(bus
, m
, s
, prefix
, path
, true);
2304 if (bus
->nodes_modified
)
2311 _public_
int sd_bus_emit_object_added(sd_bus
*bus
, const char *path
) {
2312 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2313 struct node
*object_manager
;
2317 * This emits an InterfacesAdded signal on the given path, by iterating
2318 * all registered vtables and fallback vtables on the path. All
2319 * properties are queried and included in the signal.
2320 * This call is equivalent to sd_bus_emit_interfaces_added() with an
2321 * explicit list of registered interfaces. However, unlike
2322 * interfaces_added(), this call can figure out the list of supported
2323 * interfaces itself. Furthermore, it properly adds the builtin
2324 * org.freedesktop.DBus.* interfaces.
2327 assert_return(bus
, -EINVAL
);
2328 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2329 assert_return(object_path_is_valid(path
), -EINVAL
);
2330 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2332 if (!BUS_IS_OPEN(bus
->state
))
2335 r
= bus_find_parent_object_manager(bus
, &object_manager
, path
);
2341 BUS_DONT_DESTROY(bus
);
2344 bus
->nodes_modified
= false;
2345 m
= sd_bus_message_unref(m
);
2347 r
= sd_bus_message_new_signal(bus
, &m
, object_manager
->path
, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
2351 r
= sd_bus_message_append_basic(m
, 'o', path
);
2355 r
= sd_bus_message_open_container(m
, 'a', "{sa{sv}}");
2359 r
= object_added_append_all(bus
, m
, path
);
2363 if (bus
->nodes_modified
)
2366 r
= sd_bus_message_close_container(m
);
2370 } while (bus
->nodes_modified
);
2372 return sd_bus_send(bus
, m
, NULL
);
2375 static int object_removed_append_all_prefix(
2381 bool require_fallback
) {
2383 const char *previous_interface
= NULL
;
2384 struct node_vtable
*c
;
2394 n
= hashmap_get(bus
->nodes
, prefix
);
2398 LIST_FOREACH(vtables
, c
, n
->vtables
) {
2399 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2402 if (require_fallback
&& !c
->is_fallback
)
2404 if (streq_ptr(c
->interface
, previous_interface
))
2407 /* If a child-node already handled this interface, we
2408 * skip it on any of its parents. The child vtables
2409 * always fully override any conflicting vtables of
2410 * any parent node. */
2411 if (set_get(s
, c
->interface
))
2414 r
= node_vtable_get_userdata(bus
, path
, c
, &u
, &error
);
2417 if (bus
->nodes_modified
)
2422 r
= set_put(s
, c
->interface
);
2426 r
= sd_bus_message_append(m
, "s", c
->interface
);
2430 previous_interface
= c
->interface
;
2436 static int object_removed_append_all(sd_bus
*bus
, sd_bus_message
*m
, const char *path
) {
2437 _cleanup_set_free_ Set
*s
= NULL
;
2445 /* see sd_bus_emit_object_added() for details */
2447 s
= set_new(&string_hash_ops
);
2451 r
= sd_bus_message_append(m
, "s", "org.freedesktop.DBus.Peer");
2454 r
= sd_bus_message_append(m
, "s", "org.freedesktop.DBus.Introspectable");
2457 r
= sd_bus_message_append(m
, "s", "org.freedesktop.DBus.Properties");
2460 r
= sd_bus_message_append(m
, "s", "org.freedesktop.DBus.ObjectManager");
2464 r
= object_removed_append_all_prefix(bus
, m
, s
, path
, path
, false);
2467 if (bus
->nodes_modified
)
2470 prefix
= alloca(strlen(path
) + 1);
2471 OBJECT_PATH_FOREACH_PREFIX(prefix
, path
) {
2472 r
= object_removed_append_all_prefix(bus
, m
, s
, prefix
, path
, true);
2475 if (bus
->nodes_modified
)
2482 _public_
int sd_bus_emit_object_removed(sd_bus
*bus
, const char *path
) {
2483 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2484 struct node
*object_manager
;
2488 * This is like sd_bus_emit_object_added(), but emits an
2489 * InterfacesRemoved signal on the given path. This only includes any
2490 * registered interfaces but skips the properties. Note that this will
2491 * call into the find() callbacks of any registered vtable. Therefore,
2492 * you must call this function before destroying/unlinking your object.
2493 * Otherwise, the list of interfaces will be incomplete. However, note
2494 * that this will *NOT* call into any property callback. Therefore, the
2495 * object might be in an "destructed" state, as long as we can find it.
2498 assert_return(bus
, -EINVAL
);
2499 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2500 assert_return(object_path_is_valid(path
), -EINVAL
);
2501 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2503 if (!BUS_IS_OPEN(bus
->state
))
2506 r
= bus_find_parent_object_manager(bus
, &object_manager
, path
);
2512 BUS_DONT_DESTROY(bus
);
2515 bus
->nodes_modified
= false;
2516 m
= sd_bus_message_unref(m
);
2518 r
= sd_bus_message_new_signal(bus
, &m
, object_manager
->path
, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
2522 r
= sd_bus_message_append_basic(m
, 'o', path
);
2526 r
= sd_bus_message_open_container(m
, 'a', "s");
2530 r
= object_removed_append_all(bus
, m
, path
);
2534 if (bus
->nodes_modified
)
2537 r
= sd_bus_message_close_container(m
);
2541 } while (bus
->nodes_modified
);
2543 return sd_bus_send(bus
, m
, NULL
);
2546 static int interfaces_added_append_one_prefix(
2551 const char *interface
,
2552 bool require_fallback
) {
2554 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2555 bool found_interface
= false;
2556 struct node_vtable
*c
;
2567 n
= hashmap_get(bus
->nodes
, prefix
);
2571 LIST_FOREACH(vtables
, c
, n
->vtables
) {
2572 if (require_fallback
&& !c
->is_fallback
)
2575 if (!streq(c
->interface
, interface
))
2578 r
= node_vtable_get_userdata(bus
, path
, c
, &u
, &error
);
2581 if (bus
->nodes_modified
)
2586 if (!found_interface
) {
2587 r
= sd_bus_message_append_basic(m
, 's', interface
);
2591 r
= sd_bus_message_open_container(m
, 'a', "{sv}");
2595 found_interface
= true;
2598 r
= vtable_append_all_properties(bus
, m
, path
, c
, u
, &error
);
2601 if (bus
->nodes_modified
)
2605 if (found_interface
) {
2606 r
= sd_bus_message_close_container(m
);
2611 return found_interface
;
2614 static int interfaces_added_append_one(
2618 const char *interface
) {
2628 r
= interfaces_added_append_one_prefix(bus
, m
, path
, path
, interface
, false);
2631 if (bus
->nodes_modified
)
2634 prefix
= alloca(strlen(path
) + 1);
2635 OBJECT_PATH_FOREACH_PREFIX(prefix
, path
) {
2636 r
= interfaces_added_append_one_prefix(bus
, m
, prefix
, path
, interface
, true);
2639 if (bus
->nodes_modified
)
2646 _public_
int sd_bus_emit_interfaces_added_strv(sd_bus
*bus
, const char *path
, char **interfaces
) {
2647 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2648 struct node
*object_manager
;
2652 assert_return(bus
, -EINVAL
);
2653 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2654 assert_return(object_path_is_valid(path
), -EINVAL
);
2655 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2657 if (!BUS_IS_OPEN(bus
->state
))
2660 if (strv_isempty(interfaces
))
2663 r
= bus_find_parent_object_manager(bus
, &object_manager
, path
);
2669 BUS_DONT_DESTROY(bus
);
2672 bus
->nodes_modified
= false;
2673 m
= sd_bus_message_unref(m
);
2675 r
= sd_bus_message_new_signal(bus
, &m
, object_manager
->path
, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
2679 r
= sd_bus_message_append_basic(m
, 'o', path
);
2683 r
= sd_bus_message_open_container(m
, 'a', "{sa{sv}}");
2687 STRV_FOREACH(i
, interfaces
) {
2688 assert_return(interface_name_is_valid(*i
), -EINVAL
);
2690 r
= sd_bus_message_open_container(m
, 'e', "sa{sv}");
2694 r
= interfaces_added_append_one(bus
, m
, path
, *i
);
2698 if (bus
->nodes_modified
)
2701 r
= sd_bus_message_close_container(m
);
2706 if (bus
->nodes_modified
)
2709 r
= sd_bus_message_close_container(m
);
2713 } while (bus
->nodes_modified
);
2715 return sd_bus_send(bus
, m
, NULL
);
2718 _public_
int sd_bus_emit_interfaces_added(sd_bus
*bus
, const char *path
, const char *interface
, ...) {
2721 assert_return(bus
, -EINVAL
);
2722 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2723 assert_return(object_path_is_valid(path
), -EINVAL
);
2724 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2726 if (!BUS_IS_OPEN(bus
->state
))
2729 interfaces
= strv_from_stdarg_alloca(interface
);
2731 return sd_bus_emit_interfaces_added_strv(bus
, path
, interfaces
);
2734 _public_
int sd_bus_emit_interfaces_removed_strv(sd_bus
*bus
, const char *path
, char **interfaces
) {
2735 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2736 struct node
*object_manager
;
2739 assert_return(bus
, -EINVAL
);
2740 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2741 assert_return(object_path_is_valid(path
), -EINVAL
);
2742 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2744 if (!BUS_IS_OPEN(bus
->state
))
2747 if (strv_isempty(interfaces
))
2750 r
= bus_find_parent_object_manager(bus
, &object_manager
, path
);
2756 r
= sd_bus_message_new_signal(bus
, &m
, object_manager
->path
, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
2760 r
= sd_bus_message_append_basic(m
, 'o', path
);
2764 r
= sd_bus_message_append_strv(m
, interfaces
);
2768 return sd_bus_send(bus
, m
, NULL
);
2771 _public_
int sd_bus_emit_interfaces_removed(sd_bus
*bus
, const char *path
, const char *interface
, ...) {
2774 assert_return(bus
, -EINVAL
);
2775 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2776 assert_return(object_path_is_valid(path
), -EINVAL
);
2777 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2779 if (!BUS_IS_OPEN(bus
->state
))
2782 interfaces
= strv_from_stdarg_alloca(interface
);
2784 return sd_bus_emit_interfaces_removed_strv(bus
, path
, interfaces
);
2787 _public_
int sd_bus_add_object_manager(sd_bus
*bus
, sd_bus_slot
**slot
, const char *path
) {
2792 assert_return(bus
, -EINVAL
);
2793 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2794 assert_return(object_path_is_valid(path
), -EINVAL
);
2795 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2797 n
= bus_node_allocate(bus
, path
);
2801 s
= bus_slot_allocate(bus
, !slot
, BUS_NODE_OBJECT_MANAGER
, sizeof(struct node_object_manager
), NULL
);
2807 s
->node_object_manager
.node
= n
;
2808 LIST_PREPEND(object_managers
, n
->object_managers
, &s
->node_object_manager
);
2809 bus
->nodes_modified
= true;
2817 sd_bus_slot_unref(s
);
2818 bus_node_gc(bus
, n
);