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;
567 names
= strv_new(path
, NULL
);
571 r
= sd_bus_message_new_method_call(
574 "org.freedesktop.systemd1",
575 "/org/freedesktop/systemd1",
576 "org.freedesktop.systemd1.Manager",
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
, &changes
, &n_changes
);
601 install_changes_free(changes
, n_changes
);
606 static int maybe_start_stop_restart(sd_bus
*bus
, const char *path
, const char *method
, BusWaitForJobs
*wait
) {
607 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
608 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
609 _cleanup_free_
char *name
= NULL
;
610 const char *job
= NULL
;
613 assert(STR_IN_SET(method
, "StartUnit", "StopUnit", "RestartUnit"));
618 r
= path_extract_filename(path
, &name
);
620 return log_error_errno(r
, "Failed to extract file name from '%s': %m", path
);
628 "ss", name
, "replace");
630 return log_error_errno(r
, "Failed to call %s on the portable service %s: %s",
633 bus_error_message(&error
, r
));
635 r
= sd_bus_message_read(reply
, "o", &job
);
637 return bus_log_parse_error(r
);
640 log_info("Queued %s to call %s on portable service %s.", job
, method
, name
);
643 r
= bus_wait_for_jobs_add(wait
, job
);
645 return log_error_errno(r
, "Failed to watch %s job to call %s on %s: %m",
652 static int maybe_enable_start(sd_bus
*bus
, sd_bus_message
*reply
) {
653 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*wait
= NULL
;
656 if (!arg_enable
&& !arg_now
)
660 r
= bus_wait_for_jobs_new(bus
, &wait
);
662 return log_error_errno(r
, "Could not watch jobs: %m");
665 r
= sd_bus_message_rewind(reply
, true);
668 r
= sd_bus_message_enter_container(reply
, 'a', "(sss)");
670 return bus_log_parse_error(r
);
673 char *type
, *path
, *source
;
675 r
= sd_bus_message_read(reply
, "(sss)", &type
, &path
, &source
);
677 return bus_log_parse_error(r
);
681 if (STR_IN_SET(type
, "symlink", "copy") && is_portable_managed(path
)) {
682 (void) maybe_enable_disable(bus
, path
, true);
683 (void) maybe_start_stop_restart(bus
, path
, "StartUnit", wait
);
687 r
= sd_bus_message_exit_container(reply
);
692 r
= bus_wait_for_jobs(wait
, arg_quiet
, NULL
);
700 static int maybe_stop_enable_restart(sd_bus
*bus
, sd_bus_message
*reply
) {
701 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*wait
= NULL
;
704 if (!arg_enable
&& !arg_now
)
708 r
= bus_wait_for_jobs_new(bus
, &wait
);
710 return log_error_errno(r
, "Could not watch jobs: %m");
713 r
= sd_bus_message_rewind(reply
, true);
717 /* First we get a list of units that were definitely removed, not just re-attached,
718 * so we can also stop them if the user asked us to. */
719 r
= sd_bus_message_enter_container(reply
, 'a', "(sss)");
721 return bus_log_parse_error(r
);
724 char *type
, *path
, *source
;
726 r
= sd_bus_message_read(reply
, "(sss)", &type
, &path
, &source
);
728 return bus_log_parse_error(r
);
732 if (streq(type
, "unlink") && is_portable_managed(path
))
733 (void) maybe_start_stop_restart(bus
, path
, "StopUnit", wait
);
736 r
= sd_bus_message_exit_container(reply
);
740 /* Then we get a list of units that were either added or changed, so that we can
741 * enable them and/or restart them if the user asked us to. */
742 r
= sd_bus_message_enter_container(reply
, 'a', "(sss)");
744 return bus_log_parse_error(r
);
747 char *type
, *path
, *source
;
749 r
= sd_bus_message_read(reply
, "(sss)", &type
, &path
, &source
);
751 return bus_log_parse_error(r
);
755 if (STR_IN_SET(type
, "symlink", "copy") && is_portable_managed(path
)) {
756 (void) maybe_enable_disable(bus
, path
, true);
757 (void) maybe_start_stop_restart(bus
, path
, "RestartUnit", wait
);
761 r
= sd_bus_message_exit_container(reply
);
766 r
= bus_wait_for_jobs(wait
, arg_quiet
, NULL
);
774 static int maybe_stop_disable(sd_bus
*bus
, char *image
, char *argv
[]) {
775 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*wait
= NULL
;
776 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
777 _cleanup_strv_free_
char **matches
= NULL
;
780 if (!arg_enable
&& !arg_now
)
783 r
= determine_matches(argv
[1], argv
+ 2, true, &matches
);
787 r
= bus_wait_for_jobs_new(bus
, &wait
);
789 return log_error_errno(r
, "Could not watch jobs: %m");
791 r
= get_image_metadata(bus
, image
, matches
, &reply
);
795 r
= sd_bus_message_skip(reply
, "say");
797 return bus_log_parse_error(r
);
799 /* If we specified any extensions, we'll first an array of extension-release metadata. */
800 if (!strv_isempty(arg_extension_images
)) {
801 r
= sd_bus_message_skip(reply
, "a{say}");
803 return bus_log_parse_error(r
);
806 r
= sd_bus_message_enter_container(reply
, 'a', "{say}");
808 return bus_log_parse_error(r
);
813 r
= sd_bus_message_enter_container(reply
, 'e', "say");
815 return bus_log_parse_error(r
);
819 r
= sd_bus_message_read(reply
, "s", &name
);
821 return bus_log_parse_error(r
);
823 r
= sd_bus_message_skip(reply
, "ay");
825 return bus_log_parse_error(r
);
827 r
= sd_bus_message_exit_container(reply
);
829 return bus_log_parse_error(r
);
831 (void) maybe_start_stop_restart(bus
, name
, "StopUnit", wait
);
832 (void) maybe_enable_disable(bus
, name
, false);
835 r
= sd_bus_message_exit_container(reply
);
837 return bus_log_parse_error(r
);
839 /* Stopping must always block or the detach will fail if the unit is still running */
840 r
= bus_wait_for_jobs(wait
, arg_quiet
, NULL
);
847 static int attach_reattach_image(int argc
, char *argv
[], const char *method
) {
848 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
849 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
850 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
851 _cleanup_strv_free_
char **matches
= NULL
;
852 _cleanup_free_
char *image
= NULL
;
856 assert(STR_IN_SET(method
, "AttachImage", "ReattachImage", "AttachImageWithExtensions", "ReattachImageWithExtensions"));
858 r
= determine_image(argv
[1], false, &image
);
862 r
= determine_matches(argv
[1], argv
+ 2, false, &matches
);
866 r
= acquire_bus(&bus
);
870 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
872 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, method
);
874 return bus_log_create_error(r
);
876 r
= sd_bus_message_append(m
, "s", image
);
878 return bus_log_create_error(r
);
880 r
= attach_extensions_to_message(m
, arg_extension_images
);
884 r
= sd_bus_message_append_strv(m
, matches
);
886 return bus_log_create_error(r
);
888 r
= sd_bus_message_append(m
, "s", arg_profile
);
890 return bus_log_create_error(r
);
892 if (STR_IN_SET(method
, "AttachImageWithExtensions", "ReattachImageWithExtensions")) {
893 uint64_t flags
= (arg_runtime
? PORTABLE_RUNTIME
: 0) | (arg_force
? PORTABLE_FORCE_ATTACH
| PORTABLE_FORCE_SYSEXT
: 0);
895 r
= sd_bus_message_append(m
, "st", arg_copy_mode
, flags
);
897 r
= sd_bus_message_append(m
, "bs", arg_runtime
, arg_copy_mode
);
899 return bus_log_create_error(r
);
901 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
903 return log_error_errno(r
, "%s failed: %s", method
, bus_error_message(&error
, r
));
905 (void) maybe_reload(&bus
);
907 print_changes(reply
);
909 if (STR_IN_SET(method
, "AttachImage", "AttachImageWithExtensions"))
910 (void) maybe_enable_start(bus
, reply
);
912 /* ReattachImage returns 2 lists - removed units first, and changed/added second */
913 print_changes(reply
);
914 (void) maybe_stop_enable_restart(bus
, reply
);
920 static int attach_image(int argc
, char *argv
[], void *userdata
) {
921 return attach_reattach_image(argc
, argv
, strv_isempty(arg_extension_images
) ? "AttachImage" : "AttachImageWithExtensions");
924 static int reattach_image(int argc
, char *argv
[], void *userdata
) {
925 return attach_reattach_image(argc
, argv
, strv_isempty(arg_extension_images
) ? "ReattachImage" : "ReattachImageWithExtensions");
928 static int detach_image(int argc
, char *argv
[], void *userdata
) {
929 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
930 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
931 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
932 _cleanup_free_
char *image
= NULL
;
936 r
= determine_image(argv
[1], true, &image
);
940 r
= acquire_bus(&bus
);
944 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
946 (void) maybe_stop_disable(bus
, image
, argv
);
948 method
= strv_isempty(arg_extension_images
) ? "DetachImage" : "DetachImageWithExtensions";
950 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, method
);
952 return bus_log_create_error(r
);
954 r
= sd_bus_message_append(m
, "s", image
);
956 return bus_log_create_error(r
);
958 r
= attach_extensions_to_message(m
, arg_extension_images
);
962 if (strv_isempty(arg_extension_images
))
963 r
= sd_bus_message_append(m
, "b", arg_runtime
);
965 uint64_t flags
= (arg_runtime
? PORTABLE_RUNTIME
: 0) | (arg_force
? PORTABLE_FORCE_ATTACH
| PORTABLE_FORCE_SYSEXT
: 0);
967 r
= sd_bus_message_append(m
, "t", flags
);
970 return bus_log_create_error(r
);
972 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
974 return log_error_errno(r
, "%s failed: %s", method
, bus_error_message(&error
, r
));
976 (void) maybe_reload(&bus
);
978 print_changes(reply
);
982 static int list_images(int argc
, char *argv
[], void *userdata
) {
983 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
984 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
985 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
986 _cleanup_(table_unrefp
) Table
*table
= NULL
;
989 r
= acquire_bus(&bus
);
993 r
= bus_call_method(bus
, bus_portable_mgr
, "ListImages", &error
, &reply
, NULL
);
995 return log_error_errno(r
, "Failed to list images: %s", bus_error_message(&error
, r
));
997 table
= table_new("name", "type", "ro", "crtime", "mtime", "usage", "state");
1001 r
= sd_bus_message_enter_container(reply
, 'a', "(ssbtttso)");
1003 return bus_log_parse_error(r
);
1006 const char *name
, *type
, *state
;
1007 uint64_t crtime
, mtime
, usage
;
1010 r
= sd_bus_message_read(reply
, "(ssbtttso)", &name
, &type
, &ro_int
, &crtime
, &mtime
, &usage
, &state
, NULL
);
1012 return bus_log_parse_error(r
);
1016 r
= table_add_many(table
,
1019 TABLE_BOOLEAN
, ro_int
,
1020 TABLE_SET_COLOR
, ro_int
? ansi_highlight_red() : NULL
,
1021 TABLE_TIMESTAMP
, crtime
,
1022 TABLE_TIMESTAMP
, mtime
,
1024 TABLE_STRING
, state
,
1025 TABLE_SET_COLOR
, !streq(state
, "detached") ? ansi_highlight_green() : NULL
);
1027 return table_log_add_error(r
);
1030 r
= sd_bus_message_exit_container(reply
);
1032 return bus_log_parse_error(r
);
1034 if (table_get_rows(table
) > 1) {
1035 r
= table_set_sort(table
, (size_t) 0);
1037 return table_log_sort_error(r
);
1039 table_set_header(table
, arg_legend
);
1041 r
= table_print(table
, NULL
);
1043 return table_log_print_error(r
);
1047 if (table_get_rows(table
) > 1)
1048 printf("\n%zu images listed.\n", table_get_rows(table
) - 1);
1050 printf("No images.\n");
1056 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1057 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1060 r
= acquire_bus(&bus
);
1064 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1066 for (i
= 1; i
< argc
; i
++) {
1067 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1068 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1070 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, "RemoveImage");
1072 return bus_log_create_error(r
);
1074 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1076 return bus_log_create_error(r
);
1078 /* This is a slow operation, hence turn off any method call timeouts */
1079 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1081 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1087 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1088 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1089 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1093 b
= parse_boolean(argv
[2]);
1095 return log_error_errno(b
, "Failed to parse boolean argument: %s", argv
[2]);
1098 r
= acquire_bus(&bus
);
1102 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1104 r
= bus_call_method(bus
, bus_portable_mgr
, "MarkImageReadOnly", &error
, NULL
, "sb", argv
[1], b
);
1106 return log_error_errno(r
, "Could not mark image read-only: %s", bus_error_message(&error
, r
));
1111 static int set_limit(int argc
, char *argv
[], void *userdata
) {
1112 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1113 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1117 r
= acquire_bus(&bus
);
1121 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1123 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
1126 r
= parse_size(argv
[argc
-1], 1024, &limit
);
1128 return log_error_errno(r
, "Failed to parse size: %s", argv
[argc
-1]);
1132 /* With two arguments changes the quota limit of the specified image */
1133 r
= bus_call_method(bus
, bus_portable_mgr
, "SetImageLimit", &error
, NULL
, "st", argv
[1], limit
);
1135 /* With one argument changes the pool quota limit */
1136 r
= bus_call_method(bus
, bus_portable_mgr
, "SetPoolLimit", &error
, NULL
, "t", limit
);
1139 return log_error_errno(r
, "Could not set limit: %s", bus_error_message(&error
, r
));
1144 static int is_image_attached(int argc
, char *argv
[], void *userdata
) {
1145 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1146 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1147 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1148 _cleanup_free_
char *image
= NULL
;
1149 const char *state
, *method
;
1152 r
= determine_image(argv
[1], true, &image
);
1156 r
= acquire_bus(&bus
);
1160 method
= strv_isempty(arg_extension_images
) ? "GetImageState" : "GetImageStateWithExtensions";
1162 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, method
);
1164 return bus_log_create_error(r
);
1166 r
= sd_bus_message_append(m
, "s", image
);
1168 return bus_log_create_error(r
);
1170 r
= attach_extensions_to_message(m
, arg_extension_images
);
1174 if (!strv_isempty(arg_extension_images
)) {
1175 r
= sd_bus_message_append(m
, "t", 0);
1177 return bus_log_create_error(r
);
1180 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1182 return log_error_errno(r
, "%s failed: %s", method
, bus_error_message(&error
, r
));
1184 r
= sd_bus_message_read(reply
, "s", &state
);
1191 return streq(state
, "detached");
1194 static int dump_profiles(void) {
1195 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1196 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1197 _cleanup_strv_free_
char **l
= NULL
;
1200 r
= acquire_bus(&bus
);
1204 r
= bus_get_property_strv(bus
, bus_portable_mgr
, "Profiles", &error
, &l
);
1206 return log_error_errno(r
, "Failed to acquire list of profiles: %s", bus_error_message(&error
, r
));
1209 log_info("Available unit profiles:");
1211 STRV_FOREACH(i
, l
) {
1213 fputc('\n', stdout
);
1219 static int help(int argc
, char *argv
[], void *userdata
) {
1220 _cleanup_free_
char *link
= NULL
;
1223 pager_open(arg_pager_flags
);
1225 r
= terminal_urlify_man("portablectl", "1", &link
);
1229 printf("%s [OPTIONS...] COMMAND ...\n\n"
1230 "%sAttach or detach portable services from the local system.%s\n"
1232 " list List available portable service images\n"
1233 " attach NAME|PATH [PREFIX...]\n"
1234 " Attach the specified portable service image\n"
1235 " detach NAME|PATH [PREFIX...]\n"
1236 " Detach the specified portable service image\n"
1237 " reattach NAME|PATH [PREFIX...]\n"
1238 " Reattach the specified portable service image\n"
1239 " inspect NAME|PATH [PREFIX...]\n"
1240 " Show details of specified portable service image\n"
1241 " is-attached NAME|PATH Query if portable service image is attached\n"
1242 " read-only NAME|PATH [BOOL] Mark or unmark portable service image read-only\n"
1243 " remove NAME|PATH... Remove a portable service image\n"
1244 " set-limit [NAME|PATH] Set image or pool size limit (disk quota)\n"
1246 " -h --help Show this help\n"
1247 " --version Show package version\n"
1248 " --no-pager Do not pipe output into a pager\n"
1249 " --no-legend Do not show the headers and footers\n"
1250 " --no-ask-password Do not ask for system passwords\n"
1251 " -H --host=[USER@]HOST Operate on remote host\n"
1252 " -M --machine=CONTAINER Operate on local container\n"
1253 " -q --quiet Suppress informational messages\n"
1254 " -p --profile=PROFILE Pick security profile for portable service\n"
1255 " --copy=copy|auto|symlink Prefer copying or symlinks if possible\n"
1256 " --runtime Attach portable service until next reboot only\n"
1257 " --no-reload Don't reload the system and service manager\n"
1258 " --cat When inspecting include unit and os-release file\n"
1260 " --enable Immediately enable/disable the portable service\n"
1261 " after attach/detach\n"
1262 " --now Immediately start/stop the portable service after\n"
1263 " attach/before detach\n"
1264 " --no-block Don't block waiting for attach --now to complete\n"
1265 " --extension=PATH Extend the image with an overlay\n"
1266 " --force Skip 'already active' check when attaching or\n"
1267 " detaching an image (with extensions)\n"
1268 "\nSee the %s for details.\n",
1269 program_invocation_short_name
,
1277 static int parse_argv(int argc
, char *argv
[]) {
1281 ARG_VERSION
= 0x100,
1284 ARG_NO_ASK_PASSWORD
,
1296 static const struct option options
[] = {
1297 { "help", no_argument
, NULL
, 'h' },
1298 { "version", no_argument
, NULL
, ARG_VERSION
},
1299 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1300 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1301 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
1302 { "host", required_argument
, NULL
, 'H' },
1303 { "machine", required_argument
, NULL
, 'M' },
1304 { "quiet", no_argument
, NULL
, 'q' },
1305 { "profile", required_argument
, NULL
, 'p' },
1306 { "copy", required_argument
, NULL
, ARG_COPY
},
1307 { "runtime", no_argument
, NULL
, ARG_RUNTIME
},
1308 { "no-reload", no_argument
, NULL
, ARG_NO_RELOAD
},
1309 { "cat", no_argument
, NULL
, ARG_CAT
},
1310 { "enable", no_argument
, NULL
, ARG_ENABLE
},
1311 { "now", no_argument
, NULL
, ARG_NOW
},
1312 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
1313 { "extension", required_argument
, NULL
, ARG_EXTENSION
},
1314 { "force", no_argument
, NULL
, ARG_FORCE
},
1324 c
= getopt_long(argc
, argv
, "hH:M:qp:", options
, NULL
);
1331 return help(0, NULL
, NULL
);
1337 arg_pager_flags
|= PAGER_DISABLE
;
1344 case ARG_NO_ASK_PASSWORD
:
1345 arg_ask_password
= false;
1349 arg_transport
= BUS_TRANSPORT_REMOTE
;
1354 arg_transport
= BUS_TRANSPORT_MACHINE
;
1363 if (streq(optarg
, "help"))
1364 return dump_profiles();
1366 if (!filename_is_valid(optarg
))
1367 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1368 "Unit profile name not valid: %s", optarg
);
1370 arg_profile
= optarg
;
1374 if (streq(optarg
, "auto"))
1375 arg_copy_mode
= NULL
;
1376 else if (STR_IN_SET(optarg
, "copy", "symlink"))
1377 arg_copy_mode
= optarg
;
1378 else if (streq(optarg
, "help")) {
1384 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1385 "Failed to parse --copy= argument: %s", optarg
);
1410 arg_no_block
= true;
1414 r
= strv_extend(&arg_extension_images
, optarg
);
1427 assert_not_reached();
1434 static int run(int argc
, char *argv
[]) {
1435 static const Verb verbs
[] = {
1436 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1437 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_images
},
1438 { "attach", 2, VERB_ANY
, 0, attach_image
},
1439 { "detach", 2, VERB_ANY
, 0, detach_image
},
1440 { "inspect", 2, VERB_ANY
, 0, inspect_image
},
1441 { "is-attached", 2, 2, 0, is_image_attached
},
1442 { "read-only", 2, 3, 0, read_only_image
},
1443 { "remove", 2, VERB_ANY
, 0, remove_image
},
1444 { "set-limit", 3, 3, 0, set_limit
},
1445 { "reattach", 2, VERB_ANY
, 0, reattach_image
},
1453 r
= parse_argv(argc
, argv
);
1457 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1460 DEFINE_MAIN_FUNCTION(run
);