1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "bus-forward.h"
7 #include "bus-kernel.h"
11 #include "runtime-scope.h"
12 #include "socket-util.h"
14 /* Note that we use the new /run prefix here (instead of /var/run) since we require them to be aliases and
15 * that way we become independent of /var being mounted */
16 #define DEFAULT_SYSTEM_BUS_ADDRESS "unix:path=/run/dbus/system_bus_socket"
17 #define DEFAULT_USER_BUS_ADDRESS_FMT "unix:path=%s/bus"
19 typedef struct BusReplyCallback
{
20 sd_bus_message_handler_t callback
;
21 usec_t timeout_usec
; /* this is a relative timeout until we reach the BUS_HELLO state, and an absolute one right after */
26 typedef struct BusFilterCallback
{
27 sd_bus_message_handler_t callback
;
29 unsigned last_iteration
;
31 LIST_FIELDS(BusFilterCallback
, callbacks
);
34 typedef struct BusNode
{
37 LIST_HEAD(BusNode
, child
);
38 LIST_FIELDS(BusNode
, siblings
);
40 LIST_HEAD(BusNodeCallback
, callbacks
);
41 LIST_HEAD(BusNodeVTable
, vtables
);
42 LIST_HEAD(BusNodeEnumerator
, enumerators
);
43 LIST_HEAD(BusNodeObjectManager
, object_managers
);
46 typedef struct BusNodeCallback
{
50 unsigned last_iteration
;
52 sd_bus_message_handler_t callback
;
54 LIST_FIELDS(BusNodeCallback
, callbacks
);
57 typedef struct BusNodeEnumerator
{
60 sd_bus_node_enumerator_t callback
;
62 unsigned last_iteration
;
64 LIST_FIELDS(BusNodeEnumerator
, enumerators
);
67 typedef struct BusNodeObjectManager
{
70 LIST_FIELDS(BusNodeObjectManager
, object_managers
);
71 } BusNodeObjectManager
;
73 typedef struct BusNodeVTable
{
77 unsigned last_iteration
;
80 const sd_bus_vtable
*vtable
;
81 sd_bus_object_find_t find
;
83 LIST_FIELDS(BusNodeVTable
, vtables
);
86 typedef struct BusVTableMember
{
88 const char *interface
;
90 BusNodeVTable
*parent
;
91 unsigned last_iteration
;
92 const sd_bus_vtable
*vtable
;
95 typedef enum BusSlotType
{
102 BUS_NODE_OBJECT_MANAGER
,
103 _BUS_SLOT_INVALID
= -EINVAL
,
106 typedef struct sd_bus_slot
{
110 /* Slots can be "floating" or not. If they are not floating (the usual case) then they reference the
111 * bus object they are associated with. This means the bus object stays allocated at least as long as
112 * there is a slot around associated with it. If it is floating, then the slot's lifecycle is bound
113 * to the lifecycle of the bus: it will be disconnected from the bus when the bus is destroyed, and
114 * it keeping the slot reffed hence won't mean the bus stays reffed too. Internally this means the
115 * reference direction is reversed: floating slots objects are referenced by the bus object, and not
122 sd_bus_destroy_t destroy_callback
;
126 LIST_FIELDS(sd_bus_slot
, slots
);
129 BusReplyCallback reply_callback
;
130 BusFilterCallback filter_callback
;
131 BusMatchCallback match_callback
;
132 BusNodeCallback node_callback
;
133 BusNodeEnumerator node_enumerator
;
134 BusNodeObjectManager node_object_manager
;
135 BusNodeVTable node_vtable
;
139 typedef enum BusState
{
141 BUS_WATCH_BIND
, /* waiting for the socket to appear via inotify */
142 BUS_OPENING
, /* the kernel's connect() is still not ready */
143 BUS_AUTHENTICATING
, /* we are currently in the "SASL" authorization phase of dbus */
144 BUS_HELLO
, /* we are waiting for the Hello() response */
151 static inline bool BUS_IS_OPEN(BusState state
) {
152 return state
> BUS_UNSET
&& state
< BUS_CLOSING
;
155 typedef enum BusAuth
{
161 typedef struct sd_bus
{
165 int input_fd
, output_fd
;
177 bool match_callbacks_modified
;
178 bool filter_callbacks_modified
;
181 bool manual_peer_interface
;
182 bool allow_interactive_authorization
;
183 bool exit_on_disconnect
;
190 bool attach_timestamp
;
191 bool connected_signal
;
194 RuntimeScope runtime_scope
;
201 sd_bus_message
**rqueue
;
204 sd_bus_message
**wqueue
;
209 uint64_t read_counter
; /* A counter for each incoming msg */
214 BusMatchNode match_callbacks
;
215 Prioq
*reply_callbacks_prioq
;
216 OrderedHashmap
*reply_callbacks
;
217 LIST_HEAD(BusFilterCallback
, filter_callbacks
);
221 Set
*vtable_properties
;
223 union sockaddr_union sockaddr
;
224 socklen_t sockaddr_size
;
229 sd_id128_t server_id
;
232 unsigned address_index
;
234 uid_t connect_as_uid
;
235 gid_t connect_as_gid
;
237 int last_connect_error
;
241 struct iovec auth_iovec
[3];
250 union sockaddr_union sockaddr_peer
;
251 socklen_t sockaddr_size_peer
;
262 /* We do locking around the memfd cache, since we want to
263 * allow people to process a sd_bus_message in a different
264 * thread then it was generated on and free it there. Since
265 * adding something to the memfd cache might happen when a
266 * message is released, we hence need to protect this bit with
268 pthread_mutex_t memfd_cache_mutex
;
269 struct memfd_cache memfd_cache
[MEMFD_CACHE_MAX
];
270 unsigned n_memfd_cache
;
275 unsigned iteration_counter
;
277 sd_event_source
*input_io_event_source
;
278 sd_event_source
*output_io_event_source
;
279 sd_event_source
*time_event_source
;
280 sd_event_source
*quit_event_source
;
281 sd_event_source
*inotify_event_source
;
287 sd_bus_message
*current_message
;
288 sd_bus_slot
*current_slot
;
289 sd_bus_message_handler_t current_handler
;
290 void *current_userdata
;
292 sd_bus
**default_bus_ptr
;
297 sd_bus_track
*track_queue
;
299 LIST_HEAD(sd_bus_slot
, slots
);
300 LIST_HEAD(sd_bus_track
, tracks
);
302 int *inotify_watches
;
303 size_t n_inotify_watches
;
305 /* zero means use value specified by $SYSTEMD_BUS_TIMEOUT= environment variable or built-in default */
306 usec_t method_call_timeout
;
309 /* For method calls we timeout at 25s, like in the D-Bus reference implementation */
310 #define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
312 /* For the authentication phase we grant 90s, to provide extra room during boot, when RNGs and such are not filled up
313 * with enough entropy yet and might delay the boot */
314 #define BUS_AUTH_TIMEOUT ((usec_t) DEFAULT_TIMEOUT_USEC)
316 #define BUS_WQUEUE_MAX (384*1024)
317 #define BUS_RQUEUE_MAX (384*1024)
319 #define BUS_MESSAGE_SIZE_MAX (128*1024*1024)
320 #define BUS_AUTH_SIZE_MAX (64*1024)
321 /* Note that the D-Bus specification states that bus paths shall have no size limit. We enforce here one
322 * anyway, since truly unbounded strings are a security problem. The limit we pick is relatively large however,
323 * to not clash unnecessarily with real-life applications. */
324 #define BUS_PATH_SIZE_MAX (64*1024)
326 #define BUS_CONTAINER_DEPTH 128
328 /* Defined by the specification as maximum size of an array in bytes */
329 #define BUS_ARRAY_MAX_SIZE 67108864
331 #define BUS_FDS_MAX 1024
333 #define BUS_EXEC_ARGV_MAX 256
335 bool interface_name_is_valid(const char *p
) _pure_
;
336 bool service_name_is_valid(const char *p
) _pure_
;
337 bool member_name_is_valid(const char *p
) _pure_
;
338 bool object_path_is_valid(const char *p
) _pure_
;
340 char* object_path_startswith(const char *a
, const char *b
) _pure_
;
342 bool namespace_complex_pattern(const char *pattern
, const char *value
) _pure_
;
343 bool path_complex_pattern(const char *pattern
, const char *value
) _pure_
;
345 bool namespace_simple_pattern(const char *pattern
, const char *value
) _pure_
;
346 bool path_simple_pattern(const char *pattern
, const char *value
) _pure_
;
348 int bus_message_type_from_string(const char *s
, uint8_t *u
);
349 const char* bus_message_type_to_string(uint8_t u
) _pure_
;
351 #define error_name_is_valid interface_name_is_valid
353 sd_bus
*bus_resolve(sd_bus
*bus
);
355 int bus_ensure_running(sd_bus
*bus
);
356 int bus_start_running(sd_bus
*bus
);
357 int bus_next_address(sd_bus
*bus
);
359 int bus_seal_synthetic_message(sd_bus
*b
, sd_bus_message
*m
);
361 int bus_rqueue_make_room(sd_bus
*bus
);
363 bool bus_origin_changed(sd_bus
*bus
);
365 char* bus_address_escape(const char *v
);
367 int bus_attach_io_events(sd_bus
*b
);
368 int bus_attach_inotify_event(sd_bus
*b
);
370 void bus_close_inotify_fd(sd_bus
*b
);
371 void bus_close_io_fds(sd_bus
*b
);
373 int bus_add_match_full(
378 sd_bus_message_handler_t callback
,
379 sd_bus_message_handler_t install_callback
,
381 uint64_t timeout_usec
);
383 #define OBJECT_PATH_FOREACH_PREFIX(prefix, path) \
384 for (char *_slash = ({ strcpy((prefix), (path)); streq((prefix), "/") ? NULL : strrchr((prefix), '/'); }) ; \
385 _slash && ((_slash[(_slash) == (prefix)] = 0), true); \
386 _slash = streq((prefix), "/") ? NULL : strrchr((prefix), '/'))
388 /* If we are invoking callbacks of a bus object, ensure unreffing the
389 * bus from the callback doesn't destroy the object we are working on */
390 #define BUS_DONT_DESTROY(bus) \
391 _cleanup_(sd_bus_unrefp) _unused_ sd_bus *_dont_destroy_##bus = sd_bus_ref(bus)
393 int bus_set_address_system(sd_bus
*bus
);
394 int bus_set_address_user(sd_bus
*bus
);
395 int bus_set_address_system_remote(sd_bus
*b
, const char *host
);
396 int bus_set_address_machine(sd_bus
*b
, RuntimeScope runtime_scope
, const char *machine
);
398 int bus_maybe_reply_error(sd_bus_message
*m
, int r
, const sd_bus_error
*e
);
400 #define bus_assert_return(expr, r, error) \
402 if (!assert_log(expr, #expr)) \
403 return sd_bus_error_set_errno(error, r); \
406 void bus_enter_closing(sd_bus
*bus
);
408 void bus_set_state(sd_bus
*bus
, BusState state
);