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