Debian Codegraph @

src/libsystemd/sd-bus/bus-introspect.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "sd-bus-vtable.h"
6
7 #include "bus-internal.h"
8 #include "bus-introspect.h"
9 #include "bus-objects.h"
10 #include "bus-signature.h"
11 #include "memstream-util.h"
12 #include "ordered-set.h"
13 #include "string-util.h"
14
15 #define BUS_INTROSPECT_DOCTYPE \
16 "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n" \
17 "\"https://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
18
19 #define BUS_INTROSPECT_INTERFACE_PEER \
20 " <interface name=\"org.freedesktop.DBus.Peer\">\n" \
21 " <method name=\"Ping\"/>\n" \
22 " <method name=\"GetMachineId\">\n" \
23 " <arg type=\"s\" name=\"machine_uuid\" direction=\"out\"/>\n" \
24 " </method>\n" \
25 " </interface>\n"
26
27 #define BUS_INTROSPECT_INTERFACE_INTROSPECTABLE \
28 " <interface name=\"org.freedesktop.DBus.Introspectable\">\n" \
29 " <method name=\"Introspect\">\n" \
30 " <arg name=\"xml_data\" type=\"s\" direction=\"out\"/>\n" \
31 " </method>\n" \
32 " </interface>\n"
33
34 #define BUS_INTROSPECT_INTERFACE_PROPERTIES \
35 " <interface name=\"org.freedesktop.DBus.Properties\">\n" \
36 " <method name=\"Get\">\n" \
37 " <arg name=\"interface_name\" direction=\"in\" type=\"s\"/>\n" \
38 " <arg name=\"property_name\" direction=\"in\" type=\"s\"/>\n" \
39 " <arg name=\"value\" direction=\"out\" type=\"v\"/>\n" \
40 " </method>\n" \
41 " <method name=\"GetAll\">\n" \
42 " <arg name=\"interface_name\" direction=\"in\" type=\"s\"/>\n" \
43 " <arg name=\"props\" direction=\"out\" type=\"a{sv}\"/>\n" \
44 " </method>\n" \
45 " <method name=\"Set\">\n" \
46 " <arg name=\"interface_name\" direction=\"in\" type=\"s\"/>\n" \
47 " <arg name=\"property_name\" direction=\"in\" type=\"s\"/>\n" \
48 " <arg name=\"value\" direction=\"in\" type=\"v\"/>\n" \
49 " </method>\n" \
50 " <signal name=\"PropertiesChanged\">\n" \
51 " <arg type=\"s\" name=\"interface_name\"/>\n" \
52 " <arg type=\"a{sv}\" name=\"changed_properties\"/>\n" \
53 " <arg type=\"as\" name=\"invalidated_properties\"/>\n" \
54 " </signal>\n" \
55 " </interface>\n"
56
57 #define BUS_INTROSPECT_INTERFACE_OBJECT_MANAGER \
58 " <interface name=\"org.freedesktop.DBus.ObjectManager\">\n" \
59 " <method name=\"GetManagedObjects\">\n" \
60 " <arg type=\"a{oa{sa{sv}}}\" name=\"object_paths_interfaces_and_properties\" direction=\"out\"/>\n" \
61 " </method>\n" \
62 " <signal name=\"InterfacesAdded\">\n" \
63 " <arg type=\"o\" name=\"object_path\"/>\n" \
64 " <arg type=\"a{sa{sv}}\" name=\"interfaces_and_properties\"/>\n" \
65 " </signal>\n" \
66 " <signal name=\"InterfacesRemoved\">\n" \
67 " <arg type=\"o\" name=\"object_path\"/>\n" \
68 " <arg type=\"as\" name=\"interfaces\"/>\n" \
69 " </signal>\n" \
70 " </interface>\n"
71
72 int introspect_begin(BusIntrospect *i, bool trusted) {
73 FILE *f;
74
75 assert(i);
76
77 *i = (BusIntrospect) {
78 .trusted = trusted,
79 };
80
81 f = memstream_init(&i->m);
82 if (!f)
83 return -ENOMEM;
84
85 fputs(BUS_INTROSPECT_DOCTYPE
86 "<node>\n", f);
87
88 return 0;
89 }
90
91 int introspect_write_default_interfaces(BusIntrospect *i, bool object_manager) {
92 assert(i);
93 assert(i->m.f);
94
95 fputs(BUS_INTROSPECT_INTERFACE_PEER
96 BUS_INTROSPECT_INTERFACE_INTROSPECTABLE
97 BUS_INTROSPECT_INTERFACE_PROPERTIES, i->m.f);
98
99 if (object_manager)
100 fputs(BUS_INTROSPECT_INTERFACE_OBJECT_MANAGER, i->m.f);
101
102 return 0;
103 }
104
105 static int set_interface_name(BusIntrospect *i, const char *interface_name) {
106 assert(i);
107 assert(i->m.f);
108
109 if (streq_ptr(i->interface_name, interface_name))
110 return 0;
111
112 if (i->interface_name)
113 fputs(" </interface>\n", i->m.f);
114
115 if (interface_name)
116 fprintf(i->m.f, " <interface name=\"%s\">\n", interface_name);
117
118 return free_and_strdup(&i->interface_name, interface_name);
119 }
120
121 int introspect_write_child_nodes(BusIntrospect *i, OrderedSet *s, const char *prefix) {
122 char *node;
123
124 assert(i);
125 assert(i->m.f);
126 assert(prefix);
127
128 assert_se(set_interface_name(i, NULL) >= 0);
129
130 while ((node = ordered_set_steal_first(s))) {
131 const char *e;
132
133 e = object_path_startswith(node, prefix);
134 if (e && e[0])
135 fprintf(i->m.f, " <node name=\"%s\"/>\n", e);
136
137 free(node);
138 }
139
140 return 0;
141 }
142
143 static void introspect_write_flags(BusIntrospect *i, int type, uint64_t flags) {
144 assert(i);
145 assert(i->m.f);
146
147 if (flags & SD_BUS_VTABLE_DEPRECATED)
148 fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i->m.f);
149
150 if (type == _SD_BUS_VTABLE_METHOD && (flags & SD_BUS_VTABLE_METHOD_NO_REPLY))
151 fputs(" <annotation name=\"org.freedesktop.DBus.Method.NoReply\" value=\"true\"/>\n", i->m.f);
152
153 if (IN_SET(type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY)) {
154 if (flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)
155 fputs(" <annotation name=\"org.freedesktop.systemd1.Explicit\" value=\"true\"/>\n", i->m.f);
156
157 if (flags & SD_BUS_VTABLE_PROPERTY_CONST)
158 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"const\"/>\n", i->m.f);
159 else if (flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)
160 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"invalidates\"/>\n", i->m.f);
161 else if (!(flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))
162 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"false\"/>\n", i->m.f);
163 }
164
165 if (!i->trusted &&
166 IN_SET(type, _SD_BUS_VTABLE_METHOD, _SD_BUS_VTABLE_WRITABLE_PROPERTY) &&
167 !(flags & SD_BUS_VTABLE_UNPRIVILEGED))
168 fputs(" <annotation name=\"org.freedesktop.systemd1.Privileged\" value=\"true\"/>\n", i->m.f);
169 }
170
171 /* Note that "names" is both an input and an output parameter. It initially points to the first argument name in a
172 NULL-separated list of strings, and is then advanced with each argument, and the resulting pointer is returned. */
173 static int introspect_write_arguments(BusIntrospect *i, const char *signature, const char **names, const char *direction) {
174 int r;
175
176 assert(i);
177 assert(i->m.f);
178 assert(signature);
179 assert(names);
180
181 for (;;) {
182 size_t l;
183
184 if (!*signature)
185 return 0;
186
187 r = signature_element_length(signature, &l);
188 if (r < 0)
189 return r;
190
191 fprintf(i->m.f, " <arg type=\"%.*s\"", (int) l, signature);
192
193 if (**names != '\0') {
194 fprintf(i->m.f, " name=\"%s\"", *names);
195 *names += strlen(*names) + 1;
196 }
197
198 if (direction)
199 fprintf(i->m.f, " direction=\"%s\"/>\n", direction);
200 else
201 fputs("/>\n", i->m.f);
202
203 signature += l;
204 }
205 }
206
207 int introspect_write_interface(
208 BusIntrospect *i,
209 const char *interface_name,
210 const sd_bus_vtable *v) {
211
212 const sd_bus_vtable *vtable = ASSERT_PTR(v);
213 const char *names = "";
214 int r;
215
216 assert(i);
217 assert(i->m.f);
218 assert(interface_name);
219
220 r = set_interface_name(i, interface_name);
221 if (r < 0)
222 return r;
223
224 for (; v->type != _SD_BUS_VTABLE_END; v = bus_vtable_next(vtable, v)) {
225
226 /* Ignore methods, signals and properties that are
227 * marked "hidden", but do show the interface
228 * itself */
229
230 if (v->type != _SD_BUS_VTABLE_START && (v->flags & SD_BUS_VTABLE_HIDDEN))
231 continue;
232
233 switch (v->type) {
234
235 case _SD_BUS_VTABLE_START:
236 if (v->flags & SD_BUS_VTABLE_DEPRECATED)
237 fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i->m.f);
238 break;
239
240 case _SD_BUS_VTABLE_METHOD:
241 fprintf(i->m.f, " <method name=\"%s\">\n", v->x.method.member);
242 if (bus_vtable_has_names(vtable))
243 names = strempty(v->x.method.names);
244 introspect_write_arguments(i, strempty(v->x.method.signature), &names, "in");
245 introspect_write_arguments(i, strempty(v->x.method.result), &names, "out");
246 introspect_write_flags(i, v->type, v->flags);
247 fputs(" </method>\n", i->m.f);
248 break;
249
250 case _SD_BUS_VTABLE_PROPERTY:
251 case _SD_BUS_VTABLE_WRITABLE_PROPERTY:
252 fprintf(i->m.f, " <property name=\"%s\" type=\"%s\" access=\"%s\">\n",
253 v->x.property.member,
254 v->x.property.signature,
255 v->type == _SD_BUS_VTABLE_WRITABLE_PROPERTY ? "readwrite" : "read");
256 introspect_write_flags(i, v->type, v->flags);
257 fputs(" </property>\n", i->m.f);
258 break;
259
260 case _SD_BUS_VTABLE_SIGNAL:
261 fprintf(i->m.f, " <signal name=\"%s\">\n", v->x.signal.member);
262 if (bus_vtable_has_names(vtable))
263 names = strempty(v->x.signal.names);
264 introspect_write_arguments(i, strempty(v->x.signal.signature), &names, NULL);
265 introspect_write_flags(i, v->type, v->flags);
266 fputs(" </signal>\n", i->m.f);
267 break;
268 }
269
270 }
271
272 return 0;
273 }
274
275 int introspect_finish(BusIntrospect *i, char **ret) {
276 assert(i);
277 assert(i->m.f);
278
279 assert_se(set_interface_name(i, NULL) >= 0);
280
281 fputs("</node>\n", i->m.f);
282
283 return memstream_finalize(&i->m, ret, NULL);
284 }
285
286 void introspect_done(BusIntrospect *i) {
287 assert(i);
288
289 /* Normally introspect_finish() does all the work, this is just a backup for error paths */
290
291 memstream_done(&i->m);
292 free(i->interface_name);
293 }