]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/sd_bus_default.xml
Merge pull request #8822 from fbuihuu/rfc-tmpfiles-safe-upstream
[thirdparty/systemd.git] / man / sd_bus_default.xml
1 <?xml version='1.0'?>
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4
5 <!--
6 SPDX-License-Identifier: LGPL-2.1+
7 -->
8
9 <refentry id="sd_bus_default" xmlns:xi="http://www.w3.org/2001/XInclude">
10
11 <refentryinfo>
12 <title>sd_bus_default</title>
13 <productname>systemd</productname>
14 </refentryinfo>
15
16 <refmeta>
17 <refentrytitle>sd_bus_default</refentrytitle>
18 <manvolnum>3</manvolnum>
19 </refmeta>
20
21 <refnamediv>
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>
27 <refname>sd_bus_open_with_description</refname>
28 <refname>sd_bus_open_user</refname>
29 <refname>sd_bus_open_user_with_description</refname>
30 <refname>sd_bus_open_system</refname>
31 <refname>sd_bus_open_system_with_description</refname>
32 <refname>sd_bus_open_system_remote</refname>
33 <refname>sd_bus_open_system_machine</refname>
34
35 <refpurpose>Acquire a connection to a system or user bus</refpurpose>
36 </refnamediv>
37
38 <refsynopsisdiv>
39 <funcsynopsis>
40 <funcsynopsisinfo>#include &lt;systemd/sd-bus.h&gt;</funcsynopsisinfo>
41
42 <funcprototype>
43 <funcdef>int <function>sd_bus_default</function></funcdef>
44 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
45 </funcprototype>
46
47 <funcprototype>
48 <funcdef>int <function>sd_bus_default_user</function></funcdef>
49 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
50 </funcprototype>
51
52 <funcprototype>
53 <funcdef>int <function>sd_bus_default_system</function></funcdef>
54 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
55 </funcprototype>
56
57 <funcprototype>
58 <funcdef>int <function>sd_bus_open</function></funcdef>
59 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
60 </funcprototype>
61
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
68 <funcprototype>
69 <funcdef>int <function>sd_bus_open_user</function></funcdef>
70 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
71 </funcprototype>
72
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
79 <funcprototype>
80 <funcdef>int <function>sd_bus_open_system</function></funcdef>
81 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
82 </funcprototype>
83
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
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
102 </funcsynopsis>
103 </refsynopsisdiv>
104
105 <refsect1>
106 <title>Description</title>
107
108 <para><function>sd_bus_default()</function> acquires a bus
109 connection object to the user bus when invoked in user context, or
110 to the system bus otherwise. The connection object is associated
111 with the calling thread. Each time the function is invoked from
112 the same thread, the same object is returned, but its reference
113 count is increased by one, as long as at least one reference is
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
120 explicitly before the thread ends, as otherwise, the connection will
121 leak. Also, queued but unread or unwritten messages keep the
122 bus referenced, see below.</para>
123
124 <para><function>sd_bus_default_user()</function> returns a user
125 bus connection object associated with the calling thread.
126 <function>sd_bus_default_system()</function> is similar, but
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>
130
131 <para><function>sd_bus_open()</function> creates a new,
132 independent bus connection to the user bus when invoked in user
133 context, or the system bus
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>,
139 <function>sd_bus_default_user()</function>, and
140 <function>sd_bus_default_system()</function>, these calls return
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>,
146 <function>sd_bus_default_user()</function> and
147 <function>sd_bus_default_system()</function> to connect to the
148 user or system buses.</para>
149
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
164 <para>If the <varname>$DBUS_SESSION_BUS_ADDRESS</varname> environment
165 variable is set
166 (cf. <citerefentry project='man-pages'><refentrytitle>environ</refentrytitle><manvolnum>7</manvolnum></citerefentry>),
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
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
176 not set, a suitable default for the default system D-Bus instance
177 will be used.</para>
178
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>
190
191 <para><function>sd_bus_open_system_machine()</function> connects
192 to the system bus in the specified <parameter>machine</parameter>,
193 where <parameter>machine</parameter> is the name of a local
194 container. See
195 <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
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
201 the connection to a well-known bus of some form. An alternative to
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>
208
209 </refsect1>
210
211 <refsect1>
212 <title>Reference ownership</title>
213 <para>The functions <function>sd_bus_open()</function>,
214 <function>sd_bus_open_user()</function>,
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
218 connection object and the caller owns the sole reference. When not
219 needed anymore, this reference should be destroyed with
220 <citerefentry><refentrytitle>sd_bus_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
221 </para>
222
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
226 create a new object, but increase the connection reference of an
227 existing connection object by one. Use
228 <citerefentry><refentrytitle>sd_bus_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>
229 to drop the reference.</para>
230
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>
251
252 <para>The life cycle of the default bus connection should be the
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
259 is clear that the program using it will life cycle the bus
260 connection object and flush and close it before exiting from the
261 thread. In libraries where it is not clear that the calling
262 program will life cycle the bus connection object, it is hence
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>
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>
284 <term><constant>-EINVAL</constant></term>
285
286 <listitem><para>The specified parameters are invalid.</para></listitem>
287 </varlistentry>
288
289 <varlistentry>
290 <term><constant>-ENOMEM</constant></term>
291
292 <listitem><para>Memory allocation failed.</para></listitem>
293 </varlistentry>
294
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>
300 </variablelist>
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>
304 </refsect1>
305
306 <xi:include href="libsystemd-pkgconfig.xml" />
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>,
317 <citerefentry project='die-net'><refentrytitle>ssh</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
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>