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;
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(
175 r
= portable_metadata_hashmap_to_sorted_array(unit_files
, &sorted
);
179 r
= sd_bus_message_new_method_return(message
, &reply
);
183 r
= sd_bus_message_append(reply
, "s", image
->path
);
187 r
= append_fd(reply
, os_release
);
191 /* If it was requested, also send back the extension path and the content
192 * of each extension-release file. Behind a flag, as it's an incompatible
195 PortableMetadata
*extension_release
;
197 r
= sd_bus_message_open_container(reply
, 'a', "{say}");
201 ORDERED_HASHMAP_FOREACH(extension_release
, extension_releases
) {
203 r
= sd_bus_message_open_container(reply
, 'e', "say");
207 r
= sd_bus_message_append(reply
, "s", extension_release
->image_path
);
211 r
= append_fd(reply
, extension_release
);
215 r
= sd_bus_message_close_container(reply
);
220 r
= sd_bus_message_close_container(reply
);
225 r
= sd_bus_message_open_container(reply
, 'a', "{say}");
229 for (size_t i
= 0; i
< hashmap_size(unit_files
); i
++) {
231 r
= sd_bus_message_open_container(reply
, 'e', "say");
235 r
= sd_bus_message_append(reply
, "s", sorted
[i
]->name
);
239 r
= append_fd(reply
, sorted
[i
]);
243 r
= sd_bus_message_close_container(reply
);
248 r
= sd_bus_message_close_container(reply
);
252 return sd_bus_send(NULL
, reply
, NULL
);
255 static int bus_image_method_get_metadata(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
256 return bus_image_common_get_metadata(NULL
, message
, NULL
, userdata
, error
);
259 static int bus_image_method_get_state(
260 sd_bus_message
*message
,
262 sd_bus_error
*error
) {
264 _cleanup_strv_free_
char **extension_images
= NULL
;
265 Image
*image
= ASSERT_PTR(userdata
);
271 if (sd_bus_message_is_method_call(message
, NULL
, "GetStateWithExtensions")) {
272 uint64_t input_flags
= 0;
274 r
= sd_bus_message_read_strv(message
, &extension_images
);
278 r
= sd_bus_message_read(message
, "t", &input_flags
);
282 /* No flags are supported by this method for now. */
283 if (input_flags
!= 0)
284 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
285 "Invalid 'flags' parameter '%" PRIu64
"'",
289 r
= portable_get_state(
290 sd_bus_message_get_bus(message
),
299 return sd_bus_reply_method_return(message
, "s", portable_state_to_string(state
));
302 int bus_image_common_attach(
304 sd_bus_message
*message
,
305 const char *name_or_path
,
307 sd_bus_error
*error
) {
309 _cleanup_strv_free_
char **matches
= NULL
, **extension_images
= NULL
;
310 PortableChange
*changes
= NULL
;
311 PortableFlags flags
= 0;
312 const char *profile
, *copy_mode
;
313 size_t n_changes
= 0;
317 assert(name_or_path
|| image
);
324 if (sd_bus_message_is_method_call(message
, NULL
, "AttachImageWithExtensions") ||
325 sd_bus_message_is_method_call(message
, NULL
, "AttachWithExtensions")) {
326 r
= sd_bus_message_read_strv(message
, &extension_images
);
331 r
= sd_bus_message_read_strv(message
, &matches
);
335 r
= sd_bus_message_read(message
, "s", &profile
);
339 if (sd_bus_message_is_method_call(message
, NULL
, "AttachImageWithExtensions") ||
340 sd_bus_message_is_method_call(message
, NULL
, "AttachWithExtensions")) {
341 uint64_t input_flags
= 0;
343 r
= sd_bus_message_read(message
, "st", ©_mode
, &input_flags
);
346 if ((input_flags
& ~_PORTABLE_MASK_PUBLIC
) != 0)
347 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
,
348 "Invalid 'flags' parameter '%" PRIu64
"'",
350 flags
|= input_flags
;
354 r
= sd_bus_message_read(message
, "bs", &runtime
, ©_mode
);
359 flags
|= PORTABLE_RUNTIME
;
362 if (streq(copy_mode
, "symlink"))
363 flags
|= PORTABLE_PREFER_SYMLINK
;
364 else if (streq(copy_mode
, "copy"))
365 flags
|= PORTABLE_PREFER_COPY
;
366 else if (!isempty(copy_mode
))
367 return sd_bus_reply_method_errorf(message
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown copy mode '%s'", copy_mode
);
369 r
= bus_image_acquire(m
,
373 BUS_IMAGE_AUTHENTICATE_ALL
,
374 "org.freedesktop.portable1.attach-images",
379 if (r
== 0) /* Will call us back */
383 sd_bus_message_get_bus(message
),
395 r
= reply_portable_changes(message
, changes
, n_changes
);
398 portable_changes_free(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 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
,
1049 "No image '%s' found.", name_or_path
);
1051 /* other errors are handled below… */
1053 /* Don't accept path if this is always forbidden */
1054 if (mode
== BUS_IMAGE_REFUSE_BY_PATH
)
1055 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1056 "Expected image name, not path in place of '%s'.", name_or_path
);
1058 if (!path_is_absolute(name_or_path
))
1059 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1060 "Image name '%s' is not valid or not a valid path.", name_or_path
);
1062 if (!path_is_normalized(name_or_path
))
1063 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1064 "Image path '%s' is not normalized.", name_or_path
);
1066 if (mode
== BUS_IMAGE_AUTHENTICATE_BY_PATH
) {
1067 r
= bus_verify_polkit_async(
1074 &m
->polkit_registry
,
1078 if (r
== 0) { /* Will call us back */
1084 r
= image_from_path(name_or_path
, &loaded
);
1086 if (r
== -EMEDIUMTYPE
) {
1087 sd_bus_error_setf(error
, BUS_ERROR_BAD_PORTABLE_IMAGE_TYPE
,
1088 "Type of image '%s' not recognized; supported image types are directories/btrfs subvolumes, block devices, and raw disk image files with suffix '.raw'.",
1095 /* Add what we just loaded to the cache. This has as side-effect that the object stays in memory until the
1096 * cache is purged again, i.e. at least for the current event loop iteration, which is all we need, and which
1097 * means we don't actually need to ref the return object. */
1098 r
= manager_image_cache_add(m
, loaded
);
1106 int bus_image_object_find(
1109 const char *interface
,
1112 sd_bus_error
*error
) {
1114 _cleanup_free_
char *e
= NULL
;
1115 Manager
*m
= userdata
;
1116 Image
*image
= NULL
;
1124 r
= sd_bus_path_decode(path
, "/org/freedesktop/portable1/image", &e
);
1130 /* The path is "/org/freedesktop/portable1/image" itself */
1133 r
= bus_image_acquire(m
, sd_bus_get_current_message(bus
), e
, NULL
, BUS_IMAGE_REFUSE_BY_PATH
, NULL
, &image
, error
);
1147 int bus_image_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1148 _cleanup_hashmap_free_ Hashmap
*images
= NULL
;
1149 _cleanup_strv_free_
char **l
= NULL
;
1150 Manager
*m
= userdata
;
1159 images
= hashmap_new(&image_hash_ops
);
1163 r
= manager_image_cache_discover(m
, images
, error
);
1167 HASHMAP_FOREACH(image
, images
) {
1170 r
= bus_image_path(image
, &p
);
1174 if (!GREEDY_REALLOC(l
, n
+2)) {
1183 *nodes
= TAKE_PTR(l
);
1188 const BusObjectImplementation image_object
= {
1189 "/org/freedesktop/portable1/image",
1190 "org.freedesktop.portable1.Image",
1191 .fallback_vtables
= BUS_FALLBACK_VTABLES({image_vtable
, bus_image_object_find
}),
1192 .node_enumerator
= bus_image_node_enumerator
,