]> git.proxmox.com Git - systemd.git/blame - man/sd_bus_default.xml
New upstream version 236
[systemd.git] / man / sd_bus_default.xml
CommitLineData
f47781d8 1<?xml version='1.0'?> <!--*- Mode: nxml; nxml-child-indent: 2; indent-tabs-mode: nil -*-->
60f067b4
JS
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<!--
52ad194e
MB
6 SPDX-License-Identifier: LGPL-2.1+
7
e3bff60a 8 This file is part of systemd.
60f067b4 9
e3bff60a 10 Copyright 2014 Zbigniew Jędrzejewski-Szmek
60f067b4 11
e3bff60a
MP
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.
60f067b4 16
e3bff60a
MP
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.
60f067b4 21
e3bff60a
MP
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/>.
60f067b4
JS
24-->
25
86f210e9 26<refentry id="sd_bus_default">
60f067b4
JS
27
28 <refentryinfo>
e3bff60a 29 <title>sd_bus_default</title>
60f067b4
JS
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>
e3bff60a 43 <refentrytitle>sd_bus_default</refentrytitle>
60f067b4
JS
44 <manvolnum>3</manvolnum>
45 </refmeta>
46
47 <refnamediv>
e3bff60a
MP
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>
60f067b4
JS
53 <refname>sd_bus_open_user</refname>
54 <refname>sd_bus_open_system</refname>
55 <refname>sd_bus_open_system_remote</refname>
e3bff60a 56 <refname>sd_bus_open_system_machine</refname>
60f067b4 57
e3bff60a 58 <refpurpose>Acquire a connection to a system or user bus</refpurpose>
60f067b4
JS
59 </refnamediv>
60
61 <refsynopsisdiv>
62 <funcsynopsis>
63 <funcsynopsisinfo>#include &lt;systemd/sd-bus.h&gt;</funcsynopsisinfo>
64
65 <funcprototype>
e3bff60a 66 <funcdef>int <function>sd_bus_default</function></funcdef>
60f067b4
JS
67 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
68 </funcprototype>
69
70 <funcprototype>
e3bff60a 71 <funcdef>int <function>sd_bus_default_user</function></funcdef>
60f067b4
JS
72 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
73 </funcprototype>
74
75 <funcprototype>
e3bff60a 76 <funcdef>int <function>sd_bus_default_system</function></funcdef>
60f067b4
JS
77 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
78 </funcprototype>
79
80 <funcprototype>
e3bff60a 81 <funcdef>int <function>sd_bus_open</function></funcdef>
60f067b4
JS
82 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
83 </funcprototype>
84
85 <funcprototype>
e3bff60a 86 <funcdef>int <function>sd_bus_open_user</function></funcdef>
60f067b4
JS
87 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
88 </funcprototype>
89
90 <funcprototype>
e3bff60a 91 <funcdef>int <function>sd_bus_open_system</function></funcdef>
60f067b4
JS
92 <paramdef>sd_bus **<parameter>bus</parameter></paramdef>
93 </funcprototype>
e3bff60a
MP
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
60f067b4
JS
107 </funcsynopsis>
108 </refsynopsisdiv>
109
110 <refsect1>
111 <title>Description</title>
112
e3bff60a 113 <para><function>sd_bus_default()</function> acquires a bus
fb183854 114 connection object to the user bus when invoked in user context, or
e3bff60a 115 to the system bus otherwise. The connection object is associated
7035cd9e 116 with the calling thread. Each time the function is invoked from
db2df898 117 the same thread, the same object is returned, but its reference
7035cd9e 118 count is increased by one, as long as at least one reference is
fb183854
MP
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
db2df898
MP
125 explicitly before the thread ends, as otherwise, the connection will
126 leak. Also, queued but unread or unwritten messages keep the
7035cd9e 127 bus referenced, see below.</para>
e3bff60a
MP
128
129 <para><function>sd_bus_default_user()</function> returns a user
7035cd9e 130 bus connection object associated with the calling thread.
e3bff60a 131 <function>sd_bus_default_system()</function> is similar, but
fb183854
MP
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>
60f067b4 135
e3bff60a
MP
136 <para><function>sd_bus_open()</function> creates a new,
137 independent bus connection to the user bus when invoked in user
fb183854 138 context, or the system bus
e3bff60a
MP
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>,
db2df898
MP
144 <function>sd_bus_default_user()</function>, and
145 <function>sd_bus_default_system()</function>, these calls return
e3bff60a
MP
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>,
db2df898 151 <function>sd_bus_default_user()</function> and
e3bff60a
MP
152 <function>sd_bus_default_system()</function> to connect to the
153 user or system buses.</para>
154
60f067b4
JS
155 <para>If the <varname>$DBUS_SESSION_BUS_ADDRESS</varname> environment
156 variable is set
5eef597e 157 (cf. <citerefentry project='man-pages'><refentrytitle>environ</refentrytitle><manvolnum>7</manvolnum></citerefentry>),
60f067b4
JS
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
e3bff60a
MP
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
60f067b4
JS
167 not set, a suitable default for the default system D-Bus instance
168 will be used.</para>
169
f5e65279
MB
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>
60f067b4 181
fb183854 182 <para><function>sd_bus_open_system_machine()</function> connects
f47781d8 183 to the system bus in the specified <parameter>machine</parameter>,
e3bff60a
MP
184 where <parameter>machine</parameter> is the name of a local
185 container. See
60f067b4 186 <citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>
fb183854
MP
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
192 the connection to a well-known bus of some form. An alternative to
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>
60f067b4 199
60f067b4
JS
200 </refsect1>
201
60f067b4
JS
202 <refsect1>
203 <title>Reference ownership</title>
fb183854
MP
204 <para>The functions <function>sd_bus_open()</function>,
205 <function>sd_bus_open_user()</function>,
60f067b4
JS
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
7035cd9e
MP
209 connection object and the caller owns the sole reference. When not
210 needed anymore, this reference should be destroyed with
60f067b4
JS
211 <citerefentry><refentrytitle>sd_bus_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
212 </para>
213
e3bff60a
MP
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
7035cd9e
MP
217 create a new object, but increase the connection reference of an
218 existing connection object by one. Use
e3bff60a
MP
219 <citerefentry><refentrytitle>sd_bus_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>
220 to drop the reference.</para>
7035cd9e
MP
221
222 <para>Queued but unwritten/unread messages also keep a reference
223 to their bus connection object. For this reason, even if an
db2df898
MP
224 application dropped all references to a bus connection, it might
225 not get destroyed right away. Until all incoming queued
7035cd9e
MP
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
db2df898 230 flush the unread incoming messages, use
7035cd9e 231 <function>sd_bus_close()</function>, which will also close the bus
db2df898 232 connection. When using the default bus logic, it is a good idea to
7035cd9e
MP
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
db2df898 238 <para>The life cycle of the default bus connection should be the
7035cd9e
MP
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
db2df898 245 is clear that the program using it will life cycle the bus
7035cd9e
MP
246 connection object and flush and close it before exiting from the
247 thread. In libraries where it is not clear that the calling
db2df898 248 program will life cycle the bus connection object, it is hence
7035cd9e
MP
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>
60f067b4
JS
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>
5eef597e 270 <term><constant>-EINVAL</constant></term>
60f067b4 271
e3bff60a 272 <listitem><para>The specified parameters are invalid.</para></listitem>
60f067b4
JS
273 </varlistentry>
274
275 <varlistentry>
5eef597e 276 <term><constant>-ENOMEM</constant></term>
60f067b4
JS
277
278 <listitem><para>Memory allocation failed.</para></listitem>
279 </varlistentry>
280
e3bff60a
MP
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>
60f067b4 286 </variablelist>
e3bff60a
MP
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>
60f067b4
JS
290 </refsect1>
291
292 <refsect1>
293 <title>Notes</title>
294
e3bff60a
MP
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>
60f067b4
JS
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>,
e3bff60a 312 <citerefentry project='die-net'><refentrytitle>ssh</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
60f067b4
JS
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>