HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/core/transaction.c
Git blob: 9ce5cfa587c8b11c6908bb8e76551552991b477e
Lines 1001–1410 of 1,410
}
static JobType job_type_propagate_stop_graceful(Job *j) {
JobType type;
if (!j)
return JOB_STOP;
type = JOB_STOP;
LIST_FOREACH(transaction, i, j)
switch (i->type) {
case JOB_STOP:
case JOB_RESTART:
/* Nothing to worry about, an appropriate job is in-place. */
return JOB_NOP;
case JOB_START:
/* This unit is pulled in by other dependency types in this transaction. We will run
* into job type conflict if we enqueue a stop job, so let's enqueue a restart job
* instead. */
type = JOB_RESTART;
break;
default: /* We don't care about others. */
;
}
return type;
}
int transaction_add_job_and_dependencies(
Transaction *tr,
JobType type,
Unit *unit,
Job *by,
TransactionAddFlags flags,
sd_bus_error *e) {
bool is_new;
Job *job;
int r;
assert(tr);
assert(type >= 0);
assert(type < _JOB_TYPE_MAX);
assert(type < _JOB_TYPE_MAX_IN_TRANSACTION);
assert(unit);
/* Before adding jobs for this unit, let's ensure that its state has been loaded. This matters when
* jobs are spawned as part of coldplugging itself (see e. g. path_coldplug()). This way, we
* "recursively" coldplug units, ensuring that we do not look at state of not-yet-coldplugged units. */
if (MANAGER_IS_RELOADING(unit->manager))
unit_coldplug(unit);
if (by)
log_trace("Pulling in %s/%s from %s/%s",
unit->id, job_type_to_string(type),
by->unit->id, job_type_to_string(by->type));
/* Safety check that the unit is a valid state, i.e. not in UNIT_STUB or UNIT_MERGED which should
* only be set temporarily. */
if (!UNIT_IS_LOAD_COMPLETE(unit->load_state))
return sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED, "Unit %s is not loaded properly.", unit->id);
if (type != JOB_STOP) {
/* The time-based cache allows new units to be started without daemon-reload, but if they are
* already referenced (because of dependencies or ordering) then we have to force a load of
* the fragment. As an optimization, check first if anything in the usual paths was modified
* since the last time the cache was loaded. Also check if the last time an attempt to load
* the unit was made was before the most recent cache refresh, so that we know we need to try
* again — even if the cache is current, it might have been updated in a different context
* before we had a chance to retry loading this particular unit.
*
* Given building up the transaction is a synchronous operation, attempt to load the unit
* immediately. */
if (manager_unit_cache_should_retry_load(unit)) {
assert(unit->load_state == UNIT_NOT_FOUND);
unit->load_state = UNIT_STUB;
unit->load_error = 0;
(void) unit_load(unit);
assert(unit->load_state != UNIT_STUB);
}
r = bus_unit_validate_load_state(unit, e);
if (r < 0)
return r;
}
if (!unit_job_is_applicable(unit, type))
return sd_bus_error_setf(e, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE,
"Job type %s is not applicable for unit %s.",
job_type_to_string(type), unit->id);
if (type == JOB_START) {
/* The hard concurrency limit for slice units we already enforce when a job is enqueued. */
Slice *slice = SLICE(UNIT_GET_SLICE(unit));
if (slice && slice_concurrency_hard_max_reached(slice, unit))
return sd_bus_error_setf(
e, BUS_ERROR_CONCURRENCY_LIMIT_REACHED,
"Concurrency limit of the slice unit '%s' (or any of its parents) the unit '%s' is contained in has been reached, refusing start job.",
UNIT(slice)->id, unit->id);
}
/* First add the job. */
job = transaction_add_one_job(tr, type, unit, &is_new);
if (!job)
return -ENOMEM;
if (FLAGS_SET(flags, TRANSACTION_IGNORE_ORDER))
job->ignore_order = true;
/* Then, add a link to the job. */
if (by) {
if (!job_dependency_new(by, job, FLAGS_SET(flags, TRANSACTION_MATTERS), FLAGS_SET(flags, TRANSACTION_CONFLICTS)))
return -ENOMEM;
} else {
/* If the job has no parent job, it is an anchor job. */
r = set_put(tr->anchor_jobs, job);
if (r < 0 && r != -EEXIST)
return r;
if (FLAGS_SET(flags, TRANSACTION_REENQUEUE_ANCHOR))
job->refuse_late_merge = true;
}
if (!is_new || FLAGS_SET(flags, TRANSACTION_IGNORE_REQUIREMENTS) || type == JOB_NOP)
return 0;
_cleanup_set_free_ Set *following = NULL;
Unit *dep;
/* If we are following some other unit, make sure we add all dependencies of everybody following. */
if (unit_following_set(job->unit, &following) > 0)
SET_FOREACH(dep, following) {
r = transaction_add_job_and_dependencies(tr, type, dep, job, flags & TRANSACTION_IGNORE_ORDER, e);
if (r < 0) {
log_unit_full_errno(dep, r == -ERFKILL ? LOG_INFO : LOG_WARNING, r,
"Cannot add dependency job, ignoring: %s",
bus_error_message(e, r));
sd_bus_error_free(e);
}
}
/* Finally, recursively add in all dependencies. */
if (IN_SET(type, JOB_START, JOB_RESTART)) {
UNIT_FOREACH_DEPENDENCY_SAFE(dep, job->unit, UNIT_ATOM_PULL_IN_START) {
r = transaction_add_job_and_dependencies(tr, JOB_START, dep, job, TRANSACTION_MATTERS | (flags & TRANSACTION_IGNORE_ORDER), e);
if (r < 0) {
if (r != -EBADR) /* job type not applicable */
goto fail;
sd_bus_error_free(e);
}
}
UNIT_FOREACH_DEPENDENCY_SAFE(dep, job->unit, UNIT_ATOM_PULL_IN_START_IGNORED) {
r = transaction_add_job_and_dependencies(tr, JOB_START, dep, job, flags & TRANSACTION_IGNORE_ORDER, e);
if (r < 0) {
/* unit masked, job type not applicable and unit not found are not considered
* as errors. */
log_unit_full_errno(dep,
IN_SET(r, -ERFKILL, -EBADR, -ENOENT) ? LOG_DEBUG : LOG_WARNING,
r, "Cannot add dependency job, ignoring: %s",
bus_error_message(e, r));
sd_bus_error_free(e);
}
}
UNIT_FOREACH_DEPENDENCY_SAFE(dep, job->unit, UNIT_ATOM_PULL_IN_VERIFY) {
r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, job, TRANSACTION_MATTERS | (flags & TRANSACTION_IGNORE_ORDER), e);
if (r < 0) {
if (r != -EBADR) /* job type not applicable */
goto fail;
sd_bus_error_free(e);
}
}
UNIT_FOREACH_DEPENDENCY_SAFE(dep, job->unit, UNIT_ATOM_PULL_IN_STOP) {
r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, job, TRANSACTION_MATTERS | TRANSACTION_CONFLICTS | (flags & TRANSACTION_IGNORE_ORDER), e);
if (r < 0) {
if (r != -EBADR) /* job type not applicable */
goto fail;
sd_bus_error_free(e);
}
}
UNIT_FOREACH_DEPENDENCY_SAFE(dep, job->unit, UNIT_ATOM_PULL_IN_STOP_IGNORED) {
r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, job, flags & TRANSACTION_IGNORE_ORDER, e);
if (r < 0) {
log_unit_warning(dep,
"Cannot add dependency job, ignoring: %s",
bus_error_message(e, r));
sd_bus_error_free(e);
}
}
}
if (IN_SET(type, JOB_RESTART, JOB_STOP) || (type == JOB_START && FLAGS_SET(flags, TRANSACTION_PROPAGATE_START_AS_RESTART))) {
bool is_stop = type == JOB_STOP;
UNIT_FOREACH_DEPENDENCY_SAFE(dep, job->unit, UNIT_ATOM_PROPAGATE_STOP) {
/* We propagate RESTART only as TRY_RESTART, in order not to start dependencies that
* are not around. */
JobType nt;
nt = job_type_collapse(is_stop ? JOB_STOP : JOB_TRY_RESTART, dep);
if (nt == JOB_NOP)
continue;
r = transaction_add_job_and_dependencies(tr, nt, dep, job, TRANSACTION_MATTERS | (flags & TRANSACTION_IGNORE_ORDER), e);
if (r < 0) {
if (r != -EBADR) /* job type not applicable */
return r;
sd_bus_error_free(e);
}
}
/* Process UNIT_ATOM_PROPAGATE_STOP_GRACEFUL (PropagatesStopTo=) units. We need to wait until
* all other dependencies are processed, i.e. we're the anchor job or already in the
* recursion that handles it. */
if (!by || FLAGS_SET(flags, TRANSACTION_PROCESS_PROPAGATE_STOP_GRACEFUL))
UNIT_FOREACH_DEPENDENCY_SAFE(dep, job->unit, UNIT_ATOM_PROPAGATE_STOP_GRACEFUL) {
JobType nt;
Job *j;
j = hashmap_get(tr->jobs, dep);
nt = job_type_propagate_stop_graceful(j);
if (nt == JOB_NOP)
continue;
r = transaction_add_job_and_dependencies(tr, nt, dep, job, TRANSACTION_MATTERS | (flags & TRANSACTION_IGNORE_ORDER) | TRANSACTION_PROCESS_PROPAGATE_STOP_GRACEFUL, e);
if (r < 0) {
if (r != -EBADR) /* job type not applicable */
return r;
sd_bus_error_free(e);
}
}
}
if (type == JOB_RELOAD)
transaction_add_propagate_reload_jobs(tr, job->unit, job, flags & TRANSACTION_IGNORE_ORDER);
/* JOB_VERIFY_ACTIVE requires no dependency handling. */
return 0;
fail:
/* Recursive call failed to add required jobs so let's drop top level job as well. */
log_unit_debug_errno(unit, r, "Cannot add dependency job to transaction, deleting job %s/%s again: %s",
unit->id, job_type_to_string(type), bus_error_message(e, r));
transaction_delete_job(tr, job, /* delete_dependencies= */ false);
return r;
}
static bool shall_stop_on_isolate(Transaction *tr, Unit *u) {
assert(tr);
assert(u);
if (u->ignore_on_isolate)
return false;
/* Is there already something listed for this? */
if (hashmap_contains(tr->jobs, u))
return false;
/* Keep units that are triggered by units we want to keep around. */
Unit *other;
UNIT_FOREACH_DEPENDENCY(other, u, UNIT_ATOM_TRIGGERED_BY) {
if (UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
continue;
/* Is the trigger about to go down? */
Job *other_job = hashmap_get(tr->jobs, other);
bool has_stop = false;
LIST_FOREACH(transaction, j, other_job)
if (j->type == JOB_STOP) {
has_stop = true;
break;
}
if (has_stop)
continue;
if (other->ignore_on_isolate || other_job)
return false;
}
return true;
}
int transaction_add_isolate_jobs(Transaction *tr, Manager *m) {
Unit *u;
char *k;
int r;
assert(tr);
assert(m);
assert(set_size(tr->anchor_jobs) == 1);
HASHMAP_FOREACH_KEY(u, k, m->units) {
_cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
/* Ignore aliases. */
if (u->id != k)
continue;
/* No need to stop inactive units. */
if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) && !u->job)
continue;
if (!shall_stop_on_isolate(tr, u))
continue;
/* JOB_ISOLATE only ever has a single anchor (we reject multi-anchor isolate in the
* manager) so picking the first anchor is correct. */
r = transaction_add_job_and_dependencies(tr, JOB_STOP, u, set_first(tr->anchor_jobs), TRANSACTION_MATTERS, &e);
if (r < 0)
log_unit_warning_errno(u, r, "Cannot add isolate job, ignoring: %s", bus_error_message(&e, r));
}
return 0;
}
int transaction_add_triggering_jobs(Transaction *tr, Unit *u) {
Unit *trigger;
Job *by;
int r;
assert(tr);
assert(u);
/* The unit's own anchor job is the head of the list in tr->jobs. We pull in JOB_STOP for all
* triggers and link them to that anchor, so they are correctly grouped per anchor when there are
* multiple anchors in the transaction. */
by = ASSERT_PTR(hashmap_get(tr->jobs, u));
UNIT_FOREACH_DEPENDENCY_SAFE(trigger, u, UNIT_ATOM_TRIGGERED_BY) {
_cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
/* No need to stop inactive jobs. */
if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(trigger)) && !trigger->job)
continue;
/* Is there already something listed for this? */
if (hashmap_contains(tr->jobs, trigger))
continue;
r = transaction_add_job_and_dependencies(tr, JOB_STOP, trigger, by, TRANSACTION_MATTERS, &e);
if (r < 0)
log_unit_warning_errno(u, r, "Cannot add triggered by job, ignoring: %s", bus_error_message(&e, r));
}
return 0;
}
Transaction* transaction_new(bool irreversible, uint64_t id) {
_cleanup_free_ Transaction *tr = NULL;
assert(id != 0);
tr = new(Transaction, 1);
if (!tr)
return NULL;
*tr = (Transaction) {
.jobs = hashmap_new(NULL),
.anchor_jobs = set_new(NULL),
.irreversible = irreversible,
.id = id,
};
if (!tr->jobs || !tr->anchor_jobs) {
hashmap_free(tr->jobs);
set_free(tr->anchor_jobs);
return NULL;
}
return TAKE_PTR(tr);
}
Transaction* transaction_free(Transaction *tr) {
if (!tr)
return NULL;
assert(hashmap_isempty(tr->jobs));
assert(set_isempty(tr->anchor_jobs));
set_free(tr->anchor_jobs);
hashmap_free(tr->jobs);
return mfree(tr);
}
Transaction* transaction_abort_and_free(Transaction *tr) {
if (!tr)
return NULL;
transaction_abort(tr);
return transaction_free(tr);
}