]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/bus-util.c
Merge pull request #8533 from poettering/bootup-shutdown-phase2
[thirdparty/systemd.git] / src / shared / bus-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2013 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <inttypes.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/ioctl.h>
28 #include <sys/resource.h>
29 #include <sys/socket.h>
30 #include <unistd.h>
31
32 #include "sd-bus-protocol.h"
33 #include "sd-bus.h"
34 #include "sd-daemon.h"
35 #include "sd-event.h"
36 #include "sd-id128.h"
37
38 #include "alloc-util.h"
39 #include "bus-internal.h"
40 #include "bus-label.h"
41 #include "bus-message.h"
42 #include "bus-util.h"
43 #include "cap-list.h"
44 #include "cgroup-util.h"
45 #include "def.h"
46 #include "escape.h"
47 #include "fd-util.h"
48 #include "missing.h"
49 #include "mount-util.h"
50 #include "nsflags.h"
51 #include "parse-util.h"
52 #include "proc-cmdline.h"
53 #include "rlimit-util.h"
54 #include "stdio-util.h"
55 #include "strv.h"
56 #include "user-util.h"
57
58 static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
59 sd_event *e = userdata;
60
61 assert(m);
62 assert(e);
63
64 sd_bus_close(sd_bus_message_get_bus(m));
65 sd_event_exit(e, 0);
66
67 return 1;
68 }
69
70 int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name) {
71 const char *match;
72 const char *unique;
73 int r;
74
75 assert(e);
76 assert(bus);
77 assert(name);
78
79 /* We unregister the name here and then wait for the
80 * NameOwnerChanged signal for this event to arrive before we
81 * quit. We do this in order to make sure that any queued
82 * requests are still processed before we really exit. */
83
84 r = sd_bus_get_unique_name(bus, &unique);
85 if (r < 0)
86 return r;
87
88 match = strjoina(
89 "sender='org.freedesktop.DBus',"
90 "type='signal',"
91 "interface='org.freedesktop.DBus',"
92 "member='NameOwnerChanged',"
93 "path='/org/freedesktop/DBus',"
94 "arg0='", name, "',",
95 "arg1='", unique, "',",
96 "arg2=''");
97
98 r = sd_bus_add_match_async(bus, NULL, match, name_owner_change_callback, NULL, e);
99 if (r < 0)
100 return r;
101
102 r = sd_bus_release_name_async(bus, NULL, name, NULL, NULL);
103 if (r < 0)
104 return r;
105
106 return 0;
107 }
108
109 int bus_event_loop_with_idle(
110 sd_event *e,
111 sd_bus *bus,
112 const char *name,
113 usec_t timeout,
114 check_idle_t check_idle,
115 void *userdata) {
116 bool exiting = false;
117 int r, code;
118
119 assert(e);
120 assert(bus);
121 assert(name);
122
123 for (;;) {
124 bool idle;
125
126 r = sd_event_get_state(e);
127 if (r < 0)
128 return r;
129 if (r == SD_EVENT_FINISHED)
130 break;
131
132 if (check_idle)
133 idle = check_idle(userdata);
134 else
135 idle = true;
136
137 r = sd_event_run(e, exiting || !idle ? (uint64_t) -1 : timeout);
138 if (r < 0)
139 return r;
140
141 if (r == 0 && !exiting && idle) {
142
143 r = sd_bus_try_close(bus);
144 if (r == -EBUSY)
145 continue;
146
147 /* Fallback for dbus1 connections: we
148 * unregister the name and wait for the
149 * response to come through for it */
150 if (r == -EOPNOTSUPP) {
151
152 /* Inform the service manager that we
153 * are going down, so that it will
154 * queue all further start requests,
155 * instead of assuming we are already
156 * running. */
157 sd_notify(false, "STOPPING=1");
158
159 r = bus_async_unregister_and_exit(e, bus, name);
160 if (r < 0)
161 return r;
162
163 exiting = true;
164 continue;
165 }
166
167 if (r < 0)
168 return r;
169
170 sd_event_exit(e, 0);
171 break;
172 }
173 }
174
175 r = sd_event_get_exit_code(e, &code);
176 if (r < 0)
177 return r;
178
179 return code;
180 }
181
182 int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error) {
183 _cleanup_(sd_bus_message_unrefp) sd_bus_message *rep = NULL;
184 int r, has_owner = 0;
185
186 assert(c);
187 assert(name);
188
189 r = sd_bus_call_method(c,
190 "org.freedesktop.DBus",
191 "/org/freedesktop/dbus",
192 "org.freedesktop.DBus",
193 "NameHasOwner",
194 error,
195 &rep,
196 "s",
197 name);
198 if (r < 0)
199 return r;
200
201 r = sd_bus_message_read_basic(rep, 'b', &has_owner);
202 if (r < 0)
203 return sd_bus_error_set_errno(error, r);
204
205 return has_owner;
206 }
207
208 static int check_good_user(sd_bus_message *m, uid_t good_user) {
209 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
210 uid_t sender_uid;
211 int r;
212
213 assert(m);
214
215 if (good_user == UID_INVALID)
216 return 0;
217
218 r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_EUID, &creds);
219 if (r < 0)
220 return r;
221
222 /* Don't trust augmented credentials for authorization */
223 assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_EUID) == 0, -EPERM);
224
225 r = sd_bus_creds_get_euid(creds, &sender_uid);
226 if (r < 0)
227 return r;
228
229 return sender_uid == good_user;
230 }
231
232 int bus_test_polkit(
233 sd_bus_message *call,
234 int capability,
235 const char *action,
236 const char **details,
237 uid_t good_user,
238 bool *_challenge,
239 sd_bus_error *e) {
240
241 int r;
242
243 assert(call);
244 assert(action);
245
246 /* Tests non-interactively! */
247
248 r = check_good_user(call, good_user);
249 if (r != 0)
250 return r;
251
252 r = sd_bus_query_sender_privilege(call, capability);
253 if (r < 0)
254 return r;
255 else if (r > 0)
256 return 1;
257 #if ENABLE_POLKIT
258 else {
259 _cleanup_(sd_bus_message_unrefp) sd_bus_message *request = NULL;
260 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
261 int authorized = false, challenge = false;
262 const char *sender, **k, **v;
263
264 sender = sd_bus_message_get_sender(call);
265 if (!sender)
266 return -EBADMSG;
267
268 r = sd_bus_message_new_method_call(
269 call->bus,
270 &request,
271 "org.freedesktop.PolicyKit1",
272 "/org/freedesktop/PolicyKit1/Authority",
273 "org.freedesktop.PolicyKit1.Authority",
274 "CheckAuthorization");
275 if (r < 0)
276 return r;
277
278 r = sd_bus_message_append(
279 request,
280 "(sa{sv})s",
281 "system-bus-name", 1, "name", "s", sender,
282 action);
283 if (r < 0)
284 return r;
285
286 r = sd_bus_message_open_container(request, 'a', "{ss}");
287 if (r < 0)
288 return r;
289
290 STRV_FOREACH_PAIR(k, v, details) {
291 r = sd_bus_message_append(request, "{ss}", *k, *v);
292 if (r < 0)
293 return r;
294 }
295
296 r = sd_bus_message_close_container(request);
297 if (r < 0)
298 return r;
299
300 r = sd_bus_message_append(request, "us", 0, NULL);
301 if (r < 0)
302 return r;
303
304 r = sd_bus_call(call->bus, request, 0, e, &reply);
305 if (r < 0) {
306 /* Treat no PK available as access denied */
307 if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN)) {
308 sd_bus_error_free(e);
309 return -EACCES;
310 }
311
312 return r;
313 }
314
315 r = sd_bus_message_enter_container(reply, 'r', "bba{ss}");
316 if (r < 0)
317 return r;
318
319 r = sd_bus_message_read(reply, "bb", &authorized, &challenge);
320 if (r < 0)
321 return r;
322
323 if (authorized)
324 return 1;
325
326 if (_challenge) {
327 *_challenge = challenge;
328 return 0;
329 }
330 }
331 #endif
332
333 return -EACCES;
334 }
335
336 #if ENABLE_POLKIT
337
338 typedef struct AsyncPolkitQuery {
339 sd_bus_message *request, *reply;
340 sd_bus_message_handler_t callback;
341 void *userdata;
342 sd_bus_slot *slot;
343 Hashmap *registry;
344 } AsyncPolkitQuery;
345
346 static void async_polkit_query_free(AsyncPolkitQuery *q) {
347
348 if (!q)
349 return;
350
351 sd_bus_slot_unref(q->slot);
352
353 if (q->registry && q->request)
354 hashmap_remove(q->registry, q->request);
355
356 sd_bus_message_unref(q->request);
357 sd_bus_message_unref(q->reply);
358
359 free(q);
360 }
361
362 static int async_polkit_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
363 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
364 AsyncPolkitQuery *q = userdata;
365 int r;
366
367 assert(reply);
368 assert(q);
369
370 q->slot = sd_bus_slot_unref(q->slot);
371 q->reply = sd_bus_message_ref(reply);
372
373 r = sd_bus_message_rewind(q->request, true);
374 if (r < 0) {
375 r = sd_bus_reply_method_errno(q->request, r, NULL);
376 goto finish;
377 }
378
379 r = q->callback(q->request, q->userdata, &error_buffer);
380 r = bus_maybe_reply_error(q->request, r, &error_buffer);
381
382 finish:
383 async_polkit_query_free(q);
384
385 return r;
386 }
387
388 #endif
389
390 int bus_verify_polkit_async(
391 sd_bus_message *call,
392 int capability,
393 const char *action,
394 const char **details,
395 bool interactive,
396 uid_t good_user,
397 Hashmap **registry,
398 sd_bus_error *error) {
399
400 #if ENABLE_POLKIT
401 _cleanup_(sd_bus_message_unrefp) sd_bus_message *pk = NULL;
402 AsyncPolkitQuery *q;
403 const char *sender, **k, **v;
404 sd_bus_message_handler_t callback;
405 void *userdata;
406 int c;
407 #endif
408 int r;
409
410 assert(call);
411 assert(action);
412 assert(registry);
413
414 r = check_good_user(call, good_user);
415 if (r != 0)
416 return r;
417
418 #if ENABLE_POLKIT
419 q = hashmap_get(*registry, call);
420 if (q) {
421 int authorized, challenge;
422
423 /* This is the second invocation of this function, and
424 * there's already a response from polkit, let's
425 * process it */
426 assert(q->reply);
427
428 if (sd_bus_message_is_method_error(q->reply, NULL)) {
429 const sd_bus_error *e;
430
431 /* Copy error from polkit reply */
432 e = sd_bus_message_get_error(q->reply);
433 sd_bus_error_copy(error, e);
434
435 /* Treat no PK available as access denied */
436 if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN))
437 return -EACCES;
438
439 return -sd_bus_error_get_errno(e);
440 }
441
442 r = sd_bus_message_enter_container(q->reply, 'r', "bba{ss}");
443 if (r >= 0)
444 r = sd_bus_message_read(q->reply, "bb", &authorized, &challenge);
445
446 if (r < 0)
447 return r;
448
449 if (authorized)
450 return 1;
451
452 if (challenge)
453 return sd_bus_error_set(error, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED, "Interactive authentication required.");
454
455 return -EACCES;
456 }
457 #endif
458
459 r = sd_bus_query_sender_privilege(call, capability);
460 if (r < 0)
461 return r;
462 else if (r > 0)
463 return 1;
464
465 #if ENABLE_POLKIT
466 if (sd_bus_get_current_message(call->bus) != call)
467 return -EINVAL;
468
469 callback = sd_bus_get_current_handler(call->bus);
470 if (!callback)
471 return -EINVAL;
472
473 userdata = sd_bus_get_current_userdata(call->bus);
474
475 sender = sd_bus_message_get_sender(call);
476 if (!sender)
477 return -EBADMSG;
478
479 c = sd_bus_message_get_allow_interactive_authorization(call);
480 if (c < 0)
481 return c;
482 if (c > 0)
483 interactive = true;
484
485 r = hashmap_ensure_allocated(registry, NULL);
486 if (r < 0)
487 return r;
488
489 r = sd_bus_message_new_method_call(
490 call->bus,
491 &pk,
492 "org.freedesktop.PolicyKit1",
493 "/org/freedesktop/PolicyKit1/Authority",
494 "org.freedesktop.PolicyKit1.Authority",
495 "CheckAuthorization");
496 if (r < 0)
497 return r;
498
499 r = sd_bus_message_append(
500 pk,
501 "(sa{sv})s",
502 "system-bus-name", 1, "name", "s", sender,
503 action);
504 if (r < 0)
505 return r;
506
507 r = sd_bus_message_open_container(pk, 'a', "{ss}");
508 if (r < 0)
509 return r;
510
511 STRV_FOREACH_PAIR(k, v, details) {
512 r = sd_bus_message_append(pk, "{ss}", *k, *v);
513 if (r < 0)
514 return r;
515 }
516
517 r = sd_bus_message_close_container(pk);
518 if (r < 0)
519 return r;
520
521 r = sd_bus_message_append(pk, "us", !!interactive, NULL);
522 if (r < 0)
523 return r;
524
525 q = new0(AsyncPolkitQuery, 1);
526 if (!q)
527 return -ENOMEM;
528
529 q->request = sd_bus_message_ref(call);
530 q->callback = callback;
531 q->userdata = userdata;
532
533 r = hashmap_put(*registry, call, q);
534 if (r < 0) {
535 async_polkit_query_free(q);
536 return r;
537 }
538
539 q->registry = *registry;
540
541 r = sd_bus_call_async(call->bus, &q->slot, pk, async_polkit_callback, q, 0);
542 if (r < 0) {
543 async_polkit_query_free(q);
544 return r;
545 }
546
547 return 0;
548 #endif
549
550 return -EACCES;
551 }
552
553 void bus_verify_polkit_async_registry_free(Hashmap *registry) {
554 #if ENABLE_POLKIT
555 hashmap_free_with_destructor(registry, async_polkit_query_free);
556 #endif
557 }
558
559 int bus_check_peercred(sd_bus *c) {
560 struct ucred ucred;
561 int fd, r;
562
563 assert(c);
564
565 fd = sd_bus_get_fd(c);
566 if (fd < 0)
567 return fd;
568
569 r = getpeercred(fd, &ucred);
570 if (r < 0)
571 return r;
572
573 if (ucred.uid != 0 && ucred.uid != geteuid())
574 return -EPERM;
575
576 return 1;
577 }
578
579 int bus_connect_system_systemd(sd_bus **_bus) {
580 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
581 int r;
582
583 assert(_bus);
584
585 if (geteuid() != 0)
586 return sd_bus_default_system(_bus);
587
588 /* If we are root then let's talk directly to the system
589 * instance, instead of going via the bus */
590
591 r = sd_bus_new(&bus);
592 if (r < 0)
593 return r;
594
595 r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
596 if (r < 0)
597 return r;
598
599 r = sd_bus_start(bus);
600 if (r < 0)
601 return sd_bus_default_system(_bus);
602
603 r = bus_check_peercred(bus);
604 if (r < 0)
605 return r;
606
607 *_bus = bus;
608 bus = NULL;
609
610 return 0;
611 }
612
613 int bus_connect_user_systemd(sd_bus **_bus) {
614 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
615 _cleanup_free_ char *ee = NULL;
616 const char *e;
617 int r;
618
619 assert(_bus);
620
621 e = secure_getenv("XDG_RUNTIME_DIR");
622 if (!e)
623 return sd_bus_default_user(_bus);
624
625 ee = bus_address_escape(e);
626 if (!ee)
627 return -ENOMEM;
628
629 r = sd_bus_new(&bus);
630 if (r < 0)
631 return r;
632
633 bus->address = strjoin("unix:path=", ee, "/systemd/private");
634 if (!bus->address)
635 return -ENOMEM;
636
637 r = sd_bus_start(bus);
638 if (r < 0)
639 return sd_bus_default_user(_bus);
640
641 r = bus_check_peercred(bus);
642 if (r < 0)
643 return r;
644
645 *_bus = bus;
646 bus = NULL;
647
648 return 0;
649 }
650
651 #define print_property(name, fmt, ...) \
652 do { \
653 if (value) \
654 printf(fmt "\n", __VA_ARGS__); \
655 else \
656 printf("%s=" fmt "\n", name, __VA_ARGS__); \
657 } while (0)
658
659 int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all) {
660 char type;
661 const char *contents;
662 int r;
663
664 assert(name);
665 assert(m);
666
667 r = sd_bus_message_peek_type(m, &type, &contents);
668 if (r < 0)
669 return r;
670
671 switch (type) {
672
673 case SD_BUS_TYPE_STRING: {
674 const char *s;
675
676 r = sd_bus_message_read_basic(m, type, &s);
677 if (r < 0)
678 return r;
679
680 if (all || !isempty(s)) {
681 bool good;
682
683 /* This property has a single value, so we need to take
684 * care not to print a new line, everything else is OK. */
685 good = !strchr(s, '\n');
686 print_property(name, "%s", good ? s : "[unprintable]");
687 }
688
689 return 1;
690 }
691
692 case SD_BUS_TYPE_BOOLEAN: {
693 int b;
694
695 r = sd_bus_message_read_basic(m, type, &b);
696 if (r < 0)
697 return r;
698
699 print_property(name, "%s", yes_no(b));
700
701 return 1;
702 }
703
704 case SD_BUS_TYPE_UINT64: {
705 uint64_t u;
706
707 r = sd_bus_message_read_basic(m, type, &u);
708 if (r < 0)
709 return r;
710
711 /* Yes, heuristics! But we can change this check
712 * should it turn out to not be sufficient */
713
714 if (endswith(name, "Timestamp") || STR_IN_SET(name, "NextElapseUSecRealtime", "LastTriggerUSec")) {
715 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
716
717 t = format_timestamp(timestamp, sizeof(timestamp), u);
718 if (t || all)
719 print_property(name, "%s", strempty(t));
720
721 } else if (strstr(name, "USec")) {
722 char timespan[FORMAT_TIMESPAN_MAX];
723
724 print_property(name, "%s", format_timespan(timespan, sizeof(timespan), u, 0));
725 } else if (streq(name, "RestrictNamespaces")) {
726 _cleanup_free_ char *s = NULL;
727 const char *result;
728
729 if ((u & NAMESPACE_FLAGS_ALL) == 0)
730 result = "yes";
731 else if ((u & NAMESPACE_FLAGS_ALL) == NAMESPACE_FLAGS_ALL)
732 result = "no";
733 else {
734 r = namespace_flag_to_string_many(u, &s);
735 if (r < 0)
736 return r;
737
738 result = s;
739 }
740
741 print_property(name, "%s", result);
742
743 } else if (streq(name, "MountFlags")) {
744 const char *result;
745
746 result = mount_propagation_flags_to_string(u);
747 if (!result)
748 return -EINVAL;
749
750 print_property(name, "%s", result);
751
752 } else if (STR_IN_SET(name, "CapabilityBoundingSet", "AmbientCapabilities")) {
753 _cleanup_free_ char *s = NULL;
754
755 r = capability_set_to_string_alloc(u, &s);
756 if (r < 0)
757 return r;
758
759 print_property(name, "%s", s);
760
761 } else if ((STR_IN_SET(name, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u == CGROUP_WEIGHT_INVALID) ||
762 (STR_IN_SET(name, "CPUShares", "StartupCPUShares") && u == CGROUP_CPU_SHARES_INVALID) ||
763 (STR_IN_SET(name, "BlockIOWeight", "StartupBlockIOWeight") && u == CGROUP_BLKIO_WEIGHT_INVALID) ||
764 (STR_IN_SET(name, "MemoryCurrent", "TasksCurrent") && u == (uint64_t) -1) ||
765 (endswith(name, "NSec") && u == (uint64_t) -1))
766
767 print_property(name, "%s", "[not set]");
768
769 else if ((STR_IN_SET(name, "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u == CGROUP_LIMIT_MAX) ||
770 (STR_IN_SET(name, "TasksMax", "DefaultTasksMax") && u == (uint64_t) -1) ||
771 (startswith(name, "Limit") && u == (uint64_t) -1) ||
772 (startswith(name, "DefaultLimit") && u == (uint64_t) -1))
773
774 print_property(name, "%s", "infinity");
775 else
776 print_property(name, "%"PRIu64, u);
777
778 return 1;
779 }
780
781 case SD_BUS_TYPE_INT64: {
782 int64_t i;
783
784 r = sd_bus_message_read_basic(m, type, &i);
785 if (r < 0)
786 return r;
787
788 print_property(name, "%"PRIi64, i);
789
790 return 1;
791 }
792
793 case SD_BUS_TYPE_UINT32: {
794 uint32_t u;
795
796 r = sd_bus_message_read_basic(m, type, &u);
797 if (r < 0)
798 return r;
799
800 if (strstr(name, "UMask") || strstr(name, "Mode"))
801 print_property(name, "%04o", u);
802 else if (streq(name, "UID")) {
803 if (u == UID_INVALID)
804 print_property(name, "%s", "[not set]");
805 else
806 print_property(name, "%"PRIu32, u);
807 } else if (streq(name, "GID")) {
808 if (u == GID_INVALID)
809 print_property(name, "%s", "[not set]");
810 else
811 print_property(name, "%"PRIu32, u);
812 } else
813 print_property(name, "%"PRIu32, u);
814
815 return 1;
816 }
817
818 case SD_BUS_TYPE_INT32: {
819 int32_t i;
820
821 r = sd_bus_message_read_basic(m, type, &i);
822 if (r < 0)
823 return r;
824
825 print_property(name, "%"PRIi32, i);
826 return 1;
827 }
828
829 case SD_BUS_TYPE_DOUBLE: {
830 double d;
831
832 r = sd_bus_message_read_basic(m, type, &d);
833 if (r < 0)
834 return r;
835
836 print_property(name, "%g", d);
837 return 1;
838 }
839
840 case SD_BUS_TYPE_ARRAY:
841 if (streq(contents, "s")) {
842 bool first = true;
843 const char *str;
844
845 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, contents);
846 if (r < 0)
847 return r;
848
849 while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &str)) > 0) {
850 bool good;
851
852 if (first && !value)
853 printf("%s=", name);
854
855 /* This property has multiple space-separated values, so
856 * neither spaces not newlines can be allowed in a value. */
857 good = str[strcspn(str, " \n")] == '\0';
858
859 printf("%s%s", first ? "" : " ", good ? str : "[unprintable]");
860
861 first = false;
862 }
863 if (r < 0)
864 return r;
865
866 if (first && all && !value)
867 printf("%s=", name);
868 if (!first || all)
869 puts("");
870
871 r = sd_bus_message_exit_container(m);
872 if (r < 0)
873 return r;
874
875 return 1;
876
877 } else if (streq(contents, "y")) {
878 const uint8_t *u;
879 size_t n;
880
881 r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, (const void**) &u, &n);
882 if (r < 0)
883 return r;
884
885 if (all || n > 0) {
886 unsigned int i;
887
888 if (!value)
889 printf("%s=", name);
890
891 for (i = 0; i < n; i++)
892 printf("%02x", u[i]);
893
894 puts("");
895 }
896
897 return 1;
898
899 } else if (streq(contents, "u")) {
900 uint32_t *u;
901 size_t n;
902
903 r = sd_bus_message_read_array(m, SD_BUS_TYPE_UINT32, (const void**) &u, &n);
904 if (r < 0)
905 return r;
906
907 if (all || n > 0) {
908 unsigned int i;
909
910 if (!value)
911 printf("%s=", name);
912
913 for (i = 0; i < n; i++)
914 printf("%08x", u[i]);
915
916 puts("");
917 }
918
919 return 1;
920 }
921
922 break;
923 }
924
925 return 0;
926 }
927
928 int bus_message_print_all_properties(
929 sd_bus_message *m,
930 bus_message_print_t func,
931 char **filter,
932 bool value,
933 bool all,
934 Set **found_properties) {
935
936 int r;
937
938 assert(m);
939
940 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
941 if (r < 0)
942 return r;
943
944 while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
945 const char *name;
946 const char *contents;
947
948 r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &name);
949 if (r < 0)
950 return r;
951
952 if (found_properties) {
953 r = set_ensure_allocated(found_properties, &string_hash_ops);
954 if (r < 0)
955 return log_oom();
956
957 r = set_put(*found_properties, name);
958 if (r < 0 && r != EEXIST)
959 return log_oom();
960 }
961
962 if (!filter || strv_find(filter, name)) {
963 r = sd_bus_message_peek_type(m, NULL, &contents);
964 if (r < 0)
965 return r;
966
967 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
968 if (r < 0)
969 return r;
970
971 if (func)
972 r = func(name, m, value, all);
973 if (!func || r == 0)
974 r = bus_print_property(name, m, value, all);
975 if (r < 0)
976 return r;
977 if (r == 0) {
978 if (all)
979 printf("%s=[unprintable]\n", name);
980 /* skip what we didn't read */
981 r = sd_bus_message_skip(m, contents);
982 if (r < 0)
983 return r;
984 }
985
986 r = sd_bus_message_exit_container(m);
987 if (r < 0)
988 return r;
989 } else {
990 r = sd_bus_message_skip(m, "v");
991 if (r < 0)
992 return r;
993 }
994
995 r = sd_bus_message_exit_container(m);
996 if (r < 0)
997 return r;
998 }
999 if (r < 0)
1000 return r;
1001
1002 r = sd_bus_message_exit_container(m);
1003 if (r < 0)
1004 return r;
1005
1006 return 0;
1007 }
1008
1009 int bus_print_all_properties(
1010 sd_bus *bus,
1011 const char *dest,
1012 const char *path,
1013 bus_message_print_t func,
1014 char **filter,
1015 bool value,
1016 bool all,
1017 Set **found_properties) {
1018
1019 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1020 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1021 int r;
1022
1023 assert(bus);
1024 assert(path);
1025
1026 r = sd_bus_call_method(bus,
1027 dest,
1028 path,
1029 "org.freedesktop.DBus.Properties",
1030 "GetAll",
1031 &error,
1032 &reply,
1033 "s", "");
1034 if (r < 0)
1035 return r;
1036
1037 return bus_message_print_all_properties(reply, func, filter, value, all, found_properties);
1038 }
1039
1040 int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1041 sd_id128_t *p = userdata;
1042 const void *v;
1043 size_t n;
1044 int r;
1045
1046 r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n);
1047 if (r < 0)
1048 return r;
1049
1050 if (n == 0)
1051 *p = SD_ID128_NULL;
1052 else if (n == 16)
1053 memcpy((*p).bytes, v, n);
1054 else
1055 return -EINVAL;
1056
1057 return 0;
1058 }
1059
1060 static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata, bool copy_string) {
1061 char type;
1062 int r;
1063
1064 r = sd_bus_message_peek_type(m, &type, NULL);
1065 if (r < 0)
1066 return r;
1067
1068 switch (type) {
1069
1070 case SD_BUS_TYPE_STRING: {
1071 const char **p = userdata;
1072 const char *s;
1073
1074 r = sd_bus_message_read_basic(m, type, &s);
1075 if (r < 0)
1076 return r;
1077
1078 if (isempty(s))
1079 s = NULL;
1080
1081 if (copy_string)
1082 return free_and_strdup((char **) userdata, s);
1083
1084 *p = s;
1085 return 0;
1086 }
1087
1088 case SD_BUS_TYPE_ARRAY: {
1089 _cleanup_strv_free_ char **l = NULL;
1090 char ***p = userdata;
1091
1092 r = bus_message_read_strv_extend(m, &l);
1093 if (r < 0)
1094 return r;
1095
1096 strv_free(*p);
1097 *p = TAKE_PTR(l);
1098 return 0;
1099 }
1100
1101 case SD_BUS_TYPE_BOOLEAN: {
1102 unsigned b;
1103 bool *p = userdata;
1104
1105 r = sd_bus_message_read_basic(m, type, &b);
1106 if (r < 0)
1107 return r;
1108
1109 *p = !!b;
1110 return 0;
1111 }
1112
1113 case SD_BUS_TYPE_INT32:
1114 case SD_BUS_TYPE_UINT32: {
1115 uint32_t u, *p = userdata;
1116
1117 r = sd_bus_message_read_basic(m, type, &u);
1118 if (r < 0)
1119 return r;
1120
1121 *p = u;
1122 return 0;
1123 }
1124
1125 case SD_BUS_TYPE_INT64:
1126 case SD_BUS_TYPE_UINT64: {
1127 uint64_t t, *p = userdata;
1128
1129 r = sd_bus_message_read_basic(m, type, &t);
1130 if (r < 0)
1131 return r;
1132
1133 *p = t;
1134 return 0;
1135 }
1136
1137 case SD_BUS_TYPE_DOUBLE: {
1138 double d, *p = userdata;
1139
1140 r = sd_bus_message_read_basic(m, type, &d);
1141 if (r < 0)
1142 return r;
1143
1144 *p = d;
1145 return 0;
1146 }}
1147
1148 return -EOPNOTSUPP;
1149 }
1150
1151 int bus_message_map_all_properties(
1152 sd_bus_message *m,
1153 const struct bus_properties_map *map,
1154 bool copy_string,
1155 sd_bus_error *error,
1156 void *userdata) {
1157
1158 int r;
1159
1160 assert(m);
1161 assert(map);
1162
1163 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
1164 if (r < 0)
1165 return r;
1166
1167 while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
1168 const struct bus_properties_map *prop;
1169 const char *member;
1170 const char *contents;
1171 void *v;
1172 unsigned i;
1173
1174 r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member);
1175 if (r < 0)
1176 return r;
1177
1178 for (i = 0, prop = NULL; map[i].member; i++)
1179 if (streq(map[i].member, member)) {
1180 prop = &map[i];
1181 break;
1182 }
1183
1184 if (prop) {
1185 r = sd_bus_message_peek_type(m, NULL, &contents);
1186 if (r < 0)
1187 return r;
1188
1189 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
1190 if (r < 0)
1191 return r;
1192
1193 v = (uint8_t *)userdata + prop->offset;
1194 if (map[i].set)
1195 r = prop->set(sd_bus_message_get_bus(m), member, m, error, v);
1196 else
1197 r = map_basic(sd_bus_message_get_bus(m), member, m, error, v, copy_string);
1198 if (r < 0)
1199 return r;
1200
1201 r = sd_bus_message_exit_container(m);
1202 if (r < 0)
1203 return r;
1204 } else {
1205 r = sd_bus_message_skip(m, "v");
1206 if (r < 0)
1207 return r;
1208 }
1209
1210 r = sd_bus_message_exit_container(m);
1211 if (r < 0)
1212 return r;
1213 }
1214 if (r < 0)
1215 return r;
1216
1217 return sd_bus_message_exit_container(m);
1218 }
1219
1220 int bus_message_map_properties_changed(
1221 sd_bus_message *m,
1222 const struct bus_properties_map *map,
1223 bool copy_string,
1224 sd_bus_error *error,
1225 void *userdata) {
1226
1227 const char *member;
1228 int r, invalidated, i;
1229
1230 assert(m);
1231 assert(map);
1232
1233 r = bus_message_map_all_properties(m, map, copy_string, error, userdata);
1234 if (r < 0)
1235 return r;
1236
1237 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "s");
1238 if (r < 0)
1239 return r;
1240
1241 invalidated = 0;
1242 while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member)) > 0)
1243 for (i = 0; map[i].member; i++)
1244 if (streq(map[i].member, member)) {
1245 ++invalidated;
1246 break;
1247 }
1248 if (r < 0)
1249 return r;
1250
1251 r = sd_bus_message_exit_container(m);
1252 if (r < 0)
1253 return r;
1254
1255 return invalidated;
1256 }
1257
1258 int bus_map_all_properties(
1259 sd_bus *bus,
1260 const char *destination,
1261 const char *path,
1262 const struct bus_properties_map *map,
1263 sd_bus_error *error,
1264 sd_bus_message **reply,
1265 void *userdata) {
1266
1267 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1268 int r;
1269
1270 assert(bus);
1271 assert(destination);
1272 assert(path);
1273 assert(map);
1274
1275 r = sd_bus_call_method(
1276 bus,
1277 destination,
1278 path,
1279 "org.freedesktop.DBus.Properties",
1280 "GetAll",
1281 error,
1282 &m,
1283 "s", "");
1284 if (r < 0)
1285 return r;
1286
1287 r = bus_message_map_all_properties(m, map, !reply, error, userdata);
1288 if (r < 0)
1289 return r;
1290
1291 if (reply)
1292 *reply = sd_bus_message_ref(m);
1293
1294 return r;
1295 }
1296
1297 int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) {
1298 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
1299 int r;
1300
1301 assert(transport >= 0);
1302 assert(transport < _BUS_TRANSPORT_MAX);
1303 assert(ret);
1304
1305 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1306 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1307
1308 switch (transport) {
1309
1310 case BUS_TRANSPORT_LOCAL:
1311 if (user)
1312 r = sd_bus_default_user(&bus);
1313 else
1314 r = sd_bus_default_system(&bus);
1315
1316 break;
1317
1318 case BUS_TRANSPORT_REMOTE:
1319 r = sd_bus_open_system_remote(&bus, host);
1320 break;
1321
1322 case BUS_TRANSPORT_MACHINE:
1323 r = sd_bus_open_system_machine(&bus, host);
1324 break;
1325
1326 default:
1327 assert_not_reached("Hmm, unknown transport type.");
1328 }
1329 if (r < 0)
1330 return r;
1331
1332 r = sd_bus_set_exit_on_disconnect(bus, true);
1333 if (r < 0)
1334 return r;
1335
1336 *ret = bus;
1337 bus = NULL;
1338
1339 return 0;
1340 }
1341
1342 int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
1343 int r;
1344
1345 assert(transport >= 0);
1346 assert(transport < _BUS_TRANSPORT_MAX);
1347 assert(bus);
1348
1349 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1350 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1351
1352 switch (transport) {
1353
1354 case BUS_TRANSPORT_LOCAL:
1355 if (user)
1356 r = bus_connect_user_systemd(bus);
1357 else
1358 r = bus_connect_system_systemd(bus);
1359
1360 break;
1361
1362 case BUS_TRANSPORT_REMOTE:
1363 r = sd_bus_open_system_remote(bus, host);
1364 break;
1365
1366 case BUS_TRANSPORT_MACHINE:
1367 r = sd_bus_open_system_machine(bus, host);
1368 break;
1369
1370 default:
1371 assert_not_reached("Hmm, unknown transport type.");
1372 }
1373
1374 return r;
1375 }
1376
1377 int bus_property_get_bool(
1378 sd_bus *bus,
1379 const char *path,
1380 const char *interface,
1381 const char *property,
1382 sd_bus_message *reply,
1383 void *userdata,
1384 sd_bus_error *error) {
1385
1386 int b = *(bool*) userdata;
1387
1388 return sd_bus_message_append_basic(reply, 'b', &b);
1389 }
1390
1391 int bus_property_set_bool(
1392 sd_bus *bus,
1393 const char *path,
1394 const char *interface,
1395 const char *property,
1396 sd_bus_message *value,
1397 void *userdata,
1398 sd_bus_error *error) {
1399
1400 int b, r;
1401
1402 r = sd_bus_message_read(value, "b", &b);
1403 if (r < 0)
1404 return r;
1405
1406 *(bool *) userdata = !!b;
1407 return 0;
1408 }
1409
1410 int bus_property_get_id128(
1411 sd_bus *bus,
1412 const char *path,
1413 const char *interface,
1414 const char *property,
1415 sd_bus_message *reply,
1416 void *userdata,
1417 sd_bus_error *error) {
1418
1419 sd_id128_t *id = userdata;
1420
1421 if (sd_id128_is_null(*id)) /* Add an empty array if the ID is zero */
1422 return sd_bus_message_append(reply, "ay", 0);
1423 else
1424 return sd_bus_message_append_array(reply, 'y', id->bytes, 16);
1425 }
1426
1427 #if __SIZEOF_SIZE_T__ != 8
1428 int bus_property_get_size(
1429 sd_bus *bus,
1430 const char *path,
1431 const char *interface,
1432 const char *property,
1433 sd_bus_message *reply,
1434 void *userdata,
1435 sd_bus_error *error) {
1436
1437 uint64_t sz = *(size_t*) userdata;
1438
1439 return sd_bus_message_append_basic(reply, 't', &sz);
1440 }
1441 #endif
1442
1443 #if __SIZEOF_LONG__ != 8
1444 int bus_property_get_long(
1445 sd_bus *bus,
1446 const char *path,
1447 const char *interface,
1448 const char *property,
1449 sd_bus_message *reply,
1450 void *userdata,
1451 sd_bus_error *error) {
1452
1453 int64_t l = *(long*) userdata;
1454
1455 return sd_bus_message_append_basic(reply, 'x', &l);
1456 }
1457
1458 int bus_property_get_ulong(
1459 sd_bus *bus,
1460 const char *path,
1461 const char *interface,
1462 const char *property,
1463 sd_bus_message *reply,
1464 void *userdata,
1465 sd_bus_error *error) {
1466
1467 uint64_t ul = *(unsigned long*) userdata;
1468
1469 return sd_bus_message_append_basic(reply, 't', &ul);
1470 }
1471 #endif
1472
1473 int bus_log_parse_error(int r) {
1474 return log_error_errno(r, "Failed to parse bus message: %m");
1475 }
1476
1477 int bus_log_create_error(int r) {
1478 return log_error_errno(r, "Failed to create bus message: %m");
1479 }
1480
1481 /**
1482 * bus_path_encode_unique() - encode unique object path
1483 * @b: bus connection or NULL
1484 * @prefix: object path prefix
1485 * @sender_id: unique-name of client, or NULL
1486 * @external_id: external ID to be chosen by client, or NULL
1487 * @ret_path: storage for encoded object path pointer
1488 *
1489 * Whenever we provide a bus API that allows clients to create and manage
1490 * server-side objects, we need to provide a unique name for these objects. If
1491 * we let the server choose the name, we suffer from a race condition: If a
1492 * client creates an object asynchronously, it cannot destroy that object until
1493 * it received the method reply. It cannot know the name of the new object,
1494 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1495 *
1496 * Therefore, many APIs allow the client to choose the unique name for newly
1497 * created objects. There're two problems to solve, though:
1498 * 1) Object names are usually defined via dbus object paths, which are
1499 * usually globally namespaced. Therefore, multiple clients must be able
1500 * to choose unique object names without interference.
1501 * 2) If multiple libraries share the same bus connection, they must be
1502 * able to choose unique object names without interference.
1503 * The first problem is solved easily by prefixing a name with the
1504 * unique-bus-name of a connection. The server side must enforce this and
1505 * reject any other name. The second problem is solved by providing unique
1506 * suffixes from within sd-bus.
1507 *
1508 * This helper allows clients to create unique object-paths. It uses the
1509 * template '/prefix/sender_id/external_id' and returns the new path in
1510 * @ret_path (must be freed by the caller).
1511 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1512 * NULL, this function allocates a unique suffix via @b (by requesting a new
1513 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1514 * NULL.
1515 *
1516 * Returns: 0 on success, negative error code on failure.
1517 */
1518 int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) {
1519 _cleanup_free_ char *sender_label = NULL, *external_label = NULL;
1520 char external_buf[DECIMAL_STR_MAX(uint64_t)], *p;
1521 int r;
1522
1523 assert_return(b || (sender_id && external_id), -EINVAL);
1524 assert_return(object_path_is_valid(prefix), -EINVAL);
1525 assert_return(ret_path, -EINVAL);
1526
1527 if (!sender_id) {
1528 r = sd_bus_get_unique_name(b, &sender_id);
1529 if (r < 0)
1530 return r;
1531 }
1532
1533 if (!external_id) {
1534 xsprintf(external_buf, "%"PRIu64, ++b->cookie);
1535 external_id = external_buf;
1536 }
1537
1538 sender_label = bus_label_escape(sender_id);
1539 if (!sender_label)
1540 return -ENOMEM;
1541
1542 external_label = bus_label_escape(external_id);
1543 if (!external_label)
1544 return -ENOMEM;
1545
1546 p = strjoin(prefix, "/", sender_label, "/", external_label);
1547 if (!p)
1548 return -ENOMEM;
1549
1550 *ret_path = p;
1551 return 0;
1552 }
1553
1554 /**
1555 * bus_path_decode_unique() - decode unique object path
1556 * @path: object path to decode
1557 * @prefix: object path prefix
1558 * @ret_sender: output parameter for sender-id label
1559 * @ret_external: output parameter for external-id label
1560 *
1561 * This does the reverse of bus_path_encode_unique() (see its description for
1562 * details). Both trailing labels, sender-id and external-id, are unescaped and
1563 * returned in the given output parameters (the caller must free them).
1564 *
1565 * Note that this function returns 0 if the path does not match the template
1566 * (see bus_path_encode_unique()), 1 if it matched.
1567 *
1568 * Returns: Negative error code on failure, 0 if the given object path does not
1569 * match the template (return parameters are set to NULL), 1 if it was
1570 * parsed successfully (return parameters contain allocated labels).
1571 */
1572 int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) {
1573 const char *p, *q;
1574 char *sender, *external;
1575
1576 assert(object_path_is_valid(path));
1577 assert(object_path_is_valid(prefix));
1578 assert(ret_sender);
1579 assert(ret_external);
1580
1581 p = object_path_startswith(path, prefix);
1582 if (!p) {
1583 *ret_sender = NULL;
1584 *ret_external = NULL;
1585 return 0;
1586 }
1587
1588 q = strchr(p, '/');
1589 if (!q) {
1590 *ret_sender = NULL;
1591 *ret_external = NULL;
1592 return 0;
1593 }
1594
1595 sender = bus_label_unescape_n(p, q - p);
1596 external = bus_label_unescape(q + 1);
1597 if (!sender || !external) {
1598 free(sender);
1599 free(external);
1600 return -ENOMEM;
1601 }
1602
1603 *ret_sender = sender;
1604 *ret_external = external;
1605 return 1;
1606 }
1607
1608 int bus_property_get_rlimit(
1609 sd_bus *bus,
1610 const char *path,
1611 const char *interface,
1612 const char *property,
1613 sd_bus_message *reply,
1614 void *userdata,
1615 sd_bus_error *error) {
1616
1617 struct rlimit *rl;
1618 uint64_t u;
1619 rlim_t x;
1620 const char *is_soft;
1621
1622 assert(bus);
1623 assert(reply);
1624 assert(userdata);
1625
1626 is_soft = endswith(property, "Soft");
1627 rl = *(struct rlimit**) userdata;
1628 if (rl)
1629 x = is_soft ? rl->rlim_cur : rl->rlim_max;
1630 else {
1631 struct rlimit buf = {};
1632 int z;
1633 const char *s;
1634
1635 s = is_soft ? strndupa(property, is_soft - property) : property;
1636
1637 z = rlimit_from_string(strstr(s, "Limit"));
1638 assert(z >= 0);
1639
1640 getrlimit(z, &buf);
1641 x = is_soft ? buf.rlim_cur : buf.rlim_max;
1642 }
1643
1644 /* rlim_t might have different sizes, let's map
1645 * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on
1646 * all archs */
1647 u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x;
1648
1649 return sd_bus_message_append(reply, "t", u);
1650 }
1651
1652 int bus_track_add_name_many(sd_bus_track *t, char **l) {
1653 int r = 0;
1654 char **i;
1655
1656 assert(t);
1657
1658 /* Continues adding after failure, and returns the first failure. */
1659
1660 STRV_FOREACH(i, l) {
1661 int k;
1662
1663 k = sd_bus_track_add_name(t, *i);
1664 if (k < 0 && r >= 0)
1665 r = k;
1666 }
1667
1668 return r;
1669 }
1670
1671 int bus_open_system_watch_bind(sd_bus **ret) {
1672 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
1673 const char *e;
1674 int r;
1675
1676 assert(ret);
1677
1678 /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal turned on. */
1679
1680 r = sd_bus_new(&bus);
1681 if (r < 0)
1682 return r;
1683
1684 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1685 if (!e)
1686 e = DEFAULT_SYSTEM_BUS_ADDRESS;
1687
1688 r = sd_bus_set_address(bus, e);
1689 if (r < 0)
1690 return r;
1691
1692 r = sd_bus_set_bus_client(bus, true);
1693 if (r < 0)
1694 return r;
1695
1696 r = sd_bus_set_trusted(bus, true);
1697 if (r < 0)
1698 return r;
1699
1700 r = sd_bus_negotiate_creds(bus, true, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS);
1701 if (r < 0)
1702 return r;
1703
1704 r = sd_bus_set_watch_bind(bus, true);
1705 if (r < 0)
1706 return r;
1707
1708 r = sd_bus_set_connected_signal(bus, true);
1709 if (r < 0)
1710 return r;
1711
1712 r = sd_bus_start(bus);
1713 if (r < 0)
1714 return r;
1715
1716 *ret = bus;
1717 bus = NULL;
1718
1719 return 0;
1720 }