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