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
, &image_policy_service
);
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 r
= fdopen_independent(d
->fd
, "r", &f
);
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;
114 assert(name_or_path
|| image
);
122 bool have_exti
= sd_bus_message_is_method_call(message
, NULL
, "GetImageMetadataWithExtensions") ||
123 sd_bus_message_is_method_call(message
, NULL
, "GetMetadataWithExtensions");
126 r
= sd_bus_message_read_strv(message
, &extension_images
);
131 r
= sd_bus_message_read_strv(message
, &matches
);
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(
166 /* image_policy= */ NULL
,
176 r
= portable_metadata_hashmap_to_sorted_array(unit_files
, &sorted
);
180 r
= sd_bus_message_new_method_return(message
, &reply
);
184 r
= sd_bus_message_append(reply
, "s", image
->path
);
188 r
= append_fd(reply
, os_release
);
192 /* If it was requested, also send back the extension path and the content
193 * of each extension-release file. Behind a flag, as it's an incompatible
196 PortableMetadata
*extension_release
;
198 r
= sd_bus_message_open_container(reply
, 'a', "{say}");
202 ORDERED_HASHMAP_FOREACH(extension_release
, extension_releases
) {
204 r
= sd_bus_message_open_container(reply
, 'e', "say");
208 r
= sd_bus_message_append(reply
, "s", extension_release
->image_path
);
212 r
= append_fd(reply
, extension_release
);
216 r
= sd_bus_message_close_container(reply
);
221 r
= sd_bus_message_close_container(reply
);
226 r
= sd_bus_message_open_container(reply
, 'a', "{say}");
230 for (size_t i
= 0; i
< hashmap_size(unit_files
); i
++) {
232 r
= sd_bus_message_open_container(reply
, 'e', "say");
236 r
= sd_bus_message_append(reply
, "s", sorted
[i
]->name
);
240 r
= append_fd(reply
, sorted
[i
]);
244 r
= sd_bus_message_close_container(reply
);
249 r
= sd_bus_message_close_container(reply
);
253 return sd_bus_send(NULL
, reply
, NULL
);
256 static int bus_image_method_get_metadata(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
257 return bus_image_common_get_metadata(NULL
, message
, NULL
, userdata
, error
);
260 static int bus_image_method_get_state(
261 sd_bus_message
*message
,
263 sd_bus_error
*error
) {
265 _cleanup_strv_free_
char **extension_images
= NULL
;
266 Image
*image
= ASSERT_PTR(userdata
);
272 if (sd_bus_message_is_method_call(message
, NULL
, "GetStateWithExtensions")) {
273 uint64_t input_flags
= 0;
275 r
= sd_bus_message_read_strv(message
, &extension_images
);
279 r
= sd_bus_message_read(message
, "t", &input_flags
);
283 /* No flags are supported by this method for now. */
284 if (input_flags
!= 0)
285 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
286 "Invalid 'flags' parameter '%" PRIu64
"'",
290 r
= portable_get_state(
291 sd_bus_message_get_bus(message
),
300 return sd_bus_reply_method_return(message
, "s", portable_state_to_string(state
));
303 int bus_image_common_attach(
305 sd_bus_message
*message
,
306 const char *name_or_path
,
308 sd_bus_error
*error
) {
310 _cleanup_strv_free_
char **matches
= NULL
, **extension_images
= NULL
;
311 PortableChange
*changes
= NULL
;
312 PortableFlags flags
= 0;
313 const char *profile
, *copy_mode
;
314 size_t n_changes
= 0;
318 assert(name_or_path
|| image
);
320 CLEANUP_ARRAY(changes
, n_changes
, portable_changes_free
);
327 if (sd_bus_message_is_method_call(message
, NULL
, "AttachImageWithExtensions") ||
328 sd_bus_message_is_method_call(message
, NULL
, "AttachWithExtensions")) {
329 r
= sd_bus_message_read_strv(message
, &extension_images
);
334 r
= sd_bus_message_read_strv(message
, &matches
);
338 r
= sd_bus_message_read(message
, "s", &profile
);
342 if (sd_bus_message_is_method_call(message
, NULL
, "AttachImageWithExtensions") ||
343 sd_bus_message_is_method_call(message
, NULL
, "AttachWithExtensions")) {
344 uint64_t input_flags
= 0;
346 r
= sd_bus_message_read(message
, "st", ©_mode
, &input_flags
);
349 if ((input_flags
& ~_PORTABLE_MASK_PUBLIC
) != 0)
350 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
351 "Invalid 'flags' parameter '%" PRIu64
"'",
353 flags
|= input_flags
;
357 r
= sd_bus_message_read(message
, "bs", &runtime
, ©_mode
);
362 flags
|= PORTABLE_RUNTIME
;
365 if (streq(copy_mode
, "symlink"))
366 flags
|= PORTABLE_PREFER_SYMLINK
;
367 else if (streq(copy_mode
, "copy"))
368 flags
|= PORTABLE_PREFER_COPY
;
369 else if (!isempty(copy_mode
))
370 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown copy mode '%s'", copy_mode
);
372 r
= bus_image_acquire(m
,
376 BUS_IMAGE_AUTHENTICATE_ALL
,
377 "org.freedesktop.portable1.attach-images",
382 if (r
== 0) /* Will call us back */
386 sd_bus_message_get_bus(message
),
391 /* image_policy= */ NULL
,
399 return reply_portable_changes(message
, changes
, n_changes
);
402 static int bus_image_method_attach(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
403 return bus_image_common_attach(NULL
, message
, NULL
, userdata
, error
);
406 static int bus_image_method_detach(
407 sd_bus_message
*message
,
409 sd_bus_error
*error
) {
411 _cleanup_strv_free_
char **extension_images
= NULL
;
412 PortableChange
*changes
= NULL
;
413 Image
*image
= ASSERT_PTR(userdata
);
414 Manager
*m
= ASSERT_PTR(image
->userdata
);
415 PortableFlags flags
= 0;
416 size_t n_changes
= 0;
421 CLEANUP_ARRAY(changes
, n_changes
, portable_changes_free
);
423 if (sd_bus_message_is_method_call(message
, NULL
, "DetachWithExtensions")) {
424 r
= sd_bus_message_read_strv(message
, &extension_images
);
429 if (sd_bus_message_is_method_call(message
, NULL
, "DetachWithExtensions")) {
430 uint64_t input_flags
= 0;
432 r
= sd_bus_message_read(message
, "t", &input_flags
);
436 if ((input_flags
& ~_PORTABLE_MASK_PUBLIC
) != 0)
437 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
438 "Invalid 'flags' parameter '%" PRIu64
"'",
440 flags
|= input_flags
;
444 r
= sd_bus_message_read(message
, "b", &runtime
);
449 flags
|= PORTABLE_RUNTIME
;
452 r
= bus_verify_polkit_async(
455 "org.freedesktop.portable1.attach-images",
464 return 1; /* Will call us back */
467 sd_bus_message_get_bus(message
),
477 return reply_portable_changes(message
, changes
, n_changes
);
480 int bus_image_common_remove(
482 sd_bus_message
*message
,
483 const char *name_or_path
,
485 sd_bus_error
*error
) {
487 _cleanup_close_pair_
int errno_pipe_fd
[2] = PIPE_EBADF
;
488 _cleanup_(sigkill_waitp
) pid_t child
= 0;
493 assert(name_or_path
|| image
);
500 if (m
->n_operations
>= OPERATIONS_MAX
)
501 return sd_bus_error_set(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing operations.");
503 r
= bus_image_acquire(m
,
507 BUS_IMAGE_AUTHENTICATE_ALL
,
508 "org.freedesktop.portable1.manage-images",
514 return 1; /* Will call us back */
516 r
= portable_get_state(
517 sd_bus_message_get_bus(message
),
526 if (state
!= PORTABLE_DETACHED
)
527 return sd_bus_error_set_errnof(error
, EBUSY
, "Image '%s' is not detached, refusing.", image
->path
);
529 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
530 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
532 r
= safe_fork("(sd-imgrm)", FORK_RESET_SIGNALS
, &child
);
534 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
536 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
538 r
= image_remove(image
);
540 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
547 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
549 r
= operation_new(m
, child
, message
, errno_pipe_fd
[0], NULL
);
554 errno_pipe_fd
[0] = -EBADF
;
559 static int bus_image_method_remove(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
560 return bus_image_common_remove(NULL
, message
, NULL
, userdata
, error
);
563 /* Given two PortableChange arrays, return a new array that has all elements of the first that are
564 * not also present in the second, comparing the basename of the path values. */
565 static int normalize_portable_changes(
566 const PortableChange
*changes_attached
,
567 size_t n_changes_attached
,
568 const PortableChange
*changes_detached
,
569 size_t n_changes_detached
,
570 PortableChange
**ret_changes
,
571 size_t *ret_n_changes
) {
573 PortableChange
*changes
= NULL
;
574 size_t n_changes
= 0;
576 assert(ret_n_changes
);
579 if (n_changes_detached
== 0)
580 return 0; /* Nothing to do */
582 changes
= new0(PortableChange
, n_changes_attached
+ n_changes_detached
);
586 CLEANUP_ARRAY(changes
, n_changes
, portable_changes_free
);
588 /* Corner case: only detached, nothing attached */
589 if (n_changes_attached
== 0) {
590 memcpy(changes
, changes_detached
, sizeof(PortableChange
) * n_changes_detached
);
591 *ret_changes
= TAKE_PTR(changes
);
592 *ret_n_changes
= n_changes_detached
;
596 for (size_t i
= 0; i
< n_changes_detached
; ++i
) {
599 for (size_t j
= 0; j
< n_changes_attached
; ++j
)
600 if (streq(basename(changes_detached
[i
].path
), basename(changes_attached
[j
].path
))) {
606 _cleanup_free_
char *path
= NULL
, *source
= NULL
;
608 path
= strdup(changes_detached
[i
].path
);
612 if (changes_detached
[i
].source
) {
613 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 int bus_image_common_reattach(
634 sd_bus_message
*message
,
635 const char *name_or_path
,
637 sd_bus_error
*error
) {
639 PortableChange
*changes_detached
= NULL
, *changes_attached
= NULL
, *changes_gone
= NULL
;
640 size_t n_changes_detached
= 0, n_changes_attached
= 0, n_changes_gone
= 0;
641 _cleanup_strv_free_
char **matches
= NULL
, **extension_images
= NULL
;
642 PortableFlags flags
= PORTABLE_REATTACH
;
643 const char *profile
, *copy_mode
;
647 assert(name_or_path
|| image
);
649 CLEANUP_ARRAY(changes_detached
, n_changes_detached
, portable_changes_free
);
650 CLEANUP_ARRAY(changes_attached
, n_changes_attached
, portable_changes_free
);
651 CLEANUP_ARRAY(changes_gone
, n_changes_gone
, portable_changes_free
);
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
),
734 /* image_policy= */ NULL
,
742 /* We want to return the list of units really removed by the detach,
743 * and not added again by the attach */
744 r
= normalize_portable_changes(changes_attached
, n_changes_attached
,
745 changes_detached
, n_changes_detached
,
746 &changes_gone
, &n_changes_gone
);
750 /* First, return the units that are gone (so that the caller can stop them)
751 * Then, return the units that are changed/added (so that the caller can
752 * start/restart/enable them) */
753 return reply_portable_changes_pair(message
,
754 changes_gone
, n_changes_gone
,
755 changes_attached
, n_changes_attached
);
758 static int bus_image_method_reattach(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
759 return bus_image_common_reattach(NULL
, message
, NULL
, userdata
, error
);
762 int bus_image_common_mark_read_only(
764 sd_bus_message
*message
,
765 const char *name_or_path
,
767 sd_bus_error
*error
) {
772 assert(name_or_path
|| image
);
779 r
= sd_bus_message_read(message
, "b", &read_only
);
783 r
= bus_image_acquire(m
,
787 BUS_IMAGE_AUTHENTICATE_ALL
,
788 "org.freedesktop.portable1.manage-images",
794 return 1; /* Will call us back */
796 r
= image_read_only(image
, read_only
);
800 return sd_bus_reply_method_return(message
, NULL
);
803 static int bus_image_method_mark_read_only(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
804 return bus_image_common_mark_read_only(NULL
, message
, NULL
, userdata
, error
);
807 int bus_image_common_set_limit(
809 sd_bus_message
*message
,
810 const char *name_or_path
,
812 sd_bus_error
*error
) {
818 assert(name_or_path
|| image
);
825 r
= sd_bus_message_read(message
, "t", &limit
);
828 if (!FILE_SIZE_VALID_OR_INFINITY(limit
))
829 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "New limit out of range");
831 r
= bus_image_acquire(m
,
835 BUS_IMAGE_AUTHENTICATE_ALL
,
836 "org.freedesktop.portable1.manage-images",
842 return 1; /* Will call us back */
844 r
= image_set_limit(image
, limit
);
848 return sd_bus_reply_method_return(message
, NULL
);
851 static int bus_image_method_set_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
852 return bus_image_common_set_limit(NULL
, message
, NULL
, userdata
, error
);
855 const sd_bus_vtable image_vtable
[] = {
856 SD_BUS_VTABLE_START(0),
857 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(Image
, name
), 0),
858 SD_BUS_PROPERTY("Path", "s", NULL
, offsetof(Image
, path
), 0),
859 SD_BUS_PROPERTY("Type", "s", property_get_type
, offsetof(Image
, type
), 0),
860 SD_BUS_PROPERTY("ReadOnly", "b", bus_property_get_bool
, offsetof(Image
, read_only
), 0),
861 SD_BUS_PROPERTY("CreationTimestamp", "t", NULL
, offsetof(Image
, crtime
), 0),
862 SD_BUS_PROPERTY("ModificationTimestamp", "t", NULL
, offsetof(Image
, mtime
), 0),
863 SD_BUS_PROPERTY("Usage", "t", NULL
, offsetof(Image
, usage
), 0),
864 SD_BUS_PROPERTY("Limit", "t", NULL
, offsetof(Image
, limit
), 0),
865 SD_BUS_PROPERTY("UsageExclusive", "t", NULL
, offsetof(Image
, usage_exclusive
), 0),
866 SD_BUS_PROPERTY("LimitExclusive", "t", NULL
, offsetof(Image
, limit_exclusive
), 0),
867 SD_BUS_METHOD_WITH_ARGS("GetOSRelease",
869 SD_BUS_RESULT("a{ss}", os_release
),
870 bus_image_method_get_os_release
,
871 SD_BUS_VTABLE_UNPRIVILEGED
),
872 SD_BUS_METHOD_WITH_ARGS("GetMetadata",
873 SD_BUS_ARGS("as", matches
),
874 SD_BUS_RESULT("s", image
,
877 bus_image_method_get_metadata
,
878 SD_BUS_VTABLE_UNPRIVILEGED
),
879 SD_BUS_METHOD_WITH_ARGS("GetMetadataWithExtensions",
880 SD_BUS_ARGS("as", extensions
,
883 SD_BUS_RESULT("s", image
,
885 "a{say}", extensions
,
887 bus_image_method_get_metadata
,
888 SD_BUS_VTABLE_UNPRIVILEGED
),
889 SD_BUS_METHOD_WITH_ARGS("GetState",
891 SD_BUS_RESULT("s", state
),
892 bus_image_method_get_state
,
893 SD_BUS_VTABLE_UNPRIVILEGED
),
894 SD_BUS_METHOD_WITH_ARGS("GetStateWithExtensions",
895 SD_BUS_ARGS("as", extensions
,
897 SD_BUS_RESULT("s", state
),
898 bus_image_method_get_state
,
899 SD_BUS_VTABLE_UNPRIVILEGED
),
900 SD_BUS_METHOD_WITH_ARGS("Attach",
901 SD_BUS_ARGS("as", matches
,
905 SD_BUS_RESULT("a(sss)", changes
),
906 bus_image_method_attach
,
907 SD_BUS_VTABLE_UNPRIVILEGED
),
908 SD_BUS_METHOD_WITH_ARGS("AttachWithExtensions",
909 SD_BUS_ARGS("as", extensions
,
914 SD_BUS_RESULT("a(sss)", changes
),
915 bus_image_method_attach
,
916 SD_BUS_VTABLE_UNPRIVILEGED
),
917 SD_BUS_METHOD_WITH_ARGS("Detach",
918 SD_BUS_ARGS("b", runtime
),
919 SD_BUS_RESULT("a(sss)", changes
),
920 bus_image_method_detach
,
921 SD_BUS_VTABLE_UNPRIVILEGED
),
922 SD_BUS_METHOD_WITH_ARGS("DetachWithExtensions",
923 SD_BUS_ARGS("as", extensions
,
925 SD_BUS_RESULT("a(sss)", changes
),
926 bus_image_method_detach
,
927 SD_BUS_VTABLE_UNPRIVILEGED
),
928 SD_BUS_METHOD_WITH_ARGS("Reattach",
929 SD_BUS_ARGS("as", matches
,
933 SD_BUS_RESULT("a(sss)", changes_removed
,
934 "a(sss)", changes_updated
),
935 bus_image_method_reattach
,
936 SD_BUS_VTABLE_UNPRIVILEGED
),
937 SD_BUS_METHOD_WITH_ARGS("ReattacheWithExtensions",
938 SD_BUS_ARGS("as", extensions
,
943 SD_BUS_RESULT("a(sss)", changes_removed
,
944 "a(sss)", changes_updated
),
945 bus_image_method_reattach
,
946 SD_BUS_VTABLE_UNPRIVILEGED
),
947 SD_BUS_METHOD_WITH_ARGS("Remove",
950 bus_image_method_remove
,
951 SD_BUS_VTABLE_UNPRIVILEGED
),
952 SD_BUS_METHOD_WITH_ARGS("MarkReadOnly",
953 SD_BUS_ARGS("b", read_only
),
955 bus_image_method_mark_read_only
,
956 SD_BUS_VTABLE_UNPRIVILEGED
),
957 SD_BUS_METHOD_WITH_ARGS("SetLimit",
958 SD_BUS_ARGS("t", limit
),
960 bus_image_method_set_limit
,
961 SD_BUS_VTABLE_UNPRIVILEGED
),
965 int bus_image_path(Image
*image
, char **ret
) {
969 if (!image
->discoverable
)
972 return sd_bus_path_encode("/org/freedesktop/portable1/image", image
->name
, ret
);
975 int bus_image_acquire(
977 sd_bus_message
*message
,
978 const char *name_or_path
,
980 ImageAcquireMode mode
,
981 const char *polkit_action
,
983 sd_bus_error
*error
) {
985 _cleanup_(image_unrefp
) Image
*loaded
= NULL
;
991 assert(name_or_path
|| image
);
993 assert(mode
< _BUS_IMAGE_ACQUIRE_MODE_MAX
);
994 assert(polkit_action
|| mode
== BUS_IMAGE_REFUSE_BY_PATH
);
997 /* Acquires an 'Image' object if not acquired yet, and enforces necessary authentication while doing so. */
999 if (mode
== BUS_IMAGE_AUTHENTICATE_ALL
) {
1000 r
= bus_verify_polkit_async(
1007 &m
->polkit_registry
,
1011 if (r
== 0) { /* Will call us back */
1017 /* Already passed in? */
1023 /* Let's see if this image is already cached? */
1024 cached
= manager_image_cache_get(m
, name_or_path
);
1030 if (image_name_is_valid(name_or_path
)) {
1032 /* If it's a short name, let's search for it */
1033 r
= image_find(IMAGE_PORTABLE
, name_or_path
, NULL
, &loaded
);
1035 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_PORTABLE_IMAGE
,
1036 "No image '%s' found.", name_or_path
);
1038 /* other errors are handled below… */
1040 /* Don't accept path if this is always forbidden */
1041 if (mode
== BUS_IMAGE_REFUSE_BY_PATH
)
1042 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1043 "Expected image name, not path in place of '%s'.", name_or_path
);
1045 if (!path_is_absolute(name_or_path
))
1046 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1047 "Image name '%s' is not valid or not a valid path.", name_or_path
);
1049 if (!path_is_normalized(name_or_path
))
1050 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1051 "Image path '%s' is not normalized.", name_or_path
);
1053 if (mode
== BUS_IMAGE_AUTHENTICATE_BY_PATH
) {
1054 r
= bus_verify_polkit_async(
1061 &m
->polkit_registry
,
1065 if (r
== 0) { /* Will call us back */
1071 r
= image_from_path(name_or_path
, &loaded
);
1073 if (r
== -EMEDIUMTYPE
) {
1074 sd_bus_error_setf(error
, BUS_ERROR_BAD_PORTABLE_IMAGE_TYPE
,
1075 "Type of image '%s' not recognized; supported image types are directories/btrfs subvolumes, block devices, and raw disk image files with suffix '.raw'.",
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 /* The path is "/org/freedesktop/portable1/image" itself */
1120 r
= bus_image_acquire(m
, sd_bus_get_current_message(bus
), e
, NULL
, BUS_IMAGE_REFUSE_BY_PATH
, NULL
, &image
, error
);
1134 int bus_image_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1135 _cleanup_hashmap_free_ Hashmap
*images
= NULL
;
1136 _cleanup_strv_free_
char **l
= NULL
;
1137 Manager
*m
= userdata
;
1146 images
= hashmap_new(&image_hash_ops
);
1150 r
= manager_image_cache_discover(m
, images
, error
);
1154 HASHMAP_FOREACH(image
, images
) {
1157 r
= bus_image_path(image
, &p
);
1161 if (!GREEDY_REALLOC(l
, n
+2)) {
1170 *nodes
= TAKE_PTR(l
);
1175 const BusObjectImplementation image_object
= {
1176 "/org/freedesktop/portable1/image",
1177 "org.freedesktop.portable1.Image",
1178 .fallback_vtables
= BUS_FALLBACK_VTABLES({image_vtable
, bus_image_object_find
}),
1179 .node_enumerator
= bus_image_node_enumerator
,