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