HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/core/unit.c
Git blob: 1de634c975a3311494dacb28ce1bb5e67261717a
Lines 7001–7295 of 7,295
ActivationDetails *activation_details_new(Unit *trigger_unit) {
_cleanup_free_ ActivationDetails *details = NULL;
assert(trigger_unit);
assert(trigger_unit->type != _UNIT_TYPE_INVALID);
assert(trigger_unit->id);
details = malloc0(activation_details_vtable[trigger_unit->type]->object_size);
if (!details)
return NULL;
*details = (ActivationDetails) {
.n_ref = 1,
.trigger_unit_type = trigger_unit->type,
};
details->trigger_unit_name = strdup(trigger_unit->id);
if (!details->trigger_unit_name)
return NULL;
if (ACTIVATION_DETAILS_VTABLE(details)->init)
ACTIVATION_DETAILS_VTABLE(details)->init(details, trigger_unit);
return TAKE_PTR(details);
}
static ActivationDetails *activation_details_free(ActivationDetails *details) {
if (!details)
return NULL;
if (ACTIVATION_DETAILS_VTABLE(details)->done)
ACTIVATION_DETAILS_VTABLE(details)->done(details);
free(details->trigger_unit_name);
return mfree(details);
}
void activation_details_serialize(const ActivationDetails *details, FILE *f) {
if (!details || details->trigger_unit_type == _UNIT_TYPE_INVALID)
return;
(void) serialize_item(f, "activation-details-unit-type", unit_type_to_string(details->trigger_unit_type));
if (details->trigger_unit_name)
(void) serialize_item(f, "activation-details-unit-name", details->trigger_unit_name);
if (ACTIVATION_DETAILS_VTABLE(details)->serialize)
ACTIVATION_DETAILS_VTABLE(details)->serialize(details, f);
}
int activation_details_deserialize(const char *key, const char *value, ActivationDetails **details) {
int r;
assert(key);
assert(value);
assert(details);
if (!*details) {
UnitType t;
if (!streq(key, "activation-details-unit-type"))
return -EINVAL;
t = unit_type_from_string(value);
if (t < 0)
return t;
/* The activation details vtable has defined ops only for path and timer units */
if (!activation_details_vtable[t])
return -EINVAL;
*details = malloc0(activation_details_vtable[t]->object_size);
if (!*details)
return -ENOMEM;
**details = (ActivationDetails) {
.n_ref = 1,
.trigger_unit_type = t,
};
return 0;
}
if (streq(key, "activation-details-unit-name")) {
r = free_and_strdup(&(*details)->trigger_unit_name, value);
if (r < 0)
return r;
return 0;
}
if (ACTIVATION_DETAILS_VTABLE(*details)->deserialize)
return ACTIVATION_DETAILS_VTABLE(*details)->deserialize(key, value, details);
return -EINVAL;
}
int activation_details_append_env(const ActivationDetails *details, char ***strv) {
int r = 0;
assert(strv);
if (!details)
return 0;
if (!isempty(details->trigger_unit_name)) {
char *s = strjoin("TRIGGER_UNIT=", details->trigger_unit_name);
if (!s)
return -ENOMEM;
r = strv_consume(strv, TAKE_PTR(s));
if (r < 0)
return r;
}
if (ACTIVATION_DETAILS_VTABLE(details)->append_env) {
r = ACTIVATION_DETAILS_VTABLE(details)->append_env(details, strv);
if (r < 0)
return r;
}
return r + !isempty(details->trigger_unit_name); /* Return the number of variables added to the env block */
}
int activation_details_append_pair(const ActivationDetails *details, char ***strv) {
int r = 0;
assert(strv);
if (!details)
return 0;
if (!isempty(details->trigger_unit_name)) {
r = strv_extend_many(strv, "trigger_unit", details->trigger_unit_name);
if (r < 0)
return r;
}
if (ACTIVATION_DETAILS_VTABLE(details)->append_pair) {
r = ACTIVATION_DETAILS_VTABLE(details)->append_pair(details, strv);
if (r < 0)
return r;
}
return r + !isempty(details->trigger_unit_name); /* Return the number of pairs added to the strv */
}
DEFINE_TRIVIAL_REF_UNREF_FUNC(ActivationDetails, activation_details, activation_details_free);
static const char* const unit_mount_dependency_type_table[_UNIT_MOUNT_DEPENDENCY_TYPE_MAX] = {
[UNIT_MOUNT_WANTS] = "WantsMountsFor",
[UNIT_MOUNT_REQUIRES] = "RequiresMountsFor",
};
DEFINE_STRING_TABLE_LOOKUP(unit_mount_dependency_type, UnitMountDependencyType);
static const char* const oom_policy_table[_OOM_POLICY_MAX] = {
[OOM_CONTINUE] = "continue",
[OOM_STOP] = "stop",
[OOM_KILL] = "kill",
};
DEFINE_STRING_TABLE_LOOKUP(oom_policy, OOMPolicy);
UnitDependency unit_mount_dependency_type_to_dependency_type(UnitMountDependencyType t) {
switch (t) {
case UNIT_MOUNT_WANTS:
return UNIT_WANTS;
case UNIT_MOUNT_REQUIRES:
return UNIT_REQUIRES;
default:
assert_not_reached();
}
}
int unit_queue_job_check_and_mangle_type(
Unit *u,
JobType *type, /* input and output */
bool reload_if_possible,
sd_bus_error *reterr_error) {
JobType t;
assert(u);
assert(type);
t = *type;
if (reload_if_possible && unit_can_reload(u)) {
if (t == JOB_RESTART)
t = JOB_RELOAD_OR_START;
else if (t == JOB_TRY_RESTART)
t = JOB_TRY_RELOAD;
}
/* Our transaction logic allows units not properly loaded to be stopped. But if already dead
* let's return clear error to caller. */
if (t == JOB_STOP && UNIT_IS_LOAD_ERROR(u->load_state) && unit_active_state(u) == UNIT_INACTIVE)
return sd_bus_error_setf(reterr_error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", u->id);
if ((t == JOB_START && u->refuse_manual_start) ||
(t == JOB_STOP && u->refuse_manual_stop) ||
(IN_SET(t, JOB_RESTART, JOB_TRY_RESTART) && (u->refuse_manual_start || u->refuse_manual_stop)) ||
(t == JOB_RELOAD_OR_START && job_type_collapse(t, u) == JOB_START && u->refuse_manual_start))
return sd_bus_error_setf(reterr_error,
BUS_ERROR_ONLY_BY_DEPENDENCY,
"Operation refused, unit %s may be requested by dependency only (it is configured to refuse manual start/stop).",
u->id);
/* dbus-broker issues StartUnit for activation requests, and Type=dbus services automatically
* gain dependency on dbus.socket. Therefore, if dbus has a pending stop job, the new start
* job that pulls in dbus again would cause job type conflict. Let's avoid that by rejecting
* job enqueuing early.
*
* Note that unlike signal_activation_request(), we can't use unit_inactive_or_pending()
* here. StartUnit is a more generic interface, and thus users are allowed to use e.g. systemctl
* to start Type=dbus services even when dbus is inactive. */
if (t == JOB_START && u->type == UNIT_SERVICE && SERVICE(u)->type == SERVICE_DBUS)
FOREACH_STRING(dbus_unit, SPECIAL_DBUS_SOCKET, SPECIAL_DBUS_SERVICE) {
Unit *dbus = manager_get_unit(u->manager, dbus_unit);
if (dbus && unit_stop_pending(dbus))
return sd_bus_error_setf(reterr_error,
BUS_ERROR_SHUTTING_DOWN,
"Operation for unit %s refused, D-Bus is shutting down.",
u->id);
}
*type = t;
return 0;
}
int parse_unit_marker(const char *marker, unsigned *settings, unsigned *mask) {
bool some_plus_minus = false, b = true;
assert(marker);
assert(settings);
assert(mask);
if (IN_SET(marker[0], '+', '-')) {
b = marker[0] == '+';
marker++;
some_plus_minus = true;
}
UnitMarker m = unit_marker_from_string(marker);
if (m < 0)
return -EINVAL;
/* When +- are not used, last one wins, so reset the bitmask before storing the new result */
if (!some_plus_minus)
*settings = 0;
SET_FLAG(*settings, 1u << m, b);
SET_FLAG(*mask, 1u << m, true);
return some_plus_minus;
}
unsigned unit_normalize_markers(unsigned existing_markers, unsigned new_markers) {
/* Follow the job merging logic: when new markers conflict with existing ones, the new marker
* takes precedence and clears out conflicting existing markers. Then standard normalization
* resolves any remaining conflicts. */
/* New stop wins against all existing markers */
if (BIT_SET(new_markers, UNIT_MARKER_NEEDS_STOP))
CLEAR_BITS(existing_markers, UNIT_MARKER_NEEDS_RESTART, UNIT_MARKER_NEEDS_START, UNIT_MARKER_NEEDS_RELOAD);
/* New start wins against existing stop */
if (BIT_SET(new_markers, UNIT_MARKER_NEEDS_START))
CLEAR_BIT(existing_markers, UNIT_MARKER_NEEDS_STOP);
/* New restart wins against existing start and reload */
if (BIT_SET(new_markers, UNIT_MARKER_NEEDS_RESTART))
CLEAR_BITS(existing_markers, UNIT_MARKER_NEEDS_START, UNIT_MARKER_NEEDS_RELOAD);
unsigned markers = existing_markers | new_markers;
/* Standard normalization: reload loses against everything */
if (BIT_SET(markers, UNIT_MARKER_NEEDS_RESTART) || BIT_SET(markers, UNIT_MARKER_NEEDS_START) || BIT_SET(markers, UNIT_MARKER_NEEDS_STOP))
CLEAR_BIT(markers, UNIT_MARKER_NEEDS_RELOAD);
/* Stop wins against restart and reload */
if (BIT_SET(markers, UNIT_MARKER_NEEDS_STOP))
CLEAR_BITS(markers, UNIT_MARKER_NEEDS_RESTART, UNIT_MARKER_NEEDS_RELOAD);
/* Start wins against stop */
if (BIT_SET(markers, UNIT_MARKER_NEEDS_START))
CLEAR_BIT(markers, UNIT_MARKER_NEEDS_STOP);
/* Restart wins against start */
if (BITS_SET(markers, UNIT_MARKER_NEEDS_RESTART, UNIT_MARKER_NEEDS_START))
CLEAR_BIT(markers, UNIT_MARKER_NEEDS_START);
return markers;
}