]>
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 | ||
22 | #include "util.h" | |
23 | #include "sd-bus-protocol.h" | |
24 | #include "bus-introspect.h" | |
25 | #include "bus-signature.h" | |
26 | #include "bus-internal.h" | |
0461f8cd | 27 | #include "bus-protocol.h" |
29ddb38f LP |
28 | |
29 | int introspect_begin(struct introspect *i) { | |
30 | assert(i); | |
31 | ||
32 | zero(*i); | |
33 | ||
34 | i->f = open_memstream(&i->introspection, &i->size); | |
35 | if (!i->f) | |
36 | return -ENOMEM; | |
37 | ||
0461f8cd | 38 | fputs(BUS_INTROSPECT_DOCTYPE |
29ddb38f LP |
39 | "<node>\n", i->f); |
40 | ||
41 | return 0; | |
42 | } | |
43 | ||
44 | int introspect_write_default_interfaces(struct introspect *i, bool object_manager) { | |
45 | assert(i); | |
46 | ||
0461f8cd LP |
47 | fputs(BUS_INTROSPECT_INTERFACE_PEER |
48 | BUS_INTROSPECT_INTERFACE_INTROSPECTABLE | |
49 | BUS_INTROSPECT_INTERFACE_PROPERTIES, i->f); | |
29ddb38f LP |
50 | |
51 | if (object_manager) | |
0461f8cd | 52 | fputs(BUS_INTROSPECT_INTERFACE_OBJECT_MANAGER, i->f); |
29ddb38f LP |
53 | |
54 | return 0; | |
55 | } | |
56 | ||
57 | int introspect_write_child_nodes(struct introspect *i, Set *s, const char *prefix) { | |
58 | char *node; | |
59 | ||
60 | assert(i); | |
61 | assert(prefix); | |
62 | ||
63 | while ((node = set_steal_first(s))) { | |
64 | const char *e; | |
65 | ||
66 | e = object_path_startswith(node, prefix); | |
dd8243a3 | 67 | if (e && e[0]) |
29ddb38f LP |
68 | fprintf(i->f, " <node name=\"%s\"/>\n", e); |
69 | ||
70 | free(node); | |
71 | } | |
72 | ||
73 | return 0; | |
74 | } | |
75 | ||
76 | static void introspect_write_flags(struct introspect *i, int type, int flags) { | |
77 | if (flags & SD_BUS_VTABLE_DEPRECATED) | |
78 | fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i->f); | |
79 | ||
adacb957 | 80 | if (type == _SD_BUS_VTABLE_METHOD && (flags & SD_BUS_VTABLE_METHOD_NO_REPLY)) |
29ddb38f LP |
81 | fputs(" <annotation name=\"org.freedesktop.DBus.Method.NoReply\" value=\"true\"/>\n", i->f); |
82 | ||
83 | if (type == _SD_BUS_VTABLE_PROPERTY || type == _SD_BUS_VTABLE_WRITABLE_PROPERTY) { | |
84 | if (!(flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE)) | |
85 | fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"false\"/>\n", i->f); | |
86 | else if (flags & SD_BUS_VTABLE_PROPERTY_INVALIDATE_ONLY) | |
87 | fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"invalidates\"/>\n", i->f); | |
88 | } | |
adacb957 LP |
89 | |
90 | if ((type == _SD_BUS_VTABLE_METHOD || type == _SD_BUS_VTABLE_WRITABLE_PROPERTY) && (flags & SD_BUS_VTABLE_UNPRIVILEGED)) | |
91 | fputs(" <annotation name=\"org.freedesktop.systemd1.Unprivileged\" value=\"true\"/>\n", i->f); | |
29ddb38f LP |
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) | |
6014597d | 110 | fprintf(i->f, " direction=\"%s\"/>\n", direction); |
29ddb38f | 111 | else |
6014597d | 112 | fputs("/>\n", i->f); |
29ddb38f LP |
113 | |
114 | signature += l; | |
115 | } | |
116 | } | |
117 | ||
718db961 | 118 | int introspect_write_interface(struct introspect *i, const sd_bus_vtable *v) { |
29ddb38f | 119 | assert(i); |
29ddb38f LP |
120 | assert(v); |
121 | ||
29ddb38f LP |
122 | for (; v->type != _SD_BUS_VTABLE_END; v++) { |
123 | ||
124 | switch (v->type) { | |
125 | ||
126 | case _SD_BUS_VTABLE_START: | |
127 | if (v->flags & SD_BUS_VTABLE_DEPRECATED) | |
128 | fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i->f); | |
129 | break; | |
130 | ||
131 | case _SD_BUS_VTABLE_METHOD: | |
77a874a3 | 132 | fprintf(i->f, " <method name=\"%s\">\n", v->x.method.member); |
f11ca85a LP |
133 | introspect_write_arguments(i, strempty(v->x.method.signature), "in"); |
134 | introspect_write_arguments(i, strempty(v->x.method.result), "out"); | |
29ddb38f LP |
135 | introspect_write_flags(i, v->type, v->flags); |
136 | fputs(" </method>\n", i->f); | |
137 | break; | |
138 | ||
139 | case _SD_BUS_VTABLE_PROPERTY: | |
140 | case _SD_BUS_VTABLE_WRITABLE_PROPERTY: | |
141 | fprintf(i->f, " <property name=\"%s\" type=\"%s\" access=\"%s\">\n", | |
77a874a3 LP |
142 | v->x.property.member, |
143 | v->x.property.signature, | |
29ddb38f LP |
144 | v->type == _SD_BUS_VTABLE_WRITABLE_PROPERTY ? "readwrite" : "read"); |
145 | introspect_write_flags(i, v->type, v->flags); | |
146 | fputs(" </property>\n", i->f); | |
147 | break; | |
148 | ||
149 | case _SD_BUS_VTABLE_SIGNAL: | |
77a874a3 | 150 | fprintf(i->f, " <signal name=\"%s\">\n", v->x.signal.member); |
f11ca85a | 151 | introspect_write_arguments(i, strempty(v->x.signal.signature), NULL); |
29ddb38f LP |
152 | introspect_write_flags(i, v->type, v->flags); |
153 | fputs(" </signal>\n", i->f); | |
154 | break; | |
155 | } | |
156 | ||
157 | } | |
158 | ||
29ddb38f LP |
159 | return 0; |
160 | } | |
161 | ||
162 | int introspect_finish(struct introspect *i, sd_bus *bus, sd_bus_message *m, sd_bus_message **reply) { | |
163 | sd_bus_message *q; | |
164 | int r; | |
165 | ||
166 | assert(i); | |
167 | assert(m); | |
168 | assert(reply); | |
169 | ||
170 | fputs("</node>\n", i->f); | |
171 | fflush(i->f); | |
172 | ||
173 | if (ferror(i->f)) | |
174 | return -ENOMEM; | |
175 | ||
df2d202e | 176 | r = sd_bus_message_new_method_return(m, &q); |
29ddb38f LP |
177 | if (r < 0) |
178 | return r; | |
179 | ||
180 | r = sd_bus_message_append(q, "s", i->introspection); | |
181 | if (r < 0) { | |
182 | sd_bus_message_unref(q); | |
183 | return r; | |
184 | } | |
185 | ||
186 | *reply = q; | |
187 | return 0; | |
188 | } | |
189 | ||
190 | void introspect_free(struct introspect *i) { | |
191 | assert(i); | |
192 | ||
193 | if (i->f) | |
194 | fclose(i->f); | |
195 | ||
196 | if (i->introspection) | |
197 | free(i->introspection); | |
198 | ||
199 | zero(*i); | |
200 | } |