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