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"
50 #include "bus-protocol.h"
52 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
53 static int attach_io_events(sd_bus *b);
54 static void detach_io_events(sd_bus *b);
56 static void bus_close_fds(sd_bus *b) {
62 close_nointr_nofail(b->input_fd);
64 if (b->output_fd >= 0 && b->output_fd != b->input_fd)
65 close_nointr_nofail(b->output_fd);
67 b->input_fd = b->output_fd = -1;
70 static void bus_node_destroy(sd_bus *b, struct node *n) {
71 struct node_callback *c;
72 struct node_vtable *v;
73 struct node_enumerator *e;
81 bus_node_destroy(b, n->child);
83 while ((c = n->callbacks)) {
84 LIST_REMOVE(callbacks, n->callbacks, c);
88 while ((v = n->vtables)) {
89 LIST_REMOVE(vtables, n->vtables, v);
94 while ((e = n->enumerators)) {
95 LIST_REMOVE(enumerators, n->enumerators, e);
100 LIST_REMOVE(siblings, n->parent->child, n);
102 assert_se(hashmap_remove(b->nodes, n->path) == n);
107 static void bus_reset_queues(sd_bus *b) {
112 for (i = 0; i < b->rqueue_size; i++)
113 sd_bus_message_unref(b->rqueue[i]);
116 for (i = 0; i < b->wqueue_size; i++)
117 sd_bus_message_unref(b->wqueue[i]);
120 b->rqueue = b->wqueue = NULL;
121 b->rqueue_allocated = b->wqueue_allocated = 0;
122 b->rqueue_size = b->wqueue_size = 0;
125 static void bus_free(sd_bus *b) {
126 struct filter_callback *f;
131 sd_bus_detach_event(b);
136 munmap(b->kdbus_buffer, KDBUS_POOL_SIZE);
139 free(b->unique_name);
140 free(b->auth_buffer);
146 strv_free(b->exec_argv);
148 close_many(b->fds, b->n_fds);
153 hashmap_free_free(b->reply_callbacks);
154 prioq_free(b->reply_callbacks_prioq);
156 while ((f = b->filter_callbacks)) {
157 LIST_REMOVE(callbacks, b->filter_callbacks, f);
161 bus_match_free(&b->match_callbacks);
163 hashmap_free_free(b->vtable_methods);
164 hashmap_free_free(b->vtable_properties);
166 while ((n = hashmap_first(b->nodes)))
167 bus_node_destroy(b, n);
169 hashmap_free(b->nodes);
171 bus_kernel_flush_memfd(b);
173 assert_se(pthread_mutex_destroy(&b->memfd_cache_mutex) == 0);
178 _public_ int sd_bus_new(sd_bus **ret) {
181 assert_return(ret, -EINVAL);
187 r->n_ref = REFCNT_INIT;
188 r->input_fd = r->output_fd = -1;
189 r->message_version = 1;
190 r->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
191 r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
192 r->attach_flags |= KDBUS_ATTACH_NAMES;
193 r->original_pid = getpid();
195 assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0);
197 /* We guarantee that wqueue always has space for at least one
199 if (!GREEDY_REALLOC(r->wqueue, r->wqueue_allocated, 1)) {
208 _public_ int sd_bus_set_address(sd_bus *bus, const char *address) {
211 assert_return(bus, -EINVAL);
212 assert_return(bus->state == BUS_UNSET, -EPERM);
213 assert_return(address, -EINVAL);
214 assert_return(!bus_pid_changed(bus), -ECHILD);
226 _public_ int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
227 assert_return(bus, -EINVAL);
228 assert_return(bus->state == BUS_UNSET, -EPERM);
229 assert_return(input_fd >= 0, -EINVAL);
230 assert_return(output_fd >= 0, -EINVAL);
231 assert_return(!bus_pid_changed(bus), -ECHILD);
233 bus->input_fd = input_fd;
234 bus->output_fd = output_fd;
238 _public_ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
241 assert_return(bus, -EINVAL);
242 assert_return(bus->state == BUS_UNSET, -EPERM);
243 assert_return(path, -EINVAL);
244 assert_return(!strv_isempty(argv), -EINVAL);
245 assert_return(!bus_pid_changed(bus), -ECHILD);
257 free(bus->exec_path);
258 strv_free(bus->exec_argv);
266 _public_ int sd_bus_set_bus_client(sd_bus *bus, int b) {
267 assert_return(bus, -EINVAL);
268 assert_return(bus->state == BUS_UNSET, -EPERM);
269 assert_return(!bus_pid_changed(bus), -ECHILD);
271 bus->bus_client = !!b;
275 _public_ int sd_bus_negotiate_fds(sd_bus *bus, int b) {
276 assert_return(bus, -EINVAL);
277 assert_return(bus->state == BUS_UNSET, -EPERM);
278 assert_return(!bus_pid_changed(bus), -ECHILD);
280 SET_FLAG(bus->hello_flags, KDBUS_HELLO_ACCEPT_FD, b);
284 _public_ int sd_bus_negotiate_attach_timestamp(sd_bus *bus, int b) {
285 assert_return(bus, -EINVAL);
286 assert_return(bus->state == BUS_UNSET, -EPERM);
287 assert_return(!bus_pid_changed(bus), -ECHILD);
289 SET_FLAG(bus->attach_flags, KDBUS_ATTACH_TIMESTAMP, b);
293 _public_ int sd_bus_negotiate_attach_creds(sd_bus *bus, uint64_t mask) {
294 assert_return(bus, -EINVAL);
295 assert_return(mask <= _SD_BUS_CREDS_ALL, -EINVAL);
296 assert_return(bus->state == BUS_UNSET, -EPERM);
297 assert_return(!bus_pid_changed(bus), -ECHILD);
299 /* The well knowns we need unconditionally, so that matches can work */
300 bus->creds_mask = mask | SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
302 return kdbus_translate_attach_flags(bus->creds_mask, &bus->creds_mask);
305 _public_ int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
306 assert_return(bus, -EINVAL);
307 assert_return(b || sd_id128_equal(server_id, SD_ID128_NULL), -EINVAL);
308 assert_return(bus->state == BUS_UNSET, -EPERM);
309 assert_return(!bus_pid_changed(bus), -ECHILD);
311 bus->is_server = !!b;
312 bus->server_id = server_id;
316 _public_ int sd_bus_set_anonymous(sd_bus *bus, int b) {
317 assert_return(bus, -EINVAL);
318 assert_return(bus->state == BUS_UNSET, -EPERM);
319 assert_return(!bus_pid_changed(bus), -ECHILD);
321 bus->anonymous_auth = !!b;
325 _public_ int sd_bus_set_trusted(sd_bus *bus, int b) {
326 assert_return(bus, -EINVAL);
327 assert_return(bus->state == BUS_UNSET, -EPERM);
328 assert_return(!bus_pid_changed(bus), -ECHILD);
334 static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
339 assert(bus->state == BUS_HELLO || bus->state == BUS_CLOSING);
342 r = sd_bus_message_get_errno(reply);
348 r = sd_bus_message_read(reply, "s", &s);
352 if (!service_name_is_valid(s) || s[0] != ':')
355 bus->unique_name = strdup(s);
356 if (!bus->unique_name)
359 if (bus->state == BUS_HELLO)
360 bus->state = BUS_RUNNING;
365 static int bus_send_hello(sd_bus *bus) {
366 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
371 if (!bus->bus_client || bus->is_kernel)
374 r = sd_bus_message_new_method_call(
376 "org.freedesktop.DBus",
378 "org.freedesktop.DBus",
384 return sd_bus_call_async(bus, m, hello_callback, NULL, 0, &bus->hello_serial);
387 int bus_start_running(sd_bus *bus) {
390 if (bus->bus_client && !bus->is_kernel) {
391 bus->state = BUS_HELLO;
395 bus->state = BUS_RUNNING;
399 static int parse_address_key(const char **p, const char *key, char **value) {
400 size_t l, n = 0, allocated = 0;
410 if (strncmp(*p, key, l) != 0)
423 while (*a != ';' && *a != ',' && *a != 0) {
441 c = (char) ((x << 4) | y);
448 if (!GREEDY_REALLOC(r, allocated, n + 2))
472 static void skip_address_key(const char **p) {
476 *p += strcspn(*p, ",");
482 static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
483 _cleanup_free_ char *path = NULL, *abstract = NULL;
492 while (**p != 0 && **p != ';') {
493 r = parse_address_key(p, "guid", guid);
499 r = parse_address_key(p, "path", &path);
505 r = parse_address_key(p, "abstract", &abstract);
514 if (!path && !abstract)
517 if (path && abstract)
522 if (l > sizeof(b->sockaddr.un.sun_path))
525 b->sockaddr.un.sun_family = AF_UNIX;
526 strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path));
527 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l;
528 } else if (abstract) {
529 l = strlen(abstract);
530 if (l > sizeof(b->sockaddr.un.sun_path) - 1)
533 b->sockaddr.un.sun_family = AF_UNIX;
534 b->sockaddr.un.sun_path[0] = 0;
535 strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1);
536 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
542 static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
543 _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
545 struct addrinfo *result, hints = {
546 .ai_socktype = SOCK_STREAM,
547 .ai_flags = AI_ADDRCONFIG,
555 while (**p != 0 && **p != ';') {
556 r = parse_address_key(p, "guid", guid);
562 r = parse_address_key(p, "host", &host);
568 r = parse_address_key(p, "port", &port);
574 r = parse_address_key(p, "family", &family);
587 if (streq(family, "ipv4"))
588 hints.ai_family = AF_INET;
589 else if (streq(family, "ipv6"))
590 hints.ai_family = AF_INET6;
595 r = getaddrinfo(host, port, &hints, &result);
599 return -EADDRNOTAVAIL;
601 memcpy(&b->sockaddr, result->ai_addr, result->ai_addrlen);
602 b->sockaddr_size = result->ai_addrlen;
604 freeaddrinfo(result);
609 static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
611 unsigned n_argv = 0, j;
613 size_t allocated = 0;
621 while (**p != 0 && **p != ';') {
622 r = parse_address_key(p, "guid", guid);
628 r = parse_address_key(p, "path", &path);
634 if (startswith(*p, "argv")) {
638 ul = strtoul(*p + 4, (char**) p, 10);
639 if (errno > 0 || **p != '=' || ul > 256) {
647 if (!GREEDY_REALLOC0(argv, allocated, ul + 2)) {
655 r = parse_address_key(p, NULL, argv + ul);
670 /* Make sure there are no holes in the array, with the
671 * exception of argv[0] */
672 for (j = 1; j < n_argv; j++)
678 if (argv && argv[0] == NULL) {
679 argv[0] = strdup(path);
691 for (j = 0; j < n_argv; j++)
699 static int parse_kernel_address(sd_bus *b, const char **p, char **guid) {
700 _cleanup_free_ char *path = NULL;
708 while (**p != 0 && **p != ';') {
709 r = parse_address_key(p, "guid", guid);
715 r = parse_address_key(p, "path", &path);
734 static int parse_container_unix_address(sd_bus *b, const char **p, char **guid) {
735 _cleanup_free_ char *machine = NULL;
743 while (**p != 0 && **p != ';') {
744 r = parse_address_key(p, "guid", guid);
750 r = parse_address_key(p, "machine", &machine);
762 if (!filename_is_safe(machine))
766 b->machine = machine;
769 b->sockaddr.un.sun_family = AF_UNIX;
770 strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
771 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/var/run/dbus/system_bus_socket") - 1;
776 static int parse_container_kernel_address(sd_bus *b, const char **p, char **guid) {
777 _cleanup_free_ char *machine = NULL;
785 while (**p != 0 && **p != ';') {
786 r = parse_address_key(p, "guid", guid);
792 r = parse_address_key(p, "machine", &machine);
804 if (!filename_is_safe(machine))
808 b->machine = machine;
812 b->kernel = strdup("/dev/kdbus/0-system/bus");
819 static void bus_reset_parsed_address(sd_bus *b) {
823 b->sockaddr_size = 0;
824 strv_free(b->exec_argv);
828 b->server_id = SD_ID128_NULL;
835 static int bus_parse_next_address(sd_bus *b) {
836 _cleanup_free_ char *guid = NULL;
844 if (b->address[b->address_index] == 0)
847 bus_reset_parsed_address(b);
849 a = b->address + b->address_index;
858 if (startswith(a, "unix:")) {
861 r = parse_unix_address(b, &a, &guid);
866 } else if (startswith(a, "tcp:")) {
869 r = parse_tcp_address(b, &a, &guid);
875 } else if (startswith(a, "unixexec:")) {
878 r = parse_exec_address(b, &a, &guid);
884 } else if (startswith(a, "kernel:")) {
887 r = parse_kernel_address(b, &a, &guid);
892 } else if (startswith(a, "x-container-unix:")) {
895 r = parse_container_unix_address(b, &a, &guid);
900 } else if (startswith(a, "x-container-kernel:")) {
903 r = parse_container_kernel_address(b, &a, &guid);
916 r = sd_id128_from_string(guid, &b->server_id);
921 b->address_index = a - b->address;
925 static int bus_start_address(sd_bus *b) {
931 bool skipped = false;
936 r = bus_socket_exec(b);
937 else if (b->machine && b->kernel)
938 r = bus_container_connect_kernel(b);
939 else if (b->machine && b->sockaddr.sa.sa_family != AF_UNSPEC)
940 r = bus_container_connect_socket(b);
942 r = bus_kernel_connect(b);
943 else if (b->sockaddr.sa.sa_family != AF_UNSPEC)
944 r = bus_socket_connect(b);
950 r = attach_io_events(b);
955 b->last_connect_error = -r;
958 r = bus_parse_next_address(b);
962 return b->last_connect_error ? -b->last_connect_error : -ECONNREFUSED;
966 int bus_next_address(sd_bus *b) {
969 bus_reset_parsed_address(b);
970 return bus_start_address(b);
973 static int bus_start_fd(sd_bus *b) {
978 assert(b->input_fd >= 0);
979 assert(b->output_fd >= 0);
981 r = fd_nonblock(b->input_fd, true);
985 r = fd_cloexec(b->input_fd, true);
989 if (b->input_fd != b->output_fd) {
990 r = fd_nonblock(b->output_fd, true);
994 r = fd_cloexec(b->output_fd, true);
999 if (fstat(b->input_fd, &st) < 0)
1002 if (S_ISCHR(b->input_fd))
1003 return bus_kernel_take_fd(b);
1005 return bus_socket_take_fd(b);
1008 _public_ int sd_bus_start(sd_bus *bus) {
1011 assert_return(bus, -EINVAL);
1012 assert_return(bus->state == BUS_UNSET, -EPERM);
1013 assert_return(!bus_pid_changed(bus), -ECHILD);
1015 bus->state = BUS_OPENING;
1017 if (bus->is_server && bus->bus_client)
1020 if (bus->input_fd >= 0)
1021 r = bus_start_fd(bus);
1022 else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel || bus->machine)
1023 r = bus_start_address(bus);
1030 return bus_send_hello(bus);
1033 _public_ int sd_bus_open_system(sd_bus **ret) {
1038 assert_return(ret, -EINVAL);
1044 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1046 r = sd_bus_set_address(b, e);
1049 r = sd_bus_set_address(b, "kernel:path=/dev/kdbus/0-system/bus;unix:path=/run/dbus/system_bus_socket");
1051 r = sd_bus_set_address(b, "unix:path=/run/dbus/system_bus_socket");
1057 b->bus_client = true;
1059 /* Let's do per-method access control on the system bus. We
1060 * need the caller's UID and capability set for that. */
1062 b->attach_flags |= KDBUS_ATTACH_CAPS | KDBUS_ATTACH_CREDS;
1064 r = sd_bus_start(b);
1076 _public_ int sd_bus_open_user(sd_bus **ret) {
1081 assert_return(ret, -EINVAL);
1087 e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1089 r = sd_bus_set_address(b, e);
1093 e = secure_getenv("XDG_RUNTIME_DIR");
1095 _cleanup_free_ char *ee = NULL;
1097 ee = bus_address_escape(e);
1104 asprintf(&b->address, "kernel:path=/dev/kdbus/%lu-user/bus;unix:path=%s/bus", (unsigned long) getuid(), ee);
1106 b->address = strjoin("unix:path=", ee, "/bus", NULL);
1110 asprintf(&b->address, "kernel:path=/dev/kdbus/%lu-user/bus", (unsigned long) getuid());
1112 return -ECONNREFUSED;
1122 b->bus_client = true;
1124 /* We don't do any per-method access control on the user
1128 r = sd_bus_start(b);
1140 _public_ int sd_bus_open_system_remote(const char *host, sd_bus **ret) {
1141 _cleanup_free_ char *e = NULL;
1146 assert_return(host, -EINVAL);
1147 assert_return(ret, -EINVAL);
1149 e = bus_address_escape(host);
1153 p = strjoin("unixexec:path=ssh,argv1=-xT,argv2=", e, ",argv3=systemd-stdio-bridge", NULL);
1157 r = sd_bus_new(&bus);
1164 bus->bus_client = true;
1166 r = sd_bus_start(bus);
1176 _public_ int sd_bus_open_system_container(const char *machine, sd_bus **ret) {
1177 _cleanup_free_ char *e = NULL;
1182 assert_return(machine, -EINVAL);
1183 assert_return(ret, -EINVAL);
1184 assert_return(filename_is_safe(machine), -EINVAL);
1186 e = bus_address_escape(machine);
1191 p = strjoin("x-container-kernel:machine=", e, ";x-container-unix:machine=", e, NULL);
1193 p = strjoin("x-container-unix:machine=", e, NULL);
1198 r = sd_bus_new(&bus);
1205 bus->bus_client = true;
1207 r = sd_bus_start(bus);
1217 _public_ void sd_bus_close(sd_bus *bus) {
1221 if (bus->state == BUS_CLOSED)
1223 if (bus_pid_changed(bus))
1226 bus->state = BUS_CLOSED;
1228 sd_bus_detach_event(bus);
1230 /* Drop all queued messages so that they drop references to
1231 * the bus object and the bus may be freed */
1232 bus_reset_queues(bus);
1234 if (!bus->is_kernel)
1237 /* We'll leave the fd open in case this is a kernel bus, since
1238 * there might still be memblocks around that reference this
1239 * bus, and they might need to invoke the * KDBUS_CMD_FREE
1240 * ioctl on the fd when they are freed. */
1243 static void bus_enter_closing(sd_bus *bus) {
1246 if (bus->state != BUS_OPENING &&
1247 bus->state != BUS_AUTHENTICATING &&
1248 bus->state != BUS_HELLO &&
1249 bus->state != BUS_RUNNING)
1252 bus->state = BUS_CLOSING;
1255 _public_ sd_bus *sd_bus_ref(sd_bus *bus) {
1256 assert_return(bus, NULL);
1258 assert_se(REFCNT_INC(bus->n_ref) >= 2);
1263 _public_ sd_bus *sd_bus_unref(sd_bus *bus) {
1268 if (REFCNT_DEC(bus->n_ref) <= 0)
1274 _public_ int sd_bus_is_open(sd_bus *bus) {
1276 assert_return(bus, -EINVAL);
1277 assert_return(!bus_pid_changed(bus), -ECHILD);
1279 return BUS_IS_OPEN(bus->state);
1282 _public_ int sd_bus_can_send(sd_bus *bus, char type) {
1285 assert_return(bus, -EINVAL);
1286 assert_return(bus->state != BUS_UNSET, -ENOTCONN);
1287 assert_return(!bus_pid_changed(bus), -ECHILD);
1289 if (type == SD_BUS_TYPE_UNIX_FD) {
1290 if (!(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD))
1293 r = bus_ensure_running(bus);
1297 return bus->can_fds;
1300 return bus_type_is_valid(type);
1303 _public_ int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
1306 assert_return(bus, -EINVAL);
1307 assert_return(server_id, -EINVAL);
1308 assert_return(!bus_pid_changed(bus), -ECHILD);
1310 r = bus_ensure_running(bus);
1314 *server_id = bus->server_id;
1318 static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
1322 if (b->message_version != 0 &&
1323 m->header->version != b->message_version)
1326 if (b->message_endian != 0 &&
1327 m->header->endian != b->message_endian)
1331 /* If we copy the same message to multiple
1332 * destinations, avoid using the same serial
1334 b->serial = MAX(b->serial, BUS_MESSAGE_SERIAL(m));
1339 timeout = BUS_DEFAULT_TIMEOUT;
1341 return bus_message_seal(m, ++b->serial, timeout);
1344 int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
1348 /* The bus specification says the serial number cannot be 0,
1349 * hence let's fill something in for synthetic messages. Since
1350 * synthetic messages might have a fake sender and we don't
1351 * want to interfere with the real sender's serial numbers we
1352 * pick a fixed, artifical one. We use (uint32_t) -1 rather
1353 * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
1354 * even though kdbus can do 64bit. */
1356 return bus_message_seal(m, 0xFFFFFFFFULL, 0);
1359 static int bus_write_message(sd_bus *bus, sd_bus_message *message, size_t *idx) {
1364 return bus_kernel_write_message(bus, message);
1366 return bus_socket_write_message(bus, message, idx);
1369 static int dispatch_wqueue(sd_bus *bus) {
1373 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1375 while (bus->wqueue_size > 0) {
1377 r = bus_write_message(bus, bus->wqueue[0], &bus->windex);
1381 /* Didn't do anything this time */
1383 else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
1384 /* Fully written. Let's drop the entry from
1387 * This isn't particularly optimized, but
1388 * well, this is supposed to be our worst-case
1389 * buffer only, and the socket buffer is
1390 * supposed to be our primary buffer, and if
1391 * it got full, then all bets are off
1394 sd_bus_message_unref(bus->wqueue[0]);
1395 bus->wqueue_size --;
1396 memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
1406 static int bus_read_message(sd_bus *bus) {
1410 return bus_kernel_read_message(bus);
1412 return bus_socket_read_message(bus);
1415 int bus_rqueue_make_room(sd_bus *bus) {
1418 if (bus->rqueue_size >= BUS_RQUEUE_MAX)
1421 if (!GREEDY_REALLOC(bus->rqueue, bus->rqueue_allocated, bus->rqueue_size + 1))
1427 static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
1432 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1435 if (bus->rqueue_size > 0) {
1436 /* Dispatch a queued message */
1438 *m = bus->rqueue[0];
1439 bus->rqueue_size --;
1440 memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
1444 /* Try to read a new message */
1445 r = bus_read_message(bus);
1455 _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
1458 assert_return(bus, -EINVAL);
1459 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1460 assert_return(m, -EINVAL);
1461 assert_return(!bus_pid_changed(bus), -ECHILD);
1464 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
1471 /* If the serial number isn't kept, then we know that no reply
1473 if (!serial && !m->sealed)
1474 m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
1476 r = bus_seal_message(bus, m, 0);
1480 /* If this is a reply and no reply was requested, then let's
1481 * suppress this, if we can */
1482 if (m->dont_send && !serial)
1485 if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
1488 r = bus_write_message(bus, m, &idx);
1490 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1491 bus_enter_closing(bus);
1494 } else if (!bus->is_kernel && idx < BUS_MESSAGE_SIZE(m)) {
1495 /* Wasn't fully written. So let's remember how
1496 * much was written. Note that the first entry
1497 * of the wqueue array is always allocated so
1498 * that we always can remember how much was
1500 bus->wqueue[0] = sd_bus_message_ref(m);
1501 bus->wqueue_size = 1;
1505 /* Just append it to the queue. */
1507 if (bus->wqueue_size >= BUS_WQUEUE_MAX)
1510 if (!GREEDY_REALLOC(bus->wqueue, bus->wqueue_allocated, bus->wqueue_size + 1))
1513 bus->wqueue[bus->wqueue_size ++] = sd_bus_message_ref(m);
1517 *serial = BUS_MESSAGE_SERIAL(m);
1522 _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *serial) {
1525 assert_return(bus, -EINVAL);
1526 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1527 assert_return(m, -EINVAL);
1528 assert_return(!bus_pid_changed(bus), -ECHILD);
1530 if (!streq_ptr(m->destination, destination)) {
1535 r = sd_bus_message_set_destination(m, destination);
1540 return sd_bus_send(bus, m, serial);
1543 static usec_t calc_elapse(uint64_t usec) {
1544 if (usec == (uint64_t) -1)
1547 return now(CLOCK_MONOTONIC) + usec;
1550 static int timeout_compare(const void *a, const void *b) {
1551 const struct reply_callback *x = a, *y = b;
1553 if (x->timeout != 0 && y->timeout == 0)
1556 if (x->timeout == 0 && y->timeout != 0)
1559 if (x->timeout < y->timeout)
1562 if (x->timeout > y->timeout)
1568 _public_ int sd_bus_call_async(
1571 sd_bus_message_handler_t callback,
1576 struct reply_callback *c;
1579 assert_return(bus, -EINVAL);
1580 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1581 assert_return(m, -EINVAL);
1582 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1583 assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1584 assert_return(callback, -EINVAL);
1585 assert_return(!bus_pid_changed(bus), -ECHILD);
1587 r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
1591 r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
1595 r = bus_seal_message(bus, m, usec);
1599 c = new0(struct reply_callback, 1);
1603 c->callback = callback;
1604 c->userdata = userdata;
1605 c->serial = BUS_MESSAGE_SERIAL(m);
1606 c->timeout = calc_elapse(m->timeout);
1608 r = hashmap_put(bus->reply_callbacks, &c->serial, c);
1614 if (c->timeout != 0) {
1615 r = prioq_put(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1618 sd_bus_call_async_cancel(bus, c->serial);
1623 r = sd_bus_send(bus, m, serial);
1625 sd_bus_call_async_cancel(bus, c->serial);
1632 _public_ int sd_bus_call_async_cancel(sd_bus *bus, uint64_t serial) {
1633 struct reply_callback *c;
1635 assert_return(bus, -EINVAL);
1636 assert_return(serial != 0, -EINVAL);
1637 assert_return(!bus_pid_changed(bus), -ECHILD);
1639 c = hashmap_remove(bus->reply_callbacks, &serial);
1643 if (c->timeout != 0)
1644 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1650 int bus_ensure_running(sd_bus *bus) {
1655 if (bus->state == BUS_UNSET || bus->state == BUS_CLOSED || bus->state == BUS_CLOSING)
1657 if (bus->state == BUS_RUNNING)
1661 r = sd_bus_process(bus, NULL);
1664 if (bus->state == BUS_RUNNING)
1669 r = sd_bus_wait(bus, (uint64_t) -1);
1675 _public_ int sd_bus_call(
1679 sd_bus_error *error,
1680 sd_bus_message **reply) {
1687 assert_return(bus, -EINVAL);
1688 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1689 assert_return(m, -EINVAL);
1690 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1691 assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1692 assert_return(!bus_error_is_dirty(error), -EINVAL);
1693 assert_return(!bus_pid_changed(bus), -ECHILD);
1695 r = bus_ensure_running(bus);
1699 i = bus->rqueue_size;
1701 r = bus_seal_message(bus, m, usec);
1705 r = sd_bus_send(bus, m, &serial);
1709 timeout = calc_elapse(m->timeout);
1714 while (i < bus->rqueue_size) {
1715 sd_bus_message *incoming = NULL;
1717 incoming = bus->rqueue[i];
1719 if (incoming->reply_serial == serial) {
1720 /* Found a match! */
1722 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1725 if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
1730 sd_bus_message_unref(incoming);
1733 } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
1734 r = sd_bus_error_copy(error, &incoming->error);
1738 sd_bus_message_unref(incoming);
1741 } else if (incoming->header->serial == serial &&
1744 streq(bus->unique_name, incoming->sender)) {
1746 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1749 /* Our own message? Somebody is trying
1750 * to send its own client a message,
1751 * let's not dead-lock, let's fail
1754 sd_bus_message_unref(incoming);
1758 /* Try to read more, right-away */
1762 r = bus_read_message(bus);
1764 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1765 bus_enter_closing(bus);
1775 n = now(CLOCK_MONOTONIC);
1781 left = (uint64_t) -1;
1783 r = bus_poll(bus, true, left);
1789 r = dispatch_wqueue(bus);
1791 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1792 bus_enter_closing(bus);
1799 _public_ int sd_bus_get_fd(sd_bus *bus) {
1801 assert_return(bus, -EINVAL);
1802 assert_return(bus->input_fd == bus->output_fd, -EPERM);
1803 assert_return(!bus_pid_changed(bus), -ECHILD);
1805 return bus->input_fd;
1808 _public_ int sd_bus_get_events(sd_bus *bus) {
1811 assert_return(bus, -EINVAL);
1812 assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
1813 assert_return(!bus_pid_changed(bus), -ECHILD);
1815 if (bus->state == BUS_OPENING)
1817 else if (bus->state == BUS_AUTHENTICATING) {
1819 if (bus_socket_auth_needs_write(bus))
1824 } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
1825 if (bus->rqueue_size <= 0)
1827 if (bus->wqueue_size > 0)
1834 _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
1835 struct reply_callback *c;
1837 assert_return(bus, -EINVAL);
1838 assert_return(timeout_usec, -EINVAL);
1839 assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
1840 assert_return(!bus_pid_changed(bus), -ECHILD);
1842 if (bus->state == BUS_CLOSING) {
1847 if (bus->state == BUS_AUTHENTICATING) {
1848 *timeout_usec = bus->auth_timeout;
1852 if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) {
1853 *timeout_usec = (uint64_t) -1;
1857 if (bus->rqueue_size > 0) {
1862 c = prioq_peek(bus->reply_callbacks_prioq);
1864 *timeout_usec = (uint64_t) -1;
1868 *timeout_usec = c->timeout;
1872 static int process_timeout(sd_bus *bus) {
1873 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1874 _cleanup_bus_message_unref_ sd_bus_message* m = NULL;
1875 struct reply_callback *c;
1881 c = prioq_peek(bus->reply_callbacks_prioq);
1885 n = now(CLOCK_MONOTONIC);
1889 r = bus_message_new_synthetic_error(
1892 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
1897 m->sender = "org.freedesktop.DBus";
1899 r = bus_seal_synthetic_message(bus, m);
1903 assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
1904 hashmap_remove(bus->reply_callbacks, &c->serial);
1907 bus->iteration_counter ++;
1909 r = c->callback(bus, m, c->userdata, &error_buffer);
1910 r = bus_maybe_reply_error(m, r, &error_buffer);
1913 bus->current = NULL;
1918 static int process_hello(sd_bus *bus, sd_bus_message *m) {
1922 if (bus->state != BUS_HELLO)
1925 /* Let's make sure the first message on the bus is the HELLO
1926 * reply. But note that we don't actually parse the message
1927 * here (we leave that to the usual handling), we just verify
1928 * we don't let any earlier msg through. */
1930 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
1931 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1934 if (m->reply_serial != bus->hello_serial)
1940 static int process_reply(sd_bus *bus, sd_bus_message *m) {
1941 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1942 struct reply_callback *c;
1948 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
1949 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1952 c = hashmap_remove(bus->reply_callbacks, &m->reply_serial);
1956 if (c->timeout != 0)
1957 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1959 r = sd_bus_message_rewind(m, true);
1963 r = c->callback(bus, m, c->userdata, &error_buffer);
1964 r = bus_maybe_reply_error(m, r, &error_buffer);
1970 static int process_filter(sd_bus *bus, sd_bus_message *m) {
1971 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1972 struct filter_callback *l;
1979 bus->filter_callbacks_modified = false;
1981 LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
1983 if (bus->filter_callbacks_modified)
1986 /* Don't run this more than once per iteration */
1987 if (l->last_iteration == bus->iteration_counter)
1990 l->last_iteration = bus->iteration_counter;
1992 r = sd_bus_message_rewind(m, true);
1996 r = l->callback(bus, m, l->userdata, &error_buffer);
1997 r = bus_maybe_reply_error(m, r, &error_buffer);
2003 } while (bus->filter_callbacks_modified);
2008 static int process_match(sd_bus *bus, sd_bus_message *m) {
2015 bus->match_callbacks_modified = false;
2017 r = bus_match_run(bus, &bus->match_callbacks, m);
2021 } while (bus->match_callbacks_modified);
2026 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
2027 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2033 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
2036 if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
2039 if (m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
2042 if (streq_ptr(m->member, "Ping"))
2043 r = sd_bus_message_new_method_return(m, &reply);
2044 else if (streq_ptr(m->member, "GetMachineId")) {
2048 r = sd_id128_get_machine(&id);
2052 r = sd_bus_message_new_method_return(m, &reply);
2056 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
2058 r = sd_bus_message_new_method_errorf(
2060 SD_BUS_ERROR_UNKNOWN_METHOD,
2061 "Unknown method '%s' on interface '%s'.", m->member, m->interface);
2067 r = sd_bus_send(bus, reply, NULL);
2074 static int process_message(sd_bus *bus, sd_bus_message *m) {
2081 bus->iteration_counter++;
2083 log_debug("Got message sender=%s object=%s interface=%s member=%s",
2084 strna(sd_bus_message_get_sender(m)),
2085 strna(sd_bus_message_get_path(m)),
2086 strna(sd_bus_message_get_interface(m)),
2087 strna(sd_bus_message_get_member(m)));
2089 r = process_hello(bus, m);
2093 r = process_reply(bus, m);
2097 r = process_filter(bus, m);
2101 r = process_match(bus, m);
2105 r = process_builtin(bus, m);
2109 r = bus_process_object(bus, m);
2112 bus->current = NULL;
2116 static int process_running(sd_bus *bus, sd_bus_message **ret) {
2117 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2121 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
2123 r = process_timeout(bus);
2127 r = dispatch_wqueue(bus);
2131 r = dispatch_rqueue(bus, &m);
2137 r = process_message(bus, m);
2142 r = sd_bus_message_rewind(m, true);
2151 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
2153 r = sd_bus_reply_method_errorf(
2155 SD_BUS_ERROR_UNKNOWN_OBJECT,
2156 "Unknown object '%s'.", m->path);
2170 static int process_closing(sd_bus *bus, sd_bus_message **ret) {
2171 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2172 struct reply_callback *c;
2176 assert(bus->state == BUS_CLOSING);
2178 c = hashmap_first(bus->reply_callbacks);
2180 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2182 /* First, fail all outstanding method calls */
2183 r = bus_message_new_synthetic_error(
2186 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
2191 r = bus_seal_synthetic_message(bus, m);
2195 if (c->timeout != 0)
2196 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2198 hashmap_remove(bus->reply_callbacks, &c->serial);
2201 bus->iteration_counter++;
2203 r = c->callback(bus, m, c->userdata, &error_buffer);
2204 r = bus_maybe_reply_error(m, r, &error_buffer);
2210 /* Then, synthesize a Disconnected message */
2211 r = sd_bus_message_new_signal(
2213 "/org/freedesktop/DBus/Local",
2214 "org.freedesktop.DBus.Local",
2220 m->sender = "org.freedesktop.DBus.Local";
2222 r = bus_seal_synthetic_message(bus, m);
2229 bus->iteration_counter++;
2231 r = process_filter(bus, m);
2235 r = process_match(bus, m);
2247 bus->current = NULL;
2251 _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
2252 BUS_DONT_DESTROY(bus);
2255 /* Returns 0 when we didn't do anything. This should cause the
2256 * caller to invoke sd_bus_wait() before returning the next
2257 * time. Returns > 0 when we did something, which possibly
2258 * means *ret is filled in with an unprocessed message. */
2260 assert_return(bus, -EINVAL);
2261 assert_return(!bus_pid_changed(bus), -ECHILD);
2263 /* We don't allow recursively invoking sd_bus_process(). */
2264 assert_return(!bus->current, -EBUSY);
2266 switch (bus->state) {
2275 r = bus_socket_process_opening(bus);
2276 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2277 bus_enter_closing(bus);
2285 case BUS_AUTHENTICATING:
2286 r = bus_socket_process_authenticating(bus);
2287 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2288 bus_enter_closing(bus);
2300 r = process_running(bus, ret);
2301 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2302 bus_enter_closing(bus);
2312 return process_closing(bus, ret);
2315 assert_not_reached("Unknown state");
2318 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
2319 struct pollfd p[2] = {};
2322 usec_t m = (usec_t) -1;
2326 if (bus->state == BUS_CLOSING)
2329 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2331 e = sd_bus_get_events(bus);
2336 /* The caller really needs some more data, he doesn't
2337 * care about what's already read, or any timeouts
2342 /* The caller wants to process if there's something to
2343 * process, but doesn't care otherwise */
2345 r = sd_bus_get_timeout(bus, &until);
2350 nw = now(CLOCK_MONOTONIC);
2351 m = until > nw ? until - nw : 0;
2355 if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
2358 p[0].fd = bus->input_fd;
2359 if (bus->output_fd == bus->input_fd) {
2363 p[0].events = e & POLLIN;
2364 p[1].fd = bus->output_fd;
2365 p[1].events = e & POLLOUT;
2369 r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
2373 return r > 0 ? 1 : 0;
2376 _public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
2378 assert_return(bus, -EINVAL);
2379 assert_return(!bus_pid_changed(bus), -ECHILD);
2381 if (bus->state == BUS_CLOSING)
2384 assert_return(BUS_IS_OPEN(bus->state) , -ENOTCONN);
2386 if (bus->rqueue_size > 0)
2389 return bus_poll(bus, false, timeout_usec);
2392 _public_ int sd_bus_flush(sd_bus *bus) {
2395 assert_return(bus, -EINVAL);
2396 assert_return(!bus_pid_changed(bus), -ECHILD);
2398 if (bus->state == BUS_CLOSING)
2401 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2403 r = bus_ensure_running(bus);
2407 if (bus->wqueue_size <= 0)
2411 r = dispatch_wqueue(bus);
2413 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
2414 bus_enter_closing(bus);
2419 if (bus->wqueue_size <= 0)
2422 r = bus_poll(bus, false, (uint64_t) -1);
2428 _public_ int sd_bus_add_filter(sd_bus *bus,
2429 sd_bus_message_handler_t callback,
2432 struct filter_callback *f;
2434 assert_return(bus, -EINVAL);
2435 assert_return(callback, -EINVAL);
2436 assert_return(!bus_pid_changed(bus), -ECHILD);
2438 f = new0(struct filter_callback, 1);
2441 f->callback = callback;
2442 f->userdata = userdata;
2444 bus->filter_callbacks_modified = true;
2445 LIST_PREPEND(callbacks, bus->filter_callbacks, f);
2449 _public_ int sd_bus_remove_filter(sd_bus *bus,
2450 sd_bus_message_handler_t callback,
2453 struct filter_callback *f;
2455 assert_return(bus, -EINVAL);
2456 assert_return(callback, -EINVAL);
2457 assert_return(!bus_pid_changed(bus), -ECHILD);
2459 LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
2460 if (f->callback == callback && f->userdata == userdata) {
2461 bus->filter_callbacks_modified = true;
2462 LIST_REMOVE(callbacks, bus->filter_callbacks, f);
2471 _public_ int sd_bus_add_match(sd_bus *bus,
2473 sd_bus_message_handler_t callback,
2476 struct bus_match_component *components = NULL;
2477 unsigned n_components = 0;
2478 uint64_t cookie = 0;
2481 assert_return(bus, -EINVAL);
2482 assert_return(match, -EINVAL);
2483 assert_return(!bus_pid_changed(bus), -ECHILD);
2485 r = bus_match_parse(match, &components, &n_components);
2489 if (bus->bus_client) {
2490 cookie = ++bus->match_cookie;
2492 r = bus_add_match_internal(bus, match, components, n_components, cookie);
2497 bus->match_callbacks_modified = true;
2498 r = bus_match_add(&bus->match_callbacks, components, n_components, callback, userdata, cookie, NULL);
2500 if (bus->bus_client)
2501 bus_remove_match_internal(bus, match, cookie);
2505 bus_match_parse_free(components, n_components);
2509 _public_ int sd_bus_remove_match(sd_bus *bus,
2511 sd_bus_message_handler_t callback,
2514 struct bus_match_component *components = NULL;
2515 unsigned n_components = 0;
2517 uint64_t cookie = 0;
2519 assert_return(bus, -EINVAL);
2520 assert_return(match, -EINVAL);
2521 assert_return(!bus_pid_changed(bus), -ECHILD);
2523 r = bus_match_parse(match, &components, &n_components);
2527 bus->match_callbacks_modified = true;
2528 r = bus_match_remove(&bus->match_callbacks, components, n_components, callback, userdata, &cookie);
2530 if (bus->bus_client)
2531 q = bus_remove_match_internal(bus, match, cookie);
2533 bus_match_parse_free(components, n_components);
2535 return r < 0 ? r : q;
2538 bool bus_pid_changed(sd_bus *bus) {
2541 /* We don't support people creating a bus connection and
2542 * keeping it around over a fork(). Let's complain. */
2544 return bus->original_pid != getpid();
2547 static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2548 sd_bus *bus = userdata;
2553 r = sd_bus_process(bus, NULL);
2560 static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
2561 sd_bus *bus = userdata;
2566 r = sd_bus_process(bus, NULL);
2573 static int prepare_callback(sd_event_source *s, void *userdata) {
2574 sd_bus *bus = userdata;
2581 e = sd_bus_get_events(bus);
2585 if (bus->output_fd != bus->input_fd) {
2587 r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN);
2591 r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT);
2595 r = sd_event_source_set_io_events(bus->input_io_event_source, e);
2600 r = sd_bus_get_timeout(bus, &until);
2606 j = sd_event_source_set_time(bus->time_event_source, until);
2611 r = sd_event_source_set_enabled(bus->time_event_source, r > 0);
2618 static int quit_callback(sd_event_source *event, void *userdata) {
2619 sd_bus *bus = userdata;
2628 static int attach_io_events(sd_bus *bus) {
2633 if (bus->input_fd < 0)
2639 if (!bus->input_io_event_source) {
2640 r = sd_event_add_io(bus->event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source);
2644 r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
2648 r = sd_event_source_set_priority(bus->input_io_event_source, bus->event_priority);
2650 r = sd_event_source_set_io_fd(bus->input_io_event_source, bus->input_fd);
2655 if (bus->output_fd != bus->input_fd) {
2656 assert(bus->output_fd >= 0);
2658 if (!bus->output_io_event_source) {
2659 r = sd_event_add_io(bus->event, bus->output_fd, 0, io_callback, bus, &bus->output_io_event_source);
2663 r = sd_event_source_set_priority(bus->output_io_event_source, bus->event_priority);
2665 r = sd_event_source_set_io_fd(bus->output_io_event_source, bus->output_fd);
2674 static void detach_io_events(sd_bus *bus) {
2677 if (bus->input_io_event_source) {
2678 sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
2679 bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
2682 if (bus->output_io_event_source) {
2683 sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
2684 bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
2688 _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
2691 assert_return(bus, -EINVAL);
2692 assert_return(!bus->event, -EBUSY);
2694 assert(!bus->input_io_event_source);
2695 assert(!bus->output_io_event_source);
2696 assert(!bus->time_event_source);
2699 bus->event = sd_event_ref(event);
2701 r = sd_event_default(&bus->event);
2706 bus->event_priority = priority;
2708 r = sd_event_add_monotonic(bus->event, 0, 0, time_callback, bus, &bus->time_event_source);
2712 r = sd_event_source_set_priority(bus->time_event_source, priority);
2716 r = sd_event_add_exit(bus->event, quit_callback, bus, &bus->quit_event_source);
2720 r = attach_io_events(bus);
2727 sd_bus_detach_event(bus);
2731 _public_ int sd_bus_detach_event(sd_bus *bus) {
2732 assert_return(bus, -EINVAL);
2737 detach_io_events(bus);
2739 if (bus->time_event_source) {
2740 sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF);
2741 bus->time_event_source = sd_event_source_unref(bus->time_event_source);
2744 if (bus->quit_event_source) {
2745 sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF);
2746 bus->quit_event_source = sd_event_source_unref(bus->quit_event_source);
2750 bus->event = sd_event_unref(bus->event);
2755 _public_ sd_event* sd_bus_get_event(sd_bus *bus) {
2756 assert_return(bus, NULL);
2761 _public_ sd_bus_message* sd_bus_get_current(sd_bus *bus) {
2762 assert_return(bus, NULL);
2764 return bus->current;
2767 static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
2772 assert(default_bus);
2775 return !!*default_bus;
2778 *ret = sd_bus_ref(*default_bus);
2786 b->default_bus_ptr = default_bus;
2794 _public_ int sd_bus_default_system(sd_bus **ret) {
2795 static __thread sd_bus *default_system_bus = NULL;
2797 return bus_default(sd_bus_open_system, &default_system_bus, ret);
2800 _public_ int sd_bus_default_user(sd_bus **ret) {
2801 static __thread sd_bus *default_user_bus = NULL;
2803 return bus_default(sd_bus_open_user, &default_user_bus, ret);
2806 _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
2807 assert_return(b, -EINVAL);
2808 assert_return(tid, -EINVAL);
2809 assert_return(!bus_pid_changed(b), -ECHILD);
2817 return sd_event_get_tid(b->event, tid);
2822 _public_ char *sd_bus_label_escape(const char *s) {
2826 assert_return(s, NULL);
2828 /* Escapes all chars that D-Bus' object path cannot deal
2829 * with. Can be reversed with bus_path_unescape(). We special
2830 * case the empty string. */
2835 r = new(char, strlen(s)*3 + 1);
2839 for (f = s, t = r; *f; f++) {
2841 /* Escape everything that is not a-zA-Z0-9. We also
2842 * escape 0-9 if it's the first character */
2844 if (!(*f >= 'A' && *f <= 'Z') &&
2845 !(*f >= 'a' && *f <= 'z') &&
2846 !(f > s && *f >= '0' && *f <= '9')) {
2848 *(t++) = hexchar(*f >> 4);
2849 *(t++) = hexchar(*f);
2859 _public_ char *sd_bus_label_unescape(const char *f) {
2862 assert_return(f, NULL);
2864 /* Special case for the empty string */
2868 r = new(char, strlen(f) + 1);
2872 for (t = r; *f; f++) {
2877 if ((a = unhexchar(f[1])) < 0 ||
2878 (b = unhexchar(f[2])) < 0) {
2879 /* Invalid escape code, let's take it literal then */
2882 *(t++) = (char) ((a << 4) | b);
2894 _public_ int sd_bus_get_peer_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
2899 assert_return(bus, -EINVAL);
2900 assert_return(mask <= _SD_BUS_CREDS_ALL, -ENOTSUP);
2901 assert_return(ret, -EINVAL);
2902 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2903 assert_return(!bus_pid_changed(bus), -ECHILD);
2904 assert_return(!bus->is_kernel, -ENOTSUP);
2906 if (!bus->ucred_valid && !isempty(bus->label))
2909 c = bus_creds_new();
2913 if (bus->ucred_valid) {
2914 pid = c->pid = bus->ucred.pid;
2915 c->uid = bus->ucred.uid;
2916 c->gid = bus->ucred.gid;
2918 c->mask |= (SD_BUS_CREDS_UID | SD_BUS_CREDS_PID | SD_BUS_CREDS_GID) & mask;
2921 if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) {
2922 c->label = strdup(bus->label);
2924 sd_bus_creds_unref(c);
2928 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
2931 r = bus_creds_add_more(c, mask, pid, 0);