1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 #include "alloc-util.h"
32 #include "bus-container.h"
33 #include "bus-control.h"
34 #include "bus-internal.h"
35 #include "bus-kernel.h"
36 #include "bus-label.h"
37 #include "bus-message.h"
38 #include "bus-objects.h"
39 #include "bus-protocol.h"
41 #include "bus-socket.h"
42 #include "bus-track.h"
45 #include "cgroup-util.h"
48 #include "hexdecoct.h"
49 #include "hostname-util.h"
52 #include "parse-util.h"
53 #include "string-util.h"
57 /// Additional includes needed by elogind
58 #include "process-util.h"
60 #define log_debug_bus_message(m) \
62 sd_bus_message *_mm = (m); \
63 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", \
64 bus_message_type_to_string(_mm->header->type), \
65 strna(sd_bus_message_get_sender(_mm)), \
66 strna(sd_bus_message_get_destination(_mm)), \
67 strna(sd_bus_message_get_path(_mm)), \
68 strna(sd_bus_message_get_interface(_mm)), \
69 strna(sd_bus_message_get_member(_mm)), \
70 BUS_MESSAGE_COOKIE(_mm), \
72 strna(_mm->error.name), \
73 strna(_mm->error.message)); \
76 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
77 static int attach_io_events(sd_bus *b);
78 static void detach_io_events(sd_bus *b);
80 static thread_local sd_bus *default_system_bus = NULL;
81 #if 0 /// UNNEEDED by elogind
82 static thread_local sd_bus *default_user_bus = NULL;
84 static thread_local sd_bus *default_starter_bus = NULL;
86 static void bus_close_fds(sd_bus *b) {
91 if (b->input_fd != b->output_fd)
92 safe_close(b->output_fd);
93 b->output_fd = b->input_fd = safe_close(b->input_fd);
96 static void bus_reset_queues(sd_bus *b) {
99 while (b->rqueue_size > 0)
100 sd_bus_message_unref(b->rqueue[--b->rqueue_size]);
102 b->rqueue = mfree(b->rqueue);
103 b->rqueue_allocated = 0;
105 while (b->wqueue_size > 0)
106 sd_bus_message_unref(b->wqueue[--b->wqueue_size]);
108 b->wqueue = mfree(b->wqueue);
109 b->wqueue_allocated = 0;
112 static void bus_free(sd_bus *b) {
116 assert(!b->track_queue);
119 b->state = BUS_CLOSED;
121 sd_bus_detach_event(b);
123 while ((s = b->slots)) {
124 /* At this point only floating slots can still be
125 * around, because the non-floating ones keep a
126 * reference to the bus, and we thus couldn't be
127 * destructing right now... We forcibly disconnect the
128 * slots here, so that they still can be referenced by
129 * apps, but are dead. */
132 bus_slot_disconnect(s);
133 sd_bus_slot_unref(s);
136 if (b->default_bus_ptr)
137 *b->default_bus_ptr = NULL;
144 free(b->unique_name);
145 free(b->auth_buffer);
148 free(b->cgroup_root);
149 free(b->description);
152 strv_free(b->exec_argv);
154 close_many(b->fds, b->n_fds);
159 ordered_hashmap_free_free(b->reply_callbacks);
160 prioq_free(b->reply_callbacks_prioq);
162 assert(b->match_callbacks.type == BUS_MATCH_ROOT);
163 bus_match_free(&b->match_callbacks);
165 hashmap_free_free(b->vtable_methods);
166 hashmap_free_free(b->vtable_properties);
168 assert(hashmap_isempty(b->nodes));
169 hashmap_free(b->nodes);
173 assert_se(pthread_mutex_destroy(&b->memfd_cache_mutex) == 0);
178 _public_ int sd_bus_new(sd_bus **ret) {
181 assert_return(ret, -EINVAL);
187 r->n_ref = REFCNT_INIT;
188 r->input_fd = r->output_fd = -1;
189 r->message_version = 1;
190 r->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
191 r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
192 r->attach_flags |= KDBUS_ATTACH_NAMES;
193 r->original_pid = getpid_cached();
194 r->n_groups = (size_t) -1;
196 assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0);
198 /* We guarantee that wqueue always has space for at least one
200 if (!GREEDY_REALLOC(r->wqueue, r->wqueue_allocated, 1)) {
209 _public_ int sd_bus_set_address(sd_bus *bus, const char *address) {
212 assert_return(bus, -EINVAL);
213 assert_return(bus->state == BUS_UNSET, -EPERM);
214 assert_return(address, -EINVAL);
215 assert_return(!bus_pid_changed(bus), -ECHILD);
227 _public_ int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
228 assert_return(bus, -EINVAL);
229 assert_return(bus->state == BUS_UNSET, -EPERM);
230 assert_return(input_fd >= 0, -EBADF);
231 assert_return(output_fd >= 0, -EBADF);
232 assert_return(!bus_pid_changed(bus), -ECHILD);
234 bus->input_fd = input_fd;
235 bus->output_fd = output_fd;
239 _public_ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
242 assert_return(bus, -EINVAL);
243 assert_return(bus->state == BUS_UNSET, -EPERM);
244 assert_return(path, -EINVAL);
245 assert_return(!strv_isempty(argv), -EINVAL);
246 assert_return(!bus_pid_changed(bus), -ECHILD);
258 free(bus->exec_path);
259 strv_free(bus->exec_argv);
267 _public_ int sd_bus_set_bus_client(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 bus->bus_client = !!b;
276 _public_ int sd_bus_set_monitor(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->hello_flags, KDBUS_HELLO_MONITOR, b);
285 _public_ int sd_bus_negotiate_fds(sd_bus *bus, int b) {
286 assert_return(bus, -EINVAL);
287 assert_return(bus->state == BUS_UNSET, -EPERM);
288 assert_return(!bus_pid_changed(bus), -ECHILD);
290 SET_FLAG(bus->hello_flags, KDBUS_HELLO_ACCEPT_FD, b);
294 _public_ int sd_bus_negotiate_timestamp(sd_bus *bus, int b) {
296 assert_return(bus, -EINVAL);
297 assert_return(!IN_SET(bus->state, BUS_CLOSING, BUS_CLOSED), -EPERM);
298 assert_return(!bus_pid_changed(bus), -ECHILD);
300 new_flags = bus->attach_flags;
301 SET_FLAG(new_flags, KDBUS_ATTACH_TIMESTAMP, b);
303 if (bus->attach_flags == new_flags)
306 bus->attach_flags = new_flags;
311 _public_ int sd_bus_negotiate_creds(sd_bus *bus, int b, uint64_t mask) {
314 assert_return(bus, -EINVAL);
315 assert_return(mask <= _SD_BUS_CREDS_ALL, -EINVAL);
316 assert_return(!IN_SET(bus->state, BUS_CLOSING, BUS_CLOSED), -EPERM);
317 assert_return(!bus_pid_changed(bus), -ECHILD);
319 SET_FLAG(bus->creds_mask, mask, b);
321 /* The well knowns we need unconditionally, so that matches can work */
322 bus->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
324 /* Make sure we don't lose the timestamp flag */
325 new_flags = (bus->attach_flags & KDBUS_ATTACH_TIMESTAMP) | attach_flags_to_kdbus(bus->creds_mask);
326 if (bus->attach_flags == new_flags)
329 bus->attach_flags = new_flags;
334 _public_ int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
335 assert_return(bus, -EINVAL);
336 assert_return(b || sd_id128_equal(server_id, SD_ID128_NULL), -EINVAL);
337 assert_return(bus->state == BUS_UNSET, -EPERM);
338 assert_return(!bus_pid_changed(bus), -ECHILD);
340 bus->is_server = !!b;
341 bus->server_id = server_id;
345 _public_ int sd_bus_set_anonymous(sd_bus *bus, int b) {
346 assert_return(bus, -EINVAL);
347 assert_return(bus->state == BUS_UNSET, -EPERM);
348 assert_return(!bus_pid_changed(bus), -ECHILD);
350 bus->anonymous_auth = !!b;
354 _public_ int sd_bus_set_trusted(sd_bus *bus, int b) {
355 assert_return(bus, -EINVAL);
356 assert_return(bus->state == BUS_UNSET, -EPERM);
357 assert_return(!bus_pid_changed(bus), -ECHILD);
363 _public_ int sd_bus_set_description(sd_bus *bus, const char *description) {
364 assert_return(bus, -EINVAL);
365 assert_return(bus->state == BUS_UNSET, -EPERM);
366 assert_return(!bus_pid_changed(bus), -ECHILD);
368 return free_and_strdup(&bus->description, description);
371 _public_ int sd_bus_set_allow_interactive_authorization(sd_bus *bus, int b) {
372 assert_return(bus, -EINVAL);
373 assert_return(!bus_pid_changed(bus), -ECHILD);
375 bus->allow_interactive_authorization = !!b;
379 _public_ int sd_bus_get_allow_interactive_authorization(sd_bus *bus) {
380 assert_return(bus, -EINVAL);
381 assert_return(!bus_pid_changed(bus), -ECHILD);
383 return bus->allow_interactive_authorization;
386 static int hello_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
394 assert(IN_SET(bus->state, BUS_HELLO, BUS_CLOSING));
396 r = sd_bus_message_get_errno(reply);
400 r = sd_bus_message_read(reply, "s", &s);
404 if (!service_name_is_valid(s) || s[0] != ':')
407 bus->unique_name = strdup(s);
408 if (!bus->unique_name)
411 if (bus->state == BUS_HELLO)
412 bus->state = BUS_RUNNING;
417 static int bus_send_hello(sd_bus *bus) {
418 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
423 if (!bus->bus_client)
426 r = sd_bus_message_new_method_call(
429 "org.freedesktop.DBus",
430 "/org/freedesktop/DBus",
431 "org.freedesktop.DBus",
436 return sd_bus_call_async(bus, NULL, m, hello_callback, NULL, 0);
439 int bus_start_running(sd_bus *bus) {
442 if (bus->bus_client) {
443 bus->state = BUS_HELLO;
447 bus->state = BUS_RUNNING;
451 static int parse_address_key(const char **p, const char *key, char **value) {
452 size_t l, n = 0, allocated = 0;
462 if (strncmp(*p, key, l) != 0)
475 while (!IN_SET(*a, ';', ',', 0)) {
493 c = (char) ((x << 4) | y);
500 if (!GREEDY_REALLOC(r, allocated, n + 2))
524 static void skip_address_key(const char **p) {
528 *p += strcspn(*p, ",");
534 static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
535 _cleanup_free_ char *path = NULL, *abstract = NULL;
544 while (!IN_SET(**p, 0, ';')) {
545 r = parse_address_key(p, "guid", guid);
551 r = parse_address_key(p, "path", &path);
557 r = parse_address_key(p, "abstract", &abstract);
566 if (!path && !abstract)
569 if (path && abstract)
574 if (l > sizeof(b->sockaddr.un.sun_path))
577 b->sockaddr.un.sun_family = AF_UNIX;
578 strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path));
579 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l;
580 } else if (abstract) {
581 l = strlen(abstract);
582 if (l > sizeof(b->sockaddr.un.sun_path) - 1)
585 b->sockaddr.un.sun_family = AF_UNIX;
586 b->sockaddr.un.sun_path[0] = 0;
587 strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1);
588 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
596 static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
597 _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
599 struct addrinfo *result, hints = {
600 .ai_socktype = SOCK_STREAM,
601 .ai_flags = AI_ADDRCONFIG,
609 while (!IN_SET(**p, 0, ';')) {
610 r = parse_address_key(p, "guid", guid);
616 r = parse_address_key(p, "host", &host);
622 r = parse_address_key(p, "port", &port);
628 r = parse_address_key(p, "family", &family);
641 if (streq(family, "ipv4"))
642 hints.ai_family = AF_INET;
643 else if (streq(family, "ipv6"))
644 hints.ai_family = AF_INET6;
649 r = getaddrinfo(host, port, &hints, &result);
653 return -EADDRNOTAVAIL;
655 memcpy(&b->sockaddr, result->ai_addr, result->ai_addrlen);
656 b->sockaddr_size = result->ai_addrlen;
658 freeaddrinfo(result);
665 static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
667 unsigned n_argv = 0, j;
669 size_t allocated = 0;
677 while (!IN_SET(**p, 0, ';')) {
678 r = parse_address_key(p, "guid", guid);
684 r = parse_address_key(p, "path", &path);
690 if (startswith(*p, "argv")) {
694 ul = strtoul(*p + 4, (char**) p, 10);
695 if (errno > 0 || **p != '=' || ul > 256) {
703 if (!GREEDY_REALLOC0(argv, allocated, ul + 2)) {
711 r = parse_address_key(p, NULL, argv + ul);
726 /* Make sure there are no holes in the array, with the
727 * exception of argv[0] */
728 for (j = 1; j < n_argv; j++)
734 if (argv && argv[0] == NULL) {
735 argv[0] = strdup(path);
750 for (j = 0; j < n_argv; j++)
758 static int parse_container_unix_address(sd_bus *b, const char **p, char **guid) {
759 _cleanup_free_ char *machine = NULL, *pid = NULL;
767 while (!IN_SET(**p, 0, ';')) {
768 r = parse_address_key(p, "guid", guid);
774 r = parse_address_key(p, "machine", &machine);
780 r = parse_address_key(p, "pid", &pid);
789 if (!machine == !pid)
793 if (!machine_name_is_valid(machine))
796 free_and_replace(b->machine, machine);
798 b->machine = mfree(b->machine);
802 r = parse_pid(pid, &b->nspid);
808 b->sockaddr.un.sun_family = AF_UNIX;
809 strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
810 b->sockaddr_size = SOCKADDR_UN_LEN(b->sockaddr.un);
816 static void bus_reset_parsed_address(sd_bus *b) {
820 b->sockaddr_size = 0;
821 b->exec_argv = strv_free(b->exec_argv);
822 b->exec_path = mfree(b->exec_path);
823 b->server_id = SD_ID128_NULL;
824 b->machine = mfree(b->machine);
828 static int bus_parse_next_address(sd_bus *b) {
829 _cleanup_free_ char *guid = NULL;
837 if (b->address[b->address_index] == 0)
840 bus_reset_parsed_address(b);
842 a = b->address + b->address_index;
851 if (startswith(a, "unix:")) {
854 r = parse_unix_address(b, &a, &guid);
859 } else if (startswith(a, "tcp:")) {
862 r = parse_tcp_address(b, &a, &guid);
868 } else if (startswith(a, "unixexec:")) {
871 r = parse_exec_address(b, &a, &guid);
877 } else if (startswith(a, "x-machine-unix:")) {
880 r = parse_container_unix_address(b, &a, &guid);
893 r = sd_id128_from_string(guid, &b->server_id);
898 b->address_index = a - b->address;
902 static int bus_start_address(sd_bus *b) {
910 /* If you provide multiple different bus-addresses, we
911 * try all of them in order and use the first one that
915 r = bus_socket_exec(b);
917 else if ((b->nspid > 0 || b->machine) && b->sockaddr.sa.sa_family != AF_UNSPEC)
918 r = bus_container_connect_socket(b);
920 else if (b->sockaddr.sa.sa_family != AF_UNSPEC)
921 r = bus_socket_connect(b);
927 r = attach_io_events(b);
932 b->last_connect_error = -r;
935 r = bus_parse_next_address(b);
939 return b->last_connect_error > 0 ? -b->last_connect_error : -ECONNREFUSED;
943 int bus_next_address(sd_bus *b) {
946 bus_reset_parsed_address(b);
947 return bus_start_address(b);
950 static int bus_start_fd(sd_bus *b) {
955 assert(b->input_fd >= 0);
956 assert(b->output_fd >= 0);
958 r = fd_nonblock(b->input_fd, true);
962 r = fd_cloexec(b->input_fd, true);
966 if (b->input_fd != b->output_fd) {
967 r = fd_nonblock(b->output_fd, true);
971 r = fd_cloexec(b->output_fd, true);
976 if (fstat(b->input_fd, &st) < 0)
979 return bus_socket_take_fd(b);
982 _public_ int sd_bus_start(sd_bus *bus) {
985 assert_return(bus, -EINVAL);
986 assert_return(bus->state == BUS_UNSET, -EPERM);
987 assert_return(!bus_pid_changed(bus), -ECHILD);
989 bus->state = BUS_OPENING;
991 if (bus->is_server && bus->bus_client)
994 if (bus->input_fd >= 0)
995 r = bus_start_fd(bus);
996 else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->machine)
997 r = bus_start_address(bus);
1006 return bus_send_hello(bus);
1009 _public_ int sd_bus_open(sd_bus **ret) {
1014 assert_return(ret, -EINVAL);
1016 /* Let's connect to the starter bus if it is set, and
1017 * otherwise to the bus that is appropropriate for the scope
1018 * we are running in */
1020 e = secure_getenv("DBUS_STARTER_BUS_TYPE");
1022 if (streq(e, "system"))
1023 return sd_bus_open_system(ret);
1024 #if 0 /// elogind does not support systemd user instances
1025 else if (STR_IN_SET(e, "session", "user"))
1026 return sd_bus_open_user(ret);
1030 e = secure_getenv("DBUS_STARTER_ADDRESS");
1032 #if 0 /// elogind does not support systemd user instances
1033 if (cg_pid_get_owner_uid(0, NULL) >= 0)
1034 return sd_bus_open_user(ret);
1037 return sd_bus_open_system(ret);
1044 r = sd_bus_set_address(b, e);
1048 b->bus_client = true;
1050 /* We don't know whether the bus is trusted or not, so better
1051 * be safe, and authenticate everything */
1053 b->is_local = false;
1054 b->attach_flags |= KDBUS_ATTACH_CAPS | KDBUS_ATTACH_CREDS;
1055 b->creds_mask |= SD_BUS_CREDS_UID | SD_BUS_CREDS_EUID | SD_BUS_CREDS_EFFECTIVE_CAPS;
1057 r = sd_bus_start(b);
1069 int bus_set_address_system(sd_bus *b) {
1073 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1075 return sd_bus_set_address(b, e);
1077 return sd_bus_set_address(b, DEFAULT_SYSTEM_BUS_ADDRESS);
1080 _public_ int sd_bus_open_system(sd_bus **ret) {
1084 assert_return(ret, -EINVAL);
1090 r = bus_set_address_system(b);
1094 b->bus_client = true;
1095 b->is_system = true;
1097 /* Let's do per-method access control on the system bus. We
1098 * need the caller's UID and capability set for that. */
1100 b->attach_flags |= KDBUS_ATTACH_CAPS | KDBUS_ATTACH_CREDS;
1101 b->creds_mask |= SD_BUS_CREDS_UID | SD_BUS_CREDS_EUID | SD_BUS_CREDS_EFFECTIVE_CAPS;
1104 r = sd_bus_start(b);
1116 #if 0 /// elogind can not open/use a user bus
1117 int bus_set_address_user(sd_bus *b) {
1119 _cleanup_free_ char *ee = NULL, *s = NULL;
1123 e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1125 return sd_bus_set_address(b, e);
1127 e = secure_getenv("XDG_RUNTIME_DIR");
1131 ee = bus_address_escape(e);
1135 if (asprintf(&s, UNIX_USER_BUS_ADDRESS_FMT, ee) < 0)
1145 _public_ int sd_bus_open_user(sd_bus **ret) {
1146 #if 0 /// elogind does not support user buses
1150 assert_return(ret, -EINVAL);
1156 r = bus_set_address_user(b);
1160 b->bus_client = true;
1163 /* We don't do any per-method access control on the user
1168 r = sd_bus_start(b);
1179 return sd_bus_open_system(ret);
1183 int bus_set_address_system_remote(sd_bus *b, const char *host) {
1184 _cleanup_free_ char *e = NULL;
1185 char *m = NULL, *c = NULL;
1190 /* Let's see if we shall enter some container */
1191 m = strchr(host, ':');
1195 /* Let's make sure this is not a port of some kind,
1196 * and is a valid machine name. */
1197 if (!in_charset(m, "0123456789") && machine_name_is_valid(m)) {
1200 /* Cut out the host part */
1201 t = strndupa(host, m - host - 1);
1202 e = bus_address_escape(t);
1206 c = strjoina(",argv5=--machine=", m);
1211 e = bus_address_escape(host);
1216 b->address = strjoin("unixexec:path=ssh,argv1=-xT,argv2=--,argv3=", e, ",argv4=systemd-stdio-bridge", c);
1223 _public_ int sd_bus_open_system_remote(sd_bus **ret, const char *host) {
1227 assert_return(host, -EINVAL);
1228 assert_return(ret, -EINVAL);
1230 r = sd_bus_new(&bus);
1234 r = bus_set_address_system_remote(bus, host);
1238 bus->bus_client = true;
1239 bus->trusted = false;
1240 bus->is_system = true;
1241 bus->is_local = false;
1243 r = sd_bus_start(bus);
1255 int bus_set_address_system_machine(sd_bus *b, const char *machine) {
1256 _cleanup_free_ char *e = NULL;
1261 e = bus_address_escape(machine);
1265 b->address = strjoin("x-machine-unix:machine=", e);
1272 _public_ int sd_bus_open_system_machine(sd_bus **ret, const char *machine) {
1276 assert_return(machine, -EINVAL);
1277 assert_return(ret, -EINVAL);
1278 assert_return(machine_name_is_valid(machine), -EINVAL);
1280 r = sd_bus_new(&bus);
1284 r = bus_set_address_system_machine(bus, machine);
1288 bus->bus_client = true;
1289 bus->trusted = false;
1290 bus->is_system = true;
1291 bus->is_local = false;
1293 r = sd_bus_start(bus);
1305 _public_ void sd_bus_close(sd_bus *bus) {
1309 if (bus->state == BUS_CLOSED)
1311 if (bus_pid_changed(bus))
1314 bus->state = BUS_CLOSED;
1316 sd_bus_detach_event(bus);
1318 /* Drop all queued messages so that they drop references to
1319 * the bus object and the bus may be freed */
1320 bus_reset_queues(bus);
1325 _public_ sd_bus* sd_bus_flush_close_unref(sd_bus *bus) {
1333 return sd_bus_unref(bus);
1336 static void bus_enter_closing(sd_bus *bus) {
1339 if (!IN_SET(bus->state, BUS_OPENING, BUS_AUTHENTICATING, BUS_HELLO, BUS_RUNNING))
1342 bus->state = BUS_CLOSING;
1345 _public_ sd_bus *sd_bus_ref(sd_bus *bus) {
1350 assert_se(REFCNT_INC(bus->n_ref) >= 2);
1355 _public_ sd_bus *sd_bus_unref(sd_bus *bus) {
1361 i = REFCNT_DEC(bus->n_ref);
1369 _public_ int sd_bus_is_open(sd_bus *bus) {
1371 assert_return(bus, -EINVAL);
1372 assert_return(!bus_pid_changed(bus), -ECHILD);
1374 return BUS_IS_OPEN(bus->state);
1377 _public_ int sd_bus_can_send(sd_bus *bus, char type) {
1380 assert_return(bus, -EINVAL);
1381 assert_return(bus->state != BUS_UNSET, -ENOTCONN);
1382 assert_return(!bus_pid_changed(bus), -ECHILD);
1384 if (bus->hello_flags & KDBUS_HELLO_MONITOR)
1387 if (type == SD_BUS_TYPE_UNIX_FD) {
1388 if (!(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD))
1391 r = bus_ensure_running(bus);
1395 return bus->can_fds;
1398 return bus_type_is_valid(type);
1401 _public_ int sd_bus_get_bus_id(sd_bus *bus, sd_id128_t *id) {
1404 assert_return(bus, -EINVAL);
1405 assert_return(id, -EINVAL);
1406 assert_return(!bus_pid_changed(bus), -ECHILD);
1408 r = bus_ensure_running(bus);
1412 *id = bus->server_id;
1416 static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
1421 /* If we copy the same message to multiple
1422 * destinations, avoid using the same cookie
1424 b->cookie = MAX(b->cookie, BUS_MESSAGE_COOKIE(m));
1429 timeout = BUS_DEFAULT_TIMEOUT;
1431 return sd_bus_message_seal(m, ++b->cookie, timeout);
1434 static int bus_remarshal_message(sd_bus *b, sd_bus_message **m) {
1435 bool remarshal = false;
1439 /* wrong packet version */
1440 if (b->message_version != 0 && b->message_version != (*m)->header->version)
1443 /* wrong packet endianness */
1444 if (b->message_endian != 0 && b->message_endian != (*m)->header->endian)
1447 return remarshal ? bus_message_remarshal(b, m) : 0;
1450 int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
1454 /* Fake some timestamps, if they were requested, and not
1455 * already initialized */
1456 if (b->attach_flags & KDBUS_ATTACH_TIMESTAMP) {
1457 if (m->realtime <= 0)
1458 m->realtime = now(CLOCK_REALTIME);
1460 if (m->monotonic <= 0)
1461 m->monotonic = now(CLOCK_MONOTONIC);
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. */
1471 return sd_bus_message_seal(m, 0xFFFFFFFFULL, 0);
1474 static int bus_write_message(sd_bus *bus, sd_bus_message *m, bool hint_sync_call, size_t *idx) {
1480 r = bus_socket_write_message(bus, m, idx);
1484 if (*idx >= BUS_MESSAGE_SIZE(m))
1485 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",
1486 bus_message_type_to_string(m->header->type),
1487 strna(sd_bus_message_get_sender(m)),
1488 strna(sd_bus_message_get_destination(m)),
1489 strna(sd_bus_message_get_path(m)),
1490 strna(sd_bus_message_get_interface(m)),
1491 strna(sd_bus_message_get_member(m)),
1492 BUS_MESSAGE_COOKIE(m),
1494 strna(m->error.name),
1495 strna(m->error.message));
1500 static int dispatch_wqueue(sd_bus *bus) {
1504 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
1506 while (bus->wqueue_size > 0) {
1508 r = bus_write_message(bus, bus->wqueue[0], false, &bus->windex);
1512 /* Didn't do anything this time */
1514 else if (bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
1515 /* Fully written. Let's drop the entry from
1518 * This isn't particularly optimized, but
1519 * well, this is supposed to be our worst-case
1520 * buffer only, and the socket buffer is
1521 * supposed to be our primary buffer, and if
1522 * it got full, then all bets are off
1526 sd_bus_message_unref(bus->wqueue[0]);
1527 memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
1537 static int bus_read_message(sd_bus *bus, bool hint_priority, int64_t priority) {
1540 return bus_socket_read_message(bus);
1543 int bus_rqueue_make_room(sd_bus *bus) {
1546 if (bus->rqueue_size >= BUS_RQUEUE_MAX)
1549 if (!GREEDY_REALLOC(bus->rqueue, bus->rqueue_allocated, bus->rqueue_size + 1))
1555 static int dispatch_rqueue(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **m) {
1560 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
1562 /* Note that the priority logic is only available on kdbus,
1563 * where the rqueue is unused. We check the rqueue here
1564 * anyway, because it's simple... */
1567 if (bus->rqueue_size > 0) {
1568 /* Dispatch a queued message */
1570 *m = bus->rqueue[0];
1572 memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
1576 /* Try to read a new message */
1577 r = bus_read_message(bus, hint_priority, priority);
1587 static int bus_send_internal(sd_bus *bus, sd_bus_message *_m, uint64_t *cookie, bool hint_sync_call) {
1588 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = sd_bus_message_ref(_m);
1591 assert_return(m, -EINVAL);
1596 assert_return(!bus_pid_changed(bus), -ECHILD);
1598 if (!BUS_IS_OPEN(bus->state))
1602 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
1609 /* If the cookie number isn't kept, then we know that no reply
1611 if (!cookie && !m->sealed)
1612 m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
1614 r = bus_seal_message(bus, m, 0);
1618 /* Remarshall if we have to. This will possibly unref the
1619 * message and place a replacement in m */
1620 r = bus_remarshal_message(bus, &m);
1624 /* If this is a reply and no reply was requested, then let's
1625 * suppress this, if we can */
1629 if (IN_SET(bus->state, BUS_RUNNING, BUS_HELLO) && bus->wqueue_size <= 0) {
1632 r = bus_write_message(bus, m, hint_sync_call, &idx);
1634 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
1635 bus_enter_closing(bus);
1642 if (idx < BUS_MESSAGE_SIZE(m)) {
1643 /* Wasn't fully written. So let's remember how
1644 * much was written. Note that the first entry
1645 * of the wqueue array is always allocated so
1646 * that we always can remember how much was
1648 bus->wqueue[0] = sd_bus_message_ref(m);
1649 bus->wqueue_size = 1;
1654 /* Just append it to the queue. */
1656 if (bus->wqueue_size >= BUS_WQUEUE_MAX)
1659 if (!GREEDY_REALLOC(bus->wqueue, bus->wqueue_allocated, bus->wqueue_size + 1))
1662 bus->wqueue[bus->wqueue_size++] = sd_bus_message_ref(m);
1667 *cookie = BUS_MESSAGE_COOKIE(m);
1672 _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *cookie) {
1673 return bus_send_internal(bus, m, cookie, false);
1676 _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *cookie) {
1679 assert_return(m, -EINVAL);
1684 assert_return(!bus_pid_changed(bus), -ECHILD);
1686 if (!BUS_IS_OPEN(bus->state))
1689 if (!streq_ptr(m->destination, destination)) {
1694 r = sd_bus_message_set_destination(m, destination);
1699 return sd_bus_send(bus, m, cookie);
1702 static usec_t calc_elapse(uint64_t usec) {
1703 if (usec == (uint64_t) -1)
1706 return now(CLOCK_MONOTONIC) + usec;
1709 static int timeout_compare(const void *a, const void *b) {
1710 const struct reply_callback *x = a, *y = b;
1712 if (x->timeout != 0 && y->timeout == 0)
1715 if (x->timeout == 0 && y->timeout != 0)
1718 if (x->timeout < y->timeout)
1721 if (x->timeout > y->timeout)
1727 _public_ int sd_bus_call_async(
1731 sd_bus_message_handler_t callback,
1735 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = sd_bus_message_ref(_m);
1736 _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *s = NULL;
1739 assert_return(m, -EINVAL);
1740 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1741 assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1742 assert_return(callback, -EINVAL);
1747 assert_return(!bus_pid_changed(bus), -ECHILD);
1749 if (!BUS_IS_OPEN(bus->state))
1752 r = ordered_hashmap_ensure_allocated(&bus->reply_callbacks, &uint64_hash_ops);
1756 r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
1760 r = bus_seal_message(bus, m, usec);
1764 r = bus_remarshal_message(bus, &m);
1768 s = bus_slot_allocate(bus, !slot, BUS_REPLY_CALLBACK, sizeof(struct reply_callback), userdata);
1772 s->reply_callback.callback = callback;
1774 s->reply_callback.cookie = BUS_MESSAGE_COOKIE(m);
1775 r = ordered_hashmap_put(bus->reply_callbacks, &s->reply_callback.cookie, &s->reply_callback);
1777 s->reply_callback.cookie = 0;
1781 s->reply_callback.timeout = calc_elapse(m->timeout);
1782 if (s->reply_callback.timeout != 0) {
1783 r = prioq_put(bus->reply_callbacks_prioq, &s->reply_callback, &s->reply_callback.prioq_idx);
1785 s->reply_callback.timeout = 0;
1790 r = sd_bus_send(bus, m, &s->reply_callback.cookie);
1801 int bus_ensure_running(sd_bus *bus) {
1806 if (IN_SET(bus->state, BUS_UNSET, BUS_CLOSED, BUS_CLOSING))
1808 if (bus->state == BUS_RUNNING)
1812 r = sd_bus_process(bus, NULL);
1815 if (bus->state == BUS_RUNNING)
1820 r = sd_bus_wait(bus, (uint64_t) -1);
1826 _public_ int sd_bus_call(
1830 sd_bus_error *error,
1831 sd_bus_message **reply) {
1833 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = sd_bus_message_ref(_m);
1839 bus_assert_return(m, -EINVAL, error);
1840 bus_assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL, error);
1841 bus_assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL, error);
1842 bus_assert_return(!bus_error_is_dirty(error), -EINVAL, error);
1847 bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
1849 if (!BUS_IS_OPEN(bus->state)) {
1854 r = bus_ensure_running(bus);
1858 i = bus->rqueue_size;
1860 r = bus_seal_message(bus, m, usec);
1864 r = bus_remarshal_message(bus, &m);
1868 r = bus_send_internal(bus, m, &cookie, true);
1872 timeout = calc_elapse(m->timeout);
1877 while (i < bus->rqueue_size) {
1878 sd_bus_message *incoming = NULL;
1880 incoming = bus->rqueue[i];
1882 if (incoming->reply_cookie == cookie) {
1883 /* Found a match! */
1885 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1887 log_debug_bus_message(incoming);
1889 if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
1891 if (incoming->n_fds <= 0 || (bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)) {
1895 sd_bus_message_unref(incoming);
1900 r = sd_bus_error_setf(error, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptors which I couldn't accept. Sorry.");
1901 sd_bus_message_unref(incoming);
1904 } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR) {
1905 r = sd_bus_error_copy(error, &incoming->error);
1906 sd_bus_message_unref(incoming);
1913 } else if (BUS_MESSAGE_COOKIE(incoming) == cookie &&
1916 streq(bus->unique_name, incoming->sender)) {
1918 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1921 /* Our own message? Somebody is trying
1922 * to send its own client a message,
1923 * let's not dead-lock, let's fail
1926 sd_bus_message_unref(incoming);
1931 /* Try to read more, right-away */
1935 r = bus_read_message(bus, false, 0);
1937 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
1938 bus_enter_closing(bus);
1950 n = now(CLOCK_MONOTONIC);
1958 left = (uint64_t) -1;
1960 r = bus_poll(bus, true, left);
1968 r = dispatch_wqueue(bus);
1970 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
1971 bus_enter_closing(bus);
1980 return sd_bus_error_set_errno(error, r);
1983 _public_ int sd_bus_get_fd(sd_bus *bus) {
1985 assert_return(bus, -EINVAL);
1986 assert_return(bus->input_fd == bus->output_fd, -EPERM);
1987 assert_return(!bus_pid_changed(bus), -ECHILD);
1989 return bus->input_fd;
1992 _public_ int sd_bus_get_events(sd_bus *bus) {
1995 assert_return(bus, -EINVAL);
1996 assert_return(!bus_pid_changed(bus), -ECHILD);
1998 if (!BUS_IS_OPEN(bus->state) && bus->state != BUS_CLOSING)
2001 if (bus->state == BUS_OPENING)
2003 else if (bus->state == BUS_AUTHENTICATING) {
2005 if (bus_socket_auth_needs_write(bus))
2010 } else if (IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)) {
2011 if (bus->rqueue_size <= 0)
2013 if (bus->wqueue_size > 0)
2020 _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
2021 struct reply_callback *c;
2023 assert_return(bus, -EINVAL);
2024 assert_return(timeout_usec, -EINVAL);
2025 assert_return(!bus_pid_changed(bus), -ECHILD);
2027 if (!BUS_IS_OPEN(bus->state) && bus->state != BUS_CLOSING)
2030 if (bus->track_queue) {
2035 if (bus->state == BUS_CLOSING) {
2040 if (bus->state == BUS_AUTHENTICATING) {
2041 *timeout_usec = bus->auth_timeout;
2045 if (!IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)) {
2046 *timeout_usec = (uint64_t) -1;
2050 if (bus->rqueue_size > 0) {
2055 c = prioq_peek(bus->reply_callbacks_prioq);
2057 *timeout_usec = (uint64_t) -1;
2061 if (c->timeout == 0) {
2062 *timeout_usec = (uint64_t) -1;
2066 *timeout_usec = c->timeout;
2070 static int process_timeout(sd_bus *bus) {
2071 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2072 _cleanup_(sd_bus_message_unrefp) sd_bus_message* m = NULL;
2073 struct reply_callback *c;
2080 c = prioq_peek(bus->reply_callbacks_prioq);
2084 n = now(CLOCK_MONOTONIC);
2088 r = bus_message_new_synthetic_error(
2091 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
2096 r = bus_seal_synthetic_message(bus, m);
2100 assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
2103 ordered_hashmap_remove(bus->reply_callbacks, &c->cookie);
2106 slot = container_of(c, sd_bus_slot, reply_callback);
2108 bus->iteration_counter++;
2110 bus->current_message = m;
2111 bus->current_slot = sd_bus_slot_ref(slot);
2112 bus->current_handler = c->callback;
2113 bus->current_userdata = slot->userdata;
2114 r = c->callback(m, slot->userdata, &error_buffer);
2115 bus->current_userdata = NULL;
2116 bus->current_handler = NULL;
2117 bus->current_slot = NULL;
2118 bus->current_message = NULL;
2120 if (slot->floating) {
2121 bus_slot_disconnect(slot);
2122 sd_bus_slot_unref(slot);
2125 sd_bus_slot_unref(slot);
2127 return bus_maybe_reply_error(m, r, &error_buffer);
2130 static int process_hello(sd_bus *bus, sd_bus_message *m) {
2134 if (bus->state != BUS_HELLO)
2137 /* Let's make sure the first message on the bus is the HELLO
2138 * reply. But note that we don't actually parse the message
2139 * here (we leave that to the usual handling), we just verify
2140 * we don't let any earlier msg through. */
2142 if (!IN_SET(m->header->type, SD_BUS_MESSAGE_METHOD_RETURN, SD_BUS_MESSAGE_METHOD_ERROR))
2145 if (m->reply_cookie != 1)
2151 static int process_reply(sd_bus *bus, sd_bus_message *m) {
2152 _cleanup_(sd_bus_message_unrefp) sd_bus_message *synthetic_reply = NULL;
2153 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2154 struct reply_callback *c;
2161 if (!IN_SET(m->header->type, SD_BUS_MESSAGE_METHOD_RETURN, SD_BUS_MESSAGE_METHOD_ERROR))
2164 if (m->destination && bus->unique_name && !streq_ptr(m->destination, bus->unique_name))
2167 c = ordered_hashmap_remove(bus->reply_callbacks, &m->reply_cookie);
2173 slot = container_of(c, sd_bus_slot, reply_callback);
2175 if (m->n_fds > 0 && !(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)) {
2177 /* If the reply contained a file descriptor which we
2178 * didn't want we pass an error instead. */
2180 r = bus_message_new_synthetic_error(
2183 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptor"),
2188 /* Copy over original timestamp */
2189 synthetic_reply->realtime = m->realtime;
2190 synthetic_reply->monotonic = m->monotonic;
2191 synthetic_reply->seqnum = m->seqnum;
2193 r = bus_seal_synthetic_message(bus, synthetic_reply);
2197 m = synthetic_reply;
2199 r = sd_bus_message_rewind(m, true);
2204 if (c->timeout != 0) {
2205 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2209 bus->current_slot = sd_bus_slot_ref(slot);
2210 bus->current_handler = c->callback;
2211 bus->current_userdata = slot->userdata;
2212 r = c->callback(m, slot->userdata, &error_buffer);
2213 bus->current_userdata = NULL;
2214 bus->current_handler = NULL;
2215 bus->current_slot = NULL;
2217 if (slot->floating) {
2218 bus_slot_disconnect(slot);
2219 sd_bus_slot_unref(slot);
2222 sd_bus_slot_unref(slot);
2224 return bus_maybe_reply_error(m, r, &error_buffer);
2227 static int process_filter(sd_bus *bus, sd_bus_message *m) {
2228 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2229 struct filter_callback *l;
2236 bus->filter_callbacks_modified = false;
2238 LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
2241 if (bus->filter_callbacks_modified)
2244 /* Don't run this more than once per iteration */
2245 if (l->last_iteration == bus->iteration_counter)
2248 l->last_iteration = bus->iteration_counter;
2250 r = sd_bus_message_rewind(m, true);
2254 slot = container_of(l, sd_bus_slot, filter_callback);
2256 bus->current_slot = sd_bus_slot_ref(slot);
2257 bus->current_handler = l->callback;
2258 bus->current_userdata = slot->userdata;
2259 r = l->callback(m, slot->userdata, &error_buffer);
2260 bus->current_userdata = NULL;
2261 bus->current_handler = NULL;
2262 bus->current_slot = sd_bus_slot_unref(slot);
2264 r = bus_maybe_reply_error(m, r, &error_buffer);
2270 } while (bus->filter_callbacks_modified);
2275 static int process_match(sd_bus *bus, sd_bus_message *m) {
2282 bus->match_callbacks_modified = false;
2284 r = bus_match_run(bus, &bus->match_callbacks, m);
2288 } while (bus->match_callbacks_modified);
2293 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
2294 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
2300 if (bus->hello_flags & KDBUS_HELLO_MONITOR)
2303 if (bus->manual_peer_interface)
2306 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
2309 if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
2312 if (m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
2315 if (streq_ptr(m->member, "Ping"))
2316 r = sd_bus_message_new_method_return(m, &reply);
2317 else if (streq_ptr(m->member, "GetMachineId")) {
2321 r = sd_id128_get_machine(&id);
2325 r = sd_bus_message_new_method_return(m, &reply);
2329 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
2331 r = sd_bus_message_new_method_errorf(
2333 SD_BUS_ERROR_UNKNOWN_METHOD,
2334 "Unknown method '%s' on interface '%s'.", m->member, m->interface);
2340 r = sd_bus_send(bus, reply, NULL);
2347 static int process_fd_check(sd_bus *bus, sd_bus_message *m) {
2351 /* If we got a message with a file descriptor which we didn't
2352 * want to accept, then let's drop it. How can this even
2353 * happen? For example, when the kernel queues a message into
2354 * an activatable names's queue which allows fds, and then is
2355 * delivered to us later even though we ourselves did not
2358 if (bus->hello_flags & KDBUS_HELLO_MONITOR)
2364 if (bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)
2367 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
2368 return 1; /* just eat it up */
2370 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Message contains file descriptors, which I cannot accept. Sorry.");
2373 static int process_message(sd_bus *bus, sd_bus_message *m) {
2379 bus->current_message = m;
2380 bus->iteration_counter++;
2382 log_debug_bus_message(m);
2384 r = process_hello(bus, m);
2388 r = process_reply(bus, m);
2392 r = process_fd_check(bus, m);
2396 r = process_filter(bus, m);
2400 r = process_match(bus, m);
2404 r = process_builtin(bus, m);
2408 r = bus_process_object(bus, m);
2411 bus->current_message = NULL;
2415 static int dispatch_track(sd_bus *bus) {
2418 if (!bus->track_queue)
2421 bus_track_dispatch(bus->track_queue);
2425 static int process_running(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) {
2426 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2430 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
2432 r = process_timeout(bus);
2436 r = dispatch_wqueue(bus);
2440 r = dispatch_track(bus);
2444 r = dispatch_rqueue(bus, hint_priority, priority, &m);
2450 r = process_message(bus, m);
2455 r = sd_bus_message_rewind(m, true);
2464 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
2466 log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s",
2467 strna(sd_bus_message_get_sender(m)),
2468 strna(sd_bus_message_get_path(m)),
2469 strna(sd_bus_message_get_interface(m)),
2470 strna(sd_bus_message_get_member(m)));
2472 r = sd_bus_reply_method_errorf(
2474 SD_BUS_ERROR_UNKNOWN_OBJECT,
2475 "Unknown object '%s'.", m->path);
2489 static int bus_exit_now(sd_bus *bus) {
2492 /* Exit due to close, if this is requested. If this is bus object is attached to an event source, invokes
2493 * sd_event_exit(), otherwise invokes libc exit(). */
2495 if (bus->exited) /* did we already exit? */
2497 if (!bus->exit_triggered) /* was the exit condition triggered? */
2499 if (!bus->exit_on_disconnect) /* Shall we actually exit on disconnection? */
2502 bus->exited = true; /* never exit more than once */
2504 log_debug("Bus connection disconnected, exiting.");
2507 return sd_event_exit(bus->event, EXIT_FAILURE);
2511 assert_not_reached("exit() didn't exit?");
2514 static int process_closing_reply_callback(sd_bus *bus, struct reply_callback *c) {
2515 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2516 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2523 r = bus_message_new_synthetic_error(
2526 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
2531 r = bus_seal_synthetic_message(bus, m);
2535 if (c->timeout != 0) {
2536 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2540 ordered_hashmap_remove(bus->reply_callbacks, &c->cookie);
2543 slot = container_of(c, sd_bus_slot, reply_callback);
2545 bus->iteration_counter++;
2547 bus->current_message = m;
2548 bus->current_slot = sd_bus_slot_ref(slot);
2549 bus->current_handler = c->callback;
2550 bus->current_userdata = slot->userdata;
2551 r = c->callback(m, slot->userdata, &error_buffer);
2552 bus->current_userdata = NULL;
2553 bus->current_handler = NULL;
2554 bus->current_slot = NULL;
2555 bus->current_message = NULL;
2557 if (slot->floating) {
2558 bus_slot_disconnect(slot);
2559 sd_bus_slot_unref(slot);
2562 sd_bus_slot_unref(slot);
2564 return bus_maybe_reply_error(m, r, &error_buffer);
2567 static int process_closing(sd_bus *bus, sd_bus_message **ret) {
2568 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2569 struct reply_callback *c;
2573 assert(bus->state == BUS_CLOSING);
2575 /* First, fail all outstanding method calls */
2576 c = ordered_hashmap_first(bus->reply_callbacks);
2578 return process_closing_reply_callback(bus, c);
2580 /* Then, fake-drop all remaining bus tracking references */
2582 bus_track_close(bus->tracks);
2586 /* Then, synthesize a Disconnected message */
2587 r = sd_bus_message_new_signal(
2590 "/org/freedesktop/DBus/Local",
2591 "org.freedesktop.DBus.Local",
2596 bus_message_set_sender_local(bus, m);
2598 r = bus_seal_synthetic_message(bus, m);
2604 bus->current_message = m;
2605 bus->iteration_counter++;
2607 r = process_filter(bus, m);
2611 r = process_match(bus, m);
2615 /* Nothing else to do, exit now, if the condition holds */
2616 bus->exit_triggered = true;
2617 (void) bus_exit_now(bus);
2627 bus->current_message = NULL;
2632 static int bus_process_internal(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) {
2633 BUS_DONT_DESTROY(bus);
2636 /* Returns 0 when we didn't do anything. This should cause the
2637 * caller to invoke sd_bus_wait() before returning the next
2638 * time. Returns > 0 when we did something, which possibly
2639 * means *ret is filled in with an unprocessed message. */
2641 assert_return(bus, -EINVAL);
2642 assert_return(!bus_pid_changed(bus), -ECHILD);
2644 /* We don't allow recursively invoking sd_bus_process(). */
2645 assert_return(!bus->current_message, -EBUSY);
2646 assert(!bus->current_slot);
2648 switch (bus->state) {
2657 r = bus_socket_process_opening(bus);
2658 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
2659 bus_enter_closing(bus);
2667 case BUS_AUTHENTICATING:
2668 r = bus_socket_process_authenticating(bus);
2669 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
2670 bus_enter_closing(bus);
2682 r = process_running(bus, hint_priority, priority, ret);
2683 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
2684 bus_enter_closing(bus);
2694 return process_closing(bus, ret);
2697 assert_not_reached("Unknown state");
2700 _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
2701 return bus_process_internal(bus, false, 0, ret);
2704 _public_ int sd_bus_process_priority(sd_bus *bus, int64_t priority, sd_bus_message **ret) {
2705 return bus_process_internal(bus, true, priority, ret);
2708 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
2709 struct pollfd p[2] = {};
2712 usec_t m = USEC_INFINITY;
2716 if (bus->state == BUS_CLOSING)
2719 if (!BUS_IS_OPEN(bus->state))
2722 e = sd_bus_get_events(bus);
2727 /* The caller really needs some more data, he doesn't
2728 * care about what's already read, or any timeouts
2729 * except its own. */
2733 /* The caller wants to process if there's something to
2734 * process, but doesn't care otherwise */
2736 r = sd_bus_get_timeout(bus, &until);
2741 nw = now(CLOCK_MONOTONIC);
2742 m = until > nw ? until - nw : 0;
2746 if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
2749 p[0].fd = bus->input_fd;
2750 if (bus->output_fd == bus->input_fd) {
2754 p[0].events = e & POLLIN;
2755 p[1].fd = bus->output_fd;
2756 p[1].events = e & POLLOUT;
2760 r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
2764 return r > 0 ? 1 : 0;
2767 _public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
2769 assert_return(bus, -EINVAL);
2770 assert_return(!bus_pid_changed(bus), -ECHILD);
2772 if (bus->state == BUS_CLOSING)
2775 if (!BUS_IS_OPEN(bus->state))
2778 if (bus->rqueue_size > 0)
2781 return bus_poll(bus, false, timeout_usec);
2784 _public_ int sd_bus_flush(sd_bus *bus) {
2787 assert_return(bus, -EINVAL);
2788 assert_return(!bus_pid_changed(bus), -ECHILD);
2790 if (bus->state == BUS_CLOSING)
2793 if (!BUS_IS_OPEN(bus->state))
2796 r = bus_ensure_running(bus);
2800 if (bus->wqueue_size <= 0)
2804 r = dispatch_wqueue(bus);
2806 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
2807 bus_enter_closing(bus);
2814 if (bus->wqueue_size <= 0)
2817 r = bus_poll(bus, false, (uint64_t) -1);
2823 _public_ int sd_bus_add_filter(
2826 sd_bus_message_handler_t callback,
2831 assert_return(bus, -EINVAL);
2832 assert_return(callback, -EINVAL);
2833 assert_return(!bus_pid_changed(bus), -ECHILD);
2835 s = bus_slot_allocate(bus, !slot, BUS_FILTER_CALLBACK, sizeof(struct filter_callback), userdata);
2839 s->filter_callback.callback = callback;
2841 bus->filter_callbacks_modified = true;
2842 LIST_PREPEND(callbacks, bus->filter_callbacks, &s->filter_callback);
2850 _public_ int sd_bus_add_match(
2854 sd_bus_message_handler_t callback,
2857 struct bus_match_component *components = NULL;
2858 unsigned n_components = 0;
2859 sd_bus_slot *s = NULL;
2862 assert_return(bus, -EINVAL);
2863 assert_return(match, -EINVAL);
2864 assert_return(!bus_pid_changed(bus), -ECHILD);
2866 r = bus_match_parse(match, &components, &n_components);
2870 s = bus_slot_allocate(bus, !slot, BUS_MATCH_CALLBACK, sizeof(struct match_callback), userdata);
2876 s->match_callback.callback = callback;
2878 if (bus->bus_client) {
2879 enum bus_match_scope scope;
2881 scope = bus_match_get_scope(components, n_components);
2883 /* Do not install server-side matches for matches
2884 * against the local service, interface or bus path. */
2885 if (scope != BUS_MATCH_LOCAL) {
2887 /* We store the original match string, so that
2888 * we can use it to remove the match again. */
2890 s->match_callback.match_string = strdup(match);
2891 if (!s->match_callback.match_string) {
2896 r = bus_add_match_internal(bus, s->match_callback.match_string, components, n_components);
2900 s->match_added = true;
2904 bus->match_callbacks_modified = true;
2905 r = bus_match_add(&bus->match_callbacks, components, n_components, &s->match_callback);
2914 bus_match_parse_free(components, n_components);
2915 sd_bus_slot_unref(s);
2920 #if 0 /// UNNEEDED by elogind
2921 int bus_remove_match_by_string(
2924 sd_bus_message_handler_t callback,
2927 struct bus_match_component *components = NULL;
2928 unsigned n_components = 0;
2929 struct match_callback *c;
2932 assert_return(bus, -EINVAL);
2933 assert_return(match, -EINVAL);
2934 assert_return(!bus_pid_changed(bus), -ECHILD);
2936 r = bus_match_parse(match, &components, &n_components);
2940 r = bus_match_find(&bus->match_callbacks, components, n_components, NULL, NULL, &c);
2944 sd_bus_slot_unref(container_of(c, sd_bus_slot, match_callback));
2947 bus_match_parse_free(components, n_components);
2953 bool bus_pid_changed(sd_bus *bus) {
2956 /* We don't support people creating a bus connection and
2957 * keeping it around over a fork(). Let's complain. */
2959 return bus->original_pid != getpid_cached();
2962 static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2963 sd_bus *bus = userdata;
2968 r = sd_bus_process(bus, NULL);
2975 static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
2976 sd_bus *bus = userdata;
2981 r = sd_bus_process(bus, NULL);
2988 static int prepare_callback(sd_event_source *s, void *userdata) {
2989 sd_bus *bus = userdata;
2996 e = sd_bus_get_events(bus);
3000 if (bus->output_fd != bus->input_fd) {
3002 r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN);
3006 r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT);
3010 r = sd_event_source_set_io_events(bus->input_io_event_source, e);
3015 r = sd_bus_get_timeout(bus, &until);
3021 j = sd_event_source_set_time(bus->time_event_source, until);
3026 r = sd_event_source_set_enabled(bus->time_event_source, r > 0);
3033 static int quit_callback(sd_event_source *event, void *userdata) {
3034 sd_bus *bus = userdata;
3044 static int attach_io_events(sd_bus *bus) {
3049 if (bus->input_fd < 0)
3055 if (!bus->input_io_event_source) {
3056 r = sd_event_add_io(bus->event, &bus->input_io_event_source, bus->input_fd, 0, io_callback, bus);
3060 r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
3064 r = sd_event_source_set_priority(bus->input_io_event_source, bus->event_priority);
3068 r = sd_event_source_set_description(bus->input_io_event_source, "bus-input");
3070 r = sd_event_source_set_io_fd(bus->input_io_event_source, bus->input_fd);
3075 if (bus->output_fd != bus->input_fd) {
3076 assert(bus->output_fd >= 0);
3078 if (!bus->output_io_event_source) {
3079 r = sd_event_add_io(bus->event, &bus->output_io_event_source, bus->output_fd, 0, io_callback, bus);
3083 r = sd_event_source_set_priority(bus->output_io_event_source, bus->event_priority);
3087 r = sd_event_source_set_description(bus->input_io_event_source, "bus-output");
3089 r = sd_event_source_set_io_fd(bus->output_io_event_source, bus->output_fd);
3098 static void detach_io_events(sd_bus *bus) {
3101 if (bus->input_io_event_source) {
3102 sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
3103 bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
3106 if (bus->output_io_event_source) {
3107 sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
3108 bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
3112 _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
3115 assert_return(bus, -EINVAL);
3116 assert_return(!bus->event, -EBUSY);
3118 assert(!bus->input_io_event_source);
3119 assert(!bus->output_io_event_source);
3120 assert(!bus->time_event_source);
3123 bus->event = sd_event_ref(event);
3125 r = sd_event_default(&bus->event);
3130 bus->event_priority = priority;
3132 r = sd_event_add_time(bus->event, &bus->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, bus);
3136 r = sd_event_source_set_priority(bus->time_event_source, priority);
3140 r = sd_event_source_set_description(bus->time_event_source, "bus-time");
3144 r = sd_event_add_exit(bus->event, &bus->quit_event_source, quit_callback, bus);
3148 r = sd_event_source_set_description(bus->quit_event_source, "bus-exit");
3152 r = attach_io_events(bus);
3159 sd_bus_detach_event(bus);
3163 _public_ int sd_bus_detach_event(sd_bus *bus) {
3164 assert_return(bus, -EINVAL);
3169 detach_io_events(bus);
3171 if (bus->time_event_source) {
3172 sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF);
3173 bus->time_event_source = sd_event_source_unref(bus->time_event_source);
3176 if (bus->quit_event_source) {
3177 sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF);
3178 bus->quit_event_source = sd_event_source_unref(bus->quit_event_source);
3181 bus->event = sd_event_unref(bus->event);
3185 _public_ sd_event* sd_bus_get_event(sd_bus *bus) {
3186 assert_return(bus, NULL);
3191 _public_ sd_bus_message* sd_bus_get_current_message(sd_bus *bus) {
3192 assert_return(bus, NULL);
3194 return bus->current_message;
3197 _public_ sd_bus_slot* sd_bus_get_current_slot(sd_bus *bus) {
3198 assert_return(bus, NULL);
3200 return bus->current_slot;
3203 _public_ sd_bus_message_handler_t sd_bus_get_current_handler(sd_bus *bus) {
3204 assert_return(bus, NULL);
3206 return bus->current_handler;
3209 _public_ void* sd_bus_get_current_userdata(sd_bus *bus) {
3210 assert_return(bus, NULL);
3212 return bus->current_userdata;
3215 static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
3220 assert(default_bus);
3223 return !!*default_bus;
3226 *ret = sd_bus_ref(*default_bus);
3234 b->default_bus_ptr = default_bus;
3242 _public_ int sd_bus_default_system(sd_bus **ret) {
3243 return bus_default(sd_bus_open_system, &default_system_bus, ret);
3247 _public_ int sd_bus_default_user(sd_bus **ret) {
3248 #if 0 /// elogind does not support user buses
3249 return bus_default(sd_bus_open_user, &default_user_bus, ret);
3251 return sd_bus_default_system(ret);
3255 _public_ int sd_bus_default(sd_bus **ret) {
3259 /* Let's try our best to reuse another cached connection. If
3260 * the starter bus type is set, connect via our normal
3261 * connection logic, ignoring $DBUS_STARTER_ADDRESS, so that
3262 * we can share the connection with the user/system default
3265 e = secure_getenv("DBUS_STARTER_BUS_TYPE");
3267 if (streq(e, "system"))
3268 return sd_bus_default_system(ret);
3269 #if 0 /// elogind does not support systemd units
3270 else if (STR_IN_SET(e, "user", "session"))
3271 return sd_bus_default_user(ret);
3275 /* No type is specified, so we have not other option than to
3276 * use the starter address if it is set. */
3278 e = secure_getenv("DBUS_STARTER_ADDRESS");
3281 return bus_default(sd_bus_open, &default_starter_bus, ret);
3284 /* Finally, if nothing is set use the cached connection for
3285 * the right scope */
3287 #if 0 /// elogind does not support systemd user instances
3288 if (cg_pid_get_owner_uid(0, NULL) >= 0)
3289 return sd_bus_default_user(ret);
3292 return sd_bus_default_system(ret);
3295 _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
3296 assert_return(b, -EINVAL);
3297 assert_return(tid, -EINVAL);
3298 assert_return(!bus_pid_changed(b), -ECHILD);
3306 return sd_event_get_tid(b->event, tid);
3311 _public_ int sd_bus_path_encode(const char *prefix, const char *external_id, char **ret_path) {
3312 _cleanup_free_ char *e = NULL;
3315 assert_return(object_path_is_valid(prefix), -EINVAL);
3316 assert_return(external_id, -EINVAL);
3317 assert_return(ret_path, -EINVAL);
3319 e = bus_label_escape(external_id);
3323 ret = strjoin(prefix, "/", e);
3331 _public_ int sd_bus_path_decode(const char *path, const char *prefix, char **external_id) {
3335 assert_return(object_path_is_valid(path), -EINVAL);
3336 assert_return(object_path_is_valid(prefix), -EINVAL);
3337 assert_return(external_id, -EINVAL);
3339 e = object_path_startswith(path, prefix);
3341 *external_id = NULL;
3345 ret = bus_label_unescape(e);
3353 _public_ int sd_bus_path_encode_many(char **out, const char *path_template, ...) {
3354 _cleanup_strv_free_ char **labels = NULL;
3355 char *path, *path_pos, **label_pos;
3356 const char *sep, *template_pos;
3361 assert_return(out, -EINVAL);
3362 assert_return(path_template, -EINVAL);
3364 path_length = strlen(path_template);
3366 va_start(list, path_template);
3367 for (sep = strchr(path_template, '%'); sep; sep = strchr(sep + 1, '%')) {
3371 arg = va_arg(list, const char *);
3377 label = bus_label_escape(arg);
3383 r = strv_consume(&labels, label);
3389 /* add label length, but account for the format character */
3390 path_length += strlen(label) - 1;
3394 path = malloc(path_length + 1);
3401 for (template_pos = path_template; *template_pos; ) {
3402 sep = strchrnul(template_pos, '%');
3403 path_pos = mempcpy(path_pos, template_pos, sep - template_pos);
3407 path_pos = stpcpy(path_pos, *label_pos++);
3408 template_pos = sep + 1;
3416 _public_ int sd_bus_path_decode_many(const char *path, const char *path_template, ...) {
3417 _cleanup_strv_free_ char **labels = NULL;
3418 const char *template_pos, *path_pos;
3424 * This decodes an object-path based on a template argument. The
3425 * template consists of a verbatim path, optionally including special
3428 * - Each occurrence of '%' in the template matches an arbitrary
3429 * substring of a label in the given path. At most one such
3430 * directive is allowed per label. For each such directive, the
3431 * caller must provide an output parameter (char **) via va_arg. If
3432 * NULL is passed, the given label is verified, but not returned.
3433 * For each matched label, the *decoded* label is stored in the
3434 * passed output argument, and the caller is responsible to free
3435 * it. Note that the output arguments are only modified if the
3436 * actualy path matched the template. Otherwise, they're left
3439 * This function returns <0 on error, 0 if the path does not match the
3440 * template, 1 if it matched.
3443 assert_return(path, -EINVAL);
3444 assert_return(path_template, -EINVAL);
3448 for (template_pos = path_template; *template_pos; ) {
3453 /* verify everything until the next '%' matches verbatim */
3454 sep = strchrnul(template_pos, '%');
3455 length = sep - template_pos;
3456 if (strncmp(path_pos, template_pos, length))
3460 template_pos += length;
3465 /* We found the next '%' character. Everything up until here
3466 * matched. We now skip ahead to the end of this label and make
3467 * sure it matches the tail of the label in the path. Then we
3468 * decode the string in-between and save it for later use. */
3470 ++template_pos; /* skip over '%' */
3472 sep = strchrnul(template_pos, '/');
3473 length = sep - template_pos; /* length of suffix to match verbatim */
3475 /* verify the suffixes match */
3476 sep = strchrnul(path_pos, '/');
3477 if (sep - path_pos < (ssize_t)length ||
3478 strncmp(sep - length, template_pos, length))
3481 template_pos += length; /* skip over matched label */
3482 length = sep - path_pos - length; /* length of sub-label to decode */
3484 /* store unescaped label for later use */
3485 label = bus_label_unescape_n(path_pos, length);
3489 r = strv_consume(&labels, label);
3493 path_pos = sep; /* skip decoded label and suffix */
3496 /* end of template must match end of path */
3500 /* copy the labels over to the caller */
3501 va_start(list, path_template);
3502 for (label_pos = labels; label_pos && *label_pos; ++label_pos) {
3505 arg = va_arg(list, char **);
3518 _public_ int sd_bus_try_close(sd_bus *bus) {
3519 assert_return(bus, -EINVAL);
3520 assert_return(!bus_pid_changed(bus), -ECHILD);
3525 _public_ int sd_bus_get_description(sd_bus *bus, const char **description) {
3526 assert_return(bus, -EINVAL);
3527 assert_return(description, -EINVAL);
3528 assert_return(bus->description, -ENXIO);
3529 assert_return(!bus_pid_changed(bus), -ECHILD);
3531 *description = bus->description;
3535 int bus_get_root_path(sd_bus *bus) {
3538 if (bus->cgroup_root)
3541 r = cg_get_root_path(&bus->cgroup_root);
3543 bus->cgroup_root = strdup("/");
3544 if (!bus->cgroup_root)
3553 _public_ int sd_bus_get_scope(sd_bus *bus, const char **scope) {
3554 assert_return(bus, -EINVAL);
3555 assert_return(scope, -EINVAL);
3556 assert_return(!bus_pid_changed(bus), -ECHILD);
3563 if (bus->is_system) {
3571 _public_ int sd_bus_get_address(sd_bus *bus, const char **address) {
3573 assert_return(bus, -EINVAL);
3574 assert_return(address, -EINVAL);
3575 assert_return(!bus_pid_changed(bus), -ECHILD);
3578 *address = bus->address;
3585 _public_ int sd_bus_get_creds_mask(sd_bus *bus, uint64_t *mask) {
3586 assert_return(bus, -EINVAL);
3587 assert_return(mask, -EINVAL);
3588 assert_return(!bus_pid_changed(bus), -ECHILD);
3590 *mask = bus->creds_mask;
3594 _public_ int sd_bus_is_bus_client(sd_bus *bus) {
3595 assert_return(bus, -EINVAL);
3596 assert_return(!bus_pid_changed(bus), -ECHILD);
3598 return bus->bus_client;
3601 _public_ int sd_bus_is_server(sd_bus *bus) {
3602 assert_return(bus, -EINVAL);
3603 assert_return(!bus_pid_changed(bus), -ECHILD);
3605 return bus->is_server;
3608 _public_ int sd_bus_is_anonymous(sd_bus *bus) {
3609 assert_return(bus, -EINVAL);
3610 assert_return(!bus_pid_changed(bus), -ECHILD);
3612 return bus->anonymous_auth;
3615 _public_ int sd_bus_is_trusted(sd_bus *bus) {
3616 assert_return(bus, -EINVAL);
3617 assert_return(!bus_pid_changed(bus), -ECHILD);
3619 return bus->trusted;
3622 _public_ int sd_bus_is_monitor(sd_bus *bus) {
3623 assert_return(bus, -EINVAL);
3624 assert_return(!bus_pid_changed(bus), -ECHILD);
3626 return !!(bus->hello_flags & KDBUS_HELLO_MONITOR);
3629 static void flush_close(sd_bus *bus) {
3633 /* Flushes and closes the specified bus. We take a ref before,
3634 * to ensure the flushing does not cause the bus to be
3637 sd_bus_flush_close_unref(sd_bus_ref(bus));
3640 _public_ void sd_bus_default_flush_close(void) {
3641 flush_close(default_starter_bus);
3642 #if 0 /// elogind does not support user buses
3643 flush_close(default_user_bus);
3645 flush_close(default_system_bus);
3648 _public_ int sd_bus_set_exit_on_disconnect(sd_bus *bus, int b) {
3649 assert_return(bus, -EINVAL);
3651 /* Turns on exit-on-disconnect, and triggers it immediately if the bus connection was already
3652 * disconnected. Note that this is triggered exclusively on disconnections triggered by the server side, never
3653 * from the client side. */
3654 bus->exit_on_disconnect = b;
3656 /* If the exit condition was triggered already, exit immediately. */
3657 return bus_exit_now(bus);
3660 _public_ int sd_bus_get_exit_on_disconnect(sd_bus *bus) {
3661 assert_return(bus, -EINVAL);
3663 return bus->exit_on_disconnect;