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