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 "process-util.h"
53 #include "string-util.h"
57 #define log_debug_bus_message(m) \
59 sd_bus_message *_mm = (m); \
60 log_debug("Got message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " error=%s", \
61 bus_message_type_to_string(_mm->header->type), \
62 strna(sd_bus_message_get_sender(_mm)), \
63 strna(sd_bus_message_get_destination(_mm)), \
64 strna(sd_bus_message_get_path(_mm)), \
65 strna(sd_bus_message_get_interface(_mm)), \
66 strna(sd_bus_message_get_member(_mm)), \
67 BUS_MESSAGE_COOKIE(_mm), \
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 #if 0 /// UNNEEDED by elogind
78 static thread_local sd_bus *default_user_bus = NULL;
80 static thread_local sd_bus *default_starter_bus = NULL;
82 static void bus_close_fds(sd_bus *b) {
87 if (b->input_fd != b->output_fd)
88 safe_close(b->output_fd);
89 b->output_fd = b->input_fd = safe_close(b->input_fd);
92 static void bus_reset_queues(sd_bus *b) {
95 while (b->rqueue_size > 0)
96 sd_bus_message_unref(b->rqueue[--b->rqueue_size]);
98 b->rqueue = mfree(b->rqueue);
99 b->rqueue_allocated = 0;
101 while (b->wqueue_size > 0)
102 sd_bus_message_unref(b->wqueue[--b->wqueue_size]);
104 b->wqueue = mfree(b->wqueue);
105 b->wqueue_allocated = 0;
108 static void bus_free(sd_bus *b) {
112 assert(!b->track_queue);
115 b->state = BUS_CLOSED;
117 sd_bus_detach_event(b);
119 while ((s = b->slots)) {
120 /* At this point only floating slots can still be
121 * around, because the non-floating ones keep a
122 * reference to the bus, and we thus couldn't be
123 * destructing right now... We forcibly disconnect the
124 * slots here, so that they still can be referenced by
125 * apps, but are dead. */
128 bus_slot_disconnect(s);
129 sd_bus_slot_unref(s);
132 if (b->default_bus_ptr)
133 *b->default_bus_ptr = NULL;
139 free(b->unique_name);
140 free(b->auth_buffer);
143 free(b->cgroup_root);
144 free(b->description);
147 strv_free(b->exec_argv);
149 close_many(b->fds, b->n_fds);
154 ordered_hashmap_free_free(b->reply_callbacks);
155 prioq_free(b->reply_callbacks_prioq);
157 assert(b->match_callbacks.type == BUS_MATCH_ROOT);
158 bus_match_free(&b->match_callbacks);
160 hashmap_free_free(b->vtable_methods);
161 hashmap_free_free(b->vtable_properties);
163 assert(hashmap_isempty(b->nodes));
164 hashmap_free(b->nodes);
168 assert_se(pthread_mutex_destroy(&b->memfd_cache_mutex) == 0);
173 _public_ int sd_bus_new(sd_bus **ret) {
176 assert_return(ret, -EINVAL);
182 r->n_ref = REFCNT_INIT;
183 r->input_fd = r->output_fd = -1;
184 r->message_version = 1;
185 r->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
186 r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
187 r->attach_flags |= KDBUS_ATTACH_NAMES;
188 r->original_pid = getpid_cached();
190 assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0);
192 /* We guarantee that wqueue always has space for at least one
194 if (!GREEDY_REALLOC(r->wqueue, r->wqueue_allocated, 1)) {
203 _public_ int sd_bus_set_address(sd_bus *bus, const char *address) {
206 assert_return(bus, -EINVAL);
207 assert_return(bus->state == BUS_UNSET, -EPERM);
208 assert_return(address, -EINVAL);
209 assert_return(!bus_pid_changed(bus), -ECHILD);
221 _public_ int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
222 assert_return(bus, -EINVAL);
223 assert_return(bus->state == BUS_UNSET, -EPERM);
224 assert_return(input_fd >= 0, -EBADF);
225 assert_return(output_fd >= 0, -EBADF);
226 assert_return(!bus_pid_changed(bus), -ECHILD);
228 bus->input_fd = input_fd;
229 bus->output_fd = output_fd;
233 #if 0 /// UNNEEDED by elogind
234 _public_ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
237 assert_return(bus, -EINVAL);
238 assert_return(bus->state == BUS_UNSET, -EPERM);
239 assert_return(path, -EINVAL);
240 assert_return(!strv_isempty(argv), -EINVAL);
241 assert_return(!bus_pid_changed(bus), -ECHILD);
253 free(bus->exec_path);
254 strv_free(bus->exec_argv);
263 _public_ int sd_bus_set_bus_client(sd_bus *bus, int b) {
264 assert_return(bus, -EINVAL);
265 assert_return(bus->state == BUS_UNSET, -EPERM);
266 assert_return(!bus_pid_changed(bus), -ECHILD);
268 bus->bus_client = !!b;
272 #if 0 /// UNNEEDED by elogind
273 _public_ int sd_bus_set_monitor(sd_bus *bus, int b) {
274 assert_return(bus, -EINVAL);
275 assert_return(bus->state == BUS_UNSET, -EPERM);
276 assert_return(!bus_pid_changed(bus), -ECHILD);
278 SET_FLAG(bus->hello_flags, KDBUS_HELLO_MONITOR, b);
283 _public_ int sd_bus_negotiate_fds(sd_bus *bus, int b) {
284 assert_return(bus, -EINVAL);
285 assert_return(bus->state == BUS_UNSET, -EPERM);
286 assert_return(!bus_pid_changed(bus), -ECHILD);
288 SET_FLAG(bus->hello_flags, KDBUS_HELLO_ACCEPT_FD, b);
292 #if 0 /// UNNEEDED by elogind
293 _public_ int sd_bus_negotiate_timestamp(sd_bus *bus, int b) {
295 assert_return(bus, -EINVAL);
296 assert_return(!IN_SET(bus->state, BUS_CLOSING, BUS_CLOSED), -EPERM);
297 assert_return(!bus_pid_changed(bus), -ECHILD);
299 new_flags = bus->attach_flags;
300 SET_FLAG(new_flags, KDBUS_ATTACH_TIMESTAMP, b);
302 if (bus->attach_flags == new_flags)
305 bus->attach_flags = new_flags;
310 _public_ int sd_bus_negotiate_creds(sd_bus *bus, int b, uint64_t mask) {
313 assert_return(bus, -EINVAL);
314 assert_return(mask <= _SD_BUS_CREDS_ALL, -EINVAL);
315 assert_return(!IN_SET(bus->state, BUS_CLOSING, BUS_CLOSED), -EPERM);
316 assert_return(!bus_pid_changed(bus), -ECHILD);
318 SET_FLAG(bus->creds_mask, mask, b);
320 /* The well knowns we need unconditionally, so that matches can work */
321 bus->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
323 /* Make sure we don't lose the timestamp flag */
324 new_flags = (bus->attach_flags & KDBUS_ATTACH_TIMESTAMP) | attach_flags_to_kdbus(bus->creds_mask);
325 if (bus->attach_flags == new_flags)
328 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 #if 0 /// UNNEEDED by elogind
355 _public_ int sd_bus_set_trusted(sd_bus *bus, int b) {
356 assert_return(bus, -EINVAL);
357 assert_return(bus->state == BUS_UNSET, -EPERM);
358 assert_return(!bus_pid_changed(bus), -ECHILD);
364 _public_ int sd_bus_set_description(sd_bus *bus, const char *description) {
365 assert_return(bus, -EINVAL);
366 assert_return(bus->state == BUS_UNSET, -EPERM);
367 assert_return(!bus_pid_changed(bus), -ECHILD);
369 return free_and_strdup(&bus->description, description);
373 _public_ int sd_bus_set_allow_interactive_authorization(sd_bus *bus, int b) {
374 assert_return(bus, -EINVAL);
375 assert_return(!bus_pid_changed(bus), -ECHILD);
377 bus->allow_interactive_authorization = !!b;
381 #if 0 /// UNNEEDED by elogind
382 _public_ int sd_bus_get_allow_interactive_authorization(sd_bus *bus) {
383 assert_return(bus, -EINVAL);
384 assert_return(!bus_pid_changed(bus), -ECHILD);
386 return bus->allow_interactive_authorization;
390 static int hello_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
398 assert(bus->state == BUS_HELLO || bus->state == BUS_CLOSING);
400 r = sd_bus_message_get_errno(reply);
404 r = sd_bus_message_read(reply, "s", &s);
408 if (!service_name_is_valid(s) || s[0] != ':')
411 bus->unique_name = strdup(s);
412 if (!bus->unique_name)
415 if (bus->state == BUS_HELLO)
416 bus->state = BUS_RUNNING;
421 static int bus_send_hello(sd_bus *bus) {
422 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
427 if (!bus->bus_client)
430 r = sd_bus_message_new_method_call(
433 "org.freedesktop.DBus",
434 "/org/freedesktop/DBus",
435 "org.freedesktop.DBus",
440 return sd_bus_call_async(bus, NULL, m, hello_callback, NULL, 0);
443 int bus_start_running(sd_bus *bus) {
446 if (bus->bus_client) {
447 bus->state = BUS_HELLO;
451 bus->state = BUS_RUNNING;
455 static int parse_address_key(const char **p, const char *key, char **value) {
456 size_t l, n = 0, allocated = 0;
466 if (strncmp(*p, key, l) != 0)
479 while (*a != ';' && *a != ',' && *a != 0) {
497 c = (char) ((x << 4) | y);
504 if (!GREEDY_REALLOC(r, allocated, n + 2))
528 static void skip_address_key(const char **p) {
532 *p += strcspn(*p, ",");
538 static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
539 _cleanup_free_ char *path = NULL, *abstract = NULL;
548 while (**p != 0 && **p != ';') {
549 r = parse_address_key(p, "guid", guid);
555 r = parse_address_key(p, "path", &path);
561 r = parse_address_key(p, "abstract", &abstract);
570 if (!path && !abstract)
573 if (path && abstract)
578 if (l > sizeof(b->sockaddr.un.sun_path))
581 b->sockaddr.un.sun_family = AF_UNIX;
582 strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path));
583 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l;
584 } else if (abstract) {
585 l = strlen(abstract);
586 if (l > sizeof(b->sockaddr.un.sun_path) - 1)
589 b->sockaddr.un.sun_family = AF_UNIX;
590 b->sockaddr.un.sun_path[0] = 0;
591 strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1);
592 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
600 static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
601 _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
603 struct addrinfo *result, hints = {
604 .ai_socktype = SOCK_STREAM,
605 .ai_flags = AI_ADDRCONFIG,
613 while (**p != 0 && **p != ';') {
614 r = parse_address_key(p, "guid", guid);
620 r = parse_address_key(p, "host", &host);
626 r = parse_address_key(p, "port", &port);
632 r = parse_address_key(p, "family", &family);
645 if (streq(family, "ipv4"))
646 hints.ai_family = AF_INET;
647 else if (streq(family, "ipv6"))
648 hints.ai_family = AF_INET6;
653 r = getaddrinfo(host, port, &hints, &result);
657 return -EADDRNOTAVAIL;
659 memcpy(&b->sockaddr, result->ai_addr, result->ai_addrlen);
660 b->sockaddr_size = result->ai_addrlen;
662 freeaddrinfo(result);
669 static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
671 unsigned n_argv = 0, j;
673 size_t allocated = 0;
681 while (**p != 0 && **p != ';') {
682 r = parse_address_key(p, "guid", guid);
688 r = parse_address_key(p, "path", &path);
694 if (startswith(*p, "argv")) {
698 ul = strtoul(*p + 4, (char**) p, 10);
699 if (errno > 0 || **p != '=' || ul > 256) {
707 if (!GREEDY_REALLOC0(argv, allocated, ul + 2)) {
715 r = parse_address_key(p, NULL, argv + ul);
730 /* Make sure there are no holes in the array, with the
731 * exception of argv[0] */
732 for (j = 1; j < n_argv; j++)
738 if (argv && argv[0] == NULL) {
739 argv[0] = strdup(path);
754 for (j = 0; j < n_argv; j++)
762 static int parse_container_unix_address(sd_bus *b, const char **p, char **guid) {
763 _cleanup_free_ char *machine = NULL, *pid = NULL;
771 while (**p != 0 && **p != ';') {
772 r = parse_address_key(p, "guid", guid);
778 r = parse_address_key(p, "machine", &machine);
784 r = parse_address_key(p, "pid", &pid);
793 if (!machine == !pid)
797 if (!machine_name_is_valid(machine))
801 b->machine = machine;
804 b->machine = mfree(b->machine);
808 r = parse_pid(pid, &b->nspid);
814 b->sockaddr.un.sun_family = AF_UNIX;
815 strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
816 b->sockaddr_size = SOCKADDR_UN_LEN(b->sockaddr.un);
822 static void bus_reset_parsed_address(sd_bus *b) {
826 b->sockaddr_size = 0;
827 b->exec_argv = strv_free(b->exec_argv);
828 b->exec_path = mfree(b->exec_path);
829 b->server_id = SD_ID128_NULL;
830 b->machine = mfree(b->machine);
834 static int bus_parse_next_address(sd_bus *b) {
835 _cleanup_free_ char *guid = NULL;
843 if (b->address[b->address_index] == 0)
846 bus_reset_parsed_address(b);
848 a = b->address + b->address_index;
857 if (startswith(a, "unix:")) {
860 r = parse_unix_address(b, &a, &guid);
865 } else if (startswith(a, "tcp:")) {
868 r = parse_tcp_address(b, &a, &guid);
874 } else if (startswith(a, "unixexec:")) {
877 r = parse_exec_address(b, &a, &guid);
883 } else if (startswith(a, "x-machine-unix:")) {
886 r = parse_container_unix_address(b, &a, &guid);
899 r = sd_id128_from_string(guid, &b->server_id);
904 b->address_index = a - b->address;
908 static int bus_start_address(sd_bus *b) {
916 /* If you provide multiple different bus-addresses, we
917 * try all of them in order and use the first one that
921 r = bus_socket_exec(b);
923 else if ((b->nspid > 0 || b->machine) && b->sockaddr.sa.sa_family != AF_UNSPEC)
924 r = bus_container_connect_socket(b);
926 else if (b->sockaddr.sa.sa_family != AF_UNSPEC)
927 r = bus_socket_connect(b);
933 r = attach_io_events(b);
938 b->last_connect_error = -r;
941 r = bus_parse_next_address(b);
945 return b->last_connect_error > 0 ? -b->last_connect_error : -ECONNREFUSED;
949 int bus_next_address(sd_bus *b) {
952 bus_reset_parsed_address(b);
953 return bus_start_address(b);
956 static int bus_start_fd(sd_bus *b) {
961 assert(b->input_fd >= 0);
962 assert(b->output_fd >= 0);
964 r = fd_nonblock(b->input_fd, true);
968 r = fd_cloexec(b->input_fd, true);
972 if (b->input_fd != b->output_fd) {
973 r = fd_nonblock(b->output_fd, true);
977 r = fd_cloexec(b->output_fd, true);
982 if (fstat(b->input_fd, &st) < 0)
985 return bus_socket_take_fd(b);
988 _public_ int sd_bus_start(sd_bus *bus) {
991 assert_return(bus, -EINVAL);
992 assert_return(bus->state == BUS_UNSET, -EPERM);
993 assert_return(!bus_pid_changed(bus), -ECHILD);
995 bus->state = BUS_OPENING;
997 if (bus->is_server && bus->bus_client)
1000 if (bus->input_fd >= 0)
1001 r = bus_start_fd(bus);
1002 else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->machine)
1003 r = bus_start_address(bus);
1012 return bus_send_hello(bus);
1015 _public_ int sd_bus_open(sd_bus **ret) {
1020 assert_return(ret, -EINVAL);
1022 /* Let's connect to the starter bus if it is set, and
1023 * otherwise to the bus that is appropropriate for the scope
1024 * we are running in */
1026 e = secure_getenv("DBUS_STARTER_BUS_TYPE");
1028 if (streq(e, "system"))
1029 return sd_bus_open_system(ret);
1030 #if 0 /// elogind does not support systemd units
1031 else if (STR_IN_SET(e, "session", "user"))
1032 return sd_bus_open_user(ret);
1036 e = secure_getenv("DBUS_STARTER_ADDRESS");
1038 #if 0 /// elogind does not support systemd units
1039 if (cg_pid_get_owner_uid(0, NULL) >= 0)
1040 return sd_bus_open_user(ret);
1043 return sd_bus_open_system(ret);
1050 r = sd_bus_set_address(b, e);
1054 b->bus_client = true;
1056 /* We don't know whether the bus is trusted or not, so better
1057 * be safe, and authenticate everything */
1059 b->is_local = false;
1060 b->attach_flags |= KDBUS_ATTACH_CAPS | KDBUS_ATTACH_CREDS;
1061 b->creds_mask |= SD_BUS_CREDS_UID | SD_BUS_CREDS_EUID | SD_BUS_CREDS_EFFECTIVE_CAPS;
1063 r = sd_bus_start(b);
1075 int bus_set_address_system(sd_bus *b) {
1079 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1081 return sd_bus_set_address(b, e);
1083 return sd_bus_set_address(b, DEFAULT_SYSTEM_BUS_ADDRESS);
1086 _public_ int sd_bus_open_system(sd_bus **ret) {
1090 assert_return(ret, -EINVAL);
1096 r = bus_set_address_system(b);
1100 b->bus_client = true;
1101 b->is_system = true;
1103 /* Let's do per-method access control on the system bus. We
1104 * need the caller's UID and capability set for that. */
1106 b->attach_flags |= KDBUS_ATTACH_CAPS | KDBUS_ATTACH_CREDS;
1107 b->creds_mask |= SD_BUS_CREDS_UID | SD_BUS_CREDS_EUID | SD_BUS_CREDS_EFFECTIVE_CAPS;
1110 r = sd_bus_start(b);
1122 #if 0 /// elogind can not open/use a user bus
1123 int bus_set_address_user(sd_bus *b) {
1125 _cleanup_free_ char *ee = NULL, *s = NULL;
1129 e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1131 return sd_bus_set_address(b, e);
1133 e = secure_getenv("XDG_RUNTIME_DIR");
1137 ee = bus_address_escape(e);
1141 if (asprintf(&s, UNIX_USER_BUS_ADDRESS_FMT, ee) < 0)
1151 _public_ int sd_bus_open_user(sd_bus **ret) {
1152 #if 0 /// elogind does not support user buses
1156 assert_return(ret, -EINVAL);
1162 r = bus_set_address_user(b);
1166 b->bus_client = true;
1169 /* We don't do any per-method access control on the user
1174 r = sd_bus_start(b);
1185 return sd_bus_open_system(ret);
1189 int bus_set_address_system_remote(sd_bus *b, const char *host) {
1190 _cleanup_free_ char *e = NULL;
1191 char *m = NULL, *c = NULL;
1196 /* Let's see if we shall enter some container */
1197 m = strchr(host, ':');
1201 /* Let's make sure this is not a port of some kind,
1202 * and is a valid machine name. */
1203 if (!in_charset(m, "0123456789") && machine_name_is_valid(m)) {
1206 /* Cut out the host part */
1207 t = strndupa(host, m - host - 1);
1208 e = bus_address_escape(t);
1212 c = strjoina(",argv5=--machine=", m);
1217 e = bus_address_escape(host);
1222 b->address = strjoin("unixexec:path=ssh,argv1=-xT,argv2=--,argv3=", e, ",argv4=systemd-stdio-bridge", c);
1229 _public_ int sd_bus_open_system_remote(sd_bus **ret, const char *host) {
1233 assert_return(host, -EINVAL);
1234 assert_return(ret, -EINVAL);
1236 r = sd_bus_new(&bus);
1240 r = bus_set_address_system_remote(bus, host);
1244 bus->bus_client = true;
1245 bus->trusted = false;
1246 bus->is_system = true;
1247 bus->is_local = false;
1249 r = sd_bus_start(bus);
1261 int bus_set_address_system_machine(sd_bus *b, const char *machine) {
1262 _cleanup_free_ char *e = NULL;
1267 e = bus_address_escape(machine);
1271 b->address = strjoin("x-machine-unix:machine=", e);
1278 _public_ int sd_bus_open_system_machine(sd_bus **ret, const char *machine) {
1282 assert_return(machine, -EINVAL);
1283 assert_return(ret, -EINVAL);
1284 assert_return(machine_name_is_valid(machine), -EINVAL);
1286 r = sd_bus_new(&bus);
1290 r = bus_set_address_system_machine(bus, machine);
1294 bus->bus_client = true;
1295 bus->trusted = false;
1296 bus->is_system = true;
1297 bus->is_local = false;
1299 r = sd_bus_start(bus);
1311 _public_ void sd_bus_close(sd_bus *bus) {
1315 if (bus->state == BUS_CLOSED)
1317 if (bus_pid_changed(bus))
1320 bus->state = BUS_CLOSED;
1322 sd_bus_detach_event(bus);
1324 /* Drop all queued messages so that they drop references to
1325 * the bus object and the bus may be freed */
1326 bus_reset_queues(bus);
1331 _public_ sd_bus* sd_bus_flush_close_unref(sd_bus *bus) {
1339 return sd_bus_unref(bus);
1342 static void bus_enter_closing(sd_bus *bus) {
1345 if (bus->state != BUS_OPENING &&
1346 bus->state != BUS_AUTHENTICATING &&
1347 bus->state != BUS_HELLO &&
1348 bus->state != BUS_RUNNING)
1351 bus->state = BUS_CLOSING;
1354 _public_ sd_bus *sd_bus_ref(sd_bus *bus) {
1359 assert_se(REFCNT_INC(bus->n_ref) >= 2);
1364 _public_ sd_bus *sd_bus_unref(sd_bus *bus) {
1370 i = REFCNT_DEC(bus->n_ref);
1378 #if 0 /// UNNEEDED by elogind
1379 _public_ int sd_bus_is_open(sd_bus *bus) {
1381 assert_return(bus, -EINVAL);
1382 assert_return(!bus_pid_changed(bus), -ECHILD);
1384 return BUS_IS_OPEN(bus->state);
1388 _public_ int sd_bus_can_send(sd_bus *bus, char type) {
1391 assert_return(bus, -EINVAL);
1392 assert_return(bus->state != BUS_UNSET, -ENOTCONN);
1393 assert_return(!bus_pid_changed(bus), -ECHILD);
1395 if (bus->hello_flags & KDBUS_HELLO_MONITOR)
1398 if (type == SD_BUS_TYPE_UNIX_FD) {
1399 if (!(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD))
1402 r = bus_ensure_running(bus);
1406 return bus->can_fds;
1409 return bus_type_is_valid(type);
1412 #if 0 /// UNNEEDED by elogind
1413 _public_ int sd_bus_get_bus_id(sd_bus *bus, sd_id128_t *id) {
1416 assert_return(bus, -EINVAL);
1417 assert_return(id, -EINVAL);
1418 assert_return(!bus_pid_changed(bus), -ECHILD);
1420 r = bus_ensure_running(bus);
1424 *id = bus->server_id;
1429 static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
1434 /* If we copy the same message to multiple
1435 * destinations, avoid using the same cookie
1437 b->cookie = MAX(b->cookie, BUS_MESSAGE_COOKIE(m));
1442 timeout = BUS_DEFAULT_TIMEOUT;
1444 return bus_message_seal(m, ++b->cookie, timeout);
1447 static int bus_remarshal_message(sd_bus *b, sd_bus_message **m) {
1448 bool remarshal = false;
1452 /* wrong packet version */
1453 if (b->message_version != 0 && b->message_version != (*m)->header->version)
1456 /* wrong packet endianness */
1457 if (b->message_endian != 0 && b->message_endian != (*m)->header->endian)
1460 return remarshal ? bus_message_remarshal(b, m) : 0;
1463 int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
1467 /* Fake some timestamps, if they were requested, and not
1468 * already initialized */
1469 if (b->attach_flags & KDBUS_ATTACH_TIMESTAMP) {
1470 if (m->realtime <= 0)
1471 m->realtime = now(CLOCK_REALTIME);
1473 if (m->monotonic <= 0)
1474 m->monotonic = now(CLOCK_MONOTONIC);
1477 /* The bus specification says the serial number cannot be 0,
1478 * hence let's fill something in for synthetic messages. Since
1479 * synthetic messages might have a fake sender and we don't
1480 * want to interfere with the real sender's serial numbers we
1481 * pick a fixed, artificial one. We use (uint32_t) -1 rather
1482 * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
1483 * even though kdbus can do 64bit. */
1484 return bus_message_seal(m, 0xFFFFFFFFULL, 0);
1487 static int bus_write_message(sd_bus *bus, sd_bus_message *m, bool hint_sync_call, size_t *idx) {
1493 r = bus_socket_write_message(bus, m, idx);
1497 if (*idx >= BUS_MESSAGE_SIZE(m))
1498 log_debug("Sent message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " error=%s",
1499 bus_message_type_to_string(m->header->type),
1500 strna(sd_bus_message_get_sender(m)),
1501 strna(sd_bus_message_get_destination(m)),
1502 strna(sd_bus_message_get_path(m)),
1503 strna(sd_bus_message_get_interface(m)),
1504 strna(sd_bus_message_get_member(m)),
1505 BUS_MESSAGE_COOKIE(m),
1507 strna(m->error.message));
1512 static int dispatch_wqueue(sd_bus *bus) {
1516 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1518 while (bus->wqueue_size > 0) {
1520 r = bus_write_message(bus, bus->wqueue[0], false, &bus->windex);
1524 /* Didn't do anything this time */
1526 else if (bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
1527 /* Fully written. Let's drop the entry from
1530 * This isn't particularly optimized, but
1531 * well, this is supposed to be our worst-case
1532 * buffer only, and the socket buffer is
1533 * supposed to be our primary buffer, and if
1534 * it got full, then all bets are off
1538 sd_bus_message_unref(bus->wqueue[0]);
1539 memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
1549 static int bus_read_message(sd_bus *bus, bool hint_priority, int64_t priority) {
1552 return bus_socket_read_message(bus);
1555 int bus_rqueue_make_room(sd_bus *bus) {
1558 if (bus->rqueue_size >= BUS_RQUEUE_MAX)
1561 if (!GREEDY_REALLOC(bus->rqueue, bus->rqueue_allocated, bus->rqueue_size + 1))
1567 static int dispatch_rqueue(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **m) {
1572 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1574 /* Note that the priority logic is only available on kdbus,
1575 * where the rqueue is unused. We check the rqueue here
1576 * anyway, because it's simple... */
1579 if (bus->rqueue_size > 0) {
1580 /* Dispatch a queued message */
1582 *m = bus->rqueue[0];
1584 memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
1588 /* Try to read a new message */
1589 r = bus_read_message(bus, hint_priority, priority);
1599 static int bus_send_internal(sd_bus *bus, sd_bus_message *_m, uint64_t *cookie, bool hint_sync_call) {
1600 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = sd_bus_message_ref(_m);
1603 assert_return(m, -EINVAL);
1608 assert_return(!bus_pid_changed(bus), -ECHILD);
1610 if (!BUS_IS_OPEN(bus->state))
1614 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
1621 /* If the cookie number isn't kept, then we know that no reply
1623 if (!cookie && !m->sealed)
1624 m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
1626 r = bus_seal_message(bus, m, 0);
1630 /* Remarshall if we have to. This will possibly unref the
1631 * message and place a replacement in m */
1632 r = bus_remarshal_message(bus, &m);
1636 /* If this is a reply and no reply was requested, then let's
1637 * suppress this, if we can */
1641 if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
1644 r = bus_write_message(bus, m, hint_sync_call, &idx);
1646 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
1647 bus_enter_closing(bus);
1654 if (idx < BUS_MESSAGE_SIZE(m)) {
1655 /* Wasn't fully written. So let's remember how
1656 * much was written. Note that the first entry
1657 * of the wqueue array is always allocated so
1658 * that we always can remember how much was
1660 bus->wqueue[0] = sd_bus_message_ref(m);
1661 bus->wqueue_size = 1;
1666 /* Just append it to the queue. */
1668 if (bus->wqueue_size >= BUS_WQUEUE_MAX)
1671 if (!GREEDY_REALLOC(bus->wqueue, bus->wqueue_allocated, bus->wqueue_size + 1))
1674 bus->wqueue[bus->wqueue_size++] = sd_bus_message_ref(m);
1679 *cookie = BUS_MESSAGE_COOKIE(m);
1684 _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *cookie) {
1685 return bus_send_internal(bus, m, cookie, false);
1688 #if 0 /// UNNEEDED by elogind
1689 _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *cookie) {
1692 assert_return(m, -EINVAL);
1697 assert_return(!bus_pid_changed(bus), -ECHILD);
1699 if (!BUS_IS_OPEN(bus->state))
1702 if (!streq_ptr(m->destination, destination)) {
1707 r = sd_bus_message_set_destination(m, destination);
1712 return sd_bus_send(bus, m, cookie);
1716 static usec_t calc_elapse(uint64_t usec) {
1717 if (usec == (uint64_t) -1)
1720 return now(CLOCK_MONOTONIC) + usec;
1723 static int timeout_compare(const void *a, const void *b) {
1724 const struct reply_callback *x = a, *y = b;
1726 if (x->timeout != 0 && y->timeout == 0)
1729 if (x->timeout == 0 && y->timeout != 0)
1732 if (x->timeout < y->timeout)
1735 if (x->timeout > y->timeout)
1741 _public_ int sd_bus_call_async(
1745 sd_bus_message_handler_t callback,
1749 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = sd_bus_message_ref(_m);
1750 _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *s = NULL;
1753 assert_return(m, -EINVAL);
1754 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1755 assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1756 assert_return(callback, -EINVAL);
1761 assert_return(!bus_pid_changed(bus), -ECHILD);
1763 if (!BUS_IS_OPEN(bus->state))
1766 r = ordered_hashmap_ensure_allocated(&bus->reply_callbacks, &uint64_hash_ops);
1770 r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
1774 r = bus_seal_message(bus, m, usec);
1778 r = bus_remarshal_message(bus, &m);
1782 s = bus_slot_allocate(bus, !slot, BUS_REPLY_CALLBACK, sizeof(struct reply_callback), userdata);
1786 s->reply_callback.callback = callback;
1788 s->reply_callback.cookie = BUS_MESSAGE_COOKIE(m);
1789 r = ordered_hashmap_put(bus->reply_callbacks, &s->reply_callback.cookie, &s->reply_callback);
1791 s->reply_callback.cookie = 0;
1795 s->reply_callback.timeout = calc_elapse(m->timeout);
1796 if (s->reply_callback.timeout != 0) {
1797 r = prioq_put(bus->reply_callbacks_prioq, &s->reply_callback, &s->reply_callback.prioq_idx);
1799 s->reply_callback.timeout = 0;
1804 r = sd_bus_send(bus, m, &s->reply_callback.cookie);
1815 int bus_ensure_running(sd_bus *bus) {
1820 if (bus->state == BUS_UNSET || bus->state == BUS_CLOSED || bus->state == BUS_CLOSING)
1822 if (bus->state == BUS_RUNNING)
1826 r = sd_bus_process(bus, NULL);
1829 if (bus->state == BUS_RUNNING)
1834 r = sd_bus_wait(bus, (uint64_t) -1);
1840 _public_ int sd_bus_call(
1844 sd_bus_error *error,
1845 sd_bus_message **reply) {
1847 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = sd_bus_message_ref(_m);
1853 bus_assert_return(m, -EINVAL, error);
1854 bus_assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL, error);
1855 bus_assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL, error);
1856 bus_assert_return(!bus_error_is_dirty(error), -EINVAL, error);
1861 bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
1863 if (!BUS_IS_OPEN(bus->state)) {
1868 r = bus_ensure_running(bus);
1872 i = bus->rqueue_size;
1874 r = bus_seal_message(bus, m, usec);
1878 r = bus_remarshal_message(bus, &m);
1882 r = bus_send_internal(bus, m, &cookie, true);
1886 timeout = calc_elapse(m->timeout);
1891 while (i < bus->rqueue_size) {
1892 sd_bus_message *incoming = NULL;
1894 incoming = bus->rqueue[i];
1896 if (incoming->reply_cookie == cookie) {
1897 /* Found a match! */
1899 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1901 log_debug_bus_message(incoming);
1903 if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
1905 if (incoming->n_fds <= 0 || (bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)) {
1909 sd_bus_message_unref(incoming);
1914 r = sd_bus_error_setf(error, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptors which I couldn't accept. Sorry.");
1915 sd_bus_message_unref(incoming);
1918 } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR) {
1919 r = sd_bus_error_copy(error, &incoming->error);
1920 sd_bus_message_unref(incoming);
1927 } else if (BUS_MESSAGE_COOKIE(incoming) == cookie &&
1930 streq(bus->unique_name, incoming->sender)) {
1932 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1935 /* Our own message? Somebody is trying
1936 * to send its own client a message,
1937 * let's not dead-lock, let's fail
1940 sd_bus_message_unref(incoming);
1945 /* Try to read more, right-away */
1949 r = bus_read_message(bus, false, 0);
1951 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
1952 bus_enter_closing(bus);
1964 n = now(CLOCK_MONOTONIC);
1972 left = (uint64_t) -1;
1974 r = bus_poll(bus, true, left);
1982 r = dispatch_wqueue(bus);
1984 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
1985 bus_enter_closing(bus);
1994 return sd_bus_error_set_errno(error, r);
1997 #if 0 /// UNNEEDED by elogind
1998 _public_ int sd_bus_get_fd(sd_bus *bus) {
2000 assert_return(bus, -EINVAL);
2001 assert_return(bus->input_fd == bus->output_fd, -EPERM);
2002 assert_return(!bus_pid_changed(bus), -ECHILD);
2004 return bus->input_fd;
2008 _public_ int sd_bus_get_events(sd_bus *bus) {
2011 assert_return(bus, -EINVAL);
2012 assert_return(!bus_pid_changed(bus), -ECHILD);
2014 if (!BUS_IS_OPEN(bus->state) && bus->state != BUS_CLOSING)
2017 if (bus->state == BUS_OPENING)
2019 else if (bus->state == BUS_AUTHENTICATING) {
2021 if (bus_socket_auth_needs_write(bus))
2026 } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
2027 if (bus->rqueue_size <= 0)
2029 if (bus->wqueue_size > 0)
2036 _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
2037 struct reply_callback *c;
2039 assert_return(bus, -EINVAL);
2040 assert_return(timeout_usec, -EINVAL);
2041 assert_return(!bus_pid_changed(bus), -ECHILD);
2043 if (!BUS_IS_OPEN(bus->state) && bus->state != BUS_CLOSING)
2046 if (bus->track_queue) {
2051 if (bus->state == BUS_CLOSING) {
2056 if (bus->state == BUS_AUTHENTICATING) {
2057 *timeout_usec = bus->auth_timeout;
2061 if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) {
2062 *timeout_usec = (uint64_t) -1;
2066 if (bus->rqueue_size > 0) {
2071 c = prioq_peek(bus->reply_callbacks_prioq);
2073 *timeout_usec = (uint64_t) -1;
2077 if (c->timeout == 0) {
2078 *timeout_usec = (uint64_t) -1;
2082 *timeout_usec = c->timeout;
2086 static int process_timeout(sd_bus *bus) {
2087 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2088 _cleanup_(sd_bus_message_unrefp) sd_bus_message* m = NULL;
2089 struct reply_callback *c;
2096 c = prioq_peek(bus->reply_callbacks_prioq);
2100 n = now(CLOCK_MONOTONIC);
2104 r = bus_message_new_synthetic_error(
2107 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
2112 r = bus_seal_synthetic_message(bus, m);
2116 assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
2119 ordered_hashmap_remove(bus->reply_callbacks, &c->cookie);
2122 slot = container_of(c, sd_bus_slot, reply_callback);
2124 bus->iteration_counter++;
2126 bus->current_message = m;
2127 bus->current_slot = sd_bus_slot_ref(slot);
2128 bus->current_handler = c->callback;
2129 bus->current_userdata = slot->userdata;
2130 r = c->callback(m, slot->userdata, &error_buffer);
2131 bus->current_userdata = NULL;
2132 bus->current_handler = NULL;
2133 bus->current_slot = NULL;
2134 bus->current_message = NULL;
2136 if (slot->floating) {
2137 bus_slot_disconnect(slot);
2138 sd_bus_slot_unref(slot);
2141 sd_bus_slot_unref(slot);
2143 return bus_maybe_reply_error(m, r, &error_buffer);
2146 static int process_hello(sd_bus *bus, sd_bus_message *m) {
2150 if (bus->state != BUS_HELLO)
2153 /* Let's make sure the first message on the bus is the HELLO
2154 * reply. But note that we don't actually parse the message
2155 * here (we leave that to the usual handling), we just verify
2156 * we don't let any earlier msg through. */
2158 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
2159 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
2162 if (m->reply_cookie != 1)
2168 static int process_reply(sd_bus *bus, sd_bus_message *m) {
2169 _cleanup_(sd_bus_message_unrefp) sd_bus_message *synthetic_reply = NULL;
2170 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2171 struct reply_callback *c;
2178 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
2179 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
2182 if (m->destination && bus->unique_name && !streq_ptr(m->destination, bus->unique_name))
2185 c = ordered_hashmap_remove(bus->reply_callbacks, &m->reply_cookie);
2191 slot = container_of(c, sd_bus_slot, reply_callback);
2193 if (m->n_fds > 0 && !(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)) {
2195 /* If the reply contained a file descriptor which we
2196 * didn't want we pass an error instead. */
2198 r = bus_message_new_synthetic_error(
2201 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptor"),
2206 /* Copy over original timestamp */
2207 synthetic_reply->realtime = m->realtime;
2208 synthetic_reply->monotonic = m->monotonic;
2209 synthetic_reply->seqnum = m->seqnum;
2211 r = bus_seal_synthetic_message(bus, synthetic_reply);
2215 m = synthetic_reply;
2217 r = sd_bus_message_rewind(m, true);
2222 if (c->timeout != 0) {
2223 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2227 bus->current_slot = sd_bus_slot_ref(slot);
2228 bus->current_handler = c->callback;
2229 bus->current_userdata = slot->userdata;
2230 r = c->callback(m, slot->userdata, &error_buffer);
2231 bus->current_userdata = NULL;
2232 bus->current_handler = NULL;
2233 bus->current_slot = NULL;
2235 if (slot->floating) {
2236 bus_slot_disconnect(slot);
2237 sd_bus_slot_unref(slot);
2240 sd_bus_slot_unref(slot);
2242 return bus_maybe_reply_error(m, r, &error_buffer);
2245 static int process_filter(sd_bus *bus, sd_bus_message *m) {
2246 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2247 struct filter_callback *l;
2254 bus->filter_callbacks_modified = false;
2256 LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
2259 if (bus->filter_callbacks_modified)
2262 /* Don't run this more than once per iteration */
2263 if (l->last_iteration == bus->iteration_counter)
2266 l->last_iteration = bus->iteration_counter;
2268 r = sd_bus_message_rewind(m, true);
2272 slot = container_of(l, sd_bus_slot, filter_callback);
2274 bus->current_slot = sd_bus_slot_ref(slot);
2275 bus->current_handler = l->callback;
2276 bus->current_userdata = slot->userdata;
2277 r = l->callback(m, slot->userdata, &error_buffer);
2278 bus->current_userdata = NULL;
2279 bus->current_handler = NULL;
2280 bus->current_slot = sd_bus_slot_unref(slot);
2282 r = bus_maybe_reply_error(m, r, &error_buffer);
2288 } while (bus->filter_callbacks_modified);
2293 static int process_match(sd_bus *bus, sd_bus_message *m) {
2300 bus->match_callbacks_modified = false;
2302 r = bus_match_run(bus, &bus->match_callbacks, m);
2306 } while (bus->match_callbacks_modified);
2311 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
2312 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
2318 if (bus->hello_flags & KDBUS_HELLO_MONITOR)
2321 if (bus->manual_peer_interface)
2324 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
2327 if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
2330 if (m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
2333 if (streq_ptr(m->member, "Ping"))
2334 r = sd_bus_message_new_method_return(m, &reply);
2335 else if (streq_ptr(m->member, "GetMachineId")) {
2339 r = sd_id128_get_machine(&id);
2343 r = sd_bus_message_new_method_return(m, &reply);
2347 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
2349 r = sd_bus_message_new_method_errorf(
2351 SD_BUS_ERROR_UNKNOWN_METHOD,
2352 "Unknown method '%s' on interface '%s'.", m->member, m->interface);
2358 r = sd_bus_send(bus, reply, NULL);
2365 static int process_fd_check(sd_bus *bus, sd_bus_message *m) {
2369 /* If we got a message with a file descriptor which we didn't
2370 * want to accept, then let's drop it. How can this even
2371 * happen? For example, when the kernel queues a message into
2372 * an activatable names's queue which allows fds, and then is
2373 * delivered to us later even though we ourselves did not
2376 if (bus->hello_flags & KDBUS_HELLO_MONITOR)
2382 if (bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)
2385 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
2386 return 1; /* just eat it up */
2388 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Message contains file descriptors, which I cannot accept. Sorry.");
2391 static int process_message(sd_bus *bus, sd_bus_message *m) {
2397 bus->current_message = m;
2398 bus->iteration_counter++;
2400 log_debug_bus_message(m);
2402 r = process_hello(bus, m);
2406 r = process_reply(bus, m);
2410 r = process_fd_check(bus, m);
2414 r = process_filter(bus, m);
2418 r = process_match(bus, m);
2422 r = process_builtin(bus, m);
2426 r = bus_process_object(bus, m);
2429 bus->current_message = NULL;
2433 static int dispatch_track(sd_bus *bus) {
2436 if (!bus->track_queue)
2439 bus_track_dispatch(bus->track_queue);
2443 static int process_running(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) {
2444 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2448 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
2450 r = process_timeout(bus);
2454 r = dispatch_wqueue(bus);
2458 r = dispatch_track(bus);
2462 r = dispatch_rqueue(bus, hint_priority, priority, &m);
2468 r = process_message(bus, m);
2473 r = sd_bus_message_rewind(m, true);
2482 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
2484 log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s",
2485 strna(sd_bus_message_get_sender(m)),
2486 strna(sd_bus_message_get_path(m)),
2487 strna(sd_bus_message_get_interface(m)),
2488 strna(sd_bus_message_get_member(m)));
2490 r = sd_bus_reply_method_errorf(
2492 SD_BUS_ERROR_UNKNOWN_OBJECT,
2493 "Unknown object '%s'.", m->path);
2507 static int bus_exit_now(sd_bus *bus) {
2510 /* Exit due to close, if this is requested. If this is bus object is attached to an event source, invokes
2511 * sd_event_exit(), otherwise invokes libc exit(). */
2513 if (bus->exited) /* did we already exit? */
2515 if (!bus->exit_triggered) /* was the exit condition triggered? */
2517 if (!bus->exit_on_disconnect) /* Shall we actually exit on disconnection? */
2520 bus->exited = true; /* never exit more than once */
2522 log_debug("Bus connection disconnected, exiting.");
2525 return sd_event_exit(bus->event, EXIT_FAILURE);
2529 assert_not_reached("exit() didn't exit?");
2532 static int process_closing_reply_callback(sd_bus *bus, struct reply_callback *c) {
2533 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2534 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2541 r = bus_message_new_synthetic_error(
2544 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
2549 r = bus_seal_synthetic_message(bus, m);
2553 if (c->timeout != 0) {
2554 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2558 ordered_hashmap_remove(bus->reply_callbacks, &c->cookie);
2561 slot = container_of(c, sd_bus_slot, reply_callback);
2563 bus->iteration_counter++;
2565 bus->current_message = m;
2566 bus->current_slot = sd_bus_slot_ref(slot);
2567 bus->current_handler = c->callback;
2568 bus->current_userdata = slot->userdata;
2569 r = c->callback(m, slot->userdata, &error_buffer);
2570 bus->current_userdata = NULL;
2571 bus->current_handler = NULL;
2572 bus->current_slot = NULL;
2573 bus->current_message = NULL;
2575 if (slot->floating) {
2576 bus_slot_disconnect(slot);
2577 sd_bus_slot_unref(slot);
2580 sd_bus_slot_unref(slot);
2582 return bus_maybe_reply_error(m, r, &error_buffer);
2585 static int process_closing(sd_bus *bus, sd_bus_message **ret) {
2586 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2587 struct reply_callback *c;
2591 assert(bus->state == BUS_CLOSING);
2593 /* First, fail all outstanding method calls */
2594 c = ordered_hashmap_first(bus->reply_callbacks);
2596 return process_closing_reply_callback(bus, c);
2598 /* Then, fake-drop all remaining bus tracking references */
2600 bus_track_close(bus->tracks);
2604 /* Then, synthesize a Disconnected message */
2605 r = sd_bus_message_new_signal(
2608 "/org/freedesktop/DBus/Local",
2609 "org.freedesktop.DBus.Local",
2614 bus_message_set_sender_local(bus, m);
2616 r = bus_seal_synthetic_message(bus, m);
2622 bus->current_message = m;
2623 bus->iteration_counter++;
2625 r = process_filter(bus, m);
2629 r = process_match(bus, m);
2633 /* Nothing else to do, exit now, if the condition holds */
2634 bus->exit_triggered = true;
2635 (void) bus_exit_now(bus);
2645 bus->current_message = NULL;
2650 static int bus_process_internal(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) {
2651 BUS_DONT_DESTROY(bus);
2654 /* Returns 0 when we didn't do anything. This should cause the
2655 * caller to invoke sd_bus_wait() before returning the next
2656 * time. Returns > 0 when we did something, which possibly
2657 * means *ret is filled in with an unprocessed message. */
2659 assert_return(bus, -EINVAL);
2660 assert_return(!bus_pid_changed(bus), -ECHILD);
2662 /* We don't allow recursively invoking sd_bus_process(). */
2663 assert_return(!bus->current_message, -EBUSY);
2664 assert(!bus->current_slot);
2666 switch (bus->state) {
2675 r = bus_socket_process_opening(bus);
2676 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
2677 bus_enter_closing(bus);
2685 case BUS_AUTHENTICATING:
2686 r = bus_socket_process_authenticating(bus);
2687 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
2688 bus_enter_closing(bus);
2700 r = process_running(bus, hint_priority, priority, ret);
2701 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
2702 bus_enter_closing(bus);
2712 return process_closing(bus, ret);
2715 assert_not_reached("Unknown state");
2718 _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
2719 return bus_process_internal(bus, false, 0, ret);
2722 #if 0 /// UNNEEDED by elogind
2723 _public_ int sd_bus_process_priority(sd_bus *bus, int64_t priority, sd_bus_message **ret) {
2724 return bus_process_internal(bus, true, priority, ret);
2728 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
2729 struct pollfd p[2] = {};
2732 usec_t m = USEC_INFINITY;
2736 if (bus->state == BUS_CLOSING)
2739 if (!BUS_IS_OPEN(bus->state))
2742 e = sd_bus_get_events(bus);
2747 /* The caller really needs some more data, he doesn't
2748 * care about what's already read, or any timeouts
2749 * except its own. */
2753 /* The caller wants to process if there's something to
2754 * process, but doesn't care otherwise */
2756 r = sd_bus_get_timeout(bus, &until);
2761 nw = now(CLOCK_MONOTONIC);
2762 m = until > nw ? until - nw : 0;
2766 if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
2769 p[0].fd = bus->input_fd;
2770 if (bus->output_fd == bus->input_fd) {
2774 p[0].events = e & POLLIN;
2775 p[1].fd = bus->output_fd;
2776 p[1].events = e & POLLOUT;
2780 r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
2784 return r > 0 ? 1 : 0;
2787 _public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
2789 assert_return(bus, -EINVAL);
2790 assert_return(!bus_pid_changed(bus), -ECHILD);
2792 if (bus->state == BUS_CLOSING)
2795 if (!BUS_IS_OPEN(bus->state))
2798 if (bus->rqueue_size > 0)
2801 return bus_poll(bus, false, timeout_usec);
2804 _public_ int sd_bus_flush(sd_bus *bus) {
2807 assert_return(bus, -EINVAL);
2808 assert_return(!bus_pid_changed(bus), -ECHILD);
2810 if (bus->state == BUS_CLOSING)
2813 if (!BUS_IS_OPEN(bus->state))
2816 r = bus_ensure_running(bus);
2820 if (bus->wqueue_size <= 0)
2824 r = dispatch_wqueue(bus);
2826 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
2827 bus_enter_closing(bus);
2834 if (bus->wqueue_size <= 0)
2837 r = bus_poll(bus, false, (uint64_t) -1);
2843 #if 0 /// UNNEEDED by elogind
2844 _public_ int sd_bus_add_filter(
2847 sd_bus_message_handler_t callback,
2852 assert_return(bus, -EINVAL);
2853 assert_return(callback, -EINVAL);
2854 assert_return(!bus_pid_changed(bus), -ECHILD);
2856 s = bus_slot_allocate(bus, !slot, BUS_FILTER_CALLBACK, sizeof(struct filter_callback), userdata);
2860 s->filter_callback.callback = callback;
2862 bus->filter_callbacks_modified = true;
2863 LIST_PREPEND(callbacks, bus->filter_callbacks, &s->filter_callback);
2872 _public_ int sd_bus_add_match(
2876 sd_bus_message_handler_t callback,
2879 struct bus_match_component *components = NULL;
2880 unsigned n_components = 0;
2881 sd_bus_slot *s = NULL;
2884 assert_return(bus, -EINVAL);
2885 assert_return(match, -EINVAL);
2886 assert_return(!bus_pid_changed(bus), -ECHILD);
2888 r = bus_match_parse(match, &components, &n_components);
2892 s = bus_slot_allocate(bus, !slot, BUS_MATCH_CALLBACK, sizeof(struct match_callback), userdata);
2898 s->match_callback.callback = callback;
2899 s->match_callback.cookie = ++bus->match_cookie;
2901 if (bus->bus_client) {
2902 enum bus_match_scope scope;
2904 scope = bus_match_get_scope(components, n_components);
2906 /* Do not install server-side matches for matches
2907 * against the local service, interface or bus path. */
2908 if (scope != BUS_MATCH_LOCAL) {
2910 /* We store the original match string, so that
2911 * we can use it to remove the match again. */
2913 s->match_callback.match_string = strdup(match);
2914 if (!s->match_callback.match_string) {
2919 r = bus_add_match_internal(bus, s->match_callback.match_string, components, n_components, s->match_callback.cookie);
2923 s->match_added = true;
2927 bus->match_callbacks_modified = true;
2928 r = bus_match_add(&bus->match_callbacks, components, n_components, &s->match_callback);
2937 bus_match_parse_free(components, n_components);
2938 sd_bus_slot_unref(s);
2943 #if 0 /// UNNEEDED by elogind
2944 int bus_remove_match_by_string(
2947 sd_bus_message_handler_t callback,
2950 struct bus_match_component *components = NULL;
2951 unsigned n_components = 0;
2952 struct match_callback *c;
2955 assert_return(bus, -EINVAL);
2956 assert_return(match, -EINVAL);
2957 assert_return(!bus_pid_changed(bus), -ECHILD);
2959 r = bus_match_parse(match, &components, &n_components);
2963 r = bus_match_find(&bus->match_callbacks, components, n_components, NULL, NULL, &c);
2967 sd_bus_slot_unref(container_of(c, sd_bus_slot, match_callback));
2970 bus_match_parse_free(components, n_components);
2976 bool bus_pid_changed(sd_bus *bus) {
2979 /* We don't support people creating a bus connection and
2980 * keeping it around over a fork(). Let's complain. */
2982 return bus->original_pid != getpid_cached();
2985 static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2986 sd_bus *bus = userdata;
2991 r = sd_bus_process(bus, NULL);
2998 static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
2999 sd_bus *bus = userdata;
3004 r = sd_bus_process(bus, NULL);
3011 static int prepare_callback(sd_event_source *s, void *userdata) {
3012 sd_bus *bus = userdata;
3019 e = sd_bus_get_events(bus);
3023 if (bus->output_fd != bus->input_fd) {
3025 r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN);
3029 r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT);
3033 r = sd_event_source_set_io_events(bus->input_io_event_source, e);
3038 r = sd_bus_get_timeout(bus, &until);
3044 j = sd_event_source_set_time(bus->time_event_source, until);
3049 r = sd_event_source_set_enabled(bus->time_event_source, r > 0);
3056 static int quit_callback(sd_event_source *event, void *userdata) {
3057 sd_bus *bus = userdata;
3067 static int attach_io_events(sd_bus *bus) {
3072 if (bus->input_fd < 0)
3078 if (!bus->input_io_event_source) {
3079 r = sd_event_add_io(bus->event, &bus->input_io_event_source, bus->input_fd, 0, io_callback, bus);
3083 r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
3087 r = sd_event_source_set_priority(bus->input_io_event_source, bus->event_priority);
3091 r = sd_event_source_set_description(bus->input_io_event_source, "bus-input");
3093 r = sd_event_source_set_io_fd(bus->input_io_event_source, bus->input_fd);
3098 if (bus->output_fd != bus->input_fd) {
3099 assert(bus->output_fd >= 0);
3101 if (!bus->output_io_event_source) {
3102 r = sd_event_add_io(bus->event, &bus->output_io_event_source, bus->output_fd, 0, io_callback, bus);
3106 r = sd_event_source_set_priority(bus->output_io_event_source, bus->event_priority);
3110 r = sd_event_source_set_description(bus->input_io_event_source, "bus-output");
3112 r = sd_event_source_set_io_fd(bus->output_io_event_source, bus->output_fd);
3121 static void detach_io_events(sd_bus *bus) {
3124 if (bus->input_io_event_source) {
3125 sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
3126 bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
3129 if (bus->output_io_event_source) {
3130 sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
3131 bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
3135 _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
3138 assert_return(bus, -EINVAL);
3139 assert_return(!bus->event, -EBUSY);
3141 assert(!bus->input_io_event_source);
3142 assert(!bus->output_io_event_source);
3143 assert(!bus->time_event_source);
3146 bus->event = sd_event_ref(event);
3148 r = sd_event_default(&bus->event);
3153 bus->event_priority = priority;
3155 r = sd_event_add_time(bus->event, &bus->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, bus);
3159 r = sd_event_source_set_priority(bus->time_event_source, priority);
3163 r = sd_event_source_set_description(bus->time_event_source, "bus-time");
3167 r = sd_event_add_exit(bus->event, &bus->quit_event_source, quit_callback, bus);
3171 r = sd_event_source_set_description(bus->quit_event_source, "bus-exit");
3175 r = attach_io_events(bus);
3182 sd_bus_detach_event(bus);
3186 _public_ int sd_bus_detach_event(sd_bus *bus) {
3187 assert_return(bus, -EINVAL);
3192 detach_io_events(bus);
3194 if (bus->time_event_source) {
3195 sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF);
3196 bus->time_event_source = sd_event_source_unref(bus->time_event_source);
3199 if (bus->quit_event_source) {
3200 sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF);
3201 bus->quit_event_source = sd_event_source_unref(bus->quit_event_source);
3204 bus->event = sd_event_unref(bus->event);
3208 _public_ sd_event* sd_bus_get_event(sd_bus *bus) {
3209 assert_return(bus, NULL);
3214 _public_ sd_bus_message* sd_bus_get_current_message(sd_bus *bus) {
3215 assert_return(bus, NULL);
3217 return bus->current_message;
3220 #if 0 /// UNNEEDED by elogind
3221 _public_ sd_bus_slot* sd_bus_get_current_slot(sd_bus *bus) {
3222 assert_return(bus, NULL);
3224 return bus->current_slot;
3228 _public_ sd_bus_message_handler_t sd_bus_get_current_handler(sd_bus *bus) {
3229 assert_return(bus, NULL);
3231 return bus->current_handler;
3234 _public_ void* sd_bus_get_current_userdata(sd_bus *bus) {
3235 assert_return(bus, NULL);
3237 return bus->current_userdata;
3240 static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
3245 assert(default_bus);
3248 return !!*default_bus;
3251 *ret = sd_bus_ref(*default_bus);
3259 b->default_bus_ptr = default_bus;
3267 _public_ int sd_bus_default_system(sd_bus **ret) {
3268 return bus_default(sd_bus_open_system, &default_system_bus, ret);
3272 _public_ int sd_bus_default_user(sd_bus **ret) {
3273 #if 0 /// elogind does not support user buses
3274 return bus_default(sd_bus_open_user, &default_user_bus, ret);
3276 return sd_bus_default_system(ret);
3280 _public_ int sd_bus_default(sd_bus **ret) {
3284 /* Let's try our best to reuse another cached connection. If
3285 * the starter bus type is set, connect via our normal
3286 * connection logic, ignoring $DBUS_STARTER_ADDRESS, so that
3287 * we can share the connection with the user/system default
3290 e = secure_getenv("DBUS_STARTER_BUS_TYPE");
3292 if (streq(e, "system"))
3293 return sd_bus_default_system(ret);
3294 #if 0 /// elogind does not support systemd units
3295 else if (STR_IN_SET(e, "user", "session"))
3296 return sd_bus_default_user(ret);
3300 /* No type is specified, so we have not other option than to
3301 * use the starter address if it is set. */
3303 e = secure_getenv("DBUS_STARTER_ADDRESS");
3306 return bus_default(sd_bus_open, &default_starter_bus, ret);
3309 /* Finally, if nothing is set use the cached connection for
3310 * the right scope */
3312 #if 0 /// elogind does not support systemd units
3313 if (cg_pid_get_owner_uid(0, NULL) >= 0)
3314 return sd_bus_default_user(ret);
3317 return sd_bus_default_system(ret);
3320 #if 0 /// UNNEEDED by elogind
3321 _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
3322 assert_return(b, -EINVAL);
3323 assert_return(tid, -EINVAL);
3324 assert_return(!bus_pid_changed(b), -ECHILD);
3332 return sd_event_get_tid(b->event, tid);
3337 _public_ int sd_bus_path_encode(const char *prefix, const char *external_id, char **ret_path) {
3338 _cleanup_free_ char *e = NULL;
3341 assert_return(object_path_is_valid(prefix), -EINVAL);
3342 assert_return(external_id, -EINVAL);
3343 assert_return(ret_path, -EINVAL);
3345 e = bus_label_escape(external_id);
3349 ret = strjoin(prefix, "/", e);
3357 _public_ int sd_bus_path_decode(const char *path, const char *prefix, char **external_id) {
3361 assert_return(object_path_is_valid(path), -EINVAL);
3362 assert_return(object_path_is_valid(prefix), -EINVAL);
3363 assert_return(external_id, -EINVAL);
3365 e = object_path_startswith(path, prefix);
3367 *external_id = NULL;
3371 ret = bus_label_unescape(e);
3379 _public_ int sd_bus_path_encode_many(char **out, const char *path_template, ...) {
3380 _cleanup_strv_free_ char **labels = NULL;
3381 char *path, *path_pos, **label_pos;
3382 const char *sep, *template_pos;
3387 assert_return(out, -EINVAL);
3388 assert_return(path_template, -EINVAL);
3390 path_length = strlen(path_template);
3392 va_start(list, path_template);
3393 for (sep = strchr(path_template, '%'); sep; sep = strchr(sep + 1, '%')) {
3397 arg = va_arg(list, const char *);
3403 label = bus_label_escape(arg);
3409 r = strv_consume(&labels, label);
3415 /* add label length, but account for the format character */
3416 path_length += strlen(label) - 1;
3420 path = malloc(path_length + 1);
3427 for (template_pos = path_template; *template_pos; ) {
3428 sep = strchrnul(template_pos, '%');
3429 path_pos = mempcpy(path_pos, template_pos, sep - template_pos);
3433 path_pos = stpcpy(path_pos, *label_pos++);
3434 template_pos = sep + 1;
3442 _public_ int sd_bus_path_decode_many(const char *path, const char *path_template, ...) {
3443 _cleanup_strv_free_ char **labels = NULL;
3444 const char *template_pos, *path_pos;
3450 * This decodes an object-path based on a template argument. The
3451 * template consists of a verbatim path, optionally including special
3454 * - Each occurrence of '%' in the template matches an arbitrary
3455 * substring of a label in the given path. At most one such
3456 * directive is allowed per label. For each such directive, the
3457 * caller must provide an output parameter (char **) via va_arg. If
3458 * NULL is passed, the given label is verified, but not returned.
3459 * For each matched label, the *decoded* label is stored in the
3460 * passed output argument, and the caller is responsible to free
3461 * it. Note that the output arguments are only modified if the
3462 * actualy path matched the template. Otherwise, they're left
3465 * This function returns <0 on error, 0 if the path does not match the
3466 * template, 1 if it matched.
3469 assert_return(path, -EINVAL);
3470 assert_return(path_template, -EINVAL);
3474 for (template_pos = path_template; *template_pos; ) {
3479 /* verify everything until the next '%' matches verbatim */
3480 sep = strchrnul(template_pos, '%');
3481 length = sep - template_pos;
3482 if (strncmp(path_pos, template_pos, length))
3486 template_pos += length;
3491 /* We found the next '%' character. Everything up until here
3492 * matched. We now skip ahead to the end of this label and make
3493 * sure it matches the tail of the label in the path. Then we
3494 * decode the string in-between and save it for later use. */
3496 ++template_pos; /* skip over '%' */
3498 sep = strchrnul(template_pos, '/');
3499 length = sep - template_pos; /* length of suffix to match verbatim */
3501 /* verify the suffixes match */
3502 sep = strchrnul(path_pos, '/');
3503 if (sep - path_pos < (ssize_t)length ||
3504 strncmp(sep - length, template_pos, length))
3507 template_pos += length; /* skip over matched label */
3508 length = sep - path_pos - length; /* length of sub-label to decode */
3510 /* store unescaped label for later use */
3511 label = bus_label_unescape_n(path_pos, length);
3515 r = strv_consume(&labels, label);
3519 path_pos = sep; /* skip decoded label and suffix */
3522 /* end of template must match end of path */
3526 /* copy the labels over to the caller */
3527 va_start(list, path_template);
3528 for (label_pos = labels; label_pos && *label_pos; ++label_pos) {
3531 arg = va_arg(list, char **);
3544 _public_ int sd_bus_try_close(sd_bus *bus) {
3545 assert_return(bus, -EINVAL);
3546 assert_return(!bus_pid_changed(bus), -ECHILD);
3551 _public_ int sd_bus_get_description(sd_bus *bus, const char **description) {
3552 assert_return(bus, -EINVAL);
3553 assert_return(description, -EINVAL);
3554 assert_return(bus->description, -ENXIO);
3555 assert_return(!bus_pid_changed(bus), -ECHILD);
3557 *description = bus->description;
3562 int bus_get_root_path(sd_bus *bus) {
3565 if (bus->cgroup_root)
3568 r = cg_get_root_path(&bus->cgroup_root);
3570 bus->cgroup_root = strdup("/");
3571 if (!bus->cgroup_root)
3580 #if 0 /// UNNEEDED by elogind
3581 _public_ int sd_bus_get_scope(sd_bus *bus, const char **scope) {
3582 assert_return(bus, -EINVAL);
3583 assert_return(scope, -EINVAL);
3584 assert_return(!bus_pid_changed(bus), -ECHILD);
3591 if (bus->is_system) {
3599 _public_ int sd_bus_get_address(sd_bus *bus, const char **address) {
3601 assert_return(bus, -EINVAL);
3602 assert_return(address, -EINVAL);
3603 assert_return(!bus_pid_changed(bus), -ECHILD);
3606 *address = bus->address;
3613 _public_ int sd_bus_get_creds_mask(sd_bus *bus, uint64_t *mask) {
3614 assert_return(bus, -EINVAL);
3615 assert_return(mask, -EINVAL);
3616 assert_return(!bus_pid_changed(bus), -ECHILD);
3618 *mask = bus->creds_mask;
3622 _public_ int sd_bus_is_bus_client(sd_bus *bus) {
3623 assert_return(bus, -EINVAL);
3624 assert_return(!bus_pid_changed(bus), -ECHILD);
3626 return bus->bus_client;
3629 _public_ int sd_bus_is_server(sd_bus *bus) {
3630 assert_return(bus, -EINVAL);
3631 assert_return(!bus_pid_changed(bus), -ECHILD);
3633 return bus->is_server;
3636 _public_ int sd_bus_is_anonymous(sd_bus *bus) {
3637 assert_return(bus, -EINVAL);
3638 assert_return(!bus_pid_changed(bus), -ECHILD);
3640 return bus->anonymous_auth;
3643 _public_ int sd_bus_is_trusted(sd_bus *bus) {
3644 assert_return(bus, -EINVAL);
3645 assert_return(!bus_pid_changed(bus), -ECHILD);
3647 return bus->trusted;
3650 _public_ int sd_bus_is_monitor(sd_bus *bus) {
3651 assert_return(bus, -EINVAL);
3652 assert_return(!bus_pid_changed(bus), -ECHILD);
3654 return !!(bus->hello_flags & KDBUS_HELLO_MONITOR);
3657 static void flush_close(sd_bus *bus) {
3661 /* Flushes and closes the specified bus. We take a ref before,
3662 * to ensure the flushing does not cause the bus to be
3665 sd_bus_flush_close_unref(sd_bus_ref(bus));
3668 _public_ void sd_bus_default_flush_close(void) {
3669 flush_close(default_starter_bus);
3670 flush_close(default_user_bus);
3671 flush_close(default_system_bus);
3675 _public_ int sd_bus_set_exit_on_disconnect(sd_bus *bus, int b) {
3676 assert_return(bus, -EINVAL);
3678 /* Turns on exit-on-disconnect, and triggers it immediately if the bus connection was already
3679 * disconnected. Note that this is triggered exclusively on disconnections triggered by the server side, never
3680 * from the client side. */
3681 bus->exit_on_disconnect = b;
3683 /* If the exit condition was triggered already, exit immediately. */
3684 return bus_exit_now(bus);
3687 #if 0 /// UNNEEDED by elogind
3688 _public_ int sd_bus_get_exit_on_disconnect(sd_bus *bus) {
3689 assert_return(bus, -EINVAL);
3691 return bus->exit_on_disconnect;