1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
38 #include "cgroup-util.h"
39 #include "bus-label.h"
42 #include "bus-internal.h"
43 #include "bus-message.h"
45 #include "bus-socket.h"
46 #include "bus-kernel.h"
47 #include "bus-control.h"
48 #include "bus-introspect.h"
49 #include "bus-signature.h"
50 #include "bus-objects.h"
52 #include "bus-container.h"
53 #include "bus-protocol.h"
54 #include "bus-track.h"
57 static int bus_poll(sd_bus
*bus
, bool need_more
, uint64_t timeout_usec
);
58 static int attach_io_events(sd_bus
*b
);
59 static void detach_io_events(sd_bus
*b
);
61 static void bus_close_fds(sd_bus
*b
) {
67 safe_close(b
->input_fd
);
69 if (b
->output_fd
>= 0 && b
->output_fd
!= b
->input_fd
)
70 safe_close(b
->output_fd
);
72 b
->input_fd
= b
->output_fd
= -1;
75 static void bus_reset_queues(sd_bus
*b
) {
78 while (b
->rqueue_size
> 0)
79 sd_bus_message_unref(b
->rqueue
[--b
->rqueue_size
]);
83 b
->rqueue_allocated
= 0;
85 while (b
->wqueue_size
> 0)
86 sd_bus_message_unref(b
->wqueue
[--b
->wqueue_size
]);
90 b
->wqueue_allocated
= 0;
93 static void bus_free(sd_bus
*b
) {
97 assert(!b
->track_queue
);
99 b
->state
= BUS_CLOSED
;
101 sd_bus_detach_event(b
);
103 while ((s
= b
->slots
)) {
104 /* At this point only floating slots can still be
105 * around, because the non-floating ones keep a
106 * reference to the bus, and we thus couldn't be
107 * destructing right now... We forcibly disconnect the
108 * slots here, so that they still can be referenced by
109 * apps, but are dead. */
112 bus_slot_disconnect(s
);
113 sd_bus_slot_unref(s
);
116 if (b
->default_bus_ptr
)
117 *b
->default_bus_ptr
= NULL
;
122 munmap(b
->kdbus_buffer
, KDBUS_POOL_SIZE
);
125 free(b
->unique_name
);
126 free(b
->auth_buffer
);
131 free(b
->cgroup_root
);
132 free(b
->description
);
135 strv_free(b
->exec_argv
);
137 close_many(b
->fds
, b
->n_fds
);
142 ordered_hashmap_free_free(b
->reply_callbacks
);
143 prioq_free(b
->reply_callbacks_prioq
);
145 assert(b
->match_callbacks
.type
== BUS_MATCH_ROOT
);
146 bus_match_free(&b
->match_callbacks
);
148 hashmap_free_free(b
->vtable_methods
);
149 hashmap_free_free(b
->vtable_properties
);
151 assert(hashmap_isempty(b
->nodes
));
152 hashmap_free(b
->nodes
);
154 bus_kernel_flush_memfd(b
);
156 assert_se(pthread_mutex_destroy(&b
->memfd_cache_mutex
) == 0);
161 _public_
int sd_bus_new(sd_bus
**ret
) {
164 assert_return(ret
, -EINVAL
);
170 r
->n_ref
= REFCNT_INIT
;
171 r
->input_fd
= r
->output_fd
= -1;
172 r
->message_version
= 1;
173 r
->creds_mask
|= SD_BUS_CREDS_WELL_KNOWN_NAMES
|SD_BUS_CREDS_UNIQUE_NAME
;
174 r
->hello_flags
|= KDBUS_HELLO_ACCEPT_FD
;
175 r
->attach_flags
|= KDBUS_ATTACH_NAMES
;
176 r
->original_pid
= getpid();
178 assert_se(pthread_mutex_init(&r
->memfd_cache_mutex
, NULL
) == 0);
180 /* We guarantee that wqueue always has space for at least one
182 if (!GREEDY_REALLOC(r
->wqueue
, r
->wqueue_allocated
, 1)) {
191 _public_
int sd_bus_set_address(sd_bus
*bus
, const char *address
) {
194 assert_return(bus
, -EINVAL
);
195 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
196 assert_return(address
, -EINVAL
);
197 assert_return(!bus_pid_changed(bus
), -ECHILD
);
209 _public_
int sd_bus_set_fd(sd_bus
*bus
, int input_fd
, int output_fd
) {
210 assert_return(bus
, -EINVAL
);
211 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
212 assert_return(input_fd
>= 0, -EINVAL
);
213 assert_return(output_fd
>= 0, -EINVAL
);
214 assert_return(!bus_pid_changed(bus
), -ECHILD
);
216 bus
->input_fd
= input_fd
;
217 bus
->output_fd
= output_fd
;
221 _public_
int sd_bus_set_exec(sd_bus
*bus
, const char *path
, char *const argv
[]) {
224 assert_return(bus
, -EINVAL
);
225 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
226 assert_return(path
, -EINVAL
);
227 assert_return(!strv_isempty(argv
), -EINVAL
);
228 assert_return(!bus_pid_changed(bus
), -ECHILD
);
240 free(bus
->exec_path
);
241 strv_free(bus
->exec_argv
);
249 _public_
int sd_bus_set_bus_client(sd_bus
*bus
, int b
) {
250 assert_return(bus
, -EINVAL
);
251 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
252 assert_return(!bus_pid_changed(bus
), -ECHILD
);
254 bus
->bus_client
= !!b
;
258 _public_
int sd_bus_set_monitor(sd_bus
*bus
, int b
) {
259 assert_return(bus
, -EINVAL
);
260 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
261 assert_return(!bus_pid_changed(bus
), -ECHILD
);
263 SET_FLAG(bus
->hello_flags
, KDBUS_HELLO_MONITOR
, b
);
267 _public_
int sd_bus_negotiate_fds(sd_bus
*bus
, int b
) {
268 assert_return(bus
, -EINVAL
);
269 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
270 assert_return(!bus_pid_changed(bus
), -ECHILD
);
272 SET_FLAG(bus
->hello_flags
, KDBUS_HELLO_ACCEPT_FD
, b
);
276 _public_
int sd_bus_negotiate_timestamp(sd_bus
*bus
, int b
) {
277 assert_return(bus
, -EINVAL
);
278 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
279 assert_return(!bus_pid_changed(bus
), -ECHILD
);
281 SET_FLAG(bus
->attach_flags
, KDBUS_ATTACH_TIMESTAMP
, b
);
285 _public_
int sd_bus_negotiate_creds(sd_bus
*bus
, uint64_t mask
) {
286 assert_return(bus
, -EINVAL
);
287 assert_return(mask
<= _SD_BUS_CREDS_ALL
, -EINVAL
);
288 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
289 assert_return(!bus_pid_changed(bus
), -ECHILD
);
291 /* The well knowns we need unconditionally, so that matches can work */
292 bus
->creds_mask
= mask
| SD_BUS_CREDS_WELL_KNOWN_NAMES
|SD_BUS_CREDS_UNIQUE_NAME
;
294 return kdbus_translate_attach_flags(bus
->creds_mask
, &bus
->attach_flags
);
297 _public_
int sd_bus_set_server(sd_bus
*bus
, int b
, sd_id128_t server_id
) {
298 assert_return(bus
, -EINVAL
);
299 assert_return(b
|| sd_id128_equal(server_id
, SD_ID128_NULL
), -EINVAL
);
300 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
301 assert_return(!bus_pid_changed(bus
), -ECHILD
);
303 bus
->is_server
= !!b
;
304 bus
->server_id
= server_id
;
308 _public_
int sd_bus_set_anonymous(sd_bus
*bus
, int b
) {
309 assert_return(bus
, -EINVAL
);
310 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
311 assert_return(!bus_pid_changed(bus
), -ECHILD
);
313 bus
->anonymous_auth
= !!b
;
317 _public_
int sd_bus_set_trusted(sd_bus
*bus
, int b
) {
318 assert_return(bus
, -EINVAL
);
319 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
320 assert_return(!bus_pid_changed(bus
), -ECHILD
);
326 _public_
int sd_bus_set_description(sd_bus
*bus
, const char *description
) {
327 assert_return(bus
, -EINVAL
);
328 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
329 assert_return(!bus_pid_changed(bus
), -ECHILD
);
331 return free_and_strdup(&bus
->description
, description
);
334 static int hello_callback(sd_bus
*bus
, sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
339 assert(bus
->state
== BUS_HELLO
|| bus
->state
== BUS_CLOSING
);
342 r
= sd_bus_message_get_errno(reply
);
346 r
= sd_bus_message_read(reply
, "s", &s
);
350 if (!service_name_is_valid(s
) || s
[0] != ':')
353 bus
->unique_name
= strdup(s
);
354 if (!bus
->unique_name
)
357 if (bus
->state
== BUS_HELLO
)
358 bus
->state
= BUS_RUNNING
;
363 static int bus_send_hello(sd_bus
*bus
) {
364 _cleanup_bus_message_unref_ sd_bus_message
*m
= NULL
;
369 if (!bus
->bus_client
|| bus
->is_kernel
)
372 r
= sd_bus_message_new_method_call(
375 "org.freedesktop.DBus",
376 "/org/freedesktop/DBus",
377 "org.freedesktop.DBus",
382 return sd_bus_call_async(bus
, NULL
, m
, hello_callback
, NULL
, 0);
385 int bus_start_running(sd_bus
*bus
) {
388 if (bus
->bus_client
&& !bus
->is_kernel
) {
389 bus
->state
= BUS_HELLO
;
393 bus
->state
= BUS_RUNNING
;
397 static int parse_address_key(const char **p
, const char *key
, char **value
) {
398 size_t l
, n
= 0, allocated
= 0;
408 if (strncmp(*p
, key
, l
) != 0)
421 while (*a
!= ';' && *a
!= ',' && *a
!= 0) {
439 c
= (char) ((x
<< 4) | y
);
446 if (!GREEDY_REALLOC(r
, allocated
, n
+ 2))
470 static void skip_address_key(const char **p
) {
474 *p
+= strcspn(*p
, ",");
480 static int parse_unix_address(sd_bus
*b
, const char **p
, char **guid
) {
481 _cleanup_free_
char *path
= NULL
, *abstract
= NULL
;
490 while (**p
!= 0 && **p
!= ';') {
491 r
= parse_address_key(p
, "guid", guid
);
497 r
= parse_address_key(p
, "path", &path
);
503 r
= parse_address_key(p
, "abstract", &abstract
);
512 if (!path
&& !abstract
)
515 if (path
&& abstract
)
520 if (l
> sizeof(b
->sockaddr
.un
.sun_path
))
523 b
->sockaddr
.un
.sun_family
= AF_UNIX
;
524 strncpy(b
->sockaddr
.un
.sun_path
, path
, sizeof(b
->sockaddr
.un
.sun_path
));
525 b
->sockaddr_size
= offsetof(struct sockaddr_un
, sun_path
) + l
;
526 } else if (abstract
) {
527 l
= strlen(abstract
);
528 if (l
> sizeof(b
->sockaddr
.un
.sun_path
) - 1)
531 b
->sockaddr
.un
.sun_family
= AF_UNIX
;
532 b
->sockaddr
.un
.sun_path
[0] = 0;
533 strncpy(b
->sockaddr
.un
.sun_path
+1, abstract
, sizeof(b
->sockaddr
.un
.sun_path
)-1);
534 b
->sockaddr_size
= offsetof(struct sockaddr_un
, sun_path
) + 1 + l
;
540 static int parse_tcp_address(sd_bus
*b
, const char **p
, char **guid
) {
541 _cleanup_free_
char *host
= NULL
, *port
= NULL
, *family
= NULL
;
543 struct addrinfo
*result
, hints
= {
544 .ai_socktype
= SOCK_STREAM
,
545 .ai_flags
= AI_ADDRCONFIG
,
553 while (**p
!= 0 && **p
!= ';') {
554 r
= parse_address_key(p
, "guid", guid
);
560 r
= parse_address_key(p
, "host", &host
);
566 r
= parse_address_key(p
, "port", &port
);
572 r
= parse_address_key(p
, "family", &family
);
585 if (streq(family
, "ipv4"))
586 hints
.ai_family
= AF_INET
;
587 else if (streq(family
, "ipv6"))
588 hints
.ai_family
= AF_INET6
;
593 r
= getaddrinfo(host
, port
, &hints
, &result
);
597 return -EADDRNOTAVAIL
;
599 memcpy(&b
->sockaddr
, result
->ai_addr
, result
->ai_addrlen
);
600 b
->sockaddr_size
= result
->ai_addrlen
;
602 freeaddrinfo(result
);
607 static int parse_exec_address(sd_bus
*b
, const char **p
, char **guid
) {
609 unsigned n_argv
= 0, j
;
611 size_t allocated
= 0;
619 while (**p
!= 0 && **p
!= ';') {
620 r
= parse_address_key(p
, "guid", guid
);
626 r
= parse_address_key(p
, "path", &path
);
632 if (startswith(*p
, "argv")) {
636 ul
= strtoul(*p
+ 4, (char**) p
, 10);
637 if (errno
> 0 || **p
!= '=' || ul
> 256) {
645 if (!GREEDY_REALLOC0(argv
, allocated
, ul
+ 2)) {
653 r
= parse_address_key(p
, NULL
, argv
+ ul
);
668 /* Make sure there are no holes in the array, with the
669 * exception of argv[0] */
670 for (j
= 1; j
< n_argv
; j
++)
676 if (argv
&& argv
[0] == NULL
) {
677 argv
[0] = strdup(path
);
689 for (j
= 0; j
< n_argv
; j
++)
697 static int parse_kernel_address(sd_bus
*b
, const char **p
, char **guid
) {
698 _cleanup_free_
char *path
= NULL
;
706 while (**p
!= 0 && **p
!= ';') {
707 r
= parse_address_key(p
, "guid", guid
);
713 r
= parse_address_key(p
, "path", &path
);
732 static int parse_container_unix_address(sd_bus
*b
, const char **p
, char **guid
) {
733 _cleanup_free_
char *machine
= NULL
;
741 while (**p
!= 0 && **p
!= ';') {
742 r
= parse_address_key(p
, "guid", guid
);
748 r
= parse_address_key(p
, "machine", &machine
);
760 if (!machine_name_is_valid(machine
))
764 b
->machine
= machine
;
767 b
->sockaddr
.un
.sun_family
= AF_UNIX
;
768 strncpy(b
->sockaddr
.un
.sun_path
, "/var/run/dbus/system_bus_socket", sizeof(b
->sockaddr
.un
.sun_path
));
769 b
->sockaddr_size
= offsetof(struct sockaddr_un
, sun_path
) + strlen("/var/run/dbus/system_bus_socket");
774 static int parse_container_kernel_address(sd_bus
*b
, const char **p
, char **guid
) {
775 _cleanup_free_
char *machine
= NULL
;
783 while (**p
!= 0 && **p
!= ';') {
784 r
= parse_address_key(p
, "guid", guid
);
790 r
= parse_address_key(p
, "machine", &machine
);
802 if (!machine_name_is_valid(machine
))
806 b
->machine
= machine
;
810 b
->kernel
= strdup("/sys/fs/kdbus/0-system/bus");
817 static void bus_reset_parsed_address(sd_bus
*b
) {
821 b
->sockaddr_size
= 0;
822 strv_free(b
->exec_argv
);
826 b
->server_id
= SD_ID128_NULL
;
833 static int bus_parse_next_address(sd_bus
*b
) {
834 _cleanup_free_
char *guid
= NULL
;
842 if (b
->address
[b
->address_index
] == 0)
845 bus_reset_parsed_address(b
);
847 a
= b
->address
+ b
->address_index
;
856 if (startswith(a
, "unix:")) {
859 r
= parse_unix_address(b
, &a
, &guid
);
864 } else if (startswith(a
, "tcp:")) {
867 r
= parse_tcp_address(b
, &a
, &guid
);
873 } else if (startswith(a
, "unixexec:")) {
876 r
= parse_exec_address(b
, &a
, &guid
);
882 } else if (startswith(a
, "kernel:")) {
885 r
= parse_kernel_address(b
, &a
, &guid
);
890 } else if (startswith(a
, "x-container-unix:")) {
893 r
= parse_container_unix_address(b
, &a
, &guid
);
898 } else if (startswith(a
, "x-container-kernel:")) {
901 r
= parse_container_kernel_address(b
, &a
, &guid
);
914 r
= sd_id128_from_string(guid
, &b
->server_id
);
919 b
->address_index
= a
- b
->address
;
923 static int bus_start_address(sd_bus
*b
) {
929 bool skipped
= false;
934 r
= bus_socket_exec(b
);
935 else if (b
->machine
&& b
->kernel
)
936 r
= bus_container_connect_kernel(b
);
937 else if (b
->machine
&& b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
)
938 r
= bus_container_connect_socket(b
);
940 r
= bus_kernel_connect(b
);
941 else if (b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
)
942 r
= bus_socket_connect(b
);
948 r
= attach_io_events(b
);
953 b
->last_connect_error
= -r
;
956 r
= bus_parse_next_address(b
);
960 return b
->last_connect_error
? -b
->last_connect_error
: -ECONNREFUSED
;
964 int bus_next_address(sd_bus
*b
) {
967 bus_reset_parsed_address(b
);
968 return bus_start_address(b
);
971 static int bus_start_fd(sd_bus
*b
) {
976 assert(b
->input_fd
>= 0);
977 assert(b
->output_fd
>= 0);
979 r
= fd_nonblock(b
->input_fd
, true);
983 r
= fd_cloexec(b
->input_fd
, true);
987 if (b
->input_fd
!= b
->output_fd
) {
988 r
= fd_nonblock(b
->output_fd
, true);
992 r
= fd_cloexec(b
->output_fd
, true);
997 if (fstat(b
->input_fd
, &st
) < 0)
1000 if (S_ISCHR(b
->input_fd
))
1001 return bus_kernel_take_fd(b
);
1003 return bus_socket_take_fd(b
);
1006 _public_
int sd_bus_start(sd_bus
*bus
) {
1009 assert_return(bus
, -EINVAL
);
1010 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
1011 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1013 bus
->state
= BUS_OPENING
;
1015 if (bus
->is_server
&& bus
->bus_client
)
1018 if (bus
->input_fd
>= 0)
1019 r
= bus_start_fd(bus
);
1020 else if (bus
->address
|| bus
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
|| bus
->exec_path
|| bus
->kernel
|| bus
->machine
)
1021 r
= bus_start_address(bus
);
1030 return bus_send_hello(bus
);
1033 _public_
int sd_bus_open(sd_bus
**ret
) {
1038 assert_return(ret
, -EINVAL
);
1040 /* Let's connect to the starter bus if it is set, and
1041 * otherwise to the bus that is appropropriate for the scope
1042 * we are running in */
1044 e
= secure_getenv("DBUS_STARTER_BUS_TYPE");
1046 if (streq(e
, "system"))
1047 return sd_bus_open_system(ret
);
1048 else if (STR_IN_SET(e
, "session", "user"))
1049 return sd_bus_open_user(ret
);
1052 e
= secure_getenv("DBUS_STARTER_ADDRESS");
1054 if (cg_pid_get_owner_uid(0, NULL
) >= 0)
1055 return sd_bus_open_user(ret
);
1057 return sd_bus_open_system(ret
);
1064 r
= sd_bus_set_address(b
, e
);
1068 b
->bus_client
= true;
1070 /* We don't know whether the bus is trusted or not, so better
1071 * be safe, and authenticate everything */
1073 b
->attach_flags
|= KDBUS_ATTACH_CAPS
| KDBUS_ATTACH_CREDS
;
1075 r
= sd_bus_start(b
);
1087 int bus_set_address_system(sd_bus
*b
) {
1091 e
= secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1093 return sd_bus_set_address(b
, e
);
1095 return sd_bus_set_address(b
, DEFAULT_SYSTEM_BUS_PATH
);
1098 _public_
int sd_bus_open_system(sd_bus
**ret
) {
1102 assert_return(ret
, -EINVAL
);
1108 r
= bus_set_address_system(b
);
1112 b
->bus_client
= true;
1113 b
->is_system
= true;
1115 /* Let's do per-method access control on the system bus. We
1116 * need the caller's UID and capability set for that. */
1118 b
->attach_flags
|= KDBUS_ATTACH_CAPS
| KDBUS_ATTACH_CREDS
;
1120 r
= sd_bus_start(b
);
1132 int bus_set_address_user(sd_bus
*b
) {
1137 e
= secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1139 return sd_bus_set_address(b
, e
);
1141 e
= secure_getenv("XDG_RUNTIME_DIR");
1143 _cleanup_free_
char *ee
= NULL
;
1145 ee
= bus_address_escape(e
);
1150 (void) asprintf(&b
->address
, KERNEL_USER_BUS_FMT
";" UNIX_USER_BUS_FMT
, getuid(), ee
);
1152 (void) asprintf(&b
->address
, UNIX_USER_BUS_FMT
, ee
);
1156 (void) asprintf(&b
->address
, KERNEL_USER_BUS_FMT
, getuid());
1158 return -ECONNREFUSED
;
1168 _public_
int sd_bus_open_user(sd_bus
**ret
) {
1172 assert_return(ret
, -EINVAL
);
1178 r
= bus_set_address_user(b
);
1182 b
->bus_client
= true;
1185 /* We don't do any per-method access control on the user
1189 r
= sd_bus_start(b
);
1201 int bus_set_address_system_remote(sd_bus
*b
, const char *host
) {
1202 _cleanup_free_
char *e
= NULL
;
1203 char *m
= NULL
, *c
= NULL
;
1208 /* Let's see if we shall enter some container */
1209 m
= strchr(host
, ':');
1213 /* Let's make sure this is not a port of some kind,
1214 * and is a valid machine name. */
1215 if (!in_charset(m
, "0123456789") && machine_name_is_valid(m
)) {
1218 /* Cut out the host part */
1219 t
= strndupa(host
, m
- host
- 1);
1220 e
= bus_address_escape(t
);
1224 c
= strappenda(",argv4=--machine=", m
);
1229 e
= bus_address_escape(host
);
1234 b
->address
= strjoin("unixexec:path=ssh,argv1=-xT,argv2=", e
, ",argv3=systemd-stdio-bridge", c
, NULL
);
1241 _public_
int sd_bus_open_system_remote(sd_bus
**ret
, const char *host
) {
1245 assert_return(host
, -EINVAL
);
1246 assert_return(ret
, -EINVAL
);
1248 r
= sd_bus_new(&bus
);
1252 r
= bus_set_address_system_remote(bus
, host
);
1256 bus
->bus_client
= true;
1257 bus
->trusted
= false;
1259 r
= sd_bus_start(bus
);
1271 int bus_set_address_system_container(sd_bus
*b
, const char *machine
) {
1272 _cleanup_free_
char *e
= NULL
;
1277 e
= bus_address_escape(machine
);
1282 b
->address
= strjoin("x-container-kernel:machine=", e
, ";x-container-unix:machine=", e
, NULL
);
1284 b
->address
= strjoin("x-container-unix:machine=", e
, NULL
);
1292 _public_
int sd_bus_open_system_container(sd_bus
**ret
, const char *machine
) {
1296 assert_return(machine
, -EINVAL
);
1297 assert_return(ret
, -EINVAL
);
1298 assert_return(machine_name_is_valid(machine
), -EINVAL
);
1300 r
= sd_bus_new(&bus
);
1304 r
= bus_set_address_system_container(bus
, machine
);
1308 bus
->bus_client
= true;
1309 bus
->trusted
= false;
1311 r
= sd_bus_start(bus
);
1323 _public_
void sd_bus_close(sd_bus
*bus
) {
1327 if (bus
->state
== BUS_CLOSED
)
1329 if (bus_pid_changed(bus
))
1332 bus
->state
= BUS_CLOSED
;
1334 sd_bus_detach_event(bus
);
1336 /* Drop all queued messages so that they drop references to
1337 * the bus object and the bus may be freed */
1338 bus_reset_queues(bus
);
1340 if (!bus
->is_kernel
)
1343 /* We'll leave the fd open in case this is a kernel bus, since
1344 * there might still be memblocks around that reference this
1345 * bus, and they might need to invoke the KDBUS_CMD_FREE
1346 * ioctl on the fd when they are freed. */
1349 static void bus_enter_closing(sd_bus
*bus
) {
1352 if (bus
->state
!= BUS_OPENING
&&
1353 bus
->state
!= BUS_AUTHENTICATING
&&
1354 bus
->state
!= BUS_HELLO
&&
1355 bus
->state
!= BUS_RUNNING
)
1358 bus
->state
= BUS_CLOSING
;
1361 _public_ sd_bus
*sd_bus_ref(sd_bus
*bus
) {
1362 assert_return(bus
, NULL
);
1364 assert_se(REFCNT_INC(bus
->n_ref
) >= 2);
1369 _public_ sd_bus
*sd_bus_unref(sd_bus
*bus
) {
1375 i
= REFCNT_DEC(bus
->n_ref
);
1383 _public_
int sd_bus_is_open(sd_bus
*bus
) {
1385 assert_return(bus
, -EINVAL
);
1386 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1388 return BUS_IS_OPEN(bus
->state
);
1391 _public_
int sd_bus_can_send(sd_bus
*bus
, char type
) {
1394 assert_return(bus
, -EINVAL
);
1395 assert_return(bus
->state
!= BUS_UNSET
, -ENOTCONN
);
1396 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1398 if (bus
->hello_flags
& KDBUS_HELLO_MONITOR
)
1401 if (type
== SD_BUS_TYPE_UNIX_FD
) {
1402 if (!(bus
->hello_flags
& KDBUS_HELLO_ACCEPT_FD
))
1405 r
= bus_ensure_running(bus
);
1409 return bus
->can_fds
;
1412 return bus_type_is_valid(type
);
1415 _public_
int sd_bus_get_owner_id(sd_bus
*bus
, sd_id128_t
*id
) {
1418 assert_return(bus
, -EINVAL
);
1419 assert_return(id
, -EINVAL
);
1420 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1422 r
= bus_ensure_running(bus
);
1426 *id
= bus
->server_id
;
1430 static int bus_seal_message(sd_bus
*b
, sd_bus_message
*m
, usec_t timeout
) {
1435 /* If we copy the same message to multiple
1436 * destinations, avoid using the same cookie
1438 b
->cookie
= MAX(b
->cookie
, BUS_MESSAGE_COOKIE(m
));
1443 timeout
= BUS_DEFAULT_TIMEOUT
;
1445 return bus_message_seal(m
, ++b
->cookie
, timeout
);
1448 static int bus_remarshal_message(sd_bus
*b
, sd_bus_message
**m
) {
1451 /* Do packet version and endianness already match? */
1452 if ((b
->message_version
== 0 || b
->message_version
== (*m
)->header
->version
) &&
1453 (b
->message_endian
== 0 || b
->message_endian
== (*m
)->header
->endian
))
1456 /* No? Then remarshal! */
1457 return bus_message_remarshal(b
, m
);
1460 int bus_seal_synthetic_message(sd_bus
*b
, sd_bus_message
*m
) {
1464 /* The bus specification says the serial number cannot be 0,
1465 * hence let's fill something in for synthetic messages. Since
1466 * synthetic messages might have a fake sender and we don't
1467 * want to interfere with the real sender's serial numbers we
1468 * pick a fixed, artificial one. We use (uint32_t) -1 rather
1469 * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
1470 * even though kdbus can do 64bit. */
1472 return bus_message_seal(m
, 0xFFFFFFFFULL
, 0);
1475 static int bus_write_message(sd_bus
*bus
, sd_bus_message
*m
, bool hint_sync_call
, size_t *idx
) {
1482 r
= bus_kernel_write_message(bus
, m
, hint_sync_call
);
1484 r
= bus_socket_write_message(bus
, m
, idx
);
1489 if (bus
->is_kernel
|| *idx
>= BUS_MESSAGE_SIZE(m
))
1490 log_debug("Sent message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64
" reply_cookie=%" PRIu64
" error=%s",
1491 bus_message_type_to_string(m
->header
->type
),
1492 strna(sd_bus_message_get_sender(m
)),
1493 strna(sd_bus_message_get_destination(m
)),
1494 strna(sd_bus_message_get_path(m
)),
1495 strna(sd_bus_message_get_interface(m
)),
1496 strna(sd_bus_message_get_member(m
)),
1497 BUS_MESSAGE_COOKIE(m
),
1499 strna(m
->error
.message
));
1504 static int dispatch_wqueue(sd_bus
*bus
) {
1508 assert(bus
->state
== BUS_RUNNING
|| bus
->state
== BUS_HELLO
);
1510 while (bus
->wqueue_size
> 0) {
1512 r
= bus_write_message(bus
, bus
->wqueue
[0], false, &bus
->windex
);
1516 /* Didn't do anything this time */
1518 else if (bus
->is_kernel
|| bus
->windex
>= BUS_MESSAGE_SIZE(bus
->wqueue
[0])) {
1519 /* Fully written. Let's drop the entry from
1522 * This isn't particularly optimized, but
1523 * well, this is supposed to be our worst-case
1524 * buffer only, and the socket buffer is
1525 * supposed to be our primary buffer, and if
1526 * it got full, then all bets are off
1529 bus
->wqueue_size
--;
1530 sd_bus_message_unref(bus
->wqueue
[0]);
1531 memmove(bus
->wqueue
, bus
->wqueue
+ 1, sizeof(sd_bus_message
*) * bus
->wqueue_size
);
1541 static int bus_read_message(sd_bus
*bus
, bool hint_priority
, int64_t priority
) {
1545 return bus_kernel_read_message(bus
, hint_priority
, priority
);
1547 return bus_socket_read_message(bus
);
1550 int bus_rqueue_make_room(sd_bus
*bus
) {
1553 if (bus
->rqueue_size
>= BUS_RQUEUE_MAX
)
1556 if (!GREEDY_REALLOC(bus
->rqueue
, bus
->rqueue_allocated
, bus
->rqueue_size
+ 1))
1562 static int dispatch_rqueue(sd_bus
*bus
, bool hint_priority
, int64_t priority
, sd_bus_message
**m
) {
1567 assert(bus
->state
== BUS_RUNNING
|| bus
->state
== BUS_HELLO
);
1569 /* Note that the priority logic is only available on kdbus,
1570 * where the rqueue is unused. We check the rqueue here
1571 * anyway, because it's simple... */
1574 if (bus
->rqueue_size
> 0) {
1575 /* Dispatch a queued message */
1577 *m
= bus
->rqueue
[0];
1578 bus
->rqueue_size
--;
1579 memmove(bus
->rqueue
, bus
->rqueue
+ 1, sizeof(sd_bus_message
*) * bus
->rqueue_size
);
1583 /* Try to read a new message */
1584 r
= bus_read_message(bus
, hint_priority
, priority
);
1594 static int bus_send_internal(sd_bus
*bus
, sd_bus_message
*_m
, uint64_t *cookie
, bool hint_sync_call
) {
1595 _cleanup_bus_message_unref_ sd_bus_message
*m
= sd_bus_message_ref(_m
);
1598 assert_return(bus
, -EINVAL
);
1599 assert_return(m
, -EINVAL
);
1600 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1601 assert_return(!bus
->is_kernel
|| !(bus
->hello_flags
& KDBUS_HELLO_MONITOR
), -EROFS
);
1603 if (!BUS_IS_OPEN(bus
->state
))
1607 r
= sd_bus_can_send(bus
, SD_BUS_TYPE_UNIX_FD
);
1614 /* If the cookie number isn't kept, then we know that no reply
1616 if (!cookie
&& !m
->sealed
)
1617 m
->header
->flags
|= BUS_MESSAGE_NO_REPLY_EXPECTED
;
1619 r
= bus_seal_message(bus
, m
, 0);
1623 /* Remarshall if we have to. This will possibly unref the
1624 * message and place a replacement in m */
1625 r
= bus_remarshal_message(bus
, &m
);
1629 /* If this is a reply and no reply was requested, then let's
1630 * suppress this, if we can */
1631 if (m
->dont_send
&& !cookie
)
1634 if ((bus
->state
== BUS_RUNNING
|| bus
->state
== BUS_HELLO
) && bus
->wqueue_size
<= 0) {
1637 r
= bus_write_message(bus
, m
, hint_sync_call
, &idx
);
1639 if (r
== -ENOTCONN
|| r
== -ECONNRESET
|| r
== -EPIPE
|| r
== -ESHUTDOWN
) {
1640 bus_enter_closing(bus
);
1645 } else if (!bus
->is_kernel
&& idx
< BUS_MESSAGE_SIZE(m
)) {
1646 /* Wasn't fully written. So let's remember how
1647 * much was written. Note that the first entry
1648 * of the wqueue array is always allocated so
1649 * that we always can remember how much was
1651 bus
->wqueue
[0] = sd_bus_message_ref(m
);
1652 bus
->wqueue_size
= 1;
1656 /* Just append it to the queue. */
1658 if (bus
->wqueue_size
>= BUS_WQUEUE_MAX
)
1661 if (!GREEDY_REALLOC(bus
->wqueue
, bus
->wqueue_allocated
, bus
->wqueue_size
+ 1))
1664 bus
->wqueue
[bus
->wqueue_size
++] = sd_bus_message_ref(m
);
1668 *cookie
= BUS_MESSAGE_COOKIE(m
);
1673 _public_
int sd_bus_send(sd_bus
*bus
, sd_bus_message
*m
, uint64_t *cookie
) {
1674 return bus_send_internal(bus
, m
, cookie
, false);
1677 _public_
int sd_bus_send_to(sd_bus
*bus
, sd_bus_message
*m
, const char *destination
, uint64_t *cookie
) {
1680 assert_return(bus
, -EINVAL
);
1681 assert_return(m
, -EINVAL
);
1682 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1684 if (!BUS_IS_OPEN(bus
->state
))
1687 if (!streq_ptr(m
->destination
, destination
)) {
1692 r
= sd_bus_message_set_destination(m
, destination
);
1697 return sd_bus_send(bus
, m
, cookie
);
1700 static usec_t
calc_elapse(uint64_t usec
) {
1701 if (usec
== (uint64_t) -1)
1704 return now(CLOCK_MONOTONIC
) + usec
;
1707 static int timeout_compare(const void *a
, const void *b
) {
1708 const struct reply_callback
*x
= a
, *y
= b
;
1710 if (x
->timeout
!= 0 && y
->timeout
== 0)
1713 if (x
->timeout
== 0 && y
->timeout
!= 0)
1716 if (x
->timeout
< y
->timeout
)
1719 if (x
->timeout
> y
->timeout
)
1725 _public_
int sd_bus_call_async(
1729 sd_bus_message_handler_t callback
,
1733 _cleanup_bus_message_unref_ sd_bus_message
*m
= sd_bus_message_ref(_m
);
1734 _cleanup_bus_slot_unref_ sd_bus_slot
*s
= NULL
;
1737 assert_return(bus
, -EINVAL
);
1738 assert_return(m
, -EINVAL
);
1739 assert_return(m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
, -EINVAL
);
1740 assert_return(!(m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
), -EINVAL
);
1741 assert_return(callback
, -EINVAL
);
1742 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1743 assert_return(!bus
->is_kernel
|| !(bus
->hello_flags
& KDBUS_HELLO_MONITOR
), -EROFS
);
1745 if (!BUS_IS_OPEN(bus
->state
))
1748 r
= ordered_hashmap_ensure_allocated(&bus
->reply_callbacks
, &uint64_hash_ops
);
1752 r
= prioq_ensure_allocated(&bus
->reply_callbacks_prioq
, timeout_compare
);
1756 r
= bus_seal_message(bus
, m
, usec
);
1760 r
= bus_remarshal_message(bus
, &m
);
1764 s
= bus_slot_allocate(bus
, !slot
, BUS_REPLY_CALLBACK
, sizeof(struct reply_callback
), userdata
);
1768 s
->reply_callback
.callback
= callback
;
1770 s
->reply_callback
.cookie
= BUS_MESSAGE_COOKIE(m
);
1771 r
= ordered_hashmap_put(bus
->reply_callbacks
, &s
->reply_callback
.cookie
, &s
->reply_callback
);
1773 s
->reply_callback
.cookie
= 0;
1777 s
->reply_callback
.timeout
= calc_elapse(m
->timeout
);
1778 if (s
->reply_callback
.timeout
!= 0) {
1779 r
= prioq_put(bus
->reply_callbacks_prioq
, &s
->reply_callback
, &s
->reply_callback
.prioq_idx
);
1781 s
->reply_callback
.timeout
= 0;
1786 r
= sd_bus_send(bus
, m
, &s
->reply_callback
.cookie
);
1797 int bus_ensure_running(sd_bus
*bus
) {
1802 if (bus
->state
== BUS_UNSET
|| bus
->state
== BUS_CLOSED
|| bus
->state
== BUS_CLOSING
)
1804 if (bus
->state
== BUS_RUNNING
)
1808 r
= sd_bus_process(bus
, NULL
);
1811 if (bus
->state
== BUS_RUNNING
)
1816 r
= sd_bus_wait(bus
, (uint64_t) -1);
1822 _public_
int sd_bus_call(
1826 sd_bus_error
*error
,
1827 sd_bus_message
**reply
) {
1829 _cleanup_bus_message_unref_ sd_bus_message
*m
= sd_bus_message_ref(_m
);
1835 assert_return(bus
, -EINVAL
);
1836 assert_return(m
, -EINVAL
);
1837 assert_return(m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
, -EINVAL
);
1838 assert_return(!(m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
), -EINVAL
);
1839 assert_return(!bus_error_is_dirty(error
), -EINVAL
);
1840 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1841 assert_return(!bus
->is_kernel
|| !(bus
->hello_flags
& KDBUS_HELLO_MONITOR
), -EROFS
);
1843 if (!BUS_IS_OPEN(bus
->state
))
1846 r
= bus_ensure_running(bus
);
1850 i
= bus
->rqueue_size
;
1852 r
= bus_seal_message(bus
, m
, usec
);
1856 r
= bus_remarshal_message(bus
, &m
);
1860 r
= bus_send_internal(bus
, m
, &cookie
, true);
1864 timeout
= calc_elapse(m
->timeout
);
1869 while (i
< bus
->rqueue_size
) {
1870 sd_bus_message
*incoming
= NULL
;
1872 incoming
= bus
->rqueue
[i
];
1874 if (incoming
->reply_cookie
== cookie
) {
1875 /* Found a match! */
1877 memmove(bus
->rqueue
+ i
, bus
->rqueue
+ i
+ 1, sizeof(sd_bus_message
*) * (bus
->rqueue_size
- i
- 1));
1880 if (incoming
->header
->type
== SD_BUS_MESSAGE_METHOD_RETURN
) {
1882 if (incoming
->n_fds
<= 0 || (bus
->hello_flags
& KDBUS_HELLO_ACCEPT_FD
)) {
1886 sd_bus_message_unref(incoming
);
1891 r
= sd_bus_error_setf(error
, SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Reply message contained file descriptors which I couldn't accept. Sorry.");
1893 } else if (incoming
->header
->type
== SD_BUS_MESSAGE_METHOD_ERROR
)
1894 r
= sd_bus_error_copy(error
, &incoming
->error
);
1898 sd_bus_message_unref(incoming
);
1901 } else if (BUS_MESSAGE_COOKIE(incoming
) == cookie
&&
1904 streq(bus
->unique_name
, incoming
->sender
)) {
1906 memmove(bus
->rqueue
+ i
, bus
->rqueue
+ i
+ 1, sizeof(sd_bus_message
*) * (bus
->rqueue_size
- i
- 1));
1909 /* Our own message? Somebody is trying
1910 * to send its own client a message,
1911 * let's not dead-lock, let's fail
1914 sd_bus_message_unref(incoming
);
1918 /* Try to read more, right-away */
1922 r
= bus_read_message(bus
, false, 0);
1924 if (r
== -ENOTCONN
|| r
== -ECONNRESET
|| r
== -EPIPE
|| r
== -ESHUTDOWN
) {
1925 bus_enter_closing(bus
);
1937 n
= now(CLOCK_MONOTONIC
);
1943 left
= (uint64_t) -1;
1945 r
= bus_poll(bus
, true, left
);
1951 r
= dispatch_wqueue(bus
);
1953 if (r
== -ENOTCONN
|| r
== -ECONNRESET
|| r
== -EPIPE
|| r
== -ESHUTDOWN
) {
1954 bus_enter_closing(bus
);
1963 _public_
int sd_bus_get_fd(sd_bus
*bus
) {
1965 assert_return(bus
, -EINVAL
);
1966 assert_return(bus
->input_fd
== bus
->output_fd
, -EPERM
);
1967 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1969 return bus
->input_fd
;
1972 _public_
int sd_bus_get_events(sd_bus
*bus
) {
1975 assert_return(bus
, -EINVAL
);
1976 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1978 if (!BUS_IS_OPEN(bus
->state
) && bus
->state
!= BUS_CLOSING
)
1981 if (bus
->state
== BUS_OPENING
)
1983 else if (bus
->state
== BUS_AUTHENTICATING
) {
1985 if (bus_socket_auth_needs_write(bus
))
1990 } else if (bus
->state
== BUS_RUNNING
|| bus
->state
== BUS_HELLO
) {
1991 if (bus
->rqueue_size
<= 0)
1993 if (bus
->wqueue_size
> 0)
2000 _public_
int sd_bus_get_timeout(sd_bus
*bus
, uint64_t *timeout_usec
) {
2001 struct reply_callback
*c
;
2003 assert_return(bus
, -EINVAL
);
2004 assert_return(timeout_usec
, -EINVAL
);
2005 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2007 if (!BUS_IS_OPEN(bus
->state
) && bus
->state
!= BUS_CLOSING
)
2010 if (bus
->track_queue
) {
2015 if (bus
->state
== BUS_CLOSING
) {
2020 if (bus
->state
== BUS_AUTHENTICATING
) {
2021 *timeout_usec
= bus
->auth_timeout
;
2025 if (bus
->state
!= BUS_RUNNING
&& bus
->state
!= BUS_HELLO
) {
2026 *timeout_usec
= (uint64_t) -1;
2030 if (bus
->rqueue_size
> 0) {
2035 c
= prioq_peek(bus
->reply_callbacks_prioq
);
2037 *timeout_usec
= (uint64_t) -1;
2041 if (c
->timeout
== 0) {
2042 *timeout_usec
= (uint64_t) -1;
2046 *timeout_usec
= c
->timeout
;
2050 static int process_timeout(sd_bus
*bus
) {
2051 _cleanup_bus_error_free_ sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2052 _cleanup_bus_message_unref_ sd_bus_message
* m
= NULL
;
2053 struct reply_callback
*c
;
2060 c
= prioq_peek(bus
->reply_callbacks_prioq
);
2064 n
= now(CLOCK_MONOTONIC
);
2068 r
= bus_message_new_synthetic_error(
2071 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY
, "Method call timed out"),
2076 m
->sender
= "org.freedesktop.DBus";
2078 r
= bus_seal_synthetic_message(bus
, m
);
2082 assert_se(prioq_pop(bus
->reply_callbacks_prioq
) == c
);
2085 ordered_hashmap_remove(bus
->reply_callbacks
, &c
->cookie
);
2088 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
2090 bus
->iteration_counter
++;
2092 bus
->current_message
= m
;
2093 bus
->current_slot
= sd_bus_slot_ref(slot
);
2094 bus
->current_handler
= c
->callback
;
2095 bus
->current_userdata
= slot
->userdata
;
2096 r
= c
->callback(bus
, m
, slot
->userdata
, &error_buffer
);
2097 bus
->current_userdata
= NULL
;
2098 bus
->current_handler
= NULL
;
2099 bus
->current_slot
= NULL
;
2100 bus
->current_message
= NULL
;
2102 if (slot
->floating
) {
2103 bus_slot_disconnect(slot
);
2104 sd_bus_slot_unref(slot
);
2107 sd_bus_slot_unref(slot
);
2109 return bus_maybe_reply_error(m
, r
, &error_buffer
);
2112 static int process_hello(sd_bus
*bus
, sd_bus_message
*m
) {
2116 if (bus
->state
!= BUS_HELLO
)
2119 /* Let's make sure the first message on the bus is the HELLO
2120 * reply. But note that we don't actually parse the message
2121 * here (we leave that to the usual handling), we just verify
2122 * we don't let any earlier msg through. */
2124 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_RETURN
&&
2125 m
->header
->type
!= SD_BUS_MESSAGE_METHOD_ERROR
)
2128 if (m
->reply_cookie
!= 1)
2134 static int process_reply(sd_bus
*bus
, sd_bus_message
*m
) {
2135 _cleanup_bus_message_unref_ sd_bus_message
*synthetic_reply
= NULL
;
2136 _cleanup_bus_error_free_ sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2137 struct reply_callback
*c
;
2144 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_RETURN
&&
2145 m
->header
->type
!= SD_BUS_MESSAGE_METHOD_ERROR
)
2148 if (bus
->is_kernel
&& (bus
->hello_flags
& KDBUS_HELLO_MONITOR
))
2151 if (m
->destination
&& bus
->unique_name
&& !streq_ptr(m
->destination
, bus
->unique_name
))
2154 c
= ordered_hashmap_remove(bus
->reply_callbacks
, &m
->reply_cookie
);
2160 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
2162 if (m
->n_fds
> 0 && !(bus
->hello_flags
& KDBUS_HELLO_ACCEPT_FD
)) {
2164 /* If the reply contained a file descriptor which we
2165 * didn't want we pass an error instead. */
2167 r
= bus_message_new_synthetic_error(
2170 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Reply message contained file descriptor"),
2175 r
= bus_seal_synthetic_message(bus
, synthetic_reply
);
2179 m
= synthetic_reply
;
2181 r
= sd_bus_message_rewind(m
, true);
2186 if (c
->timeout
!= 0) {
2187 prioq_remove(bus
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
2191 bus
->current_slot
= sd_bus_slot_ref(slot
);
2192 bus
->current_handler
= c
->callback
;
2193 bus
->current_userdata
= slot
->userdata
;
2194 r
= c
->callback(bus
, m
, slot
->userdata
, &error_buffer
);
2195 bus
->current_userdata
= NULL
;
2196 bus
->current_handler
= NULL
;
2197 bus
->current_slot
= NULL
;
2199 if (slot
->floating
) {
2200 bus_slot_disconnect(slot
);
2201 sd_bus_slot_unref(slot
);
2204 sd_bus_slot_unref(slot
);
2206 return bus_maybe_reply_error(m
, r
, &error_buffer
);
2209 static int process_filter(sd_bus
*bus
, sd_bus_message
*m
) {
2210 _cleanup_bus_error_free_ sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2211 struct filter_callback
*l
;
2218 bus
->filter_callbacks_modified
= false;
2220 LIST_FOREACH(callbacks
, l
, bus
->filter_callbacks
) {
2223 if (bus
->filter_callbacks_modified
)
2226 /* Don't run this more than once per iteration */
2227 if (l
->last_iteration
== bus
->iteration_counter
)
2230 l
->last_iteration
= bus
->iteration_counter
;
2232 r
= sd_bus_message_rewind(m
, true);
2236 slot
= container_of(l
, sd_bus_slot
, filter_callback
);
2238 bus
->current_slot
= sd_bus_slot_ref(slot
);
2239 bus
->current_handler
= l
->callback
;
2240 bus
->current_userdata
= slot
->userdata
;
2241 r
= l
->callback(bus
, m
, slot
->userdata
, &error_buffer
);
2242 bus
->current_userdata
= NULL
;
2243 bus
->current_handler
= NULL
;
2244 bus
->current_slot
= sd_bus_slot_unref(slot
);
2246 r
= bus_maybe_reply_error(m
, r
, &error_buffer
);
2252 } while (bus
->filter_callbacks_modified
);
2257 static int process_match(sd_bus
*bus
, sd_bus_message
*m
) {
2264 bus
->match_callbacks_modified
= false;
2266 r
= bus_match_run(bus
, &bus
->match_callbacks
, m
);
2270 } while (bus
->match_callbacks_modified
);
2275 static int process_builtin(sd_bus
*bus
, sd_bus_message
*m
) {
2276 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
2282 if (bus
->hello_flags
& KDBUS_HELLO_MONITOR
)
2285 if (bus
->manual_peer_interface
)
2288 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_CALL
)
2291 if (!streq_ptr(m
->interface
, "org.freedesktop.DBus.Peer"))
2294 if (m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
)
2297 if (streq_ptr(m
->member
, "Ping"))
2298 r
= sd_bus_message_new_method_return(m
, &reply
);
2299 else if (streq_ptr(m
->member
, "GetMachineId")) {
2303 r
= sd_id128_get_machine(&id
);
2307 r
= sd_bus_message_new_method_return(m
, &reply
);
2311 r
= sd_bus_message_append(reply
, "s", sd_id128_to_string(id
, sid
));
2313 r
= sd_bus_message_new_method_errorf(
2315 SD_BUS_ERROR_UNKNOWN_METHOD
,
2316 "Unknown method '%s' on interface '%s'.", m
->member
, m
->interface
);
2322 r
= sd_bus_send(bus
, reply
, NULL
);
2329 static int process_fd_check(sd_bus
*bus
, sd_bus_message
*m
) {
2333 /* If we got a message with a file descriptor which we didn't
2334 * want to accept, then let's drop it. How can this even
2335 * happen? For example, when the kernel queues a message into
2336 * an activatable names's queue which allows fds, and then is
2337 * delivered to us later even though we ourselves did not
2340 if (bus
->hello_flags
& KDBUS_HELLO_MONITOR
)
2346 if (bus
->hello_flags
& KDBUS_HELLO_ACCEPT_FD
)
2349 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_CALL
)
2350 return 1; /* just eat it up */
2352 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Message contains file descriptors, which I cannot accept. Sorry.");
2355 static int process_message(sd_bus
*bus
, sd_bus_message
*m
) {
2361 bus
->current_message
= m
;
2362 bus
->iteration_counter
++;
2364 log_debug("Got message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64
" reply_cookie=%" PRIu64
" error=%s",
2365 bus_message_type_to_string(m
->header
->type
),
2366 strna(sd_bus_message_get_sender(m
)),
2367 strna(sd_bus_message_get_destination(m
)),
2368 strna(sd_bus_message_get_path(m
)),
2369 strna(sd_bus_message_get_interface(m
)),
2370 strna(sd_bus_message_get_member(m
)),
2371 BUS_MESSAGE_COOKIE(m
),
2373 strna(m
->error
.message
));
2375 r
= process_hello(bus
, m
);
2379 r
= process_reply(bus
, m
);
2383 r
= process_fd_check(bus
, m
);
2387 r
= process_filter(bus
, m
);
2391 r
= process_match(bus
, m
);
2395 r
= process_builtin(bus
, m
);
2399 r
= bus_process_object(bus
, m
);
2402 bus
->current_message
= NULL
;
2406 static int dispatch_track(sd_bus
*bus
) {
2409 if (!bus
->track_queue
)
2412 bus_track_dispatch(bus
->track_queue
);
2416 static int process_running(sd_bus
*bus
, bool hint_priority
, int64_t priority
, sd_bus_message
**ret
) {
2417 _cleanup_bus_message_unref_ sd_bus_message
*m
= NULL
;
2421 assert(bus
->state
== BUS_RUNNING
|| bus
->state
== BUS_HELLO
);
2423 r
= process_timeout(bus
);
2427 r
= dispatch_wqueue(bus
);
2431 r
= dispatch_track(bus
);
2435 r
= dispatch_rqueue(bus
, hint_priority
, priority
, &m
);
2441 r
= process_message(bus
, m
);
2446 r
= sd_bus_message_rewind(m
, true);
2455 if (m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
) {
2457 log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s",
2458 strna(sd_bus_message_get_sender(m
)),
2459 strna(sd_bus_message_get_path(m
)),
2460 strna(sd_bus_message_get_interface(m
)),
2461 strna(sd_bus_message_get_member(m
)));
2463 r
= sd_bus_reply_method_errorf(
2465 SD_BUS_ERROR_UNKNOWN_OBJECT
,
2466 "Unknown object '%s'.", m
->path
);
2480 static int process_closing(sd_bus
*bus
, sd_bus_message
**ret
) {
2481 _cleanup_bus_message_unref_ sd_bus_message
*m
= NULL
;
2482 struct reply_callback
*c
;
2486 assert(bus
->state
== BUS_CLOSING
);
2488 c
= ordered_hashmap_first(bus
->reply_callbacks
);
2490 _cleanup_bus_error_free_ sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2493 /* First, fail all outstanding method calls */
2494 r
= bus_message_new_synthetic_error(
2497 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY
, "Connection terminated"),
2502 r
= bus_seal_synthetic_message(bus
, m
);
2506 if (c
->timeout
!= 0) {
2507 prioq_remove(bus
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
2511 ordered_hashmap_remove(bus
->reply_callbacks
, &c
->cookie
);
2514 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
2516 bus
->iteration_counter
++;
2518 bus
->current_message
= m
;
2519 bus
->current_slot
= sd_bus_slot_ref(slot
);
2520 bus
->current_handler
= c
->callback
;
2521 bus
->current_userdata
= slot
->userdata
;
2522 r
= c
->callback(bus
, m
, slot
->userdata
, &error_buffer
);
2523 bus
->current_userdata
= NULL
;
2524 bus
->current_handler
= NULL
;
2525 bus
->current_slot
= NULL
;
2526 bus
->current_message
= NULL
;
2528 if (slot
->floating
) {
2529 bus_slot_disconnect(slot
);
2530 sd_bus_slot_unref(slot
);
2533 sd_bus_slot_unref(slot
);
2535 return bus_maybe_reply_error(m
, r
, &error_buffer
);
2538 /* Then, synthesize a Disconnected message */
2539 r
= sd_bus_message_new_signal(
2542 "/org/freedesktop/DBus/Local",
2543 "org.freedesktop.DBus.Local",
2548 m
->sender
= "org.freedesktop.DBus.Local";
2550 r
= bus_seal_synthetic_message(bus
, m
);
2556 bus
->current_message
= m
;
2557 bus
->iteration_counter
++;
2559 r
= process_filter(bus
, m
);
2563 r
= process_match(bus
, m
);
2575 bus
->current_message
= NULL
;
2580 static int bus_process_internal(sd_bus
*bus
, bool hint_priority
, int64_t priority
, sd_bus_message
**ret
) {
2581 BUS_DONT_DESTROY(bus
);
2584 /* Returns 0 when we didn't do anything. This should cause the
2585 * caller to invoke sd_bus_wait() before returning the next
2586 * time. Returns > 0 when we did something, which possibly
2587 * means *ret is filled in with an unprocessed message. */
2589 assert_return(bus
, -EINVAL
);
2590 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2592 /* We don't allow recursively invoking sd_bus_process(). */
2593 assert_return(!bus
->current_message
, -EBUSY
);
2594 assert(!bus
->current_slot
);
2596 switch (bus
->state
) {
2605 r
= bus_socket_process_opening(bus
);
2606 if (r
== -ENOTCONN
|| r
== -ECONNRESET
|| r
== -EPIPE
|| r
== -ESHUTDOWN
) {
2607 bus_enter_closing(bus
);
2615 case BUS_AUTHENTICATING
:
2616 r
= bus_socket_process_authenticating(bus
);
2617 if (r
== -ENOTCONN
|| r
== -ECONNRESET
|| r
== -EPIPE
|| r
== -ESHUTDOWN
) {
2618 bus_enter_closing(bus
);
2630 r
= process_running(bus
, hint_priority
, priority
, ret
);
2631 if (r
== -ENOTCONN
|| r
== -ECONNRESET
|| r
== -EPIPE
|| r
== -ESHUTDOWN
) {
2632 bus_enter_closing(bus
);
2642 return process_closing(bus
, ret
);
2645 assert_not_reached("Unknown state");
2648 _public_
int sd_bus_process(sd_bus
*bus
, sd_bus_message
**ret
) {
2649 return bus_process_internal(bus
, false, 0, ret
);
2652 _public_
int sd_bus_process_priority(sd_bus
*bus
, int64_t priority
, sd_bus_message
**ret
) {
2653 return bus_process_internal(bus
, true, priority
, ret
);
2656 static int bus_poll(sd_bus
*bus
, bool need_more
, uint64_t timeout_usec
) {
2657 struct pollfd p
[2] = {};
2660 usec_t m
= USEC_INFINITY
;
2664 if (bus
->state
== BUS_CLOSING
)
2667 if (!BUS_IS_OPEN(bus
->state
))
2670 e
= sd_bus_get_events(bus
);
2675 /* The caller really needs some more data, he doesn't
2676 * care about what's already read, or any timeouts
2681 /* The caller wants to process if there's something to
2682 * process, but doesn't care otherwise */
2684 r
= sd_bus_get_timeout(bus
, &until
);
2689 nw
= now(CLOCK_MONOTONIC
);
2690 m
= until
> nw
? until
- nw
: 0;
2694 if (timeout_usec
!= (uint64_t) -1 && (m
== (uint64_t) -1 || timeout_usec
< m
))
2697 p
[0].fd
= bus
->input_fd
;
2698 if (bus
->output_fd
== bus
->input_fd
) {
2702 p
[0].events
= e
& POLLIN
;
2703 p
[1].fd
= bus
->output_fd
;
2704 p
[1].events
= e
& POLLOUT
;
2708 r
= ppoll(p
, n
, m
== (uint64_t) -1 ? NULL
: timespec_store(&ts
, m
), NULL
);
2712 return r
> 0 ? 1 : 0;
2715 _public_
int sd_bus_wait(sd_bus
*bus
, uint64_t timeout_usec
) {
2717 assert_return(bus
, -EINVAL
);
2718 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2720 if (bus
->state
== BUS_CLOSING
)
2723 if (!BUS_IS_OPEN(bus
->state
))
2726 if (bus
->rqueue_size
> 0)
2729 return bus_poll(bus
, false, timeout_usec
);
2732 _public_
int sd_bus_flush(sd_bus
*bus
) {
2735 assert_return(bus
, -EINVAL
);
2736 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2738 if (bus
->state
== BUS_CLOSING
)
2741 if (!BUS_IS_OPEN(bus
->state
))
2744 r
= bus_ensure_running(bus
);
2748 if (bus
->wqueue_size
<= 0)
2752 r
= dispatch_wqueue(bus
);
2754 if (r
== -ENOTCONN
|| r
== -ECONNRESET
|| r
== -EPIPE
|| r
== -ESHUTDOWN
) {
2755 bus_enter_closing(bus
);
2762 if (bus
->wqueue_size
<= 0)
2765 r
= bus_poll(bus
, false, (uint64_t) -1);
2771 _public_
int sd_bus_add_filter(
2774 sd_bus_message_handler_t callback
,
2779 assert_return(bus
, -EINVAL
);
2780 assert_return(callback
, -EINVAL
);
2781 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2783 s
= bus_slot_allocate(bus
, !slot
, BUS_FILTER_CALLBACK
, sizeof(struct filter_callback
), userdata
);
2787 s
->filter_callback
.callback
= callback
;
2789 bus
->filter_callbacks_modified
= true;
2790 LIST_PREPEND(callbacks
, bus
->filter_callbacks
, &s
->filter_callback
);
2798 _public_
int sd_bus_add_match(
2802 sd_bus_message_handler_t callback
,
2805 struct bus_match_component
*components
= NULL
;
2806 unsigned n_components
= 0;
2807 sd_bus_slot
*s
= NULL
;
2810 assert_return(bus
, -EINVAL
);
2811 assert_return(match
, -EINVAL
);
2812 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2814 r
= bus_match_parse(match
, &components
, &n_components
);
2818 s
= bus_slot_allocate(bus
, !slot
, BUS_MATCH_CALLBACK
, sizeof(struct match_callback
), userdata
);
2824 s
->match_callback
.callback
= callback
;
2825 s
->match_callback
.cookie
= ++bus
->match_cookie
;
2827 if (bus
->bus_client
) {
2829 if (!bus
->is_kernel
) {
2830 /* When this is not a kernel transport, we
2831 * store the original match string, so that we
2832 * can use it to remove the match again */
2834 s
->match_callback
.match_string
= strdup(match
);
2835 if (!s
->match_callback
.match_string
) {
2841 r
= bus_add_match_internal(bus
, s
->match_callback
.match_string
, components
, n_components
, s
->match_callback
.cookie
);
2846 bus
->match_callbacks_modified
= true;
2847 r
= bus_match_add(&bus
->match_callbacks
, components
, n_components
, &s
->match_callback
);
2856 bus_match_parse_free(components
, n_components
);
2857 sd_bus_slot_unref(s
);
2862 int bus_remove_match_by_string(
2865 sd_bus_message_handler_t callback
,
2868 struct bus_match_component
*components
= NULL
;
2869 unsigned n_components
= 0;
2870 struct match_callback
*c
;
2873 assert_return(bus
, -EINVAL
);
2874 assert_return(match
, -EINVAL
);
2875 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2877 r
= bus_match_parse(match
, &components
, &n_components
);
2881 r
= bus_match_find(&bus
->match_callbacks
, components
, n_components
, NULL
, NULL
, &c
);
2885 sd_bus_slot_unref(container_of(c
, sd_bus_slot
, match_callback
));
2888 bus_match_parse_free(components
, n_components
);
2893 bool bus_pid_changed(sd_bus
*bus
) {
2896 /* We don't support people creating a bus connection and
2897 * keeping it around over a fork(). Let's complain. */
2899 return bus
->original_pid
!= getpid();
2902 static int io_callback(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
2903 sd_bus
*bus
= userdata
;
2908 r
= sd_bus_process(bus
, NULL
);
2915 static int time_callback(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
2916 sd_bus
*bus
= userdata
;
2921 r
= sd_bus_process(bus
, NULL
);
2928 static int prepare_callback(sd_event_source
*s
, void *userdata
) {
2929 sd_bus
*bus
= userdata
;
2936 e
= sd_bus_get_events(bus
);
2940 if (bus
->output_fd
!= bus
->input_fd
) {
2942 r
= sd_event_source_set_io_events(bus
->input_io_event_source
, e
& POLLIN
);
2946 r
= sd_event_source_set_io_events(bus
->output_io_event_source
, e
& POLLOUT
);
2950 r
= sd_event_source_set_io_events(bus
->input_io_event_source
, e
);
2955 r
= sd_bus_get_timeout(bus
, &until
);
2961 j
= sd_event_source_set_time(bus
->time_event_source
, until
);
2966 r
= sd_event_source_set_enabled(bus
->time_event_source
, r
> 0);
2973 static int quit_callback(sd_event_source
*event
, void *userdata
) {
2974 sd_bus
*bus
= userdata
;
2984 static int attach_io_events(sd_bus
*bus
) {
2989 if (bus
->input_fd
< 0)
2995 if (!bus
->input_io_event_source
) {
2996 r
= sd_event_add_io(bus
->event
, &bus
->input_io_event_source
, bus
->input_fd
, 0, io_callback
, bus
);
3000 r
= sd_event_source_set_prepare(bus
->input_io_event_source
, prepare_callback
);
3004 r
= sd_event_source_set_priority(bus
->input_io_event_source
, bus
->event_priority
);
3008 r
= sd_event_source_set_description(bus
->input_io_event_source
, "bus-input");
3010 r
= sd_event_source_set_io_fd(bus
->input_io_event_source
, bus
->input_fd
);
3015 if (bus
->output_fd
!= bus
->input_fd
) {
3016 assert(bus
->output_fd
>= 0);
3018 if (!bus
->output_io_event_source
) {
3019 r
= sd_event_add_io(bus
->event
, &bus
->output_io_event_source
, bus
->output_fd
, 0, io_callback
, bus
);
3023 r
= sd_event_source_set_priority(bus
->output_io_event_source
, bus
->event_priority
);
3027 r
= sd_event_source_set_description(bus
->input_io_event_source
, "bus-output");
3029 r
= sd_event_source_set_io_fd(bus
->output_io_event_source
, bus
->output_fd
);
3038 static void detach_io_events(sd_bus
*bus
) {
3041 if (bus
->input_io_event_source
) {
3042 sd_event_source_set_enabled(bus
->input_io_event_source
, SD_EVENT_OFF
);
3043 bus
->input_io_event_source
= sd_event_source_unref(bus
->input_io_event_source
);
3046 if (bus
->output_io_event_source
) {
3047 sd_event_source_set_enabled(bus
->output_io_event_source
, SD_EVENT_OFF
);
3048 bus
->output_io_event_source
= sd_event_source_unref(bus
->output_io_event_source
);
3052 _public_
int sd_bus_attach_event(sd_bus
*bus
, sd_event
*event
, int priority
) {
3055 assert_return(bus
, -EINVAL
);
3056 assert_return(!bus
->event
, -EBUSY
);
3058 assert(!bus
->input_io_event_source
);
3059 assert(!bus
->output_io_event_source
);
3060 assert(!bus
->time_event_source
);
3063 bus
->event
= sd_event_ref(event
);
3065 r
= sd_event_default(&bus
->event
);
3070 bus
->event_priority
= priority
;
3072 r
= sd_event_add_time(bus
->event
, &bus
->time_event_source
, CLOCK_MONOTONIC
, 0, 0, time_callback
, bus
);
3076 r
= sd_event_source_set_priority(bus
->time_event_source
, priority
);
3080 r
= sd_event_source_set_description(bus
->time_event_source
, "bus-time");
3084 r
= sd_event_add_exit(bus
->event
, &bus
->quit_event_source
, quit_callback
, bus
);
3088 r
= sd_event_source_set_description(bus
->quit_event_source
, "bus-exit");
3092 r
= attach_io_events(bus
);
3099 sd_bus_detach_event(bus
);
3103 _public_
int sd_bus_detach_event(sd_bus
*bus
) {
3104 assert_return(bus
, -EINVAL
);
3109 detach_io_events(bus
);
3111 if (bus
->time_event_source
) {
3112 sd_event_source_set_enabled(bus
->time_event_source
, SD_EVENT_OFF
);
3113 bus
->time_event_source
= sd_event_source_unref(bus
->time_event_source
);
3116 if (bus
->quit_event_source
) {
3117 sd_event_source_set_enabled(bus
->quit_event_source
, SD_EVENT_OFF
);
3118 bus
->quit_event_source
= sd_event_source_unref(bus
->quit_event_source
);
3121 bus
->event
= sd_event_unref(bus
->event
);
3125 _public_ sd_event
* sd_bus_get_event(sd_bus
*bus
) {
3126 assert_return(bus
, NULL
);
3131 _public_ sd_bus_message
* sd_bus_get_current_message(sd_bus
*bus
) {
3132 assert_return(bus
, NULL
);
3134 return bus
->current_message
;
3137 _public_ sd_bus_slot
* sd_bus_get_current_slot(sd_bus
*bus
) {
3138 assert_return(bus
, NULL
);
3140 return bus
->current_slot
;
3143 _public_ sd_bus_message_handler_t
sd_bus_get_current_handler(sd_bus
*bus
) {
3144 assert_return(bus
, NULL
);
3146 return bus
->current_handler
;
3149 _public_
void* sd_bus_get_current_userdata(sd_bus
*bus
) {
3150 assert_return(bus
, NULL
);
3152 return bus
->current_userdata
;
3155 static int bus_default(int (*bus_open
)(sd_bus
**), sd_bus
**default_bus
, sd_bus
**ret
) {
3160 assert(default_bus
);
3163 return !!*default_bus
;
3166 *ret
= sd_bus_ref(*default_bus
);
3174 b
->default_bus_ptr
= default_bus
;
3182 _public_
int sd_bus_default_system(sd_bus
**ret
) {
3183 static thread_local sd_bus
*default_system_bus
= NULL
;
3185 return bus_default(sd_bus_open_system
, &default_system_bus
, ret
);
3188 _public_
int sd_bus_default_user(sd_bus
**ret
) {
3189 static thread_local sd_bus
*default_user_bus
= NULL
;
3191 return bus_default(sd_bus_open_user
, &default_user_bus
, ret
);
3194 _public_
int sd_bus_default(sd_bus
**ret
) {
3198 /* Let's try our best to reuse another cached connection. If
3199 * the starter bus type is set, connect via our normal
3200 * connection logic, ignoring $DBUS_STARTER_ADDRESS, so that
3201 * we can share the connection with the user/system default
3204 e
= secure_getenv("DBUS_STARTER_BUS_TYPE");
3206 if (streq(e
, "system"))
3207 return sd_bus_default_system(ret
);
3208 else if (STR_IN_SET(e
, "user", "session"))
3209 return sd_bus_default_user(ret
);
3212 /* No type is specified, so we have not other option than to
3213 * use the starter address if it is set. */
3215 e
= secure_getenv("DBUS_STARTER_ADDRESS");
3217 static thread_local sd_bus
*default_starter_bus
= NULL
;
3219 return bus_default(sd_bus_open
, &default_starter_bus
, ret
);
3222 /* Finally, if nothing is set use the cached connection for
3223 * the right scope */
3225 if (cg_pid_get_owner_uid(0, NULL
) >= 0)
3226 return sd_bus_default_user(ret
);
3228 return sd_bus_default_system(ret
);
3231 _public_
int sd_bus_get_tid(sd_bus
*b
, pid_t
*tid
) {
3232 assert_return(b
, -EINVAL
);
3233 assert_return(tid
, -EINVAL
);
3234 assert_return(!bus_pid_changed(b
), -ECHILD
);
3242 return sd_event_get_tid(b
->event
, tid
);
3247 _public_
int sd_bus_path_encode(const char *prefix
, const char *external_id
, char **ret_path
) {
3248 _cleanup_free_
char *e
= NULL
;
3251 assert_return(object_path_is_valid(prefix
), -EINVAL
);
3252 assert_return(external_id
, -EINVAL
);
3253 assert_return(ret_path
, -EINVAL
);
3255 e
= bus_label_escape(external_id
);
3259 ret
= strjoin(prefix
, "/", e
, NULL
);
3267 _public_
int sd_bus_path_decode(const char *path
, const char *prefix
, char **external_id
) {
3271 assert_return(object_path_is_valid(path
), -EINVAL
);
3272 assert_return(object_path_is_valid(prefix
), -EINVAL
);
3273 assert_return(external_id
, -EINVAL
);
3275 e
= object_path_startswith(path
, prefix
);
3277 *external_id
= NULL
;
3281 ret
= bus_label_unescape(e
);
3289 _public_
int sd_bus_try_close(sd_bus
*bus
) {
3292 assert_return(bus
, -EINVAL
);
3293 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3295 if (!bus
->is_kernel
)
3298 if (!BUS_IS_OPEN(bus
->state
))
3301 if (bus
->rqueue_size
> 0)
3304 if (bus
->wqueue_size
> 0)
3307 r
= bus_kernel_try_close(bus
);
3315 _public_
int sd_bus_get_description(sd_bus
*bus
, const char **description
) {
3316 assert_return(bus
, -EINVAL
);
3317 assert_return(description
, -EINVAL
);
3318 assert_return(bus
->description
, -ENXIO
);
3319 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3321 *description
= bus
->description
;
3325 int bus_get_root_path(sd_bus
*bus
) {
3328 if (bus
->cgroup_root
)
3331 r
= cg_get_root_path(&bus
->cgroup_root
);
3333 bus
->cgroup_root
= strdup("/");
3334 if (!bus
->cgroup_root
)