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_size = b->wqueue_size = 0;
124 static void bus_free(sd_bus *b) {
125 struct filter_callback *f;
130 sd_bus_detach_event(b);
135 munmap(b->kdbus_buffer, KDBUS_POOL_SIZE);
138 free(b->unique_name);
139 free(b->auth_buffer);
145 strv_free(b->exec_argv);
147 close_many(b->fds, b->n_fds);
152 hashmap_free_free(b->reply_callbacks);
153 prioq_free(b->reply_callbacks_prioq);
155 while ((f = b->filter_callbacks)) {
156 LIST_REMOVE(callbacks, b->filter_callbacks, f);
160 bus_match_free(&b->match_callbacks);
162 hashmap_free_free(b->vtable_methods);
163 hashmap_free_free(b->vtable_properties);
165 while ((n = hashmap_first(b->nodes)))
166 bus_node_destroy(b, n);
168 hashmap_free(b->nodes);
170 bus_kernel_flush_memfd(b);
172 assert_se(pthread_mutex_destroy(&b->memfd_cache_mutex) == 0);
177 _public_ int sd_bus_new(sd_bus **ret) {
180 assert_return(ret, -EINVAL);
186 r->n_ref = REFCNT_INIT;
187 r->input_fd = r->output_fd = -1;
188 r->message_version = 1;
189 r->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
190 r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
191 r->attach_flags |= KDBUS_ATTACH_NAMES;
192 r->original_pid = getpid();
194 assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0);
196 /* We guarantee that wqueue always has space for at least one
198 r->wqueue = new(sd_bus_message*, 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) {
410 if (strncmp(*p, key, l) != 0)
423 while (*a != ';' && *a != ',' && *a != 0) {
441 c = (char) ((x << 4) | y);
448 t = realloc(r, n + 2);
476 static void skip_address_key(const char **p) {
480 *p += strcspn(*p, ",");
486 static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
487 _cleanup_free_ char *path = NULL, *abstract = NULL;
496 while (**p != 0 && **p != ';') {
497 r = parse_address_key(p, "guid", guid);
503 r = parse_address_key(p, "path", &path);
509 r = parse_address_key(p, "abstract", &abstract);
518 if (!path && !abstract)
521 if (path && abstract)
526 if (l > sizeof(b->sockaddr.un.sun_path))
529 b->sockaddr.un.sun_family = AF_UNIX;
530 strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path));
531 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l;
532 } else if (abstract) {
533 l = strlen(abstract);
534 if (l > sizeof(b->sockaddr.un.sun_path) - 1)
537 b->sockaddr.un.sun_family = AF_UNIX;
538 b->sockaddr.un.sun_path[0] = 0;
539 strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1);
540 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
546 static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
547 _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
549 struct addrinfo *result, hints = {
550 .ai_socktype = SOCK_STREAM,
551 .ai_flags = AI_ADDRCONFIG,
559 while (**p != 0 && **p != ';') {
560 r = parse_address_key(p, "guid", guid);
566 r = parse_address_key(p, "host", &host);
572 r = parse_address_key(p, "port", &port);
578 r = parse_address_key(p, "family", &family);
591 if (streq(family, "ipv4"))
592 hints.ai_family = AF_INET;
593 else if (streq(family, "ipv6"))
594 hints.ai_family = AF_INET6;
599 r = getaddrinfo(host, port, &hints, &result);
603 return -EADDRNOTAVAIL;
605 memcpy(&b->sockaddr, result->ai_addr, result->ai_addrlen);
606 b->sockaddr_size = result->ai_addrlen;
608 freeaddrinfo(result);
613 static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
615 unsigned n_argv = 0, j;
624 while (**p != 0 && **p != ';') {
625 r = parse_address_key(p, "guid", guid);
631 r = parse_address_key(p, "path", &path);
637 if (startswith(*p, "argv")) {
641 ul = strtoul(*p + 4, (char**) p, 10);
642 if (errno > 0 || **p != '=' || ul > 256) {
652 x = realloc(argv, sizeof(char*) * (ul + 2));
658 memset(x + n_argv, 0, sizeof(char*) * (ul - n_argv + 2));
664 r = parse_address_key(p, NULL, argv + ul);
679 /* Make sure there are no holes in the array, with the
680 * exception of argv[0] */
681 for (j = 1; j < n_argv; j++)
687 if (argv && argv[0] == NULL) {
688 argv[0] = strdup(path);
700 for (j = 0; j < n_argv; j++)
708 static int parse_kernel_address(sd_bus *b, const char **p, char **guid) {
709 _cleanup_free_ char *path = NULL;
717 while (**p != 0 && **p != ';') {
718 r = parse_address_key(p, "guid", guid);
724 r = parse_address_key(p, "path", &path);
743 static int parse_container_unix_address(sd_bus *b, const char **p, char **guid) {
744 _cleanup_free_ char *machine = NULL;
752 while (**p != 0 && **p != ';') {
753 r = parse_address_key(p, "guid", guid);
759 r = parse_address_key(p, "machine", &machine);
771 if (!filename_is_safe(machine))
775 b->machine = machine;
778 b->sockaddr.un.sun_family = AF_UNIX;
779 strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
780 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/var/run/dbus/system_bus_socket") - 1;
785 static int parse_container_kernel_address(sd_bus *b, const char **p, char **guid) {
786 _cleanup_free_ char *machine = NULL;
794 while (**p != 0 && **p != ';') {
795 r = parse_address_key(p, "guid", guid);
801 r = parse_address_key(p, "machine", &machine);
813 if (!filename_is_safe(machine))
817 b->machine = machine;
821 b->kernel = strdup("/dev/kdbus/0-system/bus");
828 static void bus_reset_parsed_address(sd_bus *b) {
832 b->sockaddr_size = 0;
833 strv_free(b->exec_argv);
837 b->server_id = SD_ID128_NULL;
844 static int bus_parse_next_address(sd_bus *b) {
845 _cleanup_free_ char *guid = NULL;
853 if (b->address[b->address_index] == 0)
856 bus_reset_parsed_address(b);
858 a = b->address + b->address_index;
867 if (startswith(a, "unix:")) {
870 r = parse_unix_address(b, &a, &guid);
875 } else if (startswith(a, "tcp:")) {
878 r = parse_tcp_address(b, &a, &guid);
884 } else if (startswith(a, "unixexec:")) {
887 r = parse_exec_address(b, &a, &guid);
893 } else if (startswith(a, "kernel:")) {
896 r = parse_kernel_address(b, &a, &guid);
901 } else if (startswith(a, "x-container-unix:")) {
904 r = parse_container_unix_address(b, &a, &guid);
909 } else if (startswith(a, "x-container-kernel:")) {
912 r = parse_container_kernel_address(b, &a, &guid);
925 r = sd_id128_from_string(guid, &b->server_id);
930 b->address_index = a - b->address;
934 static int bus_start_address(sd_bus *b) {
940 bool skipped = false;
945 r = bus_socket_exec(b);
946 else if (b->machine && b->kernel)
947 r = bus_container_connect_kernel(b);
948 else if (b->machine && b->sockaddr.sa.sa_family != AF_UNSPEC)
949 r = bus_container_connect_socket(b);
951 r = bus_kernel_connect(b);
952 else if (b->sockaddr.sa.sa_family != AF_UNSPEC)
953 r = bus_socket_connect(b);
959 r = attach_io_events(b);
964 b->last_connect_error = -r;
967 r = bus_parse_next_address(b);
971 return b->last_connect_error ? -b->last_connect_error : -ECONNREFUSED;
975 int bus_next_address(sd_bus *b) {
978 bus_reset_parsed_address(b);
979 return bus_start_address(b);
982 static int bus_start_fd(sd_bus *b) {
987 assert(b->input_fd >= 0);
988 assert(b->output_fd >= 0);
990 r = fd_nonblock(b->input_fd, true);
994 r = fd_cloexec(b->input_fd, true);
998 if (b->input_fd != b->output_fd) {
999 r = fd_nonblock(b->output_fd, true);
1003 r = fd_cloexec(b->output_fd, true);
1008 if (fstat(b->input_fd, &st) < 0)
1011 if (S_ISCHR(b->input_fd))
1012 return bus_kernel_take_fd(b);
1014 return bus_socket_take_fd(b);
1017 _public_ int sd_bus_start(sd_bus *bus) {
1020 assert_return(bus, -EINVAL);
1021 assert_return(bus->state == BUS_UNSET, -EPERM);
1022 assert_return(!bus_pid_changed(bus), -ECHILD);
1024 bus->state = BUS_OPENING;
1026 if (bus->is_server && bus->bus_client)
1029 if (bus->input_fd >= 0)
1030 r = bus_start_fd(bus);
1031 else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel || bus->machine)
1032 r = bus_start_address(bus);
1039 return bus_send_hello(bus);
1042 _public_ int sd_bus_open_system(sd_bus **ret) {
1047 assert_return(ret, -EINVAL);
1053 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1055 r = sd_bus_set_address(b, e);
1058 r = sd_bus_set_address(b, "kernel:path=/dev/kdbus/0-system/bus;unix:path=/run/dbus/system_bus_socket");
1060 r = sd_bus_set_address(b, "unix:path=/run/dbus/system_bus_socket");
1066 b->bus_client = true;
1068 /* Let's do per-method access control on the system bus. We
1069 * need the caller's UID and capability set for that. */
1071 b->attach_flags |= KDBUS_ATTACH_CAPS | KDBUS_ATTACH_CREDS;
1073 r = sd_bus_start(b);
1085 _public_ int sd_bus_open_user(sd_bus **ret) {
1090 assert_return(ret, -EINVAL);
1096 e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1098 r = sd_bus_set_address(b, e);
1102 e = secure_getenv("XDG_RUNTIME_DIR");
1104 _cleanup_free_ char *ee = NULL;
1106 ee = bus_address_escape(e);
1113 asprintf(&b->address, "kernel:path=/dev/kdbus/%lu-user/bus;unix:path=%s/bus", (unsigned long) getuid(), ee);
1115 b->address = strjoin("unix:path=", ee, "/bus", NULL);
1119 asprintf(&b->address, "kernel:path=/dev/kdbus/%lu-user/bus", (unsigned long) getuid());
1121 return -ECONNREFUSED;
1131 b->bus_client = true;
1133 /* We don't do any per-method access control on the user
1137 r = sd_bus_start(b);
1149 _public_ int sd_bus_open_system_remote(const char *host, sd_bus **ret) {
1150 _cleanup_free_ char *e = NULL;
1155 assert_return(host, -EINVAL);
1156 assert_return(ret, -EINVAL);
1158 e = bus_address_escape(host);
1162 p = strjoin("unixexec:path=ssh,argv1=-xT,argv2=", e, ",argv3=systemd-stdio-bridge", NULL);
1166 r = sd_bus_new(&bus);
1173 bus->bus_client = true;
1175 r = sd_bus_start(bus);
1185 _public_ int sd_bus_open_system_container(const char *machine, sd_bus **ret) {
1186 _cleanup_free_ char *e = NULL;
1191 assert_return(machine, -EINVAL);
1192 assert_return(ret, -EINVAL);
1193 assert_return(filename_is_safe(machine), -EINVAL);
1195 e = bus_address_escape(machine);
1200 p = strjoin("x-container-kernel:machine=", e, ";x-container-unix:machine=", e, NULL);
1202 p = strjoin("x-container-unix:machine=", e, NULL);
1207 r = sd_bus_new(&bus);
1214 bus->bus_client = true;
1216 r = sd_bus_start(bus);
1226 _public_ void sd_bus_close(sd_bus *bus) {
1230 if (bus->state == BUS_CLOSED)
1232 if (bus_pid_changed(bus))
1235 bus->state = BUS_CLOSED;
1237 sd_bus_detach_event(bus);
1239 /* Drop all queued messages so that they drop references to
1240 * the bus object and the bus may be freed */
1241 bus_reset_queues(bus);
1243 if (!bus->is_kernel)
1246 /* We'll leave the fd open in case this is a kernel bus, since
1247 * there might still be memblocks around that reference this
1248 * bus, and they might need to invoke the * KDBUS_CMD_FREE
1249 * ioctl on the fd when they are freed. */
1252 static void bus_enter_closing(sd_bus *bus) {
1255 if (bus->state != BUS_OPENING &&
1256 bus->state != BUS_AUTHENTICATING &&
1257 bus->state != BUS_HELLO &&
1258 bus->state != BUS_RUNNING)
1261 bus->state = BUS_CLOSING;
1264 _public_ sd_bus *sd_bus_ref(sd_bus *bus) {
1265 assert_return(bus, NULL);
1267 assert_se(REFCNT_INC(bus->n_ref) >= 2);
1272 _public_ sd_bus *sd_bus_unref(sd_bus *bus) {
1277 if (REFCNT_DEC(bus->n_ref) <= 0)
1283 _public_ int sd_bus_is_open(sd_bus *bus) {
1285 assert_return(bus, -EINVAL);
1286 assert_return(!bus_pid_changed(bus), -ECHILD);
1288 return BUS_IS_OPEN(bus->state);
1291 _public_ int sd_bus_can_send(sd_bus *bus, char type) {
1294 assert_return(bus, -EINVAL);
1295 assert_return(bus->state != BUS_UNSET, -ENOTCONN);
1296 assert_return(!bus_pid_changed(bus), -ECHILD);
1298 if (type == SD_BUS_TYPE_UNIX_FD) {
1299 if (!(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD))
1302 r = bus_ensure_running(bus);
1306 return bus->can_fds;
1309 return bus_type_is_valid(type);
1312 _public_ int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
1315 assert_return(bus, -EINVAL);
1316 assert_return(server_id, -EINVAL);
1317 assert_return(!bus_pid_changed(bus), -ECHILD);
1319 r = bus_ensure_running(bus);
1323 *server_id = bus->server_id;
1327 static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
1331 if (b->message_version != 0 &&
1332 m->header->version != b->message_version)
1335 if (b->message_endian != 0 &&
1336 m->header->endian != b->message_endian)
1340 /* If we copy the same message to multiple
1341 * destinations, avoid using the same serial
1343 b->serial = MAX(b->serial, BUS_MESSAGE_SERIAL(m));
1348 timeout = BUS_DEFAULT_TIMEOUT;
1350 return bus_message_seal(m, ++b->serial, timeout);
1353 int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
1357 /* The bus specification says the serial number cannot be 0,
1358 * hence let's fill something in for synthetic messages. Since
1359 * synthetic messages might have a fake sender and we don't
1360 * want to interfere with the real sender's serial numbers we
1361 * pick a fixed, artifical one. We use (uint32_t) -1 rather
1362 * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
1363 * even though kdbus can do 64bit. */
1365 return bus_message_seal(m, 0xFFFFFFFFULL, 0);
1368 static int bus_write_message(sd_bus *bus, sd_bus_message *message, size_t *idx) {
1373 return bus_kernel_write_message(bus, message);
1375 return bus_socket_write_message(bus, message, idx);
1378 static int dispatch_wqueue(sd_bus *bus) {
1382 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1384 while (bus->wqueue_size > 0) {
1386 r = bus_write_message(bus, bus->wqueue[0], &bus->windex);
1390 /* Didn't do anything this time */
1392 else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
1393 /* Fully written. Let's drop the entry from
1396 * This isn't particularly optimized, but
1397 * well, this is supposed to be our worst-case
1398 * buffer only, and the socket buffer is
1399 * supposed to be our primary buffer, and if
1400 * it got full, then all bets are off
1403 sd_bus_message_unref(bus->wqueue[0]);
1404 bus->wqueue_size --;
1405 memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
1415 static int bus_read_message(sd_bus *bus) {
1419 return bus_kernel_read_message(bus);
1421 return bus_socket_read_message(bus);
1424 int bus_rqueue_make_room(sd_bus *bus) {
1428 x = bus->rqueue_size + 1;
1430 if (bus->rqueue_allocated >= x)
1433 if (x > BUS_RQUEUE_MAX)
1436 q = realloc(bus->rqueue, x * sizeof(sd_bus_message*));
1441 bus->rqueue_allocated = x;
1446 static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
1451 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1454 if (bus->rqueue_size > 0) {
1455 /* Dispatch a queued message */
1457 *m = bus->rqueue[0];
1458 bus->rqueue_size --;
1459 memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
1463 /* Try to read a new message */
1464 r = bus_read_message(bus);
1474 _public_ int sd_bus_send(sd_bus *bus, sd_bus_message *m, uint64_t *serial) {
1477 assert_return(bus, -EINVAL);
1478 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1479 assert_return(m, -EINVAL);
1480 assert_return(!bus_pid_changed(bus), -ECHILD);
1483 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
1490 /* If the serial number isn't kept, then we know that no reply
1492 if (!serial && !m->sealed)
1493 m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
1495 r = bus_seal_message(bus, m, 0);
1499 /* If this is a reply and no reply was requested, then let's
1500 * suppress this, if we can */
1501 if (m->dont_send && !serial)
1504 if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
1507 r = bus_write_message(bus, m, &idx);
1509 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1510 bus_enter_closing(bus);
1513 } else if (!bus->is_kernel && idx < BUS_MESSAGE_SIZE(m)) {
1514 /* Wasn't fully written. So let's remember how
1515 * much was written. Note that the first entry
1516 * of the wqueue array is always allocated so
1517 * that we always can remember how much was
1519 bus->wqueue[0] = sd_bus_message_ref(m);
1520 bus->wqueue_size = 1;
1526 /* Just append it to the queue. */
1528 if (bus->wqueue_size >= BUS_WQUEUE_MAX)
1531 q = realloc(bus->wqueue, sizeof(sd_bus_message*) * (bus->wqueue_size + 1));
1536 q[bus->wqueue_size ++] = sd_bus_message_ref(m);
1540 *serial = BUS_MESSAGE_SERIAL(m);
1545 _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *serial) {
1548 assert_return(bus, -EINVAL);
1549 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1550 assert_return(m, -EINVAL);
1551 assert_return(!bus_pid_changed(bus), -ECHILD);
1553 if (!streq_ptr(m->destination, destination)) {
1558 r = sd_bus_message_set_destination(m, destination);
1563 return sd_bus_send(bus, m, serial);
1566 static usec_t calc_elapse(uint64_t usec) {
1567 if (usec == (uint64_t) -1)
1570 return now(CLOCK_MONOTONIC) + usec;
1573 static int timeout_compare(const void *a, const void *b) {
1574 const struct reply_callback *x = a, *y = b;
1576 if (x->timeout != 0 && y->timeout == 0)
1579 if (x->timeout == 0 && y->timeout != 0)
1582 if (x->timeout < y->timeout)
1585 if (x->timeout > y->timeout)
1591 _public_ int sd_bus_call_async(
1594 sd_bus_message_handler_t callback,
1599 struct reply_callback *c;
1602 assert_return(bus, -EINVAL);
1603 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1604 assert_return(m, -EINVAL);
1605 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1606 assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1607 assert_return(callback, -EINVAL);
1608 assert_return(!bus_pid_changed(bus), -ECHILD);
1610 r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
1614 r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
1618 r = bus_seal_message(bus, m, usec);
1622 c = new0(struct reply_callback, 1);
1626 c->callback = callback;
1627 c->userdata = userdata;
1628 c->serial = BUS_MESSAGE_SERIAL(m);
1629 c->timeout = calc_elapse(m->timeout);
1631 r = hashmap_put(bus->reply_callbacks, &c->serial, c);
1637 if (c->timeout != 0) {
1638 r = prioq_put(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1641 sd_bus_call_async_cancel(bus, c->serial);
1646 r = sd_bus_send(bus, m, serial);
1648 sd_bus_call_async_cancel(bus, c->serial);
1655 _public_ int sd_bus_call_async_cancel(sd_bus *bus, uint64_t serial) {
1656 struct reply_callback *c;
1658 assert_return(bus, -EINVAL);
1659 assert_return(serial != 0, -EINVAL);
1660 assert_return(!bus_pid_changed(bus), -ECHILD);
1662 c = hashmap_remove(bus->reply_callbacks, &serial);
1666 if (c->timeout != 0)
1667 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1673 int bus_ensure_running(sd_bus *bus) {
1678 if (bus->state == BUS_UNSET || bus->state == BUS_CLOSED || bus->state == BUS_CLOSING)
1680 if (bus->state == BUS_RUNNING)
1684 r = sd_bus_process(bus, NULL);
1687 if (bus->state == BUS_RUNNING)
1692 r = sd_bus_wait(bus, (uint64_t) -1);
1698 _public_ int sd_bus_call(
1702 sd_bus_error *error,
1703 sd_bus_message **reply) {
1710 assert_return(bus, -EINVAL);
1711 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1712 assert_return(m, -EINVAL);
1713 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1714 assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1715 assert_return(!bus_error_is_dirty(error), -EINVAL);
1716 assert_return(!bus_pid_changed(bus), -ECHILD);
1718 r = bus_ensure_running(bus);
1722 i = bus->rqueue_size;
1724 r = bus_seal_message(bus, m, usec);
1728 r = sd_bus_send(bus, m, &serial);
1732 timeout = calc_elapse(m->timeout);
1737 while (i < bus->rqueue_size) {
1738 sd_bus_message *incoming = NULL;
1740 incoming = bus->rqueue[i];
1742 if (incoming->reply_serial == serial) {
1743 /* Found a match! */
1745 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1748 if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
1753 sd_bus_message_unref(incoming);
1756 } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
1757 r = sd_bus_error_copy(error, &incoming->error);
1761 sd_bus_message_unref(incoming);
1764 } else if (incoming->header->serial == serial &&
1767 streq(bus->unique_name, incoming->sender)) {
1769 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1772 /* Our own message? Somebody is trying
1773 * to send its own client a message,
1774 * let's not dead-lock, let's fail
1777 sd_bus_message_unref(incoming);
1781 /* Try to read more, right-away */
1785 r = bus_read_message(bus);
1787 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1788 bus_enter_closing(bus);
1798 n = now(CLOCK_MONOTONIC);
1804 left = (uint64_t) -1;
1806 r = bus_poll(bus, true, left);
1812 r = dispatch_wqueue(bus);
1814 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1815 bus_enter_closing(bus);
1822 _public_ int sd_bus_get_fd(sd_bus *bus) {
1824 assert_return(bus, -EINVAL);
1825 assert_return(bus->input_fd == bus->output_fd, -EPERM);
1826 assert_return(!bus_pid_changed(bus), -ECHILD);
1828 return bus->input_fd;
1831 _public_ int sd_bus_get_events(sd_bus *bus) {
1834 assert_return(bus, -EINVAL);
1835 assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
1836 assert_return(!bus_pid_changed(bus), -ECHILD);
1838 if (bus->state == BUS_OPENING)
1840 else if (bus->state == BUS_AUTHENTICATING) {
1842 if (bus_socket_auth_needs_write(bus))
1847 } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
1848 if (bus->rqueue_size <= 0)
1850 if (bus->wqueue_size > 0)
1857 _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
1858 struct reply_callback *c;
1860 assert_return(bus, -EINVAL);
1861 assert_return(timeout_usec, -EINVAL);
1862 assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
1863 assert_return(!bus_pid_changed(bus), -ECHILD);
1865 if (bus->state == BUS_CLOSING) {
1870 if (bus->state == BUS_AUTHENTICATING) {
1871 *timeout_usec = bus->auth_timeout;
1875 if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) {
1876 *timeout_usec = (uint64_t) -1;
1880 if (bus->rqueue_size > 0) {
1885 c = prioq_peek(bus->reply_callbacks_prioq);
1887 *timeout_usec = (uint64_t) -1;
1891 *timeout_usec = c->timeout;
1895 static int process_timeout(sd_bus *bus) {
1896 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1897 _cleanup_bus_message_unref_ sd_bus_message* m = NULL;
1898 struct reply_callback *c;
1904 c = prioq_peek(bus->reply_callbacks_prioq);
1908 n = now(CLOCK_MONOTONIC);
1912 r = bus_message_new_synthetic_error(
1915 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
1920 m->sender = "org.freedesktop.DBus";
1922 r = bus_seal_synthetic_message(bus, m);
1926 assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
1927 hashmap_remove(bus->reply_callbacks, &c->serial);
1930 bus->iteration_counter ++;
1932 r = c->callback(bus, m, c->userdata, &error_buffer);
1933 r = bus_maybe_reply_error(m, r, &error_buffer);
1936 bus->current = NULL;
1941 static int process_hello(sd_bus *bus, sd_bus_message *m) {
1945 if (bus->state != BUS_HELLO)
1948 /* Let's make sure the first message on the bus is the HELLO
1949 * reply. But note that we don't actually parse the message
1950 * here (we leave that to the usual handling), we just verify
1951 * we don't let any earlier msg through. */
1953 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
1954 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1957 if (m->reply_serial != bus->hello_serial)
1963 static int process_reply(sd_bus *bus, sd_bus_message *m) {
1964 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1965 struct reply_callback *c;
1971 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
1972 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1975 c = hashmap_remove(bus->reply_callbacks, &m->reply_serial);
1979 if (c->timeout != 0)
1980 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1982 r = sd_bus_message_rewind(m, true);
1986 r = c->callback(bus, m, c->userdata, &error_buffer);
1987 r = bus_maybe_reply_error(m, r, &error_buffer);
1993 static int process_filter(sd_bus *bus, sd_bus_message *m) {
1994 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1995 struct filter_callback *l;
2002 bus->filter_callbacks_modified = false;
2004 LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
2006 if (bus->filter_callbacks_modified)
2009 /* Don't run this more than once per iteration */
2010 if (l->last_iteration == bus->iteration_counter)
2013 l->last_iteration = bus->iteration_counter;
2015 r = sd_bus_message_rewind(m, true);
2019 r = l->callback(bus, m, l->userdata, &error_buffer);
2020 r = bus_maybe_reply_error(m, r, &error_buffer);
2026 } while (bus->filter_callbacks_modified);
2031 static int process_match(sd_bus *bus, sd_bus_message *m) {
2038 bus->match_callbacks_modified = false;
2040 r = bus_match_run(bus, &bus->match_callbacks, m);
2044 } while (bus->match_callbacks_modified);
2049 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
2050 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2056 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
2059 if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
2062 if (m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
2065 if (streq_ptr(m->member, "Ping"))
2066 r = sd_bus_message_new_method_return(m, &reply);
2067 else if (streq_ptr(m->member, "GetMachineId")) {
2071 r = sd_id128_get_machine(&id);
2075 r = sd_bus_message_new_method_return(m, &reply);
2079 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
2081 r = sd_bus_message_new_method_errorf(
2083 SD_BUS_ERROR_UNKNOWN_METHOD,
2084 "Unknown method '%s' on interface '%s'.", m->member, m->interface);
2090 r = sd_bus_send(bus, reply, NULL);
2097 static int process_message(sd_bus *bus, sd_bus_message *m) {
2104 bus->iteration_counter++;
2106 log_debug("Got message sender=%s object=%s interface=%s member=%s",
2107 strna(sd_bus_message_get_sender(m)),
2108 strna(sd_bus_message_get_path(m)),
2109 strna(sd_bus_message_get_interface(m)),
2110 strna(sd_bus_message_get_member(m)));
2112 r = process_hello(bus, m);
2116 r = process_reply(bus, m);
2120 r = process_filter(bus, m);
2124 r = process_match(bus, m);
2128 r = process_builtin(bus, m);
2132 r = bus_process_object(bus, m);
2135 bus->current = NULL;
2139 static int process_running(sd_bus *bus, sd_bus_message **ret) {
2140 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2144 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
2146 r = process_timeout(bus);
2150 r = dispatch_wqueue(bus);
2154 r = dispatch_rqueue(bus, &m);
2160 r = process_message(bus, m);
2165 r = sd_bus_message_rewind(m, true);
2174 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
2176 r = sd_bus_reply_method_errorf(
2178 SD_BUS_ERROR_UNKNOWN_OBJECT,
2179 "Unknown object '%s'.", m->path);
2193 static int process_closing(sd_bus *bus, sd_bus_message **ret) {
2194 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2195 struct reply_callback *c;
2199 assert(bus->state == BUS_CLOSING);
2201 c = hashmap_first(bus->reply_callbacks);
2203 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2205 /* First, fail all outstanding method calls */
2206 r = bus_message_new_synthetic_error(
2209 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
2214 r = bus_seal_synthetic_message(bus, m);
2218 if (c->timeout != 0)
2219 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2221 hashmap_remove(bus->reply_callbacks, &c->serial);
2224 bus->iteration_counter++;
2226 r = c->callback(bus, m, c->userdata, &error_buffer);
2227 r = bus_maybe_reply_error(m, r, &error_buffer);
2233 /* Then, synthesize a Disconnected message */
2234 r = sd_bus_message_new_signal(
2236 "/org/freedesktop/DBus/Local",
2237 "org.freedesktop.DBus.Local",
2243 m->sender = "org.freedesktop.DBus.Local";
2245 r = bus_seal_synthetic_message(bus, m);
2252 bus->iteration_counter++;
2254 r = process_filter(bus, m);
2258 r = process_match(bus, m);
2270 bus->current = NULL;
2274 _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
2275 BUS_DONT_DESTROY(bus);
2278 /* Returns 0 when we didn't do anything. This should cause the
2279 * caller to invoke sd_bus_wait() before returning the next
2280 * time. Returns > 0 when we did something, which possibly
2281 * means *ret is filled in with an unprocessed message. */
2283 assert_return(bus, -EINVAL);
2284 assert_return(!bus_pid_changed(bus), -ECHILD);
2286 /* We don't allow recursively invoking sd_bus_process(). */
2287 assert_return(!bus->current, -EBUSY);
2289 switch (bus->state) {
2298 r = bus_socket_process_opening(bus);
2299 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2300 bus_enter_closing(bus);
2308 case BUS_AUTHENTICATING:
2309 r = bus_socket_process_authenticating(bus);
2310 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2311 bus_enter_closing(bus);
2323 r = process_running(bus, ret);
2324 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2325 bus_enter_closing(bus);
2335 return process_closing(bus, ret);
2338 assert_not_reached("Unknown state");
2341 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
2342 struct pollfd p[2] = {};
2345 usec_t m = (usec_t) -1;
2349 if (bus->state == BUS_CLOSING)
2352 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2354 e = sd_bus_get_events(bus);
2359 /* The caller really needs some more data, he doesn't
2360 * care about what's already read, or any timeouts
2365 /* The caller wants to process if there's something to
2366 * process, but doesn't care otherwise */
2368 r = sd_bus_get_timeout(bus, &until);
2373 nw = now(CLOCK_MONOTONIC);
2374 m = until > nw ? until - nw : 0;
2378 if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
2381 p[0].fd = bus->input_fd;
2382 if (bus->output_fd == bus->input_fd) {
2386 p[0].events = e & POLLIN;
2387 p[1].fd = bus->output_fd;
2388 p[1].events = e & POLLOUT;
2392 r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
2396 return r > 0 ? 1 : 0;
2399 _public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
2401 assert_return(bus, -EINVAL);
2402 assert_return(!bus_pid_changed(bus), -ECHILD);
2404 if (bus->state == BUS_CLOSING)
2407 assert_return(BUS_IS_OPEN(bus->state) , -ENOTCONN);
2409 if (bus->rqueue_size > 0)
2412 return bus_poll(bus, false, timeout_usec);
2415 _public_ int sd_bus_flush(sd_bus *bus) {
2418 assert_return(bus, -EINVAL);
2419 assert_return(!bus_pid_changed(bus), -ECHILD);
2421 if (bus->state == BUS_CLOSING)
2424 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2426 r = bus_ensure_running(bus);
2430 if (bus->wqueue_size <= 0)
2434 r = dispatch_wqueue(bus);
2436 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
2437 bus_enter_closing(bus);
2442 if (bus->wqueue_size <= 0)
2445 r = bus_poll(bus, false, (uint64_t) -1);
2451 _public_ int sd_bus_add_filter(sd_bus *bus,
2452 sd_bus_message_handler_t callback,
2455 struct filter_callback *f;
2457 assert_return(bus, -EINVAL);
2458 assert_return(callback, -EINVAL);
2459 assert_return(!bus_pid_changed(bus), -ECHILD);
2461 f = new0(struct filter_callback, 1);
2464 f->callback = callback;
2465 f->userdata = userdata;
2467 bus->filter_callbacks_modified = true;
2468 LIST_PREPEND(callbacks, bus->filter_callbacks, f);
2472 _public_ int sd_bus_remove_filter(sd_bus *bus,
2473 sd_bus_message_handler_t callback,
2476 struct filter_callback *f;
2478 assert_return(bus, -EINVAL);
2479 assert_return(callback, -EINVAL);
2480 assert_return(!bus_pid_changed(bus), -ECHILD);
2482 LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
2483 if (f->callback == callback && f->userdata == userdata) {
2484 bus->filter_callbacks_modified = true;
2485 LIST_REMOVE(callbacks, bus->filter_callbacks, f);
2494 _public_ int sd_bus_add_match(sd_bus *bus,
2496 sd_bus_message_handler_t callback,
2499 struct bus_match_component *components = NULL;
2500 unsigned n_components = 0;
2501 uint64_t cookie = 0;
2504 assert_return(bus, -EINVAL);
2505 assert_return(match, -EINVAL);
2506 assert_return(!bus_pid_changed(bus), -ECHILD);
2508 r = bus_match_parse(match, &components, &n_components);
2512 if (bus->bus_client) {
2513 cookie = ++bus->match_cookie;
2515 r = bus_add_match_internal(bus, match, components, n_components, cookie);
2520 bus->match_callbacks_modified = true;
2521 r = bus_match_add(&bus->match_callbacks, components, n_components, callback, userdata, cookie, NULL);
2523 if (bus->bus_client)
2524 bus_remove_match_internal(bus, match, cookie);
2528 bus_match_parse_free(components, n_components);
2532 _public_ int sd_bus_remove_match(sd_bus *bus,
2534 sd_bus_message_handler_t callback,
2537 struct bus_match_component *components = NULL;
2538 unsigned n_components = 0;
2540 uint64_t cookie = 0;
2542 assert_return(bus, -EINVAL);
2543 assert_return(match, -EINVAL);
2544 assert_return(!bus_pid_changed(bus), -ECHILD);
2546 r = bus_match_parse(match, &components, &n_components);
2550 bus->match_callbacks_modified = true;
2551 r = bus_match_remove(&bus->match_callbacks, components, n_components, callback, userdata, &cookie);
2553 if (bus->bus_client)
2554 q = bus_remove_match_internal(bus, match, cookie);
2556 bus_match_parse_free(components, n_components);
2558 return r < 0 ? r : q;
2561 bool bus_pid_changed(sd_bus *bus) {
2564 /* We don't support people creating a bus connection and
2565 * keeping it around over a fork(). Let's complain. */
2567 return bus->original_pid != getpid();
2570 static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2571 sd_bus *bus = userdata;
2576 r = sd_bus_process(bus, NULL);
2583 static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
2584 sd_bus *bus = userdata;
2589 r = sd_bus_process(bus, NULL);
2596 static int prepare_callback(sd_event_source *s, void *userdata) {
2597 sd_bus *bus = userdata;
2604 e = sd_bus_get_events(bus);
2608 if (bus->output_fd != bus->input_fd) {
2610 r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN);
2614 r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT);
2618 r = sd_event_source_set_io_events(bus->input_io_event_source, e);
2623 r = sd_bus_get_timeout(bus, &until);
2629 j = sd_event_source_set_time(bus->time_event_source, until);
2634 r = sd_event_source_set_enabled(bus->time_event_source, r > 0);
2641 static int quit_callback(sd_event_source *event, void *userdata) {
2642 sd_bus *bus = userdata;
2651 static int attach_io_events(sd_bus *bus) {
2656 if (bus->input_fd < 0)
2662 if (!bus->input_io_event_source) {
2663 r = sd_event_add_io(bus->event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source);
2667 r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
2671 r = sd_event_source_set_priority(bus->input_io_event_source, bus->event_priority);
2673 r = sd_event_source_set_io_fd(bus->input_io_event_source, bus->input_fd);
2678 if (bus->output_fd != bus->input_fd) {
2679 assert(bus->output_fd >= 0);
2681 if (!bus->output_io_event_source) {
2682 r = sd_event_add_io(bus->event, bus->output_fd, 0, io_callback, bus, &bus->output_io_event_source);
2686 r = sd_event_source_set_priority(bus->output_io_event_source, bus->event_priority);
2688 r = sd_event_source_set_io_fd(bus->output_io_event_source, bus->output_fd);
2697 static void detach_io_events(sd_bus *bus) {
2700 if (bus->input_io_event_source) {
2701 sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
2702 bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
2705 if (bus->output_io_event_source) {
2706 sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
2707 bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
2711 _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
2714 assert_return(bus, -EINVAL);
2715 assert_return(!bus->event, -EBUSY);
2717 assert(!bus->input_io_event_source);
2718 assert(!bus->output_io_event_source);
2719 assert(!bus->time_event_source);
2722 bus->event = sd_event_ref(event);
2724 r = sd_event_default(&bus->event);
2729 bus->event_priority = priority;
2731 r = sd_event_add_monotonic(bus->event, 0, 0, time_callback, bus, &bus->time_event_source);
2735 r = sd_event_source_set_priority(bus->time_event_source, priority);
2739 r = sd_event_add_exit(bus->event, quit_callback, bus, &bus->quit_event_source);
2743 r = attach_io_events(bus);
2750 sd_bus_detach_event(bus);
2754 _public_ int sd_bus_detach_event(sd_bus *bus) {
2755 assert_return(bus, -EINVAL);
2760 detach_io_events(bus);
2762 if (bus->time_event_source) {
2763 sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF);
2764 bus->time_event_source = sd_event_source_unref(bus->time_event_source);
2767 if (bus->quit_event_source) {
2768 sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF);
2769 bus->quit_event_source = sd_event_source_unref(bus->quit_event_source);
2773 bus->event = sd_event_unref(bus->event);
2778 _public_ sd_event* sd_bus_get_event(sd_bus *bus) {
2779 assert_return(bus, NULL);
2784 _public_ sd_bus_message* sd_bus_get_current(sd_bus *bus) {
2785 assert_return(bus, NULL);
2787 return bus->current;
2790 static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
2795 assert(default_bus);
2798 return !!*default_bus;
2801 *ret = sd_bus_ref(*default_bus);
2809 b->default_bus_ptr = default_bus;
2817 _public_ int sd_bus_default_system(sd_bus **ret) {
2818 static __thread sd_bus *default_system_bus = NULL;
2820 return bus_default(sd_bus_open_system, &default_system_bus, ret);
2823 _public_ int sd_bus_default_user(sd_bus **ret) {
2824 static __thread sd_bus *default_user_bus = NULL;
2826 return bus_default(sd_bus_open_user, &default_user_bus, ret);
2829 _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
2830 assert_return(b, -EINVAL);
2831 assert_return(tid, -EINVAL);
2832 assert_return(!bus_pid_changed(b), -ECHILD);
2840 return sd_event_get_tid(b->event, tid);
2845 _public_ char *sd_bus_label_escape(const char *s) {
2849 assert_return(s, NULL);
2851 /* Escapes all chars that D-Bus' object path cannot deal
2852 * with. Can be reversed with bus_path_unescape(). We special
2853 * case the empty string. */
2858 r = new(char, strlen(s)*3 + 1);
2862 for (f = s, t = r; *f; f++) {
2864 /* Escape everything that is not a-zA-Z0-9. We also
2865 * escape 0-9 if it's the first character */
2867 if (!(*f >= 'A' && *f <= 'Z') &&
2868 !(*f >= 'a' && *f <= 'z') &&
2869 !(f > s && *f >= '0' && *f <= '9')) {
2871 *(t++) = hexchar(*f >> 4);
2872 *(t++) = hexchar(*f);
2882 _public_ char *sd_bus_label_unescape(const char *f) {
2885 assert_return(f, NULL);
2887 /* Special case for the empty string */
2891 r = new(char, strlen(f) + 1);
2895 for (t = r; *f; f++) {
2900 if ((a = unhexchar(f[1])) < 0 ||
2901 (b = unhexchar(f[2])) < 0) {
2902 /* Invalid escape code, let's take it literal then */
2905 *(t++) = (char) ((a << 4) | b);
2917 _public_ int sd_bus_get_peer_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
2922 assert_return(bus, -EINVAL);
2923 assert_return(mask <= _SD_BUS_CREDS_ALL, -ENOTSUP);
2924 assert_return(ret, -EINVAL);
2925 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2926 assert_return(!bus_pid_changed(bus), -ECHILD);
2927 assert_return(!bus->is_kernel, -ENOTSUP);
2929 if (!bus->ucred_valid && !isempty(bus->label))
2932 c = bus_creds_new();
2936 if (bus->ucred_valid) {
2937 pid = c->pid = bus->ucred.pid;
2938 c->uid = bus->ucred.uid;
2939 c->gid = bus->ucred.gid;
2941 c->mask |= (SD_BUS_CREDS_UID | SD_BUS_CREDS_PID | SD_BUS_CREDS_GID) & mask;
2944 if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) {
2945 c->label = strdup(bus->label);
2947 sd_bus_creds_unref(c);
2951 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
2954 r = bus_creds_add_more(c, mask, pid, 0);