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