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