1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
11 #include "alloc-util.h"
12 #include "bus-common-errors.h"
13 #include "bus-error.h"
14 #include "terminal-util.h"
15 #include "transaction.h"
16 #include "dbus-unit.h"
18 static void transaction_unlink_job(Transaction
*tr
, Job
*j
, bool delete_dependencies
);
20 static void transaction_delete_job(Transaction
*tr
, Job
*j
, bool delete_dependencies
) {
24 /* Deletes one job from the transaction */
26 transaction_unlink_job(tr
, j
, delete_dependencies
);
31 static void transaction_delete_unit(Transaction
*tr
, Unit
*u
) {
34 /* Deletes all jobs associated with a certain unit from the
37 while ((j
= hashmap_get(tr
->jobs
, u
)))
38 transaction_delete_job(tr
, j
, true);
41 void transaction_abort(Transaction
*tr
) {
46 while ((j
= hashmap_first(tr
->jobs
)))
47 transaction_delete_job(tr
, j
, false);
49 assert(hashmap_isempty(tr
->jobs
));
52 static void transaction_find_jobs_that_matter_to_anchor(Job
*j
, unsigned generation
) {
55 /* A recursive sweep through the graph that marks all units
56 * that matter to the anchor job, i.e. are directly or
57 * indirectly a dependency of the anchor job via paths that
58 * are fully marked as mattering. */
60 j
->matters_to_anchor
= true;
61 j
->generation
= generation
;
63 LIST_FOREACH(subject
, l
, j
->subject_list
) {
65 /* This link does not matter */
69 /* This unit has already been marked */
70 if (l
->object
->generation
== generation
)
73 transaction_find_jobs_that_matter_to_anchor(l
->object
, generation
);
77 static void transaction_merge_and_delete_job(Transaction
*tr
, Job
*j
, Job
*other
, JobType t
) {
78 JobDependency
*l
, *last
;
82 assert(j
->unit
== other
->unit
);
83 assert(!j
->installed
);
85 /* Merges 'other' into 'j' and then deletes 'other'. */
88 j
->state
= JOB_WAITING
;
89 j
->irreversible
= j
->irreversible
|| other
->irreversible
;
90 j
->matters_to_anchor
= j
->matters_to_anchor
|| other
->matters_to_anchor
;
92 /* Patch us in as new owner of the JobDependency objects */
94 LIST_FOREACH(subject
, l
, other
->subject_list
) {
95 assert(l
->subject
== other
);
100 /* Merge both lists */
102 last
->subject_next
= j
->subject_list
;
104 j
->subject_list
->subject_prev
= last
;
105 j
->subject_list
= other
->subject_list
;
108 /* Patch us in as new owner of the JobDependency objects */
110 LIST_FOREACH(object
, l
, other
->object_list
) {
111 assert(l
->object
== other
);
116 /* Merge both lists */
118 last
->object_next
= j
->object_list
;
120 j
->object_list
->object_prev
= last
;
121 j
->object_list
= other
->object_list
;
124 /* Kill the other job */
125 other
->subject_list
= NULL
;
126 other
->object_list
= NULL
;
127 transaction_delete_job(tr
, other
, true);
130 _pure_
static bool job_is_conflicted_by(Job
*j
) {
135 /* Returns true if this job is pulled in by a least one
136 * ConflictedBy dependency. */
138 LIST_FOREACH(object
, l
, j
->object_list
)
145 static int delete_one_unmergeable_job(Transaction
*tr
, Job
*j
) {
150 /* Tries to delete one item in the linked list
151 * j->transaction_next->transaction_next->... that conflicts
152 * with another one, in an attempt to make an inconsistent
153 * transaction work. */
155 /* We rely here on the fact that if a merged with b does not
156 * merge with c, either a or b merge with c neither */
157 LIST_FOREACH(transaction
, j
, j
)
158 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
161 /* Is this one mergeable? Then skip it */
162 if (job_type_is_mergeable(j
->type
, k
->type
))
165 /* Ok, we found two that conflict, let's see if we can
166 * drop one of them */
167 if (!j
->matters_to_anchor
&& !k
->matters_to_anchor
) {
169 /* Both jobs don't matter, so let's
170 * find the one that is smarter to
171 * remove. Let's think positive and
172 * rather remove stops then starts --
173 * except if something is being
174 * stopped because it is conflicted by
175 * another unit in which case we
176 * rather remove the start. */
178 log_unit_debug(j
->unit
,
179 "Looking at job %s/%s conflicted_by=%s",
180 j
->unit
->id
, job_type_to_string(j
->type
),
181 yes_no(j
->type
== JOB_STOP
&& job_is_conflicted_by(j
)));
182 log_unit_debug(k
->unit
,
183 "Looking at job %s/%s conflicted_by=%s",
184 k
->unit
->id
, job_type_to_string(k
->type
),
185 yes_no(k
->type
== JOB_STOP
&& job_is_conflicted_by(k
)));
187 if (j
->type
== JOB_STOP
) {
189 if (job_is_conflicted_by(j
))
194 } else if (k
->type
== JOB_STOP
) {
196 if (job_is_conflicted_by(k
))
203 } else if (!j
->matters_to_anchor
)
205 else if (!k
->matters_to_anchor
)
210 /* Ok, we can drop one, so let's do so. */
211 log_unit_debug(d
->unit
,
212 "Fixing conflicting jobs %s/%s,%s/%s by deleting job %s/%s",
213 j
->unit
->id
, job_type_to_string(j
->type
),
214 k
->unit
->id
, job_type_to_string(k
->type
),
215 d
->unit
->id
, job_type_to_string(d
->type
));
216 transaction_delete_job(tr
, d
, true);
223 static int transaction_merge_jobs(Transaction
*tr
, sd_bus_error
*e
) {
230 /* First step, check whether any of the jobs for one specific
231 * task conflict. If so, try to drop one of them. */
232 HASHMAP_FOREACH(j
, tr
->jobs
, i
) {
237 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
238 if (job_type_merge_and_collapse(&t
, k
->type
, j
->unit
) >= 0)
241 /* OK, we could not merge all jobs for this
242 * action. Let's see if we can get rid of one
245 r
= delete_one_unmergeable_job(tr
, j
);
247 /* Ok, we managed to drop one, now
248 * let's ask our callers to call us
249 * again after garbage collecting */
252 /* We couldn't merge anything. Failure */
253 return sd_bus_error_setf(e
, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING
,
254 "Transaction contains conflicting jobs '%s' and '%s' for %s. "
255 "Probably contradicting requirement dependencies configured.",
256 job_type_to_string(t
),
257 job_type_to_string(k
->type
),
262 /* Second step, merge the jobs. */
263 HASHMAP_FOREACH(j
, tr
->jobs
, i
) {
267 /* Merge all transaction jobs for j->unit */
268 LIST_FOREACH(transaction
, k
, j
->transaction_next
)
269 assert_se(job_type_merge_and_collapse(&t
, k
->type
, j
->unit
) == 0);
271 while ((k
= j
->transaction_next
)) {
272 if (tr
->anchor_job
== k
) {
273 transaction_merge_and_delete_job(tr
, k
, j
, t
);
276 transaction_merge_and_delete_job(tr
, j
, k
, t
);
279 assert(!j
->transaction_next
);
280 assert(!j
->transaction_prev
);
286 static void transaction_drop_redundant(Transaction
*tr
) {
290 /* Goes through the transaction and removes all jobs of the units
291 * whose jobs are all noops. If not all of a unit's jobs are
292 * redundant, they are kept. */
297 HASHMAP_FOREACH(j
, tr
->jobs
, i
) {
300 LIST_FOREACH(transaction
, k
, j
) {
302 if (tr
->anchor_job
== k
||
303 !job_type_is_redundant(k
->type
, unit_active_state(k
->unit
)) ||
304 (k
->unit
->job
&& job_type_is_conflicting(k
->type
, k
->unit
->job
->type
)))
308 /* log_debug("Found redundant job %s/%s, dropping.", j->unit->id, job_type_to_string(j->type)); */
309 transaction_delete_job(tr
, j
, false);
315 _pure_
static bool unit_matters_to_anchor(Unit
*u
, Job
*j
) {
317 assert(!j
->transaction_prev
);
319 /* Checks whether at least one of the jobs for this unit
320 * matters to the anchor. */
322 LIST_FOREACH(transaction
, j
, j
)
323 if (j
->matters_to_anchor
)
329 static char* merge_unit_ids(const char* unit_log_field
, char **pairs
) {
330 char **unit_id
, **job_type
, *ans
= NULL
;
331 size_t alloc
= 0, size
= 0, next
;
333 STRV_FOREACH_PAIR(unit_id
, job_type
, pairs
) {
334 next
= strlen(unit_log_field
) + strlen(*unit_id
);
335 if (!GREEDY_REALLOC(ans
, alloc
, size
+ next
+ 1)) {
339 sprintf(ans
+ size
, "%s%s", unit_log_field
, *unit_id
);
341 ans
[size
+ next
] = '\n';
348 static int transaction_verify_order_one(Transaction
*tr
, Job
*j
, Job
*from
, unsigned generation
, sd_bus_error
*e
) {
356 assert(!j
->transaction_prev
);
358 /* Does a recursive sweep through the ordering graph, looking
359 * for a cycle. If we find a cycle we try to break it. */
361 /* Have we seen this before? */
362 if (j
->generation
== generation
) {
363 Job
*k
, *delete = NULL
;
364 _cleanup_free_
char **array
= NULL
, *unit_ids
= NULL
;
365 char **unit_id
, **job_type
;
367 /* If the marker is NULL we have been here already and
368 * decided the job was loop-free from here. Hence
369 * shortcut things and return right-away. */
373 /* So, the marker is not NULL and we already have been here. We have
374 * a cycle. Let's try to break it. We go backwards in our path and
375 * try to find a suitable job to remove. We use the marker to find
376 * our way back, since smart how we are we stored our way back in
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_get(tr
->jobs
, k
->unit
) && !unit_matters_to_anchor(k
->unit
, 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 "MESSAGE=%s: Found %s on %s/%s",
401 unit_id
== array
? "ordering cycle" : "dependency",
407 /* logging for j not k here to provide a consistent narrative */
409 "MESSAGE=%s: Job %s/%s deleted to break ordering cycle starting with %s/%s",
410 j
->unit
->id
, delete->unit
->id
, job_type_to_string(delete->type
),
411 j
->unit
->id
, job_type_to_string(j
->type
),
414 if (log_get_show_color())
415 status
= ANSI_HIGHLIGHT_RED
" SKIP " ANSI_NORMAL
;
419 unit_status_printf(delete->unit
, status
,
420 "Ordering cycle found, skipping %s");
421 transaction_delete_unit(tr
, delete->unit
);
426 "MESSAGE=%s: Unable to break cycle starting with %s/%s",
427 j
->unit
->id
, j
->unit
->id
, job_type_to_string(j
->type
),
430 return sd_bus_error_setf(e
, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC
,
431 "Transaction order is cyclic. See system logs for details.");
434 /* Make the marker point to where we come from, so that we can
435 * find our way backwards if we want to break a cycle. We use
436 * a special marker for the beginning: we point to
438 j
->marker
= from
? from
: j
;
439 j
->generation
= generation
;
441 /* We assume that the dependencies are bidirectional, and
442 * hence can ignore UNIT_AFTER */
443 HASHMAP_FOREACH_KEY(v
, u
, j
->unit
->dependencies
[UNIT_BEFORE
], i
) {
446 /* Is there a job for this unit? */
447 o
= hashmap_get(tr
->jobs
, u
);
449 /* Ok, there is no job for this in the
450 * transaction, but maybe there is already one
457 r
= transaction_verify_order_one(tr
, o
, j
, generation
, e
);
462 /* Ok, let's backtrack, and remember that this entry is not on
463 * our path anymore. */
469 static int transaction_verify_order(Transaction
*tr
, unsigned *generation
, sd_bus_error
*e
) {
478 /* Check if the ordering graph is cyclic. If it is, try to fix
479 * that up by dropping one of the jobs. */
483 HASHMAP_FOREACH(j
, tr
->jobs
, i
) {
484 r
= transaction_verify_order_one(tr
, j
, NULL
, g
, e
);
492 static void transaction_collect_garbage(Transaction
*tr
) {
498 /* Drop jobs that are not required by any other job */
501 HASHMAP_FOREACH(j
, tr
->jobs
, i
) {
502 if (tr
->anchor_job
== j
|| j
->object_list
) {
503 /* log_debug("Keeping job %s/%s because of %s/%s", */
504 /* j->unit->id, job_type_to_string(j->type), */
505 /* j->object_list->subject ? j->object_list->subject->unit->id : "root", */
506 /* j->object_list->subject ? job_type_to_string(j->object_list->subject->type) : "root"); */
510 /* log_debug("Garbage collecting job %s/%s", j->unit->id, job_type_to_string(j->type)); */
511 transaction_delete_job(tr
, j
, true);
516 static int transaction_is_destructive(Transaction
*tr
, JobMode mode
, sd_bus_error
*e
) {
522 /* Checks whether applying this transaction means that
523 * existing jobs would be replaced */
525 HASHMAP_FOREACH(j
, tr
->jobs
, i
) {
528 assert(!j
->transaction_prev
);
529 assert(!j
->transaction_next
);
531 if (j
->unit
->job
&& (mode
== JOB_FAIL
|| j
->unit
->job
->irreversible
) &&
532 job_type_is_conflicting(j
->unit
->job
->type
, j
->type
))
533 return sd_bus_error_setf(e
, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE
,
534 "Transaction is destructive.");
540 static void transaction_minimize_impact(Transaction
*tr
) {
546 /* Drops all unnecessary jobs that reverse already active jobs
547 * or that stop a running service. */
550 HASHMAP_FOREACH(j
, tr
->jobs
, i
) {
551 LIST_FOREACH(transaction
, j
, j
) {
552 bool stops_running_service
, changes_existing_job
;
554 /* If it matters, we shouldn't drop it */
555 if (j
->matters_to_anchor
)
558 /* Would this stop a running service?
559 * Would this change an existing job?
560 * If so, let's drop this entry */
562 stops_running_service
=
563 j
->type
== JOB_STOP
&& UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j
->unit
));
565 changes_existing_job
=
567 job_type_is_conflicting(j
->type
, j
->unit
->job
->type
);
569 if (!stops_running_service
&& !changes_existing_job
)
572 if (stops_running_service
)
573 log_unit_debug(j
->unit
,
574 "%s/%s would stop a running service.",
575 j
->unit
->id
, job_type_to_string(j
->type
));
577 if (changes_existing_job
)
578 log_unit_debug(j
->unit
,
579 "%s/%s would change existing job.",
580 j
->unit
->id
, job_type_to_string(j
->type
));
582 /* Ok, let's get rid of this */
583 log_unit_debug(j
->unit
,
584 "Deleting %s/%s to minimize impact.",
585 j
->unit
->id
, job_type_to_string(j
->type
));
587 transaction_delete_job(tr
, j
, true);
593 static int transaction_apply(Transaction
*tr
, Manager
*m
, JobMode mode
) {
598 /* Moves the transaction jobs to the set of active jobs */
600 if (IN_SET(mode
, JOB_ISOLATE
, JOB_FLUSH
)) {
602 /* When isolating first kill all installed jobs which
603 * aren't part of the new transaction */
604 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
605 assert(j
->installed
);
607 if (j
->unit
->ignore_on_isolate
)
610 if (hashmap_get(tr
->jobs
, j
->unit
))
613 /* Not invalidating recursively. Avoids triggering
614 * OnFailure= actions of dependent jobs. Also avoids
615 * invalidating our iterator. */
616 job_finish_and_invalidate(j
, JOB_CANCELED
, false, false);
620 HASHMAP_FOREACH(j
, tr
->jobs
, i
) {
622 assert(!j
->transaction_prev
);
623 assert(!j
->transaction_next
);
625 r
= hashmap_put(m
->jobs
, UINT32_TO_PTR(j
->id
), j
);
630 while ((j
= hashmap_steal_first(tr
->jobs
))) {
633 /* Clean the job dependencies */
634 transaction_unlink_job(tr
, j
, false);
636 installed_job
= job_install(j
);
637 if (installed_job
!= j
) {
638 /* j has been merged into a previously installed job */
639 if (tr
->anchor_job
== j
)
640 tr
->anchor_job
= installed_job
;
641 hashmap_remove(m
->jobs
, UINT32_TO_PTR(j
->id
));
646 job_add_to_run_queue(j
);
647 job_add_to_dbus_queue(j
);
648 job_start_timer(j
, false);
649 job_shutdown_magic(j
);
656 HASHMAP_FOREACH(j
, tr
->jobs
, i
)
657 hashmap_remove(m
->jobs
, UINT32_TO_PTR(j
->id
));
662 int transaction_activate(Transaction
*tr
, Manager
*m
, JobMode mode
, sd_bus_error
*e
) {
666 unsigned generation
= 1;
670 /* This applies the changes recorded in tr->jobs to
671 * the actual list of jobs, if possible. */
673 /* Reset the generation counter of all installed jobs. The detection of cycles
674 * looks at installed jobs. If they had a non-zero generation from some previous
675 * walk of the graph, the algorithm would break. */
676 HASHMAP_FOREACH(j
, m
->jobs
, i
)
679 /* First step: figure out which jobs matter */
680 transaction_find_jobs_that_matter_to_anchor(tr
->anchor_job
, generation
++);
682 /* Second step: Try not to stop any running services if
683 * we don't have to. Don't try to reverse running
684 * jobs if we don't have to. */
685 if (mode
== JOB_FAIL
)
686 transaction_minimize_impact(tr
);
688 /* Third step: Drop redundant jobs */
689 transaction_drop_redundant(tr
);
692 /* Fourth step: Let's remove unneeded jobs that might
694 if (mode
!= JOB_ISOLATE
)
695 transaction_collect_garbage(tr
);
697 /* Fifth step: verify order makes sense and correct
698 * cycles if necessary and possible */
699 r
= transaction_verify_order(tr
, &generation
, e
);
704 log_warning("Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error_message(e
, r
));
708 /* Let's see if the resulting transaction ordering
709 * graph is still cyclic... */
713 /* Sixth step: let's drop unmergeable entries if
714 * necessary and possible, merge entries we can
716 r
= transaction_merge_jobs(tr
, e
);
721 log_warning("Requested transaction contains unmergeable jobs: %s", bus_error_message(e
, r
));
725 /* Seventh step: an entry got dropped, let's garbage
726 * collect its dependencies. */
727 if (mode
!= JOB_ISOLATE
)
728 transaction_collect_garbage(tr
);
730 /* Let's see if the resulting transaction still has
731 * unmergeable entries ... */
734 /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
735 transaction_drop_redundant(tr
);
737 /* Ninth step: check whether we can actually apply this */
738 r
= transaction_is_destructive(tr
, mode
, e
);
740 log_notice("Requested transaction contradicts existing jobs: %s", bus_error_message(e
, r
));
744 /* Tenth step: apply changes */
745 r
= transaction_apply(tr
, m
, mode
);
747 return log_warning_errno(r
, "Failed to apply transaction: %m");
749 assert(hashmap_isempty(tr
->jobs
));
751 if (!hashmap_isempty(m
->jobs
)) {
752 /* Are there any jobs now? Then make sure we have the
753 * idle pipe around. We don't really care too much
754 * whether this works or not, as the idle pipe is a
755 * feature for cosmetics, not actually useful for
756 * anything beyond that. */
758 if (m
->idle_pipe
[0] < 0 && m
->idle_pipe
[1] < 0 &&
759 m
->idle_pipe
[2] < 0 && m
->idle_pipe
[3] < 0) {
760 (void) pipe2(m
->idle_pipe
, O_NONBLOCK
|O_CLOEXEC
);
761 (void) pipe2(m
->idle_pipe
+ 2, O_NONBLOCK
|O_CLOEXEC
);
768 static Job
* transaction_add_one_job(Transaction
*tr
, JobType type
, Unit
*unit
, bool *is_new
) {
774 /* Looks for an existing prospective job and returns that. If
775 * it doesn't exist it is created and added to the prospective
778 f
= hashmap_get(tr
->jobs
, unit
);
780 LIST_FOREACH(transaction
, j
, f
) {
781 assert(j
->unit
== unit
);
783 if (j
->type
== type
) {
790 j
= job_new(unit
, type
);
796 j
->matters_to_anchor
= false;
797 j
->irreversible
= tr
->irreversible
;
799 LIST_PREPEND(transaction
, f
, j
);
801 if (hashmap_replace(tr
->jobs
, unit
, f
) < 0) {
802 LIST_REMOVE(transaction
, f
, j
);
810 /* log_debug("Added job %s/%s to transaction.", unit->id, job_type_to_string(type)); */
815 static void transaction_unlink_job(Transaction
*tr
, Job
*j
, bool delete_dependencies
) {
819 if (j
->transaction_prev
)
820 j
->transaction_prev
->transaction_next
= j
->transaction_next
;
821 else if (j
->transaction_next
)
822 hashmap_replace(tr
->jobs
, j
->unit
, j
->transaction_next
);
824 hashmap_remove_value(tr
->jobs
, j
->unit
, j
);
826 if (j
->transaction_next
)
827 j
->transaction_next
->transaction_prev
= j
->transaction_prev
;
829 j
->transaction_prev
= j
->transaction_next
= NULL
;
831 while (j
->subject_list
)
832 job_dependency_free(j
->subject_list
);
834 while (j
->object_list
) {
835 Job
*other
= j
->object_list
->matters
? j
->object_list
->subject
: NULL
;
837 job_dependency_free(j
->object_list
);
839 if (other
&& delete_dependencies
) {
840 log_unit_debug(other
->unit
,
841 "Deleting job %s/%s as dependency of job %s/%s",
842 other
->unit
->id
, job_type_to_string(other
->type
),
843 j
->unit
->id
, job_type_to_string(j
->type
));
844 transaction_delete_job(tr
, other
, delete_dependencies
);
849 void transaction_add_propagate_reload_jobs(Transaction
*tr
, Unit
*unit
, Job
*by
, bool ignore_order
, sd_bus_error
*e
) {
859 HASHMAP_FOREACH_KEY(v
, dep
, unit
->dependencies
[UNIT_PROPAGATES_RELOAD_TO
], i
) {
860 nt
= job_type_collapse(JOB_TRY_RELOAD
, dep
);
864 r
= transaction_add_job_and_dependencies(tr
, nt
, dep
, by
, false, false, false, ignore_order
, e
);
866 log_unit_warning(dep
,
867 "Cannot add dependency reload job, ignoring: %s",
868 bus_error_message(e
, r
));
869 sd_bus_error_free(e
);
874 int transaction_add_job_and_dependencies(
881 bool ignore_requirements
,
893 assert(type
< _JOB_TYPE_MAX
);
894 assert(type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
897 /* Before adding jobs for this unit, let's ensure that its state has been loaded
898 * This matters when jobs are spawned as part of coldplugging itself (see e. g. path_coldplug()).
899 * This way, we "recursively" coldplug units, ensuring that we do not look at state of
900 * not-yet-coldplugged units. */
901 if (MANAGER_IS_RELOADING(unit
->manager
))
904 /* log_debug("Pulling in %s/%s from %s/%s", */
905 /* unit->id, job_type_to_string(type), */
906 /* by ? by->unit->id : "NA", */
907 /* by ? job_type_to_string(by->type) : "NA"); */
909 if (!IN_SET(unit
->load_state
, UNIT_LOADED
, UNIT_ERROR
, UNIT_NOT_FOUND
, UNIT_MASKED
))
910 return sd_bus_error_setf(e
, BUS_ERROR_LOAD_FAILED
, "Unit %s is not loaded properly.", unit
->id
);
912 if (type
!= JOB_STOP
) {
913 r
= bus_unit_check_load_state(unit
, e
);
918 if (!unit_job_is_applicable(unit
, type
))
919 return sd_bus_error_setf(e
, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE
,
920 "Job type %s is not applicable for unit %s.",
921 job_type_to_string(type
), unit
->id
);
924 /* First add the job. */
925 ret
= transaction_add_one_job(tr
, type
, unit
, &is_new
);
929 ret
->ignore_order
= ret
->ignore_order
|| ignore_order
;
931 /* Then, add a link to the job. */
933 if (!job_dependency_new(by
, ret
, matters
, conflicts
))
936 /* If the job has no parent job, it is the anchor job. */
937 assert(!tr
->anchor_job
);
938 tr
->anchor_job
= ret
;
941 if (is_new
&& !ignore_requirements
&& type
!= JOB_NOP
) {
944 /* If we are following some other unit, make sure we
945 * add all dependencies of everybody following. */
946 if (unit_following_set(ret
->unit
, &following
) > 0) {
947 SET_FOREACH(dep
, following
, i
) {
948 r
= transaction_add_job_and_dependencies(tr
, type
, dep
, ret
, false, false, false, ignore_order
, e
);
951 r
== -ERFKILL
? LOG_INFO
: LOG_WARNING
,
952 r
, "Cannot add dependency job, ignoring: %s",
953 bus_error_message(e
, r
));
954 sd_bus_error_free(e
);
961 /* Finally, recursively add in all dependencies. */
962 if (IN_SET(type
, JOB_START
, JOB_RESTART
)) {
963 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_REQUIRES
], i
) {
964 r
= transaction_add_job_and_dependencies(tr
, JOB_START
, dep
, ret
, true, false, false, ignore_order
, e
);
966 if (r
!= -EBADR
) /* job type not applicable */
969 sd_bus_error_free(e
);
973 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_BINDS_TO
], i
) {
974 r
= transaction_add_job_and_dependencies(tr
, JOB_START
, dep
, ret
, true, false, false, ignore_order
, e
);
976 if (r
!= -EBADR
) /* job type not applicable */
979 sd_bus_error_free(e
);
983 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_WANTS
], i
) {
984 r
= transaction_add_job_and_dependencies(tr
, JOB_START
, dep
, ret
, false, false, false, ignore_order
, e
);
986 /* unit masked, job type not applicable and unit not found are not considered as errors. */
988 IN_SET(r
, -ERFKILL
, -EBADR
, -ENOENT
) ? LOG_DEBUG
: LOG_WARNING
,
989 r
, "Cannot add dependency job, ignoring: %s",
990 bus_error_message(e
, r
));
991 sd_bus_error_free(e
);
995 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_REQUISITE
], i
) {
996 r
= transaction_add_job_and_dependencies(tr
, JOB_VERIFY_ACTIVE
, dep
, ret
, true, false, false, ignore_order
, e
);
998 if (r
!= -EBADR
) /* job type not applicable */
1001 sd_bus_error_free(e
);
1005 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_CONFLICTS
], i
) {
1006 r
= transaction_add_job_and_dependencies(tr
, JOB_STOP
, dep
, ret
, true, true, false, ignore_order
, e
);
1008 if (r
!= -EBADR
) /* job type not applicable */
1011 sd_bus_error_free(e
);
1015 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_CONFLICTED_BY
], i
) {
1016 r
= transaction_add_job_and_dependencies(tr
, JOB_STOP
, dep
, ret
, false, false, false, ignore_order
, e
);
1018 log_unit_warning(dep
,
1019 "Cannot add dependency job, ignoring: %s",
1020 bus_error_message(e
, r
));
1021 sd_bus_error_free(e
);
1027 if (IN_SET(type
, JOB_STOP
, JOB_RESTART
)) {
1028 static const UnitDependency propagate_deps
[] = {
1038 /* We propagate STOP as STOP, but RESTART only
1039 * as TRY_RESTART, in order not to start
1040 * dependencies that are not around. */
1041 ptype
= type
== JOB_RESTART
? JOB_TRY_RESTART
: type
;
1043 for (j
= 0; j
< ELEMENTSOF(propagate_deps
); j
++)
1044 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[propagate_deps
[j
]], i
) {
1047 nt
= job_type_collapse(ptype
, dep
);
1051 r
= transaction_add_job_and_dependencies(tr
, nt
, dep
, ret
, true, false, false, ignore_order
, e
);
1053 if (r
!= -EBADR
) /* job type not applicable */
1056 sd_bus_error_free(e
);
1061 if (type
== JOB_RELOAD
)
1062 transaction_add_propagate_reload_jobs(tr
, ret
->unit
, ret
, ignore_order
, e
);
1064 /* JOB_VERIFY_STARTED require no dependency handling */
1073 int transaction_add_isolate_jobs(Transaction
*tr
, Manager
*m
) {
1082 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
1084 /* ignore aliases */
1088 if (u
->ignore_on_isolate
)
1091 /* No need to stop inactive jobs */
1092 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)) && !u
->job
)
1095 /* Is there already something listed for this? */
1096 if (hashmap_get(tr
->jobs
, u
))
1099 r
= transaction_add_job_and_dependencies(tr
, JOB_STOP
, u
, tr
->anchor_job
, true, false, false, false, NULL
);
1101 log_unit_warning_errno(u
, r
, "Cannot add isolate job, ignoring: %m");
1107 Transaction
*transaction_new(bool irreversible
) {
1110 tr
= new0(Transaction
, 1);
1114 tr
->jobs
= hashmap_new(NULL
);
1118 tr
->irreversible
= irreversible
;
1123 void transaction_free(Transaction
*tr
) {
1124 assert(hashmap_isempty(tr
->jobs
));
1125 hashmap_free(tr
->jobs
);