1 /* SPDX-License-Identifier: LGPL-2.1+ */
15 #include "alloc-util.h"
16 #include "bus-container.h"
17 #include "bus-control.h"
18 #include "bus-internal.h"
19 #include "bus-kernel.h"
20 #include "bus-label.h"
21 #include "bus-message.h"
22 #include "bus-objects.h"
23 #include "bus-protocol.h"
25 #include "bus-socket.h"
26 #include "bus-track.h"
29 #include "cgroup-util.h"
32 #include "hexdecoct.h"
33 #include "hostname-util.h"
36 #include "parse-util.h"
37 #include "process-util.h"
38 #include "string-util.h"
42 #define log_debug_bus_message(m) \
44 sd_bus_message *_mm = (m); \
45 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", \
46 bus_message_type_to_string(_mm->header->type), \
47 strna(sd_bus_message_get_sender(_mm)), \
48 strna(sd_bus_message_get_destination(_mm)), \
49 strna(sd_bus_message_get_path(_mm)), \
50 strna(sd_bus_message_get_interface(_mm)), \
51 strna(sd_bus_message_get_member(_mm)), \
52 BUS_MESSAGE_COOKIE(_mm), \
54 strna(_mm->root_container.signature), \
55 strna(_mm->error.name), \
56 strna(_mm->error.message)); \
59 static int bus_poll(sd_bus
*bus
, bool need_more
, uint64_t timeout_usec
);
60 static void bus_detach_io_events(sd_bus
*b
);
61 static void bus_detach_inotify_event(sd_bus
*b
);
63 static thread_local sd_bus
*default_system_bus
= NULL
;
64 static thread_local sd_bus
*default_user_bus
= NULL
;
65 static thread_local sd_bus
*default_starter_bus
= NULL
;
67 static sd_bus
**bus_choose_default(int (**bus_open
)(sd_bus
**)) {
70 /* Let's try our best to reuse another cached connection. If
71 * the starter bus type is set, connect via our normal
72 * connection logic, ignoring $DBUS_STARTER_ADDRESS, so that
73 * we can share the connection with the user/system default
76 e
= secure_getenv("DBUS_STARTER_BUS_TYPE");
78 if (streq(e
, "system")) {
80 *bus_open
= sd_bus_open_system
;
81 return &default_system_bus
;
82 } else if (STR_IN_SET(e
, "user", "session")) {
84 *bus_open
= sd_bus_open_user
;
85 return &default_user_bus
;
89 /* No type is specified, so we have not other option than to
90 * use the starter address if it is set. */
91 e
= secure_getenv("DBUS_STARTER_ADDRESS");
94 *bus_open
= sd_bus_open
;
95 return &default_starter_bus
;
98 /* Finally, if nothing is set use the cached connection for
101 if (cg_pid_get_owner_uid(0, NULL
) >= 0) {
103 *bus_open
= sd_bus_open_user
;
104 return &default_user_bus
;
107 *bus_open
= sd_bus_open_system
;
108 return &default_system_bus
;
112 sd_bus
*bus_resolve(sd_bus
*bus
) {
113 switch ((uintptr_t) bus
) {
114 case (uintptr_t) SD_BUS_DEFAULT
:
115 return *(bus_choose_default(NULL
));
116 case (uintptr_t) SD_BUS_DEFAULT_USER
:
117 return default_user_bus
;
118 case (uintptr_t) SD_BUS_DEFAULT_SYSTEM
:
119 return default_system_bus
;
125 void bus_close_io_fds(sd_bus
*b
) {
128 bus_detach_io_events(b
);
130 if (b
->input_fd
!= b
->output_fd
)
131 safe_close(b
->output_fd
);
132 b
->output_fd
= b
->input_fd
= safe_close(b
->input_fd
);
135 void bus_close_inotify_fd(sd_bus
*b
) {
138 bus_detach_inotify_event(b
);
140 b
->inotify_fd
= safe_close(b
->inotify_fd
);
141 b
->inotify_watches
= mfree(b
->inotify_watches
);
142 b
->n_inotify_watches
= 0;
145 static void bus_reset_queues(sd_bus
*b
) {
148 while (b
->rqueue_size
> 0)
149 sd_bus_message_unref(b
->rqueue
[--b
->rqueue_size
]);
151 b
->rqueue
= mfree(b
->rqueue
);
152 b
->rqueue_allocated
= 0;
154 while (b
->wqueue_size
> 0)
155 sd_bus_message_unref(b
->wqueue
[--b
->wqueue_size
]);
157 b
->wqueue
= mfree(b
->wqueue
);
158 b
->wqueue_allocated
= 0;
161 static sd_bus
* bus_free(sd_bus
*b
) {
165 assert(!b
->track_queue
);
168 b
->state
= BUS_CLOSED
;
170 sd_bus_detach_event(b
);
172 while ((s
= b
->slots
)) {
173 /* At this point only floating slots can still be
174 * around, because the non-floating ones keep a
175 * reference to the bus, and we thus couldn't be
176 * destructing right now... We forcibly disconnect the
177 * slots here, so that they still can be referenced by
178 * apps, but are dead. */
181 bus_slot_disconnect(s
, true);
184 if (b
->default_bus_ptr
)
185 *b
->default_bus_ptr
= NULL
;
188 bus_close_inotify_fd(b
);
193 free(b
->unique_name
);
194 free(b
->auth_buffer
);
197 free(b
->cgroup_root
);
198 free(b
->description
);
199 free(b
->patch_sender
);
202 strv_free(b
->exec_argv
);
204 close_many(b
->fds
, b
->n_fds
);
209 ordered_hashmap_free_free(b
->reply_callbacks
);
210 prioq_free(b
->reply_callbacks_prioq
);
212 assert(b
->match_callbacks
.type
== BUS_MATCH_ROOT
);
213 bus_match_free(&b
->match_callbacks
);
215 hashmap_free_free(b
->vtable_methods
);
216 hashmap_free_free(b
->vtable_properties
);
218 assert(hashmap_isempty(b
->nodes
));
219 hashmap_free(b
->nodes
);
223 assert_se(pthread_mutex_destroy(&b
->memfd_cache_mutex
) == 0);
228 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_bus
*, bus_free
);
230 _public_
int sd_bus_new(sd_bus
**ret
) {
231 _cleanup_free_ sd_bus
*b
= NULL
;
233 assert_return(ret
, -EINVAL
);
239 b
->n_ref
= REFCNT_INIT
;
240 b
->input_fd
= b
->output_fd
= -1;
242 b
->message_version
= 1;
243 b
->creds_mask
|= SD_BUS_CREDS_WELL_KNOWN_NAMES
|SD_BUS_CREDS_UNIQUE_NAME
;
245 b
->original_pid
= getpid_cached();
246 b
->n_groups
= (size_t) -1;
248 assert_se(pthread_mutex_init(&b
->memfd_cache_mutex
, NULL
) == 0);
250 /* We guarantee that wqueue always has space for at least one entry */
251 if (!GREEDY_REALLOC(b
->wqueue
, b
->wqueue_allocated
, 1))
258 _public_
int sd_bus_set_address(sd_bus
*bus
, const char *address
) {
259 assert_return(bus
, -EINVAL
);
260 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
261 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
262 assert_return(address
, -EINVAL
);
263 assert_return(!bus_pid_changed(bus
), -ECHILD
);
265 return free_and_strdup(&bus
->address
, address
);
268 _public_
int sd_bus_set_fd(sd_bus
*bus
, int input_fd
, int output_fd
) {
269 assert_return(bus
, -EINVAL
);
270 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
271 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
272 assert_return(input_fd
>= 0, -EBADF
);
273 assert_return(output_fd
>= 0, -EBADF
);
274 assert_return(!bus_pid_changed(bus
), -ECHILD
);
276 bus
->input_fd
= input_fd
;
277 bus
->output_fd
= output_fd
;
281 _public_
int sd_bus_set_exec(sd_bus
*bus
, const char *path
, char *const argv
[]) {
282 _cleanup_strv_free_
char **a
= NULL
;
285 assert_return(bus
, -EINVAL
);
286 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
287 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
288 assert_return(path
, -EINVAL
);
289 assert_return(!strv_isempty(argv
), -EINVAL
);
290 assert_return(!bus_pid_changed(bus
), -ECHILD
);
296 r
= free_and_strdup(&bus
->exec_path
, path
);
300 return strv_free_and_replace(bus
->exec_argv
, a
);
303 _public_
int sd_bus_set_bus_client(sd_bus
*bus
, int b
) {
304 assert_return(bus
, -EINVAL
);
305 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
306 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
307 assert_return(!bus
->patch_sender
, -EPERM
);
308 assert_return(!bus_pid_changed(bus
), -ECHILD
);
310 bus
->bus_client
= !!b
;
314 _public_
int sd_bus_set_monitor(sd_bus
*bus
, int b
) {
315 assert_return(bus
, -EINVAL
);
316 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
317 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
318 assert_return(!bus_pid_changed(bus
), -ECHILD
);
320 bus
->is_monitor
= !!b
;
324 _public_
int sd_bus_negotiate_fds(sd_bus
*bus
, int b
) {
325 assert_return(bus
, -EINVAL
);
326 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
327 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
328 assert_return(!bus_pid_changed(bus
), -ECHILD
);
330 bus
->accept_fd
= !!b
;
334 _public_
int sd_bus_negotiate_timestamp(sd_bus
*bus
, int b
) {
335 assert_return(bus
, -EINVAL
);
336 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
337 assert_return(!IN_SET(bus
->state
, BUS_CLOSING
, BUS_CLOSED
), -EPERM
);
338 assert_return(!bus_pid_changed(bus
), -ECHILD
);
340 /* This is not actually supported by any of our transports these days, but we do honour it for synthetic
341 * replies, and maybe one day classic D-Bus learns this too */
342 bus
->attach_timestamp
= !!b
;
347 _public_
int sd_bus_negotiate_creds(sd_bus
*bus
, int b
, uint64_t mask
) {
348 assert_return(bus
, -EINVAL
);
349 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
350 assert_return(mask
<= _SD_BUS_CREDS_ALL
, -EINVAL
);
351 assert_return(!IN_SET(bus
->state
, BUS_CLOSING
, BUS_CLOSED
), -EPERM
);
352 assert_return(!bus_pid_changed(bus
), -ECHILD
);
354 SET_FLAG(bus
->creds_mask
, mask
, b
);
356 /* The well knowns we need unconditionally, so that matches can work */
357 bus
->creds_mask
|= SD_BUS_CREDS_WELL_KNOWN_NAMES
|SD_BUS_CREDS_UNIQUE_NAME
;
362 _public_
int sd_bus_set_server(sd_bus
*bus
, int b
, sd_id128_t server_id
) {
363 assert_return(bus
, -EINVAL
);
364 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
365 assert_return(b
|| sd_id128_equal(server_id
, SD_ID128_NULL
), -EINVAL
);
366 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
367 assert_return(!bus_pid_changed(bus
), -ECHILD
);
369 bus
->is_server
= !!b
;
370 bus
->server_id
= server_id
;
374 _public_
int sd_bus_set_anonymous(sd_bus
*bus
, int b
) {
375 assert_return(bus
, -EINVAL
);
376 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
377 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
378 assert_return(!bus_pid_changed(bus
), -ECHILD
);
380 bus
->anonymous_auth
= !!b
;
384 _public_
int sd_bus_set_trusted(sd_bus
*bus
, int b
) {
385 assert_return(bus
, -EINVAL
);
386 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
387 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
388 assert_return(!bus_pid_changed(bus
), -ECHILD
);
394 _public_
int sd_bus_set_description(sd_bus
*bus
, const char *description
) {
395 assert_return(bus
, -EINVAL
);
396 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
397 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
398 assert_return(!bus_pid_changed(bus
), -ECHILD
);
400 return free_and_strdup(&bus
->description
, description
);
403 _public_
int sd_bus_set_allow_interactive_authorization(sd_bus
*bus
, int b
) {
404 assert_return(bus
, -EINVAL
);
405 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
406 assert_return(!bus_pid_changed(bus
), -ECHILD
);
408 bus
->allow_interactive_authorization
= !!b
;
412 _public_
int sd_bus_get_allow_interactive_authorization(sd_bus
*bus
) {
413 assert_return(bus
, -EINVAL
);
414 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
415 assert_return(!bus_pid_changed(bus
), -ECHILD
);
417 return bus
->allow_interactive_authorization
;
420 _public_
int sd_bus_set_watch_bind(sd_bus
*bus
, int b
) {
421 assert_return(bus
, -EINVAL
);
422 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
423 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
424 assert_return(!bus_pid_changed(bus
), -ECHILD
);
426 bus
->watch_bind
= !!b
;
430 _public_
int sd_bus_get_watch_bind(sd_bus
*bus
) {
431 assert_return(bus
, -EINVAL
);
432 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
433 assert_return(!bus_pid_changed(bus
), -ECHILD
);
435 return bus
->watch_bind
;
438 _public_
int sd_bus_set_connected_signal(sd_bus
*bus
, int b
) {
439 assert_return(bus
, -EINVAL
);
440 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
441 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
442 assert_return(!bus_pid_changed(bus
), -ECHILD
);
444 bus
->connected_signal
= !!b
;
448 _public_
int sd_bus_get_connected_signal(sd_bus
*bus
) {
449 assert_return(bus
, -EINVAL
);
450 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
451 assert_return(!bus_pid_changed(bus
), -ECHILD
);
453 return bus
->connected_signal
;
456 static int synthesize_connected_signal(sd_bus
*bus
) {
457 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
462 /* If enabled, synthesizes a local "Connected" signal mirroring the local "Disconnected" signal. This is called
463 * whenever we fully established a connection, i.e. after the authorization phase, and after receiving the
464 * Hello() reply. Or in other words, whenver we enter BUS_RUNNING state.
466 * This is useful so that clients can start doing stuff whenver the connection is fully established in a way
467 * that works independently from whether we connected to a full bus or just a direct connection. */
469 if (!bus
->connected_signal
)
472 r
= sd_bus_message_new_signal(
475 "/org/freedesktop/DBus/Local",
476 "org.freedesktop.DBus.Local",
481 bus_message_set_sender_local(bus
, m
);
483 r
= bus_seal_synthetic_message(bus
, m
);
487 r
= bus_rqueue_make_room(bus
);
491 /* Insert at the very front */
492 memmove(bus
->rqueue
+ 1, bus
->rqueue
, sizeof(sd_bus_message
*) * bus
->rqueue_size
);
493 bus
->rqueue
[0] = TAKE_PTR(m
);
499 void bus_set_state(sd_bus
*bus
, enum bus_state state
) {
501 static const char * const table
[_BUS_STATE_MAX
] = {
502 [BUS_UNSET
] = "UNSET",
503 [BUS_WATCH_BIND
] = "WATCH_BIND",
504 [BUS_OPENING
] = "OPENING",
505 [BUS_AUTHENTICATING
] = "AUTHENTICATING",
506 [BUS_HELLO
] = "HELLO",
507 [BUS_RUNNING
] = "RUNNING",
508 [BUS_CLOSING
] = "CLOSING",
509 [BUS_CLOSED
] = "CLOSED",
513 assert(state
< _BUS_STATE_MAX
);
515 if (state
== bus
->state
)
518 log_debug("Bus %s: changing state %s → %s", strna(bus
->description
), table
[bus
->state
], table
[state
]);
522 static int hello_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
530 assert(IN_SET(bus
->state
, BUS_HELLO
, BUS_CLOSING
));
532 r
= sd_bus_message_get_errno(reply
);
536 r
= sd_bus_message_read(reply
, "s", &s
);
540 if (!service_name_is_valid(s
) || s
[0] != ':')
543 r
= free_and_strdup(&bus
->unique_name
, s
);
547 if (bus
->state
== BUS_HELLO
) {
548 bus_set_state(bus
, BUS_RUNNING
);
550 r
= synthesize_connected_signal(bus
);
558 static int bus_send_hello(sd_bus
*bus
) {
559 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
564 if (!bus
->bus_client
)
567 r
= sd_bus_message_new_method_call(
570 "org.freedesktop.DBus",
571 "/org/freedesktop/DBus",
572 "org.freedesktop.DBus",
577 return sd_bus_call_async(bus
, NULL
, m
, hello_callback
, NULL
, 0);
580 int bus_start_running(sd_bus
*bus
) {
581 struct reply_callback
*c
;
587 assert(bus
->state
< BUS_HELLO
);
589 /* We start all method call timeouts when we enter BUS_HELLO or BUS_RUNNING mode. At this point let's convert
590 * all relative to absolute timestamps. Note that we do not reshuffle the reply callback priority queue since
591 * adding a fixed value to all entries should not alter the internal order. */
593 n
= now(CLOCK_MONOTONIC
);
594 ORDERED_HASHMAP_FOREACH(c
, bus
->reply_callbacks
, i
) {
595 if (c
->timeout_usec
== 0)
598 c
->timeout_usec
= usec_add(n
, c
->timeout_usec
);
601 if (bus
->bus_client
) {
602 bus_set_state(bus
, BUS_HELLO
);
606 bus_set_state(bus
, BUS_RUNNING
);
608 r
= synthesize_connected_signal(bus
);
615 static int parse_address_key(const char **p
, const char *key
, char **value
) {
616 size_t l
, n
= 0, allocated
= 0;
617 _cleanup_free_
char *r
= NULL
;
626 if (strncmp(*p
, key
, l
) != 0)
639 while (!IN_SET(*a
, ';', ',', 0)) {
653 c
= (char) ((x
<< 4) | y
);
660 if (!GREEDY_REALLOC(r
, allocated
, n
+ 2))
678 free_and_replace(*value
, r
);
683 static void skip_address_key(const char **p
) {
687 *p
+= strcspn(*p
, ",");
693 static int parse_unix_address(sd_bus
*b
, const char **p
, char **guid
) {
694 _cleanup_free_
char *path
= NULL
, *abstract
= NULL
;
703 while (!IN_SET(**p
, 0, ';')) {
704 r
= parse_address_key(p
, "guid", guid
);
710 r
= parse_address_key(p
, "path", &path
);
716 r
= parse_address_key(p
, "abstract", &abstract
);
725 if (!path
&& !abstract
)
728 if (path
&& abstract
)
733 if (l
> sizeof(b
->sockaddr
.un
.sun_path
))
736 b
->sockaddr
.un
.sun_family
= AF_UNIX
;
737 strncpy(b
->sockaddr
.un
.sun_path
, path
, sizeof(b
->sockaddr
.un
.sun_path
));
738 b
->sockaddr_size
= offsetof(struct sockaddr_un
, sun_path
) + l
;
739 } else if (abstract
) {
740 l
= strlen(abstract
);
741 if (l
> sizeof(b
->sockaddr
.un
.sun_path
) - 1)
744 b
->sockaddr
.un
.sun_family
= AF_UNIX
;
745 b
->sockaddr
.un
.sun_path
[0] = 0;
746 strncpy(b
->sockaddr
.un
.sun_path
+1, abstract
, sizeof(b
->sockaddr
.un
.sun_path
)-1);
747 b
->sockaddr_size
= offsetof(struct sockaddr_un
, sun_path
) + 1 + l
;
755 static int parse_tcp_address(sd_bus
*b
, const char **p
, char **guid
) {
756 _cleanup_free_
char *host
= NULL
, *port
= NULL
, *family
= NULL
;
758 struct addrinfo
*result
, hints
= {
759 .ai_socktype
= SOCK_STREAM
,
760 .ai_flags
= AI_ADDRCONFIG
,
768 while (!IN_SET(**p
, 0, ';')) {
769 r
= parse_address_key(p
, "guid", guid
);
775 r
= parse_address_key(p
, "host", &host
);
781 r
= parse_address_key(p
, "port", &port
);
787 r
= parse_address_key(p
, "family", &family
);
800 if (streq(family
, "ipv4"))
801 hints
.ai_family
= AF_INET
;
802 else if (streq(family
, "ipv6"))
803 hints
.ai_family
= AF_INET6
;
808 r
= getaddrinfo(host
, port
, &hints
, &result
);
812 return -EADDRNOTAVAIL
;
814 memcpy(&b
->sockaddr
, result
->ai_addr
, result
->ai_addrlen
);
815 b
->sockaddr_size
= result
->ai_addrlen
;
817 freeaddrinfo(result
);
824 static int parse_exec_address(sd_bus
*b
, const char **p
, char **guid
) {
826 unsigned n_argv
= 0, j
;
828 size_t allocated
= 0;
836 while (!IN_SET(**p
, 0, ';')) {
837 r
= parse_address_key(p
, "guid", guid
);
843 r
= parse_address_key(p
, "path", &path
);
849 if (startswith(*p
, "argv")) {
853 ul
= strtoul(*p
+ 4, (char**) p
, 10);
854 if (errno
> 0 || **p
!= '=' || ul
> 256) {
862 if (!GREEDY_REALLOC0(argv
, allocated
, ul
+ 2)) {
870 r
= parse_address_key(p
, NULL
, argv
+ ul
);
885 /* Make sure there are no holes in the array, with the
886 * exception of argv[0] */
887 for (j
= 1; j
< n_argv
; j
++)
893 if (argv
&& argv
[0] == NULL
) {
894 argv
[0] = strdup(path
);
909 for (j
= 0; j
< n_argv
; j
++)
917 static int parse_container_unix_address(sd_bus
*b
, const char **p
, char **guid
) {
918 _cleanup_free_
char *machine
= NULL
, *pid
= NULL
;
926 while (!IN_SET(**p
, 0, ';')) {
927 r
= parse_address_key(p
, "guid", guid
);
933 r
= parse_address_key(p
, "machine", &machine
);
939 r
= parse_address_key(p
, "pid", &pid
);
948 if (!machine
== !pid
)
952 if (!streq(machine
, ".host") && !machine_name_is_valid(machine
))
955 free_and_replace(b
->machine
, machine
);
957 b
->machine
= mfree(b
->machine
);
961 r
= parse_pid(pid
, &b
->nspid
);
967 b
->sockaddr
.un
.sun_family
= AF_UNIX
;
968 /* Note that we use the old /var/run prefix here, to increase compatibility with really old containers */
969 strncpy(b
->sockaddr
.un
.sun_path
, "/var/run/dbus/system_bus_socket", sizeof(b
->sockaddr
.un
.sun_path
));
970 b
->sockaddr_size
= SOCKADDR_UN_LEN(b
->sockaddr
.un
);
976 static void bus_reset_parsed_address(sd_bus
*b
) {
980 b
->sockaddr_size
= 0;
981 b
->exec_argv
= strv_free(b
->exec_argv
);
982 b
->exec_path
= mfree(b
->exec_path
);
983 b
->server_id
= SD_ID128_NULL
;
984 b
->machine
= mfree(b
->machine
);
988 static int bus_parse_next_address(sd_bus
*b
) {
989 _cleanup_free_
char *guid
= NULL
;
997 if (b
->address
[b
->address_index
] == 0)
1000 bus_reset_parsed_address(b
);
1002 a
= b
->address
+ b
->address_index
;
1011 if (startswith(a
, "unix:")) {
1014 r
= parse_unix_address(b
, &a
, &guid
);
1019 } else if (startswith(a
, "tcp:")) {
1022 r
= parse_tcp_address(b
, &a
, &guid
);
1028 } else if (startswith(a
, "unixexec:")) {
1031 r
= parse_exec_address(b
, &a
, &guid
);
1037 } else if (startswith(a
, "x-machine-unix:")) {
1040 r
= parse_container_unix_address(b
, &a
, &guid
);
1053 r
= sd_id128_from_string(guid
, &b
->server_id
);
1058 b
->address_index
= a
- b
->address
;
1062 static void bus_kill_exec(sd_bus
*bus
) {
1063 if (pid_is_valid(bus
->busexec_pid
) > 0) {
1064 sigterm_wait(bus
->busexec_pid
);
1065 bus
->busexec_pid
= 0;
1069 static int bus_start_address(sd_bus
*b
) {
1075 bus_close_io_fds(b
);
1076 bus_close_inotify_fd(b
);
1080 /* If you provide multiple different bus-addresses, we
1081 * try all of them in order and use the first one that
1085 r
= bus_socket_exec(b
);
1086 else if ((b
->nspid
> 0 || b
->machine
) && b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
)
1087 r
= bus_container_connect_socket(b
);
1088 else if (b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
)
1089 r
= bus_socket_connect(b
);
1096 q
= bus_attach_io_events(b
);
1100 q
= bus_attach_inotify_event(b
);
1107 b
->last_connect_error
= -r
;
1110 r
= bus_parse_next_address(b
);
1114 return b
->last_connect_error
> 0 ? -b
->last_connect_error
: -ECONNREFUSED
;
1118 int bus_next_address(sd_bus
*b
) {
1121 bus_reset_parsed_address(b
);
1122 return bus_start_address(b
);
1125 static int bus_start_fd(sd_bus
*b
) {
1130 assert(b
->input_fd
>= 0);
1131 assert(b
->output_fd
>= 0);
1133 r
= fd_nonblock(b
->input_fd
, true);
1137 r
= fd_cloexec(b
->input_fd
, true);
1141 if (b
->input_fd
!= b
->output_fd
) {
1142 r
= fd_nonblock(b
->output_fd
, true);
1146 r
= fd_cloexec(b
->output_fd
, true);
1151 if (fstat(b
->input_fd
, &st
) < 0)
1154 return bus_socket_take_fd(b
);
1157 _public_
int sd_bus_start(sd_bus
*bus
) {
1160 assert_return(bus
, -EINVAL
);
1161 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1162 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
1163 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1165 bus_set_state(bus
, BUS_OPENING
);
1167 if (bus
->is_server
&& bus
->bus_client
)
1170 if (bus
->input_fd
>= 0)
1171 r
= bus_start_fd(bus
);
1172 else if (bus
->address
|| bus
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
|| bus
->exec_path
|| bus
->machine
)
1173 r
= bus_start_address(bus
);
1182 return bus_send_hello(bus
);
1185 _public_
int sd_bus_open_with_description(sd_bus
**ret
, const char *description
) {
1187 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1190 assert_return(ret
, -EINVAL
);
1192 /* Let's connect to the starter bus if it is set, and
1193 * otherwise to the bus that is appropropriate for the scope
1194 * we are running in */
1196 e
= secure_getenv("DBUS_STARTER_BUS_TYPE");
1198 if (streq(e
, "system"))
1199 return sd_bus_open_system_with_description(ret
, description
);
1200 else if (STR_IN_SET(e
, "session", "user"))
1201 return sd_bus_open_user_with_description(ret
, description
);
1204 e
= secure_getenv("DBUS_STARTER_ADDRESS");
1206 if (cg_pid_get_owner_uid(0, NULL
) >= 0)
1207 return sd_bus_open_user_with_description(ret
, description
);
1209 return sd_bus_open_system_with_description(ret
, description
);
1216 r
= sd_bus_set_address(b
, e
);
1220 b
->bus_client
= true;
1222 /* We don't know whether the bus is trusted or not, so better
1223 * be safe, and authenticate everything */
1225 b
->is_local
= false;
1226 b
->creds_mask
|= SD_BUS_CREDS_UID
| SD_BUS_CREDS_EUID
| SD_BUS_CREDS_EFFECTIVE_CAPS
;
1228 r
= sd_bus_start(b
);
1236 _public_
int sd_bus_open(sd_bus
**ret
) {
1237 return sd_bus_open_with_description(ret
, NULL
);
1240 int bus_set_address_system(sd_bus
*b
) {
1244 e
= secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1246 return sd_bus_set_address(b
, e
);
1248 return sd_bus_set_address(b
, DEFAULT_SYSTEM_BUS_ADDRESS
);
1251 _public_
int sd_bus_open_system_with_description(sd_bus
**ret
, const char *description
) {
1252 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1255 assert_return(ret
, -EINVAL
);
1262 r
= sd_bus_set_description(b
, description
);
1267 r
= bus_set_address_system(b
);
1271 b
->bus_client
= true;
1272 b
->is_system
= true;
1274 /* Let's do per-method access control on the system bus. We
1275 * need the caller's UID and capability set for that. */
1277 b
->creds_mask
|= SD_BUS_CREDS_UID
| SD_BUS_CREDS_EUID
| SD_BUS_CREDS_EFFECTIVE_CAPS
;
1280 r
= sd_bus_start(b
);
1288 _public_
int sd_bus_open_system(sd_bus
**ret
) {
1289 return sd_bus_open_system_with_description(ret
, NULL
);
1292 int bus_set_address_user(sd_bus
*b
) {
1294 _cleanup_free_
char *ee
= NULL
, *s
= NULL
;
1298 e
= secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1300 return sd_bus_set_address(b
, e
);
1302 e
= secure_getenv("XDG_RUNTIME_DIR");
1306 ee
= bus_address_escape(e
);
1310 if (asprintf(&s
, DEFAULT_USER_BUS_ADDRESS_FMT
, ee
) < 0)
1313 b
->address
= TAKE_PTR(s
);
1318 _public_
int sd_bus_open_user_with_description(sd_bus
**ret
, const char *description
) {
1319 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1322 assert_return(ret
, -EINVAL
);
1329 r
= sd_bus_set_description(b
, description
);
1334 r
= bus_set_address_user(b
);
1338 b
->bus_client
= true;
1341 /* We don't do any per-method access control on the user
1346 r
= sd_bus_start(b
);
1354 _public_
int sd_bus_open_user(sd_bus
**ret
) {
1355 return sd_bus_open_user_with_description(ret
, NULL
);
1358 int bus_set_address_system_remote(sd_bus
*b
, const char *host
) {
1359 _cleanup_free_
char *e
= NULL
;
1360 char *m
= NULL
, *c
= NULL
, *a
, *rbracket
= NULL
, *p
= NULL
;
1365 /* Skip ":"s in ipv6 addresses */
1369 rbracket
= strchr(host
, ']');
1372 t
= strndupa(host
+ 1, rbracket
- host
- 1);
1373 e
= bus_address_escape(t
);
1376 } else if ((a
= strchr(host
, '@'))) {
1377 if (*(a
+ 1) == '[') {
1378 _cleanup_free_
char *t
= NULL
;
1380 rbracket
= strchr(a
+ 1, ']');
1383 t
= new0(char, strlen(host
));
1386 strncat(t
, host
, a
- host
+ 1);
1387 strncat(t
, a
+ 2, rbracket
- a
- 2);
1388 e
= bus_address_escape(t
);
1391 } else if (*(a
+ 1) == '\0' || strchr(a
+ 1, '@'))
1395 /* Let's see if a port was given */
1396 m
= strchr(rbracket
? rbracket
+ 1 : host
, ':');
1399 bool got_forward_slash
= false;
1405 p
= strndupa(p
, t
- p
);
1406 got_forward_slash
= true;
1409 if (!in_charset(p
, "0123456789") || *p
== '\0') {
1410 if (!machine_name_is_valid(p
) || got_forward_slash
)
1415 goto interpret_port_as_machine_old_syntax
;
1420 /* Let's see if a machine was given */
1421 m
= strchr(rbracket
? rbracket
+ 1 : host
, '/');
1424 interpret_port_as_machine_old_syntax
:
1425 /* Let's make sure this is not a port of some kind,
1426 * and is a valid machine name. */
1427 if (!in_charset(m
, "0123456789") && machine_name_is_valid(m
))
1428 c
= strjoina(",argv", p
? "7" : "5", "=--machine=", m
);
1434 t
= strndupa(host
, strcspn(host
, ":/"));
1436 e
= bus_address_escape(t
);
1441 a
= strjoin("unixexec:path=ssh,argv1=-xT", p
? ",argv2=-p,argv3=" : "", strempty(p
),
1442 ",argv", p
? "4" : "2", "=--,argv", p
? "5" : "3", "=", e
,
1443 ",argv", p
? "6" : "4", "=systemd-stdio-bridge", c
);
1447 return free_and_replace(b
->address
, a
);
1450 _public_
int sd_bus_open_system_remote(sd_bus
**ret
, const char *host
) {
1451 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1454 assert_return(host
, -EINVAL
);
1455 assert_return(ret
, -EINVAL
);
1461 r
= bus_set_address_system_remote(b
, host
);
1465 b
->bus_client
= true;
1467 b
->is_system
= true;
1468 b
->is_local
= false;
1470 r
= sd_bus_start(b
);
1478 int bus_set_address_system_machine(sd_bus
*b
, const char *machine
) {
1479 _cleanup_free_
char *e
= NULL
;
1485 e
= bus_address_escape(machine
);
1489 a
= strjoin("x-machine-unix:machine=", e
);
1493 return free_and_replace(b
->address
, a
);
1496 _public_
int sd_bus_open_system_machine(sd_bus
**ret
, const char *machine
) {
1497 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1500 assert_return(machine
, -EINVAL
);
1501 assert_return(ret
, -EINVAL
);
1502 assert_return(streq(machine
, ".host") || machine_name_is_valid(machine
), -EINVAL
);
1508 r
= bus_set_address_system_machine(b
, machine
);
1512 b
->bus_client
= true;
1514 b
->is_system
= true;
1515 b
->is_local
= false;
1517 r
= sd_bus_start(b
);
1525 _public_
void sd_bus_close(sd_bus
*bus
) {
1528 if (bus
->state
== BUS_CLOSED
)
1530 if (bus_pid_changed(bus
))
1533 /* Don't leave ssh hanging around */
1536 bus_set_state(bus
, BUS_CLOSED
);
1538 sd_bus_detach_event(bus
);
1540 /* Drop all queued messages so that they drop references to
1541 * the bus object and the bus may be freed */
1542 bus_reset_queues(bus
);
1544 bus_close_io_fds(bus
);
1545 bus_close_inotify_fd(bus
);
1548 _public_ sd_bus
* sd_bus_flush_close_unref(sd_bus
*bus
) {
1552 /* Have to do this before flush() to prevent hang */
1558 return sd_bus_unref(bus
);
1561 void bus_enter_closing(sd_bus
*bus
) {
1564 if (!IN_SET(bus
->state
, BUS_WATCH_BIND
, BUS_OPENING
, BUS_AUTHENTICATING
, BUS_HELLO
, BUS_RUNNING
))
1567 bus_set_state(bus
, BUS_CLOSING
);
1570 DEFINE_PUBLIC_ATOMIC_REF_UNREF_FUNC(sd_bus
, sd_bus
, bus_free
);
1572 _public_
int sd_bus_is_open(sd_bus
*bus
) {
1573 assert_return(bus
, -EINVAL
);
1574 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1575 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1577 return BUS_IS_OPEN(bus
->state
);
1580 _public_
int sd_bus_is_ready(sd_bus
*bus
) {
1581 assert_return(bus
, -EINVAL
);
1582 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1583 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1585 return bus
->state
== BUS_RUNNING
;
1588 _public_
int sd_bus_can_send(sd_bus
*bus
, char type
) {
1591 assert_return(bus
, -EINVAL
);
1592 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1593 assert_return(bus
->state
!= BUS_UNSET
, -ENOTCONN
);
1594 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1596 if (bus
->is_monitor
)
1599 if (type
== SD_BUS_TYPE_UNIX_FD
) {
1600 if (!bus
->accept_fd
)
1603 r
= bus_ensure_running(bus
);
1607 return bus
->can_fds
;
1610 return bus_type_is_valid(type
);
1613 _public_
int sd_bus_get_bus_id(sd_bus
*bus
, sd_id128_t
*id
) {
1616 assert_return(bus
, -EINVAL
);
1617 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1618 assert_return(id
, -EINVAL
);
1619 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1621 r
= bus_ensure_running(bus
);
1625 *id
= bus
->server_id
;
1629 static int bus_seal_message(sd_bus
*b
, sd_bus_message
*m
, usec_t timeout
) {
1636 /* If we copy the same message to multiple
1637 * destinations, avoid using the same cookie
1639 b
->cookie
= MAX(b
->cookie
, BUS_MESSAGE_COOKIE(m
));
1644 r
= sd_bus_get_method_call_timeout(b
, &timeout
);
1649 if (!m
->sender
&& b
->patch_sender
) {
1650 r
= sd_bus_message_set_sender(m
, b
->patch_sender
);
1655 return sd_bus_message_seal(m
, ++b
->cookie
, timeout
);
1658 static int bus_remarshal_message(sd_bus
*b
, sd_bus_message
**m
) {
1659 bool remarshal
= false;
1663 /* wrong packet version */
1664 if (b
->message_version
!= 0 && b
->message_version
!= (*m
)->header
->version
)
1667 /* wrong packet endianness */
1668 if (b
->message_endian
!= 0 && b
->message_endian
!= (*m
)->header
->endian
)
1671 return remarshal
? bus_message_remarshal(b
, m
) : 0;
1674 int bus_seal_synthetic_message(sd_bus
*b
, sd_bus_message
*m
) {
1678 /* Fake some timestamps, if they were requested, and not
1679 * already initialized */
1680 if (b
->attach_timestamp
) {
1681 if (m
->realtime
<= 0)
1682 m
->realtime
= now(CLOCK_REALTIME
);
1684 if (m
->monotonic
<= 0)
1685 m
->monotonic
= now(CLOCK_MONOTONIC
);
1688 /* The bus specification says the serial number cannot be 0,
1689 * hence let's fill something in for synthetic messages. Since
1690 * synthetic messages might have a fake sender and we don't
1691 * want to interfere with the real sender's serial numbers we
1692 * pick a fixed, artificial one. We use (uint32_t) -1 rather
1693 * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
1694 * even though kdbus can do 64bit. */
1695 return sd_bus_message_seal(m
, 0xFFFFFFFFULL
, 0);
1698 static int bus_write_message(sd_bus
*bus
, sd_bus_message
*m
, size_t *idx
) {
1704 r
= bus_socket_write_message(bus
, m
, idx
);
1708 if (*idx
>= BUS_MESSAGE_SIZE(m
))
1709 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",
1710 bus_message_type_to_string(m
->header
->type
),
1711 strna(sd_bus_message_get_sender(m
)),
1712 strna(sd_bus_message_get_destination(m
)),
1713 strna(sd_bus_message_get_path(m
)),
1714 strna(sd_bus_message_get_interface(m
)),
1715 strna(sd_bus_message_get_member(m
)),
1716 BUS_MESSAGE_COOKIE(m
),
1718 strna(m
->root_container
.signature
),
1719 strna(m
->error
.name
),
1720 strna(m
->error
.message
));
1725 static int dispatch_wqueue(sd_bus
*bus
) {
1729 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1731 while (bus
->wqueue_size
> 0) {
1733 r
= bus_write_message(bus
, bus
->wqueue
[0], &bus
->windex
);
1737 /* Didn't do anything this time */
1739 else if (bus
->windex
>= BUS_MESSAGE_SIZE(bus
->wqueue
[0])) {
1740 /* Fully written. Let's drop the entry from
1743 * This isn't particularly optimized, but
1744 * well, this is supposed to be our worst-case
1745 * buffer only, and the socket buffer is
1746 * supposed to be our primary buffer, and if
1747 * it got full, then all bets are off
1751 sd_bus_message_unref(bus
->wqueue
[0]);
1752 memmove(bus
->wqueue
, bus
->wqueue
+ 1, sizeof(sd_bus_message
*) * bus
->wqueue_size
);
1762 static int bus_read_message(sd_bus
*bus
, bool hint_priority
, int64_t priority
) {
1765 return bus_socket_read_message(bus
);
1768 int bus_rqueue_make_room(sd_bus
*bus
) {
1771 if (bus
->rqueue_size
>= BUS_RQUEUE_MAX
)
1774 if (!GREEDY_REALLOC(bus
->rqueue
, bus
->rqueue_allocated
, bus
->rqueue_size
+ 1))
1780 static int dispatch_rqueue(sd_bus
*bus
, bool hint_priority
, int64_t priority
, sd_bus_message
**m
) {
1785 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1787 /* Note that the priority logic is only available on kdbus,
1788 * where the rqueue is unused. We check the rqueue here
1789 * anyway, because it's simple... */
1792 if (bus
->rqueue_size
> 0) {
1793 /* Dispatch a queued message */
1795 *m
= bus
->rqueue
[0];
1797 memmove(bus
->rqueue
, bus
->rqueue
+ 1, sizeof(sd_bus_message
*) * bus
->rqueue_size
);
1801 /* Try to read a new message */
1802 r
= bus_read_message(bus
, hint_priority
, priority
);
1812 _public_
int sd_bus_send(sd_bus
*bus
, sd_bus_message
*_m
, uint64_t *cookie
) {
1813 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= sd_bus_message_ref(_m
);
1816 assert_return(m
, -EINVAL
);
1821 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1823 if (!BUS_IS_OPEN(bus
->state
))
1827 r
= sd_bus_can_send(bus
, SD_BUS_TYPE_UNIX_FD
);
1834 /* If the cookie number isn't kept, then we know that no reply
1836 if (!cookie
&& !m
->sealed
)
1837 m
->header
->flags
|= BUS_MESSAGE_NO_REPLY_EXPECTED
;
1839 r
= bus_seal_message(bus
, m
, 0);
1843 /* Remarshall if we have to. This will possibly unref the
1844 * message and place a replacement in m */
1845 r
= bus_remarshal_message(bus
, &m
);
1849 /* If this is a reply and no reply was requested, then let's
1850 * suppress this, if we can */
1854 if (IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
) && bus
->wqueue_size
<= 0) {
1857 r
= bus_write_message(bus
, m
, &idx
);
1859 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
1860 bus_enter_closing(bus
);
1867 if (idx
< BUS_MESSAGE_SIZE(m
)) {
1868 /* Wasn't fully written. So let's remember how
1869 * much was written. Note that the first entry
1870 * of the wqueue array is always allocated so
1871 * that we always can remember how much was
1873 bus
->wqueue
[0] = sd_bus_message_ref(m
);
1874 bus
->wqueue_size
= 1;
1879 /* Just append it to the queue. */
1881 if (bus
->wqueue_size
>= BUS_WQUEUE_MAX
)
1884 if (!GREEDY_REALLOC(bus
->wqueue
, bus
->wqueue_allocated
, bus
->wqueue_size
+ 1))
1887 bus
->wqueue
[bus
->wqueue_size
++] = sd_bus_message_ref(m
);
1892 *cookie
= BUS_MESSAGE_COOKIE(m
);
1897 _public_
int sd_bus_send_to(sd_bus
*bus
, sd_bus_message
*m
, const char *destination
, uint64_t *cookie
) {
1900 assert_return(m
, -EINVAL
);
1905 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1907 if (!BUS_IS_OPEN(bus
->state
))
1910 if (!streq_ptr(m
->destination
, destination
)) {
1915 r
= sd_bus_message_set_destination(m
, destination
);
1920 return sd_bus_send(bus
, m
, cookie
);
1923 static usec_t
calc_elapse(sd_bus
*bus
, uint64_t usec
) {
1926 if (usec
== (uint64_t) -1)
1929 /* We start all timeouts the instant we enter BUS_HELLO/BUS_RUNNING state, so that the don't run in parallel
1930 * with any connection setup states. Hence, if a method callback is started earlier than that we just store the
1931 * relative timestamp, and afterwards the absolute one. */
1933 if (IN_SET(bus
->state
, BUS_WATCH_BIND
, BUS_OPENING
, BUS_AUTHENTICATING
))
1936 return now(CLOCK_MONOTONIC
) + usec
;
1939 static int timeout_compare(const void *a
, const void *b
) {
1940 const struct reply_callback
*x
= a
, *y
= b
;
1942 if (x
->timeout_usec
!= 0 && y
->timeout_usec
== 0)
1945 if (x
->timeout_usec
== 0 && y
->timeout_usec
!= 0)
1948 return CMP(x
->timeout_usec
, y
->timeout_usec
);
1951 _public_
int sd_bus_call_async(
1955 sd_bus_message_handler_t callback
,
1959 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= sd_bus_message_ref(_m
);
1960 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*s
= NULL
;
1963 assert_return(m
, -EINVAL
);
1964 assert_return(m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
, -EINVAL
);
1965 assert_return(!m
->sealed
|| (!!callback
== !(m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
)), -EINVAL
);
1970 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1972 if (!BUS_IS_OPEN(bus
->state
))
1975 /* If no callback is specified and there's no interest in a slot, then there's no reason to ask for a reply */
1976 if (!callback
&& !slot
&& !m
->sealed
)
1977 m
->header
->flags
|= BUS_MESSAGE_NO_REPLY_EXPECTED
;
1979 r
= ordered_hashmap_ensure_allocated(&bus
->reply_callbacks
, &uint64_hash_ops
);
1983 r
= prioq_ensure_allocated(&bus
->reply_callbacks_prioq
, timeout_compare
);
1987 r
= bus_seal_message(bus
, m
, usec
);
1991 r
= bus_remarshal_message(bus
, &m
);
1995 if (slot
|| callback
) {
1996 s
= bus_slot_allocate(bus
, !slot
, BUS_REPLY_CALLBACK
, sizeof(struct reply_callback
), userdata
);
2000 s
->reply_callback
.callback
= callback
;
2002 s
->reply_callback
.cookie
= BUS_MESSAGE_COOKIE(m
);
2003 r
= ordered_hashmap_put(bus
->reply_callbacks
, &s
->reply_callback
.cookie
, &s
->reply_callback
);
2005 s
->reply_callback
.cookie
= 0;
2009 s
->reply_callback
.timeout_usec
= calc_elapse(bus
, m
->timeout
);
2010 if (s
->reply_callback
.timeout_usec
!= 0) {
2011 r
= prioq_put(bus
->reply_callbacks_prioq
, &s
->reply_callback
, &s
->reply_callback
.prioq_idx
);
2013 s
->reply_callback
.timeout_usec
= 0;
2019 r
= sd_bus_send(bus
, m
, s
? &s
->reply_callback
.cookie
: NULL
);
2030 int bus_ensure_running(sd_bus
*bus
) {
2035 if (IN_SET(bus
->state
, BUS_UNSET
, BUS_CLOSED
, BUS_CLOSING
))
2037 if (bus
->state
== BUS_RUNNING
)
2041 r
= sd_bus_process(bus
, NULL
);
2044 if (bus
->state
== BUS_RUNNING
)
2049 r
= sd_bus_wait(bus
, (uint64_t) -1);
2055 _public_
int sd_bus_call(
2059 sd_bus_error
*error
,
2060 sd_bus_message
**reply
) {
2062 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= sd_bus_message_ref(_m
);
2068 bus_assert_return(m
, -EINVAL
, error
);
2069 bus_assert_return(m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
, -EINVAL
, error
);
2070 bus_assert_return(!(m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
), -EINVAL
, error
);
2071 bus_assert_return(!bus_error_is_dirty(error
), -EINVAL
, error
);
2076 bus_assert_return(!bus_pid_changed(bus
), -ECHILD
, error
);
2078 if (!BUS_IS_OPEN(bus
->state
)) {
2083 r
= bus_ensure_running(bus
);
2087 i
= bus
->rqueue_size
;
2089 r
= bus_seal_message(bus
, m
, usec
);
2093 r
= bus_remarshal_message(bus
, &m
);
2097 r
= sd_bus_send(bus
, m
, &cookie
);
2101 timeout
= calc_elapse(bus
, m
->timeout
);
2106 while (i
< bus
->rqueue_size
) {
2107 sd_bus_message
*incoming
= NULL
;
2109 incoming
= bus
->rqueue
[i
];
2111 if (incoming
->reply_cookie
== cookie
) {
2112 /* Found a match! */
2114 memmove(bus
->rqueue
+ i
, bus
->rqueue
+ i
+ 1, sizeof(sd_bus_message
*) * (bus
->rqueue_size
- i
- 1));
2116 log_debug_bus_message(incoming
);
2118 if (incoming
->header
->type
== SD_BUS_MESSAGE_METHOD_RETURN
) {
2120 if (incoming
->n_fds
<= 0 || bus
->accept_fd
) {
2124 sd_bus_message_unref(incoming
);
2129 r
= sd_bus_error_setf(error
, SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Reply message contained file descriptors which I couldn't accept. Sorry.");
2130 sd_bus_message_unref(incoming
);
2133 } else if (incoming
->header
->type
== SD_BUS_MESSAGE_METHOD_ERROR
) {
2134 r
= sd_bus_error_copy(error
, &incoming
->error
);
2135 sd_bus_message_unref(incoming
);
2142 } else if (BUS_MESSAGE_COOKIE(incoming
) == cookie
&&
2145 streq(bus
->unique_name
, incoming
->sender
)) {
2147 memmove(bus
->rqueue
+ i
, bus
->rqueue
+ i
+ 1, sizeof(sd_bus_message
*) * (bus
->rqueue_size
- i
- 1));
2150 /* Our own message? Somebody is trying
2151 * to send its own client a message,
2152 * let's not dead-lock, let's fail
2155 sd_bus_message_unref(incoming
);
2160 /* Try to read more, right-away */
2164 r
= bus_read_message(bus
, false, 0);
2166 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
2167 bus_enter_closing(bus
);
2179 n
= now(CLOCK_MONOTONIC
);
2187 left
= (uint64_t) -1;
2189 r
= bus_poll(bus
, true, left
);
2197 r
= dispatch_wqueue(bus
);
2199 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
2200 bus_enter_closing(bus
);
2209 return sd_bus_error_set_errno(error
, r
);
2212 _public_
int sd_bus_get_fd(sd_bus
*bus
) {
2214 assert_return(bus
, -EINVAL
);
2215 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2216 assert_return(bus
->input_fd
== bus
->output_fd
, -EPERM
);
2217 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2219 if (bus
->state
== BUS_CLOSED
)
2222 if (bus
->inotify_fd
>= 0)
2223 return bus
->inotify_fd
;
2225 if (bus
->input_fd
>= 0)
2226 return bus
->input_fd
;
2231 _public_
int sd_bus_get_events(sd_bus
*bus
) {
2234 assert_return(bus
, -EINVAL
);
2235 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2236 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2238 switch (bus
->state
) {
2244 case BUS_WATCH_BIND
:
2252 case BUS_AUTHENTICATING
:
2253 if (bus_socket_auth_needs_write(bus
))
2261 if (bus
->rqueue_size
<= 0)
2263 if (bus
->wqueue_size
> 0)
2271 assert_not_reached("Unknown state");
2277 _public_
int sd_bus_get_timeout(sd_bus
*bus
, uint64_t *timeout_usec
) {
2278 struct reply_callback
*c
;
2280 assert_return(bus
, -EINVAL
);
2281 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2282 assert_return(timeout_usec
, -EINVAL
);
2283 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2285 if (!BUS_IS_OPEN(bus
->state
) && bus
->state
!= BUS_CLOSING
)
2288 if (bus
->track_queue
) {
2293 switch (bus
->state
) {
2295 case BUS_AUTHENTICATING
:
2296 *timeout_usec
= bus
->auth_timeout
;
2301 if (bus
->rqueue_size
> 0) {
2306 c
= prioq_peek(bus
->reply_callbacks_prioq
);
2308 *timeout_usec
= (uint64_t) -1;
2312 if (c
->timeout_usec
== 0) {
2313 *timeout_usec
= (uint64_t) -1;
2317 *timeout_usec
= c
->timeout_usec
;
2324 case BUS_WATCH_BIND
:
2326 *timeout_usec
= (uint64_t) -1;
2330 assert_not_reached("Unknown or unexpected stat");
2334 static int process_timeout(sd_bus
*bus
) {
2335 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2336 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* m
= NULL
;
2337 struct reply_callback
*c
;
2344 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
2346 c
= prioq_peek(bus
->reply_callbacks_prioq
);
2350 n
= now(CLOCK_MONOTONIC
);
2351 if (c
->timeout_usec
> n
)
2354 r
= bus_message_new_synthetic_error(
2357 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY
, "Method call timed out"),
2362 r
= bus_seal_synthetic_message(bus
, m
);
2366 assert_se(prioq_pop(bus
->reply_callbacks_prioq
) == c
);
2367 c
->timeout_usec
= 0;
2369 ordered_hashmap_remove(bus
->reply_callbacks
, &c
->cookie
);
2372 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
2374 bus
->iteration_counter
++;
2376 is_hello
= bus
->state
== BUS_HELLO
&& c
->callback
== hello_callback
;
2378 bus
->current_message
= m
;
2379 bus
->current_slot
= sd_bus_slot_ref(slot
);
2380 bus
->current_handler
= c
->callback
;
2381 bus
->current_userdata
= slot
->userdata
;
2382 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
2383 bus
->current_userdata
= NULL
;
2384 bus
->current_handler
= NULL
;
2385 bus
->current_slot
= NULL
;
2386 bus
->current_message
= NULL
;
2389 bus_slot_disconnect(slot
, true);
2391 sd_bus_slot_unref(slot
);
2393 /* When this is the hello message and it timed out, then make sure to propagate the error up, don't just log
2394 * and ignore the callback handler's return value. */
2398 return bus_maybe_reply_error(m
, r
, &error_buffer
);
2401 static int process_hello(sd_bus
*bus
, sd_bus_message
*m
) {
2405 if (bus
->state
!= BUS_HELLO
)
2408 /* Let's make sure the first message on the bus is the HELLO
2409 * reply. But note that we don't actually parse the message
2410 * here (we leave that to the usual handling), we just verify
2411 * we don't let any earlier msg through. */
2413 if (!IN_SET(m
->header
->type
, SD_BUS_MESSAGE_METHOD_RETURN
, SD_BUS_MESSAGE_METHOD_ERROR
))
2416 if (m
->reply_cookie
!= 1)
2422 static int process_reply(sd_bus
*bus
, sd_bus_message
*m
) {
2423 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*synthetic_reply
= NULL
;
2424 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2425 struct reply_callback
*c
;
2433 if (!IN_SET(m
->header
->type
, SD_BUS_MESSAGE_METHOD_RETURN
, SD_BUS_MESSAGE_METHOD_ERROR
))
2436 if (m
->destination
&& bus
->unique_name
&& !streq_ptr(m
->destination
, bus
->unique_name
))
2439 c
= ordered_hashmap_remove(bus
->reply_callbacks
, &m
->reply_cookie
);
2445 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
2447 if (m
->n_fds
> 0 && !bus
->accept_fd
) {
2449 /* If the reply contained a file descriptor which we
2450 * didn't want we pass an error instead. */
2452 r
= bus_message_new_synthetic_error(
2455 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Reply message contained file descriptor"),
2460 /* Copy over original timestamp */
2461 synthetic_reply
->realtime
= m
->realtime
;
2462 synthetic_reply
->monotonic
= m
->monotonic
;
2463 synthetic_reply
->seqnum
= m
->seqnum
;
2465 r
= bus_seal_synthetic_message(bus
, synthetic_reply
);
2469 m
= synthetic_reply
;
2471 r
= sd_bus_message_rewind(m
, true);
2476 if (c
->timeout_usec
!= 0) {
2477 prioq_remove(bus
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
2478 c
->timeout_usec
= 0;
2481 is_hello
= bus
->state
== BUS_HELLO
&& c
->callback
== hello_callback
;
2483 bus
->current_slot
= sd_bus_slot_ref(slot
);
2484 bus
->current_handler
= c
->callback
;
2485 bus
->current_userdata
= slot
->userdata
;
2486 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
2487 bus
->current_userdata
= NULL
;
2488 bus
->current_handler
= NULL
;
2489 bus
->current_slot
= NULL
;
2492 bus_slot_disconnect(slot
, true);
2494 sd_bus_slot_unref(slot
);
2496 /* When this is the hello message and it failed, then make sure to propagate the error up, don't just log and
2497 * ignore the callback handler's return value. */
2501 return bus_maybe_reply_error(m
, r
, &error_buffer
);
2504 static int process_filter(sd_bus
*bus
, sd_bus_message
*m
) {
2505 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2506 struct filter_callback
*l
;
2513 bus
->filter_callbacks_modified
= false;
2515 LIST_FOREACH(callbacks
, l
, bus
->filter_callbacks
) {
2518 if (bus
->filter_callbacks_modified
)
2521 /* Don't run this more than once per iteration */
2522 if (l
->last_iteration
== bus
->iteration_counter
)
2525 l
->last_iteration
= bus
->iteration_counter
;
2527 r
= sd_bus_message_rewind(m
, true);
2531 slot
= container_of(l
, sd_bus_slot
, filter_callback
);
2533 bus
->current_slot
= sd_bus_slot_ref(slot
);
2534 bus
->current_handler
= l
->callback
;
2535 bus
->current_userdata
= slot
->userdata
;
2536 r
= l
->callback(m
, slot
->userdata
, &error_buffer
);
2537 bus
->current_userdata
= NULL
;
2538 bus
->current_handler
= NULL
;
2539 bus
->current_slot
= sd_bus_slot_unref(slot
);
2541 r
= bus_maybe_reply_error(m
, r
, &error_buffer
);
2547 } while (bus
->filter_callbacks_modified
);
2552 static int process_match(sd_bus
*bus
, sd_bus_message
*m
) {
2559 bus
->match_callbacks_modified
= false;
2561 r
= bus_match_run(bus
, &bus
->match_callbacks
, m
);
2565 } while (bus
->match_callbacks_modified
);
2570 static int process_builtin(sd_bus
*bus
, sd_bus_message
*m
) {
2571 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2577 if (bus
->is_monitor
)
2580 if (bus
->manual_peer_interface
)
2583 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_CALL
)
2586 if (!streq_ptr(m
->interface
, "org.freedesktop.DBus.Peer"))
2589 if (m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
)
2592 if (streq_ptr(m
->member
, "Ping"))
2593 r
= sd_bus_message_new_method_return(m
, &reply
);
2594 else if (streq_ptr(m
->member
, "GetMachineId")) {
2598 r
= sd_id128_get_machine(&id
);
2602 r
= sd_bus_message_new_method_return(m
, &reply
);
2606 r
= sd_bus_message_append(reply
, "s", sd_id128_to_string(id
, sid
));
2608 r
= sd_bus_message_new_method_errorf(
2610 SD_BUS_ERROR_UNKNOWN_METHOD
,
2611 "Unknown method '%s' on interface '%s'.", m
->member
, m
->interface
);
2617 r
= sd_bus_send(bus
, reply
, NULL
);
2624 static int process_fd_check(sd_bus
*bus
, sd_bus_message
*m
) {
2628 /* If we got a message with a file descriptor which we didn't
2629 * want to accept, then let's drop it. How can this even
2630 * happen? For example, when the kernel queues a message into
2631 * an activatable names's queue which allows fds, and then is
2632 * delivered to us later even though we ourselves did not
2635 if (bus
->is_monitor
)
2644 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_CALL
)
2645 return 1; /* just eat it up */
2647 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Message contains file descriptors, which I cannot accept. Sorry.");
2650 static int process_message(sd_bus
*bus
, sd_bus_message
*m
) {
2656 bus
->current_message
= m
;
2657 bus
->iteration_counter
++;
2659 log_debug_bus_message(m
);
2661 r
= process_hello(bus
, m
);
2665 r
= process_reply(bus
, m
);
2669 r
= process_fd_check(bus
, m
);
2673 r
= process_filter(bus
, m
);
2677 r
= process_match(bus
, m
);
2681 r
= process_builtin(bus
, m
);
2685 r
= bus_process_object(bus
, m
);
2688 bus
->current_message
= NULL
;
2692 static int dispatch_track(sd_bus
*bus
) {
2695 if (!bus
->track_queue
)
2698 bus_track_dispatch(bus
->track_queue
);
2702 static int process_running(sd_bus
*bus
, bool hint_priority
, int64_t priority
, sd_bus_message
**ret
) {
2703 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2707 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
2709 r
= process_timeout(bus
);
2713 r
= dispatch_wqueue(bus
);
2717 r
= dispatch_track(bus
);
2721 r
= dispatch_rqueue(bus
, hint_priority
, priority
, &m
);
2727 r
= process_message(bus
, m
);
2732 r
= sd_bus_message_rewind(m
, true);
2741 if (m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
) {
2743 log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s",
2744 strna(sd_bus_message_get_sender(m
)),
2745 strna(sd_bus_message_get_path(m
)),
2746 strna(sd_bus_message_get_interface(m
)),
2747 strna(sd_bus_message_get_member(m
)));
2749 r
= sd_bus_reply_method_errorf(
2751 SD_BUS_ERROR_UNKNOWN_OBJECT
,
2752 "Unknown object '%s'.", m
->path
);
2766 static int bus_exit_now(sd_bus
*bus
) {
2769 /* Exit due to close, if this is requested. If this is bus object is attached to an event source, invokes
2770 * sd_event_exit(), otherwise invokes libc exit(). */
2772 if (bus
->exited
) /* did we already exit? */
2774 if (!bus
->exit_triggered
) /* was the exit condition triggered? */
2776 if (!bus
->exit_on_disconnect
) /* Shall we actually exit on disconnection? */
2779 bus
->exited
= true; /* never exit more than once */
2781 log_debug("Bus connection disconnected, exiting.");
2784 return sd_event_exit(bus
->event
, EXIT_FAILURE
);
2788 assert_not_reached("exit() didn't exit?");
2791 static int process_closing_reply_callback(sd_bus
*bus
, struct reply_callback
*c
) {
2792 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2793 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2800 r
= bus_message_new_synthetic_error(
2803 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY
, "Connection terminated"),
2808 r
= bus_seal_synthetic_message(bus
, m
);
2812 if (c
->timeout_usec
!= 0) {
2813 prioq_remove(bus
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
2814 c
->timeout_usec
= 0;
2817 ordered_hashmap_remove(bus
->reply_callbacks
, &c
->cookie
);
2820 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
2822 bus
->iteration_counter
++;
2824 bus
->current_message
= m
;
2825 bus
->current_slot
= sd_bus_slot_ref(slot
);
2826 bus
->current_handler
= c
->callback
;
2827 bus
->current_userdata
= slot
->userdata
;
2828 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
2829 bus
->current_userdata
= NULL
;
2830 bus
->current_handler
= NULL
;
2831 bus
->current_slot
= NULL
;
2832 bus
->current_message
= NULL
;
2835 bus_slot_disconnect(slot
, true);
2837 sd_bus_slot_unref(slot
);
2839 return bus_maybe_reply_error(m
, r
, &error_buffer
);
2842 static int process_closing(sd_bus
*bus
, sd_bus_message
**ret
) {
2843 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2844 struct reply_callback
*c
;
2848 assert(bus
->state
== BUS_CLOSING
);
2850 /* First, fail all outstanding method calls */
2851 c
= ordered_hashmap_first(bus
->reply_callbacks
);
2853 return process_closing_reply_callback(bus
, c
);
2855 /* Then, fake-drop all remaining bus tracking references */
2857 bus_track_close(bus
->tracks
);
2861 /* Then, synthesize a Disconnected message */
2862 r
= sd_bus_message_new_signal(
2865 "/org/freedesktop/DBus/Local",
2866 "org.freedesktop.DBus.Local",
2871 bus_message_set_sender_local(bus
, m
);
2873 r
= bus_seal_synthetic_message(bus
, m
);
2879 bus
->current_message
= m
;
2880 bus
->iteration_counter
++;
2882 r
= process_filter(bus
, m
);
2886 r
= process_match(bus
, m
);
2890 /* Nothing else to do, exit now, if the condition holds */
2891 bus
->exit_triggered
= true;
2892 (void) bus_exit_now(bus
);
2900 bus
->current_message
= NULL
;
2905 static int bus_process_internal(sd_bus
*bus
, bool hint_priority
, int64_t priority
, sd_bus_message
**ret
) {
2908 /* Returns 0 when we didn't do anything. This should cause the
2909 * caller to invoke sd_bus_wait() before returning the next
2910 * time. Returns > 0 when we did something, which possibly
2911 * means *ret is filled in with an unprocessed message. */
2913 assert_return(bus
, -EINVAL
);
2914 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2915 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2917 /* We don't allow recursively invoking sd_bus_process(). */
2918 assert_return(!bus
->current_message
, -EBUSY
);
2919 assert(!bus
->current_slot
); /* This should be NULL whenever bus->current_message is */
2921 BUS_DONT_DESTROY(bus
);
2923 switch (bus
->state
) {
2931 case BUS_WATCH_BIND
:
2932 r
= bus_socket_process_watch_bind(bus
);
2936 r
= bus_socket_process_opening(bus
);
2939 case BUS_AUTHENTICATING
:
2940 r
= bus_socket_process_authenticating(bus
);
2945 r
= process_running(bus
, hint_priority
, priority
, ret
);
2949 /* This branch initializes *ret, hence we don't use the generic error checking below */
2953 return process_closing(bus
, ret
);
2956 assert_not_reached("Unknown state");
2959 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
2960 bus_enter_closing(bus
);
2971 _public_
int sd_bus_process(sd_bus
*bus
, sd_bus_message
**ret
) {
2972 return bus_process_internal(bus
, false, 0, ret
);
2975 _public_
int sd_bus_process_priority(sd_bus
*bus
, int64_t priority
, sd_bus_message
**ret
) {
2976 return bus_process_internal(bus
, true, priority
, ret
);
2979 static int bus_poll(sd_bus
*bus
, bool need_more
, uint64_t timeout_usec
) {
2980 struct pollfd p
[2] = {};
2983 usec_t m
= USEC_INFINITY
;
2987 if (bus
->state
== BUS_CLOSING
)
2990 if (!BUS_IS_OPEN(bus
->state
))
2993 if (bus
->state
== BUS_WATCH_BIND
) {
2994 assert(bus
->inotify_fd
>= 0);
2996 p
[0].events
= POLLIN
;
2997 p
[0].fd
= bus
->inotify_fd
;
3002 e
= sd_bus_get_events(bus
);
3007 /* The caller really needs some more data, he doesn't
3008 * care about what's already read, or any timeouts
3009 * except its own. */
3013 /* The caller wants to process if there's something to
3014 * process, but doesn't care otherwise */
3016 r
= sd_bus_get_timeout(bus
, &until
);
3020 m
= usec_sub_unsigned(until
, now(CLOCK_MONOTONIC
));
3023 p
[0].fd
= bus
->input_fd
;
3024 if (bus
->output_fd
== bus
->input_fd
) {
3028 p
[0].events
= e
& POLLIN
;
3029 p
[1].fd
= bus
->output_fd
;
3030 p
[1].events
= e
& POLLOUT
;
3035 if (timeout_usec
!= (uint64_t) -1 && (m
== USEC_INFINITY
|| timeout_usec
< m
))
3038 r
= ppoll(p
, n
, m
== USEC_INFINITY
? NULL
: timespec_store(&ts
, m
), NULL
);
3042 return r
> 0 ? 1 : 0;
3045 _public_
int sd_bus_wait(sd_bus
*bus
, uint64_t timeout_usec
) {
3047 assert_return(bus
, -EINVAL
);
3048 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3049 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3051 if (bus
->state
== BUS_CLOSING
)
3054 if (!BUS_IS_OPEN(bus
->state
))
3057 if (bus
->rqueue_size
> 0)
3060 return bus_poll(bus
, false, timeout_usec
);
3063 _public_
int sd_bus_flush(sd_bus
*bus
) {
3066 assert_return(bus
, -EINVAL
);
3067 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3068 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3070 if (bus
->state
== BUS_CLOSING
)
3073 if (!BUS_IS_OPEN(bus
->state
))
3076 /* We never were connected? Don't hang in inotify for good, as there's no timeout set for it */
3077 if (bus
->state
== BUS_WATCH_BIND
)
3080 r
= bus_ensure_running(bus
);
3084 if (bus
->wqueue_size
<= 0)
3088 r
= dispatch_wqueue(bus
);
3090 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
3091 bus_enter_closing(bus
);
3098 if (bus
->wqueue_size
<= 0)
3101 r
= bus_poll(bus
, false, (uint64_t) -1);
3107 _public_
int sd_bus_add_filter(
3110 sd_bus_message_handler_t callback
,
3115 assert_return(bus
, -EINVAL
);
3116 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3117 assert_return(callback
, -EINVAL
);
3118 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3120 s
= bus_slot_allocate(bus
, !slot
, BUS_FILTER_CALLBACK
, sizeof(struct filter_callback
), userdata
);
3124 s
->filter_callback
.callback
= callback
;
3126 bus
->filter_callbacks_modified
= true;
3127 LIST_PREPEND(callbacks
, bus
->filter_callbacks
, &s
->filter_callback
);
3135 static int add_match_callback(
3138 sd_bus_error
*ret_error
) {
3140 sd_bus_slot
*match_slot
= userdata
;
3141 bool failed
= false;
3147 sd_bus_slot_ref(match_slot
);
3149 if (sd_bus_message_is_method_error(m
, NULL
)) {
3150 log_debug_errno(sd_bus_message_get_errno(m
),
3151 "Unable to add match %s, failing connection: %s",
3152 match_slot
->match_callback
.match_string
,
3153 sd_bus_message_get_error(m
)->message
);
3157 log_debug("Match %s successfully installed.", match_slot
->match_callback
.match_string
);
3159 if (match_slot
->match_callback
.install_callback
) {
3162 bus
= sd_bus_message_get_bus(m
);
3164 /* This function has been called as slot handler, and we want to call another slot handler. Let's
3165 * update the slot callback metadata temporarily with our own data, and then revert back to the old
3168 assert(bus
->current_slot
== match_slot
->match_callback
.install_slot
);
3169 assert(bus
->current_handler
== add_match_callback
);
3170 assert(bus
->current_userdata
== userdata
);
3172 bus
->current_slot
= match_slot
;
3173 bus
->current_handler
= match_slot
->match_callback
.install_callback
;
3174 bus
->current_userdata
= match_slot
->userdata
;
3176 r
= match_slot
->match_callback
.install_callback(m
, match_slot
->userdata
, ret_error
);
3178 bus
->current_slot
= match_slot
->match_callback
.install_slot
;
3179 bus
->current_handler
= add_match_callback
;
3180 bus
->current_userdata
= userdata
;
3182 match_slot
->match_callback
.install_slot
= sd_bus_slot_unref(match_slot
->match_callback
.install_slot
);
3184 if (failed
) /* Generic failure handling: destroy the connection */
3185 bus_enter_closing(sd_bus_message_get_bus(m
));
3190 if (failed
&& match_slot
->floating
)
3191 bus_slot_disconnect(match_slot
, true);
3193 sd_bus_slot_unref(match_slot
);
3198 static int bus_add_match_full(
3203 sd_bus_message_handler_t callback
,
3204 sd_bus_message_handler_t install_callback
,
3207 struct bus_match_component
*components
= NULL
;
3208 unsigned n_components
= 0;
3209 sd_bus_slot
*s
= NULL
;
3212 assert_return(bus
, -EINVAL
);
3213 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3214 assert_return(match
, -EINVAL
);
3215 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3217 r
= bus_match_parse(match
, &components
, &n_components
);
3221 s
= bus_slot_allocate(bus
, !slot
, BUS_MATCH_CALLBACK
, sizeof(struct match_callback
), userdata
);
3227 s
->match_callback
.callback
= callback
;
3228 s
->match_callback
.install_callback
= install_callback
;
3230 if (bus
->bus_client
) {
3231 enum bus_match_scope scope
;
3233 scope
= bus_match_get_scope(components
, n_components
);
3235 /* Do not install server-side matches for matches against the local service, interface or bus path. */
3236 if (scope
!= BUS_MATCH_LOCAL
) {
3238 /* We store the original match string, so that we can use it to remove the match again. */
3240 s
->match_callback
.match_string
= strdup(match
);
3241 if (!s
->match_callback
.match_string
) {
3247 r
= bus_add_match_internal_async(bus
,
3248 &s
->match_callback
.install_slot
,
3249 s
->match_callback
.match_string
,
3256 /* Make the slot of the match call floating now. We need the reference, but we don't
3257 * want that this match pins the bus object, hence we first create it non-floating, but
3258 * then make it floating. */
3259 r
= sd_bus_slot_set_floating(s
->match_callback
.install_slot
, true);
3261 r
= bus_add_match_internal(bus
, s
->match_callback
.match_string
);
3265 s
->match_added
= true;
3269 bus
->match_callbacks_modified
= true;
3270 r
= bus_match_add(&bus
->match_callbacks
, components
, n_components
, &s
->match_callback
);
3279 bus_match_parse_free(components
, n_components
);
3280 sd_bus_slot_unref(s
);
3285 _public_
int sd_bus_add_match(
3289 sd_bus_message_handler_t callback
,
3292 return bus_add_match_full(bus
, slot
, false, match
, callback
, NULL
, userdata
);
3295 _public_
int sd_bus_add_match_async(
3299 sd_bus_message_handler_t callback
,
3300 sd_bus_message_handler_t install_callback
,
3303 return bus_add_match_full(bus
, slot
, true, match
, callback
, install_callback
, userdata
);
3306 bool bus_pid_changed(sd_bus
*bus
) {
3309 /* We don't support people creating a bus connection and
3310 * keeping it around over a fork(). Let's complain. */
3312 return bus
->original_pid
!= getpid_cached();
3315 static int io_callback(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
3316 sd_bus
*bus
= userdata
;
3321 /* Note that this is called both on input_fd, output_fd as well as inotify_fd events */
3323 r
= sd_bus_process(bus
, NULL
);
3325 log_debug_errno(r
, "Processing of bus failed, closing down: %m");
3326 bus_enter_closing(bus
);
3332 static int time_callback(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
3333 sd_bus
*bus
= userdata
;
3338 r
= sd_bus_process(bus
, NULL
);
3340 log_debug_errno(r
, "Processing of bus failed, closing down: %m");
3341 bus_enter_closing(bus
);
3347 static int prepare_callback(sd_event_source
*s
, void *userdata
) {
3348 sd_bus
*bus
= userdata
;
3355 e
= sd_bus_get_events(bus
);
3361 if (bus
->output_fd
!= bus
->input_fd
) {
3363 r
= sd_event_source_set_io_events(bus
->input_io_event_source
, e
& POLLIN
);
3367 r
= sd_event_source_set_io_events(bus
->output_io_event_source
, e
& POLLOUT
);
3369 r
= sd_event_source_set_io_events(bus
->input_io_event_source
, e
);
3373 r
= sd_bus_get_timeout(bus
, &until
);
3379 j
= sd_event_source_set_time(bus
->time_event_source
, until
);
3386 r
= sd_event_source_set_enabled(bus
->time_event_source
, r
> 0);
3393 log_debug_errno(r
, "Preparing of bus events failed, closing down: %m");
3394 bus_enter_closing(bus
);
3399 static int quit_callback(sd_event_source
*event
, void *userdata
) {
3400 sd_bus
*bus
= userdata
;
3410 int bus_attach_io_events(sd_bus
*bus
) {
3415 if (bus
->input_fd
< 0)
3421 if (!bus
->input_io_event_source
) {
3422 r
= sd_event_add_io(bus
->event
, &bus
->input_io_event_source
, bus
->input_fd
, 0, io_callback
, bus
);
3426 r
= sd_event_source_set_prepare(bus
->input_io_event_source
, prepare_callback
);
3430 r
= sd_event_source_set_priority(bus
->input_io_event_source
, bus
->event_priority
);
3434 r
= sd_event_source_set_description(bus
->input_io_event_source
, "bus-input");
3436 r
= sd_event_source_set_io_fd(bus
->input_io_event_source
, bus
->input_fd
);
3441 if (bus
->output_fd
!= bus
->input_fd
) {
3442 assert(bus
->output_fd
>= 0);
3444 if (!bus
->output_io_event_source
) {
3445 r
= sd_event_add_io(bus
->event
, &bus
->output_io_event_source
, bus
->output_fd
, 0, io_callback
, bus
);
3449 r
= sd_event_source_set_priority(bus
->output_io_event_source
, bus
->event_priority
);
3453 r
= sd_event_source_set_description(bus
->input_io_event_source
, "bus-output");
3455 r
= sd_event_source_set_io_fd(bus
->output_io_event_source
, bus
->output_fd
);
3464 static void bus_detach_io_events(sd_bus
*bus
) {
3467 if (bus
->input_io_event_source
) {
3468 sd_event_source_set_enabled(bus
->input_io_event_source
, SD_EVENT_OFF
);
3469 bus
->input_io_event_source
= sd_event_source_unref(bus
->input_io_event_source
);
3472 if (bus
->output_io_event_source
) {
3473 sd_event_source_set_enabled(bus
->output_io_event_source
, SD_EVENT_OFF
);
3474 bus
->output_io_event_source
= sd_event_source_unref(bus
->output_io_event_source
);
3478 int bus_attach_inotify_event(sd_bus
*bus
) {
3483 if (bus
->inotify_fd
< 0)
3489 if (!bus
->inotify_event_source
) {
3490 r
= sd_event_add_io(bus
->event
, &bus
->inotify_event_source
, bus
->inotify_fd
, EPOLLIN
, io_callback
, bus
);
3494 r
= sd_event_source_set_priority(bus
->inotify_event_source
, bus
->event_priority
);
3498 r
= sd_event_source_set_description(bus
->inotify_event_source
, "bus-inotify");
3500 r
= sd_event_source_set_io_fd(bus
->inotify_event_source
, bus
->inotify_fd
);
3507 static void bus_detach_inotify_event(sd_bus
*bus
) {
3510 if (bus
->inotify_event_source
) {
3511 sd_event_source_set_enabled(bus
->inotify_event_source
, SD_EVENT_OFF
);
3512 bus
->inotify_event_source
= sd_event_source_unref(bus
->inotify_event_source
);
3516 _public_
int sd_bus_attach_event(sd_bus
*bus
, sd_event
*event
, int priority
) {
3519 assert_return(bus
, -EINVAL
);
3520 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3521 assert_return(!bus
->event
, -EBUSY
);
3523 assert(!bus
->input_io_event_source
);
3524 assert(!bus
->output_io_event_source
);
3525 assert(!bus
->time_event_source
);
3528 bus
->event
= sd_event_ref(event
);
3530 r
= sd_event_default(&bus
->event
);
3535 bus
->event_priority
= priority
;
3537 r
= sd_event_add_time(bus
->event
, &bus
->time_event_source
, CLOCK_MONOTONIC
, 0, 0, time_callback
, bus
);
3541 r
= sd_event_source_set_priority(bus
->time_event_source
, priority
);
3545 r
= sd_event_source_set_description(bus
->time_event_source
, "bus-time");
3549 r
= sd_event_add_exit(bus
->event
, &bus
->quit_event_source
, quit_callback
, bus
);
3553 r
= sd_event_source_set_description(bus
->quit_event_source
, "bus-exit");
3557 r
= bus_attach_io_events(bus
);
3561 r
= bus_attach_inotify_event(bus
);
3568 sd_bus_detach_event(bus
);
3572 _public_
int sd_bus_detach_event(sd_bus
*bus
) {
3573 assert_return(bus
, -EINVAL
);
3574 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3579 bus_detach_io_events(bus
);
3580 bus_detach_inotify_event(bus
);
3582 if (bus
->time_event_source
) {
3583 sd_event_source_set_enabled(bus
->time_event_source
, SD_EVENT_OFF
);
3584 bus
->time_event_source
= sd_event_source_unref(bus
->time_event_source
);
3587 if (bus
->quit_event_source
) {
3588 sd_event_source_set_enabled(bus
->quit_event_source
, SD_EVENT_OFF
);
3589 bus
->quit_event_source
= sd_event_source_unref(bus
->quit_event_source
);
3592 bus
->event
= sd_event_unref(bus
->event
);
3596 _public_ sd_event
* sd_bus_get_event(sd_bus
*bus
) {
3597 assert_return(bus
, NULL
);
3602 _public_ sd_bus_message
* sd_bus_get_current_message(sd_bus
*bus
) {
3603 assert_return(bus
, NULL
);
3605 return bus
->current_message
;
3608 _public_ sd_bus_slot
* sd_bus_get_current_slot(sd_bus
*bus
) {
3609 assert_return(bus
, NULL
);
3611 return bus
->current_slot
;
3614 _public_ sd_bus_message_handler_t
sd_bus_get_current_handler(sd_bus
*bus
) {
3615 assert_return(bus
, NULL
);
3617 return bus
->current_handler
;
3620 _public_
void* sd_bus_get_current_userdata(sd_bus
*bus
) {
3621 assert_return(bus
, NULL
);
3623 return bus
->current_userdata
;
3626 static int bus_default(int (*bus_open
)(sd_bus
**), sd_bus
**default_bus
, sd_bus
**ret
) {
3631 assert(default_bus
);
3634 return !!*default_bus
;
3637 *ret
= sd_bus_ref(*default_bus
);
3645 b
->default_bus_ptr
= default_bus
;
3653 _public_
int sd_bus_default_system(sd_bus
**ret
) {
3654 return bus_default(sd_bus_open_system
, &default_system_bus
, ret
);
3657 _public_
int sd_bus_default_user(sd_bus
**ret
) {
3658 return bus_default(sd_bus_open_user
, &default_user_bus
, ret
);
3661 _public_
int sd_bus_default(sd_bus
**ret
) {
3662 int (*bus_open
)(sd_bus
**) = NULL
;
3665 busp
= bus_choose_default(&bus_open
);
3666 return bus_default(bus_open
, busp
, ret
);
3669 _public_
int sd_bus_get_tid(sd_bus
*b
, pid_t
*tid
) {
3670 assert_return(b
, -EINVAL
);
3671 assert_return(tid
, -EINVAL
);
3672 assert_return(!bus_pid_changed(b
), -ECHILD
);
3680 return sd_event_get_tid(b
->event
, tid
);
3685 _public_
int sd_bus_path_encode(const char *prefix
, const char *external_id
, char **ret_path
) {
3686 _cleanup_free_
char *e
= NULL
;
3689 assert_return(object_path_is_valid(prefix
), -EINVAL
);
3690 assert_return(external_id
, -EINVAL
);
3691 assert_return(ret_path
, -EINVAL
);
3693 e
= bus_label_escape(external_id
);
3697 ret
= strjoin(prefix
, "/", e
);
3705 _public_
int sd_bus_path_decode(const char *path
, const char *prefix
, char **external_id
) {
3709 assert_return(object_path_is_valid(path
), -EINVAL
);
3710 assert_return(object_path_is_valid(prefix
), -EINVAL
);
3711 assert_return(external_id
, -EINVAL
);
3713 e
= object_path_startswith(path
, prefix
);
3715 *external_id
= NULL
;
3719 ret
= bus_label_unescape(e
);
3727 _public_
int sd_bus_path_encode_many(char **out
, const char *path_template
, ...) {
3728 _cleanup_strv_free_
char **labels
= NULL
;
3729 char *path
, *path_pos
, **label_pos
;
3730 const char *sep
, *template_pos
;
3735 assert_return(out
, -EINVAL
);
3736 assert_return(path_template
, -EINVAL
);
3738 path_length
= strlen(path_template
);
3740 va_start(list
, path_template
);
3741 for (sep
= strchr(path_template
, '%'); sep
; sep
= strchr(sep
+ 1, '%')) {
3745 arg
= va_arg(list
, const char *);
3751 label
= bus_label_escape(arg
);
3757 r
= strv_consume(&labels
, label
);
3763 /* add label length, but account for the format character */
3764 path_length
+= strlen(label
) - 1;
3768 path
= malloc(path_length
+ 1);
3775 for (template_pos
= path_template
; *template_pos
; ) {
3776 sep
= strchrnul(template_pos
, '%');
3777 path_pos
= mempcpy(path_pos
, template_pos
, sep
- template_pos
);
3781 path_pos
= stpcpy(path_pos
, *label_pos
++);
3782 template_pos
= sep
+ 1;
3790 _public_
int sd_bus_path_decode_many(const char *path
, const char *path_template
, ...) {
3791 _cleanup_strv_free_
char **labels
= NULL
;
3792 const char *template_pos
, *path_pos
;
3798 * This decodes an object-path based on a template argument. The
3799 * template consists of a verbatim path, optionally including special
3802 * - Each occurrence of '%' in the template matches an arbitrary
3803 * substring of a label in the given path. At most one such
3804 * directive is allowed per label. For each such directive, the
3805 * caller must provide an output parameter (char **) via va_arg. If
3806 * NULL is passed, the given label is verified, but not returned.
3807 * For each matched label, the *decoded* label is stored in the
3808 * passed output argument, and the caller is responsible to free
3809 * it. Note that the output arguments are only modified if the
3810 * actualy path matched the template. Otherwise, they're left
3813 * This function returns <0 on error, 0 if the path does not match the
3814 * template, 1 if it matched.
3817 assert_return(path
, -EINVAL
);
3818 assert_return(path_template
, -EINVAL
);
3822 for (template_pos
= path_template
; *template_pos
; ) {
3827 /* verify everything until the next '%' matches verbatim */
3828 sep
= strchrnul(template_pos
, '%');
3829 length
= sep
- template_pos
;
3830 if (strncmp(path_pos
, template_pos
, length
))
3834 template_pos
+= length
;
3839 /* We found the next '%' character. Everything up until here
3840 * matched. We now skip ahead to the end of this label and make
3841 * sure it matches the tail of the label in the path. Then we
3842 * decode the string in-between and save it for later use. */
3844 ++template_pos
; /* skip over '%' */
3846 sep
= strchrnul(template_pos
, '/');
3847 length
= sep
- template_pos
; /* length of suffix to match verbatim */
3849 /* verify the suffixes match */
3850 sep
= strchrnul(path_pos
, '/');
3851 if (sep
- path_pos
< (ssize_t
)length
||
3852 strncmp(sep
- length
, template_pos
, length
))
3855 template_pos
+= length
; /* skip over matched label */
3856 length
= sep
- path_pos
- length
; /* length of sub-label to decode */
3858 /* store unescaped label for later use */
3859 label
= bus_label_unescape_n(path_pos
, length
);
3863 r
= strv_consume(&labels
, label
);
3867 path_pos
= sep
; /* skip decoded label and suffix */
3870 /* end of template must match end of path */
3874 /* copy the labels over to the caller */
3875 va_start(list
, path_template
);
3876 for (label_pos
= labels
; label_pos
&& *label_pos
; ++label_pos
) {
3879 arg
= va_arg(list
, char **);
3887 labels
= mfree(labels
);
3891 _public_
int sd_bus_try_close(sd_bus
*bus
) {
3892 assert_return(bus
, -EINVAL
);
3893 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3894 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3899 _public_
int sd_bus_get_description(sd_bus
*bus
, const char **description
) {
3900 assert_return(bus
, -EINVAL
);
3901 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3902 assert_return(description
, -EINVAL
);
3903 assert_return(bus
->description
, -ENXIO
);
3904 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3906 if (bus
->description
)
3907 *description
= bus
->description
;
3908 else if (bus
->is_system
)
3909 *description
= "system";
3910 else if (bus
->is_user
)
3911 *description
= "user";
3913 *description
= NULL
;
3918 int bus_get_root_path(sd_bus
*bus
) {
3921 if (bus
->cgroup_root
)
3924 r
= cg_get_root_path(&bus
->cgroup_root
);
3926 bus
->cgroup_root
= strdup("/");
3927 if (!bus
->cgroup_root
)
3936 _public_
int sd_bus_get_scope(sd_bus
*bus
, const char **scope
) {
3937 assert_return(bus
, -EINVAL
);
3938 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3939 assert_return(scope
, -EINVAL
);
3940 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3947 if (bus
->is_system
) {
3955 _public_
int sd_bus_get_address(sd_bus
*bus
, const char **address
) {
3957 assert_return(bus
, -EINVAL
);
3958 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3959 assert_return(address
, -EINVAL
);
3960 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3963 *address
= bus
->address
;
3970 _public_
int sd_bus_get_creds_mask(sd_bus
*bus
, uint64_t *mask
) {
3971 assert_return(bus
, -EINVAL
);
3972 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3973 assert_return(mask
, -EINVAL
);
3974 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3976 *mask
= bus
->creds_mask
;
3980 _public_
int sd_bus_is_bus_client(sd_bus
*bus
) {
3981 assert_return(bus
, -EINVAL
);
3982 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3983 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3985 return bus
->bus_client
;
3988 _public_
int sd_bus_is_server(sd_bus
*bus
) {
3989 assert_return(bus
, -EINVAL
);
3990 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3991 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3993 return bus
->is_server
;
3996 _public_
int sd_bus_is_anonymous(sd_bus
*bus
) {
3997 assert_return(bus
, -EINVAL
);
3998 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3999 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4001 return bus
->anonymous_auth
;
4004 _public_
int sd_bus_is_trusted(sd_bus
*bus
) {
4005 assert_return(bus
, -EINVAL
);
4006 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4007 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4009 return bus
->trusted
;
4012 _public_
int sd_bus_is_monitor(sd_bus
*bus
) {
4013 assert_return(bus
, -EINVAL
);
4014 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4015 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4017 return bus
->is_monitor
;
4020 static void flush_close(sd_bus
*bus
) {
4024 /* Flushes and closes the specified bus. We take a ref before,
4025 * to ensure the flushing does not cause the bus to be
4028 sd_bus_flush_close_unref(sd_bus_ref(bus
));
4031 _public_
void sd_bus_default_flush_close(void) {
4032 flush_close(default_starter_bus
);
4033 flush_close(default_user_bus
);
4034 flush_close(default_system_bus
);
4037 _public_
int sd_bus_set_exit_on_disconnect(sd_bus
*bus
, int b
) {
4038 assert_return(bus
, -EINVAL
);
4039 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4041 /* Turns on exit-on-disconnect, and triggers it immediately if the bus connection was already
4042 * disconnected. Note that this is triggered exclusively on disconnections triggered by the server side, never
4043 * from the client side. */
4044 bus
->exit_on_disconnect
= b
;
4046 /* If the exit condition was triggered already, exit immediately. */
4047 return bus_exit_now(bus
);
4050 _public_
int sd_bus_get_exit_on_disconnect(sd_bus
*bus
) {
4051 assert_return(bus
, -EINVAL
);
4052 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4054 return bus
->exit_on_disconnect
;
4057 _public_
int sd_bus_set_sender(sd_bus
*bus
, const char *sender
) {
4058 assert_return(bus
, -EINVAL
);
4059 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4060 assert_return(!bus
->bus_client
, -EPERM
);
4061 assert_return(!sender
|| service_name_is_valid(sender
), -EINVAL
);
4063 return free_and_strdup(&bus
->patch_sender
, sender
);
4066 _public_
int sd_bus_get_sender(sd_bus
*bus
, const char **ret
) {
4067 assert_return(bus
, -EINVAL
);
4068 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4069 assert_return(ret
, -EINVAL
);
4071 if (!bus
->patch_sender
)
4074 *ret
= bus
->patch_sender
;
4078 _public_
int sd_bus_get_n_queued_read(sd_bus
*bus
, uint64_t *ret
) {
4079 assert_return(bus
, -EINVAL
);
4080 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4081 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4082 assert_return(ret
, -EINVAL
);
4084 *ret
= bus
->rqueue_size
;
4088 _public_
int sd_bus_get_n_queued_write(sd_bus
*bus
, uint64_t *ret
) {
4089 assert_return(bus
, -EINVAL
);
4090 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4091 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4092 assert_return(ret
, -EINVAL
);
4094 *ret
= bus
->wqueue_size
;
4098 _public_
int sd_bus_set_method_call_timeout(sd_bus
*bus
, uint64_t usec
) {
4099 assert_return(bus
, -EINVAL
);
4100 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4102 bus
->method_call_timeout
= usec
;
4106 _public_
int sd_bus_get_method_call_timeout(sd_bus
*bus
, uint64_t *ret
) {
4110 assert_return(bus
, -EINVAL
);
4111 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4112 assert_return(ret
, -EINVAL
);
4114 if (bus
->method_call_timeout
!= 0) {
4115 *ret
= bus
->method_call_timeout
;
4119 e
= secure_getenv("SYSTEMD_BUS_TIMEOUT");
4120 if (e
&& parse_sec(e
, &usec
) >= 0 && usec
!= 0) {
4121 /* Save the parsed value to avoid multiple parsing. To change the timeout value,
4122 * use sd_bus_set_method_call_timeout() instead of setenv(). */
4123 *ret
= bus
->method_call_timeout
= usec
;
4127 *ret
= bus
->method_call_timeout
= BUS_DEFAULT_TIMEOUT
;