2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 #include "alloc-util.h"
31 #include "bus-container.h"
32 #include "bus-control.h"
33 #include "bus-internal.h"
34 #include "bus-kernel.h"
35 #include "bus-label.h"
36 #include "bus-message.h"
37 #include "bus-objects.h"
38 #include "bus-protocol.h"
40 #include "bus-socket.h"
41 #include "bus-track.h"
44 #include "cgroup-util.h"
47 #include "hexdecoct.h"
48 #include "hostname-util.h"
51 #include "parse-util.h"
52 #include "string-util.h"
56 #define log_debug_bus_message(m) \
58 sd_bus_message *_mm = (m); \
59 log_debug("Got message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " error-name=%s error-message=%s", \
60 bus_message_type_to_string(_mm->header->type), \
61 strna(sd_bus_message_get_sender(_mm)), \
62 strna(sd_bus_message_get_destination(_mm)), \
63 strna(sd_bus_message_get_path(_mm)), \
64 strna(sd_bus_message_get_interface(_mm)), \
65 strna(sd_bus_message_get_member(_mm)), \
66 BUS_MESSAGE_COOKIE(_mm), \
68 strna(_mm->error.name), \
69 strna(_mm->error.message)); \
72 static int bus_poll(sd_bus
*bus
, bool need_more
, uint64_t timeout_usec
);
73 static int attach_io_events(sd_bus
*b
);
74 static void detach_io_events(sd_bus
*b
);
76 static thread_local sd_bus
*default_system_bus
= NULL
;
77 static thread_local sd_bus
*default_user_bus
= NULL
;
78 static thread_local sd_bus
*default_starter_bus
= NULL
;
80 static void bus_close_fds(sd_bus
*b
) {
85 if (b
->input_fd
!= b
->output_fd
)
86 safe_close(b
->output_fd
);
87 b
->output_fd
= b
->input_fd
= safe_close(b
->input_fd
);
90 static void bus_reset_queues(sd_bus
*b
) {
93 while (b
->rqueue_size
> 0)
94 sd_bus_message_unref(b
->rqueue
[--b
->rqueue_size
]);
96 b
->rqueue
= mfree(b
->rqueue
);
97 b
->rqueue_allocated
= 0;
99 while (b
->wqueue_size
> 0)
100 sd_bus_message_unref(b
->wqueue
[--b
->wqueue_size
]);
102 b
->wqueue
= mfree(b
->wqueue
);
103 b
->wqueue_allocated
= 0;
106 static void bus_free(sd_bus
*b
) {
110 assert(!b
->track_queue
);
113 b
->state
= BUS_CLOSED
;
115 sd_bus_detach_event(b
);
117 while ((s
= b
->slots
)) {
118 /* At this point only floating slots can still be
119 * around, because the non-floating ones keep a
120 * reference to the bus, and we thus couldn't be
121 * destructing right now... We forcibly disconnect the
122 * slots here, so that they still can be referenced by
123 * apps, but are dead. */
126 bus_slot_disconnect(s
);
127 sd_bus_slot_unref(s
);
130 if (b
->default_bus_ptr
)
131 *b
->default_bus_ptr
= NULL
;
137 free(b
->unique_name
);
138 free(b
->auth_buffer
);
141 free(b
->cgroup_root
);
142 free(b
->description
);
145 strv_free(b
->exec_argv
);
147 close_many(b
->fds
, b
->n_fds
);
152 ordered_hashmap_free_free(b
->reply_callbacks
);
153 prioq_free(b
->reply_callbacks_prioq
);
155 assert(b
->match_callbacks
.type
== BUS_MATCH_ROOT
);
156 bus_match_free(&b
->match_callbacks
);
158 hashmap_free_free(b
->vtable_methods
);
159 hashmap_free_free(b
->vtable_properties
);
161 assert(hashmap_isempty(b
->nodes
));
162 hashmap_free(b
->nodes
);
166 assert_se(pthread_mutex_destroy(&b
->memfd_cache_mutex
) == 0);
171 _public_
int sd_bus_new(sd_bus
**ret
) {
174 assert_return(ret
, -EINVAL
);
180 r
->n_ref
= REFCNT_INIT
;
181 r
->input_fd
= r
->output_fd
= -1;
182 r
->message_version
= 1;
183 r
->creds_mask
|= SD_BUS_CREDS_WELL_KNOWN_NAMES
|SD_BUS_CREDS_UNIQUE_NAME
;
184 r
->hello_flags
|= KDBUS_HELLO_ACCEPT_FD
;
185 r
->attach_flags
|= KDBUS_ATTACH_NAMES
;
186 r
->original_pid
= getpid_cached();
188 assert_se(pthread_mutex_init(&r
->memfd_cache_mutex
, NULL
) == 0);
190 /* We guarantee that wqueue always has space for at least one
192 if (!GREEDY_REALLOC(r
->wqueue
, r
->wqueue_allocated
, 1)) {
201 _public_
int sd_bus_set_address(sd_bus
*bus
, const char *address
) {
204 assert_return(bus
, -EINVAL
);
205 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
206 assert_return(address
, -EINVAL
);
207 assert_return(!bus_pid_changed(bus
), -ECHILD
);
219 _public_
int sd_bus_set_fd(sd_bus
*bus
, int input_fd
, int output_fd
) {
220 assert_return(bus
, -EINVAL
);
221 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
222 assert_return(input_fd
>= 0, -EBADF
);
223 assert_return(output_fd
>= 0, -EBADF
);
224 assert_return(!bus_pid_changed(bus
), -ECHILD
);
226 bus
->input_fd
= input_fd
;
227 bus
->output_fd
= output_fd
;
231 _public_
int sd_bus_set_exec(sd_bus
*bus
, const char *path
, char *const argv
[]) {
234 assert_return(bus
, -EINVAL
);
235 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
236 assert_return(path
, -EINVAL
);
237 assert_return(!strv_isempty(argv
), -EINVAL
);
238 assert_return(!bus_pid_changed(bus
), -ECHILD
);
250 free(bus
->exec_path
);
251 strv_free(bus
->exec_argv
);
259 _public_
int sd_bus_set_bus_client(sd_bus
*bus
, int b
) {
260 assert_return(bus
, -EINVAL
);
261 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
262 assert_return(!bus_pid_changed(bus
), -ECHILD
);
264 bus
->bus_client
= !!b
;
268 _public_
int sd_bus_set_monitor(sd_bus
*bus
, int b
) {
269 assert_return(bus
, -EINVAL
);
270 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
271 assert_return(!bus_pid_changed(bus
), -ECHILD
);
273 SET_FLAG(bus
->hello_flags
, KDBUS_HELLO_MONITOR
, b
);
277 _public_
int sd_bus_negotiate_fds(sd_bus
*bus
, int b
) {
278 assert_return(bus
, -EINVAL
);
279 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
280 assert_return(!bus_pid_changed(bus
), -ECHILD
);
282 SET_FLAG(bus
->hello_flags
, KDBUS_HELLO_ACCEPT_FD
, b
);
286 _public_
int sd_bus_negotiate_timestamp(sd_bus
*bus
, int b
) {
288 assert_return(bus
, -EINVAL
);
289 assert_return(!IN_SET(bus
->state
, BUS_CLOSING
, BUS_CLOSED
), -EPERM
);
290 assert_return(!bus_pid_changed(bus
), -ECHILD
);
292 new_flags
= bus
->attach_flags
;
293 SET_FLAG(new_flags
, KDBUS_ATTACH_TIMESTAMP
, b
);
295 if (bus
->attach_flags
== new_flags
)
298 bus
->attach_flags
= new_flags
;
303 _public_
int sd_bus_negotiate_creds(sd_bus
*bus
, int b
, uint64_t mask
) {
306 assert_return(bus
, -EINVAL
);
307 assert_return(mask
<= _SD_BUS_CREDS_ALL
, -EINVAL
);
308 assert_return(!IN_SET(bus
->state
, BUS_CLOSING
, BUS_CLOSED
), -EPERM
);
309 assert_return(!bus_pid_changed(bus
), -ECHILD
);
311 SET_FLAG(bus
->creds_mask
, mask
, b
);
313 /* The well knowns we need unconditionally, so that matches can work */
314 bus
->creds_mask
|= SD_BUS_CREDS_WELL_KNOWN_NAMES
|SD_BUS_CREDS_UNIQUE_NAME
;
316 /* Make sure we don't lose the timestamp flag */
317 new_flags
= (bus
->attach_flags
& KDBUS_ATTACH_TIMESTAMP
) | attach_flags_to_kdbus(bus
->creds_mask
);
318 if (bus
->attach_flags
== new_flags
)
321 bus
->attach_flags
= new_flags
;
326 _public_
int sd_bus_set_server(sd_bus
*bus
, int b
, sd_id128_t server_id
) {
327 assert_return(bus
, -EINVAL
);
328 assert_return(b
|| sd_id128_equal(server_id
, SD_ID128_NULL
), -EINVAL
);
329 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
330 assert_return(!bus_pid_changed(bus
), -ECHILD
);
332 bus
->is_server
= !!b
;
333 bus
->server_id
= server_id
;
337 _public_
int sd_bus_set_anonymous(sd_bus
*bus
, int b
) {
338 assert_return(bus
, -EINVAL
);
339 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
340 assert_return(!bus_pid_changed(bus
), -ECHILD
);
342 bus
->anonymous_auth
= !!b
;
346 _public_
int sd_bus_set_trusted(sd_bus
*bus
, int b
) {
347 assert_return(bus
, -EINVAL
);
348 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
349 assert_return(!bus_pid_changed(bus
), -ECHILD
);
355 _public_
int sd_bus_set_description(sd_bus
*bus
, const char *description
) {
356 assert_return(bus
, -EINVAL
);
357 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
358 assert_return(!bus_pid_changed(bus
), -ECHILD
);
360 return free_and_strdup(&bus
->description
, description
);
363 _public_
int sd_bus_set_allow_interactive_authorization(sd_bus
*bus
, int b
) {
364 assert_return(bus
, -EINVAL
);
365 assert_return(!bus_pid_changed(bus
), -ECHILD
);
367 bus
->allow_interactive_authorization
= !!b
;
371 _public_
int sd_bus_get_allow_interactive_authorization(sd_bus
*bus
) {
372 assert_return(bus
, -EINVAL
);
373 assert_return(!bus_pid_changed(bus
), -ECHILD
);
375 return bus
->allow_interactive_authorization
;
378 static int hello_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
386 assert(IN_SET(bus
->state
, BUS_HELLO
, BUS_CLOSING
));
388 r
= sd_bus_message_get_errno(reply
);
392 r
= sd_bus_message_read(reply
, "s", &s
);
396 if (!service_name_is_valid(s
) || s
[0] != ':')
399 bus
->unique_name
= strdup(s
);
400 if (!bus
->unique_name
)
403 if (bus
->state
== BUS_HELLO
)
404 bus
->state
= BUS_RUNNING
;
409 static int bus_send_hello(sd_bus
*bus
) {
410 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
415 if (!bus
->bus_client
)
418 r
= sd_bus_message_new_method_call(
421 "org.freedesktop.DBus",
422 "/org/freedesktop/DBus",
423 "org.freedesktop.DBus",
428 return sd_bus_call_async(bus
, NULL
, m
, hello_callback
, NULL
, 0);
431 int bus_start_running(sd_bus
*bus
) {
434 if (bus
->bus_client
) {
435 bus
->state
= BUS_HELLO
;
439 bus
->state
= BUS_RUNNING
;
443 static int parse_address_key(const char **p
, const char *key
, char **value
) {
444 size_t l
, n
= 0, allocated
= 0;
454 if (strncmp(*p
, key
, l
) != 0)
467 while (!IN_SET(*a
, ';', ',', 0)) {
485 c
= (char) ((x
<< 4) | y
);
492 if (!GREEDY_REALLOC(r
, allocated
, n
+ 2))
516 static void skip_address_key(const char **p
) {
520 *p
+= strcspn(*p
, ",");
526 static int parse_unix_address(sd_bus
*b
, const char **p
, char **guid
) {
527 _cleanup_free_
char *path
= NULL
, *abstract
= NULL
;
536 while (!IN_SET(**p
, 0, ';')) {
537 r
= parse_address_key(p
, "guid", guid
);
543 r
= parse_address_key(p
, "path", &path
);
549 r
= parse_address_key(p
, "abstract", &abstract
);
558 if (!path
&& !abstract
)
561 if (path
&& abstract
)
566 if (l
> sizeof(b
->sockaddr
.un
.sun_path
))
569 b
->sockaddr
.un
.sun_family
= AF_UNIX
;
570 strncpy(b
->sockaddr
.un
.sun_path
, path
, sizeof(b
->sockaddr
.un
.sun_path
));
571 b
->sockaddr_size
= offsetof(struct sockaddr_un
, sun_path
) + l
;
572 } else if (abstract
) {
573 l
= strlen(abstract
);
574 if (l
> sizeof(b
->sockaddr
.un
.sun_path
) - 1)
577 b
->sockaddr
.un
.sun_family
= AF_UNIX
;
578 b
->sockaddr
.un
.sun_path
[0] = 0;
579 strncpy(b
->sockaddr
.un
.sun_path
+1, abstract
, sizeof(b
->sockaddr
.un
.sun_path
)-1);
580 b
->sockaddr_size
= offsetof(struct sockaddr_un
, sun_path
) + 1 + l
;
588 static int parse_tcp_address(sd_bus
*b
, const char **p
, char **guid
) {
589 _cleanup_free_
char *host
= NULL
, *port
= NULL
, *family
= NULL
;
591 struct addrinfo
*result
, hints
= {
592 .ai_socktype
= SOCK_STREAM
,
593 .ai_flags
= AI_ADDRCONFIG
,
601 while (!IN_SET(**p
, 0, ';')) {
602 r
= parse_address_key(p
, "guid", guid
);
608 r
= parse_address_key(p
, "host", &host
);
614 r
= parse_address_key(p
, "port", &port
);
620 r
= parse_address_key(p
, "family", &family
);
633 if (streq(family
, "ipv4"))
634 hints
.ai_family
= AF_INET
;
635 else if (streq(family
, "ipv6"))
636 hints
.ai_family
= AF_INET6
;
641 r
= getaddrinfo(host
, port
, &hints
, &result
);
645 return -EADDRNOTAVAIL
;
647 memcpy(&b
->sockaddr
, result
->ai_addr
, result
->ai_addrlen
);
648 b
->sockaddr_size
= result
->ai_addrlen
;
650 freeaddrinfo(result
);
657 static int parse_exec_address(sd_bus
*b
, const char **p
, char **guid
) {
659 unsigned n_argv
= 0, j
;
661 size_t allocated
= 0;
669 while (!IN_SET(**p
, 0, ';')) {
670 r
= parse_address_key(p
, "guid", guid
);
676 r
= parse_address_key(p
, "path", &path
);
682 if (startswith(*p
, "argv")) {
686 ul
= strtoul(*p
+ 4, (char**) p
, 10);
687 if (errno
> 0 || **p
!= '=' || ul
> 256) {
695 if (!GREEDY_REALLOC0(argv
, allocated
, ul
+ 2)) {
703 r
= parse_address_key(p
, NULL
, argv
+ ul
);
718 /* Make sure there are no holes in the array, with the
719 * exception of argv[0] */
720 for (j
= 1; j
< n_argv
; j
++)
726 if (argv
&& argv
[0] == NULL
) {
727 argv
[0] = strdup(path
);
742 for (j
= 0; j
< n_argv
; j
++)
750 static int parse_container_unix_address(sd_bus
*b
, const char **p
, char **guid
) {
751 _cleanup_free_
char *machine
= NULL
, *pid
= NULL
;
759 while (!IN_SET(**p
, 0, ';')) {
760 r
= parse_address_key(p
, "guid", guid
);
766 r
= parse_address_key(p
, "machine", &machine
);
772 r
= parse_address_key(p
, "pid", &pid
);
781 if (!machine
== !pid
)
785 if (!machine_name_is_valid(machine
))
789 b
->machine
= machine
;
792 b
->machine
= mfree(b
->machine
);
796 r
= parse_pid(pid
, &b
->nspid
);
802 b
->sockaddr
.un
.sun_family
= AF_UNIX
;
803 strncpy(b
->sockaddr
.un
.sun_path
, "/var/run/dbus/system_bus_socket", sizeof(b
->sockaddr
.un
.sun_path
));
804 b
->sockaddr_size
= SOCKADDR_UN_LEN(b
->sockaddr
.un
);
810 static void bus_reset_parsed_address(sd_bus
*b
) {
814 b
->sockaddr_size
= 0;
815 b
->exec_argv
= strv_free(b
->exec_argv
);
816 b
->exec_path
= mfree(b
->exec_path
);
817 b
->server_id
= SD_ID128_NULL
;
818 b
->machine
= mfree(b
->machine
);
822 static int bus_parse_next_address(sd_bus
*b
) {
823 _cleanup_free_
char *guid
= NULL
;
831 if (b
->address
[b
->address_index
] == 0)
834 bus_reset_parsed_address(b
);
836 a
= b
->address
+ b
->address_index
;
845 if (startswith(a
, "unix:")) {
848 r
= parse_unix_address(b
, &a
, &guid
);
853 } else if (startswith(a
, "tcp:")) {
856 r
= parse_tcp_address(b
, &a
, &guid
);
862 } else if (startswith(a
, "unixexec:")) {
865 r
= parse_exec_address(b
, &a
, &guid
);
871 } else if (startswith(a
, "x-machine-unix:")) {
874 r
= parse_container_unix_address(b
, &a
, &guid
);
887 r
= sd_id128_from_string(guid
, &b
->server_id
);
892 b
->address_index
= a
- b
->address
;
896 static int bus_start_address(sd_bus
*b
) {
904 /* If you provide multiple different bus-addresses, we
905 * try all of them in order and use the first one that
909 r
= bus_socket_exec(b
);
911 else if ((b
->nspid
> 0 || b
->machine
) && b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
)
912 r
= bus_container_connect_socket(b
);
914 else if (b
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
)
915 r
= bus_socket_connect(b
);
921 r
= attach_io_events(b
);
926 b
->last_connect_error
= -r
;
929 r
= bus_parse_next_address(b
);
933 return b
->last_connect_error
> 0 ? -b
->last_connect_error
: -ECONNREFUSED
;
937 int bus_next_address(sd_bus
*b
) {
940 bus_reset_parsed_address(b
);
941 return bus_start_address(b
);
944 static int bus_start_fd(sd_bus
*b
) {
949 assert(b
->input_fd
>= 0);
950 assert(b
->output_fd
>= 0);
952 r
= fd_nonblock(b
->input_fd
, true);
956 r
= fd_cloexec(b
->input_fd
, true);
960 if (b
->input_fd
!= b
->output_fd
) {
961 r
= fd_nonblock(b
->output_fd
, true);
965 r
= fd_cloexec(b
->output_fd
, true);
970 if (fstat(b
->input_fd
, &st
) < 0)
973 return bus_socket_take_fd(b
);
976 _public_
int sd_bus_start(sd_bus
*bus
) {
979 assert_return(bus
, -EINVAL
);
980 assert_return(bus
->state
== BUS_UNSET
, -EPERM
);
981 assert_return(!bus_pid_changed(bus
), -ECHILD
);
983 bus
->state
= BUS_OPENING
;
985 if (bus
->is_server
&& bus
->bus_client
)
988 if (bus
->input_fd
>= 0)
989 r
= bus_start_fd(bus
);
990 else if (bus
->address
|| bus
->sockaddr
.sa
.sa_family
!= AF_UNSPEC
|| bus
->exec_path
|| bus
->machine
)
991 r
= bus_start_address(bus
);
1000 return bus_send_hello(bus
);
1003 _public_
int sd_bus_open(sd_bus
**ret
) {
1008 assert_return(ret
, -EINVAL
);
1010 /* Let's connect to the starter bus if it is set, and
1011 * otherwise to the bus that is appropropriate for the scope
1012 * we are running in */
1014 e
= secure_getenv("DBUS_STARTER_BUS_TYPE");
1016 if (streq(e
, "system"))
1017 return sd_bus_open_system(ret
);
1018 else if (STR_IN_SET(e
, "session", "user"))
1019 return sd_bus_open_user(ret
);
1022 e
= secure_getenv("DBUS_STARTER_ADDRESS");
1024 if (cg_pid_get_owner_uid(0, NULL
) >= 0)
1025 return sd_bus_open_user(ret
);
1027 return sd_bus_open_system(ret
);
1034 r
= sd_bus_set_address(b
, e
);
1038 b
->bus_client
= true;
1040 /* We don't know whether the bus is trusted or not, so better
1041 * be safe, and authenticate everything */
1043 b
->is_local
= false;
1044 b
->attach_flags
|= KDBUS_ATTACH_CAPS
| KDBUS_ATTACH_CREDS
;
1045 b
->creds_mask
|= SD_BUS_CREDS_UID
| SD_BUS_CREDS_EUID
| SD_BUS_CREDS_EFFECTIVE_CAPS
;
1047 r
= sd_bus_start(b
);
1059 int bus_set_address_system(sd_bus
*b
) {
1063 e
= secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1065 return sd_bus_set_address(b
, e
);
1067 return sd_bus_set_address(b
, DEFAULT_SYSTEM_BUS_ADDRESS
);
1070 _public_
int sd_bus_open_system(sd_bus
**ret
) {
1074 assert_return(ret
, -EINVAL
);
1080 r
= bus_set_address_system(b
);
1084 b
->bus_client
= true;
1085 b
->is_system
= true;
1087 /* Let's do per-method access control on the system bus. We
1088 * need the caller's UID and capability set for that. */
1090 b
->attach_flags
|= KDBUS_ATTACH_CAPS
| KDBUS_ATTACH_CREDS
;
1091 b
->creds_mask
|= SD_BUS_CREDS_UID
| SD_BUS_CREDS_EUID
| SD_BUS_CREDS_EFFECTIVE_CAPS
;
1094 r
= sd_bus_start(b
);
1106 int bus_set_address_user(sd_bus
*b
) {
1108 _cleanup_free_
char *ee
= NULL
, *s
= NULL
;
1112 e
= secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1114 return sd_bus_set_address(b
, e
);
1116 e
= secure_getenv("XDG_RUNTIME_DIR");
1120 ee
= bus_address_escape(e
);
1124 if (asprintf(&s
, UNIX_USER_BUS_ADDRESS_FMT
, ee
) < 0)
1133 _public_
int sd_bus_open_user(sd_bus
**ret
) {
1137 assert_return(ret
, -EINVAL
);
1143 r
= bus_set_address_user(b
);
1147 b
->bus_client
= true;
1150 /* We don't do any per-method access control on the user
1155 r
= sd_bus_start(b
);
1167 int bus_set_address_system_remote(sd_bus
*b
, const char *host
) {
1168 _cleanup_free_
char *e
= NULL
;
1169 char *m
= NULL
, *c
= NULL
;
1174 /* Let's see if we shall enter some container */
1175 m
= strchr(host
, ':');
1179 /* Let's make sure this is not a port of some kind,
1180 * and is a valid machine name. */
1181 if (!in_charset(m
, "0123456789") && machine_name_is_valid(m
)) {
1184 /* Cut out the host part */
1185 t
= strndupa(host
, m
- host
- 1);
1186 e
= bus_address_escape(t
);
1190 c
= strjoina(",argv5=--machine=", m
);
1195 e
= bus_address_escape(host
);
1200 b
->address
= strjoin("unixexec:path=ssh,argv1=-xT,argv2=--,argv3=", e
, ",argv4=systemd-stdio-bridge", c
);
1207 _public_
int sd_bus_open_system_remote(sd_bus
**ret
, const char *host
) {
1211 assert_return(host
, -EINVAL
);
1212 assert_return(ret
, -EINVAL
);
1214 r
= sd_bus_new(&bus
);
1218 r
= bus_set_address_system_remote(bus
, host
);
1222 bus
->bus_client
= true;
1223 bus
->trusted
= false;
1224 bus
->is_system
= true;
1225 bus
->is_local
= false;
1227 r
= sd_bus_start(bus
);
1239 int bus_set_address_system_machine(sd_bus
*b
, const char *machine
) {
1240 _cleanup_free_
char *e
= NULL
;
1245 e
= bus_address_escape(machine
);
1249 b
->address
= strjoin("x-machine-unix:machine=", e
);
1256 _public_
int sd_bus_open_system_machine(sd_bus
**ret
, const char *machine
) {
1260 assert_return(machine
, -EINVAL
);
1261 assert_return(ret
, -EINVAL
);
1262 assert_return(machine_name_is_valid(machine
), -EINVAL
);
1264 r
= sd_bus_new(&bus
);
1268 r
= bus_set_address_system_machine(bus
, machine
);
1272 bus
->bus_client
= true;
1273 bus
->trusted
= false;
1274 bus
->is_system
= true;
1275 bus
->is_local
= false;
1277 r
= sd_bus_start(bus
);
1289 _public_
void sd_bus_close(sd_bus
*bus
) {
1293 if (bus
->state
== BUS_CLOSED
)
1295 if (bus_pid_changed(bus
))
1298 bus
->state
= BUS_CLOSED
;
1300 sd_bus_detach_event(bus
);
1302 /* Drop all queued messages so that they drop references to
1303 * the bus object and the bus may be freed */
1304 bus_reset_queues(bus
);
1309 _public_ sd_bus
* sd_bus_flush_close_unref(sd_bus
*bus
) {
1317 return sd_bus_unref(bus
);
1320 static void bus_enter_closing(sd_bus
*bus
) {
1323 if (!IN_SET(bus
->state
, BUS_OPENING
, BUS_AUTHENTICATING
, BUS_HELLO
, BUS_RUNNING
))
1326 bus
->state
= BUS_CLOSING
;
1329 _public_ sd_bus
*sd_bus_ref(sd_bus
*bus
) {
1334 assert_se(REFCNT_INC(bus
->n_ref
) >= 2);
1339 _public_ sd_bus
*sd_bus_unref(sd_bus
*bus
) {
1345 i
= REFCNT_DEC(bus
->n_ref
);
1353 _public_
int sd_bus_is_open(sd_bus
*bus
) {
1355 assert_return(bus
, -EINVAL
);
1356 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1358 return BUS_IS_OPEN(bus
->state
);
1361 _public_
int sd_bus_can_send(sd_bus
*bus
, char type
) {
1364 assert_return(bus
, -EINVAL
);
1365 assert_return(bus
->state
!= BUS_UNSET
, -ENOTCONN
);
1366 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1368 if (bus
->hello_flags
& KDBUS_HELLO_MONITOR
)
1371 if (type
== SD_BUS_TYPE_UNIX_FD
) {
1372 if (!(bus
->hello_flags
& KDBUS_HELLO_ACCEPT_FD
))
1375 r
= bus_ensure_running(bus
);
1379 return bus
->can_fds
;
1382 return bus_type_is_valid(type
);
1385 _public_
int sd_bus_get_bus_id(sd_bus
*bus
, sd_id128_t
*id
) {
1388 assert_return(bus
, -EINVAL
);
1389 assert_return(id
, -EINVAL
);
1390 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1392 r
= bus_ensure_running(bus
);
1396 *id
= bus
->server_id
;
1400 static int bus_seal_message(sd_bus
*b
, sd_bus_message
*m
, usec_t timeout
) {
1405 /* If we copy the same message to multiple
1406 * destinations, avoid using the same cookie
1408 b
->cookie
= MAX(b
->cookie
, BUS_MESSAGE_COOKIE(m
));
1413 timeout
= BUS_DEFAULT_TIMEOUT
;
1415 return sd_bus_message_seal(m
, ++b
->cookie
, timeout
);
1418 static int bus_remarshal_message(sd_bus
*b
, sd_bus_message
**m
) {
1419 bool remarshal
= false;
1423 /* wrong packet version */
1424 if (b
->message_version
!= 0 && b
->message_version
!= (*m
)->header
->version
)
1427 /* wrong packet endianness */
1428 if (b
->message_endian
!= 0 && b
->message_endian
!= (*m
)->header
->endian
)
1431 return remarshal
? bus_message_remarshal(b
, m
) : 0;
1434 int bus_seal_synthetic_message(sd_bus
*b
, sd_bus_message
*m
) {
1438 /* Fake some timestamps, if they were requested, and not
1439 * already initialized */
1440 if (b
->attach_flags
& KDBUS_ATTACH_TIMESTAMP
) {
1441 if (m
->realtime
<= 0)
1442 m
->realtime
= now(CLOCK_REALTIME
);
1444 if (m
->monotonic
<= 0)
1445 m
->monotonic
= now(CLOCK_MONOTONIC
);
1448 /* The bus specification says the serial number cannot be 0,
1449 * hence let's fill something in for synthetic messages. Since
1450 * synthetic messages might have a fake sender and we don't
1451 * want to interfere with the real sender's serial numbers we
1452 * pick a fixed, artificial one. We use (uint32_t) -1 rather
1453 * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
1454 * even though kdbus can do 64bit. */
1455 return sd_bus_message_seal(m
, 0xFFFFFFFFULL
, 0);
1458 static int bus_write_message(sd_bus
*bus
, sd_bus_message
*m
, bool hint_sync_call
, size_t *idx
) {
1464 r
= bus_socket_write_message(bus
, m
, idx
);
1468 if (*idx
>= BUS_MESSAGE_SIZE(m
))
1469 log_debug("Sent message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64
" reply_cookie=%" PRIu64
" error-name=%s error-message=%s",
1470 bus_message_type_to_string(m
->header
->type
),
1471 strna(sd_bus_message_get_sender(m
)),
1472 strna(sd_bus_message_get_destination(m
)),
1473 strna(sd_bus_message_get_path(m
)),
1474 strna(sd_bus_message_get_interface(m
)),
1475 strna(sd_bus_message_get_member(m
)),
1476 BUS_MESSAGE_COOKIE(m
),
1478 strna(m
->error
.name
),
1479 strna(m
->error
.message
));
1484 static int dispatch_wqueue(sd_bus
*bus
) {
1488 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1490 while (bus
->wqueue_size
> 0) {
1492 r
= bus_write_message(bus
, bus
->wqueue
[0], false, &bus
->windex
);
1496 /* Didn't do anything this time */
1498 else if (bus
->windex
>= BUS_MESSAGE_SIZE(bus
->wqueue
[0])) {
1499 /* Fully written. Let's drop the entry from
1502 * This isn't particularly optimized, but
1503 * well, this is supposed to be our worst-case
1504 * buffer only, and the socket buffer is
1505 * supposed to be our primary buffer, and if
1506 * it got full, then all bets are off
1510 sd_bus_message_unref(bus
->wqueue
[0]);
1511 memmove(bus
->wqueue
, bus
->wqueue
+ 1, sizeof(sd_bus_message
*) * bus
->wqueue_size
);
1521 static int bus_read_message(sd_bus
*bus
, bool hint_priority
, int64_t priority
) {
1524 return bus_socket_read_message(bus
);
1527 int bus_rqueue_make_room(sd_bus
*bus
) {
1530 if (bus
->rqueue_size
>= BUS_RQUEUE_MAX
)
1533 if (!GREEDY_REALLOC(bus
->rqueue
, bus
->rqueue_allocated
, bus
->rqueue_size
+ 1))
1539 static int dispatch_rqueue(sd_bus
*bus
, bool hint_priority
, int64_t priority
, sd_bus_message
**m
) {
1544 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
1546 /* Note that the priority logic is only available on kdbus,
1547 * where the rqueue is unused. We check the rqueue here
1548 * anyway, because it's simple... */
1551 if (bus
->rqueue_size
> 0) {
1552 /* Dispatch a queued message */
1554 *m
= bus
->rqueue
[0];
1556 memmove(bus
->rqueue
, bus
->rqueue
+ 1, sizeof(sd_bus_message
*) * bus
->rqueue_size
);
1560 /* Try to read a new message */
1561 r
= bus_read_message(bus
, hint_priority
, priority
);
1571 static int bus_send_internal(sd_bus
*bus
, sd_bus_message
*_m
, uint64_t *cookie
, bool hint_sync_call
) {
1572 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= sd_bus_message_ref(_m
);
1575 assert_return(m
, -EINVAL
);
1580 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1582 if (!BUS_IS_OPEN(bus
->state
))
1586 r
= sd_bus_can_send(bus
, SD_BUS_TYPE_UNIX_FD
);
1593 /* If the cookie number isn't kept, then we know that no reply
1595 if (!cookie
&& !m
->sealed
)
1596 m
->header
->flags
|= BUS_MESSAGE_NO_REPLY_EXPECTED
;
1598 r
= bus_seal_message(bus
, m
, 0);
1602 /* Remarshall if we have to. This will possibly unref the
1603 * message and place a replacement in m */
1604 r
= bus_remarshal_message(bus
, &m
);
1608 /* If this is a reply and no reply was requested, then let's
1609 * suppress this, if we can */
1613 if (IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
) && bus
->wqueue_size
<= 0) {
1616 r
= bus_write_message(bus
, m
, hint_sync_call
, &idx
);
1618 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
1619 bus_enter_closing(bus
);
1626 if (idx
< BUS_MESSAGE_SIZE(m
)) {
1627 /* Wasn't fully written. So let's remember how
1628 * much was written. Note that the first entry
1629 * of the wqueue array is always allocated so
1630 * that we always can remember how much was
1632 bus
->wqueue
[0] = sd_bus_message_ref(m
);
1633 bus
->wqueue_size
= 1;
1638 /* Just append it to the queue. */
1640 if (bus
->wqueue_size
>= BUS_WQUEUE_MAX
)
1643 if (!GREEDY_REALLOC(bus
->wqueue
, bus
->wqueue_allocated
, bus
->wqueue_size
+ 1))
1646 bus
->wqueue
[bus
->wqueue_size
++] = sd_bus_message_ref(m
);
1651 *cookie
= BUS_MESSAGE_COOKIE(m
);
1656 _public_
int sd_bus_send(sd_bus
*bus
, sd_bus_message
*m
, uint64_t *cookie
) {
1657 return bus_send_internal(bus
, m
, cookie
, false);
1660 _public_
int sd_bus_send_to(sd_bus
*bus
, sd_bus_message
*m
, const char *destination
, uint64_t *cookie
) {
1663 assert_return(m
, -EINVAL
);
1668 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1670 if (!BUS_IS_OPEN(bus
->state
))
1673 if (!streq_ptr(m
->destination
, destination
)) {
1678 r
= sd_bus_message_set_destination(m
, destination
);
1683 return sd_bus_send(bus
, m
, cookie
);
1686 static usec_t
calc_elapse(uint64_t usec
) {
1687 if (usec
== (uint64_t) -1)
1690 return now(CLOCK_MONOTONIC
) + usec
;
1693 static int timeout_compare(const void *a
, const void *b
) {
1694 const struct reply_callback
*x
= a
, *y
= b
;
1696 if (x
->timeout
!= 0 && y
->timeout
== 0)
1699 if (x
->timeout
== 0 && y
->timeout
!= 0)
1702 if (x
->timeout
< y
->timeout
)
1705 if (x
->timeout
> y
->timeout
)
1711 _public_
int sd_bus_call_async(
1715 sd_bus_message_handler_t callback
,
1719 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= sd_bus_message_ref(_m
);
1720 _cleanup_(sd_bus_slot_unrefp
) sd_bus_slot
*s
= NULL
;
1723 assert_return(m
, -EINVAL
);
1724 assert_return(m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
, -EINVAL
);
1725 assert_return(!(m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
), -EINVAL
);
1726 assert_return(callback
, -EINVAL
);
1731 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1733 if (!BUS_IS_OPEN(bus
->state
))
1736 r
= ordered_hashmap_ensure_allocated(&bus
->reply_callbacks
, &uint64_hash_ops
);
1740 r
= prioq_ensure_allocated(&bus
->reply_callbacks_prioq
, timeout_compare
);
1744 r
= bus_seal_message(bus
, m
, usec
);
1748 r
= bus_remarshal_message(bus
, &m
);
1752 s
= bus_slot_allocate(bus
, !slot
, BUS_REPLY_CALLBACK
, sizeof(struct reply_callback
), userdata
);
1756 s
->reply_callback
.callback
= callback
;
1758 s
->reply_callback
.cookie
= BUS_MESSAGE_COOKIE(m
);
1759 r
= ordered_hashmap_put(bus
->reply_callbacks
, &s
->reply_callback
.cookie
, &s
->reply_callback
);
1761 s
->reply_callback
.cookie
= 0;
1765 s
->reply_callback
.timeout
= calc_elapse(m
->timeout
);
1766 if (s
->reply_callback
.timeout
!= 0) {
1767 r
= prioq_put(bus
->reply_callbacks_prioq
, &s
->reply_callback
, &s
->reply_callback
.prioq_idx
);
1769 s
->reply_callback
.timeout
= 0;
1774 r
= sd_bus_send(bus
, m
, &s
->reply_callback
.cookie
);
1785 int bus_ensure_running(sd_bus
*bus
) {
1790 if (IN_SET(bus
->state
, BUS_UNSET
, BUS_CLOSED
, BUS_CLOSING
))
1792 if (bus
->state
== BUS_RUNNING
)
1796 r
= sd_bus_process(bus
, NULL
);
1799 if (bus
->state
== BUS_RUNNING
)
1804 r
= sd_bus_wait(bus
, (uint64_t) -1);
1810 _public_
int sd_bus_call(
1814 sd_bus_error
*error
,
1815 sd_bus_message
**reply
) {
1817 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= sd_bus_message_ref(_m
);
1823 bus_assert_return(m
, -EINVAL
, error
);
1824 bus_assert_return(m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
, -EINVAL
, error
);
1825 bus_assert_return(!(m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
), -EINVAL
, error
);
1826 bus_assert_return(!bus_error_is_dirty(error
), -EINVAL
, error
);
1831 bus_assert_return(!bus_pid_changed(bus
), -ECHILD
, error
);
1833 if (!BUS_IS_OPEN(bus
->state
)) {
1838 r
= bus_ensure_running(bus
);
1842 i
= bus
->rqueue_size
;
1844 r
= bus_seal_message(bus
, m
, usec
);
1848 r
= bus_remarshal_message(bus
, &m
);
1852 r
= bus_send_internal(bus
, m
, &cookie
, true);
1856 timeout
= calc_elapse(m
->timeout
);
1861 while (i
< bus
->rqueue_size
) {
1862 sd_bus_message
*incoming
= NULL
;
1864 incoming
= bus
->rqueue
[i
];
1866 if (incoming
->reply_cookie
== cookie
) {
1867 /* Found a match! */
1869 memmove(bus
->rqueue
+ i
, bus
->rqueue
+ i
+ 1, sizeof(sd_bus_message
*) * (bus
->rqueue_size
- i
- 1));
1871 log_debug_bus_message(incoming
);
1873 if (incoming
->header
->type
== SD_BUS_MESSAGE_METHOD_RETURN
) {
1875 if (incoming
->n_fds
<= 0 || (bus
->hello_flags
& KDBUS_HELLO_ACCEPT_FD
)) {
1879 sd_bus_message_unref(incoming
);
1884 r
= sd_bus_error_setf(error
, SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Reply message contained file descriptors which I couldn't accept. Sorry.");
1885 sd_bus_message_unref(incoming
);
1888 } else if (incoming
->header
->type
== SD_BUS_MESSAGE_METHOD_ERROR
) {
1889 r
= sd_bus_error_copy(error
, &incoming
->error
);
1890 sd_bus_message_unref(incoming
);
1897 } else if (BUS_MESSAGE_COOKIE(incoming
) == cookie
&&
1900 streq(bus
->unique_name
, incoming
->sender
)) {
1902 memmove(bus
->rqueue
+ i
, bus
->rqueue
+ i
+ 1, sizeof(sd_bus_message
*) * (bus
->rqueue_size
- i
- 1));
1905 /* Our own message? Somebody is trying
1906 * to send its own client a message,
1907 * let's not dead-lock, let's fail
1910 sd_bus_message_unref(incoming
);
1915 /* Try to read more, right-away */
1919 r
= bus_read_message(bus
, false, 0);
1921 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
1922 bus_enter_closing(bus
);
1934 n
= now(CLOCK_MONOTONIC
);
1942 left
= (uint64_t) -1;
1944 r
= bus_poll(bus
, true, left
);
1952 r
= dispatch_wqueue(bus
);
1954 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
1955 bus_enter_closing(bus
);
1964 return sd_bus_error_set_errno(error
, r
);
1967 _public_
int sd_bus_get_fd(sd_bus
*bus
) {
1969 assert_return(bus
, -EINVAL
);
1970 assert_return(bus
->input_fd
== bus
->output_fd
, -EPERM
);
1971 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1973 return bus
->input_fd
;
1976 _public_
int sd_bus_get_events(sd_bus
*bus
) {
1979 assert_return(bus
, -EINVAL
);
1980 assert_return(!bus_pid_changed(bus
), -ECHILD
);
1982 if (!BUS_IS_OPEN(bus
->state
) && bus
->state
!= BUS_CLOSING
)
1985 if (bus
->state
== BUS_OPENING
)
1987 else if (bus
->state
== BUS_AUTHENTICATING
) {
1989 if (bus_socket_auth_needs_write(bus
))
1994 } else if (IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
)) {
1995 if (bus
->rqueue_size
<= 0)
1997 if (bus
->wqueue_size
> 0)
2004 _public_
int sd_bus_get_timeout(sd_bus
*bus
, uint64_t *timeout_usec
) {
2005 struct reply_callback
*c
;
2007 assert_return(bus
, -EINVAL
);
2008 assert_return(timeout_usec
, -EINVAL
);
2009 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2011 if (!BUS_IS_OPEN(bus
->state
) && bus
->state
!= BUS_CLOSING
)
2014 if (bus
->track_queue
) {
2019 if (bus
->state
== BUS_CLOSING
) {
2024 if (bus
->state
== BUS_AUTHENTICATING
) {
2025 *timeout_usec
= bus
->auth_timeout
;
2029 if (!IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
)) {
2030 *timeout_usec
= (uint64_t) -1;
2034 if (bus
->rqueue_size
> 0) {
2039 c
= prioq_peek(bus
->reply_callbacks_prioq
);
2041 *timeout_usec
= (uint64_t) -1;
2045 if (c
->timeout
== 0) {
2046 *timeout_usec
= (uint64_t) -1;
2050 *timeout_usec
= c
->timeout
;
2054 static int process_timeout(sd_bus
*bus
) {
2055 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2056 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
* m
= NULL
;
2057 struct reply_callback
*c
;
2064 c
= prioq_peek(bus
->reply_callbacks_prioq
);
2068 n
= now(CLOCK_MONOTONIC
);
2072 r
= bus_message_new_synthetic_error(
2075 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY
, "Method call timed out"),
2080 r
= bus_seal_synthetic_message(bus
, m
);
2084 assert_se(prioq_pop(bus
->reply_callbacks_prioq
) == c
);
2087 ordered_hashmap_remove(bus
->reply_callbacks
, &c
->cookie
);
2090 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
2092 bus
->iteration_counter
++;
2094 bus
->current_message
= m
;
2095 bus
->current_slot
= sd_bus_slot_ref(slot
);
2096 bus
->current_handler
= c
->callback
;
2097 bus
->current_userdata
= slot
->userdata
;
2098 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
2099 bus
->current_userdata
= NULL
;
2100 bus
->current_handler
= NULL
;
2101 bus
->current_slot
= NULL
;
2102 bus
->current_message
= NULL
;
2104 if (slot
->floating
) {
2105 bus_slot_disconnect(slot
);
2106 sd_bus_slot_unref(slot
);
2109 sd_bus_slot_unref(slot
);
2111 return bus_maybe_reply_error(m
, r
, &error_buffer
);
2114 static int process_hello(sd_bus
*bus
, sd_bus_message
*m
) {
2118 if (bus
->state
!= BUS_HELLO
)
2121 /* Let's make sure the first message on the bus is the HELLO
2122 * reply. But note that we don't actually parse the message
2123 * here (we leave that to the usual handling), we just verify
2124 * we don't let any earlier msg through. */
2126 if (!IN_SET(m
->header
->type
, SD_BUS_MESSAGE_METHOD_RETURN
, SD_BUS_MESSAGE_METHOD_ERROR
))
2129 if (m
->reply_cookie
!= 1)
2135 static int process_reply(sd_bus
*bus
, sd_bus_message
*m
) {
2136 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*synthetic_reply
= NULL
;
2137 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2138 struct reply_callback
*c
;
2145 if (!IN_SET(m
->header
->type
, SD_BUS_MESSAGE_METHOD_RETURN
, SD_BUS_MESSAGE_METHOD_ERROR
))
2148 if (m
->destination
&& bus
->unique_name
&& !streq_ptr(m
->destination
, bus
->unique_name
))
2151 c
= ordered_hashmap_remove(bus
->reply_callbacks
, &m
->reply_cookie
);
2157 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
2159 if (m
->n_fds
> 0 && !(bus
->hello_flags
& KDBUS_HELLO_ACCEPT_FD
)) {
2161 /* If the reply contained a file descriptor which we
2162 * didn't want we pass an error instead. */
2164 r
= bus_message_new_synthetic_error(
2167 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Reply message contained file descriptor"),
2172 /* Copy over original timestamp */
2173 synthetic_reply
->realtime
= m
->realtime
;
2174 synthetic_reply
->monotonic
= m
->monotonic
;
2175 synthetic_reply
->seqnum
= m
->seqnum
;
2177 r
= bus_seal_synthetic_message(bus
, synthetic_reply
);
2181 m
= synthetic_reply
;
2183 r
= sd_bus_message_rewind(m
, true);
2188 if (c
->timeout
!= 0) {
2189 prioq_remove(bus
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
2193 bus
->current_slot
= sd_bus_slot_ref(slot
);
2194 bus
->current_handler
= c
->callback
;
2195 bus
->current_userdata
= slot
->userdata
;
2196 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
2197 bus
->current_userdata
= NULL
;
2198 bus
->current_handler
= NULL
;
2199 bus
->current_slot
= NULL
;
2201 if (slot
->floating
) {
2202 bus_slot_disconnect(slot
);
2203 sd_bus_slot_unref(slot
);
2206 sd_bus_slot_unref(slot
);
2208 return bus_maybe_reply_error(m
, r
, &error_buffer
);
2211 static int process_filter(sd_bus
*bus
, sd_bus_message
*m
) {
2212 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2213 struct filter_callback
*l
;
2220 bus
->filter_callbacks_modified
= false;
2222 LIST_FOREACH(callbacks
, l
, bus
->filter_callbacks
) {
2225 if (bus
->filter_callbacks_modified
)
2228 /* Don't run this more than once per iteration */
2229 if (l
->last_iteration
== bus
->iteration_counter
)
2232 l
->last_iteration
= bus
->iteration_counter
;
2234 r
= sd_bus_message_rewind(m
, true);
2238 slot
= container_of(l
, sd_bus_slot
, filter_callback
);
2240 bus
->current_slot
= sd_bus_slot_ref(slot
);
2241 bus
->current_handler
= l
->callback
;
2242 bus
->current_userdata
= slot
->userdata
;
2243 r
= l
->callback(m
, slot
->userdata
, &error_buffer
);
2244 bus
->current_userdata
= NULL
;
2245 bus
->current_handler
= NULL
;
2246 bus
->current_slot
= sd_bus_slot_unref(slot
);
2248 r
= bus_maybe_reply_error(m
, r
, &error_buffer
);
2254 } while (bus
->filter_callbacks_modified
);
2259 static int process_match(sd_bus
*bus
, sd_bus_message
*m
) {
2266 bus
->match_callbacks_modified
= false;
2268 r
= bus_match_run(bus
, &bus
->match_callbacks
, m
);
2272 } while (bus
->match_callbacks_modified
);
2277 static int process_builtin(sd_bus
*bus
, sd_bus_message
*m
) {
2278 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2284 if (bus
->hello_flags
& KDBUS_HELLO_MONITOR
)
2287 if (bus
->manual_peer_interface
)
2290 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_CALL
)
2293 if (!streq_ptr(m
->interface
, "org.freedesktop.DBus.Peer"))
2296 if (m
->header
->flags
& BUS_MESSAGE_NO_REPLY_EXPECTED
)
2299 if (streq_ptr(m
->member
, "Ping"))
2300 r
= sd_bus_message_new_method_return(m
, &reply
);
2301 else if (streq_ptr(m
->member
, "GetMachineId")) {
2305 r
= sd_id128_get_machine(&id
);
2309 r
= sd_bus_message_new_method_return(m
, &reply
);
2313 r
= sd_bus_message_append(reply
, "s", sd_id128_to_string(id
, sid
));
2315 r
= sd_bus_message_new_method_errorf(
2317 SD_BUS_ERROR_UNKNOWN_METHOD
,
2318 "Unknown method '%s' on interface '%s'.", m
->member
, m
->interface
);
2324 r
= sd_bus_send(bus
, reply
, NULL
);
2331 static int process_fd_check(sd_bus
*bus
, sd_bus_message
*m
) {
2335 /* If we got a message with a file descriptor which we didn't
2336 * want to accept, then let's drop it. How can this even
2337 * happen? For example, when the kernel queues a message into
2338 * an activatable names's queue which allows fds, and then is
2339 * delivered to us later even though we ourselves did not
2342 if (bus
->hello_flags
& KDBUS_HELLO_MONITOR
)
2348 if (bus
->hello_flags
& KDBUS_HELLO_ACCEPT_FD
)
2351 if (m
->header
->type
!= SD_BUS_MESSAGE_METHOD_CALL
)
2352 return 1; /* just eat it up */
2354 return sd_bus_reply_method_errorf(m
, SD_BUS_ERROR_INCONSISTENT_MESSAGE
, "Message contains file descriptors, which I cannot accept. Sorry.");
2357 static int process_message(sd_bus
*bus
, sd_bus_message
*m
) {
2363 bus
->current_message
= m
;
2364 bus
->iteration_counter
++;
2366 log_debug_bus_message(m
);
2368 r
= process_hello(bus
, m
);
2372 r
= process_reply(bus
, m
);
2376 r
= process_fd_check(bus
, m
);
2380 r
= process_filter(bus
, m
);
2384 r
= process_match(bus
, m
);
2388 r
= process_builtin(bus
, m
);
2392 r
= bus_process_object(bus
, m
);
2395 bus
->current_message
= NULL
;
2399 static int dispatch_track(sd_bus
*bus
) {
2402 if (!bus
->track_queue
)
2405 bus_track_dispatch(bus
->track_queue
);
2409 static int process_running(sd_bus
*bus
, bool hint_priority
, int64_t priority
, sd_bus_message
**ret
) {
2410 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2414 assert(IN_SET(bus
->state
, BUS_RUNNING
, BUS_HELLO
));
2416 r
= process_timeout(bus
);
2420 r
= dispatch_wqueue(bus
);
2424 r
= dispatch_track(bus
);
2428 r
= dispatch_rqueue(bus
, hint_priority
, priority
, &m
);
2434 r
= process_message(bus
, m
);
2439 r
= sd_bus_message_rewind(m
, true);
2448 if (m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
) {
2450 log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s",
2451 strna(sd_bus_message_get_sender(m
)),
2452 strna(sd_bus_message_get_path(m
)),
2453 strna(sd_bus_message_get_interface(m
)),
2454 strna(sd_bus_message_get_member(m
)));
2456 r
= sd_bus_reply_method_errorf(
2458 SD_BUS_ERROR_UNKNOWN_OBJECT
,
2459 "Unknown object '%s'.", m
->path
);
2473 static int bus_exit_now(sd_bus
*bus
) {
2476 /* Exit due to close, if this is requested. If this is bus object is attached to an event source, invokes
2477 * sd_event_exit(), otherwise invokes libc exit(). */
2479 if (bus
->exited
) /* did we already exit? */
2481 if (!bus
->exit_triggered
) /* was the exit condition triggered? */
2483 if (!bus
->exit_on_disconnect
) /* Shall we actually exit on disconnection? */
2486 bus
->exited
= true; /* never exit more than once */
2488 log_debug("Bus connection disconnected, exiting.");
2491 return sd_event_exit(bus
->event
, EXIT_FAILURE
);
2495 assert_not_reached("exit() didn't exit?");
2498 static int process_closing_reply_callback(sd_bus
*bus
, struct reply_callback
*c
) {
2499 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
2500 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2507 r
= bus_message_new_synthetic_error(
2510 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY
, "Connection terminated"),
2515 r
= bus_seal_synthetic_message(bus
, m
);
2519 if (c
->timeout
!= 0) {
2520 prioq_remove(bus
->reply_callbacks_prioq
, c
, &c
->prioq_idx
);
2524 ordered_hashmap_remove(bus
->reply_callbacks
, &c
->cookie
);
2527 slot
= container_of(c
, sd_bus_slot
, reply_callback
);
2529 bus
->iteration_counter
++;
2531 bus
->current_message
= m
;
2532 bus
->current_slot
= sd_bus_slot_ref(slot
);
2533 bus
->current_handler
= c
->callback
;
2534 bus
->current_userdata
= slot
->userdata
;
2535 r
= c
->callback(m
, slot
->userdata
, &error_buffer
);
2536 bus
->current_userdata
= NULL
;
2537 bus
->current_handler
= NULL
;
2538 bus
->current_slot
= NULL
;
2539 bus
->current_message
= NULL
;
2541 if (slot
->floating
) {
2542 bus_slot_disconnect(slot
);
2543 sd_bus_slot_unref(slot
);
2546 sd_bus_slot_unref(slot
);
2548 return bus_maybe_reply_error(m
, r
, &error_buffer
);
2551 static int process_closing(sd_bus
*bus
, sd_bus_message
**ret
) {
2552 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
2553 struct reply_callback
*c
;
2557 assert(bus
->state
== BUS_CLOSING
);
2559 /* First, fail all outstanding method calls */
2560 c
= ordered_hashmap_first(bus
->reply_callbacks
);
2562 return process_closing_reply_callback(bus
, c
);
2564 /* Then, fake-drop all remaining bus tracking references */
2566 bus_track_close(bus
->tracks
);
2570 /* Then, synthesize a Disconnected message */
2571 r
= sd_bus_message_new_signal(
2574 "/org/freedesktop/DBus/Local",
2575 "org.freedesktop.DBus.Local",
2580 bus_message_set_sender_local(bus
, m
);
2582 r
= bus_seal_synthetic_message(bus
, m
);
2588 bus
->current_message
= m
;
2589 bus
->iteration_counter
++;
2591 r
= process_filter(bus
, m
);
2595 r
= process_match(bus
, m
);
2599 /* Nothing else to do, exit now, if the condition holds */
2600 bus
->exit_triggered
= true;
2601 (void) bus_exit_now(bus
);
2611 bus
->current_message
= NULL
;
2616 static int bus_process_internal(sd_bus
*bus
, bool hint_priority
, int64_t priority
, sd_bus_message
**ret
) {
2617 BUS_DONT_DESTROY(bus
);
2620 /* Returns 0 when we didn't do anything. This should cause the
2621 * caller to invoke sd_bus_wait() before returning the next
2622 * time. Returns > 0 when we did something, which possibly
2623 * means *ret is filled in with an unprocessed message. */
2625 assert_return(bus
, -EINVAL
);
2626 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2628 /* We don't allow recursively invoking sd_bus_process(). */
2629 assert_return(!bus
->current_message
, -EBUSY
);
2630 assert(!bus
->current_slot
);
2632 switch (bus
->state
) {
2641 r
= bus_socket_process_opening(bus
);
2642 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
2643 bus_enter_closing(bus
);
2651 case BUS_AUTHENTICATING
:
2652 r
= bus_socket_process_authenticating(bus
);
2653 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
2654 bus_enter_closing(bus
);
2666 r
= process_running(bus
, hint_priority
, priority
, ret
);
2667 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
2668 bus_enter_closing(bus
);
2678 return process_closing(bus
, ret
);
2681 assert_not_reached("Unknown state");
2684 _public_
int sd_bus_process(sd_bus
*bus
, sd_bus_message
**ret
) {
2685 return bus_process_internal(bus
, false, 0, ret
);
2688 _public_
int sd_bus_process_priority(sd_bus
*bus
, int64_t priority
, sd_bus_message
**ret
) {
2689 return bus_process_internal(bus
, true, priority
, ret
);
2692 static int bus_poll(sd_bus
*bus
, bool need_more
, uint64_t timeout_usec
) {
2693 struct pollfd p
[2] = {};
2696 usec_t m
= USEC_INFINITY
;
2700 if (bus
->state
== BUS_CLOSING
)
2703 if (!BUS_IS_OPEN(bus
->state
))
2706 e
= sd_bus_get_events(bus
);
2711 /* The caller really needs some more data, he doesn't
2712 * care about what's already read, or any timeouts
2713 * except its own. */
2717 /* The caller wants to process if there's something to
2718 * process, but doesn't care otherwise */
2720 r
= sd_bus_get_timeout(bus
, &until
);
2725 nw
= now(CLOCK_MONOTONIC
);
2726 m
= until
> nw
? until
- nw
: 0;
2730 if (timeout_usec
!= (uint64_t) -1 && (m
== (uint64_t) -1 || timeout_usec
< m
))
2733 p
[0].fd
= bus
->input_fd
;
2734 if (bus
->output_fd
== bus
->input_fd
) {
2738 p
[0].events
= e
& POLLIN
;
2739 p
[1].fd
= bus
->output_fd
;
2740 p
[1].events
= e
& POLLOUT
;
2744 r
= ppoll(p
, n
, m
== (uint64_t) -1 ? NULL
: timespec_store(&ts
, m
), NULL
);
2748 return r
> 0 ? 1 : 0;
2751 _public_
int sd_bus_wait(sd_bus
*bus
, uint64_t timeout_usec
) {
2753 assert_return(bus
, -EINVAL
);
2754 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2756 if (bus
->state
== BUS_CLOSING
)
2759 if (!BUS_IS_OPEN(bus
->state
))
2762 if (bus
->rqueue_size
> 0)
2765 return bus_poll(bus
, false, timeout_usec
);
2768 _public_
int sd_bus_flush(sd_bus
*bus
) {
2771 assert_return(bus
, -EINVAL
);
2772 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2774 if (bus
->state
== BUS_CLOSING
)
2777 if (!BUS_IS_OPEN(bus
->state
))
2780 r
= bus_ensure_running(bus
);
2784 if (bus
->wqueue_size
<= 0)
2788 r
= dispatch_wqueue(bus
);
2790 if (IN_SET(r
, -ENOTCONN
, -ECONNRESET
, -EPIPE
, -ESHUTDOWN
)) {
2791 bus_enter_closing(bus
);
2798 if (bus
->wqueue_size
<= 0)
2801 r
= bus_poll(bus
, false, (uint64_t) -1);
2807 _public_
int sd_bus_add_filter(
2810 sd_bus_message_handler_t callback
,
2815 assert_return(bus
, -EINVAL
);
2816 assert_return(callback
, -EINVAL
);
2817 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2819 s
= bus_slot_allocate(bus
, !slot
, BUS_FILTER_CALLBACK
, sizeof(struct filter_callback
), userdata
);
2823 s
->filter_callback
.callback
= callback
;
2825 bus
->filter_callbacks_modified
= true;
2826 LIST_PREPEND(callbacks
, bus
->filter_callbacks
, &s
->filter_callback
);
2834 _public_
int sd_bus_add_match(
2838 sd_bus_message_handler_t callback
,
2841 struct bus_match_component
*components
= NULL
;
2842 unsigned n_components
= 0;
2843 sd_bus_slot
*s
= NULL
;
2846 assert_return(bus
, -EINVAL
);
2847 assert_return(match
, -EINVAL
);
2848 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2850 r
= bus_match_parse(match
, &components
, &n_components
);
2854 s
= bus_slot_allocate(bus
, !slot
, BUS_MATCH_CALLBACK
, sizeof(struct match_callback
), userdata
);
2860 s
->match_callback
.callback
= callback
;
2862 if (bus
->bus_client
) {
2863 enum bus_match_scope scope
;
2865 scope
= bus_match_get_scope(components
, n_components
);
2867 /* Do not install server-side matches for matches
2868 * against the local service, interface or bus path. */
2869 if (scope
!= BUS_MATCH_LOCAL
) {
2871 /* We store the original match string, so that
2872 * we can use it to remove the match again. */
2874 s
->match_callback
.match_string
= strdup(match
);
2875 if (!s
->match_callback
.match_string
) {
2880 r
= bus_add_match_internal(bus
, s
->match_callback
.match_string
, components
, n_components
);
2884 s
->match_added
= true;
2888 bus
->match_callbacks_modified
= true;
2889 r
= bus_match_add(&bus
->match_callbacks
, components
, n_components
, &s
->match_callback
);
2898 bus_match_parse_free(components
, n_components
);
2899 sd_bus_slot_unref(s
);
2904 int bus_remove_match_by_string(
2907 sd_bus_message_handler_t callback
,
2910 struct bus_match_component
*components
= NULL
;
2911 unsigned n_components
= 0;
2912 struct match_callback
*c
;
2915 assert_return(bus
, -EINVAL
);
2916 assert_return(match
, -EINVAL
);
2917 assert_return(!bus_pid_changed(bus
), -ECHILD
);
2919 r
= bus_match_parse(match
, &components
, &n_components
);
2923 r
= bus_match_find(&bus
->match_callbacks
, components
, n_components
, NULL
, NULL
, &c
);
2927 sd_bus_slot_unref(container_of(c
, sd_bus_slot
, match_callback
));
2930 bus_match_parse_free(components
, n_components
);
2935 bool bus_pid_changed(sd_bus
*bus
) {
2938 /* We don't support people creating a bus connection and
2939 * keeping it around over a fork(). Let's complain. */
2941 return bus
->original_pid
!= getpid_cached();
2944 static int io_callback(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
2945 sd_bus
*bus
= userdata
;
2950 r
= sd_bus_process(bus
, NULL
);
2957 static int time_callback(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
2958 sd_bus
*bus
= userdata
;
2963 r
= sd_bus_process(bus
, NULL
);
2970 static int prepare_callback(sd_event_source
*s
, void *userdata
) {
2971 sd_bus
*bus
= userdata
;
2978 e
= sd_bus_get_events(bus
);
2982 if (bus
->output_fd
!= bus
->input_fd
) {
2984 r
= sd_event_source_set_io_events(bus
->input_io_event_source
, e
& POLLIN
);
2988 r
= sd_event_source_set_io_events(bus
->output_io_event_source
, e
& POLLOUT
);
2992 r
= sd_event_source_set_io_events(bus
->input_io_event_source
, e
);
2997 r
= sd_bus_get_timeout(bus
, &until
);
3003 j
= sd_event_source_set_time(bus
->time_event_source
, until
);
3008 r
= sd_event_source_set_enabled(bus
->time_event_source
, r
> 0);
3015 static int quit_callback(sd_event_source
*event
, void *userdata
) {
3016 sd_bus
*bus
= userdata
;
3026 static int attach_io_events(sd_bus
*bus
) {
3031 if (bus
->input_fd
< 0)
3037 if (!bus
->input_io_event_source
) {
3038 r
= sd_event_add_io(bus
->event
, &bus
->input_io_event_source
, bus
->input_fd
, 0, io_callback
, bus
);
3042 r
= sd_event_source_set_prepare(bus
->input_io_event_source
, prepare_callback
);
3046 r
= sd_event_source_set_priority(bus
->input_io_event_source
, bus
->event_priority
);
3050 r
= sd_event_source_set_description(bus
->input_io_event_source
, "bus-input");
3052 r
= sd_event_source_set_io_fd(bus
->input_io_event_source
, bus
->input_fd
);
3057 if (bus
->output_fd
!= bus
->input_fd
) {
3058 assert(bus
->output_fd
>= 0);
3060 if (!bus
->output_io_event_source
) {
3061 r
= sd_event_add_io(bus
->event
, &bus
->output_io_event_source
, bus
->output_fd
, 0, io_callback
, bus
);
3065 r
= sd_event_source_set_priority(bus
->output_io_event_source
, bus
->event_priority
);
3069 r
= sd_event_source_set_description(bus
->input_io_event_source
, "bus-output");
3071 r
= sd_event_source_set_io_fd(bus
->output_io_event_source
, bus
->output_fd
);
3080 static void detach_io_events(sd_bus
*bus
) {
3083 if (bus
->input_io_event_source
) {
3084 sd_event_source_set_enabled(bus
->input_io_event_source
, SD_EVENT_OFF
);
3085 bus
->input_io_event_source
= sd_event_source_unref(bus
->input_io_event_source
);
3088 if (bus
->output_io_event_source
) {
3089 sd_event_source_set_enabled(bus
->output_io_event_source
, SD_EVENT_OFF
);
3090 bus
->output_io_event_source
= sd_event_source_unref(bus
->output_io_event_source
);
3094 _public_
int sd_bus_attach_event(sd_bus
*bus
, sd_event
*event
, int priority
) {
3097 assert_return(bus
, -EINVAL
);
3098 assert_return(!bus
->event
, -EBUSY
);
3100 assert(!bus
->input_io_event_source
);
3101 assert(!bus
->output_io_event_source
);
3102 assert(!bus
->time_event_source
);
3105 bus
->event
= sd_event_ref(event
);
3107 r
= sd_event_default(&bus
->event
);
3112 bus
->event_priority
= priority
;
3114 r
= sd_event_add_time(bus
->event
, &bus
->time_event_source
, CLOCK_MONOTONIC
, 0, 0, time_callback
, bus
);
3118 r
= sd_event_source_set_priority(bus
->time_event_source
, priority
);
3122 r
= sd_event_source_set_description(bus
->time_event_source
, "bus-time");
3126 r
= sd_event_add_exit(bus
->event
, &bus
->quit_event_source
, quit_callback
, bus
);
3130 r
= sd_event_source_set_description(bus
->quit_event_source
, "bus-exit");
3134 r
= attach_io_events(bus
);
3141 sd_bus_detach_event(bus
);
3145 _public_
int sd_bus_detach_event(sd_bus
*bus
) {
3146 assert_return(bus
, -EINVAL
);
3151 detach_io_events(bus
);
3153 if (bus
->time_event_source
) {
3154 sd_event_source_set_enabled(bus
->time_event_source
, SD_EVENT_OFF
);
3155 bus
->time_event_source
= sd_event_source_unref(bus
->time_event_source
);
3158 if (bus
->quit_event_source
) {
3159 sd_event_source_set_enabled(bus
->quit_event_source
, SD_EVENT_OFF
);
3160 bus
->quit_event_source
= sd_event_source_unref(bus
->quit_event_source
);
3163 bus
->event
= sd_event_unref(bus
->event
);
3167 _public_ sd_event
* sd_bus_get_event(sd_bus
*bus
) {
3168 assert_return(bus
, NULL
);
3173 _public_ sd_bus_message
* sd_bus_get_current_message(sd_bus
*bus
) {
3174 assert_return(bus
, NULL
);
3176 return bus
->current_message
;
3179 _public_ sd_bus_slot
* sd_bus_get_current_slot(sd_bus
*bus
) {
3180 assert_return(bus
, NULL
);
3182 return bus
->current_slot
;
3185 _public_ sd_bus_message_handler_t
sd_bus_get_current_handler(sd_bus
*bus
) {
3186 assert_return(bus
, NULL
);
3188 return bus
->current_handler
;
3191 _public_
void* sd_bus_get_current_userdata(sd_bus
*bus
) {
3192 assert_return(bus
, NULL
);
3194 return bus
->current_userdata
;
3197 static int bus_default(int (*bus_open
)(sd_bus
**), sd_bus
**default_bus
, sd_bus
**ret
) {
3202 assert(default_bus
);
3205 return !!*default_bus
;
3208 *ret
= sd_bus_ref(*default_bus
);
3216 b
->default_bus_ptr
= default_bus
;
3224 _public_
int sd_bus_default_system(sd_bus
**ret
) {
3225 return bus_default(sd_bus_open_system
, &default_system_bus
, ret
);
3229 _public_
int sd_bus_default_user(sd_bus
**ret
) {
3230 return bus_default(sd_bus_open_user
, &default_user_bus
, ret
);
3233 _public_
int sd_bus_default(sd_bus
**ret
) {
3237 /* Let's try our best to reuse another cached connection. If
3238 * the starter bus type is set, connect via our normal
3239 * connection logic, ignoring $DBUS_STARTER_ADDRESS, so that
3240 * we can share the connection with the user/system default
3243 e
= secure_getenv("DBUS_STARTER_BUS_TYPE");
3245 if (streq(e
, "system"))
3246 return sd_bus_default_system(ret
);
3247 else if (STR_IN_SET(e
, "user", "session"))
3248 return sd_bus_default_user(ret
);
3251 /* No type is specified, so we have not other option than to
3252 * use the starter address if it is set. */
3254 e
= secure_getenv("DBUS_STARTER_ADDRESS");
3257 return bus_default(sd_bus_open
, &default_starter_bus
, ret
);
3260 /* Finally, if nothing is set use the cached connection for
3261 * the right scope */
3263 if (cg_pid_get_owner_uid(0, NULL
) >= 0)
3264 return sd_bus_default_user(ret
);
3266 return sd_bus_default_system(ret
);
3269 _public_
int sd_bus_get_tid(sd_bus
*b
, pid_t
*tid
) {
3270 assert_return(b
, -EINVAL
);
3271 assert_return(tid
, -EINVAL
);
3272 assert_return(!bus_pid_changed(b
), -ECHILD
);
3280 return sd_event_get_tid(b
->event
, tid
);
3285 _public_
int sd_bus_path_encode(const char *prefix
, const char *external_id
, char **ret_path
) {
3286 _cleanup_free_
char *e
= NULL
;
3289 assert_return(object_path_is_valid(prefix
), -EINVAL
);
3290 assert_return(external_id
, -EINVAL
);
3291 assert_return(ret_path
, -EINVAL
);
3293 e
= bus_label_escape(external_id
);
3297 ret
= strjoin(prefix
, "/", e
);
3305 _public_
int sd_bus_path_decode(const char *path
, const char *prefix
, char **external_id
) {
3309 assert_return(object_path_is_valid(path
), -EINVAL
);
3310 assert_return(object_path_is_valid(prefix
), -EINVAL
);
3311 assert_return(external_id
, -EINVAL
);
3313 e
= object_path_startswith(path
, prefix
);
3315 *external_id
= NULL
;
3319 ret
= bus_label_unescape(e
);
3327 _public_
int sd_bus_path_encode_many(char **out
, const char *path_template
, ...) {
3328 _cleanup_strv_free_
char **labels
= NULL
;
3329 char *path
, *path_pos
, **label_pos
;
3330 const char *sep
, *template_pos
;
3335 assert_return(out
, -EINVAL
);
3336 assert_return(path_template
, -EINVAL
);
3338 path_length
= strlen(path_template
);
3340 va_start(list
, path_template
);
3341 for (sep
= strchr(path_template
, '%'); sep
; sep
= strchr(sep
+ 1, '%')) {
3345 arg
= va_arg(list
, const char *);
3351 label
= bus_label_escape(arg
);
3357 r
= strv_consume(&labels
, label
);
3363 /* add label length, but account for the format character */
3364 path_length
+= strlen(label
) - 1;
3368 path
= malloc(path_length
+ 1);
3375 for (template_pos
= path_template
; *template_pos
; ) {
3376 sep
= strchrnul(template_pos
, '%');
3377 path_pos
= mempcpy(path_pos
, template_pos
, sep
- template_pos
);
3381 path_pos
= stpcpy(path_pos
, *label_pos
++);
3382 template_pos
= sep
+ 1;
3390 _public_
int sd_bus_path_decode_many(const char *path
, const char *path_template
, ...) {
3391 _cleanup_strv_free_
char **labels
= NULL
;
3392 const char *template_pos
, *path_pos
;
3398 * This decodes an object-path based on a template argument. The
3399 * template consists of a verbatim path, optionally including special
3402 * - Each occurrence of '%' in the template matches an arbitrary
3403 * substring of a label in the given path. At most one such
3404 * directive is allowed per label. For each such directive, the
3405 * caller must provide an output parameter (char **) via va_arg. If
3406 * NULL is passed, the given label is verified, but not returned.
3407 * For each matched label, the *decoded* label is stored in the
3408 * passed output argument, and the caller is responsible to free
3409 * it. Note that the output arguments are only modified if the
3410 * actualy path matched the template. Otherwise, they're left
3413 * This function returns <0 on error, 0 if the path does not match the
3414 * template, 1 if it matched.
3417 assert_return(path
, -EINVAL
);
3418 assert_return(path_template
, -EINVAL
);
3422 for (template_pos
= path_template
; *template_pos
; ) {
3427 /* verify everything until the next '%' matches verbatim */
3428 sep
= strchrnul(template_pos
, '%');
3429 length
= sep
- template_pos
;
3430 if (strncmp(path_pos
, template_pos
, length
))
3434 template_pos
+= length
;
3439 /* We found the next '%' character. Everything up until here
3440 * matched. We now skip ahead to the end of this label and make
3441 * sure it matches the tail of the label in the path. Then we
3442 * decode the string in-between and save it for later use. */
3444 ++template_pos
; /* skip over '%' */
3446 sep
= strchrnul(template_pos
, '/');
3447 length
= sep
- template_pos
; /* length of suffix to match verbatim */
3449 /* verify the suffixes match */
3450 sep
= strchrnul(path_pos
, '/');
3451 if (sep
- path_pos
< (ssize_t
)length
||
3452 strncmp(sep
- length
, template_pos
, length
))
3455 template_pos
+= length
; /* skip over matched label */
3456 length
= sep
- path_pos
- length
; /* length of sub-label to decode */
3458 /* store unescaped label for later use */
3459 label
= bus_label_unescape_n(path_pos
, length
);
3463 r
= strv_consume(&labels
, label
);
3467 path_pos
= sep
; /* skip decoded label and suffix */
3470 /* end of template must match end of path */
3474 /* copy the labels over to the caller */
3475 va_start(list
, path_template
);
3476 for (label_pos
= labels
; label_pos
&& *label_pos
; ++label_pos
) {
3479 arg
= va_arg(list
, char **);
3492 _public_
int sd_bus_try_close(sd_bus
*bus
) {
3493 assert_return(bus
, -EINVAL
);
3494 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3499 _public_
int sd_bus_get_description(sd_bus
*bus
, const char **description
) {
3500 assert_return(bus
, -EINVAL
);
3501 assert_return(description
, -EINVAL
);
3502 assert_return(bus
->description
, -ENXIO
);
3503 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3505 *description
= bus
->description
;
3509 int bus_get_root_path(sd_bus
*bus
) {
3512 if (bus
->cgroup_root
)
3515 r
= cg_get_root_path(&bus
->cgroup_root
);
3517 bus
->cgroup_root
= strdup("/");
3518 if (!bus
->cgroup_root
)
3527 _public_
int sd_bus_get_scope(sd_bus
*bus
, const char **scope
) {
3528 assert_return(bus
, -EINVAL
);
3529 assert_return(scope
, -EINVAL
);
3530 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3537 if (bus
->is_system
) {
3545 _public_
int sd_bus_get_address(sd_bus
*bus
, const char **address
) {
3547 assert_return(bus
, -EINVAL
);
3548 assert_return(address
, -EINVAL
);
3549 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3552 *address
= bus
->address
;
3559 _public_
int sd_bus_get_creds_mask(sd_bus
*bus
, uint64_t *mask
) {
3560 assert_return(bus
, -EINVAL
);
3561 assert_return(mask
, -EINVAL
);
3562 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3564 *mask
= bus
->creds_mask
;
3568 _public_
int sd_bus_is_bus_client(sd_bus
*bus
) {
3569 assert_return(bus
, -EINVAL
);
3570 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3572 return bus
->bus_client
;
3575 _public_
int sd_bus_is_server(sd_bus
*bus
) {
3576 assert_return(bus
, -EINVAL
);
3577 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3579 return bus
->is_server
;
3582 _public_
int sd_bus_is_anonymous(sd_bus
*bus
) {
3583 assert_return(bus
, -EINVAL
);
3584 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3586 return bus
->anonymous_auth
;
3589 _public_
int sd_bus_is_trusted(sd_bus
*bus
) {
3590 assert_return(bus
, -EINVAL
);
3591 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3593 return bus
->trusted
;
3596 _public_
int sd_bus_is_monitor(sd_bus
*bus
) {
3597 assert_return(bus
, -EINVAL
);
3598 assert_return(!bus_pid_changed(bus
), -ECHILD
);
3600 return !!(bus
->hello_flags
& KDBUS_HELLO_MONITOR
);
3603 static void flush_close(sd_bus
*bus
) {
3607 /* Flushes and closes the specified bus. We take a ref before,
3608 * to ensure the flushing does not cause the bus to be
3611 sd_bus_flush_close_unref(sd_bus_ref(bus
));
3614 _public_
void sd_bus_default_flush_close(void) {
3615 flush_close(default_starter_bus
);
3616 flush_close(default_user_bus
);
3617 flush_close(default_system_bus
);
3620 _public_
int sd_bus_set_exit_on_disconnect(sd_bus
*bus
, int b
) {
3621 assert_return(bus
, -EINVAL
);
3623 /* Turns on exit-on-disconnect, and triggers it immediately if the bus connection was already
3624 * disconnected. Note that this is triggered exclusively on disconnections triggered by the server side, never
3625 * from the client side. */
3626 bus
->exit_on_disconnect
= b
;
3628 /* If the exit condition was triggered already, exit immediately. */
3629 return bus_exit_now(bus
);
3632 _public_
int sd_bus_get_exit_on_disconnect(sd_bus
*bus
) {
3633 assert_return(bus
, -EINVAL
);
3635 return bus
->exit_on_disconnect
;