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