HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/core/dbus-manager.c

159,246 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 4a6adcb82cb77d2d424a21f5e30e9014607685c6

Lines 1–1000 of 3,732

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <linux/capability.h>
#include <unistd.h>
 
#include "alloc-util.h"
#include "architecture.h"
#include "bitfield.h"
#include "build.h"
#include "bus-common-errors.h"
#include "bus-get-properties.h"
#include "bus-log-control-api.h"
#include "bus-message-util.h"
#include "bus-util.h"
#include "chase.h"
#include "confidential-virt.h"
#include "dbus-cgroup.h"
#include "dbus-execute.h"
#include "dbus.h"
#include "dbus-job.h"
#include "dbus-manager.h"
#include "dbus-scope.h"
#include "dbus-service.h"
#include "dbus-unit.h"
#include "dbus-util.h"
#include "dynamic-user.h"
#include "env-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "format-util.h"
#include "glyph-util.h"
#include "hashmap.h"
#include "initrd-util.h"
#include "install.h"
#include "locale-util.h"
#include "log.h"
#include "manager-dump.h"
#include "manager.h"
#include "memfd-util.h"
#include "os-util.h"
#include "path-util.h"
#include "pidref.h"
#include "process-util.h"
#include "selinux-access.h"
#include "set.h"
#include "string-util.h"
#include "strv.h"
#include "syslog-util.h"
#include "taint.h"
#include "unit-name.h"
#include "user-util.h"
#include "version.h"
#include "virt.h"
#include "watchdog.h"
 
static UnitFileFlags unit_file_bools_to_flags(bool runtime, bool force) {
        return (runtime ? UNIT_FILE_RUNTIME : 0) |
               (force   ? UNIT_FILE_FORCE   : 0);
}
 
BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_oom_policy, oom_policy, OOMPolicy);
BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_emergency_action, emergency_action, EmergencyAction);
 
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_version, "s", GIT_VERSION);
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_features, "s", systemd_features);
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_architecture, "s", architecture_to_string(uname_architecture()));
static BUS_DEFINE_PROPERTY_GET2(property_get_system_state, "s", Manager, manager_state, manager_state_to_string);
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_timer_slack_nsec, "t", proc_get_timerslack());
static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size, "u", Hashmap *, hashmap_size);
static BUS_DEFINE_PROPERTY_GET_REF(property_get_set_size, "u", Set *, set_size);
static BUS_DEFINE_PROPERTY_GET(property_get_default_timeout_abort_usec, "t", Manager, manager_default_timeout_abort_usec);
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_watchdog_device, "s", watchdog_get_device());
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_watchdog_last_ping_realtime, "t", watchdog_get_last_ping(CLOCK_REALTIME));
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_watchdog_last_ping_monotonic, "t", watchdog_get_last_ping(CLOCK_MONOTONIC));
static BUS_DEFINE_PROPERTY_GET(property_get_progress, "d", Manager, manager_get_progress);
 
static int property_get_virtualization(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        Virtualization v;
 
        assert(bus);
        assert(reply);
 
        v = detect_virtualization();
 
        /* Make sure to return the empty string when we detect no virtualization, as that is the API.
         *
         * https://github.com/systemd/systemd/issues/1423
         */
 
        return sd_bus_message_append(
                        reply, "s",
                        v == VIRTUALIZATION_NONE ? NULL : virtualization_to_string(v));
}
 
static int property_get_confidential_virtualization(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        ConfidentialVirtualization v;
 
        assert(bus);
        assert(reply);
 
        v = detect_confidential_virtualization();
 
        return sd_bus_message_append(
                        reply, "s",
                        v <= 0 ? NULL : confidential_virtualization_to_string(v));
}
 
static int property_get_tainted(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        assert(bus);
        assert(reply);
 
        _cleanup_free_ char *s = taint_string();
        if (!s)
                return log_oom();
 
        return sd_bus_message_append(reply, "s", s);
}
 
static int property_set_log_target(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *value,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        Manager *m = userdata;
        const char *t;
        int r;
 
        assert(bus);
        assert(value);
 
        r = sd_bus_message_read(value, "s", &t);
        if (r < 0)
                return r;
 
        if (isempty(t))
                manager_restore_original_log_target(m);
        else {
                LogTarget target;
 
                target = log_target_from_string(t);
                if (target < 0)
                        return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid log target '%s'", t);
 
                manager_override_log_target(m, target);
        }
 
        return 0;
}
 
static int property_set_log_level(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *value,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        Manager *m = userdata;
        const char *t;
        int r;
 
        assert(bus);
        assert(value);
 
        r = sd_bus_message_read(value, "s", &t);
        if (r < 0)
                return r;
 
        if (isempty(t))
                manager_restore_original_log_level(m);
        else {
                int level;
 
                level = log_level_from_string(t);
                if (level < 0)
                        return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid log level '%s'", t);
 
                manager_override_log_level(m, level);
        }
 
        return 0;
}
 
static int property_get_environment(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        _cleanup_strv_free_ char **l = NULL;
        Manager *m = ASSERT_PTR(userdata);
        int r;
 
        assert(bus);
        assert(reply);
 
        r = manager_get_effective_environment(m, &l);
        if (r < 0)
                return r;
 
        return sd_bus_message_append_strv(reply, l);
}
 
static int property_get_show_status(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        Manager *m = ASSERT_PTR(userdata);
 
        assert(bus);
        assert(reply);
 
        return sd_bus_message_append(reply, "b", manager_get_show_status_on(m));
}
 
static int property_get_runtime_watchdog(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        Manager *m = ASSERT_PTR(userdata);
 
        assert(bus);
        assert(reply);
 
        return sd_bus_message_append(reply, "t", manager_get_watchdog(m, WATCHDOG_RUNTIME));
}
 
static int property_get_pretimeout_watchdog(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        Manager *m = ASSERT_PTR(userdata);
 
        assert(bus);
        assert(reply);
 
        return sd_bus_message_append(reply, "t", manager_get_watchdog(m, WATCHDOG_PRETIMEOUT));
}
 
static int property_get_pretimeout_watchdog_governor(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        Manager *m = ASSERT_PTR(userdata);
 
        assert(bus);
        assert(reply);
 
        return sd_bus_message_append(reply, "s", m->watchdog_pretimeout_governor);
}
 
static int property_get_reboot_watchdog(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        Manager *m = ASSERT_PTR(userdata);
 
        assert(bus);
        assert(reply);
 
        return sd_bus_message_append(reply, "t", manager_get_watchdog(m, WATCHDOG_REBOOT));
}
 
static int property_get_kexec_watchdog(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        Manager *m = ASSERT_PTR(userdata);
 
        assert(bus);
        assert(reply);
 
        return sd_bus_message_append(reply, "t", manager_get_watchdog(m, WATCHDOG_KEXEC));
}
 
static int property_set_watchdog(Manager *m, WatchdogType type, sd_bus_message *value) {
        usec_t timeout;
        int r;
 
        assert(m);
        assert(value);
 
        assert_cc(sizeof(usec_t) == sizeof(uint64_t));
 
        r = sd_bus_message_read(value, "t", &timeout);
        if (r < 0)
                return r;
 
        manager_override_watchdog(m, type, timeout);
        return 0;
}
 
static int property_set_runtime_watchdog(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *value,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        return property_set_watchdog(userdata, WATCHDOG_RUNTIME, value);
}
 
static int property_set_pretimeout_watchdog(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *value,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        return property_set_watchdog(userdata, WATCHDOG_PRETIMEOUT, value);
}
 
static int property_set_pretimeout_watchdog_governor(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *value,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        Manager *m = ASSERT_PTR(userdata);
        const char *governor;
        int r;
 
        r = sd_bus_message_read(value, "s", &governor);
        if (r < 0)
                return r;
 
        if (isempty(governor))
                governor = NULL;
        else if (!string_is_safe(governor, /* flags= */ 0))
                return -EINVAL;
 
        return manager_override_watchdog_pretimeout_governor(m, governor);
}
 
static int property_set_reboot_watchdog(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *value,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        return property_set_watchdog(userdata, WATCHDOG_REBOOT, value);
}
 
static int property_set_kexec_watchdog(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *value,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        _unused_ Manager *m = ASSERT_PTR(userdata);
 
        assert(bus);
        assert(value);
 
        return property_set_watchdog(userdata, WATCHDOG_KEXEC, value);
}
 
static int property_get_oom_score_adjust(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        Manager *m = ASSERT_PTR(userdata);
        int r, n;
 
        assert(bus);
        assert(reply);
 
        if (m->defaults.oom_score_adjust_set)
                n = m->defaults.oom_score_adjust;
        else {
                n = 0;
                r = get_oom_score_adjust(&n);
                if (r < 0)
                        log_debug_errno(r, "Failed to read current OOM score adjustment value, ignoring: %m");
        }
 
        return sd_bus_message_append(reply, "i", n);
}
 
static int property_get_transactions_with_cycle(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *reterr_error) {
 
        Manager *m = ASSERT_PTR(userdata);
        int r;
 
        assert(bus);
        assert(reply);
 
        r = sd_bus_message_open_container(reply, 'a', "t");
        if (r < 0)
                return r;
 
        uint64_t *id;
        SET_FOREACH(id, m->transactions_with_cycle) {
                r = sd_bus_message_append_basic(reply, 't', id);
                if (r < 0)
                        return r;
        }
 
        return sd_bus_message_close_container(reply);
}
 
static int bus_get_unit_by_name(Manager *m, sd_bus_message *message, const char *name, Unit **ret_unit, sd_bus_error *reterr_error) {
        Unit *u;
        int r;
 
        assert(m);
        assert(message);
        assert(ret_unit);
 
        /* More or less a wrapper around manager_get_unit() that generates nice errors and has one trick up
         * its sleeve: if the name is specified empty we use the client's unit. */
 
        if (isempty(name)) {
                _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
 
                r = bus_query_sender_pidref(message, &pidref);
                if (r < 0)
                        return r;
 
                u = manager_get_unit_by_pidref(m, &pidref);
                if (!u)
                        return sd_bus_error_set(reterr_error, BUS_ERROR_NO_SUCH_UNIT, "Client not member of any unit.");
        } else {
                u = manager_get_unit(m, name);
                if (!u)
                        return sd_bus_error_setf(reterr_error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", name);
        }
 
        *ret_unit = u;
        return 0;
}
 
static int bus_load_unit_by_name(Manager *m, sd_bus_message *message, const char *name, Unit **ret_unit, sd_bus_error *reterr_error) {
        assert(m);
        assert(message);
        assert(ret_unit);
 
        /* Pretty much the same as bus_get_unit_by_name(), but we also load the unit if necessary. */
 
        if (isempty(name))
                return bus_get_unit_by_name(m, message, name, ret_unit, reterr_error);
 
        return manager_load_unit(m, name, NULL, reterr_error, ret_unit);
}
 
static int reply_unit_path(Unit *u, sd_bus_message *message, sd_bus_error *reterr_error) {
        _cleanup_free_ char *path = NULL;
        int r;
 
        assert(u);
        assert(message);
 
        r = mac_selinux_unit_access_check(u, message, "status", reterr_error);
        if (r < 0)
                return r;
 
        path = unit_dbus_path(u);
        if (!path)
                return log_oom();
 
        return sd_bus_reply_method_return(message, "o", path);
}
 
static int method_get_unit(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        Manager *m = ASSERT_PTR(userdata);
        const char *name;
        Unit *u;
        int r;
 
        assert(message);
 
        /* Anyone can call this method */
 
        r = sd_bus_message_read(message, "s", &name);
        if (r < 0)
                return r;
 
        r = bus_get_unit_by_name(m, message, name, &u, reterr_error);
        if (r < 0)
                return r;
 
        return reply_unit_path(u, message, reterr_error);
}
 
static int method_get_unit_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        Manager *m = ASSERT_PTR(userdata);
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
        Unit *u;
        int r;
 
        assert(message);
 
        assert_cc(sizeof(pid_t) == sizeof(uint32_t));
 
        /* Anyone can call this method */
 
        r = sd_bus_message_read(message, "u", &pidref.pid);
        if (r < 0)
                return r;
        if (pidref.pid < 0)
                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid PID " PID_FMT, pidref.pid);
        if (pidref.pid == 0) {
                r = bus_query_sender_pidref(message, &pidref);
                if (r < 0)
                        return r;
        }
 
        u = manager_get_unit_by_pidref(m, &pidref);
        if (!u)
                return sd_bus_error_setf(reterr_error, BUS_ERROR_NO_UNIT_FOR_PID, "PID "PID_FMT" does not belong to any loaded unit.", pidref.pid);
 
        return reply_unit_path(u, message, reterr_error);
}
 
static int method_get_unit_by_invocation_id(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        _cleanup_free_ char *path = NULL;
        Manager *m = ASSERT_PTR(userdata);
        sd_id128_t id;
        Unit *u;
        int r;
 
        assert(message);
 
        /* Anyone can call this method */
 
        if (bus_message_read_id128(message, &id) < 0)
                return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid invocation ID");
 
        if (sd_id128_is_null(id)) {
                _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
 
                r = bus_query_sender_pidref(message, &pidref);
                if (r < 0)
                        return r;
 
                u = manager_get_unit_by_pidref(m, &pidref);
                if (!u)
                        return sd_bus_error_setf(reterr_error, BUS_ERROR_NO_SUCH_UNIT,
                                                 "Client " PID_FMT " not member of any unit.", pidref.pid);
        } else {
                u = hashmap_get(m->units_by_invocation_id, &id);
                if (!u)
                        return sd_bus_error_setf(reterr_error, BUS_ERROR_NO_UNIT_FOR_INVOCATION_ID, "No unit with the specified invocation ID " SD_ID128_FORMAT_STR " known.", SD_ID128_FORMAT_VAL(id));
        }
 
        r = mac_selinux_unit_access_check(u, message, "status", reterr_error);
        if (r < 0)
                return r;
 
        /* So here's a special trick: the bus path we return actually references the unit by its invocation
         * ID instead of the unit name. This means it stays valid only as long as the invocation ID stays the
         * same. */
        path = unit_dbus_path_invocation_id(u);
        if (!path)
                return -ENOMEM;
 
        return sd_bus_reply_method_return(message, "o", path);
}
 
static int method_get_unit_by_control_group(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        Manager *m = userdata;
        const char *cgroup;
        Unit *u;
        int r;
 
        r = sd_bus_message_read(message, "s", &cgroup);
        if (r < 0)
                return r;
 
        if (!path_is_absolute(cgroup))
                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Control group path is not absolute: %s", cgroup);
 
        if (!path_is_normalized(cgroup))
                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Control group path is not normalized: %s", cgroup);
 
        u = manager_get_unit_by_cgroup(m, cgroup);
        if (!u)
                return sd_bus_error_setf(reterr_error, BUS_ERROR_NO_SUCH_UNIT,
                                         "Control group '%s' is not valid or not managed by this instance",
                                         cgroup);
 
        return reply_unit_path(u, message, reterr_error);
}
 
static int method_get_unit_by_pidfd(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
        Manager *m = ASSERT_PTR(userdata);
        _cleanup_free_ char *path = NULL;
        int r, pidfd;
        Unit *u;
 
        assert(message);
 
        r = sd_bus_message_read(message, "h", &pidfd);
        if (r < 0)
                return r;
 
        r = pidref_set_pidfd(&pidref, pidfd);
        if (r < 0)
                return sd_bus_error_set_errnof(reterr_error, r, "Failed to get PID from PIDFD: %m");
 
        u = manager_get_unit_by_pidref(m, &pidref);
        if (!u)
                return sd_bus_error_setf(reterr_error, BUS_ERROR_NO_UNIT_FOR_PID, "PID "PID_FMT" does not belong to any loaded unit.", pidref.pid);
 
        r = mac_selinux_unit_access_check(u, message, "status", reterr_error);
        if (r < 0)
                return r;
 
        path = unit_dbus_path(u);
        if (!path)
                return log_oom();
 
        r = sd_bus_message_new_method_return(message, &reply);
        if (r < 0)
                return r;
 
        r = sd_bus_message_append(reply, "os", path, u->id);
        if (r < 0)
                return r;
 
        r = sd_bus_message_append_array(reply, 'y', u->invocation_id.bytes, sizeof(u->invocation_id.bytes));
        if (r < 0)
                return r;
 
        /* Double-check that the process is still alive and that the PID did not change before returning the
         * answer. */
        r = pidref_verify(&pidref);
        if (r == -ESRCH)
                return sd_bus_error_setf(reterr_error,
                                         BUS_ERROR_NO_SUCH_PROCESS,
                                         "The PIDFD's PID "PID_FMT" changed during the lookup operation.",
                                         pidref.pid);
        if (r < 0)
                return sd_bus_error_set_errnof(reterr_error, r, "Failed to get PID from PIDFD: %m");
 
        return sd_bus_message_send(reply);
}
 
static int method_load_unit(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        Manager *m = ASSERT_PTR(userdata);
        const char *name;
        Unit *u;
        int r;
 
        assert(message);
 
        /* Anyone can call this method */
 
        r = sd_bus_message_read(message, "s", &name);
        if (r < 0)
                return r;
 
        r = bus_load_unit_by_name(m, message, name, &u, reterr_error);
        if (r < 0)
                return r;
 
        return reply_unit_path(u, message, reterr_error);
}
 
static int method_start_unit_generic(sd_bus_message *message, Manager *m, JobType job_type, bool reload_if_possible, sd_bus_error *reterr_error) {
        const char *name;
        Unit *u;
        int r;
 
        assert(message);
        assert(m);
 
        r = sd_bus_message_read(message, "s", &name);
        if (r < 0)
                return r;
 
        r = manager_load_unit(m, name, NULL, reterr_error, &u);
        if (r < 0)
                return r;
 
        return bus_unit_method_start_generic(message, u, job_type, reload_if_possible, reterr_error);
}
 
static int method_start_unit(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        return method_start_unit_generic(message, userdata, JOB_START, /* reload_if_possible= */ false, reterr_error);
}
 
static int method_stop_unit(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        return method_start_unit_generic(message, userdata, JOB_STOP, /* reload_if_possible= */ false, reterr_error);
}
 
static int method_reload_unit(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        return method_start_unit_generic(message, userdata, JOB_RELOAD, /* reload_if_possible= */ false, reterr_error);
}
 
static int method_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        return method_start_unit_generic(message, userdata, JOB_RESTART, /* reload_if_possible= */ false, reterr_error);
}
 
static int method_try_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        return method_start_unit_generic(message, userdata, JOB_TRY_RESTART, /* reload_if_possible= */ false, reterr_error);
}
 
static int method_reload_or_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        return method_start_unit_generic(message, userdata, JOB_RESTART, /* reload_if_possible= */ true, reterr_error);
}
 
static int method_reload_or_try_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        return method_start_unit_generic(message, userdata, JOB_TRY_RESTART, /* reload_if_possible= */ true, reterr_error);
}
 
typedef enum GenericUnitOperationFlags {
        GENERIC_UNIT_LOAD            = 1 << 0, /* Load if the unit is not loaded yet */
        GENERIC_UNIT_VALIDATE_LOADED = 1 << 1, /* Verify unit is properly loaded before forwarding call */
} GenericUnitOperationFlags;
 
static int method_generic_unit_operation(
                sd_bus_message *message,
                Manager *m,
                sd_bus_error *reterr_error,
                UnitType type,
                sd_bus_message_handler_t handler,
                GenericUnitOperationFlags flags) {
 
        const char *name;
        Unit *u;
        int r;
 
        assert(message);
        assert(m);
        assert(handler);
 
        /* Read the first argument from the command and pass the operation to the specified per-unit
         * method. */
 
        r = sd_bus_message_read(message, "s", &name);
        if (r < 0)
                return r;
 
        if (!isempty(name) && FLAGS_SET(flags, GENERIC_UNIT_LOAD))
                r = manager_load_unit(m, name, NULL, reterr_error, &u);
        else
                r = bus_get_unit_by_name(m, message, name, &u, reterr_error);
        if (r < 0)
                return r;
 
        if (type != _UNIT_TYPE_INVALID && u->type != type)
                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_NOT_SUPPORTED,
                                         "%s operation is not supported for unit type '%s'",
                                         sd_bus_message_get_member(message), unit_type_to_string(u->type));
 
        if (FLAGS_SET(flags, GENERIC_UNIT_VALIDATE_LOADED)) {
                r = bus_unit_validate_load_state(u, reterr_error);
                if (r < 0)
                        return r;
        }
 
        return handler(message, u, reterr_error);
}
 
static int method_enqueue_unit_job(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        /* We don't bother with GENERIC_UNIT_VALIDATE_LOADED here, as the job logic validates that anyway */
        return method_generic_unit_operation(message, userdata, reterr_error, _UNIT_TYPE_INVALID, bus_unit_method_enqueue_job, GENERIC_UNIT_LOAD);
}
 
static int method_enqueue_unit_job_many(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        Manager *m = ASSERT_PTR(userdata);
        _cleanup_strv_free_ char **names = NULL;
        _cleanup_set_free_ Set *jobs = NULL;
        const char *jtype, *smode;
        JobType type;
        JobMode mode;
        uint64_t flags;
        bool reload_if_possible;
        Job *j;
        int r;
 
        assert(message);
 
        r = sd_bus_message_read_strv(message, &names);
        if (r < 0)
                return r;
 
        if (strv_isempty(names))
                return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "No units specified.");
 
        if (strv_length(names) > (unsigned) MANAGER_MAX_NAMES / 2)
                return sd_bus_error_set(reterr_error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Too many unit names requested.");
 
        r = sd_bus_message_read(message, "sst", &jtype, &smode, &flags);
        if (r < 0)
                return r;
 
        if (flags != 0)
                return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid flags parameter, must be 0.");
 
        r = bus_unit_parse_job_type(jtype, &type, &reload_if_possible, reterr_error);
        if (r < 0)
                return r;
 
        mode = job_mode_from_string(smode);
        if (mode < 0)
                return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s invalid", smode);
 
        r = mac_selinux_access_check(message, job_type_to_access_method(type), reterr_error);
        if (r < 0)
                return r;
 
        r = bus_verify_manage_units_async(m, message, reterr_error);
        if (r < 0)
                return r;
        if (r == 0)
                return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
 
        jobs = set_new(NULL);
        if (!jobs)
                return -ENOMEM;
 
        r = manager_add_jobs(m, type, names, reload_if_possible, mode,
                             /* extra_flags= */ 0, /* affected_jobs= */ NULL, reterr_error, jobs);
        if (r < 0)
                return r;
 
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
        r = sd_bus_message_new_method_return(message, &reply);
        if (r < 0)
                return r;
 
        r = sd_bus_message_open_container(reply, 'a', "(uosos)");
        if (r < 0)
                return r;
 
        SET_FOREACH(j, jobs) {
                _cleanup_free_ char *job_path = NULL, *unit_path = NULL;
 
                r = bus_job_track_sender(j, message);
                if (r < 0)
                        return r;
 
                bus_job_send_pending_change_signal(j, true);
 
                job_path = job_dbus_path(j);
                if (!job_path)
                        return -ENOMEM;
 
                unit_path = unit_dbus_path(j->unit);
                if (!unit_path)
                        return -ENOMEM;
 
                r = sd_bus_message_append(reply, "(uosos)",
                                          j->id, job_path,
                                          j->unit->id, unit_path,
                                          job_type_to_string(j->type));
                if (r < 0)
                        return r;
        }
 
        r = sd_bus_message_close_container(reply);
        if (r < 0)
                return r;
 
        return sd_bus_message_send(reply);
}
 
static int method_start_unit_replace(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        Manager *m = ASSERT_PTR(userdata);
        const char *old_name;
        Unit *u;
        int r;
 
        assert(message);
 
        r = sd_bus_message_read(message, "s", &old_name);
        if (r < 0)
                return r;
 
        r = bus_get_unit_by_name(m, message, old_name, &u, reterr_error);
        if (r < 0)
                return r;
        if (!u->job || u->job->type != JOB_START)
                return sd_bus_error_setf(reterr_error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
 
        return method_start_unit_generic(message, m, JOB_START, /* reload_if_possible= */ false, reterr_error);
}
 
static int method_kill_unit(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        /* We don't bother with GENERIC_UNIT_LOAD or GENERIC_UNIT_VALIDATE_LOADED here, as it shouldn't
         * matter whether a unit is loaded for killing any processes in the unit's cgroup. */
        return method_generic_unit_operation(message, userdata, reterr_error, _UNIT_TYPE_INVALID, bus_unit_method_kill, /* flags= */ 0);
}
 
static int method_kill_unit_subgroup(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        /* We don't bother with GENERIC_UNIT_LOAD or GENERIC_UNIT_VALIDATE_LOADED here, as it shouldn't
         * matter whether a unit is loaded for killing any processes in the unit's cgroup. */
        return method_generic_unit_operation(message, userdata, reterr_error, _UNIT_TYPE_INVALID, bus_unit_method_kill_subgroup, /* flags= */ 0);
}
 
static int method_clean_unit(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        /* Load the unit if necessary, in order to load it, and insist on the unit being loaded to be
         * cleaned */
        return method_generic_unit_operation(message, userdata, reterr_error, _UNIT_TYPE_INVALID, bus_unit_method_clean, GENERIC_UNIT_LOAD|GENERIC_UNIT_VALIDATE_LOADED);
}
 
static int method_freeze_unit(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        /* Only active units can be frozen, which must be properly loaded already */
        return method_generic_unit_operation(message, userdata, reterr_error, _UNIT_TYPE_INVALID, bus_unit_method_freeze, GENERIC_UNIT_VALIDATE_LOADED);
}
 
static int method_thaw_unit(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        /* Same as freeze above */
        return method_generic_unit_operation(message, userdata, reterr_error, _UNIT_TYPE_INVALID, bus_unit_method_thaw, GENERIC_UNIT_VALIDATE_LOADED);
}
 
static int method_reset_failed_unit(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
        /* Don't load the unit (because unloaded units can't be in failed state), and don't insist on the
         * unit to be loaded properly (since a failed unit might have its unit file disappeared) */