/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "sd-bus.h"
#include "alloc-util.h"
#include "bus-common-errors.h"
#include "cgroup-util.h"
#include "dbus-unit.h"
#include "dbus.h"
#include "locale-util.h"
#include "log.h"
#include "selinux-access.h"
#include "special.h"
#include "string-util.h"
#include "strv.h"
#include "user-util.h"
static int property_get_names(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
Iterator i;
const char *t;
int r;
assert(u);
if (r < 0)
return r;
SET_FOREACH(t, u->names, i) {
if (r < 0)
return r;
}
return sd_bus_message_close_container(reply);
}
static int property_get_following(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
assert(u);
f = unit_following(u);
}
static int property_get_dependencies(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
Iterator j;
Unit *u;
int r;
if (r < 0)
return r;
SET_FOREACH(u, s, j) {
if (r < 0)
return r;
}
return sd_bus_message_close_container(reply);
}
static int property_get_obsolete_dependencies(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
/* For dependency types we don't support anymore always return an empty array */
}
static int property_get_description(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
assert(u);
}
static int property_get_active_state(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
assert(u);
}
static int property_get_sub_state(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
assert(u);
}
static int property_get_unit_file_preset(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
int r;
assert(u);
r = unit_get_unit_file_preset(u);
r < 0 ? "":
r > 0 ? "enabled" : "disabled");
}
static int property_get_unit_file_state(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
assert(u);
}
static int property_get_can_start(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
assert(u);
}
static int property_get_can_stop(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
assert(u);
/* On the lower levels we assume that every unit we can start
* we can also stop */
}
static int property_get_can_reload(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
assert(u);
}
static int property_get_can_isolate(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
assert(u);
}
static int property_get_job(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
_cleanup_free_ char *p = NULL;
assert(u);
if (!u->job)
p = job_dbus_path(u->job);
if (!p)
return -ENOMEM;
}
static int property_get_need_daemon_reload(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
assert(u);
}
static int property_get_conditions(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
int r;
if (r < 0)
return r;
int tristate;
tristate =
c->result == CONDITION_UNTESTED ? 0 :
if (r < 0)
return r;
}
return sd_bus_message_close_container(reply);
}
static int property_get_load_error(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
assert(u);
if (u->load_error != 0)
sd_bus_error_set_errno(&e, u->load_error);
}
static int bus_verify_manage_units_async_full(
Unit *u,
const char *verb,
int capability,
const char *polkit_message,
sd_bus_error *error) {
"unit", u->id,
"verb", verb,
};
if (polkit_message) {
}
return bus_verify_polkit_async(call, capability, "org.freedesktop.systemd1.manage-units", details, false, UID_INVALID, &u->manager->polkit_registry, error);
}
Unit *u,
bool reload_if_possible,
sd_bus_error *error) {
const char *smode;
};
int r;
assert(u);
u, message,
error);
if (r < 0)
return r;
if (r < 0)
return r;
if (mode < 0)
if (reload_if_possible)
else
if (!verb)
return -ENOMEM;
u,
verb,
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 */
}
}
}
}
}
}
}
static int method_reload_or_try_restart(sd_bus_message *message, void *userdata, sd_bus_error *error) {
}
const char *swho;
int r;
assert(u);
if (r < 0)
return r;
if (r < 0)
return r;
else {
if (who < 0)
}
u,
"kill",
N_("Authentication is required to kill '$(unit)'."),
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 */
if (r < 0)
return r;
}
int r;
assert(u);
if (r < 0)
return r;
u,
"reset-failed",
N_("Authentication is required to reset the \"failed\" state of '$(unit)'."),
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 */
}
int runtime, r;
assert(u);
if (r < 0)
return r;
if (r < 0)
return r;
u,
"set-property",
N_("Authentication is required to set properties on '$(unit)'."),
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 */
if (r < 0)
return r;
}
SD_BUS_PROPERTY("Requires", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUIRES]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Requisite", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUISITE]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Wants", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_WANTS]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("BindsTo", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BINDS_TO]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("PartOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_PART_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("RequiredBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUIRED_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("RequisiteOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_REQUISITE_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("WantedBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_WANTED_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("BoundBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BOUND_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("ConsistsOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONSISTS_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Conflicts", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONFLICTS]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("ConflictedBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_CONFLICTED_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Before", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_BEFORE]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("After", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_AFTER]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OnFailure", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_ON_FAILURE]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Triggers", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_TRIGGERS]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("TriggeredBy", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_TRIGGERED_BY]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("PropagatesReloadTo", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_PROPAGATES_RELOAD_TO]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("ReloadPropagatedFrom", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_RELOAD_PROPAGATED_FROM]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("JoinsNamespaceOf", "as", property_get_dependencies, offsetof(Unit, dependencies[UNIT_JOINS_NAMESPACE_OF]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("RequiresOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN),
SD_BUS_PROPERTY("RequisiteOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN),
SD_BUS_PROPERTY("RequiredByOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN),
SD_BUS_PROPERTY("RequisiteOfOverridable", "as", property_get_obsolete_dependencies, 0, SD_BUS_VTABLE_HIDDEN),
SD_BUS_PROPERTY("RequiresMountsFor", "as", NULL, offsetof(Unit, requires_mounts_for), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Documentation", "as", NULL, offsetof(Unit, documentation), 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("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),
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("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("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, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("JobTimeoutUSec", "t", bus_property_get_usec, offsetof(Unit, job_timeout), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("JobTimeoutAction", "s", property_get_failure_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),
SD_BUS_PROPERTY("ConditionResult", "b", bus_property_get_bool, offsetof(Unit, condition_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("AssertResult", "b", bus_property_get_bool, offsetof(Unit, assert_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
BUS_PROPERTY_DUAL_TIMESTAMP("ConditionTimestamp", offsetof(Unit, condition_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
BUS_PROPERTY_DUAL_TIMESTAMP("AssertTimestamp", offsetof(Unit, assert_timestamp), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("Transient", "b", bus_property_get_bool, offsetof(Unit, transient), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec, offsetof(Unit, start_limit.interval), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned, offsetof(Unit, start_limit.burst), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("StartLimitAction", "s", property_get_failure_action, offsetof(Unit, start_limit_action), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("RebootArgument", "s", NULL, offsetof(Unit, reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_METHOD("ReloadOrTryRestart", "s", "o", method_reload_or_try_restart, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("SetProperties", "ba(sv)", NULL, bus_unit_method_set_properties, SD_BUS_VTABLE_UNPRIVILEGED),
};
static int property_get_slice(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
assert(u);
}
static int property_get_current_memory(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
int r;
assert(u);
r = unit_get_memory_current(u, &sz);
if (r < 0 && r != -ENODATA)
log_unit_warning_errno(u, r, "Failed to get memory.usage_in_bytes attribute: %m");
}
static int property_get_current_tasks(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
int r;
assert(u);
r = unit_get_tasks_current(u, &cn);
if (r < 0 && r != -ENODATA)
log_unit_warning_errno(u, r, "Failed to get pids.current attribute: %m");
}
static int property_get_cpu_usage(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
int r;
assert(u);
r = unit_get_cpu_usage(u, &ns);
if (r < 0 && r != -ENODATA)
log_unit_warning_errno(u, r, "Failed to get cpuacct.usage attribute: %m");
}
static int property_get_cgroup(
const char *path,
const char *interface,
const char *property,
void *userdata,
sd_bus_error *error) {
const char *t;
assert(u);
/* Three cases: a) u->cgroup_path is NULL, in which case the
* unit has no control group, which we report as the empty
* string. b) u->cgroup_path is the empty string, which
* indicates the root cgroup, which we report as "/". c) all
* other cases we report as-is. */
if (u->cgroup_path)
else
t = "";
}
};
_cleanup_free_ char *p = NULL;
int r;
assert(u);
p = unit_dbus_path(u);
if (!p)
return -ENOMEM;
bus,
&m,
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"UnitNew");
if (r < 0)
return r;
if (r < 0)
return r;
}
_cleanup_free_ char *p = NULL;
int r;
assert(u);
p = unit_dbus_path(u);
if (!p)
return -ENOMEM;
/* Send a properties changed signal. First for the specific
* type, then for the generic unit. The clients may rely on
* this order to get atomic behavior if needed. */
bus, p,
NULL);
if (r < 0)
return r;
bus, p,
"org.freedesktop.systemd1.Unit",
NULL);
}
int r;
assert(u);
if (u->in_dbus_queue) {
u->in_dbus_queue = false;
}
if (!u->id)
return;
r = bus_foreach_bus(u->manager, NULL, u->sent_dbus_new_signal ? send_changed_signal : send_new_signal, u);
if (r < 0)
u->sent_dbus_new_signal = true;
}
_cleanup_free_ char *p = NULL;
int r;
assert(u);
p = unit_dbus_path(u);
if (!p)
return -ENOMEM;
bus,
&m,
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"UnitRemoved");
if (r < 0)
return r;
if (r < 0)
return r;
}
int r;
assert(u);
if (!u->sent_dbus_new_signal)
if (!u->id)
return;
if (r < 0)
}
int bus_unit_queue_job(
Unit *u,
bool reload_if_possible,
sd_bus_error *error) {
Job *j;
int r;
assert(u);
u, message,
error);
if (r < 0)
return r;
if (reload_if_possible && unit_can_reload(u)) {
if (type == JOB_RESTART)
else if (type == JOB_TRY_RESTART)
}
unit_active_state(u) == UNIT_INACTIVE)
((type == JOB_RESTART || type == JOB_TRY_RESTART) && (u->refuse_manual_start || u->refuse_manual_stop)) ||
return sd_bus_error_setf(error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, unit %s may be requested by dependency only.", u->id);
if (r < 0)
return r;
if (!j->clients) {
if (r < 0)
return r;
}
if (r < 0)
return r;
}
path = job_dbus_path(j);
if (!path)
return -ENOMEM;
}
static int bus_unit_set_transient_property(
Unit *u,
const char *name,
sd_bus_error *error) {
int r;
assert(u);
const char *d;
if (r < 0)
return r;
if (mode != UNIT_CHECK) {
r = unit_set_description(u, d);
if (r < 0)
return r;
}
return 1;
int b;
if (r < 0)
return r;
if (mode != UNIT_CHECK) {
u->default_dependencies = b;
}
return 1;
const char *s;
if (!UNIT_HAS_CGROUP_CONTEXT(u))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "The slice property is only available for units with control groups.");
if (u->type == UNIT_SLICE)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Slice may not be set for slice units.");
if (unit_has_name(u, SPECIAL_INIT_SCOPE))
if (r < 0)
return r;
if (!unit_name_is_valid(s, UNIT_NAME_PLAIN))
if (r < 0)
return r;
if (mode != UNIT_CHECK) {
r = unit_set_slice(u, slice);
if (r < 0)
return r;
}
return 1;
} else if (STR_IN_SET(name,
"Requires", "RequiresOverridable",
"Requisite", "RequisiteOverridable",
"Wants",
"BindsTo",
"Conflicts",
"Before", "After",
"OnFailure",
"PropagatesReloadTo", "ReloadPropagatedFrom",
"PartOf")) {
const char *other;
d = UNIT_REQUIRES; /* redirect for obsolete unit dependency type */
d = UNIT_REQUISITE; /* same here */
else {
if (d < 0)
}
if (r < 0)
return r;
if (mode != UNIT_CHECK) {
if (r < 0)
return r;
if (!label)
return -ENOMEM;
}
}
if (r < 0)
return r;
if (r < 0)
return r;
return 1;
}
return 0;
}
Unit *u,
bool commit,
sd_bus_error *error) {
bool for_real = false;
unsigned n = 0;
int r;
assert(u);
/* We iterate through the array twice. First run we just check
* if all passed data is valid, second run actually applies
* it. This is to implement transaction-like behaviour without
* actually providing full transactions. */
if (r < 0)
return r;
for (;;) {
const char *name;
if (r < 0)
return r;
if (r == 0) {
break;
/* Reached EOF. Let's try again, and this time for realz... */
r = sd_bus_message_rewind(message, false);
if (r < 0)
return r;
for_real = true;
continue;
}
if (r < 0)
return r;
if (!UNIT_VTABLE(u)->bus_set_property)
return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Objects of this type do not support setting properties.");
if (r < 0)
return r;
if (r < 0)
return r;
if (r == 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_PROPERTY_READ_ONLY, "Cannot set property %s, or unknown property.", name);
if (r < 0)
return r;
if (r < 0)
return r;
n += for_real;
}
if (r < 0)
return r;
UNIT_VTABLE(u)->bus_commit_properties(u);
return n;
}
if (u->load_state == UNIT_LOADED)
return 0;
/* Give a better description of the unit error when
* possible. Note that in the case of UNIT_MASKED, load_error
* is not set. */
if (u->load_state == UNIT_MASKED)
if (u->load_state == UNIT_NOT_FOUND)
}