]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/bus-util.c
machined: return recognizable error when we try to register the same machine name...
[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 assert(ret_uid);
284 assert(ret_gid);
285
286 p = path_join("/run/capsules", capsule, suffix);
287 if (!p)
288 return -ENOMEM;
289
290 /* We enter territory owned by the user, hence let's be paranoid about symlinks and ownership */
291 r = chase(p, /* root= */ NULL, CHASE_SAFE|CHASE_PROHIBIT_SYMLINKS, /* ret_path= */ NULL, &inode_fd);
292 if (r < 0)
293 return r;
294
295 if (fstat(inode_fd, &st) < 0)
296 return negative_errno();
297
298 /* Paranoid safety check */
299 if (uid_is_system(st.st_uid) || gid_is_system(st.st_gid))
300 return -EPERM;
301
302 *ret_uid = st.st_uid;
303 *ret_gid = st.st_gid;
304
305 return TAKE_FD(inode_fd);
306 }
307
308 static int bus_set_address_capsule(sd_bus *bus, const char *capsule, const char *suffix, int *ret_pin_fd) {
309 _cleanup_close_ int inode_fd = -EBADF;
310 _cleanup_free_ char *pp = NULL;
311 uid_t uid;
312 gid_t gid;
313 int r;
314
315 assert(bus);
316 assert(capsule);
317 assert(suffix);
318 assert(ret_pin_fd);
319
320 /* Connects to a capsule's user bus. We need to do so under the capsule's UID/GID, otherwise
321 * the service manager might refuse our connection. Hence fake it. */
322
323 r = capsule_name_is_valid(capsule);
324 if (r < 0)
325 return r;
326 if (r == 0)
327 return -EINVAL;
328
329 inode_fd = pin_capsule_socket(capsule, suffix, &uid, &gid);
330 if (inode_fd < 0)
331 return inode_fd;
332
333 pp = bus_address_escape(FORMAT_PROC_FD_PATH(inode_fd));
334 if (!pp)
335 return -ENOMEM;
336
337 if (asprintf(&bus->address, "unix:path=%s,uid=" UID_FMT ",gid=" GID_FMT, pp, uid, gid) < 0)
338 return -ENOMEM;
339
340 *ret_pin_fd = TAKE_FD(inode_fd); /* This fd must be kept pinned until the connection has been established */
341 return 0;
342 }
343
344 int bus_set_address_capsule_bus(sd_bus *bus, const char *capsule, int *ret_pin_fd) {
345 return bus_set_address_capsule(bus, capsule, "bus", ret_pin_fd);
346 }
347
348 int bus_connect_capsule_systemd(const char *capsule, sd_bus **ret_bus) {
349 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
350 _cleanup_close_ int inode_fd = -EBADF;
351 int r;
352
353 assert(capsule);
354 assert(ret_bus);
355
356 r = sd_bus_new(&bus);
357 if (r < 0)
358 return r;
359
360 r = bus_set_address_capsule(bus, capsule, "systemd/private", &inode_fd);
361 if (r < 0)
362 return r;
363
364 r = sd_bus_start(bus);
365 if (r < 0)
366 return r;
367
368 *ret_bus = TAKE_PTR(bus);
369 return 0;
370 }
371
372 int bus_connect_capsule_bus(const char *capsule, sd_bus **ret_bus) {
373 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
374 _cleanup_close_ int inode_fd = -EBADF;
375 int r;
376
377 assert(capsule);
378 assert(ret_bus);
379
380 r = sd_bus_new(&bus);
381 if (r < 0)
382 return r;
383
384 r = bus_set_address_capsule_bus(bus, capsule, &inode_fd);
385 if (r < 0)
386 return r;
387
388 r = sd_bus_set_bus_client(bus, true);
389 if (r < 0)
390 return r;
391
392 r = sd_bus_start(bus);
393 if (r < 0)
394 return r;
395
396 *ret_bus = TAKE_PTR(bus);
397 return 0;
398 }
399
400 int bus_connect_transport(
401 BusTransport transport,
402 const char *host,
403 RuntimeScope runtime_scope,
404 sd_bus **ret) {
405
406 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
407 int r;
408
409 assert(transport >= 0);
410 assert(transport < _BUS_TRANSPORT_MAX);
411 assert(ret);
412
413 switch (transport) {
414
415 case BUS_TRANSPORT_LOCAL:
416 assert_return(!host, -EINVAL);
417
418 switch (runtime_scope) {
419
420 case RUNTIME_SCOPE_USER:
421 r = sd_bus_default_user(&bus);
422 break;
423
424 case RUNTIME_SCOPE_SYSTEM:
425 if (sd_booted() <= 0)
426 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
427 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN),
428 "System has not been booted with systemd as init system (PID 1). Can't operate.");
429 r = sd_bus_default_system(&bus);
430 break;
431
432 default:
433 assert_not_reached();
434 }
435 break;
436
437 case BUS_TRANSPORT_REMOTE:
438 assert_return(runtime_scope == RUNTIME_SCOPE_SYSTEM, -EOPNOTSUPP);
439
440 r = sd_bus_open_system_remote(&bus, host);
441 break;
442
443 case BUS_TRANSPORT_MACHINE:
444 switch (runtime_scope) {
445
446 case RUNTIME_SCOPE_USER:
447 r = sd_bus_open_user_machine(&bus, host);
448 break;
449
450 case RUNTIME_SCOPE_SYSTEM:
451 r = sd_bus_open_system_machine(&bus, host);
452 break;
453
454 default:
455 assert_not_reached();
456 }
457
458 break;
459
460 case BUS_TRANSPORT_CAPSULE:
461 assert_return(runtime_scope == RUNTIME_SCOPE_USER, -EINVAL);
462
463 r = bus_connect_capsule_bus(host, &bus);
464 break;
465
466 default:
467 assert_not_reached();
468 }
469 if (r < 0)
470 return r;
471
472 r = sd_bus_set_exit_on_disconnect(bus, true);
473 if (r < 0)
474 return r;
475
476 *ret = TAKE_PTR(bus);
477 return 0;
478 }
479
480 int bus_connect_transport_systemd(
481 BusTransport transport,
482 const char *host,
483 RuntimeScope runtime_scope,
484 sd_bus **ret_bus) {
485
486 assert(transport >= 0);
487 assert(transport < _BUS_TRANSPORT_MAX);
488 assert(ret_bus);
489
490 switch (transport) {
491
492 case BUS_TRANSPORT_LOCAL:
493 assert_return(!host, -EINVAL);
494
495 switch (runtime_scope) {
496
497 case RUNTIME_SCOPE_USER:
498 return bus_connect_user_systemd(ret_bus);
499
500 case RUNTIME_SCOPE_SYSTEM:
501 if (sd_booted() <= 0)
502 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
503 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN),
504 "System has not been booted with systemd as init system (PID 1). Can't operate.");
505 return bus_connect_system_systemd(ret_bus);
506
507 default:
508 assert_not_reached();
509 }
510
511 break;
512
513 case BUS_TRANSPORT_REMOTE:
514 assert_return(runtime_scope == RUNTIME_SCOPE_SYSTEM, -EOPNOTSUPP);
515 return sd_bus_open_system_remote(ret_bus, host);
516
517 case BUS_TRANSPORT_MACHINE:
518 assert_return(runtime_scope == RUNTIME_SCOPE_SYSTEM, -EOPNOTSUPP);
519 return sd_bus_open_system_machine(ret_bus, host);
520
521 case BUS_TRANSPORT_CAPSULE:
522 assert_return(runtime_scope == RUNTIME_SCOPE_USER, -EINVAL);
523 return bus_connect_capsule_systemd(host, ret_bus);
524
525 default:
526 assert_not_reached();
527 }
528 }
529
530 /**
531 * bus_path_encode_unique() - encode unique object path
532 * @b: bus connection or NULL
533 * @prefix: object path prefix
534 * @sender_id: unique-name of client, or NULL
535 * @external_id: external ID to be chosen by client, or NULL
536 * @ret_path: storage for encoded object path pointer
537 *
538 * Whenever we provide a bus API that allows clients to create and manage
539 * server-side objects, we need to provide a unique name for these objects. If
540 * we let the server choose the name, we suffer from a race condition: If a
541 * client creates an object asynchronously, it cannot destroy that object until
542 * it received the method reply. It cannot know the name of the new object,
543 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
544 *
545 * Therefore, many APIs allow the client to choose the unique name for newly
546 * created objects. There're two problems to solve, though:
547 * 1) Object names are usually defined via dbus object paths, which are
548 * usually globally namespaced. Therefore, multiple clients must be able
549 * to choose unique object names without interference.
550 * 2) If multiple libraries share the same bus connection, they must be
551 * able to choose unique object names without interference.
552 * The first problem is solved easily by prefixing a name with the
553 * unique-bus-name of a connection. The server side must enforce this and
554 * reject any other name. The second problem is solved by providing unique
555 * suffixes from within sd-bus.
556 *
557 * This helper allows clients to create unique object-paths. It uses the
558 * template '/prefix/sender_id/external_id' and returns the new path in
559 * @ret_path (must be freed by the caller).
560 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
561 * NULL, this function allocates a unique suffix via @b (by requesting a new
562 * cookie). If both @sender_id and @external_id are given, @b can be passed as
563 * NULL.
564 *
565 * Returns: 0 on success, negative error code on failure.
566 */
567 int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) {
568 _cleanup_free_ char *sender_label = NULL, *external_label = NULL;
569 char external_buf[DECIMAL_STR_MAX(uint64_t)], *p;
570 int r;
571
572 assert_return(b || (sender_id && external_id), -EINVAL);
573 assert_return(sd_bus_object_path_is_valid(prefix), -EINVAL);
574 assert_return(ret_path, -EINVAL);
575
576 if (!sender_id) {
577 r = sd_bus_get_unique_name(b, &sender_id);
578 if (r < 0)
579 return r;
580 }
581
582 if (!external_id) {
583 xsprintf(external_buf, "%"PRIu64, ++b->cookie);
584 external_id = external_buf;
585 }
586
587 sender_label = bus_label_escape(sender_id);
588 if (!sender_label)
589 return -ENOMEM;
590
591 external_label = bus_label_escape(external_id);
592 if (!external_label)
593 return -ENOMEM;
594
595 p = path_join(prefix, sender_label, external_label);
596 if (!p)
597 return -ENOMEM;
598
599 *ret_path = p;
600 return 0;
601 }
602
603 /**
604 * bus_path_decode_unique() - decode unique object path
605 * @path: object path to decode
606 * @prefix: object path prefix
607 * @ret_sender: output parameter for sender-id label
608 * @ret_external: output parameter for external-id label
609 *
610 * This does the reverse of bus_path_encode_unique() (see its description for
611 * details). Both trailing labels, sender-id and external-id, are unescaped and
612 * returned in the given output parameters (the caller must free them).
613 *
614 * Note that this function returns 0 if the path does not match the template
615 * (see bus_path_encode_unique()), 1 if it matched.
616 *
617 * Returns: Negative error code on failure, 0 if the given object path does not
618 * match the template (return parameters are set to NULL), 1 if it was
619 * parsed successfully (return parameters contain allocated labels).
620 */
621 int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) {
622 const char *p, *q;
623 char *sender, *external;
624
625 assert(sd_bus_object_path_is_valid(path));
626 assert(sd_bus_object_path_is_valid(prefix));
627 assert(ret_sender);
628 assert(ret_external);
629
630 p = object_path_startswith(path, prefix);
631 if (!p) {
632 *ret_sender = NULL;
633 *ret_external = NULL;
634 return 0;
635 }
636
637 q = strchr(p, '/');
638 if (!q) {
639 *ret_sender = NULL;
640 *ret_external = NULL;
641 return 0;
642 }
643
644 sender = bus_label_unescape_n(p, q - p);
645 external = bus_label_unescape(q + 1);
646 if (!sender || !external) {
647 free(sender);
648 free(external);
649 return -ENOMEM;
650 }
651
652 *ret_sender = sender;
653 *ret_external = external;
654 return 1;
655 }
656
657 int bus_track_add_name_many(sd_bus_track *t, char **l) {
658 int r = 0;
659
660 assert(t);
661
662 /* Continues adding after failure, and returns the first failure. */
663
664 STRV_FOREACH(i, l)
665 RET_GATHER(r, sd_bus_track_add_name(t, *i));
666 return r;
667 }
668
669 int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *description) {
670 _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
671 const char *e;
672 int r;
673
674 assert(ret);
675
676 /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal
677 * turned on. */
678
679 r = sd_bus_new(&bus);
680 if (r < 0)
681 return r;
682
683 if (description) {
684 r = sd_bus_set_description(bus, description);
685 if (r < 0)
686 return r;
687 }
688
689 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
690 if (!e)
691 e = DEFAULT_SYSTEM_BUS_ADDRESS;
692
693 r = sd_bus_set_address(bus, e);
694 if (r < 0)
695 return r;
696
697 r = sd_bus_set_bus_client(bus, true);
698 if (r < 0)
699 return r;
700
701 r = sd_bus_negotiate_creds(bus, true, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS);
702 if (r < 0)
703 return r;
704
705 r = sd_bus_set_watch_bind(bus, true);
706 if (r < 0)
707 return r;
708
709 r = sd_bus_set_connected_signal(bus, true);
710 if (r < 0)
711 return r;
712
713 r = sd_bus_start(bus);
714 if (r < 0)
715 return r;
716
717 *ret = TAKE_PTR(bus);
718
719 return 0;
720 }
721
722 int bus_reply_pair_array(sd_bus_message *m, char **l) {
723 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
724 int r;
725
726 assert(m);
727
728 /* Reply to the specified message with a message containing a dictionary put together from the
729 * specified strv */
730
731 r = sd_bus_message_new_method_return(m, &reply);
732 if (r < 0)
733 return r;
734
735 r = sd_bus_message_open_container(reply, 'a', "{ss}");
736 if (r < 0)
737 return r;
738
739 STRV_FOREACH_PAIR(k, v, l) {
740 r = sd_bus_message_append(reply, "{ss}", *k, *v);
741 if (r < 0)
742 return r;
743 }
744
745 r = sd_bus_message_close_container(reply);
746 if (r < 0)
747 return r;
748
749 return sd_bus_send(NULL, reply, NULL);
750 }
751
752 static int method_dump_memory_state_by_fd(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
753 _cleanup_(memstream_done) MemStream m = {};
754 _cleanup_free_ char *dump = NULL;
755 _cleanup_close_ int fd = -EBADF;
756 size_t dump_size;
757 FILE *f;
758 int r;
759
760 assert(message);
761
762 f = memstream_init(&m);
763 if (!f)
764 return -ENOMEM;
765
766 r = RET_NERRNO(malloc_info(/* options= */ 0, f));
767 if (r < 0)
768 return r;
769
770 r = memstream_finalize(&m, &dump, &dump_size);
771 if (r < 0)
772 return r;
773
774 fd = acquire_data_fd_full(dump, dump_size, /* flags = */ 0);
775 if (fd < 0)
776 return fd;
777
778 r = sd_bus_reply_method_return(message, "h", fd);
779 if (r < 0)
780 return r;
781
782 return 1; /* Stop further processing */
783 }
784
785 /* The default install callback will fail and disconnect the bus if it cannot register the match, but this
786 * is only a debug method, we definitely don't want to fail in case there's some permission issue. */
787 static int dummy_install_callback(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
788 return 1;
789 }
790
791 int bus_register_malloc_status(sd_bus *bus, const char *destination) {
792 const char *match;
793 int r;
794
795 assert(bus);
796 assert(!isempty(destination));
797
798 match = strjoina("type='method_call',"
799 "interface='org.freedesktop.MemoryAllocation1',"
800 "path='/org/freedesktop/MemoryAllocation1',"
801 "destination='", destination, "',",
802 "member='GetMallocInfo'");
803
804 r = sd_bus_add_match_async(bus, NULL, match, method_dump_memory_state_by_fd, dummy_install_callback, NULL);
805 if (r < 0)
806 return log_debug_errno(r, "Failed to subscribe to GetMallocInfo() calls on MemoryAllocation1 interface: %m");
807
808 return 0;
809 }
810
811 static void bus_message_unref_wrapper(void *m) {
812 sd_bus_message_unref(m);
813 }
814
815 const struct hash_ops bus_message_hash_ops = {
816 .hash = trivial_hash_func,
817 .compare = trivial_compare_func,
818 .free_value = bus_message_unref_wrapper,
819 };
820
821 int bus_message_append_string_set(sd_bus_message *m, Set *set) {
822 const char *s;
823 int r;
824
825 assert(m);
826
827 r = sd_bus_message_open_container(m, 'a', "s");
828 if (r < 0)
829 return r;
830
831 SET_FOREACH(s, set) {
832 r = sd_bus_message_append(m, "s", s);
833 if (r < 0)
834 return r;
835 }
836
837 return sd_bus_message_close_container(m);
838 }
839
840 int bus_property_get_string_set(
841 sd_bus *bus,
842 const char *path,
843 const char *interface,
844 const char *property,
845 sd_bus_message *reply,
846 void *userdata,
847 sd_bus_error *error) {
848
849 Set **s = ASSERT_PTR(userdata);
850
851 assert(bus);
852 assert(property);
853 assert(reply);
854
855 return bus_message_append_string_set(reply, *s);
856 }
857
858 int bus_creds_get_pidref(
859 sd_bus_creds *c,
860 PidRef *ret) {
861
862 int pidfd = -EBADF;
863 pid_t pid;
864 int r;
865
866 assert(c);
867 assert(ret);
868
869 r = sd_bus_creds_get_pid(c, &pid);
870 if (r < 0)
871 return r;
872
873 r = sd_bus_creds_get_pidfd_dup(c, &pidfd);
874 if (r < 0 && r != -ENODATA)
875 return r;
876
877 *ret = (PidRef) {
878 .pid = pid,
879 .fd = pidfd,
880 };
881
882 return 0;
883 }
884
885 int bus_query_sender_pidref(
886 sd_bus_message *m,
887 PidRef *ret) {
888
889 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
890 int r;
891
892 assert(m);
893 assert(ret);
894
895 r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_PID|SD_BUS_CREDS_PIDFD, &creds);
896 if (r < 0)
897 return r;
898
899 return bus_creds_get_pidref(creds, ret);
900 }
901
902 int bus_message_read_id128(sd_bus_message *m, sd_id128_t *ret) {
903 const void *a;
904 size_t sz;
905 int r;
906
907 assert(m);
908
909 r = sd_bus_message_read_array(m, 'y', &a, &sz);
910 if (r < 0)
911 return r;
912
913 switch (sz) {
914 case 0:
915 if (ret)
916 *ret = SD_ID128_NULL;
917 return 0;
918
919 case sizeof(sd_id128_t):
920 if (ret)
921 memcpy(ret, a, sz);
922 return !memeqzero(a, sz); /* This intends to sd_id128_is_null(), but ret may be NULL, so
923 * let'suse memeqzero() here. */
924
925 default:
926 return -EINVAL;
927 }
928 }