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);
996 r = sd_bus_set_address(b, "kernel:path=/dev/kdbus/0-system/bus;unix:path=/run/dbus/system_bus_socket");
1001 b->bus_client = true;
1003 r = sd_bus_start(b);
1015 _public_ int sd_bus_open_user(sd_bus **ret) {
1020 assert_return(ret, -EINVAL);
1026 e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1028 r = sd_bus_set_address(b, e);
1032 e = secure_getenv("XDG_RUNTIME_DIR");
1034 _cleanup_free_ char *ee = NULL;
1036 ee = bus_address_escape(e);
1042 asprintf(&b->address, "kernel:path=/dev/kdbus/%lu-user/bus;unix:path=%s/bus", (unsigned long) getuid(), ee);
1044 asprintf(&b->address, "kernel:path=/dev/kdbus/%lu-user/bus", (unsigned long) getuid());
1052 b->bus_client = true;
1054 r = sd_bus_start(b);
1066 _public_ int sd_bus_open_system_remote(const char *host, sd_bus **ret) {
1067 _cleanup_free_ char *e = NULL;
1072 assert_return(host, -EINVAL);
1073 assert_return(ret, -EINVAL);
1075 e = bus_address_escape(host);
1079 p = strjoin("unixexec:path=ssh,argv1=-xT,argv2=", e, ",argv3=systemd-stdio-bridge", NULL);
1083 r = sd_bus_new(&bus);
1090 bus->bus_client = true;
1092 r = sd_bus_start(bus);
1102 _public_ int sd_bus_open_system_container(const char *machine, sd_bus **ret) {
1103 _cleanup_free_ char *e = NULL;
1108 assert_return(machine, -EINVAL);
1109 assert_return(ret, -EINVAL);
1111 e = bus_address_escape(machine);
1115 p = strjoin("x-container:machine=", e, NULL);
1119 r = sd_bus_new(&bus);
1126 bus->bus_client = true;
1128 r = sd_bus_start(bus);
1138 _public_ void sd_bus_close(sd_bus *bus) {
1142 if (bus->state == BUS_CLOSED)
1144 if (bus_pid_changed(bus))
1147 bus->state = BUS_CLOSED;
1149 sd_bus_detach_event(bus);
1151 /* Drop all queued messages so that they drop references to
1152 * the bus object and the bus may be freed */
1153 bus_reset_queues(bus);
1155 if (!bus->is_kernel)
1158 /* We'll leave the fd open in case this is a kernel bus, since
1159 * there might still be memblocks around that reference this
1160 * bus, and they might need to invoke the * KDBUS_CMD_FREE
1161 * ioctl on the fd when they are freed. */
1164 static void bus_enter_closing(sd_bus *bus) {
1167 if (bus->state != BUS_OPENING &&
1168 bus->state != BUS_AUTHENTICATING &&
1169 bus->state != BUS_HELLO &&
1170 bus->state != BUS_RUNNING)
1173 bus->state = BUS_CLOSING;
1176 _public_ sd_bus *sd_bus_ref(sd_bus *bus) {
1177 assert_return(bus, NULL);
1179 assert_se(REFCNT_INC(bus->n_ref) >= 2);
1184 _public_ sd_bus *sd_bus_unref(sd_bus *bus) {
1185 assert_return(bus, NULL);
1187 if (REFCNT_DEC(bus->n_ref) <= 0)
1193 _public_ int sd_bus_is_open(sd_bus *bus) {
1195 assert_return(bus, -EINVAL);
1196 assert_return(!bus_pid_changed(bus), -ECHILD);
1198 return BUS_IS_OPEN(bus->state);
1201 _public_ int sd_bus_can_send(sd_bus *bus, char type) {
1204 assert_return(bus, -EINVAL);
1205 assert_return(bus->state != BUS_UNSET, -ENOTCONN);
1206 assert_return(!bus_pid_changed(bus), -ECHILD);
1208 if (type == SD_BUS_TYPE_UNIX_FD) {
1209 if (!(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD))
1212 r = bus_ensure_running(bus);
1216 return bus->can_fds;
1219 return bus_type_is_valid(type);
1222 _public_ int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
1225 assert_return(bus, -EINVAL);
1226 assert_return(server_id, -EINVAL);
1227 assert_return(!bus_pid_changed(bus), -ECHILD);
1229 r = bus_ensure_running(bus);
1233 *server_id = bus->server_id;
1237 static int bus_seal_message(sd_bus *b, sd_bus_message *m) {
1241 if (m->header->version > b->message_version)
1245 /* If we copy the same message to multiple
1246 * destinations, avoid using the same serial
1248 b->serial = MAX(b->serial, BUS_MESSAGE_SERIAL(m));
1252 return bus_message_seal(m, ++b->serial);
1255 int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
1259 if (m->header->version > b->message_version)
1262 /* The bus specification says the serial number cannot be 0,
1263 * hence let's fill something in for synthetic messages. Since
1264 * synthetic messages might have a fake sender and we don't
1265 * want to interfere with the real sender's serial numbers we
1266 * pick a fixed, artifical one. We use (uint32_t) -1 rather
1267 * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
1268 * even though kdbus can do 64bit. */
1270 return bus_message_seal(m, 0xFFFFFFFFULL);
1273 static int bus_write_message(sd_bus *bus, sd_bus_message *message, size_t *idx) {
1280 return bus_kernel_write_message(bus, message);
1282 return bus_socket_write_message(bus, message, idx);
1287 static int dispatch_wqueue(sd_bus *bus) {
1291 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1293 while (bus->wqueue_size > 0) {
1295 r = bus_write_message(bus, bus->wqueue[0], &bus->windex);
1299 /* Didn't do anything this time */
1301 else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
1302 /* Fully written. Let's drop the entry from
1305 * This isn't particularly optimized, but
1306 * well, this is supposed to be our worst-case
1307 * buffer only, and the socket buffer is
1308 * supposed to be our primary buffer, and if
1309 * it got full, then all bets are off
1312 sd_bus_message_unref(bus->wqueue[0]);
1313 bus->wqueue_size --;
1314 memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
1324 static int bus_read_message(sd_bus *bus) {
1328 return bus_kernel_read_message(bus);
1330 return bus_socket_read_message(bus);
1333 int bus_rqueue_make_room(sd_bus *bus) {
1337 x = bus->rqueue_size + 1;
1339 if (bus->rqueue_allocated >= x)
1342 if (x > BUS_RQUEUE_MAX)
1345 q = realloc(bus->rqueue, x * sizeof(sd_bus_message*));
1350 bus->rqueue_allocated = x;
1355 static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
1360 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1363 if (bus->rqueue_size > 0) {
1364 /* Dispatch a queued message */
1366 *m = bus->rqueue[0];
1367 bus->rqueue_size --;
1368 memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
1372 /* Try to read a new message */
1373 r = bus_read_message(bus);
1383 _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
1386 assert_return(bus, -EINVAL);
1387 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1388 assert_return(m, -EINVAL);
1389 assert_return(!bus_pid_changed(bus), -ECHILD);
1392 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
1399 /* If the serial number isn't kept, then we know that no reply
1401 if (!serial && !m->sealed)
1402 m->header->flags |= SD_BUS_MESSAGE_NO_REPLY_EXPECTED;
1404 r = bus_seal_message(bus, m);
1408 /* If this is a reply and no reply was requested, then let's
1409 * suppress this, if we can */
1410 if (m->dont_send && !serial)
1413 if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
1416 r = bus_write_message(bus, m, &idx);
1418 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1419 bus_enter_closing(bus);
1422 } else if (!bus->is_kernel && idx < BUS_MESSAGE_SIZE(m)) {
1423 /* Wasn't fully written. So let's remember how
1424 * much was written. Note that the first entry
1425 * of the wqueue array is always allocated so
1426 * that we always can remember how much was
1428 bus->wqueue[0] = sd_bus_message_ref(m);
1429 bus->wqueue_size = 1;
1435 /* Just append it to the queue. */
1437 if (bus->wqueue_size >= BUS_WQUEUE_MAX)
1440 q = realloc(bus->wqueue, sizeof(sd_bus_message*) * (bus->wqueue_size + 1));
1445 q[bus->wqueue_size ++] = sd_bus_message_ref(m);
1449 *serial = BUS_MESSAGE_SERIAL(m);
1454 _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *serial) {
1457 assert_return(bus, -EINVAL);
1458 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1459 assert_return(m, -EINVAL);
1460 assert_return(!bus_pid_changed(bus), -ECHILD);
1462 if (!streq_ptr(m->destination, destination)) {
1467 r = sd_bus_message_set_destination(m, destination);
1472 return sd_bus_send(bus, m, serial);
1475 static usec_t calc_elapse(uint64_t usec) {
1476 if (usec == (uint64_t) -1)
1480 usec = BUS_DEFAULT_TIMEOUT;
1482 return now(CLOCK_MONOTONIC) + usec;
1485 static int timeout_compare(const void *a, const void *b) {
1486 const struct reply_callback *x = a, *y = b;
1488 if (x->timeout != 0 && y->timeout == 0)
1491 if (x->timeout == 0 && y->timeout != 0)
1494 if (x->timeout < y->timeout)
1497 if (x->timeout > y->timeout)
1503 _public_ int sd_bus_call_async(
1506 sd_bus_message_handler_t callback,
1511 struct reply_callback *c;
1514 assert_return(bus, -EINVAL);
1515 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1516 assert_return(m, -EINVAL);
1517 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1518 assert_return(!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1519 assert_return(callback, -EINVAL);
1520 assert_return(!bus_pid_changed(bus), -ECHILD);
1522 r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
1526 if (usec != (uint64_t) -1) {
1527 r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
1532 r = bus_seal_message(bus, m);
1536 c = new0(struct reply_callback, 1);
1540 c->callback = callback;
1541 c->userdata = userdata;
1542 c->serial = BUS_MESSAGE_SERIAL(m);
1543 c->timeout = calc_elapse(usec);
1545 r = hashmap_put(bus->reply_callbacks, &c->serial, c);
1551 if (c->timeout != 0) {
1552 r = prioq_put(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1555 sd_bus_call_async_cancel(bus, c->serial);
1560 r = sd_bus_send(bus, m, serial);
1562 sd_bus_call_async_cancel(bus, c->serial);
1569 _public_ int sd_bus_call_async_cancel(sd_bus *bus, uint64_t serial) {
1570 struct reply_callback *c;
1572 assert_return(bus, -EINVAL);
1573 assert_return(serial != 0, -EINVAL);
1574 assert_return(!bus_pid_changed(bus), -ECHILD);
1576 c = hashmap_remove(bus->reply_callbacks, &serial);
1580 if (c->timeout != 0)
1581 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1587 int bus_ensure_running(sd_bus *bus) {
1592 if (bus->state == BUS_UNSET || bus->state == BUS_CLOSED || bus->state == BUS_CLOSING)
1594 if (bus->state == BUS_RUNNING)
1598 r = sd_bus_process(bus, NULL);
1601 if (bus->state == BUS_RUNNING)
1606 r = sd_bus_wait(bus, (uint64_t) -1);
1612 _public_ int sd_bus_call(
1616 sd_bus_error *error,
1617 sd_bus_message **reply) {
1624 assert_return(bus, -EINVAL);
1625 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1626 assert_return(m, -EINVAL);
1627 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1628 assert_return(!(m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1629 assert_return(!bus_error_is_dirty(error), -EINVAL);
1630 assert_return(!bus_pid_changed(bus), -ECHILD);
1632 r = bus_ensure_running(bus);
1636 i = bus->rqueue_size;
1638 r = sd_bus_send(bus, m, &serial);
1642 timeout = calc_elapse(usec);
1647 while (i < bus->rqueue_size) {
1648 sd_bus_message *incoming = NULL;
1650 incoming = bus->rqueue[i];
1652 if (incoming->reply_serial == serial) {
1653 /* Found a match! */
1655 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1658 if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
1663 sd_bus_message_unref(incoming);
1666 } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
1667 r = sd_bus_error_copy(error, &incoming->error);
1671 sd_bus_message_unref(incoming);
1674 } else if (incoming->header->serial == serial &&
1677 streq(bus->unique_name, incoming->sender)) {
1679 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1682 /* Our own message? Somebody is trying
1683 * to send its own client a message,
1684 * let's not dead-lock, let's fail
1687 sd_bus_message_unref(incoming);
1691 /* Try to read more, right-away */
1695 r = bus_read_message(bus);
1697 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1698 bus_enter_closing(bus);
1708 n = now(CLOCK_MONOTONIC);
1714 left = (uint64_t) -1;
1716 r = bus_poll(bus, true, left);
1720 r = dispatch_wqueue(bus);
1722 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1723 bus_enter_closing(bus);
1730 _public_ int sd_bus_get_fd(sd_bus *bus) {
1732 assert_return(bus, -EINVAL);
1733 assert_return(bus->input_fd == bus->output_fd, -EPERM);
1734 assert_return(!bus_pid_changed(bus), -ECHILD);
1736 return bus->input_fd;
1739 _public_ int sd_bus_get_events(sd_bus *bus) {
1742 assert_return(bus, -EINVAL);
1743 assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
1744 assert_return(!bus_pid_changed(bus), -ECHILD);
1746 if (bus->state == BUS_OPENING)
1748 else if (bus->state == BUS_AUTHENTICATING) {
1750 if (bus_socket_auth_needs_write(bus))
1755 } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
1756 if (bus->rqueue_size <= 0)
1758 if (bus->wqueue_size > 0)
1765 _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
1766 struct reply_callback *c;
1768 assert_return(bus, -EINVAL);
1769 assert_return(timeout_usec, -EINVAL);
1770 assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
1771 assert_return(!bus_pid_changed(bus), -ECHILD);
1773 if (bus->state == BUS_CLOSING) {
1778 if (bus->state == BUS_AUTHENTICATING) {
1779 *timeout_usec = bus->auth_timeout;
1783 if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) {
1784 *timeout_usec = (uint64_t) -1;
1788 if (bus->rqueue_size > 0) {
1793 c = prioq_peek(bus->reply_callbacks_prioq);
1795 *timeout_usec = (uint64_t) -1;
1799 *timeout_usec = c->timeout;
1803 static int process_timeout(sd_bus *bus) {
1804 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1805 _cleanup_bus_message_unref_ sd_bus_message* m = NULL;
1806 struct reply_callback *c;
1812 c = prioq_peek(bus->reply_callbacks_prioq);
1816 n = now(CLOCK_MONOTONIC);
1820 r = bus_message_new_synthetic_error(
1823 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
1828 m->sender = "org.freedesktop.DBus";
1830 r = bus_seal_synthetic_message(bus, m);
1834 assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
1835 hashmap_remove(bus->reply_callbacks, &c->serial);
1838 bus->iteration_counter ++;
1840 r = c->callback(bus, m, c->userdata, &error_buffer);
1841 r = bus_maybe_reply_error(m, r, &error_buffer);
1844 bus->current = NULL;
1849 static int process_hello(sd_bus *bus, sd_bus_message *m) {
1853 if (bus->state != BUS_HELLO)
1856 /* Let's make sure the first message on the bus is the HELLO
1857 * reply. But note that we don't actually parse the message
1858 * here (we leave that to the usual handling), we just verify
1859 * we don't let any earlier msg through. */
1861 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
1862 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1865 if (m->reply_serial != bus->hello_serial)
1871 static int process_reply(sd_bus *bus, sd_bus_message *m) {
1872 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1873 struct reply_callback *c;
1879 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
1880 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1883 c = hashmap_remove(bus->reply_callbacks, &m->reply_serial);
1887 if (c->timeout != 0)
1888 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1890 r = sd_bus_message_rewind(m, true);
1894 r = c->callback(bus, m, c->userdata, &error_buffer);
1895 r = bus_maybe_reply_error(m, r, &error_buffer);
1901 static int process_filter(sd_bus *bus, sd_bus_message *m) {
1902 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1903 struct filter_callback *l;
1910 bus->filter_callbacks_modified = false;
1912 LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
1914 if (bus->filter_callbacks_modified)
1917 /* Don't run this more than once per iteration */
1918 if (l->last_iteration == bus->iteration_counter)
1921 l->last_iteration = bus->iteration_counter;
1923 r = sd_bus_message_rewind(m, true);
1927 r = l->callback(bus, m, l->userdata, &error_buffer);
1928 r = bus_maybe_reply_error(m, r, &error_buffer);
1934 } while (bus->filter_callbacks_modified);
1939 static int process_match(sd_bus *bus, sd_bus_message *m) {
1946 bus->match_callbacks_modified = false;
1948 r = bus_match_run(bus, &bus->match_callbacks, m);
1952 } while (bus->match_callbacks_modified);
1957 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
1958 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1964 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
1967 if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
1970 if (m->header->flags & SD_BUS_MESSAGE_NO_REPLY_EXPECTED)
1973 if (streq_ptr(m->member, "Ping"))
1974 r = sd_bus_message_new_method_return(m, &reply);
1975 else if (streq_ptr(m->member, "GetMachineId")) {
1979 r = sd_id128_get_machine(&id);
1983 r = sd_bus_message_new_method_return(m, &reply);
1987 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
1989 r = sd_bus_message_new_method_errorf(
1991 SD_BUS_ERROR_UNKNOWN_METHOD,
1992 "Unknown method '%s' on interface '%s'.", m->member, m->interface);
1998 r = sd_bus_send(bus, reply, NULL);
2005 static int process_message(sd_bus *bus, sd_bus_message *m) {
2012 bus->iteration_counter++;
2014 log_debug("Got message sender=%s object=%s interface=%s member=%s",
2015 strna(sd_bus_message_get_sender(m)),
2016 strna(sd_bus_message_get_path(m)),
2017 strna(sd_bus_message_get_interface(m)),
2018 strna(sd_bus_message_get_member(m)));
2020 r = process_hello(bus, m);
2024 r = process_reply(bus, m);
2028 r = process_filter(bus, m);
2032 r = process_match(bus, m);
2036 r = process_builtin(bus, m);
2040 r = bus_process_object(bus, m);
2043 bus->current = NULL;
2047 static int process_running(sd_bus *bus, sd_bus_message **ret) {
2048 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2052 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
2054 r = process_timeout(bus);
2058 r = dispatch_wqueue(bus);
2062 r = dispatch_rqueue(bus, &m);
2068 r = process_message(bus, m);
2073 r = sd_bus_message_rewind(m, true);
2082 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
2084 r = sd_bus_reply_method_errorf(
2086 SD_BUS_ERROR_UNKNOWN_OBJECT,
2087 "Unknown object '%s'.", m->path);
2101 static int process_closing(sd_bus *bus, sd_bus_message **ret) {
2102 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2103 struct reply_callback *c;
2107 assert(bus->state == BUS_CLOSING);
2109 c = hashmap_first(bus->reply_callbacks);
2111 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2113 /* First, fail all outstanding method calls */
2114 r = bus_message_new_synthetic_error(
2117 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
2122 r = bus_seal_synthetic_message(bus, m);
2126 if (c->timeout != 0)
2127 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2129 hashmap_remove(bus->reply_callbacks, &c->serial);
2132 bus->iteration_counter++;
2134 r = c->callback(bus, m, c->userdata, &error_buffer);
2135 r = bus_maybe_reply_error(m, r, &error_buffer);
2141 /* Then, synthesize a Disconnected message */
2142 r = sd_bus_message_new_signal(
2144 "/org/freedesktop/DBus/Local",
2145 "org.freedesktop.DBus.Local",
2151 m->sender = "org.freedesktop.DBus.Local";
2153 r = bus_seal_synthetic_message(bus, m);
2160 bus->iteration_counter++;
2162 r = process_filter(bus, m);
2166 r = process_match(bus, m);
2178 bus->current = NULL;
2182 _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
2183 BUS_DONT_DESTROY(bus);
2186 /* Returns 0 when we didn't do anything. This should cause the
2187 * caller to invoke sd_bus_wait() before returning the next
2188 * time. Returns > 0 when we did something, which possibly
2189 * means *ret is filled in with an unprocessed message. */
2191 assert_return(bus, -EINVAL);
2192 assert_return(!bus_pid_changed(bus), -ECHILD);
2194 /* We don't allow recursively invoking sd_bus_process(). */
2195 assert_return(!bus->current, -EBUSY);
2197 switch (bus->state) {
2204 r = bus_socket_process_opening(bus);
2205 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2206 bus_enter_closing(bus);
2214 case BUS_AUTHENTICATING:
2215 r = bus_socket_process_authenticating(bus);
2216 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2217 bus_enter_closing(bus);
2229 r = process_running(bus, ret);
2230 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2231 bus_enter_closing(bus);
2241 return process_closing(bus, ret);
2244 assert_not_reached("Unknown state");
2247 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
2248 struct pollfd p[2] = {};
2251 usec_t m = (usec_t) -1;
2255 if (bus->state == BUS_CLOSING)
2258 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2260 e = sd_bus_get_events(bus);
2265 /* The caller really needs some more data, he doesn't
2266 * care about what's already read, or any timeouts
2271 /* The caller wants to process if there's something to
2272 * process, but doesn't care otherwise */
2274 r = sd_bus_get_timeout(bus, &until);
2279 nw = now(CLOCK_MONOTONIC);
2280 m = until > nw ? until - nw : 0;
2284 if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
2287 p[0].fd = bus->input_fd;
2288 if (bus->output_fd == bus->input_fd) {
2292 p[0].events = e & POLLIN;
2293 p[1].fd = bus->output_fd;
2294 p[1].events = e & POLLOUT;
2298 r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
2302 return r > 0 ? 1 : 0;
2305 _public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
2307 assert_return(bus, -EINVAL);
2308 assert_return(!bus_pid_changed(bus), -ECHILD);
2310 if (bus->state == BUS_CLOSING)
2313 assert_return(BUS_IS_OPEN(bus->state) , -ENOTCONN);
2315 if (bus->rqueue_size > 0)
2318 return bus_poll(bus, false, timeout_usec);
2321 _public_ int sd_bus_flush(sd_bus *bus) {
2324 assert_return(bus, -EINVAL);
2325 assert_return(!bus_pid_changed(bus), -ECHILD);
2327 if (bus->state == BUS_CLOSING)
2330 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2332 r = bus_ensure_running(bus);
2336 if (bus->wqueue_size <= 0)
2340 r = dispatch_wqueue(bus);
2342 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
2343 bus_enter_closing(bus);
2348 if (bus->wqueue_size <= 0)
2351 r = bus_poll(bus, false, (uint64_t) -1);
2357 _public_ int sd_bus_add_filter(sd_bus *bus,
2358 sd_bus_message_handler_t callback,
2361 struct filter_callback *f;
2363 assert_return(bus, -EINVAL);
2364 assert_return(callback, -EINVAL);
2365 assert_return(!bus_pid_changed(bus), -ECHILD);
2367 f = new0(struct filter_callback, 1);
2370 f->callback = callback;
2371 f->userdata = userdata;
2373 bus->filter_callbacks_modified = true;
2374 LIST_PREPEND(callbacks, bus->filter_callbacks, f);
2378 _public_ int sd_bus_remove_filter(sd_bus *bus,
2379 sd_bus_message_handler_t callback,
2382 struct filter_callback *f;
2384 assert_return(bus, -EINVAL);
2385 assert_return(callback, -EINVAL);
2386 assert_return(!bus_pid_changed(bus), -ECHILD);
2388 LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
2389 if (f->callback == callback && f->userdata == userdata) {
2390 bus->filter_callbacks_modified = true;
2391 LIST_REMOVE(callbacks, bus->filter_callbacks, f);
2400 _public_ int sd_bus_add_match(sd_bus *bus,
2402 sd_bus_message_handler_t callback,
2405 struct bus_match_component *components = NULL;
2406 unsigned n_components = 0;
2407 uint64_t cookie = 0;
2410 assert_return(bus, -EINVAL);
2411 assert_return(match, -EINVAL);
2412 assert_return(!bus_pid_changed(bus), -ECHILD);
2414 r = bus_match_parse(match, &components, &n_components);
2418 if (bus->bus_client) {
2419 cookie = ++bus->match_cookie;
2421 r = bus_add_match_internal(bus, match, components, n_components, cookie);
2426 bus->match_callbacks_modified = true;
2427 r = bus_match_add(&bus->match_callbacks, components, n_components, callback, userdata, cookie, NULL);
2429 if (bus->bus_client)
2430 bus_remove_match_internal(bus, match, cookie);
2434 bus_match_parse_free(components, n_components);
2438 _public_ int sd_bus_remove_match(sd_bus *bus,
2440 sd_bus_message_handler_t callback,
2443 struct bus_match_component *components = NULL;
2444 unsigned n_components = 0;
2446 uint64_t cookie = 0;
2448 assert_return(bus, -EINVAL);
2449 assert_return(match, -EINVAL);
2450 assert_return(!bus_pid_changed(bus), -ECHILD);
2452 r = bus_match_parse(match, &components, &n_components);
2456 bus->match_callbacks_modified = true;
2457 r = bus_match_remove(&bus->match_callbacks, components, n_components, callback, userdata, &cookie);
2459 if (bus->bus_client)
2460 q = bus_remove_match_internal(bus, match, cookie);
2462 bus_match_parse_free(components, n_components);
2464 return r < 0 ? r : q;
2467 bool bus_pid_changed(sd_bus *bus) {
2470 /* We don't support people creating a bus connection and
2471 * keeping it around over a fork(). Let's complain. */
2473 return bus->original_pid != getpid();
2476 static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2477 sd_bus *bus = userdata;
2482 r = sd_bus_process(bus, NULL);
2489 static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
2490 sd_bus *bus = userdata;
2495 r = sd_bus_process(bus, NULL);
2502 static int prepare_callback(sd_event_source *s, void *userdata) {
2503 sd_bus *bus = userdata;
2510 e = sd_bus_get_events(bus);
2514 if (bus->output_fd != bus->input_fd) {
2516 r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN);
2520 r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT);
2524 r = sd_event_source_set_io_events(bus->input_io_event_source, e);
2529 r = sd_bus_get_timeout(bus, &until);
2535 j = sd_event_source_set_time(bus->time_event_source, until);
2540 r = sd_event_source_set_enabled(bus->time_event_source, r > 0);
2547 static int quit_callback(sd_event_source *event, void *userdata) {
2548 sd_bus *bus = userdata;
2557 _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
2560 assert_return(bus, -EINVAL);
2561 assert_return(!bus->event, -EBUSY);
2563 assert(!bus->input_io_event_source);
2564 assert(!bus->output_io_event_source);
2565 assert(!bus->time_event_source);
2568 bus->event = sd_event_ref(event);
2570 r = sd_event_default(&bus->event);
2575 r = sd_event_add_io(bus->event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source);
2579 r = sd_event_source_set_priority(bus->input_io_event_source, priority);
2583 if (bus->output_fd != bus->input_fd) {
2584 r = sd_event_add_io(bus->event, bus->output_fd, 0, io_callback, bus, &bus->output_io_event_source);
2588 r = sd_event_source_set_priority(bus->output_io_event_source, priority);
2593 r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
2597 r = sd_event_add_monotonic(bus->event, 0, 0, time_callback, bus, &bus->time_event_source);
2601 r = sd_event_source_set_priority(bus->time_event_source, priority);
2605 r = sd_event_add_quit(bus->event, quit_callback, bus, &bus->quit_event_source);
2612 sd_bus_detach_event(bus);
2616 _public_ int sd_bus_detach_event(sd_bus *bus) {
2617 assert_return(bus, -EINVAL);
2618 assert_return(bus->event, -ENXIO);
2620 if (bus->input_io_event_source) {
2621 sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
2622 bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
2625 if (bus->output_io_event_source) {
2626 sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
2627 bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
2630 if (bus->time_event_source) {
2631 sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF);
2632 bus->time_event_source = sd_event_source_unref(bus->time_event_source);
2635 if (bus->quit_event_source) {
2636 sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF);
2637 bus->quit_event_source = sd_event_source_unref(bus->quit_event_source);
2641 bus->event = sd_event_unref(bus->event);
2646 _public_ sd_event* sd_bus_get_event(sd_bus *bus) {
2647 assert_return(bus, NULL);
2652 _public_ sd_bus_message* sd_bus_get_current(sd_bus *bus) {
2653 assert_return(bus, NULL);
2655 return bus->current;
2658 static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
2663 assert(default_bus);
2666 return !!*default_bus;
2669 *ret = sd_bus_ref(*default_bus);
2677 b->default_bus_ptr = default_bus;
2685 _public_ int sd_bus_default_system(sd_bus **ret) {
2686 static __thread sd_bus *default_system_bus = NULL;
2688 return bus_default(sd_bus_open_system, &default_system_bus, ret);
2691 _public_ int sd_bus_default_user(sd_bus **ret) {
2692 static __thread sd_bus *default_user_bus = NULL;
2694 return bus_default(sd_bus_open_user, &default_user_bus, ret);
2697 _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
2698 assert_return(b, -EINVAL);
2699 assert_return(tid, -EINVAL);
2700 assert_return(!bus_pid_changed(b), -ECHILD);
2708 return sd_event_get_tid(b->event, tid);
2713 _public_ char *sd_bus_label_escape(const char *s) {
2717 assert_return(s, NULL);
2719 /* Escapes all chars that D-Bus' object path cannot deal
2720 * with. Can be reversed with bus_path_unescape(). We special
2721 * case the empty string. */
2726 r = new(char, strlen(s)*3 + 1);
2730 for (f = s, t = r; *f; f++) {
2732 /* Escape everything that is not a-zA-Z0-9. We also
2733 * escape 0-9 if it's the first character */
2735 if (!(*f >= 'A' && *f <= 'Z') &&
2736 !(*f >= 'a' && *f <= 'z') &&
2737 !(f > s && *f >= '0' && *f <= '9')) {
2739 *(t++) = hexchar(*f >> 4);
2740 *(t++) = hexchar(*f);
2750 _public_ char *sd_bus_label_unescape(const char *f) {
2753 assert_return(f, NULL);
2755 /* Special case for the empty string */
2759 r = new(char, strlen(f) + 1);
2763 for (t = r; *f; f++) {
2768 if ((a = unhexchar(f[1])) < 0 ||
2769 (b = unhexchar(f[2])) < 0) {
2770 /* Invalid escape code, let's take it literal then */
2773 *(t++) = (char) ((a << 4) | b);
2785 _public_ int sd_bus_get_peer_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
2790 assert_return(bus, -EINVAL);
2791 assert_return(mask <= _SD_BUS_CREDS_MAX, -ENOTSUP);
2792 assert_return(ret, -EINVAL);
2793 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2794 assert_return(!bus_pid_changed(bus), -ECHILD);
2795 assert_return(!bus->is_kernel, -ENOTSUP);
2797 if (!bus->ucred_valid && !isempty(bus->label))
2800 c = bus_creds_new();
2804 if (bus->ucred_valid) {
2805 pid = c->pid = bus->ucred.pid;
2806 c->uid = bus->ucred.uid;
2807 c->gid = bus->ucred.gid;
2809 c->mask |= (SD_BUS_CREDS_UID | SD_BUS_CREDS_PID | SD_BUS_CREDS_GID) & mask;
2812 if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) {
2813 c->label = strdup(bus->label);
2815 sd_bus_creds_unref(c);
2819 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
2822 r = bus_creds_add_more(c, mask, pid, 0);