This source file includes following definitions.
- transaction_delete_job
- transaction_delete_unit
- transaction_abort
- transaction_find_jobs_that_matter_to_anchor
- transaction_merge_and_delete_job
- job_is_conflicted_by
- delete_one_unmergeable_job
- transaction_merge_jobs
- transaction_drop_redundant
- unit_matters_to_anchor
- transaction_verify_order_one
- transaction_verify_order
- transaction_collect_garbage
- transaction_is_destructive
- transaction_minimize_impact
- transaction_apply
- transaction_activate
- transaction_add_one_job
- transaction_unlink_job
- transaction_add_job_and_dependencies
- transaction_add_isolate_jobs
- transaction_new
- transaction_free
#include <unistd.h>
#include <fcntl.h>
#include "bus-errors.h"
#include "bus-util.h"
#include "bus-error.h"
#include "transaction.h"
static void transaction_unlink_job(Transaction *tr, Job *j, bool delete_dependencies);
static void transaction_delete_job(Transaction *tr, Job *j, bool delete_dependencies) {
assert(tr);
assert(j);
transaction_unlink_job(tr, j, delete_dependencies);
job_free(j);
}
static void transaction_delete_unit(Transaction *tr, Unit *u) {
Job *j;
while ((j = hashmap_get(tr->jobs, u)))
transaction_delete_job(tr, j, true);
}
void transaction_abort(Transaction *tr) {
Job *j;
assert(tr);
while ((j = hashmap_first(tr->jobs)))
transaction_delete_job(tr, j, false);
assert(hashmap_isempty(tr->jobs));
}
static void transaction_find_jobs_that_matter_to_anchor(Job *j, unsigned generation) {
JobDependency *l;
j->matters_to_anchor = true;
j->generation = generation;
LIST_FOREACH(subject, l, j->subject_list) {
if (!l->matters)
continue;
if (l->object->generation == generation)
continue;
transaction_find_jobs_that_matter_to_anchor(l->object, generation);
}
}
static void transaction_merge_and_delete_job(Transaction *tr, Job *j, Job *other, JobType t) {
JobDependency *l, *last;
assert(j);
assert(other);
assert(j->unit == other->unit);
assert(!j->installed);
j->type = t;
j->state = JOB_WAITING;
j->override = j->override || other->override;
j->irreversible = j->irreversible || other->irreversible;
j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
last = NULL;
LIST_FOREACH(subject, l, other->subject_list) {
assert(l->subject == other);
l->subject = j;
last = l;
}
if (last) {
last->subject_next = j->subject_list;
if (j->subject_list)
j->subject_list->subject_prev = last;
j->subject_list = other->subject_list;
}
last = NULL;
LIST_FOREACH(object, l, other->object_list) {
assert(l->object == other);
l->object = j;
last = l;
}
if (last) {
last->object_next = j->object_list;
if (j->object_list)
j->object_list->object_prev = last;
j->object_list = other->object_list;
}
other->subject_list = NULL;
other->object_list = NULL;
transaction_delete_job(tr, other, true);
}
_pure_ static bool job_is_conflicted_by(Job *j) {
JobDependency *l;
assert(j);
LIST_FOREACH(object, l, j->object_list)
if (l->conflicts)
return true;
return false;
}
static int delete_one_unmergeable_job(Transaction *tr, Job *j) {
Job *k;
assert(j);
LIST_FOREACH(transaction, j, j)
LIST_FOREACH(transaction, k, j->transaction_next) {
Job *d;
if (job_type_is_mergeable(j->type, k->type))
continue;
if (!j->matters_to_anchor && !k->matters_to_anchor) {
log_debug_unit(j->unit->id,
"Looking at job %s/%s conflicted_by=%s",
j->unit->id, job_type_to_string(j->type),
yes_no(j->type == JOB_STOP && job_is_conflicted_by(j)));
log_debug_unit(k->unit->id,
"Looking at job %s/%s conflicted_by=%s",
k->unit->id, job_type_to_string(k->type),
yes_no(k->type == JOB_STOP && job_is_conflicted_by(k)));
if (j->type == JOB_STOP) {
if (job_is_conflicted_by(j))
d = k;
else
d = j;
} else if (k->type == JOB_STOP) {
if (job_is_conflicted_by(k))
d = j;
else
d = k;
} else
d = j;
} else if (!j->matters_to_anchor)
d = j;
else if (!k->matters_to_anchor)
d = k;
else
return -ENOEXEC;
log_debug_unit(d->unit->id,
"Fixing conflicting jobs %s/%s,%s/%s by deleting job %s/%s",
j->unit->id, job_type_to_string(j->type),
k->unit->id, job_type_to_string(k->type),
d->unit->id, job_type_to_string(d->type));
transaction_delete_job(tr, d, true);
return 0;
}
return -EINVAL;
}
static int transaction_merge_jobs(Transaction *tr, sd_bus_error *e) {
Job *j;
Iterator i;
int r;
assert(tr);
HASHMAP_FOREACH(j, tr->jobs, i) {
JobType t;
Job *k;
t = j->type;
LIST_FOREACH(transaction, k, j->transaction_next) {
if (job_type_merge_and_collapse(&t, k->type, j->unit) >= 0)
continue;
r = delete_one_unmergeable_job(tr, j);
if (r >= 0)
return -EAGAIN;
sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING,
"Transaction contains conflicting jobs '%s' and '%s' for %s. "
"Probably contradicting requirement dependencies configured.",
job_type_to_string(t),
job_type_to_string(k->type),
k->unit->id);
return r;
}
}
HASHMAP_FOREACH(j, tr->jobs, i) {
JobType t = j->type;
Job *k;
LIST_FOREACH(transaction, k, j->transaction_next)
assert_se(job_type_merge_and_collapse(&t, k->type, j->unit) == 0);
while ((k = j->transaction_next)) {
if (tr->anchor_job == k) {
transaction_merge_and_delete_job(tr, k, j, t);
j = k;
} else
transaction_merge_and_delete_job(tr, j, k, t);
}
assert(!j->transaction_next);
assert(!j->transaction_prev);
}
return 0;
}
static void transaction_drop_redundant(Transaction *tr) {
Job *j;
Iterator i;
assert(tr);
rescan:
HASHMAP_FOREACH(j, tr->jobs, i) {
Job *k;
LIST_FOREACH(transaction, k, j) {
if (tr->anchor_job == k ||
!job_type_is_redundant(k->type, unit_active_state(k->unit)) ||
(k->unit->job && job_type_is_conflicting(k->type, k->unit->job->type)))
goto next_unit;
}
transaction_delete_job(tr, j, false);
goto rescan;
next_unit:;
}
}
_pure_ static bool unit_matters_to_anchor(Unit *u, Job *j) {
assert(u);
assert(!j->transaction_prev);
LIST_FOREACH(transaction, j, j)
if (j->matters_to_anchor)
return true;
return false;
}
static int transaction_verify_order_one(Transaction *tr, Job *j, Job *from, unsigned generation, sd_bus_error *e) {
Iterator i;
Unit *u;
int r;
assert(tr);
assert(j);
assert(!j->transaction_prev);
if (j->generation == generation) {
Job *k, *delete;
if (!j->marker)
return 0;
log_warning_unit(j->unit->id,
"Found ordering cycle on %s/%s",
j->unit->id, job_type_to_string(j->type));
delete = NULL;
for (k = from; k; k = ((k->generation == generation && k->marker != k) ? k->marker : NULL)) {
log_warning_unit(j->unit->id,
"Found dependency on %s/%s",
k->unit->id, job_type_to_string(k->type));
if (!delete && hashmap_get(tr->jobs, k->unit) &&
!unit_matters_to_anchor(k->unit, k)) {
delete = k;
}
if (k == j)
break;
}
if (delete) {
log_warning_unit(j->unit->id,
"Breaking ordering cycle by deleting job %s/%s",
delete->unit->id, job_type_to_string(delete->type));
log_error_unit(delete->unit->id,
"Job %s/%s deleted to break ordering cycle starting with %s/%s",
delete->unit->id, job_type_to_string(delete->type),
j->unit->id, job_type_to_string(j->type));
unit_status_printf(delete->unit, ANSI_HIGHLIGHT_RED_ON " SKIP " ANSI_HIGHLIGHT_OFF,
"Ordering cycle found, skipping %s");
transaction_delete_unit(tr, delete->unit);
return -EAGAIN;
}
log_error("Unable to break cycle");
sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC,
"Transaction order is cyclic. See system logs for details.");
return -ENOEXEC;
}
j->marker = from ? from : j;
j->generation = generation;
SET_FOREACH(u, j->unit->dependencies[UNIT_BEFORE], i) {
Job *o;
o = hashmap_get(tr->jobs, u);
if (!o) {
o = u->job;
if (!o)
continue;
}
r = transaction_verify_order_one(tr, o, j, generation, e);
if (r < 0)
return r;
}
j->marker = NULL;
return 0;
}
static int transaction_verify_order(Transaction *tr, unsigned *generation, sd_bus_error *e) {
Job *j;
int r;
Iterator i;
unsigned g;
assert(tr);
assert(generation);
g = (*generation)++;
HASHMAP_FOREACH(j, tr->jobs, i)
if ((r = transaction_verify_order_one(tr, j, NULL, g, e)) < 0)
return r;
return 0;
}
static void transaction_collect_garbage(Transaction *tr) {
Iterator i;
Job *j;
assert(tr);
rescan:
HASHMAP_FOREACH(j, tr->jobs, i) {
if (tr->anchor_job == j || j->object_list) {
continue;
}
transaction_delete_job(tr, j, true);
goto rescan;
}
}
static int transaction_is_destructive(Transaction *tr, JobMode mode, sd_bus_error *e) {
Iterator i;
Job *j;
assert(tr);
HASHMAP_FOREACH(j, tr->jobs, i) {
assert(!j->transaction_prev);
assert(!j->transaction_next);
if (j->unit->job && (mode == JOB_FAIL || j->unit->job->irreversible) &&
!job_type_is_superset(j->type, j->unit->job->type)) {
sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE,
"Transaction is destructive.");
return -EEXIST;
}
}
return 0;
}
static void transaction_minimize_impact(Transaction *tr) {
Job *j;
Iterator i;
assert(tr);
rescan:
HASHMAP_FOREACH(j, tr->jobs, i) {
LIST_FOREACH(transaction, j, j) {
bool stops_running_service, changes_existing_job;
if (j->matters_to_anchor)
continue;
stops_running_service =
j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
changes_existing_job =
j->unit->job &&
job_type_is_conflicting(j->type, j->unit->job->type);
if (!stops_running_service && !changes_existing_job)
continue;
if (stops_running_service)
log_debug_unit(j->unit->id,
"%s/%s would stop a running service.",
j->unit->id, job_type_to_string(j->type));
if (changes_existing_job)
log_debug_unit(j->unit->id,
"%s/%s would change existing job.",
j->unit->id, job_type_to_string(j->type));
log_debug_unit(j->unit->id,
"Deleting %s/%s to minimize impact.",
j->unit->id, job_type_to_string(j->type));
transaction_delete_job(tr, j, true);
goto rescan;
}
}
}
static int transaction_apply(Transaction *tr, Manager *m, JobMode mode) {
Iterator i;
Job *j;
int r;
if (mode == JOB_ISOLATE || mode == JOB_FLUSH) {
HASHMAP_FOREACH(j, m->jobs, i) {
assert(j->installed);
if (hashmap_get(tr->jobs, j->unit))
continue;
job_finish_and_invalidate(j, JOB_CANCELED, false);
}
}
HASHMAP_FOREACH(j, tr->jobs, i) {
assert(!j->transaction_prev);
assert(!j->transaction_next);
r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j);
if (r < 0)
goto rollback;
}
while ((j = hashmap_steal_first(tr->jobs))) {
Job *installed_job;
transaction_unlink_job(tr, j, false);
installed_job = job_install(j);
if (installed_job != j) {
if (tr->anchor_job == j)
tr->anchor_job = installed_job;
hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
job_free(j);
j = installed_job;
}
job_add_to_run_queue(j);
job_add_to_dbus_queue(j);
job_start_timer(j);
job_shutdown_magic(j);
}
return 0;
rollback:
HASHMAP_FOREACH(j, tr->jobs, i)
hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
return r;
}
int transaction_activate(Transaction *tr, Manager *m, JobMode mode, sd_bus_error *e) {
Iterator i;
Job *j;
int r;
unsigned generation = 1;
assert(tr);
HASHMAP_FOREACH(j, m->jobs, i)
j->generation = 0;
transaction_find_jobs_that_matter_to_anchor(tr->anchor_job, generation++);
if (mode == JOB_FAIL)
transaction_minimize_impact(tr);
transaction_drop_redundant(tr);
for (;;) {
if (mode != JOB_ISOLATE)
transaction_collect_garbage(tr);
r = transaction_verify_order(tr, &generation, e);
if (r >= 0)
break;
if (r != -EAGAIN) {
log_warning("Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error_message(e, r));
return r;
}
}
for (;;) {
r = transaction_merge_jobs(tr, e);
if (r >= 0)
break;
if (r != -EAGAIN) {
log_warning("Requested transaction contains unmergeable jobs: %s", bus_error_message(e, r));
return r;
}
if (mode != JOB_ISOLATE)
transaction_collect_garbage(tr);
}
transaction_drop_redundant(tr);
r = transaction_is_destructive(tr, mode, e);
if (r < 0) {
log_notice("Requested transaction contradicts existing jobs: %s", bus_error_message(e, r));
return r;
}
r = transaction_apply(tr, m, mode);
if (r < 0) {
log_warning("Failed to apply transaction: %s", strerror(-r));
return r;
}
assert(hashmap_isempty(tr->jobs));
if (!hashmap_isempty(m->jobs)) {
if (m->idle_pipe[0] < 0 && m->idle_pipe[1] < 0 &&
m->idle_pipe[2] < 0 && m->idle_pipe[3] < 0) {
pipe2(m->idle_pipe, O_NONBLOCK|O_CLOEXEC);
pipe2(m->idle_pipe + 2, O_NONBLOCK|O_CLOEXEC);
}
}
return 0;
}
static Job* transaction_add_one_job(Transaction *tr, JobType type, Unit *unit, bool override, bool *is_new) {
Job *j, *f;
assert(tr);
assert(unit);
f = hashmap_get(tr->jobs, unit);
LIST_FOREACH(transaction, j, f) {
assert(j->unit == unit);
if (j->type == type) {
if (is_new)
*is_new = false;
return j;
}
}
j = job_new(unit, type);
if (!j)
return NULL;
j->generation = 0;
j->marker = NULL;
j->matters_to_anchor = false;
j->override = override;
j->irreversible = tr->irreversible;
LIST_PREPEND(transaction, f, j);
if (hashmap_replace(tr->jobs, unit, f) < 0) {
LIST_REMOVE(transaction, f, j);
job_free(j);
return NULL;
}
if (is_new)
*is_new = true;
return j;
}
static void transaction_unlink_job(Transaction *tr, Job *j, bool delete_dependencies) {
assert(tr);
assert(j);
if (j->transaction_prev)
j->transaction_prev->transaction_next = j->transaction_next;
else if (j->transaction_next)
hashmap_replace(tr->jobs, j->unit, j->transaction_next);
else
hashmap_remove_value(tr->jobs, j->unit, j);
if (j->transaction_next)
j->transaction_next->transaction_prev = j->transaction_prev;
j->transaction_prev = j->transaction_next = NULL;
while (j->subject_list)
job_dependency_free(j->subject_list);
while (j->object_list) {
Job *other = j->object_list->matters ? j->object_list->subject : NULL;
job_dependency_free(j->object_list);
if (other && delete_dependencies) {
log_debug_unit(other->unit->id,
"Deleting job %s/%s as dependency of job %s/%s",
other->unit->id, job_type_to_string(other->type),
j->unit->id, job_type_to_string(j->type));
transaction_delete_job(tr, other, delete_dependencies);
}
}
}
int transaction_add_job_and_dependencies(
Transaction *tr,
JobType type,
Unit *unit,
Job *by,
bool matters,
bool override,
bool conflicts,
bool ignore_requirements,
bool ignore_order,
sd_bus_error *e) {
Job *ret;
Iterator i;
Unit *dep;
int r;
bool is_new;
assert(tr);
assert(type < _JOB_TYPE_MAX);
assert(type < _JOB_TYPE_MAX_IN_TRANSACTION);
assert(unit);
if (unit->load_state != UNIT_LOADED &&
unit->load_state != UNIT_ERROR &&
unit->load_state != UNIT_NOT_FOUND &&
unit->load_state != UNIT_MASKED) {
sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED,
"Unit %s is not loaded properly.", unit->id);
return -EINVAL;
}
if (type != JOB_STOP && unit->load_state == UNIT_ERROR) {
if (unit->load_error == -ENOENT || unit->manager->test_run)
sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED,
"Unit %s failed to load: %s.",
unit->id,
strerror(-unit->load_error));
else
sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED,
"Unit %s failed to load: %s. "
"See system logs and 'systemctl status %s' for details.",
unit->id,
strerror(-unit->load_error),
unit->id);
return -EINVAL;
}
if (type != JOB_STOP && unit->load_state == UNIT_NOT_FOUND) {
sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED,
"Unit %s failed to load: %s.",
unit->id, strerror(-unit->load_error));
return -EINVAL;
}
if (type != JOB_STOP && unit->load_state == UNIT_MASKED) {
sd_bus_error_setf(e, BUS_ERROR_UNIT_MASKED,
"Unit %s is masked.", unit->id);
return -EADDRNOTAVAIL;
}
if (!unit_job_is_applicable(unit, type)) {
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);
return -EBADR;
}
ret = transaction_add_one_job(tr, type, unit, override, &is_new);
if (!ret)
return -ENOMEM;
ret->ignore_order = ret->ignore_order || ignore_order;
if (by) {
if (!job_dependency_new(by, ret, matters, conflicts))
return -ENOMEM;
} else {
assert(!tr->anchor_job);
tr->anchor_job = ret;
}
if (is_new && !ignore_requirements && type != JOB_NOP) {
Set *following;
if (unit_following_set(ret->unit, &following) > 0) {
SET_FOREACH(dep, following, i) {
r = transaction_add_job_and_dependencies(tr, type, dep, ret, false, override, false, false, ignore_order, e);
if (r < 0) {
log_warning_unit(dep->id,
"Cannot add dependency job for unit %s, ignoring: %s",
dep->id, bus_error_message(e, r));
if (e)
sd_bus_error_free(e);
}
}
set_free(following);
}
if (type == JOB_START || type == JOB_RESTART) {
SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRES], i) {
r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, true, override, false, false, ignore_order, e);
if (r < 0) {
if (r != -EBADR)
goto fail;
if (e)
sd_bus_error_free(e);
}
}
SET_FOREACH(dep, ret->unit->dependencies[UNIT_BINDS_TO], i) {
r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, true, override, false, false, ignore_order, e);
if (r < 0) {
if (r != -EBADR)
goto fail;
if (e)
sd_bus_error_free(e);
}
}
SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRES_OVERRIDABLE], i) {
r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, !override, override, false, false, ignore_order, e);
if (r < 0) {
log_full_unit(r == -EADDRNOTAVAIL ? LOG_DEBUG : LOG_WARNING, dep->id,
"Cannot add dependency job for unit %s, ignoring: %s",
dep->id, bus_error_message(e, r));
if (e)
sd_bus_error_free(e);
}
}
SET_FOREACH(dep, ret->unit->dependencies[UNIT_WANTS], i) {
r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, false, false, false, false, ignore_order, e);
if (r < 0) {
log_full_unit(r == -EADDRNOTAVAIL ? LOG_DEBUG : LOG_WARNING, dep->id,
"Cannot add dependency job for unit %s, ignoring: %s",
dep->id, bus_error_message(e, r));
if (e)
sd_bus_error_free(e);
}
}
SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUISITE], i) {
r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, ret, true, override, false, false, ignore_order, e);
if (r < 0) {
if (r != -EBADR)
goto fail;
if (e)
sd_bus_error_free(e);
}
}
SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUISITE_OVERRIDABLE], i) {
r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, ret, !override, override, false, false, ignore_order, e);
if (r < 0) {
log_full_unit(r == -EADDRNOTAVAIL ? LOG_DEBUG : LOG_WARNING, dep->id,
"Cannot add dependency job for unit %s, ignoring: %s",
dep->id, bus_error_message(e, r));
if (e)
sd_bus_error_free(e);
}
}
SET_FOREACH(dep, ret->unit->dependencies[UNIT_CONFLICTS], i) {
r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, true, override, true, false, ignore_order, e);
if (r < 0) {
if (r != -EBADR)
goto fail;
if (e)
sd_bus_error_free(e);
}
}
SET_FOREACH(dep, ret->unit->dependencies[UNIT_CONFLICTED_BY], i) {
r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, false, override, false, false, ignore_order, e);
if (r < 0) {
log_warning_unit(dep->id,
"Cannot add dependency job for unit %s, ignoring: %s",
dep->id, bus_error_message(e, r));
if (e)
sd_bus_error_free(e);
}
}
}
if (type == JOB_STOP || type == JOB_RESTART) {
SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRED_BY], i) {
r = transaction_add_job_and_dependencies(tr, type, dep, ret, true, override, false, false, ignore_order, e);
if (r < 0) {
if (r != -EBADR)
goto fail;
if (e)
sd_bus_error_free(e);
}
}
SET_FOREACH(dep, ret->unit->dependencies[UNIT_BOUND_BY], i) {
r = transaction_add_job_and_dependencies(tr, type, dep, ret, true, override, false, false, ignore_order, e);
if (r < 0) {
if (r != -EBADR)
goto fail;
if (e)
sd_bus_error_free(e);
}
}
SET_FOREACH(dep, ret->unit->dependencies[UNIT_CONSISTS_OF], i) {
r = transaction_add_job_and_dependencies(tr, type, dep, ret, true, override, false, false, ignore_order, e);
if (r < 0) {
if (r != -EBADR)
goto fail;
if (e)
sd_bus_error_free(e);
}
}
}
if (type == JOB_RELOAD) {
SET_FOREACH(dep, ret->unit->dependencies[UNIT_PROPAGATES_RELOAD_TO], i) {
r = transaction_add_job_and_dependencies(tr, JOB_RELOAD, dep, ret, false, override, false, false, ignore_order, e);
if (r < 0) {
log_warning_unit(dep->id,
"Cannot add dependency reload job for unit %s, ignoring: %s",
dep->id, bus_error_message(e, r));
if (e)
sd_bus_error_free(e);
}
}
}
}
return 0;
fail:
return r;
}
int transaction_add_isolate_jobs(Transaction *tr, Manager *m) {
Iterator i;
Unit *u;
char *k;
int r;
assert(tr);
assert(m);
HASHMAP_FOREACH_KEY(u, k, m->units, i) {
if (u->id != k)
continue;
if (u->ignore_on_isolate)
continue;
if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) && !u->job)
continue;
if (hashmap_get(tr->jobs, u))
continue;
r = transaction_add_job_and_dependencies(tr, JOB_STOP, u, tr->anchor_job, true, false, false, false, false, NULL);
if (r < 0)
log_warning_unit(u->id,
"Cannot add isolate job for unit %s, ignoring: %s",
u->id, strerror(-r));
}
return 0;
}
Transaction *transaction_new(bool irreversible) {
Transaction *tr;
tr = new0(Transaction, 1);
if (!tr)
return NULL;
tr->jobs = hashmap_new(NULL);
if (!tr->jobs) {
free(tr);
return NULL;
}
tr->irreversible = irreversible;
return tr;
}
void transaction_free(Transaction *tr) {
assert(hashmap_isempty(tr->jobs));
hashmap_free(tr->jobs);
free(tr);
}