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">
6 This file is part of systemd.
8 Copyright 2014 Zbigniew Jędrzejewski-Szmek
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.
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.
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/>.
24 <refentry id=
"sd_bus_default">
27 <title>sd_bus_default
</title>
28 <productname>systemd
</productname>
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>
41 <refentrytitle>sd_bus_default
</refentrytitle>
42 <manvolnum>3</manvolnum>
46 <refname>sd_bus_default
</refname>
47 <refname>sd_bus_default_user
</refname>
48 <refname>sd_bus_default_system
</refname>
50 <refname>sd_bus_open
</refname>
51 <refname>sd_bus_open_user
</refname>
52 <refname>sd_bus_open_system
</refname>
53 <refname>sd_bus_open_system_remote
</refname>
54 <refname>sd_bus_open_system_machine
</refname>
56 <refpurpose>Acquire a connection to a system or user bus
</refpurpose>
61 <funcsynopsisinfo>#include
<systemd/sd-bus.h
></funcsynopsisinfo>
64 <funcdef>int
<function>sd_bus_default
</function></funcdef>
65 <paramdef>sd_bus **
<parameter>bus
</parameter></paramdef>
69 <funcdef>int
<function>sd_bus_default_user
</function></funcdef>
70 <paramdef>sd_bus **
<parameter>bus
</parameter></paramdef>
74 <funcdef>int
<function>sd_bus_default_system
</function></funcdef>
75 <paramdef>sd_bus **
<parameter>bus
</parameter></paramdef>
79 <funcdef>int
<function>sd_bus_open
</function></funcdef>
80 <paramdef>sd_bus **
<parameter>bus
</parameter></paramdef>
84 <funcdef>int
<function>sd_bus_open_user
</function></funcdef>
85 <paramdef>sd_bus **
<parameter>bus
</parameter></paramdef>
89 <funcdef>int
<function>sd_bus_open_system
</function></funcdef>
90 <paramdef>sd_bus **
<parameter>bus
</parameter></paramdef>
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>
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>
109 <title>Description
</title>
111 <para><function>sd_bus_default()
</function> acquires a bus
112 connection object to the user bus when invoked in user context, or
113 to the system bus otherwise. The connection object is associated
114 to the calling thread. Each time the function is invoked from the
115 same thread the same object is returned, but its reference count
116 is increased by one, as long as at least one reference is
117 kept. When the last reference to the connection is dropped (using
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
123 explicitly before the thread ends or otherwise the connection will
126 <para><function>sd_bus_default_user()
</function> returns a user
127 bus connection object associated to the calling thread.
128 <function>sd_bus_default_system()
</function> is similar, but
129 connects to the system bus. Note that
130 <function>sd_bus_default()
</function> is identical to these two
131 calls, depending on the execution context.
</para>
133 <para><function>sd_bus_open()
</function> creates a new,
134 independent bus connection to the user bus when invoked in user
135 context, or the system bus
136 otherwise.
<function>sd_bus_open_user()
</function> is similar, but
137 connects only to the user bus.
138 <function>sd_bus_open_system()
</function> does the same, but
139 connects to the system bus. In contrast to
140 <function>sd_bus_default()
</function>,
141 <function>sd_bus_default_user()
</function>,
142 <function>sd_bus_default_system()
</function> these calls return
143 new, independent connection objects that are not associated with
144 the invoking thread and are not shared between multiple
145 invocations. It is recommended to share connections per thread to
146 efficiently make use the available resources. Thus, it is
147 recommended to use
<function>sd_bus_default()
</function>,
148 <function>sd_bus_default_user()
</function>,
149 <function>sd_bus_default_system()
</function> to connect to the
150 user or system buses.
</para>
152 <para>If the
<varname>$DBUS_SESSION_BUS_ADDRESS
</varname> environment
154 (cf.
<citerefentry project='man-pages'
><refentrytitle>environ
</refentrytitle><manvolnum>7</manvolnum></citerefentry>),
155 it will be used as the address of the user bus. This variable can
156 contain multiple addresses separated by
<literal>;
</literal>. If
157 this variable is not set, a suitable default for the default user
158 D-Bus instance will be used.
</para>
160 <para>If the
<varname>$DBUS_SYSTEM_BUS_ADDRESS
</varname>
161 environment variable is set, it will be used as the address of the
162 system bus. This variable uses the same syntax as
163 <varname>$DBUS_SESSION_BUS_ADDRESS
</varname>. If this variable is
164 not set, a suitable default for the default system D-Bus instance
167 <para><function>sd_bus_open_system_remote()
</function> connects to
168 the system bus on the specified
<parameter>host
</parameter> using
170 project='die-net'
><refentrytitle>ssh
</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
<parameter>host
</parameter>
171 consists of an optional user name followed by the
172 <literal>@
</literal> symbol, and the hostname.
175 <para><function>sd_bus_open_system_machine()
</function> connects
176 to the system bus in the specified
<parameter>machine
</parameter>,
177 where
<parameter>machine
</parameter> is the name of a local
179 <citerefentry><refentrytitle>machinectl
</refentrytitle><manvolnum>1</manvolnum></citerefentry>
180 for more information about the
"machine" concept. Note that
181 connections into local containers are only available to privileged
182 processes at this time.
</para>
184 <para>These calls allocate a bus connection object and initiate
185 the connection to a well-known bus of some form. An alternative to
186 using these high-level calls is to create an unconnected bus
188 <citerefentry><refentrytitle>sd_bus_new
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
189 and to connect it with
190 <citerefentry><refentrytitle>sd_bus_start
</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
196 <title>Return Value
</title>
198 <para>On success, these calls return
0 or a positive
199 integer. On failure, these calls return a negative
200 errno-style error code.
</para>
204 <title>Reference ownership
</title>
205 <para>The functions
<function>sd_bus_open()
</function>,
206 <function>sd_bus_open_user()
</function>,
207 <function>sd_bus_open_system()
</function>,
208 <function>sd_bus_open_system_remote()
</function>, and
209 <function>sd_bus_open_system_machine()
</function> return a new
210 object and the caller owns the sole reference. When not needed
211 anymore, this reference should be destroyed with
212 <citerefentry><refentrytitle>sd_bus_unref
</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
215 <para>The functions
<function>sd_bus_default()
</function>,
216 <function>sd_bus_default_user()
</function> and
217 <function>sd_bus_default_system()
</function> do not necessarily
218 create a new object, but increase the connection reference by
220 <citerefentry><refentrytitle>sd_bus_unref
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
221 to drop the reference.
</para>
223 <para>Queued messages also keep a reference to the bus. For this reason, just
224 because no application is having a reference to the bus does not mean that
225 the bus object will be destroyed. Until all the messages are sent, the bus object
226 will stay alive.
<function>sd_bus_flush
</function> can be used to send all the
227 queued messages so they drop their references.
</para>
231 <title>Errors
</title>
233 <para>Returned errors may indicate the following problems:
</para>
238 <term><constant>-EINVAL
</constant></term>
240 <listitem><para>The specified parameters are invalid.
</para></listitem>
244 <term><constant>-ENOMEM
</constant></term>
246 <listitem><para>Memory allocation failed.
</para></listitem>
250 <term><constant>-ESOCKTNOSUPPORT
</constant></term>
252 <listitem><para>The protocol version required to connect to the selected bus is not supported.
</para></listitem>
256 <para>In addition, any further connection-related errors may be
257 by returned. See
<citerefentry><refentrytitle>sd_bus_send
</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
</para>
263 <para><function>sd_bus_open_user()
</function> and the other
264 functions described here are available as a shared library, which
265 can be compiled and linked to with the
266 <constant>libsystemd
</constant> <citerefentry
267 project='die-net'
><refentrytitle>pkg-config
</refentrytitle><manvolnum>1</manvolnum></citerefentry>
272 <title>See Also
</title>
275 <citerefentry><refentrytitle>systemd
</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
276 <citerefentry><refentrytitle>sd-bus
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
277 <citerefentry><refentrytitle>sd_bus_new
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
278 <citerefentry><refentrytitle>sd_bus_ref
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
279 <citerefentry><refentrytitle>sd_bus_unref
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
280 <citerefentry project='die-net'
><refentrytitle>ssh
</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
281 <citerefentry><refentrytitle>systemd-machined.service
</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
282 <citerefentry><refentrytitle>machinectl
</refentrytitle><manvolnum>1</manvolnum></citerefentry>