1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
10 #include "bus-internal.h"
11 #include "bus-introspect.h"
12 #include "bus-protocol.h"
13 #include "bus-signature.h"
16 #include "string-util.h"
19 int introspect_begin(struct introspect
*i
, bool trusted
) {
25 i
->f
= open_memstream(&i
->introspection
, &i
->size
);
29 (void) __fsetlocking(i
->f
, FSETLOCKING_BYCALLER
);
31 fputs(BUS_INTROSPECT_DOCTYPE
37 int introspect_write_default_interfaces(struct introspect
*i
, bool object_manager
) {
40 fputs(BUS_INTROSPECT_INTERFACE_PEER
41 BUS_INTROSPECT_INTERFACE_INTROSPECTABLE
42 BUS_INTROSPECT_INTERFACE_PROPERTIES
, i
->f
);
45 fputs(BUS_INTROSPECT_INTERFACE_OBJECT_MANAGER
, i
->f
);
50 int introspect_write_child_nodes(struct introspect
*i
, Set
*s
, const char *prefix
) {
56 while ((node
= set_steal_first(s
))) {
59 e
= object_path_startswith(node
, prefix
);
61 fprintf(i
->f
, " <node name=\"%s\"/>\n", e
);
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
);
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
);
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
);
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
);
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
);
94 static int introspect_write_arguments(struct introspect
*i
, const char *signature
, const char *direction
) {
103 r
= signature_element_length(signature
, &l
);
107 fprintf(i
->f
, " <arg type=\"%.*s\"", (int) l
, signature
);
110 fprintf(i
->f
, " direction=\"%s\"/>\n", direction
);
118 int introspect_write_interface(struct introspect
*i
, const sd_bus_vtable
*v
) {
122 for (; v
->type
!= _SD_BUS_VTABLE_END
; v
++) {
124 /* Ignore methods, signals and properties that are
125 * marked "hidden", but do show the interface
128 if (v
->type
!= _SD_BUS_VTABLE_START
&& (v
->flags
& SD_BUS_VTABLE_HIDDEN
))
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
);
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
);
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
);
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
);
169 int introspect_finish(struct introspect
*i
, sd_bus
*bus
, sd_bus_message
*m
, sd_bus_message
**reply
) {
177 fputs("</node>\n", i
->f
);
179 r
= fflush_and_check(i
->f
);
183 r
= sd_bus_message_new_method_return(m
, &q
);
187 r
= sd_bus_message_append(q
, "s", i
->introspection
);
189 sd_bus_message_unref(q
);
197 void introspect_free(struct introspect
*i
) {
202 free(i
->introspection
);