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->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
185 r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
186 r->attach_flags |= KDBUS_ATTACH_NAMES;
187 r->original_pid = getpid();
189 assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0);
191 /* We guarantee that wqueue always has space for at least one
193 r->wqueue = new(sd_bus_message*, 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, -EINVAL);
225 assert_return(output_fd >= 0, -EINVAL);
226 assert_return(!bus_pid_changed(bus), -ECHILD);
228 bus->input_fd = input_fd;
229 bus->output_fd = output_fd;
233 _public_ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
236 assert_return(bus, -EINVAL);
237 assert_return(bus->state == BUS_UNSET, -EPERM);
238 assert_return(path, -EINVAL);
239 assert_return(!strv_isempty(argv), -EINVAL);
240 assert_return(!bus_pid_changed(bus), -ECHILD);
252 free(bus->exec_path);
253 strv_free(bus->exec_argv);
261 _public_ int sd_bus_set_bus_client(sd_bus *bus, int b) {
262 assert_return(bus, -EINVAL);
263 assert_return(bus->state == BUS_UNSET, -EPERM);
264 assert_return(!bus_pid_changed(bus), -ECHILD);
266 bus->bus_client = !!b;
270 _public_ int sd_bus_negotiate_fds(sd_bus *bus, int b) {
271 assert_return(bus, -EINVAL);
272 assert_return(bus->state == BUS_UNSET, -EPERM);
273 assert_return(!bus_pid_changed(bus), -ECHILD);
275 SET_FLAG(bus->hello_flags, KDBUS_HELLO_ACCEPT_FD, b);
279 _public_ int sd_bus_negotiate_attach_timestamp(sd_bus *bus, int b) {
280 assert_return(bus, -EINVAL);
281 assert_return(bus->state == BUS_UNSET, -EPERM);
282 assert_return(!bus_pid_changed(bus), -ECHILD);
284 SET_FLAG(bus->attach_flags, KDBUS_ATTACH_TIMESTAMP, b);
288 _public_ int sd_bus_negotiate_attach_creds(sd_bus *bus, uint64_t mask) {
289 assert_return(bus, -EINVAL);
290 assert_return(mask <= _SD_BUS_CREDS_MAX, -EINVAL);
291 assert_return(bus->state == BUS_UNSET, -EPERM);
292 assert_return(!bus_pid_changed(bus), -ECHILD);
294 /* The well knowns we need unconditionally, so that matches can work */
295 bus->creds_mask = mask | SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
297 return kdbus_translate_attach_flags(bus->creds_mask, &bus->creds_mask);
300 _public_ int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
301 assert_return(bus, -EINVAL);
302 assert_return(b || sd_id128_equal(server_id, SD_ID128_NULL), -EINVAL);
303 assert_return(bus->state == BUS_UNSET, -EPERM);
304 assert_return(!bus_pid_changed(bus), -ECHILD);
306 bus->is_server = !!b;
307 bus->server_id = server_id;
311 _public_ int sd_bus_set_anonymous(sd_bus *bus, int b) {
312 assert_return(bus, -EINVAL);
313 assert_return(bus->state == BUS_UNSET, -EPERM);
314 assert_return(!bus_pid_changed(bus), -ECHILD);
316 bus->anonymous_auth = !!b;
320 static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
325 assert(bus->state == BUS_HELLO || bus->state == BUS_CLOSING);
328 r = sd_bus_message_get_errno(reply);
334 r = sd_bus_message_read(reply, "s", &s);
338 if (!service_name_is_valid(s) || s[0] != ':')
341 bus->unique_name = strdup(s);
342 if (!bus->unique_name)
345 if (bus->state == BUS_HELLO)
346 bus->state = BUS_RUNNING;
351 static int bus_send_hello(sd_bus *bus) {
352 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
357 if (!bus->bus_client || bus->is_kernel)
360 r = sd_bus_message_new_method_call(
362 "org.freedesktop.DBus",
364 "org.freedesktop.DBus",
370 return sd_bus_call_async(bus, m, hello_callback, NULL, 0, &bus->hello_serial);
373 int bus_start_running(sd_bus *bus) {
376 if (bus->bus_client && !bus->is_kernel) {
377 bus->state = BUS_HELLO;
381 bus->state = BUS_RUNNING;
385 static int parse_address_key(const char **p, const char *key, char **value) {
396 if (strncmp(*p, key, l) != 0)
409 while (*a != ';' && *a != ',' && *a != 0) {
427 c = (char) ((x << 4) | y);
434 t = realloc(r, n + 2);
462 static void skip_address_key(const char **p) {
466 *p += strcspn(*p, ",");
472 static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
473 _cleanup_free_ char *path = NULL, *abstract = NULL;
482 while (**p != 0 && **p != ';') {
483 r = parse_address_key(p, "guid", guid);
489 r = parse_address_key(p, "path", &path);
495 r = parse_address_key(p, "abstract", &abstract);
504 if (!path && !abstract)
507 if (path && abstract)
512 if (l > sizeof(b->sockaddr.un.sun_path))
515 b->sockaddr.un.sun_family = AF_UNIX;
516 strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path));
517 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l;
518 } else if (abstract) {
519 l = strlen(abstract);
520 if (l > sizeof(b->sockaddr.un.sun_path) - 1)
523 b->sockaddr.un.sun_family = AF_UNIX;
524 b->sockaddr.un.sun_path[0] = 0;
525 strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1);
526 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
532 static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
533 _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
535 struct addrinfo *result, hints = {
536 .ai_socktype = SOCK_STREAM,
537 .ai_flags = AI_ADDRCONFIG,
545 while (**p != 0 && **p != ';') {
546 r = parse_address_key(p, "guid", guid);
552 r = parse_address_key(p, "host", &host);
558 r = parse_address_key(p, "port", &port);
564 r = parse_address_key(p, "family", &family);
577 if (streq(family, "ipv4"))
578 hints.ai_family = AF_INET;
579 else if (streq(family, "ipv6"))
580 hints.ai_family = AF_INET6;
585 r = getaddrinfo(host, port, &hints, &result);
589 return -EADDRNOTAVAIL;
591 memcpy(&b->sockaddr, result->ai_addr, result->ai_addrlen);
592 b->sockaddr_size = result->ai_addrlen;
594 freeaddrinfo(result);
599 static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
601 unsigned n_argv = 0, j;
610 while (**p != 0 && **p != ';') {
611 r = parse_address_key(p, "guid", guid);
617 r = parse_address_key(p, "path", &path);
623 if (startswith(*p, "argv")) {
627 ul = strtoul(*p + 4, (char**) p, 10);
628 if (errno > 0 || **p != '=' || ul > 256) {
638 x = realloc(argv, sizeof(char*) * (ul + 2));
644 memset(x + n_argv, 0, sizeof(char*) * (ul - n_argv + 2));
650 r = parse_address_key(p, NULL, argv + ul);
665 /* Make sure there are no holes in the array, with the
666 * exception of argv[0] */
667 for (j = 1; j < n_argv; j++)
673 if (argv && argv[0] == NULL) {
674 argv[0] = strdup(path);
686 for (j = 0; j < n_argv; j++)
694 static int parse_kernel_address(sd_bus *b, const char **p, char **guid) {
695 _cleanup_free_ char *path = NULL;
703 while (**p != 0 && **p != ';') {
704 r = parse_address_key(p, "guid", guid);
710 r = parse_address_key(p, "path", &path);
729 static int parse_container_address(sd_bus *b, const char **p, char **guid) {
730 _cleanup_free_ char *machine = NULL;
738 while (**p != 0 && **p != ';') {
739 r = parse_address_key(p, "guid", guid);
745 r = parse_address_key(p, "machine", &machine);
758 b->machine = machine;
761 b->sockaddr.un.sun_family = AF_UNIX;
762 strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
763 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/var/run/dbus/system_bus_socket") - 1;
768 static void bus_reset_parsed_address(sd_bus *b) {
772 b->sockaddr_size = 0;
773 strv_free(b->exec_argv);
777 b->server_id = SD_ID128_NULL;
784 static int bus_parse_next_address(sd_bus *b) {
785 _cleanup_free_ char *guid = NULL;
793 if (b->address[b->address_index] == 0)
796 bus_reset_parsed_address(b);
798 a = b->address + b->address_index;
807 if (startswith(a, "unix:")) {
810 r = parse_unix_address(b, &a, &guid);
815 } else if (startswith(a, "tcp:")) {
818 r = parse_tcp_address(b, &a, &guid);
824 } else if (startswith(a, "unixexec:")) {
827 r = parse_exec_address(b, &a, &guid);
833 } else if (startswith(a, "kernel:")) {
836 r = parse_kernel_address(b, &a, &guid);
841 } else if (startswith(a, "x-container:")) {
844 r = parse_container_address(b, &a, &guid);
857 r = sd_id128_from_string(guid, &b->server_id);
862 b->address_index = a - b->address;
866 static int bus_start_address(sd_bus *b) {
876 r = bus_socket_exec(b);
880 b->last_connect_error = -r;
881 } else if (b->kernel) {
883 r = bus_kernel_connect(b);
887 b->last_connect_error = -r;
889 } else if (b->machine) {
891 r = bus_container_connect(b);
895 b->last_connect_error = -r;
897 } else if (b->sockaddr.sa.sa_family != AF_UNSPEC) {
899 r = bus_socket_connect(b);
903 b->last_connect_error = -r;
906 r = bus_parse_next_address(b);
910 return b->last_connect_error ? -b->last_connect_error : -ECONNREFUSED;
914 int bus_next_address(sd_bus *b) {
917 bus_reset_parsed_address(b);
918 return bus_start_address(b);
921 static int bus_start_fd(sd_bus *b) {
926 assert(b->input_fd >= 0);
927 assert(b->output_fd >= 0);
929 r = fd_nonblock(b->input_fd, true);
933 r = fd_cloexec(b->input_fd, true);
937 if (b->input_fd != b->output_fd) {
938 r = fd_nonblock(b->output_fd, true);
942 r = fd_cloexec(b->output_fd, true);
947 if (fstat(b->input_fd, &st) < 0)
950 if (S_ISCHR(b->input_fd))
951 return bus_kernel_take_fd(b);
953 return bus_socket_take_fd(b);
956 _public_ int sd_bus_start(sd_bus *bus) {
959 assert_return(bus, -EINVAL);
960 assert_return(bus->state == BUS_UNSET, -EPERM);
961 assert_return(!bus_pid_changed(bus), -ECHILD);
963 bus->state = BUS_OPENING;
965 if (bus->is_server && bus->bus_client)
968 if (bus->input_fd >= 0)
969 r = bus_start_fd(bus);
970 else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel || bus->machine)
971 r = bus_start_address(bus);
978 return bus_send_hello(bus);
981 _public_ int sd_bus_open_system(sd_bus **ret) {
986 assert_return(ret, -EINVAL);
992 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
994 r = sd_bus_set_address(b, e);
997 r = sd_bus_set_address(b, "kernel:path=/dev/kdbus/0-system/bus;unix:path=/run/dbus/system_bus_socket");
999 r = sd_bus_set_address(b, "unix:path=/run/dbus/system_bus_socket");
1005 b->bus_client = true;
1007 r = sd_bus_start(b);
1019 _public_ int sd_bus_open_user(sd_bus **ret) {
1024 assert_return(ret, -EINVAL);
1030 e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1032 r = sd_bus_set_address(b, e);
1036 e = secure_getenv("XDG_RUNTIME_DIR");
1038 _cleanup_free_ char *ee = NULL;
1040 ee = bus_address_escape(e);
1047 asprintf(&b->address, "kernel:path=/dev/kdbus/%lu-user/bus;unix:path=%s/bus", (unsigned long) getuid(), ee);
1049 b->address = strjoin("unix:path=", ee, "/bus", NULL);
1053 asprintf(&b->address, "kernel:path=/dev/kdbus/%lu-user/bus", (unsigned long) getuid());
1055 return -ECONNREFUSED;
1065 b->bus_client = true;
1067 r = sd_bus_start(b);
1079 _public_ int sd_bus_open_system_remote(const char *host, sd_bus **ret) {
1080 _cleanup_free_ char *e = NULL;
1085 assert_return(host, -EINVAL);
1086 assert_return(ret, -EINVAL);
1088 e = bus_address_escape(host);
1092 p = strjoin("unixexec:path=ssh,argv1=-xT,argv2=", e, ",argv3=systemd-stdio-bridge", NULL);
1096 r = sd_bus_new(&bus);
1103 bus->bus_client = true;
1105 r = sd_bus_start(bus);
1115 _public_ int sd_bus_open_system_container(const char *machine, sd_bus **ret) {
1116 _cleanup_free_ char *e = NULL;
1121 assert_return(machine, -EINVAL);
1122 assert_return(ret, -EINVAL);
1124 e = bus_address_escape(machine);
1128 p = strjoin("x-container:machine=", e, NULL);
1132 r = sd_bus_new(&bus);
1139 bus->bus_client = true;
1141 r = sd_bus_start(bus);
1151 _public_ void sd_bus_close(sd_bus *bus) {
1155 if (bus->state == BUS_CLOSED)
1157 if (bus_pid_changed(bus))
1160 bus->state = BUS_CLOSED;
1162 sd_bus_detach_event(bus);
1164 /* Drop all queued messages so that they drop references to
1165 * the bus object and the bus may be freed */
1166 bus_reset_queues(bus);
1168 if (!bus->is_kernel)
1171 /* We'll leave the fd open in case this is a kernel bus, since
1172 * there might still be memblocks around that reference this
1173 * bus, and they might need to invoke the * KDBUS_CMD_FREE
1174 * ioctl on the fd when they are freed. */
1177 static void bus_enter_closing(sd_bus *bus) {
1180 if (bus->state != BUS_OPENING &&
1181 bus->state != BUS_AUTHENTICATING &&
1182 bus->state != BUS_HELLO &&
1183 bus->state != BUS_RUNNING)
1186 bus->state = BUS_CLOSING;
1189 _public_ sd_bus *sd_bus_ref(sd_bus *bus) {
1190 assert_return(bus, NULL);
1192 assert_se(REFCNT_INC(bus->n_ref) >= 2);
1197 _public_ sd_bus *sd_bus_unref(sd_bus *bus) {
1198 assert_return(bus, NULL);
1200 if (REFCNT_DEC(bus->n_ref) <= 0)
1206 _public_ int sd_bus_is_open(sd_bus *bus) {
1208 assert_return(bus, -EINVAL);
1209 assert_return(!bus_pid_changed(bus), -ECHILD);
1211 return BUS_IS_OPEN(bus->state);
1214 _public_ int sd_bus_can_send(sd_bus *bus, char type) {
1217 assert_return(bus, -EINVAL);
1218 assert_return(bus->state != BUS_UNSET, -ENOTCONN);
1219 assert_return(!bus_pid_changed(bus), -ECHILD);
1221 if (type == SD_BUS_TYPE_UNIX_FD) {
1222 if (!(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD))
1225 r = bus_ensure_running(bus);
1229 return bus->can_fds;
1232 return bus_type_is_valid(type);
1235 _public_ int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
1238 assert_return(bus, -EINVAL);
1239 assert_return(server_id, -EINVAL);
1240 assert_return(!bus_pid_changed(bus), -ECHILD);
1242 r = bus_ensure_running(bus);
1246 *server_id = bus->server_id;
1250 static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
1254 if (m->header->version > b->message_version)
1258 /* If we copy the same message to multiple
1259 * destinations, avoid using the same serial
1261 b->serial = MAX(b->serial, BUS_MESSAGE_SERIAL(m));
1265 return bus_message_seal(m, ++b->serial);
1268 int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
1272 if (m->header->version > b->message_version)
1275 /* The bus specification says the serial number cannot be 0,
1276 * hence let's fill something in for synthetic messages. Since
1277 * synthetic messages might have a fake sender and we don't
1278 * want to interfere with the real sender's serial numbers we
1279 * pick a fixed, artifical one. We use (uint32_t) -1 rather
1280 * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
1281 * even though kdbus can do 64bit. */
1283 return bus_message_seal(m, 0xFFFFFFFFULL);
1286 static int bus_write_message(sd_bus *bus, sd_bus_message *message, size_t *idx) {
1291 return bus_kernel_write_message(bus, message);
1293 return bus_socket_write_message(bus, message, idx);
1296 static int dispatch_wqueue(sd_bus *bus) {
1300 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1302 while (bus->wqueue_size > 0) {
1304 r = bus_write_message(bus, bus->wqueue[0], &bus->windex);
1308 /* Didn't do anything this time */
1310 else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
1311 /* Fully written. Let's drop the entry from
1314 * This isn't particularly optimized, but
1315 * well, this is supposed to be our worst-case
1316 * buffer only, and the socket buffer is
1317 * supposed to be our primary buffer, and if
1318 * it got full, then all bets are off
1321 sd_bus_message_unref(bus->wqueue[0]);
1322 bus->wqueue_size --;
1323 memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
1333 static int bus_read_message(sd_bus *bus) {
1337 return bus_kernel_read_message(bus);
1339 return bus_socket_read_message(bus);
1342 int bus_rqueue_make_room(sd_bus *bus) {
1346 x = bus->rqueue_size + 1;
1348 if (bus->rqueue_allocated >= x)
1351 if (x > BUS_RQUEUE_MAX)
1354 q = realloc(bus->rqueue, x * sizeof(sd_bus_message*));
1359 bus->rqueue_allocated = x;
1364 static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
1369 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1372 if (bus->rqueue_size > 0) {
1373 /* Dispatch a queued message */
1375 *m = bus->rqueue[0];
1376 bus->rqueue_size --;
1377 memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
1381 /* Try to read a new message */
1382 r = bus_read_message(bus);
1392 _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
1395 assert_return(bus, -EINVAL);
1396 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1397 assert_return(m, -EINVAL);
1398 assert_return(!bus_pid_changed(bus), -ECHILD);
1401 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
1408 /* If the serial number isn't kept, then we know that no reply
1410 if (!serial && !m->sealed)
1411 m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
1413 r = bus_seal_message(bus, m);
1417 /* If this is a reply and no reply was requested, then let's
1418 * suppress this, if we can */
1419 if (m->dont_send && !serial)
1422 if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
1425 r = bus_write_message(bus, m, &idx);
1427 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1428 bus_enter_closing(bus);
1431 } else if (!bus->is_kernel && idx < BUS_MESSAGE_SIZE(m)) {
1432 /* Wasn't fully written. So let's remember how
1433 * much was written. Note that the first entry
1434 * of the wqueue array is always allocated so
1435 * that we always can remember how much was
1437 bus->wqueue[0] = sd_bus_message_ref(m);
1438 bus->wqueue_size = 1;
1444 /* Just append it to the queue. */
1446 if (bus->wqueue_size >= BUS_WQUEUE_MAX)
1449 q = realloc(bus->wqueue, sizeof(sd_bus_message*) * (bus->wqueue_size + 1));
1454 q[bus->wqueue_size ++] = sd_bus_message_ref(m);
1458 *serial = BUS_MESSAGE_SERIAL(m);
1463 _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *serial) {
1466 assert_return(bus, -EINVAL);
1467 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1468 assert_return(m, -EINVAL);
1469 assert_return(!bus_pid_changed(bus), -ECHILD);
1471 if (!streq_ptr(m->destination, destination)) {
1476 r = sd_bus_message_set_destination(m, destination);
1481 return sd_bus_send(bus, m, serial);
1484 static usec_t calc_elapse(uint64_t usec) {
1485 if (usec == (uint64_t) -1)
1489 usec = BUS_DEFAULT_TIMEOUT;
1491 return now(CLOCK_MONOTONIC) + usec;
1494 static int timeout_compare(const void *a, const void *b) {
1495 const struct reply_callback *x = a, *y = b;
1497 if (x->timeout != 0 && y->timeout == 0)
1500 if (x->timeout == 0 && y->timeout != 0)
1503 if (x->timeout < y->timeout)
1506 if (x->timeout > y->timeout)
1512 _public_ int sd_bus_call_async(
1515 sd_bus_message_handler_t callback,
1520 struct reply_callback *c;
1523 assert_return(bus, -EINVAL);
1524 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1525 assert_return(m, -EINVAL);
1526 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1527 assert_return(!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1528 assert_return(callback, -EINVAL);
1529 assert_return(!bus_pid_changed(bus), -ECHILD);
1531 r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
1535 if (usec != (uint64_t) -1) {
1536 r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
1541 r = bus_seal_message(bus, m);
1545 c = new0(struct reply_callback, 1);
1549 c->callback = callback;
1550 c->userdata = userdata;
1551 c->serial = BUS_MESSAGE_SERIAL(m);
1552 c->timeout = calc_elapse(usec);
1554 r = hashmap_put(bus->reply_callbacks, &c->serial, c);
1560 if (c->timeout != 0) {
1561 r = prioq_put(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1564 sd_bus_call_async_cancel(bus, c->serial);
1569 r = sd_bus_send(bus, m, serial);
1571 sd_bus_call_async_cancel(bus, c->serial);
1578 _public_ int sd_bus_call_async_cancel(sd_bus *bus, uint64_t serial) {
1579 struct reply_callback *c;
1581 assert_return(bus, -EINVAL);
1582 assert_return(serial != 0, -EINVAL);
1583 assert_return(!bus_pid_changed(bus), -ECHILD);
1585 c = hashmap_remove(bus->reply_callbacks, &serial);
1589 if (c->timeout != 0)
1590 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1596 int bus_ensure_running(sd_bus *bus) {
1601 if (bus->state == BUS_UNSET || bus->state == BUS_CLOSED || bus->state == BUS_CLOSING)
1603 if (bus->state == BUS_RUNNING)
1607 r = sd_bus_process(bus, NULL);
1610 if (bus->state == BUS_RUNNING)
1615 r = sd_bus_wait(bus, (uint64_t) -1);
1621 _public_ int sd_bus_call(
1625 sd_bus_error *error,
1626 sd_bus_message **reply) {
1633 assert_return(bus, -EINVAL);
1634 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1635 assert_return(m, -EINVAL);
1636 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1637 assert_return(!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1638 assert_return(!bus_error_is_dirty(error), -EINVAL);
1639 assert_return(!bus_pid_changed(bus), -ECHILD);
1641 r = bus_ensure_running(bus);
1645 i = bus->rqueue_size;
1647 r = sd_bus_send(bus, m, &serial);
1651 timeout = calc_elapse(usec);
1656 while (i < bus->rqueue_size) {
1657 sd_bus_message *incoming = NULL;
1659 incoming = bus->rqueue[i];
1661 if (incoming->reply_serial == serial) {
1662 /* Found a match! */
1664 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1667 if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
1672 sd_bus_message_unref(incoming);
1675 } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
1676 r = sd_bus_error_copy(error, &incoming->error);
1680 sd_bus_message_unref(incoming);
1683 } else if (incoming->header->serial == serial &&
1686 streq(bus->unique_name, incoming->sender)) {
1688 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1691 /* Our own message? Somebody is trying
1692 * to send its own client a message,
1693 * let's not dead-lock, let's fail
1696 sd_bus_message_unref(incoming);
1700 /* Try to read more, right-away */
1704 r = bus_read_message(bus);
1706 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1707 bus_enter_closing(bus);
1717 n = now(CLOCK_MONOTONIC);
1723 left = (uint64_t) -1;
1725 r = bus_poll(bus, true, left);
1729 r = dispatch_wqueue(bus);
1731 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1732 bus_enter_closing(bus);
1739 _public_ int sd_bus_get_fd(sd_bus *bus) {
1741 assert_return(bus, -EINVAL);
1742 assert_return(bus->input_fd == bus->output_fd, -EPERM);
1743 assert_return(!bus_pid_changed(bus), -ECHILD);
1745 return bus->input_fd;
1748 _public_ int sd_bus_get_events(sd_bus *bus) {
1751 assert_return(bus, -EINVAL);
1752 assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
1753 assert_return(!bus_pid_changed(bus), -ECHILD);
1755 if (bus->state == BUS_OPENING)
1757 else if (bus->state == BUS_AUTHENTICATING) {
1759 if (bus_socket_auth_needs_write(bus))
1764 } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
1765 if (bus->rqueue_size <= 0)
1767 if (bus->wqueue_size > 0)
1774 _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
1775 struct reply_callback *c;
1777 assert_return(bus, -EINVAL);
1778 assert_return(timeout_usec, -EINVAL);
1779 assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
1780 assert_return(!bus_pid_changed(bus), -ECHILD);
1782 if (bus->state == BUS_CLOSING) {
1787 if (bus->state == BUS_AUTHENTICATING) {
1788 *timeout_usec = bus->auth_timeout;
1792 if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) {
1793 *timeout_usec = (uint64_t) -1;
1797 if (bus->rqueue_size > 0) {
1802 c = prioq_peek(bus->reply_callbacks_prioq);
1804 *timeout_usec = (uint64_t) -1;
1808 *timeout_usec = c->timeout;
1812 static int process_timeout(sd_bus *bus) {
1813 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1814 _cleanup_bus_message_unref_ sd_bus_message* m = NULL;
1815 struct reply_callback *c;
1821 c = prioq_peek(bus->reply_callbacks_prioq);
1825 n = now(CLOCK_MONOTONIC);
1829 r = bus_message_new_synthetic_error(
1832 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
1837 m->sender = "org.freedesktop.DBus";
1839 r = bus_seal_synthetic_message(bus, m);
1843 assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
1844 hashmap_remove(bus->reply_callbacks, &c->serial);
1847 bus->iteration_counter ++;
1849 r = c->callback(bus, m, c->userdata, &error_buffer);
1850 r = bus_maybe_reply_error(m, r, &error_buffer);
1853 bus->current = NULL;
1858 static int process_hello(sd_bus *bus, sd_bus_message *m) {
1862 if (bus->state != BUS_HELLO)
1865 /* Let's make sure the first message on the bus is the HELLO
1866 * reply. But note that we don't actually parse the message
1867 * here (we leave that to the usual handling), we just verify
1868 * we don't let any earlier msg through. */
1870 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
1871 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1874 if (m->reply_serial != bus->hello_serial)
1880 static int process_reply(sd_bus *bus, sd_bus_message *m) {
1881 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1882 struct reply_callback *c;
1888 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
1889 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1892 c = hashmap_remove(bus->reply_callbacks, &m->reply_serial);
1896 if (c->timeout != 0)
1897 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1899 r = sd_bus_message_rewind(m, true);
1903 r = c->callback(bus, m, c->userdata, &error_buffer);
1904 r = bus_maybe_reply_error(m, r, &error_buffer);
1910 static int process_filter(sd_bus *bus, sd_bus_message *m) {
1911 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1912 struct filter_callback *l;
1919 bus->filter_callbacks_modified = false;
1921 LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
1923 if (bus->filter_callbacks_modified)
1926 /* Don't run this more than once per iteration */
1927 if (l->last_iteration == bus->iteration_counter)
1930 l->last_iteration = bus->iteration_counter;
1932 r = sd_bus_message_rewind(m, true);
1936 r = l->callback(bus, m, l->userdata, &error_buffer);
1937 r = bus_maybe_reply_error(m, r, &error_buffer);
1943 } while (bus->filter_callbacks_modified);
1948 static int process_match(sd_bus *bus, sd_bus_message *m) {
1955 bus->match_callbacks_modified = false;
1957 r = bus_match_run(bus, &bus->match_callbacks, m);
1961 } while (bus->match_callbacks_modified);
1966 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
1967 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1973 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
1976 if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
1979 if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
1982 if (streq_ptr(m->member, "Ping"))
1983 r = sd_bus_message_new_method_return(m, &reply);
1984 else if (streq_ptr(m->member, "GetMachineId")) {
1988 r = sd_id128_get_machine(&id);
1992 r = sd_bus_message_new_method_return(m, &reply);
1996 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
1998 r = sd_bus_message_new_method_errorf(
2000 SD_BUS_ERROR_UNKNOWN_METHOD,
2001 "Unknown method '%s' on interface '%s'.", m->member, m->interface);
2007 r = sd_bus_send(bus, reply, NULL);
2014 static int process_message(sd_bus *bus, sd_bus_message *m) {
2021 bus->iteration_counter++;
2023 log_debug("Got message sender=%s object=%s interface=%s member=%s",
2024 strna(sd_bus_message_get_sender(m)),
2025 strna(sd_bus_message_get_path(m)),
2026 strna(sd_bus_message_get_interface(m)),
2027 strna(sd_bus_message_get_member(m)));
2029 r = process_hello(bus, m);
2033 r = process_reply(bus, m);
2037 r = process_filter(bus, m);
2041 r = process_match(bus, m);
2045 r = process_builtin(bus, m);
2049 r = bus_process_object(bus, m);
2052 bus->current = NULL;
2056 static int process_running(sd_bus *bus, sd_bus_message **ret) {
2057 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2061 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
2063 r = process_timeout(bus);
2067 r = dispatch_wqueue(bus);
2071 r = dispatch_rqueue(bus, &m);
2077 r = process_message(bus, m);
2082 r = sd_bus_message_rewind(m, true);
2091 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
2093 r = sd_bus_reply_method_errorf(
2095 SD_BUS_ERROR_UNKNOWN_OBJECT,
2096 "Unknown object '%s'.", m->path);
2110 static int process_closing(sd_bus *bus, sd_bus_message **ret) {
2111 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2112 struct reply_callback *c;
2116 assert(bus->state == BUS_CLOSING);
2118 c = hashmap_first(bus->reply_callbacks);
2120 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2122 /* First, fail all outstanding method calls */
2123 r = bus_message_new_synthetic_error(
2126 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
2131 r = bus_seal_synthetic_message(bus, m);
2135 if (c->timeout != 0)
2136 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2138 hashmap_remove(bus->reply_callbacks, &c->serial);
2141 bus->iteration_counter++;
2143 r = c->callback(bus, m, c->userdata, &error_buffer);
2144 r = bus_maybe_reply_error(m, r, &error_buffer);
2150 /* Then, synthesize a Disconnected message */
2151 r = sd_bus_message_new_signal(
2153 "/org/freedesktop/DBus/Local",
2154 "org.freedesktop.DBus.Local",
2160 m->sender = "org.freedesktop.DBus.Local";
2162 r = bus_seal_synthetic_message(bus, m);
2169 bus->iteration_counter++;
2171 r = process_filter(bus, m);
2175 r = process_match(bus, m);
2187 bus->current = NULL;
2191 _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
2192 BUS_DONT_DESTROY(bus);
2195 /* Returns 0 when we didn't do anything. This should cause the
2196 * caller to invoke sd_bus_wait() before returning the next
2197 * time. Returns > 0 when we did something, which possibly
2198 * means *ret is filled in with an unprocessed message. */
2200 assert_return(bus, -EINVAL);
2201 assert_return(!bus_pid_changed(bus), -ECHILD);
2203 /* We don't allow recursively invoking sd_bus_process(). */
2204 assert_return(!bus->current, -EBUSY);
2206 switch (bus->state) {
2213 r = bus_socket_process_opening(bus);
2214 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2215 bus_enter_closing(bus);
2223 case BUS_AUTHENTICATING:
2224 r = bus_socket_process_authenticating(bus);
2225 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2226 bus_enter_closing(bus);
2238 r = process_running(bus, ret);
2239 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2240 bus_enter_closing(bus);
2250 return process_closing(bus, ret);
2253 assert_not_reached("Unknown state");
2256 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
2257 struct pollfd p[2] = {};
2260 usec_t m = (usec_t) -1;
2264 if (bus->state == BUS_CLOSING)
2267 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2269 e = sd_bus_get_events(bus);
2274 /* The caller really needs some more data, he doesn't
2275 * care about what's already read, or any timeouts
2280 /* The caller wants to process if there's something to
2281 * process, but doesn't care otherwise */
2283 r = sd_bus_get_timeout(bus, &until);
2288 nw = now(CLOCK_MONOTONIC);
2289 m = until > nw ? until - nw : 0;
2293 if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
2296 p[0].fd = bus->input_fd;
2297 if (bus->output_fd == bus->input_fd) {
2301 p[0].events = e & POLLIN;
2302 p[1].fd = bus->output_fd;
2303 p[1].events = e & POLLOUT;
2307 r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
2311 return r > 0 ? 1 : 0;
2314 _public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
2316 assert_return(bus, -EINVAL);
2317 assert_return(!bus_pid_changed(bus), -ECHILD);
2319 if (bus->state == BUS_CLOSING)
2322 assert_return(BUS_IS_OPEN(bus->state) , -ENOTCONN);
2324 if (bus->rqueue_size > 0)
2327 return bus_poll(bus, false, timeout_usec);
2330 _public_ int sd_bus_flush(sd_bus *bus) {
2333 assert_return(bus, -EINVAL);
2334 assert_return(!bus_pid_changed(bus), -ECHILD);
2336 if (bus->state == BUS_CLOSING)
2339 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2341 r = bus_ensure_running(bus);
2345 if (bus->wqueue_size <= 0)
2349 r = dispatch_wqueue(bus);
2351 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
2352 bus_enter_closing(bus);
2357 if (bus->wqueue_size <= 0)
2360 r = bus_poll(bus, false, (uint64_t) -1);
2366 _public_ int sd_bus_add_filter(sd_bus *bus,
2367 sd_bus_message_handler_t callback,
2370 struct filter_callback *f;
2372 assert_return(bus, -EINVAL);
2373 assert_return(callback, -EINVAL);
2374 assert_return(!bus_pid_changed(bus), -ECHILD);
2376 f = new0(struct filter_callback, 1);
2379 f->callback = callback;
2380 f->userdata = userdata;
2382 bus->filter_callbacks_modified = true;
2383 LIST_PREPEND(callbacks, bus->filter_callbacks, f);
2387 _public_ int sd_bus_remove_filter(sd_bus *bus,
2388 sd_bus_message_handler_t callback,
2391 struct filter_callback *f;
2393 assert_return(bus, -EINVAL);
2394 assert_return(callback, -EINVAL);
2395 assert_return(!bus_pid_changed(bus), -ECHILD);
2397 LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
2398 if (f->callback == callback && f->userdata == userdata) {
2399 bus->filter_callbacks_modified = true;
2400 LIST_REMOVE(callbacks, bus->filter_callbacks, f);
2409 _public_ int sd_bus_add_match(sd_bus *bus,
2411 sd_bus_message_handler_t callback,
2414 struct bus_match_component *components = NULL;
2415 unsigned n_components = 0;
2416 uint64_t cookie = 0;
2419 assert_return(bus, -EINVAL);
2420 assert_return(match, -EINVAL);
2421 assert_return(!bus_pid_changed(bus), -ECHILD);
2423 r = bus_match_parse(match, &components, &n_components);
2427 if (bus->bus_client) {
2428 cookie = ++bus->match_cookie;
2430 r = bus_add_match_internal(bus, match, components, n_components, cookie);
2435 bus->match_callbacks_modified = true;
2436 r = bus_match_add(&bus->match_callbacks, components, n_components, callback, userdata, cookie, NULL);
2438 if (bus->bus_client)
2439 bus_remove_match_internal(bus, match, cookie);
2443 bus_match_parse_free(components, n_components);
2447 _public_ int sd_bus_remove_match(sd_bus *bus,
2449 sd_bus_message_handler_t callback,
2452 struct bus_match_component *components = NULL;
2453 unsigned n_components = 0;
2455 uint64_t cookie = 0;
2457 assert_return(bus, -EINVAL);
2458 assert_return(match, -EINVAL);
2459 assert_return(!bus_pid_changed(bus), -ECHILD);
2461 r = bus_match_parse(match, &components, &n_components);
2465 bus->match_callbacks_modified = true;
2466 r = bus_match_remove(&bus->match_callbacks, components, n_components, callback, userdata, &cookie);
2468 if (bus->bus_client)
2469 q = bus_remove_match_internal(bus, match, cookie);
2471 bus_match_parse_free(components, n_components);
2473 return r < 0 ? r : q;
2476 bool bus_pid_changed(sd_bus *bus) {
2479 /* We don't support people creating a bus connection and
2480 * keeping it around over a fork(). Let's complain. */
2482 return bus->original_pid != getpid();
2485 static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2486 sd_bus *bus = userdata;
2491 r = sd_bus_process(bus, NULL);
2498 static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
2499 sd_bus *bus = userdata;
2504 r = sd_bus_process(bus, NULL);
2511 static int prepare_callback(sd_event_source *s, void *userdata) {
2512 sd_bus *bus = userdata;
2519 e = sd_bus_get_events(bus);
2523 if (bus->output_fd != bus->input_fd) {
2525 r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN);
2529 r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT);
2533 r = sd_event_source_set_io_events(bus->input_io_event_source, e);
2538 r = sd_bus_get_timeout(bus, &until);
2544 j = sd_event_source_set_time(bus->time_event_source, until);
2549 r = sd_event_source_set_enabled(bus->time_event_source, r > 0);
2556 static int quit_callback(sd_event_source *event, void *userdata) {
2557 sd_bus *bus = userdata;
2566 _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
2569 assert_return(bus, -EINVAL);
2570 assert_return(!bus->event, -EBUSY);
2572 assert(!bus->input_io_event_source);
2573 assert(!bus->output_io_event_source);
2574 assert(!bus->time_event_source);
2577 bus->event = sd_event_ref(event);
2579 r = sd_event_default(&bus->event);
2584 r = sd_event_add_io(bus->event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source);
2588 r = sd_event_source_set_priority(bus->input_io_event_source, priority);
2592 if (bus->output_fd != bus->input_fd) {
2593 r = sd_event_add_io(bus->event, bus->output_fd, 0, io_callback, bus, &bus->output_io_event_source);
2597 r = sd_event_source_set_priority(bus->output_io_event_source, priority);
2602 r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
2606 r = sd_event_add_monotonic(bus->event, 0, 0, time_callback, bus, &bus->time_event_source);
2610 r = sd_event_source_set_priority(bus->time_event_source, priority);
2614 r = sd_event_add_quit(bus->event, quit_callback, bus, &bus->quit_event_source);
2621 sd_bus_detach_event(bus);
2625 _public_ int sd_bus_detach_event(sd_bus *bus) {
2626 assert_return(bus, -EINVAL);
2627 assert_return(bus->event, -ENXIO);
2629 if (bus->input_io_event_source) {
2630 sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
2631 bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
2634 if (bus->output_io_event_source) {
2635 sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
2636 bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
2639 if (bus->time_event_source) {
2640 sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF);
2641 bus->time_event_source = sd_event_source_unref(bus->time_event_source);
2644 if (bus->quit_event_source) {
2645 sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF);
2646 bus->quit_event_source = sd_event_source_unref(bus->quit_event_source);
2650 bus->event = sd_event_unref(bus->event);
2655 _public_ sd_event* sd_bus_get_event(sd_bus *bus) {
2656 assert_return(bus, NULL);
2661 _public_ sd_bus_message* sd_bus_get_current(sd_bus *bus) {
2662 assert_return(bus, NULL);
2664 return bus->current;
2667 static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
2672 assert(default_bus);
2675 return !!*default_bus;
2678 *ret = sd_bus_ref(*default_bus);
2686 b->default_bus_ptr = default_bus;
2694 _public_ int sd_bus_default_system(sd_bus **ret) {
2695 static __thread sd_bus *default_system_bus = NULL;
2697 return bus_default(sd_bus_open_system, &default_system_bus, ret);
2700 _public_ int sd_bus_default_user(sd_bus **ret) {
2701 static __thread sd_bus *default_user_bus = NULL;
2703 return bus_default(sd_bus_open_user, &default_user_bus, ret);
2706 _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
2707 assert_return(b, -EINVAL);
2708 assert_return(tid, -EINVAL);
2709 assert_return(!bus_pid_changed(b), -ECHILD);
2717 return sd_event_get_tid(b->event, tid);
2722 _public_ char *sd_bus_label_escape(const char *s) {
2726 assert_return(s, NULL);
2728 /* Escapes all chars that D-Bus' object path cannot deal
2729 * with. Can be reversed with bus_path_unescape(). We special
2730 * case the empty string. */
2735 r = new(char, strlen(s)*3 + 1);
2739 for (f = s, t = r; *f; f++) {
2741 /* Escape everything that is not a-zA-Z0-9. We also
2742 * escape 0-9 if it's the first character */
2744 if (!(*f >= 'A' && *f <= 'Z') &&
2745 !(*f >= 'a' && *f <= 'z') &&
2746 !(f > s && *f >= '0' && *f <= '9')) {
2748 *(t++) = hexchar(*f >> 4);
2749 *(t++) = hexchar(*f);
2759 _public_ char *sd_bus_label_unescape(const char *f) {
2762 assert_return(f, NULL);
2764 /* Special case for the empty string */
2768 r = new(char, strlen(f) + 1);
2772 for (t = r; *f; f++) {
2777 if ((a = unhexchar(f[1])) < 0 ||
2778 (b = unhexchar(f[2])) < 0) {
2779 /* Invalid escape code, let's take it literal then */
2782 *(t++) = (char) ((a << 4) | b);
2794 _public_ int sd_bus_get_peer_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
2799 assert_return(bus, -EINVAL);
2800 assert_return(mask <= _SD_BUS_CREDS_MAX, -ENOTSUP);
2801 assert_return(ret, -EINVAL);
2802 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2803 assert_return(!bus_pid_changed(bus), -ECHILD);
2804 assert_return(!bus->is_kernel, -ENOTSUP);
2806 if (!bus->ucred_valid && !isempty(bus->label))
2809 c = bus_creds_new();
2813 if (bus->ucred_valid) {
2814 pid = c->pid = bus->ucred.pid;
2815 c->uid = bus->ucred.uid;
2816 c->gid = bus->ucred.gid;
2818 c->mask |= (SD_BUS_CREDS_UID | SD_BUS_CREDS_PID | SD_BUS_CREDS_GID) & mask;
2821 if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) {
2822 c->label = strdup(bus->label);
2824 sd_bus_creds_unref(c);
2828 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
2831 r = bus_creds_add_more(c, mask, pid, 0);