]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/libsystemd-bus/sd-bus.c
core/service: check if mainpid matches only if it is set
[thirdparty/systemd.git] / src / libsystemd-bus / sd-bus.c
... / ...
CommitLineData
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2013 Lennart Poettering
7
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.
12
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.
17
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/>.
20***/
21
22#include <endian.h>
23#include <assert.h>
24#include <stdlib.h>
25#include <unistd.h>
26#include <netdb.h>
27#include <sys/poll.h>
28#include <byteswap.h>
29#include <sys/mman.h>
30#include <pthread.h>
31
32#include "util.h"
33#include "macro.h"
34#include "strv.h"
35#include "set.h"
36#include "missing.h"
37
38#include "sd-bus.h"
39#include "bus-internal.h"
40#include "bus-message.h"
41#include "bus-type.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"
48#include "bus-util.h"
49#include "bus-container.h"
50#include "bus-protocol.h"
51
52static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec);
53static int attach_io_events(sd_bus *b);
54static void detach_io_events(sd_bus *b);
55
56static void bus_close_fds(sd_bus *b) {
57 assert(b);
58
59 detach_io_events(b);
60
61 if (b->input_fd >= 0)
62 close_nointr_nofail(b->input_fd);
63
64 if (b->output_fd >= 0 && b->output_fd != b->input_fd)
65 close_nointr_nofail(b->output_fd);
66
67 b->input_fd = b->output_fd = -1;
68}
69
70static 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;
74
75 assert(b);
76
77 if (!n)
78 return;
79
80 while (n->child)
81 bus_node_destroy(b, n->child);
82
83 while ((c = n->callbacks)) {
84 LIST_REMOVE(callbacks, n->callbacks, c);
85 free(c);
86 }
87
88 while ((v = n->vtables)) {
89 LIST_REMOVE(vtables, n->vtables, v);
90 free(v->interface);
91 free(v);
92 }
93
94 while ((e = n->enumerators)) {
95 LIST_REMOVE(enumerators, n->enumerators, e);
96 free(e);
97 }
98
99 if (n->parent)
100 LIST_REMOVE(siblings, n->parent->child, n);
101
102 assert_se(hashmap_remove(b->nodes, n->path) == n);
103 free(n->path);
104 free(n);
105}
106
107static void bus_reset_queues(sd_bus *b) {
108 unsigned i;
109
110 assert(b);
111
112 for (i = 0; i < b->rqueue_size; i++)
113 sd_bus_message_unref(b->rqueue[i]);
114 free(b->rqueue);
115
116 for (i = 0; i < b->wqueue_size; i++)
117 sd_bus_message_unref(b->wqueue[i]);
118 free(b->wqueue);
119
120 b->rqueue = b->wqueue = NULL;
121 b->rqueue_allocated = b->wqueue_allocated = 0;
122 b->rqueue_size = b->wqueue_size = 0;
123}
124
125static void bus_free(sd_bus *b) {
126 struct filter_callback *f;
127 struct node *n;
128
129 assert(b);
130
131 sd_bus_detach_event(b);
132
133 bus_close_fds(b);
134
135 if (b->kdbus_buffer)
136 munmap(b->kdbus_buffer, KDBUS_POOL_SIZE);
137
138 free(b->rbuffer);
139 free(b->unique_name);
140 free(b->auth_buffer);
141 free(b->address);
142 free(b->kernel);
143 free(b->machine);
144 free(b->fake_label);
145 free(b->cgroup_root);
146
147 free(b->exec_path);
148 strv_free(b->exec_argv);
149
150 close_many(b->fds, b->n_fds);
151 free(b->fds);
152
153 bus_reset_queues(b);
154
155 hashmap_free_free(b->reply_callbacks);
156 prioq_free(b->reply_callbacks_prioq);
157
158 while ((f = b->filter_callbacks)) {
159 LIST_REMOVE(callbacks, b->filter_callbacks, f);
160 free(f);
161 }
162
163 bus_match_free(&b->match_callbacks);
164
165 hashmap_free_free(b->vtable_methods);
166 hashmap_free_free(b->vtable_properties);
167
168 while ((n = hashmap_first(b->nodes)))
169 bus_node_destroy(b, n);
170
171 hashmap_free(b->nodes);
172
173 bus_kernel_flush_memfd(b);
174
175 assert_se(pthread_mutex_destroy(&b->memfd_cache_mutex) == 0);
176
177 free(b);
178}
179
180_public_ int sd_bus_new(sd_bus **ret) {
181 sd_bus *r;
182
183 assert_return(ret, -EINVAL);
184
185 r = new0(sd_bus, 1);
186 if (!r)
187 return -ENOMEM;
188
189 r->n_ref = REFCNT_INIT;
190 r->input_fd = r->output_fd = -1;
191 r->message_version = 1;
192 r->creds_mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
193 r->hello_flags |= KDBUS_HELLO_ACCEPT_FD;
194 r->attach_flags |= KDBUS_ATTACH_NAMES;
195 r->original_pid = getpid();
196
197 assert_se(pthread_mutex_init(&r->memfd_cache_mutex, NULL) == 0);
198
199 /* We guarantee that wqueue always has space for at least one
200 * entry */
201 if (!GREEDY_REALLOC(r->wqueue, r->wqueue_allocated, 1)) {
202 free(r);
203 return -ENOMEM;
204 }
205
206 *ret = r;
207 return 0;
208}
209
210_public_ int sd_bus_set_address(sd_bus *bus, const char *address) {
211 char *a;
212
213 assert_return(bus, -EINVAL);
214 assert_return(bus->state == BUS_UNSET, -EPERM);
215 assert_return(address, -EINVAL);
216 assert_return(!bus_pid_changed(bus), -ECHILD);
217
218 a = strdup(address);
219 if (!a)
220 return -ENOMEM;
221
222 free(bus->address);
223 bus->address = a;
224
225 return 0;
226}
227
228_public_ int sd_bus_set_fd(sd_bus *bus, int input_fd, int output_fd) {
229 assert_return(bus, -EINVAL);
230 assert_return(bus->state == BUS_UNSET, -EPERM);
231 assert_return(input_fd >= 0, -EINVAL);
232 assert_return(output_fd >= 0, -EINVAL);
233 assert_return(!bus_pid_changed(bus), -ECHILD);
234
235 bus->input_fd = input_fd;
236 bus->output_fd = output_fd;
237 return 0;
238}
239
240_public_ int sd_bus_set_exec(sd_bus *bus, const char *path, char *const argv[]) {
241 char *p, **a;
242
243 assert_return(bus, -EINVAL);
244 assert_return(bus->state == BUS_UNSET, -EPERM);
245 assert_return(path, -EINVAL);
246 assert_return(!strv_isempty(argv), -EINVAL);
247 assert_return(!bus_pid_changed(bus), -ECHILD);
248
249 p = strdup(path);
250 if (!p)
251 return -ENOMEM;
252
253 a = strv_copy(argv);
254 if (!a) {
255 free(p);
256 return -ENOMEM;
257 }
258
259 free(bus->exec_path);
260 strv_free(bus->exec_argv);
261
262 bus->exec_path = p;
263 bus->exec_argv = a;
264
265 return 0;
266}
267
268_public_ int sd_bus_set_bus_client(sd_bus *bus, int b) {
269 assert_return(bus, -EINVAL);
270 assert_return(bus->state == BUS_UNSET, -EPERM);
271 assert_return(!bus_pid_changed(bus), -ECHILD);
272
273 bus->bus_client = !!b;
274 return 0;
275}
276
277_public_ int sd_bus_negotiate_fds(sd_bus *bus, int b) {
278 assert_return(bus, -EINVAL);
279 assert_return(bus->state == BUS_UNSET, -EPERM);
280 assert_return(!bus_pid_changed(bus), -ECHILD);
281
282 SET_FLAG(bus->hello_flags, KDBUS_HELLO_ACCEPT_FD, b);
283 return 0;
284}
285
286_public_ int sd_bus_negotiate_attach_timestamp(sd_bus *bus, int b) {
287 assert_return(bus, -EINVAL);
288 assert_return(bus->state == BUS_UNSET, -EPERM);
289 assert_return(!bus_pid_changed(bus), -ECHILD);
290
291 SET_FLAG(bus->attach_flags, KDBUS_ATTACH_TIMESTAMP, b);
292 return 0;
293}
294
295_public_ int sd_bus_negotiate_attach_creds(sd_bus *bus, uint64_t mask) {
296 assert_return(bus, -EINVAL);
297 assert_return(mask <= _SD_BUS_CREDS_ALL, -EINVAL);
298 assert_return(bus->state == BUS_UNSET, -EPERM);
299 assert_return(!bus_pid_changed(bus), -ECHILD);
300
301 /* The well knowns we need unconditionally, so that matches can work */
302 bus->creds_mask = mask | SD_BUS_CREDS_WELL_KNOWN_NAMES|SD_BUS_CREDS_UNIQUE_NAME;
303
304 return kdbus_translate_attach_flags(bus->creds_mask, &bus->creds_mask);
305}
306
307_public_ int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server_id) {
308 assert_return(bus, -EINVAL);
309 assert_return(b || sd_id128_equal(server_id, SD_ID128_NULL), -EINVAL);
310 assert_return(bus->state == BUS_UNSET, -EPERM);
311 assert_return(!bus_pid_changed(bus), -ECHILD);
312
313 bus->is_server = !!b;
314 bus->server_id = server_id;
315 return 0;
316}
317
318_public_ int sd_bus_set_anonymous(sd_bus *bus, int b) {
319 assert_return(bus, -EINVAL);
320 assert_return(bus->state == BUS_UNSET, -EPERM);
321 assert_return(!bus_pid_changed(bus), -ECHILD);
322
323 bus->anonymous_auth = !!b;
324 return 0;
325}
326
327_public_ int sd_bus_set_trusted(sd_bus *bus, int b) {
328 assert_return(bus, -EINVAL);
329 assert_return(bus->state == BUS_UNSET, -EPERM);
330 assert_return(!bus_pid_changed(bus), -ECHILD);
331
332 bus->trusted = !!b;
333 return 0;
334}
335
336static int hello_callback(sd_bus *bus, sd_bus_message *reply, void *userdata, sd_bus_error *error) {
337 const char *s;
338 int r;
339
340 assert(bus);
341 assert(bus->state == BUS_HELLO || bus->state == BUS_CLOSING);
342 assert(reply);
343
344 r = sd_bus_message_get_errno(reply);
345 if (r < 0)
346 return r;
347 if (r > 0)
348 return -r;
349
350 r = sd_bus_message_read(reply, "s", &s);
351 if (r < 0)
352 return r;
353
354 if (!service_name_is_valid(s) || s[0] != ':')
355 return -EBADMSG;
356
357 bus->unique_name = strdup(s);
358 if (!bus->unique_name)
359 return -ENOMEM;
360
361 if (bus->state == BUS_HELLO)
362 bus->state = BUS_RUNNING;
363
364 return 1;
365}
366
367static int bus_send_hello(sd_bus *bus) {
368 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
369 int r;
370
371 assert(bus);
372
373 if (!bus->bus_client || bus->is_kernel)
374 return 0;
375
376 r = sd_bus_message_new_method_call(
377 bus,
378 "org.freedesktop.DBus",
379 "/org/freedesktop/DBus",
380 "org.freedesktop.DBus",
381 "Hello",
382 &m);
383 if (r < 0)
384 return r;
385
386 return sd_bus_call_async(bus, m, hello_callback, NULL, 0, &bus->hello_cookie);
387}
388
389int bus_start_running(sd_bus *bus) {
390 assert(bus);
391
392 if (bus->bus_client && !bus->is_kernel) {
393 bus->state = BUS_HELLO;
394 return 1;
395 }
396
397 bus->state = BUS_RUNNING;
398 return 1;
399}
400
401static int parse_address_key(const char **p, const char *key, char **value) {
402 size_t l, n = 0, allocated = 0;
403 const char *a;
404 char *r = NULL;
405
406 assert(p);
407 assert(*p);
408 assert(value);
409
410 if (key) {
411 l = strlen(key);
412 if (strncmp(*p, key, l) != 0)
413 return 0;
414
415 if ((*p)[l] != '=')
416 return 0;
417
418 if (*value)
419 return -EINVAL;
420
421 a = *p + l + 1;
422 } else
423 a = *p;
424
425 while (*a != ';' && *a != ',' && *a != 0) {
426 char c;
427
428 if (*a == '%') {
429 int x, y;
430
431 x = unhexchar(a[1]);
432 if (x < 0) {
433 free(r);
434 return x;
435 }
436
437 y = unhexchar(a[2]);
438 if (y < 0) {
439 free(r);
440 return y;
441 }
442
443 c = (char) ((x << 4) | y);
444 a += 3;
445 } else {
446 c = *a;
447 a++;
448 }
449
450 if (!GREEDY_REALLOC(r, allocated, n + 2))
451 return -ENOMEM;
452
453 r[n++] = c;
454 }
455
456 if (!r) {
457 r = strdup("");
458 if (!r)
459 return -ENOMEM;
460 } else
461 r[n] = 0;
462
463 if (*a == ',')
464 a++;
465
466 *p = a;
467
468 free(*value);
469 *value = r;
470
471 return 1;
472}
473
474static void skip_address_key(const char **p) {
475 assert(p);
476 assert(*p);
477
478 *p += strcspn(*p, ",");
479
480 if (**p == ',')
481 (*p) ++;
482}
483
484static int parse_unix_address(sd_bus *b, const char **p, char **guid) {
485 _cleanup_free_ char *path = NULL, *abstract = NULL;
486 size_t l;
487 int r;
488
489 assert(b);
490 assert(p);
491 assert(*p);
492 assert(guid);
493
494 while (**p != 0 && **p != ';') {
495 r = parse_address_key(p, "guid", guid);
496 if (r < 0)
497 return r;
498 else if (r > 0)
499 continue;
500
501 r = parse_address_key(p, "path", &path);
502 if (r < 0)
503 return r;
504 else if (r > 0)
505 continue;
506
507 r = parse_address_key(p, "abstract", &abstract);
508 if (r < 0)
509 return r;
510 else if (r > 0)
511 continue;
512
513 skip_address_key(p);
514 }
515
516 if (!path && !abstract)
517 return -EINVAL;
518
519 if (path && abstract)
520 return -EINVAL;
521
522 if (path) {
523 l = strlen(path);
524 if (l > sizeof(b->sockaddr.un.sun_path))
525 return -E2BIG;
526
527 b->sockaddr.un.sun_family = AF_UNIX;
528 strncpy(b->sockaddr.un.sun_path, path, sizeof(b->sockaddr.un.sun_path));
529 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + l;
530 } else if (abstract) {
531 l = strlen(abstract);
532 if (l > sizeof(b->sockaddr.un.sun_path) - 1)
533 return -E2BIG;
534
535 b->sockaddr.un.sun_family = AF_UNIX;
536 b->sockaddr.un.sun_path[0] = 0;
537 strncpy(b->sockaddr.un.sun_path+1, abstract, sizeof(b->sockaddr.un.sun_path)-1);
538 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
539 }
540
541 return 0;
542}
543
544static int parse_tcp_address(sd_bus *b, const char **p, char **guid) {
545 _cleanup_free_ char *host = NULL, *port = NULL, *family = NULL;
546 int r;
547 struct addrinfo *result, hints = {
548 .ai_socktype = SOCK_STREAM,
549 .ai_flags = AI_ADDRCONFIG,
550 };
551
552 assert(b);
553 assert(p);
554 assert(*p);
555 assert(guid);
556
557 while (**p != 0 && **p != ';') {
558 r = parse_address_key(p, "guid", guid);
559 if (r < 0)
560 return r;
561 else if (r > 0)
562 continue;
563
564 r = parse_address_key(p, "host", &host);
565 if (r < 0)
566 return r;
567 else if (r > 0)
568 continue;
569
570 r = parse_address_key(p, "port", &port);
571 if (r < 0)
572 return r;
573 else if (r > 0)
574 continue;
575
576 r = parse_address_key(p, "family", &family);
577 if (r < 0)
578 return r;
579 else if (r > 0)
580 continue;
581
582 skip_address_key(p);
583 }
584
585 if (!host || !port)
586 return -EINVAL;
587
588 if (family) {
589 if (streq(family, "ipv4"))
590 hints.ai_family = AF_INET;
591 else if (streq(family, "ipv6"))
592 hints.ai_family = AF_INET6;
593 else
594 return -EINVAL;
595 }
596
597 r = getaddrinfo(host, port, &hints, &result);
598 if (r == EAI_SYSTEM)
599 return -errno;
600 else if (r != 0)
601 return -EADDRNOTAVAIL;
602
603 memcpy(&b->sockaddr, result->ai_addr, result->ai_addrlen);
604 b->sockaddr_size = result->ai_addrlen;
605
606 freeaddrinfo(result);
607
608 return 0;
609}
610
611static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
612 char *path = NULL;
613 unsigned n_argv = 0, j;
614 char **argv = NULL;
615 size_t allocated = 0;
616 int r;
617
618 assert(b);
619 assert(p);
620 assert(*p);
621 assert(guid);
622
623 while (**p != 0 && **p != ';') {
624 r = parse_address_key(p, "guid", guid);
625 if (r < 0)
626 goto fail;
627 else if (r > 0)
628 continue;
629
630 r = parse_address_key(p, "path", &path);
631 if (r < 0)
632 goto fail;
633 else if (r > 0)
634 continue;
635
636 if (startswith(*p, "argv")) {
637 unsigned ul;
638
639 errno = 0;
640 ul = strtoul(*p + 4, (char**) p, 10);
641 if (errno > 0 || **p != '=' || ul > 256) {
642 r = -EINVAL;
643 goto fail;
644 }
645
646 (*p) ++;
647
648 if (ul >= n_argv) {
649 if (!GREEDY_REALLOC0(argv, allocated, ul + 2)) {
650 r = -ENOMEM;
651 goto fail;
652 }
653
654 n_argv = ul + 1;
655 }
656
657 r = parse_address_key(p, NULL, argv + ul);
658 if (r < 0)
659 goto fail;
660
661 continue;
662 }
663
664 skip_address_key(p);
665 }
666
667 if (!path) {
668 r = -EINVAL;
669 goto fail;
670 }
671
672 /* Make sure there are no holes in the array, with the
673 * exception of argv[0] */
674 for (j = 1; j < n_argv; j++)
675 if (!argv[j]) {
676 r = -EINVAL;
677 goto fail;
678 }
679
680 if (argv && argv[0] == NULL) {
681 argv[0] = strdup(path);
682 if (!argv[0]) {
683 r = -ENOMEM;
684 goto fail;
685 }
686 }
687
688 b->exec_path = path;
689 b->exec_argv = argv;
690 return 0;
691
692fail:
693 for (j = 0; j < n_argv; j++)
694 free(argv[j]);
695
696 free(argv);
697 free(path);
698 return r;
699}
700
701static int parse_kernel_address(sd_bus *b, const char **p, char **guid) {
702 _cleanup_free_ char *path = NULL;
703 int r;
704
705 assert(b);
706 assert(p);
707 assert(*p);
708 assert(guid);
709
710 while (**p != 0 && **p != ';') {
711 r = parse_address_key(p, "guid", guid);
712 if (r < 0)
713 return r;
714 else if (r > 0)
715 continue;
716
717 r = parse_address_key(p, "path", &path);
718 if (r < 0)
719 return r;
720 else if (r > 0)
721 continue;
722
723 skip_address_key(p);
724 }
725
726 if (!path)
727 return -EINVAL;
728
729 free(b->kernel);
730 b->kernel = path;
731 path = NULL;
732
733 return 0;
734}
735
736static int parse_container_unix_address(sd_bus *b, const char **p, char **guid) {
737 _cleanup_free_ char *machine = NULL;
738 int r;
739
740 assert(b);
741 assert(p);
742 assert(*p);
743 assert(guid);
744
745 while (**p != 0 && **p != ';') {
746 r = parse_address_key(p, "guid", guid);
747 if (r < 0)
748 return r;
749 else if (r > 0)
750 continue;
751
752 r = parse_address_key(p, "machine", &machine);
753 if (r < 0)
754 return r;
755 else if (r > 0)
756 continue;
757
758 skip_address_key(p);
759 }
760
761 if (!machine)
762 return -EINVAL;
763
764 if (!filename_is_safe(machine))
765 return -EINVAL;
766
767 free(b->machine);
768 b->machine = machine;
769 machine = NULL;
770
771 b->sockaddr.un.sun_family = AF_UNIX;
772 strncpy(b->sockaddr.un.sun_path, "/var/run/dbus/system_bus_socket", sizeof(b->sockaddr.un.sun_path));
773 b->sockaddr_size = offsetof(struct sockaddr_un, sun_path) + sizeof("/var/run/dbus/system_bus_socket") - 1;
774
775 return 0;
776}
777
778static int parse_container_kernel_address(sd_bus *b, const char **p, char **guid) {
779 _cleanup_free_ char *machine = NULL;
780 int r;
781
782 assert(b);
783 assert(p);
784 assert(*p);
785 assert(guid);
786
787 while (**p != 0 && **p != ';') {
788 r = parse_address_key(p, "guid", guid);
789 if (r < 0)
790 return r;
791 else if (r > 0)
792 continue;
793
794 r = parse_address_key(p, "machine", &machine);
795 if (r < 0)
796 return r;
797 else if (r > 0)
798 continue;
799
800 skip_address_key(p);
801 }
802
803 if (!machine)
804 return -EINVAL;
805
806 if (!filename_is_safe(machine))
807 return -EINVAL;
808
809 free(b->machine);
810 b->machine = machine;
811 machine = NULL;
812
813 free(b->kernel);
814 b->kernel = strdup("/dev/kdbus/0-system/bus");
815 if (!b->kernel)
816 return -ENOMEM;
817
818 return 0;
819}
820
821static void bus_reset_parsed_address(sd_bus *b) {
822 assert(b);
823
824 zero(b->sockaddr);
825 b->sockaddr_size = 0;
826 strv_free(b->exec_argv);
827 free(b->exec_path);
828 b->exec_path = NULL;
829 b->exec_argv = NULL;
830 b->server_id = SD_ID128_NULL;
831 free(b->kernel);
832 b->kernel = NULL;
833 free(b->machine);
834 b->machine = NULL;
835}
836
837static int bus_parse_next_address(sd_bus *b) {
838 _cleanup_free_ char *guid = NULL;
839 const char *a;
840 int r;
841
842 assert(b);
843
844 if (!b->address)
845 return 0;
846 if (b->address[b->address_index] == 0)
847 return 0;
848
849 bus_reset_parsed_address(b);
850
851 a = b->address + b->address_index;
852
853 while (*a != 0) {
854
855 if (*a == ';') {
856 a++;
857 continue;
858 }
859
860 if (startswith(a, "unix:")) {
861 a += 5;
862
863 r = parse_unix_address(b, &a, &guid);
864 if (r < 0)
865 return r;
866 break;
867
868 } else if (startswith(a, "tcp:")) {
869
870 a += 4;
871 r = parse_tcp_address(b, &a, &guid);
872 if (r < 0)
873 return r;
874
875 break;
876
877 } else if (startswith(a, "unixexec:")) {
878
879 a += 9;
880 r = parse_exec_address(b, &a, &guid);
881 if (r < 0)
882 return r;
883
884 break;
885
886 } else if (startswith(a, "kernel:")) {
887
888 a += 7;
889 r = parse_kernel_address(b, &a, &guid);
890 if (r < 0)
891 return r;
892
893 break;
894 } else if (startswith(a, "x-container-unix:")) {
895
896 a += 17;
897 r = parse_container_unix_address(b, &a, &guid);
898 if (r < 0)
899 return r;
900
901 break;
902 } else if (startswith(a, "x-container-kernel:")) {
903
904 a += 19;
905 r = parse_container_kernel_address(b, &a, &guid);
906 if (r < 0)
907 return r;
908
909 break;
910 }
911
912 a = strchr(a, ';');
913 if (!a)
914 return 0;
915 }
916
917 if (guid) {
918 r = sd_id128_from_string(guid, &b->server_id);
919 if (r < 0)
920 return r;
921 }
922
923 b->address_index = a - b->address;
924 return 1;
925}
926
927static int bus_start_address(sd_bus *b) {
928 int r;
929
930 assert(b);
931
932 for (;;) {
933 bool skipped = false;
934
935 bus_close_fds(b);
936
937 if (b->exec_path)
938 r = bus_socket_exec(b);
939 else if (b->machine && b->kernel)
940 r = bus_container_connect_kernel(b);
941 else if (b->machine && b->sockaddr.sa.sa_family != AF_UNSPEC)
942 r = bus_container_connect_socket(b);
943 else if (b->kernel)
944 r = bus_kernel_connect(b);
945 else if (b->sockaddr.sa.sa_family != AF_UNSPEC)
946 r = bus_socket_connect(b);
947 else
948 skipped = true;
949
950 if (!skipped) {
951 if (r >= 0) {
952 r = attach_io_events(b);
953 if (r >= 0)
954 return r;
955 }
956
957 b->last_connect_error = -r;
958 }
959
960 r = bus_parse_next_address(b);
961 if (r < 0)
962 return r;
963 if (r == 0)
964 return b->last_connect_error ? -b->last_connect_error : -ECONNREFUSED;
965 }
966}
967
968int bus_next_address(sd_bus *b) {
969 assert(b);
970
971 bus_reset_parsed_address(b);
972 return bus_start_address(b);
973}
974
975static int bus_start_fd(sd_bus *b) {
976 struct stat st;
977 int r;
978
979 assert(b);
980 assert(b->input_fd >= 0);
981 assert(b->output_fd >= 0);
982
983 r = fd_nonblock(b->input_fd, true);
984 if (r < 0)
985 return r;
986
987 r = fd_cloexec(b->input_fd, true);
988 if (r < 0)
989 return r;
990
991 if (b->input_fd != b->output_fd) {
992 r = fd_nonblock(b->output_fd, true);
993 if (r < 0)
994 return r;
995
996 r = fd_cloexec(b->output_fd, true);
997 if (r < 0)
998 return r;
999 }
1000
1001 if (fstat(b->input_fd, &st) < 0)
1002 return -errno;
1003
1004 if (S_ISCHR(b->input_fd))
1005 return bus_kernel_take_fd(b);
1006 else
1007 return bus_socket_take_fd(b);
1008}
1009
1010_public_ int sd_bus_start(sd_bus *bus) {
1011 int r;
1012
1013 assert_return(bus, -EINVAL);
1014 assert_return(bus->state == BUS_UNSET, -EPERM);
1015 assert_return(!bus_pid_changed(bus), -ECHILD);
1016
1017 bus->state = BUS_OPENING;
1018
1019 if (bus->is_server && bus->bus_client)
1020 return -EINVAL;
1021
1022 if (bus->input_fd >= 0)
1023 r = bus_start_fd(bus);
1024 else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path || bus->kernel || bus->machine)
1025 r = bus_start_address(bus);
1026 else
1027 return -EINVAL;
1028
1029 if (r < 0)
1030 return r;
1031
1032 return bus_send_hello(bus);
1033}
1034
1035_public_ int sd_bus_open_system(sd_bus **ret) {
1036 const char *e;
1037 sd_bus *b;
1038 int r;
1039
1040 assert_return(ret, -EINVAL);
1041
1042 r = sd_bus_new(&b);
1043 if (r < 0)
1044 return r;
1045
1046 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1047 if (e)
1048 r = sd_bus_set_address(b, e);
1049 else
1050#ifdef ENABLE_KDBUS
1051 r = sd_bus_set_address(b, "kernel:path=/dev/kdbus/0-system/bus;unix:path=/run/dbus/system_bus_socket");
1052#else
1053 r = sd_bus_set_address(b, "unix:path=/run/dbus/system_bus_socket");
1054#endif
1055
1056 if (r < 0)
1057 goto fail;
1058
1059 b->bus_client = true;
1060
1061 /* Let's do per-method access control on the system bus. We
1062 * need the caller's UID and capability set for that. */
1063 b->trusted = false;
1064 b->attach_flags |= KDBUS_ATTACH_CAPS | KDBUS_ATTACH_CREDS;
1065
1066 r = sd_bus_start(b);
1067 if (r < 0)
1068 goto fail;
1069
1070 *ret = b;
1071 return 0;
1072
1073fail:
1074 bus_free(b);
1075 return r;
1076}
1077
1078_public_ int sd_bus_open_user(sd_bus **ret) {
1079 const char *e;
1080 sd_bus *b;
1081 int r;
1082
1083 assert_return(ret, -EINVAL);
1084
1085 r = sd_bus_new(&b);
1086 if (r < 0)
1087 return r;
1088
1089 e = secure_getenv("DBUS_SESSION_BUS_ADDRESS");
1090 if (e) {
1091 r = sd_bus_set_address(b, e);
1092 if (r < 0)
1093 goto fail;
1094 } else {
1095 e = secure_getenv("XDG_RUNTIME_DIR");
1096 if (e) {
1097 _cleanup_free_ char *ee = NULL;
1098
1099 ee = bus_address_escape(e);
1100 if (!ee) {
1101 r = -ENOMEM;
1102 goto fail;
1103 }
1104
1105#ifdef ENABLE_KDBUS
1106 asprintf(&b->address, "kernel:path=/dev/kdbus/%lu-user/bus;unix:path=%s/bus", (unsigned long) getuid(), ee);
1107#else
1108 b->address = strjoin("unix:path=", ee, "/bus", NULL);
1109#endif
1110 } else {
1111#ifdef ENABLE_KDBUS
1112 asprintf(&b->address, "kernel:path=/dev/kdbus/%lu-user/bus", (unsigned long) getuid());
1113#else
1114 return -ECONNREFUSED;
1115#endif
1116 }
1117
1118 if (!b->address) {
1119 r = -ENOMEM;
1120 goto fail;
1121 }
1122 }
1123
1124 b->bus_client = true;
1125
1126 /* We don't do any per-method access control on the user
1127 * bus. */
1128 b->trusted = true;
1129
1130 r = sd_bus_start(b);
1131 if (r < 0)
1132 goto fail;
1133
1134 *ret = b;
1135 return 0;
1136
1137fail:
1138 bus_free(b);
1139 return r;
1140}
1141
1142_public_ int sd_bus_open_system_remote(const char *host, sd_bus **ret) {
1143 _cleanup_free_ char *e = NULL;
1144 char *p = NULL;
1145 sd_bus *bus;
1146 int r;
1147
1148 assert_return(host, -EINVAL);
1149 assert_return(ret, -EINVAL);
1150
1151 e = bus_address_escape(host);
1152 if (!e)
1153 return -ENOMEM;
1154
1155 p = strjoin("unixexec:path=ssh,argv1=-xT,argv2=", e, ",argv3=systemd-stdio-bridge", NULL);
1156 if (!p)
1157 return -ENOMEM;
1158
1159 r = sd_bus_new(&bus);
1160 if (r < 0) {
1161 free(p);
1162 return r;
1163 }
1164
1165 bus->address = p;
1166 bus->bus_client = true;
1167
1168 r = sd_bus_start(bus);
1169 if (r < 0) {
1170 bus_free(bus);
1171 return r;
1172 }
1173
1174 *ret = bus;
1175 return 0;
1176}
1177
1178_public_ int sd_bus_open_system_container(const char *machine, sd_bus **ret) {
1179 _cleanup_free_ char *e = NULL;
1180 sd_bus *bus;
1181 char *p;
1182 int r;
1183
1184 assert_return(machine, -EINVAL);
1185 assert_return(ret, -EINVAL);
1186 assert_return(filename_is_safe(machine), -EINVAL);
1187
1188 e = bus_address_escape(machine);
1189 if (!e)
1190 return -ENOMEM;
1191
1192#ifdef ENABLE_KDBUS
1193 p = strjoin("x-container-kernel:machine=", e, ";x-container-unix:machine=", e, NULL);
1194#else
1195 p = strjoin("x-container-unix:machine=", e, NULL);
1196#endif
1197 if (!p)
1198 return -ENOMEM;
1199
1200 r = sd_bus_new(&bus);
1201 if (r < 0) {
1202 free(p);
1203 return r;
1204 }
1205
1206 bus->address = p;
1207 bus->bus_client = true;
1208
1209 r = sd_bus_start(bus);
1210 if (r < 0) {
1211 bus_free(bus);
1212 return r;
1213 }
1214
1215 *ret = bus;
1216 return 0;
1217}
1218
1219_public_ void sd_bus_close(sd_bus *bus) {
1220
1221 if (!bus)
1222 return;
1223 if (bus->state == BUS_CLOSED)
1224 return;
1225 if (bus_pid_changed(bus))
1226 return;
1227
1228 bus->state = BUS_CLOSED;
1229
1230 sd_bus_detach_event(bus);
1231
1232 /* Drop all queued messages so that they drop references to
1233 * the bus object and the bus may be freed */
1234 bus_reset_queues(bus);
1235
1236 if (!bus->is_kernel)
1237 bus_close_fds(bus);
1238
1239 /* We'll leave the fd open in case this is a kernel bus, since
1240 * there might still be memblocks around that reference this
1241 * bus, and they might need to invoke the * KDBUS_CMD_FREE
1242 * ioctl on the fd when they are freed. */
1243}
1244
1245static void bus_enter_closing(sd_bus *bus) {
1246 assert(bus);
1247
1248 if (bus->state != BUS_OPENING &&
1249 bus->state != BUS_AUTHENTICATING &&
1250 bus->state != BUS_HELLO &&
1251 bus->state != BUS_RUNNING)
1252 return;
1253
1254 bus->state = BUS_CLOSING;
1255}
1256
1257_public_ sd_bus *sd_bus_ref(sd_bus *bus) {
1258 assert_return(bus, NULL);
1259
1260 assert_se(REFCNT_INC(bus->n_ref) >= 2);
1261
1262 return bus;
1263}
1264
1265_public_ sd_bus *sd_bus_unref(sd_bus *bus) {
1266
1267 if (!bus)
1268 return NULL;
1269
1270 if (REFCNT_DEC(bus->n_ref) <= 0)
1271 bus_free(bus);
1272
1273 return NULL;
1274}
1275
1276_public_ int sd_bus_is_open(sd_bus *bus) {
1277
1278 assert_return(bus, -EINVAL);
1279 assert_return(!bus_pid_changed(bus), -ECHILD);
1280
1281 return BUS_IS_OPEN(bus->state);
1282}
1283
1284_public_ int sd_bus_can_send(sd_bus *bus, char type) {
1285 int r;
1286
1287 assert_return(bus, -EINVAL);
1288 assert_return(bus->state != BUS_UNSET, -ENOTCONN);
1289 assert_return(!bus_pid_changed(bus), -ECHILD);
1290
1291 if (type == SD_BUS_TYPE_UNIX_FD) {
1292 if (!(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD))
1293 return 0;
1294
1295 r = bus_ensure_running(bus);
1296 if (r < 0)
1297 return r;
1298
1299 return bus->can_fds;
1300 }
1301
1302 return bus_type_is_valid(type);
1303}
1304
1305_public_ int sd_bus_get_server_id(sd_bus *bus, sd_id128_t *server_id) {
1306 int r;
1307
1308 assert_return(bus, -EINVAL);
1309 assert_return(server_id, -EINVAL);
1310 assert_return(!bus_pid_changed(bus), -ECHILD);
1311
1312 r = bus_ensure_running(bus);
1313 if (r < 0)
1314 return r;
1315
1316 *server_id = bus->server_id;
1317 return 0;
1318}
1319
1320static int bus_seal_message(sd_bus *b, sd_bus_message *m, usec_t timeout) {
1321 assert(b);
1322 assert(m);
1323
1324 if (m->sealed) {
1325 /* If we copy the same message to multiple
1326 * destinations, avoid using the same cookie
1327 * numbers. */
1328 b->cookie = MAX(b->cookie, BUS_MESSAGE_COOKIE(m));
1329 return 0;
1330 }
1331
1332 if (timeout == 0)
1333 timeout = BUS_DEFAULT_TIMEOUT;
1334
1335 return bus_message_seal(m, ++b->cookie, timeout);
1336}
1337
1338static int bus_remarshal_message(sd_bus *b, sd_bus_message **m) {
1339 assert(b);
1340
1341 /* Do packet version and endianess already match? */
1342 if ((b->message_version == 0 || b->message_version == (*m)->header->version) &&
1343 (b->message_endian == 0 || b->message_endian == (*m)->header->endian))
1344 return 0;
1345
1346 /* No? Then remarshal! */
1347 return bus_message_remarshal(b, m);
1348}
1349
1350int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m) {
1351 assert(b);
1352 assert(m);
1353
1354 /* The bus specification says the serial number cannot be 0,
1355 * hence let's fill something in for synthetic messages. Since
1356 * synthetic messages might have a fake sender and we don't
1357 * want to interfere with the real sender's serial numbers we
1358 * pick a fixed, artifical one. We use (uint32_t) -1 rather
1359 * than (uint64_t) -1 since dbus1 only had 32bit identifiers,
1360 * even though kdbus can do 64bit. */
1361
1362 return bus_message_seal(m, 0xFFFFFFFFULL, 0);
1363}
1364
1365static int bus_write_message(sd_bus *bus, sd_bus_message *m, size_t *idx) {
1366 int r;
1367
1368 assert(bus);
1369 assert(m);
1370
1371 if (bus->is_kernel)
1372 r = bus_kernel_write_message(bus, m);
1373 else
1374 r = bus_socket_write_message(bus, m, idx);
1375
1376 if (r <= 0)
1377 return r;
1378
1379 if (bus->is_kernel || *idx >= BUS_MESSAGE_SIZE(m))
1380 log_debug("Sent message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%lu reply_cookie=%lu error=%s",
1381 bus_message_type_to_string(m->header->type),
1382 strna(sd_bus_message_get_sender(m)),
1383 strna(sd_bus_message_get_destination(m)),
1384 strna(sd_bus_message_get_path(m)),
1385 strna(sd_bus_message_get_interface(m)),
1386 strna(sd_bus_message_get_member(m)),
1387 (unsigned long) BUS_MESSAGE_COOKIE(m),
1388 (unsigned long) m->reply_cookie,
1389 strna(m->error.message));
1390
1391 return r;
1392}
1393
1394static int dispatch_wqueue(sd_bus *bus) {
1395 int r, ret = 0;
1396
1397 assert(bus);
1398 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1399
1400 while (bus->wqueue_size > 0) {
1401
1402 r = bus_write_message(bus, bus->wqueue[0], &bus->windex);
1403 if (r < 0)
1404 return r;
1405 else if (r == 0)
1406 /* Didn't do anything this time */
1407 return ret;
1408 else if (bus->is_kernel || bus->windex >= BUS_MESSAGE_SIZE(bus->wqueue[0])) {
1409 /* Fully written. Let's drop the entry from
1410 * the queue.
1411 *
1412 * This isn't particularly optimized, but
1413 * well, this is supposed to be our worst-case
1414 * buffer only, and the socket buffer is
1415 * supposed to be our primary buffer, and if
1416 * it got full, then all bets are off
1417 * anyway. */
1418
1419 sd_bus_message_unref(bus->wqueue[0]);
1420 bus->wqueue_size --;
1421 memmove(bus->wqueue, bus->wqueue + 1, sizeof(sd_bus_message*) * bus->wqueue_size);
1422 bus->windex = 0;
1423
1424 ret = 1;
1425 }
1426 }
1427
1428 return ret;
1429}
1430
1431static int bus_read_message(sd_bus *bus) {
1432 assert(bus);
1433
1434 if (bus->is_kernel)
1435 return bus_kernel_read_message(bus);
1436 else
1437 return bus_socket_read_message(bus);
1438}
1439
1440int bus_rqueue_make_room(sd_bus *bus) {
1441 assert(bus);
1442
1443 if (bus->rqueue_size >= BUS_RQUEUE_MAX)
1444 return -ENOBUFS;
1445
1446 if (!GREEDY_REALLOC(bus->rqueue, bus->rqueue_allocated, bus->rqueue_size + 1))
1447 return -ENOMEM;
1448
1449 return 0;
1450}
1451
1452static int dispatch_rqueue(sd_bus *bus, sd_bus_message **m) {
1453 int r, ret = 0;
1454
1455 assert(bus);
1456 assert(m);
1457 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
1458
1459 for (;;) {
1460 if (bus->rqueue_size > 0) {
1461 /* Dispatch a queued message */
1462
1463 *m = bus->rqueue[0];
1464 bus->rqueue_size --;
1465 memmove(bus->rqueue, bus->rqueue + 1, sizeof(sd_bus_message*) * bus->rqueue_size);
1466 return 1;
1467 }
1468
1469 /* Try to read a new message */
1470 r = bus_read_message(bus);
1471 if (r < 0)
1472 return r;
1473 if (r == 0)
1474 return ret;
1475
1476 ret = 1;
1477 }
1478}
1479
1480_public_ int sd_bus_send(sd_bus *bus, sd_bus_message *_m, uint64_t *cookie) {
1481 _cleanup_bus_message_unref_ sd_bus_message *m = sd_bus_message_ref(_m);
1482 int r;
1483
1484 assert_return(bus, -EINVAL);
1485 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1486 assert_return(m, -EINVAL);
1487 assert_return(!bus_pid_changed(bus), -ECHILD);
1488
1489 if (m->n_fds > 0) {
1490 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
1491 if (r < 0)
1492 return r;
1493 if (r == 0)
1494 return -ENOTSUP;
1495 }
1496
1497 /* If the cookie number isn't kept, then we know that no reply
1498 * is expected */
1499 if (!cookie && !m->sealed)
1500 m->header->flags |= BUS_MESSAGE_NO_REPLY_EXPECTED;
1501
1502 r = bus_seal_message(bus, m, 0);
1503 if (r < 0)
1504 return r;
1505
1506 /* Remarshall if we have to. This will possible unref the
1507 * message and place a replacement in m */
1508 r = bus_remarshal_message(bus, &m);
1509 if (r < 0)
1510 return r;
1511
1512 /* If this is a reply and no reply was requested, then let's
1513 * suppress this, if we can */
1514 if (m->dont_send && !cookie)
1515 return 1;
1516
1517 if ((bus->state == BUS_RUNNING || bus->state == BUS_HELLO) && bus->wqueue_size <= 0) {
1518 size_t idx = 0;
1519
1520 r = bus_write_message(bus, m, &idx);
1521 if (r < 0) {
1522 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1523 bus_enter_closing(bus);
1524
1525 return r;
1526 } else if (!bus->is_kernel && idx < BUS_MESSAGE_SIZE(m)) {
1527 /* Wasn't fully written. So let's remember how
1528 * much was written. Note that the first entry
1529 * of the wqueue array is always allocated so
1530 * that we always can remember how much was
1531 * written. */
1532 bus->wqueue[0] = sd_bus_message_ref(m);
1533 bus->wqueue_size = 1;
1534 bus->windex = idx;
1535 }
1536 } else {
1537 /* Just append it to the queue. */
1538
1539 if (bus->wqueue_size >= BUS_WQUEUE_MAX)
1540 return -ENOBUFS;
1541
1542 if (!GREEDY_REALLOC(bus->wqueue, bus->wqueue_allocated, bus->wqueue_size + 1))
1543 return -ENOMEM;
1544
1545 bus->wqueue[bus->wqueue_size ++] = sd_bus_message_ref(m);
1546 }
1547
1548 if (cookie)
1549 *cookie = BUS_MESSAGE_COOKIE(m);
1550
1551 return 1;
1552}
1553
1554_public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destination, uint64_t *cookie) {
1555 int r;
1556
1557 assert_return(bus, -EINVAL);
1558 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1559 assert_return(m, -EINVAL);
1560 assert_return(!bus_pid_changed(bus), -ECHILD);
1561
1562 if (!streq_ptr(m->destination, destination)) {
1563
1564 if (!destination)
1565 return -EEXIST;
1566
1567 r = sd_bus_message_set_destination(m, destination);
1568 if (r < 0)
1569 return r;
1570 }
1571
1572 return sd_bus_send(bus, m, cookie);
1573}
1574
1575static usec_t calc_elapse(uint64_t usec) {
1576 if (usec == (uint64_t) -1)
1577 return 0;
1578
1579 return now(CLOCK_MONOTONIC) + usec;
1580}
1581
1582static int timeout_compare(const void *a, const void *b) {
1583 const struct reply_callback *x = a, *y = b;
1584
1585 if (x->timeout != 0 && y->timeout == 0)
1586 return -1;
1587
1588 if (x->timeout == 0 && y->timeout != 0)
1589 return 1;
1590
1591 if (x->timeout < y->timeout)
1592 return -1;
1593
1594 if (x->timeout > y->timeout)
1595 return 1;
1596
1597 return 0;
1598}
1599
1600_public_ int sd_bus_call_async(
1601 sd_bus *bus,
1602 sd_bus_message *_m,
1603 sd_bus_message_handler_t callback,
1604 void *userdata,
1605 uint64_t usec,
1606 uint64_t *cookie) {
1607
1608 _cleanup_bus_message_unref_ sd_bus_message *m = sd_bus_message_ref(_m);
1609 struct reply_callback *c;
1610 int r;
1611
1612 assert_return(bus, -EINVAL);
1613 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1614 assert_return(m, -EINVAL);
1615 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1616 assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1617 assert_return(callback, -EINVAL);
1618 assert_return(!bus_pid_changed(bus), -ECHILD);
1619
1620 r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func);
1621 if (r < 0)
1622 return r;
1623
1624 r = prioq_ensure_allocated(&bus->reply_callbacks_prioq, timeout_compare);
1625 if (r < 0)
1626 return r;
1627
1628 r = bus_seal_message(bus, m, usec);
1629 if (r < 0)
1630 return r;
1631
1632 r = bus_remarshal_message(bus, &m);
1633 if (r < 0)
1634 return r;
1635
1636 c = new0(struct reply_callback, 1);
1637 if (!c)
1638 return -ENOMEM;
1639
1640 c->callback = callback;
1641 c->userdata = userdata;
1642 c->cookie = BUS_MESSAGE_COOKIE(m);
1643 c->timeout = calc_elapse(m->timeout);
1644
1645 r = hashmap_put(bus->reply_callbacks, &c->cookie, c);
1646 if (r < 0) {
1647 free(c);
1648 return r;
1649 }
1650
1651 if (c->timeout != 0) {
1652 r = prioq_put(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1653 if (r < 0) {
1654 c->timeout = 0;
1655 sd_bus_call_async_cancel(bus, c->cookie);
1656 return r;
1657 }
1658 }
1659
1660 r = sd_bus_send(bus, m, cookie);
1661 if (r < 0) {
1662 sd_bus_call_async_cancel(bus, c->cookie);
1663 return r;
1664 }
1665
1666 return r;
1667}
1668
1669_public_ int sd_bus_call_async_cancel(sd_bus *bus, uint64_t cookie) {
1670 struct reply_callback *c;
1671
1672 assert_return(bus, -EINVAL);
1673 assert_return(cookie != 0, -EINVAL);
1674 assert_return(!bus_pid_changed(bus), -ECHILD);
1675
1676 c = hashmap_remove(bus->reply_callbacks, &cookie);
1677 if (!c)
1678 return 0;
1679
1680 if (c->timeout != 0)
1681 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
1682
1683 free(c);
1684 return 1;
1685}
1686
1687int bus_ensure_running(sd_bus *bus) {
1688 int r;
1689
1690 assert(bus);
1691
1692 if (bus->state == BUS_UNSET || bus->state == BUS_CLOSED || bus->state == BUS_CLOSING)
1693 return -ENOTCONN;
1694 if (bus->state == BUS_RUNNING)
1695 return 1;
1696
1697 for (;;) {
1698 r = sd_bus_process(bus, NULL);
1699 if (r < 0)
1700 return r;
1701 if (bus->state == BUS_RUNNING)
1702 return 1;
1703 if (r > 0)
1704 continue;
1705
1706 r = sd_bus_wait(bus, (uint64_t) -1);
1707 if (r < 0)
1708 return r;
1709 }
1710}
1711
1712_public_ int sd_bus_call(
1713 sd_bus *bus,
1714 sd_bus_message *_m,
1715 uint64_t usec,
1716 sd_bus_error *error,
1717 sd_bus_message **reply) {
1718
1719 _cleanup_bus_message_unref_ sd_bus_message *m = sd_bus_message_ref(_m);
1720 usec_t timeout;
1721 uint64_t cookie;
1722 unsigned i;
1723 int r;
1724
1725 assert_return(bus, -EINVAL);
1726 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
1727 assert_return(m, -EINVAL);
1728 assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL);
1729 assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL);
1730 assert_return(!bus_error_is_dirty(error), -EINVAL);
1731 assert_return(!bus_pid_changed(bus), -ECHILD);
1732
1733 r = bus_ensure_running(bus);
1734 if (r < 0)
1735 return r;
1736
1737 i = bus->rqueue_size;
1738
1739 r = bus_seal_message(bus, m, usec);
1740 if (r < 0)
1741 return r;
1742
1743 r = bus_remarshal_message(bus, &m);
1744 if (r < 0)
1745 return r;
1746
1747 r = sd_bus_send(bus, m, &cookie);
1748 if (r < 0)
1749 return r;
1750
1751 timeout = calc_elapse(m->timeout);
1752
1753 for (;;) {
1754 usec_t left;
1755
1756 while (i < bus->rqueue_size) {
1757 sd_bus_message *incoming = NULL;
1758
1759 incoming = bus->rqueue[i];
1760
1761 if (incoming->reply_cookie == cookie) {
1762 /* Found a match! */
1763
1764 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1765 bus->rqueue_size--;
1766
1767 if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
1768
1769 if (reply)
1770 *reply = incoming;
1771 else
1772 sd_bus_message_unref(incoming);
1773
1774 return 1;
1775 } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR)
1776 r = sd_bus_error_copy(error, &incoming->error);
1777 else
1778 r = -EIO;
1779
1780 sd_bus_message_unref(incoming);
1781 return r;
1782
1783 } else if (BUS_MESSAGE_COOKIE(incoming) == cookie &&
1784 bus->unique_name &&
1785 incoming->sender &&
1786 streq(bus->unique_name, incoming->sender)) {
1787
1788 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
1789 bus->rqueue_size--;
1790
1791 /* Our own message? Somebody is trying
1792 * to send its own client a message,
1793 * let's not dead-lock, let's fail
1794 * immediately. */
1795
1796 sd_bus_message_unref(incoming);
1797 return -ELOOP;
1798 }
1799
1800 /* Try to read more, right-away */
1801 i++;
1802 }
1803
1804 r = bus_read_message(bus);
1805 if (r < 0) {
1806 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1807 bus_enter_closing(bus);
1808
1809 return r;
1810 }
1811 if (r > 0)
1812 continue;
1813
1814 if (timeout > 0) {
1815 usec_t n;
1816
1817 n = now(CLOCK_MONOTONIC);
1818 if (n >= timeout)
1819 return -ETIMEDOUT;
1820
1821 left = timeout - n;
1822 } else
1823 left = (uint64_t) -1;
1824
1825 r = bus_poll(bus, true, left);
1826 if (r < 0)
1827 return r;
1828 if (r == 0)
1829 return -ETIMEDOUT;
1830
1831 r = dispatch_wqueue(bus);
1832 if (r < 0) {
1833 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
1834 bus_enter_closing(bus);
1835
1836 return r;
1837 }
1838 }
1839}
1840
1841_public_ int sd_bus_get_fd(sd_bus *bus) {
1842
1843 assert_return(bus, -EINVAL);
1844 assert_return(bus->input_fd == bus->output_fd, -EPERM);
1845 assert_return(!bus_pid_changed(bus), -ECHILD);
1846
1847 return bus->input_fd;
1848}
1849
1850_public_ int sd_bus_get_events(sd_bus *bus) {
1851 int flags = 0;
1852
1853 assert_return(bus, -EINVAL);
1854 assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
1855 assert_return(!bus_pid_changed(bus), -ECHILD);
1856
1857 if (bus->state == BUS_OPENING)
1858 flags |= POLLOUT;
1859 else if (bus->state == BUS_AUTHENTICATING) {
1860
1861 if (bus_socket_auth_needs_write(bus))
1862 flags |= POLLOUT;
1863
1864 flags |= POLLIN;
1865
1866 } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
1867 if (bus->rqueue_size <= 0)
1868 flags |= POLLIN;
1869 if (bus->wqueue_size > 0)
1870 flags |= POLLOUT;
1871 }
1872
1873 return flags;
1874}
1875
1876_public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
1877 struct reply_callback *c;
1878
1879 assert_return(bus, -EINVAL);
1880 assert_return(timeout_usec, -EINVAL);
1881 assert_return(BUS_IS_OPEN(bus->state) || bus->state == BUS_CLOSING, -ENOTCONN);
1882 assert_return(!bus_pid_changed(bus), -ECHILD);
1883
1884 if (bus->state == BUS_CLOSING) {
1885 *timeout_usec = 0;
1886 return 1;
1887 }
1888
1889 if (bus->state == BUS_AUTHENTICATING) {
1890 *timeout_usec = bus->auth_timeout;
1891 return 1;
1892 }
1893
1894 if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) {
1895 *timeout_usec = (uint64_t) -1;
1896 return 0;
1897 }
1898
1899 if (bus->rqueue_size > 0) {
1900 *timeout_usec = 0;
1901 return 1;
1902 }
1903
1904 c = prioq_peek(bus->reply_callbacks_prioq);
1905 if (!c) {
1906 *timeout_usec = (uint64_t) -1;
1907 return 0;
1908 }
1909
1910 *timeout_usec = c->timeout;
1911 return 1;
1912}
1913
1914static int process_timeout(sd_bus *bus) {
1915 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1916 _cleanup_bus_message_unref_ sd_bus_message* m = NULL;
1917 struct reply_callback *c;
1918 usec_t n;
1919 int r;
1920
1921 assert(bus);
1922
1923 c = prioq_peek(bus->reply_callbacks_prioq);
1924 if (!c)
1925 return 0;
1926
1927 n = now(CLOCK_MONOTONIC);
1928 if (c->timeout > n)
1929 return 0;
1930
1931 r = bus_message_new_synthetic_error(
1932 bus,
1933 c->cookie,
1934 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
1935 &m);
1936 if (r < 0)
1937 return r;
1938
1939 m->sender = "org.freedesktop.DBus";
1940
1941 r = bus_seal_synthetic_message(bus, m);
1942 if (r < 0)
1943 return r;
1944
1945 assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
1946 hashmap_remove(bus->reply_callbacks, &c->cookie);
1947
1948 bus->current = m;
1949 bus->iteration_counter ++;
1950
1951 r = c->callback(bus, m, c->userdata, &error_buffer);
1952 r = bus_maybe_reply_error(m, r, &error_buffer);
1953 free(c);
1954
1955 bus->current = NULL;
1956
1957 return r;
1958}
1959
1960static int process_hello(sd_bus *bus, sd_bus_message *m) {
1961 assert(bus);
1962 assert(m);
1963
1964 if (bus->state != BUS_HELLO)
1965 return 0;
1966
1967 /* Let's make sure the first message on the bus is the HELLO
1968 * reply. But note that we don't actually parse the message
1969 * here (we leave that to the usual handling), we just verify
1970 * we don't let any earlier msg through. */
1971
1972 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
1973 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1974 return -EIO;
1975
1976 if (m->reply_cookie != bus->hello_cookie)
1977 return -EIO;
1978
1979 return 0;
1980}
1981
1982static int process_reply(sd_bus *bus, sd_bus_message *m) {
1983 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
1984 struct reply_callback *c;
1985 int r;
1986
1987 assert(bus);
1988 assert(m);
1989
1990 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
1991 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
1992 return 0;
1993
1994 c = hashmap_remove(bus->reply_callbacks, &m->reply_cookie);
1995 if (!c)
1996 return 0;
1997
1998 if (c->timeout != 0)
1999 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2000
2001 r = sd_bus_message_rewind(m, true);
2002 if (r < 0)
2003 return r;
2004
2005 r = c->callback(bus, m, c->userdata, &error_buffer);
2006 r = bus_maybe_reply_error(m, r, &error_buffer);
2007 free(c);
2008
2009 return r;
2010}
2011
2012static int process_filter(sd_bus *bus, sd_bus_message *m) {
2013 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2014 struct filter_callback *l;
2015 int r;
2016
2017 assert(bus);
2018 assert(m);
2019
2020 do {
2021 bus->filter_callbacks_modified = false;
2022
2023 LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
2024
2025 if (bus->filter_callbacks_modified)
2026 break;
2027
2028 /* Don't run this more than once per iteration */
2029 if (l->last_iteration == bus->iteration_counter)
2030 continue;
2031
2032 l->last_iteration = bus->iteration_counter;
2033
2034 r = sd_bus_message_rewind(m, true);
2035 if (r < 0)
2036 return r;
2037
2038 r = l->callback(bus, m, l->userdata, &error_buffer);
2039 r = bus_maybe_reply_error(m, r, &error_buffer);
2040 if (r != 0)
2041 return r;
2042
2043 }
2044
2045 } while (bus->filter_callbacks_modified);
2046
2047 return 0;
2048}
2049
2050static int process_match(sd_bus *bus, sd_bus_message *m) {
2051 int r;
2052
2053 assert(bus);
2054 assert(m);
2055
2056 do {
2057 bus->match_callbacks_modified = false;
2058
2059 r = bus_match_run(bus, &bus->match_callbacks, m);
2060 if (r != 0)
2061 return r;
2062
2063 } while (bus->match_callbacks_modified);
2064
2065 return 0;
2066}
2067
2068static int process_builtin(sd_bus *bus, sd_bus_message *m) {
2069 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2070 int r;
2071
2072 assert(bus);
2073 assert(m);
2074
2075 if (bus->manual_peer_interface)
2076 return 0;
2077
2078 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
2079 return 0;
2080
2081 if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
2082 return 0;
2083
2084 if (m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
2085 return 1;
2086
2087 if (streq_ptr(m->member, "Ping"))
2088 r = sd_bus_message_new_method_return(m, &reply);
2089 else if (streq_ptr(m->member, "GetMachineId")) {
2090 sd_id128_t id;
2091 char sid[33];
2092
2093 r = sd_id128_get_machine(&id);
2094 if (r < 0)
2095 return r;
2096
2097 r = sd_bus_message_new_method_return(m, &reply);
2098 if (r < 0)
2099 return r;
2100
2101 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
2102 } else {
2103 r = sd_bus_message_new_method_errorf(
2104 m, &reply,
2105 SD_BUS_ERROR_UNKNOWN_METHOD,
2106 "Unknown method '%s' on interface '%s'.", m->member, m->interface);
2107 }
2108
2109 if (r < 0)
2110 return r;
2111
2112 r = sd_bus_send(bus, reply, NULL);
2113 if (r < 0)
2114 return r;
2115
2116 return 1;
2117}
2118
2119static int process_message(sd_bus *bus, sd_bus_message *m) {
2120 int r;
2121
2122 assert(bus);
2123 assert(m);
2124
2125 bus->current = m;
2126 bus->iteration_counter++;
2127
2128 log_debug("Got message type=%s sender=%s destination=%s object=%s interface=%s member=%s cookie=%lu reply_cookie=%lu error=%s",
2129 bus_message_type_to_string(m->header->type),
2130 strna(sd_bus_message_get_sender(m)),
2131 strna(sd_bus_message_get_destination(m)),
2132 strna(sd_bus_message_get_path(m)),
2133 strna(sd_bus_message_get_interface(m)),
2134 strna(sd_bus_message_get_member(m)),
2135 (unsigned long) BUS_MESSAGE_COOKIE(m),
2136 (unsigned long) m->reply_cookie,
2137 strna(m->error.message));
2138
2139 r = process_hello(bus, m);
2140 if (r != 0)
2141 goto finish;
2142
2143 r = process_reply(bus, m);
2144 if (r != 0)
2145 goto finish;
2146
2147 r = process_filter(bus, m);
2148 if (r != 0)
2149 goto finish;
2150
2151 r = process_match(bus, m);
2152 if (r != 0)
2153 goto finish;
2154
2155 r = process_builtin(bus, m);
2156 if (r != 0)
2157 goto finish;
2158
2159 r = bus_process_object(bus, m);
2160
2161finish:
2162 bus->current = NULL;
2163 return r;
2164}
2165
2166static int process_running(sd_bus *bus, sd_bus_message **ret) {
2167 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2168 int r;
2169
2170 assert(bus);
2171 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
2172
2173 r = process_timeout(bus);
2174 if (r != 0)
2175 goto null_message;
2176
2177 r = dispatch_wqueue(bus);
2178 if (r != 0)
2179 goto null_message;
2180
2181 r = dispatch_rqueue(bus, &m);
2182 if (r < 0)
2183 return r;
2184 if (!m)
2185 goto null_message;
2186
2187 r = process_message(bus, m);
2188 if (r != 0)
2189 goto null_message;
2190
2191 if (ret) {
2192 r = sd_bus_message_rewind(m, true);
2193 if (r < 0)
2194 return r;
2195
2196 *ret = m;
2197 m = NULL;
2198 return 1;
2199 }
2200
2201 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
2202
2203 log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s",
2204 strna(sd_bus_message_get_sender(m)),
2205 strna(sd_bus_message_get_path(m)),
2206 strna(sd_bus_message_get_interface(m)),
2207 strna(sd_bus_message_get_member(m)));
2208
2209 r = sd_bus_reply_method_errorf(
2210 m,
2211 SD_BUS_ERROR_UNKNOWN_OBJECT,
2212 "Unknown object '%s'.", m->path);
2213 if (r < 0)
2214 return r;
2215 }
2216
2217 return 1;
2218
2219null_message:
2220 if (r >= 0 && ret)
2221 *ret = NULL;
2222
2223 return r;
2224}
2225
2226static int process_closing(sd_bus *bus, sd_bus_message **ret) {
2227 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2228 struct reply_callback *c;
2229 int r;
2230
2231 assert(bus);
2232 assert(bus->state == BUS_CLOSING);
2233
2234 c = hashmap_first(bus->reply_callbacks);
2235 if (c) {
2236 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2237
2238 /* First, fail all outstanding method calls */
2239 r = bus_message_new_synthetic_error(
2240 bus,
2241 c->cookie,
2242 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
2243 &m);
2244 if (r < 0)
2245 return r;
2246
2247 r = bus_seal_synthetic_message(bus, m);
2248 if (r < 0)
2249 return r;
2250
2251 if (c->timeout != 0)
2252 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2253
2254 hashmap_remove(bus->reply_callbacks, &c->cookie);
2255
2256 bus->current = m;
2257 bus->iteration_counter++;
2258
2259 r = c->callback(bus, m, c->userdata, &error_buffer);
2260 r = bus_maybe_reply_error(m, r, &error_buffer);
2261 free(c);
2262
2263 goto finish;
2264 }
2265
2266 /* Then, synthesize a Disconnected message */
2267 r = sd_bus_message_new_signal(
2268 bus,
2269 "/org/freedesktop/DBus/Local",
2270 "org.freedesktop.DBus.Local",
2271 "Disconnected",
2272 &m);
2273 if (r < 0)
2274 return r;
2275
2276 m->sender = "org.freedesktop.DBus.Local";
2277
2278 r = bus_seal_synthetic_message(bus, m);
2279 if (r < 0)
2280 return r;
2281
2282 sd_bus_close(bus);
2283
2284 bus->current = m;
2285 bus->iteration_counter++;
2286
2287 r = process_filter(bus, m);
2288 if (r != 0)
2289 goto finish;
2290
2291 r = process_match(bus, m);
2292 if (r != 0)
2293 goto finish;
2294
2295 if (ret) {
2296 *ret = m;
2297 m = NULL;
2298 }
2299
2300 r = 1;
2301
2302finish:
2303 bus->current = NULL;
2304 return r;
2305}
2306
2307_public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
2308 BUS_DONT_DESTROY(bus);
2309 int r;
2310
2311 /* Returns 0 when we didn't do anything. This should cause the
2312 * caller to invoke sd_bus_wait() before returning the next
2313 * time. Returns > 0 when we did something, which possibly
2314 * means *ret is filled in with an unprocessed message. */
2315
2316 assert_return(bus, -EINVAL);
2317 assert_return(!bus_pid_changed(bus), -ECHILD);
2318
2319 /* We don't allow recursively invoking sd_bus_process(). */
2320 assert_return(!bus->current, -EBUSY);
2321
2322 switch (bus->state) {
2323
2324 case BUS_UNSET:
2325 return -ENOTCONN;
2326
2327 case BUS_CLOSED:
2328 return -ECONNRESET;
2329
2330 case BUS_OPENING:
2331 r = bus_socket_process_opening(bus);
2332 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2333 bus_enter_closing(bus);
2334 r = 1;
2335 } else if (r < 0)
2336 return r;
2337 if (ret)
2338 *ret = NULL;
2339 return r;
2340
2341 case BUS_AUTHENTICATING:
2342 r = bus_socket_process_authenticating(bus);
2343 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2344 bus_enter_closing(bus);
2345 r = 1;
2346 } else if (r < 0)
2347 return r;
2348
2349 if (ret)
2350 *ret = NULL;
2351
2352 return r;
2353
2354 case BUS_RUNNING:
2355 case BUS_HELLO:
2356 r = process_running(bus, ret);
2357 if (r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2358 bus_enter_closing(bus);
2359 r = 1;
2360
2361 if (ret)
2362 *ret = NULL;
2363 }
2364
2365 return r;
2366
2367 case BUS_CLOSING:
2368 return process_closing(bus, ret);
2369 }
2370
2371 assert_not_reached("Unknown state");
2372}
2373
2374static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
2375 struct pollfd p[2] = {};
2376 int r, e, n;
2377 struct timespec ts;
2378 usec_t m = (usec_t) -1;
2379
2380 assert(bus);
2381
2382 if (bus->state == BUS_CLOSING)
2383 return 1;
2384
2385 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2386
2387 e = sd_bus_get_events(bus);
2388 if (e < 0)
2389 return e;
2390
2391 if (need_more)
2392 /* The caller really needs some more data, he doesn't
2393 * care about what's already read, or any timeouts
2394 * except its own.*/
2395 e |= POLLIN;
2396 else {
2397 usec_t until;
2398 /* The caller wants to process if there's something to
2399 * process, but doesn't care otherwise */
2400
2401 r = sd_bus_get_timeout(bus, &until);
2402 if (r < 0)
2403 return r;
2404 if (r > 0) {
2405 usec_t nw;
2406 nw = now(CLOCK_MONOTONIC);
2407 m = until > nw ? until - nw : 0;
2408 }
2409 }
2410
2411 if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
2412 m = timeout_usec;
2413
2414 p[0].fd = bus->input_fd;
2415 if (bus->output_fd == bus->input_fd) {
2416 p[0].events = e;
2417 n = 1;
2418 } else {
2419 p[0].events = e & POLLIN;
2420 p[1].fd = bus->output_fd;
2421 p[1].events = e & POLLOUT;
2422 n = 2;
2423 }
2424
2425 r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
2426 if (r < 0)
2427 return -errno;
2428
2429 return r > 0 ? 1 : 0;
2430}
2431
2432_public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
2433
2434 assert_return(bus, -EINVAL);
2435 assert_return(!bus_pid_changed(bus), -ECHILD);
2436
2437 if (bus->state == BUS_CLOSING)
2438 return 0;
2439
2440 assert_return(BUS_IS_OPEN(bus->state) , -ENOTCONN);
2441
2442 if (bus->rqueue_size > 0)
2443 return 0;
2444
2445 return bus_poll(bus, false, timeout_usec);
2446}
2447
2448_public_ int sd_bus_flush(sd_bus *bus) {
2449 int r;
2450
2451 assert_return(bus, -EINVAL);
2452 assert_return(!bus_pid_changed(bus), -ECHILD);
2453
2454 if (bus->state == BUS_CLOSING)
2455 return 0;
2456
2457 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2458
2459 r = bus_ensure_running(bus);
2460 if (r < 0)
2461 return r;
2462
2463 if (bus->wqueue_size <= 0)
2464 return 0;
2465
2466 for (;;) {
2467 r = dispatch_wqueue(bus);
2468 if (r < 0) {
2469 if (r == -EPIPE || r == -ENOTCONN || r == -ESHUTDOWN)
2470 bus_enter_closing(bus);
2471
2472 return r;
2473 }
2474
2475 if (bus->wqueue_size <= 0)
2476 return 0;
2477
2478 r = bus_poll(bus, false, (uint64_t) -1);
2479 if (r < 0)
2480 return r;
2481 }
2482}
2483
2484_public_ int sd_bus_add_filter(sd_bus *bus,
2485 sd_bus_message_handler_t callback,
2486 void *userdata) {
2487
2488 struct filter_callback *f;
2489
2490 assert_return(bus, -EINVAL);
2491 assert_return(callback, -EINVAL);
2492 assert_return(!bus_pid_changed(bus), -ECHILD);
2493
2494 f = new0(struct filter_callback, 1);
2495 if (!f)
2496 return -ENOMEM;
2497 f->callback = callback;
2498 f->userdata = userdata;
2499
2500 bus->filter_callbacks_modified = true;
2501 LIST_PREPEND(callbacks, bus->filter_callbacks, f);
2502 return 0;
2503}
2504
2505_public_ int sd_bus_remove_filter(sd_bus *bus,
2506 sd_bus_message_handler_t callback,
2507 void *userdata) {
2508
2509 struct filter_callback *f;
2510
2511 assert_return(bus, -EINVAL);
2512 assert_return(callback, -EINVAL);
2513 assert_return(!bus_pid_changed(bus), -ECHILD);
2514
2515 LIST_FOREACH(callbacks, f, bus->filter_callbacks) {
2516 if (f->callback == callback && f->userdata == userdata) {
2517 bus->filter_callbacks_modified = true;
2518 LIST_REMOVE(callbacks, bus->filter_callbacks, f);
2519 free(f);
2520 return 1;
2521 }
2522 }
2523
2524 return 0;
2525}
2526
2527_public_ int sd_bus_add_match(sd_bus *bus,
2528 const char *match,
2529 sd_bus_message_handler_t callback,
2530 void *userdata) {
2531
2532 struct bus_match_component *components = NULL;
2533 unsigned n_components = 0;
2534 uint64_t cookie = 0;
2535 int r = 0;
2536
2537 assert_return(bus, -EINVAL);
2538 assert_return(match, -EINVAL);
2539 assert_return(!bus_pid_changed(bus), -ECHILD);
2540
2541 r = bus_match_parse(match, &components, &n_components);
2542 if (r < 0)
2543 goto finish;
2544
2545 if (bus->bus_client) {
2546 cookie = ++bus->match_cookie;
2547
2548 r = bus_add_match_internal(bus, match, components, n_components, cookie);
2549 if (r < 0)
2550 goto finish;
2551 }
2552
2553 bus->match_callbacks_modified = true;
2554 r = bus_match_add(&bus->match_callbacks, components, n_components, callback, userdata, cookie, NULL);
2555 if (r < 0) {
2556 if (bus->bus_client)
2557 bus_remove_match_internal(bus, match, cookie);
2558 }
2559
2560finish:
2561 bus_match_parse_free(components, n_components);
2562 return r;
2563}
2564
2565_public_ int sd_bus_remove_match(sd_bus *bus,
2566 const char *match,
2567 sd_bus_message_handler_t callback,
2568 void *userdata) {
2569
2570 struct bus_match_component *components = NULL;
2571 unsigned n_components = 0;
2572 int r = 0, q = 0;
2573 uint64_t cookie = 0;
2574
2575 assert_return(bus, -EINVAL);
2576 assert_return(match, -EINVAL);
2577 assert_return(!bus_pid_changed(bus), -ECHILD);
2578
2579 r = bus_match_parse(match, &components, &n_components);
2580 if (r < 0)
2581 return r;
2582
2583 bus->match_callbacks_modified = true;
2584 r = bus_match_remove(&bus->match_callbacks, components, n_components, callback, userdata, &cookie);
2585
2586 if (bus->bus_client)
2587 q = bus_remove_match_internal(bus, match, cookie);
2588
2589 bus_match_parse_free(components, n_components);
2590
2591 return r < 0 ? r : q;
2592}
2593
2594bool bus_pid_changed(sd_bus *bus) {
2595 assert(bus);
2596
2597 /* We don't support people creating a bus connection and
2598 * keeping it around over a fork(). Let's complain. */
2599
2600 return bus->original_pid != getpid();
2601}
2602
2603static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
2604 sd_bus *bus = userdata;
2605 int r;
2606
2607 assert(bus);
2608
2609 r = sd_bus_process(bus, NULL);
2610 if (r < 0)
2611 return r;
2612
2613 return 1;
2614}
2615
2616static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
2617 sd_bus *bus = userdata;
2618 int r;
2619
2620 assert(bus);
2621
2622 r = sd_bus_process(bus, NULL);
2623 if (r < 0)
2624 return r;
2625
2626 return 1;
2627}
2628
2629static int prepare_callback(sd_event_source *s, void *userdata) {
2630 sd_bus *bus = userdata;
2631 int r, e;
2632 usec_t until;
2633
2634 assert(s);
2635 assert(bus);
2636
2637 e = sd_bus_get_events(bus);
2638 if (e < 0)
2639 return e;
2640
2641 if (bus->output_fd != bus->input_fd) {
2642
2643 r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN);
2644 if (r < 0)
2645 return r;
2646
2647 r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT);
2648 if (r < 0)
2649 return r;
2650 } else {
2651 r = sd_event_source_set_io_events(bus->input_io_event_source, e);
2652 if (r < 0)
2653 return r;
2654 }
2655
2656 r = sd_bus_get_timeout(bus, &until);
2657 if (r < 0)
2658 return r;
2659 if (r > 0) {
2660 int j;
2661
2662 j = sd_event_source_set_time(bus->time_event_source, until);
2663 if (j < 0)
2664 return j;
2665 }
2666
2667 r = sd_event_source_set_enabled(bus->time_event_source, r > 0);
2668 if (r < 0)
2669 return r;
2670
2671 return 1;
2672}
2673
2674static int quit_callback(sd_event_source *event, void *userdata) {
2675 sd_bus *bus = userdata;
2676
2677 assert(event);
2678
2679 sd_bus_flush(bus);
2680
2681 return 1;
2682}
2683
2684static int attach_io_events(sd_bus *bus) {
2685 int r;
2686
2687 assert(bus);
2688
2689 if (bus->input_fd < 0)
2690 return 0;
2691
2692 if (!bus->event)
2693 return 0;
2694
2695 if (!bus->input_io_event_source) {
2696 r = sd_event_add_io(bus->event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source);
2697 if (r < 0)
2698 return r;
2699
2700 r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
2701 if (r < 0)
2702 return r;
2703
2704 r = sd_event_source_set_priority(bus->input_io_event_source, bus->event_priority);
2705 } else
2706 r = sd_event_source_set_io_fd(bus->input_io_event_source, bus->input_fd);
2707
2708 if (r < 0)
2709 return r;
2710
2711 if (bus->output_fd != bus->input_fd) {
2712 assert(bus->output_fd >= 0);
2713
2714 if (!bus->output_io_event_source) {
2715 r = sd_event_add_io(bus->event, bus->output_fd, 0, io_callback, bus, &bus->output_io_event_source);
2716 if (r < 0)
2717 return r;
2718
2719 r = sd_event_source_set_priority(bus->output_io_event_source, bus->event_priority);
2720 } else
2721 r = sd_event_source_set_io_fd(bus->output_io_event_source, bus->output_fd);
2722
2723 if (r < 0)
2724 return r;
2725 }
2726
2727 return 0;
2728}
2729
2730static void detach_io_events(sd_bus *bus) {
2731 assert(bus);
2732
2733 if (bus->input_io_event_source) {
2734 sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
2735 bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
2736 }
2737
2738 if (bus->output_io_event_source) {
2739 sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
2740 bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
2741 }
2742}
2743
2744_public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
2745 int r;
2746
2747 assert_return(bus, -EINVAL);
2748 assert_return(!bus->event, -EBUSY);
2749
2750 assert(!bus->input_io_event_source);
2751 assert(!bus->output_io_event_source);
2752 assert(!bus->time_event_source);
2753
2754 if (event)
2755 bus->event = sd_event_ref(event);
2756 else {
2757 r = sd_event_default(&bus->event);
2758 if (r < 0)
2759 return r;
2760 }
2761
2762 bus->event_priority = priority;
2763
2764 r = sd_event_add_monotonic(bus->event, 0, 0, time_callback, bus, &bus->time_event_source);
2765 if (r < 0)
2766 goto fail;
2767
2768 r = sd_event_source_set_priority(bus->time_event_source, priority);
2769 if (r < 0)
2770 goto fail;
2771
2772 r = sd_event_add_exit(bus->event, quit_callback, bus, &bus->quit_event_source);
2773 if (r < 0)
2774 goto fail;
2775
2776 r = attach_io_events(bus);
2777 if (r < 0)
2778 goto fail;
2779
2780 return 0;
2781
2782fail:
2783 sd_bus_detach_event(bus);
2784 return r;
2785}
2786
2787_public_ int sd_bus_detach_event(sd_bus *bus) {
2788 assert_return(bus, -EINVAL);
2789
2790 if (!bus->event)
2791 return 0;
2792
2793 detach_io_events(bus);
2794
2795 if (bus->time_event_source) {
2796 sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF);
2797 bus->time_event_source = sd_event_source_unref(bus->time_event_source);
2798 }
2799
2800 if (bus->quit_event_source) {
2801 sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF);
2802 bus->quit_event_source = sd_event_source_unref(bus->quit_event_source);
2803 }
2804
2805 if (bus->event)
2806 bus->event = sd_event_unref(bus->event);
2807
2808 return 1;
2809}
2810
2811_public_ sd_event* sd_bus_get_event(sd_bus *bus) {
2812 assert_return(bus, NULL);
2813
2814 return bus->event;
2815}
2816
2817_public_ sd_bus_message* sd_bus_get_current(sd_bus *bus) {
2818 assert_return(bus, NULL);
2819
2820 return bus->current;
2821}
2822
2823static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
2824 sd_bus *b = NULL;
2825 int r;
2826
2827 assert(bus_open);
2828 assert(default_bus);
2829
2830 if (!ret)
2831 return !!*default_bus;
2832
2833 if (*default_bus) {
2834 *ret = sd_bus_ref(*default_bus);
2835 return 0;
2836 }
2837
2838 r = bus_open(&b);
2839 if (r < 0)
2840 return r;
2841
2842 b->default_bus_ptr = default_bus;
2843 b->tid = gettid();
2844 *default_bus = b;
2845
2846 *ret = b;
2847 return 1;
2848}
2849
2850_public_ int sd_bus_default_system(sd_bus **ret) {
2851 static thread_local sd_bus *default_system_bus = NULL;
2852
2853 return bus_default(sd_bus_open_system, &default_system_bus, ret);
2854}
2855
2856_public_ int sd_bus_default_user(sd_bus **ret) {
2857 static thread_local sd_bus *default_user_bus = NULL;
2858
2859 return bus_default(sd_bus_open_user, &default_user_bus, ret);
2860}
2861
2862_public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
2863 assert_return(b, -EINVAL);
2864 assert_return(tid, -EINVAL);
2865 assert_return(!bus_pid_changed(b), -ECHILD);
2866
2867 if (b->tid != 0) {
2868 *tid = b->tid;
2869 return 0;
2870 }
2871
2872 if (b->event)
2873 return sd_event_get_tid(b->event, tid);
2874
2875 return -ENXIO;
2876}
2877
2878_public_ char *sd_bus_label_escape(const char *s) {
2879 char *r, *t;
2880 const char *f;
2881
2882 assert_return(s, NULL);
2883
2884 /* Escapes all chars that D-Bus' object path cannot deal
2885 * with. Can be reversed with bus_path_unescape(). We special
2886 * case the empty string. */
2887
2888 if (*s == 0)
2889 return strdup("_");
2890
2891 r = new(char, strlen(s)*3 + 1);
2892 if (!r)
2893 return NULL;
2894
2895 for (f = s, t = r; *f; f++) {
2896
2897 /* Escape everything that is not a-zA-Z0-9. We also
2898 * escape 0-9 if it's the first character */
2899
2900 if (!(*f >= 'A' && *f <= 'Z') &&
2901 !(*f >= 'a' && *f <= 'z') &&
2902 !(f > s && *f >= '0' && *f <= '9')) {
2903 *(t++) = '_';
2904 *(t++) = hexchar(*f >> 4);
2905 *(t++) = hexchar(*f);
2906 } else
2907 *(t++) = *f;
2908 }
2909
2910 *t = 0;
2911
2912 return r;
2913}
2914
2915_public_ char *sd_bus_label_unescape(const char *f) {
2916 char *r, *t;
2917
2918 assert_return(f, NULL);
2919
2920 /* Special case for the empty string */
2921 if (streq(f, "_"))
2922 return strdup("");
2923
2924 r = new(char, strlen(f) + 1);
2925 if (!r)
2926 return NULL;
2927
2928 for (t = r; *f; f++) {
2929
2930 if (*f == '_') {
2931 int a, b;
2932
2933 if ((a = unhexchar(f[1])) < 0 ||
2934 (b = unhexchar(f[2])) < 0) {
2935 /* Invalid escape code, let's take it literal then */
2936 *(t++) = '_';
2937 } else {
2938 *(t++) = (char) ((a << 4) | b);
2939 f += 2;
2940 }
2941 } else
2942 *(t++) = *f;
2943 }
2944
2945 *t = 0;
2946
2947 return r;
2948}
2949
2950_public_ int sd_bus_get_peer_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) {
2951 sd_bus_creds *c;
2952 pid_t pid = 0;
2953 int r;
2954
2955 assert_return(bus, -EINVAL);
2956 assert_return(mask <= _SD_BUS_CREDS_ALL, -ENOTSUP);
2957 assert_return(ret, -EINVAL);
2958 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
2959 assert_return(!bus_pid_changed(bus), -ECHILD);
2960 assert_return(!bus->is_kernel, -ENOTSUP);
2961
2962 if (!bus->ucred_valid && !isempty(bus->label))
2963 return -ENODATA;
2964
2965 c = bus_creds_new();
2966 if (!c)
2967 return -ENOMEM;
2968
2969 if (bus->ucred_valid) {
2970 pid = c->pid = bus->ucred.pid;
2971 c->uid = bus->ucred.uid;
2972 c->gid = bus->ucred.gid;
2973
2974 c->mask |= (SD_BUS_CREDS_UID | SD_BUS_CREDS_PID | SD_BUS_CREDS_GID) & mask;
2975 }
2976
2977 if (!isempty(bus->label) && (mask & SD_BUS_CREDS_SELINUX_CONTEXT)) {
2978 c->label = strdup(bus->label);
2979 if (!c->label) {
2980 sd_bus_creds_unref(c);
2981 return -ENOMEM;
2982 }
2983
2984 c->mask |= SD_BUS_CREDS_SELINUX_CONTEXT;
2985 }
2986
2987 r = bus_creds_add_more(c, mask, pid, 0);
2988 if (r < 0)
2989 return r;
2990
2991 *ret = c;
2992 return 0;
2993}
2994
2995_public_ int sd_bus_try_close(sd_bus *bus) {
2996 int r;
2997
2998 assert_return(bus, -EINVAL);
2999 assert_return(BUS_IS_OPEN(bus->state), -ENOTCONN);
3000 assert_return(!bus_pid_changed(bus), -ECHILD);
3001 assert_return(bus->is_kernel, -ENOTSUP);
3002
3003 if (bus->rqueue_size > 0)
3004 return -EBUSY;
3005
3006 if (bus->wqueue_size > 0)
3007 return -EBUSY;
3008
3009 r = bus_kernel_try_close(bus);
3010 if (r < 0)
3011 return r;
3012
3013 sd_bus_close(bus);
3014 return 0;
3015}