1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
7 #include "bus-common-errors.h"
11 #include "terminal-util.h"
12 #include "transaction.h"
14 static void transaction_unlink_job(Transaction
*tr
, Job
*j
, bool delete_dependencies
);
16 static void transaction_delete_job(Transaction
*tr
, Job
*j
, bool delete_dependencies
) {
20 /* Deletes one job from the transaction */
22 transaction_unlink_job(tr
, j
, delete_dependencies
);
27 static void transaction_delete_unit(Transaction
*tr
, Unit
*u
) {
30 /* Deletes all jobs associated with a certain unit from the
33 while ((j
= hashmap_get(tr
->jobs
, u
)))
34 transaction_delete_job(tr
, j
, true);
37 static void transaction_abort(Transaction
*tr
) {
42 while ((j
= hashmap_first(tr
->jobs
)))
43 transaction_delete_job(tr
, j
, false);
45 assert(hashmap_isempty(tr
->jobs
));
48 static void transaction_find_jobs_that_matter_to_anchor(Job
*j
, unsigned generation
) {
51 /* A recursive sweep through the graph that marks all units
52 * that matter to the anchor job, i.e. are directly or
53 * indirectly a dependency of the anchor job via paths that
54 * are fully marked as mattering. */
56 j
->matters_to_anchor
= true;
57 j
->generation
= generation
;
59 LIST_FOREACH(subject
, l
, j
->subject_list
) {
61 /* This link does not matter */
65 /* This unit has already been marked */
66 if (l
->object
->generation
== generation
)
69 transaction_find_jobs_that_matter_to_anchor(l
->object
, generation
);
73 static void transaction_merge_and_delete_job(Transaction
*tr
, Job
*j
, Job
*other
, JobType t
) {
78 assert(j
->unit
== other
->unit
);
79 assert(!j
->installed
);
81 /* Merges 'other' into 'j' and then deletes 'other'. */
84 j
->state
= JOB_WAITING
;
85 j
->irreversible
= j
->irreversible
|| other
->irreversible
;
86 j
->matters_to_anchor
= j
->matters_to_anchor
|| other
->matters_to_anchor
;
88 /* Patch us in as new owner of the JobDependency objects */
90 LIST_FOREACH(subject
, l
, other
->subject_list
) {
91 assert(l
->subject
== other
);
96 /* Merge both lists */
98 last
->subject_next
= j
->subject_list
;
100 j
->subject_list
->subject_prev
= last
;
101 j
->subject_list
= other
->subject_list
;
104 /* Patch us in as new owner of the JobDependency objects */
106 LIST_FOREACH(object
, l
, other
->object_list
) {
107 assert(l
->object
== other
);
112 /* Merge both lists */
114 last
->object_next
= j
->object_list
;
116 j
->object_list
->object_prev
= last
;
117 j
->object_list
= other
->object_list
;
120 /* Kill the other job */
121 other
->subject_list
= NULL
;
122 other
->object_list
= NULL
;
123 transaction_delete_job(tr
, other
, true);
126 _pure_
static bool job_is_conflicted_by(Job
*j
) {
129 /* Returns true if this job is pulled in by a least one
130 * ConflictedBy dependency. */
132 LIST_FOREACH(object
, l
, j
->object_list
)
139 static int delete_one_unmergeable_job(Transaction
*tr
, Job
*job
) {
142 /* Tries to delete one item in the linked list
143 * j->transaction_next->transaction_next->... that conflicts
144 * with another one, in an attempt to make an inconsistent
145 * transaction work. */
147 /* We rely here on the fact that if a merged with b does not
148 * merge with c, either a or b merge with c neither */
149 LIST_FOREACH(transaction
, j
, job
)
150 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
153 /* Is this one mergeable? Then skip it */
154 if (job_type_is_mergeable(j
->type
, k
->type
))
157 /* Ok, we found two that conflict, let's see if we can
158 * drop one of them */
159 if (!j
->matters_to_anchor
&& !k
->matters_to_anchor
) {
161 /* Both jobs don't matter, so let's
162 * find the one that is smarter to
163 * remove. Let's think positive and
164 * rather remove stops then starts --
165 * except if something is being
166 * stopped because it is conflicted by
167 * another unit in which case we
168 * rather remove the start. */
170 log_unit_debug(j
->unit
,
171 "Looking at job %s/%s conflicted_by=%s",
172 j
->unit
->id
, job_type_to_string(j
->type
),
173 yes_no(j
->type
== JOB_STOP
&& job_is_conflicted_by(j
)));
174 log_unit_debug(k
->unit
,
175 "Looking at job %s/%s conflicted_by=%s",
176 k
->unit
->id
, job_type_to_string(k
->type
),
177 yes_no(k
->type
== JOB_STOP
&& job_is_conflicted_by(k
)));
179 if (j
->type
== JOB_STOP
) {
181 if (job_is_conflicted_by(j
))
186 } else if (k
->type
== JOB_STOP
) {
188 if (job_is_conflicted_by(k
))
195 } else if (!j
->matters_to_anchor
)
197 else if (!k
->matters_to_anchor
)
202 /* Ok, we can drop one, so let's do so. */
203 log_unit_debug(d
->unit
,
204 "Fixing conflicting jobs %s/%s,%s/%s by deleting job %s/%s",
205 j
->unit
->id
, job_type_to_string(j
->type
),
206 k
->unit
->id
, job_type_to_string(k
->type
),
207 d
->unit
->id
, job_type_to_string(d
->type
));
208 transaction_delete_job(tr
, d
, true);
215 static int transaction_merge_jobs(Transaction
*tr
, sd_bus_error
*e
) {
221 /* First step, check whether any of the jobs for one specific
222 * task conflict. If so, try to drop one of them. */
223 HASHMAP_FOREACH(j
, tr
->jobs
) {
227 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
228 if (job_type_merge_and_collapse(&t
, k
->type
, j
->unit
) >= 0)
231 /* OK, we could not merge all jobs for this
232 * action. Let's see if we can get rid of one
235 r
= delete_one_unmergeable_job(tr
, j
);
237 /* Ok, we managed to drop one, now
238 * let's ask our callers to call us
239 * again after garbage collecting */
242 /* We couldn't merge anything. Failure */
243 return sd_bus_error_setf(e
, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING
,
244 "Transaction contains conflicting jobs '%s' and '%s' for %s. "
245 "Probably contradicting requirement dependencies configured.",
246 job_type_to_string(t
),
247 job_type_to_string(k
->type
),
252 /* Second step, merge the jobs. */
253 HASHMAP_FOREACH(j
, tr
->jobs
) {
256 /* Merge all transaction jobs for j->unit */
257 LIST_FOREACH(transaction
, k
, j
->transaction_next
)
258 assert_se(job_type_merge_and_collapse(&t
, k
->type
, j
->unit
) == 0);
261 while ((k
= j
->transaction_next
)) {
262 if (tr
->anchor_job
== k
) {
263 transaction_merge_and_delete_job(tr
, k
, j
, t
);
266 transaction_merge_and_delete_job(tr
, j
, k
, t
);
269 assert(!j
->transaction_next
);
270 assert(!j
->transaction_prev
);
276 static void transaction_drop_redundant(Transaction
*tr
) {
279 /* Goes through the transaction and removes all jobs of the units whose jobs are all noops. If not
280 * all of a unit's jobs are redundant, they are kept. */
289 HASHMAP_FOREACH(j
, tr
->jobs
) {
292 LIST_FOREACH(transaction
, k
, j
)
293 if (tr
->anchor_job
== k
||
294 !job_type_is_redundant(k
->type
, unit_active_state(k
->unit
)) ||
295 (k
->unit
->job
&& job_type_is_conflicting(k
->type
, k
->unit
->job
->type
))) {
301 log_trace("Found redundant job %s/%s, dropping from transaction.",
302 j
->unit
->id
, job_type_to_string(j
->type
));
303 transaction_delete_job(tr
, j
, false);
311 _pure_
static bool job_matters_to_anchor(Job
*job
) {
313 assert(!job
->transaction_prev
);
315 /* Checks whether at least one of the jobs for this transaction matters to the anchor. */
317 LIST_FOREACH(transaction
, j
, job
)
318 if (j
->matters_to_anchor
)
324 static char* merge_unit_ids(const char* unit_log_field
, char * const* pairs
) {
325 _cleanup_free_
char *ans
= NULL
;
328 assert(unit_log_field
);
330 STRV_FOREACH_PAIR(unit_id
, job_type
, pairs
) {
334 ans
[size
- 1] = '\n';
336 next
= strlen(unit_log_field
) + strlen(*unit_id
);
337 if (!GREEDY_REALLOC(ans
, size
+ next
+ 1))
340 sprintf(ans
+ size
, "%s%s", unit_log_field
, *unit_id
);
347 return TAKE_PTR(ans
);
350 static int transaction_verify_order_one(Transaction
*tr
, Job
*j
, Job
*from
, unsigned generation
, sd_bus_error
*e
) {
352 static const UnitDependencyAtom directions
[] = {
361 assert(!j
->transaction_prev
);
363 /* Does a recursive sweep through the ordering graph, looking for a cycle. If we find a cycle we try
366 /* Have we seen this before? */
367 if (j
->generation
== generation
) {
368 Job
*k
, *delete = NULL
;
369 _cleanup_free_
char **array
= NULL
, *unit_ids
= NULL
;
371 /* If the marker is NULL we have been here already and decided the job was loop-free from
372 * here. Hence shortcut things and return right-away. */
376 /* So, the marker is not NULL and we already have been here. We have a cycle. Let's try to
377 * break it. We go backwards in our path and try to find a suitable job to remove. We use the
378 * marker to find our way back, since smart how we are we stored our way back in there. */
379 for (k
= from
; k
; k
= ((k
->generation
== generation
&& k
->marker
!= k
) ? k
->marker
: NULL
)) {
381 /* For logging below */
382 if (strv_push_pair(&array
, k
->unit
->id
, (char*) job_type_to_string(k
->type
)) < 0)
385 if (!delete && hashmap_contains(tr
->jobs
, k
->unit
) && !job_matters_to_anchor(k
))
386 /* Ok, we can drop this one, so let's do so. */
389 /* Check if this in fact was the beginning of the cycle */
394 unit_ids
= merge_unit_ids(j
->manager
->unit_log_field
, array
); /* ignore error */
396 STRV_FOREACH_PAIR(unit_id
, job_type
, array
)
397 /* logging for j not k here to provide a consistent narrative */
398 log_struct(LOG_WARNING
,
399 LOG_UNIT_MESSAGE(j
->unit
,
401 unit_id
== array
? "ordering cycle" : "dependency",
402 *unit_id
, *job_type
),
403 "%s", strna(unit_ids
));
407 /* logging for j not k here to provide a consistent narrative */
409 LOG_UNIT_MESSAGE(j
->unit
,
410 "Job %s/%s deleted to break ordering cycle starting with %s/%s",
411 delete->unit
->id
, job_type_to_string(delete->type
),
412 j
->unit
->id
, job_type_to_string(j
->type
)),
413 "%s", strna(unit_ids
));
415 if (log_get_show_color())
416 status
= ANSI_HIGHLIGHT_RED
" SKIP " ANSI_NORMAL
;
420 unit_status_printf(delete->unit
,
423 "Ordering cycle found, skipping %s",
424 unit_status_string(delete->unit
, NULL
));
425 transaction_delete_unit(tr
, delete->unit
);
430 LOG_UNIT_MESSAGE(j
->unit
, "Unable to break cycle starting with %s/%s",
431 j
->unit
->id
, job_type_to_string(j
->type
)),
432 "%s", strna(unit_ids
));
434 return sd_bus_error_setf(e
, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC
,
435 "Transaction order is cyclic. See system logs for details.");
438 /* Make the marker point to where we come from, so that we can
439 * find our way backwards if we want to break a cycle. We use
440 * a special marker for the beginning: we point to
442 j
->marker
= from
?: j
;
443 j
->generation
= generation
;
445 /* Actual ordering of jobs depends on the unit ordering dependency and job types. We need to traverse
446 * the graph over 'before' edges in the actual job execution order. We traverse over both unit
447 * ordering dependencies and we test with job_compare() whether it is the 'before' edge in the job
448 * execution ordering. */
449 for (size_t d
= 0; d
< ELEMENTSOF(directions
); d
++) {
452 UNIT_FOREACH_DEPENDENCY(u
, j
->unit
, directions
[d
]) {
455 /* Is there a job for this unit? */
456 o
= hashmap_get(tr
->jobs
, u
);
458 /* Ok, there is no job for this in the transaction, but maybe there is
459 * already one running? */
465 /* Cut traversing if the job j is not really *before* o. */
466 if (job_compare(j
, o
, directions
[d
]) >= 0)
469 r
= transaction_verify_order_one(tr
, o
, j
, generation
, e
);
475 /* Ok, let's backtrack, and remember that this entry is not on
476 * our path anymore. */
482 static int transaction_verify_order(Transaction
*tr
, unsigned *generation
, sd_bus_error
*e
) {
490 /* Check if the ordering graph is cyclic. If it is, try to fix
491 * that up by dropping one of the jobs. */
495 HASHMAP_FOREACH(j
, tr
->jobs
) {
496 r
= transaction_verify_order_one(tr
, j
, NULL
, g
, e
);
504 static void transaction_collect_garbage(Transaction
*tr
) {
509 /* Drop jobs that are not required by any other job */
516 HASHMAP_FOREACH(j
, tr
->jobs
) {
517 if (tr
->anchor_job
== j
)
520 if (!j
->object_list
) {
521 log_trace("Garbage collecting job %s/%s", j
->unit
->id
, job_type_to_string(j
->type
));
522 transaction_delete_job(tr
, j
, true);
527 log_trace("Keeping job %s/%s because of %s/%s",
528 j
->unit
->id
, job_type_to_string(j
->type
),
529 j
->object_list
->subject
? j
->object_list
->subject
->unit
->id
: "root",
530 j
->object_list
->subject
? job_type_to_string(j
->object_list
->subject
->type
) : "root");
536 static int transaction_is_destructive(Transaction
*tr
, JobMode mode
, sd_bus_error
*e
) {
541 /* Checks whether applying this transaction means that
542 * existing jobs would be replaced */
544 HASHMAP_FOREACH(j
, tr
->jobs
) {
547 assert(!j
->transaction_prev
);
548 assert(!j
->transaction_next
);
550 if (j
->unit
->job
&& (mode
== JOB_FAIL
|| j
->unit
->job
->irreversible
) &&
551 job_type_is_conflicting(j
->unit
->job
->type
, j
->type
))
552 return sd_bus_error_setf(e
, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE
,
553 "Transaction for %s/%s is destructive (%s has '%s' job queued, but '%s' is included in transaction).",
554 tr
->anchor_job
->unit
->id
, job_type_to_string(tr
->anchor_job
->type
),
555 j
->unit
->id
, job_type_to_string(j
->unit
->job
->type
), job_type_to_string(j
->type
));
561 static void transaction_minimize_impact(Transaction
*tr
) {
566 /* Drops all unnecessary jobs that reverse already active jobs
567 * or that stop a running service. */
570 HASHMAP_FOREACH(head
, tr
->jobs
) {
571 LIST_FOREACH(transaction
, j
, head
) {
572 bool stops_running_service
, changes_existing_job
;
574 /* If it matters, we shouldn't drop it */
575 if (j
->matters_to_anchor
)
578 /* Would this stop a running service?
579 * Would this change an existing job?
580 * If so, let's drop this entry */
582 stops_running_service
=
583 j
->type
== JOB_STOP
&& UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j
->unit
));
585 changes_existing_job
=
587 job_type_is_conflicting(j
->type
, j
->unit
->job
->type
);
589 if (!stops_running_service
&& !changes_existing_job
)
592 if (stops_running_service
)
593 log_unit_debug(j
->unit
,
594 "%s/%s would stop a running service.",
595 j
->unit
->id
, job_type_to_string(j
->type
));
597 if (changes_existing_job
)
598 log_unit_debug(j
->unit
,
599 "%s/%s would change existing job.",
600 j
->unit
->id
, job_type_to_string(j
->type
));
602 /* Ok, let's get rid of this */
603 log_unit_debug(j
->unit
,
604 "Deleting %s/%s to minimize impact.",
605 j
->unit
->id
, job_type_to_string(j
->type
));
607 transaction_delete_job(tr
, j
, true);
613 static int transaction_apply(
617 Set
*affected_jobs
) {
622 /* Moves the transaction jobs to the set of active jobs */
624 if (IN_SET(mode
, JOB_ISOLATE
, JOB_FLUSH
)) {
626 /* When isolating first kill all installed jobs which
627 * aren't part of the new transaction */
628 HASHMAP_FOREACH(j
, m
->jobs
) {
629 assert(j
->installed
);
631 if (j
->unit
->ignore_on_isolate
)
634 if (hashmap_contains(tr
->jobs
, j
->unit
))
637 /* Not invalidating recursively. Avoids triggering
638 * OnFailure= actions of dependent jobs. Also avoids
639 * invalidating our iterator. */
640 job_finish_and_invalidate(j
, JOB_CANCELED
, false, false);
644 HASHMAP_FOREACH(j
, tr
->jobs
) {
646 assert(!j
->transaction_prev
);
647 assert(!j
->transaction_next
);
649 r
= hashmap_ensure_put(&m
->jobs
, NULL
, UINT32_TO_PTR(j
->id
), j
);
654 while ((j
= hashmap_steal_first(tr
->jobs
))) {
657 /* Clean the job dependencies */
658 transaction_unlink_job(tr
, j
, false);
660 installed_job
= job_install(j
);
661 if (installed_job
!= j
) {
662 /* j has been merged into a previously installed job */
663 if (tr
->anchor_job
== j
)
664 tr
->anchor_job
= installed_job
;
665 hashmap_remove_value(m
->jobs
, UINT32_TO_PTR(j
->id
), j
);
670 job_add_to_run_queue(j
);
671 job_add_to_dbus_queue(j
);
672 job_start_timer(j
, false);
673 job_shutdown_magic(j
);
675 /* When 'affected' is specified, let's track all in it all jobs that were touched because of
676 * this transaction. */
678 (void) set_put(affected_jobs
, j
);
685 HASHMAP_FOREACH(j
, tr
->jobs
)
686 hashmap_remove_value(m
->jobs
, UINT32_TO_PTR(j
->id
), j
);
691 int transaction_activate(
700 unsigned generation
= 1;
704 /* This applies the changes recorded in tr->jobs to
705 * the actual list of jobs, if possible. */
707 /* Reset the generation counter of all installed jobs. The detection of cycles
708 * looks at installed jobs. If they had a non-zero generation from some previous
709 * walk of the graph, the algorithm would break. */
710 HASHMAP_FOREACH(j
, m
->jobs
)
713 /* First step: figure out which jobs matter */
714 transaction_find_jobs_that_matter_to_anchor(tr
->anchor_job
, generation
++);
716 /* Second step: Try not to stop any running services if
717 * we don't have to. Don't try to reverse running
718 * jobs if we don't have to. */
719 if (mode
== JOB_FAIL
)
720 transaction_minimize_impact(tr
);
722 /* Third step: Drop redundant jobs */
723 transaction_drop_redundant(tr
);
726 /* Fourth step: Let's remove unneeded jobs that might
728 if (mode
!= JOB_ISOLATE
)
729 transaction_collect_garbage(tr
);
731 /* Fifth step: verify order makes sense and correct
732 * cycles if necessary and possible */
733 r
= transaction_verify_order(tr
, &generation
, e
);
738 return log_warning_errno(r
, "Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error_message(e
, r
));
740 /* Let's see if the resulting transaction ordering
741 * graph is still cyclic... */
745 /* Sixth step: let's drop unmergeable entries if
746 * necessary and possible, merge entries we can
748 r
= transaction_merge_jobs(tr
, e
);
753 return log_warning_errno(r
, "Requested transaction contains unmergeable jobs: %s", bus_error_message(e
, r
));
755 /* Seventh step: an entry got dropped, let's garbage
756 * collect its dependencies. */
757 if (mode
!= JOB_ISOLATE
)
758 transaction_collect_garbage(tr
);
760 /* Let's see if the resulting transaction still has
761 * unmergeable entries ... */
764 /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
765 transaction_drop_redundant(tr
);
767 /* Ninth step: check whether we can actually apply this */
768 r
= transaction_is_destructive(tr
, mode
, e
);
770 return log_notice_errno(r
, "Requested transaction contradicts existing jobs: %s", bus_error_message(e
, r
));
772 /* Tenth step: apply changes */
773 r
= transaction_apply(tr
, m
, mode
, affected_jobs
);
775 return log_warning_errno(r
, "Failed to apply transaction: %m");
777 assert(hashmap_isempty(tr
->jobs
));
779 if (!hashmap_isempty(m
->jobs
)) {
780 /* Are there any jobs now? Then make sure we have the
781 * idle pipe around. We don't really care too much
782 * whether this works or not, as the idle pipe is a
783 * feature for cosmetics, not actually useful for
784 * anything beyond that. */
786 if (m
->idle_pipe
[0] < 0 && m
->idle_pipe
[1] < 0 &&
787 m
->idle_pipe
[2] < 0 && m
->idle_pipe
[3] < 0) {
788 (void) pipe2(m
->idle_pipe
, O_NONBLOCK
|O_CLOEXEC
);
789 (void) pipe2(m
->idle_pipe
+ 2, O_NONBLOCK
|O_CLOEXEC
);
796 static Job
* transaction_add_one_job(Transaction
*tr
, JobType type
, Unit
*unit
, bool *is_new
) {
802 /* Looks for an existing prospective job and returns that. If
803 * it doesn't exist it is created and added to the prospective
806 f
= hashmap_get(tr
->jobs
, unit
);
808 LIST_FOREACH(transaction
, i
, f
) {
809 assert(i
->unit
== unit
);
811 if (i
->type
== type
) {
818 j
= job_new(unit
, type
);
824 j
->matters_to_anchor
= false;
825 j
->irreversible
= tr
->irreversible
;
827 LIST_PREPEND(transaction
, f
, j
);
829 if (hashmap_replace(tr
->jobs
, unit
, f
) < 0) {
830 LIST_REMOVE(transaction
, f
, j
);
838 log_trace("Added job %s/%s to transaction.", unit
->id
, job_type_to_string(type
));
843 static void transaction_unlink_job(Transaction
*tr
, Job
*j
, bool delete_dependencies
) {
847 if (j
->transaction_prev
)
848 j
->transaction_prev
->transaction_next
= j
->transaction_next
;
849 else if (j
->transaction_next
)
850 hashmap_replace(tr
->jobs
, j
->unit
, j
->transaction_next
);
852 hashmap_remove_value(tr
->jobs
, j
->unit
, j
);
854 if (j
->transaction_next
)
855 j
->transaction_next
->transaction_prev
= j
->transaction_prev
;
857 j
->transaction_prev
= j
->transaction_next
= NULL
;
859 while (j
->subject_list
)
860 job_dependency_free(j
->subject_list
);
862 while (j
->object_list
) {
863 Job
*other
= j
->object_list
->matters
? j
->object_list
->subject
: NULL
;
865 job_dependency_free(j
->object_list
);
867 if (other
&& delete_dependencies
) {
868 log_unit_debug(other
->unit
,
869 "Deleting job %s/%s as dependency of job %s/%s",
870 other
->unit
->id
, job_type_to_string(other
->type
),
871 j
->unit
->id
, job_type_to_string(j
->type
));
872 transaction_delete_job(tr
, other
, delete_dependencies
);
877 void transaction_add_propagate_reload_jobs(Transaction
*tr
, Unit
*unit
, Job
*by
, bool ignore_order
, sd_bus_error
*e
) {
885 UNIT_FOREACH_DEPENDENCY(dep
, unit
, UNIT_ATOM_PROPAGATES_RELOAD_TO
) {
886 nt
= job_type_collapse(JOB_TRY_RELOAD
, dep
);
890 r
= transaction_add_job_and_dependencies(tr
, nt
, dep
, by
, false, false, false, ignore_order
, e
);
892 log_unit_warning(dep
,
893 "Cannot add dependency reload job, ignoring: %s",
894 bus_error_message(e
, r
));
895 sd_bus_error_free(e
);
900 int transaction_add_job_and_dependencies(
907 bool ignore_requirements
,
917 assert(type
< _JOB_TYPE_MAX
);
918 assert(type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
921 /* Before adding jobs for this unit, let's ensure that its state has been loaded
922 * This matters when jobs are spawned as part of coldplugging itself (see e. g. path_coldplug()).
923 * This way, we "recursively" coldplug units, ensuring that we do not look at state of
924 * not-yet-coldplugged units. */
925 if (MANAGER_IS_RELOADING(unit
->manager
))
929 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
));
931 /* Safety check that the unit is a valid state, i.e. not in UNIT_STUB or UNIT_MERGED which should only be set
933 if (!UNIT_IS_LOAD_COMPLETE(unit
->load_state
))
934 return sd_bus_error_setf(e
, BUS_ERROR_LOAD_FAILED
, "Unit %s is not loaded properly.", unit
->id
);
936 if (type
!= JOB_STOP
) {
937 r
= bus_unit_validate_load_state(unit
, e
);
938 /* The time-based cache allows to start new units without daemon-reload,
939 * but if they are already referenced (because of dependencies or ordering)
940 * then we have to force a load of the fragment. As an optimization, check
941 * first if anything in the usual paths was modified since the last time
942 * the cache was loaded. Also check if the last time an attempt to load the
943 * unit was made was before the most recent cache refresh, so that we know
944 * we need to try again — even if the cache is current, it might have been
945 * updated in a different context before we had a chance to retry loading
946 * this particular unit.
948 * Given building up the transaction is a synchronous operation, attempt
949 * to load the unit immediately. */
950 if (r
< 0 && manager_unit_cache_should_retry_load(unit
)) {
951 sd_bus_error_free(e
);
952 unit
->load_state
= UNIT_STUB
;
954 if (r
< 0 || unit
->load_state
== UNIT_STUB
)
955 unit
->load_state
= UNIT_NOT_FOUND
;
956 r
= bus_unit_validate_load_state(unit
, e
);
962 if (!unit_job_is_applicable(unit
, type
))
963 return sd_bus_error_setf(e
, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE
,
964 "Job type %s is not applicable for unit %s.",
965 job_type_to_string(type
), unit
->id
);
967 /* First add the job. */
968 ret
= transaction_add_one_job(tr
, type
, unit
, &is_new
);
972 ret
->ignore_order
= ret
->ignore_order
|| ignore_order
;
974 /* Then, add a link to the job. */
976 if (!job_dependency_new(by
, ret
, matters
, conflicts
))
979 /* If the job has no parent job, it is the anchor job. */
980 assert(!tr
->anchor_job
);
981 tr
->anchor_job
= ret
;
984 if (is_new
&& !ignore_requirements
&& type
!= JOB_NOP
) {
985 _cleanup_set_free_ Set
*following
= NULL
;
987 /* If we are following some other unit, make sure we
988 * add all dependencies of everybody following. */
989 if (unit_following_set(ret
->unit
, &following
) > 0)
990 SET_FOREACH(dep
, following
) {
991 r
= transaction_add_job_and_dependencies(tr
, type
, dep
, ret
, false, false, false, ignore_order
, e
);
993 log_unit_full_errno(dep
, r
== -ERFKILL
? LOG_INFO
: LOG_WARNING
, r
,
994 "Cannot add dependency job, ignoring: %s",
995 bus_error_message(e
, r
));
996 sd_bus_error_free(e
);
1000 /* Finally, recursively add in all dependencies. */
1001 if (IN_SET(type
, JOB_START
, JOB_RESTART
)) {
1002 UNIT_FOREACH_DEPENDENCY(dep
, ret
->unit
, UNIT_ATOM_PULL_IN_START
) {
1003 r
= transaction_add_job_and_dependencies(tr
, JOB_START
, dep
, ret
, true, false, false, ignore_order
, e
);
1005 if (r
!= -EBADR
) /* job type not applicable */
1008 sd_bus_error_free(e
);
1012 UNIT_FOREACH_DEPENDENCY(dep
, ret
->unit
, UNIT_ATOM_PULL_IN_START_IGNORED
) {
1013 r
= transaction_add_job_and_dependencies(tr
, JOB_START
, dep
, ret
, false, false, false, ignore_order
, e
);
1015 /* unit masked, job type not applicable and unit not found are not considered as errors. */
1016 log_unit_full_errno(dep
,
1017 IN_SET(r
, -ERFKILL
, -EBADR
, -ENOENT
) ? LOG_DEBUG
: LOG_WARNING
,
1018 r
, "Cannot add dependency job, ignoring: %s",
1019 bus_error_message(e
, r
));
1020 sd_bus_error_free(e
);
1024 UNIT_FOREACH_DEPENDENCY(dep
, ret
->unit
, UNIT_ATOM_PULL_IN_VERIFY
) {
1025 r
= transaction_add_job_and_dependencies(tr
, JOB_VERIFY_ACTIVE
, dep
, ret
, true, false, false, ignore_order
, e
);
1027 if (r
!= -EBADR
) /* job type not applicable */
1030 sd_bus_error_free(e
);
1034 UNIT_FOREACH_DEPENDENCY(dep
, ret
->unit
, UNIT_ATOM_PULL_IN_STOP
) {
1035 r
= transaction_add_job_and_dependencies(tr
, JOB_STOP
, dep
, ret
, true, true, false, ignore_order
, e
);
1037 if (r
!= -EBADR
) /* job type not applicable */
1040 sd_bus_error_free(e
);
1044 UNIT_FOREACH_DEPENDENCY(dep
, ret
->unit
, UNIT_ATOM_PULL_IN_STOP_IGNORED
) {
1045 r
= transaction_add_job_and_dependencies(tr
, JOB_STOP
, dep
, ret
, false, false, false, ignore_order
, e
);
1047 log_unit_warning(dep
,
1048 "Cannot add dependency job, ignoring: %s",
1049 bus_error_message(e
, r
));
1050 sd_bus_error_free(e
);
1055 if (IN_SET(type
, JOB_STOP
, JOB_RESTART
)) {
1056 _cleanup_set_free_ Set
*propagated_restart
= NULL
;
1057 /* We propagate STOP as STOP, but RESTART only as TRY_RESTART, in order not to start
1058 * dependencies that are not around. */
1060 if (type
== JOB_RESTART
)
1061 UNIT_FOREACH_DEPENDENCY(dep
, ret
->unit
, UNIT_ATOM_PROPAGATE_RESTART
) {
1064 r
= set_ensure_put(&propagated_restart
, NULL
, dep
);
1068 nt
= job_type_collapse(JOB_TRY_RESTART
, dep
);
1072 r
= transaction_add_job_and_dependencies(tr
, nt
, dep
, ret
, true, false, false, ignore_order
, e
);
1074 if (r
!= -EBADR
) /* job type not applicable */
1077 sd_bus_error_free(e
);
1081 /* The 'stop' part of a restart job is also propagated to
1082 * units with UNIT_ATOM_PROPAGATE_STOP */
1083 UNIT_FOREACH_DEPENDENCY(dep
, ret
->unit
, UNIT_ATOM_PROPAGATE_STOP
) {
1084 /* Units experienced restart propagation are skipped */
1085 if (set_contains(propagated_restart
, dep
))
1088 r
= transaction_add_job_and_dependencies(tr
, JOB_STOP
, dep
, ret
, true, false, false, ignore_order
, e
);
1090 if (r
!= -EBADR
) /* job type not applicable */
1093 sd_bus_error_free(e
);
1098 if (type
== JOB_RELOAD
)
1099 transaction_add_propagate_reload_jobs(tr
, ret
->unit
, ret
, ignore_order
, e
);
1101 /* JOB_VERIFY_ACTIVE requires no dependency handling */
1106 /* Recursive call failed to add required jobs so let's drop top level job as well. */
1107 log_unit_debug_errno(unit
, r
, "Cannot add dependency job to transaction, deleting job %s/%s again: %s",
1108 unit
->id
, job_type_to_string(type
), bus_error_message(e
, r
));
1109 transaction_delete_job(tr
, ret
, /* delete_dependencies= */ false);
1114 static bool shall_stop_on_isolate(Transaction
*tr
, Unit
*u
) {
1118 if (u
->ignore_on_isolate
)
1121 /* Is there already something listed for this? */
1122 if (hashmap_contains(tr
->jobs
, u
))
1128 int transaction_add_isolate_jobs(Transaction
*tr
, Manager
*m
) {
1136 HASHMAP_FOREACH_KEY(u
, k
, m
->units
) {
1139 /* Ignore aliases */
1143 /* No need to stop inactive units */
1144 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)) && !u
->job
)
1147 if (!shall_stop_on_isolate(tr
, u
))
1150 /* Keep units that are triggered by units we want to keep around. */
1152 UNIT_FOREACH_DEPENDENCY(o
, u
, UNIT_ATOM_TRIGGERED_BY
)
1153 if (!shall_stop_on_isolate(tr
, o
)) {
1160 r
= transaction_add_job_and_dependencies(tr
, JOB_STOP
, u
, tr
->anchor_job
, true, false, false, false, NULL
);
1162 log_unit_warning_errno(u
, r
, "Cannot add isolate job, ignoring: %m");
1168 int transaction_add_triggering_jobs(Transaction
*tr
, Unit
*u
) {
1175 UNIT_FOREACH_DEPENDENCY(trigger
, u
, UNIT_ATOM_TRIGGERED_BY
) {
1177 /* No need to stop inactive jobs */
1178 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(trigger
)) && !trigger
->job
)
1181 /* Is there already something listed for this? */
1182 if (hashmap_contains(tr
->jobs
, trigger
))
1185 r
= transaction_add_job_and_dependencies(tr
, JOB_STOP
, trigger
, tr
->anchor_job
, true, false, false, false, NULL
);
1187 log_unit_warning_errno(u
, r
, "Cannot add triggered by job, ignoring: %m");
1193 Transaction
*transaction_new(bool irreversible
) {
1196 tr
= new0(Transaction
, 1);
1200 tr
->jobs
= hashmap_new(NULL
);
1204 tr
->irreversible
= irreversible
;
1209 Transaction
*transaction_free(Transaction
*tr
) {
1213 assert(hashmap_isempty(tr
->jobs
));
1214 hashmap_free(tr
->jobs
);
1219 Transaction
*transaction_abort_and_free(Transaction
*tr
) {
1223 transaction_abort(tr
);
1225 return transaction_free(tr
);