1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "alloc-util.h"
9 #include "bus-common-errors.h"
10 #include "bus-get-properties.h"
11 #include "bus-label.h"
12 #include "bus-object.h"
13 #include "bus-polkit.h"
15 #include "discover-image.h"
19 #include "missing_capability.h"
22 #include "portabled-bus.h"
23 #include "portabled-image-bus.h"
24 #include "portabled-image.h"
25 #include "portabled.h"
26 #include "process-util.h"
28 #include "user-util.h"
30 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type
, image_type
, ImageType
);
32 int bus_image_common_get_os_release(
34 sd_bus_message
*message
,
35 const char *name_or_path
,
37 sd_bus_error
*error
) {
41 assert(name_or_path
|| image
);
49 r
= bus_image_acquire(m
,
53 BUS_IMAGE_AUTHENTICATE_BY_PATH
,
54 "org.freedesktop.portable1.inspect-images",
59 if (r
== 0) /* Will call us back */
62 if (!image
->metadata_valid
) {
63 r
= image_read_metadata(image
);
65 return sd_bus_error_set_errnof(error
, r
, "Failed to read image metadata: %m");
68 return bus_reply_pair_array(message
, image
->os_release
);
71 static int bus_image_method_get_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
72 return bus_image_common_get_os_release(NULL
, message
, NULL
, userdata
, error
);
75 static int append_fd(sd_bus_message
*m
, PortableMetadata
*d
) {
76 _cleanup_fclose_
FILE *f
= NULL
;
77 _cleanup_free_
char *buf
= NULL
;
86 f
= take_fdopen(&d
->fd
, "r");
90 r
= read_full_stream(f
, &buf
, &n
);
95 return sd_bus_message_append_array(m
, 'y', buf
, n
);
98 int bus_image_common_get_metadata(
100 sd_bus_message
*message
,
101 const char *name_or_path
,
103 sd_bus_error
*error
) {
105 _cleanup_ordered_hashmap_free_ OrderedHashmap
*extension_releases
= NULL
;
106 _cleanup_(portable_metadata_unrefp
) PortableMetadata
*os_release
= NULL
;
107 _cleanup_strv_free_
char **matches
= NULL
, **extension_images
= NULL
;
108 _cleanup_hashmap_free_ Hashmap
*unit_files
= NULL
;
109 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
110 _cleanup_free_ PortableMetadata
**sorted
= NULL
;
113 assert(name_or_path
|| image
);
121 bool have_exti
= sd_bus_message_is_method_call(message
, NULL
, "GetImageMetadataWithExtensions") ||
122 sd_bus_message_is_method_call(message
, NULL
, "GetMetadataWithExtensions");
125 r
= sd_bus_message_read_strv(message
, &extension_images
);
130 r
= sd_bus_message_read_strv(message
, &matches
);
135 uint64_t input_flags
= 0;
137 r
= sd_bus_message_read(message
, "t", &input_flags
);
141 if ((input_flags
& ~_PORTABLE_MASK_PUBLIC
) != 0)
142 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
143 "Invalid 'flags' parameter '%" PRIu64
"'",
147 r
= bus_image_acquire(m
,
151 BUS_IMAGE_AUTHENTICATE_BY_PATH
,
152 "org.freedesktop.portable1.inspect-images",
157 if (r
== 0) /* Will call us back */
160 r
= portable_extract(
172 r
= portable_metadata_hashmap_to_sorted_array(unit_files
, &sorted
);
176 r
= sd_bus_message_new_method_return(message
, &reply
);
180 r
= sd_bus_message_append(reply
, "s", image
->path
);
184 r
= append_fd(reply
, os_release
);
188 /* If it was requested, also send back the extension path and the content
189 * of each extension-release file. Behind a flag, as it's an incompatible
192 PortableMetadata
*extension_release
;
194 r
= sd_bus_message_open_container(reply
, 'a', "{say}");
198 ORDERED_HASHMAP_FOREACH(extension_release
, extension_releases
) {
200 r
= sd_bus_message_open_container(reply
, 'e', "say");
204 r
= sd_bus_message_append(reply
, "s", extension_release
->image_path
);
208 r
= append_fd(reply
, extension_release
);
212 r
= sd_bus_message_close_container(reply
);
217 r
= sd_bus_message_close_container(reply
);
222 r
= sd_bus_message_open_container(reply
, 'a', "{say}");
226 for (size_t i
= 0; i
< hashmap_size(unit_files
); i
++) {
228 r
= sd_bus_message_open_container(reply
, 'e', "say");
232 r
= sd_bus_message_append(reply
, "s", sorted
[i
]->name
);
236 r
= append_fd(reply
, sorted
[i
]);
240 r
= sd_bus_message_close_container(reply
);
245 r
= sd_bus_message_close_container(reply
);
249 return sd_bus_send(NULL
, reply
, NULL
);
252 static int bus_image_method_get_metadata(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
253 return bus_image_common_get_metadata(NULL
, message
, NULL
, userdata
, error
);
256 static int bus_image_method_get_state(
257 sd_bus_message
*message
,
259 sd_bus_error
*error
) {
261 _cleanup_strv_free_
char **extension_images
= NULL
;
262 Image
*image
= userdata
;
269 if (sd_bus_message_is_method_call(message
, NULL
, "GetStateWithExtensions")) {
270 uint64_t input_flags
= 0;
272 r
= sd_bus_message_read_strv(message
, &extension_images
);
276 r
= sd_bus_message_read(message
, "t", &input_flags
);
280 /* No flags are supported by this method for now. */
281 if (input_flags
!= 0)
282 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
283 "Invalid 'flags' parameter '%" PRIu64
"'",
287 r
= portable_get_state(
288 sd_bus_message_get_bus(message
),
297 return sd_bus_reply_method_return(message
, "s", portable_state_to_string(state
));
300 int bus_image_common_attach(
302 sd_bus_message
*message
,
303 const char *name_or_path
,
305 sd_bus_error
*error
) {
307 _cleanup_strv_free_
char **matches
= NULL
, **extension_images
= NULL
;
308 PortableChange
*changes
= NULL
;
309 PortableFlags flags
= 0;
310 const char *profile
, *copy_mode
;
311 size_t n_changes
= 0;
315 assert(name_or_path
|| image
);
322 if (sd_bus_message_is_method_call(message
, NULL
, "AttachImageWithExtensions") ||
323 sd_bus_message_is_method_call(message
, NULL
, "AttachWithExtensions")) {
324 r
= sd_bus_message_read_strv(message
, &extension_images
);
329 r
= sd_bus_message_read_strv(message
, &matches
);
333 r
= sd_bus_message_read(message
, "s", &profile
);
337 if (sd_bus_message_is_method_call(message
, NULL
, "AttachImageWithExtensions") ||
338 sd_bus_message_is_method_call(message
, NULL
, "AttachWithExtensions")) {
339 uint64_t input_flags
= 0;
341 r
= sd_bus_message_read(message
, "st", ©_mode
, &input_flags
);
344 if ((input_flags
& ~_PORTABLE_MASK_PUBLIC
) != 0)
345 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
346 "Invalid 'flags' parameter '%" PRIu64
"'",
348 flags
|= input_flags
;
352 r
= sd_bus_message_read(message
, "bs", &runtime
, ©_mode
);
357 flags
|= PORTABLE_RUNTIME
;
360 if (streq(copy_mode
, "symlink"))
361 flags
|= PORTABLE_PREFER_SYMLINK
;
362 else if (streq(copy_mode
, "copy"))
363 flags
|= PORTABLE_PREFER_COPY
;
364 else if (!isempty(copy_mode
))
365 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown copy mode '%s'", copy_mode
);
367 r
= bus_image_acquire(m
,
371 BUS_IMAGE_AUTHENTICATE_ALL
,
372 "org.freedesktop.portable1.attach-images",
377 if (r
== 0) /* Will call us back */
381 sd_bus_message_get_bus(message
),
393 r
= reply_portable_changes(message
, changes
, n_changes
);
396 portable_changes_free(changes
, n_changes
);
400 static int bus_image_method_attach(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
401 return bus_image_common_attach(NULL
, message
, NULL
, userdata
, error
);
404 static int bus_image_method_detach(
405 sd_bus_message
*message
,
407 sd_bus_error
*error
) {
409 _cleanup_strv_free_
char **extension_images
= NULL
;
410 PortableChange
*changes
= NULL
;
411 Image
*image
= userdata
;
412 Manager
*m
= image
->userdata
;
413 PortableFlags flags
= 0;
414 size_t n_changes
= 0;
421 if (sd_bus_message_is_method_call(message
, NULL
, "DetachWithExtensions")) {
422 r
= sd_bus_message_read_strv(message
, &extension_images
);
427 if (sd_bus_message_is_method_call(message
, NULL
, "DetachWithExtensions")) {
428 uint64_t input_flags
= 0;
430 r
= sd_bus_message_read(message
, "t", &input_flags
);
434 if ((input_flags
& ~_PORTABLE_MASK_PUBLIC
) != 0)
435 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
436 "Invalid 'flags' parameter '%" PRIu64
"'",
438 flags
|= input_flags
;
442 r
= sd_bus_message_read(message
, "b", &runtime
);
447 flags
|= PORTABLE_RUNTIME
;
450 r
= bus_verify_polkit_async(
453 "org.freedesktop.portable1.attach-images",
462 return 1; /* Will call us back */
465 sd_bus_message_get_bus(message
),
475 r
= reply_portable_changes(message
, changes
, n_changes
);
478 portable_changes_free(changes
, n_changes
);
482 int bus_image_common_remove(
484 sd_bus_message
*message
,
485 const char *name_or_path
,
487 sd_bus_error
*error
) {
489 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
490 _cleanup_(sigkill_waitp
) pid_t child
= 0;
495 assert(name_or_path
|| image
);
502 if (m
->n_operations
>= OPERATIONS_MAX
)
503 return sd_bus_error_set(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing operations.");
505 r
= bus_image_acquire(m
,
509 BUS_IMAGE_AUTHENTICATE_ALL
,
510 "org.freedesktop.portable1.manage-images",
516 return 1; /* Will call us back */
518 r
= portable_get_state(
519 sd_bus_message_get_bus(message
),
528 if (state
!= PORTABLE_DETACHED
)
529 return sd_bus_error_set_errnof(error
, EBUSY
, "Image '%s' is not detached, refusing.", image
->path
);
531 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
532 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
534 r
= safe_fork("(sd-imgrm)", FORK_RESET_SIGNALS
, &child
);
536 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
538 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
540 r
= image_remove(image
);
542 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
549 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
551 r
= operation_new(m
, child
, message
, errno_pipe_fd
[0], NULL
);
556 errno_pipe_fd
[0] = -1;
561 static int bus_image_method_remove(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
562 return bus_image_common_remove(NULL
, message
, NULL
, userdata
, error
);
565 /* Given two PortableChange arrays, return a new array that has all elements of the first that are
566 * not also present in the second, comparing the basename of the path values. */
567 static int normalize_portable_changes(
568 const PortableChange
*changes_attached
,
569 size_t n_changes_attached
,
570 const PortableChange
*changes_detached
,
571 size_t n_changes_detached
,
572 PortableChange
**ret_changes
,
573 size_t *ret_n_changes
) {
575 PortableChange
*changes
= NULL
;
576 size_t n_changes
= 0;
579 assert(ret_n_changes
);
582 if (n_changes_detached
== 0)
583 return 0; /* Nothing to do */
585 changes
= new0(PortableChange
, n_changes_attached
+ n_changes_detached
);
589 /* Corner case: only detached, nothing attached */
590 if (n_changes_attached
== 0) {
591 memcpy(changes
, changes_detached
, sizeof(PortableChange
) * n_changes_detached
);
592 *ret_changes
= TAKE_PTR(changes
);
593 *ret_n_changes
= n_changes_detached
;
598 for (size_t i
= 0; i
< n_changes_detached
; ++i
) {
601 for (size_t j
= 0; j
< n_changes_attached
; ++j
)
602 if (streq(basename(changes_detached
[i
].path
), basename(changes_attached
[j
].path
))) {
608 _cleanup_free_
char *path
= NULL
, *source
= NULL
;
610 path
= strdup(changes_detached
[i
].path
);
616 if (changes_detached
[i
].source
) {
617 source
= strdup(changes_detached
[i
].source
);
624 changes
[n_changes
++] = (PortableChange
) {
625 .type_or_errno
= changes_detached
[i
].type_or_errno
,
626 .path
= TAKE_PTR(path
),
627 .source
= TAKE_PTR(source
),
632 *ret_n_changes
= n_changes
;
633 *ret_changes
= TAKE_PTR(changes
);
638 portable_changes_free(changes
, n_changes
);
642 int bus_image_common_reattach(
644 sd_bus_message
*message
,
645 const char *name_or_path
,
647 sd_bus_error
*error
) {
649 PortableChange
*changes_detached
= NULL
, *changes_attached
= NULL
, *changes_gone
= NULL
;
650 size_t n_changes_detached
= 0, n_changes_attached
= 0, n_changes_gone
= 0;
651 _cleanup_strv_free_
char **matches
= NULL
, **extension_images
= NULL
;
652 PortableFlags flags
= PORTABLE_REATTACH
;
653 const char *profile
, *copy_mode
;
657 assert(name_or_path
|| image
);
664 if (sd_bus_message_is_method_call(message
, NULL
, "ReattachImageWithExtensions") ||
665 sd_bus_message_is_method_call(message
, NULL
, "ReattachWithExtensions")) {
666 r
= sd_bus_message_read_strv(message
, &extension_images
);
671 r
= sd_bus_message_read_strv(message
, &matches
);
675 r
= sd_bus_message_read(message
, "s", &profile
);
679 if (sd_bus_message_is_method_call(message
, NULL
, "ReattachImageWithExtensions") ||
680 sd_bus_message_is_method_call(message
, NULL
, "ReattachWithExtensions")) {
681 uint64_t input_flags
= 0;
683 r
= sd_bus_message_read(message
, "st", ©_mode
, &input_flags
);
687 if ((input_flags
& ~_PORTABLE_MASK_PUBLIC
) != 0)
688 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
689 "Invalid 'flags' parameter '%" PRIu64
"'",
691 flags
|= input_flags
;
695 r
= sd_bus_message_read(message
, "bs", &runtime
, ©_mode
);
700 flags
|= PORTABLE_RUNTIME
;
703 if (streq(copy_mode
, "symlink"))
704 flags
|= PORTABLE_PREFER_SYMLINK
;
705 else if (streq(copy_mode
, "copy"))
706 flags
|= PORTABLE_PREFER_COPY
;
707 else if (!isempty(copy_mode
))
708 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown copy mode '%s'", copy_mode
);
710 r
= bus_image_acquire(m
,
714 BUS_IMAGE_AUTHENTICATE_ALL
,
715 "org.freedesktop.portable1.attach-images",
720 if (r
== 0) /* Will call us back */
724 sd_bus_message_get_bus(message
),
735 sd_bus_message_get_bus(message
),
747 /* We want to return the list of units really removed by the detach,
748 * and not added again by the attach */
749 r
= normalize_portable_changes(changes_attached
, n_changes_attached
,
750 changes_detached
, n_changes_detached
,
751 &changes_gone
, &n_changes_gone
);
755 /* First, return the units that are gone (so that the caller can stop them)
756 * Then, return the units that are changed/added (so that the caller can
757 * start/restart/enable them) */
758 r
= reply_portable_changes_pair(message
,
759 changes_gone
, n_changes_gone
,
760 changes_attached
, n_changes_attached
);
765 portable_changes_free(changes_detached
, n_changes_detached
);
766 portable_changes_free(changes_attached
, n_changes_attached
);
767 portable_changes_free(changes_gone
, n_changes_gone
);
771 static int bus_image_method_reattach(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
772 return bus_image_common_reattach(NULL
, message
, NULL
, userdata
, error
);
775 int bus_image_common_mark_read_only(
777 sd_bus_message
*message
,
778 const char *name_or_path
,
780 sd_bus_error
*error
) {
785 assert(name_or_path
|| image
);
792 r
= sd_bus_message_read(message
, "b", &read_only
);
796 r
= bus_image_acquire(m
,
800 BUS_IMAGE_AUTHENTICATE_ALL
,
801 "org.freedesktop.portable1.manage-images",
807 return 1; /* Will call us back */
809 r
= image_read_only(image
, read_only
);
813 return sd_bus_reply_method_return(message
, NULL
);
816 static int bus_image_method_mark_read_only(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
817 return bus_image_common_mark_read_only(NULL
, message
, NULL
, userdata
, error
);
820 int bus_image_common_set_limit(
822 sd_bus_message
*message
,
823 const char *name_or_path
,
825 sd_bus_error
*error
) {
831 assert(name_or_path
|| image
);
838 r
= sd_bus_message_read(message
, "t", &limit
);
841 if (!FILE_SIZE_VALID_OR_INFINITY(limit
))
842 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "New limit out of range");
844 r
= bus_image_acquire(m
,
848 BUS_IMAGE_AUTHENTICATE_ALL
,
849 "org.freedesktop.portable1.manage-images",
855 return 1; /* Will call us back */
857 r
= image_set_limit(image
, limit
);
861 return sd_bus_reply_method_return(message
, NULL
);
864 static int bus_image_method_set_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
865 return bus_image_common_set_limit(NULL
, message
, NULL
, userdata
, error
);
868 const sd_bus_vtable image_vtable
[] = {
869 SD_BUS_VTABLE_START(0),
870 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(Image
, name
), 0),
871 SD_BUS_PROPERTY("Path", "s", NULL
, offsetof(Image
, path
), 0),
872 SD_BUS_PROPERTY("Type", "s", property_get_type
, offsetof(Image
, type
), 0),
873 SD_BUS_PROPERTY("ReadOnly", "b", bus_property_get_bool
, offsetof(Image
, read_only
), 0),
874 SD_BUS_PROPERTY("CreationTimestamp", "t", NULL
, offsetof(Image
, crtime
), 0),
875 SD_BUS_PROPERTY("ModificationTimestamp", "t", NULL
, offsetof(Image
, mtime
), 0),
876 SD_BUS_PROPERTY("Usage", "t", NULL
, offsetof(Image
, usage
), 0),
877 SD_BUS_PROPERTY("Limit", "t", NULL
, offsetof(Image
, limit
), 0),
878 SD_BUS_PROPERTY("UsageExclusive", "t", NULL
, offsetof(Image
, usage_exclusive
), 0),
879 SD_BUS_PROPERTY("LimitExclusive", "t", NULL
, offsetof(Image
, limit_exclusive
), 0),
880 SD_BUS_METHOD_WITH_ARGS("GetOSRelease",
882 SD_BUS_RESULT("a{ss}", os_release
),
883 bus_image_method_get_os_release
,
884 SD_BUS_VTABLE_UNPRIVILEGED
),
885 SD_BUS_METHOD_WITH_ARGS("GetMetadata",
886 SD_BUS_ARGS("as", matches
),
887 SD_BUS_RESULT("s", image
,
890 bus_image_method_get_metadata
,
891 SD_BUS_VTABLE_UNPRIVILEGED
),
892 SD_BUS_METHOD_WITH_ARGS("GetMetadataWithExtensions",
893 SD_BUS_ARGS("as", extensions
,
896 SD_BUS_RESULT("s", image
,
898 "a{say}", extensions
,
900 bus_image_method_get_metadata
,
901 SD_BUS_VTABLE_UNPRIVILEGED
),
902 SD_BUS_METHOD_WITH_ARGS("GetState",
904 SD_BUS_RESULT("s", state
),
905 bus_image_method_get_state
,
906 SD_BUS_VTABLE_UNPRIVILEGED
),
907 SD_BUS_METHOD_WITH_ARGS("GetStateWithExtensions",
908 SD_BUS_ARGS("as", extensions
,
910 SD_BUS_RESULT("s", state
),
911 bus_image_method_get_state
,
912 SD_BUS_VTABLE_UNPRIVILEGED
),
913 SD_BUS_METHOD_WITH_ARGS("Attach",
914 SD_BUS_ARGS("as", matches
,
918 SD_BUS_RESULT("a(sss)", changes
),
919 bus_image_method_attach
,
920 SD_BUS_VTABLE_UNPRIVILEGED
),
921 SD_BUS_METHOD_WITH_ARGS("AttachWithExtensions",
922 SD_BUS_ARGS("as", extensions
,
927 SD_BUS_RESULT("a(sss)", changes
),
928 bus_image_method_attach
,
929 SD_BUS_VTABLE_UNPRIVILEGED
),
930 SD_BUS_METHOD_WITH_ARGS("Detach",
931 SD_BUS_ARGS("b", runtime
),
932 SD_BUS_RESULT("a(sss)", changes
),
933 bus_image_method_detach
,
934 SD_BUS_VTABLE_UNPRIVILEGED
),
935 SD_BUS_METHOD_WITH_ARGS("DetachWithExtensions",
936 SD_BUS_ARGS("as", extensions
,
938 SD_BUS_RESULT("a(sss)", changes
),
939 bus_image_method_detach
,
940 SD_BUS_VTABLE_UNPRIVILEGED
),
941 SD_BUS_METHOD_WITH_ARGS("Reattach",
942 SD_BUS_ARGS("as", matches
,
946 SD_BUS_RESULT("a(sss)", changes_removed
,
947 "a(sss)", changes_updated
),
948 bus_image_method_reattach
,
949 SD_BUS_VTABLE_UNPRIVILEGED
),
950 SD_BUS_METHOD_WITH_ARGS("ReattacheWithExtensions",
951 SD_BUS_ARGS("as", extensions
,
956 SD_BUS_RESULT("a(sss)", changes_removed
,
957 "a(sss)", changes_updated
),
958 bus_image_method_reattach
,
959 SD_BUS_VTABLE_UNPRIVILEGED
),
960 SD_BUS_METHOD_WITH_ARGS("Remove",
963 bus_image_method_remove
,
964 SD_BUS_VTABLE_UNPRIVILEGED
),
965 SD_BUS_METHOD_WITH_ARGS("MarkReadOnly",
966 SD_BUS_ARGS("b", read_only
),
968 bus_image_method_mark_read_only
,
969 SD_BUS_VTABLE_UNPRIVILEGED
),
970 SD_BUS_METHOD_WITH_ARGS("SetLimit",
971 SD_BUS_ARGS("t", limit
),
973 bus_image_method_set_limit
,
974 SD_BUS_VTABLE_UNPRIVILEGED
),
978 int bus_image_path(Image
*image
, char **ret
) {
982 if (!image
->discoverable
)
985 return sd_bus_path_encode("/org/freedesktop/portable1/image", image
->name
, ret
);
988 int bus_image_acquire(
990 sd_bus_message
*message
,
991 const char *name_or_path
,
993 ImageAcquireMode mode
,
994 const char *polkit_action
,
996 sd_bus_error
*error
) {
998 _cleanup_(image_unrefp
) Image
*loaded
= NULL
;
1004 assert(name_or_path
|| image
);
1006 assert(mode
< _BUS_IMAGE_ACQUIRE_MODE_MAX
);
1007 assert(polkit_action
|| mode
== BUS_IMAGE_REFUSE_BY_PATH
);
1010 /* Acquires an 'Image' object if not acquired yet, and enforces necessary authentication while doing so. */
1012 if (mode
== BUS_IMAGE_AUTHENTICATE_ALL
) {
1013 r
= bus_verify_polkit_async(
1020 &m
->polkit_registry
,
1024 if (r
== 0) { /* Will call us back */
1030 /* Already passed in? */
1036 /* Let's see if this image is already cached? */
1037 cached
= manager_image_cache_get(m
, name_or_path
);
1043 if (image_name_is_valid(name_or_path
)) {
1045 /* If it's a short name, let's search for it */
1046 r
= image_find(IMAGE_PORTABLE
, name_or_path
, NULL
, &loaded
);
1048 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_PORTABLE_IMAGE
, "No image '%s' found.", name_or_path
);
1050 /* other errors are handled below… */
1052 /* Don't accept path if this is always forbidden */
1053 if (mode
== BUS_IMAGE_REFUSE_BY_PATH
)
1054 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Expected image name, not path in place of '%s'.", name_or_path
);
1056 if (!path_is_absolute(name_or_path
))
1057 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is not valid or not a valid path.", name_or_path
);
1059 if (!path_is_normalized(name_or_path
))
1060 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image path '%s' is not normalized.", name_or_path
);
1062 if (mode
== BUS_IMAGE_AUTHENTICATE_BY_PATH
) {
1063 r
= bus_verify_polkit_async(
1070 &m
->polkit_registry
,
1074 if (r
== 0) { /* Will call us back */
1080 r
= image_from_path(name_or_path
, &loaded
);
1082 if (r
== -EMEDIUMTYPE
) {
1083 sd_bus_error_setf(error
, BUS_ERROR_BAD_PORTABLE_IMAGE_TYPE
, "Typ of image '%s' not recognized; supported image types are directories/btrfs subvolumes, block devices, and raw disk image files with suffix '.raw'.", name_or_path
);
1089 /* Add what we just loaded to the cache. This has as side-effect that the object stays in memory until the
1090 * cache is purged again, i.e. at least for the current event loop iteration, which is all we need, and which
1091 * means we don't actually need to ref the return object. */
1092 r
= manager_image_cache_add(m
, loaded
);
1100 int bus_image_object_find(
1103 const char *interface
,
1106 sd_bus_error
*error
) {
1108 _cleanup_free_
char *e
= NULL
;
1109 Manager
*m
= userdata
;
1110 Image
*image
= NULL
;
1118 r
= sd_bus_path_decode(path
, "/org/freedesktop/portable1/image", &e
);
1124 r
= bus_image_acquire(m
, sd_bus_get_current_message(bus
), e
, NULL
, BUS_IMAGE_REFUSE_BY_PATH
, NULL
, &image
, error
);
1138 int bus_image_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1139 _cleanup_hashmap_free_ Hashmap
*images
= NULL
;
1140 _cleanup_strv_free_
char **l
= NULL
;
1141 Manager
*m
= userdata
;
1150 images
= hashmap_new(&image_hash_ops
);
1154 r
= manager_image_cache_discover(m
, images
, error
);
1158 HASHMAP_FOREACH(image
, images
) {
1161 r
= bus_image_path(image
, &p
);
1165 if (!GREEDY_REALLOC(l
, n
+2)) {
1174 *nodes
= TAKE_PTR(l
);
1179 const BusObjectImplementation image_object
= {
1180 "/org/freedesktop/portable1/image",
1181 "org.freedesktop.portable1.Image",
1182 .fallback_vtables
= BUS_FALLBACK_VTABLES({image_vtable
, bus_image_object_find
}),
1183 .node_enumerator
= bus_image_node_enumerator
,