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