]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-bus/sd-bus.c
Merge pull request #953 from poettering/ebadf
[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, -EBADF);
217 assert_return(output_fd >= 0, -EBADF);
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 bus_assert_return(m, -EINVAL, error);
1955 bus_assert_return(m->header->type == SD_BUS_MESSAGE_METHOD_CALL, -EINVAL, error);
1956 bus_assert_return(!(m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED), -EINVAL, error);
1957 bus_assert_return(!bus_error_is_dirty(error), -EINVAL, error);
1958
1959 if (!bus)
1960 bus = m->bus;
1961
1962 bus_assert_return(!bus_pid_changed(bus), -ECHILD, error);
1963 bus_assert_return(!bus->is_kernel || !(bus->hello_flags & KDBUS_HELLO_MONITOR), -EROFS, error);
1964
1965 if (!BUS_IS_OPEN(bus->state)) {
1966 r = -ENOTCONN;
1967 goto fail;
1968 }
1969
1970 r = bus_ensure_running(bus);
1971 if (r < 0)
1972 goto fail;
1973
1974 i = bus->rqueue_size;
1975
1976 r = bus_seal_message(bus, m, usec);
1977 if (r < 0)
1978 goto fail;
1979
1980 r = bus_remarshal_message(bus, &m);
1981 if (r < 0)
1982 goto fail;
1983
1984 r = bus_send_internal(bus, m, &cookie, true);
1985 if (r < 0)
1986 goto fail;
1987
1988 timeout = calc_elapse(m->timeout);
1989
1990 for (;;) {
1991 usec_t left;
1992
1993 while (i < bus->rqueue_size) {
1994 sd_bus_message *incoming = NULL;
1995
1996 incoming = bus->rqueue[i];
1997
1998 if (incoming->reply_cookie == cookie) {
1999 /* Found a match! */
2000
2001 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
2002 bus->rqueue_size--;
2003 log_debug_bus_message(incoming);
2004
2005 if (incoming->header->type == SD_BUS_MESSAGE_METHOD_RETURN) {
2006
2007 if (incoming->n_fds <= 0 || (bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)) {
2008 if (reply)
2009 *reply = incoming;
2010 else
2011 sd_bus_message_unref(incoming);
2012
2013 return 1;
2014 }
2015
2016 r = sd_bus_error_setf(error, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptors which I couldn't accept. Sorry.");
2017 sd_bus_message_unref(incoming);
2018 return r;
2019
2020 } else if (incoming->header->type == SD_BUS_MESSAGE_METHOD_ERROR) {
2021 r = sd_bus_error_copy(error, &incoming->error);
2022 sd_bus_message_unref(incoming);
2023 return r;
2024 } else {
2025 r = -EIO;
2026 goto fail;
2027 }
2028
2029 } else if (BUS_MESSAGE_COOKIE(incoming) == cookie &&
2030 bus->unique_name &&
2031 incoming->sender &&
2032 streq(bus->unique_name, incoming->sender)) {
2033
2034 memmove(bus->rqueue + i, bus->rqueue + i + 1, sizeof(sd_bus_message*) * (bus->rqueue_size - i - 1));
2035 bus->rqueue_size--;
2036
2037 /* Our own message? Somebody is trying
2038 * to send its own client a message,
2039 * let's not dead-lock, let's fail
2040 * immediately. */
2041
2042 sd_bus_message_unref(incoming);
2043 r = -ELOOP;
2044 goto fail;
2045 }
2046
2047 /* Try to read more, right-away */
2048 i++;
2049 }
2050
2051 r = bus_read_message(bus, false, 0);
2052 if (r < 0) {
2053 if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2054 bus_enter_closing(bus);
2055 r = -ECONNRESET;
2056 }
2057
2058 goto fail;
2059 }
2060 if (r > 0)
2061 continue;
2062
2063 if (timeout > 0) {
2064 usec_t n;
2065
2066 n = now(CLOCK_MONOTONIC);
2067 if (n >= timeout) {
2068 r = -ETIMEDOUT;
2069 goto fail;
2070 }
2071
2072 left = timeout - n;
2073 } else
2074 left = (uint64_t) -1;
2075
2076 r = bus_poll(bus, true, left);
2077 if (r < 0)
2078 goto fail;
2079 if (r == 0) {
2080 r = -ETIMEDOUT;
2081 goto fail;
2082 }
2083
2084 r = dispatch_wqueue(bus);
2085 if (r < 0) {
2086 if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2087 bus_enter_closing(bus);
2088 r = -ECONNRESET;
2089 }
2090
2091 goto fail;
2092 }
2093 }
2094
2095 fail:
2096 return sd_bus_error_set_errno(error, r);
2097 }
2098
2099 _public_ int sd_bus_get_fd(sd_bus *bus) {
2100
2101 assert_return(bus, -EINVAL);
2102 assert_return(bus->input_fd == bus->output_fd, -EPERM);
2103 assert_return(!bus_pid_changed(bus), -ECHILD);
2104
2105 return bus->input_fd;
2106 }
2107
2108 _public_ int sd_bus_get_events(sd_bus *bus) {
2109 int flags = 0;
2110
2111 assert_return(bus, -EINVAL);
2112 assert_return(!bus_pid_changed(bus), -ECHILD);
2113
2114 if (!BUS_IS_OPEN(bus->state) && bus->state != BUS_CLOSING)
2115 return -ENOTCONN;
2116
2117 if (bus->state == BUS_OPENING)
2118 flags |= POLLOUT;
2119 else if (bus->state == BUS_AUTHENTICATING) {
2120
2121 if (bus_socket_auth_needs_write(bus))
2122 flags |= POLLOUT;
2123
2124 flags |= POLLIN;
2125
2126 } else if (bus->state == BUS_RUNNING || bus->state == BUS_HELLO) {
2127 if (bus->rqueue_size <= 0)
2128 flags |= POLLIN;
2129 if (bus->wqueue_size > 0)
2130 flags |= POLLOUT;
2131 }
2132
2133 return flags;
2134 }
2135
2136 _public_ int sd_bus_get_timeout(sd_bus *bus, uint64_t *timeout_usec) {
2137 struct reply_callback *c;
2138
2139 assert_return(bus, -EINVAL);
2140 assert_return(timeout_usec, -EINVAL);
2141 assert_return(!bus_pid_changed(bus), -ECHILD);
2142
2143 if (!BUS_IS_OPEN(bus->state) && bus->state != BUS_CLOSING)
2144 return -ENOTCONN;
2145
2146 if (bus->track_queue) {
2147 *timeout_usec = 0;
2148 return 1;
2149 }
2150
2151 if (bus->state == BUS_CLOSING) {
2152 *timeout_usec = 0;
2153 return 1;
2154 }
2155
2156 if (bus->state == BUS_AUTHENTICATING) {
2157 *timeout_usec = bus->auth_timeout;
2158 return 1;
2159 }
2160
2161 if (bus->state != BUS_RUNNING && bus->state != BUS_HELLO) {
2162 *timeout_usec = (uint64_t) -1;
2163 return 0;
2164 }
2165
2166 if (bus->rqueue_size > 0) {
2167 *timeout_usec = 0;
2168 return 1;
2169 }
2170
2171 c = prioq_peek(bus->reply_callbacks_prioq);
2172 if (!c) {
2173 *timeout_usec = (uint64_t) -1;
2174 return 0;
2175 }
2176
2177 if (c->timeout == 0) {
2178 *timeout_usec = (uint64_t) -1;
2179 return 0;
2180 }
2181
2182 *timeout_usec = c->timeout;
2183 return 1;
2184 }
2185
2186 static int process_timeout(sd_bus *bus) {
2187 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2188 _cleanup_bus_message_unref_ sd_bus_message* m = NULL;
2189 struct reply_callback *c;
2190 sd_bus_slot *slot;
2191 usec_t n;
2192 int r;
2193
2194 assert(bus);
2195
2196 c = prioq_peek(bus->reply_callbacks_prioq);
2197 if (!c)
2198 return 0;
2199
2200 n = now(CLOCK_MONOTONIC);
2201 if (c->timeout > n)
2202 return 0;
2203
2204 r = bus_message_new_synthetic_error(
2205 bus,
2206 c->cookie,
2207 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Method call timed out"),
2208 &m);
2209 if (r < 0)
2210 return r;
2211
2212 r = bus_seal_synthetic_message(bus, m);
2213 if (r < 0)
2214 return r;
2215
2216 assert_se(prioq_pop(bus->reply_callbacks_prioq) == c);
2217 c->timeout = 0;
2218
2219 ordered_hashmap_remove(bus->reply_callbacks, &c->cookie);
2220 c->cookie = 0;
2221
2222 slot = container_of(c, sd_bus_slot, reply_callback);
2223
2224 bus->iteration_counter ++;
2225
2226 bus->current_message = m;
2227 bus->current_slot = sd_bus_slot_ref(slot);
2228 bus->current_handler = c->callback;
2229 bus->current_userdata = slot->userdata;
2230 r = c->callback(m, slot->userdata, &error_buffer);
2231 bus->current_userdata = NULL;
2232 bus->current_handler = NULL;
2233 bus->current_slot = NULL;
2234 bus->current_message = NULL;
2235
2236 if (slot->floating) {
2237 bus_slot_disconnect(slot);
2238 sd_bus_slot_unref(slot);
2239 }
2240
2241 sd_bus_slot_unref(slot);
2242
2243 return bus_maybe_reply_error(m, r, &error_buffer);
2244 }
2245
2246 static int process_hello(sd_bus *bus, sd_bus_message *m) {
2247 assert(bus);
2248 assert(m);
2249
2250 if (bus->state != BUS_HELLO)
2251 return 0;
2252
2253 /* Let's make sure the first message on the bus is the HELLO
2254 * reply. But note that we don't actually parse the message
2255 * here (we leave that to the usual handling), we just verify
2256 * we don't let any earlier msg through. */
2257
2258 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
2259 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
2260 return -EIO;
2261
2262 if (m->reply_cookie != 1)
2263 return -EIO;
2264
2265 return 0;
2266 }
2267
2268 static int process_reply(sd_bus *bus, sd_bus_message *m) {
2269 _cleanup_bus_message_unref_ sd_bus_message *synthetic_reply = NULL;
2270 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2271 struct reply_callback *c;
2272 sd_bus_slot *slot;
2273 int r;
2274
2275 assert(bus);
2276 assert(m);
2277
2278 if (m->header->type != SD_BUS_MESSAGE_METHOD_RETURN &&
2279 m->header->type != SD_BUS_MESSAGE_METHOD_ERROR)
2280 return 0;
2281
2282 if (bus->is_kernel && (bus->hello_flags & KDBUS_HELLO_MONITOR))
2283 return 0;
2284
2285 if (m->destination && bus->unique_name && !streq_ptr(m->destination, bus->unique_name))
2286 return 0;
2287
2288 c = ordered_hashmap_remove(bus->reply_callbacks, &m->reply_cookie);
2289 if (!c)
2290 return 0;
2291
2292 c->cookie = 0;
2293
2294 slot = container_of(c, sd_bus_slot, reply_callback);
2295
2296 if (m->n_fds > 0 && !(bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)) {
2297
2298 /* If the reply contained a file descriptor which we
2299 * didn't want we pass an error instead. */
2300
2301 r = bus_message_new_synthetic_error(
2302 bus,
2303 m->reply_cookie,
2304 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Reply message contained file descriptor"),
2305 &synthetic_reply);
2306 if (r < 0)
2307 return r;
2308
2309 /* Copy over original timestamp */
2310 synthetic_reply->realtime = m->realtime;
2311 synthetic_reply->monotonic = m->monotonic;
2312 synthetic_reply->seqnum = m->seqnum;
2313
2314 r = bus_seal_synthetic_message(bus, synthetic_reply);
2315 if (r < 0)
2316 return r;
2317
2318 m = synthetic_reply;
2319 } else {
2320 r = sd_bus_message_rewind(m, true);
2321 if (r < 0)
2322 return r;
2323 }
2324
2325 if (c->timeout != 0) {
2326 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2327 c->timeout = 0;
2328 }
2329
2330 bus->current_slot = sd_bus_slot_ref(slot);
2331 bus->current_handler = c->callback;
2332 bus->current_userdata = slot->userdata;
2333 r = c->callback(m, slot->userdata, &error_buffer);
2334 bus->current_userdata = NULL;
2335 bus->current_handler = NULL;
2336 bus->current_slot = NULL;
2337
2338 if (slot->floating) {
2339 bus_slot_disconnect(slot);
2340 sd_bus_slot_unref(slot);
2341 }
2342
2343 sd_bus_slot_unref(slot);
2344
2345 return bus_maybe_reply_error(m, r, &error_buffer);
2346 }
2347
2348 static int process_filter(sd_bus *bus, sd_bus_message *m) {
2349 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2350 struct filter_callback *l;
2351 int r;
2352
2353 assert(bus);
2354 assert(m);
2355
2356 do {
2357 bus->filter_callbacks_modified = false;
2358
2359 LIST_FOREACH(callbacks, l, bus->filter_callbacks) {
2360 sd_bus_slot *slot;
2361
2362 if (bus->filter_callbacks_modified)
2363 break;
2364
2365 /* Don't run this more than once per iteration */
2366 if (l->last_iteration == bus->iteration_counter)
2367 continue;
2368
2369 l->last_iteration = bus->iteration_counter;
2370
2371 r = sd_bus_message_rewind(m, true);
2372 if (r < 0)
2373 return r;
2374
2375 slot = container_of(l, sd_bus_slot, filter_callback);
2376
2377 bus->current_slot = sd_bus_slot_ref(slot);
2378 bus->current_handler = l->callback;
2379 bus->current_userdata = slot->userdata;
2380 r = l->callback(m, slot->userdata, &error_buffer);
2381 bus->current_userdata = NULL;
2382 bus->current_handler = NULL;
2383 bus->current_slot = sd_bus_slot_unref(slot);
2384
2385 r = bus_maybe_reply_error(m, r, &error_buffer);
2386 if (r != 0)
2387 return r;
2388
2389 }
2390
2391 } while (bus->filter_callbacks_modified);
2392
2393 return 0;
2394 }
2395
2396 static int process_match(sd_bus *bus, sd_bus_message *m) {
2397 int r;
2398
2399 assert(bus);
2400 assert(m);
2401
2402 do {
2403 bus->match_callbacks_modified = false;
2404
2405 r = bus_match_run(bus, &bus->match_callbacks, m);
2406 if (r != 0)
2407 return r;
2408
2409 } while (bus->match_callbacks_modified);
2410
2411 return 0;
2412 }
2413
2414 static int process_builtin(sd_bus *bus, sd_bus_message *m) {
2415 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2416 int r;
2417
2418 assert(bus);
2419 assert(m);
2420
2421 if (bus->hello_flags & KDBUS_HELLO_MONITOR)
2422 return 0;
2423
2424 if (bus->manual_peer_interface)
2425 return 0;
2426
2427 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
2428 return 0;
2429
2430 if (!streq_ptr(m->interface, "org.freedesktop.DBus.Peer"))
2431 return 0;
2432
2433 if (m->header->flags & BUS_MESSAGE_NO_REPLY_EXPECTED)
2434 return 1;
2435
2436 if (streq_ptr(m->member, "Ping"))
2437 r = sd_bus_message_new_method_return(m, &reply);
2438 else if (streq_ptr(m->member, "GetMachineId")) {
2439 sd_id128_t id;
2440 char sid[33];
2441
2442 r = sd_id128_get_machine(&id);
2443 if (r < 0)
2444 return r;
2445
2446 r = sd_bus_message_new_method_return(m, &reply);
2447 if (r < 0)
2448 return r;
2449
2450 r = sd_bus_message_append(reply, "s", sd_id128_to_string(id, sid));
2451 } else {
2452 r = sd_bus_message_new_method_errorf(
2453 m, &reply,
2454 SD_BUS_ERROR_UNKNOWN_METHOD,
2455 "Unknown method '%s' on interface '%s'.", m->member, m->interface);
2456 }
2457
2458 if (r < 0)
2459 return r;
2460
2461 r = sd_bus_send(bus, reply, NULL);
2462 if (r < 0)
2463 return r;
2464
2465 return 1;
2466 }
2467
2468 static int process_fd_check(sd_bus *bus, sd_bus_message *m) {
2469 assert(bus);
2470 assert(m);
2471
2472 /* If we got a message with a file descriptor which we didn't
2473 * want to accept, then let's drop it. How can this even
2474 * happen? For example, when the kernel queues a message into
2475 * an activatable names's queue which allows fds, and then is
2476 * delivered to us later even though we ourselves did not
2477 * negotiate it. */
2478
2479 if (bus->hello_flags & KDBUS_HELLO_MONITOR)
2480 return 0;
2481
2482 if (m->n_fds <= 0)
2483 return 0;
2484
2485 if (bus->hello_flags & KDBUS_HELLO_ACCEPT_FD)
2486 return 0;
2487
2488 if (m->header->type != SD_BUS_MESSAGE_METHOD_CALL)
2489 return 1; /* just eat it up */
2490
2491 return sd_bus_reply_method_errorf(m, SD_BUS_ERROR_INCONSISTENT_MESSAGE, "Message contains file descriptors, which I cannot accept. Sorry.");
2492 }
2493
2494 static int process_message(sd_bus *bus, sd_bus_message *m) {
2495 int r;
2496
2497 assert(bus);
2498 assert(m);
2499
2500 bus->current_message = m;
2501 bus->iteration_counter++;
2502
2503 log_debug_bus_message(m);
2504
2505 r = process_hello(bus, m);
2506 if (r != 0)
2507 goto finish;
2508
2509 r = process_reply(bus, m);
2510 if (r != 0)
2511 goto finish;
2512
2513 r = process_fd_check(bus, m);
2514 if (r != 0)
2515 goto finish;
2516
2517 r = process_filter(bus, m);
2518 if (r != 0)
2519 goto finish;
2520
2521 r = process_match(bus, m);
2522 if (r != 0)
2523 goto finish;
2524
2525 r = process_builtin(bus, m);
2526 if (r != 0)
2527 goto finish;
2528
2529 r = bus_process_object(bus, m);
2530
2531 finish:
2532 bus->current_message = NULL;
2533 return r;
2534 }
2535
2536 static int dispatch_track(sd_bus *bus) {
2537 assert(bus);
2538
2539 if (!bus->track_queue)
2540 return 0;
2541
2542 bus_track_dispatch(bus->track_queue);
2543 return 1;
2544 }
2545
2546 static int process_running(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) {
2547 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2548 int r;
2549
2550 assert(bus);
2551 assert(bus->state == BUS_RUNNING || bus->state == BUS_HELLO);
2552
2553 r = process_timeout(bus);
2554 if (r != 0)
2555 goto null_message;
2556
2557 r = dispatch_wqueue(bus);
2558 if (r != 0)
2559 goto null_message;
2560
2561 r = dispatch_track(bus);
2562 if (r != 0)
2563 goto null_message;
2564
2565 r = dispatch_rqueue(bus, hint_priority, priority, &m);
2566 if (r < 0)
2567 return r;
2568 if (!m)
2569 goto null_message;
2570
2571 r = process_message(bus, m);
2572 if (r != 0)
2573 goto null_message;
2574
2575 if (ret) {
2576 r = sd_bus_message_rewind(m, true);
2577 if (r < 0)
2578 return r;
2579
2580 *ret = m;
2581 m = NULL;
2582 return 1;
2583 }
2584
2585 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL) {
2586
2587 log_debug("Unprocessed message call sender=%s object=%s interface=%s member=%s",
2588 strna(sd_bus_message_get_sender(m)),
2589 strna(sd_bus_message_get_path(m)),
2590 strna(sd_bus_message_get_interface(m)),
2591 strna(sd_bus_message_get_member(m)));
2592
2593 r = sd_bus_reply_method_errorf(
2594 m,
2595 SD_BUS_ERROR_UNKNOWN_OBJECT,
2596 "Unknown object '%s'.", m->path);
2597 if (r < 0)
2598 return r;
2599 }
2600
2601 return 1;
2602
2603 null_message:
2604 if (r >= 0 && ret)
2605 *ret = NULL;
2606
2607 return r;
2608 }
2609
2610 static int process_closing(sd_bus *bus, sd_bus_message **ret) {
2611 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
2612 struct reply_callback *c;
2613 int r;
2614
2615 assert(bus);
2616 assert(bus->state == BUS_CLOSING);
2617
2618 c = ordered_hashmap_first(bus->reply_callbacks);
2619 if (c) {
2620 _cleanup_bus_error_free_ sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
2621 sd_bus_slot *slot;
2622
2623 /* First, fail all outstanding method calls */
2624 r = bus_message_new_synthetic_error(
2625 bus,
2626 c->cookie,
2627 &SD_BUS_ERROR_MAKE_CONST(SD_BUS_ERROR_NO_REPLY, "Connection terminated"),
2628 &m);
2629 if (r < 0)
2630 return r;
2631
2632 r = bus_seal_synthetic_message(bus, m);
2633 if (r < 0)
2634 return r;
2635
2636 if (c->timeout != 0) {
2637 prioq_remove(bus->reply_callbacks_prioq, c, &c->prioq_idx);
2638 c->timeout = 0;
2639 }
2640
2641 ordered_hashmap_remove(bus->reply_callbacks, &c->cookie);
2642 c->cookie = 0;
2643
2644 slot = container_of(c, sd_bus_slot, reply_callback);
2645
2646 bus->iteration_counter++;
2647
2648 bus->current_message = m;
2649 bus->current_slot = sd_bus_slot_ref(slot);
2650 bus->current_handler = c->callback;
2651 bus->current_userdata = slot->userdata;
2652 r = c->callback(m, slot->userdata, &error_buffer);
2653 bus->current_userdata = NULL;
2654 bus->current_handler = NULL;
2655 bus->current_slot = NULL;
2656 bus->current_message = NULL;
2657
2658 if (slot->floating) {
2659 bus_slot_disconnect(slot);
2660 sd_bus_slot_unref(slot);
2661 }
2662
2663 sd_bus_slot_unref(slot);
2664
2665 return bus_maybe_reply_error(m, r, &error_buffer);
2666 }
2667
2668 /* Then, synthesize a Disconnected message */
2669 r = sd_bus_message_new_signal(
2670 bus,
2671 &m,
2672 "/org/freedesktop/DBus/Local",
2673 "org.freedesktop.DBus.Local",
2674 "Disconnected");
2675 if (r < 0)
2676 return r;
2677
2678 bus_message_set_sender_local(bus, m);
2679
2680 r = bus_seal_synthetic_message(bus, m);
2681 if (r < 0)
2682 return r;
2683
2684 sd_bus_close(bus);
2685
2686 bus->current_message = m;
2687 bus->iteration_counter++;
2688
2689 r = process_filter(bus, m);
2690 if (r != 0)
2691 goto finish;
2692
2693 r = process_match(bus, m);
2694 if (r != 0)
2695 goto finish;
2696
2697 if (ret) {
2698 *ret = m;
2699 m = NULL;
2700 }
2701
2702 r = 1;
2703
2704 finish:
2705 bus->current_message = NULL;
2706
2707 return r;
2708 }
2709
2710 static int bus_process_internal(sd_bus *bus, bool hint_priority, int64_t priority, sd_bus_message **ret) {
2711 BUS_DONT_DESTROY(bus);
2712 int r;
2713
2714 /* Returns 0 when we didn't do anything. This should cause the
2715 * caller to invoke sd_bus_wait() before returning the next
2716 * time. Returns > 0 when we did something, which possibly
2717 * means *ret is filled in with an unprocessed message. */
2718
2719 assert_return(bus, -EINVAL);
2720 assert_return(!bus_pid_changed(bus), -ECHILD);
2721
2722 /* We don't allow recursively invoking sd_bus_process(). */
2723 assert_return(!bus->current_message, -EBUSY);
2724 assert(!bus->current_slot);
2725
2726 switch (bus->state) {
2727
2728 case BUS_UNSET:
2729 return -ENOTCONN;
2730
2731 case BUS_CLOSED:
2732 return -ECONNRESET;
2733
2734 case BUS_OPENING:
2735 r = bus_socket_process_opening(bus);
2736 if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2737 bus_enter_closing(bus);
2738 r = 1;
2739 } else if (r < 0)
2740 return r;
2741 if (ret)
2742 *ret = NULL;
2743 return r;
2744
2745 case BUS_AUTHENTICATING:
2746 r = bus_socket_process_authenticating(bus);
2747 if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2748 bus_enter_closing(bus);
2749 r = 1;
2750 } else if (r < 0)
2751 return r;
2752
2753 if (ret)
2754 *ret = NULL;
2755
2756 return r;
2757
2758 case BUS_RUNNING:
2759 case BUS_HELLO:
2760 r = process_running(bus, hint_priority, priority, ret);
2761 if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2762 bus_enter_closing(bus);
2763 r = 1;
2764
2765 if (ret)
2766 *ret = NULL;
2767 }
2768
2769 return r;
2770
2771 case BUS_CLOSING:
2772 return process_closing(bus, ret);
2773 }
2774
2775 assert_not_reached("Unknown state");
2776 }
2777
2778 _public_ int sd_bus_process(sd_bus *bus, sd_bus_message **ret) {
2779 return bus_process_internal(bus, false, 0, ret);
2780 }
2781
2782 _public_ int sd_bus_process_priority(sd_bus *bus, int64_t priority, sd_bus_message **ret) {
2783 return bus_process_internal(bus, true, priority, ret);
2784 }
2785
2786 static int bus_poll(sd_bus *bus, bool need_more, uint64_t timeout_usec) {
2787 struct pollfd p[2] = {};
2788 int r, e, n;
2789 struct timespec ts;
2790 usec_t m = USEC_INFINITY;
2791
2792 assert(bus);
2793
2794 if (bus->state == BUS_CLOSING)
2795 return 1;
2796
2797 if (!BUS_IS_OPEN(bus->state))
2798 return -ENOTCONN;
2799
2800 e = sd_bus_get_events(bus);
2801 if (e < 0)
2802 return e;
2803
2804 if (need_more)
2805 /* The caller really needs some more data, he doesn't
2806 * care about what's already read, or any timeouts
2807 * except its own. */
2808 e |= POLLIN;
2809 else {
2810 usec_t until;
2811 /* The caller wants to process if there's something to
2812 * process, but doesn't care otherwise */
2813
2814 r = sd_bus_get_timeout(bus, &until);
2815 if (r < 0)
2816 return r;
2817 if (r > 0) {
2818 usec_t nw;
2819 nw = now(CLOCK_MONOTONIC);
2820 m = until > nw ? until - nw : 0;
2821 }
2822 }
2823
2824 if (timeout_usec != (uint64_t) -1 && (m == (uint64_t) -1 || timeout_usec < m))
2825 m = timeout_usec;
2826
2827 p[0].fd = bus->input_fd;
2828 if (bus->output_fd == bus->input_fd) {
2829 p[0].events = e;
2830 n = 1;
2831 } else {
2832 p[0].events = e & POLLIN;
2833 p[1].fd = bus->output_fd;
2834 p[1].events = e & POLLOUT;
2835 n = 2;
2836 }
2837
2838 r = ppoll(p, n, m == (uint64_t) -1 ? NULL : timespec_store(&ts, m), NULL);
2839 if (r < 0)
2840 return -errno;
2841
2842 return r > 0 ? 1 : 0;
2843 }
2844
2845 _public_ int sd_bus_wait(sd_bus *bus, uint64_t timeout_usec) {
2846
2847 assert_return(bus, -EINVAL);
2848 assert_return(!bus_pid_changed(bus), -ECHILD);
2849
2850 if (bus->state == BUS_CLOSING)
2851 return 0;
2852
2853 if (!BUS_IS_OPEN(bus->state))
2854 return -ENOTCONN;
2855
2856 if (bus->rqueue_size > 0)
2857 return 0;
2858
2859 return bus_poll(bus, false, timeout_usec);
2860 }
2861
2862 _public_ int sd_bus_flush(sd_bus *bus) {
2863 int r;
2864
2865 assert_return(bus, -EINVAL);
2866 assert_return(!bus_pid_changed(bus), -ECHILD);
2867
2868 if (bus->state == BUS_CLOSING)
2869 return 0;
2870
2871 if (!BUS_IS_OPEN(bus->state))
2872 return -ENOTCONN;
2873
2874 r = bus_ensure_running(bus);
2875 if (r < 0)
2876 return r;
2877
2878 if (bus->wqueue_size <= 0)
2879 return 0;
2880
2881 for (;;) {
2882 r = dispatch_wqueue(bus);
2883 if (r < 0) {
2884 if (r == -ENOTCONN || r == -ECONNRESET || r == -EPIPE || r == -ESHUTDOWN) {
2885 bus_enter_closing(bus);
2886 return -ECONNRESET;
2887 }
2888
2889 return r;
2890 }
2891
2892 if (bus->wqueue_size <= 0)
2893 return 0;
2894
2895 r = bus_poll(bus, false, (uint64_t) -1);
2896 if (r < 0)
2897 return r;
2898 }
2899 }
2900
2901 _public_ int sd_bus_add_filter(
2902 sd_bus *bus,
2903 sd_bus_slot **slot,
2904 sd_bus_message_handler_t callback,
2905 void *userdata) {
2906
2907 sd_bus_slot *s;
2908
2909 assert_return(bus, -EINVAL);
2910 assert_return(callback, -EINVAL);
2911 assert_return(!bus_pid_changed(bus), -ECHILD);
2912
2913 s = bus_slot_allocate(bus, !slot, BUS_FILTER_CALLBACK, sizeof(struct filter_callback), userdata);
2914 if (!s)
2915 return -ENOMEM;
2916
2917 s->filter_callback.callback = callback;
2918
2919 bus->filter_callbacks_modified = true;
2920 LIST_PREPEND(callbacks, bus->filter_callbacks, &s->filter_callback);
2921
2922 if (slot)
2923 *slot = s;
2924
2925 return 0;
2926 }
2927
2928 _public_ int sd_bus_add_match(
2929 sd_bus *bus,
2930 sd_bus_slot **slot,
2931 const char *match,
2932 sd_bus_message_handler_t callback,
2933 void *userdata) {
2934
2935 struct bus_match_component *components = NULL;
2936 unsigned n_components = 0;
2937 sd_bus_slot *s = NULL;
2938 int r = 0;
2939
2940 assert_return(bus, -EINVAL);
2941 assert_return(match, -EINVAL);
2942 assert_return(!bus_pid_changed(bus), -ECHILD);
2943
2944 r = bus_match_parse(match, &components, &n_components);
2945 if (r < 0)
2946 goto finish;
2947
2948 s = bus_slot_allocate(bus, !slot, BUS_MATCH_CALLBACK, sizeof(struct match_callback), userdata);
2949 if (!s) {
2950 r = -ENOMEM;
2951 goto finish;
2952 }
2953
2954 s->match_callback.callback = callback;
2955 s->match_callback.cookie = ++bus->match_cookie;
2956
2957 if (bus->bus_client) {
2958 enum bus_match_scope scope;
2959
2960 scope = bus_match_get_scope(components, n_components);
2961
2962 /* Do not install server-side matches for matches
2963 * against the local service, interface or bus
2964 * path. */
2965 if (scope != BUS_MATCH_LOCAL) {
2966
2967 if (!bus->is_kernel) {
2968 /* When this is not a kernel transport, we
2969 * store the original match string, so that we
2970 * can use it to remove the match again */
2971
2972 s->match_callback.match_string = strdup(match);
2973 if (!s->match_callback.match_string) {
2974 r = -ENOMEM;
2975 goto finish;
2976 }
2977 }
2978
2979 r = bus_add_match_internal(bus, s->match_callback.match_string, components, n_components, s->match_callback.cookie);
2980 if (r < 0)
2981 goto finish;
2982
2983 s->match_added = true;
2984 }
2985 }
2986
2987 bus->match_callbacks_modified = true;
2988 r = bus_match_add(&bus->match_callbacks, components, n_components, &s->match_callback);
2989 if (r < 0)
2990 goto finish;
2991
2992 if (slot)
2993 *slot = s;
2994 s = NULL;
2995
2996 finish:
2997 bus_match_parse_free(components, n_components);
2998 sd_bus_slot_unref(s);
2999
3000 return r;
3001 }
3002
3003 int bus_remove_match_by_string(
3004 sd_bus *bus,
3005 const char *match,
3006 sd_bus_message_handler_t callback,
3007 void *userdata) {
3008
3009 struct bus_match_component *components = NULL;
3010 unsigned n_components = 0;
3011 struct match_callback *c;
3012 int r = 0;
3013
3014 assert_return(bus, -EINVAL);
3015 assert_return(match, -EINVAL);
3016 assert_return(!bus_pid_changed(bus), -ECHILD);
3017
3018 r = bus_match_parse(match, &components, &n_components);
3019 if (r < 0)
3020 goto finish;
3021
3022 r = bus_match_find(&bus->match_callbacks, components, n_components, NULL, NULL, &c);
3023 if (r <= 0)
3024 goto finish;
3025
3026 sd_bus_slot_unref(container_of(c, sd_bus_slot, match_callback));
3027
3028 finish:
3029 bus_match_parse_free(components, n_components);
3030
3031 return r;
3032 }
3033
3034 bool bus_pid_changed(sd_bus *bus) {
3035 assert(bus);
3036
3037 /* We don't support people creating a bus connection and
3038 * keeping it around over a fork(). Let's complain. */
3039
3040 return bus->original_pid != getpid();
3041 }
3042
3043 static int io_callback(sd_event_source *s, int fd, uint32_t revents, 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 time_callback(sd_event_source *s, uint64_t usec, void *userdata) {
3057 sd_bus *bus = userdata;
3058 int r;
3059
3060 assert(bus);
3061
3062 r = sd_bus_process(bus, NULL);
3063 if (r < 0)
3064 return r;
3065
3066 return 1;
3067 }
3068
3069 static int prepare_callback(sd_event_source *s, void *userdata) {
3070 sd_bus *bus = userdata;
3071 int r, e;
3072 usec_t until;
3073
3074 assert(s);
3075 assert(bus);
3076
3077 e = sd_bus_get_events(bus);
3078 if (e < 0)
3079 return e;
3080
3081 if (bus->output_fd != bus->input_fd) {
3082
3083 r = sd_event_source_set_io_events(bus->input_io_event_source, e & POLLIN);
3084 if (r < 0)
3085 return r;
3086
3087 r = sd_event_source_set_io_events(bus->output_io_event_source, e & POLLOUT);
3088 if (r < 0)
3089 return r;
3090 } else {
3091 r = sd_event_source_set_io_events(bus->input_io_event_source, e);
3092 if (r < 0)
3093 return r;
3094 }
3095
3096 r = sd_bus_get_timeout(bus, &until);
3097 if (r < 0)
3098 return r;
3099 if (r > 0) {
3100 int j;
3101
3102 j = sd_event_source_set_time(bus->time_event_source, until);
3103 if (j < 0)
3104 return j;
3105 }
3106
3107 r = sd_event_source_set_enabled(bus->time_event_source, r > 0);
3108 if (r < 0)
3109 return r;
3110
3111 return 1;
3112 }
3113
3114 static int quit_callback(sd_event_source *event, void *userdata) {
3115 sd_bus *bus = userdata;
3116
3117 assert(event);
3118
3119 sd_bus_flush(bus);
3120 sd_bus_close(bus);
3121
3122 return 1;
3123 }
3124
3125 static int attach_io_events(sd_bus *bus) {
3126 int r;
3127
3128 assert(bus);
3129
3130 if (bus->input_fd < 0)
3131 return 0;
3132
3133 if (!bus->event)
3134 return 0;
3135
3136 if (!bus->input_io_event_source) {
3137 r = sd_event_add_io(bus->event, &bus->input_io_event_source, bus->input_fd, 0, io_callback, bus);
3138 if (r < 0)
3139 return r;
3140
3141 r = sd_event_source_set_prepare(bus->input_io_event_source, prepare_callback);
3142 if (r < 0)
3143 return r;
3144
3145 r = sd_event_source_set_priority(bus->input_io_event_source, bus->event_priority);
3146 if (r < 0)
3147 return r;
3148
3149 r = sd_event_source_set_description(bus->input_io_event_source, "bus-input");
3150 } else
3151 r = sd_event_source_set_io_fd(bus->input_io_event_source, bus->input_fd);
3152
3153 if (r < 0)
3154 return r;
3155
3156 if (bus->output_fd != bus->input_fd) {
3157 assert(bus->output_fd >= 0);
3158
3159 if (!bus->output_io_event_source) {
3160 r = sd_event_add_io(bus->event, &bus->output_io_event_source, bus->output_fd, 0, io_callback, bus);
3161 if (r < 0)
3162 return r;
3163
3164 r = sd_event_source_set_priority(bus->output_io_event_source, bus->event_priority);
3165 if (r < 0)
3166 return r;
3167
3168 r = sd_event_source_set_description(bus->input_io_event_source, "bus-output");
3169 } else
3170 r = sd_event_source_set_io_fd(bus->output_io_event_source, bus->output_fd);
3171
3172 if (r < 0)
3173 return r;
3174 }
3175
3176 return 0;
3177 }
3178
3179 static void detach_io_events(sd_bus *bus) {
3180 assert(bus);
3181
3182 if (bus->input_io_event_source) {
3183 sd_event_source_set_enabled(bus->input_io_event_source, SD_EVENT_OFF);
3184 bus->input_io_event_source = sd_event_source_unref(bus->input_io_event_source);
3185 }
3186
3187 if (bus->output_io_event_source) {
3188 sd_event_source_set_enabled(bus->output_io_event_source, SD_EVENT_OFF);
3189 bus->output_io_event_source = sd_event_source_unref(bus->output_io_event_source);
3190 }
3191 }
3192
3193 _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) {
3194 int r;
3195
3196 assert_return(bus, -EINVAL);
3197 assert_return(!bus->event, -EBUSY);
3198
3199 assert(!bus->input_io_event_source);
3200 assert(!bus->output_io_event_source);
3201 assert(!bus->time_event_source);
3202
3203 if (event)
3204 bus->event = sd_event_ref(event);
3205 else {
3206 r = sd_event_default(&bus->event);
3207 if (r < 0)
3208 return r;
3209 }
3210
3211 bus->event_priority = priority;
3212
3213 r = sd_event_add_time(bus->event, &bus->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, bus);
3214 if (r < 0)
3215 goto fail;
3216
3217 r = sd_event_source_set_priority(bus->time_event_source, priority);
3218 if (r < 0)
3219 goto fail;
3220
3221 r = sd_event_source_set_description(bus->time_event_source, "bus-time");
3222 if (r < 0)
3223 goto fail;
3224
3225 r = sd_event_add_exit(bus->event, &bus->quit_event_source, quit_callback, bus);
3226 if (r < 0)
3227 goto fail;
3228
3229 r = sd_event_source_set_description(bus->quit_event_source, "bus-exit");
3230 if (r < 0)
3231 goto fail;
3232
3233 r = attach_io_events(bus);
3234 if (r < 0)
3235 goto fail;
3236
3237 return 0;
3238
3239 fail:
3240 sd_bus_detach_event(bus);
3241 return r;
3242 }
3243
3244 _public_ int sd_bus_detach_event(sd_bus *bus) {
3245 assert_return(bus, -EINVAL);
3246
3247 if (!bus->event)
3248 return 0;
3249
3250 detach_io_events(bus);
3251
3252 if (bus->time_event_source) {
3253 sd_event_source_set_enabled(bus->time_event_source, SD_EVENT_OFF);
3254 bus->time_event_source = sd_event_source_unref(bus->time_event_source);
3255 }
3256
3257 if (bus->quit_event_source) {
3258 sd_event_source_set_enabled(bus->quit_event_source, SD_EVENT_OFF);
3259 bus->quit_event_source = sd_event_source_unref(bus->quit_event_source);
3260 }
3261
3262 bus->event = sd_event_unref(bus->event);
3263 return 1;
3264 }
3265
3266 _public_ sd_event* sd_bus_get_event(sd_bus *bus) {
3267 assert_return(bus, NULL);
3268
3269 return bus->event;
3270 }
3271
3272 _public_ sd_bus_message* sd_bus_get_current_message(sd_bus *bus) {
3273 assert_return(bus, NULL);
3274
3275 return bus->current_message;
3276 }
3277
3278 _public_ sd_bus_slot* sd_bus_get_current_slot(sd_bus *bus) {
3279 assert_return(bus, NULL);
3280
3281 return bus->current_slot;
3282 }
3283
3284 _public_ sd_bus_message_handler_t sd_bus_get_current_handler(sd_bus *bus) {
3285 assert_return(bus, NULL);
3286
3287 return bus->current_handler;
3288 }
3289
3290 _public_ void* sd_bus_get_current_userdata(sd_bus *bus) {
3291 assert_return(bus, NULL);
3292
3293 return bus->current_userdata;
3294 }
3295
3296 static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) {
3297 sd_bus *b = NULL;
3298 int r;
3299
3300 assert(bus_open);
3301 assert(default_bus);
3302
3303 if (!ret)
3304 return !!*default_bus;
3305
3306 if (*default_bus) {
3307 *ret = sd_bus_ref(*default_bus);
3308 return 0;
3309 }
3310
3311 r = bus_open(&b);
3312 if (r < 0)
3313 return r;
3314
3315 b->default_bus_ptr = default_bus;
3316 b->tid = gettid();
3317 *default_bus = b;
3318
3319 *ret = b;
3320 return 1;
3321 }
3322
3323 _public_ int sd_bus_default_system(sd_bus **ret) {
3324 static thread_local sd_bus *default_system_bus = NULL;
3325
3326 return bus_default(sd_bus_open_system, &default_system_bus, ret);
3327 }
3328
3329 _public_ int sd_bus_default_user(sd_bus **ret) {
3330 static thread_local sd_bus *default_user_bus = NULL;
3331
3332 return bus_default(sd_bus_open_user, &default_user_bus, ret);
3333 }
3334
3335 _public_ int sd_bus_default(sd_bus **ret) {
3336
3337 const char *e;
3338
3339 /* Let's try our best to reuse another cached connection. If
3340 * the starter bus type is set, connect via our normal
3341 * connection logic, ignoring $DBUS_STARTER_ADDRESS, so that
3342 * we can share the connection with the user/system default
3343 * bus. */
3344
3345 e = secure_getenv("DBUS_STARTER_BUS_TYPE");
3346 if (e) {
3347 if (streq(e, "system"))
3348 return sd_bus_default_system(ret);
3349 else if (STR_IN_SET(e, "user", "session"))
3350 return sd_bus_default_user(ret);
3351 }
3352
3353 /* No type is specified, so we have not other option than to
3354 * use the starter address if it is set. */
3355
3356 e = secure_getenv("DBUS_STARTER_ADDRESS");
3357 if (e) {
3358 static thread_local sd_bus *default_starter_bus = NULL;
3359
3360 return bus_default(sd_bus_open, &default_starter_bus, ret);
3361 }
3362
3363 /* Finally, if nothing is set use the cached connection for
3364 * the right scope */
3365
3366 if (cg_pid_get_owner_uid(0, NULL) >= 0)
3367 return sd_bus_default_user(ret);
3368 else
3369 return sd_bus_default_system(ret);
3370 }
3371
3372 _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) {
3373 assert_return(b, -EINVAL);
3374 assert_return(tid, -EINVAL);
3375 assert_return(!bus_pid_changed(b), -ECHILD);
3376
3377 if (b->tid != 0) {
3378 *tid = b->tid;
3379 return 0;
3380 }
3381
3382 if (b->event)
3383 return sd_event_get_tid(b->event, tid);
3384
3385 return -ENXIO;
3386 }
3387
3388 _public_ int sd_bus_path_encode(const char *prefix, const char *external_id, char **ret_path) {
3389 _cleanup_free_ char *e = NULL;
3390 char *ret;
3391
3392 assert_return(object_path_is_valid(prefix), -EINVAL);
3393 assert_return(external_id, -EINVAL);
3394 assert_return(ret_path, -EINVAL);
3395
3396 e = bus_label_escape(external_id);
3397 if (!e)
3398 return -ENOMEM;
3399
3400 ret = strjoin(prefix, "/", e, NULL);
3401 if (!ret)
3402 return -ENOMEM;
3403
3404 *ret_path = ret;
3405 return 0;
3406 }
3407
3408 _public_ int sd_bus_path_decode(const char *path, const char *prefix, char **external_id) {
3409 const char *e;
3410 char *ret;
3411
3412 assert_return(object_path_is_valid(path), -EINVAL);
3413 assert_return(object_path_is_valid(prefix), -EINVAL);
3414 assert_return(external_id, -EINVAL);
3415
3416 e = object_path_startswith(path, prefix);
3417 if (!e) {
3418 *external_id = NULL;
3419 return 0;
3420 }
3421
3422 ret = bus_label_unescape(e);
3423 if (!ret)
3424 return -ENOMEM;
3425
3426 *external_id = ret;
3427 return 1;
3428 }
3429
3430 _public_ int sd_bus_try_close(sd_bus *bus) {
3431 int r;
3432
3433 assert_return(bus, -EINVAL);
3434 assert_return(!bus_pid_changed(bus), -ECHILD);
3435
3436 if (!bus->is_kernel)
3437 return -EOPNOTSUPP;
3438
3439 if (!BUS_IS_OPEN(bus->state))
3440 return -ENOTCONN;
3441
3442 if (bus->rqueue_size > 0)
3443 return -EBUSY;
3444
3445 if (bus->wqueue_size > 0)
3446 return -EBUSY;
3447
3448 r = bus_kernel_try_close(bus);
3449 if (r < 0)
3450 return r;
3451
3452 sd_bus_close(bus);
3453 return 0;
3454 }
3455
3456 _public_ int sd_bus_get_description(sd_bus *bus, const char **description) {
3457 assert_return(bus, -EINVAL);
3458 assert_return(description, -EINVAL);
3459 assert_return(bus->description, -ENXIO);
3460 assert_return(!bus_pid_changed(bus), -ECHILD);
3461
3462 *description = bus->description;
3463 return 0;
3464 }
3465
3466 int bus_get_root_path(sd_bus *bus) {
3467 int r;
3468
3469 if (bus->cgroup_root)
3470 return 0;
3471
3472 r = cg_get_root_path(&bus->cgroup_root);
3473 if (r == -ENOENT) {
3474 bus->cgroup_root = strdup("/");
3475 if (!bus->cgroup_root)
3476 return -ENOMEM;
3477
3478 r = 0;
3479 }
3480
3481 return r;
3482 }
3483
3484 _public_ int sd_bus_get_scope(sd_bus *bus, const char **scope) {
3485 int r;
3486
3487 assert_return(bus, -EINVAL);
3488 assert_return(scope, -EINVAL);
3489 assert_return(!bus_pid_changed(bus), -ECHILD);
3490
3491 if (bus->is_kernel) {
3492 _cleanup_free_ char *n = NULL;
3493 const char *dash;
3494
3495 r = bus_kernel_get_bus_name(bus, &n);
3496 if (r < 0)
3497 return r;
3498
3499 if (streq(n, "0-system")) {
3500 *scope = "system";
3501 return 0;
3502 }
3503
3504 dash = strchr(n, '-');
3505 if (streq_ptr(dash, "-user")) {
3506 *scope = "user";
3507 return 0;
3508 }
3509 }
3510
3511 if (bus->is_user) {
3512 *scope = "user";
3513 return 0;
3514 }
3515
3516 if (bus->is_system) {
3517 *scope = "system";
3518 return 0;
3519 }
3520
3521 return -ENODATA;
3522 }
3523
3524 _public_ int sd_bus_get_address(sd_bus *bus, const char **address) {
3525
3526 assert_return(bus, -EINVAL);
3527 assert_return(address, -EINVAL);
3528 assert_return(!bus_pid_changed(bus), -ECHILD);
3529
3530 if (bus->address) {
3531 *address = bus->address;
3532 return 0;
3533 }
3534
3535 return -ENODATA;
3536 }
3537
3538 _public_ int sd_bus_get_creds_mask(sd_bus *bus, uint64_t *mask) {
3539 assert_return(bus, -EINVAL);
3540 assert_return(mask, -EINVAL);
3541 assert_return(!bus_pid_changed(bus), -ECHILD);
3542
3543 *mask = bus->creds_mask;
3544 return 0;
3545 }
3546
3547 _public_ int sd_bus_is_bus_client(sd_bus *bus) {
3548 assert_return(bus, -EINVAL);
3549 assert_return(!bus_pid_changed(bus), -ECHILD);
3550
3551 return bus->bus_client;
3552 }
3553
3554 _public_ int sd_bus_is_server(sd_bus *bus) {
3555 assert_return(bus, -EINVAL);
3556 assert_return(!bus_pid_changed(bus), -ECHILD);
3557
3558 return bus->is_server;
3559 }
3560
3561 _public_ int sd_bus_is_anonymous(sd_bus *bus) {
3562 assert_return(bus, -EINVAL);
3563 assert_return(!bus_pid_changed(bus), -ECHILD);
3564
3565 return bus->anonymous_auth;
3566 }
3567
3568 _public_ int sd_bus_is_trusted(sd_bus *bus) {
3569 assert_return(bus, -EINVAL);
3570 assert_return(!bus_pid_changed(bus), -ECHILD);
3571
3572 return bus->trusted;
3573 }
3574
3575 _public_ int sd_bus_is_monitor(sd_bus *bus) {
3576 assert_return(bus, -EINVAL);
3577 assert_return(!bus_pid_changed(bus), -ECHILD);
3578
3579 return !!(bus->hello_flags & KDBUS_HELLO_MONITOR);
3580 }