1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "alloc-util.h"
10 #include "bus-error.h"
11 #include "bus-locator.h"
12 #include "bus-unit-util.h"
13 #include "bus-wait-for-jobs.h"
15 #include "constants.h"
16 #include "dirent-util.h"
20 #include "format-table.h"
22 #include "locale-util.h"
23 #include "main-func.h"
26 #include "parse-argument.h"
27 #include "parse-util.h"
28 #include "path-util.h"
30 #include "pretty-print.h"
31 #include "spawn-polkit-agent.h"
32 #include "string-util.h"
34 #include "terminal-util.h"
37 static PagerFlags arg_pager_flags
= 0;
38 static bool arg_legend
= true;
39 static bool arg_ask_password
= true;
40 static bool arg_quiet
= false;
41 static const char *arg_profile
= "default";
42 static const char* arg_copy_mode
= NULL
;
43 static bool arg_runtime
= false;
44 static bool arg_reload
= true;
45 static bool arg_cat
= false;
46 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
47 static const char *arg_host
= NULL
;
48 static bool arg_enable
= false;
49 static bool arg_now
= false;
50 static bool arg_no_block
= false;
51 static char **arg_extension_images
= NULL
;
52 static bool arg_force
= false;
54 STATIC_DESTRUCTOR_REGISTER(arg_extension_images
, strv_freep
);
56 static bool is_portable_managed(const char *unit
) {
57 return ENDSWITH_SET(unit
, ".service", ".target", ".socket", ".path", ".timer");
60 static int determine_image(const char *image
, bool permit_non_existing
, char **ret
) {
63 /* If the specified name is a valid image name, we pass it as-is to portabled, which will search for it in the
64 * usual search directories. Otherwise we presume it's a path, and will normalize it on the client's side
65 * (among other things, to make the path independent of the client's working directory) before passing it
68 if (image_name_is_valid(image
)) {
71 if (!arg_quiet
&& laccess(image
, F_OK
) >= 0)
72 log_warning("Ambiguous invocation: current working directory contains file matching non-path argument '%s', ignoring. "
73 "Prefix argument with './' to force reference to file in current working directory.", image
);
83 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
84 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
85 "Operations on images by path not supported when connecting to remote systems.");
87 r
= chase(image
, NULL
, CHASE_TRAIL_SLASH
| (permit_non_existing
? CHASE_NONEXISTENT
: 0), ret
, NULL
);
89 return log_error_errno(r
, "Cannot normalize specified image path '%s': %m", image
);
94 static int attach_extensions_to_message(sd_bus_message
*m
, char **extensions
) {
99 if (strv_isempty(extensions
))
102 r
= sd_bus_message_open_container(m
, 'a', "s");
104 return bus_log_create_error(r
);
106 STRV_FOREACH(p
, extensions
) {
107 _cleanup_free_
char *resolved_extension_image
= NULL
;
109 r
= determine_image(*p
, false, &resolved_extension_image
);
113 r
= sd_bus_message_append(m
, "s", resolved_extension_image
);
115 return bus_log_create_error(r
);
118 r
= sd_bus_message_close_container(m
);
120 return bus_log_create_error(r
);
125 static int extract_prefix(const char *path
, char **ret
) {
126 _cleanup_free_
char *name
= NULL
, *bn
= NULL
;
127 const char *underscore
;
131 r
= path_extract_filename(path
, &bn
);
135 underscore
= strchr(bn
, '_');
141 e
= endswith(bn
, ".raw");
148 name
= strndup(bn
, m
);
152 /* A slightly reduced version of what's permitted in unit names. With ':' and '\' are removed, as well as '_'
153 * which we use as delimiter for the second part of the image string, which we ignore for now. */
154 if (!in_charset(name
, DIGITS LETTERS
"-."))
157 if (!filename_is_valid(name
))
160 *ret
= TAKE_PTR(name
);
164 static int determine_matches(const char *image
, char **l
, bool allow_any
, char ***ret
) {
165 _cleanup_strv_free_
char **k
= NULL
;
168 /* Determine the matches to apply. If the list is empty we derive the match from the image name. If the list
169 * contains exactly the "-" we return a wildcard list (which is the empty list), but only if this is expressly
172 if (strv_isempty(l
)) {
175 r
= extract_prefix(image
, &prefix
);
177 return log_error_errno(r
, "Failed to extract prefix of image name '%s': %m", image
);
180 log_info("(Matching unit files with prefix '%s'.)", prefix
);
182 r
= strv_consume(&k
, prefix
);
186 } else if (strv_equal(l
, STRV_MAKE("-"))) {
189 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
190 "Refusing all unit file match.");
193 log_info("(Matching all unit files.)");
201 _cleanup_free_
char *joined
= NULL
;
203 joined
= strv_join(k
, "', '");
207 log_info("(Matching unit files with prefixes '%s'.)", joined
);
216 static int acquire_bus(sd_bus
**bus
) {
224 r
= bus_connect_transport(arg_transport
, arg_host
, RUNTIME_SCOPE_SYSTEM
, bus
);
226 return bus_log_connect_error(r
, arg_transport
);
228 (void) sd_bus_set_allow_interactive_authorization(*bus
, arg_ask_password
);
233 static int maybe_reload(sd_bus
**bus
) {
234 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
235 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
241 r
= acquire_bus(bus
);
245 r
= sd_bus_message_new_method_call(
248 "org.freedesktop.systemd1",
249 "/org/freedesktop/systemd1",
250 "org.freedesktop.systemd1.Manager",
253 return bus_log_create_error(r
);
255 /* Reloading the daemon may take long, hence set a longer timeout here */
256 r
= sd_bus_call(*bus
, m
, DAEMON_RELOAD_TIMEOUT_SEC
, &error
, NULL
);
258 return log_error_errno(r
, "Failed to reload daemon: %s", bus_error_message(&error
, r
));
263 static int get_image_metadata(sd_bus
*bus
, const char *image
, char **matches
, sd_bus_message
**reply
) {
264 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
265 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
266 uint64_t flags
= arg_force
? PORTABLE_FORCE_SYSEXT
: 0;
273 method
= strv_isempty(arg_extension_images
) ? "GetImageMetadata" : "GetImageMetadataWithExtensions";
275 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, method
);
277 return bus_log_create_error(r
);
279 r
= sd_bus_message_append(m
, "s", image
);
281 return bus_log_create_error(r
);
283 r
= attach_extensions_to_message(m
, arg_extension_images
);
287 r
= sd_bus_message_append_strv(m
, matches
);
289 return bus_log_create_error(r
);
291 if (!strv_isempty(arg_extension_images
)) {
292 r
= sd_bus_message_append(m
, "t", flags
);
294 return bus_log_create_error(r
);
297 r
= sd_bus_call(bus
, m
, 0, &error
, reply
);
299 return log_error_errno(r
, "Failed to inspect image metadata: %s", bus_error_message(&error
, r
));
304 static int inspect_image(int argc
, char *argv
[], void *userdata
) {
305 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
306 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
307 _cleanup_strv_free_
char **matches
= NULL
;
308 _cleanup_free_
char *image
= NULL
;
309 bool nl
= false, header
= false;
315 r
= determine_image(argv
[1], false, &image
);
319 r
= determine_matches(argv
[1], argv
+ 2, true, &matches
);
323 r
= acquire_bus(&bus
);
327 r
= get_image_metadata(bus
, image
, matches
, &reply
);
331 r
= sd_bus_message_read(reply
, "s", &path
);
333 return bus_log_parse_error(r
);
335 r
= sd_bus_message_read_array(reply
, 'y', &data
, &sz
);
337 return bus_log_parse_error(r
);
339 pager_open(arg_pager_flags
);
342 printf("%s-- OS Release: --%s\n", ansi_highlight(), ansi_normal());
343 fwrite(data
, sz
, 1, stdout
);
347 _cleanup_free_
char *pretty_portable
= NULL
, *pretty_os
= NULL
;
348 _cleanup_fclose_
FILE *f
= NULL
;
350 f
= fmemopen_unlocked((void*) data
, sz
, "r");
352 return log_error_errno(errno
, "Failed to open /etc/os-release buffer: %m");
354 r
= parse_env_file(f
, "/etc/os-release",
355 "PORTABLE_PRETTY_NAME", &pretty_portable
,
356 "PRETTY_NAME", &pretty_os
);
358 return log_error_errno(r
, "Failed to parse /etc/os-release: %m");
360 printf("Image:\n\t%s\n"
361 "Portable Service:\n\t%s\n"
362 "Operating System:\n\t%s\n",
364 strna(pretty_portable
),
368 if (!strv_isempty(arg_extension_images
)) {
369 /* If we specified any extensions, we'll first get back exactly the paths (and
370 * extension-release content) for each one of the arguments. */
372 r
= sd_bus_message_enter_container(reply
, 'a', "{say}");
374 return bus_log_parse_error(r
);
376 for (size_t i
= 0; i
< strv_length(arg_extension_images
); ++i
) {
379 r
= sd_bus_message_enter_container(reply
, 'e', "say");
381 return bus_log_parse_error(r
);
385 r
= sd_bus_message_read(reply
, "s", &name
);
387 return bus_log_parse_error(r
);
389 r
= sd_bus_message_read_array(reply
, 'y', &data
, &sz
);
391 return bus_log_parse_error(r
);
397 printf("%s-- Extension Release: %s --%s\n", ansi_highlight(), name
, ansi_normal());
398 fwrite(data
, sz
, 1, stdout
);
402 _cleanup_free_
char *pretty_portable
= NULL
, *pretty_os
= NULL
, *sysext_level
= NULL
,
403 *sysext_id
= NULL
, *sysext_version_id
= NULL
, *sysext_scope
= NULL
, *portable_prefixes
= NULL
,
404 *id
= NULL
, *version_id
= NULL
, *image_id
= NULL
, *image_version
= NULL
, *build_id
= NULL
;
405 _cleanup_fclose_
FILE *f
= NULL
;
407 f
= fmemopen_unlocked((void*) data
, sz
, "r");
409 return log_error_errno(errno
, "Failed to open extension-release buffer: %m");
411 r
= parse_env_file(f
, name
,
412 "SYSEXT_ID", &sysext_id
,
413 "SYSEXT_VERSION_ID", &sysext_version_id
,
414 "SYSEXT_BUILD_ID", &build_id
,
415 "SYSEXT_IMAGE_ID", &image_id
,
416 "SYSEXT_IMAGE_VERSION", &image_version
,
417 "SYSEXT_PRETTY_NAME", &pretty_os
,
418 "SYSEXT_SCOPE", &sysext_scope
,
419 "SYSEXT_LEVEL", &sysext_level
,
421 "VERSION_ID", &version_id
,
422 "PORTABLE_PRETTY_NAME", &pretty_portable
,
423 "PORTABLE_PREFIXES", &portable_prefixes
);
425 return log_error_errno(r
, "Failed to parse extension release from '%s': %m", name
);
427 printf("Extension:\n\t%s\n"
428 "\tExtension Scope:\n\t\t%s\n"
429 "\tExtension Compatibility Level:\n\t\t%s\n"
430 "\tExtension Compatibility OS:\n\t\t%s\n"
431 "\tExtension Compatibility OS Version:\n\t\t%s\n"
432 "\tPortable Service:\n\t\t%s\n"
433 "\tPortable Prefixes:\n\t\t%s\n"
434 "\tExtension Image:\n\t\t%s%s%s %s%s%s\n",
440 strna(pretty_portable
),
441 strna(portable_prefixes
),
443 pretty_os
? " (" : "ID: ",
444 strna(sysext_id
?: image_id
),
445 pretty_os
? "" : "Version: ",
446 strna(sysext_version_id
?: image_version
?: build_id
),
447 pretty_os
? ")" : "");
450 r
= sd_bus_message_exit_container(reply
);
452 return bus_log_parse_error(r
);
455 r
= sd_bus_message_exit_container(reply
);
457 return bus_log_parse_error(r
);
460 r
= sd_bus_message_enter_container(reply
, 'a', "{say}");
462 return bus_log_parse_error(r
);
467 r
= sd_bus_message_enter_container(reply
, 'e', "say");
469 return bus_log_parse_error(r
);
473 r
= sd_bus_message_read(reply
, "s", &name
);
475 return bus_log_parse_error(r
);
477 r
= sd_bus_message_read_array(reply
, 'y', &data
, &sz
);
479 return bus_log_parse_error(r
);
485 printf("%s-- Unit file: %s --%s\n", ansi_highlight(), name
, ansi_normal());
486 fwrite(data
, sz
, 1, stdout
);
491 fputs("Unit files:\n", stdout
);
500 r
= sd_bus_message_exit_container(reply
);
502 return bus_log_parse_error(r
);
505 r
= sd_bus_message_exit_container(reply
);
507 return bus_log_parse_error(r
);
512 static int print_changes(sd_bus_message
*m
) {
518 r
= sd_bus_message_enter_container(m
, 'a', "(sss)");
520 return bus_log_parse_error(r
);
523 const char *type
, *path
, *source
;
525 r
= sd_bus_message_read(m
, "(sss)", &type
, &path
, &source
);
527 return bus_log_parse_error(r
);
531 if (streq(type
, "symlink"))
532 log_info("Created symlink %s %s %s.", path
, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT
), source
);
533 else if (streq(type
, "copy")) {
535 log_info("Copied %s.", path
);
537 log_info("Copied %s %s %s.", source
, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT
), path
);
538 } else if (streq(type
, "unlink"))
539 log_info("Removed %s.", path
);
540 else if (streq(type
, "write"))
541 log_info("Written %s.", path
);
542 else if (streq(type
, "mkdir"))
543 log_info("Created directory %s.", path
);
545 log_error("Unexpected change: %s/%s/%s", type
, path
, source
);
548 r
= sd_bus_message_exit_container(m
);
555 static int maybe_enable_disable(sd_bus
*bus
, const char *path
, bool enable
) {
556 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
557 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
558 _cleanup_strv_free_
char **names
= NULL
;
559 InstallChange
*changes
= NULL
;
560 const uint64_t flags
= UNIT_FILE_PORTABLE
| (arg_runtime
? UNIT_FILE_RUNTIME
: 0);
561 size_t n_changes
= 0;
564 CLEANUP_ARRAY(changes
, n_changes
, install_changes_free
);
569 names
= strv_new(path
, NULL
);
573 r
= sd_bus_message_new_method_call(
576 "org.freedesktop.systemd1",
577 "/org/freedesktop/systemd1",
578 "org.freedesktop.systemd1.Manager",
579 enable
? "EnableUnitFilesWithFlags" : "DisableUnitFilesWithFlags");
581 return bus_log_create_error(r
);
583 r
= sd_bus_message_append_strv(m
, names
);
585 return bus_log_create_error(r
);
587 r
= sd_bus_message_append(m
, "t", flags
);
589 return bus_log_create_error(r
);
591 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
593 return log_error_errno(r
, "Failed to %s the portable service %s: %s",
594 enable
? "enable" : "disable", path
, bus_error_message(&error
, r
));
597 r
= sd_bus_message_skip(reply
, "b");
599 return bus_log_parse_error(r
);
602 (void) bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
607 static int maybe_start_stop_restart(sd_bus
*bus
, const char *path
, const char *method
, BusWaitForJobs
*wait
) {
608 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
609 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
610 _cleanup_free_
char *name
= NULL
;
611 const char *job
= NULL
;
614 assert(STR_IN_SET(method
, "StartUnit", "StopUnit", "RestartUnit"));
619 r
= path_extract_filename(path
, &name
);
621 return log_error_errno(r
, "Failed to extract file name from '%s': %m", path
);
629 "ss", name
, "replace");
631 return log_error_errno(r
, "Failed to call %s on the portable service %s: %s",
634 bus_error_message(&error
, r
));
636 r
= sd_bus_message_read(reply
, "o", &job
);
638 return bus_log_parse_error(r
);
641 log_info("Queued %s to call %s on portable service %s.", job
, method
, name
);
644 r
= bus_wait_for_jobs_add(wait
, job
);
646 return log_error_errno(r
, "Failed to watch %s job to call %s on %s: %m",
653 static int maybe_enable_start(sd_bus
*bus
, sd_bus_message
*reply
) {
654 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*wait
= NULL
;
657 if (!arg_enable
&& !arg_now
)
661 r
= bus_wait_for_jobs_new(bus
, &wait
);
663 return log_error_errno(r
, "Could not watch jobs: %m");
666 r
= sd_bus_message_rewind(reply
, true);
669 r
= sd_bus_message_enter_container(reply
, 'a', "(sss)");
671 return bus_log_parse_error(r
);
674 char *type
, *path
, *source
;
676 r
= sd_bus_message_read(reply
, "(sss)", &type
, &path
, &source
);
678 return bus_log_parse_error(r
);
682 if (STR_IN_SET(type
, "symlink", "copy") && is_portable_managed(path
)) {
683 (void) maybe_enable_disable(bus
, path
, true);
684 (void) maybe_start_stop_restart(bus
, path
, "StartUnit", wait
);
688 r
= sd_bus_message_exit_container(reply
);
693 r
= bus_wait_for_jobs(wait
, arg_quiet
, NULL
);
701 static int maybe_stop_enable_restart(sd_bus
*bus
, sd_bus_message
*reply
) {
702 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*wait
= NULL
;
705 if (!arg_enable
&& !arg_now
)
709 r
= bus_wait_for_jobs_new(bus
, &wait
);
711 return log_error_errno(r
, "Could not watch jobs: %m");
714 r
= sd_bus_message_rewind(reply
, true);
718 /* First we get a list of units that were definitely removed, not just re-attached,
719 * so we can also stop them if the user asked us to. */
720 r
= sd_bus_message_enter_container(reply
, 'a', "(sss)");
722 return bus_log_parse_error(r
);
725 char *type
, *path
, *source
;
727 r
= sd_bus_message_read(reply
, "(sss)", &type
, &path
, &source
);
729 return bus_log_parse_error(r
);
733 if (streq(type
, "unlink") && is_portable_managed(path
))
734 (void) maybe_start_stop_restart(bus
, path
, "StopUnit", wait
);
737 r
= sd_bus_message_exit_container(reply
);
741 /* Then we get a list of units that were either added or changed, so that we can
742 * enable them and/or restart them if the user asked us to. */
743 r
= sd_bus_message_enter_container(reply
, 'a', "(sss)");
745 return bus_log_parse_error(r
);
748 char *type
, *path
, *source
;
750 r
= sd_bus_message_read(reply
, "(sss)", &type
, &path
, &source
);
752 return bus_log_parse_error(r
);
756 if (STR_IN_SET(type
, "symlink", "copy") && is_portable_managed(path
)) {
757 (void) maybe_enable_disable(bus
, path
, true);
758 (void) maybe_start_stop_restart(bus
, path
, "RestartUnit", wait
);
762 r
= sd_bus_message_exit_container(reply
);
767 r
= bus_wait_for_jobs(wait
, arg_quiet
, NULL
);
775 static int maybe_stop_disable(sd_bus
*bus
, char *image
, char *argv
[]) {
776 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*wait
= NULL
;
777 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
778 _cleanup_strv_free_
char **matches
= NULL
;
781 if (!arg_enable
&& !arg_now
)
784 r
= determine_matches(argv
[1], argv
+ 2, true, &matches
);
788 r
= bus_wait_for_jobs_new(bus
, &wait
);
790 return log_error_errno(r
, "Could not watch jobs: %m");
792 r
= get_image_metadata(bus
, image
, matches
, &reply
);
796 r
= sd_bus_message_skip(reply
, "say");
798 return bus_log_parse_error(r
);
800 /* If we specified any extensions, we'll first an array of extension-release metadata. */
801 if (!strv_isempty(arg_extension_images
)) {
802 r
= sd_bus_message_skip(reply
, "a{say}");
804 return bus_log_parse_error(r
);
807 r
= sd_bus_message_enter_container(reply
, 'a', "{say}");
809 return bus_log_parse_error(r
);
814 r
= sd_bus_message_enter_container(reply
, 'e', "say");
816 return bus_log_parse_error(r
);
820 r
= sd_bus_message_read(reply
, "s", &name
);
822 return bus_log_parse_error(r
);
824 r
= sd_bus_message_skip(reply
, "ay");
826 return bus_log_parse_error(r
);
828 r
= sd_bus_message_exit_container(reply
);
830 return bus_log_parse_error(r
);
832 (void) maybe_start_stop_restart(bus
, name
, "StopUnit", wait
);
833 (void) maybe_enable_disable(bus
, name
, false);
836 r
= sd_bus_message_exit_container(reply
);
838 return bus_log_parse_error(r
);
840 /* Stopping must always block or the detach will fail if the unit is still running */
841 r
= bus_wait_for_jobs(wait
, arg_quiet
, NULL
);
848 static int attach_reattach_image(int argc
, char *argv
[], const char *method
) {
849 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
850 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
851 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
852 _cleanup_strv_free_
char **matches
= NULL
;
853 _cleanup_free_
char *image
= NULL
;
857 assert(STR_IN_SET(method
, "AttachImage", "ReattachImage", "AttachImageWithExtensions", "ReattachImageWithExtensions"));
859 r
= determine_image(argv
[1], false, &image
);
863 r
= determine_matches(argv
[1], argv
+ 2, false, &matches
);
867 r
= acquire_bus(&bus
);
871 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
873 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, method
);
875 return bus_log_create_error(r
);
877 r
= sd_bus_message_append(m
, "s", image
);
879 return bus_log_create_error(r
);
881 r
= attach_extensions_to_message(m
, arg_extension_images
);
885 r
= sd_bus_message_append_strv(m
, matches
);
887 return bus_log_create_error(r
);
889 r
= sd_bus_message_append(m
, "s", arg_profile
);
891 return bus_log_create_error(r
);
893 if (STR_IN_SET(method
, "AttachImageWithExtensions", "ReattachImageWithExtensions")) {
894 uint64_t flags
= (arg_runtime
? PORTABLE_RUNTIME
: 0) | (arg_force
? PORTABLE_FORCE_ATTACH
| PORTABLE_FORCE_SYSEXT
: 0);
896 r
= sd_bus_message_append(m
, "st", arg_copy_mode
, flags
);
898 r
= sd_bus_message_append(m
, "bs", arg_runtime
, arg_copy_mode
);
900 return bus_log_create_error(r
);
902 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
904 return log_error_errno(r
, "%s failed: %s", method
, bus_error_message(&error
, r
));
906 (void) maybe_reload(&bus
);
908 print_changes(reply
);
910 if (STR_IN_SET(method
, "AttachImage", "AttachImageWithExtensions"))
911 (void) maybe_enable_start(bus
, reply
);
913 /* ReattachImage returns 2 lists - removed units first, and changed/added second */
914 print_changes(reply
);
915 (void) maybe_stop_enable_restart(bus
, reply
);
921 static int attach_image(int argc
, char *argv
[], void *userdata
) {
922 return attach_reattach_image(argc
, argv
, strv_isempty(arg_extension_images
) ? "AttachImage" : "AttachImageWithExtensions");
925 static int reattach_image(int argc
, char *argv
[], void *userdata
) {
926 return attach_reattach_image(argc
, argv
, strv_isempty(arg_extension_images
) ? "ReattachImage" : "ReattachImageWithExtensions");
929 static int detach_image(int argc
, char *argv
[], void *userdata
) {
930 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
931 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
932 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
933 _cleanup_free_
char *image
= NULL
;
937 r
= determine_image(argv
[1], true, &image
);
941 r
= acquire_bus(&bus
);
945 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
947 (void) maybe_stop_disable(bus
, image
, argv
);
949 method
= strv_isempty(arg_extension_images
) ? "DetachImage" : "DetachImageWithExtensions";
951 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, method
);
953 return bus_log_create_error(r
);
955 r
= sd_bus_message_append(m
, "s", image
);
957 return bus_log_create_error(r
);
959 r
= attach_extensions_to_message(m
, arg_extension_images
);
963 if (strv_isempty(arg_extension_images
))
964 r
= sd_bus_message_append(m
, "b", arg_runtime
);
966 uint64_t flags
= (arg_runtime
? PORTABLE_RUNTIME
: 0) | (arg_force
? PORTABLE_FORCE_ATTACH
| PORTABLE_FORCE_SYSEXT
: 0);
968 r
= sd_bus_message_append(m
, "t", flags
);
971 return bus_log_create_error(r
);
973 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
975 return log_error_errno(r
, "%s failed: %s", method
, bus_error_message(&error
, r
));
977 (void) maybe_reload(&bus
);
979 print_changes(reply
);
983 static int list_images(int argc
, char *argv
[], void *userdata
) {
984 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
985 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
986 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
987 _cleanup_(table_unrefp
) Table
*table
= NULL
;
990 r
= acquire_bus(&bus
);
994 r
= bus_call_method(bus
, bus_portable_mgr
, "ListImages", &error
, &reply
, NULL
);
996 return log_error_errno(r
, "Failed to list images: %s", bus_error_message(&error
, r
));
998 table
= table_new("name", "type", "ro", "crtime", "mtime", "usage", "state");
1002 r
= sd_bus_message_enter_container(reply
, 'a', "(ssbtttso)");
1004 return bus_log_parse_error(r
);
1007 const char *name
, *type
, *state
;
1008 uint64_t crtime
, mtime
, usage
;
1011 r
= sd_bus_message_read(reply
, "(ssbtttso)", &name
, &type
, &ro_int
, &crtime
, &mtime
, &usage
, &state
, NULL
);
1013 return bus_log_parse_error(r
);
1017 r
= table_add_many(table
,
1020 TABLE_BOOLEAN
, ro_int
,
1021 TABLE_SET_COLOR
, ro_int
? ansi_highlight_red() : NULL
,
1022 TABLE_TIMESTAMP
, crtime
,
1023 TABLE_TIMESTAMP
, mtime
,
1025 TABLE_STRING
, state
,
1026 TABLE_SET_COLOR
, !streq(state
, "detached") ? ansi_highlight_green() : NULL
);
1028 return table_log_add_error(r
);
1031 r
= sd_bus_message_exit_container(reply
);
1033 return bus_log_parse_error(r
);
1035 if (table_get_rows(table
) > 1) {
1036 r
= table_set_sort(table
, (size_t) 0);
1038 return table_log_sort_error(r
);
1040 table_set_header(table
, arg_legend
);
1042 r
= table_print(table
, NULL
);
1044 return table_log_print_error(r
);
1048 if (table_get_rows(table
) > 1)
1049 printf("\n%zu images listed.\n", table_get_rows(table
) - 1);
1051 printf("No images.\n");
1057 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1058 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1061 r
= acquire_bus(&bus
);
1065 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1067 for (i
= 1; i
< argc
; i
++) {
1068 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1069 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1071 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, "RemoveImage");
1073 return bus_log_create_error(r
);
1075 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1077 return bus_log_create_error(r
);
1079 /* This is a slow operation, hence turn off any method call timeouts */
1080 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1082 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1088 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1089 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1090 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1094 b
= parse_boolean(argv
[2]);
1096 return log_error_errno(b
, "Failed to parse boolean argument: %s", argv
[2]);
1099 r
= acquire_bus(&bus
);
1103 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1105 r
= bus_call_method(bus
, bus_portable_mgr
, "MarkImageReadOnly", &error
, NULL
, "sb", argv
[1], b
);
1107 return log_error_errno(r
, "Could not mark image read-only: %s", bus_error_message(&error
, r
));
1112 static int set_limit(int argc
, char *argv
[], void *userdata
) {
1113 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1114 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1118 r
= acquire_bus(&bus
);
1122 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1124 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
1127 r
= parse_size(argv
[argc
-1], 1024, &limit
);
1129 return log_error_errno(r
, "Failed to parse size: %s", argv
[argc
-1]);
1133 /* With two arguments changes the quota limit of the specified image */
1134 r
= bus_call_method(bus
, bus_portable_mgr
, "SetImageLimit", &error
, NULL
, "st", argv
[1], limit
);
1136 /* With one argument changes the pool quota limit */
1137 r
= bus_call_method(bus
, bus_portable_mgr
, "SetPoolLimit", &error
, NULL
, "t", limit
);
1140 return log_error_errno(r
, "Could not set limit: %s", bus_error_message(&error
, r
));
1145 static int is_image_attached(int argc
, char *argv
[], void *userdata
) {
1146 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1147 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1148 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1149 _cleanup_free_
char *image
= NULL
;
1150 const char *state
, *method
;
1153 r
= determine_image(argv
[1], true, &image
);
1157 r
= acquire_bus(&bus
);
1161 method
= strv_isempty(arg_extension_images
) ? "GetImageState" : "GetImageStateWithExtensions";
1163 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, method
);
1165 return bus_log_create_error(r
);
1167 r
= sd_bus_message_append(m
, "s", image
);
1169 return bus_log_create_error(r
);
1171 r
= attach_extensions_to_message(m
, arg_extension_images
);
1175 if (!strv_isempty(arg_extension_images
)) {
1176 r
= sd_bus_message_append(m
, "t", 0);
1178 return bus_log_create_error(r
);
1181 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1183 return log_error_errno(r
, "%s failed: %s", method
, bus_error_message(&error
, r
));
1185 r
= sd_bus_message_read(reply
, "s", &state
);
1192 return streq(state
, "detached");
1195 static int dump_profiles(void) {
1196 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1197 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1198 _cleanup_strv_free_
char **l
= NULL
;
1201 r
= acquire_bus(&bus
);
1205 r
= bus_get_property_strv(bus
, bus_portable_mgr
, "Profiles", &error
, &l
);
1207 return log_error_errno(r
, "Failed to acquire list of profiles: %s", bus_error_message(&error
, r
));
1210 log_info("Available unit profiles:");
1212 STRV_FOREACH(i
, l
) {
1214 fputc('\n', stdout
);
1220 static int help(int argc
, char *argv
[], void *userdata
) {
1221 _cleanup_free_
char *link
= NULL
;
1224 pager_open(arg_pager_flags
);
1226 r
= terminal_urlify_man("portablectl", "1", &link
);
1230 printf("%s [OPTIONS...] COMMAND ...\n\n"
1231 "%sAttach or detach portable services from the local system.%s\n"
1233 " list List available portable service images\n"
1234 " attach NAME|PATH [PREFIX...]\n"
1235 " Attach the specified portable service image\n"
1236 " detach NAME|PATH [PREFIX...]\n"
1237 " Detach the specified portable service image\n"
1238 " reattach NAME|PATH [PREFIX...]\n"
1239 " Reattach the specified portable service image\n"
1240 " inspect NAME|PATH [PREFIX...]\n"
1241 " Show details of specified portable service image\n"
1242 " is-attached NAME|PATH Query if portable service image is attached\n"
1243 " read-only NAME|PATH [BOOL] Mark or unmark portable service image read-only\n"
1244 " remove NAME|PATH... Remove a portable service image\n"
1245 " set-limit [NAME|PATH] Set image or pool size limit (disk quota)\n"
1247 " -h --help Show this help\n"
1248 " --version Show package version\n"
1249 " --no-pager Do not pipe output into a pager\n"
1250 " --no-legend Do not show the headers and footers\n"
1251 " --no-ask-password Do not ask for system passwords\n"
1252 " -H --host=[USER@]HOST Operate on remote host\n"
1253 " -M --machine=CONTAINER Operate on local container\n"
1254 " -q --quiet Suppress informational messages\n"
1255 " -p --profile=PROFILE Pick security profile for portable service\n"
1256 " --copy=copy|auto|symlink Prefer copying or symlinks if possible\n"
1257 " --runtime Attach portable service until next reboot only\n"
1258 " --no-reload Don't reload the system and service manager\n"
1259 " --cat When inspecting include unit and os-release file\n"
1261 " --enable Immediately enable/disable the portable service\n"
1262 " after attach/detach\n"
1263 " --now Immediately start/stop the portable service after\n"
1264 " attach/before detach\n"
1265 " --no-block Don't block waiting for attach --now to complete\n"
1266 " --extension=PATH Extend the image with an overlay\n"
1267 " --force Skip 'already active' check when attaching or\n"
1268 " detaching an image (with extensions)\n"
1269 "\nSee the %s for details.\n",
1270 program_invocation_short_name
,
1278 static int parse_argv(int argc
, char *argv
[]) {
1282 ARG_VERSION
= 0x100,
1285 ARG_NO_ASK_PASSWORD
,
1297 static const struct option options
[] = {
1298 { "help", no_argument
, NULL
, 'h' },
1299 { "version", no_argument
, NULL
, ARG_VERSION
},
1300 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1301 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1302 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
1303 { "host", required_argument
, NULL
, 'H' },
1304 { "machine", required_argument
, NULL
, 'M' },
1305 { "quiet", no_argument
, NULL
, 'q' },
1306 { "profile", required_argument
, NULL
, 'p' },
1307 { "copy", required_argument
, NULL
, ARG_COPY
},
1308 { "runtime", no_argument
, NULL
, ARG_RUNTIME
},
1309 { "no-reload", no_argument
, NULL
, ARG_NO_RELOAD
},
1310 { "cat", no_argument
, NULL
, ARG_CAT
},
1311 { "enable", no_argument
, NULL
, ARG_ENABLE
},
1312 { "now", no_argument
, NULL
, ARG_NOW
},
1313 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
1314 { "extension", required_argument
, NULL
, ARG_EXTENSION
},
1315 { "force", no_argument
, NULL
, ARG_FORCE
},
1325 c
= getopt_long(argc
, argv
, "hH:M:qp:", options
, NULL
);
1332 return help(0, NULL
, NULL
);
1338 arg_pager_flags
|= PAGER_DISABLE
;
1345 case ARG_NO_ASK_PASSWORD
:
1346 arg_ask_password
= false;
1350 arg_transport
= BUS_TRANSPORT_REMOTE
;
1355 arg_transport
= BUS_TRANSPORT_MACHINE
;
1364 if (streq(optarg
, "help"))
1365 return dump_profiles();
1367 if (!filename_is_valid(optarg
))
1368 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1369 "Unit profile name not valid: %s", optarg
);
1371 arg_profile
= optarg
;
1375 if (streq(optarg
, "auto"))
1376 arg_copy_mode
= NULL
;
1377 else if (STR_IN_SET(optarg
, "copy", "symlink"))
1378 arg_copy_mode
= optarg
;
1379 else if (streq(optarg
, "help")) {
1385 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1386 "Failed to parse --copy= argument: %s", optarg
);
1411 arg_no_block
= true;
1415 r
= strv_extend(&arg_extension_images
, optarg
);
1428 assert_not_reached();
1435 static int run(int argc
, char *argv
[]) {
1436 static const Verb verbs
[] = {
1437 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1438 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_images
},
1439 { "attach", 2, VERB_ANY
, 0, attach_image
},
1440 { "detach", 2, VERB_ANY
, 0, detach_image
},
1441 { "inspect", 2, VERB_ANY
, 0, inspect_image
},
1442 { "is-attached", 2, 2, 0, is_image_attached
},
1443 { "read-only", 2, 3, 0, read_only_image
},
1444 { "remove", 2, VERB_ANY
, 0, remove_image
},
1445 { "set-limit", 3, 3, 0, set_limit
},
1446 { "reattach", 2, VERB_ANY
, 0, reattach_image
},
1454 r
= parse_argv(argc
, argv
);
1458 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1461 DEFINE_MAIN_FUNCTION(run
);