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