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