1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
39 #include "bus-internal.h"
40 #include "bus-message.h"
42 #include "bus-socket.h"
43 #include "bus-kernel.h"
44 #include "bus-control.h"
45 #include "bus-introspect.h"
46 #include "bus-signature.h"
47 #include "bus-objects.h"
49 #include "bus-container.h"
51 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
53 static void bus_close_fds(sd_bus *b) {
57 close_nointr_nofail(b->input_fd);
59 if (b->output_fd >= 0 && b->output_fd != b->input_fd)
60 close_nointr_nofail(b->output_fd);
62 b->input_fd = b->output_fd = -1;
65 static void bus_node_destroy(sd_bus *b, struct node *n) {
66 struct node_callback *c;
67 struct node_vtable *v;
68 struct node_enumerator *e;
76 bus_node_destroy(b, n->child);
78 while ((c = n->callbacks)) {
79 LIST_REMOVE(callbacks, n->callbacks, c);
83 while ((v = n->vtables)) {
84 LIST_REMOVE(vtables, n->vtables, v);
89 while ((e = n->enumerators)) {
90 LIST_REMOVE(enumerators, n->enumerators, e);
95 LIST_REMOVE(siblings, n->parent->child, n);
97 assert_se(hashmap_remove(b->nodes, n->path) == n);
102 static void bus_reset_queues(sd_bus *b) {
107 for (i = 0; i < b->rqueue_size; i++)
108 sd_bus_message_unref(b->rqueue[i]);
111 for (i = 0; i < b->wqueue_size; i++)
112 sd_bus_message_unref(b->wqueue[i]);
115 b->rqueue = b->wqueue = NULL;
116 b->rqueue_size = b->wqueue_size = 0;
119 static void bus_free(sd_bus *b) {
120 struct filter_callback *f;
125 sd_bus_detach_event(b);
130 munmap(b->kdbus_buffer, KDBUS_POOL_SIZE);
133 free(b->unique_name);
134 free(b->auth_buffer);
140 strv_free(b->exec_argv);
142 close_many(b->fds, b->n_fds);
147 hashmap_free_free(b->reply_callbacks);
148 prioq_free(b->reply_callbacks_prioq);
150 while ((f = b->filter_callbacks)) {
151 LIST_REMOVE(callbacks, b->filter_callbacks, f);
155 bus_match_free(&b->match_callbacks);
157 hashmap_free_free(b->vtable_methods);
158 hashmap_free_free(b->vtable_properties);
160 while ((n = hashmap_first(b->nodes)))
161 bus_node_destroy(b, n);
163 hashmap_free(b->nodes);
165 bus_kernel_flush_memfd(b);
167 assert_se(pthread_mutex_destroy(&b->memfd_cache_mutex) == 0);
172 _public_ int sd_bus_new(sd_bus **ret) {
175 assert_return(ret, -EINVAL);
181 r->n_ref = REFCNT_INIT;
182 r->input_fd = r->output_fd = -1;
183 r->message_version = 1;
184 r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
185 r->attach_flags |= KDBUS_ATTACH_NAMES;
186 r->original_pid = getpid();
188 assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0);
190 /* We guarantee that wqueue always has space for at least one
192 r->wqueue = new(sd_bus_message*, 1);
202 _public_ int sd_bus_set_address(sd_bus *bus, const char *address) {
205 assert_return(bus, -EINVAL);
206 assert_return(bus->state == BUS_UNSET, -EPERM);
207 assert_return(address, -EINVAL);
208 assert_return(!bus_pid_changed(bus), -ECHILD);
220 _public_ int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
221 assert_return(bus, -EINVAL);
222 assert_return(bus->state == BUS_UNSET, -EPERM);
223 assert_return(input_fd >= 0, -EINVAL);
224 assert_return(output_fd >= 0, -EINVAL);
225 assert_return(!bus_pid_changed(bus), -ECHILD);
227 bus->input_fd = input_fd;
228 bus->output_fd = output_fd;
232 _public_ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
235 assert_return(bus, -EINVAL);
236 assert_return(bus->state == BUS_UNSET, -EPERM);
237 assert_return(path, -EINVAL);
238 assert_return(!strv_isempty(argv), -EINVAL);
239 assert_return(!bus_pid_changed(bus), -ECHILD);
251 free(bus->exec_path);
252 strv_free(bus->exec_argv);
260 _public_ int sd_bus_set_bus_client(sd_bus *bus, int b) {
261 assert_return(bus, -EINVAL);
262 assert_return(bus->state == BUS_UNSET, -EPERM);
263 assert_return(!bus_pid_changed(bus), -ECHILD);
265 bus->bus_client = !!b;
269 _public_ int sd_bus_negotiate_fds(sd_bus *bus, int b) {
270 assert_return(bus, -EINVAL);
271 assert_return(bus->state == BUS_UNSET, -EPERM);
272 assert_return(!bus_pid_changed(bus), -ECHILD);
274 SET_FLAG(bus->hello_flags, KDBUS_HELLO_ACCEPT_FD, b);
278 _public_ int sd_bus_negotiate_attach_timestamp(sd_bus *bus, int b) {
279 assert_return(bus, -EINVAL);
280 assert_return(bus->state == BUS_UNSET, -EPERM);
281 assert_return(!bus_pid_changed(bus), -ECHILD);
283 SET_FLAG(bus->attach_flags, KDBUS_ATTACH_TIMESTAMP, b);
287 _public_ int sd_bus_negotiate_attach_creds(sd_bus *bus, uint64_t mask) {
288 assert_return(bus, -EINVAL);
289 assert_return(mask <= _SD_BUS_CREDS_MAX, -EINVAL);
290 assert_return(bus->state == BUS_UNSET, -EPERM);
291 assert_return(!bus_pid_changed(bus), -ECHILD);
293 /* The well knowns we need unconditionally, so that matches can work */
294 mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES;
296 return kdbus_translate_attach_flags(mask, &bus->creds_mask);
299 _public_ int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
300 assert_return(bus, -EINVAL);
301 assert_return(b || sd_id128_equal(server_id, SD_ID128_NULL), -EINVAL);
302 assert_return(bus->state == BUS_UNSET, -EPERM);
303 assert_return(!bus_pid_changed(bus), -ECHILD);
305 bus->is_server = !!b;
306 bus->server_id = server_id;
310 _public_ int sd_bus_set_anonymous(sd_bus *bus, int b) {
311 assert_return(bus, -EINVAL);
312 assert_return(bus->state == BUS_UNSET, -EPERM);
313 assert_return(!bus_pid_changed(bus), -ECHILD);
315 bus->anonymous_auth = !!b;
319 static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
324 assert(bus->state == BUS_HELLO || bus->state == BUS_CLOSING);
327 r = sd_bus_message_get_errno(reply);
333 r = sd_bus_message_read(reply, "s", &s);
337 if (!service_name_is_valid(s) || s[0] != ':')
340 bus->unique_name = strdup(s);
341 if (!bus->unique_name)
344 if (bus->state == BUS_HELLO)
345 bus->state = BUS_RUNNING;
350 static int bus_send_hello(sd_bus *bus) {
351 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
356 if (!bus->bus_client || bus->is_kernel)
359 r = sd_bus_message_new_method_call(
361 "org.freedesktop.DBus",
363 "org.freedesktop.DBus",
369 return sd_bus_call_async(bus, m, hello_callback, NULL, 0, &bus->hello_serial);
372 int bus_start_running(sd_bus *bus) {
375 if (bus->bus_client && !bus->is_kernel) {
376 bus->state = BUS_HELLO;
380 bus->state = BUS_RUNNING;
384 static int parse_address_key(const char **p, const char *key, char **value) {
395 if (strncmp(*p, key, l) != 0)
408 while (*a != ';' && *a != ',' && *a != 0) {
426 c = (char) ((x << 4) | y);
433 t = realloc(r, n + 2);
461 static void skip_address_key(const char **p) {
465 *p += strcspn(*p, ",");
471 static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
472 _cleanup_free_ char *path = NULL, *abstract = NULL;
481 while (**p != 0 && **p != ';') {
482 r = parse_address_key(p, "guid", guid);
488 r = parse_address_key(p, "path", &path);
494 r = parse_address_key(p, "abstract", &abstract);
503 if (!path && !abstract)
506 if (path && abstract)
511 if (l > sizeof(b->sockaddr.un.sun_path))
514 b->sockaddr.un.sun_family = AF_UNIX;
515 strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path));
516 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l;
517 } else if (abstract) {
518 l = strlen(abstract);
519 if (l > sizeof(b->sockaddr.un.sun_path) - 1)
522 b->sockaddr.un.sun_family = AF_UNIX;
523 b->sockaddr.un.sun_path[0] = 0;
524 strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1);
525 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
531 static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
532 _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
534 struct addrinfo *result, hints = {
535 .ai_socktype = SOCK_STREAM,
536 .ai_flags = AI_ADDRCONFIG,
544 while (**p != 0 && **p != ';') {
545 r = parse_address_key(p, "guid", guid);
551 r = parse_address_key(p, "host", &host);
557 r = parse_address_key(p, "port", &port);
563 r = parse_address_key(p, "family", &family);
576 if (streq(family, "ipv4"))
577 hints.ai_family = AF_INET;
578 else if (streq(family, "ipv6"))
579 hints.ai_family = AF_INET6;
584 r = getaddrinfo(host, port, &hints, &result);
588 return -EADDRNOTAVAIL;
590 memcpy(&b->sockaddr, result->ai_addr, result->ai_addrlen);
591 b->sockaddr_size = result->ai_addrlen;
593 freeaddrinfo(result);
598 static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
600 unsigned n_argv = 0, j;
609 while (**p != 0 && **p != ';') {
610 r = parse_address_key(p, "guid", guid);
616 r = parse_address_key(p, "path", &path);
622 if (startswith(*p, "argv")) {
626 ul = strtoul(*p + 4, (char**) p, 10);
627 if (errno > 0 || **p != '=' || ul > 256) {
637 x = realloc(argv, sizeof(char*) * (ul + 2));
643 memset(x + n_argv, 0, sizeof(char*) * (ul - n_argv + 2));
649 r = parse_address_key(p, NULL, argv + ul);
664 /* Make sure there are no holes in the array, with the
665 * exception of argv[0] */
666 for (j = 1; j < n_argv; j++)
672 if (argv && argv[0] == NULL) {
673 argv[0] = strdup(path);
685 for (j = 0; j < n_argv; j++)
693 static int parse_kernel_address(sd_bus *b, const char **p, char **guid) {
694 _cleanup_free_ char *path = NULL;
702 while (**p != 0 && **p != ';') {
703 r = parse_address_key(p, "guid", guid);
709 r = parse_address_key(p, "path", &path);
728 static int parse_container_address(sd_bus *b, const char **p, char **guid) {
729 _cleanup_free_ char *machine = NULL;
737 while (**p != 0 && **p != ';') {
738 r = parse_address_key(p, "guid", guid);
744 r = parse_address_key(p, "machine", &machine);
757 b->machine = machine;
760 b->sockaddr.un.sun_family = AF_UNIX;
761 strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
762 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/var/run/dbus/system_bus_socket") - 1;
767 static void bus_reset_parsed_address(sd_bus *b) {
771 b->sockaddr_size = 0;
772 strv_free(b->exec_argv);
776 b->server_id = SD_ID128_NULL;
783 static int bus_parse_next_address(sd_bus *b) {
784 _cleanup_free_ char *guid = NULL;
792 if (b->address[b->address_index] == 0)
795 bus_reset_parsed_address(b);
797 a = b->address + b->address_index;
806 if (startswith(a, "unix:")) {
809 r = parse_unix_address(b, &a, &guid);
814 } else if (startswith(a, "tcp:")) {
817 r = parse_tcp_address(b, &a, &guid);
823 } else if (startswith(a, "unixexec:")) {
826 r = parse_exec_address(b, &a, &guid);
832 } else if (startswith(a, "kernel:")) {
835 r = parse_kernel_address(b, &a, &guid);
840 } else if (startswith(a, "x-container:")) {
843 r = parse_container_address(b, &a, &guid);
856 r = sd_id128_from_string(guid, &b->server_id);
861 b->address_index = a - b->address;
865 static int bus_start_address(sd_bus *b) {
875 r = bus_socket_exec(b);
879 b->last_connect_error = -r;
880 } else if (b->kernel) {
882 r = bus_kernel_connect(b);
886 b->last_connect_error = -r;
888 } else if (b->machine) {
890 r = bus_container_connect(b);
894 b->last_connect_error = -r;
896 } else if (b->sockaddr.sa.sa_family != AF_UNSPEC) {
898 r = bus_socket_connect(b);
902 b->last_connect_error = -r;
905 r = bus_parse_next_address(b);
909 return b->last_connect_error ? -b->last_connect_error : -ECONNREFUSED;
913 int bus_next_address(sd_bus *b) {
916 bus_reset_parsed_address(b);
917 return bus_start_address(b);
920 static int bus_start_fd(sd_bus *b) {
925 assert(b->input_fd >= 0);
926 assert(b->output_fd >= 0);
928 r = fd_nonblock(b->input_fd, true);
932 r = fd_cloexec(b->input_fd, true);
936 if (b->input_fd != b->output_fd) {
937 r = fd_nonblock(b->output_fd, true);
941 r = fd_cloexec(b->output_fd, true);
946 if (fstat(b->input_fd, &st) < 0)
949 if (S_ISCHR(b->input_fd))
950 return bus_kernel_take_fd(b);
952 return bus_socket_take_fd(b);
955 _public_ int sd_bus_start(sd_bus *bus) {
958 assert_return(bus, -EINVAL);
959 assert_return(bus->state == BUS_UNSET, -EPERM);
960 assert_return(!bus_pid_changed(bus), -ECHILD);
962 bus->state = BUS_OPENING;
964 if (bus->is_server && bus->bus_client)
967 if (bus->input_fd >= 0)
968 r = bus_start_fd(bus);
969 else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel || bus->machine)
970 r = bus_start_address(bus);
977 return bus_send_hello(bus);
980 _public_ int sd_bus_open_system(sd_bus **ret) {
985 assert_return(ret, -EINVAL);
991 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
993 r = sd_bus_set_address(b, e);
995 r = sd_bus_set_address(b, "kernel:path=/dev/kdbus/0-system/bus;unix:path=/run/dbus/system_bus_socket");
1000 b->bus_client = true;
1002 r = sd_bus_start(b);
1014 _public_ int sd_bus_open_user(sd_bus **ret) {
1019 assert_return(ret, -EINVAL);
1025 e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1027 r = sd_bus_set_address(b, e);
1031 e = secure_getenv("XDG_RUNTIME_DIR");
1033 _cleanup_free_ char *ee = NULL;
1035 ee = bus_address_escape(e);
1041 asprintf(&b->address, "kernel:path=/dev/kdbus/%lu-user/bus;unix:path=%s/bus", (unsigned long) getuid(), ee);
1043 asprintf(&b->address, "kernel:path=/dev/kdbus/%lu-user/bus", (unsigned long) getuid());
1051 b->bus_client = true;
1053 r = sd_bus_start(b);
1065 _public_ int sd_bus_open_system_remote(const char *host, sd_bus **ret) {
1066 _cleanup_free_ char *e = NULL;
1071 assert_return(host, -EINVAL);
1072 assert_return(ret, -EINVAL);
1074 e = bus_address_escape(host);
1078 p = strjoin("unixexec:path=ssh,argv1=-xT,argv2=", e, ",argv3=systemd-stdio-bridge", NULL);
1082 r = sd_bus_new(&bus);
1089 bus->bus_client = true;
1091 r = sd_bus_start(bus);
1101 _public_ int sd_bus_open_system_container(const char *machine, sd_bus **ret) {
1102 _cleanup_free_ char *e = NULL;
1107 assert_return(machine, -EINVAL);
1108 assert_return(ret, -EINVAL);
1110 e = bus_address_escape(machine);
1114 p = strjoin("x-container:machine=", e, NULL);
1118 r = sd_bus_new(&bus);
1125 bus->bus_client = true;
1127 r = sd_bus_start(bus);
1137 _public_ void sd_bus_close(sd_bus *bus) {
1141 if (bus->state == BUS_CLOSED)
1143 if (bus_pid_changed(bus))
1146 bus->state = BUS_CLOSED;
1148 sd_bus_detach_event(bus);
1150 /* Drop all queued messages so that they drop references to
1151 * the bus object and the bus may be freed */
1152 bus_reset_queues(bus);
1154 if (!bus->is_kernel)
1157 /* We'll leave the fd open in case this is a kernel bus, since
1158 * there might still be memblocks around that reference this
1159 * bus, and they might need to invoke the * KDBUS_CMD_FREE
1160 * ioctl on the fd when they are freed. */
1163 static void bus_enter_closing(sd_bus *bus) {
1166 if (bus->state != BUS_OPENING &&
1167 bus->state != BUS_AUTHENTICATING &&
1168 bus->state != BUS_HELLO &&
1169 bus->state != BUS_RUNNING)
1172 bus->state = BUS_CLOSING;
1175 _public_ sd_bus *sd_bus_ref(sd_bus *bus) {
1176 assert_return(bus, NULL);
1178 assert_se(REFCNT_INC(bus->n_ref) >= 2);
1183 _public_ sd_bus *sd_bus_unref(sd_bus *bus) {
1184 assert_return(bus, NULL);
1186 if (REFCNT_DEC(bus->n_ref) <= 0)
1192 _public_ int sd_bus_is_open(sd_bus *bus) {
1194 assert_return(bus, -EINVAL);
1195 assert_return(!bus_pid_changed(bus), -ECHILD);
1197 return BUS_IS_OPEN(bus->state);
1200 _public_ int sd_bus_can_send(sd_bus *bus, char type) {
1203 assert_return(bus, -EINVAL);
1204 assert_return(bus->state != BUS_UNSET, -ENOTCONN);
1205 assert_return(!bus_pid_changed(bus), -ECHILD);
1207 if (type == SD_BUS_TYPE_UNIX_FD) {
1208 if (!(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD))
1211 r = bus_ensure_running(bus);
1215 return bus->can_fds;
1218 return bus_type_is_valid(type);
1221 _public_ int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
1224 assert_return(bus, -EINVAL);
1225 assert_return(server_id, -EINVAL);
1226 assert_return(!bus_pid_changed(bus), -ECHILD);
1228 r = bus_ensure_running(bus);
1232 *server_id = bus->server_id;
1236 static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
1240 if (m->header->version > b->message_version)
1244 /* If we copy the same message to multiple
1245 * destinations, avoid using the same serial
1247 b->serial = MAX(b->serial, BUS_MESSAGE_SERIAL(m));
1251 return bus_message_seal(m, ++b->serial);
1254 int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
1258 if (m->header->version > b->message_version)
1261 /* The bus specification says the serial number cannot be 0,
1262 * hence let's fill something in for synthetic messages. Since
1263 * synthetic messages might have a fake sender and we don't
1264 * want to interfere with the real sender's serial numbers we
1265 * pick a fixed, artifical one. We use (uint32_t) -1 rather
1266 * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
1267 * even though kdbus can do 64bit. */
1269 return bus_message_seal(m, 0xFFFFFFFFULL);
1272 static int bus_write_message(sd_bus *bus, sd_bus_message *message, size_t *idx) {
1279 return bus_kernel_write_message(bus, message);
1281 return bus_socket_write_message(bus, message, idx);
1286 static int dispatch_wqueue(sd_bus *bus) {
1290 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1292 while (bus->wqueue_size > 0) {
1294 r = bus_write_message(bus, bus->wqueue[0], &bus->windex);
1298 /* Didn't do anything this time */
1300 else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
1301 /* Fully written. Let's drop the entry from
1304 * This isn't particularly optimized, but
1305 * well, this is supposed to be our worst-case
1306 * buffer only, and the socket buffer is
1307 * supposed to be our primary buffer, and if
1308 * it got full, then all bets are off
1311 sd_bus_message_unref(bus->wqueue[0]);
1312 bus->wqueue_size --;
1313 memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
1323 static int bus_read_message(sd_bus *bus) {
1327 return bus_kernel_read_message(bus);
1329 return bus_socket_read_message(bus);
1332 int bus_rqueue_make_room(sd_bus *bus) {
1336 x = bus->rqueue_size + 1;
1338 if (bus->rqueue_allocated >= x)
1341 if (x > BUS_RQUEUE_MAX)
1344 q = realloc(bus->rqueue, x * sizeof(sd_bus_message*));
1349 bus->rqueue_allocated = x;
1354 static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
1359 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1362 if (bus->rqueue_size > 0) {
1363 /* Dispatch a queued message */
1365 *m = bus->rqueue[0];
1366 bus->rqueue_size --;
1367 memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
1371 /* Try to read a new message */
1372 r = bus_read_message(bus);
1382 _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
1385 assert_return(bus, -EINVAL);
1386 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1387 assert_return(m, -EINVAL);
1388 assert_return(!bus_pid_changed(bus), -ECHILD);
1391 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
1398 /* If the serial number isn't kept, then we know that no reply
1400 if (!serial && !m->sealed)
1401 m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
1403 r = bus_seal_message(bus, m);
1407 /* If this is a reply and no reply was requested, then let's
1408 * suppress this, if we can */
1409 if (m->dont_send && !serial)
1412 if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
1415 r = bus_write_message(bus, m, &idx);
1417 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1418 bus_enter_closing(bus);
1421 } else if (!bus->is_kernel && idx < BUS_MESSAGE_SIZE(m)) {
1422 /* Wasn't fully written. So let's remember how
1423 * much was written. Note that the first entry
1424 * of the wqueue array is always allocated so
1425 * that we always can remember how much was
1427 bus->wqueue[0] = sd_bus_message_ref(m);
1428 bus->wqueue_size = 1;
1434 /* Just append it to the queue. */
1436 if (bus->wqueue_size >= BUS_WQUEUE_MAX)
1439 q = realloc(bus->wqueue, sizeof(sd_bus_message*) * (bus->wqueue_size + 1));
1444 q[bus->wqueue_size ++] = sd_bus_message_ref(m);
1448 *serial = BUS_MESSAGE_SERIAL(m);
1453 _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *serial) {
1456 assert_return(bus, -EINVAL);
1457 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1458 assert_return(m, -EINVAL);
1459 assert_return(!bus_pid_changed(bus), -ECHILD);
1461 if (!streq_ptr(m->destination, destination)) {
1466 r = sd_bus_message_set_destination(m, destination);
1471 return sd_bus_send(bus, m, serial);
1474 static usec_t calc_elapse(uint64_t usec) {
1475 if (usec == (uint64_t) -1)
1479 usec = BUS_DEFAULT_TIMEOUT;
1481 return now(CLOCK_MONOTONIC) + usec;
1484 static int timeout_compare(const void *a, const void *b) {
1485 const struct reply_callback *x = a, *y = b;
1487 if (x->timeout != 0 && y->timeout == 0)
1490 if (x->timeout == 0 && y->timeout != 0)
1493 if (x->timeout < y->timeout)
1496 if (x->timeout > y->timeout)
1502 _public_ int sd_bus_call_async(
1505 sd_bus_message_handler_t callback,
1510 struct reply_callback *c;
1513 assert_return(bus, -EINVAL);
1514 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1515 assert_return(m, -EINVAL);
1516 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1517 assert_return(!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1518 assert_return(callback, -EINVAL);
1519 assert_return(!bus_pid_changed(bus), -ECHILD);
1521 r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
1525 if (usec != (uint64_t) -1) {
1526 r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
1531 r = bus_seal_message(bus, m);
1535 c = new0(struct reply_callback, 1);
1539 c->callback = callback;
1540 c->userdata = userdata;
1541 c->serial = BUS_MESSAGE_SERIAL(m);
1542 c->timeout = calc_elapse(usec);
1544 r = hashmap_put(bus->reply_callbacks, &c->serial, c);
1550 if (c->timeout != 0) {
1551 r = prioq_put(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1554 sd_bus_call_async_cancel(bus, c->serial);
1559 r = sd_bus_send(bus, m, serial);
1561 sd_bus_call_async_cancel(bus, c->serial);
1568 _public_ int sd_bus_call_async_cancel(sd_bus *bus, uint64_t serial) {
1569 struct reply_callback *c;
1571 assert_return(bus, -EINVAL);
1572 assert_return(serial != 0, -EINVAL);
1573 assert_return(!bus_pid_changed(bus), -ECHILD);
1575 c = hashmap_remove(bus->reply_callbacks, &serial);
1579 if (c->timeout != 0)
1580 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1586 int bus_ensure_running(sd_bus *bus) {
1591 if (bus->state == BUS_UNSET || bus->state == BUS_CLOSED || bus->state == BUS_CLOSING)
1593 if (bus->state == BUS_RUNNING)
1597 r = sd_bus_process(bus, NULL);
1600 if (bus->state == BUS_RUNNING)
1605 r = sd_bus_wait(bus, (uint64_t) -1);
1611 _public_ int sd_bus_call(
1615 sd_bus_error *error,
1616 sd_bus_message **reply) {
1623 assert_return(bus, -EINVAL);
1624 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1625 assert_return(m, -EINVAL);
1626 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1627 assert_return(!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1628 assert_return(!bus_error_is_dirty(error), -EINVAL);
1629 assert_return(!bus_pid_changed(bus), -ECHILD);
1631 r = bus_ensure_running(bus);
1635 i = bus->rqueue_size;
1637 r = sd_bus_send(bus, m, &serial);
1641 timeout = calc_elapse(usec);
1646 while (i < bus->rqueue_size) {
1647 sd_bus_message *incoming = NULL;
1649 incoming = bus->rqueue[i];
1651 if (incoming->reply_serial == serial) {
1652 /* Found a match! */
1654 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1657 if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
1662 sd_bus_message_unref(incoming);
1665 } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
1666 r = sd_bus_error_copy(error, &incoming->error);
1670 sd_bus_message_unref(incoming);
1673 } else if (incoming->header->serial == serial &&
1676 streq(bus->unique_name, incoming->sender)) {
1678 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1681 /* Our own message? Somebody is trying
1682 * to send its own client a message,
1683 * let's not dead-lock, let's fail
1686 sd_bus_message_unref(incoming);
1690 /* Try to read more, right-away */
1694 r = bus_read_message(bus);
1696 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1697 bus_enter_closing(bus);
1707 n = now(CLOCK_MONOTONIC);
1713 left = (uint64_t) -1;
1715 r = bus_poll(bus, true, left);
1719 r = dispatch_wqueue(bus);
1721 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1722 bus_enter_closing(bus);
1729 _public_ int sd_bus_get_fd(sd_bus *bus) {
1731 assert_return(bus, -EINVAL);
1732 assert_return(bus->input_fd == bus->output_fd, -EPERM);
1733 assert_return(!bus_pid_changed(bus), -ECHILD);
1735 return bus->input_fd;
1738 _public_ int sd_bus_get_events(sd_bus *bus) {
1741 assert_return(bus, -EINVAL);
1742 assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
1743 assert_return(!bus_pid_changed(bus), -ECHILD);
1745 if (bus->state == BUS_OPENING)
1747 else if (bus->state == BUS_AUTHENTICATING) {
1749 if (bus_socket_auth_needs_write(bus))
1754 } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
1755 if (bus->rqueue_size <= 0)
1757 if (bus->wqueue_size > 0)
1764 _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
1765 struct reply_callback *c;
1767 assert_return(bus, -EINVAL);
1768 assert_return(timeout_usec, -EINVAL);
1769 assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
1770 assert_return(!bus_pid_changed(bus), -ECHILD);
1772 if (bus->state == BUS_CLOSING) {
1777 if (bus->state == BUS_AUTHENTICATING) {
1778 *timeout_usec = bus->auth_timeout;
1782 if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) {
1783 *timeout_usec = (uint64_t) -1;
1787 if (bus->rqueue_size > 0) {
1792 c = prioq_peek(bus->reply_callbacks_prioq);
1794 *timeout_usec = (uint64_t) -1;
1798 *timeout_usec = c->timeout;
1802 static int process_timeout(sd_bus *bus) {
1803 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1804 _cleanup_bus_message_unref_ sd_bus_message* m = NULL;
1805 struct reply_callback *c;
1811 c = prioq_peek(bus->reply_callbacks_prioq);
1815 n = now(CLOCK_MONOTONIC);
1819 r = bus_message_new_synthetic_error(
1822 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
1827 m->sender = "org.freedesktop.DBus";
1829 r = bus_seal_synthetic_message(bus, m);
1833 assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
1834 hashmap_remove(bus->reply_callbacks, &c->serial);
1837 bus->iteration_counter ++;
1839 r = c->callback(bus, m, c->userdata, &error_buffer);
1840 r = bus_maybe_reply_error(m, r, &error_buffer);
1843 bus->current = NULL;
1848 static int process_hello(sd_bus *bus, sd_bus_message *m) {
1852 if (bus->state != BUS_HELLO)
1855 /* Let's make sure the first message on the bus is the HELLO
1856 * reply. But note that we don't actually parse the message
1857 * here (we leave that to the usual handling), we just verify
1858 * we don't let any earlier msg through. */
1860 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
1861 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1864 if (m->reply_serial != bus->hello_serial)
1870 static int process_reply(sd_bus *bus, sd_bus_message *m) {
1871 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1872 struct reply_callback *c;
1878 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
1879 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1882 c = hashmap_remove(bus->reply_callbacks, &m->reply_serial);
1886 if (c->timeout != 0)
1887 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1889 r = sd_bus_message_rewind(m, true);
1893 r = c->callback(bus, m, c->userdata, &error_buffer);
1894 r = bus_maybe_reply_error(m, r, &error_buffer);
1900 static int process_filter(sd_bus *bus, sd_bus_message *m) {
1901 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1902 struct filter_callback *l;
1909 bus->filter_callbacks_modified = false;
1911 LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
1913 if (bus->filter_callbacks_modified)
1916 /* Don't run this more than once per iteration */
1917 if (l->last_iteration == bus->iteration_counter)
1920 l->last_iteration = bus->iteration_counter;
1922 r = sd_bus_message_rewind(m, true);
1926 r = l->callback(bus, m, l->userdata, &error_buffer);
1927 r = bus_maybe_reply_error(m, r, &error_buffer);
1933 } while (bus->filter_callbacks_modified);
1938 static int process_match(sd_bus *bus, sd_bus_message *m) {
1945 bus->match_callbacks_modified = false;
1947 r = bus_match_run(bus, &bus->match_callbacks, m);
1951 } while (bus->match_callbacks_modified);
1956 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
1957 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1963 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
1966 if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
1969 if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
1972 if (streq_ptr(m->member, "Ping"))
1973 r = sd_bus_message_new_method_return(m, &reply);
1974 else if (streq_ptr(m->member, "GetMachineId")) {
1978 r = sd_id128_get_machine(&id);
1982 r = sd_bus_message_new_method_return(m, &reply);
1986 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
1988 r = sd_bus_message_new_method_errorf(
1990 SD_BUS_ERROR_UNKNOWN_METHOD,
1991 "Unknown method '%s' on interface '%s'.", m->member, m->interface);
1997 r = sd_bus_send(bus, reply, NULL);
2004 static int process_message(sd_bus *bus, sd_bus_message *m) {
2011 bus->iteration_counter++;
2013 log_debug("Got message sender=%s object=%s interface=%s member=%s",
2014 strna(sd_bus_message_get_sender(m)),
2015 strna(sd_bus_message_get_path(m)),
2016 strna(sd_bus_message_get_interface(m)),
2017 strna(sd_bus_message_get_member(m)));
2019 r = process_hello(bus, m);
2023 r = process_reply(bus, m);
2027 r = process_filter(bus, m);
2031 r = process_match(bus, m);
2035 r = process_builtin(bus, m);
2039 r = bus_process_object(bus, m);
2042 bus->current = NULL;
2046 static int process_running(sd_bus *bus, sd_bus_message **ret) {
2047 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2051 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
2053 r = process_timeout(bus);
2057 r = dispatch_wqueue(bus);
2061 r = dispatch_rqueue(bus, &m);
2067 r = process_message(bus, m);
2072 r = sd_bus_message_rewind(m, true);
2081 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
2083 r = sd_bus_reply_method_errorf(
2085 SD_BUS_ERROR_UNKNOWN_OBJECT,
2086 "Unknown object '%s'.", m->path);
2100 static int process_closing(sd_bus *bus, sd_bus_message **ret) {
2101 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2102 struct reply_callback *c;
2106 assert(bus->state == BUS_CLOSING);
2108 c = hashmap_first(bus->reply_callbacks);
2110 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2112 /* First, fail all outstanding method calls */
2113 r = bus_message_new_synthetic_error(
2116 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
2121 r = bus_seal_synthetic_message(bus, m);
2125 if (c->timeout != 0)
2126 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2128 hashmap_remove(bus->reply_callbacks, &c->serial);
2131 bus->iteration_counter++;
2133 r = c->callback(bus, m, c->userdata, &error_buffer);
2134 r = bus_maybe_reply_error(m, r, &error_buffer);
2140 /* Then, synthesize a Disconnected message */
2141 r = sd_bus_message_new_signal(
2143 "/org/freedesktop/DBus/Local",
2144 "org.freedesktop.DBus.Local",
2150 m->sender = "org.freedesktop.DBus.Local";
2152 r = bus_seal_synthetic_message(bus, m);
2159 bus->iteration_counter++;
2161 r = process_filter(bus, m);
2165 r = process_match(bus, m);
2177 bus->current = NULL;
2181 _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
2182 BUS_DONT_DESTROY(bus);
2185 /* Returns 0 when we didn't do anything. This should cause the
2186 * caller to invoke sd_bus_wait() before returning the next
2187 * time. Returns > 0 when we did something, which possibly
2188 * means *ret is filled in with an unprocessed message. */
2190 assert_return(bus, -EINVAL);
2191 assert_return(!bus_pid_changed(bus), -ECHILD);
2193 /* We don't allow recursively invoking sd_bus_process(). */
2194 assert_return(!bus->current, -EBUSY);
2196 switch (bus->state) {
2203 r = bus_socket_process_opening(bus);
2204 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2205 bus_enter_closing(bus);
2213 case BUS_AUTHENTICATING:
2214 r = bus_socket_process_authenticating(bus);
2215 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2216 bus_enter_closing(bus);
2228 r = process_running(bus, ret);
2229 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2230 bus_enter_closing(bus);
2240 return process_closing(bus, ret);
2243 assert_not_reached("Unknown state");
2246 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
2247 struct pollfd p[2] = {};
2250 usec_t m = (usec_t) -1;
2254 if (bus->state == BUS_CLOSING)
2257 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2259 e = sd_bus_get_events(bus);
2264 /* The caller really needs some more data, he doesn't
2265 * care about what's already read, or any timeouts
2270 /* The caller wants to process if there's something to
2271 * process, but doesn't care otherwise */
2273 r = sd_bus_get_timeout(bus, &until);
2278 nw = now(CLOCK_MONOTONIC);
2279 m = until > nw ? until - nw : 0;
2283 if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
2286 p[0].fd = bus->input_fd;
2287 if (bus->output_fd == bus->input_fd) {
2291 p[0].events = e & POLLIN;
2292 p[1].fd = bus->output_fd;
2293 p[1].events = e & POLLOUT;
2297 r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
2301 return r > 0 ? 1 : 0;
2304 _public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
2306 assert_return(bus, -EINVAL);
2307 assert_return(!bus_pid_changed(bus), -ECHILD);
2309 if (bus->state == BUS_CLOSING)
2312 assert_return(BUS_IS_OPEN(bus->state) , -ENOTCONN);
2314 if (bus->rqueue_size > 0)
2317 return bus_poll(bus, false, timeout_usec);
2320 _public_ int sd_bus_flush(sd_bus *bus) {
2323 assert_return(bus, -EINVAL);
2324 assert_return(!bus_pid_changed(bus), -ECHILD);
2326 if (bus->state == BUS_CLOSING)
2329 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2331 r = bus_ensure_running(bus);
2335 if (bus->wqueue_size <= 0)
2339 r = dispatch_wqueue(bus);
2341 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
2342 bus_enter_closing(bus);
2347 if (bus->wqueue_size <= 0)
2350 r = bus_poll(bus, false, (uint64_t) -1);
2356 _public_ int sd_bus_add_filter(sd_bus *bus,
2357 sd_bus_message_handler_t callback,
2360 struct filter_callback *f;
2362 assert_return(bus, -EINVAL);
2363 assert_return(callback, -EINVAL);
2364 assert_return(!bus_pid_changed(bus), -ECHILD);
2366 f = new0(struct filter_callback, 1);
2369 f->callback = callback;
2370 f->userdata = userdata;
2372 bus->filter_callbacks_modified = true;
2373 LIST_PREPEND(callbacks, bus->filter_callbacks, f);
2377 _public_ int sd_bus_remove_filter(sd_bus *bus,
2378 sd_bus_message_handler_t callback,
2381 struct filter_callback *f;
2383 assert_return(bus, -EINVAL);
2384 assert_return(callback, -EINVAL);
2385 assert_return(!bus_pid_changed(bus), -ECHILD);
2387 LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
2388 if (f->callback == callback && f->userdata == userdata) {
2389 bus->filter_callbacks_modified = true;
2390 LIST_REMOVE(callbacks, bus->filter_callbacks, f);
2399 _public_ int sd_bus_add_match(sd_bus *bus,
2401 sd_bus_message_handler_t callback,
2404 struct bus_match_component *components = NULL;
2405 unsigned n_components = 0;
2406 uint64_t cookie = 0;
2409 assert_return(bus, -EINVAL);
2410 assert_return(match, -EINVAL);
2411 assert_return(!bus_pid_changed(bus), -ECHILD);
2413 r = bus_match_parse(match, &components, &n_components);
2417 if (bus->bus_client) {
2418 cookie = ++bus->match_cookie;
2420 r = bus_add_match_internal(bus, match, components, n_components, cookie);
2425 bus->match_callbacks_modified = true;
2426 r = bus_match_add(&bus->match_callbacks, components, n_components, callback, userdata, cookie, NULL);
2428 if (bus->bus_client)
2429 bus_remove_match_internal(bus, match, cookie);
2433 bus_match_parse_free(components, n_components);
2437 _public_ int sd_bus_remove_match(sd_bus *bus,
2439 sd_bus_message_handler_t callback,
2442 struct bus_match_component *components = NULL;
2443 unsigned n_components = 0;
2445 uint64_t cookie = 0;
2447 assert_return(bus, -EINVAL);
2448 assert_return(match, -EINVAL);
2449 assert_return(!bus_pid_changed(bus), -ECHILD);
2451 r = bus_match_parse(match, &components, &n_components);
2455 bus->match_callbacks_modified = true;
2456 r = bus_match_remove(&bus->match_callbacks, components, n_components, callback, userdata, &cookie);
2458 if (bus->bus_client)
2459 q = bus_remove_match_internal(bus, match, cookie);
2461 bus_match_parse_free(components, n_components);
2463 return r < 0 ? r : q;
2466 bool bus_pid_changed(sd_bus *bus) {
2469 /* We don't support people creating a bus connection and
2470 * keeping it around over a fork(). Let's complain. */
2472 return bus->original_pid != getpid();
2475 static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2476 sd_bus *bus = userdata;
2481 r = sd_bus_process(bus, NULL);
2488 static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
2489 sd_bus *bus = userdata;
2494 r = sd_bus_process(bus, NULL);
2501 static int prepare_callback(sd_event_source *s, void *userdata) {
2502 sd_bus *bus = userdata;
2509 e = sd_bus_get_events(bus);
2513 if (bus->output_fd != bus->input_fd) {
2515 r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN);
2519 r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT);
2523 r = sd_event_source_set_io_events(bus->input_io_event_source, e);
2528 r = sd_bus_get_timeout(bus, &until);
2534 j = sd_event_source_set_time(bus->time_event_source, until);
2539 r = sd_event_source_set_enabled(bus->time_event_source, r > 0);
2546 static int quit_callback(sd_event_source *event, void *userdata) {
2547 sd_bus *bus = userdata;
2556 _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
2559 assert_return(bus, -EINVAL);
2560 assert_return(!bus->event, -EBUSY);
2562 assert(!bus->input_io_event_source);
2563 assert(!bus->output_io_event_source);
2564 assert(!bus->time_event_source);
2567 bus->event = sd_event_ref(event);
2569 r = sd_event_default(&bus->event);
2574 r = sd_event_add_io(bus->event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source);
2578 r = sd_event_source_set_priority(bus->input_io_event_source, priority);
2582 if (bus->output_fd != bus->input_fd) {
2583 r = sd_event_add_io(bus->event, bus->output_fd, 0, io_callback, bus, &bus->output_io_event_source);
2587 r = sd_event_source_set_priority(bus->output_io_event_source, priority);
2592 r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
2596 r = sd_event_add_monotonic(bus->event, 0, 0, time_callback, bus, &bus->time_event_source);
2600 r = sd_event_source_set_priority(bus->time_event_source, priority);
2604 r = sd_event_add_quit(bus->event, quit_callback, bus, &bus->quit_event_source);
2611 sd_bus_detach_event(bus);
2615 _public_ int sd_bus_detach_event(sd_bus *bus) {
2616 assert_return(bus, -EINVAL);
2617 assert_return(bus->event, -ENXIO);
2619 if (bus->input_io_event_source) {
2620 sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
2621 bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
2624 if (bus->output_io_event_source) {
2625 sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
2626 bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
2629 if (bus->time_event_source) {
2630 sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF);
2631 bus->time_event_source = sd_event_source_unref(bus->time_event_source);
2634 if (bus->quit_event_source) {
2635 sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF);
2636 bus->quit_event_source = sd_event_source_unref(bus->quit_event_source);
2640 bus->event = sd_event_unref(bus->event);
2645 _public_ sd_event* sd_bus_get_event(sd_bus *bus) {
2646 assert_return(bus, NULL);
2651 _public_ sd_bus_message* sd_bus_get_current(sd_bus *bus) {
2652 assert_return(bus, NULL);
2654 return bus->current;
2657 static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
2662 assert(default_bus);
2665 return !!*default_bus;
2668 *ret = sd_bus_ref(*default_bus);
2676 b->default_bus_ptr = default_bus;
2684 _public_ int sd_bus_default_system(sd_bus **ret) {
2685 static __thread sd_bus *default_system_bus = NULL;
2687 return bus_default(sd_bus_open_system, &default_system_bus, ret);
2690 _public_ int sd_bus_default_user(sd_bus **ret) {
2691 static __thread sd_bus *default_user_bus = NULL;
2693 return bus_default(sd_bus_open_user, &default_user_bus, ret);
2696 _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
2697 assert_return(b, -EINVAL);
2698 assert_return(tid, -EINVAL);
2699 assert_return(!bus_pid_changed(b), -ECHILD);
2707 return sd_event_get_tid(b->event, tid);
2712 _public_ char *sd_bus_label_escape(const char *s) {
2716 assert_return(s, NULL);
2718 /* Escapes all chars that D-Bus' object path cannot deal
2719 * with. Can be reversed with bus_path_unescape(). We special
2720 * case the empty string. */
2725 r = new(char, strlen(s)*3 + 1);
2729 for (f = s, t = r; *f; f++) {
2731 /* Escape everything that is not a-zA-Z0-9. We also
2732 * escape 0-9 if it's the first character */
2734 if (!(*f >= 'A' && *f <= 'Z') &&
2735 !(*f >= 'a' && *f <= 'z') &&
2736 !(f > s && *f >= '0' && *f <= '9')) {
2738 *(t++) = hexchar(*f >> 4);
2739 *(t++) = hexchar(*f);
2749 _public_ char *sd_bus_label_unescape(const char *f) {
2752 assert_return(f, NULL);
2754 /* Special case for the empty string */
2758 r = new(char, strlen(f) + 1);
2762 for (t = r; *f; f++) {
2767 if ((a = unhexchar(f[1])) < 0 ||
2768 (b = unhexchar(f[2])) < 0) {
2769 /* Invalid escape code, let's take it literal then */
2772 *(t++) = (char) ((a << 4) | b);
2784 _public_ int sd_bus_get_peer_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
2789 assert_return(bus, -EINVAL);
2790 assert_return(mask <= _SD_BUS_CREDS_MAX, -ENOTSUP);
2791 assert_return(ret, -EINVAL);
2792 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2793 assert_return(!bus_pid_changed(bus), -ECHILD);
2794 assert_return(!bus->is_kernel, -ENOTSUP);
2796 if (!bus->ucred_valid && !isempty(bus->label))
2799 c = bus_creds_new();
2803 if (bus->ucred_valid) {
2804 pid = c->pid = bus->ucred.pid;
2805 c->uid = bus->ucred.uid;
2806 c->gid = bus->ucred.gid;
2808 c->mask |= ((SD_BUS_CREDS_UID | SD_BUS_CREDS_PID | SD_BUS_CREDS_GID) & mask) & bus->creds_mask;
2811 if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) {
2812 c->label = strdup(bus->label);
2814 sd_bus_creds_unref(c);
2818 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT | bus->creds_mask;
2821 r = bus_creds_add_more(c, mask, pid, 0);