]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/bus-util.c
sd-bus: optionally, exit process or event loop on disconnect
[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 1018 switch (type) {
8b3b6f58 1019
9f6eb1cd 1020 case SD_BUS_TYPE_STRING: {
9f6eb1cd 1021 char **p = userdata;
8b3b6f58 1022 const char *s;
ffc06c35 1023
9f6eb1cd
KS
1024 r = sd_bus_message_read_basic(m, type, &s);
1025 if (r < 0)
8b3b6f58 1026 return r;
ffc06c35 1027
9f6eb1cd 1028 if (isempty(s))
0b83b8a4 1029 s = NULL;
ffc06c35 1030
8b3b6f58 1031 return free_and_strdup(p, s);
9f6eb1cd 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)
8b3b6f58 1040 return r;
ffc06c35 1041
9f6eb1cd
KS
1042 strv_free(*p);
1043 *p = l;
1044 l = NULL;
8b3b6f58 1045 return 0;
9f6eb1cd 1046 }
ffc06c35 1047
9f6eb1cd
KS
1048 case SD_BUS_TYPE_BOOLEAN: {
1049 unsigned b;
43dcc86a 1050 int *p = userdata;
ffc06c35 1051
9f6eb1cd
KS
1052 r = sd_bus_message_read_basic(m, type, &b);
1053 if (r < 0)
8b3b6f58 1054 return r;
ffc06c35 1055
9f6eb1cd 1056 *p = b;
8b3b6f58 1057 return 0;
9f6eb1cd 1058 }
ffc06c35 1059
bdf97b8a 1060 case SD_BUS_TYPE_INT32:
9f6eb1cd 1061 case SD_BUS_TYPE_UINT32: {
bdf97b8a 1062 uint32_t u, *p = userdata;
9f6eb1cd
KS
1063
1064 r = sd_bus_message_read_basic(m, type, &u);
1065 if (r < 0)
8b3b6f58 1066 return r;
ffc06c35 1067
9f6eb1cd 1068 *p = u;
8b3b6f58 1069 return 0;
9f6eb1cd
KS
1070 }
1071
bdf97b8a 1072 case SD_BUS_TYPE_INT64:
9f6eb1cd 1073 case SD_BUS_TYPE_UINT64: {
bdf97b8a 1074 uint64_t t, *p = userdata;
9f6eb1cd
KS
1075
1076 r = sd_bus_message_read_basic(m, type, &t);
1077 if (r < 0)
8b3b6f58 1078 return r;
ffc06c35 1079
9f6eb1cd 1080 *p = t;
8b3b6f58 1081 return 0;
9f6eb1cd
KS
1082 }
1083
52e045f9 1084 case SD_BUS_TYPE_DOUBLE: {
8b3b6f58 1085 double d, *p = userdata;
52e045f9
SS
1086
1087 r = sd_bus_message_read_basic(m, type, &d);
1088 if (r < 0)
8b3b6f58 1089 return r;
52e045f9
SS
1090
1091 *p = d;
8b3b6f58
LP
1092 return 0;
1093 }}
52e045f9 1094
8b3b6f58 1095 return -EOPNOTSUPP;
9f6eb1cd
KS
1096}
1097
fe506d56
LP
1098int bus_message_map_all_properties(
1099 sd_bus_message *m,
1100 const struct bus_properties_map *map,
1101 void *userdata) {
1102
4afd3348 1103 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
9f6eb1cd
KS
1104 int r;
1105
aae2b488 1106 assert(m);
9f6eb1cd
KS
1107 assert(map);
1108
9f6eb1cd
KS
1109 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
1110 if (r < 0)
1111 return r;
1112
1113 while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
1114 const struct bus_properties_map *prop;
1115 const char *member;
1116 const char *contents;
1117 void *v;
1118 unsigned i;
1119
1120 r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member);
ffc06c35
KS
1121 if (r < 0)
1122 return r;
1123
9f6eb1cd
KS
1124 for (i = 0, prop = NULL; map[i].member; i++)
1125 if (streq(map[i].member, member)) {
1126 prop = &map[i];
1127 break;
1128 }
1129
1130 if (prop) {
1131 r = sd_bus_message_peek_type(m, NULL, &contents);
1132 if (r < 0)
1133 return r;
1134
1135 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
1136 if (r < 0)
1137 return r;
1138
1139 v = (uint8_t *)userdata + prop->offset;
27e72d6b 1140 if (map[i].set)
fe506d56 1141 r = prop->set(sd_bus_message_get_bus(m), member, m, &error, v);
9f6eb1cd 1142 else
fe506d56 1143 r = map_basic(sd_bus_message_get_bus(m), member, m, &error, v);
2b49a470
TA
1144 if (r < 0)
1145 return r;
9f6eb1cd
KS
1146
1147 r = sd_bus_message_exit_container(m);
1148 if (r < 0)
1149 return r;
1150 } else {
1151 r = sd_bus_message_skip(m, "v");
1152 if (r < 0)
6c1508b8 1153 return r;
9f6eb1cd
KS
1154 }
1155
ffc06c35
KS
1156 r = sd_bus_message_exit_container(m);
1157 if (r < 0)
1158 return r;
1159 }
fe506d56
LP
1160 if (r < 0)
1161 return r;
ffc06c35 1162
aae2b488
DH
1163 return sd_bus_message_exit_container(m);
1164}
1165
fe506d56
LP
1166int bus_message_map_properties_changed(
1167 sd_bus_message *m,
1168 const struct bus_properties_map *map,
1169 void *userdata) {
1170
aae2b488
DH
1171 const char *member;
1172 int r, invalidated, i;
1173
aae2b488
DH
1174 assert(m);
1175 assert(map);
1176
fe506d56 1177 r = bus_message_map_all_properties(m, map, userdata);
aae2b488
DH
1178 if (r < 0)
1179 return r;
1180
1181 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "s");
1182 if (r < 0)
1183 return r;
1184
1185 invalidated = 0;
1186 while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member)) > 0)
1187 for (i = 0; map[i].member; i++)
1188 if (streq(map[i].member, member)) {
1189 ++invalidated;
1190 break;
1191 }
fe506d56
LP
1192 if (r < 0)
1193 return r;
aae2b488
DH
1194
1195 r = sd_bus_message_exit_container(m);
1196 if (r < 0)
1197 return r;
1198
1199 return invalidated;
1200}
1201
fe506d56
LP
1202int bus_map_all_properties(
1203 sd_bus *bus,
1204 const char *destination,
1205 const char *path,
1206 const struct bus_properties_map *map,
1207 void *userdata) {
1208
4afd3348
LP
1209 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1210 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
aae2b488
DH
1211 int r;
1212
1213 assert(bus);
1214 assert(destination);
1215 assert(path);
1216 assert(map);
1217
1218 r = sd_bus_call_method(
1219 bus,
1220 destination,
1221 path,
1222 "org.freedesktop.DBus.Properties",
1223 "GetAll",
1224 &error,
1225 &m,
1226 "s", "");
1227 if (r < 0)
1228 return r;
1229
fe506d56 1230 return bus_message_map_all_properties(m, map, userdata);
ffc06c35
KS
1231}
1232
266f3e26 1233int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **bus) {
d21ed1ea
LP
1234 int r;
1235
1236 assert(transport >= 0);
1237 assert(transport < _BUS_TRANSPORT_MAX);
1238 assert(bus);
1239
1240 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
15411c0c 1241 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
d21ed1ea
LP
1242
1243 switch (transport) {
1244
1245 case BUS_TRANSPORT_LOCAL:
1246 if (user)
76b54375 1247 r = sd_bus_default_user(bus);
d21ed1ea 1248 else
76b54375 1249 r = sd_bus_default_system(bus);
d21ed1ea
LP
1250
1251 break;
1252
1253 case BUS_TRANSPORT_REMOTE:
3db729cb 1254 r = sd_bus_open_system_remote(bus, host);
41dd15e4
LP
1255 break;
1256
de33fc62
LP
1257 case BUS_TRANSPORT_MACHINE:
1258 r = sd_bus_open_system_machine(bus, host);
41dd15e4
LP
1259 break;
1260
1261 default:
1262 assert_not_reached("Hmm, unknown transport type.");
1263 }
1264
1265 return r;
1266}
1267
266f3e26 1268int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
41dd15e4
LP
1269 int r;
1270
1271 assert(transport >= 0);
1272 assert(transport < _BUS_TRANSPORT_MAX);
1273 assert(bus);
1274
1275 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
15411c0c 1276 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
41dd15e4
LP
1277
1278 switch (transport) {
1279
1280 case BUS_TRANSPORT_LOCAL:
1281 if (user)
266f3e26 1282 r = bus_connect_user_systemd(bus);
41dd15e4 1283 else
266f3e26 1284 r = bus_connect_system_systemd(bus);
41dd15e4
LP
1285
1286 break;
1287
1288 case BUS_TRANSPORT_REMOTE:
3db729cb 1289 r = sd_bus_open_system_remote(bus, host);
d21ed1ea
LP
1290 break;
1291
de33fc62
LP
1292 case BUS_TRANSPORT_MACHINE:
1293 r = sd_bus_open_system_machine(bus, host);
d21ed1ea
LP
1294 break;
1295
1296 default:
1297 assert_not_reached("Hmm, unknown transport type.");
1298 }
1299
1300 return r;
1301}
e6504030
LP
1302
1303int bus_property_get_bool(
1304 sd_bus *bus,
1305 const char *path,
1306 const char *interface,
1307 const char *property,
1308 sd_bus_message *reply,
ebcf1f97
LP
1309 void *userdata,
1310 sd_bus_error *error) {
e6504030
LP
1311
1312 int b = *(bool*) userdata;
1313
1314 return sd_bus_message_append_basic(reply, 'b', &b);
1315}
1316
718db961
LP
1317#if __SIZEOF_SIZE_T__ != 8
1318int bus_property_get_size(
e6504030
LP
1319 sd_bus *bus,
1320 const char *path,
1321 const char *interface,
1322 const char *property,
1323 sd_bus_message *reply,
ebcf1f97
LP
1324 void *userdata,
1325 sd_bus_error *error) {
e6504030 1326
718db961 1327 uint64_t sz = *(size_t*) userdata;
e6504030 1328
718db961 1329 return sd_bus_message_append_basic(reply, 't', &sz);
e6504030 1330}
718db961
LP
1331#endif
1332
1333#if __SIZEOF_LONG__ != 8
1334int bus_property_get_long(
1335 sd_bus *bus,
1336 const char *path,
1337 const char *interface,
1338 const char *property,
1339 sd_bus_message *reply,
ebcf1f97
LP
1340 void *userdata,
1341 sd_bus_error *error) {
718db961
LP
1342
1343 int64_t l = *(long*) userdata;
1344
1345 return sd_bus_message_append_basic(reply, 'x', &l);
1346}
1347
1348int bus_property_get_ulong(
1349 sd_bus *bus,
1350 const char *path,
1351 const char *interface,
1352 const char *property,
1353 sd_bus_message *reply,
ebcf1f97
LP
1354 void *userdata,
1355 sd_bus_error *error) {
718db961
LP
1356
1357 uint64_t ul = *(unsigned long*) userdata;
1358
1359 return sd_bus_message_append_basic(reply, 't', &ul);
1360}
1361#endif
5b30bef8
LP
1362
1363int bus_log_parse_error(int r) {
23bbb0de 1364 return log_error_errno(r, "Failed to parse bus message: %m");
5b30bef8 1365}
f459b602
MAP
1366
1367int bus_log_create_error(int r) {
23bbb0de 1368 return log_error_errno(r, "Failed to create bus message: %m");
f459b602
MAP
1369}
1370
98a4c30b
DH
1371/**
1372 * bus_path_encode_unique() - encode unique object path
1373 * @b: bus connection or NULL
1374 * @prefix: object path prefix
1375 * @sender_id: unique-name of client, or NULL
1376 * @external_id: external ID to be chosen by client, or NULL
1377 * @ret_path: storage for encoded object path pointer
1378 *
1379 * Whenever we provide a bus API that allows clients to create and manage
1380 * server-side objects, we need to provide a unique name for these objects. If
1381 * we let the server choose the name, we suffer from a race condition: If a
1382 * client creates an object asynchronously, it cannot destroy that object until
1383 * it received the method reply. It cannot know the name of the new object,
1384 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1385 *
1386 * Therefore, many APIs allow the client to choose the unique name for newly
1387 * created objects. There're two problems to solve, though:
1388 * 1) Object names are usually defined via dbus object paths, which are
1389 * usually globally namespaced. Therefore, multiple clients must be able
1390 * to choose unique object names without interference.
1391 * 2) If multiple libraries share the same bus connection, they must be
1392 * able to choose unique object names without interference.
1393 * The first problem is solved easily by prefixing a name with the
1394 * unique-bus-name of a connection. The server side must enforce this and
1395 * reject any other name. The second problem is solved by providing unique
1396 * suffixes from within sd-bus.
1397 *
1398 * This helper allows clients to create unique object-paths. It uses the
1399 * template '/prefix/sender_id/external_id' and returns the new path in
1400 * @ret_path (must be freed by the caller).
1401 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1402 * NULL, this function allocates a unique suffix via @b (by requesting a new
1403 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1404 * NULL.
1405 *
1406 * Returns: 0 on success, negative error code on failure.
1407 */
1408int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) {
1409 _cleanup_free_ char *sender_label = NULL, *external_label = NULL;
1410 char external_buf[DECIMAL_STR_MAX(uint64_t)], *p;
1411 int r;
1412
1413 assert_return(b || (sender_id && external_id), -EINVAL);
1414 assert_return(object_path_is_valid(prefix), -EINVAL);
1415 assert_return(ret_path, -EINVAL);
1416
1417 if (!sender_id) {
1418 r = sd_bus_get_unique_name(b, &sender_id);
1419 if (r < 0)
1420 return r;
1421 }
1422
1423 if (!external_id) {
1424 xsprintf(external_buf, "%"PRIu64, ++b->cookie);
1425 external_id = external_buf;
1426 }
1427
1428 sender_label = bus_label_escape(sender_id);
1429 if (!sender_label)
1430 return -ENOMEM;
1431
1432 external_label = bus_label_escape(external_id);
1433 if (!external_label)
1434 return -ENOMEM;
1435
1436 p = strjoin(prefix, "/", sender_label, "/", external_label, NULL);
1437 if (!p)
1438 return -ENOMEM;
1439
1440 *ret_path = p;
1441 return 0;
1442}
1443
1444/**
1445 * bus_path_decode_unique() - decode unique object path
1446 * @path: object path to decode
1447 * @prefix: object path prefix
1448 * @ret_sender: output parameter for sender-id label
1449 * @ret_external: output parameter for external-id label
1450 *
1451 * This does the reverse of bus_path_encode_unique() (see its description for
1452 * details). Both trailing labels, sender-id and external-id, are unescaped and
1453 * returned in the given output parameters (the caller must free them).
1454 *
1455 * Note that this function returns 0 if the path does not match the template
1456 * (see bus_path_encode_unique()), 1 if it matched.
1457 *
1458 * Returns: Negative error code on failure, 0 if the given object path does not
1459 * match the template (return parameters are set to NULL), 1 if it was
1460 * parsed successfully (return parameters contain allocated labels).
1461 */
1462int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) {
1463 const char *p, *q;
1464 char *sender, *external;
1465
1466 assert(object_path_is_valid(path));
1467 assert(object_path_is_valid(prefix));
1468 assert(ret_sender);
1469 assert(ret_external);
1470
1471 p = object_path_startswith(path, prefix);
1472 if (!p) {
1473 *ret_sender = NULL;
1474 *ret_external = NULL;
1475 return 0;
1476 }
1477
1478 q = strchr(p, '/');
1479 if (!q) {
1480 *ret_sender = NULL;
1481 *ret_external = NULL;
1482 return 0;
1483 }
1484
1485 sender = bus_label_unescape_n(p, q - p);
1486 external = bus_label_unescape(q + 1);
1487 if (!sender || !external) {
1488 free(sender);
1489 free(external);
1490 return -ENOMEM;
1491 }
1492
1493 *ret_sender = sender;
1494 *ret_external = external;
1495 return 1;
1496}
057171ef 1497
c9d031c3
EV
1498int bus_property_get_rlimit(
1499 sd_bus *bus,
1500 const char *path,
1501 const char *interface,
1502 const char *property,
1503 sd_bus_message *reply,
1504 void *userdata,
1505 sd_bus_error *error) {
1506
1507 struct rlimit *rl;
1508 uint64_t u;
1509 rlim_t x;
147f6858 1510 const char *is_soft;
c9d031c3
EV
1511
1512 assert(bus);
1513 assert(reply);
1514 assert(userdata);
1515
147f6858 1516 is_soft = endswith(property, "Soft");
c9d031c3
EV
1517 rl = *(struct rlimit**) userdata;
1518 if (rl)
147f6858 1519 x = is_soft ? rl->rlim_cur : rl->rlim_max;
c9d031c3
EV
1520 else {
1521 struct rlimit buf = {};
1522 int z;
147f6858
EV
1523 const char *s;
1524
1525 s = is_soft ? strndupa(property, is_soft - property) : property;
c9d031c3 1526
147f6858 1527 z = rlimit_from_string(strstr(s, "Limit"));
c9d031c3
EV
1528 assert(z >= 0);
1529
1530 getrlimit(z, &buf);
147f6858 1531 x = is_soft ? buf.rlim_cur : buf.rlim_max;
c9d031c3
EV
1532 }
1533
1534 /* rlim_t might have different sizes, let's map
1535 * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on
1536 * all archs */
1537 u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x;
1538
1539 return sd_bus_message_append(reply, "t", u);
1540}