1 <?xml version='1.0'?> <!--*- Mode: nxml; nxml-child-indent: 2; indent-tabs-mode: nil -*-->
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
6 SPDX-License-Identifier: LGPL-2.1+
8 Copyright 2016 Julian Orth
11 <refentry id="sd_bus_add_match">
14 <title>sd_bus_add_match</title>
15 <productname>systemd</productname>
19 <firstname>Julian</firstname>
20 <surname>Orth</surname>
21 <email>ju.orth@gmail.com</email>
27 <refentrytitle>sd_bus_add_match</refentrytitle>
28 <manvolnum>3</manvolnum>
32 <refname>sd_bus_add_match</refname>
33 <refname>sd_bus_add_match_async</refname>
34 <refname>sd_bus_match_signal</refname>
35 <refname>sd_bus_match_signal_async</refname>
37 <refpurpose>Add a match rule for incoming message dispatching</refpurpose>
42 <funcsynopsisinfo>#include <systemd/sd-bus.h></funcsynopsisinfo>
45 <funcdef>typedef int (*<function>sd_bus_message_handler_t</function>)</funcdef>
46 <paramdef>sd_bus_message *<parameter>m</parameter></paramdef>
47 <paramdef>void *<parameter>userdata</parameter></paramdef>
48 <paramdef>sd_bus_error *<parameter>ret_error</parameter></paramdef>
52 <funcdef>int <function>sd_bus_add_match</function></funcdef>
53 <paramdef>sd_bus *<parameter>bus</parameter></paramdef>
54 <paramdef>sd_bus_slot **<parameter>slot</parameter></paramdef>
55 <paramdef>const char *<parameter>match</parameter></paramdef>
56 <paramdef>sd_bus_message_handler_t <parameter>callback</parameter></paramdef>
57 <paramdef>void *<parameter>userdata</parameter></paramdef>
61 <funcdef>int <function>sd_bus_add_match_async</function></funcdef>
62 <paramdef>sd_bus *<parameter>bus</parameter></paramdef>
63 <paramdef>sd_bus_slot **<parameter>slot</parameter></paramdef>
64 <paramdef>const char *<parameter>match</parameter></paramdef>
65 <paramdef>sd_bus_message_handler_t <parameter>callback</parameter></paramdef>
66 <paramdef>sd_bus_message_handler_t <parameter>install_callback</parameter></paramdef>
67 <paramdef>void *<parameter>userdata</parameter></paramdef>
71 <funcdef>int <function>sd_bus_match_signal</function></funcdef>
72 <paramdef>sd_bus *<parameter>bus</parameter></paramdef>
73 <paramdef>sd_bus_slot **<parameter>slot</parameter></paramdef>
74 <paramdef>const char *<parameter>sender</parameter></paramdef>
75 <paramdef>const char *<parameter>path</parameter></paramdef>
76 <paramdef>const char *<parameter>interface</parameter></paramdef>
77 <paramdef>const char *<parameter>member</parameter></paramdef>
78 <paramdef>sd_bus_message_handler_t <parameter>callback</parameter></paramdef>
79 <paramdef>void *<parameter>userdata</parameter></paramdef>
83 <funcdef>int <function>sd_bus_match_signal_async</function></funcdef>
84 <paramdef>sd_bus *<parameter>bus</parameter></paramdef>
85 <paramdef>sd_bus_slot **<parameter>slot</parameter></paramdef>
86 <paramdef>const char *<parameter>sender</parameter></paramdef>
87 <paramdef>const char *<parameter>path</parameter></paramdef>
88 <paramdef>const char *<parameter>interface</parameter></paramdef>
89 <paramdef>const char *<parameter>member</parameter></paramdef>
90 <paramdef>sd_bus_message_handler_t <parameter>callback</parameter></paramdef>
91 <paramdef>sd_bus_message_handler_t <parameter>install_callback</parameter></paramdef>
92 <paramdef>void *<parameter>userdata</parameter></paramdef>
99 <title>Description</title>
101 <para><function>sd_bus_add_match()</function> installs a match rule for messages received on the specified bus
102 connection object <parameter>bus</parameter>. The syntax of the match rule expression passed in
103 <parameter>match</parameter> is described in the <ulink
104 url="https://dbus.freedesktop.org/doc/dbus-specification.html">D-Bus Specification</ulink>. The specified handler
105 function <parameter>callback</parameter> is called for eaching incoming message matching the specified expression,
106 the <parameter>userdata</parameter> parameter is passed as-is to the callback function. The match is installed
107 synchronously when connected to a bus broker, i.e. the call sends a control message requested the match to be added
108 to the broker and waits until the broker confirms the match has been installed successfully.</para>
110 <para><function>sd_bus_add_match_async()</function> operates very similar to
111 <function>sd_bus_match_signal()</function>, however it installs the match asynchronously, in a non-blocking
112 fashion: a request is sent to the broker, but the call does not wait for a response. The
113 <parameter>install_callback</parameter> function is called when the response is later received, with the response
114 message from the broker as parameter. If this function is specified as <constant>NULL</constant> a default
115 implementation is used that terminates the bus connection should installing the match fail.</para>
117 <para><function>sd_bus_match_signal()</function> is very similar to <function>sd_bus_add_match()</function>, but
118 only matches signals, and instead of a match expression accepts four parameters: <parameter>sender</parameter> (the
119 service name of the sender), <parameter>path</parameter> (the object path of the emitting object),
120 <parameter>interface</parameter> (the interface the signal belongs to), <parameter>member</parameter> (the signal
121 name), from which the match string is internally generated. Optionally, these parameters may be specified as
122 <constant>NULL</constant> in which case the relevant field of incoming signals is not tested.</para>
124 <para><function>sd_bus_match_signal_async()</function> combines the signal matching logic of
125 <function>sd_bus_match_signal()</function> with the asynchronous behaviour of
126 <function>sd_bus_add_match_async()</function>.</para>
128 <para>On success, and if non-<constant>NULL</constant>, the <parameter>slot</parameter> return parameter will be
129 set to a slot object that may be used as a reference to the installed match, and may be utilized to remove it again
131 <citerefentry><refentrytitle>sd_bus_slot_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>. If specified
132 as <constant>NULL</constant> the lifetime of the match is bound to the lifetime of the bus object itself, and the
133 match is generally not removed independently. See
134 <citerefentry><refentrytitle>sd_bus_slot_set_floating</refentrytitle><manvolnum>3</manvolnum></citerefentry> for
137 <para>The message <parameter>m</parameter> passed to the callback is only borrowed, that is, the callback should
138 not call <citerefentry><refentrytitle>sd_bus_message_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>
139 on it. If the callback wants to hold on to the message beyond the lifetime of the callback, it needs to call
140 <citerefentry><refentrytitle>sd_bus_message_ref</refentrytitle><manvolnum>3</manvolnum></citerefentry> to create a
141 new reference.</para>
143 <para>If an error occurs during the callback invocation, the callback should return a negative error number
144 (optionally, a more precise error may be returned in <parameter>ret_error</parameter>, as well). If it wants other
145 callbacks that match the same rule to be called, it should return 0. Otherwise it should return a positive integer.
148 <para>If the <parameter>bus</parameter> refers to a direct connection (i.e. not a bus connection, as set with
149 <citerefentry><refentrytitle>sd_bus_set_bus_client</refentrytitle><manvolnum>3</manvolnum></citerefentry>) the
150 match is only installed on the client side, and the synchronous and asynchronous functions operate the same.</para>
154 <title>Return Value</title>
157 On success, <function>sd_bus_add_match()</function> and the other calls return 0 or a positive integer. On
158 failure, they return a negative errno-style error code.
165 <para><function>sd_bus_add_match()</function> and the other functions described here are available as a shared
166 library, which can be compiled and linked to with the <constant>libelogind</constant> <citerefentry
167 project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry> file.</para>
171 <title>See Also</title>
174 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
175 <citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
176 <citerefentry><refentrytitle>sd_bus_slot_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
177 <citerefentry><refentrytitle>sd_bus_message_ref</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
178 <citerefentry><refentrytitle>sd_bus_set_bus_client</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
179 <citerefentry><refentrytitle>sd_bus_slot_set_floating</refentrytitle><manvolnum>3</manvolnum></citerefentry>