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);
18 static void transaction_delete_unit(Transaction *tr, Unit *u) {
21 /* Deletes all jobs associated with a certain unit from the
24 while ((j = hashmap_get(tr->jobs, u)))
25 transaction_delete_job(tr, j, true);
28 void transaction_abort(Transaction *tr) {
33 while ((j = hashmap_first(tr->jobs)))
34 transaction_delete_job(tr, j, true);
36 assert(hashmap_isempty(tr->jobs));
39 static void transaction_find_jobs_that_matter_to_anchor(Job *j, unsigned generation) {
42 /* A recursive sweep through the graph that marks all units
43 * that matter to the anchor job, i.e. are directly or
44 * indirectly a dependency of the anchor job via paths that
45 * are fully marked as mattering. */
47 j->matters_to_anchor = true;
48 j->generation = generation;
50 LIST_FOREACH(subject, l, j->subject_list) {
52 /* This link does not matter */
56 /* This unit has already been marked */
57 if (l->object->generation == generation)
60 transaction_find_jobs_that_matter_to_anchor(l->object, generation);
64 static void transaction_merge_and_delete_job(Transaction *tr, Job *j, Job *other, JobType t) {
65 JobDependency *l, *last;
69 assert(j->unit == other->unit);
70 assert(!j->installed);
72 /* Merges 'other' into 'j' and then deletes 'other'. */
75 j->state = JOB_WAITING;
76 j->override = j->override || other->override;
78 j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
80 /* Patch us in as new owner of the JobDependency objects */
82 LIST_FOREACH(subject, l, other->subject_list) {
83 assert(l->subject == other);
88 /* Merge both lists */
90 last->subject_next = j->subject_list;
92 j->subject_list->subject_prev = last;
93 j->subject_list = other->subject_list;
96 /* Patch us in as new owner of the JobDependency objects */
98 LIST_FOREACH(object, l, other->object_list) {
99 assert(l->object == other);
104 /* Merge both lists */
106 last->object_next = j->object_list;
108 j->object_list->object_prev = last;
109 j->object_list = other->object_list;
112 /* Kill the other job */
113 other->subject_list = NULL;
114 other->object_list = NULL;
115 transaction_delete_job(tr, other, true);
118 static bool job_is_conflicted_by(Job *j) {
123 /* Returns true if this job is pulled in by a least one
124 * ConflictedBy dependency. */
126 LIST_FOREACH(object, l, j->object_list)
133 static int delete_one_unmergeable_job(Transaction *tr, Job *j) {
138 /* Tries to delete one item in the linked list
139 * j->transaction_next->transaction_next->... that conflicts
140 * with another one, in an attempt to make an inconsistent
141 * transaction work. */
143 /* We rely here on the fact that if a merged with b does not
144 * merge with c, either a or b merge with c neither */
145 LIST_FOREACH(transaction, j, j)
146 LIST_FOREACH(transaction, k, j->transaction_next) {
149 /* Is this one mergeable? Then skip it */
150 if (job_type_is_mergeable(j->type, k->type))
153 /* Ok, we found two that conflict, let's see if we can
154 * drop one of them */
155 if (!j->matters_to_anchor && !k->matters_to_anchor) {
157 /* Both jobs don't matter, so let's
158 * find the one that is smarter to
159 * remove. Let's think positive and
160 * rather remove stops then starts --
161 * except if something is being
162 * stopped because it is conflicted by
163 * another unit in which case we
164 * rather remove the start. */
166 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)));
167 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)));
169 if (j->type == JOB_STOP) {
171 if (job_is_conflicted_by(j))
176 } else if (k->type == JOB_STOP) {
178 if (job_is_conflicted_by(k))
185 } else if (!j->matters_to_anchor)
187 else if (!k->matters_to_anchor)
192 /* Ok, we can drop one, so let's do so. */
193 log_debug("Fixing conflicting jobs by deleting job %s/%s", d->unit->id, job_type_to_string(d->type));
194 transaction_delete_job(tr, d, true);
201 static int transaction_merge_jobs(Transaction *tr, DBusError *e) {
208 /* First step, check whether any of the jobs for one specific
209 * task conflict. If so, try to drop one of them. */
210 HASHMAP_FOREACH(j, tr->jobs, i) {
215 LIST_FOREACH(transaction, k, j->transaction_next) {
216 if (job_type_merge(&t, k->type) >= 0)
219 /* OK, we could not merge all jobs for this
220 * action. Let's see if we can get rid of one
223 r = delete_one_unmergeable_job(tr, j);
225 /* Ok, we managed to drop one, now
226 * let's ask our callers to call us
227 * again after garbage collecting */
230 /* We couldn't merge anything. Failure */
231 dbus_set_error(e, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING, "Transaction contains conflicting jobs '%s' and '%s' for %s. Probably contradicting requirement dependencies configured.",
232 job_type_to_string(t), job_type_to_string(k->type), k->unit->id);
237 /* Second step, merge the jobs. */
238 HASHMAP_FOREACH(j, tr->jobs, i) {
242 /* Merge all transactions */
243 LIST_FOREACH(transaction, k, j->transaction_next)
244 assert_se(job_type_merge(&t, k->type) == 0);
246 while ((k = j->transaction_next)) {
247 if (tr->anchor_job == k) {
248 transaction_merge_and_delete_job(tr, k, j, t);
251 transaction_merge_and_delete_job(tr, j, k, t);
254 assert(!j->transaction_next);
255 assert(!j->transaction_prev);
261 static void transaction_drop_redundant(Transaction *tr) {
266 /* Goes through the transaction and removes all jobs that are
275 HASHMAP_FOREACH(j, tr->jobs, i) {
276 bool changes_something = false;
279 LIST_FOREACH(transaction, k, j) {
281 if (tr->anchor_job != k &&
282 (k->installed || job_type_is_redundant(k->type, unit_active_state(k->unit))) &&
283 (!k->unit->job || !job_type_is_conflicting(k->type, k->unit->job->type)))
286 changes_something = true;
290 if (changes_something)
293 /* log_debug("Found redundant job %s/%s, dropping.", j->unit->id, job_type_to_string(j->type)); */
294 transaction_delete_job(tr, j, false);
302 static bool unit_matters_to_anchor(Unit *u, Job *j) {
304 assert(!j->transaction_prev);
306 /* Checks whether at least one of the jobs for this unit
307 * matters to the anchor. */
309 LIST_FOREACH(transaction, j, j)
310 if (j->matters_to_anchor)
316 static int transaction_verify_order_one(Transaction *tr, Job *j, Job *from, unsigned generation, DBusError *e) {
323 assert(!j->transaction_prev);
325 /* Does a recursive sweep through the ordering graph, looking
326 * for a cycle. If we find cycle we try to break it. */
328 /* Have we seen this before? */
329 if (j->generation == generation) {
332 /* If the marker is NULL we have been here already and
333 * decided the job was loop-free from here. Hence
334 * shortcut things and return right-away. */
338 /* So, the marker is not NULL and we already have been
339 * here. We have a cycle. Let's try to break it. We go
340 * backwards in our path and try to find a suitable
341 * job to remove. We use the marker to find our way
342 * back, since smart how we are we stored our way back
344 log_warning("Found ordering cycle on %s/%s", j->unit->id, job_type_to_string(j->type));
347 for (k = from; k; k = ((k->generation == generation && k->marker != k) ? k->marker : NULL)) {
349 log_info("Walked on cycle path to %s/%s", k->unit->id, job_type_to_string(k->type));
353 !unit_matters_to_anchor(k->unit, k)) {
354 /* Ok, we can drop this one, so let's
359 /* Check if this in fact was the beginning of
367 log_warning("Breaking ordering cycle by deleting job %s/%s", delete->unit->id, job_type_to_string(delete->type));
368 transaction_delete_unit(tr, delete->unit);
372 log_error("Unable to break cycle");
374 dbus_set_error(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC, "Transaction order is cyclic. See system logs for details.");
378 /* Make the marker point to where we come from, so that we can
379 * find our way backwards if we want to break a cycle. We use
380 * a special marker for the beginning: we point to
382 j->marker = from ? from : j;
383 j->generation = generation;
385 /* We assume that the the dependencies are bidirectional, and
386 * hence can ignore UNIT_AFTER */
387 SET_FOREACH(u, j->unit->dependencies[UNIT_BEFORE], i) {
390 /* Is there a job for this unit? */
391 o = hashmap_get(tr->jobs, u);
393 /* Ok, there is no job for this in the
394 * transaction, but maybe there is already one
401 r = transaction_verify_order_one(tr, o, j, generation, e);
406 /* Ok, let's backtrack, and remember that this entry is not on
407 * our path anymore. */
413 static int transaction_verify_order(Transaction *tr, unsigned *generation, DBusError *e) {
422 /* Check if the ordering graph is cyclic. If it is, try to fix
423 * that up by dropping one of the jobs. */
427 HASHMAP_FOREACH(j, tr->jobs, i)
428 if ((r = transaction_verify_order_one(tr, j, NULL, g, e)) < 0)
434 static void transaction_collect_garbage(Transaction *tr) {
439 /* Drop jobs that are not required by any other job */
447 HASHMAP_FOREACH(j, tr->jobs, i) {
448 if (tr->anchor_job == j || j->object_list) {
449 /* log_debug("Keeping job %s/%s because of %s/%s", */
450 /* j->unit->id, job_type_to_string(j->type), */
451 /* j->object_list->subject ? j->object_list->subject->unit->id : "root", */
452 /* j->object_list->subject ? job_type_to_string(j->object_list->subject->type) : "root"); */
456 /* log_debug("Garbage collecting job %s/%s", j->unit->id, job_type_to_string(j->type)); */
457 transaction_delete_job(tr, j, true);
465 static int transaction_is_destructive(Transaction *tr, DBusError *e) {
471 /* Checks whether applying this transaction means that
472 * existing jobs would be replaced */
474 HASHMAP_FOREACH(j, tr->jobs, i) {
477 assert(!j->transaction_prev);
478 assert(!j->transaction_next);
482 !job_type_is_superset(j->type, j->unit->job->type)) {
484 dbus_set_error(e, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE, "Transaction is destructive.");
492 static void transaction_minimize_impact(Transaction *tr) {
496 /* Drops all unnecessary jobs that reverse already active jobs
497 * or that stop a running service. */
505 HASHMAP_FOREACH(j, tr->jobs, i) {
506 LIST_FOREACH(transaction, j, j) {
507 bool stops_running_service, changes_existing_job;
509 /* If it matters, we shouldn't drop it */
510 if (j->matters_to_anchor)
513 /* Would this stop a running service?
514 * Would this change an existing job?
515 * If so, let's drop this entry */
517 stops_running_service =
518 j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
520 changes_existing_job =
522 job_type_is_conflicting(j->type, j->unit->job->type);
524 if (!stops_running_service && !changes_existing_job)
527 if (stops_running_service)
528 log_debug("%s/%s would stop a running service.", j->unit->id, job_type_to_string(j->type));
530 if (changes_existing_job)
531 log_debug("%s/%s would change existing job.", j->unit->id, job_type_to_string(j->type));
533 /* Ok, let's get rid of this */
534 log_debug("Deleting %s/%s to minimize impact.", j->unit->id, job_type_to_string(j->type));
536 transaction_delete_job(tr, j, true);
548 static int transaction_apply(Transaction *tr, Manager *m, JobMode mode) {
553 /* Moves the transaction jobs to the set of active jobs */
555 if (mode == JOB_ISOLATE) {
557 /* When isolating first kill all installed jobs which
558 * aren't part of the new transaction */
560 HASHMAP_FOREACH(j, m->jobs, i) {
561 assert(j->installed);
563 if (hashmap_get(tr->jobs, j->unit))
566 /* 'j' itself is safe to remove, but if other jobs
567 are invalidated recursively, our iterator may become
568 invalid and we need to start over. */
569 if (job_finish_and_invalidate(j, JOB_CANCELED) > 0)
574 HASHMAP_FOREACH(j, tr->jobs, i) {
576 assert(!j->transaction_prev);
577 assert(!j->transaction_next);
582 r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j);
587 while ((j = hashmap_steal_first(tr->jobs))) {
591 /* log_debug("Skipping already installed job %s/%s as %u", j->unit->id, job_type_to_string(j->type), (unsigned) j->id); */
595 /* Clean the job dependencies */
596 transaction_unlink_job(tr, j, false);
598 installed_job = job_install(j);
599 if (installed_job != j) {
600 /* j has been merged into a previously installed job */
601 if (tr->anchor_job == j)
602 tr->anchor_job = installed_job;
603 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
608 job_add_to_run_queue(j);
609 job_add_to_dbus_queue(j);
617 HASHMAP_FOREACH(j, tr->jobs, i) {
621 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
627 int transaction_activate(Transaction *tr, Manager *m, JobMode mode, DBusError *e) {
629 unsigned generation = 1;
633 /* This applies the changes recorded in tr->jobs to
634 * the actual list of jobs, if possible. */
636 /* First step: figure out which jobs matter */
637 transaction_find_jobs_that_matter_to_anchor(tr->anchor_job, generation++);
639 /* Second step: Try not to stop any running services if
640 * we don't have to. Don't try to reverse running
641 * jobs if we don't have to. */
642 if (mode == JOB_FAIL)
643 transaction_minimize_impact(tr);
645 /* Third step: Drop redundant jobs */
646 transaction_drop_redundant(tr);
649 /* Fourth step: Let's remove unneeded jobs that might
651 if (mode != JOB_ISOLATE)
652 transaction_collect_garbage(tr);
654 /* Fifth step: verify order makes sense and correct
655 * cycles if necessary and possible */
656 r = transaction_verify_order(tr, &generation, e);
661 log_warning("Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error(e, r));
665 /* Let's see if the resulting transaction ordering
666 * graph is still cyclic... */
670 /* Sixth step: let's drop unmergeable entries if
671 * necessary and possible, merge entries we can
673 r = transaction_merge_jobs(tr, e);
678 log_warning("Requested transaction contains unmergeable jobs: %s", bus_error(e, r));
682 /* Seventh step: an entry got dropped, let's garbage
683 * collect its dependencies. */
684 if (mode != JOB_ISOLATE)
685 transaction_collect_garbage(tr);
687 /* Let's see if the resulting transaction still has
688 * unmergeable entries ... */
691 /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
692 transaction_drop_redundant(tr);
694 /* Ninth step: check whether we can actually apply this */
695 if (mode == JOB_FAIL) {
696 r = transaction_is_destructive(tr, e);
698 log_notice("Requested transaction contradicts existing jobs: %s", bus_error(e, r));
703 /* Tenth step: apply changes */
704 r = transaction_apply(tr, m, mode);
706 log_warning("Failed to apply transaction: %s", strerror(-r));
710 assert(hashmap_isempty(tr->jobs));
715 static Job* transaction_add_one_job(Transaction *tr, JobType type, Unit *unit, bool override, bool *is_new) {
721 /* Looks for an existing prospective job and returns that. If
722 * it doesn't exist it is created and added to the prospective
725 f = hashmap_get(tr->jobs, unit);
727 LIST_FOREACH(transaction, j, f) {
728 assert(j->unit == unit);
730 if (j->type == type) {
737 j = job_new(unit, type);
743 j->matters_to_anchor = false;
744 j->override = override;
746 LIST_PREPEND(Job, transaction, f, j);
748 if (hashmap_replace(tr->jobs, unit, f) < 0) {
749 LIST_REMOVE(Job, transaction, f, j);
757 /* log_debug("Added job %s/%s to transaction.", unit->id, job_type_to_string(type)); */
762 static void transaction_unlink_job(Transaction *tr, Job *j, bool delete_dependencies) {
766 if (j->transaction_prev)
767 j->transaction_prev->transaction_next = j->transaction_next;
768 else if (j->transaction_next)
769 hashmap_replace(tr->jobs, j->unit, j->transaction_next);
771 hashmap_remove_value(tr->jobs, j->unit, j);
773 if (j->transaction_next)
774 j->transaction_next->transaction_prev = j->transaction_prev;
776 j->transaction_prev = j->transaction_next = NULL;
778 while (j->subject_list)
779 job_dependency_free(j->subject_list);
781 while (j->object_list) {
782 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
784 job_dependency_free(j->object_list);
786 if (other && delete_dependencies) {
787 log_debug("Deleting job %s/%s as dependency of job %s/%s",
788 other->unit->id, job_type_to_string(other->type),
789 j->unit->id, job_type_to_string(j->type));
790 transaction_delete_job(tr, other, delete_dependencies);
795 int transaction_add_job_and_dependencies(
803 bool ignore_requirements,
813 assert(type < _JOB_TYPE_MAX);
816 /* log_debug("Pulling in %s/%s from %s/%s", */
817 /* unit->id, job_type_to_string(type), */
818 /* by ? by->unit->id : "NA", */
819 /* by ? job_type_to_string(by->type) : "NA"); */
821 if (unit->load_state != UNIT_LOADED &&
822 unit->load_state != UNIT_ERROR &&
823 unit->load_state != UNIT_MASKED) {
824 dbus_set_error(e, BUS_ERROR_LOAD_FAILED, "Unit %s is not loaded properly.", unit->id);
828 if (type != JOB_STOP && unit->load_state == UNIT_ERROR) {
829 dbus_set_error(e, BUS_ERROR_LOAD_FAILED,
830 "Unit %s failed to load: %s. "
831 "See system logs and 'systemctl status %s' for details.",
833 strerror(-unit->load_error),
838 if (type != JOB_STOP && unit->load_state == UNIT_MASKED) {
839 dbus_set_error(e, BUS_ERROR_MASKED, "Unit %s is masked.", unit->id);
843 if (!unit_job_is_applicable(unit, type)) {
844 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);
848 /* First add the job. */
849 ret = transaction_add_one_job(tr, type, unit, override, &is_new);
853 ret->ignore_order = ret->ignore_order || ignore_order;
855 /* Then, add a link to the job. */
857 if (!job_dependency_new(by, ret, matters, conflicts))
860 /* If the job has no parent job, it is the anchor job. */
861 assert(!tr->anchor_job);
862 tr->anchor_job = ret;
864 if (is_new && !ignore_requirements) {
867 /* If we are following some other unit, make sure we
868 * add all dependencies of everybody following. */
869 if (unit_following_set(ret->unit, &following) > 0) {
870 SET_FOREACH(dep, following, i) {
871 r = transaction_add_job_and_dependencies(tr, type, dep, ret, false, override, false, false, ignore_order, e);
873 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
883 /* Finally, recursively add in all dependencies. */
884 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
885 SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRES], i) {
886 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, true, override, false, false, ignore_order, e);
896 SET_FOREACH(dep, ret->unit->dependencies[UNIT_BIND_TO], i) {
897 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, true, override, false, false, ignore_order, e);
907 SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRES_OVERRIDABLE], i) {
908 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, !override, override, false, false, ignore_order, e);
910 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
917 SET_FOREACH(dep, ret->unit->dependencies[UNIT_WANTS], i) {
918 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, false, false, false, false, ignore_order, e);
920 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
927 SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUISITE], i) {
928 r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, ret, true, override, false, false, ignore_order, e);
938 SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUISITE_OVERRIDABLE], i) {
939 r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, ret, !override, override, false, false, ignore_order, e);
941 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
948 SET_FOREACH(dep, ret->unit->dependencies[UNIT_CONFLICTS], i) {
949 r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, true, override, true, false, ignore_order, e);
959 SET_FOREACH(dep, ret->unit->dependencies[UNIT_CONFLICTED_BY], i) {
960 r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, false, override, false, false, ignore_order, e);
962 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
971 if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
973 SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRED_BY], i) {
974 r = transaction_add_job_and_dependencies(tr, type, dep, ret, true, override, false, false, ignore_order, e);
984 SET_FOREACH(dep, ret->unit->dependencies[UNIT_BOUND_BY], i) {
985 r = transaction_add_job_and_dependencies(tr, type, dep, ret, true, override, false, false, ignore_order, e);
996 if (type == JOB_RELOAD || type == JOB_RELOAD_OR_START) {
998 SET_FOREACH(dep, ret->unit->dependencies[UNIT_PROPAGATE_RELOAD_TO], i) {
999 r = transaction_add_job_and_dependencies(tr, JOB_RELOAD, dep, ret, false, override, false, false, ignore_order, e);
1001 log_warning("Cannot add dependency reload job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
1009 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
1018 int transaction_add_isolate_jobs(Transaction *tr, Manager *m) {
1027 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
1029 /* ignore aliases */
1033 if (u->ignore_on_isolate)
1036 /* No need to stop inactive jobs */
1037 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) && !u->job)
1040 /* Is there already something listed for this? */
1041 if (hashmap_get(tr->jobs, u))
1044 r = transaction_add_job_and_dependencies(tr, JOB_STOP, u, tr->anchor_job, true, false, false, false, false, NULL);
1046 log_warning("Cannot add isolate job for unit %s, ignoring: %s", u->id, strerror(-r));
1052 Transaction *transaction_new(void) {
1055 tr = new0(Transaction, 1);
1059 tr->jobs = hashmap_new(trivial_hash_func, trivial_compare_func);
1068 void transaction_free(Transaction *tr) {
1069 assert(hashmap_isempty(tr->jobs));
1070 hashmap_free(tr->jobs);