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