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
;
111 PortableFlags flags
= 0;
115 assert(name_or_path
|| image
);
123 if (sd_bus_message_is_method_call(message
, NULL
, "GetImageMetadataWithExtensions") ||
124 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
);
134 if (sd_bus_message_is_method_call(message
, NULL
, "GetImageMetadataWithExtensions") ||
135 sd_bus_message_is_method_call(message
, NULL
, "GetMetadataWithExtensions")) {
136 uint64_t input_flags
= 0;
138 r
= sd_bus_message_read(message
, "t", &input_flags
);
142 if ((input_flags
& ~_PORTABLE_MASK_PUBLIC
) != 0)
143 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
144 "Invalid 'flags' parameter '%" PRIu64
"'",
146 flags
|= input_flags
;
149 r
= bus_image_acquire(m
,
153 BUS_IMAGE_AUTHENTICATE_BY_PATH
,
154 "org.freedesktop.portable1.inspect-images",
159 if (r
== 0) /* Will call us back */
162 r
= portable_extract(
174 r
= portable_metadata_hashmap_to_sorted_array(unit_files
, &sorted
);
178 r
= sd_bus_message_new_method_return(message
, &reply
);
182 r
= sd_bus_message_append(reply
, "s", image
->path
);
186 r
= append_fd(reply
, os_release
);
190 r
= sd_bus_message_open_container(reply
, 'a', "{say}");
194 /* If it was requested, also send back the extension path and the content
195 * of each extension-release file. Behind a flag, as it's an incompatible
197 if (FLAGS_SET(flags
, PORTABLE_INSPECT_EXTENSION_RELEASES
)) {
198 PortableMetadata
*extension_release
;
200 ORDERED_HASHMAP_FOREACH(extension_release
, extension_releases
) {
202 r
= sd_bus_message_open_container(reply
, 'e', "say");
206 r
= sd_bus_message_append(reply
, "s", extension_release
->image_path
);
210 r
= append_fd(reply
, extension_release
);
214 r
= sd_bus_message_close_container(reply
);
220 for (i
= 0; i
< hashmap_size(unit_files
); i
++) {
222 r
= sd_bus_message_open_container(reply
, 'e', "say");
226 r
= sd_bus_message_append(reply
, "s", sorted
[i
]->name
);
230 r
= append_fd(reply
, sorted
[i
]);
234 r
= sd_bus_message_close_container(reply
);
239 r
= sd_bus_message_close_container(reply
);
243 return sd_bus_send(NULL
, reply
, NULL
);
246 static int bus_image_method_get_metadata(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
247 return bus_image_common_get_metadata(NULL
, message
, NULL
, userdata
, error
);
250 static int bus_image_method_get_state(
251 sd_bus_message
*message
,
253 sd_bus_error
*error
) {
255 _cleanup_strv_free_
char **extension_images
= NULL
;
256 Image
*image
= userdata
;
263 if (sd_bus_message_is_method_call(message
, NULL
, "GetStateWithExtensions")) {
264 uint64_t input_flags
= 0;
266 r
= sd_bus_message_read_strv(message
, &extension_images
);
270 r
= sd_bus_message_read(message
, "t", &input_flags
);
274 /* No flags are supported by this method for now. */
275 if (input_flags
!= 0)
276 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
277 "Invalid 'flags' parameter '%" PRIu64
"'",
281 r
= portable_get_state(
282 sd_bus_message_get_bus(message
),
291 return sd_bus_reply_method_return(message
, "s", portable_state_to_string(state
));
294 int bus_image_common_attach(
296 sd_bus_message
*message
,
297 const char *name_or_path
,
299 sd_bus_error
*error
) {
301 _cleanup_strv_free_
char **matches
= NULL
, **extension_images
= NULL
;
302 PortableChange
*changes
= NULL
;
303 PortableFlags flags
= 0;
304 const char *profile
, *copy_mode
;
305 size_t n_changes
= 0;
309 assert(name_or_path
|| image
);
316 if (sd_bus_message_is_method_call(message
, NULL
, "AttachImageWithExtensions") ||
317 sd_bus_message_is_method_call(message
, NULL
, "AttachWithExtensions")) {
318 r
= sd_bus_message_read_strv(message
, &extension_images
);
323 r
= sd_bus_message_read_strv(message
, &matches
);
327 r
= sd_bus_message_read(message
, "s", &profile
);
331 if (sd_bus_message_is_method_call(message
, NULL
, "AttachImageWithExtensions") ||
332 sd_bus_message_is_method_call(message
, NULL
, "AttachWithExtensions")) {
333 uint64_t input_flags
= 0;
335 r
= sd_bus_message_read(message
, "st", ©_mode
, &input_flags
);
338 if ((input_flags
& ~_PORTABLE_MASK_PUBLIC
) != 0)
339 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
340 "Invalid 'flags' parameter '%" PRIu64
"'",
342 flags
|= input_flags
;
346 r
= sd_bus_message_read(message
, "bs", &runtime
, ©_mode
);
351 flags
|= PORTABLE_RUNTIME
;
354 if (streq(copy_mode
, "symlink"))
355 flags
|= PORTABLE_PREFER_SYMLINK
;
356 else if (streq(copy_mode
, "copy"))
357 flags
|= PORTABLE_PREFER_COPY
;
358 else if (!isempty(copy_mode
))
359 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown copy mode '%s'", copy_mode
);
361 r
= bus_image_acquire(m
,
365 BUS_IMAGE_AUTHENTICATE_ALL
,
366 "org.freedesktop.portable1.attach-images",
371 if (r
== 0) /* Will call us back */
375 sd_bus_message_get_bus(message
),
387 r
= reply_portable_changes(message
, changes
, n_changes
);
390 portable_changes_free(changes
, n_changes
);
394 static int bus_image_method_attach(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
395 return bus_image_common_attach(NULL
, message
, NULL
, userdata
, error
);
398 static int bus_image_method_detach(
399 sd_bus_message
*message
,
401 sd_bus_error
*error
) {
403 _cleanup_strv_free_
char **extension_images
= NULL
;
404 PortableChange
*changes
= NULL
;
405 Image
*image
= userdata
;
406 Manager
*m
= image
->userdata
;
407 PortableFlags flags
= 0;
408 size_t n_changes
= 0;
415 if (sd_bus_message_is_method_call(message
, NULL
, "DetachWithExtensions")) {
416 r
= sd_bus_message_read_strv(message
, &extension_images
);
421 if (sd_bus_message_is_method_call(message
, NULL
, "DetachWithExtensions")) {
422 uint64_t input_flags
= 0;
424 r
= sd_bus_message_read(message
, "t", &input_flags
);
428 if ((input_flags
& ~_PORTABLE_MASK_PUBLIC
) != 0)
429 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
430 "Invalid 'flags' parameter '%" PRIu64
"'",
432 flags
|= input_flags
;
436 r
= sd_bus_message_read(message
, "b", &runtime
);
441 flags
|= PORTABLE_RUNTIME
;
444 r
= bus_verify_polkit_async(
447 "org.freedesktop.portable1.attach-images",
456 return 1; /* Will call us back */
459 sd_bus_message_get_bus(message
),
469 r
= reply_portable_changes(message
, changes
, n_changes
);
472 portable_changes_free(changes
, n_changes
);
476 int bus_image_common_remove(
478 sd_bus_message
*message
,
479 const char *name_or_path
,
481 sd_bus_error
*error
) {
483 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
484 _cleanup_(sigkill_waitp
) pid_t child
= 0;
489 assert(name_or_path
|| image
);
496 if (m
->n_operations
>= OPERATIONS_MAX
)
497 return sd_bus_error_set(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing operations.");
499 r
= bus_image_acquire(m
,
503 BUS_IMAGE_AUTHENTICATE_ALL
,
504 "org.freedesktop.portable1.manage-images",
510 return 1; /* Will call us back */
512 r
= portable_get_state(
513 sd_bus_message_get_bus(message
),
522 if (state
!= PORTABLE_DETACHED
)
523 return sd_bus_error_set_errnof(error
, EBUSY
, "Image '%s' is not detached, refusing.", image
->path
);
525 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
526 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
528 r
= safe_fork("(sd-imgrm)", FORK_RESET_SIGNALS
, &child
);
530 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
532 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
534 r
= image_remove(image
);
536 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
543 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
545 r
= operation_new(m
, child
, message
, errno_pipe_fd
[0], NULL
);
550 errno_pipe_fd
[0] = -1;
555 static int bus_image_method_remove(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
556 return bus_image_common_remove(NULL
, message
, NULL
, userdata
, error
);
559 /* Given two PortableChange arrays, return a new array that has all elements of the first that are
560 * not also present in the second, comparing the basename of the path values. */
561 static int normalize_portable_changes(
562 const PortableChange
*changes_attached
,
563 size_t n_changes_attached
,
564 const PortableChange
*changes_detached
,
565 size_t n_changes_detached
,
566 PortableChange
**ret_changes
,
567 size_t *ret_n_changes
) {
569 PortableChange
*changes
= NULL
;
570 size_t n_changes
= 0;
573 assert(ret_n_changes
);
576 if (n_changes_detached
== 0)
577 return 0; /* Nothing to do */
579 changes
= new0(PortableChange
, n_changes_attached
+ n_changes_detached
);
583 /* Corner case: only detached, nothing attached */
584 if (n_changes_attached
== 0) {
585 memcpy(changes
, changes_detached
, sizeof(PortableChange
) * n_changes_detached
);
586 *ret_changes
= TAKE_PTR(changes
);
587 *ret_n_changes
= n_changes_detached
;
592 for (size_t i
= 0; i
< n_changes_detached
; ++i
) {
595 for (size_t j
= 0; j
< n_changes_attached
; ++j
)
596 if (streq(basename(changes_detached
[i
].path
), basename(changes_attached
[j
].path
))) {
602 _cleanup_free_
char *path
= NULL
, *source
= NULL
;
604 path
= strdup(changes_detached
[i
].path
);
610 if (changes_detached
[i
].source
) {
611 source
= strdup(changes_detached
[i
].source
);
618 changes
[n_changes
++] = (PortableChange
) {
619 .type_or_errno
= changes_detached
[i
].type_or_errno
,
620 .path
= TAKE_PTR(path
),
621 .source
= TAKE_PTR(source
),
626 *ret_n_changes
= n_changes
;
627 *ret_changes
= TAKE_PTR(changes
);
632 portable_changes_free(changes
, n_changes
);
636 int bus_image_common_reattach(
638 sd_bus_message
*message
,
639 const char *name_or_path
,
641 sd_bus_error
*error
) {
643 PortableChange
*changes_detached
= NULL
, *changes_attached
= NULL
, *changes_gone
= NULL
;
644 size_t n_changes_detached
= 0, n_changes_attached
= 0, n_changes_gone
= 0;
645 _cleanup_strv_free_
char **matches
= NULL
, **extension_images
= NULL
;
646 PortableFlags flags
= PORTABLE_REATTACH
;
647 const char *profile
, *copy_mode
;
651 assert(name_or_path
|| image
);
658 if (sd_bus_message_is_method_call(message
, NULL
, "ReattachImageWithExtensions") ||
659 sd_bus_message_is_method_call(message
, NULL
, "ReattachWithExtensions")) {
660 r
= sd_bus_message_read_strv(message
, &extension_images
);
665 r
= sd_bus_message_read_strv(message
, &matches
);
669 r
= sd_bus_message_read(message
, "s", &profile
);
673 if (sd_bus_message_is_method_call(message
, NULL
, "ReattachImageWithExtensions") ||
674 sd_bus_message_is_method_call(message
, NULL
, "ReattachWithExtensions")) {
675 uint64_t input_flags
= 0;
677 r
= sd_bus_message_read(message
, "st", ©_mode
, &input_flags
);
681 if ((input_flags
& ~_PORTABLE_MASK_PUBLIC
) != 0)
682 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
683 "Invalid 'flags' parameter '%" PRIu64
"'",
685 flags
|= input_flags
;
689 r
= sd_bus_message_read(message
, "bs", &runtime
, ©_mode
);
694 flags
|= PORTABLE_RUNTIME
;
697 if (streq(copy_mode
, "symlink"))
698 flags
|= PORTABLE_PREFER_SYMLINK
;
699 else if (streq(copy_mode
, "copy"))
700 flags
|= PORTABLE_PREFER_COPY
;
701 else if (!isempty(copy_mode
))
702 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown copy mode '%s'", copy_mode
);
704 r
= bus_image_acquire(m
,
708 BUS_IMAGE_AUTHENTICATE_ALL
,
709 "org.freedesktop.portable1.attach-images",
714 if (r
== 0) /* Will call us back */
718 sd_bus_message_get_bus(message
),
729 sd_bus_message_get_bus(message
),
741 /* We want to return the list of units really removed by the detach,
742 * and not added again by the attach */
743 r
= normalize_portable_changes(changes_attached
, n_changes_attached
,
744 changes_detached
, n_changes_detached
,
745 &changes_gone
, &n_changes_gone
);
749 /* First, return the units that are gone (so that the caller can stop them)
750 * Then, return the units that are changed/added (so that the caller can
751 * start/restart/enable them) */
752 r
= reply_portable_changes_pair(message
,
753 changes_gone
, n_changes_gone
,
754 changes_attached
, n_changes_attached
);
759 portable_changes_free(changes_detached
, n_changes_detached
);
760 portable_changes_free(changes_attached
, n_changes_attached
);
761 portable_changes_free(changes_gone
, n_changes_gone
);
765 static int bus_image_method_reattach(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
766 return bus_image_common_reattach(NULL
, message
, NULL
, userdata
, error
);
769 int bus_image_common_mark_read_only(
771 sd_bus_message
*message
,
772 const char *name_or_path
,
774 sd_bus_error
*error
) {
779 assert(name_or_path
|| image
);
786 r
= sd_bus_message_read(message
, "b", &read_only
);
790 r
= bus_image_acquire(m
,
794 BUS_IMAGE_AUTHENTICATE_ALL
,
795 "org.freedesktop.portable1.manage-images",
801 return 1; /* Will call us back */
803 r
= image_read_only(image
, read_only
);
807 return sd_bus_reply_method_return(message
, NULL
);
810 static int bus_image_method_mark_read_only(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
811 return bus_image_common_mark_read_only(NULL
, message
, NULL
, userdata
, error
);
814 int bus_image_common_set_limit(
816 sd_bus_message
*message
,
817 const char *name_or_path
,
819 sd_bus_error
*error
) {
825 assert(name_or_path
|| image
);
832 r
= sd_bus_message_read(message
, "t", &limit
);
835 if (!FILE_SIZE_VALID_OR_INFINITY(limit
))
836 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "New limit out of range");
838 r
= bus_image_acquire(m
,
842 BUS_IMAGE_AUTHENTICATE_ALL
,
843 "org.freedesktop.portable1.manage-images",
849 return 1; /* Will call us back */
851 r
= image_set_limit(image
, limit
);
855 return sd_bus_reply_method_return(message
, NULL
);
858 static int bus_image_method_set_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
859 return bus_image_common_set_limit(NULL
, message
, NULL
, userdata
, error
);
862 const sd_bus_vtable image_vtable
[] = {
863 SD_BUS_VTABLE_START(0),
864 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(Image
, name
), 0),
865 SD_BUS_PROPERTY("Path", "s", NULL
, offsetof(Image
, path
), 0),
866 SD_BUS_PROPERTY("Type", "s", property_get_type
, offsetof(Image
, type
), 0),
867 SD_BUS_PROPERTY("ReadOnly", "b", bus_property_get_bool
, offsetof(Image
, read_only
), 0),
868 SD_BUS_PROPERTY("CreationTimestamp", "t", NULL
, offsetof(Image
, crtime
), 0),
869 SD_BUS_PROPERTY("ModificationTimestamp", "t", NULL
, offsetof(Image
, mtime
), 0),
870 SD_BUS_PROPERTY("Usage", "t", NULL
, offsetof(Image
, usage
), 0),
871 SD_BUS_PROPERTY("Limit", "t", NULL
, offsetof(Image
, limit
), 0),
872 SD_BUS_PROPERTY("UsageExclusive", "t", NULL
, offsetof(Image
, usage_exclusive
), 0),
873 SD_BUS_PROPERTY("LimitExclusive", "t", NULL
, offsetof(Image
, limit_exclusive
), 0),
874 SD_BUS_METHOD_WITH_ARGS("GetOSRelease",
876 SD_BUS_RESULT("a{ss}", os_release
),
877 bus_image_method_get_os_release
,
878 SD_BUS_VTABLE_UNPRIVILEGED
),
879 SD_BUS_METHOD_WITH_ARGS("GetMetadata",
880 SD_BUS_ARGS("as", matches
),
881 SD_BUS_RESULT("s", image
,
884 bus_image_method_get_metadata
,
885 SD_BUS_VTABLE_UNPRIVILEGED
),
886 SD_BUS_METHOD_WITH_ARGS("GetMetadataWithExtensions",
887 SD_BUS_ARGS("as", extensions
,
890 SD_BUS_RESULT("s", image
,
893 bus_image_method_get_metadata
,
894 SD_BUS_VTABLE_UNPRIVILEGED
),
895 SD_BUS_METHOD_WITH_ARGS("GetState",
897 SD_BUS_RESULT("s", state
),
898 bus_image_method_get_state
,
899 SD_BUS_VTABLE_UNPRIVILEGED
),
900 SD_BUS_METHOD_WITH_ARGS("GetStateWithExtensions",
901 SD_BUS_ARGS("as", extensions
,
903 SD_BUS_RESULT("s", state
),
904 bus_image_method_get_state
,
905 SD_BUS_VTABLE_UNPRIVILEGED
),
906 SD_BUS_METHOD_WITH_ARGS("Attach",
907 SD_BUS_ARGS("as", matches
,
911 SD_BUS_RESULT("a(sss)", changes
),
912 bus_image_method_attach
,
913 SD_BUS_VTABLE_UNPRIVILEGED
),
914 SD_BUS_METHOD_WITH_ARGS("AttachWithExtensions",
915 SD_BUS_ARGS("as", extensions
,
920 SD_BUS_RESULT("a(sss)", changes
),
921 bus_image_method_attach
,
922 SD_BUS_VTABLE_UNPRIVILEGED
),
923 SD_BUS_METHOD_WITH_ARGS("Detach",
924 SD_BUS_ARGS("b", runtime
),
925 SD_BUS_RESULT("a(sss)", changes
),
926 bus_image_method_detach
,
927 SD_BUS_VTABLE_UNPRIVILEGED
),
928 SD_BUS_METHOD_WITH_ARGS("DetachWithExtensions",
929 SD_BUS_ARGS("as", extensions
,
931 SD_BUS_RESULT("a(sss)", changes
),
932 bus_image_method_detach
,
933 SD_BUS_VTABLE_UNPRIVILEGED
),
934 SD_BUS_METHOD_WITH_ARGS("Reattach",
935 SD_BUS_ARGS("as", matches
,
939 SD_BUS_RESULT("a(sss)", changes_removed
,
940 "a(sss)", changes_updated
),
941 bus_image_method_reattach
,
942 SD_BUS_VTABLE_UNPRIVILEGED
),
943 SD_BUS_METHOD_WITH_ARGS("ReattacheWithExtensions",
944 SD_BUS_ARGS("as", extensions
,
949 SD_BUS_RESULT("a(sss)", changes_removed
,
950 "a(sss)", changes_updated
),
951 bus_image_method_reattach
,
952 SD_BUS_VTABLE_UNPRIVILEGED
),
953 SD_BUS_METHOD_WITH_ARGS("Remove",
956 bus_image_method_remove
,
957 SD_BUS_VTABLE_UNPRIVILEGED
),
958 SD_BUS_METHOD_WITH_ARGS("MarkReadOnly",
959 SD_BUS_ARGS("b", read_only
),
961 bus_image_method_mark_read_only
,
962 SD_BUS_VTABLE_UNPRIVILEGED
),
963 SD_BUS_METHOD_WITH_ARGS("SetLimit",
964 SD_BUS_ARGS("t", limit
),
966 bus_image_method_set_limit
,
967 SD_BUS_VTABLE_UNPRIVILEGED
),
971 int bus_image_path(Image
*image
, char **ret
) {
975 if (!image
->discoverable
)
978 return sd_bus_path_encode("/org/freedesktop/portable1/image", image
->name
, ret
);
981 int bus_image_acquire(
983 sd_bus_message
*message
,
984 const char *name_or_path
,
986 ImageAcquireMode mode
,
987 const char *polkit_action
,
989 sd_bus_error
*error
) {
991 _cleanup_(image_unrefp
) Image
*loaded
= NULL
;
997 assert(name_or_path
|| image
);
999 assert(mode
< _BUS_IMAGE_ACQUIRE_MODE_MAX
);
1000 assert(polkit_action
|| mode
== BUS_IMAGE_REFUSE_BY_PATH
);
1003 /* Acquires an 'Image' object if not acquired yet, and enforces necessary authentication while doing so. */
1005 if (mode
== BUS_IMAGE_AUTHENTICATE_ALL
) {
1006 r
= bus_verify_polkit_async(
1013 &m
->polkit_registry
,
1017 if (r
== 0) { /* Will call us back */
1023 /* Already passed in? */
1029 /* Let's see if this image is already cached? */
1030 cached
= manager_image_cache_get(m
, name_or_path
);
1036 if (image_name_is_valid(name_or_path
)) {
1038 /* If it's a short name, let's search for it */
1039 r
= image_find(IMAGE_PORTABLE
, name_or_path
, NULL
, &loaded
);
1041 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_PORTABLE_IMAGE
, "No image '%s' found.", name_or_path
);
1043 /* other errors are handled below… */
1045 /* Don't accept path if this is always forbidden */
1046 if (mode
== BUS_IMAGE_REFUSE_BY_PATH
)
1047 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Expected image name, not path in place of '%s'.", name_or_path
);
1049 if (!path_is_absolute(name_or_path
))
1050 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
);
1052 if (!path_is_normalized(name_or_path
))
1053 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image path '%s' is not normalized.", name_or_path
);
1055 if (mode
== BUS_IMAGE_AUTHENTICATE_BY_PATH
) {
1056 r
= bus_verify_polkit_async(
1063 &m
->polkit_registry
,
1067 if (r
== 0) { /* Will call us back */
1073 r
= image_from_path(name_or_path
, &loaded
);
1075 if (r
== -EMEDIUMTYPE
) {
1076 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
);
1082 /* Add what we just loaded to the cache. This has as side-effect that the object stays in memory until the
1083 * cache is purged again, i.e. at least for the current event loop iteration, which is all we need, and which
1084 * means we don't actually need to ref the return object. */
1085 r
= manager_image_cache_add(m
, loaded
);
1093 int bus_image_object_find(
1096 const char *interface
,
1099 sd_bus_error
*error
) {
1101 _cleanup_free_
char *e
= NULL
;
1102 Manager
*m
= userdata
;
1103 Image
*image
= NULL
;
1111 r
= sd_bus_path_decode(path
, "/org/freedesktop/portable1/image", &e
);
1117 r
= bus_image_acquire(m
, sd_bus_get_current_message(bus
), e
, NULL
, BUS_IMAGE_REFUSE_BY_PATH
, NULL
, &image
, error
);
1131 int bus_image_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1132 _cleanup_hashmap_free_ Hashmap
*images
= NULL
;
1133 _cleanup_strv_free_
char **l
= NULL
;
1134 Manager
*m
= userdata
;
1143 images
= hashmap_new(&image_hash_ops
);
1147 r
= manager_image_cache_discover(m
, images
, error
);
1151 HASHMAP_FOREACH(image
, images
) {
1154 r
= bus_image_path(image
, &p
);
1158 if (!GREEDY_REALLOC(l
, n
+2)) {
1167 *nodes
= TAKE_PTR(l
);
1172 const BusObjectImplementation image_object
= {
1173 "/org/freedesktop/portable1/image",
1174 "org.freedesktop.portable1.Image",
1175 .fallback_vtables
= BUS_FALLBACK_VTABLES({image_vtable
, bus_image_object_find
}),
1176 .node_enumerator
= bus_image_node_enumerator
,