1 <?xml version='
1.0'
?> <!--*-nxml-*-->
2 <!DOCTYPE refentry PUBLIC
"-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4 <!-- SPDX-License-Identifier: LGPL-2.1+ -->
6 <refentry id=
"sd_bus_add_object_vtable"
7 xmlns:
xi=
"http://www.w3.org/2001/XInclude">
10 <title>sd_bus_add_object_vtable
</title>
11 <productname>systemd
</productname>
15 <refentrytitle>sd_bus_add_object_vtable
</refentrytitle>
16 <manvolnum>3</manvolnum>
20 <refname>sd_bus_add_object_vtable
</refname>
21 <refname>sd_bus_add_fallback_vtable
</refname>
22 <refname>SD_BUS_VTABLE_START
</refname>
23 <refname>SD_BUS_VTABLE_END
</refname>
24 <refname>SD_BUS_METHOD_WITH_NAMES_OFFSET
</refname>
25 <refname>SD_BUS_METHOD_WITH_NAMES
</refname>
26 <refname>SD_BUS_METHOD_WITH_OFFSET
</refname>
27 <refname>SD_BUS_METHOD
</refname>
28 <refname>SD_BUS_SIGNAL_WITH_NAMES
</refname>
29 <refname>SD_BUS_SIGNAL
</refname>
30 <refname>SD_BUS_WRITABLE_PROPERTY
</refname>
31 <refname>SD_BUS_PROPERTY
</refname>
32 <refname>SD_BUS_PARAM
</refname>
34 <refpurpose>Declare properties and methods for a D-Bus path
</refpurpose>
39 <funcsynopsisinfo>#include
<systemd/sd-bus-vtable.h
></funcsynopsisinfo>
42 <funcdef>typedef int (*
<function>sd_bus_message_handler_t
</function>)
</funcdef>
43 <paramdef>sd_bus_message *
<parameter>m
</parameter></paramdef>
44 <paramdef>void *
<parameter>userdata
</parameter></paramdef>
45 <paramdef>sd_bus_error *
<parameter>ret_error
</parameter></paramdef>
49 <funcdef>typedef int (*
<function>sd_bus_property_get_t
</function>)
</funcdef>
50 <paramdef>sd_bus *
<parameter>bus
</parameter></paramdef>
51 <paramdef>const char *
<parameter>path
</parameter></paramdef>
52 <paramdef>const char *
<parameter>interface
</parameter></paramdef>
53 <paramdef>const char *
<parameter>property
</parameter></paramdef>
54 <paramdef>sd_bus_message *
<parameter>reply
</parameter></paramdef>
55 <paramdef>void *
<parameter>userdata
</parameter></paramdef>
56 <paramdef>sd_bus_error *
<parameter>ret_error
</parameter></paramdef>
60 <funcdef>typedef int (*
<function>sd_bus_property_set_t
</function>)
</funcdef>
61 <paramdef>sd_bus *
<parameter>bus
</parameter></paramdef>
62 <paramdef>const char *
<parameter>path
</parameter></paramdef>
63 <paramdef>const char *
<parameter>interface
</parameter></paramdef>
64 <paramdef>const char *
<parameter>property
</parameter></paramdef>
65 <paramdef>sd_bus_message *
<parameter>value
</parameter></paramdef>
66 <paramdef>void *
<parameter>userdata
</parameter></paramdef>
67 <paramdef>sd_bus_error *
<parameter>ret_error
</parameter></paramdef>
71 <funcdef>typedef int (*
<function>sd_bus_object_find_t
</function>)
</funcdef>
72 <paramdef>const char *
<parameter>path
</parameter></paramdef>
73 <paramdef>const char *
<parameter>interface
</parameter></paramdef>
74 <paramdef>void *
<parameter>userdata
</parameter></paramdef>
75 <paramdef>void **
<parameter>ret_found
</parameter></paramdef>
76 <paramdef>sd_bus_error *
<parameter>ret_error
</parameter></paramdef>
80 <funcdef>int
<function>sd_bus_add_object_vtable
</function></funcdef>
81 <paramdef>sd_bus *
<parameter>bus
</parameter></paramdef>
82 <paramdef>sd_bus_slot **
<parameter>slot
</parameter></paramdef>
83 <paramdef>const char *
<parameter>path
</parameter></paramdef>
84 <paramdef>const char *
<parameter>interface
</parameter></paramdef>
85 <paramdef>const sd_bus_vtable *
<parameter>vtable
</parameter></paramdef>
86 <paramdef>void *
<parameter>userdata
</parameter></paramdef>
90 <funcdef>int
<function>sd_bus_add_fallback_vtable
</function></funcdef>
91 <paramdef>sd_bus *
<parameter>bus
</parameter></paramdef>
92 <paramdef>sd_bus_slot **
<parameter>slot
</parameter></paramdef>
93 <paramdef>const char *
<parameter>prefix
</parameter></paramdef>
94 <paramdef>const char *
<parameter>interface
</parameter></paramdef>
95 <paramdef>const sd_bus_vtable *
<parameter>vtable
</parameter></paramdef>
96 <paramdef>sd_bus_object_find_t
<parameter>find
</parameter></paramdef>
97 <paramdef>void *
<parameter>userdata
</parameter></paramdef>
101 <constant>SD_BUS_VTABLE_START(
<replaceable>flags
</replaceable>)
</constant>
104 <constant>SD_BUS_VTABLE_END
</constant>
107 <constant>SD_BUS_METHOD_WITH_NAMES_OFFSET(
108 <replaceable>member
</replaceable>,
109 <replaceable>signature
</replaceable>,
110 <replaceable>in_names
</replaceable>,
111 <replaceable>result
</replaceable>,
112 <replaceable>out_names
</replaceable>,
113 <replaceable>handler
</replaceable>,
114 <replaceable>offset
</replaceable>,
115 <replaceable>flags
</replaceable>)
119 <constant>SD_BUS_METHOD_WITH_NAMES(
120 <replaceable>member
</replaceable>,
121 <replaceable>signature
</replaceable>,
122 <replaceable>in_names
</replaceable>,
123 <replaceable>result
</replaceable>,
124 <replaceable>out_names
</replaceable>,
125 <replaceable>handler
</replaceable>,
126 <replaceable>flags
</replaceable>)
130 <constant>SD_BUS_METHOD_WITH_OFFSET(
131 <replaceable>member
</replaceable>,
132 <replaceable>signature
</replaceable>,
133 <replaceable>result
</replaceable>,
134 <replaceable>handler
</replaceable>,
135 <replaceable>offset
</replaceable>,
136 <replaceable>flags
</replaceable>)
140 <constant>SD_BUS_METHOD(
141 <replaceable>member
</replaceable>,
142 <replaceable>signature
</replaceable>,
143 <replaceable>result
</replaceable>,
144 <replaceable>handler
</replaceable>,
145 <replaceable>flags
</replaceable>)
149 <constant>SD_BUS_SIGNAL_WITH_NAMES(
150 <replaceable>member
</replaceable>,
151 <replaceable>signature
</replaceable>,
152 <replaceable>names
</replaceable>,
153 <replaceable>flags
</replaceable>)
157 <constant>SD_BUS_SIGNAL(
158 <replaceable>member
</replaceable>,
159 <replaceable>signature
</replaceable>,
160 <replaceable>flags
</replaceable>)
164 <constant>SD_BUS_WRITABLE_PROPERTY(
165 <replaceable>member
</replaceable>,
166 <replaceable>signature
</replaceable>,
167 <replaceable>get
</replaceable>,
168 <replaceable>set
</replaceable>,
169 <replaceable>offset
</replaceable>,
170 <replaceable>flags
</replaceable>)
174 <constant>SD_BUS_PROPERTY(
175 <replaceable>member
</replaceable>,
176 <replaceable>signature
</replaceable>,
177 <replaceable>get
</replaceable>,
178 <replaceable>offset
</replaceable>,
179 <replaceable>flags
</replaceable>)
183 <constant>SD_BUS_PARAM(
<replaceable>name
</replaceable>)
</constant>
189 <title>Description
</title>
191 <para><function>sd_bus_add_object_vtable()
</function> is used to declare attributes for the path object
192 path
<parameter>path
</parameter> connected to the bus connection
<parameter>bus
</parameter> under the
193 interface
<parameter>interface
</parameter>. The table
<parameter>vtable
</parameter> may contain property
194 declarations using
<constant>SD_BUS_PROPERTY()
</constant> or
195 <constant>SD_BUS_WRITABLE_PROPERTY()
</constant>, method declarations using
196 <constant>SD_BUS_METHOD()
</constant>,
<constant>SD_BUS_METHOD_WITH_NAMES()
</constant>,
197 <constant>SD_BUS_METHOD_WITH_OFFSET()
</constant>, or
198 <constant>SD_BUS_METHOD_WITH_NAMES_OFFSET()
</constant>, and signal declarations using
199 <constant>SD_BUS_SIGNAL_WITH_NAMES()
</constant> or
<constant>SD_BUS_SIGNAL()
</constant>, see below. The
200 <replaceable>userdata
</replaceable> parameter contains a pointer that will be passed to various callback
201 functions. It may be specified as
<constant>NULL
</constant> if no value is necessary.
</para>
203 <para><function>sd_bus_add_object_vtable()
</function> is similar to
204 <function>sd_bus_add_object_vtable()
</function>, but is used to register
"fallback" attributes. When
205 looking for an attribute declaration, bus object paths registered with
206 <function>sd_bus_add_object_vtable()
</function> are checked first. If no match is found, the fallback
207 vtables are checked for each prefix of the bus object path, i.e. with the last slash-separated components
208 successively removed. This allows the vtable to be used for an arbitrary number of dynamically created
211 <para>Parameter
<replaceable>find
</replaceable> is a function which is used to locate the target object
212 based on the bus object path
<replaceable>path
</replaceable>. It must return
<constant>1</constant> and
213 set the
<parameter>ret_found
</parameter> output parameter if the object is found, return
214 <constant>0</constant> if the object was not found, and return a negative errno-style error code or
215 initialize the error structure
<replaceable>ret_error
</replaceable> on error. The pointer passed in
216 <parameter>ret_found
</parameter> will be used as the
<parameter>userdata
</parameter> parameter for the
217 callback functions (offset by the
<parameter>offset
</parameter> offsets as specified in the vtable
220 <para>For both functions, a match slot is created internally. If the output parameter
221 <replaceable>slot
</replaceable> is
<constant>NULL
</constant>, a
"floating" slot object is created, see
222 <citerefentry><refentrytitle>sd_bus_slot_set_floating
</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
223 Otherwise, a pointer to the slot object is returned. In that case, the reference to the slot object
224 should be dropped when the vtable is not needed anymore, see
225 <citerefentry><refentrytitle>sd_bus_slot_unref
</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
229 <title>The
<structname>sd_bus_vtable
</structname> array
</title>
231 <para>The array consists of the structures of type
<structname>sd_bus_vtable
</structname>, but it
232 should never be filled in manually, but through one of the following macros:
</para>
236 <term><constant>SD_BUS_VTABLE_START()
</constant></term>
237 <term><constant>SD_BUS_VTABLE_END
</constant></term>
239 <listitem><para>Those must always be the first and last element.
</para></listitem>
243 <term><constant>SD_BUS_METHOD_WITH_NAMES_OFFSET()
</constant></term>
244 <term><constant>SD_BUS_METHOD_WITH_NAMES()
</constant></term>
245 <term><constant>SD_BUS_METHOD_WITH_OFFSET()
</constant></term>
246 <term><constant>SD_BUS_METHOD()
</constant></term>
248 <listitem><para>Declare a D-Bus method with the name
<replaceable>member
</replaceable>, parameter
249 signature
<replaceable>signature
</replaceable>, result signature
<replaceable>result
</replaceable>.
250 Parameters
<replaceable>in_names
</replaceable> and
<replaceable>out_names
</replaceable> specify the
251 argument names of the input and output arguments in the function signature. The handler function
252 <replaceable>handler
</replaceable> must be of type
<function>sd_bus_message_handler_t
</function>.
253 It will be called to handle the incoming messages that call this method. It receives a pointer that
254 is the
<replaceable>userdata
</replaceable> parameter passed to the registration function offset by
255 <replaceable>offset
</replaceable> bytes. This may be used to pass pointers to different fields in
256 the same data structure to different methods in the same
257 vtable.
<replaceable>in_names
</replaceable> and
<replaceable>out_names
</replaceable> should be
258 created using the
<constant>SD_BUS_PARAM()
</constant> macro, see below. Parameter
259 <replaceable>flags
</replaceable> is a combination of flags, see below.
</para>
261 <para><constant>SD_BUS_METHOD_WITH_NAMES()
</constant>,
262 <constant>SD_BUS_METHOD_WITH_OFFSET()
</constant>, and
<constant>SD_BUS_METHOD()
</constant> are
263 variants which specify zero offset (
<replaceable>userdata
</replaceable> parameter is passed with
264 no change), leave the names unset (i.e. no parameter names), or both.
</para>
269 <term><constant>SD_BUS_SIGNAL_WITH_NAMES()
</constant></term>
270 <term><constant>SD_BUS_SIGNAL()
</constant></term>
272 <listitem><para>Declare a D-Bus signal with the name
<replaceable>member
</replaceable>,
273 parameter signature
<replaceable>signature
</replaceable>, and argument names
274 <replaceable>names
</replaceable>.
<replaceable>names
</replaceable> should be
275 created using the
<constant>SD_BUS_PARAM()
</constant> macro, see below.
276 Parameter
<replaceable>flags
</replaceable> is a combination of flags, see below.
279 <para>Equivalent to
<constant>SD_BUS_SIGNAL_WITH_NAMES()
</constant> with the
280 <replaceable>names
</replaceable> parameter unset (i.e. no parameter names).
</para>
285 <term><constant>SD_BUS_WRITABLE_PROPERTY()
</constant></term>
286 <term><constant>SD_BUS_PROPERTY()
</constant></term>
288 <listitem><para>Declare a D-Bus property with the name
<replaceable>member
</replaceable> and value
289 signature
<replaceable>signature
</replaceable>. Parameters
<replaceable>get
</replaceable> and
290 <replaceable>set
</replaceable> are the getter and setter methods. They are called with a pointer
291 that is the
<replaceable>userdata
</replaceable> parameter passed to the registration function
292 offset by
<replaceable>offset
</replaceable> bytes. This may be used pass pointers to different
293 fields in the same data structure to different setters and getters in the same vtable. Parameter
294 <replaceable>flags
</replaceable> is a combination of flags, see below.
</para>
296 <para>The setter and getter methods may be omitted (specified as
<constant>NULL
</constant>), if the
297 property has one of the basic types or
<literal>as
</literal> in case of read-only properties. In
298 those cases, the
<replaceable>userdata
</replaceable> and
<replaceable>offset
</replaceable>
299 parameters must together point to valid variable of the corresponding type. A default setter and
300 getters will be provided, which simply copy the argument between this variable and the message.
303 <para><constant>SD_BUS_PROPERTY()
</constant> is used to define a read-only property.
</para>
308 <term><constant>SD_BUS_PARAM()
</constant></term>
309 <listitem><para>Parameter names should be wrapped in this macro, see the example below.
</para>
318 <para>The
<replaceable>flags
</replaceable> parameter is used to specify a combination of
319 <ulink url=
"https://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format">D-Bus annotations
</ulink>.
324 <term><constant>SD_BUS_VTABLE_DEPRECATED
</constant></term>
326 <listitem><para>Mark this vtable entry as deprecated using the
327 <constant>org.freedesktop.DBus.Deprecated
</constant> annotation in introspection data. If
328 specified for
<constant>SD_BUS_VTABLE_START()
</constant>, the annotation is applied to the
329 enclosing interface.
</para></listitem>
333 <term><constant>SD_BUS_VTABLE_HIDDEN
</constant></term>
335 <listitem><para>Make this vtable entry hidden. It will not be shown in introspection data. If
336 specified for
<constant>SD_BUS_VTABLE_START()
</constant>, all entries in the array are hidden.
342 <term><constant>SD_BUS_VTABLE_UNPRIVILEGED
</constant></term>
344 <listitem><para>Mark this vtable entry as unprivileged. If not specified, the
345 <constant>org.freedesktop.systemd1.Privileged
</constant> annotation with value
346 <literal>true
</literal> will be shown in introspection data.
</para>
351 <term><constant>SD_BUS_VTABLE_METHOD_NO_REPLY
</constant></term>
353 <listitem><para>Mark his vtable entry as a method that will not return a reply using the
354 <constant>org.freedesktop.DBus.Method.NoReply
</constant> annotation in introspection data.
359 <term><constant>SD_BUS_VTABLE_CONST
</constant></term>
360 <term><constant>SD_BUS_VTABLE_EMITS_CHANGE
</constant></term>
361 <term><constant>SD_BUS_VTABLE_EMITS_INVALIDATION
</constant></term>
363 <listitem><para>Those three flags correspond to different values of the
364 <constant>org.freedesktop.DBus.Property.EmitsChangedSignal
</constant> annotation, which specifies
365 whether the
<constant>org.freedesktop.DBus.Properties.PropertiesChanged
</constant> signal is
366 emitted whenever the property changes.
<constant>SD_BUS_VTABLE_CONST
</constant> corresponds to
367 <constant>const
</constant> and means that the property never changes during the lifetime of the
368 object it belongs to, so no signal needs to be emitted.
369 <constant>SD_BUS_VTABLE_EMITS_CHANGE
</constant> corresponds to
<constant>true
</constant> and means
370 that the signal is emitted.
<constant>SD_BUS_VTABLE_EMITS_INVALIDATION
</constant> corresponds to
371 <constant>invalides
</constant> and means that the signal is emitted, but the value is not included
372 in the signal.
</para>
377 <term><constant>SD_BUS_VTABLE_PROPERTY_EXPLICIT
</constant></term>
379 <listitem><para>Mark this vtable property entry as requiring explicit request to for the value to
380 be shown (generally because the value is large or slow to calculate). This entry cannot be combined
381 with
<constant>SD_BUS_VTABLE_EMITS_CHANGE
</constant>, and will not be shown in property listings by
382 default (e.g.
<command>busctl introspect
</command>). This corresponds to the
383 <constant>org.freedesktop.systemd1.Explicit
</constant> annotation in introspection data.
</para>
391 <title>Examples
</title>
394 <title>Create a simple listener on the bus
</title>
396 <programlisting><xi:include href=
"vtable-example.c" parse=
"text" /></programlisting>
398 <para>This creates a simple client on the bus (the user bus, when run as normal user).
399 We may use the D-Bus
<constant>org.freedesktop.DBus.Introspectable.Introspect
</constant>
400 call to acquire the XML description of the interface:
</para>
402 <programlisting><xi:include href=
"vtable-example.xml" parse=
"text" /></programlisting>
407 <title>Return Value
</title>
409 <para>On success,
<function>sd_bus_add_object_vtable
</function> and
410 <function>sd_bus_add_fallback_vtable
</function> calls return
0 or a positive integer. On failure, they
411 return a negative errno-style error code.
</para>
414 <title>Errors
</title>
416 <para>Returned errors may indicate the following problems:
</para>
420 <term><constant>-EINVAL
</constant></term>
422 <listitem><para>One of the required parameters is
<constant>NULL
</constant> or invalid. A reserved
423 D-Bus interface was passed as the
<replaceable>interface
</replaceable> parameter.
</para></listitem>
427 <term><constant>-ENOPKG
</constant></term>
429 <listitem><para>The bus cannot be resolved.
</para></listitem>
433 <term><constant>-ECHILD
</constant></term>
435 <listitem><para>The bus was created in a different process.
</para></listitem>
439 <term><constant>-ENOMEM
</constant></term>
441 <listitem><para>Memory allocation failed.
</para></listitem>
445 <term><constant>-EPROTOTYPE
</constant></term>
447 <listitem><para><function>sd_bus_add_object_vtable
</function> and
448 <function>sd_bus_add_fallback_vtable
</function> have been both called
449 for the same bus object path, which is not allowed.
</para></listitem>
453 <term><constant>-EEXIST
</constant></term>
455 <listitem><para>This vtable has already been registered for this
456 <replaceable>interface
</replaceable> and
<replaceable>path
</replaceable>.
463 <xi:include href=
"libsystemd-pkgconfig.xml" />
466 <title>See Also
</title>
469 <citerefentry><refentrytitle>sd-bus
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
470 <citerefentry><refentrytitle>busctl
</refentrytitle><manvolnum>1</manvolnum></citerefentry>