1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "bus-internal.h"
6 #include "bus-introspect.h"
7 #include "bus-protocol.h"
8 #include "bus-signature.h"
11 #include "string-util.h"
14 int introspect_begin(struct introspect
*i
, bool trusted
) {
20 i
->f
= open_memstream(&i
->introspection
, &i
->size
);
24 (void) __fsetlocking(i
->f
, FSETLOCKING_BYCALLER
);
26 fputs(BUS_INTROSPECT_DOCTYPE
32 int introspect_write_default_interfaces(struct introspect
*i
, bool object_manager
) {
35 fputs(BUS_INTROSPECT_INTERFACE_PEER
36 BUS_INTROSPECT_INTERFACE_INTROSPECTABLE
37 BUS_INTROSPECT_INTERFACE_PROPERTIES
, i
->f
);
40 fputs(BUS_INTROSPECT_INTERFACE_OBJECT_MANAGER
, i
->f
);
45 int introspect_write_child_nodes(struct introspect
*i
, Set
*s
, const char *prefix
) {
51 while ((node
= set_steal_first(s
))) {
54 e
= object_path_startswith(node
, prefix
);
56 fprintf(i
->f
, " <node name=\"%s\"/>\n", e
);
64 static void introspect_write_flags(struct introspect
*i
, int type
, uint64_t flags
) {
65 if (flags
& SD_BUS_VTABLE_DEPRECATED
)
66 fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i
->f
);
68 if (type
== _SD_BUS_VTABLE_METHOD
&& (flags
& SD_BUS_VTABLE_METHOD_NO_REPLY
))
69 fputs(" <annotation name=\"org.freedesktop.DBus.Method.NoReply\" value=\"true\"/>\n", i
->f
);
71 if (IN_SET(type
, _SD_BUS_VTABLE_PROPERTY
, _SD_BUS_VTABLE_WRITABLE_PROPERTY
)) {
72 if (flags
& SD_BUS_VTABLE_PROPERTY_EXPLICIT
)
73 fputs(" <annotation name=\"org.freedesktop.systemd1.Explicit\" value=\"true\"/>\n", i
->f
);
75 if (flags
& SD_BUS_VTABLE_PROPERTY_CONST
)
76 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"const\"/>\n", i
->f
);
77 else if (flags
& SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
)
78 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"invalidates\"/>\n", i
->f
);
79 else if (!(flags
& SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
))
80 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"false\"/>\n", i
->f
);
84 IN_SET(type
, _SD_BUS_VTABLE_METHOD
, _SD_BUS_VTABLE_WRITABLE_PROPERTY
) &&
85 !(flags
& SD_BUS_VTABLE_UNPRIVILEGED
))
86 fputs(" <annotation name=\"org.freedesktop.systemd1.Privileged\" value=\"true\"/>\n", i
->f
);
89 static int introspect_write_arguments(struct introspect
*i
, const char *signature
, const char *direction
) {
98 r
= signature_element_length(signature
, &l
);
102 fprintf(i
->f
, " <arg type=\"%.*s\"", (int) l
, signature
);
105 fprintf(i
->f
, " direction=\"%s\"/>\n", direction
);
113 int introspect_write_interface(struct introspect
*i
, const sd_bus_vtable
*v
) {
117 for (; v
->type
!= _SD_BUS_VTABLE_END
; v
++) {
119 /* Ignore methods, signals and properties that are
120 * marked "hidden", but do show the interface
123 if (v
->type
!= _SD_BUS_VTABLE_START
&& (v
->flags
& SD_BUS_VTABLE_HIDDEN
))
128 case _SD_BUS_VTABLE_START
:
129 if (v
->flags
& SD_BUS_VTABLE_DEPRECATED
)
130 fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i
->f
);
133 case _SD_BUS_VTABLE_METHOD
:
134 fprintf(i
->f
, " <method name=\"%s\">\n", v
->x
.method
.member
);
135 introspect_write_arguments(i
, strempty(v
->x
.method
.signature
), "in");
136 introspect_write_arguments(i
, strempty(v
->x
.method
.result
), "out");
137 introspect_write_flags(i
, v
->type
, v
->flags
);
138 fputs(" </method>\n", i
->f
);
141 case _SD_BUS_VTABLE_PROPERTY
:
142 case _SD_BUS_VTABLE_WRITABLE_PROPERTY
:
143 fprintf(i
->f
, " <property name=\"%s\" type=\"%s\" access=\"%s\">\n",
144 v
->x
.property
.member
,
145 v
->x
.property
.signature
,
146 v
->type
== _SD_BUS_VTABLE_WRITABLE_PROPERTY
? "readwrite" : "read");
147 introspect_write_flags(i
, v
->type
, v
->flags
);
148 fputs(" </property>\n", i
->f
);
151 case _SD_BUS_VTABLE_SIGNAL
:
152 fprintf(i
->f
, " <signal name=\"%s\">\n", v
->x
.signal
.member
);
153 introspect_write_arguments(i
, strempty(v
->x
.signal
.signature
), NULL
);
154 introspect_write_flags(i
, v
->type
, v
->flags
);
155 fputs(" </signal>\n", i
->f
);
164 int introspect_finish(struct introspect
*i
, sd_bus
*bus
, sd_bus_message
*m
, sd_bus_message
**reply
) {
172 fputs("</node>\n", i
->f
);
174 r
= fflush_and_check(i
->f
);
178 r
= sd_bus_message_new_method_return(m
, &q
);
182 r
= sd_bus_message_append(q
, "s", i
->introspection
);
184 sd_bus_message_unref(q
);
192 void introspect_free(struct introspect
*i
) {
197 free(i
->introspection
);