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