1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 Lennart Poettering
18 #include "alloc-util.h"
19 #include "bus-container.h"
20 #include "bus-control.h"
21 #include "bus-internal.h"
22 #include "bus-kernel.h"
23 #include "bus-label.h"
24 #include "bus-message.h"
25 #include "bus-objects.h"
26 #include "bus-protocol.h"
28 #include "bus-socket.h"
29 #include "bus-track.h"
32 #include "cgroup-util.h"
35 #include "hexdecoct.h"
36 #include "hostname-util.h"
39 #include "parse-util.h"
40 #include "process-util.h"
41 #include "string-util.h"
45 #define log_debug_bus_message(m) \
47 sd_bus_message *_mm = (m); \
48 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", \
49 bus_message_type_to_string(_mm->header->type), \
50 strna(sd_bus_message_get_sender(_mm)), \
51 strna(sd_bus_message_get_destination(_mm)), \
52 strna(sd_bus_message_get_path(_mm)), \
53 strna(sd_bus_message_get_interface(_mm)), \
54 strna(sd_bus_message_get_member(_mm)), \
55 BUS_MESSAGE_COOKIE(_mm), \
57 strna(_mm->root_container.signature), \
58 strna(_mm->error.name), \
59 strna(_mm->error.message)); \
62 static int bus_poll(sd_bus
*bus
, bool need_more
, uint64_t timeout_usec
);
63 static void bus_detach_io_events(sd_bus
*b
);
64 static void bus_detach_inotify_event(sd_bus
*b
);
66 static thread_local sd_bus
*default_system_bus
= NULL
;
67 static thread_local sd_bus
*default_user_bus
= NULL
;
68 static thread_local sd_bus
*default_starter_bus
= NULL
;
70 static sd_bus
**bus_choose_default(int (**bus_open
)(sd_bus
**)) {
73 /* Let's try our best to reuse another cached connection. If
74 * the starter bus type is set, connect via our normal
75 * connection logic, ignoring $DBUS_STARTER_ADDRESS, so that
76 * we can share the connection with the user/system default
79 e
= secure_getenv("DBUS_STARTER_BUS_TYPE");
81 if (streq(e
, "system")) {
83 *bus_open
= sd_bus_open_system
;
84 return &default_system_bus
;
85 } else if (STR_IN_SET(e
, "user", "session")) {
87 *bus_open
= sd_bus_open_user
;
88 return &default_user_bus
;
92 /* No type is specified, so we have not other option than to
93 * use the starter address if it is set. */
94 e
= secure_getenv("DBUS_STARTER_ADDRESS");
97 *bus_open
= sd_bus_open
;
98 return &default_starter_bus
;
101 /* Finally, if nothing is set use the cached connection for
104 if (cg_pid_get_owner_uid(0, NULL
) >= 0) {
106 *bus_open
= sd_bus_open_user
;
107 return &default_user_bus
;
110 *bus_open
= sd_bus_open_system
;
111 return &default_system_bus
;
115 sd_bus
*bus_resolve(sd_bus
*bus
) {
116 switch ((uintptr_t) bus
) {
117 case (uintptr_t) SD_BUS_DEFAULT
:
118 return *(bus_choose_default(NULL
));
119 case (uintptr_t) SD_BUS_DEFAULT_USER
:
120 return default_user_bus
;
121 case (uintptr_t) SD_BUS_DEFAULT_SYSTEM
:
122 return default_system_bus
;
128 void bus_close_io_fds(sd_bus
*b
) {
131 bus_detach_io_events(b
);
133 if (b
->input_fd
!= b
->output_fd
)
134 safe_close(b
->output_fd
);
135 b
->output_fd
= b
->input_fd
= safe_close(b
->input_fd
);
138 void bus_close_inotify_fd(sd_bus
*b
) {
141 bus_detach_inotify_event(b
);
143 b
->inotify_fd
= safe_close(b
->inotify_fd
);
144 b
->inotify_watches
= mfree(b
->inotify_watches
);
145 b
->n_inotify_watches
= 0;
148 static void bus_reset_queues(sd_bus
*b
) {
151 while (b
->rqueue_size
> 0)
152 sd_bus_message_unref(b
->rqueue
[--b
->rqueue_size
]);
154 b
->rqueue
= mfree(b
->rqueue
);
155 b
->rqueue_allocated
= 0;
157 while (b
->wqueue_size
> 0)
158 sd_bus_message_unref(b
->wqueue
[--b
->wqueue_size
]);
160 b
->wqueue
= mfree(b
->wqueue
);
161 b
->wqueue_allocated
= 0;
164 static sd_bus
* bus_free(sd_bus
*b
) {
168 assert(!b
->track_queue
);
171 b
->state
= BUS_CLOSED
;
173 sd_bus_detach_event(b
);
175 while ((s
= b
->slots
)) {
176 /* At this point only floating slots can still be
177 * around, because the non-floating ones keep a
178 * reference to the bus, and we thus couldn't be
179 * destructing right now... We forcibly disconnect the
180 * slots here, so that they still can be referenced by
181 * apps, but are dead. */
184 bus_slot_disconnect(s
);
185 sd_bus_slot_unref(s
);
188 if (b
->default_bus_ptr
)
189 *b
->default_bus_ptr
= NULL
;
192 bus_close_inotify_fd(b
);
197 free(b
->unique_name
);
198 free(b
->auth_buffer
);
201 free(b
->cgroup_root
);
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
);
243 b
->n_ref
= REFCNT_INIT
;
244 b
->input_fd
= b
->output_fd
= -1;
246 b
->message_version
= 1;
247 b
->creds_mask
|= SD_BUS_CREDS_WELL_KNOWN_NAMES
|SD_BUS_CREDS_UNIQUE_NAME
;
249 b
->original_pid
= getpid_cached();
250 b
->n_groups
= (size_t) -1;
252 assert_se(pthread_mutex_init(&b
->memfd_cache_mutex
, NULL
) == 0);
254 /* We guarantee that wqueue always has space for at least one entry */
255 if (!GREEDY_REALLOC(b
->wqueue
, b
->wqueue_allocated
, 1))
262 _public_
int sd_bus_set_address(sd_bus
*bus
, const char *address
) {
263 assert_return(bus
, -EINVAL
);
264 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
265 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
266 assert_return(address
, -EINVAL
);
267 assert_return(!bus_pid_changed(bus
), -ECHILD
);
269 return free_and_strdup(&bus
->address
, address
);
272 _public_
int sd_bus_set_fd(sd_bus
*bus
, int input_fd
, int output_fd
) {
273 assert_return(bus
, -EINVAL
);
274 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
275 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
276 assert_return(input_fd
>= 0, -EBADF
);
277 assert_return(output_fd
>= 0, -EBADF
);
278 assert_return(!bus_pid_changed(bus
), -ECHILD
);
280 bus
->input_fd
= input_fd
;
281 bus
->output_fd
= output_fd
;
285 _public_
int sd_bus_set_exec(sd_bus
*bus
, const char *path
, char *const argv
[]) {
286 _cleanup_strv_free_
char **a
= NULL
;
289 assert_return(bus
, -EINVAL
);
290 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
291 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
292 assert_return(path
, -EINVAL
);
293 assert_return(!strv_isempty(argv
), -EINVAL
);
294 assert_return(!bus_pid_changed(bus
), -ECHILD
);
300 r
= free_and_strdup(&bus
->exec_path
, path
);
304 return strv_free_and_replace(bus
->exec_argv
, a
);
307 _public_
int sd_bus_set_bus_client(sd_bus
*bus
, int b
) {
308 assert_return(bus
, -EINVAL
);
309 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
310 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
311 assert_return(!bus
->patch_sender
, -EPERM
);
312 assert_return(!bus_pid_changed(bus
), -ECHILD
);
314 bus
->bus_client
= !!b
;
318 _public_
int sd_bus_set_monitor(sd_bus
*bus
, int b
) {
319 assert_return(bus
, -EINVAL
);
320 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
321 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
322 assert_return(!bus_pid_changed(bus
), -ECHILD
);
324 bus
->is_monitor
= !!b
;
328 _public_
int sd_bus_negotiate_fds(sd_bus
*bus
, int b
) {
329 assert_return(bus
, -EINVAL
);
330 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
331 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
332 assert_return(!bus_pid_changed(bus
), -ECHILD
);
334 bus
->accept_fd
= !!b
;
338 _public_
int sd_bus_negotiate_timestamp(sd_bus
*bus
, int b
) {
339 assert_return(bus
, -EINVAL
);
340 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
341 assert_return(!IN_SET(bus
->state
, BUS_CLOSING
, BUS_CLOSED
), -EPERM
);
342 assert_return(!bus_pid_changed(bus
), -ECHILD
);
344 /* This is not actually supported by any of our transports these days, but we do honour it for synthetic
345 * replies, and maybe one day classic D-Bus learns this too */
346 bus
->attach_timestamp
= !!b
;
351 _public_
int sd_bus_negotiate_creds(sd_bus
*bus
, int b
, uint64_t mask
) {
352 assert_return(bus
, -EINVAL
);
353 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
354 assert_return(mask
<= _SD_BUS_CREDS_ALL
, -EINVAL
);
355 assert_return(!IN_SET(bus
->state
, BUS_CLOSING
, BUS_CLOSED
), -EPERM
);
356 assert_return(!bus_pid_changed(bus
), -ECHILD
);
358 SET_FLAG(bus
->creds_mask
, mask
, b
);
360 /* The well knowns we need unconditionally, so that matches can work */
361 bus
->creds_mask
|= SD_BUS_CREDS_WELL_KNOWN_NAMES
|SD_BUS_CREDS_UNIQUE_NAME
;
366 _public_
int sd_bus_set_server(sd_bus
*bus
, int b
, sd_id128_t server_id
) {
367 assert_return(bus
, -EINVAL
);
368 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
369 assert_return(b
|| sd_id128_equal(server_id
, SD_ID128_NULL
), -EINVAL
);
370 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
371 assert_return(!bus_pid_changed(bus
), -ECHILD
);
373 bus
->is_server
= !!b
;
374 bus
->server_id
= server_id
;
378 _public_
int sd_bus_set_anonymous(sd_bus
*bus
, int b
) {
379 assert_return(bus
, -EINVAL
);
380 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
381 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
382 assert_return(!bus_pid_changed(bus
), -ECHILD
);
384 bus
->anonymous_auth
= !!b
;
388 _public_
int sd_bus_set_trusted(sd_bus
*bus
, int b
) {
389 assert_return(bus
, -EINVAL
);
390 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
391 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
392 assert_return(!bus_pid_changed(bus
), -ECHILD
);
398 _public_
int sd_bus_set_description(sd_bus
*bus
, const char *description
) {
399 assert_return(bus
, -EINVAL
);
400 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
401 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
402 assert_return(!bus_pid_changed(bus
), -ECHILD
);
404 return free_and_strdup(&bus
->description
, description
);
407 _public_
int sd_bus_set_allow_interactive_authorization(sd_bus
*bus
, int b
) {
408 assert_return(bus
, -EINVAL
);
409 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
410 assert_return(!bus_pid_changed(bus
), -ECHILD
);
412 bus
->allow_interactive_authorization
= !!b
;
416 _public_
int sd_bus_get_allow_interactive_authorization(sd_bus
*bus
) {
417 assert_return(bus
, -EINVAL
);
418 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
419 assert_return(!bus_pid_changed(bus
), -ECHILD
);
421 return bus
->allow_interactive_authorization
;
424 _public_
int sd_bus_set_watch_bind(sd_bus
*bus
, int b
) {
425 assert_return(bus
, -EINVAL
);
426 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
427 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
428 assert_return(!bus_pid_changed(bus
), -ECHILD
);
430 bus
->watch_bind
= !!b
;
434 _public_
int sd_bus_get_watch_bind(sd_bus
*bus
) {
435 assert_return(bus
, -EINVAL
);
436 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
437 assert_return(!bus_pid_changed(bus
), -ECHILD
);
439 return bus
->watch_bind
;
442 _public_
int sd_bus_set_connected_signal(sd_bus
*bus
, int b
) {
443 assert_return(bus
, -EINVAL
);
444 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
445 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
446 assert_return(!bus_pid_changed(bus
), -ECHILD
);
448 bus
->connected_signal
= !!b
;
452 _public_
int sd_bus_get_connected_signal(sd_bus
*bus
) {
453 assert_return(bus
, -EINVAL
);
454 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
455 assert_return(!bus_pid_changed(bus
), -ECHILD
);
457 return bus
->connected_signal
;
460 static int synthesize_connected_signal(sd_bus
*bus
) {
461 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
466 /* If enabled, synthesizes a local "Connected" signal mirroring the local "Disconnected" signal. This is called
467 * whenever we fully established a connection, i.e. after the authorization phase, and after receiving the
468 * Hello() reply. Or in other words, whenver we enter BUS_RUNNING state.
470 * This is useful so that clients can start doing stuff whenver the connection is fully established in a way
471 * that works independently from whether we connected to a full bus or just a direct connection. */
473 if (!bus
->connected_signal
)
476 r
= sd_bus_message_new_signal(
479 "/org/freedesktop/DBus/Local",
480 "org.freedesktop.DBus.Local",
485 bus_message_set_sender_local(bus
, m
);
487 r
= bus_seal_synthetic_message(bus
, m
);
491 r
= bus_rqueue_make_room(bus
);
495 /* Insert at the very front */
496 memmove(bus
->rqueue
+ 1, bus
->rqueue
, sizeof(sd_bus_message
*) * bus
->rqueue_size
);
497 bus
->rqueue
[0] = TAKE_PTR(m
);
503 void bus_set_state(sd_bus
*bus
, enum bus_state state
) {
505 static const char * const table
[_BUS_STATE_MAX
] = {
506 [BUS_UNSET
] = "UNSET",
507 [BUS_WATCH_BIND
] = "WATCH_BIND",
508 [BUS_OPENING
] = "OPENING",
509 [BUS_AUTHENTICATING
] = "AUTHENTICATING",
510 [BUS_HELLO
] = "HELLO",
511 [BUS_RUNNING
] = "RUNNING",
512 [BUS_CLOSING
] = "CLOSING",
513 [BUS_CLOSED
] = "CLOSED",
517 assert(state
< _BUS_STATE_MAX
);
519 if (state
== bus
->state
)
522 log_debug("Bus %s: changing state %s → %s", strna(bus
->description
), table
[bus
->state
], table
[state
]);
526 static int hello_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
534 assert(IN_SET(bus
->state
, BUS_HELLO
, BUS_CLOSING
));
536 r
= sd_bus_message_get_errno(reply
);
540 r
= sd_bus_message_read(reply
, "s", &s
);
544 if (!service_name_is_valid(s
) || s
[0] != ':')
547 r
= free_and_strdup(&bus
->unique_name
, s
);
551 if (bus
->state
== BUS_HELLO
) {
552 bus_set_state(bus
, BUS_RUNNING
);
554 r
= synthesize_connected_signal(bus
);
562 static int bus_send_hello(sd_bus
*bus
) {
563 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
568 if (!bus
->bus_client
)
571 r
= sd_bus_message_new_method_call(
574 "org.freedesktop.DBus",
575 "/org/freedesktop/DBus",
576 "org.freedesktop.DBus",
581 return sd_bus_call_async(bus
, NULL
, m
, hello_callback
, NULL
, 0);
584 int bus_start_running(sd_bus
*bus
) {
585 struct reply_callback
*c
;
591 assert(bus
->state
< BUS_HELLO
);
593 /* We start all method call timeouts when we enter BUS_HELLO or BUS_RUNNING mode. At this point let's convert
594 * all relative to absolute timestamps. Note that we do not reshuffle the reply callback priority queue since
595 * adding a fixed value to all entries should not alter the internal order. */
597 n
= now(CLOCK_MONOTONIC
);
598 ORDERED_HASHMAP_FOREACH(c
, bus
->reply_callbacks
, i
) {
599 if (c
->timeout_usec
== 0)
602 c
->timeout_usec
= usec_add(n
, c
->timeout_usec
);
605 if (bus
->bus_client
) {
606 bus_set_state(bus
, BUS_HELLO
);
610 bus_set_state(bus
, BUS_RUNNING
);
612 r
= synthesize_connected_signal(bus
);
619 static int parse_address_key(const char **p
, const char *key
, char **value
) {
620 size_t l
, n
= 0, allocated
= 0;
621 _cleanup_free_
char *r
= NULL
;
630 if (strncmp(*p
, key
, l
) != 0)
643 while (!IN_SET(*a
, ';', ',', 0)) {
657 c
= (char) ((x
<< 4) | y
);
664 if (!GREEDY_REALLOC(r
, allocated
, n
+ 2))
682 free_and_replace(*value
, r
);
687 static void skip_address_key(const char **p
) {
691 *p
+= strcspn(*p
, ",");
697 static int parse_unix_address(sd_bus
*b
, const char **p
, char **guid
) {
698 _cleanup_free_
char *path
= NULL
, *abstract
= NULL
;
707 while (!IN_SET(**p
, 0, ';')) {
708 r
= parse_address_key(p
, "guid", guid
);
714 r
= parse_address_key(p
, "path", &path
);
720 r
= parse_address_key(p
, "abstract", &abstract
);
729 if (!path
&& !abstract
)
732 if (path
&& abstract
)
737 if (l
> sizeof(b
->sockaddr
.un
.sun_path
))
740 b
->sockaddr
.un
.sun_family
= AF_UNIX
;
741 strncpy(b
->sockaddr
.un
.sun_path
, path
, sizeof(b
->sockaddr
.un
.sun_path
));
742 b
->sockaddr_size
= offsetof(struct sockaddr_un
, sun_path
) + l
;
743 } else if (abstract
) {
744 l
= strlen(abstract
);
745 if (l
> sizeof(b
->sockaddr
.un
.sun_path
) - 1)
748 b
->sockaddr
.un
.sun_family
= AF_UNIX
;
749 b
->sockaddr
.un
.sun_path
[0] = 0;
750 strncpy(b
->sockaddr
.un
.sun_path
+1, abstract
, sizeof(b
->sockaddr
.un
.sun_path
)-1);
751 b
->sockaddr_size
= offsetof(struct sockaddr_un
, sun_path
) + 1 + l
;
759 static int parse_tcp_address(sd_bus
*b
, const char **p
, char **guid
) {
760 _cleanup_free_
char *host
= NULL
, *port
= NULL
, *family
= NULL
;
762 struct addrinfo
*result
, hints
= {
763 .ai_socktype
= SOCK_STREAM
,
764 .ai_flags
= AI_ADDRCONFIG
,
772 while (!IN_SET(**p
, 0, ';')) {
773 r
= parse_address_key(p
, "guid", guid
);
779 r
= parse_address_key(p
, "host", &host
);
785 r
= parse_address_key(p
, "port", &port
);
791 r
= parse_address_key(p
, "family", &family
);
804 if (streq(family
, "ipv4"))
805 hints
.ai_family
= AF_INET
;
806 else if (streq(family
, "ipv6"))
807 hints
.ai_family
= AF_INET6
;
812 r
= getaddrinfo(host
, port
, &hints
, &result
);
816 return -EADDRNOTAVAIL
;
818 memcpy(&b
->sockaddr
, result
->ai_addr
, result
->ai_addrlen
);
819 b
->sockaddr_size
= result
->ai_addrlen
;
821 freeaddrinfo(result
);
828 static int parse_exec_address(sd_bus
*b
, const char **p
, char **guid
) {
830 unsigned n_argv
= 0, j
;
832 size_t allocated
= 0;
840 while (!IN_SET(**p
, 0, ';')) {
841 r
= parse_address_key(p
, "guid", guid
);
847 r
= parse_address_key(p
, "path", &path
);
853 if (startswith(*p
, "argv")) {
857 ul
= strtoul(*p
+ 4, (char**) p
, 10);
858 if (errno
> 0 || **p
!= '=' || ul
> 256) {
866 if (!GREEDY_REALLOC0(argv
, allocated
, ul
+ 2)) {
874 r
= parse_address_key(p
, NULL
, argv
+ ul
);
889 /* Make sure there are no holes in the array, with the
890 * exception of argv[0] */
891 for (j
= 1; j
< n_argv
; j
++)
897 if (argv
&& argv
[0] == NULL
) {
898 argv
[0] = strdup(path
);
913 for (j
= 0; j
< n_argv
; j
++)
921 static int parse_container_unix_address(sd_bus
*b
, const char **p
, char **guid
) {
922 _cleanup_free_
char *machine
= NULL
, *pid
= NULL
;
930 while (!IN_SET(**p
, 0, ';')) {
931 r
= parse_address_key(p
, "guid", guid
);
937 r
= parse_address_key(p
, "machine", &machine
);
943 r
= parse_address_key(p
, "pid", &pid
);
952 if (!machine
== !pid
)
956 if (!machine_name_is_valid(machine
))
959 free_and_replace(b
->machine
, machine
);
961 b
->machine
= mfree(b
->machine
);
965 r
= parse_pid(pid
, &b
->nspid
);
971 b
->sockaddr
.un
.sun_family
= AF_UNIX
;
972 /* Note that we use the old /var/run prefix here, to increase compatibility with really old containers */
973 strncpy(b
->sockaddr
.un
.sun_path
, "/var/run/dbus/system_bus_socket", sizeof(b
->sockaddr
.un
.sun_path
));
974 b
->sockaddr_size
= SOCKADDR_UN_LEN(b
->sockaddr
.un
);
980 static void bus_reset_parsed_address(sd_bus
*b
) {
984 b
->sockaddr_size
= 0;
985 b
->exec_argv
= strv_free(b
->exec_argv
);
986 b
->exec_path
= mfree(b
->exec_path
);
987 b
->server_id
= SD_ID128_NULL
;
988 b
->machine
= mfree(b
->machine
);
992 static int bus_parse_next_address(sd_bus
*b
) {
993 _cleanup_free_
char *guid
= NULL
;
1001 if (b
->address
[b
->address_index
] == 0)
1004 bus_reset_parsed_address(b
);
1006 a
= b
->address
+ b
->address_index
;
1015 if (startswith(a
, "unix:")) {
1018 r
= parse_unix_address(b
, &a
, &guid
);
1023 } else if (startswith(a
, "tcp:")) {
1026 r
= parse_tcp_address(b
, &a
, &guid
);
1032 } else if (startswith(a
, "unixexec:")) {
1035 r
= parse_exec_address(b
, &a
, &guid
);
1041 } else if (startswith(a
, "x-machine-unix:")) {
1044 r
= parse_container_unix_address(b
, &a
, &guid
);
1057 r
= sd_id128_from_string(guid
, &b
->server_id
);
1062 b
->address_index
= a
- b
->address
;
1066 static void bus_kill_exec(sd_bus
*bus
) {
1067 if (pid_is_valid(bus
->busexec_pid
) > 0) {
1068 sigterm_wait(bus
->busexec_pid
);
1069 bus
->busexec_pid
= 0;
1073 static int bus_start_address(sd_bus
*b
) {
1079 bus_close_io_fds(b
);
1080 bus_close_inotify_fd(b
);
1084 /* If you provide multiple different bus-addresses, we
1085 * try all of them in order and use the first one that
1089 r
= bus_socket_exec(b
);
1090 else if ((b
->nspid
> 0 || b
->machine
) && b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
)
1091 r
= bus_container_connect_socket(b
);
1092 else if (b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
)
1093 r
= bus_socket_connect(b
);
1100 q
= bus_attach_io_events(b
);
1104 q
= bus_attach_inotify_event(b
);
1111 b
->last_connect_error
= -r
;
1114 r
= bus_parse_next_address(b
);
1118 return b
->last_connect_error
> 0 ? -b
->last_connect_error
: -ECONNREFUSED
;
1122 int bus_next_address(sd_bus
*b
) {
1125 bus_reset_parsed_address(b
);
1126 return bus_start_address(b
);
1129 static int bus_start_fd(sd_bus
*b
) {
1134 assert(b
->input_fd
>= 0);
1135 assert(b
->output_fd
>= 0);
1137 r
= fd_nonblock(b
->input_fd
, true);
1141 r
= fd_cloexec(b
->input_fd
, true);
1145 if (b
->input_fd
!= b
->output_fd
) {
1146 r
= fd_nonblock(b
->output_fd
, true);
1150 r
= fd_cloexec(b
->output_fd
, true);
1155 if (fstat(b
->input_fd
, &st
) < 0)
1158 return bus_socket_take_fd(b
);
1161 _public_
int sd_bus_start(sd_bus
*bus
) {
1164 assert_return(bus
, -EINVAL
);
1165 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1166 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
1167 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1169 bus_set_state(bus
, BUS_OPENING
);
1171 if (bus
->is_server
&& bus
->bus_client
)
1174 if (bus
->input_fd
>= 0)
1175 r
= bus_start_fd(bus
);
1176 else if (bus
->address
|| bus
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
|| bus
->exec_path
|| bus
->machine
)
1177 r
= bus_start_address(bus
);
1186 return bus_send_hello(bus
);
1189 _public_
int sd_bus_open_with_description(sd_bus
**ret
, const char *description
) {
1191 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1194 assert_return(ret
, -EINVAL
);
1196 /* Let's connect to the starter bus if it is set, and
1197 * otherwise to the bus that is appropropriate for the scope
1198 * we are running in */
1200 e
= secure_getenv("DBUS_STARTER_BUS_TYPE");
1202 if (streq(e
, "system"))
1203 return sd_bus_open_system_with_description(ret
, description
);
1204 else if (STR_IN_SET(e
, "session", "user"))
1205 return sd_bus_open_user_with_description(ret
, description
);
1208 e
= secure_getenv("DBUS_STARTER_ADDRESS");
1210 if (cg_pid_get_owner_uid(0, NULL
) >= 0)
1211 return sd_bus_open_user_with_description(ret
, description
);
1213 return sd_bus_open_system_with_description(ret
, description
);
1220 r
= sd_bus_set_address(b
, e
);
1224 b
->bus_client
= true;
1226 /* We don't know whether the bus is trusted or not, so better
1227 * be safe, and authenticate everything */
1229 b
->is_local
= false;
1230 b
->creds_mask
|= SD_BUS_CREDS_UID
| SD_BUS_CREDS_EUID
| SD_BUS_CREDS_EFFECTIVE_CAPS
;
1232 r
= sd_bus_start(b
);
1240 _public_
int sd_bus_open(sd_bus
**ret
) {
1241 return sd_bus_open_with_description(ret
, NULL
);
1244 int bus_set_address_system(sd_bus
*b
) {
1248 e
= secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1250 return sd_bus_set_address(b
, e
);
1252 return sd_bus_set_address(b
, DEFAULT_SYSTEM_BUS_ADDRESS
);
1255 _public_
int sd_bus_open_system_with_description(sd_bus
**ret
, const char *description
) {
1256 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1259 assert_return(ret
, -EINVAL
);
1266 r
= sd_bus_set_description(b
, description
);
1271 r
= bus_set_address_system(b
);
1275 b
->bus_client
= true;
1276 b
->is_system
= true;
1278 /* Let's do per-method access control on the system bus. We
1279 * need the caller's UID and capability set for that. */
1281 b
->creds_mask
|= SD_BUS_CREDS_UID
| SD_BUS_CREDS_EUID
| SD_BUS_CREDS_EFFECTIVE_CAPS
;
1284 r
= sd_bus_start(b
);
1292 _public_
int sd_bus_open_system(sd_bus
**ret
) {
1293 return sd_bus_open_system_with_description(ret
, NULL
);
1296 int bus_set_address_user(sd_bus
*b
) {
1298 _cleanup_free_
char *ee
= NULL
, *s
= NULL
;
1302 e
= secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1304 return sd_bus_set_address(b
, e
);
1306 e
= secure_getenv("XDG_RUNTIME_DIR");
1310 ee
= bus_address_escape(e
);
1314 if (asprintf(&s
, DEFAULT_USER_BUS_ADDRESS_FMT
, ee
) < 0)
1317 b
->address
= TAKE_PTR(s
);
1322 _public_
int sd_bus_open_user_with_description(sd_bus
**ret
, const char *description
) {
1323 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1326 assert_return(ret
, -EINVAL
);
1333 r
= sd_bus_set_description(b
, description
);
1338 r
= bus_set_address_user(b
);
1342 b
->bus_client
= true;
1345 /* We don't do any per-method access control on the user
1350 r
= sd_bus_start(b
);
1358 _public_
int sd_bus_open_user(sd_bus
**ret
) {
1359 return sd_bus_open_user_with_description(ret
, NULL
);
1362 int bus_set_address_system_remote(sd_bus
*b
, const char *host
) {
1363 _cleanup_free_
char *e
= NULL
;
1364 char *m
= NULL
, *c
= NULL
, *a
;
1369 /* Let's see if we shall enter some container */
1370 m
= strchr(host
, ':');
1374 /* Let's make sure this is not a port of some kind,
1375 * and is a valid machine name. */
1376 if (!in_charset(m
, DIGITS
) && machine_name_is_valid(m
)) {
1379 /* Cut out the host part */
1380 t
= strndupa(host
, m
- host
- 1);
1381 e
= bus_address_escape(t
);
1385 c
= strjoina(",argv5=--machine=", m
);
1390 e
= bus_address_escape(host
);
1395 a
= strjoin("unixexec:path=ssh,argv1=-xT,argv2=--,argv3=", e
, ",argv4=systemd-stdio-bridge", c
);
1399 return free_and_replace(b
->address
, a
);
1402 _public_
int sd_bus_open_system_remote(sd_bus
**ret
, const char *host
) {
1403 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1406 assert_return(host
, -EINVAL
);
1407 assert_return(ret
, -EINVAL
);
1413 r
= bus_set_address_system_remote(b
, host
);
1417 b
->bus_client
= true;
1419 b
->is_system
= true;
1420 b
->is_local
= false;
1422 r
= sd_bus_start(b
);
1430 int bus_set_address_system_machine(sd_bus
*b
, const char *machine
) {
1431 _cleanup_free_
char *e
= NULL
;
1437 e
= bus_address_escape(machine
);
1441 a
= strjoin("x-machine-unix:machine=", e
);
1445 return free_and_replace(b
->address
, a
);
1448 _public_
int sd_bus_open_system_machine(sd_bus
**ret
, const char *machine
) {
1449 _cleanup_(bus_freep
) sd_bus
*b
= NULL
;
1452 assert_return(machine
, -EINVAL
);
1453 assert_return(ret
, -EINVAL
);
1454 assert_return(machine_name_is_valid(machine
), -EINVAL
);
1460 r
= bus_set_address_system_machine(b
, machine
);
1464 b
->bus_client
= true;
1466 b
->is_system
= true;
1467 b
->is_local
= false;
1469 r
= sd_bus_start(b
);
1477 _public_
void sd_bus_close(sd_bus
*bus
) {
1480 if (bus
->state
== BUS_CLOSED
)
1482 if (bus_pid_changed(bus
))
1485 /* Don't leave ssh hanging around */
1488 bus_set_state(bus
, BUS_CLOSED
);
1490 sd_bus_detach_event(bus
);
1492 /* Drop all queued messages so that they drop references to
1493 * the bus object and the bus may be freed */
1494 bus_reset_queues(bus
);
1496 bus_close_io_fds(bus
);
1497 bus_close_inotify_fd(bus
);
1500 _public_ sd_bus
* sd_bus_flush_close_unref(sd_bus
*bus
) {
1504 /* Have to do this before flush() to prevent hang */
1510 return sd_bus_unref(bus
);
1513 void bus_enter_closing(sd_bus
*bus
) {
1516 if (!IN_SET(bus
->state
, BUS_WATCH_BIND
, BUS_OPENING
, BUS_AUTHENTICATING
, BUS_HELLO
, BUS_RUNNING
))
1519 bus_set_state(bus
, BUS_CLOSING
);
1522 _public_ sd_bus
*sd_bus_ref(sd_bus
*bus
) {
1526 assert_se(REFCNT_INC(bus
->n_ref
) >= 2);
1531 _public_ sd_bus
*sd_bus_unref(sd_bus
*bus
) {
1537 i
= REFCNT_DEC(bus
->n_ref
);
1541 return bus_free(bus
);
1544 _public_
int sd_bus_is_open(sd_bus
*bus
) {
1545 assert_return(bus
, -EINVAL
);
1546 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1547 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1549 return BUS_IS_OPEN(bus
->state
);
1552 _public_
int sd_bus_is_ready(sd_bus
*bus
) {
1553 assert_return(bus
, -EINVAL
);
1554 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1555 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1557 return bus
->state
== BUS_RUNNING
;
1560 _public_
int sd_bus_can_send(sd_bus
*bus
, char type
) {
1563 assert_return(bus
, -EINVAL
);
1564 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1565 assert_return(bus
->state
!= BUS_UNSET
, -ENOTCONN
);
1566 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1568 if (bus
->is_monitor
)
1571 if (type
== SD_BUS_TYPE_UNIX_FD
) {
1572 if (!bus
->accept_fd
)
1575 r
= bus_ensure_running(bus
);
1579 return bus
->can_fds
;
1582 return bus_type_is_valid(type
);
1585 _public_
int sd_bus_get_bus_id(sd_bus
*bus
, sd_id128_t
*id
) {
1588 assert_return(bus
, -EINVAL
);
1589 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
1590 assert_return(id
, -EINVAL
);
1591 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1593 r
= bus_ensure_running(bus
);
1597 *id
= bus
->server_id
;
1601 static int bus_seal_message(sd_bus
*b
, sd_bus_message
*m
, usec_t timeout
) {
1608 /* If we copy the same message to multiple
1609 * destinations, avoid using the same cookie
1611 b
->cookie
= MAX(b
->cookie
, BUS_MESSAGE_COOKIE(m
));
1616 timeout
= BUS_DEFAULT_TIMEOUT
;
1618 if (!m
->sender
&& b
->patch_sender
) {
1619 r
= sd_bus_message_set_sender(m
, b
->patch_sender
);
1624 return sd_bus_message_seal(m
, ++b
->cookie
, timeout
);
1627 static int bus_remarshal_message(sd_bus
*b
, sd_bus_message
**m
) {
1628 bool remarshal
= false;
1632 /* wrong packet version */
1633 if (b
->message_version
!= 0 && b
->message_version
!= (*m
)->header
->version
)
1636 /* wrong packet endianness */
1637 if (b
->message_endian
!= 0 && b
->message_endian
!= (*m
)->header
->endian
)
1640 return remarshal
? bus_message_remarshal(b
, m
) : 0;
1643 int bus_seal_synthetic_message(sd_bus
*b
, sd_bus_message
*m
) {
1647 /* Fake some timestamps, if they were requested, and not
1648 * already initialized */
1649 if (b
->attach_timestamp
) {
1650 if (m
->realtime
<= 0)
1651 m
->realtime
= now(CLOCK_REALTIME
);
1653 if (m
->monotonic
<= 0)
1654 m
->monotonic
= now(CLOCK_MONOTONIC
);
1657 /* The bus specification says the serial number cannot be 0,
1658 * hence let's fill something in for synthetic messages. Since
1659 * synthetic messages might have a fake sender and we don't
1660 * want to interfere with the real sender's serial numbers we
1661 * pick a fixed, artificial one. We use (uint32_t) -1 rather
1662 * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
1663 * even though kdbus can do 64bit. */
1664 return sd_bus_message_seal(m
, 0xFFFFFFFFULL
, 0);
1667 static int bus_write_message(sd_bus
*bus
, sd_bus_message
*m
, size_t *idx
) {
1673 r
= bus_socket_write_message(bus
, m
, idx
);
1677 if (*idx
>= BUS_MESSAGE_SIZE(m
))
1678 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",
1679 bus_message_type_to_string(m
->header
->type
),
1680 strna(sd_bus_message_get_sender(m
)),
1681 strna(sd_bus_message_get_destination(m
)),
1682 strna(sd_bus_message_get_path(m
)),
1683 strna(sd_bus_message_get_interface(m
)),
1684 strna(sd_bus_message_get_member(m
)),
1685 BUS_MESSAGE_COOKIE(m
),
1687 strna(m
->root_container
.signature
),
1688 strna(m
->error
.name
),
1689 strna(m
->error
.message
));
1694 static int dispatch_wqueue(sd_bus
*bus
) {
1698 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1700 while (bus
->wqueue_size
> 0) {
1702 r
= bus_write_message(bus
, bus
->wqueue
[0], &bus
->windex
);
1706 /* Didn't do anything this time */
1708 else if (bus
->windex
>= BUS_MESSAGE_SIZE(bus
->wqueue
[0])) {
1709 /* Fully written. Let's drop the entry from
1712 * This isn't particularly optimized, but
1713 * well, this is supposed to be our worst-case
1714 * buffer only, and the socket buffer is
1715 * supposed to be our primary buffer, and if
1716 * it got full, then all bets are off
1720 sd_bus_message_unref(bus
->wqueue
[0]);
1721 memmove(bus
->wqueue
, bus
->wqueue
+ 1, sizeof(sd_bus_message
*) * bus
->wqueue_size
);
1731 static int bus_read_message(sd_bus
*bus
, bool hint_priority
, int64_t priority
) {
1734 return bus_socket_read_message(bus
);
1737 int bus_rqueue_make_room(sd_bus
*bus
) {
1740 if (bus
->rqueue_size
>= BUS_RQUEUE_MAX
)
1743 if (!GREEDY_REALLOC(bus
->rqueue
, bus
->rqueue_allocated
, bus
->rqueue_size
+ 1))
1749 static int dispatch_rqueue(sd_bus
*bus
, bool hint_priority
, int64_t priority
, sd_bus_message
**m
) {
1754 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1756 /* Note that the priority logic is only available on kdbus,
1757 * where the rqueue is unused. We check the rqueue here
1758 * anyway, because it's simple... */
1761 if (bus
->rqueue_size
> 0) {
1762 /* Dispatch a queued message */
1764 *m
= bus
->rqueue
[0];
1766 memmove(bus
->rqueue
, bus
->rqueue
+ 1, sizeof(sd_bus_message
*) * bus
->rqueue_size
);
1770 /* Try to read a new message */
1771 r
= bus_read_message(bus
, hint_priority
, priority
);
1781 _public_
int sd_bus_send(sd_bus
*bus
, sd_bus_message
*_m
, uint64_t *cookie
) {
1782 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= sd_bus_message_ref(_m
);
1785 assert_return(m
, -EINVAL
);
1790 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1792 if (!BUS_IS_OPEN(bus
->state
))
1796 r
= sd_bus_can_send(bus
, SD_BUS_TYPE_UNIX_FD
);
1803 /* If the cookie number isn't kept, then we know that no reply
1805 if (!cookie
&& !m
->sealed
)
1806 m
->header
->flags
|= BUS_MESSAGE_NO_REPLY_EXPECTED
;
1808 r
= bus_seal_message(bus
, m
, 0);
1812 /* Remarshall if we have to. This will possibly unref the
1813 * message and place a replacement in m */
1814 r
= bus_remarshal_message(bus
, &m
);
1818 /* If this is a reply and no reply was requested, then let's
1819 * suppress this, if we can */
1823 if (IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
) && bus
->wqueue_size
<= 0) {
1826 r
= bus_write_message(bus
, m
, &idx
);
1828 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
1829 bus_enter_closing(bus
);
1836 if (idx
< BUS_MESSAGE_SIZE(m
)) {
1837 /* Wasn't fully written. So let's remember how
1838 * much was written. Note that the first entry
1839 * of the wqueue array is always allocated so
1840 * that we always can remember how much was
1842 bus
->wqueue
[0] = sd_bus_message_ref(m
);
1843 bus
->wqueue_size
= 1;
1848 /* Just append it to the queue. */
1850 if (bus
->wqueue_size
>= BUS_WQUEUE_MAX
)
1853 if (!GREEDY_REALLOC(bus
->wqueue
, bus
->wqueue_allocated
, bus
->wqueue_size
+ 1))
1856 bus
->wqueue
[bus
->wqueue_size
++] = sd_bus_message_ref(m
);
1861 *cookie
= BUS_MESSAGE_COOKIE(m
);
1866 _public_
int sd_bus_send_to(sd_bus
*bus
, sd_bus_message
*m
, const char *destination
, uint64_t *cookie
) {
1869 assert_return(m
, -EINVAL
);
1874 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1876 if (!BUS_IS_OPEN(bus
->state
))
1879 if (!streq_ptr(m
->destination
, destination
)) {
1884 r
= sd_bus_message_set_destination(m
, destination
);
1889 return sd_bus_send(bus
, m
, cookie
);
1892 static usec_t
calc_elapse(sd_bus
*bus
, uint64_t usec
) {
1895 if (usec
== (uint64_t) -1)
1898 /* We start all timeouts the instant we enter BUS_HELLO/BUS_RUNNING state, so that the don't run in parallel
1899 * with any connection setup states. Hence, if a method callback is started earlier than that we just store the
1900 * relative timestamp, and afterwards the absolute one. */
1902 if (IN_SET(bus
->state
, BUS_WATCH_BIND
, BUS_OPENING
, BUS_AUTHENTICATING
))
1905 return now(CLOCK_MONOTONIC
) + usec
;
1908 static int timeout_compare(const void *a
, const void *b
) {
1909 const struct reply_callback
*x
= a
, *y
= b
;
1911 if (x
->timeout_usec
!= 0 && y
->timeout_usec
== 0)
1914 if (x
->timeout_usec
== 0 && y
->timeout_usec
!= 0)
1917 if (x
->timeout_usec
< y
->timeout_usec
)
1920 if (x
->timeout_usec
> y
->timeout_usec
)
1926 _public_
int sd_bus_call_async(
1930 sd_bus_message_handler_t callback
,
1934 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= sd_bus_message_ref(_m
);
1935 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*s
= NULL
;
1938 assert_return(m
, -EINVAL
);
1939 assert_return(m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
, -EINVAL
);
1940 assert_return(!m
->sealed
|| (!!callback
== !(m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
)), -EINVAL
);
1945 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1947 if (!BUS_IS_OPEN(bus
->state
))
1950 /* If no callback is specified and there's no interest in a slot, then there's no reason to ask for a reply */
1951 if (!callback
&& !slot
&& !m
->sealed
)
1952 m
->header
->flags
|= BUS_MESSAGE_NO_REPLY_EXPECTED
;
1954 r
= ordered_hashmap_ensure_allocated(&bus
->reply_callbacks
, &uint64_hash_ops
);
1958 r
= prioq_ensure_allocated(&bus
->reply_callbacks_prioq
, timeout_compare
);
1962 r
= bus_seal_message(bus
, m
, usec
);
1966 r
= bus_remarshal_message(bus
, &m
);
1970 if (slot
|| callback
) {
1971 s
= bus_slot_allocate(bus
, !slot
, BUS_REPLY_CALLBACK
, sizeof(struct reply_callback
), userdata
);
1975 s
->reply_callback
.callback
= callback
;
1977 s
->reply_callback
.cookie
= BUS_MESSAGE_COOKIE(m
);
1978 r
= ordered_hashmap_put(bus
->reply_callbacks
, &s
->reply_callback
.cookie
, &s
->reply_callback
);
1980 s
->reply_callback
.cookie
= 0;
1984 s
->reply_callback
.timeout_usec
= calc_elapse(bus
, m
->timeout
);
1985 if (s
->reply_callback
.timeout_usec
!= 0) {
1986 r
= prioq_put(bus
->reply_callbacks_prioq
, &s
->reply_callback
, &s
->reply_callback
.prioq_idx
);
1988 s
->reply_callback
.timeout_usec
= 0;
1994 r
= sd_bus_send(bus
, m
, s
? &s
->reply_callback
.cookie
: NULL
);
2005 int bus_ensure_running(sd_bus
*bus
) {
2010 if (IN_SET(bus
->state
, BUS_UNSET
, BUS_CLOSED
, BUS_CLOSING
))
2012 if (bus
->state
== BUS_RUNNING
)
2016 r
= sd_bus_process(bus
, NULL
);
2019 if (bus
->state
== BUS_RUNNING
)
2024 r
= sd_bus_wait(bus
, (uint64_t) -1);
2030 _public_
int sd_bus_call(
2034 sd_bus_error
*error
,
2035 sd_bus_message
**reply
) {
2037 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= sd_bus_message_ref(_m
);
2043 bus_assert_return(m
, -EINVAL
, error
);
2044 bus_assert_return(m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
, -EINVAL
, error
);
2045 bus_assert_return(!(m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
), -EINVAL
, error
);
2046 bus_assert_return(!bus_error_is_dirty(error
), -EINVAL
, error
);
2051 bus_assert_return(!bus_pid_changed(bus
), -ECHILD
, error
);
2053 if (!BUS_IS_OPEN(bus
->state
)) {
2058 r
= bus_ensure_running(bus
);
2062 i
= bus
->rqueue_size
;
2064 r
= bus_seal_message(bus
, m
, usec
);
2068 r
= bus_remarshal_message(bus
, &m
);
2072 r
= sd_bus_send(bus
, m
, &cookie
);
2076 timeout
= calc_elapse(bus
, m
->timeout
);
2081 while (i
< bus
->rqueue_size
) {
2082 sd_bus_message
*incoming
= NULL
;
2084 incoming
= bus
->rqueue
[i
];
2086 if (incoming
->reply_cookie
== cookie
) {
2087 /* Found a match! */
2089 memmove(bus
->rqueue
+ i
, bus
->rqueue
+ i
+ 1, sizeof(sd_bus_message
*) * (bus
->rqueue_size
- i
- 1));
2091 log_debug_bus_message(incoming
);
2093 if (incoming
->header
->type
== SD_BUS_MESSAGE_METHOD_RETURN
) {
2095 if (incoming
->n_fds
<= 0 || bus
->accept_fd
) {
2099 sd_bus_message_unref(incoming
);
2104 r
= sd_bus_error_setf(error
, SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Reply message contained file descriptors which I couldn't accept. Sorry.");
2105 sd_bus_message_unref(incoming
);
2108 } else if (incoming
->header
->type
== SD_BUS_MESSAGE_METHOD_ERROR
) {
2109 r
= sd_bus_error_copy(error
, &incoming
->error
);
2110 sd_bus_message_unref(incoming
);
2117 } else if (BUS_MESSAGE_COOKIE(incoming
) == cookie
&&
2120 streq(bus
->unique_name
, incoming
->sender
)) {
2122 memmove(bus
->rqueue
+ i
, bus
->rqueue
+ i
+ 1, sizeof(sd_bus_message
*) * (bus
->rqueue_size
- i
- 1));
2125 /* Our own message? Somebody is trying
2126 * to send its own client a message,
2127 * let's not dead-lock, let's fail
2130 sd_bus_message_unref(incoming
);
2135 /* Try to read more, right-away */
2139 r
= bus_read_message(bus
, false, 0);
2141 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
2142 bus_enter_closing(bus
);
2154 n
= now(CLOCK_MONOTONIC
);
2162 left
= (uint64_t) -1;
2164 r
= bus_poll(bus
, true, left
);
2172 r
= dispatch_wqueue(bus
);
2174 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
2175 bus_enter_closing(bus
);
2184 return sd_bus_error_set_errno(error
, r
);
2187 _public_
int sd_bus_get_fd(sd_bus
*bus
) {
2189 assert_return(bus
, -EINVAL
);
2190 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2191 assert_return(bus
->input_fd
== bus
->output_fd
, -EPERM
);
2192 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2194 if (bus
->state
== BUS_CLOSED
)
2197 if (bus
->inotify_fd
>= 0)
2198 return bus
->inotify_fd
;
2200 if (bus
->input_fd
>= 0)
2201 return bus
->input_fd
;
2206 _public_
int sd_bus_get_events(sd_bus
*bus
) {
2209 assert_return(bus
, -EINVAL
);
2210 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2211 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2213 switch (bus
->state
) {
2219 case BUS_WATCH_BIND
:
2227 case BUS_AUTHENTICATING
:
2228 if (bus_socket_auth_needs_write(bus
))
2236 if (bus
->rqueue_size
<= 0)
2238 if (bus
->wqueue_size
> 0)
2246 assert_not_reached("Unknown state");
2252 _public_
int sd_bus_get_timeout(sd_bus
*bus
, uint64_t *timeout_usec
) {
2253 struct reply_callback
*c
;
2255 assert_return(bus
, -EINVAL
);
2256 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2257 assert_return(timeout_usec
, -EINVAL
);
2258 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2260 if (!BUS_IS_OPEN(bus
->state
) && bus
->state
!= BUS_CLOSING
)
2263 if (bus
->track_queue
) {
2268 switch (bus
->state
) {
2270 case BUS_AUTHENTICATING
:
2271 *timeout_usec
= bus
->auth_timeout
;
2276 if (bus
->rqueue_size
> 0) {
2281 c
= prioq_peek(bus
->reply_callbacks_prioq
);
2283 *timeout_usec
= (uint64_t) -1;
2287 if (c
->timeout_usec
== 0) {
2288 *timeout_usec
= (uint64_t) -1;
2292 *timeout_usec
= c
->timeout_usec
;
2299 case BUS_WATCH_BIND
:
2301 *timeout_usec
= (uint64_t) -1;
2305 assert_not_reached("Unknown or unexpected stat");
2309 static int process_timeout(sd_bus
*bus
) {
2310 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2311 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* m
= NULL
;
2312 struct reply_callback
*c
;
2319 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
2321 c
= prioq_peek(bus
->reply_callbacks_prioq
);
2325 n
= now(CLOCK_MONOTONIC
);
2326 if (c
->timeout_usec
> n
)
2329 r
= bus_message_new_synthetic_error(
2332 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY
, "Method call timed out"),
2337 r
= bus_seal_synthetic_message(bus
, m
);
2341 assert_se(prioq_pop(bus
->reply_callbacks_prioq
) == c
);
2342 c
->timeout_usec
= 0;
2344 ordered_hashmap_remove(bus
->reply_callbacks
, &c
->cookie
);
2347 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
2349 bus
->iteration_counter
++;
2351 is_hello
= bus
->state
== BUS_HELLO
&& c
->callback
== hello_callback
;
2353 bus
->current_message
= m
;
2354 bus
->current_slot
= sd_bus_slot_ref(slot
);
2355 bus
->current_handler
= c
->callback
;
2356 bus
->current_userdata
= slot
->userdata
;
2357 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
2358 bus
->current_userdata
= NULL
;
2359 bus
->current_handler
= NULL
;
2360 bus
->current_slot
= NULL
;
2361 bus
->current_message
= NULL
;
2363 if (slot
->floating
) {
2364 bus_slot_disconnect(slot
);
2365 sd_bus_slot_unref(slot
);
2368 sd_bus_slot_unref(slot
);
2370 /* When this is the hello message and it timed out, then make sure to propagate the error up, don't just log
2371 * and ignore the callback handler's return value. */
2375 return bus_maybe_reply_error(m
, r
, &error_buffer
);
2378 static int process_hello(sd_bus
*bus
, sd_bus_message
*m
) {
2382 if (bus
->state
!= BUS_HELLO
)
2385 /* Let's make sure the first message on the bus is the HELLO
2386 * reply. But note that we don't actually parse the message
2387 * here (we leave that to the usual handling), we just verify
2388 * we don't let any earlier msg through. */
2390 if (!IN_SET(m
->header
->type
, SD_BUS_MESSAGE_METHOD_RETURN
, SD_BUS_MESSAGE_METHOD_ERROR
))
2393 if (m
->reply_cookie
!= 1)
2399 static int process_reply(sd_bus
*bus
, sd_bus_message
*m
) {
2400 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*synthetic_reply
= NULL
;
2401 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2402 struct reply_callback
*c
;
2410 if (!IN_SET(m
->header
->type
, SD_BUS_MESSAGE_METHOD_RETURN
, SD_BUS_MESSAGE_METHOD_ERROR
))
2413 if (m
->destination
&& bus
->unique_name
&& !streq_ptr(m
->destination
, bus
->unique_name
))
2416 c
= ordered_hashmap_remove(bus
->reply_callbacks
, &m
->reply_cookie
);
2422 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
2424 if (m
->n_fds
> 0 && !bus
->accept_fd
) {
2426 /* If the reply contained a file descriptor which we
2427 * didn't want we pass an error instead. */
2429 r
= bus_message_new_synthetic_error(
2432 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Reply message contained file descriptor"),
2437 /* Copy over original timestamp */
2438 synthetic_reply
->realtime
= m
->realtime
;
2439 synthetic_reply
->monotonic
= m
->monotonic
;
2440 synthetic_reply
->seqnum
= m
->seqnum
;
2442 r
= bus_seal_synthetic_message(bus
, synthetic_reply
);
2446 m
= synthetic_reply
;
2448 r
= sd_bus_message_rewind(m
, true);
2453 if (c
->timeout_usec
!= 0) {
2454 prioq_remove(bus
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
2455 c
->timeout_usec
= 0;
2458 is_hello
= bus
->state
== BUS_HELLO
&& c
->callback
== hello_callback
;
2460 bus
->current_slot
= sd_bus_slot_ref(slot
);
2461 bus
->current_handler
= c
->callback
;
2462 bus
->current_userdata
= slot
->userdata
;
2463 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
2464 bus
->current_userdata
= NULL
;
2465 bus
->current_handler
= NULL
;
2466 bus
->current_slot
= NULL
;
2468 if (slot
->floating
) {
2469 bus_slot_disconnect(slot
);
2470 sd_bus_slot_unref(slot
);
2473 sd_bus_slot_unref(slot
);
2475 /* When this is the hello message and it failed, then make sure to propagate the error up, don't just log and
2476 * ignore the callback handler's return value. */
2480 return bus_maybe_reply_error(m
, r
, &error_buffer
);
2483 static int process_filter(sd_bus
*bus
, sd_bus_message
*m
) {
2484 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2485 struct filter_callback
*l
;
2492 bus
->filter_callbacks_modified
= false;
2494 LIST_FOREACH(callbacks
, l
, bus
->filter_callbacks
) {
2497 if (bus
->filter_callbacks_modified
)
2500 /* Don't run this more than once per iteration */
2501 if (l
->last_iteration
== bus
->iteration_counter
)
2504 l
->last_iteration
= bus
->iteration_counter
;
2506 r
= sd_bus_message_rewind(m
, true);
2510 slot
= container_of(l
, sd_bus_slot
, filter_callback
);
2512 bus
->current_slot
= sd_bus_slot_ref(slot
);
2513 bus
->current_handler
= l
->callback
;
2514 bus
->current_userdata
= slot
->userdata
;
2515 r
= l
->callback(m
, slot
->userdata
, &error_buffer
);
2516 bus
->current_userdata
= NULL
;
2517 bus
->current_handler
= NULL
;
2518 bus
->current_slot
= sd_bus_slot_unref(slot
);
2520 r
= bus_maybe_reply_error(m
, r
, &error_buffer
);
2526 } while (bus
->filter_callbacks_modified
);
2531 static int process_match(sd_bus
*bus
, sd_bus_message
*m
) {
2538 bus
->match_callbacks_modified
= false;
2540 r
= bus_match_run(bus
, &bus
->match_callbacks
, m
);
2544 } while (bus
->match_callbacks_modified
);
2549 static int process_builtin(sd_bus
*bus
, sd_bus_message
*m
) {
2550 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2556 if (bus
->is_monitor
)
2559 if (bus
->manual_peer_interface
)
2562 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_CALL
)
2565 if (!streq_ptr(m
->interface
, "org.freedesktop.DBus.Peer"))
2568 if (m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
)
2571 if (streq_ptr(m
->member
, "Ping"))
2572 r
= sd_bus_message_new_method_return(m
, &reply
);
2573 else if (streq_ptr(m
->member
, "GetMachineId")) {
2577 r
= sd_id128_get_machine(&id
);
2581 r
= sd_bus_message_new_method_return(m
, &reply
);
2585 r
= sd_bus_message_append(reply
, "s", sd_id128_to_string(id
, sid
));
2587 r
= sd_bus_message_new_method_errorf(
2589 SD_BUS_ERROR_UNKNOWN_METHOD
,
2590 "Unknown method '%s' on interface '%s'.", m
->member
, m
->interface
);
2596 r
= sd_bus_send(bus
, reply
, NULL
);
2603 static int process_fd_check(sd_bus
*bus
, sd_bus_message
*m
) {
2607 /* If we got a message with a file descriptor which we didn't
2608 * want to accept, then let's drop it. How can this even
2609 * happen? For example, when the kernel queues a message into
2610 * an activatable names's queue which allows fds, and then is
2611 * delivered to us later even though we ourselves did not
2614 if (bus
->is_monitor
)
2623 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_CALL
)
2624 return 1; /* just eat it up */
2626 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Message contains file descriptors, which I cannot accept. Sorry.");
2629 static int process_message(sd_bus
*bus
, sd_bus_message
*m
) {
2635 bus
->current_message
= m
;
2636 bus
->iteration_counter
++;
2638 log_debug_bus_message(m
);
2640 r
= process_hello(bus
, m
);
2644 r
= process_reply(bus
, m
);
2648 r
= process_fd_check(bus
, m
);
2652 r
= process_filter(bus
, m
);
2656 r
= process_match(bus
, m
);
2660 r
= process_builtin(bus
, m
);
2664 r
= bus_process_object(bus
, m
);
2667 bus
->current_message
= NULL
;
2671 static int dispatch_track(sd_bus
*bus
) {
2674 if (!bus
->track_queue
)
2677 bus_track_dispatch(bus
->track_queue
);
2681 static int process_running(sd_bus
*bus
, bool hint_priority
, int64_t priority
, sd_bus_message
**ret
) {
2682 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2686 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
2688 r
= process_timeout(bus
);
2692 r
= dispatch_wqueue(bus
);
2696 r
= dispatch_track(bus
);
2700 r
= dispatch_rqueue(bus
, hint_priority
, priority
, &m
);
2706 r
= process_message(bus
, m
);
2711 r
= sd_bus_message_rewind(m
, true);
2720 if (m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
) {
2722 log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s",
2723 strna(sd_bus_message_get_sender(m
)),
2724 strna(sd_bus_message_get_path(m
)),
2725 strna(sd_bus_message_get_interface(m
)),
2726 strna(sd_bus_message_get_member(m
)));
2728 r
= sd_bus_reply_method_errorf(
2730 SD_BUS_ERROR_UNKNOWN_OBJECT
,
2731 "Unknown object '%s'.", m
->path
);
2745 static int bus_exit_now(sd_bus
*bus
) {
2748 /* Exit due to close, if this is requested. If this is bus object is attached to an event source, invokes
2749 * sd_event_exit(), otherwise invokes libc exit(). */
2751 if (bus
->exited
) /* did we already exit? */
2753 if (!bus
->exit_triggered
) /* was the exit condition triggered? */
2755 if (!bus
->exit_on_disconnect
) /* Shall we actually exit on disconnection? */
2758 bus
->exited
= true; /* never exit more than once */
2760 log_debug("Bus connection disconnected, exiting.");
2763 return sd_event_exit(bus
->event
, EXIT_FAILURE
);
2767 assert_not_reached("exit() didn't exit?");
2770 static int process_closing_reply_callback(sd_bus
*bus
, struct reply_callback
*c
) {
2771 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2772 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2779 r
= bus_message_new_synthetic_error(
2782 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY
, "Connection terminated"),
2787 r
= bus_seal_synthetic_message(bus
, m
);
2791 if (c
->timeout_usec
!= 0) {
2792 prioq_remove(bus
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
2793 c
->timeout_usec
= 0;
2796 ordered_hashmap_remove(bus
->reply_callbacks
, &c
->cookie
);
2799 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
2801 bus
->iteration_counter
++;
2803 bus
->current_message
= m
;
2804 bus
->current_slot
= sd_bus_slot_ref(slot
);
2805 bus
->current_handler
= c
->callback
;
2806 bus
->current_userdata
= slot
->userdata
;
2807 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
2808 bus
->current_userdata
= NULL
;
2809 bus
->current_handler
= NULL
;
2810 bus
->current_slot
= NULL
;
2811 bus
->current_message
= NULL
;
2813 if (slot
->floating
) {
2814 bus_slot_disconnect(slot
);
2815 sd_bus_slot_unref(slot
);
2818 sd_bus_slot_unref(slot
);
2820 return bus_maybe_reply_error(m
, r
, &error_buffer
);
2823 static int process_closing(sd_bus
*bus
, sd_bus_message
**ret
) {
2824 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2825 struct reply_callback
*c
;
2829 assert(bus
->state
== BUS_CLOSING
);
2831 /* First, fail all outstanding method calls */
2832 c
= ordered_hashmap_first(bus
->reply_callbacks
);
2834 return process_closing_reply_callback(bus
, c
);
2836 /* Then, fake-drop all remaining bus tracking references */
2838 bus_track_close(bus
->tracks
);
2842 /* Then, synthesize a Disconnected message */
2843 r
= sd_bus_message_new_signal(
2846 "/org/freedesktop/DBus/Local",
2847 "org.freedesktop.DBus.Local",
2852 bus_message_set_sender_local(bus
, m
);
2854 r
= bus_seal_synthetic_message(bus
, m
);
2860 bus
->current_message
= m
;
2861 bus
->iteration_counter
++;
2863 r
= process_filter(bus
, m
);
2867 r
= process_match(bus
, m
);
2871 /* Nothing else to do, exit now, if the condition holds */
2872 bus
->exit_triggered
= true;
2873 (void) bus_exit_now(bus
);
2881 bus
->current_message
= NULL
;
2886 static int bus_process_internal(sd_bus
*bus
, bool hint_priority
, int64_t priority
, sd_bus_message
**ret
) {
2887 BUS_DONT_DESTROY(bus
);
2890 /* Returns 0 when we didn't do anything. This should cause the
2891 * caller to invoke sd_bus_wait() before returning the next
2892 * time. Returns > 0 when we did something, which possibly
2893 * means *ret is filled in with an unprocessed message. */
2895 assert_return(bus
, -EINVAL
);
2896 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
2897 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2899 /* We don't allow recursively invoking sd_bus_process(). */
2900 assert_return(!bus
->current_message
, -EBUSY
);
2901 assert(!bus
->current_slot
);
2903 switch (bus
->state
) {
2911 case BUS_WATCH_BIND
:
2912 r
= bus_socket_process_watch_bind(bus
);
2916 r
= bus_socket_process_opening(bus
);
2919 case BUS_AUTHENTICATING
:
2920 r
= bus_socket_process_authenticating(bus
);
2925 r
= process_running(bus
, hint_priority
, priority
, ret
);
2929 /* This branch initializes *ret, hence we don't use the generic error checking below */
2933 return process_closing(bus
, ret
);
2936 assert_not_reached("Unknown state");
2939 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
2940 bus_enter_closing(bus
);
2951 _public_
int sd_bus_process(sd_bus
*bus
, sd_bus_message
**ret
) {
2952 return bus_process_internal(bus
, false, 0, ret
);
2955 _public_
int sd_bus_process_priority(sd_bus
*bus
, int64_t priority
, sd_bus_message
**ret
) {
2956 return bus_process_internal(bus
, true, priority
, ret
);
2959 static int bus_poll(sd_bus
*bus
, bool need_more
, uint64_t timeout_usec
) {
2960 struct pollfd p
[2] = {};
2963 usec_t m
= USEC_INFINITY
;
2967 if (bus
->state
== BUS_CLOSING
)
2970 if (!BUS_IS_OPEN(bus
->state
))
2973 if (bus
->state
== BUS_WATCH_BIND
) {
2974 assert(bus
->inotify_fd
>= 0);
2976 p
[0].events
= POLLIN
;
2977 p
[0].fd
= bus
->inotify_fd
;
2982 e
= sd_bus_get_events(bus
);
2987 /* The caller really needs some more data, he doesn't
2988 * care about what's already read, or any timeouts
2989 * except its own. */
2993 /* The caller wants to process if there's something to
2994 * process, but doesn't care otherwise */
2996 r
= sd_bus_get_timeout(bus
, &until
);
3000 m
= usec_sub_unsigned(until
, now(CLOCK_MONOTONIC
));
3003 p
[0].fd
= bus
->input_fd
;
3004 if (bus
->output_fd
== bus
->input_fd
) {
3008 p
[0].events
= e
& POLLIN
;
3009 p
[1].fd
= bus
->output_fd
;
3010 p
[1].events
= e
& POLLOUT
;
3015 if (timeout_usec
!= (uint64_t) -1 && (m
== USEC_INFINITY
|| timeout_usec
< m
))
3018 r
= ppoll(p
, n
, m
== USEC_INFINITY
? NULL
: timespec_store(&ts
, m
), NULL
);
3022 return r
> 0 ? 1 : 0;
3025 _public_
int sd_bus_wait(sd_bus
*bus
, uint64_t timeout_usec
) {
3027 assert_return(bus
, -EINVAL
);
3028 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3029 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3031 if (bus
->state
== BUS_CLOSING
)
3034 if (!BUS_IS_OPEN(bus
->state
))
3037 if (bus
->rqueue_size
> 0)
3040 return bus_poll(bus
, false, timeout_usec
);
3043 _public_
int sd_bus_flush(sd_bus
*bus
) {
3046 assert_return(bus
, -EINVAL
);
3047 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3048 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3050 if (bus
->state
== BUS_CLOSING
)
3053 if (!BUS_IS_OPEN(bus
->state
))
3056 /* We never were connected? Don't hang in inotify for good, as there's no timeout set for it */
3057 if (bus
->state
== BUS_WATCH_BIND
)
3060 r
= bus_ensure_running(bus
);
3064 if (bus
->wqueue_size
<= 0)
3068 r
= dispatch_wqueue(bus
);
3070 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
3071 bus_enter_closing(bus
);
3078 if (bus
->wqueue_size
<= 0)
3081 r
= bus_poll(bus
, false, (uint64_t) -1);
3087 _public_
int sd_bus_add_filter(
3090 sd_bus_message_handler_t callback
,
3095 assert_return(bus
, -EINVAL
);
3096 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3097 assert_return(callback
, -EINVAL
);
3098 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3100 s
= bus_slot_allocate(bus
, !slot
, BUS_FILTER_CALLBACK
, sizeof(struct filter_callback
), userdata
);
3104 s
->filter_callback
.callback
= callback
;
3106 bus
->filter_callbacks_modified
= true;
3107 LIST_PREPEND(callbacks
, bus
->filter_callbacks
, &s
->filter_callback
);
3115 static int add_match_callback(
3118 sd_bus_error
*ret_error
) {
3120 sd_bus_slot
*match_slot
= userdata
;
3121 bool failed
= false;
3127 sd_bus_slot_ref(match_slot
);
3129 if (sd_bus_message_is_method_error(m
, NULL
)) {
3130 log_debug_errno(sd_bus_message_get_errno(m
),
3131 "Unable to add match %s, failing connection: %s",
3132 match_slot
->match_callback
.match_string
,
3133 sd_bus_message_get_error(m
)->message
);
3137 log_debug("Match %s successfully installed.", match_slot
->match_callback
.match_string
);
3139 if (match_slot
->match_callback
.install_callback
) {
3142 bus
= sd_bus_message_get_bus(m
);
3144 /* This function has been called as slot handler, and we want to call another slot handler. Let's
3145 * update the slot callback metadata temporarily with our own data, and then revert back to the old
3148 assert(bus
->current_slot
== match_slot
->match_callback
.install_slot
);
3149 assert(bus
->current_handler
== add_match_callback
);
3150 assert(bus
->current_userdata
== userdata
);
3152 bus
->current_slot
= match_slot
;
3153 bus
->current_handler
= match_slot
->match_callback
.install_callback
;
3154 bus
->current_userdata
= match_slot
->userdata
;
3156 r
= match_slot
->match_callback
.install_callback(m
, match_slot
->userdata
, ret_error
);
3158 bus
->current_slot
= match_slot
->match_callback
.install_slot
;
3159 bus
->current_handler
= add_match_callback
;
3160 bus
->current_userdata
= userdata
;
3162 match_slot
->match_callback
.install_slot
= sd_bus_slot_unref(match_slot
->match_callback
.install_slot
);
3164 if (failed
) /* Generic failure handling: destroy the connection */
3165 bus_enter_closing(sd_bus_message_get_bus(m
));
3170 if (failed
&& match_slot
->floating
) {
3171 bus_slot_disconnect(match_slot
);
3172 sd_bus_slot_unref(match_slot
);
3175 sd_bus_slot_unref(match_slot
);
3180 static int bus_add_match_full(
3185 sd_bus_message_handler_t callback
,
3186 sd_bus_message_handler_t install_callback
,
3189 struct bus_match_component
*components
= NULL
;
3190 unsigned n_components
= 0;
3191 sd_bus_slot
*s
= NULL
;
3194 assert_return(bus
, -EINVAL
);
3195 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3196 assert_return(match
, -EINVAL
);
3197 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3199 r
= bus_match_parse(match
, &components
, &n_components
);
3203 s
= bus_slot_allocate(bus
, !slot
, BUS_MATCH_CALLBACK
, sizeof(struct match_callback
), userdata
);
3209 s
->match_callback
.callback
= callback
;
3210 s
->match_callback
.install_callback
= install_callback
;
3212 if (bus
->bus_client
) {
3213 enum bus_match_scope scope
;
3215 scope
= bus_match_get_scope(components
, n_components
);
3217 /* Do not install server-side matches for matches against the local service, interface or bus path. */
3218 if (scope
!= BUS_MATCH_LOCAL
) {
3220 /* We store the original match string, so that we can use it to remove the match again. */
3222 s
->match_callback
.match_string
= strdup(match
);
3223 if (!s
->match_callback
.match_string
) {
3229 r
= bus_add_match_internal_async(bus
,
3230 &s
->match_callback
.install_slot
,
3231 s
->match_callback
.match_string
,
3238 /* Make the slot of the match call floating now. We need the reference, but we don't
3239 * want that this match pins the bus object, hence we first create it non-floating, but
3240 * then make it floating. */
3241 r
= sd_bus_slot_set_floating(s
->match_callback
.install_slot
, true);
3243 r
= bus_add_match_internal(bus
, s
->match_callback
.match_string
);
3247 s
->match_added
= true;
3251 bus
->match_callbacks_modified
= true;
3252 r
= bus_match_add(&bus
->match_callbacks
, components
, n_components
, &s
->match_callback
);
3261 bus_match_parse_free(components
, n_components
);
3262 sd_bus_slot_unref(s
);
3267 _public_
int sd_bus_add_match(
3271 sd_bus_message_handler_t callback
,
3274 return bus_add_match_full(bus
, slot
, false, match
, callback
, NULL
, userdata
);
3277 _public_
int sd_bus_add_match_async(
3281 sd_bus_message_handler_t callback
,
3282 sd_bus_message_handler_t install_callback
,
3285 return bus_add_match_full(bus
, slot
, true, match
, callback
, install_callback
, userdata
);
3288 bool bus_pid_changed(sd_bus
*bus
) {
3291 /* We don't support people creating a bus connection and
3292 * keeping it around over a fork(). Let's complain. */
3294 return bus
->original_pid
!= getpid_cached();
3297 static int io_callback(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
3298 sd_bus
*bus
= userdata
;
3303 /* Note that this is called both on input_fd, output_fd as well as inotify_fd events */
3305 r
= sd_bus_process(bus
, NULL
);
3307 log_debug_errno(r
, "Processing of bus failed, closing down: %m");
3308 bus_enter_closing(bus
);
3314 static int time_callback(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
3315 sd_bus
*bus
= userdata
;
3320 r
= sd_bus_process(bus
, NULL
);
3322 log_debug_errno(r
, "Processing of bus failed, closing down: %m");
3323 bus_enter_closing(bus
);
3329 static int prepare_callback(sd_event_source
*s
, void *userdata
) {
3330 sd_bus
*bus
= userdata
;
3337 e
= sd_bus_get_events(bus
);
3343 if (bus
->output_fd
!= bus
->input_fd
) {
3345 r
= sd_event_source_set_io_events(bus
->input_io_event_source
, e
& POLLIN
);
3349 r
= sd_event_source_set_io_events(bus
->output_io_event_source
, e
& POLLOUT
);
3351 r
= sd_event_source_set_io_events(bus
->input_io_event_source
, e
);
3355 r
= sd_bus_get_timeout(bus
, &until
);
3361 j
= sd_event_source_set_time(bus
->time_event_source
, until
);
3368 r
= sd_event_source_set_enabled(bus
->time_event_source
, r
> 0);
3375 log_debug_errno(r
, "Preparing of bus events failed, closing down: %m");
3376 bus_enter_closing(bus
);
3381 static int quit_callback(sd_event_source
*event
, void *userdata
) {
3382 sd_bus
*bus
= userdata
;
3392 int bus_attach_io_events(sd_bus
*bus
) {
3397 if (bus
->input_fd
< 0)
3403 if (!bus
->input_io_event_source
) {
3404 r
= sd_event_add_io(bus
->event
, &bus
->input_io_event_source
, bus
->input_fd
, 0, io_callback
, bus
);
3408 r
= sd_event_source_set_prepare(bus
->input_io_event_source
, prepare_callback
);
3412 r
= sd_event_source_set_priority(bus
->input_io_event_source
, bus
->event_priority
);
3416 r
= sd_event_source_set_description(bus
->input_io_event_source
, "bus-input");
3418 r
= sd_event_source_set_io_fd(bus
->input_io_event_source
, bus
->input_fd
);
3423 if (bus
->output_fd
!= bus
->input_fd
) {
3424 assert(bus
->output_fd
>= 0);
3426 if (!bus
->output_io_event_source
) {
3427 r
= sd_event_add_io(bus
->event
, &bus
->output_io_event_source
, bus
->output_fd
, 0, io_callback
, bus
);
3431 r
= sd_event_source_set_priority(bus
->output_io_event_source
, bus
->event_priority
);
3435 r
= sd_event_source_set_description(bus
->input_io_event_source
, "bus-output");
3437 r
= sd_event_source_set_io_fd(bus
->output_io_event_source
, bus
->output_fd
);
3446 static void bus_detach_io_events(sd_bus
*bus
) {
3449 if (bus
->input_io_event_source
) {
3450 sd_event_source_set_enabled(bus
->input_io_event_source
, SD_EVENT_OFF
);
3451 bus
->input_io_event_source
= sd_event_source_unref(bus
->input_io_event_source
);
3454 if (bus
->output_io_event_source
) {
3455 sd_event_source_set_enabled(bus
->output_io_event_source
, SD_EVENT_OFF
);
3456 bus
->output_io_event_source
= sd_event_source_unref(bus
->output_io_event_source
);
3460 int bus_attach_inotify_event(sd_bus
*bus
) {
3465 if (bus
->inotify_fd
< 0)
3471 if (!bus
->inotify_event_source
) {
3472 r
= sd_event_add_io(bus
->event
, &bus
->inotify_event_source
, bus
->inotify_fd
, EPOLLIN
, io_callback
, bus
);
3476 r
= sd_event_source_set_priority(bus
->inotify_event_source
, bus
->event_priority
);
3480 r
= sd_event_source_set_description(bus
->inotify_event_source
, "bus-inotify");
3482 r
= sd_event_source_set_io_fd(bus
->inotify_event_source
, bus
->inotify_fd
);
3489 static void bus_detach_inotify_event(sd_bus
*bus
) {
3492 if (bus
->inotify_event_source
) {
3493 sd_event_source_set_enabled(bus
->inotify_event_source
, SD_EVENT_OFF
);
3494 bus
->inotify_event_source
= sd_event_source_unref(bus
->inotify_event_source
);
3498 _public_
int sd_bus_attach_event(sd_bus
*bus
, sd_event
*event
, int priority
) {
3501 assert_return(bus
, -EINVAL
);
3502 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3503 assert_return(!bus
->event
, -EBUSY
);
3505 assert(!bus
->input_io_event_source
);
3506 assert(!bus
->output_io_event_source
);
3507 assert(!bus
->time_event_source
);
3510 bus
->event
= sd_event_ref(event
);
3512 r
= sd_event_default(&bus
->event
);
3517 bus
->event_priority
= priority
;
3519 r
= sd_event_add_time(bus
->event
, &bus
->time_event_source
, CLOCK_MONOTONIC
, 0, 0, time_callback
, bus
);
3523 r
= sd_event_source_set_priority(bus
->time_event_source
, priority
);
3527 r
= sd_event_source_set_description(bus
->time_event_source
, "bus-time");
3531 r
= sd_event_add_exit(bus
->event
, &bus
->quit_event_source
, quit_callback
, bus
);
3535 r
= sd_event_source_set_description(bus
->quit_event_source
, "bus-exit");
3539 r
= bus_attach_io_events(bus
);
3543 r
= bus_attach_inotify_event(bus
);
3550 sd_bus_detach_event(bus
);
3554 _public_
int sd_bus_detach_event(sd_bus
*bus
) {
3555 assert_return(bus
, -EINVAL
);
3556 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3561 bus_detach_io_events(bus
);
3562 bus_detach_inotify_event(bus
);
3564 if (bus
->time_event_source
) {
3565 sd_event_source_set_enabled(bus
->time_event_source
, SD_EVENT_OFF
);
3566 bus
->time_event_source
= sd_event_source_unref(bus
->time_event_source
);
3569 if (bus
->quit_event_source
) {
3570 sd_event_source_set_enabled(bus
->quit_event_source
, SD_EVENT_OFF
);
3571 bus
->quit_event_source
= sd_event_source_unref(bus
->quit_event_source
);
3574 bus
->event
= sd_event_unref(bus
->event
);
3578 _public_ sd_event
* sd_bus_get_event(sd_bus
*bus
) {
3579 assert_return(bus
, NULL
);
3584 _public_ sd_bus_message
* sd_bus_get_current_message(sd_bus
*bus
) {
3585 assert_return(bus
, NULL
);
3587 return bus
->current_message
;
3590 _public_ sd_bus_slot
* sd_bus_get_current_slot(sd_bus
*bus
) {
3591 assert_return(bus
, NULL
);
3593 return bus
->current_slot
;
3596 _public_ sd_bus_message_handler_t
sd_bus_get_current_handler(sd_bus
*bus
) {
3597 assert_return(bus
, NULL
);
3599 return bus
->current_handler
;
3602 _public_
void* sd_bus_get_current_userdata(sd_bus
*bus
) {
3603 assert_return(bus
, NULL
);
3605 return bus
->current_userdata
;
3608 static int bus_default(int (*bus_open
)(sd_bus
**), sd_bus
**default_bus
, sd_bus
**ret
) {
3613 assert(default_bus
);
3616 return !!*default_bus
;
3619 *ret
= sd_bus_ref(*default_bus
);
3627 b
->default_bus_ptr
= default_bus
;
3635 _public_
int sd_bus_default_system(sd_bus
**ret
) {
3636 return bus_default(sd_bus_open_system
, &default_system_bus
, ret
);
3639 _public_
int sd_bus_default_user(sd_bus
**ret
) {
3640 return bus_default(sd_bus_open_user
, &default_user_bus
, ret
);
3643 _public_
int sd_bus_default(sd_bus
**ret
) {
3644 int (*bus_open
)(sd_bus
**) = NULL
;
3647 busp
= bus_choose_default(&bus_open
);
3648 return bus_default(bus_open
, busp
, ret
);
3651 _public_
int sd_bus_get_tid(sd_bus
*b
, pid_t
*tid
) {
3652 assert_return(b
, -EINVAL
);
3653 assert_return(tid
, -EINVAL
);
3654 assert_return(!bus_pid_changed(b
), -ECHILD
);
3662 return sd_event_get_tid(b
->event
, tid
);
3667 _public_
int sd_bus_path_encode(const char *prefix
, const char *external_id
, char **ret_path
) {
3668 _cleanup_free_
char *e
= NULL
;
3671 assert_return(object_path_is_valid(prefix
), -EINVAL
);
3672 assert_return(external_id
, -EINVAL
);
3673 assert_return(ret_path
, -EINVAL
);
3675 e
= bus_label_escape(external_id
);
3679 ret
= strjoin(prefix
, "/", e
);
3687 _public_
int sd_bus_path_decode(const char *path
, const char *prefix
, char **external_id
) {
3691 assert_return(object_path_is_valid(path
), -EINVAL
);
3692 assert_return(object_path_is_valid(prefix
), -EINVAL
);
3693 assert_return(external_id
, -EINVAL
);
3695 e
= object_path_startswith(path
, prefix
);
3697 *external_id
= NULL
;
3701 ret
= bus_label_unescape(e
);
3709 _public_
int sd_bus_path_encode_many(char **out
, const char *path_template
, ...) {
3710 _cleanup_strv_free_
char **labels
= NULL
;
3711 char *path
, *path_pos
, **label_pos
;
3712 const char *sep
, *template_pos
;
3717 assert_return(out
, -EINVAL
);
3718 assert_return(path_template
, -EINVAL
);
3720 path_length
= strlen(path_template
);
3722 va_start(list
, path_template
);
3723 for (sep
= strchr(path_template
, '%'); sep
; sep
= strchr(sep
+ 1, '%')) {
3727 arg
= va_arg(list
, const char *);
3733 label
= bus_label_escape(arg
);
3739 r
= strv_consume(&labels
, label
);
3745 /* add label length, but account for the format character */
3746 path_length
+= strlen(label
) - 1;
3750 path
= malloc(path_length
+ 1);
3757 for (template_pos
= path_template
; *template_pos
; ) {
3758 sep
= strchrnul(template_pos
, '%');
3759 path_pos
= mempcpy(path_pos
, template_pos
, sep
- template_pos
);
3763 path_pos
= stpcpy(path_pos
, *label_pos
++);
3764 template_pos
= sep
+ 1;
3772 _public_
int sd_bus_path_decode_many(const char *path
, const char *path_template
, ...) {
3773 _cleanup_strv_free_
char **labels
= NULL
;
3774 const char *template_pos
, *path_pos
;
3780 * This decodes an object-path based on a template argument. The
3781 * template consists of a verbatim path, optionally including special
3784 * - Each occurrence of '%' in the template matches an arbitrary
3785 * substring of a label in the given path. At most one such
3786 * directive is allowed per label. For each such directive, the
3787 * caller must provide an output parameter (char **) via va_arg. If
3788 * NULL is passed, the given label is verified, but not returned.
3789 * For each matched label, the *decoded* label is stored in the
3790 * passed output argument, and the caller is responsible to free
3791 * it. Note that the output arguments are only modified if the
3792 * actualy path matched the template. Otherwise, they're left
3795 * This function returns <0 on error, 0 if the path does not match the
3796 * template, 1 if it matched.
3799 assert_return(path
, -EINVAL
);
3800 assert_return(path_template
, -EINVAL
);
3804 for (template_pos
= path_template
; *template_pos
; ) {
3809 /* verify everything until the next '%' matches verbatim */
3810 sep
= strchrnul(template_pos
, '%');
3811 length
= sep
- template_pos
;
3812 if (strncmp(path_pos
, template_pos
, length
))
3816 template_pos
+= length
;
3821 /* We found the next '%' character. Everything up until here
3822 * matched. We now skip ahead to the end of this label and make
3823 * sure it matches the tail of the label in the path. Then we
3824 * decode the string in-between and save it for later use. */
3826 ++template_pos
; /* skip over '%' */
3828 sep
= strchrnul(template_pos
, '/');
3829 length
= sep
- template_pos
; /* length of suffix to match verbatim */
3831 /* verify the suffixes match */
3832 sep
= strchrnul(path_pos
, '/');
3833 if (sep
- path_pos
< (ssize_t
)length
||
3834 strncmp(sep
- length
, template_pos
, length
))
3837 template_pos
+= length
; /* skip over matched label */
3838 length
= sep
- path_pos
- length
; /* length of sub-label to decode */
3840 /* store unescaped label for later use */
3841 label
= bus_label_unescape_n(path_pos
, length
);
3845 r
= strv_consume(&labels
, label
);
3849 path_pos
= sep
; /* skip decoded label and suffix */
3852 /* end of template must match end of path */
3856 /* copy the labels over to the caller */
3857 va_start(list
, path_template
);
3858 for (label_pos
= labels
; label_pos
&& *label_pos
; ++label_pos
) {
3861 arg
= va_arg(list
, char **);
3869 labels
= mfree(labels
);
3873 _public_
int sd_bus_try_close(sd_bus
*bus
) {
3874 assert_return(bus
, -EINVAL
);
3875 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3876 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3881 _public_
int sd_bus_get_description(sd_bus
*bus
, const char **description
) {
3882 assert_return(bus
, -EINVAL
);
3883 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3884 assert_return(description
, -EINVAL
);
3885 assert_return(bus
->description
, -ENXIO
);
3886 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3888 if (bus
->description
)
3889 *description
= bus
->description
;
3890 else if (bus
->is_system
)
3891 *description
= "system";
3892 else if (bus
->is_user
)
3893 *description
= "user";
3895 *description
= NULL
;
3900 int bus_get_root_path(sd_bus
*bus
) {
3903 if (bus
->cgroup_root
)
3906 r
= cg_get_root_path(&bus
->cgroup_root
);
3908 bus
->cgroup_root
= strdup("/");
3909 if (!bus
->cgroup_root
)
3918 _public_
int sd_bus_get_scope(sd_bus
*bus
, const char **scope
) {
3919 assert_return(bus
, -EINVAL
);
3920 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3921 assert_return(scope
, -EINVAL
);
3922 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3929 if (bus
->is_system
) {
3937 _public_
int sd_bus_get_address(sd_bus
*bus
, const char **address
) {
3939 assert_return(bus
, -EINVAL
);
3940 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3941 assert_return(address
, -EINVAL
);
3942 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3945 *address
= bus
->address
;
3952 _public_
int sd_bus_get_creds_mask(sd_bus
*bus
, uint64_t *mask
) {
3953 assert_return(bus
, -EINVAL
);
3954 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3955 assert_return(mask
, -EINVAL
);
3956 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3958 *mask
= bus
->creds_mask
;
3962 _public_
int sd_bus_is_bus_client(sd_bus
*bus
) {
3963 assert_return(bus
, -EINVAL
);
3964 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3965 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3967 return bus
->bus_client
;
3970 _public_
int sd_bus_is_server(sd_bus
*bus
) {
3971 assert_return(bus
, -EINVAL
);
3972 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3973 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3975 return bus
->is_server
;
3978 _public_
int sd_bus_is_anonymous(sd_bus
*bus
) {
3979 assert_return(bus
, -EINVAL
);
3980 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3981 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3983 return bus
->anonymous_auth
;
3986 _public_
int sd_bus_is_trusted(sd_bus
*bus
) {
3987 assert_return(bus
, -EINVAL
);
3988 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3989 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3991 return bus
->trusted
;
3994 _public_
int sd_bus_is_monitor(sd_bus
*bus
) {
3995 assert_return(bus
, -EINVAL
);
3996 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
3997 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3999 return bus
->is_monitor
;
4002 static void flush_close(sd_bus
*bus
) {
4006 /* Flushes and closes the specified bus. We take a ref before,
4007 * to ensure the flushing does not cause the bus to be
4010 sd_bus_flush_close_unref(sd_bus_ref(bus
));
4013 _public_
void sd_bus_default_flush_close(void) {
4014 flush_close(default_starter_bus
);
4015 flush_close(default_user_bus
);
4016 flush_close(default_system_bus
);
4019 _public_
int sd_bus_set_exit_on_disconnect(sd_bus
*bus
, int b
) {
4020 assert_return(bus
, -EINVAL
);
4021 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4023 /* Turns on exit-on-disconnect, and triggers it immediately if the bus connection was already
4024 * disconnected. Note that this is triggered exclusively on disconnections triggered by the server side, never
4025 * from the client side. */
4026 bus
->exit_on_disconnect
= b
;
4028 /* If the exit condition was triggered already, exit immediately. */
4029 return bus_exit_now(bus
);
4032 _public_
int sd_bus_get_exit_on_disconnect(sd_bus
*bus
) {
4033 assert_return(bus
, -EINVAL
);
4034 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4036 return bus
->exit_on_disconnect
;
4039 _public_
int sd_bus_set_sender(sd_bus
*bus
, const char *sender
) {
4040 assert_return(bus
, -EINVAL
);
4041 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4042 assert_return(!bus
->bus_client
, -EPERM
);
4043 assert_return(!sender
|| service_name_is_valid(sender
), -EINVAL
);
4045 return free_and_strdup(&bus
->patch_sender
, sender
);
4048 _public_
int sd_bus_get_sender(sd_bus
*bus
, const char **ret
) {
4049 assert_return(bus
, -EINVAL
);
4050 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4051 assert_return(ret
, -EINVAL
);
4053 if (!bus
->patch_sender
)
4056 *ret
= bus
->patch_sender
;
4060 _public_
int sd_bus_get_n_queued_read(sd_bus
*bus
, uint64_t *ret
) {
4061 assert_return(bus
, -EINVAL
);
4062 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4063 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4064 assert_return(ret
, -EINVAL
);
4066 *ret
= bus
->rqueue_size
;
4070 _public_
int sd_bus_get_n_queued_write(sd_bus
*bus
, uint64_t *ret
) {
4071 assert_return(bus
, -EINVAL
);
4072 assert_return(bus
= bus_resolve(bus
), -ENOPKG
);
4073 assert_return(!bus_pid_changed(bus
), -ECHILD
);
4074 assert_return(ret
, -EINVAL
);
4076 *ret
= bus
->wqueue_size
;