1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "bus-internal.h"
8 #include "bus-introspect.h"
9 #include "bus-protocol.h"
10 #include "bus-signature.h"
13 #include "string-util.h"
16 int introspect_begin(struct introspect *i, bool trusted) {
22 i->f = open_memstream(&i->introspection, &i->size);
26 (void) __fsetlocking(i->f, FSETLOCKING_BYCALLER);
28 fputs(BUS_INTROSPECT_DOCTYPE
34 int introspect_write_default_interfaces(struct introspect *i, bool object_manager) {
37 fputs(BUS_INTROSPECT_INTERFACE_PEER
38 BUS_INTROSPECT_INTERFACE_INTROSPECTABLE
39 BUS_INTROSPECT_INTERFACE_PROPERTIES, i->f);
42 fputs(BUS_INTROSPECT_INTERFACE_OBJECT_MANAGER, i->f);
47 int introspect_write_child_nodes(struct introspect *i, Set *s, const char *prefix) {
53 while ((node = set_steal_first(s))) {
56 e = object_path_startswith(node, prefix);
58 fprintf(i->f, " <node name=\"%s\"/>\n", e);
66 static void introspect_write_flags(struct introspect *i, int type, int flags) {
67 if (flags & SD_BUS_VTABLE_DEPRECATED)
68 fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i->f);
70 if (type == _SD_BUS_VTABLE_METHOD && (flags & SD_BUS_VTABLE_METHOD_NO_REPLY))
71 fputs(" <annotation name=\"org.freedesktop.DBus.Method.NoReply\" value=\"true\"/>\n", i->f);
73 if (IN_SET(type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY)) {
74 if (flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)
75 fputs(" <annotation name=\"org.freedesktop.elogind1.Explicit\" value=\"true\"/>\n", i->f);
77 if (flags & SD_BUS_VTABLE_PROPERTY_CONST)
78 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"const\"/>\n", i->f);
79 else if (flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)
80 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"invalidates\"/>\n", i->f);
81 else if (!(flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))
82 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"false\"/>\n", i->f);
86 IN_SET(type, _SD_BUS_VTABLE_METHOD, _SD_BUS_VTABLE_WRITABLE_PROPERTY) &&
87 !(flags & SD_BUS_VTABLE_UNPRIVILEGED))
88 fputs(" <annotation name=\"org.freedesktop.elogind1.Privileged\" value=\"true\"/>\n", i->f);
91 static int introspect_write_arguments(struct introspect *i, const char *signature, const char *direction) {
100 r = signature_element_length(signature, &l);
104 fprintf(i->f, " <arg type=\"%.*s\"", (int) l, signature);
107 fprintf(i->f, " direction=\"%s\"/>\n", direction);
115 int introspect_write_interface(struct introspect *i, const sd_bus_vtable *v) {
119 for (; v->type != _SD_BUS_VTABLE_END; v++) {
121 /* Ignore methods, signals and properties that are
122 * marked "hidden", but do show the interface
125 if (v->type != _SD_BUS_VTABLE_START && (v->flags & SD_BUS_VTABLE_HIDDEN))
130 case _SD_BUS_VTABLE_START:
131 if (v->flags & SD_BUS_VTABLE_DEPRECATED)
132 fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i->f);
135 case _SD_BUS_VTABLE_METHOD:
136 fprintf(i->f, " <method name=\"%s\">\n", v->x.method.member);
137 introspect_write_arguments(i, strempty(v->x.method.signature), "in");
138 introspect_write_arguments(i, strempty(v->x.method.result), "out");
139 introspect_write_flags(i, v->type, v->flags);
140 fputs(" </method>\n", i->f);
143 case _SD_BUS_VTABLE_PROPERTY:
144 case _SD_BUS_VTABLE_WRITABLE_PROPERTY:
145 fprintf(i->f, " <property name=\"%s\" type=\"%s\" access=\"%s\">\n",
146 v->x.property.member,
147 v->x.property.signature,
148 v->type == _SD_BUS_VTABLE_WRITABLE_PROPERTY ? "readwrite" : "read");
149 introspect_write_flags(i, v->type, v->flags);
150 fputs(" </property>\n", i->f);
153 case _SD_BUS_VTABLE_SIGNAL:
154 fprintf(i->f, " <signal name=\"%s\">\n", v->x.signal.member);
155 introspect_write_arguments(i, strempty(v->x.signal.signature), NULL);
156 introspect_write_flags(i, v->type, v->flags);
157 fputs(" </signal>\n", i->f);
166 int introspect_finish(struct introspect *i, sd_bus *bus, sd_bus_message *m, sd_bus_message **reply) {
174 fputs("</node>\n", i->f);
176 r = fflush_and_check(i->f);
180 r = sd_bus_message_new_method_return(m, &q);
184 r = sd_bus_message_append(q, "s", i->introspection);
186 sd_bus_message_unref(q);
194 void introspect_free(struct introspect *i) {
199 free(i->introspection);