]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/sd_bus_default.xml
travis: add more ASan options
[thirdparty/systemd.git] / man / sd_bus_default.xml
1 <?xml version='1.0'?>
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_default" xmlns:xi="http://www.w3.org/2001/XInclude">
7
8 <refentryinfo>
9 <title>sd_bus_default</title>
10 <productname>systemd</productname>
11 </refentryinfo>
12
13 <refmeta>
14 <refentrytitle>sd_bus_default</refentrytitle>
15 <manvolnum>3</manvolnum>
16 </refmeta>
17
18 <refnamediv>
19 <refname>sd_bus_default</refname>
20 <refname>sd_bus_default_user</refname>
21 <refname>sd_bus_default_system</refname>
22
23 <refname>sd_bus_open</refname>
24 <refname>sd_bus_open_with_description</refname>
25 <refname>sd_bus_open_user</refname>
26 <refname>sd_bus_open_user_with_description</refname>
27 <refname>sd_bus_open_system</refname>
28 <refname>sd_bus_open_system_with_description</refname>
29 <refname>sd_bus_open_system_remote</refname>
30 <refname>sd_bus_open_system_machine</refname>
31
32 <refpurpose>Acquire a connection to a system or user bus</refpurpose>
33 </refnamediv>
34
35 <refsynopsisdiv>
36 <funcsynopsis>
37 <funcsynopsisinfo>#include &lt;systemd/sd-bus.h&gt;</funcsynopsisinfo>
38
39 <funcprototype>
40 <funcdef>int <function>sd_bus_default</function></funcdef>
41 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
42 </funcprototype>
43
44 <funcprototype>
45 <funcdef>int <function>sd_bus_default_user</function></funcdef>
46 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
47 </funcprototype>
48
49 <funcprototype>
50 <funcdef>int <function>sd_bus_default_system</function></funcdef>
51 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
52 </funcprototype>
53
54 <funcprototype>
55 <funcdef>int <function>sd_bus_open</function></funcdef>
56 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
57 </funcprototype>
58
59 <funcprototype>
60 <funcdef>int <function>sd_bus_open_with_description</function></funcdef>
61 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
62 <paramdef>const char *<parameter>description</parameter></paramdef>
63 </funcprototype>
64
65 <funcprototype>
66 <funcdef>int <function>sd_bus_open_user</function></funcdef>
67 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
68 </funcprototype>
69
70 <funcprototype>
71 <funcdef>int <function>sd_bus_open_user_with_description</function></funcdef>
72 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
73 <paramdef>const char *<parameter>description</parameter></paramdef>
74 </funcprototype>
75
76 <funcprototype>
77 <funcdef>int <function>sd_bus_open_system</function></funcdef>
78 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
79 </funcprototype>
80
81 <funcprototype>
82 <funcdef>int <function>sd_bus_open_system_with_description</function></funcdef>
83 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
84 <paramdef>const char *<parameter>description</parameter></paramdef>
85 </funcprototype>
86
87 <funcprototype>
88 <funcdef>int <function>sd_bus_open_system_remote</function></funcdef>
89 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
90 <paramdef>const char *<parameter>host</parameter></paramdef>
91 </funcprototype>
92
93 <funcprototype>
94 <funcdef>int <function>sd_bus_open_system_machine</function></funcdef>
95 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
96 <paramdef>const char *<parameter>machine</parameter></paramdef>
97 </funcprototype>
98
99 </funcsynopsis>
100 </refsynopsisdiv>
101
102 <refsect1>
103 <title>Description</title>
104
105 <para><function>sd_bus_default()</function> acquires a bus
106 connection object to the user bus when invoked in user context, or
107 to the system bus otherwise. The connection object is associated
108 with the calling thread. Each time the function is invoked from
109 the same thread, the same object is returned, but its reference
110 count is increased by one, as long as at least one reference is
111 kept. When the last reference to the connection is dropped (using
112 the
113 <citerefentry><refentrytitle>sd_bus_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>
114 call), the connection is terminated. Note that the connection is
115 not automatically terminated when the associated thread ends. It
116 is important to drop the last reference to the bus connection
117 explicitly before the thread ends, as otherwise, the connection will
118 leak. Also, queued but unread or unwritten messages keep the
119 bus referenced, see below.</para>
120
121 <para><function>sd_bus_default_user()</function> returns a user
122 bus connection object associated with the calling thread.
123 <function>sd_bus_default_system()</function> is similar, but
124 connects to the system bus. Note that
125 <function>sd_bus_default()</function> is identical to these two
126 calls, depending on the execution context.</para>
127
128 <para><function>sd_bus_open()</function> creates a new,
129 independent bus connection to the user bus when invoked in user
130 context, or the system bus
131 otherwise. <function>sd_bus_open_user()</function> is similar, but
132 connects only to the user bus.
133 <function>sd_bus_open_system()</function> does the same, but
134 connects to the system bus. In contrast to
135 <function>sd_bus_default()</function>,
136 <function>sd_bus_default_user()</function>, and
137 <function>sd_bus_default_system()</function>, these calls return
138 new, independent connection objects that are not associated with
139 the invoking thread and are not shared between multiple
140 invocations. It is recommended to share connections per thread to
141 efficiently make use the available resources. Thus, it is
142 recommended to use <function>sd_bus_default()</function>,
143 <function>sd_bus_default_user()</function> and
144 <function>sd_bus_default_system()</function> to connect to the
145 user or system buses.</para>
146
147 <para><function>sd_bus_open_with_description()</function>,
148 <function>sd_bus_open_user_with_description()</function>, and
149 <function>sd_bus_open_system_with_description()</function> are similar to
150 <function>sd_bus_open()</function>, <function>sd_bus_open_user()</function>, and
151 <function>sd_bus_open_system()</function>, but allow a description string to be set, see
152 <citerefentry><refentrytitle>sd_bus_set_description</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
153 <parameter>description</parameter> may be <constant>NULL</constant>, in which case this function
154 is equivalent to <function>sd_bus_open()</function>. This description string is used in log
155 messages about the bus object, and including a "name" for the bus makes them easier to
156 understand. Some messages are emitted during bus initialization, hence using this function is
157 prefereable to setting the description later with
158 <function>sd_bus_open_with_description()</function>. The argument is copied internally and will
159 not be referenced after the function returns.</para>
160
161 <para>If the <varname>$DBUS_SESSION_BUS_ADDRESS</varname> environment
162 variable is set
163 (cf. <citerefentry project='man-pages'><refentrytitle>environ</refentrytitle><manvolnum>7</manvolnum></citerefentry>),
164 it will be used as the address of the user bus. This variable can
165 contain multiple addresses separated by <literal>;</literal>. If
166 this variable is not set, a suitable default for the default user
167 D-Bus instance will be used.</para>
168
169 <para>If the <varname>$DBUS_SYSTEM_BUS_ADDRESS</varname>
170 environment variable is set, it will be used as the address of the
171 system bus. This variable uses the same syntax as
172 <varname>$DBUS_SESSION_BUS_ADDRESS</varname>. If this variable is
173 not set, a suitable default for the default system D-Bus instance
174 will be used.</para>
175
176 <para><function>sd_bus_open_system_remote()</function> connects to the system bus on
177 the specified host using
178 <citerefentry project='die-net'><refentrytitle>ssh</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
179 <parameter>host</parameter> consists of an optional user name followed by the
180 <literal>@</literal> symbol, and the hostname, optionally followed by a
181 <literal>:</literal> and a port, optionally followed by a
182 <literal>/</literal> and a machine name. If the machine name is given, a connection
183 is created to the system bus in the specified container on the remote machine, and
184 otherwise a connection to the system bus on the specified host is created.</para>
185
186 <para>Note that entering a container is a privileged operation, and will likely only
187 work for the root user on the remote machine.</para>
188
189 <para><function>sd_bus_open_system_machine()</function> connects
190 to the system bus in the specified <parameter>machine</parameter>,
191 where <parameter>machine</parameter> is the name of a local
192 container. See
193 <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
194 for more information about the "machine" concept. Note that
195 connections into local containers are only available to privileged
196 processes at this time.</para>
197
198 <para>These calls allocate a bus connection object and initiate
199 the connection to a well-known bus of some form. An alternative to
200 using these high-level calls is to create an unconnected bus
201 object with
202 <citerefentry><refentrytitle>sd_bus_new</refentrytitle><manvolnum>3</manvolnum></citerefentry>
203 and to connect it with
204 <citerefentry><refentrytitle>sd_bus_start</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
205 </para>
206
207 </refsect1>
208
209 <refsect1>
210 <title>Reference ownership</title>
211 <para>The functions <function>sd_bus_open()</function>,
212 <function>sd_bus_open_user()</function>,
213 <function>sd_bus_open_system()</function>,
214 <function>sd_bus_open_system_remote()</function>, and
215 <function>sd_bus_open_system_machine()</function> return a new
216 connection object and the caller owns the sole reference. When not
217 needed anymore, this reference should be destroyed with
218 <citerefentry><refentrytitle>sd_bus_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
219 </para>
220
221 <para>The functions <function>sd_bus_default()</function>,
222 <function>sd_bus_default_user()</function> and
223 <function>sd_bus_default_system()</function> do not necessarily
224 create a new object, but increase the connection reference of an
225 existing connection object by one. Use
226 <citerefentry><refentrytitle>sd_bus_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>
227 to drop the reference.</para>
228
229 <para>Queued but unwritten/unread messages keep a reference to their bus connection object. For this reason, even
230 if an application dropped all references to a bus connection, it might not get destroyed right away. Until all
231 incoming queued messages are read, and until all outgoing unwritten messages are written, the bus object will stay
232 alive. <function>sd_bus_flush()</function> may be used to write all outgoing queued messages so they drop their
233 references. To flush the unread incoming messages, use <function>sd_bus_close()</function>, which will also close
234 the bus connection. When using the default bus logic, it is a good idea to first invoke
235 <function>sd_bus_flush()</function> followed by <function>sd_bus_close()</function> when a thread or process
236 terminates, and thus its bus connection object should be freed.</para>
237
238 <para>Normally, slot objects (as created by
239 <citerefentry><refentrytitle>sd_bus_add_match</refentrytitle><manvolnum>3</manvolnum></citerefentry> and similar
240 calls) keep a reference to their bus connection object, too. Thus, as long as a bus slot object remains referenced
241 its bus object will remain allocated too. Optionally, bus slot objects may be placed in "floating" mode. When in
242 floating mode the life cycle of the bus slot object is bound to the bus object, i.e. when the bus object is freed
243 the bus slot object is automatically unreferenced too. The floating state of a slot object may be controlled
244 explicitly with
245 <citerefentry><refentrytitle>sd_bus_slot_set_floating</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
246 though usually floating bus slot objects are created by passing <constant>NULL</constant> as the
247 <parameter>slot</parameter> parameter of <function>sd_bus_add_match()</function> and related calls, thus indicating
248 that the caller is not directly interested in referencing and managing the bus slot object.</para>
249
250 <para>The life cycle of the default bus connection should be the
251 responsibility of the code that creates/owns the thread the
252 default bus connection object is associated with. Library code
253 should neither call <function>sd_bus_flush()</function> nor
254 <function>sd_bus_close()</function> on default bus objects unless
255 it does so in its own private, self-allocated thread. Library code
256 should not use the default bus object in other threads unless it
257 is clear that the program using it will life cycle the bus
258 connection object and flush and close it before exiting from the
259 thread. In libraries where it is not clear that the calling
260 program will life cycle the bus connection object, it is hence
261 recommended to use <function>sd_bus_open_system()</function>
262 instead of <function>sd_bus_default_system()</function> and
263 related calls.</para>
264 </refsect1>
265
266 <refsect1>
267 <title>Return Value</title>
268
269 <para>On success, these calls return 0 or a positive
270 integer. On failure, these calls return a negative
271 errno-style error code.</para>
272
273 <refsect2>
274 <title>Errors</title>
275
276 <para>Returned errors may indicate the following problems:</para>
277
278 <variablelist>
279
280 <varlistentry>
281 <term><constant>-EINVAL</constant></term>
282
283 <listitem><para>The specified parameters are invalid.</para></listitem>
284 </varlistentry>
285
286 <varlistentry>
287 <term><constant>-ENOMEM</constant></term>
288
289 <listitem><para>Memory allocation failed.</para></listitem>
290 </varlistentry>
291
292 <varlistentry>
293 <term><constant>-ESOCKTNOSUPPORT</constant></term>
294
295 <listitem><para>The protocol version required to connect to the selected bus is not
296 supported.</para></listitem>
297 </varlistentry>
298 </variablelist>
299
300 <para>In addition, any further connection-related errors may be by returned. See
301 <citerefentry><refentrytitle>sd_bus_send</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
302 </refsect2>
303 </refsect1>
304
305 <xi:include href="libsystemd-pkgconfig.xml" />
306
307 <refsect1>
308 <title>See Also</title>
309
310 <para>
311 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
312 <citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
313 <citerefentry><refentrytitle>sd_bus_new</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
314 <citerefentry><refentrytitle>sd_bus_ref</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
315 <citerefentry><refentrytitle>sd_bus_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
316 <citerefentry project='die-net'><refentrytitle>ssh</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
317 <citerefentry><refentrytitle>systemd-machined.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
318 <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
319 </para>
320 </refsect1>
321
322 </refentry>