1SD_BUS_ADD_OBJECT_VTABLE(3)sd_bus_add_object_vtableSD_BUS_ADD_OBJECT_VTABLE(3)
2
3
4
6 sd_bus_add_object_vtable, sd_bus_add_fallback_vtable,
7 SD_BUS_VTABLE_START, SD_BUS_VTABLE_END,
8 SD_BUS_METHOD_WITH_NAMES_OFFSET, SD_BUS_METHOD_WITH_NAMES,
9 SD_BUS_METHOD_WITH_OFFSET, SD_BUS_METHOD, SD_BUS_SIGNAL_WITH_NAMES,
10 SD_BUS_SIGNAL, SD_BUS_WRITABLE_PROPERTY, SD_BUS_PROPERTY, SD_BUS_PARAM
11 - Declare properties and methods for a D-Bus path
12
14 #include <systemd/sd-bus-vtable.h>
15
16
17 typedef int (*sd_bus_message_handler_t)(sd_bus_message *m,
18 void *userdata,
19 sd_bus_error *ret_error);
20
21 typedef int (*sd_bus_property_get_t)(sd_bus *bus, const char *path,
22 const char *interface,
23 const char *property,
24 sd_bus_message *reply,
25 void *userdata,
26 sd_bus_error *ret_error);
27
28 typedef int (*sd_bus_property_set_t)(sd_bus *bus, const char *path,
29 const char *interface,
30 const char *property,
31 sd_bus_message *value,
32 void *userdata,
33 sd_bus_error *ret_error);
34
35 typedef int (*sd_bus_object_find_t)(const char *path,
36 const char *interface,
37 void *userdata, void **ret_found,
38 sd_bus_error *ret_error);
39
40 int sd_bus_add_object_vtable(sd_bus *bus, sd_bus_slot **slot,
41 const char *path, const char *interface,
42 const sd_bus_vtable *vtable,
43 void *userdata);
44
45 int sd_bus_add_fallback_vtable(sd_bus *bus, sd_bus_slot **slot,
46 const char *prefix,
47 const char *interface,
48 const sd_bus_vtable *vtable,
49 sd_bus_object_find_t find,
50 void *userdata);
51
52 SD_BUS_VTABLE_START(flags)
53
54 SD_BUS_VTABLE_END
55
56 SD_BUS_METHOD_WITH_NAMES_OFFSET( member, signature, in_names, result,
57 out_names, handler, offset, flags)
58
59 SD_BUS_METHOD_WITH_NAMES( member, signature, in_names, result,
60 out_names, handler, flags)
61
62 SD_BUS_METHOD_WITH_OFFSET( member, signature, result, handler, offset,
63 flags)
64
65 SD_BUS_METHOD( member, signature, result, handler, flags)
66
67 SD_BUS_SIGNAL_WITH_NAMES( member, signature, names, flags)
68
69 SD_BUS_SIGNAL( member, signature, flags)
70
71 SD_BUS_WRITABLE_PROPERTY( member, signature, get, set, offset, flags)
72
73 SD_BUS_PROPERTY( member, signature, get, offset, flags)
74
75 SD_BUS_PARAM(name)
76
78 sd_bus_add_object_vtable() is used to declare attributes for the path
79 object path path connected to the bus connection bus under the
80 interface interface. The table vtable may contain property declarations
81 using SD_BUS_PROPERTY() or SD_BUS_WRITABLE_PROPERTY(), method
82 declarations using SD_BUS_METHOD(), SD_BUS_METHOD_WITH_NAMES(),
83 SD_BUS_METHOD_WITH_OFFSET(), or SD_BUS_METHOD_WITH_NAMES_OFFSET(), and
84 signal declarations using SD_BUS_SIGNAL_WITH_NAMES() or
85 SD_BUS_SIGNAL(), see below. The userdata parameter contains a pointer
86 that will be passed to various callback functions. It may be specified
87 as NULL if no value is necessary.
88
89 sd_bus_add_fallback_vtable() is similar to sd_bus_add_object_vtable(),
90 but is used to register "fallback" attributes. When looking for an
91 attribute declaration, bus object paths registered with
92 sd_bus_add_object_vtable() are checked first. If no match is found, the
93 fallback vtables are checked for each prefix of the bus object path,
94 i.e. with the last slash-separated components successively removed.
95 This allows the vtable to be used for an arbitrary number of
96 dynamically created objects.
97
98 Parameter find is a function which is used to locate the target object
99 based on the bus object path path. It must return 1 and set the
100 ret_found output parameter if the object is found, return 0 if the
101 object was not found, and return a negative errno-style error code or
102 initialize the error structure ret_error on error. The pointer passed
103 in ret_found will be used as the userdata parameter for the callback
104 functions (offset by the offset offsets as specified in the vtable
105 entries).
106
107 For both functions, a match slot is created internally. If the output
108 parameter slot is NULL, a "floating" slot object is created, see
109 sd_bus_slot_set_floating(3). Otherwise, a pointer to the slot object is
110 returned. In that case, the reference to the slot object should be
111 dropped when the vtable is not needed anymore, see
112 sd_bus_slot_unref(3).
113
114 The sd_bus_vtable array
115 The array consists of the structures of type sd_bus_vtable, but it
116 should never be filled in manually, but through one of the following
117 macros:
118
119 SD_BUS_VTABLE_START(), SD_BUS_VTABLE_END
120 Those must always be the first and last element.
121
122 SD_BUS_METHOD_WITH_NAMES_OFFSET(), SD_BUS_METHOD_WITH_NAMES(),
123 SD_BUS_METHOD_WITH_OFFSET(), SD_BUS_METHOD()
124 Declare a D-Bus method with the name member, parameter signature
125 signature, result signature result. Parameters in_names and
126 out_names specify the argument names of the input and output
127 arguments in the function signature. The handler function handler
128 must be of type sd_bus_message_handler_t. It will be called to
129 handle the incoming messages that call this method. It receives a
130 pointer that is the userdata parameter passed to the registration
131 function offset by offset bytes. This may be used to pass pointers
132 to different fields in the same data structure to different methods
133 in the same vtable. in_names and out_names should be created using
134 the SD_BUS_PARAM() macro, see below. Parameter flags is a
135 combination of flags, see below.
136
137 SD_BUS_METHOD_WITH_NAMES(), SD_BUS_METHOD_WITH_OFFSET(), and
138 SD_BUS_METHOD() are variants which specify zero offset (userdata
139 parameter is passed with no change), leave the names unset (i.e. no
140 parameter names), or both.
141
142 SD_BUS_SIGNAL_WITH_NAMES(), SD_BUS_SIGNAL()
143 Declare a D-Bus signal with the name member, parameter signature
144 signature, and argument names names. names should be created using
145 the SD_BUS_PARAM() macro, see below. Parameter flags is a
146 combination of flags, see below.
147
148 Equivalent to SD_BUS_SIGNAL_WITH_NAMES() with the names parameter
149 unset (i.e. no parameter names).
150
151 SD_BUS_WRITABLE_PROPERTY(), SD_BUS_PROPERTY()
152 Declare a D-Bus property with the name member and value signature
153 signature. Parameters get and set are the getter and setter
154 methods. They are called with a pointer that is the userdata
155 parameter passed to the registration function offset by offset
156 bytes. This may be used pass pointers to different fields in the
157 same data structure to different setters and getters in the same
158 vtable. Parameter flags is a combination of flags, see below.
159
160 The setter and getter methods may be omitted (specified as NULL),
161 if the property has one of the basic types or "as" in case of
162 read-only properties. In those cases, the userdata and offset
163 parameters must together point to valid variable of the
164 corresponding type. A default setter and getters will be provided,
165 which simply copy the argument between this variable and the
166 message.
167
168 SD_BUS_PROPERTY() is used to define a read-only property.
169
170 SD_BUS_PARAM()
171 Parameter names should be wrapped in this macro, see the example
172 below.
173
174 Flags
175 The flags parameter is used to specify a combination of D-Bus
176 annotations[1].
177
178 SD_BUS_VTABLE_DEPRECATED
179 Mark this vtable entry as deprecated using the
180 org.freedesktop.DBus.Deprecated annotation in introspection data.
181 If specified for SD_BUS_VTABLE_START(), the annotation is applied
182 to the enclosing interface.
183
184 SD_BUS_VTABLE_HIDDEN
185 Make this vtable entry hidden. It will not be shown in
186 introspection data. If specified for SD_BUS_VTABLE_START(), all
187 entries in the array are hidden.
188
189 SD_BUS_VTABLE_UNPRIVILEGED
190 Mark this vtable entry as unprivileged. If not specified, the
191 org.freedesktop.systemd1.Privileged annotation with value "true"
192 will be shown in introspection data.
193
194 SD_BUS_VTABLE_METHOD_NO_REPLY
195 Mark his vtable entry as a method that will not return a reply
196 using the org.freedesktop.DBus.Method.NoReply annotation in
197 introspection data.
198
199 SD_BUS_VTABLE_PROPERTY_CONST, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE,
200 SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
201 Those three flags correspond to different values of the
202 org.freedesktop.DBus.Property.EmitsChangedSignal annotation, which
203 specifies whether the
204 org.freedesktop.DBus.Properties.PropertiesChanged signal is emitted
205 whenever the property changes. SD_BUS_VTABLE_PROPERTY_CONST
206 corresponds to const and means that the property never changes
207 during the lifetime of the object it belongs to, so no signal needs
208 to be emitted. SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE corresponds to
209 true and means that the signal is emitted.
210 SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION corresponds to
211 invalidates and means that the signal is emitted, but the value is
212 not included in the signal.
213
214 SD_BUS_VTABLE_PROPERTY_EXPLICIT
215 Mark this vtable property entry as requiring explicit request to
216 for the value to be shown (generally because the value is large or
217 slow to calculate). This entry cannot be combined with
218 SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE, and will not be shown in
219 property listings by default (e.g. busctl introspect). This
220 corresponds to the org.freedesktop.systemd1.Explicit annotation in
221 introspection data.
222
224 Example 1. Create a simple listener on the bus
225
226 #include <errno.h>
227 #include <stdbool.h>
228 #include <stddef.h>
229 #include <stdlib.h>
230 #include <stdio.h>
231 #include <systemd/sd-bus.h>
232
233 #define _cleanup_(f) __attribute__((cleanup(f)))
234
235 typedef struct object {
236 char *name;
237 uint32_t number;
238 } object;
239
240 static int method(sd_bus_message *m, void *userdata, sd_bus_error *error) {
241 printf("Got called with userdata=%p\n", userdata);
242 return 1;
243 }
244
245 static const sd_bus_vtable vtable[] = {
246 SD_BUS_VTABLE_START(0),
247 SD_BUS_METHOD(
248 "Method1", "s", "s", method, 0),
249 SD_BUS_METHOD_WITH_NAMES_OFFSET(
250 "Method2",
251 "so", SD_BUS_PARAM(string) SD_BUS_PARAM(path),
252 "s", SD_BUS_PARAM(returnstring),
253 method, offsetof(object, number),
254 SD_BUS_VTABLE_DEPRECATED),
255 SD_BUS_WRITABLE_PROPERTY(
256 "AutomaticStringProperty", "s", NULL, NULL,
257 offsetof(object, name),
258 SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
259 SD_BUS_WRITABLE_PROPERTY(
260 "AutomaticIntegerProperty", "u", NULL, NULL,
261 offsetof(object, number),
262 SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
263 SD_BUS_VTABLE_END
264 };
265
266 #define check(x) ({ \
267 int r = x; \
268 errno = r < 0 ? -r : 0; \
269 printf(#x ": %m\n"); \
270 if (r < 0) \
271 return EXIT_FAILURE; \
272 })
273
274 int main(int argc, char **argv) {
275 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
276
277 sd_bus_default(&bus);
278
279 object object = { .number = 666 };
280 check((object.name = strdup("name")) != NULL);
281
282 check(sd_bus_add_object_vtable(bus, NULL, "/object",
283 "org.freedesktop.systemd.VtableExample",
284 vtable,
285 &object));
286
287 for (;;) {
288 check(sd_bus_wait(bus, UINT64_MAX));
289 check(sd_bus_process(bus, NULL));
290 }
291
292 free(object.name);
293
294 return 0;
295 }
296
297 This creates a simple client on the bus (the user bus, when run as
298 normal user). We may use the D-Bus
299 org.freedesktop.DBus.Introspectable.Introspect call to acquire the XML
300 description of the interface:
301
302 <!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
303 "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
304 <node>
305 <interface name="org.freedesktop.DBus.Peer">
306 <method name="Ping"/>
307 <method name="GetMachineId">
308 <arg type="s" name="machine_uuid" direction="out"/>
309 </method>
310 </interface>
311 <interface name="org.freedesktop.DBus.Introspectable">
312 <method name="Introspect">
313 <arg name="data" type="s" direction="out"/>
314 </method>
315 </interface>
316 <interface name="org.freedesktop.DBus.Properties">
317 <method name="Get">
318 <arg name="interface" direction="in" type="s"/>
319 <arg name="property" direction="in" type="s"/>
320 <arg name="value" direction="out" type="v"/>
321 </method>
322 <method name="GetAll">
323 <arg name="interface" direction="in" type="s"/>
324 <arg name="properties" direction="out" type="a{sv}"/>
325 </method>
326 <method name="Set">
327 <arg name="interface" direction="in" type="s"/>
328 <arg name="property" direction="in" type="s"/>
329 <arg name="value" direction="in" type="v"/>
330 </method>
331 <signal name="PropertiesChanged">
332 <arg type="s" name="interface"/>
333 <arg type="a{sv}" name="changed_properties"/>
334 <arg type="as" name="invalidated_properties"/>
335 </signal>
336 </interface>
337 <interface name="org.freedesktop.systemd.VtableExample">
338 <method name="Method1">
339 <arg type="s" direction="in"/>
340 <arg type="s" direction="out"/>
341 </method>
342 <method name="Method2">
343 <arg type="s" name="string" direction="in"/>
344 <arg type="o" name="path" direction="in"/>
345 <arg type="s" name="returnstring" direction="out"/>
346 <annotation name="org.freedesktop.DBus.Deprecated" value="true"/>
347 </method>
348 <property name="AutomaticStringProperty" type="s" access="readwrite">
349 </property>
350 <property name="AutomaticIntegerProperty" type="u" access="readwrite">
351 <annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="invalidates"/>
352 </property>
353 </interface>
354 </node>
355
356
358 On success, sd_bus_add_object_vtable and sd_bus_add_fallback_vtable
359 calls return 0 or a positive integer. On failure, they return a
360 negative errno-style error code.
361
362 Errors
363 Returned errors may indicate the following problems:
364
365 -EINVAL
366 One of the required parameters is NULL or invalid. A reserved D-Bus
367 interface was passed as the interface parameter.
368
369 -ENOPKG
370 The bus cannot be resolved.
371
372 -ECHILD
373 The bus was created in a different process.
374
375 -ENOMEM
376 Memory allocation failed.
377
378 -EPROTOTYPE
379 sd_bus_add_object_vtable and sd_bus_add_fallback_vtable have been
380 both called for the same bus object path, which is not allowed.
381
382 -EEXIST
383 This vtable has already been registered for this interface and
384 path.
385
387 These APIs are implemented as a shared library, which can be compiled
388 and linked to with the libsystemd pkg-config(1) file.
389
391 sd-bus(3), busctl(1)
392
394 1. D-Bus annotations
395 https://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format
396
397
398
399systemd 245 SD_BUS_ADD_OBJECT_VTABLE(3)