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 return kdbus_translate_attach_flags(mask, &bus->creds_mask);
296 _public_ int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
297 assert_return(bus, -EINVAL);
298 assert_return(b || sd_id128_equal(server_id, SD_ID128_NULL), -EINVAL);
299 assert_return(bus->state == BUS_UNSET, -EPERM);
300 assert_return(!bus_pid_changed(bus), -ECHILD);
302 bus->is_server = !!b;
303 bus->server_id = server_id;
307 _public_ int sd_bus_set_anonymous(sd_bus *bus, int b) {
308 assert_return(bus, -EINVAL);
309 assert_return(bus->state == BUS_UNSET, -EPERM);
310 assert_return(!bus_pid_changed(bus), -ECHILD);
312 bus->anonymous_auth = !!b;
316 static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
321 assert(bus->state == BUS_HELLO || bus->state == BUS_CLOSING);
324 r = sd_bus_message_get_errno(reply);
330 r = sd_bus_message_read(reply, "s", &s);
334 if (!service_name_is_valid(s) || s[0] != ':')
337 bus->unique_name = strdup(s);
338 if (!bus->unique_name)
341 if (bus->state == BUS_HELLO)
342 bus->state = BUS_RUNNING;
347 static int bus_send_hello(sd_bus *bus) {
348 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
353 if (!bus->bus_client || bus->is_kernel)
356 r = sd_bus_message_new_method_call(
358 "org.freedesktop.DBus",
360 "org.freedesktop.DBus",
366 return sd_bus_call_async(bus, m, hello_callback, NULL, 0, &bus->hello_serial);
369 int bus_start_running(sd_bus *bus) {
372 if (bus->bus_client && !bus->is_kernel) {
373 bus->state = BUS_HELLO;
377 bus->state = BUS_RUNNING;
381 static int parse_address_key(const char **p, const char *key, char **value) {
392 if (strncmp(*p, key, l) != 0)
405 while (*a != ';' && *a != ',' && *a != 0) {
423 c = (char) ((x << 4) | y);
430 t = realloc(r, n + 2);
458 static void skip_address_key(const char **p) {
462 *p += strcspn(*p, ",");
468 static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
469 _cleanup_free_ char *path = NULL, *abstract = NULL;
478 while (**p != 0 && **p != ';') {
479 r = parse_address_key(p, "guid", guid);
485 r = parse_address_key(p, "path", &path);
491 r = parse_address_key(p, "abstract", &abstract);
500 if (!path && !abstract)
503 if (path && abstract)
508 if (l > sizeof(b->sockaddr.un.sun_path))
511 b->sockaddr.un.sun_family = AF_UNIX;
512 strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path));
513 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l;
514 } else if (abstract) {
515 l = strlen(abstract);
516 if (l > sizeof(b->sockaddr.un.sun_path) - 1)
519 b->sockaddr.un.sun_family = AF_UNIX;
520 b->sockaddr.un.sun_path[0] = 0;
521 strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1);
522 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
528 static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
529 _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
531 struct addrinfo *result, hints = {
532 .ai_socktype = SOCK_STREAM,
533 .ai_flags = AI_ADDRCONFIG,
541 while (**p != 0 && **p != ';') {
542 r = parse_address_key(p, "guid", guid);
548 r = parse_address_key(p, "host", &host);
554 r = parse_address_key(p, "port", &port);
560 r = parse_address_key(p, "family", &family);
573 if (streq(family, "ipv4"))
574 hints.ai_family = AF_INET;
575 else if (streq(family, "ipv6"))
576 hints.ai_family = AF_INET6;
581 r = getaddrinfo(host, port, &hints, &result);
585 return -EADDRNOTAVAIL;
587 memcpy(&b->sockaddr, result->ai_addr, result->ai_addrlen);
588 b->sockaddr_size = result->ai_addrlen;
590 freeaddrinfo(result);
595 static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
597 unsigned n_argv = 0, j;
606 while (**p != 0 && **p != ';') {
607 r = parse_address_key(p, "guid", guid);
613 r = parse_address_key(p, "path", &path);
619 if (startswith(*p, "argv")) {
623 ul = strtoul(*p + 4, (char**) p, 10);
624 if (errno > 0 || **p != '=' || ul > 256) {
634 x = realloc(argv, sizeof(char*) * (ul + 2));
640 memset(x + n_argv, 0, sizeof(char*) * (ul - n_argv + 2));
646 r = parse_address_key(p, NULL, argv + ul);
661 /* Make sure there are no holes in the array, with the
662 * exception of argv[0] */
663 for (j = 1; j < n_argv; j++)
669 if (argv && argv[0] == NULL) {
670 argv[0] = strdup(path);
682 for (j = 0; j < n_argv; j++)
690 static int parse_kernel_address(sd_bus *b, const char **p, char **guid) {
691 _cleanup_free_ char *path = NULL;
699 while (**p != 0 && **p != ';') {
700 r = parse_address_key(p, "guid", guid);
706 r = parse_address_key(p, "path", &path);
725 static int parse_container_address(sd_bus *b, const char **p, char **guid) {
726 _cleanup_free_ char *machine = NULL;
734 while (**p != 0 && **p != ';') {
735 r = parse_address_key(p, "guid", guid);
741 r = parse_address_key(p, "machine", &machine);
754 b->machine = machine;
757 b->sockaddr.un.sun_family = AF_UNIX;
758 strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
759 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/var/run/dbus/system_bus_socket") - 1;
764 static void bus_reset_parsed_address(sd_bus *b) {
768 b->sockaddr_size = 0;
769 strv_free(b->exec_argv);
773 b->server_id = SD_ID128_NULL;
780 static int bus_parse_next_address(sd_bus *b) {
781 _cleanup_free_ char *guid = NULL;
789 if (b->address[b->address_index] == 0)
792 bus_reset_parsed_address(b);
794 a = b->address + b->address_index;
803 if (startswith(a, "unix:")) {
806 r = parse_unix_address(b, &a, &guid);
811 } else if (startswith(a, "tcp:")) {
814 r = parse_tcp_address(b, &a, &guid);
820 } else if (startswith(a, "unixexec:")) {
823 r = parse_exec_address(b, &a, &guid);
829 } else if (startswith(a, "kernel:")) {
832 r = parse_kernel_address(b, &a, &guid);
837 } else if (startswith(a, "x-container:")) {
840 r = parse_container_address(b, &a, &guid);
853 r = sd_id128_from_string(guid, &b->server_id);
858 b->address_index = a - b->address;
862 static int bus_start_address(sd_bus *b) {
872 r = bus_socket_exec(b);
876 b->last_connect_error = -r;
877 } else if (b->kernel) {
879 r = bus_kernel_connect(b);
883 b->last_connect_error = -r;
885 } else if (b->machine) {
887 r = bus_container_connect(b);
891 b->last_connect_error = -r;
893 } else if (b->sockaddr.sa.sa_family != AF_UNSPEC) {
895 r = bus_socket_connect(b);
899 b->last_connect_error = -r;
902 r = bus_parse_next_address(b);
906 return b->last_connect_error ? -b->last_connect_error : -ECONNREFUSED;
910 int bus_next_address(sd_bus *b) {
913 bus_reset_parsed_address(b);
914 return bus_start_address(b);
917 static int bus_start_fd(sd_bus *b) {
922 assert(b->input_fd >= 0);
923 assert(b->output_fd >= 0);
925 r = fd_nonblock(b->input_fd, true);
929 r = fd_cloexec(b->input_fd, true);
933 if (b->input_fd != b->output_fd) {
934 r = fd_nonblock(b->output_fd, true);
938 r = fd_cloexec(b->output_fd, true);
943 if (fstat(b->input_fd, &st) < 0)
946 if (S_ISCHR(b->input_fd))
947 return bus_kernel_take_fd(b);
949 return bus_socket_take_fd(b);
952 _public_ int sd_bus_start(sd_bus *bus) {
955 assert_return(bus, -EINVAL);
956 assert_return(bus->state == BUS_UNSET, -EPERM);
957 assert_return(!bus_pid_changed(bus), -ECHILD);
959 bus->state = BUS_OPENING;
961 if (bus->is_server && bus->bus_client)
964 if (bus->input_fd >= 0)
965 r = bus_start_fd(bus);
966 else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel || bus->machine)
967 r = bus_start_address(bus);
974 return bus_send_hello(bus);
977 _public_ int sd_bus_open_system(sd_bus **ret) {
982 assert_return(ret, -EINVAL);
988 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
990 r = sd_bus_set_address(b, e);
994 b->sockaddr.un.sun_family = AF_UNIX;
995 strncpy(b->sockaddr.un.sun_path, "/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
996 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/dbus/system_bus_socket") - 1;
999 b->bus_client = true;
1001 r = sd_bus_start(b);
1013 _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");
1038 if (l + 4 > sizeof(b->sockaddr.un.sun_path)) {
1043 b->sockaddr.un.sun_family = AF_UNIX;
1044 memcpy(mempcpy(b->sockaddr.un.sun_path, e, l), "/bus", 4);
1045 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l + 4;
1048 b->bus_client = true;
1050 r = sd_bus_start(b);
1062 _public_ int sd_bus_open_system_remote(const char *host, sd_bus **ret) {
1063 _cleanup_free_ char *e = NULL;
1068 assert_return(host, -EINVAL);
1069 assert_return(ret, -EINVAL);
1071 e = bus_address_escape(host);
1075 p = strjoin("unixexec:path=ssh,argv1=-xT,argv2=", e, ",argv3=systemd-stdio-bridge", NULL);
1079 r = sd_bus_new(&bus);
1086 bus->bus_client = true;
1088 r = sd_bus_start(bus);
1098 _public_ int sd_bus_open_system_container(const char *machine, sd_bus **ret) {
1099 _cleanup_free_ char *e = NULL;
1104 assert_return(machine, -EINVAL);
1105 assert_return(ret, -EINVAL);
1107 e = bus_address_escape(machine);
1111 p = strjoin("x-container:machine=", e, NULL);
1115 r = sd_bus_new(&bus);
1122 bus->bus_client = true;
1124 r = sd_bus_start(bus);
1134 _public_ void sd_bus_close(sd_bus *bus) {
1138 if (bus->state == BUS_CLOSED)
1140 if (bus_pid_changed(bus))
1143 bus->state = BUS_CLOSED;
1145 sd_bus_detach_event(bus);
1147 /* Drop all queued messages so that they drop references to
1148 * the bus object and the bus may be freed */
1149 bus_reset_queues(bus);
1151 if (!bus->is_kernel)
1154 /* We'll leave the fd open in case this is a kernel bus, since
1155 * there might still be memblocks around that reference this
1156 * bus, and they might need to invoke the
1157 * KDBUS_CMD_MSG_RELEASE ioctl on the fd when they are
1161 static void bus_enter_closing(sd_bus *bus) {
1164 if (bus->state != BUS_OPENING &&
1165 bus->state != BUS_AUTHENTICATING &&
1166 bus->state != BUS_HELLO &&
1167 bus->state != BUS_RUNNING)
1170 bus->state = BUS_CLOSING;
1173 _public_ sd_bus *sd_bus_ref(sd_bus *bus) {
1174 assert_return(bus, NULL);
1176 assert_se(REFCNT_INC(bus->n_ref) >= 2);
1181 _public_ sd_bus *sd_bus_unref(sd_bus *bus) {
1182 assert_return(bus, NULL);
1184 if (REFCNT_DEC(bus->n_ref) <= 0)
1190 _public_ int sd_bus_is_open(sd_bus *bus) {
1192 assert_return(bus, -EINVAL);
1193 assert_return(!bus_pid_changed(bus), -ECHILD);
1195 return BUS_IS_OPEN(bus->state);
1198 _public_ int sd_bus_can_send(sd_bus *bus, char type) {
1201 assert_return(bus, -EINVAL);
1202 assert_return(bus->state != BUS_UNSET, -ENOTCONN);
1203 assert_return(!bus_pid_changed(bus), -ECHILD);
1205 if (type == SD_BUS_TYPE_UNIX_FD) {
1206 if (!(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD))
1209 r = bus_ensure_running(bus);
1213 return bus->can_fds;
1216 return bus_type_is_valid(type);
1219 _public_ int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
1222 assert_return(bus, -EINVAL);
1223 assert_return(server_id, -EINVAL);
1224 assert_return(!bus_pid_changed(bus), -ECHILD);
1226 r = bus_ensure_running(bus);
1230 *server_id = bus->server_id;
1234 static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
1238 if (m->header->version > b->message_version)
1242 /* If we copy the same message to multiple
1243 * destinations, avoid using the same serial
1245 b->serial = MAX(b->serial, BUS_MESSAGE_SERIAL(m));
1249 return bus_message_seal(m, ++b->serial);
1252 int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
1256 if (m->header->version > b->message_version)
1259 /* The bus specification says the serial number cannot be 0,
1260 * hence let's fill something in for synthetic messages. Since
1261 * synthetic messages might have a fake sender and we don't
1262 * want to interfere with the real sender's serial numbers we
1263 * pick a fixed, artifical one. We use (uint32_t) -1 rather
1264 * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
1265 * even though kdbus can do 64bit. */
1267 return bus_message_seal(m, 0xFFFFFFFFULL);
1270 static int bus_write_message(sd_bus *bus, sd_bus_message *message, size_t *idx) {
1277 r = bus_kernel_write_message(bus, message);
1279 r = bus_socket_write_message(bus, message, idx);
1284 static int dispatch_wqueue(sd_bus *bus) {
1288 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1290 while (bus->wqueue_size > 0) {
1292 r = bus_write_message(bus, bus->wqueue[0], &bus->windex);
1296 /* Didn't do anything this time */
1298 else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
1299 /* Fully written. Let's drop the entry from
1302 * This isn't particularly optimized, but
1303 * well, this is supposed to be our worst-case
1304 * buffer only, and the socket buffer is
1305 * supposed to be our primary buffer, and if
1306 * it got full, then all bets are off
1309 sd_bus_message_unref(bus->wqueue[0]);
1310 bus->wqueue_size --;
1311 memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
1321 static int bus_read_message(sd_bus *bus) {
1325 return bus_kernel_read_message(bus);
1327 return bus_socket_read_message(bus);
1330 int bus_rqueue_make_room(sd_bus *bus) {
1334 x = bus->rqueue_size + 1;
1336 if (bus->rqueue_allocated >= x)
1339 if (x > BUS_RQUEUE_MAX)
1342 q = realloc(bus->rqueue, x * sizeof(sd_bus_message*));
1347 bus->rqueue_allocated = x;
1352 static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
1357 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1360 if (bus->rqueue_size > 0) {
1361 /* Dispatch a queued message */
1363 *m = bus->rqueue[0];
1364 bus->rqueue_size --;
1365 memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
1369 /* Try to read a new message */
1370 r = bus_read_message(bus);
1380 _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
1383 assert_return(bus, -EINVAL);
1384 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1385 assert_return(m, -EINVAL);
1386 assert_return(!bus_pid_changed(bus), -ECHILD);
1389 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
1396 /* If the serial number isn't kept, then we know that no reply
1398 if (!serial && !m->sealed)
1399 m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
1401 r = bus_seal_message(bus, m);
1405 /* If this is a reply and no reply was requested, then let's
1406 * suppress this, if we can */
1407 if (m->dont_send && !serial)
1410 if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
1413 r = bus_write_message(bus, m, &idx);
1416 else if (!bus->is_kernel && idx < BUS_MESSAGE_SIZE(m)) {
1417 /* Wasn't fully written. So let's remember how
1418 * much was written. Note that the first entry
1419 * of the wqueue array is always allocated so
1420 * that we always can remember how much was
1422 bus->wqueue[0] = sd_bus_message_ref(m);
1423 bus->wqueue_size = 1;
1429 /* Just append it to the queue. */
1431 if (bus->wqueue_size >= BUS_WQUEUE_MAX)
1434 q = realloc(bus->wqueue, sizeof(sd_bus_message*) * (bus->wqueue_size + 1));
1439 q[bus->wqueue_size ++] = sd_bus_message_ref(m);
1443 *serial = BUS_MESSAGE_SERIAL(m);
1448 _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *serial) {
1451 assert_return(bus, -EINVAL);
1452 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1453 assert_return(m, -EINVAL);
1454 assert_return(!bus_pid_changed(bus), -ECHILD);
1456 if (!streq_ptr(m->destination, destination)) {
1461 r = sd_bus_message_set_destination(m, destination);
1466 return sd_bus_send(bus, m, serial);
1469 static usec_t calc_elapse(uint64_t usec) {
1470 if (usec == (uint64_t) -1)
1474 usec = BUS_DEFAULT_TIMEOUT;
1476 return now(CLOCK_MONOTONIC) + usec;
1479 static int timeout_compare(const void *a, const void *b) {
1480 const struct reply_callback *x = a, *y = b;
1482 if (x->timeout != 0 && y->timeout == 0)
1485 if (x->timeout == 0 && y->timeout != 0)
1488 if (x->timeout < y->timeout)
1491 if (x->timeout > y->timeout)
1497 _public_ int sd_bus_call_async(
1500 sd_bus_message_handler_t callback,
1505 struct reply_callback *c;
1508 assert_return(bus, -EINVAL);
1509 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1510 assert_return(m, -EINVAL);
1511 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1512 assert_return(!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1513 assert_return(callback, -EINVAL);
1514 assert_return(!bus_pid_changed(bus), -ECHILD);
1516 r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
1520 if (usec != (uint64_t) -1) {
1521 r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
1526 r = bus_seal_message(bus, m);
1530 c = new0(struct reply_callback, 1);
1534 c->callback = callback;
1535 c->userdata = userdata;
1536 c->serial = BUS_MESSAGE_SERIAL(m);
1537 c->timeout = calc_elapse(usec);
1539 r = hashmap_put(bus->reply_callbacks, &c->serial, c);
1545 if (c->timeout != 0) {
1546 r = prioq_put(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1549 sd_bus_call_async_cancel(bus, c->serial);
1554 r = sd_bus_send(bus, m, serial);
1556 sd_bus_call_async_cancel(bus, c->serial);
1563 _public_ int sd_bus_call_async_cancel(sd_bus *bus, uint64_t serial) {
1564 struct reply_callback *c;
1566 assert_return(bus, -EINVAL);
1567 assert_return(serial != 0, -EINVAL);
1568 assert_return(!bus_pid_changed(bus), -ECHILD);
1570 c = hashmap_remove(bus->reply_callbacks, &serial);
1574 if (c->timeout != 0)
1575 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1581 int bus_ensure_running(sd_bus *bus) {
1586 if (bus->state == BUS_UNSET || bus->state == BUS_CLOSED || bus->state == BUS_CLOSING)
1588 if (bus->state == BUS_RUNNING)
1592 r = sd_bus_process(bus, NULL);
1595 if (bus->state == BUS_RUNNING)
1600 r = sd_bus_wait(bus, (uint64_t) -1);
1606 _public_ int sd_bus_call(
1610 sd_bus_error *error,
1611 sd_bus_message **reply) {
1618 assert_return(bus, -EINVAL);
1619 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1620 assert_return(m, -EINVAL);
1621 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1622 assert_return(!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1623 assert_return(!bus_error_is_dirty(error), -EINVAL);
1624 assert_return(!bus_pid_changed(bus), -ECHILD);
1626 r = bus_ensure_running(bus);
1630 r = sd_bus_send(bus, m, &serial);
1634 timeout = calc_elapse(usec);
1635 i = bus->rqueue_size;
1640 r = bus_read_message(bus);
1644 while (i < bus->rqueue_size) {
1645 sd_bus_message *incoming = NULL;
1647 incoming = bus->rqueue[i];
1649 if (incoming->reply_serial == serial) {
1650 /* Found a match! */
1652 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1655 if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
1660 sd_bus_message_unref(incoming);
1665 if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR) {
1668 r = sd_bus_error_copy(error, &incoming->error);
1670 sd_bus_message_unref(incoming);
1674 k = sd_bus_error_get_errno(&incoming->error);
1675 sd_bus_message_unref(incoming);
1679 sd_bus_message_unref(incoming);
1682 } else if (incoming->header->serial == serial &&
1685 streq(bus->unique_name, incoming->sender)) {
1687 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1690 /* Our own message? Somebody is trying
1691 * to send its own client a message,
1692 * let's not dead-lock, let's fail
1695 sd_bus_message_unref(incoming);
1699 /* Try to read more, right-away */
1709 n = now(CLOCK_MONOTONIC);
1715 left = (uint64_t) -1;
1717 r = bus_poll(bus, true, left);
1721 r = dispatch_wqueue(bus);
1727 _public_ int sd_bus_get_fd(sd_bus *bus) {
1729 assert_return(bus, -EINVAL);
1730 assert_return(bus->input_fd == bus->output_fd, -EPERM);
1731 assert_return(!bus_pid_changed(bus), -ECHILD);
1733 return bus->input_fd;
1736 _public_ int sd_bus_get_events(sd_bus *bus) {
1739 assert_return(bus, -EINVAL);
1740 assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
1741 assert_return(!bus_pid_changed(bus), -ECHILD);
1743 if (bus->state == BUS_OPENING)
1745 else if (bus->state == BUS_AUTHENTICATING) {
1747 if (bus_socket_auth_needs_write(bus))
1752 } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
1753 if (bus->rqueue_size <= 0)
1755 if (bus->wqueue_size > 0)
1762 _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
1763 struct reply_callback *c;
1765 assert_return(bus, -EINVAL);
1766 assert_return(timeout_usec, -EINVAL);
1767 assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
1768 assert_return(!bus_pid_changed(bus), -ECHILD);
1770 if (bus->state == BUS_CLOSING) {
1775 if (bus->state == BUS_AUTHENTICATING) {
1776 *timeout_usec = bus->auth_timeout;
1780 if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) {
1781 *timeout_usec = (uint64_t) -1;
1785 if (bus->rqueue_size > 0) {
1790 c = prioq_peek(bus->reply_callbacks_prioq);
1792 *timeout_usec = (uint64_t) -1;
1796 *timeout_usec = c->timeout;
1800 static int process_timeout(sd_bus *bus) {
1801 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1802 _cleanup_bus_message_unref_ sd_bus_message* m = NULL;
1803 struct reply_callback *c;
1809 c = prioq_peek(bus->reply_callbacks_prioq);
1813 n = now(CLOCK_MONOTONIC);
1817 r = bus_message_new_synthetic_error(
1820 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
1825 m->sender = "org.freedesktop.DBus";
1827 r = bus_seal_synthetic_message(bus, m);
1831 assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
1832 hashmap_remove(bus->reply_callbacks, &c->serial);
1835 bus->iteration_counter ++;
1837 r = c->callback(bus, m, c->userdata, &error_buffer);
1838 r = bus_maybe_reply_error(m, r, &error_buffer);
1841 bus->current = NULL;
1846 static int process_hello(sd_bus *bus, sd_bus_message *m) {
1850 if (bus->state != BUS_HELLO)
1853 /* Let's make sure the first message on the bus is the HELLO
1854 * reply. But note that we don't actually parse the message
1855 * here (we leave that to the usual handling), we just verify
1856 * we don't let any earlier msg through. */
1858 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
1859 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1862 if (m->reply_serial != bus->hello_serial)
1868 static int process_reply(sd_bus *bus, sd_bus_message *m) {
1869 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1870 struct reply_callback *c;
1876 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
1877 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1880 c = hashmap_remove(bus->reply_callbacks, &m->reply_serial);
1884 if (c->timeout != 0)
1885 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1887 r = sd_bus_message_rewind(m, true);
1891 r = c->callback(bus, m, c->userdata, &error_buffer);
1892 r = bus_maybe_reply_error(m, r, &error_buffer);
1898 static int process_filter(sd_bus *bus, sd_bus_message *m) {
1899 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1900 struct filter_callback *l;
1907 bus->filter_callbacks_modified = false;
1909 LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
1911 if (bus->filter_callbacks_modified)
1914 /* Don't run this more than once per iteration */
1915 if (l->last_iteration == bus->iteration_counter)
1918 l->last_iteration = bus->iteration_counter;
1920 r = sd_bus_message_rewind(m, true);
1924 r = l->callback(bus, m, l->userdata, &error_buffer);
1925 r = bus_maybe_reply_error(m, r, &error_buffer);
1931 } while (bus->filter_callbacks_modified);
1936 static int process_match(sd_bus *bus, sd_bus_message *m) {
1943 bus->match_callbacks_modified = false;
1945 r = bus_match_run(bus, &bus->match_callbacks, m);
1949 } while (bus->match_callbacks_modified);
1954 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
1955 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1961 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
1964 if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
1967 if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
1970 if (streq_ptr(m->member, "Ping"))
1971 r = sd_bus_message_new_method_return(m, &reply);
1972 else if (streq_ptr(m->member, "GetMachineId")) {
1976 r = sd_id128_get_machine(&id);
1980 r = sd_bus_message_new_method_return(m, &reply);
1984 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
1986 r = sd_bus_message_new_method_errorf(
1988 SD_BUS_ERROR_UNKNOWN_METHOD,
1989 "Unknown method '%s' on interface '%s'.", m->member, m->interface);
1995 r = sd_bus_send(bus, reply, NULL);
2002 static int process_message(sd_bus *bus, sd_bus_message *m) {
2009 bus->iteration_counter++;
2011 log_debug("Got message sender=%s object=%s interface=%s member=%s",
2012 strna(sd_bus_message_get_sender(m)),
2013 strna(sd_bus_message_get_path(m)),
2014 strna(sd_bus_message_get_interface(m)),
2015 strna(sd_bus_message_get_member(m)));
2017 r = process_hello(bus, m);
2021 r = process_reply(bus, m);
2025 r = process_filter(bus, m);
2029 r = process_match(bus, m);
2033 r = process_builtin(bus, m);
2037 r = bus_process_object(bus, m);
2040 bus->current = NULL;
2044 static int process_running(sd_bus *bus, sd_bus_message **ret) {
2045 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2049 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
2051 r = process_timeout(bus);
2055 r = dispatch_wqueue(bus);
2059 r = dispatch_rqueue(bus, &m);
2065 r = process_message(bus, m);
2070 r = sd_bus_message_rewind(m, true);
2079 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
2081 r = sd_bus_reply_method_errorf(
2083 SD_BUS_ERROR_UNKNOWN_OBJECT,
2084 "Unknown object '%s'.", m->path);
2098 static int process_closing(sd_bus *bus, sd_bus_message **ret) {
2099 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2100 struct reply_callback *c;
2104 assert(bus->state == BUS_CLOSING);
2106 c = hashmap_first(bus->reply_callbacks);
2108 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2110 /* First, fail all outstanding method calls */
2111 r = bus_message_new_synthetic_error(
2114 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
2119 r = bus_seal_synthetic_message(bus, m);
2123 if (c->timeout != 0)
2124 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2126 hashmap_remove(bus->reply_callbacks, &c->serial);
2129 bus->iteration_counter++;
2131 r = c->callback(bus, m, c->userdata, &error_buffer);
2132 r = bus_maybe_reply_error(m, r, &error_buffer);
2138 /* Then, synthesize a Disconnected message */
2139 r = sd_bus_message_new_signal(
2141 "/org/freedesktop/DBus/Local",
2142 "org.freedesktop.DBus.Local",
2148 m->sender = "org.freedesktop.DBus.Local";
2150 r = bus_seal_synthetic_message(bus, m);
2157 bus->iteration_counter++;
2159 r = process_filter(bus, m);
2163 r = process_match(bus, m);
2175 bus->current = NULL;
2179 _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
2180 BUS_DONT_DESTROY(bus);
2183 /* Returns 0 when we didn't do anything. This should cause the
2184 * caller to invoke sd_bus_wait() before returning the next
2185 * time. Returns > 0 when we did something, which possibly
2186 * means *ret is filled in with an unprocessed message. */
2188 assert_return(bus, -EINVAL);
2189 assert_return(!bus_pid_changed(bus), -ECHILD);
2191 /* We don't allow recursively invoking sd_bus_process(). */
2192 assert_return(!bus->current, -EBUSY);
2194 switch (bus->state) {
2201 r = bus_socket_process_opening(bus);
2202 if (r == -ECONNRESET || r == -EPIPE) {
2203 bus_enter_closing(bus);
2211 case BUS_AUTHENTICATING:
2212 r = bus_socket_process_authenticating(bus);
2213 if (r == -ECONNRESET || r == -EPIPE) {
2214 bus_enter_closing(bus);
2226 r = process_running(bus, ret);
2227 if (r == -ECONNRESET || r == -EPIPE) {
2228 bus_enter_closing(bus);
2238 return process_closing(bus, ret);
2241 assert_not_reached("Unknown state");
2244 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
2245 struct pollfd p[2] = {};
2248 usec_t m = (usec_t) -1;
2252 if (bus->state == BUS_CLOSING)
2255 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2257 e = sd_bus_get_events(bus);
2262 /* The caller really needs some more data, he doesn't
2263 * care about what's already read, or any timeouts
2268 /* The caller wants to process if there's something to
2269 * process, but doesn't care otherwise */
2271 r = sd_bus_get_timeout(bus, &until);
2276 nw = now(CLOCK_MONOTONIC);
2277 m = until > nw ? until - nw : 0;
2281 if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
2284 p[0].fd = bus->input_fd;
2285 if (bus->output_fd == bus->input_fd) {
2289 p[0].events = e & POLLIN;
2290 p[1].fd = bus->output_fd;
2291 p[1].events = e & POLLOUT;
2295 r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
2299 return r > 0 ? 1 : 0;
2302 _public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
2304 assert_return(bus, -EINVAL);
2305 assert_return(!bus_pid_changed(bus), -ECHILD);
2307 if (bus->state == BUS_CLOSING)
2310 assert_return(BUS_IS_OPEN(bus->state) , -ENOTCONN);
2312 if (bus->rqueue_size > 0)
2315 return bus_poll(bus, false, timeout_usec);
2318 _public_ int sd_bus_flush(sd_bus *bus) {
2321 assert_return(bus, -EINVAL);
2322 assert_return(!bus_pid_changed(bus), -ECHILD);
2324 if (bus->state == BUS_CLOSING)
2327 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2329 r = bus_ensure_running(bus);
2333 if (bus->wqueue_size <= 0)
2337 r = dispatch_wqueue(bus);
2341 if (bus->wqueue_size <= 0)
2344 r = bus_poll(bus, false, (uint64_t) -1);
2350 _public_ int sd_bus_add_filter(sd_bus *bus,
2351 sd_bus_message_handler_t callback,
2354 struct filter_callback *f;
2356 assert_return(bus, -EINVAL);
2357 assert_return(callback, -EINVAL);
2358 assert_return(!bus_pid_changed(bus), -ECHILD);
2360 f = new0(struct filter_callback, 1);
2363 f->callback = callback;
2364 f->userdata = userdata;
2366 bus->filter_callbacks_modified = true;
2367 LIST_PREPEND(callbacks, bus->filter_callbacks, f);
2371 _public_ int sd_bus_remove_filter(sd_bus *bus,
2372 sd_bus_message_handler_t callback,
2375 struct filter_callback *f;
2377 assert_return(bus, -EINVAL);
2378 assert_return(callback, -EINVAL);
2379 assert_return(!bus_pid_changed(bus), -ECHILD);
2381 LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
2382 if (f->callback == callback && f->userdata == userdata) {
2383 bus->filter_callbacks_modified = true;
2384 LIST_REMOVE(callbacks, bus->filter_callbacks, f);
2393 _public_ int sd_bus_add_match(sd_bus *bus,
2395 sd_bus_message_handler_t callback,
2398 struct bus_match_component *components = NULL;
2399 unsigned n_components = 0;
2400 uint64_t cookie = 0;
2403 assert_return(bus, -EINVAL);
2404 assert_return(match, -EINVAL);
2405 assert_return(!bus_pid_changed(bus), -ECHILD);
2407 r = bus_match_parse(match, &components, &n_components);
2411 if (bus->bus_client) {
2412 cookie = ++bus->match_cookie;
2414 r = bus_add_match_internal(bus, match, components, n_components, cookie);
2419 bus->match_callbacks_modified = true;
2420 r = bus_match_add(&bus->match_callbacks, components, n_components, callback, userdata, cookie, NULL);
2422 if (bus->bus_client)
2423 bus_remove_match_internal(bus, match, cookie);
2427 bus_match_parse_free(components, n_components);
2431 _public_ int sd_bus_remove_match(sd_bus *bus,
2433 sd_bus_message_handler_t callback,
2436 struct bus_match_component *components = NULL;
2437 unsigned n_components = 0;
2439 uint64_t cookie = 0;
2441 assert_return(bus, -EINVAL);
2442 assert_return(match, -EINVAL);
2443 assert_return(!bus_pid_changed(bus), -ECHILD);
2445 r = bus_match_parse(match, &components, &n_components);
2449 bus->match_callbacks_modified = true;
2450 r = bus_match_remove(&bus->match_callbacks, components, n_components, callback, userdata, &cookie);
2452 if (bus->bus_client)
2453 q = bus_remove_match_internal(bus, match, cookie);
2455 bus_match_parse_free(components, n_components);
2457 return r < 0 ? r : q;
2460 bool bus_pid_changed(sd_bus *bus) {
2463 /* We don't support people creating a bus connection and
2464 * keeping it around over a fork(). Let's complain. */
2466 return bus->original_pid != getpid();
2469 static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2470 sd_bus *bus = userdata;
2475 r = sd_bus_process(bus, NULL);
2482 static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
2483 sd_bus *bus = userdata;
2488 r = sd_bus_process(bus, NULL);
2495 static int prepare_callback(sd_event_source *s, void *userdata) {
2496 sd_bus *bus = userdata;
2503 e = sd_bus_get_events(bus);
2507 if (bus->output_fd != bus->input_fd) {
2509 r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN);
2513 r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT);
2517 r = sd_event_source_set_io_events(bus->input_io_event_source, e);
2522 r = sd_bus_get_timeout(bus, &until);
2528 j = sd_event_source_set_time(bus->time_event_source, until);
2533 r = sd_event_source_set_enabled(bus->time_event_source, r > 0);
2540 static int quit_callback(sd_event_source *event, void *userdata) {
2541 sd_bus *bus = userdata;
2550 _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
2553 assert_return(bus, -EINVAL);
2554 assert_return(!bus->event, -EBUSY);
2556 assert(!bus->input_io_event_source);
2557 assert(!bus->output_io_event_source);
2558 assert(!bus->time_event_source);
2561 bus->event = sd_event_ref(event);
2563 r = sd_event_default(&bus->event);
2568 r = sd_event_add_io(bus->event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source);
2572 r = sd_event_source_set_priority(bus->input_io_event_source, priority);
2576 if (bus->output_fd != bus->input_fd) {
2577 r = sd_event_add_io(bus->event, bus->output_fd, 0, io_callback, bus, &bus->output_io_event_source);
2581 r = sd_event_source_set_priority(bus->output_io_event_source, priority);
2586 r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
2590 r = sd_event_add_monotonic(bus->event, 0, 0, time_callback, bus, &bus->time_event_source);
2594 r = sd_event_source_set_priority(bus->time_event_source, priority);
2598 r = sd_event_add_quit(bus->event, quit_callback, bus, &bus->quit_event_source);
2605 sd_bus_detach_event(bus);
2609 _public_ int sd_bus_detach_event(sd_bus *bus) {
2610 assert_return(bus, -EINVAL);
2611 assert_return(bus->event, -ENXIO);
2613 if (bus->input_io_event_source) {
2614 sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
2615 bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
2618 if (bus->output_io_event_source) {
2619 sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
2620 bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
2623 if (bus->time_event_source) {
2624 sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF);
2625 bus->time_event_source = sd_event_source_unref(bus->time_event_source);
2628 if (bus->quit_event_source) {
2629 sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF);
2630 bus->quit_event_source = sd_event_source_unref(bus->quit_event_source);
2634 bus->event = sd_event_unref(bus->event);
2639 _public_ sd_event* sd_bus_get_event(sd_bus *bus) {
2640 assert_return(bus, NULL);
2645 _public_ sd_bus_message* sd_bus_get_current(sd_bus *bus) {
2646 assert_return(bus, NULL);
2648 return bus->current;
2651 static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
2656 assert(default_bus);
2659 return !!*default_bus;
2662 *ret = sd_bus_ref(*default_bus);
2670 b->default_bus_ptr = default_bus;
2678 _public_ int sd_bus_default_system(sd_bus **ret) {
2679 static __thread sd_bus *default_system_bus = NULL;
2681 return bus_default(sd_bus_open_system, &default_system_bus, ret);
2684 _public_ int sd_bus_default_user(sd_bus **ret) {
2685 static __thread sd_bus *default_user_bus = NULL;
2687 return bus_default(sd_bus_open_user, &default_user_bus, ret);
2690 _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
2691 assert_return(b, -EINVAL);
2692 assert_return(tid, -EINVAL);
2693 assert_return(!bus_pid_changed(b), -ECHILD);
2701 return sd_event_get_tid(b->event, tid);
2706 _public_ char *sd_bus_label_escape(const char *s) {
2710 assert_return(s, NULL);
2712 /* Escapes all chars that D-Bus' object path cannot deal
2713 * with. Can be reversed with bus_path_unescape(). We special
2714 * case the empty string. */
2719 r = new(char, strlen(s)*3 + 1);
2723 for (f = s, t = r; *f; f++) {
2725 /* Escape everything that is not a-zA-Z0-9. We also
2726 * escape 0-9 if it's the first character */
2728 if (!(*f >= 'A' && *f <= 'Z') &&
2729 !(*f >= 'a' && *f <= 'z') &&
2730 !(f > s && *f >= '0' && *f <= '9')) {
2732 *(t++) = hexchar(*f >> 4);
2733 *(t++) = hexchar(*f);
2743 _public_ char *sd_bus_label_unescape(const char *f) {
2746 assert_return(f, NULL);
2748 /* Special case for the empty string */
2752 r = new(char, strlen(f) + 1);
2756 for (t = r; *f; f++) {
2761 if ((a = unhexchar(f[1])) < 0 ||
2762 (b = unhexchar(f[2])) < 0) {
2763 /* Invalid escape code, let's take it literal then */
2766 *(t++) = (char) ((a << 4) | b);
2778 _public_ int sd_bus_get_peer_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
2783 assert_return(bus, -EINVAL);
2784 assert_return(mask <= _SD_BUS_CREDS_MAX, -ENOTSUP);
2785 assert_return(ret, -EINVAL);
2786 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2787 assert_return(!bus_pid_changed(bus), -ECHILD);
2788 assert_return(!bus->is_kernel, -ENOTSUP);
2790 if (!bus->ucred_valid && !isempty(bus->label))
2793 c = bus_creds_new();
2797 if (bus->ucred_valid) {
2798 pid = c->pid = bus->ucred.pid;
2799 c->uid = bus->ucred.uid;
2800 c->gid = bus->ucred.gid;
2802 c->mask |= ((SD_BUS_CREDS_UID | SD_BUS_CREDS_PID | SD_BUS_CREDS_GID) & mask) & bus->creds_mask;
2805 if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) {
2806 c->label = strdup(bus->label);
2808 sd_bus_creds_unref(c);
2812 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT | bus->creds_mask;
2815 r = bus_creds_add_more(c, mask, pid, 0);