]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/bus-util.c
Merge pull request #14628 from poettering/hwdb-asus-tp500la
[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 <stdlib.h>
7 #include <sys/ioctl.h>
8 #include <sys/resource.h>
9 #include <sys/socket.h>
10 #include <unistd.h>
11
12 #include "sd-bus-protocol.h"
13 #include "sd-bus.h"
14 #include "sd-daemon.h"
15 #include "sd-event.h"
16 #include "sd-id128.h"
17
18 #include "alloc-util.h"
19 #include "bus-internal.h"
20 #include "bus-label.h"
21 #include "bus-message.h"
22 #include "bus-util.h"
23 #include "cap-list.h"
24 #include "cgroup-util.h"
25 #include "def.h"
26 #include "escape.h"
27 #include "fd-util.h"
28 #include "mountpoint-util.h"
29 #include "nsflags.h"
30 #include "parse-util.h"
31 #include "path-util.h"
32 #include "proc-cmdline.h"
33 #include "rlimit-util.h"
34 #include "stdio-util.h"
35 #include "strv.h"
36 #include "user-util.h"
37
38 static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
39 sd_event *e = userdata;
40
41 assert(m);
42 assert(e);
43
44 sd_bus_close(sd_bus_message_get_bus(m));
45 sd_event_exit(e, 0);
46
47 return 1;
48 }
49
50 int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name) {
51 const char *match;
52 const char *unique;
53 int r;
54
55 assert(e);
56 assert(bus);
57 assert(name);
58
59 /* We unregister the name here and then wait for the
60 * NameOwnerChanged signal for this event to arrive before we
61 * quit. We do this in order to make sure that any queued
62 * requests are still processed before we really exit. */
63
64 r = sd_bus_get_unique_name(bus, &unique);
65 if (r < 0)
66 return r;
67
68 match = strjoina(
69 "sender='org.freedesktop.DBus',"
70 "type='signal',"
71 "interface='org.freedesktop.DBus',"
72 "member='NameOwnerChanged',"
73 "path='/org/freedesktop/DBus',"
74 "arg0='", name, "',",
75 "arg1='", unique, "',",
76 "arg2=''");
77
78 r = sd_bus_add_match_async(bus, NULL, match, name_owner_change_callback, NULL, e);
79 if (r < 0)
80 return r;
81
82 r = sd_bus_release_name_async(bus, NULL, name, NULL, NULL);
83 if (r < 0)
84 return r;
85
86 return 0;
87 }
88
89 int bus_event_loop_with_idle(
90 sd_event *e,
91 sd_bus *bus,
92 const char *name,
93 usec_t timeout,
94 check_idle_t check_idle,
95 void *userdata) {
96 bool exiting = false;
97 int r, code;
98
99 assert(e);
100 assert(bus);
101 assert(name);
102
103 for (;;) {
104 bool idle;
105
106 r = sd_event_get_state(e);
107 if (r < 0)
108 return r;
109 if (r == SD_EVENT_FINISHED)
110 break;
111
112 if (check_idle)
113 idle = check_idle(userdata);
114 else
115 idle = true;
116
117 r = sd_event_run(e, exiting || !idle ? (uint64_t) -1 : timeout);
118 if (r < 0)
119 return r;
120
121 if (r == 0 && !exiting && idle) {
122
123 r = sd_bus_try_close(bus);
124 if (r == -EBUSY)
125 continue;
126
127 /* Fallback for dbus1 connections: we
128 * unregister the name and wait for the
129 * response to come through for it */
130 if (r == -EOPNOTSUPP) {
131
132 /* Inform the service manager that we
133 * are going down, so that it will
134 * queue all further start requests,
135 * instead of assuming we are already
136 * running. */
137 sd_notify(false, "STOPPING=1");
138
139 r = bus_async_unregister_and_exit(e, bus, name);
140 if (r < 0)
141 return r;
142
143 exiting = true;
144 continue;
145 }
146
147 if (r < 0)
148 return r;
149
150 sd_event_exit(e, 0);
151 break;
152 }
153 }
154
155 r = sd_event_get_exit_code(e, &code);
156 if (r < 0)
157 return r;
158
159 return code;
160 }
161
162 int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error) {
163 _cleanup_(sd_bus_message_unrefp) sd_bus_message *rep = NULL;
164 int r, has_owner = 0;
165
166 assert(c);
167 assert(name);
168
169 r = sd_bus_call_method(c,
170 "org.freedesktop.DBus",
171 "/org/freedesktop/dbus",
172 "org.freedesktop.DBus",
173 "NameHasOwner",
174 error,
175 &rep,
176 "s",
177 name);
178 if (r < 0)
179 return r;
180
181 r = sd_bus_message_read_basic(rep, 'b', &has_owner);
182 if (r < 0)
183 return sd_bus_error_set_errno(error, r);
184
185 return has_owner;
186 }
187
188 static int check_good_user(sd_bus_message *m, uid_t good_user) {
189 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
190 uid_t sender_uid;
191 int r;
192
193 assert(m);
194
195 if (good_user == UID_INVALID)
196 return 0;
197
198 r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_EUID, &creds);
199 if (r < 0)
200 return r;
201
202 /* Don't trust augmented credentials for authorization */
203 assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_EUID) == 0, -EPERM);
204
205 r = sd_bus_creds_get_euid(creds, &sender_uid);
206 if (r < 0)
207 return r;
208
209 return sender_uid == good_user;
210 }
211
212 int bus_test_polkit(
213 sd_bus_message *call,
214 int capability,
215 const char *action,
216 const char **details,
217 uid_t good_user,
218 bool *_challenge,
219 sd_bus_error *e) {
220
221 int r;
222
223 assert(call);
224 assert(action);
225
226 /* Tests non-interactively! */
227
228 r = check_good_user(call, good_user);
229 if (r != 0)
230 return r;
231
232 r = sd_bus_query_sender_privilege(call, capability);
233 if (r < 0)
234 return r;
235 else if (r > 0)
236 return 1;
237 #if ENABLE_POLKIT
238 else {
239 _cleanup_(sd_bus_message_unrefp) sd_bus_message *request = NULL;
240 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
241 int authorized = false, challenge = false;
242 const char *sender, **k, **v;
243
244 sender = sd_bus_message_get_sender(call);
245 if (!sender)
246 return -EBADMSG;
247
248 r = sd_bus_message_new_method_call(
249 call->bus,
250 &request,
251 "org.freedesktop.PolicyKit1",
252 "/org/freedesktop/PolicyKit1/Authority",
253 "org.freedesktop.PolicyKit1.Authority",
254 "CheckAuthorization");
255 if (r < 0)
256 return r;
257
258 r = sd_bus_message_append(
259 request,
260 "(sa{sv})s",
261 "system-bus-name", 1, "name", "s", sender,
262 action);
263 if (r < 0)
264 return r;
265
266 r = sd_bus_message_open_container(request, 'a', "{ss}");
267 if (r < 0)
268 return r;
269
270 STRV_FOREACH_PAIR(k, v, details) {
271 r = sd_bus_message_append(request, "{ss}", *k, *v);
272 if (r < 0)
273 return r;
274 }
275
276 r = sd_bus_message_close_container(request);
277 if (r < 0)
278 return r;
279
280 r = sd_bus_message_append(request, "us", 0, NULL);
281 if (r < 0)
282 return r;
283
284 r = sd_bus_call(call->bus, request, 0, e, &reply);
285 if (r < 0) {
286 /* Treat no PK available as access denied */
287 if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN)) {
288 sd_bus_error_free(e);
289 return -EACCES;
290 }
291
292 return r;
293 }
294
295 r = sd_bus_message_enter_container(reply, 'r', "bba{ss}");
296 if (r < 0)
297 return r;
298
299 r = sd_bus_message_read(reply, "bb", &authorized, &challenge);
300 if (r < 0)
301 return r;
302
303 if (authorized)
304 return 1;
305
306 if (_challenge) {
307 *_challenge = challenge;
308 return 0;
309 }
310 }
311 #endif
312
313 return -EACCES;
314 }
315
316 #if ENABLE_POLKIT
317
318 typedef struct AsyncPolkitQuery {
319 sd_bus_message *request, *reply;
320 sd_bus_message_handler_t callback;
321 void *userdata;
322 sd_bus_slot *slot;
323 Hashmap *registry;
324 } AsyncPolkitQuery;
325
326 static void async_polkit_query_free(AsyncPolkitQuery *q) {
327
328 if (!q)
329 return;
330
331 sd_bus_slot_unref(q->slot);
332
333 if (q->registry && q->request)
334 hashmap_remove(q->registry, q->request);
335
336 sd_bus_message_unref(q->request);
337 sd_bus_message_unref(q->reply);
338
339 free(q);
340 }
341
342 static int async_polkit_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
343 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
344 AsyncPolkitQuery *q = userdata;
345 int r;
346
347 assert(reply);
348 assert(q);
349
350 q->slot = sd_bus_slot_unref(q->slot);
351 q->reply = sd_bus_message_ref(reply);
352
353 r = sd_bus_message_rewind(q->request, true);
354 if (r < 0) {
355 r = sd_bus_reply_method_errno(q->request, r, NULL);
356 goto finish;
357 }
358
359 r = q->callback(q->request, q->userdata, &error_buffer);
360 r = bus_maybe_reply_error(q->request, r, &error_buffer);
361
362 finish:
363 async_polkit_query_free(q);
364
365 return r;
366 }
367
368 #endif
369
370 int bus_verify_polkit_async(
371 sd_bus_message *call,
372 int capability,
373 const char *action,
374 const char **details,
375 bool interactive,
376 uid_t good_user,
377 Hashmap **registry,
378 sd_bus_error *error) {
379
380 #if ENABLE_POLKIT
381 _cleanup_(sd_bus_message_unrefp) sd_bus_message *pk = NULL;
382 AsyncPolkitQuery *q;
383 const char *sender, **k, **v;
384 sd_bus_message_handler_t callback;
385 void *userdata;
386 int c;
387 #endif
388 int r;
389
390 assert(call);
391 assert(action);
392 assert(registry);
393
394 r = check_good_user(call, good_user);
395 if (r != 0)
396 return r;
397
398 #if ENABLE_POLKIT
399 q = hashmap_get(*registry, call);
400 if (q) {
401 int authorized, challenge;
402
403 /* This is the second invocation of this function, and
404 * there's already a response from polkit, let's
405 * process it */
406 assert(q->reply);
407
408 if (sd_bus_message_is_method_error(q->reply, NULL)) {
409 const sd_bus_error *e;
410
411 e = sd_bus_message_get_error(q->reply);
412
413 /* Treat no PK available as access denied */
414 if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN) ||
415 sd_bus_error_has_name(e, SD_BUS_ERROR_NAME_HAS_NO_OWNER))
416 return -EACCES;
417
418 /* Copy error from polkit reply */
419 sd_bus_error_copy(error, e);
420 return -sd_bus_error_get_errno(e);
421 }
422
423 r = sd_bus_message_enter_container(q->reply, 'r', "bba{ss}");
424 if (r >= 0)
425 r = sd_bus_message_read(q->reply, "bb", &authorized, &challenge);
426 if (r < 0)
427 return r;
428
429 if (authorized)
430 return 1;
431
432 if (challenge)
433 return sd_bus_error_set(error, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED, "Interactive authentication required.");
434
435 return -EACCES;
436 }
437 #endif
438
439 r = sd_bus_query_sender_privilege(call, capability);
440 if (r < 0)
441 return r;
442 else if (r > 0)
443 return 1;
444
445 #if ENABLE_POLKIT
446 if (sd_bus_get_current_message(call->bus) != call)
447 return -EINVAL;
448
449 callback = sd_bus_get_current_handler(call->bus);
450 if (!callback)
451 return -EINVAL;
452
453 userdata = sd_bus_get_current_userdata(call->bus);
454
455 sender = sd_bus_message_get_sender(call);
456 if (!sender)
457 return -EBADMSG;
458
459 c = sd_bus_message_get_allow_interactive_authorization(call);
460 if (c < 0)
461 return c;
462 if (c > 0)
463 interactive = true;
464
465 r = hashmap_ensure_allocated(registry, NULL);
466 if (r < 0)
467 return r;
468
469 r = sd_bus_message_new_method_call(
470 call->bus,
471 &pk,
472 "org.freedesktop.PolicyKit1",
473 "/org/freedesktop/PolicyKit1/Authority",
474 "org.freedesktop.PolicyKit1.Authority",
475 "CheckAuthorization");
476 if (r < 0)
477 return r;
478
479 r = sd_bus_message_append(
480 pk,
481 "(sa{sv})s",
482 "system-bus-name", 1, "name", "s", sender,
483 action);
484 if (r < 0)
485 return r;
486
487 r = sd_bus_message_open_container(pk, 'a', "{ss}");
488 if (r < 0)
489 return r;
490
491 STRV_FOREACH_PAIR(k, v, details) {
492 r = sd_bus_message_append(pk, "{ss}", *k, *v);
493 if (r < 0)
494 return r;
495 }
496
497 r = sd_bus_message_close_container(pk);
498 if (r < 0)
499 return r;
500
501 r = sd_bus_message_append(pk, "us", interactive, NULL);
502 if (r < 0)
503 return r;
504
505 q = new0(AsyncPolkitQuery, 1);
506 if (!q)
507 return -ENOMEM;
508
509 q->request = sd_bus_message_ref(call);
510 q->callback = callback;
511 q->userdata = userdata;
512
513 r = hashmap_put(*registry, call, q);
514 if (r < 0) {
515 async_polkit_query_free(q);
516 return r;
517 }
518
519 q->registry = *registry;
520
521 r = sd_bus_call_async(call->bus, &q->slot, pk, async_polkit_callback, q, 0);
522 if (r < 0) {
523 async_polkit_query_free(q);
524 return r;
525 }
526
527 return 0;
528 #endif
529
530 return -EACCES;
531 }
532
533 void bus_verify_polkit_async_registry_free(Hashmap *registry) {
534 #if ENABLE_POLKIT
535 hashmap_free_with_destructor(registry, async_polkit_query_free);
536 #endif
537 }
538
539 int bus_check_peercred(sd_bus *c) {
540 struct ucred ucred;
541 int fd, r;
542
543 assert(c);
544
545 fd = sd_bus_get_fd(c);
546 if (fd < 0)
547 return fd;
548
549 r = getpeercred(fd, &ucred);
550 if (r < 0)
551 return r;
552
553 if (ucred.uid != 0 && ucred.uid != geteuid())
554 return -EPERM;
555
556 return 1;
557 }
558
559 int bus_connect_system_systemd(sd_bus **_bus) {
560 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
561 int r;
562
563 assert(_bus);
564
565 if (geteuid() != 0)
566 return sd_bus_default_system(_bus);
567
568 /* If we are root then let's talk directly to the system
569 * instance, instead of going via the bus */
570
571 r = sd_bus_new(&bus);
572 if (r < 0)
573 return r;
574
575 r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
576 if (r < 0)
577 return r;
578
579 r = sd_bus_start(bus);
580 if (r < 0)
581 return sd_bus_default_system(_bus);
582
583 r = bus_check_peercred(bus);
584 if (r < 0)
585 return r;
586
587 *_bus = TAKE_PTR(bus);
588
589 return 0;
590 }
591
592 int bus_connect_user_systemd(sd_bus **_bus) {
593 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
594 _cleanup_free_ char *ee = NULL;
595 const char *e;
596 int r;
597
598 assert(_bus);
599
600 e = secure_getenv("XDG_RUNTIME_DIR");
601 if (!e)
602 return sd_bus_default_user(_bus);
603
604 ee = bus_address_escape(e);
605 if (!ee)
606 return -ENOMEM;
607
608 r = sd_bus_new(&bus);
609 if (r < 0)
610 return r;
611
612 bus->address = strjoin("unix:path=", ee, "/systemd/private");
613 if (!bus->address)
614 return -ENOMEM;
615
616 r = sd_bus_start(bus);
617 if (r < 0)
618 return sd_bus_default_user(_bus);
619
620 r = bus_check_peercred(bus);
621 if (r < 0)
622 return r;
623
624 *_bus = TAKE_PTR(bus);
625
626 return 0;
627 }
628
629 int bus_print_property_value(const char *name, const char *expected_value, bool only_value, const char *value) {
630 assert(name);
631
632 if (expected_value && !streq_ptr(expected_value, value))
633 return 0;
634
635 if (only_value)
636 puts(value);
637 else
638 printf("%s=%s\n", name, value);
639
640 return 0;
641 }
642
643 int bus_print_property_valuef(const char *name, const char *expected_value, bool only_value, const char *fmt, ...) {
644 va_list ap;
645 int r;
646
647 assert(name);
648 assert(fmt);
649
650 if (expected_value) {
651 _cleanup_free_ char *s = NULL;
652
653 va_start(ap, fmt);
654 r = vasprintf(&s, fmt, ap);
655 va_end(ap);
656 if (r < 0)
657 return -ENOMEM;
658
659 if (streq_ptr(expected_value, s)) {
660 if (only_value)
661 puts(s);
662 else
663 printf("%s=%s\n", name, s);
664 }
665
666 return 0;
667 }
668
669 if (!only_value)
670 printf("%s=", name);
671 va_start(ap, fmt);
672 vprintf(fmt, ap);
673 va_end(ap);
674 puts("");
675
676 return 0;
677 }
678
679 static int bus_print_property(const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all) {
680 char type;
681 const char *contents;
682 int r;
683
684 assert(name);
685 assert(m);
686
687 r = sd_bus_message_peek_type(m, &type, &contents);
688 if (r < 0)
689 return r;
690
691 switch (type) {
692
693 case SD_BUS_TYPE_STRING: {
694 const char *s;
695
696 r = sd_bus_message_read_basic(m, type, &s);
697 if (r < 0)
698 return r;
699
700 if (all || !isempty(s)) {
701 bool good;
702
703 /* This property has a single value, so we need to take
704 * care not to print a new line, everything else is OK. */
705 good = !strchr(s, '\n');
706 bus_print_property_value(name, expected_value, value, good ? s : "[unprintable]");
707 }
708
709 return 1;
710 }
711
712 case SD_BUS_TYPE_BOOLEAN: {
713 int b;
714
715 r = sd_bus_message_read_basic(m, type, &b);
716 if (r < 0)
717 return r;
718
719 if (expected_value && parse_boolean(expected_value) != b)
720 return 1;
721
722 bus_print_property_value(name, NULL, value, yes_no(b));
723 return 1;
724 }
725
726 case SD_BUS_TYPE_UINT64: {
727 uint64_t u;
728
729 r = sd_bus_message_read_basic(m, type, &u);
730 if (r < 0)
731 return r;
732
733 /* Yes, heuristics! But we can change this check
734 * should it turn out to not be sufficient */
735
736 if (endswith(name, "Timestamp") ||
737 STR_IN_SET(name, "NextElapseUSecRealtime", "LastTriggerUSec", "TimeUSec", "RTCTimeUSec")) {
738 char timestamp[FORMAT_TIMESTAMP_MAX];
739 const char *t;
740
741 t = format_timestamp(timestamp, sizeof(timestamp), u);
742 if (t || all)
743 bus_print_property_value(name, expected_value, value, strempty(t));
744
745 } else if (strstr(name, "USec")) {
746 char timespan[FORMAT_TIMESPAN_MAX];
747
748 (void) format_timespan(timespan, sizeof(timespan), u, 0);
749 bus_print_property_value(name, expected_value, value, timespan);
750
751 } else if (streq(name, "RestrictNamespaces")) {
752 _cleanup_free_ char *s = NULL;
753 const char *result;
754
755 if ((u & NAMESPACE_FLAGS_ALL) == 0)
756 result = "yes";
757 else if (FLAGS_SET(u, NAMESPACE_FLAGS_ALL))
758 result = "no";
759 else {
760 r = namespace_flags_to_string(u, &s);
761 if (r < 0)
762 return r;
763
764 result = s;
765 }
766
767 bus_print_property_value(name, expected_value, value, result);
768
769 } else if (streq(name, "MountFlags")) {
770 const char *result;
771
772 result = mount_propagation_flags_to_string(u);
773 if (!result)
774 return -EINVAL;
775
776 bus_print_property_value(name, expected_value, value, result);
777
778 } else if (STR_IN_SET(name, "CapabilityBoundingSet", "AmbientCapabilities")) {
779 _cleanup_free_ char *s = NULL;
780
781 r = capability_set_to_string_alloc(u, &s);
782 if (r < 0)
783 return r;
784
785 bus_print_property_value(name, expected_value, value, s);
786
787 } else if ((STR_IN_SET(name, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u == CGROUP_WEIGHT_INVALID) ||
788 (STR_IN_SET(name, "CPUShares", "StartupCPUShares") && u == CGROUP_CPU_SHARES_INVALID) ||
789 (STR_IN_SET(name, "BlockIOWeight", "StartupBlockIOWeight") && u == CGROUP_BLKIO_WEIGHT_INVALID) ||
790 (STR_IN_SET(name, "MemoryCurrent", "TasksCurrent") && u == (uint64_t) -1) ||
791 (endswith(name, "NSec") && u == (uint64_t) -1))
792
793 bus_print_property_value(name, expected_value, value, "[not set]");
794
795 else if ((STR_IN_SET(name, "DefaultMemoryLow", "DefaultMemoryMin", "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u == CGROUP_LIMIT_MAX) ||
796 (STR_IN_SET(name, "TasksMax", "DefaultTasksMax") && u == (uint64_t) -1) ||
797 (startswith(name, "Limit") && u == (uint64_t) -1) ||
798 (startswith(name, "DefaultLimit") && u == (uint64_t) -1))
799
800 bus_print_property_value(name, expected_value, value, "infinity");
801 else if (STR_IN_SET(name, "IPIngressBytes", "IPIngressPackets", "IPEgressBytes", "IPEgressPackets") && u == (uint64_t) -1)
802 bus_print_property_value(name, expected_value, value, "[no data]");
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 = strjoin(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 case SD_BUS_TYPE_OBJECT_PATH: {
1105 const char **p = userdata;
1106 const char *s;
1107
1108 r = sd_bus_message_read_basic(m, type, &s);
1109 if (r < 0)
1110 return r;
1111
1112 if (isempty(s))
1113 s = NULL;
1114
1115 if (flags & BUS_MAP_STRDUP)
1116 return free_and_strdup((char **) userdata, s);
1117
1118 *p = s;
1119 return 0;
1120 }
1121
1122 case SD_BUS_TYPE_ARRAY: {
1123 _cleanup_strv_free_ char **l = NULL;
1124 char ***p = userdata;
1125
1126 r = bus_message_read_strv_extend(m, &l);
1127 if (r < 0)
1128 return r;
1129
1130 return strv_extend_strv(p, l, false);
1131 }
1132
1133 case SD_BUS_TYPE_BOOLEAN: {
1134 int b;
1135
1136 r = sd_bus_message_read_basic(m, type, &b);
1137 if (r < 0)
1138 return r;
1139
1140 if (flags & BUS_MAP_BOOLEAN_AS_BOOL)
1141 *(bool*) userdata = b;
1142 else
1143 *(int*) userdata = b;
1144
1145 return 0;
1146 }
1147
1148 case SD_BUS_TYPE_INT32:
1149 case SD_BUS_TYPE_UINT32: {
1150 uint32_t u, *p = userdata;
1151
1152 r = sd_bus_message_read_basic(m, type, &u);
1153 if (r < 0)
1154 return r;
1155
1156 *p = u;
1157 return 0;
1158 }
1159
1160 case SD_BUS_TYPE_INT64:
1161 case SD_BUS_TYPE_UINT64: {
1162 uint64_t t, *p = userdata;
1163
1164 r = sd_bus_message_read_basic(m, type, &t);
1165 if (r < 0)
1166 return r;
1167
1168 *p = t;
1169 return 0;
1170 }
1171
1172 case SD_BUS_TYPE_DOUBLE: {
1173 double d, *p = userdata;
1174
1175 r = sd_bus_message_read_basic(m, type, &d);
1176 if (r < 0)
1177 return r;
1178
1179 *p = d;
1180 return 0;
1181 }}
1182
1183 return -EOPNOTSUPP;
1184 }
1185
1186 int bus_message_map_all_properties(
1187 sd_bus_message *m,
1188 const struct bus_properties_map *map,
1189 unsigned flags,
1190 sd_bus_error *error,
1191 void *userdata) {
1192
1193 int r;
1194
1195 assert(m);
1196 assert(map);
1197
1198 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
1199 if (r < 0)
1200 return r;
1201
1202 while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
1203 const struct bus_properties_map *prop;
1204 const char *member;
1205 const char *contents;
1206 void *v;
1207 unsigned i;
1208
1209 r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member);
1210 if (r < 0)
1211 return r;
1212
1213 for (i = 0, prop = NULL; map[i].member; i++)
1214 if (streq(map[i].member, member)) {
1215 prop = &map[i];
1216 break;
1217 }
1218
1219 if (prop) {
1220 r = sd_bus_message_peek_type(m, NULL, &contents);
1221 if (r < 0)
1222 return r;
1223
1224 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
1225 if (r < 0)
1226 return r;
1227
1228 v = (uint8_t *)userdata + prop->offset;
1229 if (map[i].set)
1230 r = prop->set(sd_bus_message_get_bus(m), member, m, error, v);
1231 else
1232 r = map_basic(sd_bus_message_get_bus(m), member, m, flags, error, v);
1233 if (r < 0)
1234 return r;
1235
1236 r = sd_bus_message_exit_container(m);
1237 if (r < 0)
1238 return r;
1239 } else {
1240 r = sd_bus_message_skip(m, "v");
1241 if (r < 0)
1242 return r;
1243 }
1244
1245 r = sd_bus_message_exit_container(m);
1246 if (r < 0)
1247 return r;
1248 }
1249 if (r < 0)
1250 return r;
1251
1252 return sd_bus_message_exit_container(m);
1253 }
1254
1255 int bus_map_all_properties(
1256 sd_bus *bus,
1257 const char *destination,
1258 const char *path,
1259 const struct bus_properties_map *map,
1260 unsigned flags,
1261 sd_bus_error *error,
1262 sd_bus_message **reply,
1263 void *userdata) {
1264
1265 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1266 int r;
1267
1268 assert(bus);
1269 assert(destination);
1270 assert(path);
1271 assert(map);
1272 assert(reply || (flags & BUS_MAP_STRDUP));
1273
1274 r = sd_bus_call_method(
1275 bus,
1276 destination,
1277 path,
1278 "org.freedesktop.DBus.Properties",
1279 "GetAll",
1280 error,
1281 &m,
1282 "s", "");
1283 if (r < 0)
1284 return r;
1285
1286 r = bus_message_map_all_properties(m, map, flags, error, userdata);
1287 if (r < 0)
1288 return r;
1289
1290 if (reply)
1291 *reply = sd_bus_message_ref(m);
1292
1293 return r;
1294 }
1295
1296 int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) {
1297 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
1298 int r;
1299
1300 assert(transport >= 0);
1301 assert(transport < _BUS_TRANSPORT_MAX);
1302 assert(ret);
1303
1304 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1305 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1306
1307 switch (transport) {
1308
1309 case BUS_TRANSPORT_LOCAL:
1310 if (user)
1311 r = sd_bus_default_user(&bus);
1312 else {
1313 if (sd_booted() <= 0) {
1314 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
1315 log_error("System has not been booted with systemd as init system (PID 1). Can't operate.");
1316
1317 return -EHOSTDOWN;
1318 }
1319 r = sd_bus_default_system(&bus);
1320 }
1321 break;
1322
1323 case BUS_TRANSPORT_REMOTE:
1324 r = sd_bus_open_system_remote(&bus, host);
1325 break;
1326
1327 case BUS_TRANSPORT_MACHINE:
1328 r = sd_bus_open_system_machine(&bus, host);
1329 break;
1330
1331 default:
1332 assert_not_reached("Hmm, unknown transport type.");
1333 }
1334 if (r < 0)
1335 return r;
1336
1337 r = sd_bus_set_exit_on_disconnect(bus, true);
1338 if (r < 0)
1339 return r;
1340
1341 *ret = TAKE_PTR(bus);
1342
1343 return 0;
1344 }
1345
1346 int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
1347 int r;
1348
1349 assert(transport >= 0);
1350 assert(transport < _BUS_TRANSPORT_MAX);
1351 assert(bus);
1352
1353 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1354 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1355
1356 switch (transport) {
1357
1358 case BUS_TRANSPORT_LOCAL:
1359 if (user)
1360 r = bus_connect_user_systemd(bus);
1361 else {
1362 if (sd_booted() <= 0)
1363 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
1364 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN),
1365 "System has not been booted with systemd as init system (PID 1). Can't operate.");
1366 r = bus_connect_system_systemd(bus);
1367 }
1368 break;
1369
1370 case BUS_TRANSPORT_REMOTE:
1371 r = sd_bus_open_system_remote(bus, host);
1372 break;
1373
1374 case BUS_TRANSPORT_MACHINE:
1375 r = sd_bus_open_system_machine(bus, host);
1376 break;
1377
1378 default:
1379 assert_not_reached("Hmm, unknown transport type.");
1380 }
1381
1382 return r;
1383 }
1384
1385 int bus_property_get_bool(
1386 sd_bus *bus,
1387 const char *path,
1388 const char *interface,
1389 const char *property,
1390 sd_bus_message *reply,
1391 void *userdata,
1392 sd_bus_error *error) {
1393
1394 int b = *(bool*) userdata;
1395
1396 return sd_bus_message_append_basic(reply, 'b', &b);
1397 }
1398
1399 int bus_property_set_bool(
1400 sd_bus *bus,
1401 const char *path,
1402 const char *interface,
1403 const char *property,
1404 sd_bus_message *value,
1405 void *userdata,
1406 sd_bus_error *error) {
1407
1408 int b, r;
1409
1410 r = sd_bus_message_read(value, "b", &b);
1411 if (r < 0)
1412 return r;
1413
1414 *(bool*) userdata = b;
1415 return 0;
1416 }
1417
1418 int bus_property_get_id128(
1419 sd_bus *bus,
1420 const char *path,
1421 const char *interface,
1422 const char *property,
1423 sd_bus_message *reply,
1424 void *userdata,
1425 sd_bus_error *error) {
1426
1427 sd_id128_t *id = userdata;
1428
1429 if (sd_id128_is_null(*id)) /* Add an empty array if the ID is zero */
1430 return sd_bus_message_append(reply, "ay", 0);
1431 else
1432 return sd_bus_message_append_array(reply, 'y', id->bytes, 16);
1433 }
1434
1435 #if __SIZEOF_SIZE_T__ != 8
1436 int bus_property_get_size(
1437 sd_bus *bus,
1438 const char *path,
1439 const char *interface,
1440 const char *property,
1441 sd_bus_message *reply,
1442 void *userdata,
1443 sd_bus_error *error) {
1444
1445 uint64_t sz = *(size_t*) userdata;
1446
1447 return sd_bus_message_append_basic(reply, 't', &sz);
1448 }
1449 #endif
1450
1451 #if __SIZEOF_LONG__ != 8
1452 int bus_property_get_long(
1453 sd_bus *bus,
1454 const char *path,
1455 const char *interface,
1456 const char *property,
1457 sd_bus_message *reply,
1458 void *userdata,
1459 sd_bus_error *error) {
1460
1461 int64_t l = *(long*) userdata;
1462
1463 return sd_bus_message_append_basic(reply, 'x', &l);
1464 }
1465
1466 int bus_property_get_ulong(
1467 sd_bus *bus,
1468 const char *path,
1469 const char *interface,
1470 const char *property,
1471 sd_bus_message *reply,
1472 void *userdata,
1473 sd_bus_error *error) {
1474
1475 uint64_t ul = *(unsigned long*) userdata;
1476
1477 return sd_bus_message_append_basic(reply, 't', &ul);
1478 }
1479 #endif
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 = path_join(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 const char *is_soft;
1618 struct rlimit *rl;
1619 uint64_t u;
1620 rlim_t x;
1621
1622 assert(bus);
1623 assert(reply);
1624 assert(userdata);
1625
1626 is_soft = endswith(property, "Soft");
1627
1628 rl = *(struct rlimit**) userdata;
1629 if (rl)
1630 x = is_soft ? rl->rlim_cur : rl->rlim_max;
1631 else {
1632 struct rlimit buf = {};
1633 const char *s, *p;
1634 int z;
1635
1636 /* Chop off "Soft" suffix */
1637 s = is_soft ? strndupa(property, is_soft - property) : property;
1638
1639 /* Skip over any prefix, such as "Default" */
1640 assert_se(p = strstr(s, "Limit"));
1641
1642 z = rlimit_from_string(p + 5);
1643 assert(z >= 0);
1644
1645 (void) getrlimit(z, &buf);
1646 x = is_soft ? buf.rlim_cur : buf.rlim_max;
1647 }
1648
1649 /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on all
1650 * archs */
1651 u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x;
1652
1653 return sd_bus_message_append(reply, "t", u);
1654 }
1655
1656 int bus_track_add_name_many(sd_bus_track *t, char **l) {
1657 int r = 0;
1658 char **i;
1659
1660 assert(t);
1661
1662 /* Continues adding after failure, and returns the first failure. */
1663
1664 STRV_FOREACH(i, l) {
1665 int k;
1666
1667 k = sd_bus_track_add_name(t, *i);
1668 if (k < 0 && r >= 0)
1669 r = k;
1670 }
1671
1672 return r;
1673 }
1674
1675 int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *description) {
1676 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
1677 const char *e;
1678 int r;
1679
1680 assert(ret);
1681
1682 /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal
1683 * turned on. */
1684
1685 r = sd_bus_new(&bus);
1686 if (r < 0)
1687 return r;
1688
1689 if (description) {
1690 r = sd_bus_set_description(bus, description);
1691 if (r < 0)
1692 return r;
1693 }
1694
1695 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1696 if (!e)
1697 e = DEFAULT_SYSTEM_BUS_ADDRESS;
1698
1699 r = sd_bus_set_address(bus, e);
1700 if (r < 0)
1701 return r;
1702
1703 r = sd_bus_set_bus_client(bus, true);
1704 if (r < 0)
1705 return r;
1706
1707 r = sd_bus_negotiate_creds(bus, true, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS);
1708 if (r < 0)
1709 return r;
1710
1711 r = sd_bus_set_watch_bind(bus, true);
1712 if (r < 0)
1713 return r;
1714
1715 r = sd_bus_set_connected_signal(bus, true);
1716 if (r < 0)
1717 return r;
1718
1719 r = sd_bus_start(bus);
1720 if (r < 0)
1721 return r;
1722
1723 *ret = TAKE_PTR(bus);
1724
1725 return 0;
1726 }
1727
1728 int bus_reply_pair_array(sd_bus_message *m, char **l) {
1729 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1730 char **k, **v;
1731 int r;
1732
1733 assert(m);
1734
1735 /* Reply to the specified message with a message containing a dictionary put together from the
1736 * specified strv */
1737
1738 r = sd_bus_message_new_method_return(m, &reply);
1739 if (r < 0)
1740 return r;
1741
1742 r = sd_bus_message_open_container(reply, 'a', "{ss}");
1743 if (r < 0)
1744 return r;
1745
1746 STRV_FOREACH_PAIR(k, v, l) {
1747 r = sd_bus_message_append(reply, "{ss}", *k, *v);
1748 if (r < 0)
1749 return r;
1750 }
1751
1752 r = sd_bus_message_close_container(reply);
1753 if (r < 0)
1754 return r;
1755
1756 return sd_bus_send(NULL, reply, NULL);
1757 }
1758
1759 static void bus_message_unref_wrapper(void *m) {
1760 sd_bus_message_unref(m);
1761 }
1762
1763 const struct hash_ops bus_message_hash_ops = {
1764 .hash = trivial_hash_func,
1765 .compare = trivial_compare_func,
1766 .free_value = bus_message_unref_wrapper,
1767 };