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