1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 Lennart Poettering
8 #include "bus-internal.h"
9 #include "bus-introspect.h"
10 #include "bus-protocol.h"
11 #include "bus-signature.h"
14 #include "string-util.h"
17 int introspect_begin(struct introspect *i, bool trusted) {
23 i->f = open_memstream(&i->introspection, &i->size);
27 (void) __fsetlocking(i->f, FSETLOCKING_BYCALLER);
29 fputs(BUS_INTROSPECT_DOCTYPE
35 int introspect_write_default_interfaces(struct introspect *i, bool object_manager) {
38 fputs(BUS_INTROSPECT_INTERFACE_PEER
39 BUS_INTROSPECT_INTERFACE_INTROSPECTABLE
40 BUS_INTROSPECT_INTERFACE_PROPERTIES, i->f);
43 fputs(BUS_INTROSPECT_INTERFACE_OBJECT_MANAGER, i->f);
48 int introspect_write_child_nodes(struct introspect *i, Set *s, const char *prefix) {
54 while ((node = set_steal_first(s))) {
57 e = object_path_startswith(node, prefix);
59 fprintf(i->f, " <node name=\"%s\"/>\n", e);
67 static void introspect_write_flags(struct introspect *i, int type, int flags) {
68 if (flags & SD_BUS_VTABLE_DEPRECATED)
69 fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i->f);
71 if (type == _SD_BUS_VTABLE_METHOD && (flags & SD_BUS_VTABLE_METHOD_NO_REPLY))
72 fputs(" <annotation name=\"org.freedesktop.DBus.Method.NoReply\" value=\"true\"/>\n", i->f);
74 if (IN_SET(type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY)) {
75 if (flags & SD_BUS_VTABLE_PROPERTY_EXPLICIT)
76 fputs(" <annotation name=\"org.freedesktop.elogind1.Explicit\" value=\"true\"/>\n", i->f);
78 if (flags & SD_BUS_VTABLE_PROPERTY_CONST)
79 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"const\"/>\n", i->f);
80 else if (flags & SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)
81 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"invalidates\"/>\n", i->f);
82 else if (!(flags & SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE))
83 fputs(" <annotation name=\"org.freedesktop.DBus.Property.EmitsChangedSignal\" value=\"false\"/>\n", i->f);
87 IN_SET(type, _SD_BUS_VTABLE_METHOD, _SD_BUS_VTABLE_WRITABLE_PROPERTY) &&
88 !(flags & SD_BUS_VTABLE_UNPRIVILEGED))
89 fputs(" <annotation name=\"org.freedesktop.elogind1.Privileged\" value=\"true\"/>\n", i->f);
92 static int introspect_write_arguments(struct introspect *i, const char *signature, const char *direction) {
101 r = signature_element_length(signature, &l);
105 fprintf(i->f, " <arg type=\"%.*s\"", (int) l, signature);
108 fprintf(i->f, " direction=\"%s\"/>\n", direction);
116 int introspect_write_interface(struct introspect *i, const sd_bus_vtable *v) {
120 for (; v->type != _SD_BUS_VTABLE_END; v++) {
122 /* Ignore methods, signals and properties that are
123 * marked "hidden", but do show the interface
126 if (v->type != _SD_BUS_VTABLE_START && (v->flags & SD_BUS_VTABLE_HIDDEN))
131 case _SD_BUS_VTABLE_START:
132 if (v->flags & SD_BUS_VTABLE_DEPRECATED)
133 fputs(" <annotation name=\"org.freedesktop.DBus.Deprecated\" value=\"true\"/>\n", i->f);
136 case _SD_BUS_VTABLE_METHOD:
137 fprintf(i->f, " <method name=\"%s\">\n", v->x.method.member);
138 introspect_write_arguments(i, strempty(v->x.method.signature), "in");
139 introspect_write_arguments(i, strempty(v->x.method.result), "out");
140 introspect_write_flags(i, v->type, v->flags);
141 fputs(" </method>\n", i->f);
144 case _SD_BUS_VTABLE_PROPERTY:
145 case _SD_BUS_VTABLE_WRITABLE_PROPERTY:
146 fprintf(i->f, " <property name=\"%s\" type=\"%s\" access=\"%s\">\n",
147 v->x.property.member,
148 v->x.property.signature,
149 v->type == _SD_BUS_VTABLE_WRITABLE_PROPERTY ? "readwrite" : "read");
150 introspect_write_flags(i, v->type, v->flags);
151 fputs(" </property>\n", i->f);
154 case _SD_BUS_VTABLE_SIGNAL:
155 fprintf(i->f, " <signal name=\"%s\">\n", v->x.signal.member);
156 introspect_write_arguments(i, strempty(v->x.signal.signature), NULL);
157 introspect_write_flags(i, v->type, v->flags);
158 fputs(" </signal>\n", i->f);
167 int introspect_finish(struct introspect *i, sd_bus *bus, sd_bus_message *m, sd_bus_message **reply) {
175 fputs("</node>\n", i->f);
177 r = fflush_and_check(i->f);
181 r = sd_bus_message_new_method_return(m, &q);
185 r = sd_bus_message_append(q, "s", i->introspection);
187 sd_bus_message_unref(q);
195 void introspect_free(struct introspect *i) {
200 free(i->introspection);