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