]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/bus-introspect.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / libsystemd / sd-bus / bus-introspect.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 Lennart Poettering
6 ***/
7
8 #include <stdio_ext.h>
9
10 #include "bus-internal.h"
11 #include "bus-introspect.h"
12 #include "bus-protocol.h"
13 #include "bus-signature.h"
14 #include "fd-util.h"
15 #include "fileio.h"
16 #include "string-util.h"
17 #include "util.h"
18
19 int introspect_begin(struct introspect *i, bool trusted) {
20 assert(i);
21
22 zero(*i);
23 i->trusted = trusted;
24
25 i->f = open_memstream(&i->introspection, &i->size);
26 if (!i->f)
27 return -ENOMEM;
28
29 (void) __fsetlocking(i->f, FSETLOCKING_BYCALLER);
30
31 fputs(BUS_INTROSPECT_DOCTYPE
32 "<node>\n", i->f);
33
34 return 0;
35 }
36
37 int introspect_write_default_interfaces(struct introspect *i, bool object_manager) {
38 assert(i);
39
40 fputs(BUS_INTROSPECT_INTERFACE_PEER
41 BUS_INTROSPECT_INTERFACE_INTROSPECTABLE
42 BUS_INTROSPECT_INTERFACE_PROPERTIES, i->f);
43
44 if (object_manager)
45 fputs(BUS_INTROSPECT_INTERFACE_OBJECT_MANAGER, i->f);
46
47 return 0;
48 }
49
50 int introspect_write_child_nodes(struct introspect *i, Set *s, const char *prefix) {
51 char *node;
52
53 assert(i);
54 assert(prefix);
55
56 while ((node = set_steal_first(s))) {
57 const char *e;
58
59 e = object_path_startswith(node, prefix);
60 if (e && e[0])
61 fprintf(i->f, " <node name=\"%s\"/>\n", e);
62
63 free(node);
64 }
65
66 return 0;
67 }
68
69 static void introspect_write_flags(struct introspect *i, int type, int flags) {
70 if (flags & SD_BUS_VTABLE_DEPRECATED)
71 fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i->f);
72
73 if (type == _SD_BUS_VTABLE_METHOD && (flags & SD_BUS_VTABLE_METHOD_NO_REPLY))
74 fputs(" <annotation name=\"org.freedesktop.DBus.Method.NoReply\" value=\"true\"/>\n", i->f);
75
76 if (IN_SET(type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY)) {
77 if (flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)
78 fputs(" <annotation name=\"org.freedesktop.systemd1.Explicit\" value=\"true\"/>\n", i->f);
79
80 if (flags & SD_BUS_VTABLE_PROPERTY_CONST)
81 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"const\"/>\n", i->f);
82 else if (flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)
83 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"invalidates\"/>\n", i->f);
84 else if (!(flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))
85 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"false\"/>\n", i->f);
86 }
87
88 if (!i->trusted &&
89 IN_SET(type, _SD_BUS_VTABLE_METHOD, _SD_BUS_VTABLE_WRITABLE_PROPERTY) &&
90 !(flags & SD_BUS_VTABLE_UNPRIVILEGED))
91 fputs(" <annotation name=\"org.freedesktop.systemd1.Privileged\" value=\"true\"/>\n", i->f);
92 }
93
94 static int introspect_write_arguments(struct introspect *i, const char *signature, const char *direction) {
95 int r;
96
97 for (;;) {
98 size_t l;
99
100 if (!*signature)
101 return 0;
102
103 r = signature_element_length(signature, &l);
104 if (r < 0)
105 return r;
106
107 fprintf(i->f, " <arg type=\"%.*s\"", (int) l, signature);
108
109 if (direction)
110 fprintf(i->f, " direction=\"%s\"/>\n", direction);
111 else
112 fputs("/>\n", i->f);
113
114 signature += l;
115 }
116 }
117
118 int introspect_write_interface(struct introspect *i, const sd_bus_vtable *v) {
119 assert(i);
120 assert(v);
121
122 for (; v->type != _SD_BUS_VTABLE_END; v++) {
123
124 /* Ignore methods, signals and properties that are
125 * marked "hidden", but do show the interface
126 * itself */
127
128 if (v->type != _SD_BUS_VTABLE_START && (v->flags & SD_BUS_VTABLE_HIDDEN))
129 continue;
130
131 switch (v->type) {
132
133 case _SD_BUS_VTABLE_START:
134 if (v->flags & SD_BUS_VTABLE_DEPRECATED)
135 fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i->f);
136 break;
137
138 case _SD_BUS_VTABLE_METHOD:
139 fprintf(i->f, " <method name=\"%s\">\n", v->x.method.member);
140 introspect_write_arguments(i, strempty(v->x.method.signature), "in");
141 introspect_write_arguments(i, strempty(v->x.method.result), "out");
142 introspect_write_flags(i, v->type, v->flags);
143 fputs(" </method>\n", i->f);
144 break;
145
146 case _SD_BUS_VTABLE_PROPERTY:
147 case _SD_BUS_VTABLE_WRITABLE_PROPERTY:
148 fprintf(i->f, " <property name=\"%s\" type=\"%s\" access=\"%s\">\n",
149 v->x.property.member,
150 v->x.property.signature,
151 v->type == _SD_BUS_VTABLE_WRITABLE_PROPERTY ? "readwrite" : "read");
152 introspect_write_flags(i, v->type, v->flags);
153 fputs(" </property>\n", i->f);
154 break;
155
156 case _SD_BUS_VTABLE_SIGNAL:
157 fprintf(i->f, " <signal name=\"%s\">\n", v->x.signal.member);
158 introspect_write_arguments(i, strempty(v->x.signal.signature), NULL);
159 introspect_write_flags(i, v->type, v->flags);
160 fputs(" </signal>\n", i->f);
161 break;
162 }
163
164 }
165
166 return 0;
167 }
168
169 int introspect_finish(struct introspect *i, sd_bus *bus, sd_bus_message *m, sd_bus_message **reply) {
170 sd_bus_message *q;
171 int r;
172
173 assert(i);
174 assert(m);
175 assert(reply);
176
177 fputs("</node>\n", i->f);
178
179 r = fflush_and_check(i->f);
180 if (r < 0)
181 return r;
182
183 r = sd_bus_message_new_method_return(m, &q);
184 if (r < 0)
185 return r;
186
187 r = sd_bus_message_append(q, "s", i->introspection);
188 if (r < 0) {
189 sd_bus_message_unref(q);
190 return r;
191 }
192
193 *reply = q;
194 return 0;
195 }
196
197 void introspect_free(struct introspect *i) {
198 assert(i);
199
200 safe_fclose(i->f);
201
202 free(i->introspection);
203 zero(*i);
204 }