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