HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/portable/portablectl.c

63,027 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 682e151314daf5651f5d01626fb2032d532b0dd0

Lines 1001–1709 of 1,709

        (void) maybe_start_stop_restart_units(bus, start_names, "start", wait);
 
        if (!arg_no_block) {
                r = bus_wait_for_jobs(wait, arg_quiet, NULL);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static int maybe_stop_enable_restart(sd_bus *bus, sd_bus_message *reply) {
        _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *wait = NULL;
        _cleanup_strv_free_ char **stop_names = NULL, **restart_names = NULL;
        int r;
 
        if (!arg_enable && !arg_now)
                return 0;
 
        if (!arg_no_block) {
                r = bus_wait_for_jobs_new(bus, &wait);
                if (r < 0)
                        return log_error_errno(r, "Could not watch jobs: %m");
        }
 
        r = sd_bus_message_rewind(reply, true);
        if (r < 0)
                return r;
 
        /* First we get a list of units that were definitely removed, not just re-attached,
         * so we can also stop them if the user asked us to. */
        r = sd_bus_message_enter_container(reply, 'a', "(sss)");
        if (r < 0)
                return bus_log_parse_error(r);
 
        for (;;) {
                char *type, *path, *source;
 
                r = sd_bus_message_read(reply, "(sss)", &type, &path, &source);
                if (r < 0)
                        return bus_log_parse_error(r);
                if (r == 0)
                        break;
 
                if (streq(type, "unlink") && is_portable_managed(path) && arg_now) {
                        _cleanup_free_ char *name = NULL;
 
                        r = path_extract_filename(path, &name);
                        if (r < 0)
                                return log_error_errno(r, "Failed to extract file name from '%s': %m", path);
 
                        r = strv_consume(&stop_names, TAKE_PTR(name));
                        if (r < 0)
                                return log_oom();
                }
        }
 
        r = sd_bus_message_exit_container(reply);
        if (r < 0)
                return r;
 
        (void) maybe_start_stop_restart_units(bus, stop_names, "stop", wait);
 
        /* Then we get a list of units that were either added or changed, so that we can
         * enable them and/or restart them if the user asked us to. */
        r = sd_bus_message_enter_container(reply, 'a', "(sss)");
        if (r < 0)
                return bus_log_parse_error(r);
 
        for (;;) {
                char *type, *path, *source;
 
                r = sd_bus_message_read(reply, "(sss)", &type, &path, &source);
                if (r < 0)
                        return bus_log_parse_error(r);
                if (r == 0)
                        break;
 
                if (STR_IN_SET(type, "symlink", "copy") && is_portable_managed(path)) {
                        (void) maybe_enable_disable(bus, path, true);
 
                        _cleanup_free_ char *name = NULL;
                        r = path_extract_filename(path, &name);
                        if (r < 0)
                                return log_error_errno(r, "Failed to extract file name from '%s': %m", path);
 
                        r = strv_consume(&restart_names, TAKE_PTR(name));
                        if (r < 0)
                                return log_oom();
                }
        }
 
        r = sd_bus_message_exit_container(reply);
        if (r < 0)
                return r;
 
        (void) maybe_start_stop_restart_units(bus, restart_names, "restart", wait);
 
        if (!arg_no_block) {
                r = bus_wait_for_jobs(wait, arg_quiet, NULL);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static int maybe_clean_units(sd_bus *bus, char **units) {
        int r;
 
        assert(bus);
 
        if (!arg_clean)
                return 0;
 
        STRV_FOREACH(name, units) {
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
                _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
 
                r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "CleanUnit");
                if (r < 0)
                        return bus_log_create_error(r);
 
                r = sd_bus_message_append(m, "s", *name);
                if (r < 0)
                        return bus_log_create_error(r);
 
                r = sd_bus_message_append_strv(m, STRV_MAKE("all", "fdstore"));
                if (r < 0)
                        return bus_log_create_error(r);
 
                r = sd_bus_call(bus, m, 0, &error, NULL);
                if (r < 0)
                        return log_error_errno(
                                        r,
                                        "Failed to call CleanUnit on portable service %s: %s",
                                        *name,
                                        bus_error_message(&error, r));
        }
 
        return 0;
}
 
static int maybe_stop_disable_clean(sd_bus *bus, char *image, char *argv[]) {
        _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *wait = NULL;
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        _cleanup_strv_free_ char **matches = NULL, **units = NULL;
        int r;
 
        if (!arg_enable && !arg_now && !arg_clean)
                return 0;
 
        r = determine_matches(argv[1], argv + 2, true, &matches);
        if (r < 0)
                return r;
 
        r = bus_wait_for_jobs_new(bus, &wait);
        if (r < 0)
                return log_error_errno(r, "Could not watch jobs: %m");
 
        r = get_image_metadata(bus, image, matches, &reply);
        if (r < 0)
                return r;
 
        r = sd_bus_message_skip(reply, "say");
        if (r < 0)
                return bus_log_parse_error(r);
 
        /* If we specified any extensions or --force (which makes the request go through the new
         * WithExtensions calls), we'll first get an array of extension-release metadata. */
        if (!strv_isempty(arg_extension_images) || arg_force) {
                r = sd_bus_message_skip(reply, "a{say}");
                if (r < 0)
                        return bus_log_parse_error(r);
        }
 
        r = sd_bus_message_enter_container(reply, 'a', "{say}");
        if (r < 0)
                return bus_log_parse_error(r);
 
        for (;;) {
                const char *name;
 
                r = sd_bus_message_enter_container(reply, 'e', "say");
                if (r < 0)
                        return bus_log_parse_error(r);
                if (r == 0)
                        break;
 
                r = sd_bus_message_read(reply, "s", &name);
                if (r < 0)
                        return bus_log_parse_error(r);
 
                r = sd_bus_message_skip(reply, "ay");
                if (r < 0)
                        return bus_log_parse_error(r);
 
                r = sd_bus_message_exit_container(reply);
                if (r < 0)
                        return bus_log_parse_error(r);
 
                r = strv_extend(&units, name);
                if (r < 0)
                        return log_oom();
        }
 
        r = sd_bus_message_exit_container(reply);
        if (r < 0)
                return bus_log_parse_error(r);
 
        (void) maybe_start_stop_restart_units(bus, units, "stop", wait);
 
        /* Disable after stopping to match the idiomatic stop-then-disable lifecycle order. */
        STRV_FOREACH(name, units)
                (void) maybe_enable_disable(bus, *name, false);
 
        /* Stopping must always block or the detach will fail if the unit is still running */
        r = bus_wait_for_jobs(wait, arg_quiet, NULL);
        if (r < 0)
                return r;
 
        /* Need to ensure all units are stopped before calling CleanUnit, as files might be in use. */
        (void) maybe_clean_units(bus, units);
 
        return 0;
}
 
static int attach_reattach_image(int argc, char *argv[], const char *method) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
        _cleanup_strv_free_ char **matches = NULL;
        _cleanup_free_ char *image = NULL;
        int r;
 
        assert(method);
        assert(STR_IN_SET(method, "AttachImage", "ReattachImage", "AttachImageWithExtensions", "ReattachImageWithExtensions"));
 
        r = determine_image(argv[1], false, &image);
        if (r < 0)
                return r;
 
        r = determine_matches(argv[1], argv + 2, false, &matches);
        if (r < 0)
                return r;
 
        r = acquire_bus(&bus);
        if (r < 0)
                return r;
 
        (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
 
        r = bus_message_new_method_call(bus, &m, bus_portable_mgr, method);
        if (r < 0)
                return bus_log_create_error(r);
 
        r = sd_bus_message_append(m, "s", image);
        if (r < 0)
                return bus_log_create_error(r);
 
        r = attach_extensions_to_message(m, method, arg_extension_images);
        if (r < 0)
                return r;
 
        r = sd_bus_message_append_strv(m, matches);
        if (r < 0)
                return bus_log_create_error(r);
 
        r = sd_bus_message_append(m, "s", arg_profile);
        if (r < 0)
                return bus_log_create_error(r);
 
        if (STR_IN_SET(method, "AttachImageWithExtensions", "ReattachImageWithExtensions")) {
                uint64_t flags = (arg_runtime ? PORTABLE_RUNTIME : 0) | (arg_force ? PORTABLE_FORCE_ATTACH | PORTABLE_FORCE_EXTENSION : 0);
 
                r = sd_bus_message_append(m, "st", arg_copy_mode, flags);
        } else
                r = sd_bus_message_append(m, "bs", arg_runtime, arg_copy_mode);
        if (r < 0)
                return bus_log_create_error(r);
 
        r = sd_bus_call(bus, m, 0, &error, &reply);
        if (r < 0)
                return log_error_errno(r, "%s failed: %s", method, bus_error_message(&error, r));
 
        (void) maybe_reload(&bus);
 
        print_changes(reply);
 
        if (STR_IN_SET(method, "AttachImage", "AttachImageWithExtensions"))
                (void) maybe_enable_start(bus, reply);
        else {
                /* ReattachImage returns 2 lists - removed units first, and changed/added second */
                print_changes(reply);
                (void) maybe_stop_enable_restart(bus, reply);
        }
 
        return 0;
}
 
VERB(verb_attach_image, "attach", "NAME|PATH [PREFIX…]\0", 2, VERB_ANY, 0,
     "Attach the specified portable service image");
static int verb_attach_image(int argc, char *argv[], uintptr_t _data, void *userdata) {
        return attach_reattach_image(argc, argv, strv_isempty(arg_extension_images) && !arg_force ? "AttachImage" : "AttachImageWithExtensions");
}
 
VERB(verb_detach_image, "detach", "NAME|PATH [PREFIX…]\0", 2, VERB_ANY, 0,
     "Detach the specified portable service image");
static int verb_detach_image(int argc, char *argv[], uintptr_t _data, void *userdata) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
        _cleanup_free_ char *image = NULL;
        const char *method;
        int r;
 
        r = determine_image(argv[1], true, &image);
        if (r < 0)
                return r;
 
        r = acquire_bus(&bus);
        if (r < 0)
                return r;
 
        (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
 
        (void) maybe_stop_disable_clean(bus, image, argv);
 
        method = strv_isempty(arg_extension_images) && !arg_force ? "DetachImage" : "DetachImageWithExtensions";
 
        r = bus_message_new_method_call(bus, &m, bus_portable_mgr, method);
        if (r < 0)
                return bus_log_create_error(r);
 
        r = sd_bus_message_append(m, "s", image);
        if (r < 0)
                return bus_log_create_error(r);
 
        r = attach_extensions_to_message(m, method, arg_extension_images);
        if (r < 0)
                return r;
 
        if (streq(method, "DetachImage"))
                r = sd_bus_message_append(m, "b", arg_runtime);
        else {
                uint64_t flags = (arg_runtime ? PORTABLE_RUNTIME : 0) | (arg_force ? PORTABLE_FORCE_ATTACH | PORTABLE_FORCE_EXTENSION : 0);
 
                r = sd_bus_message_append(m, "t", flags);
        }
        if (r < 0)
                return bus_log_create_error(r);
 
        r = sd_bus_call(bus, m, 0, &error, &reply);
        if (r < 0)
                return log_error_errno(r, "%s failed: %s", method, bus_error_message(&error, r));
 
        (void) maybe_reload(&bus);
 
        print_changes(reply);
        return 0;
}
 
VERB(verb_reattach_image, "reattach", "NAME|PATH [PREFIX…]\0", 2, VERB_ANY, 0,
     "Reattach the specified portable service image");
static int verb_reattach_image(int argc, char *argv[], uintptr_t _data, void *userdata) {
        return attach_reattach_image(argc, argv, strv_isempty(arg_extension_images) && !arg_force ? "ReattachImage" : "ReattachImageWithExtensions");
}
 
VERB(verb_is_image_attached, "is-attached", "NAME|PATH\0", 2, 2, 0,
     "Query if portable service image is attached");
static int verb_is_image_attached(int argc, char *argv[], uintptr_t _data, void *userdata) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
        _cleanup_free_ char *image = NULL;
        const char *state, *method;
        int r;
 
        r = determine_image(argv[1], true, &image);
        if (r < 0)
                return r;
 
        r = acquire_bus(&bus);
        if (r < 0)
                return r;
 
        method = strv_isempty(arg_extension_images) ? "GetImageState" : "GetImageStateWithExtensions";
 
        r = bus_message_new_method_call(bus, &m, bus_portable_mgr, method);
        if (r < 0)
                return bus_log_create_error(r);
 
        r = sd_bus_message_append(m, "s", image);
        if (r < 0)
                return bus_log_create_error(r);
 
        r = attach_extensions_to_message(m, method, arg_extension_images);
        if (r < 0)
                return r;
 
        if (!strv_isempty(arg_extension_images)) {
                r = sd_bus_message_append(m, "t", UINT64_C(0));
                if (r < 0)
                        return bus_log_create_error(r);
        }
 
        r = sd_bus_call(bus, m, 0, &error, &reply);
        if (r < 0)
                return log_error_errno(r, "%s failed: %s", method, bus_error_message(&error, r));
 
        r = sd_bus_message_read(reply, "s", &state);
        if (r < 0)
                return r;
 
        if (!arg_quiet)
                puts(state);
 
        return streq(state, "detached");
}
 
VERB(verb_read_only_image, "read-only", "NAME|PATH [BOOL]\0", 2, 3, 0,
     "Mark or unmark portable service image read-only");
static int verb_read_only_image(int argc, char *argv[], uintptr_t _data, void *userdata) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
        _cleanup_free_ char *image = NULL;
        int b = true, r;
 
        if (argc > 2) {
                b = parse_boolean(argv[2]);
                if (b < 0)
                        return log_error_errno(b, "Failed to parse boolean argument: %s", argv[2]);
        }
 
        r = determine_image(argv[1], false, &image);
        if (r < 0)
                return r;
 
        r = acquire_bus(&bus);
        if (r < 0)
                return r;
 
        (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
 
        r = bus_call_method(bus, bus_portable_mgr, "MarkImageReadOnly", &error, NULL, "sb", image, b);
        if (r < 0)
                return log_error_errno(r, "Could not mark image read-only: %s", bus_error_message(&error, r));
 
        return 0;
}
 
VERB(verb_remove_image, "remove", "NAME|PATH…\0", 2, VERB_ANY, 0,
     "Remove a portable service image");
static int verb_remove_image(int argc, char *argv[], uintptr_t _data, void *userdata) {
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
        int r, i;
 
        r = acquire_bus(&bus);
        if (r < 0)
                return r;
 
        (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
 
        for (i = 1; i < argc; i++) {
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
                _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
                _cleanup_free_ char *image = NULL;
 
                r = determine_image(argv[i], false, &image);
                if (r < 0)
                        return r;
 
                r = bus_message_new_method_call(bus, &m, bus_portable_mgr, "RemoveImage");
                if (r < 0)
                        return bus_log_create_error(r);
 
                r = sd_bus_message_append(m, "s", image);
                if (r < 0)
                        return bus_log_create_error(r);
 
                /* This is a slow operation, hence turn off any method call timeouts */
                r = sd_bus_call(bus, m, USEC_INFINITY, &error, NULL);
                if (r < 0)
                        return log_error_errno(r, "Could not remove image: %s", bus_error_message(&error, r));
        }
 
        return 0;
}
 
VERB(verb_set_limit, "set-limit", "[NAME|PATH] LIMIT\0", 2, 3, 0,
     "Set image or pool size limit (disk quota)");
static int verb_set_limit(int argc, char *argv[], uintptr_t _data, void *userdata) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
        _cleanup_free_ char *image = NULL;
        uint64_t limit;
        int r;
 
        r = acquire_bus(&bus);
        if (r < 0)
                return r;
 
        (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
 
        if (STR_IN_SET(argv[argc-1], "-", "none", "infinity"))
                limit = UINT64_MAX;
        else {
                r = parse_size(argv[argc-1], 1024, &limit);
                if (r < 0)
                        return log_error_errno(r, "Failed to parse size: %s", argv[argc-1]);
        }
 
        if (argc > 2) {
                /* With two arguments changes the quota limit of the specified image */
                r = determine_image(argv[1], false, &image);
                if (r < 0)
                        return r;
 
                r = bus_call_method(bus, bus_portable_mgr, "SetImageLimit", &error, NULL, "st", image, limit);
        } else
                /* With one argument changes the pool quota limit */
                r = bus_call_method(bus, bus_portable_mgr, "SetPoolLimit", &error, NULL, "t", limit);
 
        if (r < 0)
                return log_error_errno(r, "Could not set limit: %s", bus_error_message(&error, r));
 
        return 0;
}
 
static int dump_profiles(void) {
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
        _cleanup_strv_free_ char **l = NULL;
        int r;
 
        r = acquire_bus(&bus);
        if (r < 0)
                return r;
 
        r = bus_get_property_strv(bus, bus_portable_mgr, "Profiles", &error, &l);
        if (r < 0)
                return log_error_errno(r, "Failed to acquire list of profiles: %s", bus_error_message(&error, r));
 
        if (arg_legend)
                log_info("Available unit profiles:");
 
        STRV_FOREACH(i, l) {
                fputs(*i, stdout);
                fputc('\n', stdout);
        }
 
        return 0;
}
 
VERB_COMMON_HELP_AUTO_HIDDEN();
 
static int parse_argv(int argc, char *argv[], char ***remaining_args) {
        assert(argc >= 0);
        assert(argv);
        assert(remaining_args);
 
        OptionParser opts = { argc, argv };
        int r;
 
        FOREACH_OPTION_OR_RETURN(c, &opts)
                switch (c) {
 
                OPTION_COMMON_HELP:
                        return command_print_help();
 
                OPTION_COMMON_VERSION:
                        return version();
 
                OPTION_COMMON_NO_PAGER:
                        arg_pager_flags |= PAGER_DISABLE;
                        break;
 
                OPTION_COMMON_NO_LEGEND:
                        arg_legend = false;
                        break;
 
                OPTION_COMMON_NO_ASK_PASSWORD:
                        arg_ask_password = false;
                        break;
 
                OPTION_COMMON_HOST:
                        arg_transport = BUS_TRANSPORT_REMOTE;
                        arg_host = opts.arg;
                        break;
 
                OPTION_COMMON_MACHINE:
                        r = parse_machine_argument(opts.arg, &arg_host, &arg_transport);
                        if (r < 0)
                                return r;
                        break;
 
                OPTION('q', "quiet", NULL, "Suppress informational messages"):
                        arg_quiet = true;
                        break;
 
                OPTION('p', "profile", "PROFILE",
                       "Pick security profile for portable service"):
                        if (streq(opts.arg, "help"))
                                return dump_profiles();
 
                        if (!filename_is_valid(opts.arg))
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                       "Profile name '%s' is not valid.", opts.arg);
 
                        arg_profile = opts.arg;
                        break;
 
                OPTION_LONG("copy", "MODE",
                            "Pick copying or symlinking of resources "
                            "(copy, auto, symlink, mixed)"):
                        if (streq(opts.arg, "auto"))
                                arg_copy_mode = NULL;
                        else if (STR_IN_SET(opts.arg, "copy", "symlink", "mixed"))
                                arg_copy_mode = opts.arg;
                        else if (streq(opts.arg, "help")) {
                                puts("auto\n"
                                     "copy\n"
                                     "symlink\n"
                                     "mixed\n");
                                return 0;
                        } else
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                       "Failed to parse --copy= argument: %s", opts.arg);
                        break;
 
                OPTION_LONG("runtime", NULL,
                            "Attach portable service until next reboot only"):
                        arg_runtime = true;
                        break;
 
                OPTION_LONG("no-reload", NULL,
                            "Don't reload the system and service manager"):
                        arg_reload = false;
                        break;
 
                OPTION_LONG("cat", NULL,
                            "When inspecting include unit and os-release file contents"):
                        arg_cat = true;
                        break;
 
                OPTION_LONG("enable", NULL,
                            "Immediately enable/disable the portable service after attach/detach"):
                        arg_enable = true;
                        break;
 
                OPTION_LONG("now", NULL,
                            "Immediately start/stop the portable service after attach/before detach"):
                        arg_now = true;
                        break;
 
                OPTION_LONG("no-block", NULL,
                            "Don't block waiting for attach --now to complete"):
                        arg_no_block = true;
                        break;
 
                OPTION_LONG("extension", "PATH",
                            "Extend the image with an overlay"):
                        r = strv_extend(&arg_extension_images, opts.arg);
                        if (r < 0)
                                return log_oom();
                        break;
 
                OPTION_LONG("force", NULL,
                            "Skip 'already active' check when attaching or detaching an image (with extensions)"):
                        arg_force = true;
                        break;
 
                OPTION_LONG("clean", NULL,
                            "When detaching, also remove configuration, state, "
                            "cache, logs or runtime data of the portable service(s)"):
                        arg_clean = true;
                        break;
 
                OPTION_LONG("user", NULL, /* help= */ NULL):
                        arg_runtime_scope = RUNTIME_SCOPE_USER;
                        break;
 
                OPTION_LONG("system", NULL, /* help= */ NULL):
                        arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
                        break;
 
                OPTION_COMMON_INTROSPECT_CLI:
                        return introspect_cli(SD_JSON_FORMAT_OFF);
                }
 
        *remaining_args = option_parser_get_args(&opts);
        return 1;
}
 
static int run(int argc, char *argv[]) {
        char **args = NULL;
        int r;
 
        log_setup();
 
        r = parse_argv(argc, argv, &args);
        if (r <= 0)
                return r;
 
        return dispatch_verb(args, NULL);
}
 
DEFINE_MAIN_FUNCTION(run);