]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - man/sd_bus_add_object_vtable.xml
man: fix reference to sd_bus_add_fallback_vtable
[thirdparty/systemd.git] / man / sd_bus_add_object_vtable.xml
... / ...
CommitLineData
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+ -->
5
6<refentry id="sd_bus_add_object_vtable"
7 xmlns:xi="http://www.w3.org/2001/XInclude">
8
9 <refentryinfo>
10 <title>sd_bus_add_object_vtable</title>
11 <productname>systemd</productname>
12 </refentryinfo>
13
14 <refmeta>
15 <refentrytitle>sd_bus_add_object_vtable</refentrytitle>
16 <manvolnum>3</manvolnum>
17 </refmeta>
18
19 <refnamediv>
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>
33
34 <refpurpose>Declare properties and methods for a D-Bus path</refpurpose>
35 </refnamediv>
36
37 <refsynopsisdiv>
38 <funcsynopsis>
39 <funcsynopsisinfo>#include &lt;systemd/sd-bus-vtable.h&gt;</funcsynopsisinfo>
40
41 <funcprototype>
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>
46 </funcprototype>
47
48 <funcprototype>
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>
57 </funcprototype>
58
59 <funcprototype>
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>
68 </funcprototype>
69
70 <funcprototype>
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>
77 </funcprototype>
78
79 <funcprototype>
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>
87 </funcprototype>
88
89 <funcprototype>
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>
98 </funcprototype>
99
100 <para>
101 <constant>SD_BUS_VTABLE_START(<replaceable>flags</replaceable>)</constant>
102 </para>
103 <para>
104 <constant>SD_BUS_VTABLE_END</constant>
105 </para>
106 <para>
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>)
116 </constant>
117 </para>
118 <para>
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>)
127 </constant>
128 </para>
129 <para>
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>)
137 </constant>
138 </para>
139 <para>
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>)
146 </constant>
147 </para>
148 <para>
149 <constant>SD_BUS_SIGNAL_WITH_NAMES(
150 <replaceable>member</replaceable>,
151 <replaceable>signature</replaceable>,
152 <replaceable>names</replaceable>,
153 <replaceable>flags</replaceable>)
154 </constant>
155 </para>
156 <para>
157 <constant>SD_BUS_SIGNAL(
158 <replaceable>member</replaceable>,
159 <replaceable>signature</replaceable>,
160 <replaceable>flags</replaceable>)
161 </constant>
162 </para>
163 <para>
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>)
171 </constant>
172 </para>
173 <para>
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>)
180 </constant>
181 </para>
182 <para>
183 <constant>SD_BUS_PARAM(<replaceable>name</replaceable>)</constant>
184 </para>
185 </funcsynopsis>
186 </refsynopsisdiv>
187
188 <refsect1>
189 <title>Description</title>
190
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>
202
203 <para><function>sd_bus_add_fallback_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
209 objects.</para>
210
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
218 entries).</para>
219
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>.
226 </para>
227
228 <refsect2>
229 <title>The <structname>sd_bus_vtable</structname> array</title>
230
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>
233
234 <variablelist>
235 <varlistentry>
236 <term><constant>SD_BUS_VTABLE_START()</constant></term>
237 <term><constant>SD_BUS_VTABLE_END</constant></term>
238
239 <listitem><para>Those must always be the first and last element.</para></listitem>
240 </varlistentry>
241
242 <varlistentry>
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>
247
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>
260
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>
265 </listitem>
266 </varlistentry>
267
268 <varlistentry>
269 <term><constant>SD_BUS_SIGNAL_WITH_NAMES()</constant></term>
270 <term><constant>SD_BUS_SIGNAL()</constant></term>
271
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.
277 </para>
278
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>
281 </listitem>
282 </varlistentry>
283
284 <varlistentry>
285 <term><constant>SD_BUS_WRITABLE_PROPERTY()</constant></term>
286 <term><constant>SD_BUS_PROPERTY()</constant></term>
287
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>
295
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.
301 </para>
302
303 <para><constant>SD_BUS_PROPERTY()</constant> is used to define a read-only property.</para>
304 </listitem>
305 </varlistentry>
306
307 <varlistentry>
308 <term><constant>SD_BUS_PARAM()</constant></term>
309 <listitem><para>Parameter names should be wrapped in this macro, see the example below.</para>
310 </listitem>
311 </varlistentry>
312 </variablelist>
313 </refsect2>
314
315 <refsect2>
316 <title>Flags</title>
317
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>.
320 </para>
321
322 <variablelist>
323 <varlistentry>
324 <term><constant>SD_BUS_VTABLE_DEPRECATED</constant></term>
325
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>
330 </varlistentry>
331
332 <varlistentry>
333 <term><constant>SD_BUS_VTABLE_HIDDEN</constant></term>
334
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.
337 </para>
338 </listitem>
339 </varlistentry>
340
341 <varlistentry>
342 <term><constant>SD_BUS_VTABLE_UNPRIVILEGED</constant></term>
343
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>
347 </listitem>
348 </varlistentry>
349
350 <varlistentry>
351 <term><constant>SD_BUS_VTABLE_METHOD_NO_REPLY</constant></term>
352
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.
355 </para></listitem>
356 </varlistentry>
357
358 <varlistentry>
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>
362
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>
373 </listitem>
374 </varlistentry>
375
376 <varlistentry>
377 <term><constant>SD_BUS_VTABLE_PROPERTY_EXPLICIT</constant></term>
378
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>
384 </listitem>
385 </varlistentry>
386 </variablelist>
387 </refsect2>
388 </refsect1>
389
390 <refsect1>
391 <title>Examples</title>
392
393 <example>
394 <title>Create a simple listener on the bus</title>
395
396 <programlisting><xi:include href="vtable-example.c" parse="text" /></programlisting>
397
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>
401
402 <programlisting><xi:include href="vtable-example.xml" parse="text" /></programlisting>
403 </example>
404 </refsect1>
405
406 <refsect1>
407 <title>Return Value</title>
408
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>
412
413 <refsect2>
414 <title>Errors</title>
415
416 <para>Returned errors may indicate the following problems:</para>
417
418 <variablelist>
419 <varlistentry>
420 <term><constant>-EINVAL</constant></term>
421
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>
424 </varlistentry>
425
426 <varlistentry>
427 <term><constant>-ENOPKG</constant></term>
428
429 <listitem><para>The bus cannot be resolved.</para></listitem>
430 </varlistentry>
431
432 <varlistentry>
433 <term><constant>-ECHILD</constant></term>
434
435 <listitem><para>The bus was created in a different process.</para></listitem>
436 </varlistentry>
437
438 <varlistentry>
439 <term><constant>-ENOMEM</constant></term>
440
441 <listitem><para>Memory allocation failed.</para></listitem>
442 </varlistentry>
443
444 <varlistentry>
445 <term><constant>-EPROTOTYPE</constant></term>
446
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>
450 </varlistentry>
451
452 <varlistentry>
453 <term><constant>-EEXIST</constant></term>
454
455 <listitem><para>This vtable has already been registered for this
456 <replaceable>interface</replaceable> and <replaceable>path</replaceable>.
457 </para></listitem>
458 </varlistentry>
459 </variablelist>
460 </refsect2>
461 </refsect1>
462
463 <xi:include href="libsystemd-pkgconfig.xml" />
464
465 <refsect1>
466 <title>See Also</title>
467
468 <para>
469 <citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
470 <citerefentry><refentrytitle>busctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
471 </para>
472 </refsect1>
473</refentry>