1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
16 #include "alloc-util.h"
17 #include "bus-container.h"
18 #include "bus-control.h"
19 #include "bus-internal.h"
20 #include "bus-kernel.h"
21 #include "bus-label.h"
22 #include "bus-message.h"
23 #include "bus-objects.h"
24 #include "bus-protocol.h"
26 #include "bus-socket.h"
27 #include "bus-track.h"
29 #include "cgroup-util.h"
30 #include "constants.h"
31 #include "errno-util.h"
33 #include "glyph-util.h"
34 #include "hexdecoct.h"
35 #include "hostname-util.h"
38 #include "memory-util.h"
39 #include "missing_syscall.h"
40 #include "parse-util.h"
41 #include "path-util.h"
42 #include "process-util.h"
43 #include "stdio-util.h"
44 #include "string-util.h"
46 #include "user-util.h"
48 #define log_debug_bus_message(m) \
50 sd_bus_message *_mm = (m); \
51 log_debug("Got message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " signature=%s error-name=%s error-message=%s", \
52 strna(bus_message_type_to_string(_mm->header->type)), \
53 strna(sd_bus_message_get_sender(_mm)), \
54 strna(sd_bus_message_get_destination(_mm)), \
55 strna(sd_bus_message_get_path(_mm)), \
56 strna(sd_bus_message_get_interface(_mm)), \
57 strna(sd_bus_message_get_member(_mm)), \
58 BUS_MESSAGE_COOKIE(_mm), \
60 strna(_mm->root_container.signature), \
61 strna(_mm->error.name), \
62 strna(_mm->error.message)); \
65 static int bus_poll(sd_bus
*bus
, bool need_more
, uint64_t timeout_usec
);
66 static void bus_detach_io_events(sd_bus
*b
);
68 static thread_local sd_bus
*default_system_bus
= NULL
;
69 static thread_local sd_bus
*default_user_bus
= NULL
;
70 static thread_local sd_bus
*default_starter_bus
= NULL
;
72 static sd_bus
**bus_choose_default(int (**bus_open
)(sd_bus
**)) {
75 /* Let's try our best to reuse another cached connection. If
76 * the starter bus type is set, connect via our normal
77 * connection logic, ignoring $DBUS_STARTER_ADDRESS, so that
78 * we can share the connection with the user/system default
81 e
= secure_getenv("DBUS_STARTER_BUS_TYPE");
83 if (streq(e
, "system")) {
85 *bus_open
= sd_bus_open_system
;
86 return &default_system_bus
;
87 } else if (STR_IN_SET(e
, "user", "session")) {
89 *bus_open
= sd_bus_open_user
;
90 return &default_user_bus
;
94 /* No type is specified, so we have not other option than to
95 * use the starter address if it is set. */
96 e
= secure_getenv("DBUS_STARTER_ADDRESS");
99 *bus_open
= sd_bus_open
;
100 return &default_starter_bus
;
103 /* Finally, if nothing is set use the cached connection for
106 if (cg_pid_get_owner_uid(0, NULL
) >= 0) {
108 *bus_open
= sd_bus_open_user
;
109 return &default_user_bus
;
112 *bus_open
= sd_bus_open_system
;
113 return &default_system_bus
;
117 sd_bus
*bus_resolve(sd_bus
*bus
) {
118 switch ((uintptr_t) bus
) {
119 case (uintptr_t) SD_BUS_DEFAULT
:
120 return *(bus_choose_default(NULL
));
121 case (uintptr_t) SD_BUS_DEFAULT_USER
:
122 return default_user_bus
;
123 case (uintptr_t) SD_BUS_DEFAULT_SYSTEM
:
124 return default_system_bus
;
130 void bus_close_io_fds(sd_bus
*b
) {
133 bus_detach_io_events(b
);
135 if (b
->input_fd
!= b
->output_fd
)
136 safe_close(b
->output_fd
);
137 b
->output_fd
= b
->input_fd
= safe_close(b
->input_fd
);
140 void bus_close_inotify_fd(sd_bus
*b
) {
143 b
->inotify_event_source
= sd_event_source_disable_unref(b
->inotify_event_source
);
145 b
->inotify_fd
= safe_close(b
->inotify_fd
);
146 b
->inotify_watches
= mfree(b
->inotify_watches
);
147 b
->n_inotify_watches
= 0;
150 static void bus_reset_queues(sd_bus
*b
) {
153 while (b
->rqueue_size
> 0)
154 bus_message_unref_queued(b
->rqueue
[--b
->rqueue_size
], b
);
156 b
->rqueue
= mfree(b
->rqueue
);
158 while (b
->wqueue_size
> 0)
159 bus_message_unref_queued(b
->wqueue
[--b
->wqueue_size
], b
);
161 b
->wqueue
= mfree(b
->wqueue
);
164 static sd_bus
* bus_free(sd_bus
*b
) {
168 assert(!b
->track_queue
);
171 b
->state
= BUS_CLOSED
;
173 sd_bus_detach_event(b
);
175 while ((s
= b
->slots
)) {
176 /* At this point only floating slots can still be
177 * around, because the non-floating ones keep a
178 * reference to the bus, and we thus couldn't be
179 * destructing right now... We forcibly disconnect the
180 * slots here, so that they still can be referenced by
181 * apps, but are dead. */
184 bus_slot_disconnect(s
, true);
187 if (b
->default_bus_ptr
)
188 *b
->default_bus_ptr
= NULL
;
191 bus_close_inotify_fd(b
);
196 free(b
->unique_name
);
197 free(b
->auth_buffer
);
200 free(b
->description
);
201 free(b
->patch_sender
);
204 strv_free(b
->exec_argv
);
206 close_many(b
->fds
, b
->n_fds
);
211 ordered_hashmap_free_free(b
->reply_callbacks
);
212 prioq_free(b
->reply_callbacks_prioq
);
214 assert(b
->match_callbacks
.type
== BUS_MATCH_ROOT
);
215 bus_match_free(&b
->match_callbacks
);
217 hashmap_free_free(b
->vtable_methods
);
218 hashmap_free_free(b
->vtable_properties
);
220 assert(hashmap_isempty(b
->nodes
));
221 hashmap_free(b
->nodes
);
225 assert_se(pthread_mutex_destroy(&b
->memfd_cache_mutex
) == 0);
230 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus
*, bus_free
);
232 _public_
int sd_bus_new(sd_bus
**ret
) {
233 _cleanup_free_ sd_bus
*b
= NULL
;
235 assert_return(ret
, -EINVAL
);
245 .inotify_fd
= -EBADF
,
246 .message_version
= 1,
247 .creds_mask
= SD_BUS_CREDS_WELL_KNOWN_NAMES
|SD_BUS_CREDS_UNIQUE_NAME
,
249 .original_pid
= getpid_cached(),
250 .n_groups
= SIZE_MAX
,
251 .close_on_exit
= true,
252 .ucred
= UCRED_INVALID
,
255 /* We guarantee that wqueue always has space for at least one entry */
256 if (!GREEDY_REALLOC(b
->wqueue
, 1))
259 assert_se(pthread_mutex_init(&b
->memfd_cache_mutex
, NULL
) == 0);
265 _public_
int sd_bus_set_address(sd_bus
*bus
, const char *address
) {
266 assert_return(bus
, -EINVAL
);
267 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
268 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
269 assert_return(address
, -EINVAL
);
270 assert_return(!bus_pid_changed(bus
), -ECHILD
);
272 return free_and_strdup(&bus
->address
, address
);
275 _public_
int sd_bus_set_fd(sd_bus
*bus
, int input_fd
, int output_fd
) {
276 assert_return(bus
, -EINVAL
);
277 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
278 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
279 assert_return(input_fd
>= 0, -EBADF
);
280 assert_return(output_fd
>= 0, -EBADF
);
281 assert_return(!bus_pid_changed(bus
), -ECHILD
);
283 bus
->input_fd
= input_fd
;
284 bus
->output_fd
= output_fd
;
288 _public_
int sd_bus_set_exec(sd_bus
*bus
, const char *path
, char *const *argv
) {
289 _cleanup_strv_free_
char **a
= NULL
;
292 assert_return(bus
, -EINVAL
);
293 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
294 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
295 assert_return(path
, -EINVAL
);
296 assert_return(!strv_isempty(argv
), -EINVAL
);
297 assert_return(!bus_pid_changed(bus
), -ECHILD
);
303 r
= free_and_strdup(&bus
->exec_path
, path
);
307 return strv_free_and_replace(bus
->exec_argv
, a
);
310 _public_
int sd_bus_set_bus_client(sd_bus
*bus
, int b
) {
311 assert_return(bus
, -EINVAL
);
312 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
313 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
314 assert_return(!bus
->patch_sender
, -EPERM
);
315 assert_return(!bus_pid_changed(bus
), -ECHILD
);
317 bus
->bus_client
= !!b
;
321 _public_
int sd_bus_set_monitor(sd_bus
*bus
, int b
) {
322 assert_return(bus
, -EINVAL
);
323 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
324 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
325 assert_return(!bus_pid_changed(bus
), -ECHILD
);
327 bus
->is_monitor
= !!b
;
331 _public_
int sd_bus_negotiate_fds(sd_bus
*bus
, int b
) {
332 assert_return(bus
, -EINVAL
);
333 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
334 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
335 assert_return(!bus_pid_changed(bus
), -ECHILD
);
337 bus
->accept_fd
= !!b
;
341 _public_
int sd_bus_negotiate_timestamp(sd_bus
*bus
, int b
) {
342 assert_return(bus
, -EINVAL
);
343 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
344 assert_return(!IN_SET(bus
->state
, BUS_CLOSING
, BUS_CLOSED
), -EPERM
);
345 assert_return(!bus_pid_changed(bus
), -ECHILD
);
347 /* This is not actually supported by any of our transports these days, but we do honour it for synthetic
348 * replies, and maybe one day classic D-Bus learns this too */
349 bus
->attach_timestamp
= !!b
;
354 _public_
int sd_bus_negotiate_creds(sd_bus
*bus
, int b
, uint64_t mask
) {
355 assert_return(bus
, -EINVAL
);
356 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
357 assert_return(mask
<= _SD_BUS_CREDS_ALL
, -EINVAL
);
358 assert_return(!IN_SET(bus
->state
, BUS_CLOSING
, BUS_CLOSED
), -EPERM
);
359 assert_return(!bus_pid_changed(bus
), -ECHILD
);
361 SET_FLAG(bus
->creds_mask
, mask
, b
);
363 /* The well knowns we need unconditionally, so that matches can work */
364 bus
->creds_mask
|= SD_BUS_CREDS_WELL_KNOWN_NAMES
|SD_BUS_CREDS_UNIQUE_NAME
;
369 _public_
int sd_bus_set_server(sd_bus
*bus
, int b
, sd_id128_t server_id
) {
370 assert_return(bus
, -EINVAL
);
371 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
372 assert_return(b
|| sd_id128_equal(server_id
, SD_ID128_NULL
), -EINVAL
);
373 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
374 assert_return(!bus_pid_changed(bus
), -ECHILD
);
376 bus
->is_server
= !!b
;
377 bus
->server_id
= server_id
;
381 _public_
int sd_bus_set_anonymous(sd_bus
*bus
, int b
) {
382 assert_return(bus
, -EINVAL
);
383 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
384 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
385 assert_return(!bus_pid_changed(bus
), -ECHILD
);
387 bus
->anonymous_auth
= !!b
;
391 _public_
int sd_bus_set_trusted(sd_bus
*bus
, int b
) {
392 assert_return(bus
, -EINVAL
);
393 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
394 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
395 assert_return(!bus_pid_changed(bus
), -ECHILD
);
401 _public_
int sd_bus_set_description(sd_bus
*bus
, const char *description
) {
402 assert_return(bus
, -EINVAL
);
403 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
404 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
405 assert_return(!bus_pid_changed(bus
), -ECHILD
);
407 return free_and_strdup(&bus
->description
, description
);
410 _public_
int sd_bus_set_allow_interactive_authorization(sd_bus
*bus
, int b
) {
411 assert_return(bus
, -EINVAL
);
412 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
413 assert_return(!bus_pid_changed(bus
), -ECHILD
);
415 bus
->allow_interactive_authorization
= !!b
;
419 _public_
int sd_bus_get_allow_interactive_authorization(sd_bus
*bus
) {
420 assert_return(bus
, -EINVAL
);
421 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
422 assert_return(!bus_pid_changed(bus
), -ECHILD
);
424 return bus
->allow_interactive_authorization
;
427 _public_
int sd_bus_set_watch_bind(sd_bus
*bus
, int b
) {
428 assert_return(bus
, -EINVAL
);
429 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
430 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
431 assert_return(!bus_pid_changed(bus
), -ECHILD
);
433 bus
->watch_bind
= !!b
;
437 _public_
int sd_bus_get_watch_bind(sd_bus
*bus
) {
438 assert_return(bus
, -EINVAL
);
439 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
440 assert_return(!bus_pid_changed(bus
), -ECHILD
);
442 return bus
->watch_bind
;
445 _public_
int sd_bus_set_connected_signal(sd_bus
*bus
, int b
) {
446 assert_return(bus
, -EINVAL
);
447 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
448 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
449 assert_return(!bus_pid_changed(bus
), -ECHILD
);
451 bus
->connected_signal
= !!b
;
455 _public_
int sd_bus_get_connected_signal(sd_bus
*bus
) {
456 assert_return(bus
, -EINVAL
);
457 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
458 assert_return(!bus_pid_changed(bus
), -ECHILD
);
460 return bus
->connected_signal
;
463 static int synthesize_connected_signal(sd_bus
*bus
) {
464 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
469 /* If enabled, synthesizes a local "Connected" signal mirroring the local "Disconnected" signal. This is called
470 * whenever we fully established a connection, i.e. after the authorization phase, and after receiving the
471 * Hello() reply. Or in other words, whenever we enter BUS_RUNNING state.
473 * This is useful so that clients can start doing stuff whenever the connection is fully established in a way
474 * that works independently from whether we connected to a full bus or just a direct connection. */
476 if (!bus
->connected_signal
)
479 r
= sd_bus_message_new_signal(
482 "/org/freedesktop/DBus/Local",
483 "org.freedesktop.DBus.Local",
488 bus_message_set_sender_local(bus
, m
);
489 m
->read_counter
= ++bus
->read_counter
;
491 r
= bus_seal_synthetic_message(bus
, m
);
495 r
= bus_rqueue_make_room(bus
);
499 /* Insert at the very front */
500 memmove(bus
->rqueue
+ 1, bus
->rqueue
, sizeof(sd_bus_message
*) * bus
->rqueue_size
);
501 bus
->rqueue
[0] = bus_message_ref_queued(m
, bus
);
507 void bus_set_state(sd_bus
*bus
, enum bus_state state
) {
508 static const char* const table
[_BUS_STATE_MAX
] = {
509 [BUS_UNSET
] = "UNSET",
510 [BUS_WATCH_BIND
] = "WATCH_BIND",
511 [BUS_OPENING
] = "OPENING",
512 [BUS_AUTHENTICATING
] = "AUTHENTICATING",
513 [BUS_HELLO
] = "HELLO",
514 [BUS_RUNNING
] = "RUNNING",
515 [BUS_CLOSING
] = "CLOSING",
516 [BUS_CLOSED
] = "CLOSED",
520 assert(state
< _BUS_STATE_MAX
);
522 if (state
== bus
->state
)
525 log_debug("Bus %s: changing state %s %s %s", strna(bus
->description
),
526 table
[bus
->state
], special_glyph(SPECIAL_GLYPH_ARROW_RIGHT
), table
[state
]);
530 static int hello_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
538 assert(IN_SET(bus
->state
, BUS_HELLO
, BUS_CLOSING
));
540 r
= sd_bus_message_get_errno(reply
);
546 r
= sd_bus_message_read(reply
, "s", &s
);
550 if (!service_name_is_valid(s
) || s
[0] != ':') {
555 r
= free_and_strdup(&bus
->unique_name
, s
);
559 if (bus
->state
== BUS_HELLO
) {
560 bus_set_state(bus
, BUS_RUNNING
);
562 r
= synthesize_connected_signal(bus
);
570 /* When Hello() failed, let's propagate this in two ways: first we return the error immediately here,
571 * which is the propagated up towards the event loop. Let's also invalidate the connection, so that
572 * if the user then calls back into us again we won't wait any longer. */
574 bus_set_state(bus
, BUS_CLOSING
);
578 static int bus_send_hello(sd_bus
*bus
) {
579 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
584 if (!bus
->bus_client
)
587 r
= sd_bus_message_new_method_call(
590 "org.freedesktop.DBus",
591 "/org/freedesktop/DBus",
592 "org.freedesktop.DBus",
597 return sd_bus_call_async(bus
, NULL
, m
, hello_callback
, NULL
, 0);
600 int bus_start_running(sd_bus
*bus
) {
601 struct reply_callback
*c
;
606 assert(bus
->state
< BUS_HELLO
);
608 /* We start all method call timeouts when we enter BUS_HELLO or BUS_RUNNING mode. At this point let's convert
609 * all relative to absolute timestamps. Note that we do not reshuffle the reply callback priority queue since
610 * adding a fixed value to all entries should not alter the internal order. */
612 n
= now(CLOCK_MONOTONIC
);
613 ORDERED_HASHMAP_FOREACH(c
, bus
->reply_callbacks
) {
614 if (c
->timeout_usec
== 0)
617 c
->timeout_usec
= usec_add(n
, c
->timeout_usec
);
620 if (bus
->bus_client
) {
621 bus_set_state(bus
, BUS_HELLO
);
625 bus_set_state(bus
, BUS_RUNNING
);
627 r
= synthesize_connected_signal(bus
);
634 static int parse_address_key(const char **p
, const char *key
, char **value
) {
635 _cleanup_free_
char *r
= NULL
;
645 if (strncmp(*p
, key
, l
) != 0)
658 while (!IN_SET(*a
, ';', ',', 0)) {
672 c
= (char) ((x
<< 4) | y
);
679 if (!GREEDY_REALLOC(r
, n
+ 2))
697 free_and_replace(*value
, r
);
702 static void skip_address_key(const char **p
) {
706 *p
+= strcspn(*p
, ",");
712 static int parse_unix_address(sd_bus
*b
, const char **p
, char **guid
) {
713 _cleanup_free_
char *path
= NULL
, *abstract
= NULL
;
722 while (!IN_SET(**p
, 0, ';')) {
723 r
= parse_address_key(p
, "guid", guid
);
729 r
= parse_address_key(p
, "path", &path
);
735 r
= parse_address_key(p
, "abstract", &abstract
);
744 if (!path
&& !abstract
)
747 if (path
&& abstract
)
752 if (l
>= sizeof(b
->sockaddr
.un
.sun_path
)) /* We insist on NUL termination */
755 b
->sockaddr
.un
= (struct sockaddr_un
) {
756 .sun_family
= AF_UNIX
,
759 memcpy(b
->sockaddr
.un
.sun_path
, path
, l
);
760 b
->sockaddr_size
= offsetof(struct sockaddr_un
, sun_path
) + l
+ 1;
765 l
= strlen(abstract
);
766 if (l
>= sizeof(b
->sockaddr
.un
.sun_path
) - 1) /* We insist on NUL termination */
769 b
->sockaddr
.un
= (struct sockaddr_un
) {
770 .sun_family
= AF_UNIX
,
773 memcpy(b
->sockaddr
.un
.sun_path
+1, abstract
, l
);
774 b
->sockaddr_size
= offsetof(struct sockaddr_un
, sun_path
) + 1 + l
;
782 static int parse_tcp_address(sd_bus
*b
, const char **p
, char **guid
) {
783 _cleanup_free_
char *host
= NULL
, *port
= NULL
, *family
= NULL
;
785 struct addrinfo
*result
, hints
= {
786 .ai_socktype
= SOCK_STREAM
,
794 while (!IN_SET(**p
, 0, ';')) {
795 r
= parse_address_key(p
, "guid", guid
);
801 r
= parse_address_key(p
, "host", &host
);
807 r
= parse_address_key(p
, "port", &port
);
813 r
= parse_address_key(p
, "family", &family
);
826 hints
.ai_family
= af_from_ipv4_ipv6(family
);
827 if (hints
.ai_family
== AF_UNSPEC
)
831 r
= getaddrinfo(host
, port
, &hints
, &result
);
835 return -EADDRNOTAVAIL
;
837 memcpy(&b
->sockaddr
, result
->ai_addr
, result
->ai_addrlen
);
838 b
->sockaddr_size
= result
->ai_addrlen
;
840 freeaddrinfo(result
);
847 static int parse_exec_address(sd_bus
*b
, const char **p
, char **guid
) {
849 unsigned n_argv
= 0, j
;
858 while (!IN_SET(**p
, 0, ';')) {
859 r
= parse_address_key(p
, "guid", guid
);
865 r
= parse_address_key(p
, "path", &path
);
871 if (startswith(*p
, "argv")) {
875 ul
= strtoul(*p
+ 4, (char**) p
, 10);
876 if (errno
> 0 || **p
!= '=' || ul
> 256) {
884 if (!GREEDY_REALLOC0(argv
, ul
+ 2)) {
892 r
= parse_address_key(p
, NULL
, argv
+ ul
);
907 /* Make sure there are no holes in the array, with the
908 * exception of argv[0] */
909 for (j
= 1; j
< n_argv
; j
++)
915 if (argv
&& argv
[0] == NULL
) {
916 argv
[0] = strdup(path
);
931 for (j
= 0; j
< n_argv
; j
++)
939 static int parse_container_unix_address(sd_bus
*b
, const char **p
, char **guid
) {
940 _cleanup_free_
char *machine
= NULL
, *pid
= NULL
;
948 while (!IN_SET(**p
, 0, ';')) {
949 r
= parse_address_key(p
, "guid", guid
);
955 r
= parse_address_key(p
, "machine", &machine
);
961 r
= parse_address_key(p
, "pid", &pid
);
970 if (!machine
== !pid
)
974 if (!hostname_is_valid(machine
, VALID_HOSTNAME_DOT_HOST
))
977 free_and_replace(b
->machine
, machine
);
979 b
->machine
= mfree(b
->machine
);
982 r
= parse_pid(pid
, &b
->nspid
);
988 b
->sockaddr
.un
= (struct sockaddr_un
) {
989 .sun_family
= AF_UNIX
,
990 /* Note that we use the old /var/run prefix here, to increase compatibility with really old containers */
991 .sun_path
= "/var/run/dbus/system_bus_socket",
993 b
->sockaddr_size
= SOCKADDR_UN_LEN(b
->sockaddr
.un
);
999 static void bus_reset_parsed_address(sd_bus
*b
) {
1003 b
->sockaddr_size
= 0;
1004 b
->exec_argv
= strv_free(b
->exec_argv
);
1005 b
->exec_path
= mfree(b
->exec_path
);
1006 b
->server_id
= SD_ID128_NULL
;
1007 b
->machine
= mfree(b
->machine
);
1011 static int bus_parse_next_address(sd_bus
*b
) {
1012 _cleanup_free_
char *guid
= NULL
;
1020 if (b
->address
[b
->address_index
] == 0)
1023 bus_reset_parsed_address(b
);
1025 a
= b
->address
+ b
->address_index
;
1034 if (startswith(a
, "unix:")) {
1037 r
= parse_unix_address(b
, &a
, &guid
);
1042 } else if (startswith(a
, "tcp:")) {
1045 r
= parse_tcp_address(b
, &a
, &guid
);
1051 } else if (startswith(a
, "unixexec:")) {
1054 r
= parse_exec_address(b
, &a
, &guid
);
1060 } else if (startswith(a
, "x-machine-unix:")) {
1063 r
= parse_container_unix_address(b
, &a
, &guid
);
1076 r
= sd_id128_from_string(guid
, &b
->server_id
);
1081 b
->address_index
= a
- b
->address
;
1085 static void bus_kill_exec(sd_bus
*bus
) {
1086 if (!pid_is_valid(bus
->busexec_pid
))
1089 sigterm_wait(TAKE_PID(bus
->busexec_pid
));
1092 static int bus_start_address(sd_bus
*b
) {
1098 bus_close_io_fds(b
);
1099 bus_close_inotify_fd(b
);
1103 /* If you provide multiple different bus-addresses, we
1104 * try all of them in order and use the first one that
1108 r
= bus_socket_exec(b
);
1109 else if ((b
->nspid
> 0 || b
->machine
) && b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
)
1110 r
= bus_container_connect_socket(b
);
1111 else if (b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
)
1112 r
= bus_socket_connect(b
);
1119 q
= bus_attach_io_events(b
);
1123 q
= bus_attach_inotify_event(b
);
1130 b
->last_connect_error
= -r
;
1133 r
= bus_parse_next_address(b
);
1137 return b
->last_connect_error
> 0 ? -b
->last_connect_error
: -ECONNREFUSED
;
1141 int bus_next_address(sd_bus
*b
) {
1144 bus_reset_parsed_address(b
);
1145 return bus_start_address(b
);
1148 static int bus_start_fd(sd_bus
*b
) {
1153 assert(b
->input_fd
>= 0);
1154 assert(b
->output_fd
>= 0);
1156 if (DEBUG_LOGGING
) {
1157 _cleanup_free_
char *pi
= NULL
, *po
= NULL
;
1158 (void) fd_get_path(b
->input_fd
, &pi
);
1159 (void) fd_get_path(b
->output_fd
, &po
);
1160 log_debug("sd-bus: starting bus%s%s on fds %d/%d (%s, %s)...",
1161 b
->description
? " " : "", strempty(b
->description
),
1162 b
->input_fd
, b
->output_fd
,
1163 pi
?: "???", po
?: "???");
1166 r
= fd_nonblock(b
->input_fd
, true);
1170 r
= fd_cloexec(b
->input_fd
, true);
1174 if (b
->input_fd
!= b
->output_fd
) {
1175 r
= fd_nonblock(b
->output_fd
, true);
1179 r
= fd_cloexec(b
->output_fd
, true);
1184 if (fstat(b
->input_fd
, &st
) < 0)
1187 return bus_socket_take_fd(b
);
1190 _public_
int sd_bus_start(sd_bus
*bus
) {
1193 assert_return(bus
, -EINVAL
);
1194 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1195 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
1196 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1198 bus_set_state(bus
, BUS_OPENING
);
1200 if (bus
->is_server
&& bus
->bus_client
)
1203 if (bus
->input_fd
>= 0)
1204 r
= bus_start_fd(bus
);
1205 else if (bus
->address
|| bus
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
|| bus
->exec_path
|| bus
->machine
)
1206 r
= bus_start_address(bus
);
1215 return bus_send_hello(bus
);
1218 _public_
int sd_bus_open_with_description(sd_bus
**ret
, const char *description
) {
1220 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1223 assert_return(ret
, -EINVAL
);
1225 /* Let's connect to the starter bus if it is set, and
1226 * otherwise to the bus that is appropriate for the scope
1227 * we are running in */
1229 e
= secure_getenv("DBUS_STARTER_BUS_TYPE");
1231 if (streq(e
, "system"))
1232 return sd_bus_open_system_with_description(ret
, description
);
1233 else if (STR_IN_SET(e
, "session", "user"))
1234 return sd_bus_open_user_with_description(ret
, description
);
1237 e
= secure_getenv("DBUS_STARTER_ADDRESS");
1239 if (cg_pid_get_owner_uid(0, NULL
) >= 0)
1240 return sd_bus_open_user_with_description(ret
, description
);
1242 return sd_bus_open_system_with_description(ret
, description
);
1249 r
= sd_bus_set_address(b
, e
);
1253 b
->bus_client
= true;
1255 /* We don't know whether the bus is trusted or not, so better
1256 * be safe, and authenticate everything */
1258 b
->is_local
= false;
1259 b
->creds_mask
|= SD_BUS_CREDS_UID
| SD_BUS_CREDS_EUID
| SD_BUS_CREDS_EFFECTIVE_CAPS
;
1261 r
= sd_bus_start(b
);
1269 _public_
int sd_bus_open(sd_bus
**ret
) {
1270 return sd_bus_open_with_description(ret
, NULL
);
1273 int bus_set_address_system(sd_bus
*b
) {
1279 e
= secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1281 r
= sd_bus_set_address(b
, e
?: DEFAULT_SYSTEM_BUS_ADDRESS
);
1283 b
->is_system
= true;
1287 _public_
int sd_bus_open_system_with_description(sd_bus
**ret
, const char *description
) {
1288 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1291 assert_return(ret
, -EINVAL
);
1298 r
= sd_bus_set_description(b
, description
);
1303 r
= bus_set_address_system(b
);
1307 b
->bus_client
= true;
1309 /* Let's do per-method access control on the system bus. We
1310 * need the caller's UID and capability set for that. */
1312 b
->creds_mask
|= SD_BUS_CREDS_UID
| SD_BUS_CREDS_EUID
| SD_BUS_CREDS_EFFECTIVE_CAPS
;
1315 r
= sd_bus_start(b
);
1323 _public_
int sd_bus_open_system(sd_bus
**ret
) {
1324 return sd_bus_open_system_with_description(ret
, NULL
);
1327 int bus_set_address_user(sd_bus
*b
) {
1329 _cleanup_free_
char *_a
= NULL
;
1334 a
= secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1337 _cleanup_free_
char *ee
= NULL
;
1339 e
= secure_getenv("XDG_RUNTIME_DIR");
1341 return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM
),
1342 "sd-bus: $XDG_RUNTIME_DIR not set, cannot connect to user bus.");
1344 ee
= bus_address_escape(e
);
1348 if (asprintf(&_a
, DEFAULT_USER_BUS_ADDRESS_FMT
, ee
) < 0)
1353 r
= sd_bus_set_address(b
, a
);
1359 _public_
int sd_bus_open_user_with_description(sd_bus
**ret
, const char *description
) {
1360 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1363 assert_return(ret
, -EINVAL
);
1370 r
= sd_bus_set_description(b
, description
);
1375 r
= bus_set_address_user(b
);
1379 b
->bus_client
= true;
1381 /* We don't do any per-method access control on the user bus. */
1385 r
= sd_bus_start(b
);
1393 _public_
int sd_bus_open_user(sd_bus
**ret
) {
1394 return sd_bus_open_user_with_description(ret
, NULL
);
1397 int bus_set_address_system_remote(sd_bus
*b
, const char *host
) {
1398 _cleanup_free_
char *e
= NULL
;
1399 char *m
= NULL
, *c
= NULL
, *a
, *rbracket
= NULL
, *p
= NULL
;
1404 /* Skip ":"s in ipv6 addresses */
1408 rbracket
= strchr(host
, ']');
1411 t
= strndupa_safe(host
+ 1, rbracket
- host
- 1);
1412 e
= bus_address_escape(t
);
1415 } else if ((a
= strchr(host
, '@'))) {
1416 if (*(a
+ 1) == '[') {
1417 _cleanup_free_
char *t
= NULL
;
1419 rbracket
= strchr(a
+ 1, ']');
1422 t
= new0(char, strlen(host
));
1425 strncat(t
, host
, a
- host
+ 1);
1426 strncat(t
, a
+ 2, rbracket
- a
- 2);
1427 e
= bus_address_escape(t
);
1430 } else if (*(a
+ 1) == '\0' || strchr(a
+ 1, '@'))
1434 /* Let's see if a port was given */
1435 m
= strchr(rbracket
? rbracket
+ 1 : host
, ':');
1438 bool got_forward_slash
= false;
1444 p
= strndupa_safe(p
, t
- p
);
1445 got_forward_slash
= true;
1448 if (!in_charset(p
, "0123456789") || *p
== '\0') {
1449 if (!hostname_is_valid(p
, 0) || got_forward_slash
)
1453 goto interpret_port_as_machine_old_syntax
;
1457 /* Let's see if a machine was given */
1458 m
= strchr(rbracket
? rbracket
+ 1 : host
, '/');
1461 interpret_port_as_machine_old_syntax
:
1462 /* Let's make sure this is not a port of some kind,
1463 * and is a valid machine name. */
1464 if (!in_charset(m
, "0123456789") && hostname_is_valid(m
, 0))
1465 c
= strjoina(",argv", p
? "7" : "5", "=--machine=", m
);
1471 t
= strndupa_safe(host
, strcspn(host
, ":/"));
1473 e
= bus_address_escape(t
);
1478 a
= strjoin("unixexec:path=ssh,argv1=-xT", p
? ",argv2=-p,argv3=" : "", strempty(p
),
1479 ",argv", p
? "4" : "2", "=--,argv", p
? "5" : "3", "=", e
,
1480 ",argv", p
? "6" : "4", "=systemd-stdio-bridge", c
);
1484 return free_and_replace(b
->address
, a
);
1487 _public_
int sd_bus_open_system_remote(sd_bus
**ret
, const char *host
) {
1488 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1491 assert_return(host
, -EINVAL
);
1492 assert_return(ret
, -EINVAL
);
1498 r
= bus_set_address_system_remote(b
, host
);
1502 b
->bus_client
= true;
1504 b
->is_system
= true;
1505 b
->is_local
= false;
1507 r
= sd_bus_start(b
);
1515 int bus_set_address_machine(sd_bus
*b
, bool user
, const char *machine
) {
1516 _cleanup_free_
char *a
= NULL
;
1522 rhs
= strchr(machine
, '@');
1524 _cleanup_free_
char *u
= NULL
, *eu
= NULL
, *erhs
= NULL
;
1526 /* If there's an "@" in the container specification, we'll connect as a user specified at its
1527 * left hand side, which is useful in combination with user=true. This isn't as trivial as it
1528 * might sound: it's not sufficient to enter the container and connect to some socket there,
1529 * since the --user socket path depends on $XDG_RUNTIME_DIR which is set via PAM. Thus, to be
1530 * able to connect, we need to have a PAM session. Our way out? We use systemd-run to get
1531 * into the container and acquire a PAM session there, and then invoke systemd-stdio-bridge
1532 * in it, which propagates the bus transport to us. */
1536 u
= strndup(machine
, rhs
- machine
);
1538 u
= getusername_malloc(); /* Empty user name, let's use the local one */
1542 eu
= bus_address_escape(u
);
1548 /* No "@" specified but we shall connect to the user instance? Then assume root (and
1549 * not a user named identically to the calling one). This means:
1551 * --machine=foobar --user → connect to user bus of root user in container "foobar"
1552 * --machine=@foobar --user → connect to user bus of user named like the calling user in container "foobar"
1554 * Why? so that behaviour for "--machine=foobar --system" is roughly similar to
1555 * "--machine=foobar --user": both times we unconditionally connect as root user
1556 * regardless what the calling user is. */
1561 if (!isempty(rhs
)) {
1562 erhs
= bus_address_escape(rhs
);
1567 /* systemd-run -M… -PGq --wait -pUser=… -pPAMName=login systemd-stdio-bridge */
1569 a
= strjoin("unixexec:path=systemd-run,"
1570 "argv1=-M", erhs
?: ".host", ","
1573 "argv4=-pUser%3d", eu
?: "root", ",",
1574 "argv5=-pPAMName%3dlogin,"
1575 "argv6=systemd-stdio-bridge");
1580 /* Ideally we'd use the "--user" switch to systemd-stdio-bridge here, but it's only
1581 * available in recent systemd versions. Using the "-p" switch with the explicit path
1582 * is a working alternative, and is compatible with older versions, hence that's what
1584 if (!strextend(&a
, ",argv7=-punix:path%3d%24%7bXDG_RUNTIME_DIR%7d/bus"))
1588 _cleanup_free_
char *e
= NULL
;
1590 /* Just a container name, we can go the simple way, and just join the container, and connect
1591 * to the well-known path of the system bus there. */
1593 e
= bus_address_escape(machine
);
1597 a
= strjoin("x-machine-unix:machine=", e
);
1602 return free_and_replace(b
->address
, a
);
1605 static int user_and_machine_valid(const char *user_and_machine
) {
1608 /* Checks if a container specification in the form "user@container" or just "container" is valid.
1610 * If the "@" syntax is used we'll allow either the "user" or the "container" part to be omitted, but
1613 h
= strchr(user_and_machine
, '@');
1615 h
= user_and_machine
;
1617 _cleanup_free_
char *user
= NULL
;
1619 user
= strndup(user_and_machine
, h
- user_and_machine
);
1623 if (!isempty(user
) && !valid_user_group_name(user
, VALID_USER_RELAX
| VALID_USER_ALLOW_NUMERIC
))
1629 return !isempty(user
);
1632 return hostname_is_valid(h
, VALID_HOSTNAME_DOT_HOST
);
1635 static int user_and_machine_equivalent(const char *user_and_machine
) {
1636 _cleanup_free_
char *un
= NULL
;
1639 /* Returns true if the specified user+machine name are actually equivalent to our own identity and
1640 * our own host. If so we can shortcut things. Why bother? Because that way we don't have to fork
1641 * off short-lived worker processes that are then unavailable for authentication and logging in the
1642 * peer. Moreover joining a namespace requires privileges. If we are in the right namespace anyway,
1643 * we can avoid permission problems thus. */
1645 assert(user_and_machine
);
1647 /* Omitting the user name means that we shall use the same user name as we run as locally, which
1648 * means we'll end up on the same host, let's shortcut */
1649 if (streq(user_and_machine
, "@.host"))
1652 /* Otherwise, if we are root, then we can also allow the ".host" syntax, as that's the user this
1653 * would connect to. */
1654 uid_t uid
= geteuid();
1656 if (uid
== 0 && STR_IN_SET(user_and_machine
, ".host", "root@.host", "0@.host"))
1659 /* Otherwise, we have to figure out our user id and name, and compare things with that. */
1660 char buf
[DECIMAL_STR_MAX(uid_t
)];
1661 xsprintf(buf
, UID_FMT
, uid
);
1663 f
= startswith(user_and_machine
, buf
);
1665 un
= getusername_malloc();
1669 f
= startswith(user_and_machine
, un
);
1674 return STR_IN_SET(f
, "@", "@.host");
1677 _public_
int sd_bus_open_system_machine(sd_bus
**ret
, const char *user_and_machine
) {
1678 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1681 assert_return(user_and_machine
, -EINVAL
);
1682 assert_return(ret
, -EINVAL
);
1684 if (user_and_machine_equivalent(user_and_machine
))
1685 return sd_bus_open_system(ret
);
1687 r
= user_and_machine_valid(user_and_machine
);
1691 assert_return(r
> 0, -EINVAL
);
1697 r
= bus_set_address_machine(b
, false, user_and_machine
);
1701 b
->bus_client
= true;
1702 b
->is_system
= true;
1704 r
= sd_bus_start(b
);
1712 _public_
int sd_bus_open_user_machine(sd_bus
**ret
, const char *user_and_machine
) {
1713 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1716 assert_return(user_and_machine
, -EINVAL
);
1717 assert_return(ret
, -EINVAL
);
1719 /* Shortcut things if we'd end up on this host and as the same user. */
1720 if (user_and_machine_equivalent(user_and_machine
))
1721 return sd_bus_open_user(ret
);
1723 r
= user_and_machine_valid(user_and_machine
);
1727 assert_return(r
> 0, -EINVAL
);
1733 r
= bus_set_address_machine(b
, true, user_and_machine
);
1737 b
->bus_client
= true;
1740 r
= sd_bus_start(b
);
1748 _public_
void sd_bus_close(sd_bus
*bus
) {
1751 if (bus
->state
== BUS_CLOSED
)
1753 if (bus_pid_changed(bus
))
1756 /* Don't leave ssh hanging around */
1759 bus_set_state(bus
, BUS_CLOSED
);
1761 sd_bus_detach_event(bus
);
1763 /* Drop all queued messages so that they drop references to
1764 * the bus object and the bus may be freed */
1765 bus_reset_queues(bus
);
1767 bus_close_io_fds(bus
);
1768 bus_close_inotify_fd(bus
);
1771 _public_ sd_bus
*sd_bus_close_unref(sd_bus
*bus
) {
1777 return sd_bus_unref(bus
);
1780 _public_ sd_bus
* sd_bus_flush_close_unref(sd_bus
*bus
) {
1784 /* Have to do this before flush() to prevent hang */
1788 return sd_bus_close_unref(bus
);
1791 void bus_enter_closing(sd_bus
*bus
) {
1794 if (!IN_SET(bus
->state
, BUS_WATCH_BIND
, BUS_OPENING
, BUS_AUTHENTICATING
, BUS_HELLO
, BUS_RUNNING
))
1797 bus_set_state(bus
, BUS_CLOSING
);
1800 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_bus
, sd_bus
, bus_free
);
1802 _public_
int sd_bus_is_open(sd_bus
*bus
) {
1806 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1807 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1809 return BUS_IS_OPEN(bus
->state
);
1812 _public_
int sd_bus_is_ready(sd_bus
*bus
) {
1816 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1817 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1819 return bus
->state
== BUS_RUNNING
;
1822 _public_
int sd_bus_can_send(sd_bus
*bus
, char type
) {
1825 assert_return(bus
, -EINVAL
);
1826 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1827 assert_return(bus
->state
!= BUS_UNSET
, -ENOTCONN
);
1828 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1830 if (bus
->is_monitor
)
1833 if (type
== SD_BUS_TYPE_UNIX_FD
) {
1834 if (!bus
->accept_fd
)
1837 r
= bus_ensure_running(bus
);
1841 return bus
->can_fds
;
1844 return bus_type_is_valid(type
);
1847 _public_
int sd_bus_get_bus_id(sd_bus
*bus
, sd_id128_t
*id
) {
1850 assert_return(bus
, -EINVAL
);
1851 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1852 assert_return(id
, -EINVAL
);
1853 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1855 r
= bus_ensure_running(bus
);
1859 *id
= bus
->server_id
;
1863 #define COOKIE_CYCLED (UINT32_C(1) << 31)
1865 static uint64_t cookie_inc(uint64_t cookie
) {
1867 /* Stay within the 32bit range, since classic D-Bus can't deal with more */
1868 if (cookie
>= UINT32_MAX
)
1869 return COOKIE_CYCLED
; /* Don't go back to zero, but use the highest bit for checking
1870 * whether we are looping. */
1875 static int next_cookie(sd_bus
*b
) {
1876 uint64_t new_cookie
;
1880 new_cookie
= cookie_inc(b
->cookie
);
1882 /* Small optimization: don't bother with checking for cookie reuse until we overran cookiespace at
1883 * least once, but then do it thorougly. */
1884 if (FLAGS_SET(new_cookie
, COOKIE_CYCLED
)) {
1887 /* Check if the cookie is currently in use. If so, pick the next one */
1888 for (i
= 0; i
< COOKIE_CYCLED
; i
++) {
1889 if (!ordered_hashmap_contains(b
->reply_callbacks
, &new_cookie
))
1892 new_cookie
= cookie_inc(new_cookie
);
1895 /* Can't fulfill request */
1900 b
->cookie
= new_cookie
;
1904 static int bus_seal_message(sd_bus
*b
, sd_bus_message
*m
, usec_t timeout
) {
1911 /* If we copy the same message to multiple
1912 * destinations, avoid using the same cookie
1914 b
->cookie
= MAX(b
->cookie
, BUS_MESSAGE_COOKIE(m
));
1919 r
= sd_bus_get_method_call_timeout(b
, &timeout
);
1924 if (!m
->sender
&& b
->patch_sender
) {
1925 r
= sd_bus_message_set_sender(m
, b
->patch_sender
);
1934 return sd_bus_message_seal(m
, b
->cookie
, timeout
);
1937 static int bus_remarshal_message(sd_bus
*b
, sd_bus_message
**m
) {
1938 bool remarshal
= false;
1942 /* wrong packet version */
1943 if (b
->message_version
!= 0 && b
->message_version
!= (*m
)->header
->version
)
1946 /* wrong packet endianness */
1947 if (b
->message_endian
!= 0 && b
->message_endian
!= (*m
)->header
->endian
)
1950 return remarshal
? bus_message_remarshal(b
, m
) : 0;
1953 int bus_seal_synthetic_message(sd_bus
*b
, sd_bus_message
*m
) {
1957 /* Fake some timestamps, if they were requested, and not
1958 * already initialized */
1959 if (b
->attach_timestamp
) {
1960 if (m
->realtime
<= 0)
1961 m
->realtime
= now(CLOCK_REALTIME
);
1963 if (m
->monotonic
<= 0)
1964 m
->monotonic
= now(CLOCK_MONOTONIC
);
1967 /* The bus specification says the serial number cannot be 0,
1968 * hence let's fill something in for synthetic messages. Since
1969 * synthetic messages might have a fake sender and we don't
1970 * want to interfere with the real sender's serial numbers we
1971 * pick a fixed, artificial one. */
1972 return sd_bus_message_seal(m
, UINT32_MAX
, 0);
1975 static int bus_write_message(sd_bus
*bus
, sd_bus_message
*m
, size_t *idx
) {
1981 r
= bus_socket_write_message(bus
, m
, idx
);
1985 if (*idx
>= BUS_MESSAGE_SIZE(m
))
1986 log_debug("Sent message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%" PRIu64
" reply_cookie=%" PRIu64
" signature=%s error-name=%s error-message=%s",
1987 bus_message_type_to_string(m
->header
->type
),
1988 strna(sd_bus_message_get_sender(m
)),
1989 strna(sd_bus_message_get_destination(m
)),
1990 strna(sd_bus_message_get_path(m
)),
1991 strna(sd_bus_message_get_interface(m
)),
1992 strna(sd_bus_message_get_member(m
)),
1993 BUS_MESSAGE_COOKIE(m
),
1995 strna(m
->root_container
.signature
),
1996 strna(m
->error
.name
),
1997 strna(m
->error
.message
));
2002 static int dispatch_wqueue(sd_bus
*bus
) {
2006 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
2008 while (bus
->wqueue_size
> 0) {
2010 r
= bus_write_message(bus
, bus
->wqueue
[0], &bus
->windex
);
2014 /* Didn't do anything this time */
2016 else if (bus
->windex
>= BUS_MESSAGE_SIZE(bus
->wqueue
[0])) {
2017 /* Fully written. Let's drop the entry from
2020 * This isn't particularly optimized, but
2021 * well, this is supposed to be our worst-case
2022 * buffer only, and the socket buffer is
2023 * supposed to be our primary buffer, and if
2024 * it got full, then all bets are off
2028 bus_message_unref_queued(bus
->wqueue
[0], bus
);
2029 memmove(bus
->wqueue
, bus
->wqueue
+ 1, sizeof(sd_bus_message
*) * bus
->wqueue_size
);
2039 static int bus_read_message(sd_bus
*bus
) {
2042 return bus_socket_read_message(bus
);
2045 int bus_rqueue_make_room(sd_bus
*bus
) {
2048 if (bus
->rqueue_size
>= BUS_RQUEUE_MAX
)
2051 if (!GREEDY_REALLOC(bus
->rqueue
, bus
->rqueue_size
+ 1))
2057 static void rqueue_drop_one(sd_bus
*bus
, size_t i
) {
2059 assert(i
< bus
->rqueue_size
);
2061 bus_message_unref_queued(bus
->rqueue
[i
], bus
);
2062 memmove(bus
->rqueue
+ i
, bus
->rqueue
+ i
+ 1, sizeof(sd_bus_message
*) * (bus
->rqueue_size
- i
- 1));
2066 static int dispatch_rqueue(sd_bus
*bus
, sd_bus_message
**m
) {
2071 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
2074 if (bus
->rqueue_size
> 0) {
2075 /* Dispatch a queued message */
2076 *m
= sd_bus_message_ref(bus
->rqueue
[0]);
2077 rqueue_drop_one(bus
, 0);
2081 /* Try to read a new message */
2082 r
= bus_read_message(bus
);
2094 _public_
int sd_bus_send(sd_bus
*bus
, sd_bus_message
*_m
, uint64_t *cookie
) {
2095 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= sd_bus_message_ref(_m
);
2098 assert_return(m
, -EINVAL
);
2101 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2103 assert_return(bus
= m
->bus
, -ENOTCONN
);
2104 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2106 if (!BUS_IS_OPEN(bus
->state
))
2110 r
= sd_bus_can_send(bus
, SD_BUS_TYPE_UNIX_FD
);
2117 /* If the cookie number isn't kept, then we know that no reply
2119 if (!cookie
&& !m
->sealed
)
2120 m
->header
->flags
|= BUS_MESSAGE_NO_REPLY_EXPECTED
;
2122 r
= bus_seal_message(bus
, m
, 0);
2126 /* Remarshall if we have to. This will possibly unref the
2127 * message and place a replacement in m */
2128 r
= bus_remarshal_message(bus
, &m
);
2132 /* If this is a reply and no reply was requested, then let's
2133 * suppress this, if we can */
2137 if (IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
) && bus
->wqueue_size
<= 0) {
2140 r
= bus_write_message(bus
, m
, &idx
);
2142 if (ERRNO_IS_DISCONNECT(r
)) {
2143 bus_enter_closing(bus
);
2150 if (idx
< BUS_MESSAGE_SIZE(m
)) {
2151 /* Wasn't fully written. So let's remember how
2152 * much was written. Note that the first entry
2153 * of the wqueue array is always allocated so
2154 * that we always can remember how much was
2156 bus
->wqueue
[0] = bus_message_ref_queued(m
, bus
);
2157 bus
->wqueue_size
= 1;
2162 /* Just append it to the queue. */
2164 if (bus
->wqueue_size
>= BUS_WQUEUE_MAX
)
2167 if (!GREEDY_REALLOC(bus
->wqueue
, bus
->wqueue_size
+ 1))
2170 bus
->wqueue
[bus
->wqueue_size
++] = bus_message_ref_queued(m
, bus
);
2175 *cookie
= BUS_MESSAGE_COOKIE(m
);
2180 _public_
int sd_bus_send_to(sd_bus
*bus
, sd_bus_message
*m
, const char *destination
, uint64_t *cookie
) {
2183 assert_return(m
, -EINVAL
);
2186 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2188 assert_return(bus
= m
->bus
, -ENOTCONN
);
2189 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2191 if (!BUS_IS_OPEN(bus
->state
))
2194 if (!streq_ptr(m
->destination
, destination
)) {
2199 r
= sd_bus_message_set_destination(m
, destination
);
2204 return sd_bus_send(bus
, m
, cookie
);
2207 static usec_t
calc_elapse(sd_bus
*bus
, uint64_t usec
) {
2210 assert_cc(sizeof(usec_t
) == sizeof(uint64_t));
2212 if (usec
== USEC_INFINITY
)
2215 /* We start all timeouts the instant we enter BUS_HELLO/BUS_RUNNING state, so that the don't run in parallel
2216 * with any connection setup states. Hence, if a method callback is started earlier than that we just store the
2217 * relative timestamp, and afterwards the absolute one. */
2219 if (IN_SET(bus
->state
, BUS_WATCH_BIND
, BUS_OPENING
, BUS_AUTHENTICATING
))
2222 return usec_add(now(CLOCK_MONOTONIC
), usec
);
2225 static int timeout_compare(const void *a
, const void *b
) {
2226 const struct reply_callback
*x
= a
, *y
= b
;
2228 if (x
->timeout_usec
!= 0 && y
->timeout_usec
== 0)
2231 if (x
->timeout_usec
== 0 && y
->timeout_usec
!= 0)
2234 return CMP(x
->timeout_usec
, y
->timeout_usec
);
2237 _public_
int sd_bus_call_async(
2241 sd_bus_message_handler_t callback
,
2245 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= sd_bus_message_ref(_m
);
2246 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*s
= NULL
;
2249 assert_return(m
, -EINVAL
);
2250 assert_return(m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
, -EINVAL
);
2251 assert_return(!m
->sealed
|| (!!callback
== !(m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
)), -EINVAL
);
2254 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2256 assert_return(bus
= m
->bus
, -ENOTCONN
);
2257 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2259 if (!BUS_IS_OPEN(bus
->state
))
2262 /* If no callback is specified and there's no interest in a slot, then there's no reason to ask for a reply */
2263 if (!callback
&& !slot
&& !m
->sealed
)
2264 m
->header
->flags
|= BUS_MESSAGE_NO_REPLY_EXPECTED
;
2266 r
= ordered_hashmap_ensure_allocated(&bus
->reply_callbacks
, &uint64_hash_ops
);
2270 r
= prioq_ensure_allocated(&bus
->reply_callbacks_prioq
, timeout_compare
);
2274 r
= bus_seal_message(bus
, m
, usec
);
2278 r
= bus_remarshal_message(bus
, &m
);
2282 if (slot
|| callback
) {
2283 s
= bus_slot_allocate(bus
, !slot
, BUS_REPLY_CALLBACK
, sizeof(struct reply_callback
), userdata
);
2287 s
->reply_callback
.callback
= callback
;
2289 s
->reply_callback
.cookie
= BUS_MESSAGE_COOKIE(m
);
2290 r
= ordered_hashmap_put(bus
->reply_callbacks
, &s
->reply_callback
.cookie
, &s
->reply_callback
);
2292 s
->reply_callback
.cookie
= 0;
2296 s
->reply_callback
.timeout_usec
= calc_elapse(bus
, m
->timeout
);
2297 if (s
->reply_callback
.timeout_usec
!= 0) {
2298 r
= prioq_put(bus
->reply_callbacks_prioq
, &s
->reply_callback
, &s
->reply_callback
.prioq_idx
);
2300 s
->reply_callback
.timeout_usec
= 0;
2306 r
= sd_bus_send(bus
, m
, s
? &s
->reply_callback
.cookie
: NULL
);
2317 int bus_ensure_running(sd_bus
*bus
) {
2322 if (bus
->state
== BUS_RUNNING
)
2326 if (IN_SET(bus
->state
, BUS_UNSET
, BUS_CLOSED
, BUS_CLOSING
))
2329 r
= sd_bus_process(bus
, NULL
);
2332 if (bus
->state
== BUS_RUNNING
)
2337 r
= sd_bus_wait(bus
, UINT64_MAX
);
2343 _public_
int sd_bus_call(
2347 sd_bus_error
*error
,
2348 sd_bus_message
**reply
) {
2350 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= sd_bus_message_ref(_m
);
2356 bus_assert_return(m
, -EINVAL
, error
);
2357 bus_assert_return(m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
, -EINVAL
, error
);
2358 bus_assert_return(!(m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
), -EINVAL
, error
);
2359 bus_assert_return(!bus_error_is_dirty(error
), -EINVAL
, error
);
2362 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2364 assert_return(bus
= m
->bus
, -ENOTCONN
);
2365 bus_assert_return(!bus_pid_changed(bus
), -ECHILD
, error
);
2367 if (!BUS_IS_OPEN(bus
->state
)) {
2372 r
= bus_ensure_running(bus
);
2376 i
= bus
->rqueue_size
;
2378 r
= bus_seal_message(bus
, m
, usec
);
2382 r
= bus_remarshal_message(bus
, &m
);
2386 r
= sd_bus_send(bus
, m
, &cookie
);
2390 timeout
= calc_elapse(bus
, m
->timeout
);
2395 while (i
< bus
->rqueue_size
) {
2396 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*incoming
= NULL
;
2398 incoming
= sd_bus_message_ref(bus
->rqueue
[i
]);
2400 if (incoming
->reply_cookie
== cookie
) {
2401 /* Found a match! */
2403 rqueue_drop_one(bus
, i
);
2404 log_debug_bus_message(incoming
);
2406 if (incoming
->header
->type
== SD_BUS_MESSAGE_METHOD_RETURN
) {
2408 if (incoming
->n_fds
<= 0 || bus
->accept_fd
) {
2410 *reply
= TAKE_PTR(incoming
);
2415 return sd_bus_error_set(error
, SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Reply message contained file descriptors which I couldn't accept. Sorry.");
2417 } else if (incoming
->header
->type
== SD_BUS_MESSAGE_METHOD_ERROR
)
2418 return sd_bus_error_copy(error
, &incoming
->error
);
2424 } else if (BUS_MESSAGE_COOKIE(incoming
) == cookie
&&
2427 streq(bus
->unique_name
, incoming
->sender
)) {
2429 rqueue_drop_one(bus
, i
);
2431 /* Our own message? Somebody is trying to send its own client a message,
2432 * let's not dead-lock, let's fail immediately. */
2438 /* Try to read more, right-away */
2442 r
= bus_read_message(bus
);
2444 if (ERRNO_IS_DISCONNECT(r
)) {
2445 bus_enter_closing(bus
);
2457 n
= now(CLOCK_MONOTONIC
);
2467 r
= bus_poll(bus
, true, left
);
2469 if (ERRNO_IS_TRANSIENT(r
))
2478 r
= dispatch_wqueue(bus
);
2480 if (ERRNO_IS_DISCONNECT(r
)) {
2481 bus_enter_closing(bus
);
2490 return sd_bus_error_set_errno(error
, r
);
2493 _public_
int sd_bus_get_fd(sd_bus
*bus
) {
2494 assert_return(bus
, -EINVAL
);
2495 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2496 assert_return(bus
->input_fd
== bus
->output_fd
, -EPERM
);
2497 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2499 if (bus
->state
== BUS_CLOSED
)
2502 if (bus
->inotify_fd
>= 0)
2503 return bus
->inotify_fd
;
2505 if (bus
->input_fd
>= 0)
2506 return bus
->input_fd
;
2511 _public_
int sd_bus_get_events(sd_bus
*bus
) {
2514 assert_return(bus
, -EINVAL
);
2515 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2516 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2518 switch (bus
->state
) {
2524 case BUS_WATCH_BIND
:
2532 case BUS_AUTHENTICATING
:
2533 if (bus_socket_auth_needs_write(bus
))
2541 if (bus
->rqueue_size
<= 0)
2543 if (bus
->wqueue_size
> 0)
2551 assert_not_reached();
2557 _public_
int sd_bus_get_timeout(sd_bus
*bus
, uint64_t *timeout_usec
) {
2558 struct reply_callback
*c
;
2560 assert_return(bus
, -EINVAL
);
2561 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2562 assert_return(timeout_usec
, -EINVAL
);
2563 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2565 if (!BUS_IS_OPEN(bus
->state
) && bus
->state
!= BUS_CLOSING
)
2568 if (bus
->track_queue
) {
2573 switch (bus
->state
) {
2575 case BUS_AUTHENTICATING
:
2576 *timeout_usec
= bus
->auth_timeout
;
2581 if (bus
->rqueue_size
> 0) {
2586 c
= prioq_peek(bus
->reply_callbacks_prioq
);
2588 *timeout_usec
= UINT64_MAX
;
2592 if (c
->timeout_usec
== 0) {
2593 *timeout_usec
= UINT64_MAX
;
2597 *timeout_usec
= c
->timeout_usec
;
2604 case BUS_WATCH_BIND
:
2606 *timeout_usec
= UINT64_MAX
;
2610 assert_not_reached();
2614 static int process_timeout(sd_bus
*bus
) {
2615 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2616 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* m
= NULL
;
2617 struct reply_callback
*c
;
2624 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
2626 c
= prioq_peek(bus
->reply_callbacks_prioq
);
2630 n
= now(CLOCK_MONOTONIC
);
2631 if (c
->timeout_usec
> n
)
2634 r
= bus_message_new_synthetic_error(
2637 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY
, "Method call timed out"),
2642 m
->read_counter
= ++bus
->read_counter
;
2644 r
= bus_seal_synthetic_message(bus
, m
);
2648 assert_se(prioq_pop(bus
->reply_callbacks_prioq
) == c
);
2649 c
->timeout_usec
= 0;
2651 ordered_hashmap_remove(bus
->reply_callbacks
, &c
->cookie
);
2654 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
2656 bus
->iteration_counter
++;
2658 is_hello
= bus
->state
== BUS_HELLO
&& c
->callback
== hello_callback
;
2660 bus
->current_message
= m
;
2661 bus
->current_slot
= sd_bus_slot_ref(slot
);
2662 bus
->current_handler
= c
->callback
;
2663 bus
->current_userdata
= slot
->userdata
;
2664 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
2665 bus
->current_userdata
= NULL
;
2666 bus
->current_handler
= NULL
;
2667 bus
->current_slot
= NULL
;
2668 bus
->current_message
= NULL
;
2671 bus_slot_disconnect(slot
, true);
2673 sd_bus_slot_unref(slot
);
2675 /* When this is the hello message and it timed out, then make sure to propagate the error up, don't just log
2676 * and ignore the callback handler's return value. */
2680 return bus_maybe_reply_error(m
, r
, &error_buffer
);
2683 static int process_hello(sd_bus
*bus
, sd_bus_message
*m
) {
2687 if (bus
->state
!= BUS_HELLO
)
2690 /* Let's make sure the first message on the bus is the HELLO
2691 * reply. But note that we don't actually parse the message
2692 * here (we leave that to the usual handling), we just verify
2693 * we don't let any earlier msg through. */
2695 if (!IN_SET(m
->header
->type
, SD_BUS_MESSAGE_METHOD_RETURN
, SD_BUS_MESSAGE_METHOD_ERROR
))
2698 if (m
->reply_cookie
!= 1)
2704 static int process_reply(sd_bus
*bus
, sd_bus_message
*m
) {
2705 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*synthetic_reply
= NULL
;
2706 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2707 struct reply_callback
*c
;
2715 if (!IN_SET(m
->header
->type
, SD_BUS_MESSAGE_METHOD_RETURN
, SD_BUS_MESSAGE_METHOD_ERROR
))
2718 if (m
->destination
&& bus
->unique_name
&& !streq_ptr(m
->destination
, bus
->unique_name
))
2721 c
= ordered_hashmap_remove(bus
->reply_callbacks
, &m
->reply_cookie
);
2727 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
2729 if (m
->n_fds
> 0 && !bus
->accept_fd
) {
2731 /* If the reply contained a file descriptor which we
2732 * didn't want we pass an error instead. */
2734 r
= bus_message_new_synthetic_error(
2737 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Reply message contained file descriptor"),
2742 /* Copy over original timestamp */
2743 synthetic_reply
->realtime
= m
->realtime
;
2744 synthetic_reply
->monotonic
= m
->monotonic
;
2745 synthetic_reply
->seqnum
= m
->seqnum
;
2746 synthetic_reply
->read_counter
= m
->read_counter
;
2748 r
= bus_seal_synthetic_message(bus
, synthetic_reply
);
2752 m
= synthetic_reply
;
2754 r
= sd_bus_message_rewind(m
, true);
2759 if (c
->timeout_usec
!= 0) {
2760 prioq_remove(bus
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
2761 c
->timeout_usec
= 0;
2764 is_hello
= bus
->state
== BUS_HELLO
&& c
->callback
== hello_callback
;
2766 bus
->current_slot
= sd_bus_slot_ref(slot
);
2767 bus
->current_handler
= c
->callback
;
2768 bus
->current_userdata
= slot
->userdata
;
2769 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
2770 bus
->current_userdata
= NULL
;
2771 bus
->current_handler
= NULL
;
2772 bus
->current_slot
= NULL
;
2775 bus_slot_disconnect(slot
, true);
2777 sd_bus_slot_unref(slot
);
2779 /* When this is the hello message and it failed, then make sure to propagate the error up, don't just log and
2780 * ignore the callback handler's return value. */
2784 return bus_maybe_reply_error(m
, r
, &error_buffer
);
2787 static int process_filter(sd_bus
*bus
, sd_bus_message
*m
) {
2788 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2795 bus
->filter_callbacks_modified
= false;
2797 LIST_FOREACH(callbacks
, l
, bus
->filter_callbacks
) {
2800 if (bus
->filter_callbacks_modified
)
2803 /* Don't run this more than once per iteration */
2804 if (l
->last_iteration
== bus
->iteration_counter
)
2807 l
->last_iteration
= bus
->iteration_counter
;
2809 r
= sd_bus_message_rewind(m
, true);
2813 slot
= container_of(l
, sd_bus_slot
, filter_callback
);
2815 bus
->current_slot
= sd_bus_slot_ref(slot
);
2816 bus
->current_handler
= l
->callback
;
2817 bus
->current_userdata
= slot
->userdata
;
2818 r
= l
->callback(m
, slot
->userdata
, &error_buffer
);
2819 bus
->current_userdata
= NULL
;
2820 bus
->current_handler
= NULL
;
2821 bus
->current_slot
= sd_bus_slot_unref(slot
);
2823 r
= bus_maybe_reply_error(m
, r
, &error_buffer
);
2829 } while (bus
->filter_callbacks_modified
);
2834 static int process_match(sd_bus
*bus
, sd_bus_message
*m
) {
2841 bus
->match_callbacks_modified
= false;
2843 r
= bus_match_run(bus
, &bus
->match_callbacks
, m
);
2847 } while (bus
->match_callbacks_modified
);
2852 static int process_builtin(sd_bus
*bus
, sd_bus_message
*m
) {
2853 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2859 if (bus
->is_monitor
)
2862 if (bus
->manual_peer_interface
)
2865 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_CALL
)
2868 if (!streq_ptr(m
->interface
, "org.freedesktop.DBus.Peer"))
2871 if (m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
)
2874 if (streq_ptr(m
->member
, "Ping"))
2875 r
= sd_bus_message_new_method_return(m
, &reply
);
2876 else if (streq_ptr(m
->member
, "GetMachineId")) {
2879 r
= sd_id128_get_machine(&id
);
2883 r
= sd_bus_message_new_method_return(m
, &reply
);
2887 r
= sd_bus_message_append(reply
, "s", SD_ID128_TO_STRING(id
));
2889 r
= sd_bus_message_new_method_errorf(
2891 SD_BUS_ERROR_UNKNOWN_METHOD
,
2892 "Unknown method '%s' on interface '%s'.", m
->member
, m
->interface
);
2897 r
= sd_bus_send(bus
, reply
, NULL
);
2904 static int process_fd_check(sd_bus
*bus
, sd_bus_message
*m
) {
2908 /* If we got a message with a file descriptor which we didn't
2909 * want to accept, then let's drop it. How can this even
2910 * happen? For example, when the kernel queues a message into
2911 * an activatable names's queue which allows fds, and then is
2912 * delivered to us later even though we ourselves did not
2915 if (bus
->is_monitor
)
2924 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_CALL
)
2925 return 1; /* just eat it up */
2927 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Message contains file descriptors, which I cannot accept. Sorry.");
2930 static int process_message(sd_bus
*bus
, sd_bus_message
*m
) {
2936 bus
->current_message
= m
;
2937 bus
->iteration_counter
++;
2939 log_debug_bus_message(m
);
2941 r
= process_hello(bus
, m
);
2945 r
= process_reply(bus
, m
);
2949 r
= process_fd_check(bus
, m
);
2953 r
= process_filter(bus
, m
);
2957 r
= process_match(bus
, m
);
2961 r
= process_builtin(bus
, m
);
2965 r
= bus_process_object(bus
, m
);
2968 bus
->current_message
= NULL
;
2972 static int dispatch_track(sd_bus
*bus
) {
2975 if (!bus
->track_queue
)
2978 bus_track_dispatch(bus
->track_queue
);
2982 static int process_running(sd_bus
*bus
, sd_bus_message
**ret
) {
2983 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2987 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
2989 r
= process_timeout(bus
);
2993 r
= dispatch_wqueue(bus
);
2997 r
= dispatch_track(bus
);
3001 r
= dispatch_rqueue(bus
, &m
);
3007 r
= process_message(bus
, m
);
3012 r
= sd_bus_message_rewind(m
, true);
3020 if (m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
) {
3022 log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s",
3023 strna(sd_bus_message_get_sender(m
)),
3024 strna(sd_bus_message_get_path(m
)),
3025 strna(sd_bus_message_get_interface(m
)),
3026 strna(sd_bus_message_get_member(m
)));
3028 r
= sd_bus_reply_method_errorf(
3030 SD_BUS_ERROR_UNKNOWN_OBJECT
,
3031 "Unknown object '%s'.", m
->path
);
3045 static int bus_exit_now(sd_bus
*bus
) {
3048 /* Exit due to close, if this is requested. If this is bus object is attached to an event source, invokes
3049 * sd_event_exit(), otherwise invokes libc exit(). */
3051 if (bus
->exited
) /* did we already exit? */
3053 if (!bus
->exit_triggered
) /* was the exit condition triggered? */
3055 if (!bus
->exit_on_disconnect
) /* Shall we actually exit on disconnection? */
3058 bus
->exited
= true; /* never exit more than once */
3060 log_debug("Bus connection disconnected, exiting.");
3063 return sd_event_exit(bus
->event
, EXIT_FAILURE
);
3067 assert_not_reached();
3070 static int process_closing_reply_callback(sd_bus
*bus
, struct reply_callback
*c
) {
3071 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
3072 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
3079 r
= bus_message_new_synthetic_error(
3082 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY
, "Connection terminated"),
3087 m
->read_counter
= ++bus
->read_counter
;
3089 r
= bus_seal_synthetic_message(bus
, m
);
3093 if (c
->timeout_usec
!= 0) {
3094 prioq_remove(bus
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
3095 c
->timeout_usec
= 0;
3098 ordered_hashmap_remove(bus
->reply_callbacks
, &c
->cookie
);
3101 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
3103 bus
->iteration_counter
++;
3105 bus
->current_message
= m
;
3106 bus
->current_slot
= sd_bus_slot_ref(slot
);
3107 bus
->current_handler
= c
->callback
;
3108 bus
->current_userdata
= slot
->userdata
;
3109 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
3110 bus
->current_userdata
= NULL
;
3111 bus
->current_handler
= NULL
;
3112 bus
->current_slot
= NULL
;
3113 bus
->current_message
= NULL
;
3116 bus_slot_disconnect(slot
, true);
3118 sd_bus_slot_unref(slot
);
3120 return bus_maybe_reply_error(m
, r
, &error_buffer
);
3123 static int process_closing(sd_bus
*bus
, sd_bus_message
**ret
) {
3124 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
3125 struct reply_callback
*c
;
3129 assert(bus
->state
== BUS_CLOSING
);
3131 /* First, fail all outstanding method calls */
3132 c
= ordered_hashmap_first(bus
->reply_callbacks
);
3134 return process_closing_reply_callback(bus
, c
);
3136 /* Then, fake-drop all remaining bus tracking references */
3138 bus_track_close(bus
->tracks
);
3142 /* Then, synthesize a Disconnected message */
3143 r
= sd_bus_message_new_signal(
3146 "/org/freedesktop/DBus/Local",
3147 "org.freedesktop.DBus.Local",
3152 bus_message_set_sender_local(bus
, m
);
3153 m
->read_counter
= ++bus
->read_counter
;
3155 r
= bus_seal_synthetic_message(bus
, m
);
3161 bus
->current_message
= m
;
3162 bus
->iteration_counter
++;
3164 r
= process_filter(bus
, m
);
3168 r
= process_match(bus
, m
);
3172 /* Nothing else to do, exit now, if the condition holds */
3173 bus
->exit_triggered
= true;
3174 (void) bus_exit_now(bus
);
3182 bus
->current_message
= NULL
;
3187 static int bus_process_internal(sd_bus
*bus
, sd_bus_message
**ret
) {
3190 /* Returns 0 when we didn't do anything. This should cause the
3191 * caller to invoke sd_bus_wait() before returning the next
3192 * time. Returns > 0 when we did something, which possibly
3193 * means *ret is filled in with an unprocessed message. */
3195 assert_return(bus
, -EINVAL
);
3196 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3197 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3199 /* We don't allow recursively invoking sd_bus_process(). */
3200 assert_return(!bus
->current_message
, -EBUSY
);
3201 assert(!bus
->current_slot
); /* This should be NULL whenever bus->current_message is */
3203 BUS_DONT_DESTROY(bus
);
3205 switch (bus
->state
) {
3213 case BUS_WATCH_BIND
:
3214 r
= bus_socket_process_watch_bind(bus
);
3218 r
= bus_socket_process_opening(bus
);
3221 case BUS_AUTHENTICATING
:
3222 r
= bus_socket_process_authenticating(bus
);
3227 r
= process_running(bus
, ret
);
3231 /* This branch initializes *ret, hence we don't use the generic error checking below */
3235 return process_closing(bus
, ret
);
3238 assert_not_reached();
3241 if (ERRNO_IS_DISCONNECT(r
)) {
3242 bus_enter_closing(bus
);
3253 _public_
int sd_bus_process(sd_bus
*bus
, sd_bus_message
**ret
) {
3254 return bus_process_internal(bus
, ret
);
3257 _public_
int sd_bus_process_priority(sd_bus
*bus
, int64_t priority
, sd_bus_message
**ret
) {
3258 return bus_process_internal(bus
, ret
);
3261 static int bus_poll(sd_bus
*bus
, bool need_more
, uint64_t timeout_usec
) {
3262 struct pollfd p
[2] = {};
3263 usec_t m
= USEC_INFINITY
;
3268 if (bus
->state
== BUS_CLOSING
)
3271 if (!BUS_IS_OPEN(bus
->state
))
3274 if (bus
->state
== BUS_WATCH_BIND
) {
3275 assert(bus
->inotify_fd
>= 0);
3277 p
[0].events
= POLLIN
;
3278 p
[0].fd
= bus
->inotify_fd
;
3283 e
= sd_bus_get_events(bus
);
3288 /* The caller really needs some more data, they don't
3289 * care about what's already read, or any timeouts
3290 * except its own. */
3294 /* The caller wants to process if there's something to
3295 * process, but doesn't care otherwise */
3297 r
= sd_bus_get_timeout(bus
, &until
);
3301 m
= usec_sub_unsigned(until
, now(CLOCK_MONOTONIC
));
3304 p
[0].fd
= bus
->input_fd
;
3305 if (bus
->output_fd
== bus
->input_fd
) {
3309 p
[0].events
= e
& POLLIN
;
3310 p
[1].fd
= bus
->output_fd
;
3311 p
[1].events
= e
& POLLOUT
;
3316 if (timeout_usec
!= UINT64_MAX
&& (m
== USEC_INFINITY
|| timeout_usec
< m
))
3319 r
= ppoll_usec(p
, n
, m
);
3326 _public_
int sd_bus_wait(sd_bus
*bus
, uint64_t timeout_usec
) {
3329 assert_return(bus
, -EINVAL
);
3330 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3331 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3333 if (bus
->state
== BUS_CLOSING
)
3336 if (!BUS_IS_OPEN(bus
->state
))
3339 if (bus
->rqueue_size
> 0)
3342 r
= bus_poll(bus
, false, timeout_usec
);
3343 if (r
< 0 && ERRNO_IS_TRANSIENT(r
))
3344 return 1; /* treat EINTR as success, but let's exit, so that the caller will call back into us soon. */
3349 _public_
int sd_bus_flush(sd_bus
*bus
) {
3352 assert_return(bus
, -EINVAL
);
3353 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3354 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3356 if (bus
->state
== BUS_CLOSING
)
3359 if (!BUS_IS_OPEN(bus
->state
))
3362 /* We never were connected? Don't hang in inotify for good, as there's no timeout set for it */
3363 if (bus
->state
== BUS_WATCH_BIND
)
3366 r
= bus_ensure_running(bus
);
3370 if (bus
->wqueue_size
<= 0)
3374 r
= dispatch_wqueue(bus
);
3376 if (ERRNO_IS_DISCONNECT(r
)) {
3377 bus_enter_closing(bus
);
3384 if (bus
->wqueue_size
<= 0)
3387 r
= bus_poll(bus
, false, UINT64_MAX
);
3389 if (ERRNO_IS_TRANSIENT(r
))
3397 _public_
int sd_bus_add_filter(
3400 sd_bus_message_handler_t callback
,
3405 assert_return(bus
, -EINVAL
);
3406 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3407 assert_return(callback
, -EINVAL
);
3408 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3410 s
= bus_slot_allocate(bus
, !slot
, BUS_FILTER_CALLBACK
, sizeof(struct filter_callback
), userdata
);
3414 s
->filter_callback
.callback
= callback
;
3416 bus
->filter_callbacks_modified
= true;
3417 LIST_PREPEND(callbacks
, bus
->filter_callbacks
, &s
->filter_callback
);
3425 static int add_match_callback(
3428 sd_bus_error
*ret_error
) {
3430 sd_bus_slot
*match_slot
= ASSERT_PTR(userdata
);
3431 bool failed
= false;
3436 sd_bus_slot_ref(match_slot
);
3438 if (sd_bus_message_is_method_error(m
, NULL
)) {
3439 log_debug_errno(sd_bus_message_get_errno(m
),
3440 "Unable to add match %s, failing connection: %s",
3441 match_slot
->match_callback
.match_string
,
3442 sd_bus_message_get_error(m
)->message
);
3446 log_debug("Match %s successfully installed.", match_slot
->match_callback
.match_string
);
3448 if (match_slot
->match_callback
.install_callback
) {
3451 bus
= sd_bus_message_get_bus(m
);
3453 /* This function has been called as slot handler, and we want to call another slot handler. Let's
3454 * update the slot callback metadata temporarily with our own data, and then revert back to the old
3457 assert(bus
->current_slot
== match_slot
->match_callback
.install_slot
);
3458 assert(bus
->current_handler
== add_match_callback
);
3459 assert(bus
->current_userdata
== userdata
);
3461 bus
->current_slot
= match_slot
;
3462 bus
->current_handler
= match_slot
->match_callback
.install_callback
;
3463 bus
->current_userdata
= match_slot
->userdata
;
3465 r
= match_slot
->match_callback
.install_callback(m
, match_slot
->userdata
, ret_error
);
3467 bus
->current_slot
= match_slot
->match_callback
.install_slot
;
3468 bus
->current_handler
= add_match_callback
;
3469 bus
->current_userdata
= userdata
;
3471 if (failed
) /* Generic failure handling: destroy the connection */
3472 bus_enter_closing(sd_bus_message_get_bus(m
));
3477 /* We don't need the install method reply slot anymore, let's free it */
3478 match_slot
->match_callback
.install_slot
= sd_bus_slot_unref(match_slot
->match_callback
.install_slot
);
3480 if (failed
&& match_slot
->floating
)
3481 bus_slot_disconnect(match_slot
, true);
3483 sd_bus_slot_unref(match_slot
);
3488 static int bus_add_match_full(
3493 sd_bus_message_handler_t callback
,
3494 sd_bus_message_handler_t install_callback
,
3497 struct bus_match_component
*components
= NULL
;
3498 unsigned n_components
= 0;
3499 sd_bus_slot
*s
= NULL
;
3502 assert_return(bus
, -EINVAL
);
3503 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3504 assert_return(match
, -EINVAL
);
3505 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3507 r
= bus_match_parse(match
, &components
, &n_components
);
3511 s
= bus_slot_allocate(bus
, !slot
, BUS_MATCH_CALLBACK
, sizeof(struct match_callback
), userdata
);
3517 s
->match_callback
.callback
= callback
;
3518 s
->match_callback
.install_callback
= install_callback
;
3520 if (bus
->bus_client
) {
3521 enum bus_match_scope scope
;
3523 scope
= bus_match_get_scope(components
, n_components
);
3525 /* Do not install server-side matches for matches against the local service, interface or bus path. */
3526 if (scope
!= BUS_MATCH_LOCAL
) {
3528 /* We store the original match string, so that we can use it to remove the match again. */
3530 s
->match_callback
.match_string
= strdup(match
);
3531 if (!s
->match_callback
.match_string
) {
3537 r
= bus_add_match_internal_async(bus
,
3538 &s
->match_callback
.install_slot
,
3539 s
->match_callback
.match_string
,
3546 /* Make the slot of the match call floating now. We need the reference, but we don't
3547 * want that this match pins the bus object, hence we first create it non-floating, but
3548 * then make it floating. */
3549 r
= sd_bus_slot_set_floating(s
->match_callback
.install_slot
, true);
3551 r
= bus_add_match_internal(bus
, s
->match_callback
.match_string
, &s
->match_callback
.after
);
3555 s
->match_added
= true;
3559 bus
->match_callbacks_modified
= true;
3560 r
= bus_match_add(&bus
->match_callbacks
, components
, n_components
, &s
->match_callback
);
3569 bus_match_parse_free(components
, n_components
);
3570 sd_bus_slot_unref(s
);
3575 _public_
int sd_bus_add_match(
3579 sd_bus_message_handler_t callback
,
3582 return bus_add_match_full(bus
, slot
, false, match
, callback
, NULL
, userdata
);
3585 _public_
int sd_bus_add_match_async(
3589 sd_bus_message_handler_t callback
,
3590 sd_bus_message_handler_t install_callback
,
3593 return bus_add_match_full(bus
, slot
, true, match
, callback
, install_callback
, userdata
);
3596 bool bus_pid_changed(sd_bus
*bus
) {
3599 /* We don't support people creating a bus connection and
3600 * keeping it around over a fork(). Let's complain. */
3602 return bus
->original_pid
!= getpid_cached();
3605 static int io_callback(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
3606 sd_bus
*bus
= ASSERT_PTR(userdata
);
3609 /* Note that this is called both on input_fd, output_fd as well as inotify_fd events */
3611 r
= sd_bus_process(bus
, NULL
);
3613 log_debug_errno(r
, "Processing of bus failed, closing down: %m");
3614 bus_enter_closing(bus
);
3620 static int time_callback(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
3621 sd_bus
*bus
= ASSERT_PTR(userdata
);
3624 r
= sd_bus_process(bus
, NULL
);
3626 log_debug_errno(r
, "Processing of bus failed, closing down: %m");
3627 bus_enter_closing(bus
);
3633 static int prepare_callback(sd_event_source
*s
, void *userdata
) {
3634 sd_bus
*bus
= ASSERT_PTR(userdata
);
3640 e
= sd_bus_get_events(bus
);
3646 if (bus
->output_fd
!= bus
->input_fd
) {
3648 r
= sd_event_source_set_io_events(bus
->input_io_event_source
, e
& POLLIN
);
3652 r
= sd_event_source_set_io_events(bus
->output_io_event_source
, e
& POLLOUT
);
3654 r
= sd_event_source_set_io_events(bus
->input_io_event_source
, e
);
3658 r
= sd_bus_get_timeout(bus
, &until
);
3664 j
= sd_event_source_set_time(bus
->time_event_source
, until
);
3671 r
= sd_event_source_set_enabled(bus
->time_event_source
, r
> 0 ? SD_EVENT_ONESHOT
: SD_EVENT_OFF
);
3678 log_debug_errno(r
, "Preparing of bus events failed, closing down: %m");
3679 bus_enter_closing(bus
);
3684 static int quit_callback(sd_event_source
*event
, void *userdata
) {
3685 sd_bus
*bus
= userdata
;
3689 if (bus
->close_on_exit
) {
3697 int bus_attach_io_events(sd_bus
*bus
) {
3702 if (bus
->input_fd
< 0)
3708 if (!bus
->input_io_event_source
) {
3709 r
= sd_event_add_io(bus
->event
, &bus
->input_io_event_source
, bus
->input_fd
, 0, io_callback
, bus
);
3713 r
= sd_event_source_set_prepare(bus
->input_io_event_source
, prepare_callback
);
3717 r
= sd_event_source_set_priority(bus
->input_io_event_source
, bus
->event_priority
);
3721 r
= sd_event_source_set_description(bus
->input_io_event_source
, "bus-input");
3723 r
= sd_event_source_set_io_fd(bus
->input_io_event_source
, bus
->input_fd
);
3728 if (bus
->output_fd
!= bus
->input_fd
) {
3729 assert(bus
->output_fd
>= 0);
3731 if (!bus
->output_io_event_source
) {
3732 r
= sd_event_add_io(bus
->event
, &bus
->output_io_event_source
, bus
->output_fd
, 0, io_callback
, bus
);
3736 r
= sd_event_source_set_priority(bus
->output_io_event_source
, bus
->event_priority
);
3740 r
= sd_event_source_set_description(bus
->input_io_event_source
, "bus-output");
3742 r
= sd_event_source_set_io_fd(bus
->output_io_event_source
, bus
->output_fd
);
3751 static void bus_detach_io_events(sd_bus
*bus
) {
3754 bus
->input_io_event_source
= sd_event_source_disable_unref(bus
->input_io_event_source
);
3755 bus
->output_io_event_source
= sd_event_source_disable_unref(bus
->output_io_event_source
);
3758 int bus_attach_inotify_event(sd_bus
*bus
) {
3763 if (bus
->inotify_fd
< 0)
3769 if (!bus
->inotify_event_source
) {
3770 r
= sd_event_add_io(bus
->event
, &bus
->inotify_event_source
, bus
->inotify_fd
, EPOLLIN
, io_callback
, bus
);
3774 r
= sd_event_source_set_priority(bus
->inotify_event_source
, bus
->event_priority
);
3778 r
= sd_event_source_set_description(bus
->inotify_event_source
, "bus-inotify");
3780 r
= sd_event_source_set_io_fd(bus
->inotify_event_source
, bus
->inotify_fd
);
3787 _public_
int sd_bus_attach_event(sd_bus
*bus
, sd_event
*event
, int priority
) {
3790 assert_return(bus
, -EINVAL
);
3791 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3792 assert_return(!bus
->event
, -EBUSY
);
3794 assert(!bus
->input_io_event_source
);
3795 assert(!bus
->output_io_event_source
);
3796 assert(!bus
->time_event_source
);
3799 bus
->event
= sd_event_ref(event
);
3801 r
= sd_event_default(&bus
->event
);
3806 bus
->event_priority
= priority
;
3808 r
= sd_event_add_time(bus
->event
, &bus
->time_event_source
, CLOCK_MONOTONIC
, 0, 0, time_callback
, bus
);
3812 r
= sd_event_source_set_priority(bus
->time_event_source
, priority
);
3816 r
= sd_event_source_set_description(bus
->time_event_source
, "bus-time");
3820 r
= sd_event_add_exit(bus
->event
, &bus
->quit_event_source
, quit_callback
, bus
);
3824 r
= sd_event_source_set_description(bus
->quit_event_source
, "bus-exit");
3828 r
= bus_attach_io_events(bus
);
3832 r
= bus_attach_inotify_event(bus
);
3839 sd_bus_detach_event(bus
);
3843 _public_
int sd_bus_detach_event(sd_bus
*bus
) {
3844 assert_return(bus
, -EINVAL
);
3845 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3850 bus_detach_io_events(bus
);
3851 bus
->inotify_event_source
= sd_event_source_disable_unref(bus
->inotify_event_source
);
3852 bus
->time_event_source
= sd_event_source_disable_unref(bus
->time_event_source
);
3853 bus
->quit_event_source
= sd_event_source_disable_unref(bus
->quit_event_source
);
3855 bus
->event
= sd_event_unref(bus
->event
);
3859 _public_ sd_event
* sd_bus_get_event(sd_bus
*bus
) {
3860 assert_return(bus
= bus_resolve(bus
), NULL
);
3865 _public_ sd_bus_message
* sd_bus_get_current_message(sd_bus
*bus
) {
3866 assert_return(bus
= bus_resolve(bus
), NULL
);
3868 return bus
->current_message
;
3871 _public_ sd_bus_slot
* sd_bus_get_current_slot(sd_bus
*bus
) {
3872 assert_return(bus
= bus_resolve(bus
), NULL
);
3874 return bus
->current_slot
;
3877 _public_ sd_bus_message_handler_t
sd_bus_get_current_handler(sd_bus
*bus
) {
3878 assert_return(bus
= bus_resolve(bus
), NULL
);
3880 return bus
->current_handler
;
3883 _public_
void* sd_bus_get_current_userdata(sd_bus
*bus
) {
3884 assert_return(bus
= bus_resolve(bus
), NULL
);
3886 return bus
->current_userdata
;
3889 static int bus_default(int (*bus_open
)(sd_bus
**), sd_bus
**default_bus
, sd_bus
**ret
) {
3894 assert(default_bus
);
3897 return !!*default_bus
;
3900 *ret
= sd_bus_ref(*default_bus
);
3908 b
->default_bus_ptr
= default_bus
;
3916 _public_
int sd_bus_default_system(sd_bus
**ret
) {
3917 return bus_default(sd_bus_open_system
, &default_system_bus
, ret
);
3920 _public_
int sd_bus_default_user(sd_bus
**ret
) {
3921 return bus_default(sd_bus_open_user
, &default_user_bus
, ret
);
3924 _public_
int sd_bus_default(sd_bus
**ret
) {
3925 int (*bus_open
)(sd_bus
**) = NULL
;
3928 busp
= bus_choose_default(&bus_open
);
3929 return bus_default(bus_open
, busp
, ret
);
3932 _public_
int sd_bus_get_tid(sd_bus
*b
, pid_t
*tid
) {
3933 assert_return(b
, -EINVAL
);
3934 assert_return(tid
, -EINVAL
);
3935 assert_return(!bus_pid_changed(b
), -ECHILD
);
3943 return sd_event_get_tid(b
->event
, tid
);
3948 _public_
int sd_bus_path_encode(const char *prefix
, const char *external_id
, char **ret_path
) {
3949 _cleanup_free_
char *e
= NULL
;
3952 assert_return(object_path_is_valid(prefix
), -EINVAL
);
3953 assert_return(external_id
, -EINVAL
);
3954 assert_return(ret_path
, -EINVAL
);
3956 e
= bus_label_escape(external_id
);
3960 ret
= path_join(prefix
, e
);
3968 _public_
int sd_bus_path_decode(const char *path
, const char *prefix
, char **external_id
) {
3972 assert_return(object_path_is_valid(path
), -EINVAL
);
3973 assert_return(object_path_is_valid(prefix
), -EINVAL
);
3974 assert_return(external_id
, -EINVAL
);
3976 e
= object_path_startswith(path
, prefix
);
3978 *external_id
= NULL
;
3982 /* Note that 'e' might be an empty string here. That's expected. E.g. a case where the subtree
3983 * corresponds to a subtree on a disk, and we want to return something that represents the root
3984 * of the filesystem. */
3986 ret
= bus_label_unescape(e
);
3994 _public_
int sd_bus_path_encode_many(char **out
, const char *path_template
, ...) {
3995 _cleanup_strv_free_
char **labels
= NULL
;
3996 char *path
, *path_pos
, **label_pos
;
3997 const char *sep
, *template_pos
;
4002 assert_return(out
, -EINVAL
);
4003 assert_return(path_template
, -EINVAL
);
4005 path_length
= strlen(path_template
);
4007 va_start(list
, path_template
);
4008 for (sep
= strchr(path_template
, '%'); sep
; sep
= strchr(sep
+ 1, '%')) {
4012 arg
= va_arg(list
, const char *);
4018 label
= bus_label_escape(arg
);
4024 r
= strv_consume(&labels
, label
);
4030 /* add label length, but account for the format character */
4031 path_length
+= strlen(label
) - 1;
4035 path
= malloc(path_length
+ 1);
4042 for (template_pos
= path_template
; *template_pos
; ) {
4043 sep
= strchrnul(template_pos
, '%');
4044 path_pos
= mempcpy(path_pos
, template_pos
, sep
- template_pos
);
4048 path_pos
= stpcpy(path_pos
, *label_pos
++);
4049 template_pos
= sep
+ 1;
4057 _public_
int sd_bus_path_decode_many(const char *path
, const char *path_template
, ...) {
4058 _cleanup_strv_free_
char **labels
= NULL
;
4059 const char *template_pos
, *path_pos
;
4065 * This decodes an object-path based on a template argument. The
4066 * template consists of a verbatim path, optionally including special
4069 * - Each occurrence of '%' in the template matches an arbitrary
4070 * substring of a label in the given path. At most one such
4071 * directive is allowed per label. For each such directive, the
4072 * caller must provide an output parameter (char **) via va_arg. If
4073 * NULL is passed, the given label is verified, but not returned.
4074 * For each matched label, the *decoded* label is stored in the
4075 * passed output argument, and the caller is responsible to free
4076 * it. Note that the output arguments are only modified if the
4077 * actually path matched the template. Otherwise, they're left
4080 * This function returns <0 on error, 0 if the path does not match the
4081 * template, 1 if it matched.
4084 assert_return(path
, -EINVAL
);
4085 assert_return(path_template
, -EINVAL
);
4089 for (template_pos
= path_template
; *template_pos
; ) {
4094 /* verify everything until the next '%' matches verbatim */
4095 sep
= strchrnul(template_pos
, '%');
4096 length
= sep
- template_pos
;
4097 if (strncmp(path_pos
, template_pos
, length
))
4101 template_pos
+= length
;
4106 /* We found the next '%' character. Everything up until here
4107 * matched. We now skip ahead to the end of this label and make
4108 * sure it matches the tail of the label in the path. Then we
4109 * decode the string in-between and save it for later use. */
4111 ++template_pos
; /* skip over '%' */
4113 sep
= strchrnul(template_pos
, '/');
4114 length
= sep
- template_pos
; /* length of suffix to match verbatim */
4116 /* verify the suffixes match */
4117 sep
= strchrnul(path_pos
, '/');
4118 if (sep
- path_pos
< (ssize_t
)length
||
4119 strncmp(sep
- length
, template_pos
, length
))
4122 template_pos
+= length
; /* skip over matched label */
4123 length
= sep
- path_pos
- length
; /* length of sub-label to decode */
4125 /* store unescaped label for later use */
4126 label
= bus_label_unescape_n(path_pos
, length
);
4130 r
= strv_consume(&labels
, label
);
4134 path_pos
= sep
; /* skip decoded label and suffix */
4137 /* end of template must match end of path */
4141 /* copy the labels over to the caller */
4142 va_start(list
, path_template
);
4143 for (label_pos
= labels
; label_pos
&& *label_pos
; ++label_pos
) {
4146 arg
= va_arg(list
, char **);
4154 labels
= mfree(labels
);
4158 _public_
int sd_bus_try_close(sd_bus
*bus
) {
4159 assert_return(bus
, -EINVAL
);
4160 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4161 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4166 _public_
int sd_bus_get_description(sd_bus
*bus
, const char **description
) {
4167 assert_return(bus
, -EINVAL
);
4168 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4169 assert_return(description
, -EINVAL
);
4170 assert_return(bus
->description
, -ENXIO
);
4171 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4173 if (bus
->description
)
4174 *description
= bus
->description
;
4175 else if (bus
->is_system
)
4176 *description
= "system";
4177 else if (bus
->is_user
)
4178 *description
= "user";
4180 *description
= NULL
;
4185 _public_
int sd_bus_get_scope(sd_bus
*bus
, const char **scope
) {
4186 assert_return(bus
, -EINVAL
);
4187 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4188 assert_return(scope
, -EINVAL
);
4189 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4196 if (bus
->is_system
) {
4204 _public_
int sd_bus_get_address(sd_bus
*bus
, const char **address
) {
4205 assert_return(bus
, -EINVAL
);
4206 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4207 assert_return(address
, -EINVAL
);
4208 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4211 *address
= bus
->address
;
4218 _public_
int sd_bus_get_creds_mask(sd_bus
*bus
, uint64_t *mask
) {
4219 assert_return(bus
, -EINVAL
);
4220 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4221 assert_return(mask
, -EINVAL
);
4222 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4224 *mask
= bus
->creds_mask
;
4228 _public_
int sd_bus_is_bus_client(sd_bus
*bus
) {
4229 assert_return(bus
, -EINVAL
);
4230 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4231 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4233 return bus
->bus_client
;
4236 _public_
int sd_bus_is_server(sd_bus
*bus
) {
4237 assert_return(bus
, -EINVAL
);
4238 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4239 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4241 return bus
->is_server
;
4244 _public_
int sd_bus_is_anonymous(sd_bus
*bus
) {
4245 assert_return(bus
, -EINVAL
);
4246 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4247 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4249 return bus
->anonymous_auth
;
4252 _public_
int sd_bus_is_trusted(sd_bus
*bus
) {
4253 assert_return(bus
, -EINVAL
);
4254 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4255 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4257 return bus
->trusted
;
4260 _public_
int sd_bus_is_monitor(sd_bus
*bus
) {
4261 assert_return(bus
, -EINVAL
);
4262 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4263 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4265 return bus
->is_monitor
;
4268 static void flush_close(sd_bus
*bus
) {
4272 /* Flushes and closes the specified bus. We take a ref before,
4273 * to ensure the flushing does not cause the bus to be
4276 sd_bus_flush_close_unref(sd_bus_ref(bus
));
4279 _public_
void sd_bus_default_flush_close(void) {
4280 flush_close(default_starter_bus
);
4281 flush_close(default_user_bus
);
4282 flush_close(default_system_bus
);
4285 _public_
int sd_bus_set_exit_on_disconnect(sd_bus
*bus
, int b
) {
4286 assert_return(bus
, -EINVAL
);
4287 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4289 /* Turns on exit-on-disconnect, and triggers it immediately if the bus connection was already
4290 * disconnected. Note that this is triggered exclusively on disconnections triggered by the server side, never
4291 * from the client side. */
4292 bus
->exit_on_disconnect
= b
;
4294 /* If the exit condition was triggered already, exit immediately. */
4295 return bus_exit_now(bus
);
4298 _public_
int sd_bus_get_exit_on_disconnect(sd_bus
*bus
) {
4299 assert_return(bus
, -EINVAL
);
4300 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4302 return bus
->exit_on_disconnect
;
4305 _public_
int sd_bus_set_sender(sd_bus
*bus
, const char *sender
) {
4306 assert_return(bus
, -EINVAL
);
4307 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4308 assert_return(!bus
->bus_client
, -EPERM
);
4309 assert_return(!sender
|| service_name_is_valid(sender
), -EINVAL
);
4311 return free_and_strdup(&bus
->patch_sender
, sender
);
4314 _public_
int sd_bus_get_sender(sd_bus
*bus
, const char **ret
) {
4315 assert_return(bus
, -EINVAL
);
4316 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4317 assert_return(ret
, -EINVAL
);
4319 if (!bus
->patch_sender
)
4322 *ret
= bus
->patch_sender
;
4326 _public_
int sd_bus_get_n_queued_read(sd_bus
*bus
, uint64_t *ret
) {
4327 assert_return(bus
, -EINVAL
);
4328 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4329 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4330 assert_return(ret
, -EINVAL
);
4332 *ret
= bus
->rqueue_size
;
4336 _public_
int sd_bus_get_n_queued_write(sd_bus
*bus
, uint64_t *ret
) {
4337 assert_return(bus
, -EINVAL
);
4338 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4339 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4340 assert_return(ret
, -EINVAL
);
4342 *ret
= bus
->wqueue_size
;
4346 _public_
int sd_bus_set_method_call_timeout(sd_bus
*bus
, uint64_t usec
) {
4347 assert_return(bus
, -EINVAL
);
4348 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4350 bus
->method_call_timeout
= usec
;
4354 _public_
int sd_bus_get_method_call_timeout(sd_bus
*bus
, uint64_t *ret
) {
4358 assert_return(bus
, -EINVAL
);
4359 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4360 assert_return(ret
, -EINVAL
);
4362 if (bus
->method_call_timeout
!= 0) {
4363 *ret
= bus
->method_call_timeout
;
4367 e
= secure_getenv("SYSTEMD_BUS_TIMEOUT");
4368 if (e
&& parse_sec(e
, &usec
) >= 0 && usec
!= 0) {
4369 /* Save the parsed value to avoid multiple parsing. To change the timeout value,
4370 * use sd_bus_set_method_call_timeout() instead of setenv(). */
4371 *ret
= bus
->method_call_timeout
= usec
;
4375 *ret
= bus
->method_call_timeout
= BUS_DEFAULT_TIMEOUT
;
4379 _public_
int sd_bus_set_close_on_exit(sd_bus
*bus
, int b
) {
4380 assert_return(bus
, -EINVAL
);
4381 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4383 bus
->close_on_exit
= b
;
4387 _public_
int sd_bus_get_close_on_exit(sd_bus
*bus
) {
4388 assert_return(bus
, -EINVAL
);
4389 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4391 return bus
->close_on_exit
;
4394 _public_
int sd_bus_enqueue_for_read(sd_bus
*bus
, sd_bus_message
*m
) {
4397 assert_return(bus
, -EINVAL
);
4398 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4399 assert_return(m
, -EINVAL
);
4400 assert_return(m
->sealed
, -EINVAL
);
4401 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4403 if (!BUS_IS_OPEN(bus
->state
))
4406 /* Re-enqueue a message for reading. This is primarily useful for PolicyKit-style authentication,
4407 * where we accept a message, then determine we need to interactively authenticate the user, and then
4408 * we want to process the message again. */
4410 r
= bus_rqueue_make_room(bus
);
4414 bus
->rqueue
[bus
->rqueue_size
++] = bus_message_ref_queued(m
, bus
);