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