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 1–1000 of 1,709

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include "sd-bus.h"
#include "sd-json.h"
 
#include "alloc-util.h"
#include "ansi-color.h"
#include "build.h"
#include "bus-common-errors.h"
#include "bus-error.h"
#include "bus-locator.h"
#include "bus-unit-util.h"
#include "bus-util.h"
#include "bus-wait-for-jobs.h"
#include "chase.h"
#include "env-file.h"
#include "format-table.h"
#include "fs-util.h"
#include "glyph-util.h"
#include "install.h"
#include "main-func.h"
#include "os-util.h"
#include "pager.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "path-util.h"
#include "polkit-agent.h"
#include "portable.h"
#include "string-util.h"
#include "strv.h"
#include "unit-name.h"
#include "verbs.h"
 
static PagerFlags arg_pager_flags = 0;
static bool arg_legend = true;
static bool arg_ask_password = true;
static bool arg_quiet = false;
static const char *arg_profile = "default";
static const char *arg_copy_mode = NULL;
static bool arg_runtime = false;
static bool arg_reload = true;
static bool arg_cat = false;
static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
static const char *arg_host = NULL;
static bool arg_enable = false;
static bool arg_now = false;
static bool arg_no_block = false;
static char **arg_extension_images = NULL;
static bool arg_force = false;
static bool arg_clean = false;
static RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
 
STATIC_DESTRUCTOR_REGISTER(arg_extension_images, strv_freep);
 
COMMAND(
        "portablectl\0",
        "Attach or detach portable services in the local system.",
        .man_pages = "portablectl(1)\0",
        .pager_flags = &arg_pager_flags,
);
 
static bool is_portable_managed(const char *unit) {
        return ENDSWITH_SET(unit, ".service", ".target", ".socket", ".path", ".timer");
}
 
static int determine_image(const char *image, bool permit_non_existing, char **ret) {
        int r;
 
        assert(ret);
 
        /* If the specified name is a valid image name, we pass it as-is to portabled, which will search for it in the
         * usual search directories. Otherwise we presume it's a path, and will normalize it on the client's side
         * (among other things, to make the path independent of the client's working directory) before passing it
         * over. */
 
        if (image_name_is_valid(image)) {
                char *c;
 
                if (!arg_quiet && access_nofollow(image, F_OK) >= 0)
                        log_warning("Ambiguous invocation: current working directory contains file matching non-path argument '%s', ignoring. "
                                    "Prefix argument with './' to force reference to file in current working directory.", image);
 
                c = strdup(image);
                if (!c)
                        return log_oom();
 
                *ret = c;
                return 0;
        }
 
        if (arg_transport != BUS_TRANSPORT_LOCAL)
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
                                       "Operations on images by path not supported when connecting to remote systems.");
 
        r = chase(image, NULL, CHASE_TRAIL_SLASH | (permit_non_existing ? CHASE_NONEXISTENT : 0), ret, NULL);
        if (r < 0)
                return log_error_errno(r, "Cannot normalize specified image path '%s': %m", image);
 
        return 0;
}
 
static int attach_extensions_to_message(sd_bus_message *m, const char *method, char **extensions) {
        int r;
 
        assert(m);
        assert(method);
 
        /* The new methods also have flags parameters that are independent of the extensions */
        if (strv_isempty(extensions) && !endswith(method, "WithExtensions"))
                return 0;
 
        r = sd_bus_message_open_container(m, 'a', "s");
        if (r < 0)
                return bus_log_create_error(r);
 
        STRV_FOREACH(p, extensions) {
                _cleanup_free_ char *resolved_extension_image = NULL;
 
                r = determine_image(
                                *p,
                                startswith_strv(method, STRV_MAKE("Get", "Detach")),
                                &resolved_extension_image);
                if (r < 0)
                        return r;
 
                r = sd_bus_message_append(m, "s", resolved_extension_image);
                if (r < 0)
                        return bus_log_create_error(r);
        }
 
        r = sd_bus_message_close_container(m);
        if (r < 0)
                return bus_log_create_error(r);
 
        return 0;
}
 
static int extract_prefix(const char *path, char **ret) {
        _cleanup_free_ char *name = NULL, *bn = NULL;
        const char *underscore;
        size_t m;
        int r;
 
        assert(ret);
 
        r = path_extract_filename(path, &bn);
        if (r < 0)
                return r;
 
        underscore = strchr(bn, '_');
        if (underscore)
                m = underscore - bn;
        else {
                const char *e;
 
                e = ENDSWITH_SET(bn, ".raw.v", ".raw", ".v");
                if (!e)
                        e = strchr(bn, 0);
 
                m = e - bn;
        }
 
        name = strndup(bn, m);
        if (!name)
                return -ENOMEM;
 
        /* A slightly reduced version of what's permitted in unit names. With ':' and '\' are removed, as
         * well as '_' which we use as delimiter for the second part of the image string, which we ignore for
         * now. */
        if (!in_charset(name, ALPHANUMERICAL "-."))
                return -EINVAL;
 
        if (!filename_is_valid(name))
                return -EINVAL;
 
        *ret = TAKE_PTR(name);
        return 0;
}
 
static int log_unit_file_matches(char **matches) {
        if (arg_quiet)
                return 0;
 
        if (strv_isempty(matches))
                log_info("(Matching all unit files.)");
        else if (strv_length(matches) == 1)
                log_info("(Matching unit files with prefix '%s'.)", matches[0]);
        else {
                _cleanup_free_ char *joined = NULL;
 
                joined = strv_join(matches, "', '");
                if (!joined)
                        return log_oom();
 
                log_info("(Matching unit files with prefixes '%s'.)", joined);
        }
 
        return 0;
}
 
static int determine_matches(const char *image, char **l, bool allow_any, char ***ret) {
        _cleanup_strv_free_ char **k = NULL;
        int r;
 
        assert(image);
        assert(ret);
 
        /* Determine the matches to apply. If the list is empty we derive the match from the image name. If
         * the list contains exactly the "-" we return a wildcard list (which is the empty list), but only if
         * this is expressly permitted. */
 
        if (strv_isempty(l)) {
                char *prefix;
 
                r = extract_prefix(image, &prefix);
                if (r < 0)
                        return log_error_errno(r, "Failed to extract prefix of image name '%s': %m", image);
 
                r = strv_consume(&k, prefix);
                if (r < 0)
                        return log_oom();
 
        } else if (strv_equal(l, STRV_MAKE("-"))) {
 
                if (!allow_any)
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                               "Refusing all unit file match.");
 
        } else {
 
                k = strv_copy(l);
                if (!k)
                        return log_oom();
        }
 
        r = log_unit_file_matches(k);
        if (r < 0)
                return r;
 
        *ret = TAKE_PTR(k);
 
        return 0;
}
 
static int acquire_bus(sd_bus **bus) {
        int r;
 
        assert(bus);
 
        if (*bus)
                return 0;
 
        r = bus_connect_transport(arg_transport, arg_host, arg_runtime_scope, bus);
        if (r < 0)
                return bus_log_connect_error(r, arg_transport, arg_runtime_scope);
 
        (void) sd_bus_set_allow_interactive_authorization(*bus, arg_ask_password);
 
        return 0;
}
 
static int maybe_reload(sd_bus **bus) {
        int r;
 
        assert(bus);
 
        if (!arg_reload)
                return 0;
 
        r = acquire_bus(bus);
        if (r < 0)
                return r;
 
        return bus_service_manager_reload(*bus);
}
 
VERB_DEFAULT_NOARG(verb_list_images, "list",
     "List available portable service images");
static int verb_list_images(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_message_unrefp) sd_bus_message *reply = NULL;
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
        _cleanup_(table_unrefp) Table *table = NULL;
        int r;
 
        r = acquire_bus(&bus);
        if (r < 0)
                return r;
 
        r = bus_call_method(bus, bus_portable_mgr, "ListImages", &error, &reply, NULL);
        if (r < 0)
                return log_error_errno(r, "Failed to list images: %s", bus_error_message(&error, r));
 
        table = table_new("name", "type", "ro", "crtime", "mtime", "usage", "state");
        if (!table)
                return log_oom();
 
        r = sd_bus_message_enter_container(reply, 'a', "(ssbtttso)");
        if (r < 0)
                return bus_log_parse_error(r);
 
        for (;;) {
                const char *name, *type, *state;
                uint64_t crtime, mtime, usage;
                int ro_int;
 
                r = sd_bus_message_read(reply, "(ssbtttso)", &name, &type, &ro_int, &crtime, &mtime, &usage, &state, NULL);
                if (r < 0)
                        return bus_log_parse_error(r);
                if (r == 0)
                        break;
 
                r = table_add_many(table,
                                   TABLE_STRING, name,
                                   TABLE_STRING, type,
                                   TABLE_BOOLEAN, ro_int,
                                   TABLE_SET_COLOR, ro_int ? ansi_highlight_red() : NULL,
                                   TABLE_TIMESTAMP, crtime,
                                   TABLE_TIMESTAMP, mtime,
                                   TABLE_SIZE, usage,
                                   TABLE_STRING, state,
                                   TABLE_SET_COLOR, !streq(state, "detached") ? ansi_highlight_green() : NULL);
                if (r < 0)
                        return table_log_add_error(r);
        }
 
        r = sd_bus_message_exit_container(reply);
        if (r < 0)
                return bus_log_parse_error(r);
 
        if (!table_isempty(table)) {
                r = table_set_sort(table, (size_t) 0);
                if (r < 0)
                        return table_log_sort_error(r);
 
                table_set_header(table, arg_legend);
 
                r = table_print_or_warn(table);
                if (r < 0)
                        return r;
        }
 
        if (arg_legend) {
                if (table_isempty(table))
                        printf("No images.\n");
                else
                        printf("\n%zu images listed.\n", table_get_rows(table) - 1);
        }
 
        return 0;
}
 
static int determine_matches_from_os_release(sd_bus *bus, const char *image, char ***ret) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_strv_free_ char **matches = NULL;
        int r;
 
        assert(bus);
        assert(image);
        assert(ret);
 
        r = bus_call_method(bus, bus_portable_mgr, "GetImageOSRelease", &error, &reply, "s", image);
        if (r < 0) {
                log_debug_errno(r, "Failed to inspect image os-release: %s", bus_error_message(&error, r));
                *ret = NULL;
                return 0;
        }
 
        r = sd_bus_message_enter_container(reply, 'a', "{ss}");
        if (r < 0)
                return bus_log_parse_error(r);
 
        for (;;) {
                _cleanup_strv_free_ char **split = NULL;
                const char *key, *value;
 
                r = sd_bus_message_read(reply, "{ss}", &key, &value);
                if (r < 0)
                        return bus_log_parse_error(r);
                if (r == 0)
                        break;
 
                if (!streq(key, "PORTABLE_PREFIXES"))
                        continue;
 
                split = strv_split(value, WHITESPACE);
                if (!split)
                        return log_oom();
 
                STRV_FOREACH(prefix, split)
                        if (!string_is_safe(*prefix, STRING_FILENAME_PART))
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                       "Invalid PORTABLE_PREFIXES= entry in image os-release, refusing.");
 
                strv_free_and_replace(matches, split);
        }
 
        r = sd_bus_message_exit_container(reply);
        if (r < 0)
                return bus_log_parse_error(r);
 
        if (strv_isempty(matches)) {
                *ret = NULL;
                return 0;
        }
 
        *ret = TAKE_PTR(matches);
        return 0;
}
 
static int make_get_image_metadata_message(
                sd_bus *bus,
                const char *image,
                char **matches,
                sd_bus_message **ret) {
 
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
        uint64_t flags = arg_force ? PORTABLE_FORCE_EXTENSION : 0;
        const char *method;
        int r;
 
        assert(bus);
        assert(ret);
 
        method = strv_isempty(arg_extension_images) && !arg_force ? "GetImageMetadata" : "GetImageMetadataWithExtensions";
 
        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);
 
        if (streq(method, "GetImageMetadataWithExtensions")) {
                r = sd_bus_message_append(m, "t", flags);
                if (r < 0)
                        return bus_log_create_error(r);
        }
 
        *ret = TAKE_PTR(m);
        return 0;
}
 
static int log_image_metadata_error(int r, const sd_bus_error *error) {
        return log_error_errno(r, "Failed to inspect image metadata: %s", bus_error_message(error, r));
}
 
static int get_image_metadata(sd_bus *bus, const char *image, char **matches, sd_bus_message **reply) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        int r;
 
        r = make_get_image_metadata_message(bus, image, matches, &m);
        if (r < 0)
                return r;
 
        r = sd_bus_call(bus, m, 0, &error, reply);
        if (r < 0)
                return log_image_metadata_error(r, &error);
 
        return 0;
}
 
static bool image_metadata_error_is_no_match(const sd_bus_error *error) {
        return sd_bus_error_has_name(error, BUS_ERROR_NO_MATCHING_UNIT_FILES);
}
 
VERB(verb_inspect_image, "inspect", "NAME|PATH [PREFIX…]\0", 2, VERB_ANY, 0,
     "Show details of specified portable service image");
static int verb_inspect_image(int argc, char *argv[], uintptr_t _data, void *userdata) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        _cleanup_strv_free_ char **matches = NULL, **fallback_matches = NULL;
        _cleanup_free_ char *image = NULL;
        bool nl = false, header = false;
        const char *path;
        const void *data;
        size_t sz;
        int r;
 
        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 = determine_matches(argv[1], argv + 2, true, &matches);
        if (r < 0)
                return r;
 
        r = make_get_image_metadata_message(bus, image, matches, &m);
        if (r < 0)
                return r;
 
        r = sd_bus_call(bus, m, 0, &error, &reply);
        if (r < 0) {
                int first_error = r;
 
                if (!strv_isempty(argv + 2) || !image_metadata_error_is_no_match(&error))
                        return log_image_metadata_error(r, &error);
 
                r = determine_matches_from_os_release(bus, image, &fallback_matches);
                if (r < 0)
                        return r;
 
                if (strv_isempty(fallback_matches))
                        return log_image_metadata_error(first_error, &error);
 
                if (!arg_quiet)
                        log_info("(No matching unit files found with the image name prefix, retrying with PORTABLE_PREFIXES= from os-release.)");
 
                r = log_unit_file_matches(fallback_matches);
                if (r < 0)
                        return r;
 
                r = get_image_metadata(bus, image, fallback_matches, &reply);
                if (r < 0)
                        return r;
        }
 
        r = sd_bus_message_read(reply, "s", &path);
        if (r < 0)
                return bus_log_parse_error(r);
 
        r = sd_bus_message_read_array(reply, 'y', &data, &sz);
        if (r < 0)
                return bus_log_parse_error(r);
 
        pager_open(arg_pager_flags);
 
        if (arg_cat) {
                printf("%s-- OS Release: --%s\n", ansi_highlight(), ansi_normal());
                fwrite(data, sz, 1, stdout);
                fflush(stdout);
                nl = true;
        } else {
                _cleanup_free_ char *pretty_portable = NULL, *pretty_os = NULL;
 
                r = parse_env_data(
                                data, sz,
                                "/etc/os-release",
                                "PORTABLE_PRETTY_NAME", &pretty_portable,
                                "PRETTY_NAME", &pretty_os);
                if (r < 0)
                        return log_error_errno(r, "Failed to parse /etc/os-release: %m");
 
                printf("Image:\n\t%s\n"
                       "Portable Service:\n\t%s\n"
                       "Operating System:\n\t%s\n",
                       path,
                       strna(pretty_portable),
                       strna(pretty_os));
        }
 
        if (!strv_isempty(arg_extension_images) || arg_force) {
                /* If we specified any extensions, we'll first get back exactly the paths (and
                 * extension-release content) for each one of the arguments. */
 
                r = sd_bus_message_enter_container(reply, 'a', "{say}");
                if (r < 0)
                        return bus_log_parse_error(r);
 
                for (size_t i = 0; i < strv_length(arg_extension_images); ++i) {
                        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_read_array(reply, 'y', &data, &sz);
                        if (r < 0)
                                return bus_log_parse_error(r);
 
                        if (arg_cat) {
                                if (nl)
                                        fputc('\n', stdout);
 
                                printf("%s-- Extension Release: %s --%s\n", ansi_highlight(), name, ansi_normal());
                                fwrite(data, sz, 1, stdout);
                                fflush(stdout);
                                nl = true;
                        } else {
                                _cleanup_free_ char *pretty_portable = NULL, *sysext_pretty_os = NULL,
                                                    *sysext_level = NULL, *sysext_id = NULL,
                                                    *sysext_version_id = NULL, *sysext_scope = NULL,
                                                    *portable_prefixes = NULL, *id = NULL, *version_id = NULL,
                                                    *sysext_image_id = NULL, *sysext_image_version = NULL,
                                                    *sysext_build_id = NULL, *confext_pretty_os = NULL,
                                                    *confext_level = NULL, *confext_id = NULL,
                                                    *confext_version_id = NULL, *confext_scope = NULL,
                                                    *confext_image_id = NULL, *confext_image_version = NULL,
                                                    *confext_build_id = NULL;
 
                                r = parse_env_data(
                                                data, sz,
                                                name,
                                                "SYSEXT_ID", &sysext_id,
                                                "SYSEXT_VERSION_ID", &sysext_version_id,
                                                "SYSEXT_BUILD_ID", &sysext_build_id,
                                                "SYSEXT_IMAGE_ID", &sysext_image_id,
                                                "SYSEXT_IMAGE_VERSION", &sysext_image_version,
                                                "SYSEXT_SCOPE", &sysext_scope,
                                                "SYSEXT_LEVEL", &sysext_level,
                                                "SYSEXT_PRETTY_NAME", &sysext_pretty_os,
                                                "CONFEXT_ID", &confext_id,
                                                "CONFEXT_VERSION_ID", &confext_version_id,
                                                "CONFEXT_BUILD_ID", &confext_build_id,
                                                "CONFEXT_IMAGE_ID", &confext_image_id,
                                                "CONFEXT_IMAGE_VERSION", &confext_image_version,
                                                "CONFEXT_SCOPE", &confext_scope,
                                                "CONFEXT_LEVEL", &confext_level,
                                                "CONFEXT_PRETTY_NAME", &confext_pretty_os,
                                                "ID", &id,
                                                "VERSION_ID", &version_id,
                                                "PORTABLE_PRETTY_NAME", &pretty_portable,
                                                "PORTABLE_PREFIXES", &portable_prefixes);
                                if (r < 0)
                                        return log_error_errno(r, "Failed to parse extension release from '%s': %m", name);
 
                                printf("Extension:\n\t%s\n"
                                       "\tExtension Scope:\n\t\t%s\n"
                                       "\tExtension Compatibility Level:\n\t\t%s\n"
                                       "\tExtension Compatibility OS:\n\t\t%s\n"
                                       "\tExtension Compatibility OS Version:\n\t\t%s\n"
                                       "\tPortable Service:\n\t\t%s\n"
                                       "\tPortable Prefixes:\n\t\t%s\n"
                                       "\tExtension Image:\n\t\t%s%s%s %s%s%s\n",
                                       name,
                                       strna(sysext_scope ?: confext_scope),
                                       strna(sysext_level ?: confext_level),
                                       strna(id),
                                       strna(version_id),
                                       strna(pretty_portable),
                                       strna(portable_prefixes),
                                       strempty(sysext_pretty_os ?: confext_pretty_os),
                                       (sysext_pretty_os ?: confext_pretty_os) ? " (" : "ID: ",
                                       strna(sysext_id ?: sysext_image_id ?: confext_id ?: confext_image_id),
                                       (sysext_pretty_os ?: confext_pretty_os)  ? "" : "Version: ",
                                       strna(sysext_version_id ?: sysext_image_version ?: sysext_build_id ?: confext_version_id ?: confext_image_version ?: confext_build_id),
                                       (sysext_pretty_os ?: confext_pretty_os)  ? ")" : "");
                        }
 
                        r = sd_bus_message_exit_container(reply);
                        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 = 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_read_array(reply, 'y', &data, &sz);
                if (r < 0)
                        return bus_log_parse_error(r);
 
                if (arg_cat) {
                        if (nl)
                                fputc('\n', stdout);
 
                        printf("%s-- Unit file: %s --%s\n", ansi_highlight(), name, ansi_normal());
                        fwrite(data, sz, 1, stdout);
                        fflush(stdout);
                        nl = true;
                } else {
                        if (!header) {
                                fputs("Unit files:\n", stdout);
                                header = true;
                        }
 
                        fputc('\t', stdout);
                        fputs(name, stdout);
                        fputc('\n', stdout);
                }
 
                r = sd_bus_message_exit_container(reply);
                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);
 
        return 0;
}
 
static int print_changes(sd_bus_message *m) {
        int r;
 
        if (arg_quiet)
                return 0;
 
        r = sd_bus_message_enter_container(m, 'a', "(sss)");
        if (r < 0)
                return bus_log_parse_error(r);
 
        for (;;) {
                const char *type, *path, *source;
 
                r = sd_bus_message_read(m, "(sss)", &type, &path, &source);
                if (r < 0)
                        return bus_log_parse_error(r);
                if (r == 0)
                        break;
 
                if (streq(type, "symlink"))
                        log_info("Created symlink %s %s %s.", path, glyph(GLYPH_ARROW_RIGHT), source);
                else if (streq(type, "copy")) {
                        if (isempty(source))
                                log_info("Copied %s.", path);
                        else
                                log_info("Copied %s %s %s.", source, glyph(GLYPH_ARROW_RIGHT), path);
                } else if (streq(type, "unlink"))
                        log_info("Removed %s.", path);
                else if (streq(type, "write"))
                        log_info("Written %s.", path);
                else if (streq(type, "mkdir"))
                        log_info("Created directory %s.", path);
                else
                        log_error("Unexpected change: %s/%s/%s", type, path, source);
        }
 
        r = sd_bus_message_exit_container(m);
        if (r < 0)
                return r;
 
        return 0;
}
 
static int maybe_enable_disable(sd_bus *bus, const char *path, bool enable) {
        _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_strv_free_ char **names = NULL;
        const uint64_t flags = UNIT_FILE_PORTABLE | (arg_runtime ? UNIT_FILE_RUNTIME : 0);
        int r;
 
        if (!arg_enable)
                return 0;
 
        names = strv_new(path, NULL);
        if (!names)
                return log_oom();
 
        r = bus_message_new_method_call(
                bus,
                &m,
                bus_systemd_mgr,
                enable ? "EnableUnitFilesWithFlags" : "DisableUnitFilesWithFlags");
        if (r < 0)
                return bus_log_create_error(r);
 
        r = sd_bus_message_append_strv(m, names);
        if (r < 0)
                return bus_log_create_error(r);
 
        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, "Failed to %s the portable service %s: %s",
                        enable ? "enable" : "disable", path, bus_error_message(&error, r));
 
        if (enable) {
                r = sd_bus_message_skip(reply, "b");
                if (r < 0)
                        return bus_log_parse_error(r);
        }
 
        (void) bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet);
 
        return 0;
}
 
static int maybe_start_stop_restart(sd_bus *bus, const char *name, const char *method, BusWaitForJobs *wait) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
        const char *job = NULL;
        int r;
 
        assert(name);
        assert(STR_IN_SET(method, "StartUnit", "StopUnit", "RestartUnit"));
 
        if (!arg_now)
                return 0;
 
        r = bus_call_method(
                        bus,
                        bus_systemd_mgr,
                        method,
                        &error,
                        &reply,
                        "ss", name, "replace");
        if (r < 0)
                return log_error_errno(r, "Failed to call %s on the portable service %s: %s",
                                       method,
                                       name,
                                       bus_error_message(&error, r));
 
        r = sd_bus_message_read(reply, "o", &job);
        if (r < 0)
                return bus_log_parse_error(r);
 
        if (!arg_quiet)
                log_info("Queued %s to call %s on portable service %s.", job, method, name);
 
        if (wait) {
                r = bus_wait_for_jobs_add(wait, job);
                if (r < 0)
                        return log_error_errno(r, "Failed to watch %s job to call %s on %s: %m",
                                               job, method, name);
        }
 
        return 0;
}
 
static int maybe_start_stop_restart_units(
                sd_bus *bus,
                char * const *names,
                const char *job_type,
                BusWaitForJobs *wait) {
 
        _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_strv_free_ char **units = NULL;
        size_t n_units = 0;
        const char *method;
        int r;
 
        assert(bus);
        assert(STR_IN_SET(job_type, "start", "stop", "restart"));
 
        if (!arg_now || strv_isempty(names))
                return 0;
 
        method = streq(job_type, "start") ? "StartUnit" :
                 streq(job_type, "stop")  ? "StopUnit"  :
                                            "RestartUnit";
 
        /* Trying to start/stop/restart templates will result in an error being returned and printed */
        STRV_FOREACH(name, names) {
                if (unit_name_is_valid(*name, UNIT_NAME_TEMPLATE))
                        continue;
                if (strv_extend_with_size(&units, &n_units, *name) < 0)
                        return log_oom();
        }
        if (n_units == 0)
                return 0;
 
        /* Prefer the new EnqueueUnitJobMany() method which submits all units in a single transaction.
         * Falls back to per-unit calls on older managers (UnknownMethod) or when the new method rejects
         * something about the request (InvalidArgs). */
        r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "EnqueueUnitJobMany");
        if (r < 0)
                return bus_log_create_error(r);
 
        r = sd_bus_message_append_strv(m, units);
        if (r < 0)
                return bus_log_create_error(r);
 
        r = sd_bus_message_append(m, "sst", job_type, "replace", UINT64_C(0));
        if (r < 0)
                return bus_log_create_error(r);
 
        r = sd_bus_call(bus, m, 0, &error, &reply);
        if (r >= 0) {
                r = sd_bus_message_enter_container(reply, 'a', "(uosos)");
                if (r < 0)
                        return bus_log_parse_error(r);
 
                for (;;) {
                        const char *path, *unit_id;
                        uint32_t id;
 
                        r = sd_bus_message_read(reply, "(uosos)", &id, &path, &unit_id, NULL, NULL);
                        if (r < 0)
                                return bus_log_parse_error(r);
                        if (r == 0)
                                break;
 
                        if (!arg_quiet)
                                log_info("Queued %s to call %s on portable service %s.", path, method, unit_id);
 
                        if (wait) {
                                r = bus_wait_for_jobs_add(wait, path);
                                if (r < 0)
                                        return log_error_errno(r, "Failed to watch %s job to call %s on %s: %m",
                                                               path, method, unit_id);
                        }
                }
 
                r = sd_bus_message_exit_container(reply);
                if (r < 0)
                        return bus_log_parse_error(r);
 
                return 0;
        }
 
        if (!sd_bus_error_has_names(&error, SD_BUS_ERROR_UNKNOWN_METHOD, SD_BUS_ERROR_INVALID_ARGS))
                return log_error_errno(r, "Failed to enqueue jobs for portable services: %s",
                                       bus_error_message(&error, r));
 
        log_debug_errno(r, "EnqueueUnitJobMany() not supported (%s), falling back to per-unit calls.",
                        bus_error_message(&error, r));
 
        STRV_FOREACH(unit, units)
                (void) maybe_start_stop_restart(bus, *unit, method, wait);
 
        return 0;
}
 
static int maybe_enable_start(sd_bus *bus, sd_bus_message *reply) {
        _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *wait = NULL;
        _cleanup_strv_free_ char **start_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;
        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(&start_names, TAKE_PTR(name));
                        if (r < 0)
                                return log_oom();
                }
        }
 
        r = sd_bus_message_exit_container(reply);
        if (r < 0)
                return r;