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