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 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
) {
74 JobDependency
*l
, *last
;
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
) {
131 /* Returns true if this job is pulled in by a least one
132 * ConflictedBy dependency. */
134 LIST_FOREACH(object
, l
, j
->object_list
)
141 static int delete_one_unmergeable_job(Transaction
*tr
, Job
*j
) {
146 /* Tries to delete one item in the linked list
147 * j->transaction_next->transaction_next->... that conflicts
148 * with another one, in an attempt to make an inconsistent
149 * transaction work. */
151 /* We rely here on the fact that if a merged with b does not
152 * merge with c, either a or b merge with c neither */
153 LIST_FOREACH(transaction
, j
, j
)
154 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
157 /* Is this one mergeable? Then skip it */
158 if (job_type_is_mergeable(j
->type
, k
->type
))
161 /* Ok, we found two that conflict, let's see if we can
162 * drop one of them */
163 if (!j
->matters_to_anchor
&& !k
->matters_to_anchor
) {
165 /* Both jobs don't matter, so let's
166 * find the one that is smarter to
167 * remove. Let's think positive and
168 * rather remove stops then starts --
169 * except if something is being
170 * stopped because it is conflicted by
171 * another unit in which case we
172 * rather remove the start. */
174 log_unit_debug(j
->unit
,
175 "Looking at job %s/%s conflicted_by=%s",
176 j
->unit
->id
, job_type_to_string(j
->type
),
177 yes_no(j
->type
== JOB_STOP
&& job_is_conflicted_by(j
)));
178 log_unit_debug(k
->unit
,
179 "Looking at job %s/%s conflicted_by=%s",
180 k
->unit
->id
, job_type_to_string(k
->type
),
181 yes_no(k
->type
== JOB_STOP
&& job_is_conflicted_by(k
)));
183 if (j
->type
== JOB_STOP
) {
185 if (job_is_conflicted_by(j
))
190 } else if (k
->type
== JOB_STOP
) {
192 if (job_is_conflicted_by(k
))
199 } else if (!j
->matters_to_anchor
)
201 else if (!k
->matters_to_anchor
)
206 /* Ok, we can drop one, so let's do so. */
207 log_unit_debug(d
->unit
,
208 "Fixing conflicting jobs %s/%s,%s/%s by deleting job %s/%s",
209 j
->unit
->id
, job_type_to_string(j
->type
),
210 k
->unit
->id
, job_type_to_string(k
->type
),
211 d
->unit
->id
, job_type_to_string(d
->type
));
212 transaction_delete_job(tr
, d
, true);
219 static int transaction_merge_jobs(Transaction
*tr
, sd_bus_error
*e
) {
225 /* First step, check whether any of the jobs for one specific
226 * task conflict. If so, try to drop one of them. */
227 HASHMAP_FOREACH(j
, tr
->jobs
) {
232 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
233 if (job_type_merge_and_collapse(&t
, k
->type
, j
->unit
) >= 0)
236 /* OK, we could not merge all jobs for this
237 * action. Let's see if we can get rid of one
240 r
= delete_one_unmergeable_job(tr
, j
);
242 /* Ok, we managed to drop one, now
243 * let's ask our callers to call us
244 * again after garbage collecting */
247 /* We couldn't merge anything. Failure */
248 return sd_bus_error_setf(e
, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING
,
249 "Transaction contains conflicting jobs '%s' and '%s' for %s. "
250 "Probably contradicting requirement dependencies configured.",
251 job_type_to_string(t
),
252 job_type_to_string(k
->type
),
257 /* Second step, merge the jobs. */
258 HASHMAP_FOREACH(j
, tr
->jobs
) {
262 /* Merge all transaction jobs for j->unit */
263 LIST_FOREACH(transaction
, k
, j
->transaction_next
)
264 assert_se(job_type_merge_and_collapse(&t
, k
->type
, j
->unit
) == 0);
266 while ((k
= j
->transaction_next
)) {
267 if (tr
->anchor_job
== k
) {
268 transaction_merge_and_delete_job(tr
, k
, j
, t
);
271 transaction_merge_and_delete_job(tr
, j
, k
, t
);
274 assert(!j
->transaction_next
);
275 assert(!j
->transaction_prev
);
281 static void transaction_drop_redundant(Transaction
*tr
) {
284 /* Goes through the transaction and removes all jobs of the units whose jobs are all noops. If not
285 * all of a unit's jobs are redundant, they are kept. */
294 HASHMAP_FOREACH(j
, tr
->jobs
) {
298 LIST_FOREACH(transaction
, k
, j
)
299 if (tr
->anchor_job
== k
||
300 !job_type_is_redundant(k
->type
, unit_active_state(k
->unit
)) ||
301 (k
->unit
->job
&& job_type_is_conflicting(k
->type
, k
->unit
->job
->type
))) {
307 log_trace("Found redundant job %s/%s, dropping from transaction.",
308 j
->unit
->id
, job_type_to_string(j
->type
));
309 transaction_delete_job(tr
, j
, false);
317 _pure_
static bool unit_matters_to_anchor(Unit
*u
, Job
*j
) {
319 assert(!j
->transaction_prev
);
321 /* Checks whether at least one of the jobs for this unit
322 * matters to the anchor. */
324 LIST_FOREACH(transaction
, j
, j
)
325 if (j
->matters_to_anchor
)
331 static char* merge_unit_ids(const char* unit_log_field
, char **pairs
) {
332 char **unit_id
, **job_type
, *ans
= NULL
;
333 size_t alloc
= 0, size
= 0, next
;
335 STRV_FOREACH_PAIR(unit_id
, job_type
, pairs
) {
336 next
= strlen(unit_log_field
) + strlen(*unit_id
);
337 if (!GREEDY_REALLOC(ans
, alloc
, size
+ next
+ 1))
340 sprintf(ans
+ size
, "%s%s", unit_log_field
, *unit_id
);
342 ans
[size
+ next
] = '\n';
349 static int transaction_verify_order_one(Transaction
*tr
, Job
*j
, Job
*from
, unsigned generation
, sd_bus_error
*e
) {
353 static const UnitDependency directions
[] = {
361 assert(!j
->transaction_prev
);
363 /* Does a recursive sweep through the ordering graph, looking
364 * for a cycle. If we find a cycle we try to break it. */
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
;
370 char **unit_id
, **job_type
;
372 /* If the marker is NULL we have been here already and
373 * decided the job was loop-free from here. Hence
374 * shortcut things and return right-away. */
378 /* So, the marker is not NULL and we already have been here. We have
379 * a cycle. Let's try to break it. We go backwards in our path and
380 * try to find a suitable job to remove. We use the marker to find
381 * our way back, since smart how we are we stored our way back in
384 for (k
= from
; k
; k
= ((k
->generation
== generation
&& k
->marker
!= k
) ? k
->marker
: NULL
)) {
386 /* For logging below */
387 if (strv_push_pair(&array
, k
->unit
->id
, (char*) job_type_to_string(k
->type
)) < 0)
390 if (!delete && hashmap_get(tr
->jobs
, k
->unit
) && !unit_matters_to_anchor(k
->unit
, k
))
391 /* Ok, we can drop this one, so let's do so. */
394 /* Check if this in fact was the beginning of the cycle */
399 unit_ids
= merge_unit_ids(j
->manager
->unit_log_field
, array
); /* ignore error */
401 STRV_FOREACH_PAIR(unit_id
, job_type
, array
)
402 /* logging for j not k here to provide a consistent narrative */
403 log_struct(LOG_WARNING
,
404 "MESSAGE=%s: Found %s on %s/%s",
406 unit_id
== array
? "ordering cycle" : "dependency",
412 /* logging for j not k here to provide a consistent narrative */
414 "MESSAGE=%s: Job %s/%s deleted to break ordering cycle starting with %s/%s",
415 j
->unit
->id
, delete->unit
->id
, job_type_to_string(delete->type
),
416 j
->unit
->id
, job_type_to_string(j
->type
),
419 if (log_get_show_color())
420 status
= ANSI_HIGHLIGHT_RED
" SKIP " ANSI_NORMAL
;
424 unit_status_printf(delete->unit
,
427 "Ordering cycle found, skipping %s");
428 transaction_delete_unit(tr
, delete->unit
);
433 "MESSAGE=%s: Unable to break cycle starting with %s/%s",
434 j
->unit
->id
, j
->unit
->id
, job_type_to_string(j
->type
),
437 return sd_bus_error_setf(e
, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC
,
438 "Transaction order is cyclic. See system logs for details.");
441 /* Make the marker point to where we come from, so that we can
442 * find our way backwards if we want to break a cycle. We use
443 * a special marker for the beginning: we point to
445 j
->marker
= from
? from
: j
;
446 j
->generation
= generation
;
448 /* Actual ordering of jobs depends on the unit ordering dependency and job types. We need to traverse
449 * the graph over 'before' edges in the actual job execution order. We traverse over both unit
450 * ordering dependencies and we test with job_compare() whether it is the 'before' edge in the job
451 * execution ordering. */
452 for (d
= 0; d
< ELEMENTSOF(directions
); d
++) {
453 HASHMAP_FOREACH_KEY(v
, u
, j
->unit
->dependencies
[directions
[d
]]) {
456 /* Is there a job for this unit? */
457 o
= hashmap_get(tr
->jobs
, u
);
459 /* Ok, there is no job for this in the
460 * transaction, but maybe there is already one
467 /* Cut traversing if the job j is not really *before* o. */
468 if (job_compare(j
, o
, directions
[d
]) >= 0)
471 r
= transaction_verify_order_one(tr
, o
, j
, generation
, e
);
477 /* Ok, let's backtrack, and remember that this entry is not on
478 * our path anymore. */
484 static int transaction_verify_order(Transaction
*tr
, unsigned *generation
, sd_bus_error
*e
) {
492 /* Check if the ordering graph is cyclic. If it is, try to fix
493 * that up by dropping one of the jobs. */
497 HASHMAP_FOREACH(j
, tr
->jobs
) {
498 r
= transaction_verify_order_one(tr
, j
, NULL
, g
, e
);
506 static void transaction_collect_garbage(Transaction
*tr
) {
511 /* Drop jobs that are not required by any other job */
518 HASHMAP_FOREACH(j
, tr
->jobs
) {
519 if (tr
->anchor_job
== j
)
522 if (!j
->object_list
) {
523 log_trace("Garbage collecting job %s/%s", j
->unit
->id
, job_type_to_string(j
->type
));
524 transaction_delete_job(tr
, j
, true);
529 log_trace("Keeping job %s/%s because of %s/%s",
530 j
->unit
->id
, job_type_to_string(j
->type
),
531 j
->object_list
->subject
? j
->object_list
->subject
->unit
->id
: "root",
532 j
->object_list
->subject
? job_type_to_string(j
->object_list
->subject
->type
) : "root");
538 static int transaction_is_destructive(Transaction
*tr
, JobMode mode
, sd_bus_error
*e
) {
543 /* Checks whether applying this transaction means that
544 * existing jobs would be replaced */
546 HASHMAP_FOREACH(j
, tr
->jobs
) {
549 assert(!j
->transaction_prev
);
550 assert(!j
->transaction_next
);
552 if (j
->unit
->job
&& (mode
== JOB_FAIL
|| j
->unit
->job
->irreversible
) &&
553 job_type_is_conflicting(j
->unit
->job
->type
, j
->type
))
554 return sd_bus_error_setf(e
, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE
,
555 "Transaction for %s/%s is destructive (%s has '%s' job queued, but '%s' is included in transaction).",
556 tr
->anchor_job
->unit
->id
, job_type_to_string(tr
->anchor_job
->type
),
557 j
->unit
->id
, job_type_to_string(j
->unit
->job
->type
), job_type_to_string(j
->type
));
563 static void transaction_minimize_impact(Transaction
*tr
) {
568 /* Drops all unnecessary jobs that reverse already active jobs
569 * or that stop a running service. */
572 HASHMAP_FOREACH(j
, tr
->jobs
) {
573 LIST_FOREACH(transaction
, j
, j
) {
574 bool stops_running_service
, changes_existing_job
;
576 /* If it matters, we shouldn't drop it */
577 if (j
->matters_to_anchor
)
580 /* Would this stop a running service?
581 * Would this change an existing job?
582 * If so, let's drop this entry */
584 stops_running_service
=
585 j
->type
== JOB_STOP
&& UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j
->unit
));
587 changes_existing_job
=
589 job_type_is_conflicting(j
->type
, j
->unit
->job
->type
);
591 if (!stops_running_service
&& !changes_existing_job
)
594 if (stops_running_service
)
595 log_unit_debug(j
->unit
,
596 "%s/%s would stop a running service.",
597 j
->unit
->id
, job_type_to_string(j
->type
));
599 if (changes_existing_job
)
600 log_unit_debug(j
->unit
,
601 "%s/%s would change existing job.",
602 j
->unit
->id
, job_type_to_string(j
->type
));
604 /* Ok, let's get rid of this */
605 log_unit_debug(j
->unit
,
606 "Deleting %s/%s to minimize impact.",
607 j
->unit
->id
, job_type_to_string(j
->type
));
609 transaction_delete_job(tr
, j
, true);
615 static int transaction_apply(
619 Set
*affected_jobs
) {
624 /* Moves the transaction jobs to the set of active jobs */
626 if (IN_SET(mode
, JOB_ISOLATE
, JOB_FLUSH
)) {
628 /* When isolating first kill all installed jobs which
629 * aren't part of the new transaction */
630 HASHMAP_FOREACH(j
, m
->jobs
) {
631 assert(j
->installed
);
633 if (j
->unit
->ignore_on_isolate
)
636 if (hashmap_get(tr
->jobs
, j
->unit
))
639 /* Not invalidating recursively. Avoids triggering
640 * OnFailure= actions of dependent jobs. Also avoids
641 * invalidating our iterator. */
642 job_finish_and_invalidate(j
, JOB_CANCELED
, false, false);
646 HASHMAP_FOREACH(j
, tr
->jobs
) {
648 assert(!j
->transaction_prev
);
649 assert(!j
->transaction_next
);
651 r
= hashmap_ensure_put(&m
->jobs
, NULL
, UINT32_TO_PTR(j
->id
), j
);
656 while ((j
= hashmap_steal_first(tr
->jobs
))) {
659 /* Clean the job dependencies */
660 transaction_unlink_job(tr
, j
, false);
662 installed_job
= job_install(j
);
663 if (installed_job
!= j
) {
664 /* j has been merged into a previously installed job */
665 if (tr
->anchor_job
== j
)
666 tr
->anchor_job
= installed_job
;
667 hashmap_remove(m
->jobs
, UINT32_TO_PTR(j
->id
));
672 job_add_to_run_queue(j
);
673 job_add_to_dbus_queue(j
);
674 job_start_timer(j
, false);
675 job_shutdown_magic(j
);
677 /* When 'affected' is specified, let's track all in it all jobs that were touched because of
678 * this transaction. */
680 (void) set_put(affected_jobs
, j
);
687 HASHMAP_FOREACH(j
, tr
->jobs
)
688 hashmap_remove(m
->jobs
, UINT32_TO_PTR(j
->id
));
693 int transaction_activate(
702 unsigned generation
= 1;
706 /* This applies the changes recorded in tr->jobs to
707 * the actual list of jobs, if possible. */
709 /* Reset the generation counter of all installed jobs. The detection of cycles
710 * looks at installed jobs. If they had a non-zero generation from some previous
711 * walk of the graph, the algorithm would break. */
712 HASHMAP_FOREACH(j
, m
->jobs
)
715 /* First step: figure out which jobs matter */
716 transaction_find_jobs_that_matter_to_anchor(tr
->anchor_job
, generation
++);
718 /* Second step: Try not to stop any running services if
719 * we don't have to. Don't try to reverse running
720 * jobs if we don't have to. */
721 if (mode
== JOB_FAIL
)
722 transaction_minimize_impact(tr
);
724 /* Third step: Drop redundant jobs */
725 transaction_drop_redundant(tr
);
728 /* Fourth step: Let's remove unneeded jobs that might
730 if (mode
!= JOB_ISOLATE
)
731 transaction_collect_garbage(tr
);
733 /* Fifth step: verify order makes sense and correct
734 * cycles if necessary and possible */
735 r
= transaction_verify_order(tr
, &generation
, e
);
740 return log_warning_errno(r
, "Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error_message(e
, r
));
742 /* Let's see if the resulting transaction ordering
743 * graph is still cyclic... */
747 /* Sixth step: let's drop unmergeable entries if
748 * necessary and possible, merge entries we can
750 r
= transaction_merge_jobs(tr
, e
);
755 return log_warning_errno(r
, "Requested transaction contains unmergeable jobs: %s", bus_error_message(e
, r
));
757 /* Seventh step: an entry got dropped, let's garbage
758 * collect its dependencies. */
759 if (mode
!= JOB_ISOLATE
)
760 transaction_collect_garbage(tr
);
762 /* Let's see if the resulting transaction still has
763 * unmergeable entries ... */
766 /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
767 transaction_drop_redundant(tr
);
769 /* Ninth step: check whether we can actually apply this */
770 r
= transaction_is_destructive(tr
, mode
, e
);
772 return log_notice_errno(r
, "Requested transaction contradicts existing jobs: %s", bus_error_message(e
, r
));
774 /* Tenth step: apply changes */
775 r
= transaction_apply(tr
, m
, mode
, affected_jobs
);
777 return log_warning_errno(r
, "Failed to apply transaction: %m");
779 assert(hashmap_isempty(tr
->jobs
));
781 if (!hashmap_isempty(m
->jobs
)) {
782 /* Are there any jobs now? Then make sure we have the
783 * idle pipe around. We don't really care too much
784 * whether this works or not, as the idle pipe is a
785 * feature for cosmetics, not actually useful for
786 * anything beyond that. */
788 if (m
->idle_pipe
[0] < 0 && m
->idle_pipe
[1] < 0 &&
789 m
->idle_pipe
[2] < 0 && m
->idle_pipe
[3] < 0) {
790 (void) pipe2(m
->idle_pipe
, O_NONBLOCK
|O_CLOEXEC
);
791 (void) pipe2(m
->idle_pipe
+ 2, O_NONBLOCK
|O_CLOEXEC
);
798 static Job
* transaction_add_one_job(Transaction
*tr
, JobType type
, Unit
*unit
, bool *is_new
) {
804 /* Looks for an existing prospective job and returns that. If
805 * it doesn't exist it is created and added to the prospective
808 f
= hashmap_get(tr
->jobs
, unit
);
810 LIST_FOREACH(transaction
, j
, f
) {
811 assert(j
->unit
== unit
);
813 if (j
->type
== type
) {
820 j
= job_new(unit
, type
);
826 j
->matters_to_anchor
= false;
827 j
->irreversible
= tr
->irreversible
;
829 LIST_PREPEND(transaction
, f
, j
);
831 if (hashmap_replace(tr
->jobs
, unit
, f
) < 0) {
832 LIST_REMOVE(transaction
, f
, j
);
840 log_trace("Added job %s/%s to transaction.", unit
->id
, job_type_to_string(type
));
845 static void transaction_unlink_job(Transaction
*tr
, Job
*j
, bool delete_dependencies
) {
849 if (j
->transaction_prev
)
850 j
->transaction_prev
->transaction_next
= j
->transaction_next
;
851 else if (j
->transaction_next
)
852 hashmap_replace(tr
->jobs
, j
->unit
, j
->transaction_next
);
854 hashmap_remove_value(tr
->jobs
, j
->unit
, j
);
856 if (j
->transaction_next
)
857 j
->transaction_next
->transaction_prev
= j
->transaction_prev
;
859 j
->transaction_prev
= j
->transaction_next
= NULL
;
861 while (j
->subject_list
)
862 job_dependency_free(j
->subject_list
);
864 while (j
->object_list
) {
865 Job
*other
= j
->object_list
->matters
? j
->object_list
->subject
: NULL
;
867 job_dependency_free(j
->object_list
);
869 if (other
&& delete_dependencies
) {
870 log_unit_debug(other
->unit
,
871 "Deleting job %s/%s as dependency of job %s/%s",
872 other
->unit
->id
, job_type_to_string(other
->type
),
873 j
->unit
->id
, job_type_to_string(j
->type
));
874 transaction_delete_job(tr
, other
, delete_dependencies
);
879 void transaction_add_propagate_reload_jobs(Transaction
*tr
, Unit
*unit
, Job
*by
, bool ignore_order
, sd_bus_error
*e
) {
888 HASHMAP_FOREACH_KEY(v
, dep
, unit
->dependencies
[UNIT_PROPAGATES_RELOAD_TO
]) {
889 nt
= job_type_collapse(JOB_TRY_RELOAD
, dep
);
893 r
= transaction_add_job_and_dependencies(tr
, nt
, dep
, by
, false, false, false, ignore_order
, e
);
895 log_unit_warning(dep
,
896 "Cannot add dependency reload job, ignoring: %s",
897 bus_error_message(e
, r
));
898 sd_bus_error_free(e
);
903 int transaction_add_job_and_dependencies(
910 bool ignore_requirements
,
921 assert(type
< _JOB_TYPE_MAX
);
922 assert(type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
925 /* Before adding jobs for this unit, let's ensure that its state has been loaded
926 * This matters when jobs are spawned as part of coldplugging itself (see e. g. path_coldplug()).
927 * This way, we "recursively" coldplug units, ensuring that we do not look at state of
928 * not-yet-coldplugged units. */
929 if (MANAGER_IS_RELOADING(unit
->manager
))
933 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
));
935 /* Safety check that the unit is a valid state, i.e. not in UNIT_STUB or UNIT_MERGED which should only be set
937 if (!UNIT_IS_LOAD_COMPLETE(unit
->load_state
))
938 return sd_bus_error_setf(e
, BUS_ERROR_LOAD_FAILED
, "Unit %s is not loaded properly.", unit
->id
);
940 if (type
!= JOB_STOP
) {
941 r
= bus_unit_validate_load_state(unit
, e
);
942 /* The time-based cache allows to start new units without daemon-reload,
943 * but if they are already referenced (because of dependencies or ordering)
944 * then we have to force a load of the fragment. As an optimization, check
945 * first if anything in the usual paths was modified since the last time
946 * the cache was loaded. Also check if the last time an attempt to load the
947 * unit was made was before the most recent cache refresh, so that we know
948 * we need to try again — even if the cache is current, it might have been
949 * updated in a different context before we had a chance to retry loading
950 * this particular unit.
952 * Given building up the transaction is a synchronous operation, attempt
953 * to load the unit immediately. */
954 if (r
< 0 && manager_unit_cache_should_retry_load(unit
)) {
955 sd_bus_error_free(e
);
956 unit
->load_state
= UNIT_STUB
;
958 if (r
< 0 || unit
->load_state
== UNIT_STUB
)
959 unit
->load_state
= UNIT_NOT_FOUND
;
960 r
= bus_unit_validate_load_state(unit
, e
);
966 if (!unit_job_is_applicable(unit
, type
))
967 return sd_bus_error_setf(e
, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE
,
968 "Job type %s is not applicable for unit %s.",
969 job_type_to_string(type
), unit
->id
);
971 /* First add the job. */
972 ret
= transaction_add_one_job(tr
, type
, unit
, &is_new
);
976 ret
->ignore_order
= ret
->ignore_order
|| ignore_order
;
978 /* Then, add a link to the job. */
980 if (!job_dependency_new(by
, ret
, matters
, conflicts
))
983 /* If the job has no parent job, it is the anchor job. */
984 assert(!tr
->anchor_job
);
985 tr
->anchor_job
= ret
;
988 if (is_new
&& !ignore_requirements
&& type
!= JOB_NOP
) {
991 /* If we are following some other unit, make sure we
992 * add all dependencies of everybody following. */
993 if (unit_following_set(ret
->unit
, &following
) > 0) {
994 SET_FOREACH(dep
, following
) {
995 r
= transaction_add_job_and_dependencies(tr
, type
, dep
, ret
, false, false, false, ignore_order
, e
);
997 log_unit_full_errno(dep
, r
== -ERFKILL
? LOG_INFO
: LOG_WARNING
, r
,
998 "Cannot add dependency job, ignoring: %s",
999 bus_error_message(e
, r
));
1000 sd_bus_error_free(e
);
1004 set_free(following
);
1007 /* Finally, recursively add in all dependencies. */
1008 if (IN_SET(type
, JOB_START
, JOB_RESTART
)) {
1009 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_REQUIRES
]) {
1010 r
= transaction_add_job_and_dependencies(tr
, JOB_START
, dep
, ret
, true, false, false, ignore_order
, e
);
1012 if (r
!= -EBADR
) /* job type not applicable */
1015 sd_bus_error_free(e
);
1019 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_BINDS_TO
]) {
1020 r
= transaction_add_job_and_dependencies(tr
, JOB_START
, dep
, ret
, true, false, false, ignore_order
, e
);
1022 if (r
!= -EBADR
) /* job type not applicable */
1025 sd_bus_error_free(e
);
1029 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_WANTS
]) {
1030 r
= transaction_add_job_and_dependencies(tr
, JOB_START
, dep
, ret
, false, false, false, ignore_order
, e
);
1032 /* unit masked, job type not applicable and unit not found are not considered as errors. */
1033 log_unit_full_errno(dep
,
1034 IN_SET(r
, -ERFKILL
, -EBADR
, -ENOENT
) ? LOG_DEBUG
: LOG_WARNING
,
1035 r
, "Cannot add dependency job, ignoring: %s",
1036 bus_error_message(e
, r
));
1037 sd_bus_error_free(e
);
1041 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_REQUISITE
]) {
1042 r
= transaction_add_job_and_dependencies(tr
, JOB_VERIFY_ACTIVE
, dep
, ret
, true, false, false, ignore_order
, e
);
1044 if (r
!= -EBADR
) /* job type not applicable */
1047 sd_bus_error_free(e
);
1051 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_CONFLICTS
]) {
1052 r
= transaction_add_job_and_dependencies(tr
, JOB_STOP
, dep
, ret
, true, true, false, ignore_order
, e
);
1054 if (r
!= -EBADR
) /* job type not applicable */
1057 sd_bus_error_free(e
);
1061 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_CONFLICTED_BY
]) {
1062 r
= transaction_add_job_and_dependencies(tr
, JOB_STOP
, dep
, ret
, false, false, false, ignore_order
, e
);
1064 log_unit_warning(dep
,
1065 "Cannot add dependency job, ignoring: %s",
1066 bus_error_message(e
, r
));
1067 sd_bus_error_free(e
);
1073 if (IN_SET(type
, JOB_STOP
, JOB_RESTART
)) {
1074 static const UnitDependency propagate_deps
[] = {
1084 /* We propagate STOP as STOP, but RESTART only
1085 * as TRY_RESTART, in order not to start
1086 * dependencies that are not around. */
1087 ptype
= type
== JOB_RESTART
? JOB_TRY_RESTART
: type
;
1089 for (j
= 0; j
< ELEMENTSOF(propagate_deps
); j
++)
1090 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[propagate_deps
[j
]]) {
1093 nt
= job_type_collapse(ptype
, dep
);
1097 r
= transaction_add_job_and_dependencies(tr
, nt
, dep
, ret
, true, false, false, ignore_order
, e
);
1099 if (r
!= -EBADR
) /* job type not applicable */
1102 sd_bus_error_free(e
);
1107 if (type
== JOB_RELOAD
)
1108 transaction_add_propagate_reload_jobs(tr
, ret
->unit
, ret
, ignore_order
, e
);
1110 /* JOB_VERIFY_ACTIVE requires no dependency handling */
1119 int transaction_add_isolate_jobs(Transaction
*tr
, Manager
*m
) {
1127 HASHMAP_FOREACH_KEY(u
, k
, m
->units
) {
1129 /* ignore aliases */
1133 if (u
->ignore_on_isolate
)
1136 /* No need to stop inactive jobs */
1137 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)) && !u
->job
)
1140 /* Is there already something listed for this? */
1141 if (hashmap_get(tr
->jobs
, u
))
1144 r
= transaction_add_job_and_dependencies(tr
, JOB_STOP
, u
, tr
->anchor_job
, true, false, false, false, NULL
);
1146 log_unit_warning_errno(u
, r
, "Cannot add isolate job, ignoring: %m");
1152 int transaction_add_triggering_jobs(Transaction
*tr
, Unit
*u
) {
1160 HASHMAP_FOREACH_KEY(v
, trigger
, u
->dependencies
[UNIT_TRIGGERED_BY
]) {
1161 /* No need to stop inactive jobs */
1162 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(trigger
)) && !trigger
->job
)
1165 /* Is there already something listed for this? */
1166 if (hashmap_get(tr
->jobs
, trigger
))
1169 r
= transaction_add_job_and_dependencies(tr
, JOB_STOP
, trigger
, tr
->anchor_job
, true, false, false, false, NULL
);
1171 log_unit_warning_errno(u
, r
, "Cannot add triggered by job, ignoring: %m");
1177 Transaction
*transaction_new(bool irreversible
) {
1180 tr
= new0(Transaction
, 1);
1184 tr
->jobs
= hashmap_new(NULL
);
1188 tr
->irreversible
= irreversible
;
1193 void transaction_free(Transaction
*tr
) {
1194 assert(hashmap_isempty(tr
->jobs
));
1195 hashmap_free(tr
->jobs
);