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