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) {
1293 return bus_kernel_write_message(bus, message);
1295 return bus_socket_write_message(bus, message, idx);
1300 static int dispatch_wqueue(sd_bus *bus) {
1304 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1306 while (bus->wqueue_size > 0) {
1308 r = bus_write_message(bus, bus->wqueue[0], &bus->windex);
1312 /* Didn't do anything this time */
1314 else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
1315 /* Fully written. Let's drop the entry from
1318 * This isn't particularly optimized, but
1319 * well, this is supposed to be our worst-case
1320 * buffer only, and the socket buffer is
1321 * supposed to be our primary buffer, and if
1322 * it got full, then all bets are off
1325 sd_bus_message_unref(bus->wqueue[0]);
1326 bus->wqueue_size --;
1327 memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
1337 static int bus_read_message(sd_bus *bus) {
1341 return bus_kernel_read_message(bus);
1343 return bus_socket_read_message(bus);
1346 int bus_rqueue_make_room(sd_bus *bus) {
1350 x = bus->rqueue_size + 1;
1352 if (bus->rqueue_allocated >= x)
1355 if (x > BUS_RQUEUE_MAX)
1358 q = realloc(bus->rqueue, x * sizeof(sd_bus_message*));
1363 bus->rqueue_allocated = x;
1368 static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
1373 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1376 if (bus->rqueue_size > 0) {
1377 /* Dispatch a queued message */
1379 *m = bus->rqueue[0];
1380 bus->rqueue_size --;
1381 memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
1385 /* Try to read a new message */
1386 r = bus_read_message(bus);
1396 _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
1399 assert_return(bus, -EINVAL);
1400 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1401 assert_return(m, -EINVAL);
1402 assert_return(!bus_pid_changed(bus), -ECHILD);
1405 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
1412 /* If the serial number isn't kept, then we know that no reply
1414 if (!serial && !m->sealed)
1415 m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
1417 r = bus_seal_message(bus, m);
1421 /* If this is a reply and no reply was requested, then let's
1422 * suppress this, if we can */
1423 if (m->dont_send && !serial)
1426 if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
1429 r = bus_write_message(bus, m, &idx);
1431 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1432 bus_enter_closing(bus);
1435 } else if (!bus->is_kernel && idx < BUS_MESSAGE_SIZE(m)) {
1436 /* Wasn't fully written. So let's remember how
1437 * much was written. Note that the first entry
1438 * of the wqueue array is always allocated so
1439 * that we always can remember how much was
1441 bus->wqueue[0] = sd_bus_message_ref(m);
1442 bus->wqueue_size = 1;
1448 /* Just append it to the queue. */
1450 if (bus->wqueue_size >= BUS_WQUEUE_MAX)
1453 q = realloc(bus->wqueue, sizeof(sd_bus_message*) * (bus->wqueue_size + 1));
1458 q[bus->wqueue_size ++] = sd_bus_message_ref(m);
1462 *serial = BUS_MESSAGE_SERIAL(m);
1467 _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *serial) {
1470 assert_return(bus, -EINVAL);
1471 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1472 assert_return(m, -EINVAL);
1473 assert_return(!bus_pid_changed(bus), -ECHILD);
1475 if (!streq_ptr(m->destination, destination)) {
1480 r = sd_bus_message_set_destination(m, destination);
1485 return sd_bus_send(bus, m, serial);
1488 static usec_t calc_elapse(uint64_t usec) {
1489 if (usec == (uint64_t) -1)
1493 usec = BUS_DEFAULT_TIMEOUT;
1495 return now(CLOCK_MONOTONIC) + usec;
1498 static int timeout_compare(const void *a, const void *b) {
1499 const struct reply_callback *x = a, *y = b;
1501 if (x->timeout != 0 && y->timeout == 0)
1504 if (x->timeout == 0 && y->timeout != 0)
1507 if (x->timeout < y->timeout)
1510 if (x->timeout > y->timeout)
1516 _public_ int sd_bus_call_async(
1519 sd_bus_message_handler_t callback,
1524 struct reply_callback *c;
1527 assert_return(bus, -EINVAL);
1528 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1529 assert_return(m, -EINVAL);
1530 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1531 assert_return(!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1532 assert_return(callback, -EINVAL);
1533 assert_return(!bus_pid_changed(bus), -ECHILD);
1535 r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
1539 if (usec != (uint64_t) -1) {
1540 r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
1545 r = bus_seal_message(bus, m);
1549 c = new0(struct reply_callback, 1);
1553 c->callback = callback;
1554 c->userdata = userdata;
1555 c->serial = BUS_MESSAGE_SERIAL(m);
1556 c->timeout = calc_elapse(usec);
1558 r = hashmap_put(bus->reply_callbacks, &c->serial, c);
1564 if (c->timeout != 0) {
1565 r = prioq_put(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1568 sd_bus_call_async_cancel(bus, c->serial);
1573 r = sd_bus_send(bus, m, serial);
1575 sd_bus_call_async_cancel(bus, c->serial);
1582 _public_ int sd_bus_call_async_cancel(sd_bus *bus, uint64_t serial) {
1583 struct reply_callback *c;
1585 assert_return(bus, -EINVAL);
1586 assert_return(serial != 0, -EINVAL);
1587 assert_return(!bus_pid_changed(bus), -ECHILD);
1589 c = hashmap_remove(bus->reply_callbacks, &serial);
1593 if (c->timeout != 0)
1594 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1600 int bus_ensure_running(sd_bus *bus) {
1605 if (bus->state == BUS_UNSET || bus->state == BUS_CLOSED || bus->state == BUS_CLOSING)
1607 if (bus->state == BUS_RUNNING)
1611 r = sd_bus_process(bus, NULL);
1614 if (bus->state == BUS_RUNNING)
1619 r = sd_bus_wait(bus, (uint64_t) -1);
1625 _public_ int sd_bus_call(
1629 sd_bus_error *error,
1630 sd_bus_message **reply) {
1637 assert_return(bus, -EINVAL);
1638 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1639 assert_return(m, -EINVAL);
1640 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1641 assert_return(!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1642 assert_return(!bus_error_is_dirty(error), -EINVAL);
1643 assert_return(!bus_pid_changed(bus), -ECHILD);
1645 r = bus_ensure_running(bus);
1649 i = bus->rqueue_size;
1651 r = sd_bus_send(bus, m, &serial);
1655 timeout = calc_elapse(usec);
1660 while (i < bus->rqueue_size) {
1661 sd_bus_message *incoming = NULL;
1663 incoming = bus->rqueue[i];
1665 if (incoming->reply_serial == serial) {
1666 /* Found a match! */
1668 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1671 if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
1676 sd_bus_message_unref(incoming);
1679 } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
1680 r = sd_bus_error_copy(error, &incoming->error);
1684 sd_bus_message_unref(incoming);
1687 } else if (incoming->header->serial == serial &&
1690 streq(bus->unique_name, incoming->sender)) {
1692 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1695 /* Our own message? Somebody is trying
1696 * to send its own client a message,
1697 * let's not dead-lock, let's fail
1700 sd_bus_message_unref(incoming);
1704 /* Try to read more, right-away */
1708 r = bus_read_message(bus);
1710 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1711 bus_enter_closing(bus);
1721 n = now(CLOCK_MONOTONIC);
1727 left = (uint64_t) -1;
1729 r = bus_poll(bus, true, left);
1733 r = dispatch_wqueue(bus);
1735 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1736 bus_enter_closing(bus);
1743 _public_ int sd_bus_get_fd(sd_bus *bus) {
1745 assert_return(bus, -EINVAL);
1746 assert_return(bus->input_fd == bus->output_fd, -EPERM);
1747 assert_return(!bus_pid_changed(bus), -ECHILD);
1749 return bus->input_fd;
1752 _public_ int sd_bus_get_events(sd_bus *bus) {
1755 assert_return(bus, -EINVAL);
1756 assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
1757 assert_return(!bus_pid_changed(bus), -ECHILD);
1759 if (bus->state == BUS_OPENING)
1761 else if (bus->state == BUS_AUTHENTICATING) {
1763 if (bus_socket_auth_needs_write(bus))
1768 } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
1769 if (bus->rqueue_size <= 0)
1771 if (bus->wqueue_size > 0)
1778 _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
1779 struct reply_callback *c;
1781 assert_return(bus, -EINVAL);
1782 assert_return(timeout_usec, -EINVAL);
1783 assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
1784 assert_return(!bus_pid_changed(bus), -ECHILD);
1786 if (bus->state == BUS_CLOSING) {
1791 if (bus->state == BUS_AUTHENTICATING) {
1792 *timeout_usec = bus->auth_timeout;
1796 if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) {
1797 *timeout_usec = (uint64_t) -1;
1801 if (bus->rqueue_size > 0) {
1806 c = prioq_peek(bus->reply_callbacks_prioq);
1808 *timeout_usec = (uint64_t) -1;
1812 *timeout_usec = c->timeout;
1816 static int process_timeout(sd_bus *bus) {
1817 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1818 _cleanup_bus_message_unref_ sd_bus_message* m = NULL;
1819 struct reply_callback *c;
1825 c = prioq_peek(bus->reply_callbacks_prioq);
1829 n = now(CLOCK_MONOTONIC);
1833 r = bus_message_new_synthetic_error(
1836 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
1841 m->sender = "org.freedesktop.DBus";
1843 r = bus_seal_synthetic_message(bus, m);
1847 assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
1848 hashmap_remove(bus->reply_callbacks, &c->serial);
1851 bus->iteration_counter ++;
1853 r = c->callback(bus, m, c->userdata, &error_buffer);
1854 r = bus_maybe_reply_error(m, r, &error_buffer);
1857 bus->current = NULL;
1862 static int process_hello(sd_bus *bus, sd_bus_message *m) {
1866 if (bus->state != BUS_HELLO)
1869 /* Let's make sure the first message on the bus is the HELLO
1870 * reply. But note that we don't actually parse the message
1871 * here (we leave that to the usual handling), we just verify
1872 * we don't let any earlier msg through. */
1874 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
1875 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1878 if (m->reply_serial != bus->hello_serial)
1884 static int process_reply(sd_bus *bus, sd_bus_message *m) {
1885 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1886 struct reply_callback *c;
1892 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
1893 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1896 c = hashmap_remove(bus->reply_callbacks, &m->reply_serial);
1900 if (c->timeout != 0)
1901 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1903 r = sd_bus_message_rewind(m, true);
1907 r = c->callback(bus, m, c->userdata, &error_buffer);
1908 r = bus_maybe_reply_error(m, r, &error_buffer);
1914 static int process_filter(sd_bus *bus, sd_bus_message *m) {
1915 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1916 struct filter_callback *l;
1923 bus->filter_callbacks_modified = false;
1925 LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
1927 if (bus->filter_callbacks_modified)
1930 /* Don't run this more than once per iteration */
1931 if (l->last_iteration == bus->iteration_counter)
1934 l->last_iteration = bus->iteration_counter;
1936 r = sd_bus_message_rewind(m, true);
1940 r = l->callback(bus, m, l->userdata, &error_buffer);
1941 r = bus_maybe_reply_error(m, r, &error_buffer);
1947 } while (bus->filter_callbacks_modified);
1952 static int process_match(sd_bus *bus, sd_bus_message *m) {
1959 bus->match_callbacks_modified = false;
1961 r = bus_match_run(bus, &bus->match_callbacks, m);
1965 } while (bus->match_callbacks_modified);
1970 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
1971 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1977 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
1980 if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
1983 if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
1986 if (streq_ptr(m->member, "Ping"))
1987 r = sd_bus_message_new_method_return(m, &reply);
1988 else if (streq_ptr(m->member, "GetMachineId")) {
1992 r = sd_id128_get_machine(&id);
1996 r = sd_bus_message_new_method_return(m, &reply);
2000 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
2002 r = sd_bus_message_new_method_errorf(
2004 SD_BUS_ERROR_UNKNOWN_METHOD,
2005 "Unknown method '%s' on interface '%s'.", m->member, m->interface);
2011 r = sd_bus_send(bus, reply, NULL);
2018 static int process_message(sd_bus *bus, sd_bus_message *m) {
2025 bus->iteration_counter++;
2027 log_debug("Got message sender=%s object=%s interface=%s member=%s",
2028 strna(sd_bus_message_get_sender(m)),
2029 strna(sd_bus_message_get_path(m)),
2030 strna(sd_bus_message_get_interface(m)),
2031 strna(sd_bus_message_get_member(m)));
2033 r = process_hello(bus, m);
2037 r = process_reply(bus, m);
2041 r = process_filter(bus, m);
2045 r = process_match(bus, m);
2049 r = process_builtin(bus, m);
2053 r = bus_process_object(bus, m);
2056 bus->current = NULL;
2060 static int process_running(sd_bus *bus, sd_bus_message **ret) {
2061 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2065 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
2067 r = process_timeout(bus);
2071 r = dispatch_wqueue(bus);
2075 r = dispatch_rqueue(bus, &m);
2081 r = process_message(bus, m);
2086 r = sd_bus_message_rewind(m, true);
2095 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
2097 r = sd_bus_reply_method_errorf(
2099 SD_BUS_ERROR_UNKNOWN_OBJECT,
2100 "Unknown object '%s'.", m->path);
2114 static int process_closing(sd_bus *bus, sd_bus_message **ret) {
2115 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2116 struct reply_callback *c;
2120 assert(bus->state == BUS_CLOSING);
2122 c = hashmap_first(bus->reply_callbacks);
2124 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2126 /* First, fail all outstanding method calls */
2127 r = bus_message_new_synthetic_error(
2130 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
2135 r = bus_seal_synthetic_message(bus, m);
2139 if (c->timeout != 0)
2140 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2142 hashmap_remove(bus->reply_callbacks, &c->serial);
2145 bus->iteration_counter++;
2147 r = c->callback(bus, m, c->userdata, &error_buffer);
2148 r = bus_maybe_reply_error(m, r, &error_buffer);
2154 /* Then, synthesize a Disconnected message */
2155 r = sd_bus_message_new_signal(
2157 "/org/freedesktop/DBus/Local",
2158 "org.freedesktop.DBus.Local",
2164 m->sender = "org.freedesktop.DBus.Local";
2166 r = bus_seal_synthetic_message(bus, m);
2173 bus->iteration_counter++;
2175 r = process_filter(bus, m);
2179 r = process_match(bus, m);
2191 bus->current = NULL;
2195 _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
2196 BUS_DONT_DESTROY(bus);
2199 /* Returns 0 when we didn't do anything. This should cause the
2200 * caller to invoke sd_bus_wait() before returning the next
2201 * time. Returns > 0 when we did something, which possibly
2202 * means *ret is filled in with an unprocessed message. */
2204 assert_return(bus, -EINVAL);
2205 assert_return(!bus_pid_changed(bus), -ECHILD);
2207 /* We don't allow recursively invoking sd_bus_process(). */
2208 assert_return(!bus->current, -EBUSY);
2210 switch (bus->state) {
2217 r = bus_socket_process_opening(bus);
2218 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2219 bus_enter_closing(bus);
2227 case BUS_AUTHENTICATING:
2228 r = bus_socket_process_authenticating(bus);
2229 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2230 bus_enter_closing(bus);
2242 r = process_running(bus, ret);
2243 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2244 bus_enter_closing(bus);
2254 return process_closing(bus, ret);
2257 assert_not_reached("Unknown state");
2260 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
2261 struct pollfd p[2] = {};
2264 usec_t m = (usec_t) -1;
2268 if (bus->state == BUS_CLOSING)
2271 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2273 e = sd_bus_get_events(bus);
2278 /* The caller really needs some more data, he doesn't
2279 * care about what's already read, or any timeouts
2284 /* The caller wants to process if there's something to
2285 * process, but doesn't care otherwise */
2287 r = sd_bus_get_timeout(bus, &until);
2292 nw = now(CLOCK_MONOTONIC);
2293 m = until > nw ? until - nw : 0;
2297 if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
2300 p[0].fd = bus->input_fd;
2301 if (bus->output_fd == bus->input_fd) {
2305 p[0].events = e & POLLIN;
2306 p[1].fd = bus->output_fd;
2307 p[1].events = e & POLLOUT;
2311 r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
2315 return r > 0 ? 1 : 0;
2318 _public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
2320 assert_return(bus, -EINVAL);
2321 assert_return(!bus_pid_changed(bus), -ECHILD);
2323 if (bus->state == BUS_CLOSING)
2326 assert_return(BUS_IS_OPEN(bus->state) , -ENOTCONN);
2328 if (bus->rqueue_size > 0)
2331 return bus_poll(bus, false, timeout_usec);
2334 _public_ int sd_bus_flush(sd_bus *bus) {
2337 assert_return(bus, -EINVAL);
2338 assert_return(!bus_pid_changed(bus), -ECHILD);
2340 if (bus->state == BUS_CLOSING)
2343 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2345 r = bus_ensure_running(bus);
2349 if (bus->wqueue_size <= 0)
2353 r = dispatch_wqueue(bus);
2355 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
2356 bus_enter_closing(bus);
2361 if (bus->wqueue_size <= 0)
2364 r = bus_poll(bus, false, (uint64_t) -1);
2370 _public_ int sd_bus_add_filter(sd_bus *bus,
2371 sd_bus_message_handler_t callback,
2374 struct filter_callback *f;
2376 assert_return(bus, -EINVAL);
2377 assert_return(callback, -EINVAL);
2378 assert_return(!bus_pid_changed(bus), -ECHILD);
2380 f = new0(struct filter_callback, 1);
2383 f->callback = callback;
2384 f->userdata = userdata;
2386 bus->filter_callbacks_modified = true;
2387 LIST_PREPEND(callbacks, bus->filter_callbacks, f);
2391 _public_ int sd_bus_remove_filter(sd_bus *bus,
2392 sd_bus_message_handler_t callback,
2395 struct filter_callback *f;
2397 assert_return(bus, -EINVAL);
2398 assert_return(callback, -EINVAL);
2399 assert_return(!bus_pid_changed(bus), -ECHILD);
2401 LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
2402 if (f->callback == callback && f->userdata == userdata) {
2403 bus->filter_callbacks_modified = true;
2404 LIST_REMOVE(callbacks, bus->filter_callbacks, f);
2413 _public_ int sd_bus_add_match(sd_bus *bus,
2415 sd_bus_message_handler_t callback,
2418 struct bus_match_component *components = NULL;
2419 unsigned n_components = 0;
2420 uint64_t cookie = 0;
2423 assert_return(bus, -EINVAL);
2424 assert_return(match, -EINVAL);
2425 assert_return(!bus_pid_changed(bus), -ECHILD);
2427 r = bus_match_parse(match, &components, &n_components);
2431 if (bus->bus_client) {
2432 cookie = ++bus->match_cookie;
2434 r = bus_add_match_internal(bus, match, components, n_components, cookie);
2439 bus->match_callbacks_modified = true;
2440 r = bus_match_add(&bus->match_callbacks, components, n_components, callback, userdata, cookie, NULL);
2442 if (bus->bus_client)
2443 bus_remove_match_internal(bus, match, cookie);
2447 bus_match_parse_free(components, n_components);
2451 _public_ int sd_bus_remove_match(sd_bus *bus,
2453 sd_bus_message_handler_t callback,
2456 struct bus_match_component *components = NULL;
2457 unsigned n_components = 0;
2459 uint64_t cookie = 0;
2461 assert_return(bus, -EINVAL);
2462 assert_return(match, -EINVAL);
2463 assert_return(!bus_pid_changed(bus), -ECHILD);
2465 r = bus_match_parse(match, &components, &n_components);
2469 bus->match_callbacks_modified = true;
2470 r = bus_match_remove(&bus->match_callbacks, components, n_components, callback, userdata, &cookie);
2472 if (bus->bus_client)
2473 q = bus_remove_match_internal(bus, match, cookie);
2475 bus_match_parse_free(components, n_components);
2477 return r < 0 ? r : q;
2480 bool bus_pid_changed(sd_bus *bus) {
2483 /* We don't support people creating a bus connection and
2484 * keeping it around over a fork(). Let's complain. */
2486 return bus->original_pid != getpid();
2489 static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2490 sd_bus *bus = userdata;
2495 r = sd_bus_process(bus, NULL);
2502 static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
2503 sd_bus *bus = userdata;
2508 r = sd_bus_process(bus, NULL);
2515 static int prepare_callback(sd_event_source *s, void *userdata) {
2516 sd_bus *bus = userdata;
2523 e = sd_bus_get_events(bus);
2527 if (bus->output_fd != bus->input_fd) {
2529 r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN);
2533 r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT);
2537 r = sd_event_source_set_io_events(bus->input_io_event_source, e);
2542 r = sd_bus_get_timeout(bus, &until);
2548 j = sd_event_source_set_time(bus->time_event_source, until);
2553 r = sd_event_source_set_enabled(bus->time_event_source, r > 0);
2560 static int quit_callback(sd_event_source *event, void *userdata) {
2561 sd_bus *bus = userdata;
2570 _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
2573 assert_return(bus, -EINVAL);
2574 assert_return(!bus->event, -EBUSY);
2576 assert(!bus->input_io_event_source);
2577 assert(!bus->output_io_event_source);
2578 assert(!bus->time_event_source);
2581 bus->event = sd_event_ref(event);
2583 r = sd_event_default(&bus->event);
2588 r = sd_event_add_io(bus->event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source);
2592 r = sd_event_source_set_priority(bus->input_io_event_source, priority);
2596 if (bus->output_fd != bus->input_fd) {
2597 r = sd_event_add_io(bus->event, bus->output_fd, 0, io_callback, bus, &bus->output_io_event_source);
2601 r = sd_event_source_set_priority(bus->output_io_event_source, priority);
2606 r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
2610 r = sd_event_add_monotonic(bus->event, 0, 0, time_callback, bus, &bus->time_event_source);
2614 r = sd_event_source_set_priority(bus->time_event_source, priority);
2618 r = sd_event_add_quit(bus->event, quit_callback, bus, &bus->quit_event_source);
2625 sd_bus_detach_event(bus);
2629 _public_ int sd_bus_detach_event(sd_bus *bus) {
2630 assert_return(bus, -EINVAL);
2631 assert_return(bus->event, -ENXIO);
2633 if (bus->input_io_event_source) {
2634 sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
2635 bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
2638 if (bus->output_io_event_source) {
2639 sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
2640 bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
2643 if (bus->time_event_source) {
2644 sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF);
2645 bus->time_event_source = sd_event_source_unref(bus->time_event_source);
2648 if (bus->quit_event_source) {
2649 sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF);
2650 bus->quit_event_source = sd_event_source_unref(bus->quit_event_source);
2654 bus->event = sd_event_unref(bus->event);
2659 _public_ sd_event* sd_bus_get_event(sd_bus *bus) {
2660 assert_return(bus, NULL);
2665 _public_ sd_bus_message* sd_bus_get_current(sd_bus *bus) {
2666 assert_return(bus, NULL);
2668 return bus->current;
2671 static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
2676 assert(default_bus);
2679 return !!*default_bus;
2682 *ret = sd_bus_ref(*default_bus);
2690 b->default_bus_ptr = default_bus;
2698 _public_ int sd_bus_default_system(sd_bus **ret) {
2699 static __thread sd_bus *default_system_bus = NULL;
2701 return bus_default(sd_bus_open_system, &default_system_bus, ret);
2704 _public_ int sd_bus_default_user(sd_bus **ret) {
2705 static __thread sd_bus *default_user_bus = NULL;
2707 return bus_default(sd_bus_open_user, &default_user_bus, ret);
2710 _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
2711 assert_return(b, -EINVAL);
2712 assert_return(tid, -EINVAL);
2713 assert_return(!bus_pid_changed(b), -ECHILD);
2721 return sd_event_get_tid(b->event, tid);
2726 _public_ char *sd_bus_label_escape(const char *s) {
2730 assert_return(s, NULL);
2732 /* Escapes all chars that D-Bus' object path cannot deal
2733 * with. Can be reversed with bus_path_unescape(). We special
2734 * case the empty string. */
2739 r = new(char, strlen(s)*3 + 1);
2743 for (f = s, t = r; *f; f++) {
2745 /* Escape everything that is not a-zA-Z0-9. We also
2746 * escape 0-9 if it's the first character */
2748 if (!(*f >= 'A' && *f <= 'Z') &&
2749 !(*f >= 'a' && *f <= 'z') &&
2750 !(f > s && *f >= '0' && *f <= '9')) {
2752 *(t++) = hexchar(*f >> 4);
2753 *(t++) = hexchar(*f);
2763 _public_ char *sd_bus_label_unescape(const char *f) {
2766 assert_return(f, NULL);
2768 /* Special case for the empty string */
2772 r = new(char, strlen(f) + 1);
2776 for (t = r; *f; f++) {
2781 if ((a = unhexchar(f[1])) < 0 ||
2782 (b = unhexchar(f[2])) < 0) {
2783 /* Invalid escape code, let's take it literal then */
2786 *(t++) = (char) ((a << 4) | b);
2798 _public_ int sd_bus_get_peer_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
2803 assert_return(bus, -EINVAL);
2804 assert_return(mask <= _SD_BUS_CREDS_MAX, -ENOTSUP);
2805 assert_return(ret, -EINVAL);
2806 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2807 assert_return(!bus_pid_changed(bus), -ECHILD);
2808 assert_return(!bus->is_kernel, -ENOTSUP);
2810 if (!bus->ucred_valid && !isempty(bus->label))
2813 c = bus_creds_new();
2817 if (bus->ucred_valid) {
2818 pid = c->pid = bus->ucred.pid;
2819 c->uid = bus->ucred.uid;
2820 c->gid = bus->ucred.gid;
2822 c->mask |= (SD_BUS_CREDS_UID | SD_BUS_CREDS_PID | SD_BUS_CREDS_GID) & mask;
2825 if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) {
2826 c->label = strdup(bus->label);
2828 sd_bus_creds_unref(c);
2832 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
2835 r = bus_creds_add_more(c, mask, pid, 0);