]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/bus-util.c
sd-bus: adjust indentation of comments
[thirdparty/systemd.git] / src / shared / bus-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
40ca29a1 2
a8fbdf54
TA
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>
0c842e0a 11#include <sys/socket.h>
a8fbdf54 12#include <unistd.h>
0c842e0a 13
a8fbdf54 14#include "sd-bus-protocol.h"
4f5dd394 15#include "sd-bus.h"
ebd011d9
LP
16#include "sd-daemon.h"
17#include "sd-event.h"
a8fbdf54 18#include "sd-id128.h"
d53d9474 19
b5efdb8a 20#include "alloc-util.h"
d53d9474
LP
21#include "bus-internal.h"
22#include "bus-label.h"
23#include "bus-message.h"
3ffd4af2 24#include "bus-util.h"
52610b02 25#include "cap-list.h"
21771f33 26#include "cgroup-util.h"
40ca29a1 27#include "def.h"
4f5dd394 28#include "escape.h"
3ffd4af2 29#include "fd-util.h"
2bba9a57 30#include "missing.h"
049af8ad 31#include "mountpoint-util.h"
73186d53 32#include "nsflags.h"
6bedfcbb 33#include "parse-util.h"
657ee2d8 34#include "path-util.h"
ee104e11 35#include "proc-cmdline.h"
78f22b97 36#include "rlimit-util.h"
15a5e950 37#include "stdio-util.h"
d53d9474 38#include "strv.h"
ee104e11 39#include "user-util.h"
40ca29a1 40
19070062 41static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
40ca29a1
LP
42 sd_event *e = userdata;
43
40ca29a1
LP
44 assert(m);
45 assert(e);
46
19070062 47 sd_bus_close(sd_bus_message_get_bus(m));
6203e07a 48 sd_event_exit(e, 0);
b27adf35 49
40ca29a1
LP
50 return 1;
51}
52
6203e07a 53int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name) {
75152a4d 54 const char *match;
11846aa7 55 const char *unique;
40ca29a1
LP
56 int r;
57
58 assert(e);
59 assert(bus);
60 assert(name);
61
6203e07a
LP
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
11846aa7 67 r = sd_bus_get_unique_name(bus, &unique);
40ca29a1
LP
68 if (r < 0)
69 return r;
70
75152a4d
LP
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);
40ca29a1
LP
82 if (r < 0)
83 return r;
84
75152a4d 85 r = sd_bus_release_name_async(bus, NULL, name, NULL, NULL);
40ca29a1
LP
86 if (r < 0)
87 return r;
88
40ca29a1
LP
89 return 0;
90}
91
37224a5f
LP
92int 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) {
40ca29a1 99 bool exiting = false;
6203e07a 100 int r, code;
40ca29a1
LP
101
102 assert(e);
103 assert(bus);
104 assert(name);
105
106 for (;;) {
37224a5f
LP
107 bool idle;
108
40ca29a1
LP
109 r = sd_event_get_state(e);
110 if (r < 0)
111 return r;
40ca29a1
LP
112 if (r == SD_EVENT_FINISHED)
113 break;
114
37224a5f
LP
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);
40ca29a1
LP
121 if (r < 0)
122 return r;
123
a8ba6cd1 124 if (r == 0 && !exiting && idle) {
b27adf35
LP
125
126 r = sd_bus_try_close(bus);
127 if (r == -EBUSY)
128 continue;
129
430e21c2
LP
130 /* Fallback for dbus1 connections: we
131 * unregister the name and wait for the
132 * response to come through for it */
15411c0c 133 if (r == -EOPNOTSUPP) {
430e21c2
LP
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");
b27adf35
LP
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
40ca29a1
LP
150 if (r < 0)
151 return r;
152
b27adf35
LP
153 sd_event_exit(e, 0);
154 break;
40ca29a1
LP
155 }
156 }
157
6203e07a
LP
158 r = sd_event_get_exit_code(e, &code);
159 if (r < 0)
160 return r;
161
162 return code;
40ca29a1
LP
163}
164
5fd38859 165int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error) {
4afd3348 166 _cleanup_(sd_bus_message_unrefp) sd_bus_message *rep = NULL;
5fd38859
DH
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
c529695e 191static int check_good_user(sd_bus_message *m, uid_t good_user) {
4afd3348 192 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
c529695e
LP
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
0f514420
LP
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
c529695e
LP
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
ceb24229 215int bus_test_polkit(
f3885791 216 sd_bus_message *call,
def9a7aa 217 int capability,
40ca29a1 218 const char *action,
403ed0e5 219 const char **details,
c529695e 220 uid_t good_user,
40ca29a1
LP
221 bool *_challenge,
222 sd_bus_error *e) {
223
40ca29a1
LP
224 int r;
225
f3885791 226 assert(call);
40ca29a1
LP
227 assert(action);
228
ceb24229
LP
229 /* Tests non-interactively! */
230
c529695e
LP
231 r = check_good_user(call, good_user);
232 if (r != 0)
233 return r;
234
f3885791 235 r = sd_bus_query_sender_privilege(call, capability);
5b12334d
LP
236 if (r < 0)
237 return r;
f3885791 238 else if (r > 0)
40ca29a1 239 return 1;
349cc4a5 240#if ENABLE_POLKIT
40ca29a1 241 else {
4afd3348
LP
242 _cleanup_(sd_bus_message_unrefp) sd_bus_message *request = NULL;
243 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
ceb24229 244 int authorized = false, challenge = false;
403ed0e5 245 const char *sender, **k, **v;
5b12334d 246
f3885791 247 sender = sd_bus_message_get_sender(call);
5b12334d
LP
248 if (!sender)
249 return -EBADMSG;
40ca29a1 250
403ed0e5 251 r = sd_bus_message_new_method_call(
f3885791 252 call->bus,
403ed0e5 253 &request,
40ca29a1
LP
254 "org.freedesktop.PolicyKit1",
255 "/org/freedesktop/PolicyKit1/Authority",
256 "org.freedesktop.PolicyKit1.Authority",
403ed0e5
MC
257 "CheckAuthorization");
258 if (r < 0)
259 return r;
260
261 r = sd_bus_message_append(
262 request,
263 "(sa{sv})s",
40ca29a1 264 "system-bus-name", 1, "name", "s", sender,
403ed0e5
MC
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;
40ca29a1 272
403ed0e5
MC
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);
40ca29a1
LP
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
313333b4 298 r = sd_bus_message_enter_container(reply, 'r', "bba{ss}");
2b49a470
TA
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;
40ca29a1
LP
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
349cc4a5 319#if ENABLE_POLKIT
40ca29a1
LP
320
321typedef struct AsyncPolkitQuery {
322 sd_bus_message *request, *reply;
323 sd_bus_message_handler_t callback;
324 void *userdata;
19befb2d 325 sd_bus_slot *slot;
ebcf1f97 326 Hashmap *registry;
40ca29a1
LP
327} AsyncPolkitQuery;
328
19befb2d 329static void async_polkit_query_free(AsyncPolkitQuery *q) {
ebcf1f97
LP
330
331 if (!q)
332 return;
333
19befb2d 334 sd_bus_slot_unref(q->slot);
ebcf1f97
LP
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
19070062 345static int async_polkit_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
4afd3348 346 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
ebcf1f97 347 AsyncPolkitQuery *q = userdata;
40ca29a1
LP
348 int r;
349
40ca29a1
LP
350 assert(reply);
351 assert(q);
352
19befb2d 353 q->slot = sd_bus_slot_unref(q->slot);
40ca29a1 354 q->reply = sd_bus_message_ref(reply);
40ca29a1 355
ebcf1f97
LP
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 }
40ca29a1 361
19070062 362 r = q->callback(q->request, q->userdata, &error_buffer);
ebcf1f97 363 r = bus_maybe_reply_error(q->request, r, &error_buffer);
40ca29a1 364
ebcf1f97 365finish:
19befb2d
LP
366 async_polkit_query_free(q);
367
ebcf1f97 368 return r;
40ca29a1
LP
369}
370
371#endif
372
373int bus_verify_polkit_async(
f3885791 374 sd_bus_message *call,
def9a7aa 375 int capability,
40ca29a1 376 const char *action,
403ed0e5 377 const char **details,
40ca29a1 378 bool interactive,
c529695e 379 uid_t good_user,
f3885791
LP
380 Hashmap **registry,
381 sd_bus_error *error) {
40ca29a1 382
349cc4a5 383#if ENABLE_POLKIT
4afd3348 384 _cleanup_(sd_bus_message_unrefp) sd_bus_message *pk = NULL;
40ca29a1 385 AsyncPolkitQuery *q;
403ed0e5 386 const char *sender, **k, **v;
f3885791
LP
387 sd_bus_message_handler_t callback;
388 void *userdata;
b911eb15 389 int c;
5b12334d 390#endif
40ca29a1
LP
391 int r;
392
f3885791 393 assert(call);
40ca29a1 394 assert(action);
f3885791 395 assert(registry);
40ca29a1 396
c529695e
LP
397 r = check_good_user(call, good_user);
398 if (r != 0)
399 return r;
400
349cc4a5 401#if ENABLE_POLKIT
f3885791 402 q = hashmap_get(*registry, call);
40ca29a1 403 if (q) {
102d8f81 404 int authorized, challenge;
40ca29a1
LP
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
40ca29a1 414 e = sd_bus_message_get_error(q->reply);
40ca29a1 415
ebcf1f97
LP
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
8c69fe79
YW
420 /* Copy error from polkit reply */
421 sd_bus_error_copy(error, e);
5958d089 422 return -sd_bus_error_get_errno(e);
40ca29a1
LP
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
40ca29a1
LP
429 if (r < 0)
430 return r;
431
432 if (authorized)
433 return 1;
434
f2288cc6
LP
435 if (challenge)
436 return sd_bus_error_set(error, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED, "Interactive authentication required.");
437
40ca29a1
LP
438 return -EACCES;
439 }
440#endif
441
f3885791 442 r = sd_bus_query_sender_privilege(call, capability);
5b12334d
LP
443 if (r < 0)
444 return r;
f3885791 445 else if (r > 0)
40ca29a1 446 return 1;
5b12334d 447
349cc4a5 448#if ENABLE_POLKIT
f3885791
LP
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);
5b12334d
LP
459 if (!sender)
460 return -EBADMSG;
40ca29a1 461
b911eb15
LP
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
d5099efc 468 r = hashmap_ensure_allocated(registry, NULL);
40ca29a1
LP
469 if (r < 0)
470 return r;
471
472 r = sd_bus_message_new_method_call(
f3885791 473 call->bus,
151b9b96 474 &pk,
40ca29a1
LP
475 "org.freedesktop.PolicyKit1",
476 "/org/freedesktop/PolicyKit1/Authority",
477 "org.freedesktop.PolicyKit1.Authority",
151b9b96 478 "CheckAuthorization");
40ca29a1
LP
479 if (r < 0)
480 return r;
481
482 r = sd_bus_message_append(
483 pk,
403ed0e5 484 "(sa{sv})s",
40ca29a1 485 "system-bus-name", 1, "name", "s", sender,
403ed0e5
MC
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
031651a5 504 r = sd_bus_message_append(pk, "us", interactive, NULL);
40ca29a1
LP
505 if (r < 0)
506 return r;
507
508 q = new0(AsyncPolkitQuery, 1);
509 if (!q)
510 return -ENOMEM;
511
f3885791 512 q->request = sd_bus_message_ref(call);
40ca29a1
LP
513 q->callback = callback;
514 q->userdata = userdata;
515
f3885791 516 r = hashmap_put(*registry, call, q);
40ca29a1 517 if (r < 0) {
19befb2d 518 async_polkit_query_free(q);
40ca29a1
LP
519 return r;
520 }
521
ebcf1f97
LP
522 q->registry = *registry;
523
f3885791 524 r = sd_bus_call_async(call->bus, &q->slot, pk, async_polkit_callback, q, 0);
ebcf1f97 525 if (r < 0) {
19befb2d 526 async_polkit_query_free(q);
40ca29a1 527 return r;
ebcf1f97 528 }
40ca29a1
LP
529
530 return 0;
531#endif
532
533 return -EACCES;
534}
535
36e34057 536void bus_verify_polkit_async_registry_free(Hashmap *registry) {
349cc4a5 537#if ENABLE_POLKIT
224b0e7a 538 hashmap_free_with_destructor(registry, async_polkit_query_free);
40ca29a1
LP
539#endif
540}
0c842e0a 541
718db961 542int bus_check_peercred(sd_bus *c) {
0c842e0a 543 struct ucred ucred;
3e641e36 544 int fd, r;
0c842e0a
TG
545
546 assert(c);
547
548 fd = sd_bus_get_fd(c);
0f8bd8de
LP
549 if (fd < 0)
550 return fd;
0c842e0a 551
3e641e36
LP
552 r = getpeercred(fd, &ucred);
553 if (r < 0)
554 return r;
0c842e0a
TG
555
556 if (ucred.uid != 0 && ucred.uid != geteuid())
557 return -EPERM;
558
559 return 1;
560}
561
266f3e26 562int bus_connect_system_systemd(sd_bus **_bus) {
b1a4981a 563 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
0c842e0a 564 int r;
0c842e0a
TG
565
566 assert(_bus);
567
0f8bd8de 568 if (geteuid() != 0)
266f3e26 569 return sd_bus_default_system(_bus);
a1da8583 570
a132bef0
ZJS
571 /* If we are root then let's talk directly to the system
572 * instance, instead of going via the bus */
a6aa8912
LP
573
574 r = sd_bus_new(&bus);
0f8bd8de
LP
575 if (r < 0)
576 return r;
a1da8583 577
a6aa8912
LP
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)
266f3e26 584 return sd_bus_default_system(_bus);
a6aa8912 585
0f8bd8de 586 r = bus_check_peercred(bus);
a1da8583
TG
587 if (r < 0)
588 return r;
589
1cc6c93a 590 *_bus = TAKE_PTR(bus);
0f8bd8de 591
a1da8583
TG
592 return 0;
593}
594
266f3e26 595int bus_connect_user_systemd(sd_bus **_bus) {
b1a4981a 596 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
a6aa8912 597 _cleanup_free_ char *ee = NULL;
41dd15e4
LP
598 const char *e;
599 int r;
600
41dd15e4
LP
601 assert(_bus);
602
603 e = secure_getenv("XDG_RUNTIME_DIR");
537220d9 604 if (!e)
266f3e26 605 return sd_bus_default_user(_bus);
537220d9 606
a6aa8912
LP
607 ee = bus_address_escape(e);
608 if (!ee)
537220d9 609 return -ENOMEM;
41dd15e4
LP
610
611 r = sd_bus_new(&bus);
612 if (r < 0)
613 return r;
614
605405c6 615 bus->address = strjoin("unix:path=", ee, "/systemd/private");
a6aa8912
LP
616 if (!bus->address)
617 return -ENOMEM;
41dd15e4
LP
618
619 r = sd_bus_start(bus);
620 if (r < 0)
266f3e26 621 return sd_bus_default_user(_bus);
41dd15e4
LP
622
623 r = bus_check_peercred(bus);
624 if (r < 0)
625 return r;
626
1cc6c93a 627 *_bus = TAKE_PTR(bus);
41dd15e4
LP
628
629 return 0;
630}
631
102b0214
YW
632int 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
646int bus_print_property_valuef(const char *name, const char *expected_value, bool only_value, const char *fmt, ...) {
eda19357
YW
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
682static int bus_print_property(const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all) {
a1da8583
TG
683 char type;
684 const char *contents;
9f6eb1cd 685 int r;
a1da8583
TG
686
687 assert(name);
07636114 688 assert(m);
a1da8583 689
07636114 690 r = sd_bus_message_peek_type(m, &type, &contents);
9f6eb1cd
KS
691 if (r < 0)
692 return r;
a1da8583
TG
693
694 switch (type) {
695
696 case SD_BUS_TYPE_STRING: {
697 const char *s;
9f6eb1cd 698
07636114 699 r = sd_bus_message_read_basic(m, type, &s);
9f6eb1cd
KS
700 if (r < 0)
701 return r;
a1da8583 702
5993d46a
ZJS
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');
102b0214 709 bus_print_property_value(name, expected_value, value, good ? s : "[unprintable]");
5993d46a 710 }
a1da8583
TG
711
712 return 1;
713 }
714
715 case SD_BUS_TYPE_BOOLEAN: {
c2fa048c 716 int b;
a1da8583 717
07636114 718 r = sd_bus_message_read_basic(m, type, &b);
9f6eb1cd
KS
719 if (r < 0)
720 return r;
721
eda19357
YW
722 if (expected_value && parse_boolean(expected_value) != b)
723 return 1;
a1da8583 724
102b0214 725 bus_print_property_value(name, NULL, value, yes_no(b));
a1da8583
TG
726 return 1;
727 }
728
729 case SD_BUS_TYPE_UINT64: {
730 uint64_t u;
731
07636114 732 r = sd_bus_message_read_basic(m, type, &u);
9f6eb1cd
KS
733 if (r < 0)
734 return r;
a1da8583
TG
735
736 /* Yes, heuristics! But we can change this check
737 * should it turn out to not be sufficient */
738
ead0adb1
YW
739 if (endswith(name, "Timestamp") ||
740 STR_IN_SET(name, "NextElapseUSecRealtime", "LastTriggerUSec", "TimeUSec", "RTCTimeUSec")) {
4d9685be
ZJS
741 char timestamp[FORMAT_TIMESTAMP_MAX];
742 const char *t;
a1da8583
TG
743
744 t = format_timestamp(timestamp, sizeof(timestamp), u);
745 if (t || all)
102b0214 746 bus_print_property_value(name, expected_value, value, strempty(t));
a1da8583
TG
747
748 } else if (strstr(name, "USec")) {
749 char timespan[FORMAT_TIMESPAN_MAX];
750
eda19357 751 (void) format_timespan(timespan, sizeof(timespan), u, 0);
102b0214 752 bus_print_property_value(name, expected_value, value, timespan);
eda19357 753
73186d53
DH
754 } else if (streq(name, "RestrictNamespaces")) {
755 _cleanup_free_ char *s = NULL;
ae978b9f 756 const char *result;
73186d53
DH
757
758 if ((u & NAMESPACE_FLAGS_ALL) == 0)
759 result = "yes";
0c21dafb 760 else if (FLAGS_SET(u, NAMESPACE_FLAGS_ALL))
73186d53
DH
761 result = "no";
762 else {
86c2a9f1 763 r = namespace_flags_to_string(u, &s);
73186d53
DH
764 if (r < 0)
765 return r;
766
767 result = s;
768 }
769
102b0214 770 bus_print_property_value(name, expected_value, value, result);
21771f33
YW
771
772 } else if (streq(name, "MountFlags")) {
ae978b9f 773 const char *result;
21771f33
YW
774
775 result = mount_propagation_flags_to_string(u);
776 if (!result)
777 return -EINVAL;
778
102b0214 779 bus_print_property_value(name, expected_value, value, result);
21771f33 780
52610b02
YW
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
102b0214 788 bus_print_property_value(name, expected_value, value, s);
52610b02 789
21771f33
YW
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
102b0214 796 bus_print_property_value(name, expected_value, value, "[not set]");
21771f33 797
7e7223b3 798 else if ((STR_IN_SET(name, "DefaultMemoryLow", "DefaultMemoryMin", "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u == CGROUP_LIMIT_MAX) ||
21771f33
YW
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
102b0214 803 bus_print_property_value(name, expected_value, value, "infinity");
56c6b690
YW
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]");
21771f33 806 else
102b0214 807 bus_print_property_valuef(name, expected_value, value, "%"PRIu64, u);
a1da8583
TG
808
809 return 1;
810 }
811
d7161865
DM
812 case SD_BUS_TYPE_INT64: {
813 int64_t i;
814
07636114 815 r = sd_bus_message_read_basic(m, type, &i);
d7161865
DM
816 if (r < 0)
817 return r;
818
102b0214 819 bus_print_property_valuef(name, expected_value, value, "%"PRIi64, i);
d7161865
DM
820 return 1;
821 }
822
a1da8583
TG
823 case SD_BUS_TYPE_UINT32: {
824 uint32_t u;
825
07636114 826 r = sd_bus_message_read_basic(m, type, &u);
9f6eb1cd
KS
827 if (r < 0)
828 return r;
a1da8583
TG
829
830 if (strstr(name, "UMask") || strstr(name, "Mode"))
102b0214 831 bus_print_property_valuef(name, expected_value, value, "%04o", u);
eda19357 832
c533658a
ZJS
833 else if (streq(name, "UID")) {
834 if (u == UID_INVALID)
102b0214 835 bus_print_property_value(name, expected_value, value, "[not set]");
c533658a 836 else
102b0214 837 bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u);
c533658a
ZJS
838 } else if (streq(name, "GID")) {
839 if (u == GID_INVALID)
102b0214 840 bus_print_property_value(name, expected_value, value, "[not set]");
c533658a 841 else
102b0214 842 bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u);
c533658a 843 } else
102b0214 844 bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u);
a1da8583
TG
845
846 return 1;
847 }
848
849 case SD_BUS_TYPE_INT32: {
850 int32_t i;
851
07636114 852 r = sd_bus_message_read_basic(m, type, &i);
9f6eb1cd
KS
853 if (r < 0)
854 return r;
a1da8583 855
102b0214 856 bus_print_property_valuef(name, expected_value, value, "%"PRIi32, i);
a1da8583
TG
857 return 1;
858 }
859
860 case SD_BUS_TYPE_DOUBLE: {
861 double d;
862
07636114 863 r = sd_bus_message_read_basic(m, type, &d);
9f6eb1cd
KS
864 if (r < 0)
865 return r;
a1da8583 866
102b0214 867 bus_print_property_valuef(name, expected_value, value, "%g", d);
a1da8583
TG
868 return 1;
869 }
870
871 case SD_BUS_TYPE_ARRAY:
a1da8583 872 if (streq(contents, "s")) {
261afec5
MAP
873 bool first = true;
874 const char *str;
a1da8583 875
07636114 876 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, contents);
9f6eb1cd
KS
877 if (r < 0)
878 return r;
879
07636114 880 while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &str)) > 0) {
5993d46a
ZJS
881 bool good;
882
4f9a9105 883 if (first && !value)
261afec5
MAP
884 printf("%s=", name);
885
ed88a900 886 /* This property has multiple space-separated values, so
ead6bd25 887 * neither spaces nor newlines can be allowed in a value. */
5993d46a
ZJS
888 good = str[strcspn(str, " \n")] == '\0';
889
890 printf("%s%s", first ? "" : " ", good ? str : "[unprintable]");
261afec5
MAP
891
892 first = false;
893 }
9f6eb1cd
KS
894 if (r < 0)
895 return r;
a1da8583 896
4f9a9105 897 if (first && all && !value)
a1da8583 898 printf("%s=", name);
261afec5 899 if (!first || all)
a1da8583 900 puts("");
a1da8583 901
07636114 902 r = sd_bus_message_exit_container(m);
9f6eb1cd
KS
903 if (r < 0)
904 return r;
a1da8583
TG
905
906 return 1;
907
908 } else if (streq(contents, "y")) {
909 const uint8_t *u;
910 size_t n;
911
07636114 912 r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, (const void**) &u, &n);
9f6eb1cd
KS
913 if (r < 0)
914 return r;
915
a1da8583 916 if (all || n > 0) {
14cb109d 917 unsigned i;
a1da8583 918
4f9a9105
ZJS
919 if (!value)
920 printf("%s=", name);
a1da8583
TG
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
07636114 934 r = sd_bus_message_read_array(m, SD_BUS_TYPE_UINT32, (const void**) &u, &n);
9f6eb1cd
KS
935 if (r < 0)
936 return r;
937
a1da8583 938 if (all || n > 0) {
14cb109d 939 unsigned i;
a1da8583 940
4f9a9105
ZJS
941 if (!value)
942 printf("%s=", name);
a1da8583
TG
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}
d21ed1ea 958
07636114
YW
959int 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) {
ffc06c35 966
07636114 967 int r;
9f6eb1cd 968
07636114 969 assert(m);
ffc06c35 970
07636114 971 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
ffc06c35
KS
972 if (r < 0)
973 return r;
974
07636114 975 while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
eda19357
YW
976 _cleanup_free_ char *name_with_equal = NULL;
977 const char *name, *contents, *expected_value = NULL;
ffc06c35 978
07636114 979 r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &name);
ffc06c35
KS
980 if (r < 0)
981 return r;
982
07636114
YW
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);
0ceff906 989 if (r < 0 && r != -EEXIST)
07636114
YW
990 return log_oom();
991 }
992
b910cc72 993 name_with_equal = strjoin(name, "=");
eda19357
YW
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))) {
07636114 999 r = sd_bus_message_peek_type(m, NULL, &contents);
9f6eb1cd
KS
1000 if (r < 0)
1001 return r;
1002
07636114 1003 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
9f6eb1cd
KS
1004 if (r < 0)
1005 return r;
ffc06c35 1006
07636114 1007 if (func)
eda19357 1008 r = func(name, expected_value, m, value, all);
07636114 1009 if (!func || r == 0)
eda19357 1010 r = bus_print_property(name, expected_value, m, value, all);
9f6eb1cd
KS
1011 if (r < 0)
1012 return r;
27e72d6b 1013 if (r == 0) {
eda19357 1014 if (all && !expected_value)
27e72d6b
SP
1015 printf("%s=[unprintable]\n", name);
1016 /* skip what we didn't read */
07636114 1017 r = sd_bus_message_skip(m, contents);
27e72d6b
SP
1018 if (r < 0)
1019 return r;
1020 }
9f6eb1cd 1021
07636114 1022 r = sd_bus_message_exit_container(m);
9f6eb1cd
KS
1023 if (r < 0)
1024 return r;
1025 } else {
07636114 1026 r = sd_bus_message_skip(m, "v");
9f6eb1cd
KS
1027 if (r < 0)
1028 return r;
1029 }
1030
07636114 1031 r = sd_bus_message_exit_container(m);
ffc06c35
KS
1032 if (r < 0)
1033 return r;
9f6eb1cd
KS
1034 }
1035 if (r < 0)
1036 return r;
ffc06c35 1037
07636114 1038 r = sd_bus_message_exit_container(m);
9f6eb1cd
KS
1039 if (r < 0)
1040 return r;
ffc06c35 1041
9f6eb1cd
KS
1042 return 0;
1043}
ffc06c35 1044
07636114
YW
1045int 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
9f6eb1cd
KS
1076int 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;
ffc06c35 1081
9f6eb1cd
KS
1082 r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n);
1083 if (r < 0)
1084 return r;
ffc06c35 1085
9f6eb1cd
KS
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;
ffc06c35 1092
9f6eb1cd
KS
1093 return 0;
1094}
ffc06c35 1095
a7e4861c 1096static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, unsigned flags, sd_bus_error *error, void *userdata) {
9f6eb1cd
KS
1097 char type;
1098 int r;
ffc06c35 1099
9f6eb1cd
KS
1100 r = sd_bus_message_peek_type(m, &type, NULL);
1101 if (r < 0)
1102 return r;
ffc06c35 1103
9f6eb1cd 1104 switch (type) {
8b3b6f58 1105
9f6eb1cd 1106 case SD_BUS_TYPE_STRING: {
f37f8a61 1107 const char **p = userdata;
8b3b6f58 1108 const char *s;
ffc06c35 1109
9f6eb1cd
KS
1110 r = sd_bus_message_read_basic(m, type, &s);
1111 if (r < 0)
8b3b6f58 1112 return r;
ffc06c35 1113
9f6eb1cd 1114 if (isempty(s))
0b83b8a4 1115 s = NULL;
ffc06c35 1116
a7e4861c 1117 if (flags & BUS_MAP_STRDUP)
f37f8a61
YW
1118 return free_and_strdup((char **) userdata, s);
1119
1120 *p = s;
1121 return 0;
9f6eb1cd 1122 }
ffc06c35 1123
9f6eb1cd 1124 case SD_BUS_TYPE_ARRAY: {
7d6884b6
TA
1125 _cleanup_strv_free_ char **l = NULL;
1126 char ***p = userdata;
ffc06c35 1127
9f6eb1cd
KS
1128 r = bus_message_read_strv_extend(m, &l);
1129 if (r < 0)
8b3b6f58 1130 return r;
ffc06c35 1131
130d3d22 1132 return strv_free_and_replace(*p, l);
9f6eb1cd 1133 }
ffc06c35 1134
9f6eb1cd 1135 case SD_BUS_TYPE_BOOLEAN: {
a7e4861c 1136 int b;
ffc06c35 1137
9f6eb1cd
KS
1138 r = sd_bus_message_read_basic(m, type, &b);
1139 if (r < 0)
8b3b6f58 1140 return r;
ffc06c35 1141
a7e4861c 1142 if (flags & BUS_MAP_BOOLEAN_AS_BOOL)
5d904a6a 1143 *(bool*) userdata = b;
a7e4861c 1144 else
5d904a6a 1145 *(int*) userdata = b;
a7e4861c 1146
8b3b6f58 1147 return 0;
9f6eb1cd 1148 }
ffc06c35 1149
bdf97b8a 1150 case SD_BUS_TYPE_INT32:
9f6eb1cd 1151 case SD_BUS_TYPE_UINT32: {
bdf97b8a 1152 uint32_t u, *p = userdata;
9f6eb1cd
KS
1153
1154 r = sd_bus_message_read_basic(m, type, &u);
1155 if (r < 0)
8b3b6f58 1156 return r;
ffc06c35 1157
9f6eb1cd 1158 *p = u;
8b3b6f58 1159 return 0;
9f6eb1cd
KS
1160 }
1161
bdf97b8a 1162 case SD_BUS_TYPE_INT64:
9f6eb1cd 1163 case SD_BUS_TYPE_UINT64: {
bdf97b8a 1164 uint64_t t, *p = userdata;
9f6eb1cd
KS
1165
1166 r = sd_bus_message_read_basic(m, type, &t);
1167 if (r < 0)
8b3b6f58 1168 return r;
ffc06c35 1169
9f6eb1cd 1170 *p = t;
8b3b6f58 1171 return 0;
9f6eb1cd
KS
1172 }
1173
52e045f9 1174 case SD_BUS_TYPE_DOUBLE: {
8b3b6f58 1175 double d, *p = userdata;
52e045f9
SS
1176
1177 r = sd_bus_message_read_basic(m, type, &d);
1178 if (r < 0)
8b3b6f58 1179 return r;
52e045f9
SS
1180
1181 *p = d;
8b3b6f58
LP
1182 return 0;
1183 }}
52e045f9 1184
8b3b6f58 1185 return -EOPNOTSUPP;
9f6eb1cd
KS
1186}
1187
fe506d56
LP
1188int bus_message_map_all_properties(
1189 sd_bus_message *m,
1190 const struct bus_properties_map *map,
a7e4861c 1191 unsigned flags,
f9e0eefc 1192 sd_bus_error *error,
fe506d56
LP
1193 void *userdata) {
1194
9f6eb1cd
KS
1195 int r;
1196
aae2b488 1197 assert(m);
9f6eb1cd
KS
1198 assert(map);
1199
9f6eb1cd
KS
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);
ffc06c35
KS
1212 if (r < 0)
1213 return r;
1214
9f6eb1cd
KS
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;
27e72d6b 1231 if (map[i].set)
f9e0eefc 1232 r = prop->set(sd_bus_message_get_bus(m), member, m, error, v);
9f6eb1cd 1233 else
a7e4861c 1234 r = map_basic(sd_bus_message_get_bus(m), member, m, flags, error, v);
2b49a470
TA
1235 if (r < 0)
1236 return r;
9f6eb1cd
KS
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)
6c1508b8 1244 return r;
9f6eb1cd
KS
1245 }
1246
ffc06c35
KS
1247 r = sd_bus_message_exit_container(m);
1248 if (r < 0)
1249 return r;
1250 }
fe506d56
LP
1251 if (r < 0)
1252 return r;
ffc06c35 1253
aae2b488
DH
1254 return sd_bus_message_exit_container(m);
1255}
1256
fe506d56
LP
1257int bus_map_all_properties(
1258 sd_bus *bus,
1259 const char *destination,
1260 const char *path,
1261 const struct bus_properties_map *map,
a7e4861c 1262 unsigned flags,
f9e0eefc 1263 sd_bus_error *error,
f37f8a61 1264 sd_bus_message **reply,
fe506d56
LP
1265 void *userdata) {
1266
4afd3348 1267 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
aae2b488
DH
1268 int r;
1269
1270 assert(bus);
1271 assert(destination);
1272 assert(path);
1273 assert(map);
a7e4861c 1274 assert(reply || (flags & BUS_MAP_STRDUP));
aae2b488
DH
1275
1276 r = sd_bus_call_method(
1277 bus,
1278 destination,
1279 path,
1280 "org.freedesktop.DBus.Properties",
1281 "GetAll",
f9e0eefc 1282 error,
aae2b488
DH
1283 &m,
1284 "s", "");
1285 if (r < 0)
1286 return r;
1287
a7e4861c 1288 r = bus_message_map_all_properties(m, map, flags, error, userdata);
f37f8a61
YW
1289 if (r < 0)
1290 return r;
1291
1292 if (reply)
1293 *reply = sd_bus_message_ref(m);
1294
1295 return r;
ffc06c35
KS
1296}
1297
38303498 1298int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) {
b1a4981a 1299 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
d21ed1ea
LP
1300 int r;
1301
1302 assert(transport >= 0);
1303 assert(transport < _BUS_TRANSPORT_MAX);
38303498 1304 assert(ret);
d21ed1ea
LP
1305
1306 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
15411c0c 1307 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
d21ed1ea
LP
1308
1309 switch (transport) {
1310
1311 case BUS_TRANSPORT_LOCAL:
1312 if (user)
38303498 1313 r = sd_bus_default_user(&bus);
fb507898
YW
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.");
d21ed1ea 1318
fb507898
YW
1319 return -EHOSTDOWN;
1320 }
1321 r = sd_bus_default_system(&bus);
1322 }
d21ed1ea
LP
1323 break;
1324
1325 case BUS_TRANSPORT_REMOTE:
38303498 1326 r = sd_bus_open_system_remote(&bus, host);
41dd15e4
LP
1327 break;
1328
de33fc62 1329 case BUS_TRANSPORT_MACHINE:
38303498 1330 r = sd_bus_open_system_machine(&bus, host);
41dd15e4
LP
1331 break;
1332
1333 default:
1334 assert_not_reached("Hmm, unknown transport type.");
1335 }
38303498
LP
1336 if (r < 0)
1337 return r;
41dd15e4 1338
38303498
LP
1339 r = sd_bus_set_exit_on_disconnect(bus, true);
1340 if (r < 0)
1341 return r;
1342
1cc6c93a 1343 *ret = TAKE_PTR(bus);
38303498
LP
1344
1345 return 0;
41dd15e4
LP
1346}
1347
266f3e26 1348int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
41dd15e4
LP
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);
15411c0c 1356 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
41dd15e4
LP
1357
1358 switch (transport) {
1359
1360 case BUS_TRANSPORT_LOCAL:
1361 if (user)
266f3e26 1362 r = bus_connect_user_systemd(bus);
fb507898 1363 else {
baaa35ad 1364 if (sd_booted() <= 0)
fb507898 1365 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
baaa35ad
ZJS
1366 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN),
1367 "System has not been booted with systemd as init system (PID 1). Can't operate.");
fb507898
YW
1368 r = bus_connect_system_systemd(bus);
1369 }
41dd15e4
LP
1370 break;
1371
1372 case BUS_TRANSPORT_REMOTE:
3db729cb 1373 r = sd_bus_open_system_remote(bus, host);
d21ed1ea
LP
1374 break;
1375
de33fc62
LP
1376 case BUS_TRANSPORT_MACHINE:
1377 r = sd_bus_open_system_machine(bus, host);
d21ed1ea
LP
1378 break;
1379
1380 default:
1381 assert_not_reached("Hmm, unknown transport type.");
1382 }
1383
1384 return r;
1385}
e6504030
LP
1386
1387int 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,
ebcf1f97
LP
1393 void *userdata,
1394 sd_bus_error *error) {
e6504030
LP
1395
1396 int b = *(bool*) userdata;
1397
1398 return sd_bus_message_append_basic(reply, 'b', &b);
1399}
1400
43ce15ac
JK
1401int 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
5d904a6a 1416 *(bool*) userdata = b;
43ce15ac
JK
1417 return 0;
1418}
1419
766c94ad
LP
1420int 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
4b58153d 1434 return sd_bus_message_append_array(reply, 'y', id->bytes, 16);
766c94ad
LP
1435}
1436
718db961
LP
1437#if __SIZEOF_SIZE_T__ != 8
1438int bus_property_get_size(
e6504030
LP
1439 sd_bus *bus,
1440 const char *path,
1441 const char *interface,
1442 const char *property,
1443 sd_bus_message *reply,
ebcf1f97
LP
1444 void *userdata,
1445 sd_bus_error *error) {
e6504030 1446
718db961 1447 uint64_t sz = *(size_t*) userdata;
e6504030 1448
718db961 1449 return sd_bus_message_append_basic(reply, 't', &sz);
e6504030 1450}
718db961
LP
1451#endif
1452
1453#if __SIZEOF_LONG__ != 8
1454int 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,
ebcf1f97
LP
1460 void *userdata,
1461 sd_bus_error *error) {
718db961
LP
1462
1463 int64_t l = *(long*) userdata;
1464
1465 return sd_bus_message_append_basic(reply, 'x', &l);
1466}
1467
1468int 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,
ebcf1f97
LP
1474 void *userdata,
1475 sd_bus_error *error) {
718db961
LP
1476
1477 uint64_t ul = *(unsigned long*) userdata;
1478
1479 return sd_bus_message_append_basic(reply, 't', &ul);
1480}
1481#endif
5b30bef8 1482
98a4c30b
DH
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 */
1520int 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
657ee2d8 1548 p = path_join(prefix, sender_label, external_label);
98a4c30b
DH
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 */
1574int 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}
057171ef 1609
c9d031c3
EV
1610int 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
6550c24c 1619 const char *is_soft;
c9d031c3
EV
1620 struct rlimit *rl;
1621 uint64_t u;
1622 rlim_t x;
1623
1624 assert(bus);
1625 assert(reply);
1626 assert(userdata);
1627
147f6858 1628 is_soft = endswith(property, "Soft");
6550c24c 1629
c9d031c3
EV
1630 rl = *(struct rlimit**) userdata;
1631 if (rl)
147f6858 1632 x = is_soft ? rl->rlim_cur : rl->rlim_max;
c9d031c3
EV
1633 else {
1634 struct rlimit buf = {};
6550c24c 1635 const char *s, *p;
c9d031c3 1636 int z;
147f6858 1637
6550c24c 1638 /* Chop off "Soft" suffix */
147f6858 1639 s = is_soft ? strndupa(property, is_soft - property) : property;
c9d031c3 1640
6550c24c
LP
1641 /* Skip over any prefix, such as "Default" */
1642 assert_se(p = strstr(s, "Limit"));
1643
1644 z = rlimit_from_string(p + 5);
c9d031c3
EV
1645 assert(z >= 0);
1646
6550c24c 1647 (void) getrlimit(z, &buf);
147f6858 1648 x = is_soft ? buf.rlim_cur : buf.rlim_max;
c9d031c3
EV
1649 }
1650
6550c24c
LP
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 */
c9d031c3
EV
1653 u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x;
1654
1655 return sd_bus_message_append(reply, "t", u);
1656}
984794ba
LP
1657
1658int 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}
d7afd945 1676
0ddf50ff 1677int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *description) {
b1a4981a 1678 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
d7afd945
LP
1679 const char *e;
1680 int r;
1681
1682 assert(ret);
1683
61252bae
ZJS
1684 /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal
1685 * turned on. */
d7afd945
LP
1686
1687 r = sd_bus_new(&bus);
1688 if (r < 0)
1689 return r;
1690
0ddf50ff
YW
1691 if (description) {
1692 r = sd_bus_set_description(bus, description);
1693 if (r < 0)
1694 return r;
1695 }
1696
d7afd945
LP
1697 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1698 if (!e)
1699 e = DEFAULT_SYSTEM_BUS_ADDRESS;
1700
1701 r = sd_bus_set_address(bus, e);
1702 if (r < 0)
1703 return r;
1704
1705 r = sd_bus_set_bus_client(bus, true);
1706 if (r < 0)
1707 return r;
1708
d7afd945
LP
1709 r = sd_bus_negotiate_creds(bus, true, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS);
1710 if (r < 0)
1711 return r;
1712
1713 r = sd_bus_set_watch_bind(bus, true);
1714 if (r < 0)
1715 return r;
1716
1717 r = sd_bus_set_connected_signal(bus, true);
1718 if (r < 0)
1719 return r;
1720
1721 r = sd_bus_start(bus);
1722 if (r < 0)
1723 return r;
1724
1cc6c93a 1725 *ret = TAKE_PTR(bus);
d7afd945
LP
1726
1727 return 0;
1728}
906cb2eb 1729
19017acb
LP
1730int bus_reply_pair_array(sd_bus_message *m, char **l) {
1731 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1732 char **k, **v;
1733 int r;
1734
1735 assert(m);
1736
61252bae
ZJS
1737 /* Reply to the specified message with a message containing a dictionary put together from the
1738 * specified strv */
19017acb
LP
1739
1740 r = sd_bus_message_new_method_return(m, &reply);
1741 if (r < 0)
1742 return r;
1743
1744 r = sd_bus_message_open_container(reply, 'a', "{ss}");
1745 if (r < 0)
1746 return r;
1747
1748 STRV_FOREACH_PAIR(k, v, l) {
1749 r = sd_bus_message_append(reply, "{ss}", *k, *v);
1750 if (r < 0)
1751 return r;
1752 }
1753
1754 r = sd_bus_message_close_container(reply);
1755 if (r < 0)
1756 return r;
1757
1758 return sd_bus_send(NULL, reply, NULL);
1759}
2a66c2a1
LP
1760
1761static void bus_message_unref_wrapper(void *m) {
1762 sd_bus_message_unref(m);
1763}
1764
1765const struct hash_ops bus_message_hash_ops = {
1766 .hash = trivial_hash_func,
1767 .compare = trivial_compare_func,
1768 .free_value = bus_message_unref_wrapper,
1769};