]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/bus-util.c
Merge pull request #31943 from yuwata/sd-ndisc-option-getter-remover
[thirdparty/systemd.git] / src / shared / bus-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <inttypes.h>
6 #include <stdlib.h>
7 #include <sys/ioctl.h>
8 #include <sys/resource.h>
9 #include <sys/socket.h>
10 #include <unistd.h>
11
12 #include "sd-bus.h"
13 #include "sd-daemon.h"
14 #include "sd-event.h"
15 #include "sd-id128.h"
16
17 #include "bus-common-errors.h"
18 #include "bus-internal.h"
19 #include "bus-label.h"
20 #include "bus-util.h"
21 #include "capsule-util.h"
22 #include "chase.h"
23 #include "daemon-util.h"
24 #include "data-fd-util.h"
25 #include "fd-util.h"
26 #include "format-util.h"
27 #include "memstream-util.h"
28 #include "path-util.h"
29 #include "socket-util.h"
30 #include "stdio-util.h"
31 #include "uid-classification.h"
32
33 static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
34 sd_event *e = ASSERT_PTR(userdata);
35
36 assert(m);
37
38 sd_bus_close(sd_bus_message_get_bus(m));
39 sd_event_exit(e, 0);
40
41 return 1;
42 }
43
44 int bus_log_address_error(int r, BusTransport transport) {
45 bool hint = transport == BUS_TRANSPORT_LOCAL && r == -ENOMEDIUM;
46
47 return log_error_errno(r,
48 hint ? "Failed to set bus address: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined (consider using --machine=<user>@.host --user to connect to bus of other user)" :
49 "Failed to set bus address: %m");
50 }
51
52 int bus_log_connect_error(int r, BusTransport transport) {
53 bool hint_vars = transport == BUS_TRANSPORT_LOCAL && r == -ENOMEDIUM,
54 hint_addr = transport == BUS_TRANSPORT_LOCAL && ERRNO_IS_PRIVILEGE(r);
55
56 return log_error_errno(r,
57 r == hint_vars ? "Failed to connect to bus: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined (consider using --machine=<user>@.host --user to connect to bus of other user)" :
58 r == hint_addr ? "Failed to connect to bus: Operation not permitted (consider using --machine=<user>@.host --user to connect to bus of other user)" :
59 "Failed to connect to bus: %m");
60 }
61
62 int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name) {
63 const char *match;
64 const char *unique;
65 int r;
66
67 assert(e);
68 assert(bus);
69 assert(name);
70
71 /* We unregister the name here and then wait for the
72 * NameOwnerChanged signal for this event to arrive before we
73 * quit. We do this in order to make sure that any queued
74 * requests are still processed before we really exit. */
75
76 r = sd_bus_get_unique_name(bus, &unique);
77 if (r < 0)
78 return r;
79
80 match = strjoina(
81 "sender='org.freedesktop.DBus',"
82 "type='signal',"
83 "interface='org.freedesktop.DBus',"
84 "member='NameOwnerChanged',"
85 "path='/org/freedesktop/DBus',"
86 "arg0='", name, "',",
87 "arg1='", unique, "',",
88 "arg2=''");
89
90 r = sd_bus_add_match_async(bus, NULL, match, name_owner_change_callback, NULL, e);
91 if (r < 0)
92 return r;
93
94 r = sd_bus_release_name_async(bus, NULL, name, NULL, NULL);
95 if (r < 0)
96 return r;
97
98 return 0;
99 }
100
101 int bus_event_loop_with_idle(
102 sd_event *e,
103 sd_bus *bus,
104 const char *name,
105 usec_t timeout,
106 check_idle_t check_idle,
107 void *userdata) {
108
109 bool exiting = false;
110 int r, code;
111
112 assert(e);
113 assert(bus);
114 assert(name);
115
116 for (;;) {
117 bool idle;
118
119 r = sd_event_get_state(e);
120 if (r < 0)
121 return r;
122 if (r == SD_EVENT_FINISHED)
123 break;
124
125 if (check_idle)
126 idle = check_idle(userdata);
127 else
128 idle = true;
129
130 r = sd_event_run(e, exiting || !idle ? UINT64_MAX : timeout);
131 if (r < 0)
132 return r;
133
134 if (r == 0 && !exiting && idle) {
135 /* Inform the service manager that we are going down, so that it will queue all
136 * further start requests, instead of assuming we are still running. */
137 (void) sd_notify(false, NOTIFY_STOPPING);
138
139 r = bus_async_unregister_and_exit(e, bus, name);
140 if (r < 0)
141 return r;
142
143 exiting = true;
144 }
145 }
146
147 r = sd_event_get_exit_code(e, &code);
148 if (r < 0)
149 return r;
150
151 return code;
152 }
153
154 int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error) {
155 _cleanup_(sd_bus_message_unrefp) sd_bus_message *rep = NULL;
156 int r, has_owner = 0;
157
158 assert(c);
159 assert(name);
160
161 r = sd_bus_call_method(c,
162 "org.freedesktop.DBus",
163 "/org/freedesktop/dbus",
164 "org.freedesktop.DBus",
165 "NameHasOwner",
166 error,
167 &rep,
168 "s",
169 name);
170 if (r < 0)
171 return r;
172
173 r = sd_bus_message_read_basic(rep, 'b', &has_owner);
174 if (r < 0)
175 return sd_bus_error_set_errno(error, r);
176
177 return has_owner;
178 }
179
180 bool bus_error_is_unknown_service(const sd_bus_error *error) {
181 return sd_bus_error_has_names(error,
182 SD_BUS_ERROR_SERVICE_UNKNOWN,
183 SD_BUS_ERROR_NAME_HAS_NO_OWNER,
184 BUS_ERROR_NO_SUCH_UNIT);
185 }
186
187 int bus_check_peercred(sd_bus *c) {
188 struct ucred ucred;
189 int fd, r;
190
191 assert(c);
192
193 fd = sd_bus_get_fd(c);
194 if (fd < 0)
195 return fd;
196
197 r = getpeercred(fd, &ucred);
198 if (r < 0)
199 return r;
200
201 if (ucred.uid != 0 && ucred.uid != geteuid())
202 return -EPERM;
203
204 return 1;
205 }
206
207 int bus_connect_system_systemd(sd_bus **ret_bus) {
208 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
209 int r;
210
211 assert(ret_bus);
212
213 if (geteuid() != 0)
214 return sd_bus_default_system(ret_bus);
215
216 /* If we are root then let's talk directly to the system
217 * instance, instead of going via the bus */
218
219 r = sd_bus_new(&bus);
220 if (r < 0)
221 return r;
222
223 r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
224 if (r < 0)
225 return r;
226
227 r = sd_bus_start(bus);
228 if (r < 0)
229 return sd_bus_default_system(ret_bus);
230
231 r = bus_check_peercred(bus);
232 if (r < 0)
233 return r;
234
235 *ret_bus = TAKE_PTR(bus);
236 return 0;
237 }
238
239 int bus_connect_user_systemd(sd_bus **ret_bus) {
240 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
241 _cleanup_free_ char *ee = NULL;
242 const char *e;
243 int r;
244
245 assert(ret_bus);
246
247 e = secure_getenv("XDG_RUNTIME_DIR");
248 if (!e)
249 return sd_bus_default_user(ret_bus);
250
251 ee = bus_address_escape(e);
252 if (!ee)
253 return -ENOMEM;
254
255 r = sd_bus_new(&bus);
256 if (r < 0)
257 return r;
258
259 bus->address = strjoin("unix:path=", ee, "/systemd/private");
260 if (!bus->address)
261 return -ENOMEM;
262
263 r = sd_bus_start(bus);
264 if (r < 0)
265 return sd_bus_default_user(ret_bus);
266
267 r = bus_check_peercred(bus);
268 if (r < 0)
269 return r;
270
271 *ret_bus = TAKE_PTR(bus);
272 return 0;
273 }
274
275 static int pin_capsule_socket(const char *capsule, const char *suffix, uid_t *ret_uid, gid_t *ret_gid) {
276 _cleanup_close_ int inode_fd = -EBADF;
277 _cleanup_free_ char *p = NULL;
278 struct stat st;
279 int r;
280
281 assert(capsule);
282 assert(suffix);
283
284 p = path_join("/run/capsules", capsule, suffix);
285 if (!p)
286 return -ENOMEM;
287
288 /* We enter territory owned by the user, hence let's be paranoid about symlinks and ownership */
289 r = chase(p, /* root= */ NULL, CHASE_SAFE|CHASE_PROHIBIT_SYMLINKS, /* ret_path= */ NULL, &inode_fd);
290 if (r < 0)
291 return r;
292
293 if (fstat(inode_fd, &st) < 0)
294 return -errno;
295
296 /* Paranoid safety check */
297 if (uid_is_system(st.st_uid) || gid_is_system(st.st_gid))
298 return -EPERM;
299
300 *ret_uid = st.st_uid;
301 *ret_gid = st.st_gid;
302
303 return TAKE_FD(inode_fd);
304 }
305
306 int bus_connect_capsule_systemd(const char *capsule, sd_bus **ret_bus) {
307 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
308 _cleanup_close_ int inode_fd = -EBADF;
309 _cleanup_free_ char *pp = NULL;
310 uid_t uid;
311 gid_t gid;
312 int r;
313
314 assert(capsule);
315 assert(ret_bus);
316
317 r = capsule_name_is_valid(capsule);
318 if (r < 0)
319 return r;
320 if (r == 0)
321 return -EINVAL;
322
323 /* Connects to a capsule's user bus. We need to do so under the capsule's UID/GID, otherwise the
324 * the service manager might refuse our connection. Hence fake it. */
325
326 inode_fd = pin_capsule_socket(capsule, "systemd/private", &uid, &gid);
327 if (inode_fd < 0)
328 return inode_fd;
329
330 pp = bus_address_escape(FORMAT_PROC_FD_PATH(inode_fd));
331 if (!pp)
332 return -ENOMEM;
333
334 r = sd_bus_new(&bus);
335 if (r < 0)
336 return r;
337
338 if (asprintf(&bus->address, "unix:path=%s,uid=" UID_FMT ",gid=" GID_FMT, pp, uid, gid) < 0)
339 return -ENOMEM;
340
341 r = sd_bus_start(bus);
342 if (r < 0)
343 return r;
344
345 *ret_bus = TAKE_PTR(bus);
346 return 0;
347 }
348
349 int bus_set_address_capsule_bus(sd_bus *bus, const char *capsule, int *ret_pin_fd) {
350 _cleanup_free_ char *pp = NULL;
351 _cleanup_close_ int inode_fd = -EBADF;
352 uid_t uid;
353 gid_t gid;
354 int r;
355
356 assert(bus);
357 assert(capsule);
358 assert(ret_pin_fd);
359
360 r = capsule_name_is_valid(capsule);
361 if (r < 0)
362 return r;
363 if (r == 0)
364 return -EINVAL;
365
366 inode_fd = pin_capsule_socket(capsule, "bus", &uid, &gid);
367 if (inode_fd < 0)
368 return inode_fd;
369
370 pp = bus_address_escape(FORMAT_PROC_FD_PATH(inode_fd));
371 if (!pp)
372 return -ENOMEM;
373
374 if (asprintf(&bus->address, "unix:path=%s,uid=" UID_FMT ",gid=" GID_FMT, pp, uid, gid) < 0)
375 return -ENOMEM;
376
377 *ret_pin_fd = TAKE_FD(inode_fd); /* This fd must be kept pinned until the connection has been established */
378 return 0;
379 }
380
381 int bus_connect_capsule_bus(const char *capsule, sd_bus **ret_bus) {
382 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
383 _cleanup_close_ int inode_fd = -EBADF;
384 int r;
385
386 assert(capsule);
387 assert(ret_bus);
388
389 r = sd_bus_new(&bus);
390 if (r < 0)
391 return r;
392
393 r = bus_set_address_capsule_bus(bus, capsule, &inode_fd);
394 if (r < 0)
395 return r;
396
397 r = sd_bus_set_bus_client(bus, true);
398 if (r < 0)
399 return r;
400
401 r = sd_bus_start(bus);
402 if (r < 0)
403 return r;
404
405 *ret_bus = TAKE_PTR(bus);
406 return 0;
407 }
408
409 int bus_connect_transport(
410 BusTransport transport,
411 const char *host,
412 RuntimeScope runtime_scope,
413 sd_bus **ret) {
414
415 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
416 int r;
417
418 assert(transport >= 0);
419 assert(transport < _BUS_TRANSPORT_MAX);
420 assert(ret);
421
422 switch (transport) {
423
424 case BUS_TRANSPORT_LOCAL:
425 assert_return(!host, -EINVAL);
426
427 switch (runtime_scope) {
428
429 case RUNTIME_SCOPE_USER:
430 r = sd_bus_default_user(&bus);
431 break;
432
433 case RUNTIME_SCOPE_SYSTEM:
434 if (sd_booted() <= 0)
435 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
436 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN),
437 "System has not been booted with systemd as init system (PID 1). Can't operate.");
438 r = sd_bus_default_system(&bus);
439 break;
440
441 default:
442 assert_not_reached();
443 }
444 break;
445
446 case BUS_TRANSPORT_REMOTE:
447 assert_return(runtime_scope == RUNTIME_SCOPE_SYSTEM, -EOPNOTSUPP);
448
449 r = sd_bus_open_system_remote(&bus, host);
450 break;
451
452 case BUS_TRANSPORT_MACHINE:
453 switch (runtime_scope) {
454
455 case RUNTIME_SCOPE_USER:
456 r = sd_bus_open_user_machine(&bus, host);
457 break;
458
459 case RUNTIME_SCOPE_SYSTEM:
460 r = sd_bus_open_system_machine(&bus, host);
461 break;
462
463 default:
464 assert_not_reached();
465 }
466
467 break;
468
469 case BUS_TRANSPORT_CAPSULE:
470 assert_return(runtime_scope == RUNTIME_SCOPE_USER, -EINVAL);
471
472 r = bus_connect_capsule_bus(host, &bus);
473 break;
474
475 default:
476 assert_not_reached();
477 }
478 if (r < 0)
479 return r;
480
481 r = sd_bus_set_exit_on_disconnect(bus, true);
482 if (r < 0)
483 return r;
484
485 *ret = TAKE_PTR(bus);
486 return 0;
487 }
488
489 int bus_connect_transport_systemd(
490 BusTransport transport,
491 const char *host,
492 RuntimeScope runtime_scope,
493 sd_bus **ret_bus) {
494
495 assert(transport >= 0);
496 assert(transport < _BUS_TRANSPORT_MAX);
497 assert(ret_bus);
498
499 switch (transport) {
500
501 case BUS_TRANSPORT_LOCAL:
502 assert_return(!host, -EINVAL);
503
504 switch (runtime_scope) {
505
506 case RUNTIME_SCOPE_USER:
507 return bus_connect_user_systemd(ret_bus);
508
509 case RUNTIME_SCOPE_SYSTEM:
510 if (sd_booted() <= 0)
511 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
512 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN),
513 "System has not been booted with systemd as init system (PID 1). Can't operate.");
514 return bus_connect_system_systemd(ret_bus);
515
516 default:
517 assert_not_reached();
518 }
519
520 break;
521
522 case BUS_TRANSPORT_REMOTE:
523 assert_return(runtime_scope == RUNTIME_SCOPE_SYSTEM, -EOPNOTSUPP);
524 return sd_bus_open_system_remote(ret_bus, host);
525
526 case BUS_TRANSPORT_MACHINE:
527 assert_return(runtime_scope == RUNTIME_SCOPE_SYSTEM, -EOPNOTSUPP);
528 return sd_bus_open_system_machine(ret_bus, host);
529
530 case BUS_TRANSPORT_CAPSULE:
531 assert_return(runtime_scope == RUNTIME_SCOPE_USER, -EINVAL);
532 return bus_connect_capsule_systemd(host, ret_bus);
533
534 default:
535 assert_not_reached();
536 }
537 }
538
539 /**
540 * bus_path_encode_unique() - encode unique object path
541 * @b: bus connection or NULL
542 * @prefix: object path prefix
543 * @sender_id: unique-name of client, or NULL
544 * @external_id: external ID to be chosen by client, or NULL
545 * @ret_path: storage for encoded object path pointer
546 *
547 * Whenever we provide a bus API that allows clients to create and manage
548 * server-side objects, we need to provide a unique name for these objects. If
549 * we let the server choose the name, we suffer from a race condition: If a
550 * client creates an object asynchronously, it cannot destroy that object until
551 * it received the method reply. It cannot know the name of the new object,
552 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
553 *
554 * Therefore, many APIs allow the client to choose the unique name for newly
555 * created objects. There're two problems to solve, though:
556 * 1) Object names are usually defined via dbus object paths, which are
557 * usually globally namespaced. Therefore, multiple clients must be able
558 * to choose unique object names without interference.
559 * 2) If multiple libraries share the same bus connection, they must be
560 * able to choose unique object names without interference.
561 * The first problem is solved easily by prefixing a name with the
562 * unique-bus-name of a connection. The server side must enforce this and
563 * reject any other name. The second problem is solved by providing unique
564 * suffixes from within sd-bus.
565 *
566 * This helper allows clients to create unique object-paths. It uses the
567 * template '/prefix/sender_id/external_id' and returns the new path in
568 * @ret_path (must be freed by the caller).
569 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
570 * NULL, this function allocates a unique suffix via @b (by requesting a new
571 * cookie). If both @sender_id and @external_id are given, @b can be passed as
572 * NULL.
573 *
574 * Returns: 0 on success, negative error code on failure.
575 */
576 int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) {
577 _cleanup_free_ char *sender_label = NULL, *external_label = NULL;
578 char external_buf[DECIMAL_STR_MAX(uint64_t)], *p;
579 int r;
580
581 assert_return(b || (sender_id && external_id), -EINVAL);
582 assert_return(sd_bus_object_path_is_valid(prefix), -EINVAL);
583 assert_return(ret_path, -EINVAL);
584
585 if (!sender_id) {
586 r = sd_bus_get_unique_name(b, &sender_id);
587 if (r < 0)
588 return r;
589 }
590
591 if (!external_id) {
592 xsprintf(external_buf, "%"PRIu64, ++b->cookie);
593 external_id = external_buf;
594 }
595
596 sender_label = bus_label_escape(sender_id);
597 if (!sender_label)
598 return -ENOMEM;
599
600 external_label = bus_label_escape(external_id);
601 if (!external_label)
602 return -ENOMEM;
603
604 p = path_join(prefix, sender_label, external_label);
605 if (!p)
606 return -ENOMEM;
607
608 *ret_path = p;
609 return 0;
610 }
611
612 /**
613 * bus_path_decode_unique() - decode unique object path
614 * @path: object path to decode
615 * @prefix: object path prefix
616 * @ret_sender: output parameter for sender-id label
617 * @ret_external: output parameter for external-id label
618 *
619 * This does the reverse of bus_path_encode_unique() (see its description for
620 * details). Both trailing labels, sender-id and external-id, are unescaped and
621 * returned in the given output parameters (the caller must free them).
622 *
623 * Note that this function returns 0 if the path does not match the template
624 * (see bus_path_encode_unique()), 1 if it matched.
625 *
626 * Returns: Negative error code on failure, 0 if the given object path does not
627 * match the template (return parameters are set to NULL), 1 if it was
628 * parsed successfully (return parameters contain allocated labels).
629 */
630 int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) {
631 const char *p, *q;
632 char *sender, *external;
633
634 assert(sd_bus_object_path_is_valid(path));
635 assert(sd_bus_object_path_is_valid(prefix));
636 assert(ret_sender);
637 assert(ret_external);
638
639 p = object_path_startswith(path, prefix);
640 if (!p) {
641 *ret_sender = NULL;
642 *ret_external = NULL;
643 return 0;
644 }
645
646 q = strchr(p, '/');
647 if (!q) {
648 *ret_sender = NULL;
649 *ret_external = NULL;
650 return 0;
651 }
652
653 sender = bus_label_unescape_n(p, q - p);
654 external = bus_label_unescape(q + 1);
655 if (!sender || !external) {
656 free(sender);
657 free(external);
658 return -ENOMEM;
659 }
660
661 *ret_sender = sender;
662 *ret_external = external;
663 return 1;
664 }
665
666 int bus_track_add_name_many(sd_bus_track *t, char **l) {
667 int r = 0;
668
669 assert(t);
670
671 /* Continues adding after failure, and returns the first failure. */
672
673 STRV_FOREACH(i, l)
674 RET_GATHER(r, sd_bus_track_add_name(t, *i));
675 return r;
676 }
677
678 int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *description) {
679 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
680 const char *e;
681 int r;
682
683 assert(ret);
684
685 /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal
686 * turned on. */
687
688 r = sd_bus_new(&bus);
689 if (r < 0)
690 return r;
691
692 if (description) {
693 r = sd_bus_set_description(bus, description);
694 if (r < 0)
695 return r;
696 }
697
698 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
699 if (!e)
700 e = DEFAULT_SYSTEM_BUS_ADDRESS;
701
702 r = sd_bus_set_address(bus, e);
703 if (r < 0)
704 return r;
705
706 r = sd_bus_set_bus_client(bus, true);
707 if (r < 0)
708 return r;
709
710 r = sd_bus_negotiate_creds(bus, true, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS);
711 if (r < 0)
712 return r;
713
714 r = sd_bus_set_watch_bind(bus, true);
715 if (r < 0)
716 return r;
717
718 r = sd_bus_set_connected_signal(bus, true);
719 if (r < 0)
720 return r;
721
722 r = sd_bus_start(bus);
723 if (r < 0)
724 return r;
725
726 *ret = TAKE_PTR(bus);
727
728 return 0;
729 }
730
731 int bus_reply_pair_array(sd_bus_message *m, char **l) {
732 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
733 int r;
734
735 assert(m);
736
737 /* Reply to the specified message with a message containing a dictionary put together from the
738 * specified strv */
739
740 r = sd_bus_message_new_method_return(m, &reply);
741 if (r < 0)
742 return r;
743
744 r = sd_bus_message_open_container(reply, 'a', "{ss}");
745 if (r < 0)
746 return r;
747
748 STRV_FOREACH_PAIR(k, v, l) {
749 r = sd_bus_message_append(reply, "{ss}", *k, *v);
750 if (r < 0)
751 return r;
752 }
753
754 r = sd_bus_message_close_container(reply);
755 if (r < 0)
756 return r;
757
758 return sd_bus_send(NULL, reply, NULL);
759 }
760
761 static int method_dump_memory_state_by_fd(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
762 _cleanup_(memstream_done) MemStream m = {};
763 _cleanup_free_ char *dump = NULL;
764 _cleanup_close_ int fd = -EBADF;
765 size_t dump_size;
766 FILE *f;
767 int r;
768
769 assert(message);
770
771 f = memstream_init(&m);
772 if (!f)
773 return -ENOMEM;
774
775 r = RET_NERRNO(malloc_info(/* options= */ 0, f));
776 if (r < 0)
777 return r;
778
779 r = memstream_finalize(&m, &dump, &dump_size);
780 if (r < 0)
781 return r;
782
783 fd = acquire_data_fd_full(dump, dump_size, /* flags = */ 0);
784 if (fd < 0)
785 return fd;
786
787 r = sd_bus_reply_method_return(message, "h", fd);
788 if (r < 0)
789 return r;
790
791 return 1; /* Stop further processing */
792 }
793
794 /* The default install callback will fail and disconnect the bus if it cannot register the match, but this
795 * is only a debug method, we definitely don't want to fail in case there's some permission issue. */
796 static int dummy_install_callback(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
797 return 1;
798 }
799
800 int bus_register_malloc_status(sd_bus *bus, const char *destination) {
801 const char *match;
802 int r;
803
804 assert(bus);
805 assert(!isempty(destination));
806
807 match = strjoina("type='method_call',"
808 "interface='org.freedesktop.MemoryAllocation1',"
809 "path='/org/freedesktop/MemoryAllocation1',"
810 "destination='", destination, "',",
811 "member='GetMallocInfo'");
812
813 r = sd_bus_add_match_async(bus, NULL, match, method_dump_memory_state_by_fd, dummy_install_callback, NULL);
814 if (r < 0)
815 return log_debug_errno(r, "Failed to subscribe to GetMallocInfo() calls on MemoryAllocation1 interface: %m");
816
817 return 0;
818 }
819
820 static void bus_message_unref_wrapper(void *m) {
821 sd_bus_message_unref(m);
822 }
823
824 const struct hash_ops bus_message_hash_ops = {
825 .hash = trivial_hash_func,
826 .compare = trivial_compare_func,
827 .free_value = bus_message_unref_wrapper,
828 };
829
830 int bus_message_append_string_set(sd_bus_message *m, Set *set) {
831 const char *s;
832 int r;
833
834 assert(m);
835
836 r = sd_bus_message_open_container(m, 'a', "s");
837 if (r < 0)
838 return r;
839
840 SET_FOREACH(s, set) {
841 r = sd_bus_message_append(m, "s", s);
842 if (r < 0)
843 return r;
844 }
845
846 return sd_bus_message_close_container(m);
847 }
848
849 int bus_property_get_string_set(
850 sd_bus *bus,
851 const char *path,
852 const char *interface,
853 const char *property,
854 sd_bus_message *reply,
855 void *userdata,
856 sd_bus_error *error) {
857
858 Set **s = ASSERT_PTR(userdata);
859
860 assert(bus);
861 assert(property);
862 assert(reply);
863
864 return bus_message_append_string_set(reply, *s);
865 }
866
867 int bus_creds_get_pidref(
868 sd_bus_creds *c,
869 PidRef *ret) {
870
871 int pidfd = -EBADF;
872 pid_t pid;
873 int r;
874
875 assert(c);
876 assert(ret);
877
878 r = sd_bus_creds_get_pid(c, &pid);
879 if (r < 0)
880 return r;
881
882 r = sd_bus_creds_get_pidfd_dup(c, &pidfd);
883 if (r < 0 && r != -ENODATA)
884 return r;
885
886 *ret = (PidRef) {
887 .pid = pid,
888 .fd = pidfd,
889 };
890
891 return 0;
892 }
893
894 int bus_query_sender_pidref(
895 sd_bus_message *m,
896 PidRef *ret) {
897
898 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
899 int r;
900
901 assert(m);
902 assert(ret);
903
904 r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_PID|SD_BUS_CREDS_PIDFD, &creds);
905 if (r < 0)
906 return r;
907
908 return bus_creds_get_pidref(creds, ret);
909 }
910
911 int bus_message_read_id128(sd_bus_message *m, sd_id128_t *ret) {
912 const void *a;
913 size_t sz;
914 int r;
915
916 assert(m);
917
918 r = sd_bus_message_read_array(m, 'y', &a, &sz);
919 if (r < 0)
920 return r;
921
922 switch (sz) {
923 case 0:
924 if (ret)
925 *ret = SD_ID128_NULL;
926 break;
927
928 case sizeof(sd_id128_t):
929 if (ret)
930 memcpy(ret, a, sz);
931 break;
932
933 default:
934 return -EINVAL;
935 }
936
937 return 0;
938 }