1 #include "transaction.h"
2 #include "bus-errors.h"
4 static void transaction_unlink_job(Transaction *tr, Job *j, bool delete_dependencies);
6 static void transaction_delete_job(Transaction *tr, Job *j, bool delete_dependencies) {
10 /* Deletes one job from the transaction */
12 transaction_unlink_job(tr, j, delete_dependencies);
17 static void transaction_delete_unit(Transaction *tr, Unit *u) {
20 /* Deletes all jobs associated with a certain unit from the
23 while ((j = hashmap_get(tr->jobs, u)))
24 transaction_delete_job(tr, j, true);
27 void transaction_abort(Transaction *tr) {
32 while ((j = hashmap_first(tr->jobs)))
33 transaction_delete_job(tr, j, true);
35 assert(hashmap_isempty(tr->jobs));
38 static void transaction_find_jobs_that_matter_to_anchor(Job *j, unsigned generation) {
41 /* A recursive sweep through the graph that marks all units
42 * that matter to the anchor job, i.e. are directly or
43 * indirectly a dependency of the anchor job via paths that
44 * are fully marked as mattering. */
46 j->matters_to_anchor = true;
47 j->generation = generation;
49 LIST_FOREACH(subject, l, j->subject_list) {
51 /* This link does not matter */
55 /* This unit has already been marked */
56 if (l->object->generation == generation)
59 transaction_find_jobs_that_matter_to_anchor(l->object, generation);
63 static void transaction_merge_and_delete_job(Transaction *tr, Job *j, Job *other, JobType t) {
64 JobDependency *l, *last;
68 assert(j->unit == other->unit);
69 assert(!j->installed);
71 /* Merges 'other' into 'j' and then deletes 'other'. */
74 j->state = JOB_WAITING;
75 j->override = j->override || other->override;
77 j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
79 /* Patch us in as new owner of the JobDependency objects */
81 LIST_FOREACH(subject, l, other->subject_list) {
82 assert(l->subject == other);
87 /* Merge both lists */
89 last->subject_next = j->subject_list;
91 j->subject_list->subject_prev = last;
92 j->subject_list = other->subject_list;
95 /* Patch us in as new owner of the JobDependency objects */
97 LIST_FOREACH(object, l, other->object_list) {
98 assert(l->object == other);
103 /* Merge both lists */
105 last->object_next = j->object_list;
107 j->object_list->object_prev = last;
108 j->object_list = other->object_list;
111 /* Kill the other job */
112 other->subject_list = NULL;
113 other->object_list = NULL;
114 transaction_delete_job(tr, other, true);
117 static bool job_is_conflicted_by(Job *j) {
122 /* Returns true if this job is pulled in by a least one
123 * ConflictedBy dependency. */
125 LIST_FOREACH(object, l, j->object_list)
132 static int delete_one_unmergeable_job(Transaction *tr, Job *j) {
137 /* Tries to delete one item in the linked list
138 * j->transaction_next->transaction_next->... that conflicts
139 * with another one, in an attempt to make an inconsistent
140 * transaction work. */
142 /* We rely here on the fact that if a merged with b does not
143 * merge with c, either a or b merge with c neither */
144 LIST_FOREACH(transaction, j, j)
145 LIST_FOREACH(transaction, k, j->transaction_next) {
148 /* Is this one mergeable? Then skip it */
149 if (job_type_is_mergeable(j->type, k->type))
152 /* Ok, we found two that conflict, let's see if we can
153 * drop one of them */
154 if (!j->matters_to_anchor && !k->matters_to_anchor) {
156 /* Both jobs don't matter, so let's
157 * find the one that is smarter to
158 * remove. Let's think positive and
159 * rather remove stops then starts --
160 * except if something is being
161 * stopped because it is conflicted by
162 * another unit in which case we
163 * rather remove the start. */
165 log_debug("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)));
166 log_debug("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)));
168 if (j->type == JOB_STOP) {
170 if (job_is_conflicted_by(j))
175 } else if (k->type == JOB_STOP) {
177 if (job_is_conflicted_by(k))
184 } else if (!j->matters_to_anchor)
186 else if (!k->matters_to_anchor)
191 /* Ok, we can drop one, so let's do so. */
192 log_debug("Fixing conflicting jobs by deleting job %s/%s", d->unit->id, job_type_to_string(d->type));
193 transaction_delete_job(tr, d, true);
200 static int transaction_merge_jobs(Transaction *tr, DBusError *e) {
207 /* First step, check whether any of the jobs for one specific
208 * task conflict. If so, try to drop one of them. */
209 HASHMAP_FOREACH(j, tr->jobs, i) {
214 LIST_FOREACH(transaction, k, j->transaction_next) {
215 if (job_type_merge(&t, k->type) >= 0)
218 /* OK, we could not merge all jobs for this
219 * action. Let's see if we can get rid of one
222 r = delete_one_unmergeable_job(tr, j);
224 /* Ok, we managed to drop one, now
225 * let's ask our callers to call us
226 * again after garbage collecting */
229 /* We couldn't merge anything. Failure */
230 dbus_set_error(e, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING, "Transaction contains conflicting jobs '%s' and '%s' for %s. Probably contradicting requirement dependencies configured.",
231 job_type_to_string(t), job_type_to_string(k->type), k->unit->id);
236 /* Second step, merge the jobs. */
237 HASHMAP_FOREACH(j, tr->jobs, i) {
241 /* Merge all transactions */
242 LIST_FOREACH(transaction, k, j->transaction_next)
243 assert_se(job_type_merge(&t, k->type) == 0);
245 while ((k = j->transaction_next)) {
246 if (tr->anchor_job == k) {
247 transaction_merge_and_delete_job(tr, k, j, t);
250 transaction_merge_and_delete_job(tr, j, k, t);
253 assert(!j->transaction_next);
254 assert(!j->transaction_prev);
260 static void transaction_drop_redundant(Transaction *tr) {
265 /* Goes through the transaction and removes all jobs that are
274 HASHMAP_FOREACH(j, tr->jobs, i) {
275 bool changes_something = false;
278 LIST_FOREACH(transaction, k, j) {
280 if (tr->anchor_job != k &&
281 job_type_is_redundant(k->type, unit_active_state(k->unit)) &&
282 (!k->unit->job || !job_type_is_conflicting(k->type, k->unit->job->type)))
285 changes_something = true;
289 if (changes_something)
292 /* log_debug("Found redundant job %s/%s, dropping.", j->unit->id, job_type_to_string(j->type)); */
293 transaction_delete_job(tr, j, false);
301 static bool unit_matters_to_anchor(Unit *u, Job *j) {
303 assert(!j->transaction_prev);
305 /* Checks whether at least one of the jobs for this unit
306 * matters to the anchor. */
308 LIST_FOREACH(transaction, j, j)
309 if (j->matters_to_anchor)
315 static int transaction_verify_order_one(Transaction *tr, Job *j, Job *from, unsigned generation, DBusError *e) {
322 assert(!j->transaction_prev);
324 /* Does a recursive sweep through the ordering graph, looking
325 * for a cycle. If we find cycle we try to break it. */
327 /* Have we seen this before? */
328 if (j->generation == generation) {
331 /* If the marker is NULL we have been here already and
332 * decided the job was loop-free from here. Hence
333 * shortcut things and return right-away. */
337 /* So, the marker is not NULL and we already have been
338 * here. We have a cycle. Let's try to break it. We go
339 * backwards in our path and try to find a suitable
340 * job to remove. We use the marker to find our way
341 * back, since smart how we are we stored our way back
343 log_warning("Found ordering cycle on %s/%s", j->unit->id, job_type_to_string(j->type));
346 for (k = from; k; k = ((k->generation == generation && k->marker != k) ? k->marker : NULL)) {
348 log_info("Walked on cycle path to %s/%s", k->unit->id, job_type_to_string(k->type));
351 !unit_matters_to_anchor(k->unit, k)) {
352 /* Ok, we can drop this one, so let's
357 /* Check if this in fact was the beginning of
365 log_warning("Breaking ordering cycle by deleting job %s/%s", delete->unit->id, job_type_to_string(delete->type));
366 transaction_delete_unit(tr, delete->unit);
370 log_error("Unable to break cycle");
372 dbus_set_error(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC, "Transaction order is cyclic. See system logs for details.");
376 /* Make the marker point to where we come from, so that we can
377 * find our way backwards if we want to break a cycle. We use
378 * a special marker for the beginning: we point to
380 j->marker = from ? from : j;
381 j->generation = generation;
383 /* We assume that the the dependencies are bidirectional, and
384 * hence can ignore UNIT_AFTER */
385 SET_FOREACH(u, j->unit->dependencies[UNIT_BEFORE], i) {
388 /* Is there a job for this unit? */
389 o = hashmap_get(tr->jobs, u);
391 /* Ok, there is no job for this in the
392 * transaction, but maybe there is already one
399 r = transaction_verify_order_one(tr, o, j, generation, e);
404 /* Ok, let's backtrack, and remember that this entry is not on
405 * our path anymore. */
411 static int transaction_verify_order(Transaction *tr, unsigned *generation, DBusError *e) {
420 /* Check if the ordering graph is cyclic. If it is, try to fix
421 * that up by dropping one of the jobs. */
425 HASHMAP_FOREACH(j, tr->jobs, i)
426 if ((r = transaction_verify_order_one(tr, j, NULL, g, e)) < 0)
432 static void transaction_collect_garbage(Transaction *tr) {
437 /* Drop jobs that are not required by any other job */
445 HASHMAP_FOREACH(j, tr->jobs, i) {
446 if (tr->anchor_job == j || j->object_list) {
447 /* log_debug("Keeping job %s/%s because of %s/%s", */
448 /* j->unit->id, job_type_to_string(j->type), */
449 /* j->object_list->subject ? j->object_list->subject->unit->id : "root", */
450 /* j->object_list->subject ? job_type_to_string(j->object_list->subject->type) : "root"); */
454 /* log_debug("Garbage collecting job %s/%s", j->unit->id, job_type_to_string(j->type)); */
455 transaction_delete_job(tr, j, true);
463 static int transaction_is_destructive(Transaction *tr, DBusError *e) {
469 /* Checks whether applying this transaction means that
470 * existing jobs would be replaced */
472 HASHMAP_FOREACH(j, tr->jobs, i) {
475 assert(!j->transaction_prev);
476 assert(!j->transaction_next);
479 !job_type_is_superset(j->type, j->unit->job->type)) {
481 dbus_set_error(e, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE, "Transaction is destructive.");
489 static void transaction_minimize_impact(Transaction *tr) {
493 /* Drops all unnecessary jobs that reverse already active jobs
494 * or that stop a running service. */
502 HASHMAP_FOREACH(j, tr->jobs, i) {
503 LIST_FOREACH(transaction, j, j) {
504 bool stops_running_service, changes_existing_job;
506 /* If it matters, we shouldn't drop it */
507 if (j->matters_to_anchor)
510 /* Would this stop a running service?
511 * Would this change an existing job?
512 * If so, let's drop this entry */
514 stops_running_service =
515 j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
517 changes_existing_job =
519 job_type_is_conflicting(j->type, j->unit->job->type);
521 if (!stops_running_service && !changes_existing_job)
524 if (stops_running_service)
525 log_debug("%s/%s would stop a running service.", j->unit->id, job_type_to_string(j->type));
527 if (changes_existing_job)
528 log_debug("%s/%s would change existing job.", j->unit->id, job_type_to_string(j->type));
530 /* Ok, let's get rid of this */
531 log_debug("Deleting %s/%s to minimize impact.", j->unit->id, job_type_to_string(j->type));
533 transaction_delete_job(tr, j, true);
545 static int transaction_apply(Transaction *tr, Manager *m, JobMode mode) {
550 /* Moves the transaction jobs to the set of active jobs */
552 if (mode == JOB_ISOLATE) {
554 /* When isolating first kill all installed jobs which
555 * aren't part of the new transaction */
557 HASHMAP_FOREACH(j, m->jobs, i) {
558 assert(j->installed);
560 if (hashmap_get(tr->jobs, j->unit))
563 /* 'j' itself is safe to remove, but if other jobs
564 are invalidated recursively, our iterator may become
565 invalid and we need to start over. */
566 if (job_finish_and_invalidate(j, JOB_CANCELED) > 0)
571 HASHMAP_FOREACH(j, tr->jobs, i) {
573 assert(!j->transaction_prev);
574 assert(!j->transaction_next);
576 r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j);
581 while ((j = hashmap_steal_first(tr->jobs))) {
584 /* Clean the job dependencies */
585 transaction_unlink_job(tr, j, false);
587 installed_job = job_install(j);
588 if (installed_job != j) {
589 /* j has been merged into a previously installed job */
590 if (tr->anchor_job == j)
591 tr->anchor_job = installed_job;
592 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
597 job_add_to_run_queue(j);
598 job_add_to_dbus_queue(j);
606 HASHMAP_FOREACH(j, tr->jobs, i)
607 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
612 int transaction_activate(Transaction *tr, Manager *m, JobMode mode, DBusError *e) {
614 unsigned generation = 1;
618 /* This applies the changes recorded in tr->jobs to
619 * the actual list of jobs, if possible. */
621 /* First step: figure out which jobs matter */
622 transaction_find_jobs_that_matter_to_anchor(tr->anchor_job, generation++);
624 /* Second step: Try not to stop any running services if
625 * we don't have to. Don't try to reverse running
626 * jobs if we don't have to. */
627 if (mode == JOB_FAIL)
628 transaction_minimize_impact(tr);
630 /* Third step: Drop redundant jobs */
631 transaction_drop_redundant(tr);
634 /* Fourth step: Let's remove unneeded jobs that might
636 if (mode != JOB_ISOLATE)
637 transaction_collect_garbage(tr);
639 /* Fifth step: verify order makes sense and correct
640 * cycles if necessary and possible */
641 r = transaction_verify_order(tr, &generation, e);
646 log_warning("Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error(e, r));
650 /* Let's see if the resulting transaction ordering
651 * graph is still cyclic... */
655 /* Sixth step: let's drop unmergeable entries if
656 * necessary and possible, merge entries we can
658 r = transaction_merge_jobs(tr, e);
663 log_warning("Requested transaction contains unmergeable jobs: %s", bus_error(e, r));
667 /* Seventh step: an entry got dropped, let's garbage
668 * collect its dependencies. */
669 if (mode != JOB_ISOLATE)
670 transaction_collect_garbage(tr);
672 /* Let's see if the resulting transaction still has
673 * unmergeable entries ... */
676 /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
677 transaction_drop_redundant(tr);
679 /* Ninth step: check whether we can actually apply this */
680 if (mode == JOB_FAIL) {
681 r = transaction_is_destructive(tr, e);
683 log_notice("Requested transaction contradicts existing jobs: %s", bus_error(e, r));
688 /* Tenth step: apply changes */
689 r = transaction_apply(tr, m, mode);
691 log_warning("Failed to apply transaction: %s", strerror(-r));
695 assert(hashmap_isempty(tr->jobs));
700 static Job* transaction_add_one_job(Transaction *tr, JobType type, Unit *unit, bool override, bool *is_new) {
706 /* Looks for an existing prospective job and returns that. If
707 * it doesn't exist it is created and added to the prospective
710 f = hashmap_get(tr->jobs, unit);
712 LIST_FOREACH(transaction, j, f) {
713 assert(j->unit == unit);
715 if (j->type == type) {
722 j = job_new(unit, type);
728 j->matters_to_anchor = false;
729 j->override = override;
731 LIST_PREPEND(Job, transaction, f, j);
733 if (hashmap_replace(tr->jobs, unit, f) < 0) {
734 LIST_REMOVE(Job, transaction, f, j);
742 /* log_debug("Added job %s/%s to transaction.", unit->id, job_type_to_string(type)); */
747 static void transaction_unlink_job(Transaction *tr, Job *j, bool delete_dependencies) {
751 if (j->transaction_prev)
752 j->transaction_prev->transaction_next = j->transaction_next;
753 else if (j->transaction_next)
754 hashmap_replace(tr->jobs, j->unit, j->transaction_next);
756 hashmap_remove_value(tr->jobs, j->unit, j);
758 if (j->transaction_next)
759 j->transaction_next->transaction_prev = j->transaction_prev;
761 j->transaction_prev = j->transaction_next = NULL;
763 while (j->subject_list)
764 job_dependency_free(j->subject_list);
766 while (j->object_list) {
767 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
769 job_dependency_free(j->object_list);
771 if (other && delete_dependencies) {
772 log_debug("Deleting job %s/%s as dependency of job %s/%s",
773 other->unit->id, job_type_to_string(other->type),
774 j->unit->id, job_type_to_string(j->type));
775 transaction_delete_job(tr, other, delete_dependencies);
780 int transaction_add_job_and_dependencies(
788 bool ignore_requirements,
798 assert(type < _JOB_TYPE_MAX);
801 /* log_debug("Pulling in %s/%s from %s/%s", */
802 /* unit->id, job_type_to_string(type), */
803 /* by ? by->unit->id : "NA", */
804 /* by ? job_type_to_string(by->type) : "NA"); */
806 if (unit->load_state != UNIT_LOADED &&
807 unit->load_state != UNIT_ERROR &&
808 unit->load_state != UNIT_MASKED) {
809 dbus_set_error(e, BUS_ERROR_LOAD_FAILED, "Unit %s is not loaded properly.", unit->id);
813 if (type != JOB_STOP && unit->load_state == UNIT_ERROR) {
814 dbus_set_error(e, BUS_ERROR_LOAD_FAILED,
815 "Unit %s failed to load: %s. "
816 "See system logs and 'systemctl status %s' for details.",
818 strerror(-unit->load_error),
823 if (type != JOB_STOP && unit->load_state == UNIT_MASKED) {
824 dbus_set_error(e, BUS_ERROR_MASKED, "Unit %s is masked.", unit->id);
828 if (!unit_job_is_applicable(unit, type)) {
829 dbus_set_error(e, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE, "Job type %s is not applicable for unit %s.", job_type_to_string(type), unit->id);
833 /* First add the job. */
834 ret = transaction_add_one_job(tr, type, unit, override, &is_new);
838 ret->ignore_order = ret->ignore_order || ignore_order;
840 /* Then, add a link to the job. */
842 if (!job_dependency_new(by, ret, matters, conflicts))
845 /* If the job has no parent job, it is the anchor job. */
846 assert(!tr->anchor_job);
847 tr->anchor_job = ret;
849 if (is_new && !ignore_requirements) {
852 /* If we are following some other unit, make sure we
853 * add all dependencies of everybody following. */
854 if (unit_following_set(ret->unit, &following) > 0) {
855 SET_FOREACH(dep, following, i) {
856 r = transaction_add_job_and_dependencies(tr, type, dep, ret, false, override, false, false, ignore_order, e);
858 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
868 /* Finally, recursively add in all dependencies. */
869 if (type == JOB_START || type == JOB_RELOAD_OR_START || type == JOB_RESTART) {
870 SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRES], i) {
871 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, true, override, false, false, ignore_order, e);
881 SET_FOREACH(dep, ret->unit->dependencies[UNIT_BIND_TO], i) {
882 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, true, override, false, false, ignore_order, e);
892 SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRES_OVERRIDABLE], i) {
893 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, !override, override, false, false, ignore_order, e);
895 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
902 SET_FOREACH(dep, ret->unit->dependencies[UNIT_WANTS], i) {
903 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, false, false, false, false, ignore_order, e);
905 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
912 SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUISITE], i) {
913 r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, ret, true, override, false, false, ignore_order, e);
923 SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUISITE_OVERRIDABLE], i) {
924 r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, ret, !override, override, false, false, ignore_order, e);
926 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
933 SET_FOREACH(dep, ret->unit->dependencies[UNIT_CONFLICTS], i) {
934 r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, true, override, true, false, ignore_order, e);
944 SET_FOREACH(dep, ret->unit->dependencies[UNIT_CONFLICTED_BY], i) {
945 r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, false, override, false, false, ignore_order, e);
947 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
956 if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
958 SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRED_BY], i) {
959 r = transaction_add_job_and_dependencies(tr, type, dep, ret, true, override, false, false, ignore_order, e);
969 SET_FOREACH(dep, ret->unit->dependencies[UNIT_BOUND_BY], i) {
970 r = transaction_add_job_and_dependencies(tr, type, dep, ret, true, override, false, false, ignore_order, e);
981 if (type == JOB_RELOAD || type == JOB_RELOAD_OR_START) {
983 SET_FOREACH(dep, ret->unit->dependencies[UNIT_PROPAGATE_RELOAD_TO], i) {
984 r = transaction_add_job_and_dependencies(tr, JOB_RELOAD, dep, ret, false, override, false, false, ignore_order, e);
986 log_warning("Cannot add dependency reload job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
994 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
1003 int transaction_add_isolate_jobs(Transaction *tr, Manager *m) {
1012 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
1014 /* ignore aliases */
1018 if (u->ignore_on_isolate)
1021 /* No need to stop inactive jobs */
1022 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) && !u->job)
1025 /* Is there already something listed for this? */
1026 if (hashmap_get(tr->jobs, u))
1029 r = transaction_add_job_and_dependencies(tr, JOB_STOP, u, tr->anchor_job, true, false, false, false, false, NULL);
1031 log_warning("Cannot add isolate job for unit %s, ignoring: %s", u->id, strerror(-r));
1037 Transaction *transaction_new(void) {
1040 tr = new0(Transaction, 1);
1044 tr->jobs = hashmap_new(trivial_hash_func, trivial_compare_func);
1053 void transaction_free(Transaction *tr) {
1054 assert(hashmap_isempty(tr->jobs));
1055 hashmap_free(tr->jobs);