1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
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.
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.
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/>.
22 #include "bus-internal.h"
23 #include "bus-introspect.h"
24 #include "bus-protocol.h"
25 #include "bus-signature.h"
27 #include "string-util.h"
30 int introspect_begin(struct introspect
*i
, bool trusted
) {
36 i
->f
= open_memstream(&i
->introspection
, &i
->size
);
40 fputs(BUS_INTROSPECT_DOCTYPE
46 int introspect_write_default_interfaces(struct introspect
*i
, bool object_manager
) {
49 fputs(BUS_INTROSPECT_INTERFACE_PEER
50 BUS_INTROSPECT_INTERFACE_INTROSPECTABLE
51 BUS_INTROSPECT_INTERFACE_PROPERTIES
, i
->f
);
54 fputs(BUS_INTROSPECT_INTERFACE_OBJECT_MANAGER
, i
->f
);
59 int introspect_write_child_nodes(struct introspect
*i
, Set
*s
, const char *prefix
) {
65 while ((node
= set_steal_first(s
))) {
68 e
= object_path_startswith(node
, prefix
);
70 fprintf(i
->f
, " <node name=\"%s\"/>\n", e
);
78 static void introspect_write_flags(struct introspect
*i
, int type
, int flags
) {
79 if (flags
& SD_BUS_VTABLE_DEPRECATED
)
80 fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i
->f
);
82 if (type
== _SD_BUS_VTABLE_METHOD
&& (flags
& SD_BUS_VTABLE_METHOD_NO_REPLY
))
83 fputs(" <annotation name=\"org.freedesktop.DBus.Method.NoReply\" value=\"true\"/>\n", i
->f
);
85 if (type
== _SD_BUS_VTABLE_PROPERTY
|| type
== _SD_BUS_VTABLE_WRITABLE_PROPERTY
) {
86 if (flags
& SD_BUS_VTABLE_PROPERTY_EXPLICIT
)
87 fputs(" <annotation name=\"org.freedesktop.systemd1.Explicit\" value=\"true\"/>\n", i
->f
);
89 if (flags
& SD_BUS_VTABLE_PROPERTY_CONST
)
90 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"const\"/>\n", i
->f
);
91 else if (flags
& SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
)
92 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"invalidates\"/>\n", i
->f
);
93 else if (!(flags
& SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
))
94 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"false\"/>\n", i
->f
);
98 (type
== _SD_BUS_VTABLE_METHOD
|| type
== _SD_BUS_VTABLE_WRITABLE_PROPERTY
) &&
99 !(flags
& SD_BUS_VTABLE_UNPRIVILEGED
))
100 fputs(" <annotation name=\"org.freedesktop.systemd1.Privileged\" value=\"true\"/>\n", i
->f
);
103 static int introspect_write_arguments(struct introspect
*i
, const char *signature
, const char *direction
) {
112 r
= signature_element_length(signature
, &l
);
116 fprintf(i
->f
, " <arg type=\"%.*s\"", (int) l
, signature
);
119 fprintf(i
->f
, " direction=\"%s\"/>\n", direction
);
127 int introspect_write_interface(struct introspect
*i
, const sd_bus_vtable
*v
) {
131 for (; v
->type
!= _SD_BUS_VTABLE_END
; v
++) {
133 /* Ignore methods, signals and properties that are
134 * marked "hidden", but do show the interface
137 if (v
->type
!= _SD_BUS_VTABLE_START
&& (v
->flags
& SD_BUS_VTABLE_HIDDEN
))
142 case _SD_BUS_VTABLE_START
:
143 if (v
->flags
& SD_BUS_VTABLE_DEPRECATED
)
144 fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i
->f
);
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(" </method>\n", i
->f
);
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(" </property>\n", i
->f
);
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(" </signal>\n", i
->f
);
178 int introspect_finish(struct introspect
*i
, sd_bus
*bus
, sd_bus_message
*m
, sd_bus_message
**reply
) {
186 fputs("</node>\n", i
->f
);
188 r
= fflush_and_check(i
->f
);
192 r
= sd_bus_message_new_method_return(m
, &q
);
196 r
= sd_bus_message_append(q
, "s", i
->introspection
);
198 sd_bus_message_unref(q
);
206 void introspect_free(struct introspect
*i
) {
211 free(i
->introspection
);