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 machinectl(1) for more information about the "machine" concept. Note
109 that connections into local containers are only available to privileged
110 processes at this time.
111
112 These calls allocate a bus connection object and initiate the
113 connection to a well-known bus of some form. An alternative to using
114 these high-level calls is to create an unconnected bus object with
115 sd_bus_new(3) and to connect it with sd_bus_start(3).
116
118 The functions sd_bus_open(), sd_bus_open_user(), sd_bus_open_system(),
119 sd_bus_open_system_remote(), and sd_bus_open_system_machine() return a
120 new connection object and the caller owns the sole reference. When not
121 needed anymore, this reference should be destroyed with
122 sd_bus_unref(3).
123
124 The functions sd_bus_default(), sd_bus_default_user() and
125 sd_bus_default_system() do not necessarily create a new object, but
126 increase the connection reference of an existing connection object by
127 one. Use sd_bus_unref(3) to drop the reference.
128
129 Queued but unwritten/unread messages keep a reference to their bus
130 connection object. For this reason, even if an application dropped all
131 references to a bus connection, it might not get destroyed right away.
132 Until all incoming queued messages are read, and until all outgoing
133 unwritten messages are written, the bus object will stay alive.
134 sd_bus_flush() may be used to write all outgoing queued messages so
135 they drop their references. To flush the unread incoming messages, use
136 sd_bus_close(), which will also close the bus connection. When using
137 the default bus logic, it is a good idea to first invoke sd_bus_flush()
138 followed by sd_bus_close() when a thread or process terminates, and
139 thus its bus connection object should be freed.
140
141 Normally, slot objects (as created by sd_bus_add_match(3) and similar
142 calls) keep a reference to their bus connection object, too. Thus, as
143 long as a bus slot object remains referenced its bus object will remain
144 allocated too. Optionally, bus slot objects may be placed in "floating"
145 mode. When in floating mode the life cycle of the bus slot object is
146 bound to the bus object, i.e. when the bus object is freed the bus slot
147 object is automatically unreferenced too. The floating state of a slot
148 object may be controlled explicitly with sd_bus_slot_set_floating(3),
149 though usually floating bus slot objects are created by passing NULL as
150 the slot parameter of sd_bus_add_match() and related calls, thus
151 indicating that the caller is not directly interested in referencing
152 and managing the bus slot object.
153
154 The life cycle of the default bus connection should be the
155 responsibility of the code that creates/owns the thread the default bus
156 connection object is associated with. Library code should neither call
157 sd_bus_flush() nor sd_bus_close() on default bus objects unless it does
158 so in its own private, self-allocated thread. Library code should not
159 use the default bus object in other threads unless it is clear that the
160 program using it will life cycle the bus connection object and flush
161 and close it before exiting from the thread. In libraries where it is
162 not clear that the calling program will life cycle the bus connection
163 object, it is hence recommended to use sd_bus_open_system() instead of
164 sd_bus_default_system() and related calls.
165
167 On success, these calls return 0 or a positive integer. On failure,
168 these calls return a negative errno-style error code.
169
171 Returned errors may indicate the following problems:
172
173 -EINVAL
174 The specified parameters are invalid.
175
176 -ENOMEM
177 Memory allocation failed.
178
179 -ESOCKTNOSUPPORT
180 The protocol version required to connect to the selected bus is not
181 supported.
182
183 In addition, any further connection-related errors may be by returned.
184 See sd_bus_send(3).
185
187 These APIs are implemented as a shared library, which can be compiled
188 and linked to with the libsystemd pkg-config(1) file.
189
191 systemd(1), sd-bus(3), sd_bus_new(3), sd_bus_ref(3), sd_bus_unref(3),
192 ssh(1), systemd-machined.service(8), machinectl(1)
193
194
195
196systemd 241 SD_BUS_DEFAULT(3)