]> git.ipfire.org Git - thirdparty/systemd.git/blame - man/sd_bus_default.xml
verify: use manager_load_startable_unit_or_warn() to load units for verification
[thirdparty/systemd.git] / man / sd_bus_default.xml
CommitLineData
3802a3d3 1<?xml version='1.0'?> <!--*- Mode: nxml; nxml-child-indent: 2; indent-tabs-mode: nil -*-->
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
ZJS
6 SPDX-License-Identifier: LGPL-2.1+
7
5de0ccff 8 This file is part of systemd.
cd6d5e1c 9
5de0ccff 10 Copyright 2014 Zbigniew Jędrzejewski-Szmek
cd6d5e1c 11
5de0ccff
ZJS
12 systemd is free software; you can redistribute it and/or modify it
13 under the terms of the GNU Lesser General Public License as published by
14 the Free Software Foundation; either version 2.1 of the License, or
15 (at your option) any later version.
cd6d5e1c 16
5de0ccff
ZJS
17 systemd is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
cd6d5e1c 21
5de0ccff
ZJS
22 You should have received a copy of the GNU Lesser General Public License
23 along with systemd; If not, see <http://www.gnu.org/licenses/>.
cd6d5e1c
ZJS
24-->
25
48f69d8f 26<refentry id="sd_bus_default">
cd6d5e1c
ZJS
27
28 <refentryinfo>
4a2af8d7 29 <title>sd_bus_default</title>
cd6d5e1c
ZJS
30 <productname>systemd</productname>
31
32 <authorgroup>
33 <author>
34 <contrib>A monkey with a typewriter</contrib>
35 <firstname>Zbigniew</firstname>
36 <surname>Jędrzejewski-Szmek</surname>
37 <email>zbyszek@in.waw.pl</email>
38 </author>
39 </authorgroup>
40 </refentryinfo>
41
42 <refmeta>
4a2af8d7 43 <refentrytitle>sd_bus_default</refentrytitle>
cd6d5e1c
ZJS
44 <manvolnum>3</manvolnum>
45 </refmeta>
46
47 <refnamediv>
4a2af8d7
LP
48 <refname>sd_bus_default</refname>
49 <refname>sd_bus_default_user</refname>
50 <refname>sd_bus_default_system</refname>
51
52 <refname>sd_bus_open</refname>
cd6d5e1c
ZJS
53 <refname>sd_bus_open_user</refname>
54 <refname>sd_bus_open_system</refname>
55 <refname>sd_bus_open_system_remote</refname>
4a2af8d7 56 <refname>sd_bus_open_system_machine</refname>
cd6d5e1c 57
4a2af8d7 58 <refpurpose>Acquire a connection to a system or user bus</refpurpose>
cd6d5e1c
ZJS
59 </refnamediv>
60
61 <refsynopsisdiv>
62 <funcsynopsis>
63 <funcsynopsisinfo>#include &lt;systemd/sd-bus.h&gt;</funcsynopsisinfo>
64
65 <funcprototype>
4a2af8d7 66 <funcdef>int <function>sd_bus_default</function></funcdef>
8dc385e7 67 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
cd6d5e1c
ZJS
68 </funcprototype>
69
70 <funcprototype>
4a2af8d7 71 <funcdef>int <function>sd_bus_default_user</function></funcdef>
8dc385e7 72 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
cd6d5e1c
ZJS
73 </funcprototype>
74
75 <funcprototype>
4a2af8d7 76 <funcdef>int <function>sd_bus_default_system</function></funcdef>
8dc385e7 77 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
cd6d5e1c
ZJS
78 </funcprototype>
79
80 <funcprototype>
4a2af8d7 81 <funcdef>int <function>sd_bus_open</function></funcdef>
8dc385e7 82 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
cd6d5e1c
ZJS
83 </funcprototype>
84
85 <funcprototype>
4a2af8d7 86 <funcdef>int <function>sd_bus_open_user</function></funcdef>
8dc385e7 87 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
cd6d5e1c
ZJS
88 </funcprototype>
89
90 <funcprototype>
4a2af8d7 91 <funcdef>int <function>sd_bus_open_system</function></funcdef>
8dc385e7 92 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
cd6d5e1c 93 </funcprototype>
4a2af8d7
LP
94
95 <funcprototype>
96 <funcdef>int <function>sd_bus_open_system_remote</function></funcdef>
97 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
98 <paramdef>const char *<parameter>host</parameter></paramdef>
99 </funcprototype>
100
101 <funcprototype>
102 <funcdef>int <function>sd_bus_open_system_machine</function></funcdef>
103 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
104 <paramdef>const char *<parameter>machine</parameter></paramdef>
105 </funcprototype>
106
cd6d5e1c
ZJS
107 </funcsynopsis>
108 </refsynopsisdiv>
109
110 <refsect1>
111 <title>Description</title>
112
4a2af8d7 113 <para><function>sd_bus_default()</function> acquires a bus
850df10a 114 connection object to the user bus when invoked in user context, or
4a2af8d7 115 to the system bus otherwise. The connection object is associated
b898f9bd 116 with the calling thread. Each time the function is invoked from
b938cb90 117 the same thread, the same object is returned, but its reference
b898f9bd 118 count is increased by one, as long as at least one reference is
850df10a
LP
119 kept. When the last reference to the connection is dropped (using
120 the
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
66f756d4 155 <para>If the <varname>$DBUS_SESSION_BUS_ADDRESS</varname> environment
cd6d5e1c 156 variable is set
5aded369 157 (cf. <citerefentry project='man-pages'><refentrytitle>environ</refentrytitle><manvolnum>7</manvolnum></citerefentry>),
cd6d5e1c
ZJS
158 it will be used as the address of the user bus. This variable can
159 contain multiple addresses separated by <literal>;</literal>. If
160 this variable is not set, a suitable default for the default user
161 D-Bus instance will be used.</para>
162
4a2af8d7
LP
163 <para>If the <varname>$DBUS_SYSTEM_BUS_ADDRESS</varname>
164 environment variable is set, it will be used as the address of the
165 system bus. This variable uses the same syntax as
166 <varname>$DBUS_SESSION_BUS_ADDRESS</varname>. If this variable is
cd6d5e1c
ZJS
167 not set, a suitable default for the default system D-Bus instance
168 will be used.</para>
169
f8f1e666
ZJS
170 <para><function>sd_bus_open_system_remote()</function> connects to the system bus on
171 the specified host using
172 <citerefentry project='die-net'><refentrytitle>ssh</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
173 <parameter>host</parameter> consists of an optional user name followed by the
174 <literal>@</literal> symbol, and the hostname, optionally followed by a
175 <literal>:</literal> and a machine name. If the machine name is given, a connection
176 is created to the system bus in the specified container on the remote machine, and
177 otherwise a connection to the system bus on the specified host is created.</para>
178
179 <para>Note that entering a container is a privileged operation, and will likely only
180 work for the root user on the remote machine.</para>
cd6d5e1c 181
30cd7638 182 <para><function>sd_bus_open_system_machine()</function> connects
174299e3 183 to the system bus in the specified <parameter>machine</parameter>,
4a2af8d7
LP
184 where <parameter>machine</parameter> is the name of a local
185 container. See
cd6d5e1c 186 <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
850df10a
LP
187 for more information about the "machine" concept. Note that
188 connections into local containers are only available to privileged
189 processes at this time.</para>
190
191 <para>These calls allocate a bus connection object and initiate
67ad70e4 192 the connection to a well-known bus of some form. An alternative to
850df10a
LP
193 using these high-level calls is to create an unconnected bus
194 object with
195 <citerefentry><refentrytitle>sd_bus_new</refentrytitle><manvolnum>3</manvolnum></citerefentry>
196 and to connect it with
197 <citerefentry><refentrytitle>sd_bus_start</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
198 </para>
cd6d5e1c 199
cd6d5e1c
ZJS
200 </refsect1>
201
cd6d5e1c
ZJS
202 <refsect1>
203 <title>Reference ownership</title>
850df10a
LP
204 <para>The functions <function>sd_bus_open()</function>,
205 <function>sd_bus_open_user()</function>,
cd6d5e1c
ZJS
206 <function>sd_bus_open_system()</function>,
207 <function>sd_bus_open_system_remote()</function>, and
208 <function>sd_bus_open_system_machine()</function> return a new
b898f9bd
LP
209 connection object and the caller owns the sole reference. When not
210 needed anymore, this reference should be destroyed with
cd6d5e1c
ZJS
211 <citerefentry><refentrytitle>sd_bus_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
212 </para>
213
4a2af8d7
LP
214 <para>The functions <function>sd_bus_default()</function>,
215 <function>sd_bus_default_user()</function> and
216 <function>sd_bus_default_system()</function> do not necessarily
b898f9bd
LP
217 create a new object, but increase the connection reference of an
218 existing connection object by one. Use
4a2af8d7
LP
219 <citerefentry><refentrytitle>sd_bus_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>
220 to drop the reference.</para>
db03761e 221
b898f9bd
LP
222 <para>Queued but unwritten/unread messages also keep a reference
223 to their bus connection object. For this reason, even if an
b938cb90
JE
224 application dropped all references to a bus connection, it might
225 not get destroyed right away. Until all incoming queued
b898f9bd
LP
226 messages are read, and until all outgoing unwritten messages are
227 written, the bus object will stay
228 alive. <function>sd_bus_flush()</function> may be used to write
229 all outgoing queued messages so they drop their references. To
b938cb90 230 flush the unread incoming messages, use
b898f9bd 231 <function>sd_bus_close()</function>, which will also close the bus
b938cb90 232 connection. When using the default bus logic, it is a good idea to
b898f9bd
LP
233 first invoke <function>sd_bus_flush()</function> followed by
234 <function>sd_bus_close()</function> when a thread or process
235 terminates, and thus its bus connection object should be
236 freed.</para>
237
b938cb90 238 <para>The life cycle of the default bus connection should be the
b898f9bd
LP
239 responsibility of the code that creates/owns the thread the
240 default bus connection object is associated with. Library code
241 should neither call <function>sd_bus_flush()</function> nor
242 <function>sd_bus_close()</function> on default bus objects unless
243 it does so in its own private, self-allocated thread. Library code
244 should not use the default bus object in other threads unless it
b938cb90 245 is clear that the program using it will life cycle the bus
b898f9bd
LP
246 connection object and flush and close it before exiting from the
247 thread. In libraries where it is not clear that the calling
b938cb90 248 program will life cycle the bus connection object, it is hence
b898f9bd
LP
249 recommended to use <function>sd_bus_open_system()</function>
250 instead of <function>sd_bus_default_system()</function> and
251 related calls.</para>
252 </refsect1>
253
254 <refsect1>
255 <title>Return Value</title>
256
257 <para>On success, these calls return 0 or a positive
258 integer. On failure, these calls return a negative
259 errno-style error code.</para>
cd6d5e1c
ZJS
260 </refsect1>
261
262 <refsect1>
263 <title>Errors</title>
264
265 <para>Returned errors may indicate the following problems:</para>
266
267 <variablelist>
268
269 <varlistentry>
8474b70c 270 <term><constant>-EINVAL</constant></term>
cd6d5e1c 271
4a2af8d7 272 <listitem><para>The specified parameters are invalid.</para></listitem>
cd6d5e1c
ZJS
273 </varlistentry>
274
275 <varlistentry>
8474b70c 276 <term><constant>-ENOMEM</constant></term>
cd6d5e1c
ZJS
277
278 <listitem><para>Memory allocation failed.</para></listitem>
279 </varlistentry>
280
4a2af8d7
LP
281 <varlistentry>
282 <term><constant>-ESOCKTNOSUPPORT</constant></term>
283
284 <listitem><para>The protocol version required to connect to the selected bus is not supported.</para></listitem>
285 </varlistentry>
cd6d5e1c 286 </variablelist>
4a2af8d7
LP
287
288 <para>In addition, any further connection-related errors may be
289 by returned. See <citerefentry><refentrytitle>sd_bus_send</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
cd6d5e1c
ZJS
290 </refsect1>
291
292 <refsect1>
293 <title>Notes</title>
294
4a2af8d7
LP
295 <para><function>sd_bus_open_user()</function> and the other
296 functions described here are available as a shared library, which
297 can be compiled and linked to with the
298 <constant>libsystemd</constant> <citerefentry
299 project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
cd6d5e1c
ZJS
300 file.</para>
301 </refsect1>
302
303 <refsect1>
304 <title>See Also</title>
305
306 <para>
307 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
308 <citerefentry><refentrytitle>sd-bus</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
309 <citerefentry><refentrytitle>sd_bus_new</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
310 <citerefentry><refentrytitle>sd_bus_ref</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
311 <citerefentry><refentrytitle>sd_bus_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
3ba3a79d 312 <citerefentry project='die-net'><refentrytitle>ssh</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
cd6d5e1c
ZJS
313 <citerefentry><refentrytitle>systemd-machined.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
314 <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
315 </para>
316 </refsect1>
317
318</refentry>