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