1SD_BUS_DEFAULT(3) sd_bus_default SD_BUS_DEFAULT(3)
2
3
4
6 sd_bus_default, sd_bus_default_user, sd_bus_default_system,
7 sd_bus_open, sd_bus_open_with_description, sd_bus_open_user,
8 sd_bus_open_user_with_description, sd_bus_open_user_machine,
9 sd_bus_open_system, sd_bus_open_system_with_description,
10 sd_bus_open_system_remote, sd_bus_open_system_machine - Acquire a
11 connection to a system or user bus
12
14 #include <systemd/sd-bus.h>
15
16 int sd_bus_default(sd_bus **bus);
17
18 int sd_bus_default_user(sd_bus **bus);
19
20 int sd_bus_default_system(sd_bus **bus);
21
22 int sd_bus_open(sd_bus **bus);
23
24 int sd_bus_open_with_description(sd_bus **bus,
25 const char *description);
26
27 int sd_bus_open_user(sd_bus **bus);
28
29 int sd_bus_open_user_with_description(sd_bus **bus,
30 const char *description);
31
32 int sd_bus_open_user_machine(sd_bus **bus, const char *machine);
33
34 int sd_bus_open_system(sd_bus **bus);
35
36 int sd_bus_open_system_with_description(sd_bus **bus,
37 const char *description);
38
39 int sd_bus_open_system_remote(sd_bus **bus, const char *host);
40
41 int sd_bus_open_system_machine(sd_bus **bus, const char *machine);
42
44 sd_bus_default() acquires a bus connection object to the user bus when
45 invoked in user context, or to the system bus otherwise. The connection
46 object is associated with the calling thread. Each time the function is
47 invoked from the same thread, the same object is returned, but its
48 reference count is increased by one, as long as at least one reference
49 is kept. When the last reference to the connection is dropped (using
50 the sd_bus_unref(3) call), the connection is terminated. Note that the
51 connection is not automatically terminated when the associated thread
52 ends. It is important to drop the last reference to the bus connection
53 explicitly before the thread ends, as otherwise, the connection will
54 leak. Also, queued but unread or unwritten messages keep the bus
55 referenced, see below.
56
57 sd_bus_default_user() returns a user bus connection object associated
58 with the calling thread. sd_bus_default_system() is similar, but
59 connects to the system bus. Note that sd_bus_default() is identical to
60 these two calls, depending on the execution context.
61
62 sd_bus_open() creates a new, independent bus connection to the user bus
63 when invoked in user context, or the system bus otherwise.
64 sd_bus_open_user() is similar, but connects only to the user bus.
65 sd_bus_open_system() does the same, but connects to the system bus. In
66 contrast to sd_bus_default(), sd_bus_default_user(), and
67 sd_bus_default_system(), these calls return new, independent connection
68 objects that are not associated with the invoking thread and are not
69 shared between multiple invocations. It is recommended to share
70 connections per thread to efficiently make use the available resources.
71 Thus, it is recommended to use sd_bus_default(), sd_bus_default_user()
72 and sd_bus_default_system() to connect to the user or system buses.
73
74 sd_bus_open_with_description(), sd_bus_open_user_with_description(),
75 and sd_bus_open_system_with_description() are similar to sd_bus_open(),
76 sd_bus_open_user(), and sd_bus_open_system(), but allow a description
77 string to be set, see sd_bus_set_description(3). description may be
78 NULL, in which case this function is equivalent to sd_bus_open(). This
79 description string is used in log messages about the bus object, and
80 including a "name" for the bus makes them easier to understand. Some
81 messages are emitted during bus initialization, hence using this
82 function is preferable to setting the description later with
83 sd_bus_open_with_description(). The argument is copied internally and
84 will not be referenced after the function returns.
85
86 If the $DBUS_SESSION_BUS_ADDRESS environment variable is set (cf.
87 environ(7)), it will be used as the address of the user bus. This
88 variable can contain multiple addresses separated by ";". If this
89 variable is not set, a suitable default for the default user D-Bus
90 instance will be used.
91
92 If the $DBUS_SYSTEM_BUS_ADDRESS environment variable is set, it will be
93 used as the address of the system bus. This variable uses the same
94 syntax as $DBUS_SESSION_BUS_ADDRESS. If this variable is not set, a
95 suitable default for the default system D-Bus instance will be used.
96
97 sd_bus_open_system_remote() connects to the system bus on the specified
98 host using ssh(1). host consists of an optional user name followed by
99 the "@" symbol, and the hostname, optionally followed by a ":" and a
100 port, optionally followed by a "/" and a machine name. If the machine
101 name is given, a connection is created to the system bus in the
102 specified container on the remote machine, and otherwise a connection
103 to the system bus on the specified host is created.
104
105 Note that entering a container is a privileged operation, and will
106 likely only work for the root user on the remote machine.
107
108 sd_bus_open_system_machine() connects to the system bus in the
109 specified machine, where machine is the name of a local container,
110 possibly prefixed by a user name and a separating "@". If the container
111 name is specified as the special string ".host" the connection is made
112 to the local system. This is useful to connect to the local system bus
113 as specific user, e.g. "foobar@.host" to connect to the local system
114 bus as local user "foobar". If the "@" syntax is used either the
115 left-hand side or the right-hand side may be omitted (but not both) in
116 which case the local user name or ".host" is implied. If the "@" syntax
117 is not used the connection is always made as root user. See
118 sd_bus_set_address(3) for a description of the address syntax, and
119 machinectl(1) for more information about the "machine" concept. Note
120 that connections into local containers are only available to privileged
121 processes at this time.
122
123 sd_bus_open_user_machine() is similar to sd_bus_open_system_machine(),
124 but connects to the user bus of the root user, or if the "@" syntax is
125 used, of the specified user.
126
127 These calls allocate a bus connection object and initiate the
128 connection to a well-known bus of some form. An alternative to using
129 these high-level calls is to create an unconnected bus object with
130 sd_bus_new(3) and to connect it with sd_bus_start(3).
131
133 The functions sd_bus_open(), sd_bus_open_user(),
134 sd_bus_open_user_machine(), sd_bus_open_system(),
135 sd_bus_open_system_remote(), and sd_bus_open_system_machine() return a
136 new connection object and the caller owns the sole reference. When not
137 needed anymore, this reference should be destroyed with
138 sd_bus_unref(3).
139
140 The functions sd_bus_default(), sd_bus_default_user() and
141 sd_bus_default_system() do not necessarily create a new object, but
142 increase the connection reference of an existing connection object by
143 one. Use sd_bus_unref(3) to drop the reference.
144
145 Queued but unwritten/unread messages keep a reference to their bus
146 connection object. For this reason, even if an application dropped all
147 references to a bus connection, it might not get destroyed right away.
148 Until all incoming queued messages are read, and until all outgoing
149 unwritten messages are written, the bus object will stay alive.
150 sd_bus_flush() may be used to write all outgoing queued messages so
151 they drop their references. To flush the unread incoming messages, use
152 sd_bus_close(), which will also close the bus connection. When using
153 the default bus logic, it is a good idea to first invoke sd_bus_flush()
154 followed by sd_bus_close() when a thread or process terminates, and
155 thus its bus connection object should be freed.
156
157 Normally, slot objects (as created by sd_bus_add_match(3) and similar
158 calls) keep a reference to their bus connection object, too. Thus, as
159 long as a bus slot object remains referenced its bus object will remain
160 allocated too. Optionally, bus slot objects may be placed in "floating"
161 mode. When in floating mode the life cycle of the bus slot object is
162 bound to the bus object, i.e. when the bus object is freed the bus slot
163 object is automatically unreferenced too. The floating state of a slot
164 object may be controlled explicitly with sd_bus_slot_set_floating(3),
165 though usually floating bus slot objects are created by passing NULL as
166 the slot parameter of sd_bus_add_match() and related calls, thus
167 indicating that the caller is not directly interested in referencing
168 and managing the bus slot object.
169
170 The life cycle of the default bus connection should be the
171 responsibility of the code that creates/owns the thread the default bus
172 connection object is associated with. Library code should neither call
173 sd_bus_flush() nor sd_bus_close() on default bus objects unless it does
174 so in its own private, self-allocated thread. Library code should not
175 use the default bus object in other threads unless it is clear that the
176 program using it will life cycle the bus connection object and flush
177 and close it before exiting from the thread. In libraries where it is
178 not clear that the calling program will life cycle the bus connection
179 object, it is hence recommended to use sd_bus_open_system() instead of
180 sd_bus_default_system() and related calls.
181
183 On success, these calls return 0 or a positive integer. On failure,
184 these calls return a negative errno-style error code.
185
186 Errors
187 Returned errors may indicate the following problems:
188
189 -EINVAL
190 The specified parameters are invalid.
191
192 -ENOMEDIUM
193 The requested bus type is not available because of invalid
194 environment (for example the user session bus is not available
195 because $XDG_RUNTIME_DIR is not set).
196
197 -ENOMEM
198 Memory allocation failed.
199
200 -ESOCKTNOSUPPORT
201 The protocol version required to connect to the selected bus is not
202 supported.
203
204 In addition, other connection-related errors may be returned. See
205 sd_bus_send(3).
206
208 These APIs are implemented as a shared library, which can be compiled
209 and linked to with the libsystemd pkg-config(1) file.
210
212 systemd(1), sd-bus(3), sd_bus_new(3), sd_bus_ref(3), sd_bus_unref(3),
213 sd_bus_close(3), ssh(1), systemd-machined.service(8), machinectl(1)
214
215
216
217systemd 248 SD_BUS_DEFAULT(3)