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