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 " \
52 " cookie=%" PRIu64 " reply_cookie=%" PRIu64 \
53 " signature=%s error-name=%s error-message=%s", \
54 strna(bus_message_type_to_string(_mm->header->type)), \
55 strna(sd_bus_message_get_sender(_mm)), \
56 strna(sd_bus_message_get_destination(_mm)), \
57 strna(sd_bus_message_get_path(_mm)), \
58 strna(sd_bus_message_get_interface(_mm)), \
59 strna(sd_bus_message_get_member(_mm)), \
60 BUS_MESSAGE_COOKIE(_mm), \
62 strna(_mm->root_container.signature), \
63 strna(_mm->error.name), \
64 strna(_mm->error.message)); \
67 static int bus_poll(sd_bus
*bus
, bool need_more
, uint64_t timeout_usec
);
68 static void bus_detach_io_events(sd_bus
*b
);
70 static thread_local sd_bus
*default_system_bus
= NULL
;
71 static thread_local sd_bus
*default_user_bus
= NULL
;
72 static thread_local sd_bus
*default_starter_bus
= NULL
;
74 static sd_bus
**bus_choose_default(int (**bus_open
)(sd_bus
**)) {
77 /* Let's try our best to reuse another cached connection. If
78 * the starter bus type is set, connect via our normal
79 * connection logic, ignoring $DBUS_STARTER_ADDRESS, so that
80 * we can share the connection with the user/system default
83 e
= secure_getenv("DBUS_STARTER_BUS_TYPE");
85 if (streq(e
, "system")) {
87 *bus_open
= sd_bus_open_system
;
88 return &default_system_bus
;
89 } else if (STR_IN_SET(e
, "user", "session")) {
91 *bus_open
= sd_bus_open_user
;
92 return &default_user_bus
;
96 /* No type is specified, so we have not other option than to
97 * use the starter address if it is set. */
98 e
= secure_getenv("DBUS_STARTER_ADDRESS");
101 *bus_open
= sd_bus_open
;
102 return &default_starter_bus
;
105 /* Finally, if nothing is set use the cached connection for
108 if (cg_pid_get_owner_uid(0, NULL
) >= 0) {
110 *bus_open
= sd_bus_open_user
;
111 return &default_user_bus
;
114 *bus_open
= sd_bus_open_system
;
115 return &default_system_bus
;
119 sd_bus
*bus_resolve(sd_bus
*bus
) {
120 switch ((uintptr_t) bus
) {
121 case (uintptr_t) SD_BUS_DEFAULT
:
122 return *(bus_choose_default(NULL
));
123 case (uintptr_t) SD_BUS_DEFAULT_USER
:
124 return default_user_bus
;
125 case (uintptr_t) SD_BUS_DEFAULT_SYSTEM
:
126 return default_system_bus
;
132 void bus_close_io_fds(sd_bus
*b
) {
135 bus_detach_io_events(b
);
137 if (b
->input_fd
!= b
->output_fd
)
138 safe_close(b
->output_fd
);
139 b
->output_fd
= b
->input_fd
= safe_close(b
->input_fd
);
142 void bus_close_inotify_fd(sd_bus
*b
) {
145 b
->inotify_event_source
= sd_event_source_disable_unref(b
->inotify_event_source
);
147 b
->inotify_fd
= safe_close(b
->inotify_fd
);
148 b
->inotify_watches
= mfree(b
->inotify_watches
);
149 b
->n_inotify_watches
= 0;
152 static void bus_reset_queues(sd_bus
*b
) {
155 while (b
->rqueue_size
> 0)
156 bus_message_unref_queued(b
->rqueue
[--b
->rqueue_size
], b
);
158 b
->rqueue
= mfree(b
->rqueue
);
160 while (b
->wqueue_size
> 0)
161 bus_message_unref_queued(b
->wqueue
[--b
->wqueue_size
], b
);
163 b
->wqueue
= mfree(b
->wqueue
);
166 static sd_bus
* bus_free(sd_bus
*b
) {
170 assert(!b
->track_queue
);
173 b
->state
= BUS_CLOSED
;
175 sd_bus_detach_event(b
);
177 while ((s
= b
->slots
)) {
178 /* At this point only floating slots can still be
179 * around, because the non-floating ones keep a
180 * reference to the bus, and we thus couldn't be
181 * destructing right now... We forcibly disconnect the
182 * slots here, so that they still can be referenced by
183 * apps, but are dead. */
186 bus_slot_disconnect(s
, true);
189 if (b
->default_bus_ptr
)
190 *b
->default_bus_ptr
= NULL
;
193 bus_close_inotify_fd(b
);
198 free(b
->unique_name
);
199 free(b
->auth_buffer
);
202 free(b
->description
);
203 free(b
->patch_sender
);
206 strv_free(b
->exec_argv
);
208 close_many(b
->fds
, b
->n_fds
);
213 ordered_hashmap_free_free(b
->reply_callbacks
);
214 prioq_free(b
->reply_callbacks_prioq
);
216 assert(b
->match_callbacks
.type
== BUS_MATCH_ROOT
);
217 bus_match_free(&b
->match_callbacks
);
219 hashmap_free_free(b
->vtable_methods
);
220 hashmap_free_free(b
->vtable_properties
);
222 assert(hashmap_isempty(b
->nodes
));
223 hashmap_free(b
->nodes
);
227 assert_se(pthread_mutex_destroy(&b
->memfd_cache_mutex
) == 0);
232 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus
*, bus_free
);
234 _public_
int sd_bus_new(sd_bus
**ret
) {
235 _cleanup_free_ sd_bus
*b
= NULL
;
237 assert_return(ret
, -EINVAL
);
247 .inotify_fd
= -EBADF
,
248 .message_version
= 1,
249 .creds_mask
= SD_BUS_CREDS_WELL_KNOWN_NAMES
|SD_BUS_CREDS_UNIQUE_NAME
,
251 .original_pid
= getpid_cached(),
252 .n_groups
= SIZE_MAX
,
253 .close_on_exit
= true,
254 .ucred
= UCRED_INVALID
,
257 /* We guarantee that wqueue always has space for at least one entry */
258 if (!GREEDY_REALLOC(b
->wqueue
, 1))
261 assert_se(pthread_mutex_init(&b
->memfd_cache_mutex
, NULL
) == 0);
267 _public_
int sd_bus_set_address(sd_bus
*bus
, const char *address
) {
268 assert_return(bus
, -EINVAL
);
269 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
270 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
271 assert_return(address
, -EINVAL
);
272 assert_return(!bus_pid_changed(bus
), -ECHILD
);
274 return free_and_strdup(&bus
->address
, address
);
277 _public_
int sd_bus_set_fd(sd_bus
*bus
, int input_fd
, int output_fd
) {
278 assert_return(bus
, -EINVAL
);
279 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
280 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
281 assert_return(input_fd
>= 0, -EBADF
);
282 assert_return(output_fd
>= 0, -EBADF
);
283 assert_return(!bus_pid_changed(bus
), -ECHILD
);
285 bus
->input_fd
= input_fd
;
286 bus
->output_fd
= output_fd
;
290 _public_
int sd_bus_set_exec(sd_bus
*bus
, const char *path
, char *const *argv
) {
291 _cleanup_strv_free_
char **a
= NULL
;
294 assert_return(bus
, -EINVAL
);
295 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
296 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
297 assert_return(path
, -EINVAL
);
298 assert_return(!strv_isempty(argv
), -EINVAL
);
299 assert_return(!bus_pid_changed(bus
), -ECHILD
);
305 r
= free_and_strdup(&bus
->exec_path
, path
);
309 return strv_free_and_replace(bus
->exec_argv
, a
);
312 _public_
int sd_bus_set_bus_client(sd_bus
*bus
, int b
) {
313 assert_return(bus
, -EINVAL
);
314 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
315 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
316 assert_return(!bus
->patch_sender
, -EPERM
);
317 assert_return(!bus_pid_changed(bus
), -ECHILD
);
319 bus
->bus_client
= !!b
;
323 _public_
int sd_bus_set_monitor(sd_bus
*bus
, int b
) {
324 assert_return(bus
, -EINVAL
);
325 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
326 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
327 assert_return(!bus_pid_changed(bus
), -ECHILD
);
329 bus
->is_monitor
= !!b
;
333 _public_
int sd_bus_negotiate_fds(sd_bus
*bus
, int b
) {
334 assert_return(bus
, -EINVAL
);
335 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
336 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
337 assert_return(!bus_pid_changed(bus
), -ECHILD
);
339 bus
->accept_fd
= !!b
;
343 _public_
int sd_bus_negotiate_timestamp(sd_bus
*bus
, int b
) {
344 assert_return(bus
, -EINVAL
);
345 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
346 assert_return(!IN_SET(bus
->state
, BUS_CLOSING
, BUS_CLOSED
), -EPERM
);
347 assert_return(!bus_pid_changed(bus
), -ECHILD
);
349 /* This is not actually supported by any of our transports these days, but we do honour it for synthetic
350 * replies, and maybe one day classic D-Bus learns this too */
351 bus
->attach_timestamp
= !!b
;
356 _public_
int sd_bus_negotiate_creds(sd_bus
*bus
, int b
, uint64_t mask
) {
357 assert_return(bus
, -EINVAL
);
358 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
359 assert_return(mask
<= _SD_BUS_CREDS_ALL
, -EINVAL
);
360 assert_return(!IN_SET(bus
->state
, BUS_CLOSING
, BUS_CLOSED
), -EPERM
);
361 assert_return(!bus_pid_changed(bus
), -ECHILD
);
363 SET_FLAG(bus
->creds_mask
, mask
, b
);
365 /* The well knowns we need unconditionally, so that matches can work */
366 bus
->creds_mask
|= SD_BUS_CREDS_WELL_KNOWN_NAMES
|SD_BUS_CREDS_UNIQUE_NAME
;
371 _public_
int sd_bus_set_server(sd_bus
*bus
, int b
, sd_id128_t server_id
) {
372 assert_return(bus
, -EINVAL
);
373 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
374 assert_return(b
|| sd_id128_equal(server_id
, SD_ID128_NULL
), -EINVAL
);
375 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
376 assert_return(!bus_pid_changed(bus
), -ECHILD
);
378 bus
->is_server
= !!b
;
379 bus
->server_id
= server_id
;
383 _public_
int sd_bus_set_anonymous(sd_bus
*bus
, int b
) {
384 assert_return(bus
, -EINVAL
);
385 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
386 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
387 assert_return(!bus_pid_changed(bus
), -ECHILD
);
389 bus
->anonymous_auth
= !!b
;
393 _public_
int sd_bus_set_trusted(sd_bus
*bus
, int b
) {
394 assert_return(bus
, -EINVAL
);
395 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
396 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
397 assert_return(!bus_pid_changed(bus
), -ECHILD
);
403 _public_
int sd_bus_set_description(sd_bus
*bus
, const char *description
) {
404 assert_return(bus
, -EINVAL
);
405 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
406 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
407 assert_return(!bus_pid_changed(bus
), -ECHILD
);
409 return free_and_strdup(&bus
->description
, description
);
412 _public_
int sd_bus_set_allow_interactive_authorization(sd_bus
*bus
, int b
) {
413 assert_return(bus
, -EINVAL
);
414 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
415 assert_return(!bus_pid_changed(bus
), -ECHILD
);
417 bus
->allow_interactive_authorization
= !!b
;
421 _public_
int sd_bus_get_allow_interactive_authorization(sd_bus
*bus
) {
422 assert_return(bus
, -EINVAL
);
423 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
424 assert_return(!bus_pid_changed(bus
), -ECHILD
);
426 return bus
->allow_interactive_authorization
;
429 _public_
int sd_bus_set_watch_bind(sd_bus
*bus
, int b
) {
430 assert_return(bus
, -EINVAL
);
431 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
432 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
433 assert_return(!bus_pid_changed(bus
), -ECHILD
);
435 bus
->watch_bind
= !!b
;
439 _public_
int sd_bus_get_watch_bind(sd_bus
*bus
) {
440 assert_return(bus
, -EINVAL
);
441 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
442 assert_return(!bus_pid_changed(bus
), -ECHILD
);
444 return bus
->watch_bind
;
447 _public_
int sd_bus_set_connected_signal(sd_bus
*bus
, int b
) {
448 assert_return(bus
, -EINVAL
);
449 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
450 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
451 assert_return(!bus_pid_changed(bus
), -ECHILD
);
453 bus
->connected_signal
= !!b
;
457 _public_
int sd_bus_get_connected_signal(sd_bus
*bus
) {
458 assert_return(bus
, -EINVAL
);
459 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
460 assert_return(!bus_pid_changed(bus
), -ECHILD
);
462 return bus
->connected_signal
;
465 static int synthesize_connected_signal(sd_bus
*bus
) {
466 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
471 /* If enabled, synthesizes a local "Connected" signal mirroring the local "Disconnected" signal. This is called
472 * whenever we fully established a connection, i.e. after the authorization phase, and after receiving the
473 * Hello() reply. Or in other words, whenever we enter BUS_RUNNING state.
475 * This is useful so that clients can start doing stuff whenever the connection is fully established in a way
476 * that works independently from whether we connected to a full bus or just a direct connection. */
478 if (!bus
->connected_signal
)
481 r
= sd_bus_message_new_signal(
484 "/org/freedesktop/DBus/Local",
485 "org.freedesktop.DBus.Local",
490 bus_message_set_sender_local(bus
, m
);
491 m
->read_counter
= ++bus
->read_counter
;
493 r
= bus_seal_synthetic_message(bus
, m
);
497 r
= bus_rqueue_make_room(bus
);
501 /* Insert at the very front */
502 memmove(bus
->rqueue
+ 1, bus
->rqueue
, sizeof(sd_bus_message
*) * bus
->rqueue_size
);
503 bus
->rqueue
[0] = bus_message_ref_queued(m
, bus
);
509 void bus_set_state(sd_bus
*bus
, enum bus_state state
) {
510 static const char* const table
[_BUS_STATE_MAX
] = {
511 [BUS_UNSET
] = "UNSET",
512 [BUS_WATCH_BIND
] = "WATCH_BIND",
513 [BUS_OPENING
] = "OPENING",
514 [BUS_AUTHENTICATING
] = "AUTHENTICATING",
515 [BUS_HELLO
] = "HELLO",
516 [BUS_RUNNING
] = "RUNNING",
517 [BUS_CLOSING
] = "CLOSING",
518 [BUS_CLOSED
] = "CLOSED",
522 assert(state
< _BUS_STATE_MAX
);
524 if (state
== bus
->state
)
527 log_debug("Bus %s: changing state %s %s %s", strna(bus
->description
),
528 table
[bus
->state
], special_glyph(SPECIAL_GLYPH_ARROW_RIGHT
), table
[state
]);
532 static int hello_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
540 assert(IN_SET(bus
->state
, BUS_HELLO
, BUS_CLOSING
));
542 r
= sd_bus_message_get_errno(reply
);
548 r
= sd_bus_message_read(reply
, "s", &s
);
552 if (!service_name_is_valid(s
) || s
[0] != ':') {
557 r
= free_and_strdup(&bus
->unique_name
, s
);
561 if (bus
->state
== BUS_HELLO
) {
562 bus_set_state(bus
, BUS_RUNNING
);
564 r
= synthesize_connected_signal(bus
);
572 /* When Hello() failed, let's propagate this in two ways: first we return the error immediately here,
573 * which is the propagated up towards the event loop. Let's also invalidate the connection, so that
574 * if the user then calls back into us again we won't wait any longer. */
576 bus_set_state(bus
, BUS_CLOSING
);
580 static int bus_send_hello(sd_bus
*bus
) {
581 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
586 if (!bus
->bus_client
)
589 r
= sd_bus_message_new_method_call(
592 "org.freedesktop.DBus",
593 "/org/freedesktop/DBus",
594 "org.freedesktop.DBus",
599 return sd_bus_call_async(bus
, NULL
, m
, hello_callback
, NULL
, 0);
602 int bus_start_running(sd_bus
*bus
) {
603 struct reply_callback
*c
;
608 assert(bus
->state
< BUS_HELLO
);
610 /* We start all method call timeouts when we enter BUS_HELLO or BUS_RUNNING mode. At this point let's convert
611 * all relative to absolute timestamps. Note that we do not reshuffle the reply callback priority queue since
612 * adding a fixed value to all entries should not alter the internal order. */
614 n
= now(CLOCK_MONOTONIC
);
615 ORDERED_HASHMAP_FOREACH(c
, bus
->reply_callbacks
) {
616 if (c
->timeout_usec
== 0)
619 c
->timeout_usec
= usec_add(n
, c
->timeout_usec
);
622 if (bus
->bus_client
) {
623 bus_set_state(bus
, BUS_HELLO
);
627 bus_set_state(bus
, BUS_RUNNING
);
629 r
= synthesize_connected_signal(bus
);
636 static int parse_address_key(const char **p
, const char *key
, char **value
) {
637 _cleanup_free_
char *r
= NULL
;
647 if (strncmp(*p
, key
, l
) != 0)
660 while (!IN_SET(*a
, ';', ',', 0)) {
674 c
= (char) ((x
<< 4) | y
);
681 if (!GREEDY_REALLOC(r
, n
+ 2))
699 free_and_replace(*value
, r
);
704 static void skip_address_key(const char **p
) {
708 *p
+= strcspn(*p
, ",");
714 static int parse_unix_address(sd_bus
*b
, const char **p
, char **guid
) {
715 _cleanup_free_
char *path
= NULL
, *abstract
= NULL
;
724 while (!IN_SET(**p
, 0, ';')) {
725 r
= parse_address_key(p
, "guid", guid
);
731 r
= parse_address_key(p
, "path", &path
);
737 r
= parse_address_key(p
, "abstract", &abstract
);
746 if (!path
&& !abstract
)
749 if (path
&& abstract
)
754 if (l
>= sizeof(b
->sockaddr
.un
.sun_path
)) /* We insist on NUL termination */
757 b
->sockaddr
.un
= (struct sockaddr_un
) {
758 .sun_family
= AF_UNIX
,
761 memcpy(b
->sockaddr
.un
.sun_path
, path
, l
);
762 b
->sockaddr_size
= offsetof(struct sockaddr_un
, sun_path
) + l
+ 1;
767 l
= strlen(abstract
);
768 if (l
>= sizeof(b
->sockaddr
.un
.sun_path
) - 1) /* We insist on NUL termination */
771 b
->sockaddr
.un
= (struct sockaddr_un
) {
772 .sun_family
= AF_UNIX
,
775 memcpy(b
->sockaddr
.un
.sun_path
+1, abstract
, l
);
776 b
->sockaddr_size
= offsetof(struct sockaddr_un
, sun_path
) + 1 + l
;
784 static int parse_tcp_address(sd_bus
*b
, const char **p
, char **guid
) {
785 _cleanup_free_
char *host
= NULL
, *port
= NULL
, *family
= NULL
;
787 struct addrinfo
*result
, hints
= {
788 .ai_socktype
= SOCK_STREAM
,
796 while (!IN_SET(**p
, 0, ';')) {
797 r
= parse_address_key(p
, "guid", guid
);
803 r
= parse_address_key(p
, "host", &host
);
809 r
= parse_address_key(p
, "port", &port
);
815 r
= parse_address_key(p
, "family", &family
);
828 hints
.ai_family
= af_from_ipv4_ipv6(family
);
829 if (hints
.ai_family
== AF_UNSPEC
)
833 r
= getaddrinfo(host
, port
, &hints
, &result
);
837 return -EADDRNOTAVAIL
;
839 memcpy(&b
->sockaddr
, result
->ai_addr
, result
->ai_addrlen
);
840 b
->sockaddr_size
= result
->ai_addrlen
;
842 freeaddrinfo(result
);
849 static int parse_exec_address(sd_bus
*b
, const char **p
, char **guid
) {
851 unsigned n_argv
= 0, j
;
860 while (!IN_SET(**p
, 0, ';')) {
861 r
= parse_address_key(p
, "guid", guid
);
867 r
= parse_address_key(p
, "path", &path
);
873 if (startswith(*p
, "argv")) {
877 ul
= strtoul(*p
+ 4, (char**) p
, 10);
878 if (errno
> 0 || **p
!= '=' || ul
> 256) {
886 if (!GREEDY_REALLOC0(argv
, ul
+ 2)) {
894 r
= parse_address_key(p
, NULL
, argv
+ ul
);
909 /* Make sure there are no holes in the array, with the
910 * exception of argv[0] */
911 for (j
= 1; j
< n_argv
; j
++)
917 if (argv
&& argv
[0] == NULL
) {
918 argv
[0] = strdup(path
);
933 for (j
= 0; j
< n_argv
; j
++)
941 static int parse_container_unix_address(sd_bus
*b
, const char **p
, char **guid
) {
942 _cleanup_free_
char *machine
= NULL
, *pid
= NULL
;
950 while (!IN_SET(**p
, 0, ';')) {
951 r
= parse_address_key(p
, "guid", guid
);
957 r
= parse_address_key(p
, "machine", &machine
);
963 r
= parse_address_key(p
, "pid", &pid
);
972 if (!machine
== !pid
)
976 if (!hostname_is_valid(machine
, VALID_HOSTNAME_DOT_HOST
))
979 free_and_replace(b
->machine
, machine
);
981 b
->machine
= mfree(b
->machine
);
984 r
= parse_pid(pid
, &b
->nspid
);
990 b
->sockaddr
.un
= (struct sockaddr_un
) {
991 .sun_family
= AF_UNIX
,
992 /* Note that we use the old /var/run prefix here, to increase compatibility with really old containers */
993 .sun_path
= "/var/run/dbus/system_bus_socket",
995 b
->sockaddr_size
= SOCKADDR_UN_LEN(b
->sockaddr
.un
);
1001 static void bus_reset_parsed_address(sd_bus
*b
) {
1005 b
->sockaddr_size
= 0;
1006 b
->exec_argv
= strv_free(b
->exec_argv
);
1007 b
->exec_path
= mfree(b
->exec_path
);
1008 b
->server_id
= SD_ID128_NULL
;
1009 b
->machine
= mfree(b
->machine
);
1013 static int bus_parse_next_address(sd_bus
*b
) {
1014 _cleanup_free_
char *guid
= NULL
;
1022 if (b
->address
[b
->address_index
] == 0)
1025 bus_reset_parsed_address(b
);
1027 a
= b
->address
+ b
->address_index
;
1036 if (startswith(a
, "unix:")) {
1039 r
= parse_unix_address(b
, &a
, &guid
);
1044 } else if (startswith(a
, "tcp:")) {
1047 r
= parse_tcp_address(b
, &a
, &guid
);
1053 } else if (startswith(a
, "unixexec:")) {
1056 r
= parse_exec_address(b
, &a
, &guid
);
1062 } else if (startswith(a
, "x-machine-unix:")) {
1065 r
= parse_container_unix_address(b
, &a
, &guid
);
1078 r
= sd_id128_from_string(guid
, &b
->server_id
);
1083 b
->address_index
= a
- b
->address
;
1087 static void bus_kill_exec(sd_bus
*bus
) {
1088 if (!pid_is_valid(bus
->busexec_pid
))
1091 sigterm_wait(TAKE_PID(bus
->busexec_pid
));
1094 static int bus_start_address(sd_bus
*b
) {
1100 bus_close_io_fds(b
);
1101 bus_close_inotify_fd(b
);
1105 /* If you provide multiple different bus-addresses, we
1106 * try all of them in order and use the first one that
1110 r
= bus_socket_exec(b
);
1111 else if ((b
->nspid
> 0 || b
->machine
) && b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
)
1112 r
= bus_container_connect_socket(b
);
1113 else if (b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
)
1114 r
= bus_socket_connect(b
);
1121 q
= bus_attach_io_events(b
);
1125 q
= bus_attach_inotify_event(b
);
1132 b
->last_connect_error
= -r
;
1135 r
= bus_parse_next_address(b
);
1139 return b
->last_connect_error
> 0 ? -b
->last_connect_error
: -ECONNREFUSED
;
1143 int bus_next_address(sd_bus
*b
) {
1146 bus_reset_parsed_address(b
);
1147 return bus_start_address(b
);
1150 static int bus_start_fd(sd_bus
*b
) {
1155 assert(b
->input_fd
>= 0);
1156 assert(b
->output_fd
>= 0);
1158 if (DEBUG_LOGGING
) {
1159 _cleanup_free_
char *pi
= NULL
, *po
= NULL
;
1160 (void) fd_get_path(b
->input_fd
, &pi
);
1161 (void) fd_get_path(b
->output_fd
, &po
);
1162 log_debug("sd-bus: starting bus%s%s on fds %d/%d (%s, %s)...",
1163 b
->description
? " " : "", strempty(b
->description
),
1164 b
->input_fd
, b
->output_fd
,
1165 pi
?: "???", po
?: "???");
1168 r
= fd_nonblock(b
->input_fd
, true);
1172 r
= fd_cloexec(b
->input_fd
, true);
1176 if (b
->input_fd
!= b
->output_fd
) {
1177 r
= fd_nonblock(b
->output_fd
, true);
1181 r
= fd_cloexec(b
->output_fd
, true);
1186 if (fstat(b
->input_fd
, &st
) < 0)
1189 return bus_socket_take_fd(b
);
1192 _public_
int sd_bus_start(sd_bus
*bus
) {
1195 assert_return(bus
, -EINVAL
);
1196 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1197 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
1198 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1200 bus_set_state(bus
, BUS_OPENING
);
1202 if (bus
->is_server
&& bus
->bus_client
)
1205 if (bus
->input_fd
>= 0)
1206 r
= bus_start_fd(bus
);
1207 else if (bus
->address
|| bus
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
|| bus
->exec_path
|| bus
->machine
)
1208 r
= bus_start_address(bus
);
1217 return bus_send_hello(bus
);
1220 _public_
int sd_bus_open_with_description(sd_bus
**ret
, const char *description
) {
1222 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1225 assert_return(ret
, -EINVAL
);
1227 /* Let's connect to the starter bus if it is set, and
1228 * otherwise to the bus that is appropriate for the scope
1229 * we are running in */
1231 e
= secure_getenv("DBUS_STARTER_BUS_TYPE");
1233 if (streq(e
, "system"))
1234 return sd_bus_open_system_with_description(ret
, description
);
1235 else if (STR_IN_SET(e
, "session", "user"))
1236 return sd_bus_open_user_with_description(ret
, description
);
1239 e
= secure_getenv("DBUS_STARTER_ADDRESS");
1241 if (cg_pid_get_owner_uid(0, NULL
) >= 0)
1242 return sd_bus_open_user_with_description(ret
, description
);
1244 return sd_bus_open_system_with_description(ret
, description
);
1251 r
= sd_bus_set_address(b
, e
);
1255 b
->bus_client
= true;
1257 /* We don't know whether the bus is trusted or not, so better
1258 * be safe, and authenticate everything */
1260 b
->is_local
= false;
1261 b
->creds_mask
|= SD_BUS_CREDS_UID
| SD_BUS_CREDS_EUID
| SD_BUS_CREDS_EFFECTIVE_CAPS
;
1263 r
= sd_bus_start(b
);
1271 _public_
int sd_bus_open(sd_bus
**ret
) {
1272 return sd_bus_open_with_description(ret
, NULL
);
1275 int bus_set_address_system(sd_bus
*b
) {
1281 e
= secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1283 r
= sd_bus_set_address(b
, e
?: DEFAULT_SYSTEM_BUS_ADDRESS
);
1285 b
->is_system
= true;
1289 _public_
int sd_bus_open_system_with_description(sd_bus
**ret
, const char *description
) {
1290 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1293 assert_return(ret
, -EINVAL
);
1300 r
= sd_bus_set_description(b
, description
);
1305 r
= bus_set_address_system(b
);
1309 b
->bus_client
= true;
1311 /* Let's do per-method access control on the system bus. We
1312 * need the caller's UID and capability set for that. */
1314 b
->creds_mask
|= SD_BUS_CREDS_UID
| SD_BUS_CREDS_EUID
| SD_BUS_CREDS_EFFECTIVE_CAPS
;
1317 r
= sd_bus_start(b
);
1325 _public_
int sd_bus_open_system(sd_bus
**ret
) {
1326 return sd_bus_open_system_with_description(ret
, NULL
);
1329 int bus_set_address_user(sd_bus
*b
) {
1331 _cleanup_free_
char *_a
= NULL
;
1336 a
= secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1339 _cleanup_free_
char *ee
= NULL
;
1341 e
= secure_getenv("XDG_RUNTIME_DIR");
1343 return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM
),
1344 "sd-bus: $XDG_RUNTIME_DIR not set, cannot connect to user bus.");
1346 ee
= bus_address_escape(e
);
1350 if (asprintf(&_a
, DEFAULT_USER_BUS_ADDRESS_FMT
, ee
) < 0)
1355 r
= sd_bus_set_address(b
, a
);
1361 _public_
int sd_bus_open_user_with_description(sd_bus
**ret
, const char *description
) {
1362 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1365 assert_return(ret
, -EINVAL
);
1372 r
= sd_bus_set_description(b
, description
);
1377 r
= bus_set_address_user(b
);
1381 b
->bus_client
= true;
1383 /* We don't do any per-method access control on the user bus. */
1387 r
= sd_bus_start(b
);
1395 _public_
int sd_bus_open_user(sd_bus
**ret
) {
1396 return sd_bus_open_user_with_description(ret
, NULL
);
1399 int bus_set_address_system_remote(sd_bus
*b
, const char *host
) {
1400 _cleanup_free_
char *e
= NULL
;
1401 char *m
= NULL
, *c
= NULL
, *a
, *rbracket
= NULL
, *p
= NULL
;
1406 /* Skip ":"s in ipv6 addresses */
1410 rbracket
= strchr(host
, ']');
1413 t
= strndupa_safe(host
+ 1, rbracket
- host
- 1);
1414 e
= bus_address_escape(t
);
1417 } else if ((a
= strchr(host
, '@'))) {
1418 if (*(a
+ 1) == '[') {
1419 _cleanup_free_
char *t
= NULL
;
1421 rbracket
= strchr(a
+ 1, ']');
1424 t
= new0(char, strlen(host
));
1427 strncat(t
, host
, a
- host
+ 1);
1428 strncat(t
, a
+ 2, rbracket
- a
- 2);
1429 e
= bus_address_escape(t
);
1432 } else if (*(a
+ 1) == '\0' || strchr(a
+ 1, '@'))
1436 /* Let's see if a port was given */
1437 m
= strchr(rbracket
? rbracket
+ 1 : host
, ':');
1440 bool got_forward_slash
= false;
1446 p
= strndupa_safe(p
, t
- p
);
1447 got_forward_slash
= true;
1450 if (!in_charset(p
, "0123456789") || *p
== '\0') {
1451 if (!hostname_is_valid(p
, 0) || got_forward_slash
)
1455 goto interpret_port_as_machine_old_syntax
;
1459 /* Let's see if a machine was given */
1460 m
= strchr(rbracket
? rbracket
+ 1 : host
, '/');
1463 interpret_port_as_machine_old_syntax
:
1464 /* Let's make sure this is not a port of some kind,
1465 * and is a valid machine name. */
1466 if (!in_charset(m
, "0123456789") && hostname_is_valid(m
, 0))
1467 c
= strjoina(",argv", p
? "7" : "5", "=--machine=", m
);
1473 t
= strndupa_safe(host
, strcspn(host
, ":/"));
1475 e
= bus_address_escape(t
);
1480 a
= strjoin("unixexec:path=ssh,argv1=-xT", p
? ",argv2=-p,argv3=" : "", strempty(p
),
1481 ",argv", p
? "4" : "2", "=--,argv", p
? "5" : "3", "=", e
,
1482 ",argv", p
? "6" : "4", "=systemd-stdio-bridge", c
);
1486 return free_and_replace(b
->address
, a
);
1489 _public_
int sd_bus_open_system_remote(sd_bus
**ret
, const char *host
) {
1490 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1493 assert_return(host
, -EINVAL
);
1494 assert_return(ret
, -EINVAL
);
1500 r
= bus_set_address_system_remote(b
, host
);
1504 b
->bus_client
= true;
1506 b
->is_system
= true;
1507 b
->is_local
= false;
1509 r
= sd_bus_start(b
);
1517 int bus_set_address_machine(sd_bus
*b
, bool user
, const char *machine
) {
1518 _cleanup_free_
char *a
= NULL
;
1524 rhs
= strchr(machine
, '@');
1526 _cleanup_free_
char *u
= NULL
, *eu
= NULL
, *erhs
= NULL
;
1528 /* If there's an "@" in the container specification, we'll connect as a user specified at its
1529 * left hand side, which is useful in combination with user=true. This isn't as trivial as it
1530 * might sound: it's not sufficient to enter the container and connect to some socket there,
1531 * since the --user socket path depends on $XDG_RUNTIME_DIR which is set via PAM. Thus, to be
1532 * able to connect, we need to have a PAM session. Our way out? We use systemd-run to get
1533 * into the container and acquire a PAM session there, and then invoke systemd-stdio-bridge
1534 * in it, which propagates the bus transport to us. */
1538 u
= strndup(machine
, rhs
- machine
);
1540 u
= getusername_malloc(); /* Empty user name, let's use the local one */
1544 eu
= bus_address_escape(u
);
1550 /* No "@" specified but we shall connect to the user instance? Then assume root (and
1551 * not a user named identically to the calling one). This means:
1553 * --machine=foobar --user → connect to user bus of root user in container "foobar"
1554 * --machine=@foobar --user → connect to user bus of user named like the calling user in container "foobar"
1556 * Why? so that behaviour for "--machine=foobar --system" is roughly similar to
1557 * "--machine=foobar --user": both times we unconditionally connect as root user
1558 * regardless what the calling user is. */
1563 if (!isempty(rhs
)) {
1564 erhs
= bus_address_escape(rhs
);
1569 /* systemd-run -M… -PGq --wait -pUser=… -pPAMName=login systemd-stdio-bridge */
1571 a
= strjoin("unixexec:path=systemd-run,"
1572 "argv1=-M", erhs
?: ".host", ","
1575 "argv4=-pUser%3d", eu
?: "root", ",",
1576 "argv5=-pPAMName%3dlogin,"
1577 "argv6=systemd-stdio-bridge");
1582 /* Ideally we'd use the "--user" switch to systemd-stdio-bridge here, but it's only
1583 * available in recent systemd versions. Using the "-p" switch with the explicit path
1584 * is a working alternative, and is compatible with older versions, hence that's what
1586 if (!strextend(&a
, ",argv7=-punix:path%3d%24%7bXDG_RUNTIME_DIR%7d/bus"))
1590 _cleanup_free_
char *e
= NULL
;
1592 /* Just a container name, we can go the simple way, and just join the container, and connect
1593 * to the well-known path of the system bus there. */
1595 e
= bus_address_escape(machine
);
1599 a
= strjoin("x-machine-unix:machine=", e
);
1604 return free_and_replace(b
->address
, a
);
1607 static int user_and_machine_valid(const char *user_and_machine
) {
1610 /* Checks if a container specification in the form "user@container" or just "container" is valid.
1612 * If the "@" syntax is used we'll allow either the "user" or the "container" part to be omitted, but
1615 h
= strchr(user_and_machine
, '@');
1617 h
= user_and_machine
;
1619 _cleanup_free_
char *user
= NULL
;
1621 user
= strndup(user_and_machine
, h
- user_and_machine
);
1625 if (!isempty(user
) && !valid_user_group_name(user
, VALID_USER_RELAX
| VALID_USER_ALLOW_NUMERIC
))
1631 return !isempty(user
);
1634 return hostname_is_valid(h
, VALID_HOSTNAME_DOT_HOST
);
1637 static int user_and_machine_equivalent(const char *user_and_machine
) {
1638 _cleanup_free_
char *un
= NULL
;
1641 /* Returns true if the specified user+machine name are actually equivalent to our own identity and
1642 * our own host. If so we can shortcut things. Why bother? Because that way we don't have to fork
1643 * off short-lived worker processes that are then unavailable for authentication and logging in the
1644 * peer. Moreover joining a namespace requires privileges. If we are in the right namespace anyway,
1645 * we can avoid permission problems thus. */
1647 assert(user_and_machine
);
1649 /* Omitting the user name means that we shall use the same user name as we run as locally, which
1650 * means we'll end up on the same host, let's shortcut */
1651 if (streq(user_and_machine
, "@.host"))
1654 /* Otherwise, if we are root, then we can also allow the ".host" syntax, as that's the user this
1655 * would connect to. */
1656 uid_t uid
= geteuid();
1658 if (uid
== 0 && STR_IN_SET(user_and_machine
, ".host", "root@.host", "0@.host"))
1661 /* Otherwise, we have to figure out our user id and name, and compare things with that. */
1662 char buf
[DECIMAL_STR_MAX(uid_t
)];
1663 xsprintf(buf
, UID_FMT
, uid
);
1665 f
= startswith(user_and_machine
, buf
);
1667 un
= getusername_malloc();
1671 f
= startswith(user_and_machine
, un
);
1676 return STR_IN_SET(f
, "@", "@.host");
1679 _public_
int sd_bus_open_system_machine(sd_bus
**ret
, const char *user_and_machine
) {
1680 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1683 assert_return(user_and_machine
, -EINVAL
);
1684 assert_return(ret
, -EINVAL
);
1686 if (user_and_machine_equivalent(user_and_machine
))
1687 return sd_bus_open_system(ret
);
1689 r
= user_and_machine_valid(user_and_machine
);
1693 assert_return(r
> 0, -EINVAL
);
1699 r
= bus_set_address_machine(b
, false, user_and_machine
);
1703 b
->bus_client
= true;
1704 b
->is_system
= true;
1706 r
= sd_bus_start(b
);
1714 _public_
int sd_bus_open_user_machine(sd_bus
**ret
, const char *user_and_machine
) {
1715 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1718 assert_return(user_and_machine
, -EINVAL
);
1719 assert_return(ret
, -EINVAL
);
1721 /* Shortcut things if we'd end up on this host and as the same user. */
1722 if (user_and_machine_equivalent(user_and_machine
))
1723 return sd_bus_open_user(ret
);
1725 r
= user_and_machine_valid(user_and_machine
);
1729 assert_return(r
> 0, -EINVAL
);
1735 r
= bus_set_address_machine(b
, true, user_and_machine
);
1739 b
->bus_client
= true;
1742 r
= sd_bus_start(b
);
1750 _public_
void sd_bus_close(sd_bus
*bus
) {
1753 if (bus
->state
== BUS_CLOSED
)
1755 if (bus_pid_changed(bus
))
1758 /* Don't leave ssh hanging around */
1761 bus_set_state(bus
, BUS_CLOSED
);
1763 sd_bus_detach_event(bus
);
1765 /* Drop all queued messages so that they drop references to
1766 * the bus object and the bus may be freed */
1767 bus_reset_queues(bus
);
1769 bus_close_io_fds(bus
);
1770 bus_close_inotify_fd(bus
);
1773 _public_ sd_bus
*sd_bus_close_unref(sd_bus
*bus
) {
1779 return sd_bus_unref(bus
);
1782 _public_ sd_bus
* sd_bus_flush_close_unref(sd_bus
*bus
) {
1786 /* Have to do this before flush() to prevent hang */
1790 return sd_bus_close_unref(bus
);
1793 void bus_enter_closing(sd_bus
*bus
) {
1796 if (!IN_SET(bus
->state
, BUS_WATCH_BIND
, BUS_OPENING
, BUS_AUTHENTICATING
, BUS_HELLO
, BUS_RUNNING
))
1799 bus_set_state(bus
, BUS_CLOSING
);
1802 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_bus
, sd_bus
, bus_free
);
1804 _public_
int sd_bus_is_open(sd_bus
*bus
) {
1808 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1809 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1811 return BUS_IS_OPEN(bus
->state
);
1814 _public_
int sd_bus_is_ready(sd_bus
*bus
) {
1818 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1819 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1821 return bus
->state
== BUS_RUNNING
;
1824 _public_
int sd_bus_can_send(sd_bus
*bus
, char type
) {
1827 assert_return(bus
, -EINVAL
);
1828 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1829 assert_return(bus
->state
!= BUS_UNSET
, -ENOTCONN
);
1830 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1832 if (bus
->is_monitor
)
1835 if (type
== SD_BUS_TYPE_UNIX_FD
) {
1836 if (!bus
->accept_fd
)
1839 r
= bus_ensure_running(bus
);
1843 return bus
->can_fds
;
1846 return bus_type_is_valid(type
);
1849 _public_
int sd_bus_get_bus_id(sd_bus
*bus
, sd_id128_t
*id
) {
1852 assert_return(bus
, -EINVAL
);
1853 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1854 assert_return(id
, -EINVAL
);
1855 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1857 r
= bus_ensure_running(bus
);
1861 *id
= bus
->server_id
;
1865 #define COOKIE_CYCLED (UINT32_C(1) << 31)
1867 static uint64_t cookie_inc(uint64_t cookie
) {
1869 /* Stay within the 32bit range, since classic D-Bus can't deal with more */
1870 if (cookie
>= UINT32_MAX
)
1871 return COOKIE_CYCLED
; /* Don't go back to zero, but use the highest bit for checking
1872 * whether we are looping. */
1877 static int next_cookie(sd_bus
*b
) {
1878 uint64_t new_cookie
;
1882 new_cookie
= cookie_inc(b
->cookie
);
1884 /* Small optimization: don't bother with checking for cookie reuse until we overran cookiespace at
1885 * least once, but then do it thorougly. */
1886 if (FLAGS_SET(new_cookie
, COOKIE_CYCLED
)) {
1889 /* Check if the cookie is currently in use. If so, pick the next one */
1890 for (i
= 0; i
< COOKIE_CYCLED
; i
++) {
1891 if (!ordered_hashmap_contains(b
->reply_callbacks
, &new_cookie
))
1894 new_cookie
= cookie_inc(new_cookie
);
1897 /* Can't fulfill request */
1902 b
->cookie
= new_cookie
;
1906 static int bus_seal_message(sd_bus
*b
, sd_bus_message
*m
, usec_t timeout
) {
1913 /* If we copy the same message to multiple
1914 * destinations, avoid using the same cookie
1916 b
->cookie
= MAX(b
->cookie
, BUS_MESSAGE_COOKIE(m
));
1921 r
= sd_bus_get_method_call_timeout(b
, &timeout
);
1926 if (!m
->sender
&& b
->patch_sender
) {
1927 r
= sd_bus_message_set_sender(m
, b
->patch_sender
);
1936 return sd_bus_message_seal(m
, b
->cookie
, timeout
);
1939 static int bus_remarshal_message(sd_bus
*b
, sd_bus_message
**m
) {
1940 bool remarshal
= false;
1944 /* wrong packet version */
1945 if (b
->message_version
!= 0 && b
->message_version
!= (*m
)->header
->version
)
1948 /* wrong packet endianness */
1949 if (b
->message_endian
!= 0 && b
->message_endian
!= (*m
)->header
->endian
)
1952 return remarshal
? bus_message_remarshal(b
, m
) : 0;
1955 int bus_seal_synthetic_message(sd_bus
*b
, sd_bus_message
*m
) {
1959 /* Fake some timestamps, if they were requested, and not
1960 * already initialized */
1961 if (b
->attach_timestamp
) {
1962 if (m
->realtime
<= 0)
1963 m
->realtime
= now(CLOCK_REALTIME
);
1965 if (m
->monotonic
<= 0)
1966 m
->monotonic
= now(CLOCK_MONOTONIC
);
1969 /* The bus specification says the serial number cannot be 0,
1970 * hence let's fill something in for synthetic messages. Since
1971 * synthetic messages might have a fake sender and we don't
1972 * want to interfere with the real sender's serial numbers we
1973 * pick a fixed, artificial one. */
1974 return sd_bus_message_seal(m
, UINT32_MAX
, 0);
1977 static int bus_write_message(sd_bus
*bus
, sd_bus_message
*m
, size_t *idx
) {
1983 r
= bus_socket_write_message(bus
, m
, idx
);
1987 if (*idx
>= BUS_MESSAGE_SIZE(m
))
1988 log_debug("Sent message type=%s sender=%s destination=%s path=%s interface=%s member=%s"
1989 " cookie=%" PRIu64
" reply_cookie=%" PRIu64
1990 " signature=%s error-name=%s error-message=%s",
1991 bus_message_type_to_string(m
->header
->type
),
1992 strna(sd_bus_message_get_sender(m
)),
1993 strna(sd_bus_message_get_destination(m
)),
1994 strna(sd_bus_message_get_path(m
)),
1995 strna(sd_bus_message_get_interface(m
)),
1996 strna(sd_bus_message_get_member(m
)),
1997 BUS_MESSAGE_COOKIE(m
),
1999 strna(m
->root_container
.signature
),
2000 strna(m
->error
.name
),
2001 strna(m
->error
.message
));
2006 static int dispatch_wqueue(sd_bus
*bus
) {
2010 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
2012 while (bus
->wqueue_size
> 0) {
2014 r
= bus_write_message(bus
, bus
->wqueue
[0], &bus
->windex
);
2018 /* Didn't do anything this time */
2020 else if (bus
->windex
>= BUS_MESSAGE_SIZE(bus
->wqueue
[0])) {
2021 /* Fully written. Let's drop the entry from
2024 * This isn't particularly optimized, but
2025 * well, this is supposed to be our worst-case
2026 * buffer only, and the socket buffer is
2027 * supposed to be our primary buffer, and if
2028 * it got full, then all bets are off
2032 bus_message_unref_queued(bus
->wqueue
[0], bus
);
2033 memmove(bus
->wqueue
, bus
->wqueue
+ 1, sizeof(sd_bus_message
*) * bus
->wqueue_size
);
2043 static int bus_read_message(sd_bus
*bus
) {
2046 return bus_socket_read_message(bus
);
2049 int bus_rqueue_make_room(sd_bus
*bus
) {
2052 if (bus
->rqueue_size
>= BUS_RQUEUE_MAX
)
2055 if (!GREEDY_REALLOC(bus
->rqueue
, bus
->rqueue_size
+ 1))
2061 static void rqueue_drop_one(sd_bus
*bus
, size_t i
) {
2063 assert(i
< bus
->rqueue_size
);
2065 bus_message_unref_queued(bus
->rqueue
[i
], bus
);
2066 memmove(bus
->rqueue
+ i
, bus
->rqueue
+ i
+ 1, sizeof(sd_bus_message
*) * (bus
->rqueue_size
- i
- 1));
2070 static int dispatch_rqueue(sd_bus
*bus
, sd_bus_message
**m
) {
2075 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
2078 if (bus
->rqueue_size
> 0) {
2079 /* Dispatch a queued message */
2080 *m
= sd_bus_message_ref(bus
->rqueue
[0]);
2081 rqueue_drop_one(bus
, 0);
2085 /* Try to read a new message */
2086 r
= bus_read_message(bus
);
2098 _public_
int sd_bus_send(sd_bus
*bus
, sd_bus_message
*_m
, uint64_t *cookie
) {
2099 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= sd_bus_message_ref(_m
);
2102 assert_return(m
, -EINVAL
);
2105 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2107 assert_return(bus
= m
->bus
, -ENOTCONN
);
2108 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2110 if (!BUS_IS_OPEN(bus
->state
))
2114 r
= sd_bus_can_send(bus
, SD_BUS_TYPE_UNIX_FD
);
2121 /* If the cookie number isn't kept, then we know that no reply
2123 if (!cookie
&& !m
->sealed
)
2124 m
->header
->flags
|= BUS_MESSAGE_NO_REPLY_EXPECTED
;
2126 r
= bus_seal_message(bus
, m
, 0);
2130 /* Remarshall if we have to. This will possibly unref the
2131 * message and place a replacement in m */
2132 r
= bus_remarshal_message(bus
, &m
);
2136 /* If this is a reply and no reply was requested, then let's
2137 * suppress this, if we can */
2141 if (IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
) && bus
->wqueue_size
<= 0) {
2144 r
= bus_write_message(bus
, m
, &idx
);
2146 if (ERRNO_IS_DISCONNECT(r
)) {
2147 bus_enter_closing(bus
);
2154 if (idx
< BUS_MESSAGE_SIZE(m
)) {
2155 /* Wasn't fully written. So let's remember how
2156 * much was written. Note that the first entry
2157 * of the wqueue array is always allocated so
2158 * that we always can remember how much was
2160 bus
->wqueue
[0] = bus_message_ref_queued(m
, bus
);
2161 bus
->wqueue_size
= 1;
2166 /* Just append it to the queue. */
2168 if (bus
->wqueue_size
>= BUS_WQUEUE_MAX
)
2171 if (!GREEDY_REALLOC(bus
->wqueue
, bus
->wqueue_size
+ 1))
2174 bus
->wqueue
[bus
->wqueue_size
++] = bus_message_ref_queued(m
, bus
);
2179 *cookie
= BUS_MESSAGE_COOKIE(m
);
2184 _public_
int sd_bus_send_to(sd_bus
*bus
, sd_bus_message
*m
, const char *destination
, uint64_t *cookie
) {
2187 assert_return(m
, -EINVAL
);
2190 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2192 assert_return(bus
= m
->bus
, -ENOTCONN
);
2193 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2195 if (!BUS_IS_OPEN(bus
->state
))
2198 if (!streq_ptr(m
->destination
, destination
)) {
2203 r
= sd_bus_message_set_destination(m
, destination
);
2208 return sd_bus_send(bus
, m
, cookie
);
2211 static usec_t
calc_elapse(sd_bus
*bus
, uint64_t usec
) {
2214 assert_cc(sizeof(usec_t
) == sizeof(uint64_t));
2216 if (usec
== USEC_INFINITY
)
2219 /* We start all timeouts the instant we enter BUS_HELLO/BUS_RUNNING state, so that the don't run in parallel
2220 * with any connection setup states. Hence, if a method callback is started earlier than that we just store the
2221 * relative timestamp, and afterwards the absolute one. */
2223 if (IN_SET(bus
->state
, BUS_WATCH_BIND
, BUS_OPENING
, BUS_AUTHENTICATING
))
2226 return usec_add(now(CLOCK_MONOTONIC
), usec
);
2229 static int timeout_compare(const void *a
, const void *b
) {
2230 const struct reply_callback
*x
= a
, *y
= b
;
2232 if (x
->timeout_usec
!= 0 && y
->timeout_usec
== 0)
2235 if (x
->timeout_usec
== 0 && y
->timeout_usec
!= 0)
2238 return CMP(x
->timeout_usec
, y
->timeout_usec
);
2241 _public_
int sd_bus_call_async(
2245 sd_bus_message_handler_t callback
,
2249 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= sd_bus_message_ref(_m
);
2250 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*s
= NULL
;
2253 assert_return(m
, -EINVAL
);
2254 assert_return(m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
, -EINVAL
);
2255 assert_return(!m
->sealed
|| (!!callback
== !(m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
)), -EINVAL
);
2258 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2260 assert_return(bus
= m
->bus
, -ENOTCONN
);
2261 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2263 if (!BUS_IS_OPEN(bus
->state
))
2266 /* If no callback is specified and there's no interest in a slot, then there's no reason to ask for a reply */
2267 if (!callback
&& !slot
&& !m
->sealed
)
2268 m
->header
->flags
|= BUS_MESSAGE_NO_REPLY_EXPECTED
;
2270 r
= ordered_hashmap_ensure_allocated(&bus
->reply_callbacks
, &uint64_hash_ops
);
2274 r
= prioq_ensure_allocated(&bus
->reply_callbacks_prioq
, timeout_compare
);
2278 r
= bus_seal_message(bus
, m
, usec
);
2282 r
= bus_remarshal_message(bus
, &m
);
2286 if (slot
|| callback
) {
2287 s
= bus_slot_allocate(bus
, !slot
, BUS_REPLY_CALLBACK
, sizeof(struct reply_callback
), userdata
);
2291 s
->reply_callback
.callback
= callback
;
2293 s
->reply_callback
.cookie
= BUS_MESSAGE_COOKIE(m
);
2294 r
= ordered_hashmap_put(bus
->reply_callbacks
, &s
->reply_callback
.cookie
, &s
->reply_callback
);
2296 s
->reply_callback
.cookie
= 0;
2300 s
->reply_callback
.timeout_usec
= calc_elapse(bus
, m
->timeout
);
2301 if (s
->reply_callback
.timeout_usec
!= 0) {
2302 r
= prioq_put(bus
->reply_callbacks_prioq
, &s
->reply_callback
, &s
->reply_callback
.prioq_idx
);
2304 s
->reply_callback
.timeout_usec
= 0;
2310 r
= sd_bus_send(bus
, m
, s
? &s
->reply_callback
.cookie
: NULL
);
2321 int bus_ensure_running(sd_bus
*bus
) {
2326 if (bus
->state
== BUS_RUNNING
)
2330 if (IN_SET(bus
->state
, BUS_UNSET
, BUS_CLOSED
, BUS_CLOSING
))
2333 r
= sd_bus_process(bus
, NULL
);
2336 if (bus
->state
== BUS_RUNNING
)
2341 r
= sd_bus_wait(bus
, UINT64_MAX
);
2347 _public_
int sd_bus_call(
2351 sd_bus_error
*error
,
2352 sd_bus_message
**reply
) {
2354 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= sd_bus_message_ref(_m
);
2360 bus_assert_return(m
, -EINVAL
, error
);
2361 bus_assert_return(m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
, -EINVAL
, error
);
2362 bus_assert_return(!(m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
), -EINVAL
, error
);
2363 bus_assert_return(!bus_error_is_dirty(error
), -EINVAL
, error
);
2366 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2368 assert_return(bus
= m
->bus
, -ENOTCONN
);
2369 bus_assert_return(!bus_pid_changed(bus
), -ECHILD
, error
);
2371 if (!BUS_IS_OPEN(bus
->state
)) {
2376 r
= bus_ensure_running(bus
);
2380 i
= bus
->rqueue_size
;
2382 r
= bus_seal_message(bus
, m
, usec
);
2386 r
= bus_remarshal_message(bus
, &m
);
2390 r
= sd_bus_send(bus
, m
, &cookie
);
2394 timeout
= calc_elapse(bus
, m
->timeout
);
2399 while (i
< bus
->rqueue_size
) {
2400 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*incoming
= NULL
;
2402 incoming
= sd_bus_message_ref(bus
->rqueue
[i
]);
2404 if (incoming
->reply_cookie
== cookie
) {
2405 /* Found a match! */
2407 rqueue_drop_one(bus
, i
);
2408 log_debug_bus_message(incoming
);
2410 if (incoming
->header
->type
== SD_BUS_MESSAGE_METHOD_RETURN
) {
2412 if (incoming
->n_fds
<= 0 || bus
->accept_fd
) {
2414 *reply
= TAKE_PTR(incoming
);
2419 return sd_bus_error_set(error
, SD_BUS_ERROR_INCONSISTENT_MESSAGE
,
2420 "Reply message contained file descriptors which I couldn't accept. Sorry.");
2422 } else if (incoming
->header
->type
== SD_BUS_MESSAGE_METHOD_ERROR
)
2423 return sd_bus_error_copy(error
, &incoming
->error
);
2429 } else if (BUS_MESSAGE_COOKIE(incoming
) == cookie
&&
2432 streq(bus
->unique_name
, incoming
->sender
)) {
2434 rqueue_drop_one(bus
, i
);
2436 /* Our own message? Somebody is trying to send its own client a message,
2437 * let's not dead-lock, let's fail immediately. */
2443 /* Try to read more, right-away */
2447 r
= bus_read_message(bus
);
2449 if (ERRNO_IS_DISCONNECT(r
)) {
2450 bus_enter_closing(bus
);
2462 n
= now(CLOCK_MONOTONIC
);
2472 r
= bus_poll(bus
, true, left
);
2474 if (ERRNO_IS_TRANSIENT(r
))
2483 r
= dispatch_wqueue(bus
);
2485 if (ERRNO_IS_DISCONNECT(r
)) {
2486 bus_enter_closing(bus
);
2495 return sd_bus_error_set_errno(error
, r
);
2498 _public_
int sd_bus_get_fd(sd_bus
*bus
) {
2499 assert_return(bus
, -EINVAL
);
2500 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2501 assert_return(bus
->input_fd
== bus
->output_fd
, -EPERM
);
2502 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2504 if (bus
->state
== BUS_CLOSED
)
2507 if (bus
->inotify_fd
>= 0)
2508 return bus
->inotify_fd
;
2510 if (bus
->input_fd
>= 0)
2511 return bus
->input_fd
;
2516 _public_
int sd_bus_get_events(sd_bus
*bus
) {
2519 assert_return(bus
, -EINVAL
);
2520 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2521 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2523 switch (bus
->state
) {
2529 case BUS_WATCH_BIND
:
2537 case BUS_AUTHENTICATING
:
2538 if (bus_socket_auth_needs_write(bus
))
2546 if (bus
->rqueue_size
<= 0)
2548 if (bus
->wqueue_size
> 0)
2556 assert_not_reached();
2562 _public_
int sd_bus_get_timeout(sd_bus
*bus
, uint64_t *timeout_usec
) {
2563 struct reply_callback
*c
;
2565 assert_return(bus
, -EINVAL
);
2566 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2567 assert_return(timeout_usec
, -EINVAL
);
2568 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2570 if (!BUS_IS_OPEN(bus
->state
) && bus
->state
!= BUS_CLOSING
)
2573 if (bus
->track_queue
) {
2578 switch (bus
->state
) {
2580 case BUS_AUTHENTICATING
:
2581 *timeout_usec
= bus
->auth_timeout
;
2586 if (bus
->rqueue_size
> 0) {
2591 c
= prioq_peek(bus
->reply_callbacks_prioq
);
2593 *timeout_usec
= UINT64_MAX
;
2597 if (c
->timeout_usec
== 0) {
2598 *timeout_usec
= UINT64_MAX
;
2602 *timeout_usec
= c
->timeout_usec
;
2609 case BUS_WATCH_BIND
:
2611 *timeout_usec
= UINT64_MAX
;
2615 assert_not_reached();
2619 static int process_timeout(sd_bus
*bus
) {
2620 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2621 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* m
= NULL
;
2622 struct reply_callback
*c
;
2629 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
2631 c
= prioq_peek(bus
->reply_callbacks_prioq
);
2635 n
= now(CLOCK_MONOTONIC
);
2636 if (c
->timeout_usec
> n
)
2639 r
= bus_message_new_synthetic_error(
2642 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY
, "Method call timed out"),
2647 m
->read_counter
= ++bus
->read_counter
;
2649 r
= bus_seal_synthetic_message(bus
, m
);
2653 assert_se(prioq_pop(bus
->reply_callbacks_prioq
) == c
);
2654 c
->timeout_usec
= 0;
2656 ordered_hashmap_remove(bus
->reply_callbacks
, &c
->cookie
);
2659 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
2661 bus
->iteration_counter
++;
2663 is_hello
= bus
->state
== BUS_HELLO
&& c
->callback
== hello_callback
;
2665 bus
->current_message
= m
;
2666 bus
->current_slot
= sd_bus_slot_ref(slot
);
2667 bus
->current_handler
= c
->callback
;
2668 bus
->current_userdata
= slot
->userdata
;
2669 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
2670 bus
->current_userdata
= NULL
;
2671 bus
->current_handler
= NULL
;
2672 bus
->current_slot
= NULL
;
2673 bus
->current_message
= NULL
;
2676 bus_slot_disconnect(slot
, true);
2678 sd_bus_slot_unref(slot
);
2680 /* When this is the hello message and it timed out, then make sure to propagate the error up, don't just log
2681 * and ignore the callback handler's return value. */
2685 return bus_maybe_reply_error(m
, r
, &error_buffer
);
2688 static int process_hello(sd_bus
*bus
, sd_bus_message
*m
) {
2692 if (bus
->state
!= BUS_HELLO
)
2695 /* Let's make sure the first message on the bus is the HELLO
2696 * reply. But note that we don't actually parse the message
2697 * here (we leave that to the usual handling), we just verify
2698 * we don't let any earlier msg through. */
2700 if (!IN_SET(m
->header
->type
, SD_BUS_MESSAGE_METHOD_RETURN
, SD_BUS_MESSAGE_METHOD_ERROR
))
2703 if (m
->reply_cookie
!= 1)
2709 static int process_reply(sd_bus
*bus
, sd_bus_message
*m
) {
2710 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*synthetic_reply
= NULL
;
2711 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2712 struct reply_callback
*c
;
2720 if (!IN_SET(m
->header
->type
, SD_BUS_MESSAGE_METHOD_RETURN
, SD_BUS_MESSAGE_METHOD_ERROR
))
2723 if (m
->destination
&& bus
->unique_name
&& !streq_ptr(m
->destination
, bus
->unique_name
))
2726 c
= ordered_hashmap_remove(bus
->reply_callbacks
, &m
->reply_cookie
);
2732 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
2734 if (m
->n_fds
> 0 && !bus
->accept_fd
) {
2736 /* If the reply contained a file descriptor which we
2737 * didn't want we pass an error instead. */
2739 r
= bus_message_new_synthetic_error(
2742 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Reply message contained file descriptor"),
2747 /* Copy over original timestamp */
2748 synthetic_reply
->realtime
= m
->realtime
;
2749 synthetic_reply
->monotonic
= m
->monotonic
;
2750 synthetic_reply
->seqnum
= m
->seqnum
;
2751 synthetic_reply
->read_counter
= m
->read_counter
;
2753 r
= bus_seal_synthetic_message(bus
, synthetic_reply
);
2757 m
= synthetic_reply
;
2759 r
= sd_bus_message_rewind(m
, true);
2764 if (c
->timeout_usec
!= 0) {
2765 prioq_remove(bus
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
2766 c
->timeout_usec
= 0;
2769 is_hello
= bus
->state
== BUS_HELLO
&& c
->callback
== hello_callback
;
2771 bus
->current_slot
= sd_bus_slot_ref(slot
);
2772 bus
->current_handler
= c
->callback
;
2773 bus
->current_userdata
= slot
->userdata
;
2774 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
2775 bus
->current_userdata
= NULL
;
2776 bus
->current_handler
= NULL
;
2777 bus
->current_slot
= NULL
;
2780 bus_slot_disconnect(slot
, true);
2782 sd_bus_slot_unref(slot
);
2784 /* When this is the hello message and it failed, then make sure to propagate the error up, don't just log and
2785 * ignore the callback handler's return value. */
2789 return bus_maybe_reply_error(m
, r
, &error_buffer
);
2792 static int process_filter(sd_bus
*bus
, sd_bus_message
*m
) {
2793 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2800 bus
->filter_callbacks_modified
= false;
2802 LIST_FOREACH(callbacks
, l
, bus
->filter_callbacks
) {
2805 if (bus
->filter_callbacks_modified
)
2808 /* Don't run this more than once per iteration */
2809 if (l
->last_iteration
== bus
->iteration_counter
)
2812 l
->last_iteration
= bus
->iteration_counter
;
2814 r
= sd_bus_message_rewind(m
, true);
2818 slot
= container_of(l
, sd_bus_slot
, filter_callback
);
2820 bus
->current_slot
= sd_bus_slot_ref(slot
);
2821 bus
->current_handler
= l
->callback
;
2822 bus
->current_userdata
= slot
->userdata
;
2823 r
= l
->callback(m
, slot
->userdata
, &error_buffer
);
2824 bus
->current_userdata
= NULL
;
2825 bus
->current_handler
= NULL
;
2826 bus
->current_slot
= sd_bus_slot_unref(slot
);
2828 r
= bus_maybe_reply_error(m
, r
, &error_buffer
);
2834 } while (bus
->filter_callbacks_modified
);
2839 static int process_match(sd_bus
*bus
, sd_bus_message
*m
) {
2846 bus
->match_callbacks_modified
= false;
2848 r
= bus_match_run(bus
, &bus
->match_callbacks
, m
);
2852 } while (bus
->match_callbacks_modified
);
2857 static int process_builtin(sd_bus
*bus
, sd_bus_message
*m
) {
2858 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2864 if (bus
->is_monitor
)
2867 if (bus
->manual_peer_interface
)
2870 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_CALL
)
2873 if (!streq_ptr(m
->interface
, "org.freedesktop.DBus.Peer"))
2876 if (m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
)
2879 if (streq_ptr(m
->member
, "Ping"))
2880 r
= sd_bus_message_new_method_return(m
, &reply
);
2881 else if (streq_ptr(m
->member
, "GetMachineId")) {
2884 r
= sd_id128_get_machine(&id
);
2888 r
= sd_bus_message_new_method_return(m
, &reply
);
2892 r
= sd_bus_message_append(reply
, "s", SD_ID128_TO_STRING(id
));
2894 r
= sd_bus_message_new_method_errorf(
2896 SD_BUS_ERROR_UNKNOWN_METHOD
,
2897 "Unknown method '%s' on interface '%s'.", m
->member
, m
->interface
);
2902 r
= sd_bus_send(bus
, reply
, NULL
);
2909 static int process_fd_check(sd_bus
*bus
, sd_bus_message
*m
) {
2913 /* If we got a message with a file descriptor which we didn't
2914 * want to accept, then let's drop it. How can this even
2915 * happen? For example, when the kernel queues a message into
2916 * an activatable names's queue which allows fds, and then is
2917 * delivered to us later even though we ourselves did not
2920 if (bus
->is_monitor
)
2929 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_CALL
)
2930 return 1; /* just eat it up */
2932 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_INCONSISTENT_MESSAGE
,
2933 "Message contains file descriptors, which I cannot accept. Sorry.");
2936 static int process_message(sd_bus
*bus
, sd_bus_message
*m
) {
2937 _unused_
_cleanup_(log_context_freep
) LogContext
*c
= NULL
;
2943 bus
->current_message
= m
;
2944 bus
->iteration_counter
++;
2946 if (log_context_enabled())
2947 c
= log_context_new_consume(bus_message_make_log_fields(m
));
2949 log_debug_bus_message(m
);
2951 r
= process_hello(bus
, m
);
2955 r
= process_reply(bus
, m
);
2959 r
= process_fd_check(bus
, m
);
2963 r
= process_filter(bus
, m
);
2967 r
= process_match(bus
, m
);
2971 r
= process_builtin(bus
, m
);
2975 r
= bus_process_object(bus
, m
);
2978 bus
->current_message
= NULL
;
2982 static int dispatch_track(sd_bus
*bus
) {
2985 if (!bus
->track_queue
)
2988 bus_track_dispatch(bus
->track_queue
);
2992 static int process_running(sd_bus
*bus
, sd_bus_message
**ret
) {
2993 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2997 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
2999 r
= process_timeout(bus
);
3003 r
= dispatch_wqueue(bus
);
3007 r
= dispatch_track(bus
);
3011 r
= dispatch_rqueue(bus
, &m
);
3017 r
= process_message(bus
, m
);
3022 r
= sd_bus_message_rewind(m
, true);
3030 if (m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
) {
3032 log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s",
3033 strna(sd_bus_message_get_sender(m
)),
3034 strna(sd_bus_message_get_path(m
)),
3035 strna(sd_bus_message_get_interface(m
)),
3036 strna(sd_bus_message_get_member(m
)));
3038 r
= sd_bus_reply_method_errorf(
3040 SD_BUS_ERROR_UNKNOWN_OBJECT
,
3041 "Unknown object '%s'.", m
->path
);
3055 static int bus_exit_now(sd_bus
*bus
) {
3058 /* Exit due to close, if this is requested. If this is bus object is attached to an event source, invokes
3059 * sd_event_exit(), otherwise invokes libc exit(). */
3061 if (bus
->exited
) /* did we already exit? */
3063 if (!bus
->exit_triggered
) /* was the exit condition triggered? */
3065 if (!bus
->exit_on_disconnect
) /* Shall we actually exit on disconnection? */
3068 bus
->exited
= true; /* never exit more than once */
3070 log_debug("Bus connection disconnected, exiting.");
3073 return sd_event_exit(bus
->event
, EXIT_FAILURE
);
3077 assert_not_reached();
3080 static int process_closing_reply_callback(sd_bus
*bus
, struct reply_callback
*c
) {
3081 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
3082 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
3089 r
= bus_message_new_synthetic_error(
3092 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY
, "Connection terminated"),
3097 m
->read_counter
= ++bus
->read_counter
;
3099 r
= bus_seal_synthetic_message(bus
, m
);
3103 if (c
->timeout_usec
!= 0) {
3104 prioq_remove(bus
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
3105 c
->timeout_usec
= 0;
3108 ordered_hashmap_remove(bus
->reply_callbacks
, &c
->cookie
);
3111 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
3113 bus
->iteration_counter
++;
3115 bus
->current_message
= m
;
3116 bus
->current_slot
= sd_bus_slot_ref(slot
);
3117 bus
->current_handler
= c
->callback
;
3118 bus
->current_userdata
= slot
->userdata
;
3119 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
3120 bus
->current_userdata
= NULL
;
3121 bus
->current_handler
= NULL
;
3122 bus
->current_slot
= NULL
;
3123 bus
->current_message
= NULL
;
3126 bus_slot_disconnect(slot
, true);
3128 sd_bus_slot_unref(slot
);
3130 return bus_maybe_reply_error(m
, r
, &error_buffer
);
3133 static int process_closing(sd_bus
*bus
, sd_bus_message
**ret
) {
3134 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
3135 struct reply_callback
*c
;
3139 assert(bus
->state
== BUS_CLOSING
);
3141 /* First, fail all outstanding method calls */
3142 c
= ordered_hashmap_first(bus
->reply_callbacks
);
3144 return process_closing_reply_callback(bus
, c
);
3146 /* Then, fake-drop all remaining bus tracking references */
3148 bus_track_close(bus
->tracks
);
3152 /* Then, synthesize a Disconnected message */
3153 r
= sd_bus_message_new_signal(
3156 "/org/freedesktop/DBus/Local",
3157 "org.freedesktop.DBus.Local",
3162 bus_message_set_sender_local(bus
, m
);
3163 m
->read_counter
= ++bus
->read_counter
;
3165 r
= bus_seal_synthetic_message(bus
, m
);
3171 bus
->current_message
= m
;
3172 bus
->iteration_counter
++;
3174 r
= process_filter(bus
, m
);
3178 r
= process_match(bus
, m
);
3182 /* Nothing else to do, exit now, if the condition holds */
3183 bus
->exit_triggered
= true;
3184 (void) bus_exit_now(bus
);
3192 bus
->current_message
= NULL
;
3197 static int bus_process_internal(sd_bus
*bus
, sd_bus_message
**ret
) {
3200 /* Returns 0 when we didn't do anything. This should cause the
3201 * caller to invoke sd_bus_wait() before returning the next
3202 * time. Returns > 0 when we did something, which possibly
3203 * means *ret is filled in with an unprocessed message. */
3205 assert_return(bus
, -EINVAL
);
3206 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3207 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3209 /* We don't allow recursively invoking sd_bus_process(). */
3210 assert_return(!bus
->current_message
, -EBUSY
);
3211 assert(!bus
->current_slot
); /* This should be NULL whenever bus->current_message is */
3213 BUS_DONT_DESTROY(bus
);
3215 switch (bus
->state
) {
3223 case BUS_WATCH_BIND
:
3224 r
= bus_socket_process_watch_bind(bus
);
3228 r
= bus_socket_process_opening(bus
);
3231 case BUS_AUTHENTICATING
:
3232 r
= bus_socket_process_authenticating(bus
);
3237 r
= process_running(bus
, ret
);
3241 /* This branch initializes *ret, hence we don't use the generic error checking below */
3245 return process_closing(bus
, ret
);
3248 assert_not_reached();
3251 if (ERRNO_IS_DISCONNECT(r
)) {
3252 bus_enter_closing(bus
);
3263 _public_
int sd_bus_process(sd_bus
*bus
, sd_bus_message
**ret
) {
3264 return bus_process_internal(bus
, ret
);
3267 _public_
int sd_bus_process_priority(sd_bus
*bus
, int64_t priority
, sd_bus_message
**ret
) {
3268 return bus_process_internal(bus
, ret
);
3271 static int bus_poll(sd_bus
*bus
, bool need_more
, uint64_t timeout_usec
) {
3272 struct pollfd p
[2] = {};
3273 usec_t m
= USEC_INFINITY
;
3278 if (bus
->state
== BUS_CLOSING
)
3281 if (!BUS_IS_OPEN(bus
->state
))
3284 if (bus
->state
== BUS_WATCH_BIND
) {
3285 assert(bus
->inotify_fd
>= 0);
3287 p
[0].events
= POLLIN
;
3288 p
[0].fd
= bus
->inotify_fd
;
3293 e
= sd_bus_get_events(bus
);
3298 /* The caller really needs some more data, they don't
3299 * care about what's already read, or any timeouts
3300 * except its own. */
3304 /* The caller wants to process if there's something to
3305 * process, but doesn't care otherwise */
3307 r
= sd_bus_get_timeout(bus
, &until
);
3311 m
= usec_sub_unsigned(until
, now(CLOCK_MONOTONIC
));
3314 p
[0].fd
= bus
->input_fd
;
3315 if (bus
->output_fd
== bus
->input_fd
) {
3319 p
[0].events
= e
& POLLIN
;
3320 p
[1].fd
= bus
->output_fd
;
3321 p
[1].events
= e
& POLLOUT
;
3326 if (timeout_usec
!= UINT64_MAX
&& (m
== USEC_INFINITY
|| timeout_usec
< m
))
3329 r
= ppoll_usec(p
, n
, m
);
3336 _public_
int sd_bus_wait(sd_bus
*bus
, uint64_t timeout_usec
) {
3339 assert_return(bus
, -EINVAL
);
3340 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3341 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3343 if (bus
->state
== BUS_CLOSING
)
3346 if (!BUS_IS_OPEN(bus
->state
))
3349 if (bus
->rqueue_size
> 0)
3352 r
= bus_poll(bus
, false, timeout_usec
);
3353 if (r
< 0 && ERRNO_IS_TRANSIENT(r
))
3354 return 1; /* treat EINTR as success, but let's exit, so that the caller will call back into us soon. */
3359 _public_
int sd_bus_flush(sd_bus
*bus
) {
3362 assert_return(bus
, -EINVAL
);
3363 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3364 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3366 if (bus
->state
== BUS_CLOSING
)
3369 if (!BUS_IS_OPEN(bus
->state
))
3372 /* We never were connected? Don't hang in inotify for good, as there's no timeout set for it */
3373 if (bus
->state
== BUS_WATCH_BIND
)
3376 r
= bus_ensure_running(bus
);
3380 if (bus
->wqueue_size
<= 0)
3384 r
= dispatch_wqueue(bus
);
3386 if (ERRNO_IS_DISCONNECT(r
)) {
3387 bus_enter_closing(bus
);
3394 if (bus
->wqueue_size
<= 0)
3397 r
= bus_poll(bus
, false, UINT64_MAX
);
3399 if (ERRNO_IS_TRANSIENT(r
))
3407 _public_
int sd_bus_add_filter(
3410 sd_bus_message_handler_t callback
,
3415 assert_return(bus
, -EINVAL
);
3416 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3417 assert_return(callback
, -EINVAL
);
3418 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3420 s
= bus_slot_allocate(bus
, !slot
, BUS_FILTER_CALLBACK
, sizeof(struct filter_callback
), userdata
);
3424 s
->filter_callback
.callback
= callback
;
3426 bus
->filter_callbacks_modified
= true;
3427 LIST_PREPEND(callbacks
, bus
->filter_callbacks
, &s
->filter_callback
);
3435 static int add_match_callback(
3438 sd_bus_error
*ret_error
) {
3440 sd_bus_slot
*match_slot
= ASSERT_PTR(userdata
);
3441 bool failed
= false;
3446 sd_bus_slot_ref(match_slot
);
3448 if (sd_bus_message_is_method_error(m
, NULL
)) {
3449 log_debug_errno(sd_bus_message_get_errno(m
),
3450 "Unable to add match %s, failing connection: %s",
3451 match_slot
->match_callback
.match_string
,
3452 sd_bus_message_get_error(m
)->message
);
3456 log_debug("Match %s successfully installed.", match_slot
->match_callback
.match_string
);
3458 if (match_slot
->match_callback
.install_callback
) {
3461 bus
= sd_bus_message_get_bus(m
);
3463 /* This function has been called as slot handler, and we want to call another slot handler. Let's
3464 * update the slot callback metadata temporarily with our own data, and then revert back to the old
3467 assert(bus
->current_slot
== match_slot
->match_callback
.install_slot
);
3468 assert(bus
->current_handler
== add_match_callback
);
3469 assert(bus
->current_userdata
== userdata
);
3471 bus
->current_slot
= match_slot
;
3472 bus
->current_handler
= match_slot
->match_callback
.install_callback
;
3473 bus
->current_userdata
= match_slot
->userdata
;
3475 r
= match_slot
->match_callback
.install_callback(m
, match_slot
->userdata
, ret_error
);
3477 bus
->current_slot
= match_slot
->match_callback
.install_slot
;
3478 bus
->current_handler
= add_match_callback
;
3479 bus
->current_userdata
= userdata
;
3481 if (failed
) /* Generic failure handling: destroy the connection */
3482 bus_enter_closing(sd_bus_message_get_bus(m
));
3487 /* We don't need the install method reply slot anymore, let's free it */
3488 match_slot
->match_callback
.install_slot
= sd_bus_slot_unref(match_slot
->match_callback
.install_slot
);
3490 if (failed
&& match_slot
->floating
)
3491 bus_slot_disconnect(match_slot
, true);
3493 sd_bus_slot_unref(match_slot
);
3498 static int bus_add_match_full(
3503 sd_bus_message_handler_t callback
,
3504 sd_bus_message_handler_t install_callback
,
3507 struct bus_match_component
*components
= NULL
;
3508 unsigned n_components
= 0;
3509 sd_bus_slot
*s
= NULL
;
3512 assert_return(bus
, -EINVAL
);
3513 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3514 assert_return(match
, -EINVAL
);
3515 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3517 r
= bus_match_parse(match
, &components
, &n_components
);
3521 s
= bus_slot_allocate(bus
, !slot
, BUS_MATCH_CALLBACK
, sizeof(struct match_callback
), userdata
);
3527 s
->match_callback
.callback
= callback
;
3528 s
->match_callback
.install_callback
= install_callback
;
3530 if (bus
->bus_client
) {
3531 enum bus_match_scope scope
;
3533 scope
= bus_match_get_scope(components
, n_components
);
3535 /* Do not install server-side matches for matches against the local service, interface or bus path. */
3536 if (scope
!= BUS_MATCH_LOCAL
) {
3538 /* We store the original match string, so that we can use it to remove the match again. */
3540 s
->match_callback
.match_string
= strdup(match
);
3541 if (!s
->match_callback
.match_string
) {
3547 r
= bus_add_match_internal_async(bus
,
3548 &s
->match_callback
.install_slot
,
3549 s
->match_callback
.match_string
,
3556 /* Make the slot of the match call floating now. We need the reference, but we don't
3557 * want that this match pins the bus object, hence we first create it non-floating, but
3558 * then make it floating. */
3559 r
= sd_bus_slot_set_floating(s
->match_callback
.install_slot
, true);
3561 r
= bus_add_match_internal(bus
, s
->match_callback
.match_string
, &s
->match_callback
.after
);
3565 s
->match_added
= true;
3569 bus
->match_callbacks_modified
= true;
3570 r
= bus_match_add(&bus
->match_callbacks
, components
, n_components
, &s
->match_callback
);
3579 bus_match_parse_free(components
, n_components
);
3580 sd_bus_slot_unref(s
);
3585 _public_
int sd_bus_add_match(
3589 sd_bus_message_handler_t callback
,
3592 return bus_add_match_full(bus
, slot
, false, match
, callback
, NULL
, userdata
);
3595 _public_
int sd_bus_add_match_async(
3599 sd_bus_message_handler_t callback
,
3600 sd_bus_message_handler_t install_callback
,
3603 return bus_add_match_full(bus
, slot
, true, match
, callback
, install_callback
, userdata
);
3606 bool bus_pid_changed(sd_bus
*bus
) {
3609 /* We don't support people creating a bus connection and
3610 * keeping it around over a fork(). Let's complain. */
3612 return bus
->original_pid
!= getpid_cached();
3615 static int io_callback(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
3616 sd_bus
*bus
= ASSERT_PTR(userdata
);
3619 /* Note that this is called both on input_fd, output_fd as well as inotify_fd events */
3621 r
= sd_bus_process(bus
, NULL
);
3623 log_debug_errno(r
, "Processing of bus failed, closing down: %m");
3624 bus_enter_closing(bus
);
3630 static int time_callback(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
3631 sd_bus
*bus
= ASSERT_PTR(userdata
);
3634 r
= sd_bus_process(bus
, NULL
);
3636 log_debug_errno(r
, "Processing of bus failed, closing down: %m");
3637 bus_enter_closing(bus
);
3643 static int prepare_callback(sd_event_source
*s
, void *userdata
) {
3644 sd_bus
*bus
= ASSERT_PTR(userdata
);
3650 e
= sd_bus_get_events(bus
);
3656 if (bus
->output_fd
!= bus
->input_fd
) {
3658 r
= sd_event_source_set_io_events(bus
->input_io_event_source
, e
& POLLIN
);
3662 r
= sd_event_source_set_io_events(bus
->output_io_event_source
, e
& POLLOUT
);
3664 r
= sd_event_source_set_io_events(bus
->input_io_event_source
, e
);
3668 r
= sd_bus_get_timeout(bus
, &until
);
3674 j
= sd_event_source_set_time(bus
->time_event_source
, until
);
3681 r
= sd_event_source_set_enabled(bus
->time_event_source
, r
> 0 ? SD_EVENT_ONESHOT
: SD_EVENT_OFF
);
3688 log_debug_errno(r
, "Preparing of bus events failed, closing down: %m");
3689 bus_enter_closing(bus
);
3694 static int quit_callback(sd_event_source
*event
, void *userdata
) {
3695 sd_bus
*bus
= userdata
;
3699 if (bus
->close_on_exit
) {
3707 int bus_attach_io_events(sd_bus
*bus
) {
3712 if (bus
->input_fd
< 0)
3718 if (!bus
->input_io_event_source
) {
3719 r
= sd_event_add_io(bus
->event
, &bus
->input_io_event_source
, bus
->input_fd
, 0, io_callback
, bus
);
3723 r
= sd_event_source_set_prepare(bus
->input_io_event_source
, prepare_callback
);
3727 r
= sd_event_source_set_priority(bus
->input_io_event_source
, bus
->event_priority
);
3731 r
= sd_event_source_set_description(bus
->input_io_event_source
, "bus-input");
3733 r
= sd_event_source_set_io_fd(bus
->input_io_event_source
, bus
->input_fd
);
3738 if (bus
->output_fd
!= bus
->input_fd
) {
3739 assert(bus
->output_fd
>= 0);
3741 if (!bus
->output_io_event_source
) {
3742 r
= sd_event_add_io(bus
->event
, &bus
->output_io_event_source
, bus
->output_fd
, 0, io_callback
, bus
);
3746 r
= sd_event_source_set_priority(bus
->output_io_event_source
, bus
->event_priority
);
3750 r
= sd_event_source_set_description(bus
->input_io_event_source
, "bus-output");
3752 r
= sd_event_source_set_io_fd(bus
->output_io_event_source
, bus
->output_fd
);
3761 static void bus_detach_io_events(sd_bus
*bus
) {
3764 bus
->input_io_event_source
= sd_event_source_disable_unref(bus
->input_io_event_source
);
3765 bus
->output_io_event_source
= sd_event_source_disable_unref(bus
->output_io_event_source
);
3768 int bus_attach_inotify_event(sd_bus
*bus
) {
3773 if (bus
->inotify_fd
< 0)
3779 if (!bus
->inotify_event_source
) {
3780 r
= sd_event_add_io(bus
->event
, &bus
->inotify_event_source
, bus
->inotify_fd
, EPOLLIN
, io_callback
, bus
);
3784 r
= sd_event_source_set_priority(bus
->inotify_event_source
, bus
->event_priority
);
3788 r
= sd_event_source_set_description(bus
->inotify_event_source
, "bus-inotify");
3790 r
= sd_event_source_set_io_fd(bus
->inotify_event_source
, bus
->inotify_fd
);
3797 _public_
int sd_bus_attach_event(sd_bus
*bus
, sd_event
*event
, int priority
) {
3800 assert_return(bus
, -EINVAL
);
3801 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3802 assert_return(!bus
->event
, -EBUSY
);
3804 assert(!bus
->input_io_event_source
);
3805 assert(!bus
->output_io_event_source
);
3806 assert(!bus
->time_event_source
);
3809 bus
->event
= sd_event_ref(event
);
3811 r
= sd_event_default(&bus
->event
);
3816 bus
->event_priority
= priority
;
3818 r
= sd_event_add_time(bus
->event
, &bus
->time_event_source
, CLOCK_MONOTONIC
, 0, 0, time_callback
, bus
);
3822 r
= sd_event_source_set_priority(bus
->time_event_source
, priority
);
3826 r
= sd_event_source_set_description(bus
->time_event_source
, "bus-time");
3830 r
= sd_event_add_exit(bus
->event
, &bus
->quit_event_source
, quit_callback
, bus
);
3834 r
= sd_event_source_set_description(bus
->quit_event_source
, "bus-exit");
3838 r
= bus_attach_io_events(bus
);
3842 r
= bus_attach_inotify_event(bus
);
3849 sd_bus_detach_event(bus
);
3853 _public_
int sd_bus_detach_event(sd_bus
*bus
) {
3854 assert_return(bus
, -EINVAL
);
3855 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3860 bus_detach_io_events(bus
);
3861 bus
->inotify_event_source
= sd_event_source_disable_unref(bus
->inotify_event_source
);
3862 bus
->time_event_source
= sd_event_source_disable_unref(bus
->time_event_source
);
3863 bus
->quit_event_source
= sd_event_source_disable_unref(bus
->quit_event_source
);
3865 bus
->event
= sd_event_unref(bus
->event
);
3869 _public_ sd_event
* sd_bus_get_event(sd_bus
*bus
) {
3870 assert_return(bus
= bus_resolve(bus
), NULL
);
3875 _public_ sd_bus_message
* sd_bus_get_current_message(sd_bus
*bus
) {
3876 assert_return(bus
= bus_resolve(bus
), NULL
);
3878 return bus
->current_message
;
3881 _public_ sd_bus_slot
* sd_bus_get_current_slot(sd_bus
*bus
) {
3882 assert_return(bus
= bus_resolve(bus
), NULL
);
3884 return bus
->current_slot
;
3887 _public_ sd_bus_message_handler_t
sd_bus_get_current_handler(sd_bus
*bus
) {
3888 assert_return(bus
= bus_resolve(bus
), NULL
);
3890 return bus
->current_handler
;
3893 _public_
void* sd_bus_get_current_userdata(sd_bus
*bus
) {
3894 assert_return(bus
= bus_resolve(bus
), NULL
);
3896 return bus
->current_userdata
;
3899 static int bus_default(int (*bus_open
)(sd_bus
**), sd_bus
**default_bus
, sd_bus
**ret
) {
3904 assert(default_bus
);
3907 return !!*default_bus
;
3910 *ret
= sd_bus_ref(*default_bus
);
3918 b
->default_bus_ptr
= default_bus
;
3926 _public_
int sd_bus_default_system(sd_bus
**ret
) {
3927 return bus_default(sd_bus_open_system
, &default_system_bus
, ret
);
3930 _public_
int sd_bus_default_user(sd_bus
**ret
) {
3931 return bus_default(sd_bus_open_user
, &default_user_bus
, ret
);
3934 _public_
int sd_bus_default(sd_bus
**ret
) {
3935 int (*bus_open
)(sd_bus
**) = NULL
;
3938 busp
= bus_choose_default(&bus_open
);
3939 return bus_default(bus_open
, busp
, ret
);
3942 _public_
int sd_bus_get_tid(sd_bus
*b
, pid_t
*tid
) {
3943 assert_return(b
, -EINVAL
);
3944 assert_return(tid
, -EINVAL
);
3945 assert_return(!bus_pid_changed(b
), -ECHILD
);
3953 return sd_event_get_tid(b
->event
, tid
);
3958 _public_
int sd_bus_path_encode(const char *prefix
, const char *external_id
, char **ret_path
) {
3959 _cleanup_free_
char *e
= NULL
;
3962 assert_return(object_path_is_valid(prefix
), -EINVAL
);
3963 assert_return(external_id
, -EINVAL
);
3964 assert_return(ret_path
, -EINVAL
);
3966 e
= bus_label_escape(external_id
);
3970 ret
= path_join(prefix
, e
);
3978 _public_
int sd_bus_path_decode(const char *path
, const char *prefix
, char **external_id
) {
3982 assert_return(object_path_is_valid(path
), -EINVAL
);
3983 assert_return(object_path_is_valid(prefix
), -EINVAL
);
3984 assert_return(external_id
, -EINVAL
);
3986 e
= object_path_startswith(path
, prefix
);
3988 *external_id
= NULL
;
3992 /* Note that 'e' might be an empty string here. That's expected. E.g. a case where the subtree
3993 * corresponds to a subtree on a disk, and we want to return something that represents the root
3994 * of the filesystem. */
3996 ret
= bus_label_unescape(e
);
4004 _public_
int sd_bus_path_encode_many(char **out
, const char *path_template
, ...) {
4005 _cleanup_strv_free_
char **labels
= NULL
;
4006 char *path
, *path_pos
, **label_pos
;
4007 const char *sep
, *template_pos
;
4012 assert_return(out
, -EINVAL
);
4013 assert_return(path_template
, -EINVAL
);
4015 path_length
= strlen(path_template
);
4017 va_start(list
, path_template
);
4018 for (sep
= strchr(path_template
, '%'); sep
; sep
= strchr(sep
+ 1, '%')) {
4022 arg
= va_arg(list
, const char *);
4028 label
= bus_label_escape(arg
);
4034 r
= strv_consume(&labels
, label
);
4040 /* add label length, but account for the format character */
4041 path_length
+= strlen(label
) - 1;
4045 path
= malloc(path_length
+ 1);
4052 for (template_pos
= path_template
; *template_pos
; ) {
4053 sep
= strchrnul(template_pos
, '%');
4054 path_pos
= mempcpy(path_pos
, template_pos
, sep
- template_pos
);
4058 path_pos
= stpcpy(path_pos
, *label_pos
++);
4059 template_pos
= sep
+ 1;
4067 _public_
int sd_bus_path_decode_many(const char *path
, const char *path_template
, ...) {
4068 _cleanup_strv_free_
char **labels
= NULL
;
4069 const char *template_pos
, *path_pos
;
4075 * This decodes an object-path based on a template argument. The
4076 * template consists of a verbatim path, optionally including special
4079 * - Each occurrence of '%' in the template matches an arbitrary
4080 * substring of a label in the given path. At most one such
4081 * directive is allowed per label. For each such directive, the
4082 * caller must provide an output parameter (char **) via va_arg. If
4083 * NULL is passed, the given label is verified, but not returned.
4084 * For each matched label, the *decoded* label is stored in the
4085 * passed output argument, and the caller is responsible to free
4086 * it. Note that the output arguments are only modified if the
4087 * actually path matched the template. Otherwise, they're left
4090 * This function returns <0 on error, 0 if the path does not match the
4091 * template, 1 if it matched.
4094 assert_return(path
, -EINVAL
);
4095 assert_return(path_template
, -EINVAL
);
4099 for (template_pos
= path_template
; *template_pos
; ) {
4104 /* verify everything until the next '%' matches verbatim */
4105 sep
= strchrnul(template_pos
, '%');
4106 length
= sep
- template_pos
;
4107 if (strncmp(path_pos
, template_pos
, length
))
4111 template_pos
+= length
;
4116 /* We found the next '%' character. Everything up until here
4117 * matched. We now skip ahead to the end of this label and make
4118 * sure it matches the tail of the label in the path. Then we
4119 * decode the string in-between and save it for later use. */
4121 ++template_pos
; /* skip over '%' */
4123 sep
= strchrnul(template_pos
, '/');
4124 length
= sep
- template_pos
; /* length of suffix to match verbatim */
4126 /* verify the suffixes match */
4127 sep
= strchrnul(path_pos
, '/');
4128 if (sep
- path_pos
< (ssize_t
)length
||
4129 strncmp(sep
- length
, template_pos
, length
))
4132 template_pos
+= length
; /* skip over matched label */
4133 length
= sep
- path_pos
- length
; /* length of sub-label to decode */
4135 /* store unescaped label for later use */
4136 label
= bus_label_unescape_n(path_pos
, length
);
4140 r
= strv_consume(&labels
, label
);
4144 path_pos
= sep
; /* skip decoded label and suffix */
4147 /* end of template must match end of path */
4151 /* copy the labels over to the caller */
4152 va_start(list
, path_template
);
4153 for (label_pos
= labels
; label_pos
&& *label_pos
; ++label_pos
) {
4156 arg
= va_arg(list
, char **);
4164 labels
= mfree(labels
);
4168 _public_
int sd_bus_try_close(sd_bus
*bus
) {
4169 assert_return(bus
, -EINVAL
);
4170 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4171 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4176 _public_
int sd_bus_get_description(sd_bus
*bus
, const char **description
) {
4177 assert_return(bus
, -EINVAL
);
4178 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4179 assert_return(description
, -EINVAL
);
4180 assert_return(bus
->description
, -ENXIO
);
4181 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4183 if (bus
->description
)
4184 *description
= bus
->description
;
4185 else if (bus
->is_system
)
4186 *description
= "system";
4187 else if (bus
->is_user
)
4188 *description
= "user";
4190 *description
= NULL
;
4195 _public_
int sd_bus_get_scope(sd_bus
*bus
, const char **scope
) {
4196 assert_return(bus
, -EINVAL
);
4197 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4198 assert_return(scope
, -EINVAL
);
4199 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4206 if (bus
->is_system
) {
4214 _public_
int sd_bus_get_address(sd_bus
*bus
, const char **address
) {
4215 assert_return(bus
, -EINVAL
);
4216 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4217 assert_return(address
, -EINVAL
);
4218 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4221 *address
= bus
->address
;
4228 _public_
int sd_bus_get_creds_mask(sd_bus
*bus
, uint64_t *mask
) {
4229 assert_return(bus
, -EINVAL
);
4230 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4231 assert_return(mask
, -EINVAL
);
4232 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4234 *mask
= bus
->creds_mask
;
4238 _public_
int sd_bus_is_bus_client(sd_bus
*bus
) {
4239 assert_return(bus
, -EINVAL
);
4240 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4241 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4243 return bus
->bus_client
;
4246 _public_
int sd_bus_is_server(sd_bus
*bus
) {
4247 assert_return(bus
, -EINVAL
);
4248 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4249 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4251 return bus
->is_server
;
4254 _public_
int sd_bus_is_anonymous(sd_bus
*bus
) {
4255 assert_return(bus
, -EINVAL
);
4256 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4257 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4259 return bus
->anonymous_auth
;
4262 _public_
int sd_bus_is_trusted(sd_bus
*bus
) {
4263 assert_return(bus
, -EINVAL
);
4264 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4265 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4267 return bus
->trusted
;
4270 _public_
int sd_bus_is_monitor(sd_bus
*bus
) {
4271 assert_return(bus
, -EINVAL
);
4272 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4273 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4275 return bus
->is_monitor
;
4278 static void flush_close(sd_bus
*bus
) {
4282 /* Flushes and closes the specified bus. We take a ref before,
4283 * to ensure the flushing does not cause the bus to be
4286 sd_bus_flush_close_unref(sd_bus_ref(bus
));
4289 _public_
void sd_bus_default_flush_close(void) {
4290 flush_close(default_starter_bus
);
4291 flush_close(default_user_bus
);
4292 flush_close(default_system_bus
);
4295 _public_
int sd_bus_set_exit_on_disconnect(sd_bus
*bus
, int b
) {
4296 assert_return(bus
, -EINVAL
);
4297 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4299 /* Turns on exit-on-disconnect, and triggers it immediately if the bus connection was already
4300 * disconnected. Note that this is triggered exclusively on disconnections triggered by the server side, never
4301 * from the client side. */
4302 bus
->exit_on_disconnect
= b
;
4304 /* If the exit condition was triggered already, exit immediately. */
4305 return bus_exit_now(bus
);
4308 _public_
int sd_bus_get_exit_on_disconnect(sd_bus
*bus
) {
4309 assert_return(bus
, -EINVAL
);
4310 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4312 return bus
->exit_on_disconnect
;
4315 _public_
int sd_bus_set_sender(sd_bus
*bus
, const char *sender
) {
4316 assert_return(bus
, -EINVAL
);
4317 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4318 assert_return(!bus
->bus_client
, -EPERM
);
4319 assert_return(!sender
|| service_name_is_valid(sender
), -EINVAL
);
4321 return free_and_strdup(&bus
->patch_sender
, sender
);
4324 _public_
int sd_bus_get_sender(sd_bus
*bus
, const char **ret
) {
4325 assert_return(bus
, -EINVAL
);
4326 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4327 assert_return(ret
, -EINVAL
);
4329 if (!bus
->patch_sender
)
4332 *ret
= bus
->patch_sender
;
4336 _public_
int sd_bus_get_n_queued_read(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
->rqueue_size
;
4346 _public_
int sd_bus_get_n_queued_write(sd_bus
*bus
, uint64_t *ret
) {
4347 assert_return(bus
, -EINVAL
);
4348 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4349 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4350 assert_return(ret
, -EINVAL
);
4352 *ret
= bus
->wqueue_size
;
4356 _public_
int sd_bus_set_method_call_timeout(sd_bus
*bus
, uint64_t usec
) {
4357 assert_return(bus
, -EINVAL
);
4358 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4360 bus
->method_call_timeout
= usec
;
4364 _public_
int sd_bus_get_method_call_timeout(sd_bus
*bus
, uint64_t *ret
) {
4368 assert_return(bus
, -EINVAL
);
4369 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4370 assert_return(ret
, -EINVAL
);
4372 if (bus
->method_call_timeout
!= 0) {
4373 *ret
= bus
->method_call_timeout
;
4377 e
= secure_getenv("SYSTEMD_BUS_TIMEOUT");
4378 if (e
&& parse_sec(e
, &usec
) >= 0 && usec
!= 0) {
4379 /* Save the parsed value to avoid multiple parsing. To change the timeout value,
4380 * use sd_bus_set_method_call_timeout() instead of setenv(). */
4381 *ret
= bus
->method_call_timeout
= usec
;
4385 *ret
= bus
->method_call_timeout
= BUS_DEFAULT_TIMEOUT
;
4389 _public_
int sd_bus_set_close_on_exit(sd_bus
*bus
, int b
) {
4390 assert_return(bus
, -EINVAL
);
4391 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4393 bus
->close_on_exit
= b
;
4397 _public_
int sd_bus_get_close_on_exit(sd_bus
*bus
) {
4398 assert_return(bus
, -EINVAL
);
4399 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4401 return bus
->close_on_exit
;
4404 _public_
int sd_bus_enqueue_for_read(sd_bus
*bus
, sd_bus_message
*m
) {
4407 assert_return(bus
, -EINVAL
);
4408 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4409 assert_return(m
, -EINVAL
);
4410 assert_return(m
->sealed
, -EINVAL
);
4411 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4413 if (!BUS_IS_OPEN(bus
->state
))
4416 /* Re-enqueue a message for reading. This is primarily useful for PolicyKit-style authentication,
4417 * where we accept a message, then determine we need to interactively authenticate the user, and then
4418 * we want to process the message again. */
4420 r
= bus_rqueue_make_room(bus
);
4424 bus
->rqueue
[bus
->rqueue_size
++] = bus_message_ref_queued(m
, bus
);