HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/core/dbus-unit.c
Git blob: db9688a7c7f1b455a1a7e1b100b5c3c6e2ab5d0e
Lines 1–1000 of 2,807
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <unistd.h>
#include "sd-bus.h"
#include "alloc-util.h"
#include "bitfield.h"
#include "bus-common-errors.h"
#include "bus-get-properties.h"
#include "bus-util.h"
#include "cgroup-util.h"
#include "condition.h"
#include "dbus.h"
#include "dbus-job.h"
#include "dbus-manager.h"
#include "dbus-unit.h"
#include "dbus-util.h"
#include "fd-util.h"
#include "format-util.h"
#include "install.h"
#include "locale-util.h"
#include "log.h"
#include "manager.h"
#include "namespace-util.h"
#include "path-util.h"
#include "pidref.h"
#include "process-util.h"
#include "selinux-access.h"
#include "set.h"
#include "signal-util.h"
#include "special.h"
#include "string-util.h"
#include "strv.h"
#include "transaction.h" /* IWYU pragma: keep */
#include "unit-name.h"
#include "user-util.h"
#include "web-util.h"
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_collect_mode, collect_mode, CollectMode);
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_load_state, unit_load_state, UnitLoadState);
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_job_mode, job_mode, JobMode);
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_freezer_state, freezer_state, FreezerState);
static BUS_DEFINE_PROPERTY_GET(property_get_description, "s", Unit, unit_description);
static BUS_DEFINE_PROPERTY_GET2(property_get_active_state, "s", Unit, unit_active_state, unit_active_state_to_string);
static BUS_DEFINE_PROPERTY_GET(property_get_sub_state, "s", Unit, unit_sub_state_to_string);
static BUS_DEFINE_PROPERTY_GET2(property_get_unit_file_state, "s", Unit, unit_get_unit_file_state, unit_file_state_to_string);
static BUS_DEFINE_PROPERTY_GET(property_get_can_reload, "b", Unit, unit_can_reload);
static BUS_DEFINE_PROPERTY_GET(property_get_can_start, "b", Unit, unit_can_start_refuse_manual);
static BUS_DEFINE_PROPERTY_GET(property_get_can_stop, "b", Unit, unit_can_stop_refuse_manual);
static BUS_DEFINE_PROPERTY_GET(property_get_can_isolate, "b", Unit, unit_can_isolate_refuse_manual);
static BUS_DEFINE_PROPERTY_GET(property_get_can_freeze, "b", Unit, unit_can_freeze);
static BUS_DEFINE_PROPERTY_GET(property_get_need_daemon_reload, "b", Unit, unit_need_daemon_reload);
static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_empty_strv, "as", 0);
static int property_get_can_clean(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *reterr_error) {
Unit *u = userdata;
ExecCleanMask mask;
int r;
assert(bus);
assert(reply);
r = unit_can_clean(u, &mask);
if (r < 0)
return r;
r = sd_bus_message_open_container(reply, 'a', "s");
if (r < 0)
return r;
for (ExecDirectoryType t = 0; t < _EXEC_DIRECTORY_TYPE_MAX; t++) {
if (!BIT_SET(mask, t))
continue;
r = sd_bus_message_append(reply, "s", exec_resource_type_to_string(t));
if (r < 0)
return r;
}
if (FLAGS_SET(mask, EXEC_CLEAN_FDSTORE)) {
r = sd_bus_message_append(reply, "s", "fdstore");
if (r < 0)
return r;
}
return sd_bus_message_close_container(reply);
}
static int property_get_can_live_mount(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *reterr_error) {
Unit *u = ASSERT_PTR(userdata);
assert(bus);
assert(reply);
return sd_bus_message_append(reply, "b", unit_can_live_mount(u, /* reterr_error= */ NULL) >= 0);
}
static int property_get_names(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *reterr_error) {
Unit *u = ASSERT_PTR(userdata);
const char *t;
int r;
assert(bus);
assert(reply);
r = sd_bus_message_open_container(reply, 'a', "s");
if (r < 0)
return r;
r = sd_bus_message_append(reply, "s", u->id);
if (r < 0)
return r;
SET_FOREACH(t, u->aliases) {
r = sd_bus_message_append(reply, "s", t);
if (r < 0)
return r;
}
return sd_bus_message_close_container(reply);
}
static int property_get_following(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *reterr_error) {
Unit *u = userdata, *f;
assert(bus);
assert(reply);
assert(u);
f = unit_following(u);
return sd_bus_message_append(reply, "s", f ? f->id : NULL);
}
static int property_get_dependencies(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *reterr_error) {
Unit *u = userdata, *other;
UnitDependency d;
Hashmap *deps;
void *v;
int r;
assert(bus);
assert(reply);
assert(u);
d = unit_dependency_from_string(property);
assert_se(d >= 0);
deps = unit_get_dependencies(u, d);
r = sd_bus_message_open_container(reply, 'a', "s");
if (r < 0)
return r;
HASHMAP_FOREACH_KEY(v, other, deps) {
r = sd_bus_message_append(reply, "s", other->id);
if (r < 0)
return r;
}
return sd_bus_message_close_container(reply);
}
static int property_get_mounts_for(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *reterr_error) {
Hashmap **h = ASSERT_PTR(userdata);
const char *p;
void *v;
int r;
assert(bus);
assert(reply);
r = sd_bus_message_open_container(reply, 'a', "s");
if (r < 0)
return r;
HASHMAP_FOREACH_KEY(v, p, *h) {
r = sd_bus_message_append(reply, "s", p);
if (r < 0)
return r;
}
return sd_bus_message_close_container(reply);
}
static int property_get_unit_file_preset(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *reterr_error) {
Unit *u = ASSERT_PTR(userdata);
int r;
assert(bus);
assert(reply);
r = unit_get_unit_file_preset(u);
return sd_bus_message_append(reply, "s", preset_action_past_tense_to_string(r));
}
static int property_get_job(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *reterr_error) {
_cleanup_free_ char *p = NULL;
Job **j = ASSERT_PTR(userdata);
assert(bus);
assert(reply);
if (!*j)
return sd_bus_message_append(reply, "(uo)", 0, "/");
p = job_dbus_path(*j);
if (!p)
return -ENOMEM;
return sd_bus_message_append(reply, "(uo)", (*j)->id, p);
}
static int property_get_conditions(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *reterr_error) {
const char *(*to_string)(ConditionType type) = NULL;
Condition **list = ASSERT_PTR(userdata);
int r;
assert(bus);
assert(reply);
to_string = streq(property, "Asserts") ? assert_type_to_string : condition_type_to_string;
r = sd_bus_message_open_container(reply, 'a', "(sbbsi)");
if (r < 0)
return r;
LIST_FOREACH(conditions, c, *list) {
int tristate;
tristate =
c->result == CONDITION_UNTESTED ? 0 :
c->result == CONDITION_SUCCEEDED ? 1 : -1;
r = sd_bus_message_append(reply, "(sbbsi)",
to_string(c->type),
c->trigger, c->negate,
c->parameter, tristate);
if (r < 0)
return r;
}
return sd_bus_message_close_container(reply);
}
static int property_get_load_error(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *reterr_error) {
_cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
Unit *u = ASSERT_PTR(userdata);
int r;
assert(bus);
assert(reply);
r = bus_unit_validate_load_state(u, &e);
if (r < 0)
return sd_bus_message_append(reply, "(ss)", e.name, e.message);
return sd_bus_message_append(reply, "(ss)", NULL, NULL);
}
static int property_get_markers(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *reterr_error) {
unsigned *markers = ASSERT_PTR(userdata);
int r;
assert(bus);
assert(reply);
r = sd_bus_message_open_container(reply, 'a', "s");
if (r < 0)
return r;
BIT_FOREACH(m, *markers) {
r = sd_bus_message_append(reply, "s", unit_marker_to_string(m));
if (r < 0)
return r;
}
return sd_bus_message_close_container(reply);
}
static const char *const polkit_message_for_job[_JOB_TYPE_MAX] = {
[JOB_START] = N_("Authentication is required to start '$(unit)'."),
[JOB_STOP] = N_("Authentication is required to stop '$(unit)'."),
[JOB_RELOAD] = N_("Authentication is required to reload '$(unit)'."),
[JOB_RESTART] = N_("Authentication is required to restart '$(unit)'."),
[JOB_TRY_RESTART] = N_("Authentication is required to restart '$(unit)'."),
};
int bus_unit_method_start_generic(
sd_bus_message *message,
Unit *u,
JobType job_type,
bool reload_if_possible,
sd_bus_error *reterr_error) {
BusUnitQueueFlags job_flags = reload_if_possible ? BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE : 0;
const char *smode, *verb;
JobMode mode;
int r;
assert(message);
assert(u);
assert(job_type >= 0 && job_type < _JOB_TYPE_MAX);
r = mac_selinux_unit_access_check(
u, message,
job_type_to_access_method(job_type),
reterr_error);
if (r < 0)
return r;
r = sd_bus_message_read(message, "s", &smode);
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);
if (reload_if_possible)
verb = strjoina("reload-or-", job_type_to_string(job_type));
else
verb = job_type_to_string(job_type);
if (sd_bus_message_is_method_call(message, NULL, "StartUnitWithFlags")) {
uint64_t input_flags = 0;
r = sd_bus_message_read(message, "t", &input_flags);
if (r < 0)
return r;
/* Let clients know that this version doesn't support any flags at the moment. */
if (input_flags != 0)
return sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_ARGS,
"Invalid 'flags' parameter '%" PRIu64 "'",
input_flags);
}
r = bus_verify_manage_units_async_full(
u,
verb,
polkit_message_for_job[job_type],
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 */
return bus_unit_queue_job(message, u, job_type, mode, job_flags, reterr_error);
}
static int bus_unit_method_start(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return bus_unit_method_start_generic(message, userdata, JOB_START, false, reterr_error);
}
static int bus_unit_method_stop(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return bus_unit_method_start_generic(message, userdata, JOB_STOP, false, reterr_error);
}
static int bus_unit_method_reload(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return bus_unit_method_start_generic(message, userdata, JOB_RELOAD, false, reterr_error);
}
static int bus_unit_method_restart(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return bus_unit_method_start_generic(message, userdata, JOB_RESTART, false, reterr_error);
}
static int bus_unit_method_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, false, reterr_error);
}
static int bus_unit_method_reload_or_restart(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return bus_unit_method_start_generic(message, userdata, JOB_RESTART, true, reterr_error);
}
static int bus_unit_method_reload_or_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return bus_unit_method_start_generic(message, userdata, JOB_TRY_RESTART, true, reterr_error);
}
int bus_unit_parse_job_type(
const char *jtype,
JobType *ret_type,
bool *ret_reload_if_possible,
sd_bus_error *reterr_error) {
JobType type;
bool reload_if_possible = false;
assert(jtype);
assert(ret_type);
assert(ret_reload_if_possible);
/* Parses the job type string as accepted by the EnqueueUnitJob()/EnqueueUnitJobMany() bus methods. The
* two magic "reload-or-…" types are handled manually, the rest generically. The actual
* reload-vs-restart choice is unit-specific and applied per-unit later. */
if (streq(jtype, "reload-or-restart")) {
type = JOB_RESTART;
reload_if_possible = true;
} else if (streq(jtype, "reload-or-try-restart")) {
type = JOB_TRY_RESTART;
reload_if_possible = true;
} else {
type = job_type_from_string(jtype);
if (type < 0)
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Job type %s invalid", jtype);
}
*ret_type = type;
*ret_reload_if_possible = reload_if_possible;
return 0;
}
int bus_unit_method_enqueue_job(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
BusUnitQueueFlags flags = BUS_UNIT_QUEUE_VERBOSE_REPLY;
const char *jtype, *smode;
Unit *u = ASSERT_PTR(userdata);
bool reload_if_possible;
JobType type;
JobMode mode;
int r;
assert(message);
r = sd_bus_message_read(message, "ss", &jtype, &smode);
if (r < 0)
return r;
r = bus_unit_parse_job_type(jtype, &type, &reload_if_possible, reterr_error);
if (r < 0)
return r;
if (reload_if_possible)
flags |= BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE;
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_unit_access_check(
u, message,
job_type_to_access_method(type),
reterr_error);
if (r < 0)
return r;
r = bus_verify_manage_units_async_full(
u,
jtype,
polkit_message_for_job[type],
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 */
return bus_unit_queue_job(message, u, type, mode, flags, reterr_error);
}
int bus_unit_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
Unit *u = ASSERT_PTR(userdata);
int32_t value = 0;
const char *swhom;
int32_t signo;
KillWhom whom;
int r, code;
assert(message);
r = mac_selinux_unit_access_check(u, message, "stop", reterr_error);
if (r < 0)
return r;
r = sd_bus_message_read(message, "si", &swhom, &signo);
if (r < 0)
return r;
if (startswith(sd_bus_message_get_member(message), "QueueSignal")) {
r = sd_bus_message_read(message, "i", &value);
if (r < 0)
return r;
code = SI_QUEUE;
} else
code = SI_USER;
if (isempty(swhom))
whom = KILL_ALL;
else {
whom = kill_whom_from_string(swhom);
if (whom < 0)
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid whom argument: %s", swhom);
}
if (!SIGNAL_VALID(signo))
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Signal number out of range.");
if (code == SI_QUEUE && !((signo >= SIGRTMIN) && (signo <= SIGRTMAX)))
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS,
"Value parameter only accepted for realtime signals (SIGRTMIN…SIGRTMAX), refusing for signal SIG%s.", signal_to_string(signo));
r = bus_verify_manage_units_async_full(
u,
"kill",
N_("Authentication is required to send a UNIX signal to the processes of '$(unit)'."),
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 */
r = unit_kill(u, whom, /* subgroup= */ NULL, signo, code, value, reterr_error);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, NULL);
}
int bus_unit_method_kill_subgroup(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
Unit *u = ASSERT_PTR(userdata);
int r;
assert(message);
r = mac_selinux_unit_access_check(u, message, "stop", reterr_error);
if (r < 0)
return r;
const char *swhom, *subgroup;
int32_t signo;
r = sd_bus_message_read(message, "ssi", &swhom, &subgroup, &signo);
if (r < 0)
return r;
KillWhom whom;
if (isempty(swhom))
whom = KILL_CGROUP;
else {
whom = kill_whom_from_string(swhom);
if (whom < 0)
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid whom argument: %s", swhom);
}
if (isempty(subgroup))
subgroup = NULL;
else if (!path_is_normalized(subgroup))
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Specified cgroup sub-path is not valid.");
else if (!IN_SET(whom, KILL_CGROUP, KILL_CGROUP_FAIL))
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Subgroup can only be specified in combination with 'cgroup' or 'cgroup-fail'.");
if (!SIGNAL_VALID(signo))
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Signal number out of range.");
r = bus_verify_manage_units_async_full(
u,
"kill-subgroup",
N_("Authentication is required to send a UNIX signal to the processes of subgroup of '$(unit)'."),
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 */
r = unit_kill(u, whom, subgroup, signo, SI_USER, /* value= */ 0, reterr_error);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, NULL);
}
int bus_unit_method_reset_failed(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
Unit *u = ASSERT_PTR(userdata);
int r;
assert(message);
r = mac_selinux_unit_access_check(u, message, "reload", reterr_error);
if (r < 0)
return r;
r = bus_verify_manage_units_async_full(
u,
"reset-failed",
N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
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 */
unit_reset_failed(u);
return sd_bus_reply_method_return(message, NULL);
}
int bus_unit_method_set_properties(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
Unit *u = ASSERT_PTR(userdata);
int runtime, r;
assert(message);
r = mac_selinux_unit_access_check(u, message, "start", reterr_error);
if (r < 0)
return r;
r = sd_bus_message_read(message, "b", &runtime);
if (r < 0)
return r;
r = bus_verify_manage_units_async_full(
u,
"set-property",
N_("Authentication is required to set properties on '$(unit)'."),
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 */
r = bus_unit_set_properties(u, message, runtime ? UNIT_RUNTIME : UNIT_PERSISTENT, true, reterr_error);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, NULL);
}
int bus_unit_method_ref(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
Unit *u = ASSERT_PTR(userdata);
int r;
assert(message);
r = mac_selinux_unit_access_check(u, message, "start", reterr_error);
if (r < 0)
return r;
r = bus_verify_manage_units_async_full(
u,
"ref",
/* polkit_message= */ NULL,
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 */
r = bus_unit_track_add_sender(u, message);
if (r < 0)
return r;
return sd_bus_reply_method_return(message, NULL);
}
int bus_unit_method_unref(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
Unit *u = ASSERT_PTR(userdata);
int r;
assert(message);
r = bus_unit_track_remove_sender(u, message);
if (r == -EUNATCH)
return sd_bus_error_set(reterr_error, BUS_ERROR_NOT_REFERENCED, "Unit has not been referenced yet.");
if (r < 0)
return r;
return sd_bus_reply_method_return(message, NULL);
}
int bus_unit_method_clean(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
ExecCleanMask mask = 0;
Unit *u = ASSERT_PTR(userdata);
int r;
assert(message);
r = mac_selinux_unit_access_check(u, message, "stop", reterr_error);
if (r < 0)
return r;
r = sd_bus_message_enter_container(message, 'a', "s");
if (r < 0)
return r;
for (;;) {
ExecCleanMask m;
const char *i;
r = sd_bus_message_read(message, "s", &i);
if (r < 0)
return r;
if (r == 0)
break;
m = exec_clean_mask_from_string(i);
if (m < 0)
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid resource type: %s", i);
mask |= m;
}
r = sd_bus_message_exit_container(message);
if (r < 0)
return r;
r = bus_verify_manage_units_async_full(
u,
"clean",
N_("Authentication is required to delete files and directories associated with '$(unit)'."),
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 */
r = unit_clean(u, mask);
if (r == -EOPNOTSUPP)
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_NOT_SUPPORTED, "Unit '%s' does not support cleaning.", u->id);
if (r == -EUNATCH)
return sd_bus_error_set(reterr_error, BUS_ERROR_NOTHING_TO_CLEAN, "No matching resources found.");
if (r == -EBUSY)
return sd_bus_error_set(reterr_error, BUS_ERROR_UNIT_BUSY, "Unit is not inactive or has pending job.");
if (r < 0)
return r;
return sd_bus_reply_method_return(message, NULL);
}
static int bus_unit_method_freezer_generic(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error, FreezerAction action) {
Unit *u = ASSERT_PTR(userdata);
int r;
assert(message);
assert(IN_SET(action, FREEZER_FREEZE, FREEZER_THAW));
const char *perm = action == FREEZER_FREEZE ? "stop" : "start";
r = mac_selinux_unit_access_check(u, message, perm, reterr_error);
if (r < 0)
return r;
r = bus_verify_manage_units_async_full(
u,
perm,
N_("Authentication is required to freeze or thaw the processes of '$(unit)' unit."),
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 */
r = unit_freezer_action(u, action);
if (r == -EOPNOTSUPP)
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_NOT_SUPPORTED, "Unit does not support freeze/thaw");
if (r == -EBUSY)
return sd_bus_error_set(reterr_error, BUS_ERROR_UNIT_BUSY, "Unit has a pending job");
if (r == -EHOSTDOWN)
return sd_bus_error_set(reterr_error, BUS_ERROR_UNIT_INACTIVE, "Unit is not active");
if (r == -EALREADY)
return sd_bus_error_set(reterr_error, BUS_ERROR_UNIT_BUSY, "Previously requested freezer operation for unit is still in progress");
if (r == -EDEADLK)
return sd_bus_error_set(reterr_error, BUS_ERROR_FROZEN_BY_PARENT, "Unit is frozen by a parent slice");
if (r < 0)
return r;
bool reply_now = r == 0;
if (u->pending_freezer_invocation) {
bus_unit_send_pending_freezer_message(u, true);
assert(!u->pending_freezer_invocation);
}
u->pending_freezer_invocation = sd_bus_message_ref(message);
if (reply_now) {
r = bus_unit_send_pending_freezer_message(u, false);
if (r < 0)
return r;
}
return 1;
}
int bus_unit_method_thaw(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return bus_unit_method_freezer_generic(message, userdata, reterr_error, FREEZER_THAW);
}
int bus_unit_method_freeze(sd_bus_message *message, void *userdata, sd_bus_error *reterr_error) {
return bus_unit_method_freezer_generic(message, userdata, reterr_error, FREEZER_FREEZE);
}
static int property_get_refs(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *reterr_error) {
Unit *u = userdata;
int r;
assert(bus);
assert(reply);
r = sd_bus_message_open_container(reply, 'a', "s");
if (r < 0)
return r;
for (const char *i = sd_bus_track_first(u->bus_track); i; i = sd_bus_track_next(u->bus_track)) {
int c;
c = sd_bus_track_count_name(u->bus_track, i);
if (c < 0)
return c;
/* Add the item multiple times if the ref count for each is above 1 */
for (int k = 0; k < c; k++) {
r = sd_bus_message_append(reply, "s", i);
if (r < 0)
return r;
}
}
return sd_bus_message_close_container(reply);
}
const sd_bus_vtable bus_unit_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Unit, id), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Names", "as", property_get_names, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Following", "s", property_get_following, 0, 0),
SD_BUS_PROPERTY("Requires", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Wants", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Upholds", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("UpheldBy", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Before", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("After", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OnSuccess", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OnSuccessOf", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OnFailureOf", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("PropagatesStopTo", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("StopPropagatedFrom", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("SliceOf", "as", property_get_dependencies, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("RequiresMountsFor", "as", property_get_mounts_for, offsetof(Unit, mounts_for[UNIT_MOUNT_REQUIRES]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("WantsMountsFor", "as", property_get_mounts_for, offsetof(Unit, mounts_for[UNIT_MOUNT_WANTS]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Documentation", "as", NULL, offsetof(Unit, documentation), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Description", "s", property_get_description, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("AccessSELinuxContext", "s", NULL, offsetof(Unit, access_selinux_context), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("LoadState", "s", property_get_load_state, offsetof(Unit, load_state), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("ActiveState", "s", property_get_active_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("FreezerState", "s", property_get_freezer_state, offsetof(Unit, freezer_state), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("SubState", "s", property_get_sub_state, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("FragmentPath", "s", NULL, offsetof(Unit, fragment_path), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("SourcePath", "s", NULL, offsetof(Unit, source_path), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("DropInPaths", "as", NULL, offsetof(Unit, dropin_paths), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("UnitFileState", "s", property_get_unit_file_state, 0, 0),
SD_BUS_PROPERTY("UnitFilePreset", "s", property_get_unit_file_preset, 0, 0),
BUS_PROPERTY_DUAL_TIMESTAMP("StateChangeTimestamp", offsetof(Unit, state_change_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
BUS_PROPERTY_DUAL_TIMESTAMP("InactiveExitTimestamp", offsetof(Unit, inactive_exit_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
BUS_PROPERTY_DUAL_TIMESTAMP("ActiveEnterTimestamp", offsetof(Unit, active_enter_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
BUS_PROPERTY_DUAL_TIMESTAMP("ActiveExitTimestamp", offsetof(Unit, active_exit_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
BUS_PROPERTY_DUAL_TIMESTAMP("InactiveEnterTimestamp", offsetof(Unit, inactive_enter_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("CanStart", "b", property_get_can_start, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("CanStop", "b", property_get_can_stop, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("CanReload", "b", property_get_can_reload, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("CanIsolate", "b", property_get_can_isolate, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("CanClean", "as", property_get_can_clean, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("CanFreeze", "b", property_get_can_freeze, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("CanLiveMount", "b", property_get_can_live_mount, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Job", "(uo)", property_get_job, offsetof(Unit, job), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("StopWhenUnneeded", "b", bus_property_get_bool, offsetof(Unit, stop_when_unneeded), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("RefuseManualStart", "b", bus_property_get_bool, offsetof(Unit, refuse_manual_start), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("RefuseManualStop", "b", bus_property_get_bool, offsetof(Unit, refuse_manual_stop), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("AllowIsolate", "b", bus_property_get_bool, offsetof(Unit, allow_isolate), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("DefaultDependencies", "b", bus_property_get_bool, offsetof(Unit, default_dependencies), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("SurviveFinalKillSignal", "b", bus_property_get_bool, offsetof(Unit, survive_final_kill_signal), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OnSuccesJobMode", "s", property_get_job_mode, offsetof(Unit, on_success_job_mode), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), /* deprecated */
SD_BUS_PROPERTY("OnSuccessJobMode", "s", property_get_job_mode, offsetof(Unit, on_success_job_mode), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OnFailureJobMode", "s", property_get_job_mode, offsetof(Unit, on_failure_job_mode), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("IgnoreOnIsolate", "b", bus_property_get_bool, offsetof(Unit, ignore_on_isolate), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("NeedDaemonReload", "b", property_get_need_daemon_reload, 0, 0),
SD_BUS_PROPERTY("Markers", "as", property_get_markers, offsetof(Unit, markers), 0),
SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec, offsetof(Unit, job_timeout), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("JobRunningTimeoutUSec", "t", bus_property_get_usec, offsetof(Unit, job_running_timeout), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("JobTimeoutAction", "s", bus_property_get_emergency_action, offsetof(Unit, job_timeout_action), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("JobTimeoutRebootArgument", "s", NULL, offsetof(Unit, job_timeout_reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST),