1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "bus-internal.h"
4 #include "bus-introspect.h"
5 #include "bus-objects.h"
6 #include "bus-protocol.h"
7 #include "bus-signature.h"
10 #include "memory-util.h"
11 #include "string-util.h"
13 int introspect_begin(struct introspect
*i
, bool trusted
) {
19 i
->f
= open_memstream_unlocked(&i
->introspection
, &i
->size
);
23 fputs(BUS_INTROSPECT_DOCTYPE
29 int introspect_write_default_interfaces(struct introspect
*i
, bool object_manager
) {
32 fputs(BUS_INTROSPECT_INTERFACE_PEER
33 BUS_INTROSPECT_INTERFACE_INTROSPECTABLE
34 BUS_INTROSPECT_INTERFACE_PROPERTIES
, i
->f
);
37 fputs(BUS_INTROSPECT_INTERFACE_OBJECT_MANAGER
, i
->f
);
42 int introspect_write_child_nodes(struct introspect
*i
, Set
*s
, const char *prefix
) {
48 while ((node
= set_steal_first(s
))) {
51 e
= object_path_startswith(node
, prefix
);
53 fprintf(i
->f
, " <node name=\"%s\"/>\n", e
);
61 static void introspect_write_flags(struct introspect
*i
, int type
, uint64_t flags
) {
62 if (flags
& SD_BUS_VTABLE_DEPRECATED
)
63 fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i
->f
);
65 if (type
== _SD_BUS_VTABLE_METHOD
&& (flags
& SD_BUS_VTABLE_METHOD_NO_REPLY
))
66 fputs(" <annotation name=\"org.freedesktop.DBus.Method.NoReply\" value=\"true\"/>\n", i
->f
);
68 if (IN_SET(type
, _SD_BUS_VTABLE_PROPERTY
, _SD_BUS_VTABLE_WRITABLE_PROPERTY
)) {
69 if (flags
& SD_BUS_VTABLE_PROPERTY_EXPLICIT
)
70 fputs(" <annotation name=\"org.freedesktop.systemd1.Explicit\" value=\"true\"/>\n", i
->f
);
72 if (flags
& SD_BUS_VTABLE_PROPERTY_CONST
)
73 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"const\"/>\n", i
->f
);
74 else if (flags
& SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
)
75 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"invalidates\"/>\n", i
->f
);
76 else if (!(flags
& SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
))
77 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"false\"/>\n", i
->f
);
81 IN_SET(type
, _SD_BUS_VTABLE_METHOD
, _SD_BUS_VTABLE_WRITABLE_PROPERTY
) &&
82 !(flags
& SD_BUS_VTABLE_UNPRIVILEGED
))
83 fputs(" <annotation name=\"org.freedesktop.systemd1.Privileged\" value=\"true\"/>\n", i
->f
);
86 /* Note that "names" is both an input and an output parameter. It initially points to the first argument name in a
87 NULL-separated list of strings, and is then advanced with each argument, and the resulting pointer is returned. */
88 static int introspect_write_arguments(struct introspect
*i
, const char *signature
, const char **names
, const char *direction
) {
97 r
= signature_element_length(signature
, &l
);
101 fprintf(i
->f
, " <arg type=\"%.*s\"", (int) l
, signature
);
103 if (**names
!= '\0') {
104 fprintf(i
->f
, " name=\"%s\"", *names
);
105 *names
+= strlen(*names
) + 1;
109 fprintf(i
->f
, " direction=\"%s\"/>\n", direction
);
117 int introspect_write_interface(struct introspect
*i
, const sd_bus_vtable
*v
) {
118 const sd_bus_vtable
*vtable
= v
;
119 const char *names
= "";
124 for (; v
->type
!= _SD_BUS_VTABLE_END
; v
= bus_vtable_next(vtable
, v
)) {
126 /* Ignore methods, signals and properties that are
127 * marked "hidden", but do show the interface
130 if (v
->type
!= _SD_BUS_VTABLE_START
&& (v
->flags
& SD_BUS_VTABLE_HIDDEN
))
135 case _SD_BUS_VTABLE_START
:
136 if (v
->flags
& SD_BUS_VTABLE_DEPRECATED
)
137 fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i
->f
);
140 case _SD_BUS_VTABLE_METHOD
:
141 fprintf(i
->f
, " <method name=\"%s\">\n", v
->x
.method
.member
);
142 if (bus_vtable_has_names(vtable
))
143 names
= strempty(v
->x
.method
.names
);
144 introspect_write_arguments(i
, strempty(v
->x
.method
.signature
), &names
, "in");
145 introspect_write_arguments(i
, strempty(v
->x
.method
.result
), &names
, "out");
146 introspect_write_flags(i
, v
->type
, v
->flags
);
147 fputs(" </method>\n", i
->f
);
150 case _SD_BUS_VTABLE_PROPERTY
:
151 case _SD_BUS_VTABLE_WRITABLE_PROPERTY
:
152 fprintf(i
->f
, " <property name=\"%s\" type=\"%s\" access=\"%s\">\n",
153 v
->x
.property
.member
,
154 v
->x
.property
.signature
,
155 v
->type
== _SD_BUS_VTABLE_WRITABLE_PROPERTY
? "readwrite" : "read");
156 introspect_write_flags(i
, v
->type
, v
->flags
);
157 fputs(" </property>\n", i
->f
);
160 case _SD_BUS_VTABLE_SIGNAL
:
161 fprintf(i
->f
, " <signal name=\"%s\">\n", v
->x
.signal
.member
);
162 if (bus_vtable_has_names(vtable
))
163 names
= strempty(v
->x
.method
.names
);
164 introspect_write_arguments(i
, strempty(v
->x
.signal
.signature
), &names
, NULL
);
165 introspect_write_flags(i
, v
->type
, v
->flags
);
166 fputs(" </signal>\n", i
->f
);
175 int introspect_finish(struct introspect
*i
, sd_bus
*bus
, sd_bus_message
*m
, sd_bus_message
**reply
) {
183 fputs("</node>\n", i
->f
);
185 r
= fflush_and_check(i
->f
);
189 r
= sd_bus_message_new_method_return(m
, &q
);
193 r
= sd_bus_message_append(q
, "s", i
->introspection
);
195 sd_bus_message_unref(q
);
203 void introspect_free(struct introspect
*i
) {
208 free(i
->introspection
);