'\" t .TH "SD_BUS_DEFAULT" "3" "" "systemd 255" "sd_bus_default" .\" ----------------------------------------------------------------- .\" * Define some portability stuff .\" ----------------------------------------------------------------- .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .\" http://bugs.debian.org/507673 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" ----------------------------------------------------------------- .\" * set default formatting .\" ----------------------------------------------------------------- .\" disable hyphenation .nh .\" disable justification (adjust text to left margin only) .ad l .\" ----------------------------------------------------------------- .\" * MAIN CONTENT STARTS HERE * .\" ----------------------------------------------------------------- .SH "NAME" sd_bus_default, sd_bus_default_user, sd_bus_default_system, sd_bus_open, sd_bus_open_with_description, sd_bus_open_user, sd_bus_open_user_with_description, sd_bus_open_user_machine, sd_bus_open_system, sd_bus_open_system_with_description, sd_bus_open_system_remote, sd_bus_open_system_machine \- Acquire a connection to a system or user bus .SH "SYNOPSIS" .sp .ft B .nf #include .fi .ft .HP \w'int\ sd_bus_default('u .BI "int sd_bus_default(sd_bus\ **" "bus" ");" .HP \w'int\ sd_bus_default_user('u .BI "int sd_bus_default_user(sd_bus\ **" "bus" ");" .HP \w'int\ sd_bus_default_system('u .BI "int sd_bus_default_system(sd_bus\ **" "bus" ");" .HP \w'int\ sd_bus_open('u .BI "int sd_bus_open(sd_bus\ **" "bus" ");" .HP \w'int\ sd_bus_open_with_description('u .BI "int sd_bus_open_with_description(sd_bus\ **" "bus" ", const\ char\ *" "description" ");" .HP \w'int\ sd_bus_open_user('u .BI "int sd_bus_open_user(sd_bus\ **" "bus" ");" .HP \w'int\ sd_bus_open_user_with_description('u .BI "int sd_bus_open_user_with_description(sd_bus\ **" "bus" ", const\ char\ *" "description" ");" .HP \w'int\ sd_bus_open_user_machine('u .BI "int sd_bus_open_user_machine(sd_bus\ **" "bus" ", const\ char\ *" "machine" ");" .HP \w'int\ sd_bus_open_system('u .BI "int sd_bus_open_system(sd_bus\ **" "bus" ");" .HP \w'int\ sd_bus_open_system_with_description('u .BI "int sd_bus_open_system_with_description(sd_bus\ **" "bus" ", const\ char\ *" "description" ");" .HP \w'int\ sd_bus_open_system_remote('u .BI "int sd_bus_open_system_remote(sd_bus\ **" "bus" ", const\ char\ *" "host" ");" .HP \w'int\ sd_bus_open_system_machine('u .BI "int sd_bus_open_system_machine(sd_bus\ **" "bus" ", const\ char\ *" "machine" ");" .SH "DESCRIPTION" .PP \fBsd_bus_default()\fR acquires a bus connection object to the user bus when invoked from within a user slice (any session under "user\-*\&.slice", e\&.g\&.: "user@1000\&.service"), or to the system bus otherwise\&. The connection object is associated with the calling thread\&. Each time the function is invoked from the same thread, the same object is returned, but its reference count is increased by one, as long as at least one reference is kept\&. When the last reference to the connection is dropped (using the \fBsd_bus_unref\fR(3) call), the connection is terminated\&. Note that the connection is not automatically terminated when the associated thread ends\&. It is important to drop the last reference to the bus connection explicitly before the thread ends, as otherwise, the connection will leak\&. Also, queued but unread or unwritten messages keep the bus referenced, see below\&. .PP \fBsd_bus_default_user()\fR returns a user bus connection object associated with the calling thread\&. \fBsd_bus_default_system()\fR is similar, but connects to the system bus\&. Note that \fBsd_bus_default()\fR is identical to these two calls, depending on the execution context\&. .PP \fBsd_bus_open()\fR creates a new, independent bus connection to the user bus when invoked in user context, or the system bus otherwise\&. \fBsd_bus_open_user()\fR is similar, but connects only to the user bus\&. \fBsd_bus_open_system()\fR does the same, but connects to the system bus\&. In contrast to \fBsd_bus_default()\fR, \fBsd_bus_default_user()\fR, and \fBsd_bus_default_system()\fR, these calls return new, independent connection objects that are not associated with the invoking thread and are not shared between multiple invocations\&. It is recommended to share connections per thread to efficiently make use the available resources\&. Thus, it is recommended to use \fBsd_bus_default()\fR, \fBsd_bus_default_user()\fR and \fBsd_bus_default_system()\fR to connect to the user or system buses\&. .PP \fBsd_bus_open_with_description()\fR, \fBsd_bus_open_user_with_description()\fR, and \fBsd_bus_open_system_with_description()\fR are similar to \fBsd_bus_open()\fR, \fBsd_bus_open_user()\fR, and \fBsd_bus_open_system()\fR, but allow a description string to be set, see \fBsd_bus_set_description\fR(3)\&. \fIdescription\fR may be \fBNULL\fR, in which case this function is equivalent to \fBsd_bus_open()\fR\&. This description string is used in log messages about the bus object, and including a "name" for the bus makes them easier to understand\&. Some messages are emitted during bus initialization, hence using this function is preferable to setting the description later with \fBsd_bus_open_with_description()\fR\&. The argument is copied internally and will not be referenced after the function returns\&. .PP If the \fI$DBUS_SESSION_BUS_ADDRESS\fR environment variable is set (cf\&. \fBenviron\fR(7)), it will be used as the address of the user bus\&. This variable can contain multiple addresses separated by ";"\&. If this variable is not set, a suitable default for the default user D\-Bus instance will be used\&. .PP If the \fI$DBUS_SYSTEM_BUS_ADDRESS\fR environment variable is set, it will be used as the address of the system bus\&. This variable uses the same syntax as \fI$DBUS_SESSION_BUS_ADDRESS\fR\&. If this variable is not set, a suitable default for the default system D\-Bus instance will be used\&. .PP \fBsd_bus_open_system_remote()\fR connects to the system bus on the specified host using \fBssh\fR(1)\&. \fIhost\fR consists of an optional user name followed by the "@" symbol, and the hostname, optionally followed by a ":" and a port, optionally followed by a "/" and a machine name\&. If the machine name is given, a connection is created to the system bus in the specified container on the remote machine, and otherwise a connection to the system bus on the specified host is created\&. .PP Note that entering a container is a privileged operation, and will likely only work for the root user on the remote machine\&. .PP \fBsd_bus_open_system_machine()\fR connects to the system bus in the specified \fImachine\fR, where \fImachine\fR is the name of a local container, possibly prefixed by a user name and a separating "@"\&. If the container name is specified as the special string "\&.host" the connection is made to the local system\&. This is useful to connect to the local system bus as specific user, e\&.g\&. "foobar@\&.host" to connect to the local system bus as local user "foobar"\&. If the "@" syntax is used either the left\-hand side or the right\-hand side may be omitted (but not both) in which case the local user name or "\&.host" is implied\&. If the "@" syntax is not used the connection is always made as root user\&. See \fBsd_bus_set_address\fR(3) for a description of the address syntax, and \fBmachinectl\fR(1) for more information about the "machine" concept\&. Note that connections into local containers are only available to privileged processes at this time\&. .PP \fBsd_bus_open_user_machine()\fR is similar to \fBsd_bus_open_system_machine()\fR, but connects to the user bus of the root user, or if the "@" syntax is used, of the specified user\&. .PP These calls allocate a bus connection object and initiate the connection to a well\-known bus of some form\&. An alternative to using these high\-level calls is to create an unconnected bus object with \fBsd_bus_new\fR(3) and to connect it with \fBsd_bus_start\fR(3)\&. .SH "REFERENCE OWNERSHIP" .PP The functions \fBsd_bus_open()\fR, \fBsd_bus_open_user()\fR, \fBsd_bus_open_user_machine()\fR, \fBsd_bus_open_system()\fR, \fBsd_bus_open_system_remote()\fR, and \fBsd_bus_open_system_machine()\fR return a new connection object and the caller owns the sole reference\&. When not needed anymore, this reference should be destroyed with \fBsd_bus_unref\fR(3)\&. .PP The functions \fBsd_bus_default()\fR, \fBsd_bus_default_user()\fR and \fBsd_bus_default_system()\fR do not necessarily create a new object, but increase the connection reference of an existing connection object by one\&. Use \fBsd_bus_unref\fR(3) to drop the reference\&. .PP Queued but unwritten/unread messages keep a reference to their bus connection object\&. For this reason, even if an application dropped all references to a bus connection, it might not get destroyed right away\&. Until all incoming queued messages are read, and until all outgoing unwritten messages are written, the bus object will stay alive\&. \fBsd_bus_flush()\fR may be used to write all outgoing queued messages so they drop their references\&. To flush the unread incoming messages, use \fBsd_bus_close()\fR, which will also close the bus connection\&. When using the default bus logic, it is a good idea to first invoke \fBsd_bus_flush()\fR followed by \fBsd_bus_close()\fR when a thread or process terminates, and thus its bus connection object should be freed\&. .PP Normally, slot objects (as created by \fBsd_bus_add_match\fR(3) and similar calls) keep a reference to their bus connection object, too\&. Thus, as long as a bus slot object remains referenced its bus object will remain allocated too\&. Optionally, bus slot objects may be placed in "floating" mode\&. When in floating mode the life cycle of the bus slot object is bound to the bus object, i\&.e\&. when the bus object is freed the bus slot object is automatically unreferenced too\&. The floating state of a slot object may be controlled explicitly with \fBsd_bus_slot_set_floating\fR(3), though usually floating bus slot objects are created by passing \fBNULL\fR as the \fIslot\fR parameter of \fBsd_bus_add_match()\fR and related calls, thus indicating that the caller is not directly interested in referencing and managing the bus slot object\&. .PP The life cycle of the default bus connection should be the responsibility of the code that creates/owns the thread the default bus connection object is associated with\&. Library code should neither call \fBsd_bus_flush()\fR nor \fBsd_bus_close()\fR on default bus objects unless it does so in its own private, self\-allocated thread\&. Library code should not use the default bus object in other threads unless it is clear that the program using it will life cycle the bus connection object and flush and close it before exiting from the thread\&. In libraries where it is not clear that the calling program will life cycle the bus connection object, it is hence recommended to use \fBsd_bus_open_system()\fR instead of \fBsd_bus_default_system()\fR and related calls\&. .SH "RETURN VALUE" .PP On success, these calls return 0 or a positive integer\&. On failure, these calls return a negative errno\-style error code\&. .SS "Errors" .PP Returned errors may indicate the following problems: .PP \fB\-EINVAL\fR .RS 4 The specified parameters are invalid\&. .RE .PP \fB\-ENOMEDIUM\fR .RS 4 The requested bus type is not available because of invalid environment (for example the user session bus is not available because \fI$XDG_RUNTIME_DIR\fR is not set)\&. .sp Added in version 247\&. .RE .PP \fB\-ENOMEM\fR .RS 4 Memory allocation failed\&. .RE .PP \fB\-ESOCKTNOSUPPORT\fR .RS 4 The protocol version required to connect to the selected bus is not supported\&. .RE .PP In addition, other connection\-related errors may be returned\&. See \fBsd_bus_send\fR(3)\&. .SH "NOTES" .PP Functions described here are available as a shared library, which can be compiled against and linked to with the \fBlibsystemd\fR\ \&\fBpkg-config\fR(1) file\&. .PP The code described here uses \fBgetenv\fR(3), which is declared to be not multi\-thread\-safe\&. This means that the code calling the functions described here must not call \fBsetenv\fR(3) from a parallel thread\&. It is recommended to only do calls to \fBsetenv()\fR from an early phase of the program when no other threads have been started\&. .SH "HISTORY" .PP \fBsd_bus_default()\fR, \fBsd_bus_default_user()\fR, \fBsd_bus_default_system()\fR, \fBsd_bus_open()\fR, \fBsd_bus_open_user()\fR, \fBsd_bus_open_system()\fR, \fBsd_bus_open_system_remote()\fR, and \fBsd_bus_open_system_machine()\fR were added in version 220\&. .PP \fBsd_bus_open_with_description()\fR, \fBsd_bus_open_user_with_description()\fR, and \fBsd_bus_open_system_with_description()\fR were added in version 240\&. .PP \fBsd_bus_open_user_machine()\fR was added in version 248\&. .SH "SEE ALSO" .PP \fBsystemd\fR(1), \fBsd-bus\fR(3), \fBsd_bus_new\fR(3), \fBsd_bus_ref\fR(3), \fBsd_bus_unref\fR(3), \fBsd_bus_close\fR(3), \fBssh\fR(1), \fBsystemd-machined.service\fR(8), \fBmachinectl\fR(1)