1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
7 #include "bus-common-errors.h"
9 #include "terminal-util.h"
10 #include "transaction.h"
11 #include "dbus-unit.h"
13 static void transaction_unlink_job(Transaction
*tr
, Job
*j
, bool delete_dependencies
);
15 static void transaction_delete_job(Transaction
*tr
, Job
*j
, bool delete_dependencies
) {
19 /* Deletes one job from the transaction */
21 transaction_unlink_job(tr
, j
, delete_dependencies
);
26 static void transaction_delete_unit(Transaction
*tr
, Unit
*u
) {
29 /* Deletes all jobs associated with a certain unit from the
32 while ((j
= hashmap_get(tr
->jobs
, u
)))
33 transaction_delete_job(tr
, j
, true);
36 void transaction_abort(Transaction
*tr
) {
41 while ((j
= hashmap_first(tr
->jobs
)))
42 transaction_delete_job(tr
, j
, false);
44 assert(hashmap_isempty(tr
->jobs
));
47 static void transaction_find_jobs_that_matter_to_anchor(Job
*j
, unsigned generation
) {
50 /* A recursive sweep through the graph that marks all units
51 * that matter to the anchor job, i.e. are directly or
52 * indirectly a dependency of the anchor job via paths that
53 * are fully marked as mattering. */
55 j
->matters_to_anchor
= true;
56 j
->generation
= generation
;
58 LIST_FOREACH(subject
, l
, j
->subject_list
) {
60 /* This link does not matter */
64 /* This unit has already been marked */
65 if (l
->object
->generation
== generation
)
68 transaction_find_jobs_that_matter_to_anchor(l
->object
, generation
);
72 static void transaction_merge_and_delete_job(Transaction
*tr
, Job
*j
, Job
*other
, JobType t
) {
73 JobDependency
*l
, *last
;
77 assert(j
->unit
== other
->unit
);
78 assert(!j
->installed
);
80 /* Merges 'other' into 'j' and then deletes 'other'. */
83 j
->state
= JOB_WAITING
;
84 j
->irreversible
= j
->irreversible
|| other
->irreversible
;
85 j
->matters_to_anchor
= j
->matters_to_anchor
|| other
->matters_to_anchor
;
87 /* Patch us in as new owner of the JobDependency objects */
89 LIST_FOREACH(subject
, l
, other
->subject_list
) {
90 assert(l
->subject
== other
);
95 /* Merge both lists */
97 last
->subject_next
= j
->subject_list
;
99 j
->subject_list
->subject_prev
= last
;
100 j
->subject_list
= other
->subject_list
;
103 /* Patch us in as new owner of the JobDependency objects */
105 LIST_FOREACH(object
, l
, other
->object_list
) {
106 assert(l
->object
== other
);
111 /* Merge both lists */
113 last
->object_next
= j
->object_list
;
115 j
->object_list
->object_prev
= last
;
116 j
->object_list
= other
->object_list
;
119 /* Kill the other job */
120 other
->subject_list
= NULL
;
121 other
->object_list
= NULL
;
122 transaction_delete_job(tr
, other
, true);
125 _pure_
static bool job_is_conflicted_by(Job
*j
) {
130 /* Returns true if this job is pulled in by a least one
131 * ConflictedBy dependency. */
133 LIST_FOREACH(object
, l
, j
->object_list
)
140 static int delete_one_unmergeable_job(Transaction
*tr
, Job
*j
) {
145 /* Tries to delete one item in the linked list
146 * j->transaction_next->transaction_next->... that conflicts
147 * with another one, in an attempt to make an inconsistent
148 * transaction work. */
150 /* We rely here on the fact that if a merged with b does not
151 * merge with c, either a or b merge with c neither */
152 LIST_FOREACH(transaction
, j
, j
)
153 LIST_FOREACH(transaction
, k
, j
->transaction_next
) {
156 /* Is this one mergeable? Then skip it */
157 if (job_type_is_mergeable(j
->type
, k
->type
))
160 /* Ok, we found two that conflict, let's see if we can
161 * drop one of them */
162 if (!j
->matters_to_anchor
&& !k
->matters_to_anchor
) {
164 /* Both jobs don't matter, so let's
165 * find the one that is smarter to
166 * remove. Let's think positive and
167 * rather remove stops then starts --
168 * except if something is being
169 * stopped because it is conflicted by
170 * another unit in which case we
171 * rather remove the start. */
173 log_unit_debug(j
->unit
,
174 "Looking at job %s/%s conflicted_by=%s",
175 j
->unit
->id
, job_type_to_string(j
->type
),
176 yes_no(j
->type
== JOB_STOP
&& job_is_conflicted_by(j
)));
177 log_unit_debug(k
->unit
,
178 "Looking at job %s/%s conflicted_by=%s",
179 k
->unit
->id
, job_type_to_string(k
->type
),
180 yes_no(k
->type
== JOB_STOP
&& job_is_conflicted_by(k
)));
182 if (j
->type
== JOB_STOP
) {
184 if (job_is_conflicted_by(j
))
189 } else if (k
->type
== JOB_STOP
) {
191 if (job_is_conflicted_by(k
))
198 } else if (!j
->matters_to_anchor
)
200 else if (!k
->matters_to_anchor
)
205 /* Ok, we can drop one, so let's do so. */
206 log_unit_debug(d
->unit
,
207 "Fixing conflicting jobs %s/%s,%s/%s by deleting job %s/%s",
208 j
->unit
->id
, job_type_to_string(j
->type
),
209 k
->unit
->id
, job_type_to_string(k
->type
),
210 d
->unit
->id
, job_type_to_string(d
->type
));
211 transaction_delete_job(tr
, d
, true);
218 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
, i
) {
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
, i
) {
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
) {
285 /* Goes through the transaction and removes all jobs of the units
286 * whose jobs are all noops. If not all of a unit's jobs are
287 * redundant, they are kept. */
292 HASHMAP_FOREACH(j
, tr
->jobs
, i
) {
295 LIST_FOREACH(transaction
, k
, j
) {
297 if (tr
->anchor_job
== k
||
298 !job_type_is_redundant(k
->type
, unit_active_state(k
->unit
)) ||
299 (k
->unit
->job
&& job_type_is_conflicting(k
->type
, k
->unit
->job
->type
)))
303 /* log_debug("Found redundant job %s/%s, dropping.", j->unit->id, job_type_to_string(j->type)); */
304 transaction_delete_job(tr
, j
, false);
310 _pure_
static bool unit_matters_to_anchor(Unit
*u
, Job
*j
) {
312 assert(!j
->transaction_prev
);
314 /* Checks whether at least one of the jobs for this unit
315 * matters to the anchor. */
317 LIST_FOREACH(transaction
, j
, j
)
318 if (j
->matters_to_anchor
)
324 static char* merge_unit_ids(const char* unit_log_field
, char **pairs
) {
325 char **unit_id
, **job_type
, *ans
= NULL
;
326 size_t alloc
= 0, size
= 0, next
;
328 STRV_FOREACH_PAIR(unit_id
, job_type
, pairs
) {
329 next
= strlen(unit_log_field
) + strlen(*unit_id
);
330 if (!GREEDY_REALLOC(ans
, alloc
, size
+ next
+ 1)) {
334 sprintf(ans
+ size
, "%s%s", unit_log_field
, *unit_id
);
336 ans
[size
+ next
] = '\n';
343 static int transaction_verify_order_one(Transaction
*tr
, Job
*j
, Job
*from
, unsigned generation
, sd_bus_error
*e
) {
351 assert(!j
->transaction_prev
);
353 /* Does a recursive sweep through the ordering graph, looking
354 * for a cycle. If we find a cycle we try to break it. */
356 /* Have we seen this before? */
357 if (j
->generation
== generation
) {
358 Job
*k
, *delete = NULL
;
359 _cleanup_free_
char **array
= NULL
, *unit_ids
= NULL
;
360 char **unit_id
, **job_type
;
362 /* If the marker is NULL we have been here already and
363 * decided the job was loop-free from here. Hence
364 * shortcut things and return right-away. */
368 /* So, the marker is not NULL and we already have been here. We have
369 * a cycle. Let's try to break it. We go backwards in our path and
370 * try to find a suitable job to remove. We use the marker to find
371 * our way back, since smart how we are we stored our way back in
374 for (k
= from
; k
; k
= ((k
->generation
== generation
&& k
->marker
!= k
) ? k
->marker
: NULL
)) {
376 /* For logging below */
377 if (strv_push_pair(&array
, k
->unit
->id
, (char*) job_type_to_string(k
->type
)) < 0)
380 if (!delete && hashmap_get(tr
->jobs
, k
->unit
) && !unit_matters_to_anchor(k
->unit
, k
))
381 /* Ok, we can drop this one, so let's do so. */
384 /* Check if this in fact was the beginning of the cycle */
389 unit_ids
= merge_unit_ids(j
->manager
->unit_log_field
, array
); /* ignore error */
391 STRV_FOREACH_PAIR(unit_id
, job_type
, array
)
392 /* logging for j not k here to provide a consistent narrative */
393 log_struct(LOG_WARNING
,
394 "MESSAGE=%s: Found %s on %s/%s",
396 unit_id
== array
? "ordering cycle" : "dependency",
402 /* logging for j not k here to provide a consistent narrative */
404 "MESSAGE=%s: Job %s/%s deleted to break ordering cycle starting with %s/%s",
405 j
->unit
->id
, delete->unit
->id
, job_type_to_string(delete->type
),
406 j
->unit
->id
, job_type_to_string(j
->type
),
409 if (log_get_show_color())
410 status
= ANSI_HIGHLIGHT_RED
" SKIP " ANSI_NORMAL
;
414 unit_status_printf(delete->unit
, status
,
415 "Ordering cycle found, skipping %s");
416 transaction_delete_unit(tr
, delete->unit
);
421 "MESSAGE=%s: Unable to break cycle starting with %s/%s",
422 j
->unit
->id
, j
->unit
->id
, job_type_to_string(j
->type
),
425 return sd_bus_error_setf(e
, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC
,
426 "Transaction order is cyclic. See system logs for details.");
429 /* Make the marker point to where we come from, so that we can
430 * find our way backwards if we want to break a cycle. We use
431 * a special marker for the beginning: we point to
433 j
->marker
= from
? from
: j
;
434 j
->generation
= generation
;
436 /* We assume that the dependencies are bidirectional, and
437 * hence can ignore UNIT_AFTER */
438 HASHMAP_FOREACH_KEY(v
, u
, j
->unit
->dependencies
[UNIT_BEFORE
], i
) {
441 /* Is there a job for this unit? */
442 o
= hashmap_get(tr
->jobs
, u
);
444 /* Ok, there is no job for this in the
445 * transaction, but maybe there is already one
452 r
= transaction_verify_order_one(tr
, o
, j
, generation
, e
);
457 /* Ok, let's backtrack, and remember that this entry is not on
458 * our path anymore. */
464 static int transaction_verify_order(Transaction
*tr
, unsigned *generation
, sd_bus_error
*e
) {
473 /* Check if the ordering graph is cyclic. If it is, try to fix
474 * that up by dropping one of the jobs. */
478 HASHMAP_FOREACH(j
, tr
->jobs
, i
) {
479 r
= transaction_verify_order_one(tr
, j
, NULL
, g
, e
);
487 static void transaction_collect_garbage(Transaction
*tr
) {
493 /* Drop jobs that are not required by any other job */
496 HASHMAP_FOREACH(j
, tr
->jobs
, i
) {
497 if (tr
->anchor_job
== j
|| j
->object_list
) {
498 /* log_debug("Keeping job %s/%s because of %s/%s", */
499 /* j->unit->id, job_type_to_string(j->type), */
500 /* j->object_list->subject ? j->object_list->subject->unit->id : "root", */
501 /* j->object_list->subject ? job_type_to_string(j->object_list->subject->type) : "root"); */
505 /* log_debug("Garbage collecting job %s/%s", j->unit->id, job_type_to_string(j->type)); */
506 transaction_delete_job(tr
, j
, true);
511 static int transaction_is_destructive(Transaction
*tr
, JobMode mode
, sd_bus_error
*e
) {
517 /* Checks whether applying this transaction means that
518 * existing jobs would be replaced */
520 HASHMAP_FOREACH(j
, tr
->jobs
, i
) {
523 assert(!j
->transaction_prev
);
524 assert(!j
->transaction_next
);
526 if (j
->unit
->job
&& (mode
== JOB_FAIL
|| j
->unit
->job
->irreversible
) &&
527 job_type_is_conflicting(j
->unit
->job
->type
, j
->type
))
528 return sd_bus_error_setf(e
, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE
,
529 "Transaction for %s/%s is destructive (%s has '%s' job queued, but '%s' is included in transaction).",
530 tr
->anchor_job
->unit
->id
, job_type_to_string(tr
->anchor_job
->type
),
531 j
->unit
->id
, job_type_to_string(j
->unit
->job
->type
), job_type_to_string(j
->type
));
537 static void transaction_minimize_impact(Transaction
*tr
) {
543 /* Drops all unnecessary jobs that reverse already active jobs
544 * or that stop a running service. */
547 HASHMAP_FOREACH(j
, tr
->jobs
, i
) {
548 LIST_FOREACH(transaction
, j
, j
) {
549 bool stops_running_service
, changes_existing_job
;
551 /* If it matters, we shouldn't drop it */
552 if (j
->matters_to_anchor
)
555 /* Would this stop a running service?
556 * Would this change an existing job?
557 * If so, let's drop this entry */
559 stops_running_service
=
560 j
->type
== JOB_STOP
&& UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j
->unit
));
562 changes_existing_job
=
564 job_type_is_conflicting(j
->type
, j
->unit
->job
->type
);
566 if (!stops_running_service
&& !changes_existing_job
)
569 if (stops_running_service
)
570 log_unit_debug(j
->unit
,
571 "%s/%s would stop a running service.",
572 j
->unit
->id
, job_type_to_string(j
->type
));
574 if (changes_existing_job
)
575 log_unit_debug(j
->unit
,
576 "%s/%s would change existing job.",
577 j
->unit
->id
, job_type_to_string(j
->type
));
579 /* Ok, let's get rid of this */
580 log_unit_debug(j
->unit
,
581 "Deleting %s/%s to minimize impact.",
582 j
->unit
->id
, job_type_to_string(j
->type
));
584 transaction_delete_job(tr
, j
, true);
590 static int transaction_apply(Transaction
*tr
, Manager
*m
, JobMode mode
) {
595 /* Moves the transaction jobs to the set of active jobs */
597 if (IN_SET(mode
, JOB_ISOLATE
, JOB_FLUSH
)) {
599 /* When isolating first kill all installed jobs which
600 * aren't part of the new transaction */
601 HASHMAP_FOREACH(j
, m
->jobs
, i
) {
602 assert(j
->installed
);
604 if (j
->unit
->ignore_on_isolate
)
607 if (hashmap_get(tr
->jobs
, j
->unit
))
610 /* Not invalidating recursively. Avoids triggering
611 * OnFailure= actions of dependent jobs. Also avoids
612 * invalidating our iterator. */
613 job_finish_and_invalidate(j
, JOB_CANCELED
, false, false);
617 HASHMAP_FOREACH(j
, tr
->jobs
, i
) {
619 assert(!j
->transaction_prev
);
620 assert(!j
->transaction_next
);
622 r
= hashmap_put(m
->jobs
, UINT32_TO_PTR(j
->id
), j
);
627 while ((j
= hashmap_steal_first(tr
->jobs
))) {
630 /* Clean the job dependencies */
631 transaction_unlink_job(tr
, j
, false);
633 installed_job
= job_install(j
);
634 if (installed_job
!= j
) {
635 /* j has been merged into a previously installed job */
636 if (tr
->anchor_job
== j
)
637 tr
->anchor_job
= installed_job
;
638 hashmap_remove(m
->jobs
, UINT32_TO_PTR(j
->id
));
643 job_add_to_run_queue(j
);
644 job_add_to_dbus_queue(j
);
645 job_start_timer(j
, false);
646 job_shutdown_magic(j
);
653 HASHMAP_FOREACH(j
, tr
->jobs
, i
)
654 hashmap_remove(m
->jobs
, UINT32_TO_PTR(j
->id
));
659 int transaction_activate(Transaction
*tr
, Manager
*m
, JobMode mode
, sd_bus_error
*e
) {
663 unsigned generation
= 1;
667 /* This applies the changes recorded in tr->jobs to
668 * the actual list of jobs, if possible. */
670 /* Reset the generation counter of all installed jobs. The detection of cycles
671 * looks at installed jobs. If they had a non-zero generation from some previous
672 * walk of the graph, the algorithm would break. */
673 HASHMAP_FOREACH(j
, m
->jobs
, i
)
676 /* First step: figure out which jobs matter */
677 transaction_find_jobs_that_matter_to_anchor(tr
->anchor_job
, generation
++);
679 /* Second step: Try not to stop any running services if
680 * we don't have to. Don't try to reverse running
681 * jobs if we don't have to. */
682 if (mode
== JOB_FAIL
)
683 transaction_minimize_impact(tr
);
685 /* Third step: Drop redundant jobs */
686 transaction_drop_redundant(tr
);
689 /* Fourth step: Let's remove unneeded jobs that might
691 if (mode
!= JOB_ISOLATE
)
692 transaction_collect_garbage(tr
);
694 /* Fifth step: verify order makes sense and correct
695 * cycles if necessary and possible */
696 r
= transaction_verify_order(tr
, &generation
, e
);
701 return log_warning_errno(r
, "Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error_message(e
, r
));
703 /* Let's see if the resulting transaction ordering
704 * graph is still cyclic... */
708 /* Sixth step: let's drop unmergeable entries if
709 * necessary and possible, merge entries we can
711 r
= transaction_merge_jobs(tr
, e
);
716 return log_warning_errno(r
, "Requested transaction contains unmergeable jobs: %s", bus_error_message(e
, r
));
718 /* Seventh step: an entry got dropped, let's garbage
719 * collect its dependencies. */
720 if (mode
!= JOB_ISOLATE
)
721 transaction_collect_garbage(tr
);
723 /* Let's see if the resulting transaction still has
724 * unmergeable entries ... */
727 /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
728 transaction_drop_redundant(tr
);
730 /* Ninth step: check whether we can actually apply this */
731 r
= transaction_is_destructive(tr
, mode
, e
);
733 return log_notice_errno(r
, "Requested transaction contradicts existing jobs: %s", bus_error_message(e
, r
));
735 /* Tenth step: apply changes */
736 r
= transaction_apply(tr
, m
, mode
);
738 return log_warning_errno(r
, "Failed to apply transaction: %m");
740 assert(hashmap_isempty(tr
->jobs
));
742 if (!hashmap_isempty(m
->jobs
)) {
743 /* Are there any jobs now? Then make sure we have the
744 * idle pipe around. We don't really care too much
745 * whether this works or not, as the idle pipe is a
746 * feature for cosmetics, not actually useful for
747 * anything beyond that. */
749 if (m
->idle_pipe
[0] < 0 && m
->idle_pipe
[1] < 0 &&
750 m
->idle_pipe
[2] < 0 && m
->idle_pipe
[3] < 0) {
751 (void) pipe2(m
->idle_pipe
, O_NONBLOCK
|O_CLOEXEC
);
752 (void) pipe2(m
->idle_pipe
+ 2, O_NONBLOCK
|O_CLOEXEC
);
759 static Job
* transaction_add_one_job(Transaction
*tr
, JobType type
, Unit
*unit
, bool *is_new
) {
765 /* Looks for an existing prospective job and returns that. If
766 * it doesn't exist it is created and added to the prospective
769 f
= hashmap_get(tr
->jobs
, unit
);
771 LIST_FOREACH(transaction
, j
, f
) {
772 assert(j
->unit
== unit
);
774 if (j
->type
== type
) {
781 j
= job_new(unit
, type
);
787 j
->matters_to_anchor
= false;
788 j
->irreversible
= tr
->irreversible
;
790 LIST_PREPEND(transaction
, f
, j
);
792 if (hashmap_replace(tr
->jobs
, unit
, f
) < 0) {
793 LIST_REMOVE(transaction
, f
, j
);
801 /* log_debug("Added job %s/%s to transaction.", unit->id, job_type_to_string(type)); */
806 static void transaction_unlink_job(Transaction
*tr
, Job
*j
, bool delete_dependencies
) {
810 if (j
->transaction_prev
)
811 j
->transaction_prev
->transaction_next
= j
->transaction_next
;
812 else if (j
->transaction_next
)
813 hashmap_replace(tr
->jobs
, j
->unit
, j
->transaction_next
);
815 hashmap_remove_value(tr
->jobs
, j
->unit
, j
);
817 if (j
->transaction_next
)
818 j
->transaction_next
->transaction_prev
= j
->transaction_prev
;
820 j
->transaction_prev
= j
->transaction_next
= NULL
;
822 while (j
->subject_list
)
823 job_dependency_free(j
->subject_list
);
825 while (j
->object_list
) {
826 Job
*other
= j
->object_list
->matters
? j
->object_list
->subject
: NULL
;
828 job_dependency_free(j
->object_list
);
830 if (other
&& delete_dependencies
) {
831 log_unit_debug(other
->unit
,
832 "Deleting job %s/%s as dependency of job %s/%s",
833 other
->unit
->id
, job_type_to_string(other
->type
),
834 j
->unit
->id
, job_type_to_string(j
->type
));
835 transaction_delete_job(tr
, other
, delete_dependencies
);
840 void transaction_add_propagate_reload_jobs(Transaction
*tr
, Unit
*unit
, Job
*by
, bool ignore_order
, sd_bus_error
*e
) {
850 HASHMAP_FOREACH_KEY(v
, dep
, unit
->dependencies
[UNIT_PROPAGATES_RELOAD_TO
], i
) {
851 nt
= job_type_collapse(JOB_TRY_RELOAD
, dep
);
855 r
= transaction_add_job_and_dependencies(tr
, nt
, dep
, by
, false, false, false, ignore_order
, e
);
857 log_unit_warning(dep
,
858 "Cannot add dependency reload job, ignoring: %s",
859 bus_error_message(e
, r
));
860 sd_bus_error_free(e
);
865 int transaction_add_job_and_dependencies(
872 bool ignore_requirements
,
884 assert(type
< _JOB_TYPE_MAX
);
885 assert(type
< _JOB_TYPE_MAX_IN_TRANSACTION
);
888 /* Before adding jobs for this unit, let's ensure that its state has been loaded
889 * This matters when jobs are spawned as part of coldplugging itself (see e. g. path_coldplug()).
890 * This way, we "recursively" coldplug units, ensuring that we do not look at state of
891 * not-yet-coldplugged units. */
892 if (MANAGER_IS_RELOADING(unit
->manager
))
895 /* log_debug("Pulling in %s/%s from %s/%s", */
896 /* unit->id, job_type_to_string(type), */
897 /* by ? by->unit->id : "NA", */
898 /* by ? job_type_to_string(by->type) : "NA"); */
900 /* Safety check that the unit is a valid state, i.e. not in UNIT_STUB or UNIT_MERGED which should only be set
902 if (!IN_SET(unit
->load_state
, UNIT_LOADED
, UNIT_ERROR
, UNIT_NOT_FOUND
, UNIT_BAD_SETTING
, UNIT_MASKED
))
903 return sd_bus_error_setf(e
, BUS_ERROR_LOAD_FAILED
, "Unit %s is not loaded properly.", unit
->id
);
905 if (type
!= JOB_STOP
) {
906 r
= bus_unit_validate_load_state(unit
, e
);
911 if (!unit_job_is_applicable(unit
, type
))
912 return sd_bus_error_setf(e
, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE
,
913 "Job type %s is not applicable for unit %s.",
914 job_type_to_string(type
), unit
->id
);
916 /* First add the job. */
917 ret
= transaction_add_one_job(tr
, type
, unit
, &is_new
);
921 ret
->ignore_order
= ret
->ignore_order
|| ignore_order
;
923 /* Then, add a link to the job. */
925 if (!job_dependency_new(by
, ret
, matters
, conflicts
))
928 /* If the job has no parent job, it is the anchor job. */
929 assert(!tr
->anchor_job
);
930 tr
->anchor_job
= ret
;
933 if (is_new
&& !ignore_requirements
&& type
!= JOB_NOP
) {
936 /* If we are following some other unit, make sure we
937 * add all dependencies of everybody following. */
938 if (unit_following_set(ret
->unit
, &following
) > 0) {
939 SET_FOREACH(dep
, following
, i
) {
940 r
= transaction_add_job_and_dependencies(tr
, type
, dep
, ret
, false, false, false, ignore_order
, e
);
943 r
== -ERFKILL
? LOG_INFO
: LOG_WARNING
,
944 r
, "Cannot add dependency job, ignoring: %s",
945 bus_error_message(e
, r
));
946 sd_bus_error_free(e
);
953 /* Finally, recursively add in all dependencies. */
954 if (IN_SET(type
, JOB_START
, JOB_RESTART
)) {
955 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_REQUIRES
], i
) {
956 r
= transaction_add_job_and_dependencies(tr
, JOB_START
, dep
, ret
, true, false, false, ignore_order
, e
);
958 if (r
!= -EBADR
) /* job type not applicable */
961 sd_bus_error_free(e
);
965 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_BINDS_TO
], i
) {
966 r
= transaction_add_job_and_dependencies(tr
, JOB_START
, dep
, ret
, true, false, false, ignore_order
, e
);
968 if (r
!= -EBADR
) /* job type not applicable */
971 sd_bus_error_free(e
);
975 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_WANTS
], i
) {
976 r
= transaction_add_job_and_dependencies(tr
, JOB_START
, dep
, ret
, false, false, false, ignore_order
, e
);
978 /* unit masked, job type not applicable and unit not found are not considered as errors. */
980 IN_SET(r
, -ERFKILL
, -EBADR
, -ENOENT
) ? LOG_DEBUG
: LOG_WARNING
,
981 r
, "Cannot add dependency job, ignoring: %s",
982 bus_error_message(e
, r
));
983 sd_bus_error_free(e
);
987 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_REQUISITE
], i
) {
988 r
= transaction_add_job_and_dependencies(tr
, JOB_VERIFY_ACTIVE
, dep
, ret
, true, false, false, ignore_order
, e
);
990 if (r
!= -EBADR
) /* job type not applicable */
993 sd_bus_error_free(e
);
997 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_CONFLICTS
], i
) {
998 r
= transaction_add_job_and_dependencies(tr
, JOB_STOP
, dep
, ret
, true, true, false, ignore_order
, e
);
1000 if (r
!= -EBADR
) /* job type not applicable */
1003 sd_bus_error_free(e
);
1007 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[UNIT_CONFLICTED_BY
], i
) {
1008 r
= transaction_add_job_and_dependencies(tr
, JOB_STOP
, dep
, ret
, false, false, false, ignore_order
, e
);
1010 log_unit_warning(dep
,
1011 "Cannot add dependency job, ignoring: %s",
1012 bus_error_message(e
, r
));
1013 sd_bus_error_free(e
);
1019 if (IN_SET(type
, JOB_STOP
, JOB_RESTART
)) {
1020 static const UnitDependency propagate_deps
[] = {
1030 /* We propagate STOP as STOP, but RESTART only
1031 * as TRY_RESTART, in order not to start
1032 * dependencies that are not around. */
1033 ptype
= type
== JOB_RESTART
? JOB_TRY_RESTART
: type
;
1035 for (j
= 0; j
< ELEMENTSOF(propagate_deps
); j
++)
1036 HASHMAP_FOREACH_KEY(v
, dep
, ret
->unit
->dependencies
[propagate_deps
[j
]], i
) {
1039 nt
= job_type_collapse(ptype
, dep
);
1043 r
= transaction_add_job_and_dependencies(tr
, nt
, dep
, ret
, true, false, false, ignore_order
, e
);
1045 if (r
!= -EBADR
) /* job type not applicable */
1048 sd_bus_error_free(e
);
1053 if (type
== JOB_RELOAD
)
1054 transaction_add_propagate_reload_jobs(tr
, ret
->unit
, ret
, ignore_order
, e
);
1056 /* JOB_VERIFY_STARTED require no dependency handling */
1065 int transaction_add_isolate_jobs(Transaction
*tr
, Manager
*m
) {
1074 HASHMAP_FOREACH_KEY(u
, k
, m
->units
, i
) {
1076 /* ignore aliases */
1080 if (u
->ignore_on_isolate
)
1083 /* No need to stop inactive jobs */
1084 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u
)) && !u
->job
)
1087 /* Is there already something listed for this? */
1088 if (hashmap_get(tr
->jobs
, u
))
1091 r
= transaction_add_job_and_dependencies(tr
, JOB_STOP
, u
, tr
->anchor_job
, true, false, false, false, NULL
);
1093 log_unit_warning_errno(u
, r
, "Cannot add isolate job, ignoring: %m");
1099 Transaction
*transaction_new(bool irreversible
) {
1102 tr
= new0(Transaction
, 1);
1106 tr
->jobs
= hashmap_new(NULL
);
1110 tr
->irreversible
= irreversible
;
1115 void transaction_free(Transaction
*tr
) {
1116 assert(hashmap_isempty(tr
->jobs
));
1117 hashmap_free(tr
->jobs
);