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