2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
21 #include "bus-internal.h"
22 #include "bus-introspect.h"
23 #include "bus-message.h"
24 #include "bus-objects.h"
25 #include "bus-signature.h"
30 #include "string-util.h"
33 static int node_vtable_get_userdata(
36 struct node_vtable
*c
,
38 sd_bus_error
*error
) {
48 s
= container_of(c
, sd_bus_slot
, node_vtable
);
51 bus
->current_slot
= sd_bus_slot_ref(s
);
52 bus
->current_userdata
= u
;
53 r
= c
->find(bus
, path
, c
->interface
, u
, &found_u
, error
);
54 bus
->current_userdata
= NULL
;
55 bus
->current_slot
= sd_bus_slot_unref(s
);
59 if (sd_bus_error_is_set(error
))
60 return -sd_bus_error_get_errno(error
);
72 static void *vtable_method_convert_userdata(const sd_bus_vtable
*p
, void *u
) {
75 return (uint8_t*) u
+ p
->x
.method
.offset
;
78 static void *vtable_property_convert_userdata(const sd_bus_vtable
*p
, void *u
) {
81 return (uint8_t*) u
+ p
->x
.property
.offset
;
84 static int vtable_property_get_userdata(
87 struct vtable_member
*p
,
89 sd_bus_error
*error
) {
99 r
= node_vtable_get_userdata(bus
, path
, p
->parent
, &u
, error
);
102 if (bus
->nodes_modified
)
105 *userdata
= vtable_property_convert_userdata(p
->vtable
, u
);
109 static int add_enumerated_to_set(
112 struct node_enumerator
*first
,
114 sd_bus_error
*error
) {
116 struct node_enumerator
*c
;
123 LIST_FOREACH(enumerators
, c
, first
) {
124 char **children
= NULL
, **k
;
127 if (bus
->nodes_modified
)
130 slot
= container_of(c
, sd_bus_slot
, node_enumerator
);
132 bus
->current_slot
= sd_bus_slot_ref(slot
);
133 bus
->current_userdata
= slot
->userdata
;
134 r
= c
->callback(bus
, prefix
, slot
->userdata
, &children
, error
);
135 bus
->current_userdata
= NULL
;
136 bus
->current_slot
= sd_bus_slot_unref(slot
);
140 if (sd_bus_error_is_set(error
))
141 return -sd_bus_error_get_errno(error
);
143 STRV_FOREACH(k
, children
) {
149 if (!object_path_is_valid(*k
)) {
155 if (!object_path_startswith(*k
, prefix
)) {
160 r
= set_consume(s
, *k
);
174 /* if set, add_subtree() works recursively */
175 CHILDREN_RECURSIVE
= (1U << 1),
176 /* if set, add_subtree() scans object-manager hierarchies recursively */
177 CHILDREN_SUBHIERARCHIES
= (1U << 0),
180 static int add_subtree_to_set(
186 sd_bus_error
*error
) {
196 r
= add_enumerated_to_set(bus
, prefix
, n
->enumerators
, s
, error
);
199 if (bus
->nodes_modified
)
202 LIST_FOREACH(siblings
, i
, n
->child
) {
205 if (!object_path_startswith(i
->path
, prefix
))
212 r
= set_consume(s
, t
);
213 if (r
< 0 && r
!= -EEXIST
)
216 if ((flags
& CHILDREN_RECURSIVE
) &&
217 ((flags
& CHILDREN_SUBHIERARCHIES
) || !i
->object_managers
)) {
218 r
= add_subtree_to_set(bus
, prefix
, i
, flags
, s
, error
);
221 if (bus
->nodes_modified
)
229 static int get_child_nodes(
235 sd_bus_error
*error
) {
245 s
= set_new(&string_hash_ops
);
249 r
= add_subtree_to_set(bus
, prefix
, n
, flags
, s
, error
);
259 static int node_callbacks_run(
262 struct node_callback
*first
,
263 bool require_fallback
,
264 bool *found_object
) {
266 struct node_callback
*c
;
271 assert(found_object
);
273 LIST_FOREACH(callbacks
, c
, first
) {
274 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
277 if (bus
->nodes_modified
)
280 if (require_fallback
&& !c
->is_fallback
)
283 *found_object
= true;
285 if (c
->last_iteration
== bus
->iteration_counter
)
288 c
->last_iteration
= bus
->iteration_counter
;
290 r
= sd_bus_message_rewind(m
, true);
294 slot
= container_of(c
, sd_bus_slot
, node_callback
);
296 bus
->current_slot
= sd_bus_slot_ref(slot
);
297 bus
->current_handler
= c
->callback
;
298 bus
->current_userdata
= slot
->userdata
;
299 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
300 bus
->current_userdata
= NULL
;
301 bus
->current_handler
= NULL
;
302 bus
->current_slot
= sd_bus_slot_unref(slot
);
304 r
= bus_maybe_reply_error(m
, r
, &error_buffer
);
312 #define CAPABILITY_SHIFT(x) (((x) >> __builtin_ctzll(_SD_BUS_VTABLE_CAPABILITY_MASK)) & 0xFFFF)
314 static int check_access(sd_bus
*bus
, sd_bus_message
*m
, struct vtable_member
*c
, sd_bus_error
*error
) {
322 /* If the entire bus is trusted let's grant access */
326 /* If the member is marked UNPRIVILEGED let's grant access */
327 if (c
->vtable
->flags
& SD_BUS_VTABLE_UNPRIVILEGED
)
330 /* Check have the caller has the requested capability
331 * set. Note that the flags value contains the capability
332 * number plus one, which we need to subtract here. We do this
333 * so that we have 0 as special value for "default
335 cap
= CAPABILITY_SHIFT(c
->vtable
->flags
);
337 cap
= CAPABILITY_SHIFT(c
->parent
->vtable
[0].flags
);
343 r
= sd_bus_query_sender_privilege(m
, cap
);
349 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
, "Access to %s.%s() not permitted.", c
->interface
, c
->member
);
352 static int method_callbacks_run(
355 struct vtable_member
*c
,
356 bool require_fallback
,
357 bool *found_object
) {
359 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
360 const char *signature
;
367 assert(found_object
);
369 if (require_fallback
&& !c
->parent
->is_fallback
)
372 r
= check_access(bus
, m
, c
, &error
);
374 return bus_maybe_reply_error(m
, r
, &error
);
376 r
= node_vtable_get_userdata(bus
, m
->path
, c
->parent
, &u
, &error
);
378 return bus_maybe_reply_error(m
, r
, &error
);
379 if (bus
->nodes_modified
)
382 u
= vtable_method_convert_userdata(c
->vtable
, u
);
384 *found_object
= true;
386 if (c
->last_iteration
== bus
->iteration_counter
)
389 c
->last_iteration
= bus
->iteration_counter
;
391 r
= sd_bus_message_rewind(m
, true);
395 signature
= sd_bus_message_get_signature(m
, true);
399 if (!streq(strempty(c
->vtable
->x
.method
.signature
), signature
))
400 return sd_bus_reply_method_errorf(
402 SD_BUS_ERROR_INVALID_ARGS
,
403 "Invalid arguments '%s' to call %s.%s(), expecting '%s'.",
404 signature
, c
->interface
, c
->member
, strempty(c
->vtable
->x
.method
.signature
));
406 /* Keep track what the signature of the reply to this message
407 * should be, so that this can be enforced when sealing the
409 m
->enforced_reply_signature
= strempty(c
->vtable
->x
.method
.result
);
411 if (c
->vtable
->x
.method
.handler
) {
414 slot
= container_of(c
->parent
, sd_bus_slot
, node_vtable
);
416 bus
->current_slot
= sd_bus_slot_ref(slot
);
417 bus
->current_handler
= c
->vtable
->x
.method
.handler
;
418 bus
->current_userdata
= u
;
419 r
= c
->vtable
->x
.method
.handler(m
, u
, &error
);
420 bus
->current_userdata
= NULL
;
421 bus
->current_handler
= NULL
;
422 bus
->current_slot
= sd_bus_slot_unref(slot
);
424 return bus_maybe_reply_error(m
, r
, &error
);
427 /* If the method callback is NULL, make this a successful NOP */
428 r
= sd_bus_reply_method_return(m
, NULL
);
435 static int invoke_property_get(
438 const sd_bus_vtable
*v
,
440 const char *interface
,
441 const char *property
,
442 sd_bus_message
*reply
,
444 sd_bus_error
*error
) {
457 if (v
->x
.property
.get
) {
459 bus
->current_slot
= sd_bus_slot_ref(slot
);
460 bus
->current_userdata
= userdata
;
461 r
= v
->x
.property
.get(bus
, path
, interface
, property
, reply
, userdata
, error
);
462 bus
->current_userdata
= NULL
;
463 bus
->current_slot
= sd_bus_slot_unref(slot
);
467 if (sd_bus_error_is_set(error
))
468 return -sd_bus_error_get_errno(error
);
472 /* Automatic handling if no callback is defined. */
474 if (streq(v
->x
.property
.signature
, "as"))
475 return sd_bus_message_append_strv(reply
, *(char***) userdata
);
477 assert(signature_is_single(v
->x
.property
.signature
, false));
478 assert(bus_type_is_basic(v
->x
.property
.signature
[0]));
480 switch (v
->x
.property
.signature
[0]) {
482 case SD_BUS_TYPE_STRING
:
483 case SD_BUS_TYPE_SIGNATURE
:
484 p
= strempty(*(char**) userdata
);
487 case SD_BUS_TYPE_OBJECT_PATH
:
488 p
= *(char**) userdata
;
497 return sd_bus_message_append_basic(reply
, v
->x
.property
.signature
[0], p
);
500 static int invoke_property_set(
503 const sd_bus_vtable
*v
,
505 const char *interface
,
506 const char *property
,
507 sd_bus_message
*value
,
509 sd_bus_error
*error
) {
521 if (v
->x
.property
.set
) {
523 bus
->current_slot
= sd_bus_slot_ref(slot
);
524 bus
->current_userdata
= userdata
;
525 r
= v
->x
.property
.set(bus
, path
, interface
, property
, value
, userdata
, error
);
526 bus
->current_userdata
= NULL
;
527 bus
->current_slot
= sd_bus_slot_unref(slot
);
531 if (sd_bus_error_is_set(error
))
532 return -sd_bus_error_get_errno(error
);
536 /* Automatic handling if no callback is defined. */
538 assert(signature_is_single(v
->x
.property
.signature
, false));
539 assert(bus_type_is_basic(v
->x
.property
.signature
[0]));
541 switch (v
->x
.property
.signature
[0]) {
543 case SD_BUS_TYPE_STRING
:
544 case SD_BUS_TYPE_OBJECT_PATH
:
545 case SD_BUS_TYPE_SIGNATURE
: {
549 r
= sd_bus_message_read_basic(value
, v
->x
.property
.signature
[0], &p
);
557 free(*(char**) userdata
);
558 *(char**) userdata
= n
;
564 r
= sd_bus_message_read_basic(value
, v
->x
.property
.signature
[0], userdata
);
574 static int property_get_set_callbacks_run(
577 struct vtable_member
*c
,
578 bool require_fallback
,
580 bool *found_object
) {
582 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
583 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
591 assert(found_object
);
593 if (require_fallback
&& !c
->parent
->is_fallback
)
596 r
= vtable_property_get_userdata(bus
, m
->path
, c
, &u
, &error
);
598 return bus_maybe_reply_error(m
, r
, &error
);
599 if (bus
->nodes_modified
)
602 slot
= container_of(c
->parent
, sd_bus_slot
, node_vtable
);
604 *found_object
= true;
606 r
= sd_bus_message_new_method_return(m
, &reply
);
611 /* Note that we do not protect against reexecution
612 * here (using the last_iteration check, see below),
613 * should the node tree have changed and we got called
614 * again. We assume that property Get() calls are
615 * ultimately without side-effects or if they aren't
616 * then at least idempotent. */
618 r
= sd_bus_message_open_container(reply
, 'v', c
->vtable
->x
.property
.signature
);
622 /* Note that we do not do an access check here. Read
623 * access to properties is always unrestricted, since
624 * PropertiesChanged signals broadcast contents
627 r
= invoke_property_get(bus
, slot
, c
->vtable
, m
->path
, c
->interface
, c
->member
, reply
, u
, &error
);
629 return bus_maybe_reply_error(m
, r
, &error
);
631 if (bus
->nodes_modified
)
634 r
= sd_bus_message_close_container(reply
);
639 const char *signature
= NULL
;
642 if (c
->vtable
->type
!= _SD_BUS_VTABLE_WRITABLE_PROPERTY
)
643 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_PROPERTY_READ_ONLY
, "Property '%s' is not writable.", c
->member
);
645 /* Avoid that we call the set routine more than once
646 * if the processing of this message got restarted
647 * because the node tree changed. */
648 if (c
->last_iteration
== bus
->iteration_counter
)
651 c
->last_iteration
= bus
->iteration_counter
;
653 r
= sd_bus_message_peek_type(m
, &type
, &signature
);
657 if (type
!= 'v' || !streq(strempty(signature
), strempty(c
->vtable
->x
.property
.signature
)))
658 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_INVALID_ARGS
, "Incorrect parameters for property '%s', expected '%s', got '%s'.", c
->member
, strempty(c
->vtable
->x
.property
.signature
), strempty(signature
));
660 r
= sd_bus_message_enter_container(m
, 'v', c
->vtable
->x
.property
.signature
);
664 r
= check_access(bus
, m
, c
, &error
);
666 return bus_maybe_reply_error(m
, r
, &error
);
668 r
= invoke_property_set(bus
, slot
, c
->vtable
, m
->path
, c
->interface
, c
->member
, m
, u
, &error
);
670 return bus_maybe_reply_error(m
, r
, &error
);
672 if (bus
->nodes_modified
)
675 r
= sd_bus_message_exit_container(m
);
680 r
= sd_bus_send(bus
, reply
, NULL
);
687 static int vtable_append_one_property(
689 sd_bus_message
*reply
,
691 struct node_vtable
*c
,
692 const sd_bus_vtable
*v
,
694 sd_bus_error
*error
) {
705 r
= sd_bus_message_open_container(reply
, 'e', "sv");
709 r
= sd_bus_message_append(reply
, "s", v
->x
.property
.member
);
713 r
= sd_bus_message_open_container(reply
, 'v', v
->x
.property
.signature
);
717 slot
= container_of(c
, sd_bus_slot
, node_vtable
);
719 r
= invoke_property_get(bus
, slot
, v
, path
, c
->interface
, v
->x
.property
.member
, reply
, vtable_property_convert_userdata(v
, userdata
), error
);
722 if (bus
->nodes_modified
)
725 r
= sd_bus_message_close_container(reply
);
729 r
= sd_bus_message_close_container(reply
);
736 static int vtable_append_all_properties(
738 sd_bus_message
*reply
,
740 struct node_vtable
*c
,
742 sd_bus_error
*error
) {
744 const sd_bus_vtable
*v
;
752 if (c
->vtable
[0].flags
& SD_BUS_VTABLE_HIDDEN
)
755 for (v
= c
->vtable
+1; v
->type
!= _SD_BUS_VTABLE_END
; v
++) {
756 if (!IN_SET(v
->type
, _SD_BUS_VTABLE_PROPERTY
, _SD_BUS_VTABLE_WRITABLE_PROPERTY
))
759 if (v
->flags
& SD_BUS_VTABLE_HIDDEN
)
762 if (v
->flags
& SD_BUS_VTABLE_PROPERTY_EXPLICIT
)
765 r
= vtable_append_one_property(bus
, reply
, path
, c
, v
, userdata
, error
);
768 if (bus
->nodes_modified
)
775 static int property_get_all_callbacks_run(
778 struct node_vtable
*first
,
779 bool require_fallback
,
781 bool *found_object
) {
783 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
784 struct node_vtable
*c
;
785 bool found_interface
;
790 assert(found_object
);
792 r
= sd_bus_message_new_method_return(m
, &reply
);
796 r
= sd_bus_message_open_container(reply
, 'a', "{sv}");
800 found_interface
= !iface
||
801 streq(iface
, "org.freedesktop.DBus.Properties") ||
802 streq(iface
, "org.freedesktop.DBus.Peer") ||
803 streq(iface
, "org.freedesktop.DBus.Introspectable");
805 LIST_FOREACH(vtables
, c
, first
) {
806 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
809 if (require_fallback
&& !c
->is_fallback
)
812 r
= node_vtable_get_userdata(bus
, m
->path
, c
, &u
, &error
);
814 return bus_maybe_reply_error(m
, r
, &error
);
815 if (bus
->nodes_modified
)
820 *found_object
= true;
822 if (iface
&& !streq(c
->interface
, iface
))
824 found_interface
= true;
826 r
= vtable_append_all_properties(bus
, reply
, m
->path
, c
, u
, &error
);
828 return bus_maybe_reply_error(m
, r
, &error
);
829 if (bus
->nodes_modified
)
836 if (!found_interface
) {
837 r
= sd_bus_reply_method_errorf(
839 SD_BUS_ERROR_UNKNOWN_INTERFACE
,
840 "Unknown interface '%s'.", iface
);
847 r
= sd_bus_message_close_container(reply
);
851 r
= sd_bus_send(bus
, reply
, NULL
);
858 static int bus_node_exists(
862 bool require_fallback
) {
864 struct node_vtable
*c
;
865 struct node_callback
*k
;
872 /* Tests if there's anything attached directly to this node
873 * for the specified path */
875 if (!require_fallback
&& (n
->enumerators
|| n
->object_managers
))
878 LIST_FOREACH(callbacks
, k
, n
->callbacks
) {
879 if (require_fallback
&& !k
->is_fallback
)
885 LIST_FOREACH(vtables
, c
, n
->vtables
) {
886 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
888 if (require_fallback
&& !c
->is_fallback
)
891 r
= node_vtable_get_userdata(bus
, path
, c
, NULL
, &error
);
894 if (bus
->nodes_modified
)
901 static int process_introspect(
905 bool require_fallback
,
906 bool *found_object
) {
908 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
909 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
910 _cleanup_set_free_free_ Set
*s
= NULL
;
911 const char *previous_interface
= NULL
;
912 struct introspect intro
;
913 struct node_vtable
*c
;
920 assert(found_object
);
922 r
= get_child_nodes(bus
, m
->path
, n
, 0, &s
, &error
);
924 return bus_maybe_reply_error(m
, r
, &error
);
925 if (bus
->nodes_modified
)
928 r
= introspect_begin(&intro
, bus
->trusted
);
932 r
= introspect_write_default_interfaces(&intro
, !require_fallback
&& n
->object_managers
);
936 empty
= set_isempty(s
);
938 LIST_FOREACH(vtables
, c
, n
->vtables
) {
939 if (require_fallback
&& !c
->is_fallback
)
942 r
= node_vtable_get_userdata(bus
, m
->path
, c
, NULL
, &error
);
944 r
= bus_maybe_reply_error(m
, r
, &error
);
947 if (bus
->nodes_modified
) {
956 if (c
->vtable
[0].flags
& SD_BUS_VTABLE_HIDDEN
)
959 if (!streq_ptr(previous_interface
, c
->interface
)) {
961 if (previous_interface
)
962 fputs_unlocked(" </interface>\n", intro
.f
);
964 fprintf(intro
.f
, " <interface name=\"%s\">\n", c
->interface
);
967 r
= introspect_write_interface(&intro
, c
->vtable
);
971 previous_interface
= c
->interface
;
974 if (previous_interface
)
975 fputs_unlocked(" </interface>\n", intro
.f
);
978 /* Nothing?, let's see if we exist at all, and if not
979 * refuse to do anything */
980 r
= bus_node_exists(bus
, n
, m
->path
, require_fallback
);
982 r
= bus_maybe_reply_error(m
, r
, &error
);
985 if (bus
->nodes_modified
) {
991 *found_object
= true;
993 r
= introspect_write_child_nodes(&intro
, s
, m
->path
);
997 r
= introspect_finish(&intro
, bus
, m
, &reply
);
1001 r
= sd_bus_send(bus
, reply
, NULL
);
1008 introspect_free(&intro
);
1012 static int object_manager_serialize_path(
1014 sd_bus_message
*reply
,
1017 bool require_fallback
,
1018 sd_bus_error
*error
) {
1020 const char *previous_interface
= NULL
;
1021 bool found_something
= false;
1022 struct node_vtable
*i
;
1032 n
= hashmap_get(bus
->nodes
, prefix
);
1036 LIST_FOREACH(vtables
, i
, n
->vtables
) {
1039 if (require_fallback
&& !i
->is_fallback
)
1042 r
= node_vtable_get_userdata(bus
, path
, i
, &u
, error
);
1045 if (bus
->nodes_modified
)
1050 if (!found_something
) {
1052 /* Open the object part */
1054 r
= sd_bus_message_open_container(reply
, 'e', "oa{sa{sv}}");
1058 r
= sd_bus_message_append(reply
, "o", path
);
1062 r
= sd_bus_message_open_container(reply
, 'a', "{sa{sv}}");
1066 r
= sd_bus_message_append(reply
, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
1070 r
= sd_bus_message_append(reply
, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
1074 r
= sd_bus_message_append(reply
, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
1078 r
= sd_bus_message_append(reply
, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
1082 found_something
= true;
1085 if (!streq_ptr(previous_interface
, i
->interface
)) {
1087 /* Maybe close the previous interface part */
1089 if (previous_interface
) {
1090 r
= sd_bus_message_close_container(reply
);
1094 r
= sd_bus_message_close_container(reply
);
1099 /* Open the new interface part */
1101 r
= sd_bus_message_open_container(reply
, 'e', "sa{sv}");
1105 r
= sd_bus_message_append(reply
, "s", i
->interface
);
1109 r
= sd_bus_message_open_container(reply
, 'a', "{sv}");
1114 r
= vtable_append_all_properties(bus
, reply
, path
, i
, u
, error
);
1117 if (bus
->nodes_modified
)
1120 previous_interface
= i
->interface
;
1123 if (previous_interface
) {
1124 r
= sd_bus_message_close_container(reply
);
1128 r
= sd_bus_message_close_container(reply
);
1133 if (found_something
) {
1134 r
= sd_bus_message_close_container(reply
);
1138 r
= sd_bus_message_close_container(reply
);
1146 static int object_manager_serialize_path_and_fallbacks(
1148 sd_bus_message
*reply
,
1150 sd_bus_error
*error
) {
1160 /* First, add all vtables registered for this path */
1161 r
= object_manager_serialize_path(bus
, reply
, path
, path
, false, error
);
1164 if (bus
->nodes_modified
)
1167 /* Second, add fallback vtables registered for any of the prefixes */
1168 prefix
= alloca(strlen(path
) + 1);
1169 OBJECT_PATH_FOREACH_PREFIX(prefix
, path
) {
1170 r
= object_manager_serialize_path(bus
, reply
, prefix
, path
, true, error
);
1173 if (bus
->nodes_modified
)
1180 static int process_get_managed_objects(
1184 bool require_fallback
,
1185 bool *found_object
) {
1187 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1188 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1189 _cleanup_set_free_free_ Set
*s
= NULL
;
1197 assert(found_object
);
1199 /* Spec says, GetManagedObjects() is only implemented on the root of a
1200 * sub-tree. Therefore, we require a registered object-manager on
1201 * exactly the queried path, otherwise, we refuse to respond. */
1203 if (require_fallback
|| !n
->object_managers
)
1206 r
= get_child_nodes(bus
, m
->path
, n
, CHILDREN_RECURSIVE
, &s
, &error
);
1208 return bus_maybe_reply_error(m
, r
, &error
);
1209 if (bus
->nodes_modified
)
1212 r
= sd_bus_message_new_method_return(m
, &reply
);
1216 r
= sd_bus_message_open_container(reply
, 'a', "{oa{sa{sv}}}");
1220 SET_FOREACH(path
, s
, i
) {
1221 r
= object_manager_serialize_path_and_fallbacks(bus
, reply
, path
, &error
);
1223 return bus_maybe_reply_error(m
, r
, &error
);
1225 if (bus
->nodes_modified
)
1229 r
= sd_bus_message_close_container(reply
);
1233 r
= sd_bus_send(bus
, reply
, NULL
);
1240 static int object_find_and_run(
1244 bool require_fallback
,
1245 bool *found_object
) {
1248 struct vtable_member vtable_key
, *v
;
1254 assert(found_object
);
1256 n
= hashmap_get(bus
->nodes
, p
);
1260 /* First, try object callbacks */
1261 r
= node_callbacks_run(bus
, m
, n
->callbacks
, require_fallback
, found_object
);
1264 if (bus
->nodes_modified
)
1267 if (!m
->interface
|| !m
->member
)
1270 /* Then, look for a known method */
1271 vtable_key
.path
= (char*) p
;
1272 vtable_key
.interface
= m
->interface
;
1273 vtable_key
.member
= m
->member
;
1275 v
= hashmap_get(bus
->vtable_methods
, &vtable_key
);
1277 r
= method_callbacks_run(bus
, m
, v
, require_fallback
, found_object
);
1280 if (bus
->nodes_modified
)
1284 /* Then, look for a known property */
1285 if (streq(m
->interface
, "org.freedesktop.DBus.Properties")) {
1288 get
= streq(m
->member
, "Get");
1290 if (get
|| streq(m
->member
, "Set")) {
1292 r
= sd_bus_message_rewind(m
, true);
1296 vtable_key
.path
= (char*) p
;
1298 r
= sd_bus_message_read(m
, "ss", &vtable_key
.interface
, &vtable_key
.member
);
1300 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_INVALID_ARGS
, "Expected interface and member parameters");
1302 v
= hashmap_get(bus
->vtable_properties
, &vtable_key
);
1304 r
= property_get_set_callbacks_run(bus
, m
, v
, require_fallback
, get
, found_object
);
1309 } else if (streq(m
->member
, "GetAll")) {
1312 r
= sd_bus_message_rewind(m
, true);
1316 r
= sd_bus_message_read(m
, "s", &iface
);
1318 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_INVALID_ARGS
, "Expected interface parameter");
1323 r
= property_get_all_callbacks_run(bus
, m
, n
->vtables
, require_fallback
, iface
, found_object
);
1328 } else if (sd_bus_message_is_method_call(m
, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1330 if (!isempty(sd_bus_message_get_signature(m
, true)))
1331 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_INVALID_ARGS
, "Expected no parameters");
1333 r
= process_introspect(bus
, m
, n
, require_fallback
, found_object
);
1337 } else if (sd_bus_message_is_method_call(m
, "org.freedesktop.DBus.ObjectManager", "GetManagedObjects")) {
1339 if (!isempty(sd_bus_message_get_signature(m
, true)))
1340 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_INVALID_ARGS
, "Expected no parameters");
1342 r
= process_get_managed_objects(bus
, m
, n
, require_fallback
, found_object
);
1347 if (bus
->nodes_modified
)
1350 if (!*found_object
) {
1351 r
= bus_node_exists(bus
, n
, m
->path
, require_fallback
);
1353 return bus_maybe_reply_error(m
, r
, NULL
);
1354 if (bus
->nodes_modified
)
1357 *found_object
= true;
1363 int bus_process_object(sd_bus
*bus
, sd_bus_message
*m
) {
1366 bool found_object
= false;
1371 if (bus
->hello_flags
& KDBUS_HELLO_MONITOR
)
1374 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_CALL
)
1377 if (hashmap_isempty(bus
->nodes
))
1380 /* Never respond to broadcast messages */
1381 if (bus
->bus_client
&& !m
->destination
)
1387 pl
= strlen(m
->path
);
1391 bus
->nodes_modified
= false;
1393 r
= object_find_and_run(bus
, m
, m
->path
, false, &found_object
);
1397 /* Look for fallback prefixes */
1398 OBJECT_PATH_FOREACH_PREFIX(prefix
, m
->path
) {
1400 if (bus
->nodes_modified
)
1403 r
= object_find_and_run(bus
, m
, prefix
, true, &found_object
);
1408 } while (bus
->nodes_modified
);
1413 if (sd_bus_message_is_method_call(m
, "org.freedesktop.DBus.Properties", "Get") ||
1414 sd_bus_message_is_method_call(m
, "org.freedesktop.DBus.Properties", "Set"))
1415 r
= sd_bus_reply_method_errorf(
1417 SD_BUS_ERROR_UNKNOWN_PROPERTY
,
1418 "Unknown property or interface.");
1420 r
= sd_bus_reply_method_errorf(
1422 SD_BUS_ERROR_UNKNOWN_METHOD
,
1423 "Unknown method '%s' or interface '%s'.", m
->member
, m
->interface
);
1431 static struct node
*bus_node_allocate(sd_bus
*bus
, const char *path
) {
1432 struct node
*n
, *parent
;
1434 _cleanup_free_
char *s
= NULL
;
1440 assert(path
[0] == '/');
1442 n
= hashmap_get(bus
->nodes
, path
);
1446 r
= hashmap_ensure_allocated(&bus
->nodes
, &string_hash_ops
);
1454 if (streq(path
, "/"))
1457 e
= strrchr(path
, '/');
1460 p
= strndupa(path
, MAX(1, e
- path
));
1462 parent
= bus_node_allocate(bus
, p
);
1467 n
= new0(struct node
, 1);
1473 s
= NULL
; /* do not free */
1475 r
= hashmap_put(bus
->nodes
, n
->path
, n
);
1483 LIST_PREPEND(siblings
, parent
->child
, n
);
1488 void bus_node_gc(sd_bus
*b
, struct node
*n
) {
1501 assert(hashmap_remove(b
->nodes
, n
->path
) == n
);
1504 LIST_REMOVE(siblings
, n
->parent
->child
, n
);
1507 bus_node_gc(b
, n
->parent
);
1511 static int bus_find_parent_object_manager(sd_bus
*bus
, struct node
**out
, const char *path
) {
1517 n
= hashmap_get(bus
->nodes
, path
);
1521 prefix
= alloca(strlen(path
) + 1);
1522 OBJECT_PATH_FOREACH_PREFIX(prefix
, path
) {
1523 n
= hashmap_get(bus
->nodes
, prefix
);
1529 while (n
&& !n
->object_managers
)
1537 static int bus_add_object(
1542 sd_bus_message_handler_t callback
,
1549 assert_return(bus
, -EINVAL
);
1550 assert_return(object_path_is_valid(path
), -EINVAL
);
1551 assert_return(callback
, -EINVAL
);
1552 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1554 n
= bus_node_allocate(bus
, path
);
1558 s
= bus_slot_allocate(bus
, !slot
, BUS_NODE_CALLBACK
, sizeof(struct node_callback
), userdata
);
1564 s
->node_callback
.callback
= callback
;
1565 s
->node_callback
.is_fallback
= fallback
;
1567 s
->node_callback
.node
= n
;
1568 LIST_PREPEND(callbacks
, n
->callbacks
, &s
->node_callback
);
1569 bus
->nodes_modified
= true;
1577 sd_bus_slot_unref(s
);
1578 bus_node_gc(bus
, n
);
1583 _public_
int sd_bus_add_object(
1587 sd_bus_message_handler_t callback
,
1590 return bus_add_object(bus
, slot
, false, path
, callback
, userdata
);
1593 _public_
int sd_bus_add_fallback(
1597 sd_bus_message_handler_t callback
,
1600 return bus_add_object(bus
, slot
, true, prefix
, callback
, userdata
);
1603 static void vtable_member_hash_func(const void *a
, struct siphash
*state
) {
1604 const struct vtable_member
*m
= a
;
1608 string_hash_func(m
->path
, state
);
1609 string_hash_func(m
->interface
, state
);
1610 string_hash_func(m
->member
, state
);
1613 static int vtable_member_compare_func(const void *a
, const void *b
) {
1614 const struct vtable_member
*x
= a
, *y
= b
;
1620 r
= strcmp(x
->path
, y
->path
);
1624 r
= strcmp(x
->interface
, y
->interface
);
1628 return strcmp(x
->member
, y
->member
);
1631 static const struct hash_ops vtable_member_hash_ops
= {
1632 .hash
= vtable_member_hash_func
,
1633 .compare
= vtable_member_compare_func
1636 static int add_object_vtable_internal(
1640 const char *interface
,
1641 const sd_bus_vtable
*vtable
,
1643 sd_bus_object_find_t find
,
1646 sd_bus_slot
*s
= NULL
;
1647 struct node_vtable
*i
, *existing
= NULL
;
1648 const sd_bus_vtable
*v
;
1652 assert_return(bus
, -EINVAL
);
1653 assert_return(object_path_is_valid(path
), -EINVAL
);
1654 assert_return(interface_name_is_valid(interface
), -EINVAL
);
1655 assert_return(vtable
, -EINVAL
);
1656 assert_return(vtable
[0].type
== _SD_BUS_VTABLE_START
, -EINVAL
);
1657 assert_return(vtable
[0].x
.start
.element_size
== sizeof(struct sd_bus_vtable
), -EINVAL
);
1658 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1659 assert_return(!streq(interface
, "org.freedesktop.DBus.Properties") &&
1660 !streq(interface
, "org.freedesktop.DBus.Introspectable") &&
1661 !streq(interface
, "org.freedesktop.DBus.Peer") &&
1662 !streq(interface
, "org.freedesktop.DBus.ObjectManager"), -EINVAL
);
1664 r
= hashmap_ensure_allocated(&bus
->vtable_methods
, &vtable_member_hash_ops
);
1668 r
= hashmap_ensure_allocated(&bus
->vtable_properties
, &vtable_member_hash_ops
);
1672 n
= bus_node_allocate(bus
, path
);
1676 LIST_FOREACH(vtables
, i
, n
->vtables
) {
1677 if (i
->is_fallback
!= fallback
) {
1682 if (streq(i
->interface
, interface
)) {
1684 if (i
->vtable
== vtable
) {
1693 s
= bus_slot_allocate(bus
, !slot
, BUS_NODE_VTABLE
, sizeof(struct node_vtable
), userdata
);
1699 s
->node_vtable
.is_fallback
= fallback
;
1700 s
->node_vtable
.vtable
= vtable
;
1701 s
->node_vtable
.find
= find
;
1703 s
->node_vtable
.interface
= strdup(interface
);
1704 if (!s
->node_vtable
.interface
) {
1709 for (v
= s
->node_vtable
.vtable
+1; v
->type
!= _SD_BUS_VTABLE_END
; v
++) {
1713 case _SD_BUS_VTABLE_METHOD
: {
1714 struct vtable_member
*m
;
1716 if (!member_name_is_valid(v
->x
.method
.member
) ||
1717 !signature_is_valid(strempty(v
->x
.method
.signature
), false) ||
1718 !signature_is_valid(strempty(v
->x
.method
.result
), false) ||
1719 !(v
->x
.method
.handler
|| (isempty(v
->x
.method
.signature
) && isempty(v
->x
.method
.result
))) ||
1720 v
->flags
& (SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
)) {
1725 m
= new0(struct vtable_member
, 1);
1731 m
->parent
= &s
->node_vtable
;
1733 m
->interface
= s
->node_vtable
.interface
;
1734 m
->member
= v
->x
.method
.member
;
1737 r
= hashmap_put(bus
->vtable_methods
, m
, m
);
1746 case _SD_BUS_VTABLE_WRITABLE_PROPERTY
:
1748 if (!(v
->x
.property
.set
|| bus_type_is_basic(v
->x
.property
.signature
[0]))) {
1753 if (v
->flags
& SD_BUS_VTABLE_PROPERTY_CONST
) {
1760 case _SD_BUS_VTABLE_PROPERTY
: {
1761 struct vtable_member
*m
;
1763 if (!member_name_is_valid(v
->x
.property
.member
) ||
1764 !signature_is_single(v
->x
.property
.signature
, false) ||
1765 !(v
->x
.property
.get
|| bus_type_is_basic(v
->x
.property
.signature
[0]) || streq(v
->x
.property
.signature
, "as")) ||
1766 (v
->flags
& SD_BUS_VTABLE_METHOD_NO_REPLY
) ||
1767 (!!(v
->flags
& SD_BUS_VTABLE_PROPERTY_CONST
) + !!(v
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
) + !!(v
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
)) > 1 ||
1768 ((v
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
) && (v
->flags
& SD_BUS_VTABLE_PROPERTY_EXPLICIT
)) ||
1769 (v
->flags
& SD_BUS_VTABLE_UNPRIVILEGED
&& v
->type
== _SD_BUS_VTABLE_PROPERTY
)) {
1774 m
= new0(struct vtable_member
, 1);
1780 m
->parent
= &s
->node_vtable
;
1782 m
->interface
= s
->node_vtable
.interface
;
1783 m
->member
= v
->x
.property
.member
;
1786 r
= hashmap_put(bus
->vtable_properties
, m
, m
);
1795 case _SD_BUS_VTABLE_SIGNAL
:
1797 if (!member_name_is_valid(v
->x
.signal
.member
) ||
1798 !signature_is_valid(strempty(v
->x
.signal
.signature
), false) ||
1799 v
->flags
& SD_BUS_VTABLE_UNPRIVILEGED
) {
1812 s
->node_vtable
.node
= n
;
1813 LIST_INSERT_AFTER(vtables
, n
->vtables
, existing
, &s
->node_vtable
);
1814 bus
->nodes_modified
= true;
1822 sd_bus_slot_unref(s
);
1823 bus_node_gc(bus
, n
);
1828 _public_
int sd_bus_add_object_vtable(
1832 const char *interface
,
1833 const sd_bus_vtable
*vtable
,
1836 return add_object_vtable_internal(bus
, slot
, path
, interface
, vtable
, false, NULL
, userdata
);
1839 _public_
int sd_bus_add_fallback_vtable(
1843 const char *interface
,
1844 const sd_bus_vtable
*vtable
,
1845 sd_bus_object_find_t find
,
1848 return add_object_vtable_internal(bus
, slot
, prefix
, interface
, vtable
, true, find
, userdata
);
1851 _public_
int sd_bus_add_node_enumerator(
1855 sd_bus_node_enumerator_t callback
,
1862 assert_return(bus
, -EINVAL
);
1863 assert_return(object_path_is_valid(path
), -EINVAL
);
1864 assert_return(callback
, -EINVAL
);
1865 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1867 n
= bus_node_allocate(bus
, path
);
1871 s
= bus_slot_allocate(bus
, !slot
, BUS_NODE_ENUMERATOR
, sizeof(struct node_enumerator
), userdata
);
1877 s
->node_enumerator
.callback
= callback
;
1879 s
->node_enumerator
.node
= n
;
1880 LIST_PREPEND(enumerators
, n
->enumerators
, &s
->node_enumerator
);
1881 bus
->nodes_modified
= true;
1889 sd_bus_slot_unref(s
);
1890 bus_node_gc(bus
, n
);
1895 static int emit_properties_changed_on_interface(
1899 const char *interface
,
1900 bool require_fallback
,
1901 bool *found_interface
,
1904 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1905 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1906 bool has_invalidating
= false, has_changing
= false;
1907 struct vtable_member key
= {};
1908 struct node_vtable
*c
;
1918 assert(found_interface
);
1920 n
= hashmap_get(bus
->nodes
, prefix
);
1924 r
= sd_bus_message_new_signal(bus
, &m
, path
, "org.freedesktop.DBus.Properties", "PropertiesChanged");
1928 r
= sd_bus_message_append(m
, "s", interface
);
1932 r
= sd_bus_message_open_container(m
, 'a', "{sv}");
1937 key
.interface
= interface
;
1939 LIST_FOREACH(vtables
, c
, n
->vtables
) {
1940 if (require_fallback
&& !c
->is_fallback
)
1943 if (!streq(c
->interface
, interface
))
1946 r
= node_vtable_get_userdata(bus
, path
, c
, &u
, &error
);
1949 if (bus
->nodes_modified
)
1954 *found_interface
= true;
1957 /* If the caller specified a list of
1958 * properties we include exactly those in the
1959 * PropertiesChanged message */
1961 STRV_FOREACH(property
, names
) {
1962 struct vtable_member
*v
;
1964 assert_return(member_name_is_valid(*property
), -EINVAL
);
1966 key
.member
= *property
;
1967 v
= hashmap_get(bus
->vtable_properties
, &key
);
1971 /* If there are two vtables for the same
1972 * interface, let's handle this property when
1973 * we come to that vtable. */
1977 assert_return(v
->vtable
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
||
1978 v
->vtable
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
, -EDOM
);
1980 assert_return(!(v
->vtable
->flags
& SD_BUS_VTABLE_HIDDEN
), -EDOM
);
1982 if (v
->vtable
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
) {
1983 has_invalidating
= true;
1987 has_changing
= true;
1989 r
= vtable_append_one_property(bus
, m
, m
->path
, c
, v
->vtable
, u
, &error
);
1992 if (bus
->nodes_modified
)
1996 const sd_bus_vtable
*v
;
1998 /* If the caller specified no properties list
1999 * we include all properties that are marked
2000 * as changing in the message. */
2002 for (v
= c
->vtable
+1; v
->type
!= _SD_BUS_VTABLE_END
; v
++) {
2003 if (!IN_SET(v
->type
, _SD_BUS_VTABLE_PROPERTY
, _SD_BUS_VTABLE_WRITABLE_PROPERTY
))
2006 if (v
->flags
& SD_BUS_VTABLE_HIDDEN
)
2009 if (v
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
) {
2010 has_invalidating
= true;
2014 if (!(v
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
))
2017 has_changing
= true;
2019 r
= vtable_append_one_property(bus
, m
, m
->path
, c
, v
, u
, &error
);
2022 if (bus
->nodes_modified
)
2028 if (!has_invalidating
&& !has_changing
)
2031 r
= sd_bus_message_close_container(m
);
2035 r
= sd_bus_message_open_container(m
, 'a', "s");
2039 if (has_invalidating
) {
2040 LIST_FOREACH(vtables
, c
, n
->vtables
) {
2041 if (require_fallback
&& !c
->is_fallback
)
2044 if (!streq(c
->interface
, interface
))
2047 r
= node_vtable_get_userdata(bus
, path
, c
, &u
, &error
);
2050 if (bus
->nodes_modified
)
2056 STRV_FOREACH(property
, names
) {
2057 struct vtable_member
*v
;
2059 key
.member
= *property
;
2060 assert_se(v
= hashmap_get(bus
->vtable_properties
, &key
));
2061 assert(c
== v
->parent
);
2063 if (!(v
->vtable
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
))
2066 r
= sd_bus_message_append(m
, "s", *property
);
2071 const sd_bus_vtable
*v
;
2073 for (v
= c
->vtable
+1; v
->type
!= _SD_BUS_VTABLE_END
; v
++) {
2074 if (!IN_SET(v
->type
, _SD_BUS_VTABLE_PROPERTY
, _SD_BUS_VTABLE_WRITABLE_PROPERTY
))
2077 if (v
->flags
& SD_BUS_VTABLE_HIDDEN
)
2080 if (!(v
->flags
& SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
))
2083 r
= sd_bus_message_append(m
, "s", v
->x
.property
.member
);
2091 r
= sd_bus_message_close_container(m
);
2095 r
= sd_bus_send(bus
, m
, NULL
);
2102 _public_
int sd_bus_emit_properties_changed_strv(
2105 const char *interface
,
2108 BUS_DONT_DESTROY(bus
);
2109 bool found_interface
= false;
2113 assert_return(bus
, -EINVAL
);
2114 assert_return(object_path_is_valid(path
), -EINVAL
);
2115 assert_return(interface_name_is_valid(interface
), -EINVAL
);
2116 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2118 if (!BUS_IS_OPEN(bus
->state
))
2121 /* A non-NULL but empty names list means nothing needs to be
2122 generated. A NULL list OTOH indicates that all properties
2123 that are set to EMITS_CHANGE or EMITS_INVALIDATION shall be
2124 included in the PropertiesChanged message. */
2125 if (names
&& names
[0] == NULL
)
2129 bus
->nodes_modified
= false;
2131 r
= emit_properties_changed_on_interface(bus
, path
, path
, interface
, false, &found_interface
, names
);
2134 if (bus
->nodes_modified
)
2137 prefix
= alloca(strlen(path
) + 1);
2138 OBJECT_PATH_FOREACH_PREFIX(prefix
, path
) {
2139 r
= emit_properties_changed_on_interface(bus
, prefix
, path
, interface
, true, &found_interface
, names
);
2142 if (bus
->nodes_modified
)
2146 } while (bus
->nodes_modified
);
2148 return found_interface
? 0 : -ENOENT
;
2151 _public_
int sd_bus_emit_properties_changed(
2154 const char *interface
,
2155 const char *name
, ...) {
2159 assert_return(bus
, -EINVAL
);
2160 assert_return(object_path_is_valid(path
), -EINVAL
);
2161 assert_return(interface_name_is_valid(interface
), -EINVAL
);
2162 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2164 if (!BUS_IS_OPEN(bus
->state
))
2170 names
= strv_from_stdarg_alloca(name
);
2172 return sd_bus_emit_properties_changed_strv(bus
, path
, interface
, names
);
2175 static int object_added_append_all_prefix(
2181 bool require_fallback
) {
2183 const char *previous_interface
= NULL
;
2184 struct node_vtable
*c
;
2194 n
= hashmap_get(bus
->nodes
, prefix
);
2198 LIST_FOREACH(vtables
, c
, n
->vtables
) {
2199 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2202 if (require_fallback
&& !c
->is_fallback
)
2205 r
= node_vtable_get_userdata(bus
, path
, c
, &u
, &error
);
2208 if (bus
->nodes_modified
)
2213 if (!streq_ptr(c
->interface
, previous_interface
)) {
2214 /* If a child-node already handled this interface, we
2215 * skip it on any of its parents. The child vtables
2216 * always fully override any conflicting vtables of
2217 * any parent node. */
2218 if (set_get(s
, c
->interface
))
2221 r
= set_put(s
, c
->interface
);
2225 if (previous_interface
) {
2226 r
= sd_bus_message_close_container(m
);
2229 r
= sd_bus_message_close_container(m
);
2234 r
= sd_bus_message_open_container(m
, 'e', "sa{sv}");
2237 r
= sd_bus_message_append(m
, "s", c
->interface
);
2240 r
= sd_bus_message_open_container(m
, 'a', "{sv}");
2244 previous_interface
= c
->interface
;
2247 r
= vtable_append_all_properties(bus
, m
, path
, c
, u
, &error
);
2250 if (bus
->nodes_modified
)
2254 if (previous_interface
) {
2255 r
= sd_bus_message_close_container(m
);
2258 r
= sd_bus_message_close_container(m
);
2266 static int object_added_append_all(sd_bus
*bus
, sd_bus_message
*m
, const char *path
) {
2267 _cleanup_set_free_ Set
*s
= NULL
;
2276 * This appends all interfaces registered on path @path. We first add
2277 * the builtin interfaces, which are always available and handled by
2278 * sd-bus. Then, we add all interfaces registered on the exact node,
2279 * followed by all fallback interfaces registered on any parent prefix.
2281 * If an interface is registered multiple times on the same node with
2282 * different vtables, we merge all the properties across all vtables.
2283 * However, if a child node has the same interface registered as one of
2284 * its parent nodes has as fallback, we make the child overwrite the
2285 * parent instead of extending it. Therefore, we keep a "Set" of all
2286 * handled interfaces during parent traversal, so we skip interfaces on
2287 * a parent that were overwritten by a child.
2290 s
= set_new(&string_hash_ops
);
2294 r
= sd_bus_message_append(m
, "{sa{sv}}", "org.freedesktop.DBus.Peer", 0);
2297 r
= sd_bus_message_append(m
, "{sa{sv}}", "org.freedesktop.DBus.Introspectable", 0);
2300 r
= sd_bus_message_append(m
, "{sa{sv}}", "org.freedesktop.DBus.Properties", 0);
2303 r
= sd_bus_message_append(m
, "{sa{sv}}", "org.freedesktop.DBus.ObjectManager", 0);
2307 r
= object_added_append_all_prefix(bus
, m
, s
, path
, path
, false);
2310 if (bus
->nodes_modified
)
2313 prefix
= alloca(strlen(path
) + 1);
2314 OBJECT_PATH_FOREACH_PREFIX(prefix
, path
) {
2315 r
= object_added_append_all_prefix(bus
, m
, s
, prefix
, path
, true);
2318 if (bus
->nodes_modified
)
2325 _public_
int sd_bus_emit_object_added(sd_bus
*bus
, const char *path
) {
2326 BUS_DONT_DESTROY(bus
);
2328 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2329 struct node
*object_manager
;
2333 * This emits an InterfacesAdded signal on the given path, by iterating
2334 * all registered vtables and fallback vtables on the path. All
2335 * properties are queried and included in the signal.
2336 * This call is equivalent to sd_bus_emit_interfaces_added() with an
2337 * explicit list of registered interfaces. However, unlike
2338 * interfaces_added(), this call can figure out the list of supported
2339 * interfaces itself. Furthermore, it properly adds the builtin
2340 * org.freedesktop.DBus.* interfaces.
2343 assert_return(bus
, -EINVAL
);
2344 assert_return(object_path_is_valid(path
), -EINVAL
);
2345 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2347 if (!BUS_IS_OPEN(bus
->state
))
2350 r
= bus_find_parent_object_manager(bus
, &object_manager
, path
);
2357 bus
->nodes_modified
= false;
2358 m
= sd_bus_message_unref(m
);
2360 r
= sd_bus_message_new_signal(bus
, &m
, object_manager
->path
, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
2364 r
= sd_bus_message_append_basic(m
, 'o', path
);
2368 r
= sd_bus_message_open_container(m
, 'a', "{sa{sv}}");
2372 r
= object_added_append_all(bus
, m
, path
);
2376 if (bus
->nodes_modified
)
2379 r
= sd_bus_message_close_container(m
);
2383 } while (bus
->nodes_modified
);
2385 return sd_bus_send(bus
, m
, NULL
);
2388 static int object_removed_append_all_prefix(
2394 bool require_fallback
) {
2396 const char *previous_interface
= NULL
;
2397 struct node_vtable
*c
;
2407 n
= hashmap_get(bus
->nodes
, prefix
);
2411 LIST_FOREACH(vtables
, c
, n
->vtables
) {
2412 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2415 if (require_fallback
&& !c
->is_fallback
)
2417 if (streq_ptr(c
->interface
, previous_interface
))
2420 /* If a child-node already handled this interface, we
2421 * skip it on any of its parents. The child vtables
2422 * always fully override any conflicting vtables of
2423 * any parent node. */
2424 if (set_get(s
, c
->interface
))
2427 r
= node_vtable_get_userdata(bus
, path
, c
, &u
, &error
);
2430 if (bus
->nodes_modified
)
2435 r
= set_put(s
, c
->interface
);
2439 r
= sd_bus_message_append(m
, "s", c
->interface
);
2443 previous_interface
= c
->interface
;
2449 static int object_removed_append_all(sd_bus
*bus
, sd_bus_message
*m
, const char *path
) {
2450 _cleanup_set_free_ Set
*s
= NULL
;
2458 /* see sd_bus_emit_object_added() for details */
2460 s
= set_new(&string_hash_ops
);
2464 r
= sd_bus_message_append(m
, "s", "org.freedesktop.DBus.Peer");
2467 r
= sd_bus_message_append(m
, "s", "org.freedesktop.DBus.Introspectable");
2470 r
= sd_bus_message_append(m
, "s", "org.freedesktop.DBus.Properties");
2473 r
= sd_bus_message_append(m
, "s", "org.freedesktop.DBus.ObjectManager");
2477 r
= object_removed_append_all_prefix(bus
, m
, s
, path
, path
, false);
2480 if (bus
->nodes_modified
)
2483 prefix
= alloca(strlen(path
) + 1);
2484 OBJECT_PATH_FOREACH_PREFIX(prefix
, path
) {
2485 r
= object_removed_append_all_prefix(bus
, m
, s
, prefix
, path
, true);
2488 if (bus
->nodes_modified
)
2495 _public_
int sd_bus_emit_object_removed(sd_bus
*bus
, const char *path
) {
2496 BUS_DONT_DESTROY(bus
);
2498 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2499 struct node
*object_manager
;
2503 * This is like sd_bus_emit_object_added(), but emits an
2504 * InterfacesRemoved signal on the given path. This only includes any
2505 * registered interfaces but skips the properties. Note that this will
2506 * call into the find() callbacks of any registered vtable. Therefore,
2507 * you must call this function before destroying/unlinking your object.
2508 * Otherwise, the list of interfaces will be incomplete. However, note
2509 * that this will *NOT* call into any property callback. Therefore, the
2510 * object might be in an "destructed" state, as long as we can find it.
2513 assert_return(bus
, -EINVAL
);
2514 assert_return(object_path_is_valid(path
), -EINVAL
);
2515 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2517 if (!BUS_IS_OPEN(bus
->state
))
2520 r
= bus_find_parent_object_manager(bus
, &object_manager
, path
);
2527 bus
->nodes_modified
= false;
2528 m
= sd_bus_message_unref(m
);
2530 r
= sd_bus_message_new_signal(bus
, &m
, object_manager
->path
, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
2534 r
= sd_bus_message_append_basic(m
, 'o', path
);
2538 r
= sd_bus_message_open_container(m
, 'a', "s");
2542 r
= object_removed_append_all(bus
, m
, path
);
2546 if (bus
->nodes_modified
)
2549 r
= sd_bus_message_close_container(m
);
2553 } while (bus
->nodes_modified
);
2555 return sd_bus_send(bus
, m
, NULL
);
2558 static int interfaces_added_append_one_prefix(
2563 const char *interface
,
2564 bool require_fallback
) {
2566 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2567 bool found_interface
= false;
2568 struct node_vtable
*c
;
2579 n
= hashmap_get(bus
->nodes
, prefix
);
2583 LIST_FOREACH(vtables
, c
, n
->vtables
) {
2584 if (require_fallback
&& !c
->is_fallback
)
2587 if (!streq(c
->interface
, interface
))
2590 r
= node_vtable_get_userdata(bus
, path
, c
, &u
, &error
);
2593 if (bus
->nodes_modified
)
2598 if (!found_interface
) {
2599 r
= sd_bus_message_append_basic(m
, 's', interface
);
2603 r
= sd_bus_message_open_container(m
, 'a', "{sv}");
2607 found_interface
= true;
2610 r
= vtable_append_all_properties(bus
, m
, path
, c
, u
, &error
);
2613 if (bus
->nodes_modified
)
2617 if (found_interface
) {
2618 r
= sd_bus_message_close_container(m
);
2623 return found_interface
;
2626 static int interfaces_added_append_one(
2630 const char *interface
) {
2640 r
= interfaces_added_append_one_prefix(bus
, m
, path
, path
, interface
, false);
2643 if (bus
->nodes_modified
)
2646 prefix
= alloca(strlen(path
) + 1);
2647 OBJECT_PATH_FOREACH_PREFIX(prefix
, path
) {
2648 r
= interfaces_added_append_one_prefix(bus
, m
, prefix
, path
, interface
, true);
2651 if (bus
->nodes_modified
)
2658 _public_
int sd_bus_emit_interfaces_added_strv(sd_bus
*bus
, const char *path
, char **interfaces
) {
2659 BUS_DONT_DESTROY(bus
);
2661 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2662 struct node
*object_manager
;
2666 assert_return(bus
, -EINVAL
);
2667 assert_return(object_path_is_valid(path
), -EINVAL
);
2668 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2670 if (!BUS_IS_OPEN(bus
->state
))
2673 if (strv_isempty(interfaces
))
2676 r
= bus_find_parent_object_manager(bus
, &object_manager
, path
);
2683 bus
->nodes_modified
= false;
2684 m
= sd_bus_message_unref(m
);
2686 r
= sd_bus_message_new_signal(bus
, &m
, object_manager
->path
, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded");
2690 r
= sd_bus_message_append_basic(m
, 'o', path
);
2694 r
= sd_bus_message_open_container(m
, 'a', "{sa{sv}}");
2698 STRV_FOREACH(i
, interfaces
) {
2699 assert_return(interface_name_is_valid(*i
), -EINVAL
);
2701 r
= sd_bus_message_open_container(m
, 'e', "sa{sv}");
2705 r
= interfaces_added_append_one(bus
, m
, path
, *i
);
2709 if (bus
->nodes_modified
)
2712 r
= sd_bus_message_close_container(m
);
2717 if (bus
->nodes_modified
)
2720 r
= sd_bus_message_close_container(m
);
2724 } while (bus
->nodes_modified
);
2726 return sd_bus_send(bus
, m
, NULL
);
2729 _public_
int sd_bus_emit_interfaces_added(sd_bus
*bus
, const char *path
, const char *interface
, ...) {
2732 assert_return(bus
, -EINVAL
);
2733 assert_return(object_path_is_valid(path
), -EINVAL
);
2734 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2736 if (!BUS_IS_OPEN(bus
->state
))
2739 interfaces
= strv_from_stdarg_alloca(interface
);
2741 return sd_bus_emit_interfaces_added_strv(bus
, path
, interfaces
);
2744 _public_
int sd_bus_emit_interfaces_removed_strv(sd_bus
*bus
, const char *path
, char **interfaces
) {
2745 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2746 struct node
*object_manager
;
2749 assert_return(bus
, -EINVAL
);
2750 assert_return(object_path_is_valid(path
), -EINVAL
);
2751 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2753 if (!BUS_IS_OPEN(bus
->state
))
2756 if (strv_isempty(interfaces
))
2759 r
= bus_find_parent_object_manager(bus
, &object_manager
, path
);
2765 r
= sd_bus_message_new_signal(bus
, &m
, object_manager
->path
, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved");
2769 r
= sd_bus_message_append_basic(m
, 'o', path
);
2773 r
= sd_bus_message_append_strv(m
, interfaces
);
2777 return sd_bus_send(bus
, m
, NULL
);
2780 _public_
int sd_bus_emit_interfaces_removed(sd_bus
*bus
, const char *path
, const char *interface
, ...) {
2783 assert_return(bus
, -EINVAL
);
2784 assert_return(object_path_is_valid(path
), -EINVAL
);
2785 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2787 if (!BUS_IS_OPEN(bus
->state
))
2790 interfaces
= strv_from_stdarg_alloca(interface
);
2792 return sd_bus_emit_interfaces_removed_strv(bus
, path
, interfaces
);
2795 _public_
int sd_bus_add_object_manager(sd_bus
*bus
, sd_bus_slot
**slot
, const char *path
) {
2800 assert_return(bus
, -EINVAL
);
2801 assert_return(object_path_is_valid(path
), -EINVAL
);
2802 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2804 n
= bus_node_allocate(bus
, path
);
2808 s
= bus_slot_allocate(bus
, !slot
, BUS_NODE_OBJECT_MANAGER
, sizeof(struct node_object_manager
), NULL
);
2814 s
->node_object_manager
.node
= n
;
2815 LIST_PREPEND(object_managers
, n
->object_managers
, &s
->node_object_manager
);
2816 bus
->nodes_modified
= true;
2824 sd_bus_slot_unref(s
);
2825 bus_node_gc(bus
, n
);