2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
30 #include "alloc-util.h"
31 #include "bus-container.h"
32 #include "bus-control.h"
33 #include "bus-internal.h"
34 #include "bus-kernel.h"
35 #include "bus-label.h"
36 #include "bus-message.h"
37 #include "bus-objects.h"
38 #include "bus-protocol.h"
40 #include "bus-socket.h"
41 #include "bus-track.h"
44 #include "cgroup-util.h"
47 #include "hexdecoct.h"
48 #include "hostname-util.h"
51 #include "parse-util.h"
52 #include "string-util.h"
56 #define log_debug_bus_message(m) \
58 sd_bus_message *_mm = (m); \
59 log_debug("Got message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%" PRIu64 " reply_cookie=%" PRIu64 " error-name=%s error-message=%s", \
60 bus_message_type_to_string(_mm->header->type), \
61 strna(sd_bus_message_get_sender(_mm)), \
62 strna(sd_bus_message_get_destination(_mm)), \
63 strna(sd_bus_message_get_path(_mm)), \
64 strna(sd_bus_message_get_interface(_mm)), \
65 strna(sd_bus_message_get_member(_mm)), \
66 BUS_MESSAGE_COOKIE(_mm), \
68 strna(_mm->error.name), \
69 strna(_mm->error.message)); \
72 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
73 static int attach_io_events(sd_bus *b);
74 static void detach_io_events(sd_bus *b);
76 static thread_local sd_bus *default_system_bus = NULL;
77 #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(IN_SET(bus->state, BUS_HELLO, 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 (!IN_SET(*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 (!IN_SET(**p, 0, ';')) {
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 (!IN_SET(**p, 0, ';')) {
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 (!IN_SET(**p, 0, ';')) {
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 (!IN_SET(**p, 0, ';')) {
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 (!IN_SET(bus->state, BUS_OPENING, BUS_AUTHENTICATING, BUS_HELLO, BUS_RUNNING))
1348 bus->state = BUS_CLOSING;
1351 _public_ sd_bus *sd_bus_ref(sd_bus *bus) {
1356 assert_se(REFCNT_INC(bus->n_ref) >= 2);
1361 _public_ sd_bus *sd_bus_unref(sd_bus *bus) {
1367 i = REFCNT_DEC(bus->n_ref);
1375 #if 0 /// UNNEEDED by elogind
1376 _public_ int sd_bus_is_open(sd_bus *bus) {
1378 assert_return(bus, -EINVAL);
1379 assert_return(!bus_pid_changed(bus), -ECHILD);
1381 return BUS_IS_OPEN(bus->state);
1385 _public_ int sd_bus_can_send(sd_bus *bus, char type) {
1388 assert_return(bus, -EINVAL);
1389 assert_return(bus->state != BUS_UNSET, -ENOTCONN);
1390 assert_return(!bus_pid_changed(bus), -ECHILD);
1392 if (bus->hello_flags & KDBUS_HELLO_MONITOR)
1395 if (type == SD_BUS_TYPE_UNIX_FD) {
1396 if (!(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD))
1399 r = bus_ensure_running(bus);
1403 return bus->can_fds;
1406 return bus_type_is_valid(type);
1409 #if 0 /// UNNEEDED by elogind
1410 _public_ int sd_bus_get_bus_id(sd_bus *bus, sd_id128_t *id) {
1413 assert_return(bus, -EINVAL);
1414 assert_return(id, -EINVAL);
1415 assert_return(!bus_pid_changed(bus), -ECHILD);
1417 r = bus_ensure_running(bus);
1421 *id = bus->server_id;
1426 static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
1431 /* If we copy the same message to multiple
1432 * destinations, avoid using the same cookie
1434 b->cookie = MAX(b->cookie, BUS_MESSAGE_COOKIE(m));
1439 timeout = BUS_DEFAULT_TIMEOUT;
1441 return bus_message_seal(m, ++b->cookie, timeout);
1444 static int bus_remarshal_message(sd_bus *b, sd_bus_message **m) {
1445 bool remarshal = false;
1449 /* wrong packet version */
1450 if (b->message_version != 0 && b->message_version != (*m)->header->version)
1453 /* wrong packet endianness */
1454 if (b->message_endian != 0 && b->message_endian != (*m)->header->endian)
1457 return remarshal ? bus_message_remarshal(b, m) : 0;
1460 int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
1464 /* Fake some timestamps, if they were requested, and not
1465 * already initialized */
1466 if (b->attach_flags & KDBUS_ATTACH_TIMESTAMP) {
1467 if (m->realtime <= 0)
1468 m->realtime = now(CLOCK_REALTIME);
1470 if (m->monotonic <= 0)
1471 m->monotonic = now(CLOCK_MONOTONIC);
1474 /* The bus specification says the serial number cannot be 0,
1475 * hence let's fill something in for synthetic messages. Since
1476 * synthetic messages might have a fake sender and we don't
1477 * want to interfere with the real sender's serial numbers we
1478 * pick a fixed, artificial one. We use (uint32_t) -1 rather
1479 * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
1480 * even though kdbus can do 64bit. */
1481 return bus_message_seal(m, 0xFFFFFFFFULL, 0);
1484 static int bus_write_message(sd_bus *bus, sd_bus_message *m, bool hint_sync_call, size_t *idx) {
1490 r = bus_socket_write_message(bus, m, idx);
1494 if (*idx >= BUS_MESSAGE_SIZE(m))
1495 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",
1496 bus_message_type_to_string(m->header->type),
1497 strna(sd_bus_message_get_sender(m)),
1498 strna(sd_bus_message_get_destination(m)),
1499 strna(sd_bus_message_get_path(m)),
1500 strna(sd_bus_message_get_interface(m)),
1501 strna(sd_bus_message_get_member(m)),
1502 BUS_MESSAGE_COOKIE(m),
1504 strna(m->error.name),
1505 strna(m->error.message));
1510 static int dispatch_wqueue(sd_bus *bus) {
1514 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
1516 while (bus->wqueue_size > 0) {
1518 r = bus_write_message(bus, bus->wqueue[0], false, &bus->windex);
1522 /* Didn't do anything this time */
1524 else if (bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
1525 /* Fully written. Let's drop the entry from
1528 * This isn't particularly optimized, but
1529 * well, this is supposed to be our worst-case
1530 * buffer only, and the socket buffer is
1531 * supposed to be our primary buffer, and if
1532 * it got full, then all bets are off
1536 sd_bus_message_unref(bus->wqueue[0]);
1537 memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
1547 static int bus_read_message(sd_bus *bus, bool hint_priority, int64_t priority) {
1550 return bus_socket_read_message(bus);
1553 int bus_rqueue_make_room(sd_bus *bus) {
1556 if (bus->rqueue_size >= BUS_RQUEUE_MAX)
1559 if (!GREEDY_REALLOC(bus->rqueue, bus->rqueue_allocated, bus->rqueue_size + 1))
1565 static int dispatch_rqueue(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **m) {
1570 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
1572 /* Note that the priority logic is only available on kdbus,
1573 * where the rqueue is unused. We check the rqueue here
1574 * anyway, because it's simple... */
1577 if (bus->rqueue_size > 0) {
1578 /* Dispatch a queued message */
1580 *m = bus->rqueue[0];
1582 memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
1586 /* Try to read a new message */
1587 r = bus_read_message(bus, hint_priority, priority);
1597 static int bus_send_internal(sd_bus *bus, sd_bus_message *_m, uint64_t *cookie, bool hint_sync_call) {
1598 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = sd_bus_message_ref(_m);
1601 assert_return(m, -EINVAL);
1606 assert_return(!bus_pid_changed(bus), -ECHILD);
1608 if (!BUS_IS_OPEN(bus->state))
1612 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
1619 /* If the cookie number isn't kept, then we know that no reply
1621 if (!cookie && !m->sealed)
1622 m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
1624 r = bus_seal_message(bus, m, 0);
1628 /* Remarshall if we have to. This will possibly unref the
1629 * message and place a replacement in m */
1630 r = bus_remarshal_message(bus, &m);
1634 /* If this is a reply and no reply was requested, then let's
1635 * suppress this, if we can */
1639 if (IN_SET(bus->state, BUS_RUNNING, BUS_HELLO) && bus->wqueue_size <= 0) {
1642 r = bus_write_message(bus, m, hint_sync_call, &idx);
1644 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
1645 bus_enter_closing(bus);
1652 if (idx < BUS_MESSAGE_SIZE(m)) {
1653 /* Wasn't fully written. So let's remember how
1654 * much was written. Note that the first entry
1655 * of the wqueue array is always allocated so
1656 * that we always can remember how much was
1658 bus->wqueue[0] = sd_bus_message_ref(m);
1659 bus->wqueue_size = 1;
1664 /* Just append it to the queue. */
1666 if (bus->wqueue_size >= BUS_WQUEUE_MAX)
1669 if (!GREEDY_REALLOC(bus->wqueue, bus->wqueue_allocated, bus->wqueue_size + 1))
1672 bus->wqueue[bus->wqueue_size++] = sd_bus_message_ref(m);
1677 *cookie = BUS_MESSAGE_COOKIE(m);
1682 _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *cookie) {
1683 return bus_send_internal(bus, m, cookie, false);
1686 #if 0 /// UNNEEDED by elogind
1687 _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *cookie) {
1690 assert_return(m, -EINVAL);
1695 assert_return(!bus_pid_changed(bus), -ECHILD);
1697 if (!BUS_IS_OPEN(bus->state))
1700 if (!streq_ptr(m->destination, destination)) {
1705 r = sd_bus_message_set_destination(m, destination);
1710 return sd_bus_send(bus, m, cookie);
1714 static usec_t calc_elapse(uint64_t usec) {
1715 if (usec == (uint64_t) -1)
1718 return now(CLOCK_MONOTONIC) + usec;
1721 static int timeout_compare(const void *a, const void *b) {
1722 const struct reply_callback *x = a, *y = b;
1724 if (x->timeout != 0 && y->timeout == 0)
1727 if (x->timeout == 0 && y->timeout != 0)
1730 if (x->timeout < y->timeout)
1733 if (x->timeout > y->timeout)
1739 _public_ int sd_bus_call_async(
1743 sd_bus_message_handler_t callback,
1747 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = sd_bus_message_ref(_m);
1748 _cleanup_(sd_bus_slot_unrefp) sd_bus_slot *s = NULL;
1751 assert_return(m, -EINVAL);
1752 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1753 assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1754 assert_return(callback, -EINVAL);
1759 assert_return(!bus_pid_changed(bus), -ECHILD);
1761 if (!BUS_IS_OPEN(bus->state))
1764 r = ordered_hashmap_ensure_allocated(&bus->reply_callbacks, &uint64_hash_ops);
1768 r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
1772 r = bus_seal_message(bus, m, usec);
1776 r = bus_remarshal_message(bus, &m);
1780 s = bus_slot_allocate(bus, !slot, BUS_REPLY_CALLBACK, sizeof(struct reply_callback), userdata);
1784 s->reply_callback.callback = callback;
1786 s->reply_callback.cookie = BUS_MESSAGE_COOKIE(m);
1787 r = ordered_hashmap_put(bus->reply_callbacks, &s->reply_callback.cookie, &s->reply_callback);
1789 s->reply_callback.cookie = 0;
1793 s->reply_callback.timeout = calc_elapse(m->timeout);
1794 if (s->reply_callback.timeout != 0) {
1795 r = prioq_put(bus->reply_callbacks_prioq, &s->reply_callback, &s->reply_callback.prioq_idx);
1797 s->reply_callback.timeout = 0;
1802 r = sd_bus_send(bus, m, &s->reply_callback.cookie);
1813 int bus_ensure_running(sd_bus *bus) {
1818 if (IN_SET(bus->state, BUS_UNSET, BUS_CLOSED, BUS_CLOSING))
1820 if (bus->state == BUS_RUNNING)
1824 r = sd_bus_process(bus, NULL);
1827 if (bus->state == BUS_RUNNING)
1832 r = sd_bus_wait(bus, (uint64_t) -1);
1838 _public_ int sd_bus_call(
1842 sd_bus_error *error,
1843 sd_bus_message **reply) {
1845 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = sd_bus_message_ref(_m);
1851 bus_assert_return(m, -EINVAL, error);
1852 bus_assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL, error);
1853 bus_assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL, error);
1854 bus_assert_return(!bus_error_is_dirty(error), -EINVAL, error);
1859 bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
1861 if (!BUS_IS_OPEN(bus->state)) {
1866 r = bus_ensure_running(bus);
1870 i = bus->rqueue_size;
1872 r = bus_seal_message(bus, m, usec);
1876 r = bus_remarshal_message(bus, &m);
1880 r = bus_send_internal(bus, m, &cookie, true);
1884 timeout = calc_elapse(m->timeout);
1889 while (i < bus->rqueue_size) {
1890 sd_bus_message *incoming = NULL;
1892 incoming = bus->rqueue[i];
1894 if (incoming->reply_cookie == cookie) {
1895 /* Found a match! */
1897 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1899 log_debug_bus_message(incoming);
1901 if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
1903 if (incoming->n_fds <= 0 || (bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)) {
1907 sd_bus_message_unref(incoming);
1912 r = sd_bus_error_setf(error, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptors which I couldn't accept. Sorry.");
1913 sd_bus_message_unref(incoming);
1916 } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR) {
1917 r = sd_bus_error_copy(error, &incoming->error);
1918 sd_bus_message_unref(incoming);
1925 } else if (BUS_MESSAGE_COOKIE(incoming) == cookie &&
1928 streq(bus->unique_name, incoming->sender)) {
1930 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1933 /* Our own message? Somebody is trying
1934 * to send its own client a message,
1935 * let's not dead-lock, let's fail
1938 sd_bus_message_unref(incoming);
1943 /* Try to read more, right-away */
1947 r = bus_read_message(bus, false, 0);
1949 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
1950 bus_enter_closing(bus);
1962 n = now(CLOCK_MONOTONIC);
1970 left = (uint64_t) -1;
1972 r = bus_poll(bus, true, left);
1980 r = dispatch_wqueue(bus);
1982 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
1983 bus_enter_closing(bus);
1992 return sd_bus_error_set_errno(error, r);
1995 #if 0 /// UNNEEDED by elogind
1996 _public_ int sd_bus_get_fd(sd_bus *bus) {
1998 assert_return(bus, -EINVAL);
1999 assert_return(bus->input_fd == bus->output_fd, -EPERM);
2000 assert_return(!bus_pid_changed(bus), -ECHILD);
2002 return bus->input_fd;
2006 _public_ int sd_bus_get_events(sd_bus *bus) {
2009 assert_return(bus, -EINVAL);
2010 assert_return(!bus_pid_changed(bus), -ECHILD);
2012 if (!BUS_IS_OPEN(bus->state) && bus->state != BUS_CLOSING)
2015 if (bus->state == BUS_OPENING)
2017 else if (bus->state == BUS_AUTHENTICATING) {
2019 if (bus_socket_auth_needs_write(bus))
2024 } else if (IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)) {
2025 if (bus->rqueue_size <= 0)
2027 if (bus->wqueue_size > 0)
2034 _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
2035 struct reply_callback *c;
2037 assert_return(bus, -EINVAL);
2038 assert_return(timeout_usec, -EINVAL);
2039 assert_return(!bus_pid_changed(bus), -ECHILD);
2041 if (!BUS_IS_OPEN(bus->state) && bus->state != BUS_CLOSING)
2044 if (bus->track_queue) {
2049 if (bus->state == BUS_CLOSING) {
2054 if (bus->state == BUS_AUTHENTICATING) {
2055 *timeout_usec = bus->auth_timeout;
2059 if (!IN_SET(bus->state, BUS_RUNNING, BUS_HELLO)) {
2060 *timeout_usec = (uint64_t) -1;
2064 if (bus->rqueue_size > 0) {
2069 c = prioq_peek(bus->reply_callbacks_prioq);
2071 *timeout_usec = (uint64_t) -1;
2075 if (c->timeout == 0) {
2076 *timeout_usec = (uint64_t) -1;
2080 *timeout_usec = c->timeout;
2084 static int process_timeout(sd_bus *bus) {
2085 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2086 _cleanup_(sd_bus_message_unrefp) sd_bus_message* m = NULL;
2087 struct reply_callback *c;
2094 c = prioq_peek(bus->reply_callbacks_prioq);
2098 n = now(CLOCK_MONOTONIC);
2102 r = bus_message_new_synthetic_error(
2105 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
2110 r = bus_seal_synthetic_message(bus, m);
2114 assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
2117 ordered_hashmap_remove(bus->reply_callbacks, &c->cookie);
2120 slot = container_of(c, sd_bus_slot, reply_callback);
2122 bus->iteration_counter++;
2124 bus->current_message = m;
2125 bus->current_slot = sd_bus_slot_ref(slot);
2126 bus->current_handler = c->callback;
2127 bus->current_userdata = slot->userdata;
2128 r = c->callback(m, slot->userdata, &error_buffer);
2129 bus->current_userdata = NULL;
2130 bus->current_handler = NULL;
2131 bus->current_slot = NULL;
2132 bus->current_message = NULL;
2134 if (slot->floating) {
2135 bus_slot_disconnect(slot);
2136 sd_bus_slot_unref(slot);
2139 sd_bus_slot_unref(slot);
2141 return bus_maybe_reply_error(m, r, &error_buffer);
2144 static int process_hello(sd_bus *bus, sd_bus_message *m) {
2148 if (bus->state != BUS_HELLO)
2151 /* Let's make sure the first message on the bus is the HELLO
2152 * reply. But note that we don't actually parse the message
2153 * here (we leave that to the usual handling), we just verify
2154 * we don't let any earlier msg through. */
2156 if (!IN_SET(m->header->type, SD_BUS_MESSAGE_METHOD_RETURN, SD_BUS_MESSAGE_METHOD_ERROR))
2159 if (m->reply_cookie != 1)
2165 static int process_reply(sd_bus *bus, sd_bus_message *m) {
2166 _cleanup_(sd_bus_message_unrefp) sd_bus_message *synthetic_reply = NULL;
2167 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2168 struct reply_callback *c;
2175 if (!IN_SET(m->header->type, SD_BUS_MESSAGE_METHOD_RETURN, SD_BUS_MESSAGE_METHOD_ERROR))
2178 if (m->destination && bus->unique_name && !streq_ptr(m->destination, bus->unique_name))
2181 c = ordered_hashmap_remove(bus->reply_callbacks, &m->reply_cookie);
2187 slot = container_of(c, sd_bus_slot, reply_callback);
2189 if (m->n_fds > 0 && !(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)) {
2191 /* If the reply contained a file descriptor which we
2192 * didn't want we pass an error instead. */
2194 r = bus_message_new_synthetic_error(
2197 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptor"),
2202 /* Copy over original timestamp */
2203 synthetic_reply->realtime = m->realtime;
2204 synthetic_reply->monotonic = m->monotonic;
2205 synthetic_reply->seqnum = m->seqnum;
2207 r = bus_seal_synthetic_message(bus, synthetic_reply);
2211 m = synthetic_reply;
2213 r = sd_bus_message_rewind(m, true);
2218 if (c->timeout != 0) {
2219 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2223 bus->current_slot = sd_bus_slot_ref(slot);
2224 bus->current_handler = c->callback;
2225 bus->current_userdata = slot->userdata;
2226 r = c->callback(m, slot->userdata, &error_buffer);
2227 bus->current_userdata = NULL;
2228 bus->current_handler = NULL;
2229 bus->current_slot = NULL;
2231 if (slot->floating) {
2232 bus_slot_disconnect(slot);
2233 sd_bus_slot_unref(slot);
2236 sd_bus_slot_unref(slot);
2238 return bus_maybe_reply_error(m, r, &error_buffer);
2241 static int process_filter(sd_bus *bus, sd_bus_message *m) {
2242 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2243 struct filter_callback *l;
2250 bus->filter_callbacks_modified = false;
2252 LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
2255 if (bus->filter_callbacks_modified)
2258 /* Don't run this more than once per iteration */
2259 if (l->last_iteration == bus->iteration_counter)
2262 l->last_iteration = bus->iteration_counter;
2264 r = sd_bus_message_rewind(m, true);
2268 slot = container_of(l, sd_bus_slot, filter_callback);
2270 bus->current_slot = sd_bus_slot_ref(slot);
2271 bus->current_handler = l->callback;
2272 bus->current_userdata = slot->userdata;
2273 r = l->callback(m, slot->userdata, &error_buffer);
2274 bus->current_userdata = NULL;
2275 bus->current_handler = NULL;
2276 bus->current_slot = sd_bus_slot_unref(slot);
2278 r = bus_maybe_reply_error(m, r, &error_buffer);
2284 } while (bus->filter_callbacks_modified);
2289 static int process_match(sd_bus *bus, sd_bus_message *m) {
2296 bus->match_callbacks_modified = false;
2298 r = bus_match_run(bus, &bus->match_callbacks, m);
2302 } while (bus->match_callbacks_modified);
2307 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
2308 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
2314 if (bus->hello_flags & KDBUS_HELLO_MONITOR)
2317 if (bus->manual_peer_interface)
2320 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
2323 if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
2326 if (m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
2329 if (streq_ptr(m->member, "Ping"))
2330 r = sd_bus_message_new_method_return(m, &reply);
2331 else if (streq_ptr(m->member, "GetMachineId")) {
2335 r = sd_id128_get_machine(&id);
2339 r = sd_bus_message_new_method_return(m, &reply);
2343 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
2345 r = sd_bus_message_new_method_errorf(
2347 SD_BUS_ERROR_UNKNOWN_METHOD,
2348 "Unknown method '%s' on interface '%s'.", m->member, m->interface);
2354 r = sd_bus_send(bus, reply, NULL);
2361 static int process_fd_check(sd_bus *bus, sd_bus_message *m) {
2365 /* If we got a message with a file descriptor which we didn't
2366 * want to accept, then let's drop it. How can this even
2367 * happen? For example, when the kernel queues a message into
2368 * an activatable names's queue which allows fds, and then is
2369 * delivered to us later even though we ourselves did not
2372 if (bus->hello_flags & KDBUS_HELLO_MONITOR)
2378 if (bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)
2381 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
2382 return 1; /* just eat it up */
2384 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Message contains file descriptors, which I cannot accept. Sorry.");
2387 static int process_message(sd_bus *bus, sd_bus_message *m) {
2393 bus->current_message = m;
2394 bus->iteration_counter++;
2396 log_debug_bus_message(m);
2398 r = process_hello(bus, m);
2402 r = process_reply(bus, m);
2406 r = process_fd_check(bus, m);
2410 r = process_filter(bus, m);
2414 r = process_match(bus, m);
2418 r = process_builtin(bus, m);
2422 r = bus_process_object(bus, m);
2425 bus->current_message = NULL;
2429 static int dispatch_track(sd_bus *bus) {
2432 if (!bus->track_queue)
2435 bus_track_dispatch(bus->track_queue);
2439 static int process_running(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) {
2440 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2444 assert(IN_SET(bus->state, BUS_RUNNING, BUS_HELLO));
2446 r = process_timeout(bus);
2450 r = dispatch_wqueue(bus);
2454 r = dispatch_track(bus);
2458 r = dispatch_rqueue(bus, hint_priority, priority, &m);
2464 r = process_message(bus, m);
2469 r = sd_bus_message_rewind(m, true);
2478 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
2480 log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s",
2481 strna(sd_bus_message_get_sender(m)),
2482 strna(sd_bus_message_get_path(m)),
2483 strna(sd_bus_message_get_interface(m)),
2484 strna(sd_bus_message_get_member(m)));
2486 r = sd_bus_reply_method_errorf(
2488 SD_BUS_ERROR_UNKNOWN_OBJECT,
2489 "Unknown object '%s'.", m->path);
2503 static int bus_exit_now(sd_bus *bus) {
2506 /* Exit due to close, if this is requested. If this is bus object is attached to an event source, invokes
2507 * sd_event_exit(), otherwise invokes libc exit(). */
2509 if (bus->exited) /* did we already exit? */
2511 if (!bus->exit_triggered) /* was the exit condition triggered? */
2513 if (!bus->exit_on_disconnect) /* Shall we actually exit on disconnection? */
2516 bus->exited = true; /* never exit more than once */
2518 log_debug("Bus connection disconnected, exiting.");
2521 return sd_event_exit(bus->event, EXIT_FAILURE);
2525 assert_not_reached("exit() didn't exit?");
2528 static int process_closing_reply_callback(sd_bus *bus, struct reply_callback *c) {
2529 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2530 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2537 r = bus_message_new_synthetic_error(
2540 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
2545 r = bus_seal_synthetic_message(bus, m);
2549 if (c->timeout != 0) {
2550 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2554 ordered_hashmap_remove(bus->reply_callbacks, &c->cookie);
2557 slot = container_of(c, sd_bus_slot, reply_callback);
2559 bus->iteration_counter++;
2561 bus->current_message = m;
2562 bus->current_slot = sd_bus_slot_ref(slot);
2563 bus->current_handler = c->callback;
2564 bus->current_userdata = slot->userdata;
2565 r = c->callback(m, slot->userdata, &error_buffer);
2566 bus->current_userdata = NULL;
2567 bus->current_handler = NULL;
2568 bus->current_slot = NULL;
2569 bus->current_message = NULL;
2571 if (slot->floating) {
2572 bus_slot_disconnect(slot);
2573 sd_bus_slot_unref(slot);
2576 sd_bus_slot_unref(slot);
2578 return bus_maybe_reply_error(m, r, &error_buffer);
2581 static int process_closing(sd_bus *bus, sd_bus_message **ret) {
2582 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
2583 struct reply_callback *c;
2587 assert(bus->state == BUS_CLOSING);
2589 /* First, fail all outstanding method calls */
2590 c = ordered_hashmap_first(bus->reply_callbacks);
2592 return process_closing_reply_callback(bus, c);
2594 /* Then, fake-drop all remaining bus tracking references */
2596 bus_track_close(bus->tracks);
2600 /* Then, synthesize a Disconnected message */
2601 r = sd_bus_message_new_signal(
2604 "/org/freedesktop/DBus/Local",
2605 "org.freedesktop.DBus.Local",
2610 bus_message_set_sender_local(bus, m);
2612 r = bus_seal_synthetic_message(bus, m);
2618 bus->current_message = m;
2619 bus->iteration_counter++;
2621 r = process_filter(bus, m);
2625 r = process_match(bus, m);
2629 /* Nothing else to do, exit now, if the condition holds */
2630 bus->exit_triggered = true;
2631 (void) bus_exit_now(bus);
2641 bus->current_message = NULL;
2646 static int bus_process_internal(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) {
2647 BUS_DONT_DESTROY(bus);
2650 /* Returns 0 when we didn't do anything. This should cause the
2651 * caller to invoke sd_bus_wait() before returning the next
2652 * time. Returns > 0 when we did something, which possibly
2653 * means *ret is filled in with an unprocessed message. */
2655 assert_return(bus, -EINVAL);
2656 assert_return(!bus_pid_changed(bus), -ECHILD);
2658 /* We don't allow recursively invoking sd_bus_process(). */
2659 assert_return(!bus->current_message, -EBUSY);
2660 assert(!bus->current_slot);
2662 switch (bus->state) {
2671 r = bus_socket_process_opening(bus);
2672 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
2673 bus_enter_closing(bus);
2681 case BUS_AUTHENTICATING:
2682 r = bus_socket_process_authenticating(bus);
2683 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
2684 bus_enter_closing(bus);
2696 r = process_running(bus, hint_priority, priority, ret);
2697 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
2698 bus_enter_closing(bus);
2708 return process_closing(bus, ret);
2711 assert_not_reached("Unknown state");
2714 _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
2715 return bus_process_internal(bus, false, 0, ret);
2718 #if 0 /// UNNEEDED by elogind
2719 _public_ int sd_bus_process_priority(sd_bus *bus, int64_t priority, sd_bus_message **ret) {
2720 return bus_process_internal(bus, true, priority, ret);
2724 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
2725 struct pollfd p[2] = {};
2728 usec_t m = USEC_INFINITY;
2732 if (bus->state == BUS_CLOSING)
2735 if (!BUS_IS_OPEN(bus->state))
2738 e = sd_bus_get_events(bus);
2743 /* The caller really needs some more data, he doesn't
2744 * care about what's already read, or any timeouts
2745 * except its own. */
2749 /* The caller wants to process if there's something to
2750 * process, but doesn't care otherwise */
2752 r = sd_bus_get_timeout(bus, &until);
2757 nw = now(CLOCK_MONOTONIC);
2758 m = until > nw ? until - nw : 0;
2762 if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
2765 p[0].fd = bus->input_fd;
2766 if (bus->output_fd == bus->input_fd) {
2770 p[0].events = e & POLLIN;
2771 p[1].fd = bus->output_fd;
2772 p[1].events = e & POLLOUT;
2776 r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
2780 return r > 0 ? 1 : 0;
2783 _public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
2785 assert_return(bus, -EINVAL);
2786 assert_return(!bus_pid_changed(bus), -ECHILD);
2788 if (bus->state == BUS_CLOSING)
2791 if (!BUS_IS_OPEN(bus->state))
2794 if (bus->rqueue_size > 0)
2797 return bus_poll(bus, false, timeout_usec);
2800 _public_ int sd_bus_flush(sd_bus *bus) {
2803 assert_return(bus, -EINVAL);
2804 assert_return(!bus_pid_changed(bus), -ECHILD);
2806 if (bus->state == BUS_CLOSING)
2809 if (!BUS_IS_OPEN(bus->state))
2812 r = bus_ensure_running(bus);
2816 if (bus->wqueue_size <= 0)
2820 r = dispatch_wqueue(bus);
2822 if (IN_SET(r, -ENOTCONN, -ECONNRESET, -EPIPE, -ESHUTDOWN)) {
2823 bus_enter_closing(bus);
2830 if (bus->wqueue_size <= 0)
2833 r = bus_poll(bus, false, (uint64_t) -1);
2839 #if 0 /// UNNEEDED by elogind
2840 _public_ int sd_bus_add_filter(
2843 sd_bus_message_handler_t callback,
2848 assert_return(bus, -EINVAL);
2849 assert_return(callback, -EINVAL);
2850 assert_return(!bus_pid_changed(bus), -ECHILD);
2852 s = bus_slot_allocate(bus, !slot, BUS_FILTER_CALLBACK, sizeof(struct filter_callback), userdata);
2856 s->filter_callback.callback = callback;
2858 bus->filter_callbacks_modified = true;
2859 LIST_PREPEND(callbacks, bus->filter_callbacks, &s->filter_callback);
2868 _public_ int sd_bus_add_match(
2872 sd_bus_message_handler_t callback,
2875 struct bus_match_component *components = NULL;
2876 unsigned n_components = 0;
2877 sd_bus_slot *s = NULL;
2880 assert_return(bus, -EINVAL);
2881 assert_return(match, -EINVAL);
2882 assert_return(!bus_pid_changed(bus), -ECHILD);
2884 r = bus_match_parse(match, &components, &n_components);
2888 s = bus_slot_allocate(bus, !slot, BUS_MATCH_CALLBACK, sizeof(struct match_callback), userdata);
2894 s->match_callback.callback = callback;
2895 s->match_callback.cookie = ++bus->match_cookie;
2897 if (bus->bus_client) {
2898 enum bus_match_scope scope;
2900 scope = bus_match_get_scope(components, n_components);
2902 /* Do not install server-side matches for matches
2903 * against the local service, interface or bus path. */
2904 if (scope != BUS_MATCH_LOCAL) {
2906 /* We store the original match string, so that
2907 * we can use it to remove the match again. */
2909 s->match_callback.match_string = strdup(match);
2910 if (!s->match_callback.match_string) {
2915 r = bus_add_match_internal(bus, s->match_callback.match_string, components, n_components, s->match_callback.cookie);
2919 s->match_added = true;
2923 bus->match_callbacks_modified = true;
2924 r = bus_match_add(&bus->match_callbacks, components, n_components, &s->match_callback);
2933 bus_match_parse_free(components, n_components);
2934 sd_bus_slot_unref(s);
2939 #if 0 /// UNNEEDED by elogind
2940 int bus_remove_match_by_string(
2943 sd_bus_message_handler_t callback,
2946 struct bus_match_component *components = NULL;
2947 unsigned n_components = 0;
2948 struct match_callback *c;
2951 assert_return(bus, -EINVAL);
2952 assert_return(match, -EINVAL);
2953 assert_return(!bus_pid_changed(bus), -ECHILD);
2955 r = bus_match_parse(match, &components, &n_components);
2959 r = bus_match_find(&bus->match_callbacks, components, n_components, NULL, NULL, &c);
2963 sd_bus_slot_unref(container_of(c, sd_bus_slot, match_callback));
2966 bus_match_parse_free(components, n_components);
2972 bool bus_pid_changed(sd_bus *bus) {
2975 /* We don't support people creating a bus connection and
2976 * keeping it around over a fork(). Let's complain. */
2978 return bus->original_pid != getpid_cached();
2981 static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2982 sd_bus *bus = userdata;
2987 r = sd_bus_process(bus, NULL);
2994 static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
2995 sd_bus *bus = userdata;
3000 r = sd_bus_process(bus, NULL);
3007 static int prepare_callback(sd_event_source *s, void *userdata) {
3008 sd_bus *bus = userdata;
3015 e = sd_bus_get_events(bus);
3019 if (bus->output_fd != bus->input_fd) {
3021 r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN);
3025 r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT);
3029 r = sd_event_source_set_io_events(bus->input_io_event_source, e);
3034 r = sd_bus_get_timeout(bus, &until);
3040 j = sd_event_source_set_time(bus->time_event_source, until);
3045 r = sd_event_source_set_enabled(bus->time_event_source, r > 0);
3052 static int quit_callback(sd_event_source *event, void *userdata) {
3053 sd_bus *bus = userdata;
3063 static int attach_io_events(sd_bus *bus) {
3068 if (bus->input_fd < 0)
3074 if (!bus->input_io_event_source) {
3075 r = sd_event_add_io(bus->event, &bus->input_io_event_source, bus->input_fd, 0, io_callback, bus);
3079 r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
3083 r = sd_event_source_set_priority(bus->input_io_event_source, bus->event_priority);
3087 r = sd_event_source_set_description(bus->input_io_event_source, "bus-input");
3089 r = sd_event_source_set_io_fd(bus->input_io_event_source, bus->input_fd);
3094 if (bus->output_fd != bus->input_fd) {
3095 assert(bus->output_fd >= 0);
3097 if (!bus->output_io_event_source) {
3098 r = sd_event_add_io(bus->event, &bus->output_io_event_source, bus->output_fd, 0, io_callback, bus);
3102 r = sd_event_source_set_priority(bus->output_io_event_source, bus->event_priority);
3106 r = sd_event_source_set_description(bus->input_io_event_source, "bus-output");
3108 r = sd_event_source_set_io_fd(bus->output_io_event_source, bus->output_fd);
3117 static void detach_io_events(sd_bus *bus) {
3120 if (bus->input_io_event_source) {
3121 sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
3122 bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
3125 if (bus->output_io_event_source) {
3126 sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
3127 bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
3131 _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
3134 assert_return(bus, -EINVAL);
3135 assert_return(!bus->event, -EBUSY);
3137 assert(!bus->input_io_event_source);
3138 assert(!bus->output_io_event_source);
3139 assert(!bus->time_event_source);
3142 bus->event = sd_event_ref(event);
3144 r = sd_event_default(&bus->event);
3149 bus->event_priority = priority;
3151 r = sd_event_add_time(bus->event, &bus->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, bus);
3155 r = sd_event_source_set_priority(bus->time_event_source, priority);
3159 r = sd_event_source_set_description(bus->time_event_source, "bus-time");
3163 r = sd_event_add_exit(bus->event, &bus->quit_event_source, quit_callback, bus);
3167 r = sd_event_source_set_description(bus->quit_event_source, "bus-exit");
3171 r = attach_io_events(bus);
3178 sd_bus_detach_event(bus);
3182 _public_ int sd_bus_detach_event(sd_bus *bus) {
3183 assert_return(bus, -EINVAL);
3188 detach_io_events(bus);
3190 if (bus->time_event_source) {
3191 sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF);
3192 bus->time_event_source = sd_event_source_unref(bus->time_event_source);
3195 if (bus->quit_event_source) {
3196 sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF);
3197 bus->quit_event_source = sd_event_source_unref(bus->quit_event_source);
3200 bus->event = sd_event_unref(bus->event);
3204 _public_ sd_event* sd_bus_get_event(sd_bus *bus) {
3205 assert_return(bus, NULL);
3210 _public_ sd_bus_message* sd_bus_get_current_message(sd_bus *bus) {
3211 assert_return(bus, NULL);
3213 return bus->current_message;
3216 #if 0 /// UNNEEDED by elogind
3217 _public_ sd_bus_slot* sd_bus_get_current_slot(sd_bus *bus) {
3218 assert_return(bus, NULL);
3220 return bus->current_slot;
3224 _public_ sd_bus_message_handler_t sd_bus_get_current_handler(sd_bus *bus) {
3225 assert_return(bus, NULL);
3227 return bus->current_handler;
3230 _public_ void* sd_bus_get_current_userdata(sd_bus *bus) {
3231 assert_return(bus, NULL);
3233 return bus->current_userdata;
3236 static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
3241 assert(default_bus);
3244 return !!*default_bus;
3247 *ret = sd_bus_ref(*default_bus);
3255 b->default_bus_ptr = default_bus;
3263 _public_ int sd_bus_default_system(sd_bus **ret) {
3264 return bus_default(sd_bus_open_system, &default_system_bus, ret);
3268 _public_ int sd_bus_default_user(sd_bus **ret) {
3269 #if 0 /// elogind does not support user buses
3270 return bus_default(sd_bus_open_user, &default_user_bus, ret);
3272 return sd_bus_default_system(ret);
3276 _public_ int sd_bus_default(sd_bus **ret) {
3280 /* Let's try our best to reuse another cached connection. If
3281 * the starter bus type is set, connect via our normal
3282 * connection logic, ignoring $DBUS_STARTER_ADDRESS, so that
3283 * we can share the connection with the user/system default
3286 e = secure_getenv("DBUS_STARTER_BUS_TYPE");
3288 if (streq(e, "system"))
3289 return sd_bus_default_system(ret);
3290 #if 0 /// elogind does not support systemd units
3291 else if (STR_IN_SET(e, "user", "session"))
3292 return sd_bus_default_user(ret);
3296 /* No type is specified, so we have not other option than to
3297 * use the starter address if it is set. */
3299 e = secure_getenv("DBUS_STARTER_ADDRESS");
3302 return bus_default(sd_bus_open, &default_starter_bus, ret);
3305 /* Finally, if nothing is set use the cached connection for
3306 * the right scope */
3308 #if 0 /// elogind does not support systemd units
3309 if (cg_pid_get_owner_uid(0, NULL) >= 0)
3310 return sd_bus_default_user(ret);
3313 return sd_bus_default_system(ret);
3316 #if 0 /// UNNEEDED by elogind
3317 _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
3318 assert_return(b, -EINVAL);
3319 assert_return(tid, -EINVAL);
3320 assert_return(!bus_pid_changed(b), -ECHILD);
3328 return sd_event_get_tid(b->event, tid);
3333 _public_ int sd_bus_path_encode(const char *prefix, const char *external_id, char **ret_path) {
3334 _cleanup_free_ char *e = NULL;
3337 assert_return(object_path_is_valid(prefix), -EINVAL);
3338 assert_return(external_id, -EINVAL);
3339 assert_return(ret_path, -EINVAL);
3341 e = bus_label_escape(external_id);
3345 ret = strjoin(prefix, "/", e);
3353 _public_ int sd_bus_path_decode(const char *path, const char *prefix, char **external_id) {
3357 assert_return(object_path_is_valid(path), -EINVAL);
3358 assert_return(object_path_is_valid(prefix), -EINVAL);
3359 assert_return(external_id, -EINVAL);
3361 e = object_path_startswith(path, prefix);
3363 *external_id = NULL;
3367 ret = bus_label_unescape(e);
3375 _public_ int sd_bus_path_encode_many(char **out, const char *path_template, ...) {
3376 _cleanup_strv_free_ char **labels = NULL;
3377 char *path, *path_pos, **label_pos;
3378 const char *sep, *template_pos;
3383 assert_return(out, -EINVAL);
3384 assert_return(path_template, -EINVAL);
3386 path_length = strlen(path_template);
3388 va_start(list, path_template);
3389 for (sep = strchr(path_template, '%'); sep; sep = strchr(sep + 1, '%')) {
3393 arg = va_arg(list, const char *);
3399 label = bus_label_escape(arg);
3405 r = strv_consume(&labels, label);
3411 /* add label length, but account for the format character */
3412 path_length += strlen(label) - 1;
3416 path = malloc(path_length + 1);
3423 for (template_pos = path_template; *template_pos; ) {
3424 sep = strchrnul(template_pos, '%');
3425 path_pos = mempcpy(path_pos, template_pos, sep - template_pos);
3429 path_pos = stpcpy(path_pos, *label_pos++);
3430 template_pos = sep + 1;
3438 _public_ int sd_bus_path_decode_many(const char *path, const char *path_template, ...) {
3439 _cleanup_strv_free_ char **labels = NULL;
3440 const char *template_pos, *path_pos;
3446 * This decodes an object-path based on a template argument. The
3447 * template consists of a verbatim path, optionally including special
3450 * - Each occurrence of '%' in the template matches an arbitrary
3451 * substring of a label in the given path. At most one such
3452 * directive is allowed per label. For each such directive, the
3453 * caller must provide an output parameter (char **) via va_arg. If
3454 * NULL is passed, the given label is verified, but not returned.
3455 * For each matched label, the *decoded* label is stored in the
3456 * passed output argument, and the caller is responsible to free
3457 * it. Note that the output arguments are only modified if the
3458 * actualy path matched the template. Otherwise, they're left
3461 * This function returns <0 on error, 0 if the path does not match the
3462 * template, 1 if it matched.
3465 assert_return(path, -EINVAL);
3466 assert_return(path_template, -EINVAL);
3470 for (template_pos = path_template; *template_pos; ) {
3475 /* verify everything until the next '%' matches verbatim */
3476 sep = strchrnul(template_pos, '%');
3477 length = sep - template_pos;
3478 if (strncmp(path_pos, template_pos, length))
3482 template_pos += length;
3487 /* We found the next '%' character. Everything up until here
3488 * matched. We now skip ahead to the end of this label and make
3489 * sure it matches the tail of the label in the path. Then we
3490 * decode the string in-between and save it for later use. */
3492 ++template_pos; /* skip over '%' */
3494 sep = strchrnul(template_pos, '/');
3495 length = sep - template_pos; /* length of suffix to match verbatim */
3497 /* verify the suffixes match */
3498 sep = strchrnul(path_pos, '/');
3499 if (sep - path_pos < (ssize_t)length ||
3500 strncmp(sep - length, template_pos, length))
3503 template_pos += length; /* skip over matched label */
3504 length = sep - path_pos - length; /* length of sub-label to decode */
3506 /* store unescaped label for later use */
3507 label = bus_label_unescape_n(path_pos, length);
3511 r = strv_consume(&labels, label);
3515 path_pos = sep; /* skip decoded label and suffix */
3518 /* end of template must match end of path */
3522 /* copy the labels over to the caller */
3523 va_start(list, path_template);
3524 for (label_pos = labels; label_pos && *label_pos; ++label_pos) {
3527 arg = va_arg(list, char **);
3540 _public_ int sd_bus_try_close(sd_bus *bus) {
3541 assert_return(bus, -EINVAL);
3542 assert_return(!bus_pid_changed(bus), -ECHILD);
3547 _public_ int sd_bus_get_description(sd_bus *bus, const char **description) {
3548 assert_return(bus, -EINVAL);
3549 assert_return(description, -EINVAL);
3550 assert_return(bus->description, -ENXIO);
3551 assert_return(!bus_pid_changed(bus), -ECHILD);
3553 *description = bus->description;
3558 int bus_get_root_path(sd_bus *bus) {
3561 if (bus->cgroup_root)
3564 r = cg_get_root_path(&bus->cgroup_root);
3566 bus->cgroup_root = strdup("/");
3567 if (!bus->cgroup_root)
3576 #if 0 /// UNNEEDED by elogind
3577 _public_ int sd_bus_get_scope(sd_bus *bus, const char **scope) {
3578 assert_return(bus, -EINVAL);
3579 assert_return(scope, -EINVAL);
3580 assert_return(!bus_pid_changed(bus), -ECHILD);
3587 if (bus->is_system) {
3595 _public_ int sd_bus_get_address(sd_bus *bus, const char **address) {
3597 assert_return(bus, -EINVAL);
3598 assert_return(address, -EINVAL);
3599 assert_return(!bus_pid_changed(bus), -ECHILD);
3602 *address = bus->address;
3609 _public_ int sd_bus_get_creds_mask(sd_bus *bus, uint64_t *mask) {
3610 assert_return(bus, -EINVAL);
3611 assert_return(mask, -EINVAL);
3612 assert_return(!bus_pid_changed(bus), -ECHILD);
3614 *mask = bus->creds_mask;
3618 _public_ int sd_bus_is_bus_client(sd_bus *bus) {
3619 assert_return(bus, -EINVAL);
3620 assert_return(!bus_pid_changed(bus), -ECHILD);
3622 return bus->bus_client;
3625 _public_ int sd_bus_is_server(sd_bus *bus) {
3626 assert_return(bus, -EINVAL);
3627 assert_return(!bus_pid_changed(bus), -ECHILD);
3629 return bus->is_server;
3632 _public_ int sd_bus_is_anonymous(sd_bus *bus) {
3633 assert_return(bus, -EINVAL);
3634 assert_return(!bus_pid_changed(bus), -ECHILD);
3636 return bus->anonymous_auth;
3639 _public_ int sd_bus_is_trusted(sd_bus *bus) {
3640 assert_return(bus, -EINVAL);
3641 assert_return(!bus_pid_changed(bus), -ECHILD);
3643 return bus->trusted;
3646 _public_ int sd_bus_is_monitor(sd_bus *bus) {
3647 assert_return(bus, -EINVAL);
3648 assert_return(!bus_pid_changed(bus), -ECHILD);
3650 return !!(bus->hello_flags & KDBUS_HELLO_MONITOR);
3653 static void flush_close(sd_bus *bus) {
3657 /* Flushes and closes the specified bus. We take a ref before,
3658 * to ensure the flushing does not cause the bus to be
3661 sd_bus_flush_close_unref(sd_bus_ref(bus));
3664 _public_ void sd_bus_default_flush_close(void) {
3665 flush_close(default_starter_bus);
3666 flush_close(default_user_bus);
3667 flush_close(default_system_bus);
3671 _public_ int sd_bus_set_exit_on_disconnect(sd_bus *bus, int b) {
3672 assert_return(bus, -EINVAL);
3674 /* Turns on exit-on-disconnect, and triggers it immediately if the bus connection was already
3675 * disconnected. Note that this is triggered exclusively on disconnections triggered by the server side, never
3676 * from the client side. */
3677 bus->exit_on_disconnect = b;
3679 /* If the exit condition was triggered already, exit immediately. */
3680 return bus_exit_now(bus);
3683 #if 0 /// UNNEEDED by elogind
3684 _public_ int sd_bus_get_exit_on_disconnect(sd_bus *bus) {
3685 assert_return(bus, -EINVAL);
3687 return bus->exit_on_disconnect;