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