1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "alloc-util.h"
10 #include "bus-locator.h"
11 #include "bus-unit-util.h"
12 #include "bus-wait-for-jobs.h"
13 #include "chase-symlinks.h"
15 #include "dirent-util.h"
19 #include "format-table.h"
21 #include "locale-util.h"
22 #include "main-func.h"
25 #include "parse-argument.h"
26 #include "parse-util.h"
27 #include "path-util.h"
29 #include "pretty-print.h"
30 #include "spawn-polkit-agent.h"
31 #include "string-util.h"
33 #include "terminal-util.h"
36 static PagerFlags arg_pager_flags
= 0;
37 static bool arg_legend
= true;
38 static bool arg_ask_password
= true;
39 static bool arg_quiet
= false;
40 static const char *arg_profile
= "default";
41 static const char* arg_copy_mode
= NULL
;
42 static bool arg_runtime
= false;
43 static bool arg_reload
= true;
44 static bool arg_cat
= false;
45 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
46 static const char *arg_host
= NULL
;
47 static bool arg_enable
= false;
48 static bool arg_now
= false;
49 static bool arg_no_block
= false;
50 static char **arg_extension_images
= NULL
;
52 STATIC_DESTRUCTOR_REGISTER(arg_extension_images
, strv_freep
);
54 static bool is_portable_managed(const char *unit
) {
55 return ENDSWITH_SET(unit
, ".service", ".target", ".socket", ".path", ".timer");
58 static int determine_image(const char *image
, bool permit_non_existing
, char **ret
) {
61 /* If the specified name is a valid image name, we pass it as-is to portabled, which will search for it in the
62 * usual search directories. Otherwise we presume it's a path, and will normalize it on the client's side
63 * (among other things, to make the path independent of the client's working directory) before passing it
66 if (image_name_is_valid(image
)) {
69 if (!arg_quiet
&& laccess(image
, F_OK
) >= 0)
70 log_warning("Ambiguous invocation: current working directory contains file matching non-path argument '%s', ignoring. "
71 "Prefix argument with './' to force reference to file in current working directory.", image
);
81 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
82 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
83 "Operations on images by path not supported when connecting to remote systems.");
85 r
= chase_symlinks(image
, NULL
, CHASE_TRAIL_SLASH
| (permit_non_existing
? CHASE_NONEXISTENT
: 0), ret
, NULL
);
87 return log_error_errno(r
, "Cannot normalize specified image path '%s': %m", image
);
92 static int attach_extensions_to_message(sd_bus_message
*m
, char **extensions
) {
98 if (strv_isempty(extensions
))
101 r
= sd_bus_message_open_container(m
, 'a', "s");
103 return bus_log_create_error(r
);
105 STRV_FOREACH(p
, extensions
) {
106 _cleanup_free_
char *resolved_extension_image
= NULL
;
108 r
= determine_image(*p
, false, &resolved_extension_image
);
112 r
= sd_bus_message_append(m
, "s", resolved_extension_image
);
114 return bus_log_create_error(r
);
117 r
= sd_bus_message_close_container(m
);
119 return bus_log_create_error(r
);
124 static int extract_prefix(const char *path
, char **ret
) {
125 _cleanup_free_
char *name
= NULL
;
126 const char *bn
, *underscore
;
131 underscore
= strchr(bn
, '_');
137 e
= endswith(bn
, ".raw");
144 name
= strndup(bn
, m
);
148 /* A slightly reduced version of what's permitted in unit names. With ':' and '\' are removed, as well as '_'
149 * which we use as delimiter for the second part of the image string, which we ignore for now. */
150 if (!in_charset(name
, DIGITS LETTERS
"-."))
153 if (!filename_is_valid(name
))
156 *ret
= TAKE_PTR(name
);
161 static int determine_matches(const char *image
, char **l
, bool allow_any
, char ***ret
) {
162 _cleanup_strv_free_
char **k
= NULL
;
165 /* Determine the matches to apply. If the list is empty we derive the match from the image name. If the list
166 * contains exactly the "-" we return a wildcard list (which is the empty list), but only if this is expressly
169 if (strv_isempty(l
)) {
172 r
= extract_prefix(image
, &prefix
);
174 return log_error_errno(r
, "Failed to extract prefix of image name '%s': %m", image
);
177 log_info("(Matching unit files with prefix '%s'.)", prefix
);
179 r
= strv_consume(&k
, prefix
);
183 } else if (strv_equal(l
, STRV_MAKE("-"))) {
186 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
187 "Refusing all unit file match.");
190 log_info("(Matching all unit files.)");
198 _cleanup_free_
char *joined
= NULL
;
200 joined
= strv_join(k
, "', '");
204 log_info("(Matching unit files with prefixes '%s'.)", joined
);
213 static int acquire_bus(sd_bus
**bus
) {
221 r
= bus_connect_transport(arg_transport
, arg_host
, false, bus
);
223 return bus_log_connect_error(r
, arg_transport
);
225 (void) sd_bus_set_allow_interactive_authorization(*bus
, arg_ask_password
);
230 static int maybe_reload(sd_bus
**bus
) {
231 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
232 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
238 r
= acquire_bus(bus
);
242 r
= sd_bus_message_new_method_call(
245 "org.freedesktop.systemd1",
246 "/org/freedesktop/systemd1",
247 "org.freedesktop.systemd1.Manager",
250 return bus_log_create_error(r
);
252 /* Reloading the daemon may take long, hence set a longer timeout here */
253 r
= sd_bus_call(*bus
, m
, DEFAULT_TIMEOUT_USEC
* 2, &error
, NULL
);
255 return log_error_errno(r
, "Failed to reload daemon: %s", bus_error_message(&error
, r
));
260 static int get_image_metadata(sd_bus
*bus
, const char *image
, char **matches
, sd_bus_message
**reply
) {
261 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
262 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
263 PortableFlags flags
= PORTABLE_INSPECT_EXTENSION_RELEASES
;
270 method
= strv_isempty(arg_extension_images
) ? "GetImageMetadata" : "GetImageMetadataWithExtensions";
272 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, method
);
274 return bus_log_create_error(r
);
276 r
= sd_bus_message_append(m
, "s", image
);
278 return bus_log_create_error(r
);
280 r
= attach_extensions_to_message(m
, arg_extension_images
);
284 r
= sd_bus_message_append_strv(m
, matches
);
286 return bus_log_create_error(r
);
288 if (!strv_isempty(arg_extension_images
)) {
289 r
= sd_bus_message_append(m
, "t", flags
);
291 return bus_log_create_error(r
);
294 r
= sd_bus_call(bus
, m
, 0, &error
, reply
);
296 return log_error_errno(r
, "Failed to inspect image metadata: %s", bus_error_message(&error
, r
));
301 static int inspect_image(int argc
, char *argv
[], void *userdata
) {
302 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
303 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
304 _cleanup_strv_free_
char **matches
= NULL
;
305 _cleanup_free_
char *image
= NULL
;
306 bool nl
= false, header
= false;
312 r
= determine_image(argv
[1], false, &image
);
316 r
= determine_matches(argv
[1], argv
+ 2, true, &matches
);
320 r
= acquire_bus(&bus
);
324 r
= get_image_metadata(bus
, image
, matches
, &reply
);
328 r
= sd_bus_message_read(reply
, "s", &path
);
330 return bus_log_parse_error(r
);
332 r
= sd_bus_message_read_array(reply
, 'y', &data
, &sz
);
334 return bus_log_parse_error(r
);
336 pager_open(arg_pager_flags
);
339 printf("%s-- OS Release: --%s\n", ansi_highlight(), ansi_normal());
340 fwrite(data
, sz
, 1, stdout
);
344 _cleanup_free_
char *pretty_portable
= NULL
, *pretty_os
= NULL
;
345 _cleanup_fclose_
FILE *f
= NULL
;
347 f
= fmemopen_unlocked((void*) data
, sz
, "re");
349 return log_error_errno(errno
, "Failed to open /etc/os-release buffer: %m");
351 r
= parse_env_file(f
, "/etc/os-release",
352 "PORTABLE_PRETTY_NAME", &pretty_portable
,
353 "PRETTY_NAME", &pretty_os
);
355 return log_error_errno(r
, "Failed to parse /etc/os-release: %m");
357 printf("Image:\n\t%s\n"
358 "Portable Service:\n\t%s\n"
359 "Operating System:\n\t%s\n",
361 strna(pretty_portable
),
365 r
= sd_bus_message_enter_container(reply
, 'a', "{say}");
367 return bus_log_parse_error(r
);
369 /* If we specified any extensions, we'll first get back exactly the
370 * paths (and extension-release content) for each one of the arguments. */
371 for (size_t i
= 0; i
< strv_length(arg_extension_images
); ++i
) {
374 r
= sd_bus_message_enter_container(reply
, 'e', "say");
376 return bus_log_parse_error(r
);
380 r
= sd_bus_message_read(reply
, "s", &name
);
382 return bus_log_parse_error(r
);
384 r
= sd_bus_message_read_array(reply
, 'y', &data
, &sz
);
386 return bus_log_parse_error(r
);
392 printf("%s-- Extension Release: %s --%s\n", ansi_highlight(), name
, ansi_normal());
393 fwrite(data
, sz
, 1, stdout
);
397 _cleanup_free_
char *pretty_portable
= NULL
, *pretty_os
= NULL
, *sysext_level
= NULL
,
398 *id
= NULL
, *version_id
= NULL
, *sysext_scope
= NULL
, *portable_prefixes
= NULL
;
399 _cleanup_fclose_
FILE *f
= NULL
;
401 f
= fmemopen_unlocked((void*) data
, sz
, "re");
403 return log_error_errno(errno
, "Failed to open extension-release buffer: %m");
405 r
= parse_env_file(f
, name
,
407 "VERSION_ID", &version_id
,
408 "SYSEXT_SCOPE", &sysext_scope
,
409 "SYSEXT_LEVEL", &sysext_level
,
410 "PORTABLE_PRETTY_NAME", &pretty_portable
,
411 "PORTABLE_PREFIXES", &portable_prefixes
,
412 "PRETTY_NAME", &pretty_os
);
414 return log_error_errno(r
, "Failed to parse extension release from '%s': %m", name
);
416 printf("Extension:\n\t%s\n"
417 "\tExtension Scope:\n\t\t%s\n"
418 "\tExtension Compatibility Level:\n\t\t%s\n"
419 "\tPortable Service:\n\t\t%s\n"
420 "\tPortable Prefixes:\n\t\t%s\n"
421 "\tOperating System:\n\t\t%s (%s %s)\n",
425 strna(pretty_portable
),
426 strna(portable_prefixes
),
432 r
= sd_bus_message_exit_container(reply
);
434 return bus_log_parse_error(r
);
440 r
= sd_bus_message_enter_container(reply
, 'e', "say");
442 return bus_log_parse_error(r
);
446 r
= sd_bus_message_read(reply
, "s", &name
);
448 return bus_log_parse_error(r
);
450 r
= sd_bus_message_read_array(reply
, 'y', &data
, &sz
);
452 return bus_log_parse_error(r
);
458 printf("%s-- Unit file: %s --%s\n", ansi_highlight(), name
, ansi_normal());
459 fwrite(data
, sz
, 1, stdout
);
464 fputs("Unit files:\n", stdout
);
473 r
= sd_bus_message_exit_container(reply
);
475 return bus_log_parse_error(r
);
478 r
= sd_bus_message_exit_container(reply
);
480 return bus_log_parse_error(r
);
485 static int print_changes(sd_bus_message
*m
) {
491 r
= sd_bus_message_enter_container(m
, 'a', "(sss)");
493 return bus_log_parse_error(r
);
496 const char *type
, *path
, *source
;
498 r
= sd_bus_message_read(m
, "(sss)", &type
, &path
, &source
);
500 return bus_log_parse_error(r
);
504 if (streq(type
, "symlink"))
505 log_info("Created symlink %s %s %s.", path
, special_glyph(SPECIAL_GLYPH_ARROW
), source
);
506 else if (streq(type
, "copy")) {
508 log_info("Copied %s.", path
);
510 log_info("Copied %s %s %s.", source
, special_glyph(SPECIAL_GLYPH_ARROW
), path
);
511 } else if (streq(type
, "unlink"))
512 log_info("Removed %s.", path
);
513 else if (streq(type
, "write"))
514 log_info("Written %s.", path
);
515 else if (streq(type
, "mkdir"))
516 log_info("Created directory %s.", path
);
518 log_error("Unexpected change: %s/%s/%s", type
, path
, source
);
521 r
= sd_bus_message_exit_container(m
);
528 static int maybe_enable_disable(sd_bus
*bus
, const char *path
, bool enable
) {
529 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
530 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
531 _cleanup_strv_free_
char **names
= NULL
;
532 UnitFileChange
*changes
= NULL
;
533 const uint64_t flags
= UNIT_FILE_PORTABLE
| (arg_runtime
? UNIT_FILE_RUNTIME
: 0);
534 size_t n_changes
= 0;
540 names
= strv_new(path
, NULL
);
544 r
= sd_bus_message_new_method_call(
547 "org.freedesktop.systemd1",
548 "/org/freedesktop/systemd1",
549 "org.freedesktop.systemd1.Manager",
550 enable
? "EnableUnitFilesWithFlags" : "DisableUnitFilesWithFlags");
552 return bus_log_create_error(r
);
554 r
= sd_bus_message_append_strv(m
, names
);
556 return bus_log_create_error(r
);
558 r
= sd_bus_message_append(m
, "t", flags
);
560 return bus_log_create_error(r
);
562 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
564 return log_error_errno(r
, "Failed to %s the portable service %s: %s",
565 enable
? "enable" : "disable", path
, bus_error_message(&error
, r
));
568 r
= sd_bus_message_skip(reply
, "b");
570 return bus_log_parse_error(r
);
572 (void) bus_deserialize_and_dump_unit_file_changes(reply
, arg_quiet
, &changes
, &n_changes
);
577 static int maybe_start_stop_restart(sd_bus
*bus
, const char *path
, const char *method
, BusWaitForJobs
*wait
) {
578 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
579 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
580 char *name
= (char *)basename(path
), *job
= NULL
;
583 assert(STR_IN_SET(method
, "StartUnit", "StopUnit", "RestartUnit"));
588 r
= sd_bus_call_method(
590 "org.freedesktop.systemd1",
591 "/org/freedesktop/systemd1",
592 "org.freedesktop.systemd1.Manager",
596 "ss", name
, "replace");
598 return log_error_errno(r
, "Failed to call %s on the portable service %s: %s",
601 bus_error_message(&error
, r
));
603 r
= sd_bus_message_read(reply
, "o", &job
);
605 return bus_log_parse_error(r
);
608 log_info("Queued %s to call %s on portable service %s.", job
, method
, name
);
611 r
= bus_wait_for_jobs_add(wait
, job
);
613 return log_error_errno(r
, "Failed to watch %s job to call %s on %s: %m",
620 static int maybe_enable_start(sd_bus
*bus
, sd_bus_message
*reply
) {
621 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*wait
= NULL
;
624 if (!arg_enable
&& !arg_now
)
628 r
= bus_wait_for_jobs_new(bus
, &wait
);
630 return log_error_errno(r
, "Could not watch jobs: %m");
633 r
= sd_bus_message_rewind(reply
, true);
636 r
= sd_bus_message_enter_container(reply
, 'a', "(sss)");
638 return bus_log_parse_error(r
);
641 char *type
, *path
, *source
;
643 r
= sd_bus_message_read(reply
, "(sss)", &type
, &path
, &source
);
645 return bus_log_parse_error(r
);
649 if (STR_IN_SET(type
, "symlink", "copy") && is_portable_managed(path
)) {
650 (void) maybe_enable_disable(bus
, path
, true);
651 (void) maybe_start_stop_restart(bus
, path
, "StartUnit", wait
);
655 r
= sd_bus_message_exit_container(reply
);
660 r
= bus_wait_for_jobs(wait
, arg_quiet
, NULL
);
668 static int maybe_stop_enable_restart(sd_bus
*bus
, sd_bus_message
*reply
) {
669 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*wait
= NULL
;
672 if (!arg_enable
&& !arg_now
)
676 r
= bus_wait_for_jobs_new(bus
, &wait
);
678 return log_error_errno(r
, "Could not watch jobs: %m");
681 r
= sd_bus_message_rewind(reply
, true);
685 /* First we get a list of units that were definitely removed, not just re-attached,
686 * so we can also stop them if the user asked us to. */
687 r
= sd_bus_message_enter_container(reply
, 'a', "(sss)");
689 return bus_log_parse_error(r
);
692 char *type
, *path
, *source
;
694 r
= sd_bus_message_read(reply
, "(sss)", &type
, &path
, &source
);
696 return bus_log_parse_error(r
);
700 if (streq(type
, "unlink") && is_portable_managed(path
))
701 (void) maybe_start_stop_restart(bus
, path
, "StopUnit", wait
);
704 r
= sd_bus_message_exit_container(reply
);
708 /* Then we get a list of units that were either added or changed, so that we can
709 * enable them and/or restart them if the user asked us to. */
710 r
= sd_bus_message_enter_container(reply
, 'a', "(sss)");
712 return bus_log_parse_error(r
);
715 char *type
, *path
, *source
;
717 r
= sd_bus_message_read(reply
, "(sss)", &type
, &path
, &source
);
719 return bus_log_parse_error(r
);
723 if (STR_IN_SET(type
, "symlink", "copy") && is_portable_managed(path
)) {
724 (void) maybe_enable_disable(bus
, path
, true);
725 (void) maybe_start_stop_restart(bus
, path
, "RestartUnit", wait
);
729 r
= sd_bus_message_exit_container(reply
);
734 r
= bus_wait_for_jobs(wait
, arg_quiet
, NULL
);
742 static int maybe_stop_disable(sd_bus
*bus
, char *image
, char *argv
[]) {
743 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*wait
= NULL
;
744 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
745 _cleanup_strv_free_
char **matches
= NULL
;
748 if (!arg_enable
&& !arg_now
)
751 r
= determine_matches(argv
[1], argv
+ 2, true, &matches
);
755 r
= bus_wait_for_jobs_new(bus
, &wait
);
757 return log_error_errno(r
, "Could not watch jobs: %m");
759 r
= get_image_metadata(bus
, image
, matches
, &reply
);
763 r
= sd_bus_message_skip(reply
, "say");
765 return bus_log_parse_error(r
);
767 r
= sd_bus_message_enter_container(reply
, 'a', "{say}");
769 return bus_log_parse_error(r
);
771 /* If we specified any extensions, we'll first get back exactly the
772 * paths (and extension-release content) for each one of the arguments. */
773 for (size_t i
= 0; i
< strv_length(arg_extension_images
); ++i
) {
774 r
= sd_bus_message_skip(reply
, "{say}");
776 return bus_log_parse_error(r
);
782 r
= sd_bus_message_enter_container(reply
, 'e', "say");
784 return bus_log_parse_error(r
);
788 r
= sd_bus_message_read(reply
, "s", &name
);
790 return bus_log_parse_error(r
);
792 r
= sd_bus_message_skip(reply
, "ay");
794 return bus_log_parse_error(r
);
796 r
= sd_bus_message_exit_container(reply
);
798 return bus_log_parse_error(r
);
800 (void) maybe_start_stop_restart(bus
, name
, "StopUnit", wait
);
801 (void) maybe_enable_disable(bus
, name
, false);
804 r
= sd_bus_message_exit_container(reply
);
806 return bus_log_parse_error(r
);
808 /* Stopping must always block or the detach will fail if the unit is still running */
809 r
= bus_wait_for_jobs(wait
, arg_quiet
, NULL
);
816 static int attach_reattach_image(int argc
, char *argv
[], const char *method
) {
817 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
818 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
819 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
820 _cleanup_strv_free_
char **matches
= NULL
;
821 _cleanup_free_
char *image
= NULL
;
825 assert(STR_IN_SET(method
, "AttachImage", "ReattachImage", "AttachImageWithExtensions", "ReattachImageWithExtensions"));
827 r
= determine_image(argv
[1], false, &image
);
831 r
= determine_matches(argv
[1], argv
+ 2, false, &matches
);
835 r
= acquire_bus(&bus
);
839 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
841 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, method
);
843 return bus_log_create_error(r
);
845 r
= sd_bus_message_append(m
, "s", image
);
847 return bus_log_create_error(r
);
849 r
= attach_extensions_to_message(m
, arg_extension_images
);
853 r
= sd_bus_message_append_strv(m
, matches
);
855 return bus_log_create_error(r
);
857 r
= sd_bus_message_append(m
, "s", arg_profile
);
859 return bus_log_create_error(r
);
861 if (STR_IN_SET(method
, "AttachImageWithExtensions", "ReattachImageWithExtensions")) {
862 uint64_t flags
= arg_runtime
? PORTABLE_RUNTIME
: 0;
864 r
= sd_bus_message_append(m
, "st", arg_copy_mode
, flags
);
866 r
= sd_bus_message_append(m
, "bs", arg_runtime
, arg_copy_mode
);
868 return bus_log_create_error(r
);
870 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
872 return log_error_errno(r
, "%s failed: %s", method
, bus_error_message(&error
, r
));
874 (void) maybe_reload(&bus
);
876 print_changes(reply
);
878 if (STR_IN_SET(method
, "AttachImage", "AttachImageWithExtensions"))
879 (void) maybe_enable_start(bus
, reply
);
881 /* ReattachImage returns 2 lists - removed units first, and changed/added second */
882 print_changes(reply
);
883 (void) maybe_stop_enable_restart(bus
, reply
);
889 static int attach_image(int argc
, char *argv
[], void *userdata
) {
890 return attach_reattach_image(argc
, argv
, strv_isempty(arg_extension_images
) ? "AttachImage" : "AttachImageWithExtensions");
893 static int reattach_image(int argc
, char *argv
[], void *userdata
) {
894 return attach_reattach_image(argc
, argv
, strv_isempty(arg_extension_images
) ? "ReattachImage" : "ReattachImageWithExtensions");
897 static int detach_image(int argc
, char *argv
[], void *userdata
) {
898 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
899 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
900 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
901 _cleanup_free_
char *image
= NULL
;
905 r
= determine_image(argv
[1], true, &image
);
909 r
= acquire_bus(&bus
);
913 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
915 (void) maybe_stop_disable(bus
, image
, argv
);
917 method
= strv_isempty(arg_extension_images
) ? "DetachImage" : "DetachImageWithExtensions";
919 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, method
);
921 return bus_log_create_error(r
);
923 r
= sd_bus_message_append(m
, "s", image
);
925 return bus_log_create_error(r
);
927 r
= attach_extensions_to_message(m
, arg_extension_images
);
931 if (!strv_isempty(arg_extension_images
)) {
932 uint64_t flags
= arg_runtime
? PORTABLE_RUNTIME
: 0;
934 r
= sd_bus_message_append(m
, "t", flags
);
936 r
= sd_bus_message_append(m
, "b", arg_runtime
);
938 return bus_log_create_error(r
);
940 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
942 return log_error_errno(r
, "%s failed: %s", method
, bus_error_message(&error
, r
));
944 (void) maybe_reload(&bus
);
946 print_changes(reply
);
950 static int list_images(int argc
, char *argv
[], void *userdata
) {
951 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
952 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
953 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
954 _cleanup_(table_unrefp
) Table
*table
= NULL
;
957 r
= acquire_bus(&bus
);
961 r
= bus_call_method(bus
, bus_portable_mgr
, "ListImages", &error
, &reply
, NULL
);
963 return log_error_errno(r
, "Failed to list images: %s", bus_error_message(&error
, r
));
965 table
= table_new("name", "type", "ro", "crtime", "mtime", "usage", "state");
969 r
= sd_bus_message_enter_container(reply
, 'a', "(ssbtttso)");
971 return bus_log_parse_error(r
);
974 const char *name
, *type
, *state
;
975 uint64_t crtime
, mtime
, usage
;
978 r
= sd_bus_message_read(reply
, "(ssbtttso)", &name
, &type
, &ro_int
, &crtime
, &mtime
, &usage
, &state
, NULL
);
980 return bus_log_parse_error(r
);
984 r
= table_add_many(table
,
987 TABLE_BOOLEAN
, ro_int
,
988 TABLE_SET_COLOR
, ro_int
? ansi_highlight_red() : NULL
,
989 TABLE_TIMESTAMP
, crtime
,
990 TABLE_TIMESTAMP
, mtime
,
993 TABLE_SET_COLOR
, !streq(state
, "detached") ? ansi_highlight_green() : NULL
);
995 return table_log_add_error(r
);
998 r
= sd_bus_message_exit_container(reply
);
1000 return bus_log_parse_error(r
);
1002 if (table_get_rows(table
) > 1) {
1003 r
= table_set_sort(table
, (size_t) 0);
1005 return table_log_sort_error(r
);
1007 table_set_header(table
, arg_legend
);
1009 r
= table_print(table
, NULL
);
1011 return table_log_print_error(r
);
1015 if (table_get_rows(table
) > 1)
1016 printf("\n%zu images listed.\n", table_get_rows(table
) - 1);
1018 printf("No images.\n");
1024 static int remove_image(int argc
, char *argv
[], void *userdata
) {
1025 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1028 r
= acquire_bus(&bus
);
1032 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1034 for (i
= 1; i
< argc
; i
++) {
1035 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1036 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1038 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, "RemoveImage");
1040 return bus_log_create_error(r
);
1042 r
= sd_bus_message_append(m
, "s", argv
[i
]);
1044 return bus_log_create_error(r
);
1046 /* This is a slow operation, hence turn off any method call timeouts */
1047 r
= sd_bus_call(bus
, m
, USEC_INFINITY
, &error
, NULL
);
1049 return log_error_errno(r
, "Could not remove image: %s", bus_error_message(&error
, r
));
1055 static int read_only_image(int argc
, char *argv
[], void *userdata
) {
1056 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1057 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1061 b
= parse_boolean(argv
[2]);
1063 return log_error_errno(b
, "Failed to parse boolean argument: %s", argv
[2]);
1066 r
= acquire_bus(&bus
);
1070 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1072 r
= bus_call_method(bus
, bus_portable_mgr
, "MarkImageReadOnly", &error
, NULL
, "sb", argv
[1], b
);
1074 return log_error_errno(r
, "Could not mark image read-only: %s", bus_error_message(&error
, r
));
1079 static int set_limit(int argc
, char *argv
[], void *userdata
) {
1080 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1081 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1085 r
= acquire_bus(&bus
);
1089 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1091 if (STR_IN_SET(argv
[argc
-1], "-", "none", "infinity"))
1094 r
= parse_size(argv
[argc
-1], 1024, &limit
);
1096 return log_error_errno(r
, "Failed to parse size: %s", argv
[argc
-1]);
1100 /* With two arguments changes the quota limit of the specified image */
1101 r
= bus_call_method(bus
, bus_portable_mgr
, "SetImageLimit", &error
, NULL
, "st", argv
[1], limit
);
1103 /* With one argument changes the pool quota limit */
1104 r
= bus_call_method(bus
, bus_portable_mgr
, "SetPoolLimit", &error
, NULL
, "t", limit
);
1107 return log_error_errno(r
, "Could not set limit: %s", bus_error_message(&error
, r
));
1112 static int is_image_attached(int argc
, char *argv
[], void *userdata
) {
1113 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1114 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1115 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1116 _cleanup_free_
char *image
= NULL
;
1117 const char *state
, *method
;
1120 r
= determine_image(argv
[1], true, &image
);
1124 r
= acquire_bus(&bus
);
1128 method
= strv_isempty(arg_extension_images
) ? "GetImageState" : "GetImageStateWithExtensions";
1130 r
= bus_message_new_method_call(bus
, &m
, bus_portable_mgr
, method
);
1132 return bus_log_create_error(r
);
1134 r
= sd_bus_message_append(m
, "s", image
);
1136 return bus_log_create_error(r
);
1138 r
= attach_extensions_to_message(m
, arg_extension_images
);
1142 if (!strv_isempty(arg_extension_images
)) {
1143 r
= sd_bus_message_append(m
, "t", 0);
1145 return bus_log_create_error(r
);
1148 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1150 return log_error_errno(r
, "%s failed: %s", method
, bus_error_message(&error
, r
));
1152 r
= sd_bus_message_read(reply
, "s", &state
);
1159 return streq(state
, "detached");
1162 static int dump_profiles(void) {
1163 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1164 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1165 _cleanup_strv_free_
char **l
= NULL
;
1169 r
= acquire_bus(&bus
);
1173 r
= bus_get_property_strv(bus
, bus_portable_mgr
, "Profiles", &error
, &l
);
1175 return log_error_errno(r
, "Failed to acquire list of profiles: %s", bus_error_message(&error
, r
));
1178 log_info("Available unit profiles:");
1180 STRV_FOREACH(i
, l
) {
1182 fputc('\n', stdout
);
1188 static int help(int argc
, char *argv
[], void *userdata
) {
1189 _cleanup_free_
char *link
= NULL
;
1192 pager_open(arg_pager_flags
);
1194 r
= terminal_urlify_man("portablectl", "1", &link
);
1198 printf("%s [OPTIONS...] COMMAND ...\n\n"
1199 "%sAttach or detach portable services from the local system.%s\n"
1201 " list List available portable service images\n"
1202 " attach NAME|PATH [PREFIX...]\n"
1203 " Attach the specified portable service image\n"
1204 " detach NAME|PATH [PREFIX...]\n"
1205 " Detach the specified portable service image\n"
1206 " reattach NAME|PATH [PREFIX...]\n"
1207 " Reattach the specified portable service image\n"
1208 " inspect NAME|PATH [PREFIX...]\n"
1209 " Show details of specified portable service image\n"
1210 " is-attached NAME|PATH Query if portable service image is attached\n"
1211 " read-only NAME|PATH [BOOL] Mark or unmark portable service image read-only\n"
1212 " remove NAME|PATH... Remove a portable service image\n"
1213 " set-limit [NAME|PATH] Set image or pool size limit (disk quota)\n"
1215 " -h --help Show this help\n"
1216 " --version Show package version\n"
1217 " --no-pager Do not pipe output into a pager\n"
1218 " --no-legend Do not show the headers and footers\n"
1219 " --no-ask-password Do not ask for system passwords\n"
1220 " -H --host=[USER@]HOST Operate on remote host\n"
1221 " -M --machine=CONTAINER Operate on local container\n"
1222 " -q --quiet Suppress informational messages\n"
1223 " -p --profile=PROFILE Pick security profile for portable service\n"
1224 " --copy=copy|auto|symlink Prefer copying or symlinks if possible\n"
1225 " --runtime Attach portable service until next reboot only\n"
1226 " --no-reload Don't reload the system and service manager\n"
1227 " --cat When inspecting include unit and os-release file\n"
1229 " --enable Immediately enable/disable the portable service\n"
1230 " after attach/detach\n"
1231 " --now Immediately start/stop the portable service after\n"
1232 " attach/before detach\n"
1233 " --no-block Don't block waiting for attach --now to complete\n"
1234 " --extension=PATH Extend the image with an overlay\n"
1235 "\nSee the %s for details.\n",
1236 program_invocation_short_name
,
1244 static int parse_argv(int argc
, char *argv
[]) {
1248 ARG_VERSION
= 0x100,
1251 ARG_NO_ASK_PASSWORD
,
1262 static const struct option options
[] = {
1263 { "help", no_argument
, NULL
, 'h' },
1264 { "version", no_argument
, NULL
, ARG_VERSION
},
1265 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
1266 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
1267 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
1268 { "host", required_argument
, NULL
, 'H' },
1269 { "machine", required_argument
, NULL
, 'M' },
1270 { "quiet", no_argument
, NULL
, 'q' },
1271 { "profile", required_argument
, NULL
, 'p' },
1272 { "copy", required_argument
, NULL
, ARG_COPY
},
1273 { "runtime", no_argument
, NULL
, ARG_RUNTIME
},
1274 { "no-reload", no_argument
, NULL
, ARG_NO_RELOAD
},
1275 { "cat", no_argument
, NULL
, ARG_CAT
},
1276 { "enable", no_argument
, NULL
, ARG_ENABLE
},
1277 { "now", no_argument
, NULL
, ARG_NOW
},
1278 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
1279 { "extension", required_argument
, NULL
, ARG_EXTENSION
},
1289 c
= getopt_long(argc
, argv
, "hH:M:qp:", options
, NULL
);
1296 return help(0, NULL
, NULL
);
1302 arg_pager_flags
|= PAGER_DISABLE
;
1309 case ARG_NO_ASK_PASSWORD
:
1310 arg_ask_password
= false;
1314 arg_transport
= BUS_TRANSPORT_REMOTE
;
1319 arg_transport
= BUS_TRANSPORT_MACHINE
;
1328 if (streq(optarg
, "help"))
1329 return dump_profiles();
1331 if (!filename_is_valid(optarg
))
1332 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1333 "Unit profile name not valid: %s", optarg
);
1335 arg_profile
= optarg
;
1339 if (streq(optarg
, "auto"))
1340 arg_copy_mode
= NULL
;
1341 else if (STR_IN_SET(optarg
, "copy", "symlink"))
1342 arg_copy_mode
= optarg
;
1343 else if (streq(optarg
, "help")) {
1349 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1350 "Failed to parse --copy= argument: %s", optarg
);
1375 arg_no_block
= true;
1379 r
= strv_extend(&arg_extension_images
, optarg
);
1388 assert_not_reached();
1395 static int run(int argc
, char *argv
[]) {
1396 static const Verb verbs
[] = {
1397 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
1398 { "list", VERB_ANY
, 1, VERB_DEFAULT
, list_images
},
1399 { "attach", 2, VERB_ANY
, 0, attach_image
},
1400 { "detach", 2, VERB_ANY
, 0, detach_image
},
1401 { "inspect", 2, VERB_ANY
, 0, inspect_image
},
1402 { "is-attached", 2, 2, 0, is_image_attached
},
1403 { "read-only", 2, 3, 0, read_only_image
},
1404 { "remove", 2, VERB_ANY
, 0, remove_image
},
1405 { "set-limit", 3, 3, 0, set_limit
},
1406 { "reattach", 2, VERB_ANY
, 0, reattach_image
},
1414 r
= parse_argv(argc
, argv
);
1418 return dispatch_verb(argc
, argv
, verbs
, NULL
);
1421 DEFINE_MAIN_FUNCTION(run
);