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;
53 static bool arg_clean
= false;
55 STATIC_DESTRUCTOR_REGISTER(arg_extension_images
, strv_freep
);
57 static bool is_portable_managed(const char *unit
) {
58 return ENDSWITH_SET(unit
, ".service", ".target", ".socket", ".path", ".timer");
61 static int determine_image(const char *image
, bool permit_non_existing
, char **ret
) {
64 /* If the specified name is a valid image name, we pass it as-is to portabled, which will search for it in the
65 * usual search directories. Otherwise we presume it's a path, and will normalize it on the client's side
66 * (among other things, to make the path independent of the client's working directory) before passing it
69 if (image_name_is_valid(image
)) {
72 if (!arg_quiet
&& laccess(image
, F_OK
) >= 0)
73 log_warning("Ambiguous invocation: current working directory contains file matching non-path argument '%s', ignoring. "
74 "Prefix argument with './' to force reference to file in current working directory.", image
);
84 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
85 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
86 "Operations on images by path not supported when connecting to remote systems.");
88 r
= chase(image
, NULL
, CHASE_TRAIL_SLASH
| (permit_non_existing
? CHASE_NONEXISTENT
: 0), ret
, NULL
);
90 return log_error_errno(r
, "Cannot normalize specified image path '%s': %m", image
);
95 static int attach_extensions_to_message(sd_bus_message
*m
, const char *method
, char **extensions
) {
101 /* The new methods also have flags parameters that are independent of the extensions */
102 if (strv_isempty(extensions
) && !endswith(method
, "WithExtensions"))
105 r
= sd_bus_message_open_container(m
, 'a', "s");
107 return bus_log_create_error(r
);
109 STRV_FOREACH(p
, extensions
) {
110 _cleanup_free_
char *resolved_extension_image
= NULL
;
114 startswith_strv(method
, STRV_MAKE("Get", "Detach")),
115 &resolved_extension_image
);
119 r
= sd_bus_message_append(m
, "s", resolved_extension_image
);
121 return bus_log_create_error(r
);
124 r
= sd_bus_message_close_container(m
);
126 return bus_log_create_error(r
);
131 static int extract_prefix(const char *path
, char **ret
) {
132 _cleanup_free_
char *name
= NULL
, *bn
= NULL
;
133 const char *underscore
;
137 r
= path_extract_filename(path
, &bn
);
141 underscore
= strchr(bn
, '_');
147 e
= endswith(bn
, ".raw");
154 name
= strndup(bn
, m
);
158 /* A slightly reduced version of what's permitted in unit names. With ':' and '\' are removed, as well as '_'
159 * which we use as delimiter for the second part of the image string, which we ignore for now. */
160 if (!in_charset(name
, DIGITS LETTERS
"-."))
163 if (!filename_is_valid(name
))
166 *ret
= TAKE_PTR(name
);
170 static int determine_matches(const char *image
, char **l
, bool allow_any
, char ***ret
) {
171 _cleanup_strv_free_
char **k
= NULL
;
174 /* Determine the matches to apply. If the list is empty we derive the match from the image name. If the list
175 * contains exactly the "-" we return a wildcard list (which is the empty list), but only if this is expressly
178 if (strv_isempty(l
)) {
181 r
= extract_prefix(image
, &prefix
);
183 return log_error_errno(r
, "Failed to extract prefix of image name '%s': %m", image
);
186 log_info("(Matching unit files with prefix '%s'.)", prefix
);
188 r
= strv_consume(&k
, prefix
);
192 } else if (strv_equal(l
, STRV_MAKE("-"))) {
195 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
196 "Refusing all unit file match.");
199 log_info("(Matching all unit files.)");
207 _cleanup_free_
char *joined
= NULL
;
209 joined
= strv_join(k
, "', '");
213 log_info("(Matching unit files with prefixes '%s'.)", joined
);
222 static int acquire_bus(sd_bus
**bus
) {
230 r
= bus_connect_transport(arg_transport
, arg_host
, RUNTIME_SCOPE_SYSTEM
, bus
);
232 return bus_log_connect_error(r
, arg_transport
);
234 (void) sd_bus_set_allow_interactive_authorization(*bus
, arg_ask_password
);
239 static int maybe_reload(sd_bus
**bus
) {
245 r
= acquire_bus(bus
);
249 return bus_service_manager_reload(*bus
);
252 static int get_image_metadata(sd_bus
*bus
, const char *image
, char **matches
, sd_bus_message
**reply
) {
253 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
254 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
255 uint64_t flags
= arg_force
? PORTABLE_FORCE_EXTENSION
: 0;
262 method
= strv_isempty(arg_extension_images
) && !arg_force
? "GetImageMetadata" : "GetImageMetadataWithExtensions";
264 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, method
);
266 return bus_log_create_error(r
);
268 r
= sd_bus_message_append(m
, "s", image
);
270 return bus_log_create_error(r
);
272 r
= attach_extensions_to_message(m
, method
, arg_extension_images
);
276 r
= sd_bus_message_append_strv(m
, matches
);
278 return bus_log_create_error(r
);
280 if (streq(method
, "GetImageMetadataWithExtensions")) {
281 r
= sd_bus_message_append(m
, "t", flags
);
283 return bus_log_create_error(r
);
286 r
= sd_bus_call(bus
, m
, 0, &error
, reply
);
288 return log_error_errno(r
, "Failed to inspect image metadata: %s", bus_error_message(&error
, r
));
293 static int inspect_image(int argc
, char *argv
[], void *userdata
) {
294 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
295 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
296 _cleanup_strv_free_
char **matches
= NULL
;
297 _cleanup_free_
char *image
= NULL
;
298 bool nl
= false, header
= false;
304 r
= determine_image(argv
[1], false, &image
);
308 r
= determine_matches(argv
[1], argv
+ 2, true, &matches
);
312 r
= acquire_bus(&bus
);
316 r
= get_image_metadata(bus
, image
, matches
, &reply
);
320 r
= sd_bus_message_read(reply
, "s", &path
);
322 return bus_log_parse_error(r
);
324 r
= sd_bus_message_read_array(reply
, 'y', &data
, &sz
);
326 return bus_log_parse_error(r
);
328 pager_open(arg_pager_flags
);
331 printf("%s-- OS Release: --%s\n", ansi_highlight(), ansi_normal());
332 fwrite(data
, sz
, 1, stdout
);
336 _cleanup_free_
char *pretty_portable
= NULL
, *pretty_os
= NULL
;
337 _cleanup_fclose_
FILE *f
= NULL
;
339 f
= fmemopen_unlocked((void*) data
, sz
, "r");
341 return log_error_errno(errno
, "Failed to open /etc/os-release buffer: %m");
343 r
= parse_env_file(f
, "/etc/os-release",
344 "PORTABLE_PRETTY_NAME", &pretty_portable
,
345 "PRETTY_NAME", &pretty_os
);
347 return log_error_errno(r
, "Failed to parse /etc/os-release: %m");
349 printf("Image:\n\t%s\n"
350 "Portable Service:\n\t%s\n"
351 "Operating System:\n\t%s\n",
353 strna(pretty_portable
),
357 if (!strv_isempty(arg_extension_images
)) {
358 /* If we specified any extensions, we'll first get back exactly the paths (and
359 * extension-release content) for each one of the arguments. */
361 r
= sd_bus_message_enter_container(reply
, 'a', "{say}");
363 return bus_log_parse_error(r
);
365 for (size_t i
= 0; i
< strv_length(arg_extension_images
); ++i
) {
368 r
= sd_bus_message_enter_container(reply
, 'e', "say");
370 return bus_log_parse_error(r
);
374 r
= sd_bus_message_read(reply
, "s", &name
);
376 return bus_log_parse_error(r
);
378 r
= sd_bus_message_read_array(reply
, 'y', &data
, &sz
);
380 return bus_log_parse_error(r
);
386 printf("%s-- Extension Release: %s --%s\n", ansi_highlight(), name
, ansi_normal());
387 fwrite(data
, sz
, 1, stdout
);
391 _cleanup_free_
char *pretty_portable
= NULL
, *sysext_pretty_os
= NULL
,
392 *sysext_level
= NULL
, *sysext_id
= NULL
,
393 *sysext_version_id
= NULL
, *sysext_scope
= NULL
,
394 *portable_prefixes
= NULL
, *id
= NULL
, *version_id
= NULL
,
395 *sysext_image_id
= NULL
, *sysext_image_version
= NULL
,
396 *sysext_build_id
= NULL
, *confext_pretty_os
= NULL
,
397 *confext_level
= NULL
, *confext_id
= NULL
,
398 *confext_version_id
= NULL
, *confext_scope
= NULL
,
399 *confext_image_id
= NULL
, *confext_image_version
= NULL
,
400 *confext_build_id
= NULL
;
401 _cleanup_fclose_
FILE *f
= NULL
;
403 f
= fmemopen_unlocked((void*) data
, sz
, "r");
405 return log_error_errno(errno
, "Failed to open extension-release buffer: %m");
407 r
= parse_env_file(f
, name
,
408 "SYSEXT_ID", &sysext_id
,
409 "SYSEXT_VERSION_ID", &sysext_version_id
,
410 "SYSEXT_BUILD_ID", &sysext_build_id
,
411 "SYSEXT_IMAGE_ID", &sysext_image_id
,
412 "SYSEXT_IMAGE_VERSION", &sysext_image_version
,
413 "SYSEXT_SCOPE", &sysext_scope
,
414 "SYSEXT_LEVEL", &sysext_level
,
415 "SYSEXT_PRETTY_NAME", &sysext_pretty_os
,
416 "CONFEXT_ID", &confext_id
,
417 "CONFEXT_VERSION_ID", &confext_version_id
,
418 "CONFEXT_BUILD_ID", &confext_build_id
,
419 "CONFEXT_IMAGE_ID", &confext_image_id
,
420 "CONFEXT_IMAGE_VERSION", &confext_image_version
,
421 "CONFEXT_SCOPE", &confext_scope
,
422 "CONFEXT_LEVEL", &confext_level
,
423 "CONFEXT_PRETTY_NAME", &confext_pretty_os
,
425 "VERSION_ID", &version_id
,
426 "PORTABLE_PRETTY_NAME", &pretty_portable
,
427 "PORTABLE_PREFIXES", &portable_prefixes
);
429 return log_error_errno(r
, "Failed to parse extension release from '%s': %m", name
);
431 printf("Extension:\n\t%s\n"
432 "\tExtension Scope:\n\t\t%s\n"
433 "\tExtension Compatibility Level:\n\t\t%s\n"
434 "\tExtension Compatibility OS:\n\t\t%s\n"
435 "\tExtension Compatibility OS Version:\n\t\t%s\n"
436 "\tPortable Service:\n\t\t%s\n"
437 "\tPortable Prefixes:\n\t\t%s\n"
438 "\tExtension Image:\n\t\t%s%s%s %s%s%s\n",
440 strna(sysext_scope
?: confext_scope
),
441 strna(sysext_level
?: confext_level
),
444 strna(pretty_portable
),
445 strna(portable_prefixes
),
446 strempty(sysext_pretty_os
?: confext_pretty_os
),
447 (sysext_pretty_os
?: confext_pretty_os
) ? " (" : "ID: ",
448 strna(sysext_id
?: sysext_image_id
?: confext_id
?: confext_image_id
),
449 (sysext_pretty_os
?: confext_pretty_os
) ? "" : "Version: ",
450 strna(sysext_version_id
?: sysext_image_version
?: sysext_build_id
?: confext_version_id
?: confext_image_version
?: confext_build_id
),
451 (sysext_pretty_os
?: confext_pretty_os
) ? ")" : "");
454 r
= sd_bus_message_exit_container(reply
);
456 return bus_log_parse_error(r
);
459 r
= sd_bus_message_exit_container(reply
);
461 return bus_log_parse_error(r
);
464 r
= sd_bus_message_enter_container(reply
, 'a', "{say}");
466 return bus_log_parse_error(r
);
471 r
= sd_bus_message_enter_container(reply
, 'e', "say");
473 return bus_log_parse_error(r
);
477 r
= sd_bus_message_read(reply
, "s", &name
);
479 return bus_log_parse_error(r
);
481 r
= sd_bus_message_read_array(reply
, 'y', &data
, &sz
);
483 return bus_log_parse_error(r
);
489 printf("%s-- Unit file: %s --%s\n", ansi_highlight(), name
, ansi_normal());
490 fwrite(data
, sz
, 1, stdout
);
495 fputs("Unit files:\n", stdout
);
504 r
= sd_bus_message_exit_container(reply
);
506 return bus_log_parse_error(r
);
509 r
= sd_bus_message_exit_container(reply
);
511 return bus_log_parse_error(r
);
516 static int print_changes(sd_bus_message
*m
) {
522 r
= sd_bus_message_enter_container(m
, 'a', "(sss)");
524 return bus_log_parse_error(r
);
527 const char *type
, *path
, *source
;
529 r
= sd_bus_message_read(m
, "(sss)", &type
, &path
, &source
);
531 return bus_log_parse_error(r
);
535 if (streq(type
, "symlink"))
536 log_info("Created symlink %s %s %s.", path
, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT
), source
);
537 else if (streq(type
, "copy")) {
539 log_info("Copied %s.", path
);
541 log_info("Copied %s %s %s.", source
, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT
), path
);
542 } else if (streq(type
, "unlink"))
543 log_info("Removed %s.", path
);
544 else if (streq(type
, "write"))
545 log_info("Written %s.", path
);
546 else if (streq(type
, "mkdir"))
547 log_info("Created directory %s.", path
);
549 log_error("Unexpected change: %s/%s/%s", type
, path
, source
);
552 r
= sd_bus_message_exit_container(m
);
559 static int maybe_enable_disable(sd_bus
*bus
, const char *path
, bool enable
) {
560 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
561 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
562 _cleanup_strv_free_
char **names
= NULL
;
563 const uint64_t flags
= UNIT_FILE_PORTABLE
| (arg_runtime
? UNIT_FILE_RUNTIME
: 0);
569 names
= strv_new(path
, NULL
);
573 r
= bus_message_new_method_call(
577 enable
? "EnableUnitFilesWithFlags" : "DisableUnitFilesWithFlags");
579 return bus_log_create_error(r
);
581 r
= sd_bus_message_append_strv(m
, names
);
583 return bus_log_create_error(r
);
585 r
= sd_bus_message_append(m
, "t", flags
);
587 return bus_log_create_error(r
);
589 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
591 return log_error_errno(r
, "Failed to %s the portable service %s: %s",
592 enable
? "enable" : "disable", path
, bus_error_message(&error
, r
));
595 r
= sd_bus_message_skip(reply
, "b");
597 return bus_log_parse_error(r
);
600 (void) bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
);
605 static int maybe_start_stop_restart(sd_bus
*bus
, const char *path
, const char *method
, BusWaitForJobs
*wait
) {
606 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
607 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
608 _cleanup_free_
char *name
= NULL
;
609 const char *job
= NULL
;
612 assert(STR_IN_SET(method
, "StartUnit", "StopUnit", "RestartUnit"));
617 r
= path_extract_filename(path
, &name
);
619 return log_error_errno(r
, "Failed to extract file name from '%s': %m", path
);
627 "ss", name
, "replace");
629 return log_error_errno(r
, "Failed to call %s on the portable service %s: %s",
632 bus_error_message(&error
, r
));
634 r
= sd_bus_message_read(reply
, "o", &job
);
636 return bus_log_parse_error(r
);
639 log_info("Queued %s to call %s on portable service %s.", job
, method
, name
);
642 r
= bus_wait_for_jobs_add(wait
, job
);
644 return log_error_errno(r
, "Failed to watch %s job to call %s on %s: %m",
651 static int maybe_enable_start(sd_bus
*bus
, sd_bus_message
*reply
) {
652 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*wait
= NULL
;
655 if (!arg_enable
&& !arg_now
)
659 r
= bus_wait_for_jobs_new(bus
, &wait
);
661 return log_error_errno(r
, "Could not watch jobs: %m");
664 r
= sd_bus_message_rewind(reply
, true);
667 r
= sd_bus_message_enter_container(reply
, 'a', "(sss)");
669 return bus_log_parse_error(r
);
672 char *type
, *path
, *source
;
674 r
= sd_bus_message_read(reply
, "(sss)", &type
, &path
, &source
);
676 return bus_log_parse_error(r
);
680 if (STR_IN_SET(type
, "symlink", "copy") && is_portable_managed(path
)) {
681 (void) maybe_enable_disable(bus
, path
, true);
682 (void) maybe_start_stop_restart(bus
, path
, "StartUnit", wait
);
686 r
= sd_bus_message_exit_container(reply
);
691 r
= bus_wait_for_jobs(wait
, arg_quiet
, NULL
);
699 static int maybe_stop_enable_restart(sd_bus
*bus
, sd_bus_message
*reply
) {
700 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*wait
= NULL
;
703 if (!arg_enable
&& !arg_now
)
707 r
= bus_wait_for_jobs_new(bus
, &wait
);
709 return log_error_errno(r
, "Could not watch jobs: %m");
712 r
= sd_bus_message_rewind(reply
, true);
716 /* First we get a list of units that were definitely removed, not just re-attached,
717 * so we can also stop them if the user asked us to. */
718 r
= sd_bus_message_enter_container(reply
, 'a', "(sss)");
720 return bus_log_parse_error(r
);
723 char *type
, *path
, *source
;
725 r
= sd_bus_message_read(reply
, "(sss)", &type
, &path
, &source
);
727 return bus_log_parse_error(r
);
731 if (streq(type
, "unlink") && is_portable_managed(path
))
732 (void) maybe_start_stop_restart(bus
, path
, "StopUnit", wait
);
735 r
= sd_bus_message_exit_container(reply
);
739 /* Then we get a list of units that were either added or changed, so that we can
740 * enable them and/or restart them if the user asked us to. */
741 r
= sd_bus_message_enter_container(reply
, 'a', "(sss)");
743 return bus_log_parse_error(r
);
746 char *type
, *path
, *source
;
748 r
= sd_bus_message_read(reply
, "(sss)", &type
, &path
, &source
);
750 return bus_log_parse_error(r
);
754 if (STR_IN_SET(type
, "symlink", "copy") && is_portable_managed(path
)) {
755 (void) maybe_enable_disable(bus
, path
, true);
756 (void) maybe_start_stop_restart(bus
, path
, "RestartUnit", wait
);
760 r
= sd_bus_message_exit_container(reply
);
765 r
= bus_wait_for_jobs(wait
, arg_quiet
, NULL
);
773 static int maybe_clean_units(sd_bus
*bus
, char **units
) {
781 STRV_FOREACH(name
, units
) {
782 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
783 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
785 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "CleanUnit");
787 return bus_log_create_error(r
);
789 r
= sd_bus_message_append(m
, "s", *name
);
791 return bus_log_create_error(r
);
793 r
= sd_bus_message_append_strv(m
, STRV_MAKE("all", "fdstore"));
795 return bus_log_create_error(r
);
797 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
799 return log_error_errno(
801 "Failed to call CleanUnit on portable service %s: %s",
803 bus_error_message(&error
, r
));
809 static int maybe_stop_disable_clean(sd_bus
*bus
, char *image
, char *argv
[]) {
810 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*wait
= NULL
;
811 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
812 _cleanup_strv_free_
char **matches
= NULL
, **units
= NULL
;
815 if (!arg_enable
&& !arg_now
&& !arg_clean
)
818 r
= determine_matches(argv
[1], argv
+ 2, true, &matches
);
822 r
= bus_wait_for_jobs_new(bus
, &wait
);
824 return log_error_errno(r
, "Could not watch jobs: %m");
826 r
= get_image_metadata(bus
, image
, matches
, &reply
);
830 r
= sd_bus_message_skip(reply
, "say");
832 return bus_log_parse_error(r
);
834 /* If we specified any extensions or --force (which makes the request go through the new
835 * WithExtensions calls), we'll first get an array of extension-release metadata. */
836 if (!strv_isempty(arg_extension_images
) || arg_force
) {
837 r
= sd_bus_message_skip(reply
, "a{say}");
839 return bus_log_parse_error(r
);
842 r
= sd_bus_message_enter_container(reply
, 'a', "{say}");
844 return bus_log_parse_error(r
);
849 r
= sd_bus_message_enter_container(reply
, 'e', "say");
851 return bus_log_parse_error(r
);
855 r
= sd_bus_message_read(reply
, "s", &name
);
857 return bus_log_parse_error(r
);
859 r
= sd_bus_message_skip(reply
, "ay");
861 return bus_log_parse_error(r
);
863 r
= sd_bus_message_exit_container(reply
);
865 return bus_log_parse_error(r
);
867 (void) maybe_start_stop_restart(bus
, name
, "StopUnit", wait
);
868 (void) maybe_enable_disable(bus
, name
, false);
870 r
= strv_extend(&units
, name
);
875 r
= sd_bus_message_exit_container(reply
);
877 return bus_log_parse_error(r
);
879 /* Stopping must always block or the detach will fail if the unit is still running */
880 r
= bus_wait_for_jobs(wait
, arg_quiet
, NULL
);
884 /* Need to ensure all units are stopped before calling CleanUnit, as files might be in use. */
885 (void) maybe_clean_units(bus
, units
);
890 static int attach_reattach_image(int argc
, char *argv
[], const char *method
) {
891 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
892 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
893 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
894 _cleanup_strv_free_
char **matches
= NULL
;
895 _cleanup_free_
char *image
= NULL
;
899 assert(STR_IN_SET(method
, "AttachImage", "ReattachImage", "AttachImageWithExtensions", "ReattachImageWithExtensions"));
901 r
= determine_image(argv
[1], false, &image
);
905 r
= determine_matches(argv
[1], argv
+ 2, false, &matches
);
909 r
= acquire_bus(&bus
);
913 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
915 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, method
);
917 return bus_log_create_error(r
);
919 r
= sd_bus_message_append(m
, "s", image
);
921 return bus_log_create_error(r
);
923 r
= attach_extensions_to_message(m
, method
, arg_extension_images
);
927 r
= sd_bus_message_append_strv(m
, matches
);
929 return bus_log_create_error(r
);
931 r
= sd_bus_message_append(m
, "s", arg_profile
);
933 return bus_log_create_error(r
);
935 if (STR_IN_SET(method
, "AttachImageWithExtensions", "ReattachImageWithExtensions")) {
936 uint64_t flags
= (arg_runtime
? PORTABLE_RUNTIME
: 0) | (arg_force
? PORTABLE_FORCE_ATTACH
| PORTABLE_FORCE_EXTENSION
: 0);
938 r
= sd_bus_message_append(m
, "st", arg_copy_mode
, flags
);
940 r
= sd_bus_message_append(m
, "bs", arg_runtime
, arg_copy_mode
);
942 return bus_log_create_error(r
);
944 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
946 return log_error_errno(r
, "%s failed: %s", method
, bus_error_message(&error
, r
));
948 (void) maybe_reload(&bus
);
950 print_changes(reply
);
952 if (STR_IN_SET(method
, "AttachImage", "AttachImageWithExtensions"))
953 (void) maybe_enable_start(bus
, reply
);
955 /* ReattachImage returns 2 lists - removed units first, and changed/added second */
956 print_changes(reply
);
957 (void) maybe_stop_enable_restart(bus
, reply
);
963 static int attach_image(int argc
, char *argv
[], void *userdata
) {
964 return attach_reattach_image(argc
, argv
, strv_isempty(arg_extension_images
) && !arg_force
? "AttachImage" : "AttachImageWithExtensions");
967 static int reattach_image(int argc
, char *argv
[], void *userdata
) {
968 return attach_reattach_image(argc
, argv
, strv_isempty(arg_extension_images
) && !arg_force
? "ReattachImage" : "ReattachImageWithExtensions");
971 static int detach_image(int argc
, char *argv
[], void *userdata
) {
972 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
973 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
974 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
975 _cleanup_free_
char *image
= NULL
;
979 r
= determine_image(argv
[1], true, &image
);
983 r
= acquire_bus(&bus
);
987 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
989 (void) maybe_stop_disable_clean(bus
, image
, argv
);
991 method
= strv_isempty(arg_extension_images
) && !arg_force
? "DetachImage" : "DetachImageWithExtensions";
993 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, method
);
995 return bus_log_create_error(r
);
997 r
= sd_bus_message_append(m
, "s", image
);
999 return bus_log_create_error(r
);
1001 r
= attach_extensions_to_message(m
, method
, arg_extension_images
);
1005 if (streq(method
, "DetachImage"))
1006 r
= sd_bus_message_append(m
, "b", arg_runtime
);
1008 uint64_t flags
= (arg_runtime
? PORTABLE_RUNTIME
: 0) | (arg_force
? PORTABLE_FORCE_ATTACH
| PORTABLE_FORCE_EXTENSION
: 0);
1010 r
= sd_bus_message_append(m
, "t", flags
);
1013 return bus_log_create_error(r
);
1015 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1017 return log_error_errno(r
, "%s failed: %s", method
, bus_error_message(&error
, r
));
1019 (void) maybe_reload(&bus
);
1021 print_changes(reply
);
1025 static int list_images(int argc
, char *argv
[], void *userdata
) {
1026 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1027 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1028 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1029 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1032 r
= acquire_bus(&bus
);
1036 r
= bus_call_method(bus
, bus_portable_mgr
, "ListImages", &error
, &reply
, NULL
);
1038 return log_error_errno(r
, "Failed to list images: %s", bus_error_message(&error
, r
));
1040 table
= table_new("name", "type", "ro", "crtime", "mtime", "usage", "state");
1044 r
= sd_bus_message_enter_container(reply
, 'a', "(ssbtttso)");
1046 return bus_log_parse_error(r
);
1049 const char *name
, *type
, *state
;
1050 uint64_t crtime
, mtime
, usage
;
1053 r
= sd_bus_message_read(reply
, "(ssbtttso)", &name
, &type
, &ro_int
, &crtime
, &mtime
, &usage
, &state
, NULL
);
1055 return bus_log_parse_error(r
);
1059 r
= table_add_many(table
,
1062 TABLE_BOOLEAN
, ro_int
,
1063 TABLE_SET_COLOR
, ro_int
? ansi_highlight_red() : NULL
,
1064 TABLE_TIMESTAMP
, crtime
,
1065 TABLE_TIMESTAMP
, mtime
,
1067 TABLE_STRING
, state
,
1068 TABLE_SET_COLOR
, !streq(state
, "detached") ? ansi_highlight_green() : NULL
);
1070 return table_log_add_error(r
);
1073 r
= sd_bus_message_exit_container(reply
);
1075 return bus_log_parse_error(r
);
1077 if (!table_isempty(table
)) {
1078 r
= table_set_sort(table
, (size_t) 0);
1080 return table_log_sort_error(r
);
1082 table_set_header(table
, arg_legend
);
1084 r
= table_print(table
, NULL
);
1086 return table_log_print_error(r
);
1090 if (table_isempty(table
))
1091 printf("No images.\n");
1093 printf("\n%zu images listed.\n", table_get_rows(table
) - 1);
1099 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1100 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1103 r
= acquire_bus(&bus
);
1107 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1109 for (i
= 1; i
< argc
; i
++) {
1110 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1111 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1113 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, "RemoveImage");
1115 return bus_log_create_error(r
);
1117 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1119 return bus_log_create_error(r
);
1121 /* This is a slow operation, hence turn off any method call timeouts */
1122 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1124 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1130 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1131 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1132 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1136 b
= parse_boolean(argv
[2]);
1138 return log_error_errno(b
, "Failed to parse boolean argument: %s", argv
[2]);
1141 r
= acquire_bus(&bus
);
1145 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1147 r
= bus_call_method(bus
, bus_portable_mgr
, "MarkImageReadOnly", &error
, NULL
, "sb", argv
[1], b
);
1149 return log_error_errno(r
, "Could not mark image read-only: %s", bus_error_message(&error
, r
));
1154 static int set_limit(int argc
, char *argv
[], void *userdata
) {
1155 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1156 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1160 r
= acquire_bus(&bus
);
1164 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1166 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
1169 r
= parse_size(argv
[argc
-1], 1024, &limit
);
1171 return log_error_errno(r
, "Failed to parse size: %s", argv
[argc
-1]);
1175 /* With two arguments changes the quota limit of the specified image */
1176 r
= bus_call_method(bus
, bus_portable_mgr
, "SetImageLimit", &error
, NULL
, "st", argv
[1], limit
);
1178 /* With one argument changes the pool quota limit */
1179 r
= bus_call_method(bus
, bus_portable_mgr
, "SetPoolLimit", &error
, NULL
, "t", limit
);
1182 return log_error_errno(r
, "Could not set limit: %s", bus_error_message(&error
, r
));
1187 static int is_image_attached(int argc
, char *argv
[], void *userdata
) {
1188 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1189 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1190 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1191 _cleanup_free_
char *image
= NULL
;
1192 const char *state
, *method
;
1195 r
= determine_image(argv
[1], true, &image
);
1199 r
= acquire_bus(&bus
);
1203 method
= strv_isempty(arg_extension_images
) ? "GetImageState" : "GetImageStateWithExtensions";
1205 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, method
);
1207 return bus_log_create_error(r
);
1209 r
= sd_bus_message_append(m
, "s", image
);
1211 return bus_log_create_error(r
);
1213 r
= attach_extensions_to_message(m
, method
, arg_extension_images
);
1217 if (!strv_isempty(arg_extension_images
)) {
1218 r
= sd_bus_message_append(m
, "t", UINT64_C(0));
1220 return bus_log_create_error(r
);
1223 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1225 return log_error_errno(r
, "%s failed: %s", method
, bus_error_message(&error
, r
));
1227 r
= sd_bus_message_read(reply
, "s", &state
);
1234 return streq(state
, "detached");
1237 static int dump_profiles(void) {
1238 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1239 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1240 _cleanup_strv_free_
char **l
= NULL
;
1243 r
= acquire_bus(&bus
);
1247 r
= bus_get_property_strv(bus
, bus_portable_mgr
, "Profiles", &error
, &l
);
1249 return log_error_errno(r
, "Failed to acquire list of profiles: %s", bus_error_message(&error
, r
));
1252 log_info("Available unit profiles:");
1254 STRV_FOREACH(i
, l
) {
1256 fputc('\n', stdout
);
1262 static int help(int argc
, char *argv
[], void *userdata
) {
1263 _cleanup_free_
char *link
= NULL
;
1266 pager_open(arg_pager_flags
);
1268 r
= terminal_urlify_man("portablectl", "1", &link
);
1272 printf("%s [OPTIONS...] COMMAND ...\n\n"
1273 "%sAttach or detach portable services from the local system.%s\n"
1275 " list List available portable service images\n"
1276 " attach NAME|PATH [PREFIX...]\n"
1277 " Attach the specified portable service image\n"
1278 " detach NAME|PATH [PREFIX...]\n"
1279 " Detach the specified portable service image\n"
1280 " reattach NAME|PATH [PREFIX...]\n"
1281 " Reattach the specified portable service image\n"
1282 " inspect NAME|PATH [PREFIX...]\n"
1283 " Show details of specified portable service image\n"
1284 " is-attached NAME|PATH Query if portable service image is attached\n"
1285 " read-only NAME|PATH [BOOL] Mark or unmark portable service image read-only\n"
1286 " remove NAME|PATH... Remove a portable service image\n"
1287 " set-limit [NAME|PATH] Set image or pool size limit (disk quota)\n"
1289 " -h --help Show this help\n"
1290 " --version Show package version\n"
1291 " --no-pager Do not pipe output into a pager\n"
1292 " --no-legend Do not show the headers and footers\n"
1293 " --no-ask-password Do not ask for system passwords\n"
1294 " -H --host=[USER@]HOST Operate on remote host\n"
1295 " -M --machine=CONTAINER Operate on local container\n"
1296 " -q --quiet Suppress informational messages\n"
1297 " -p --profile=PROFILE Pick security profile for portable service\n"
1298 " --copy=copy|auto|symlink|mixed\n"
1299 " Pick copying or symlinking of resources\n"
1300 " --runtime Attach portable service until next reboot only\n"
1301 " --no-reload Don't reload the system and service manager\n"
1302 " --cat When inspecting include unit and os-release file\n"
1304 " --enable Immediately enable/disable the portable service\n"
1305 " after attach/detach\n"
1306 " --now Immediately start/stop the portable service after\n"
1307 " attach/before detach\n"
1308 " --no-block Don't block waiting for attach --now to complete\n"
1309 " --extension=PATH Extend the image with an overlay\n"
1310 " --force Skip 'already active' check when attaching or\n"
1311 " detaching an image (with extensions)\n"
1312 " --clean When detaching, also remove configuration, state,\n"
1313 " cache, logs or runtime data of the portable\n"
1315 "\nSee the %s for details.\n",
1316 program_invocation_short_name
,
1324 static int parse_argv(int argc
, char *argv
[]) {
1328 ARG_VERSION
= 0x100,
1331 ARG_NO_ASK_PASSWORD
,
1344 static const struct option options
[] = {
1345 { "help", no_argument
, NULL
, 'h' },
1346 { "version", no_argument
, NULL
, ARG_VERSION
},
1347 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1348 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1349 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
1350 { "host", required_argument
, NULL
, 'H' },
1351 { "machine", required_argument
, NULL
, 'M' },
1352 { "quiet", no_argument
, NULL
, 'q' },
1353 { "profile", required_argument
, NULL
, 'p' },
1354 { "copy", required_argument
, NULL
, ARG_COPY
},
1355 { "runtime", no_argument
, NULL
, ARG_RUNTIME
},
1356 { "no-reload", no_argument
, NULL
, ARG_NO_RELOAD
},
1357 { "cat", no_argument
, NULL
, ARG_CAT
},
1358 { "enable", no_argument
, NULL
, ARG_ENABLE
},
1359 { "now", no_argument
, NULL
, ARG_NOW
},
1360 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
1361 { "extension", required_argument
, NULL
, ARG_EXTENSION
},
1362 { "force", no_argument
, NULL
, ARG_FORCE
},
1363 { "clean", no_argument
, NULL
, ARG_CLEAN
},
1373 c
= getopt_long(argc
, argv
, "hH:M:qp:", options
, NULL
);
1380 return help(0, NULL
, NULL
);
1386 arg_pager_flags
|= PAGER_DISABLE
;
1393 case ARG_NO_ASK_PASSWORD
:
1394 arg_ask_password
= false;
1398 arg_transport
= BUS_TRANSPORT_REMOTE
;
1403 arg_transport
= BUS_TRANSPORT_MACHINE
;
1412 if (streq(optarg
, "help"))
1413 return dump_profiles();
1415 if (!filename_is_valid(optarg
))
1416 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1417 "Unit profile name not valid: %s", optarg
);
1419 arg_profile
= optarg
;
1423 if (streq(optarg
, "auto"))
1424 arg_copy_mode
= NULL
;
1425 else if (STR_IN_SET(optarg
, "copy", "symlink", "mixed"))
1426 arg_copy_mode
= optarg
;
1427 else if (streq(optarg
, "help")) {
1434 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1435 "Failed to parse --copy= argument: %s", optarg
);
1460 arg_no_block
= true;
1464 r
= strv_extend(&arg_extension_images
, optarg
);
1481 assert_not_reached();
1488 static int run(int argc
, char *argv
[]) {
1489 static const Verb verbs
[] = {
1490 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1491 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_images
},
1492 { "attach", 2, VERB_ANY
, 0, attach_image
},
1493 { "detach", 2, VERB_ANY
, 0, detach_image
},
1494 { "inspect", 2, VERB_ANY
, 0, inspect_image
},
1495 { "is-attached", 2, 2, 0, is_image_attached
},
1496 { "read-only", 2, 3, 0, read_only_image
},
1497 { "remove", 2, VERB_ANY
, 0, remove_image
},
1498 { "set-limit", 3, 3, 0, set_limit
},
1499 { "reattach", 2, VERB_ANY
, 0, reattach_image
},
1507 r
= parse_argv(argc
, argv
);
1511 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1514 DEFINE_MAIN_FUNCTION(run
);