]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/transaction.c
tree-wide: shorten error logging a bit
[thirdparty/systemd.git] / src / core / transaction.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <fcntl.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "bus-common-errors.h"
8 #include "bus-error.h"
9 #include "terminal-util.h"
10 #include "transaction.h"
11 #include "dbus-unit.h"
12
13 static void transaction_unlink_job(Transaction *tr, Job *j, bool delete_dependencies);
14
15 static void transaction_delete_job(Transaction *tr, Job *j, bool delete_dependencies) {
16 assert(tr);
17 assert(j);
18
19 /* Deletes one job from the transaction */
20
21 transaction_unlink_job(tr, j, delete_dependencies);
22
23 job_free(j);
24 }
25
26 static void transaction_delete_unit(Transaction *tr, Unit *u) {
27 Job *j;
28
29 /* Deletes all jobs associated with a certain unit from the
30 * transaction */
31
32 while ((j = hashmap_get(tr->jobs, u)))
33 transaction_delete_job(tr, j, true);
34 }
35
36 void transaction_abort(Transaction *tr) {
37 Job *j;
38
39 assert(tr);
40
41 while ((j = hashmap_first(tr->jobs)))
42 transaction_delete_job(tr, j, false);
43
44 assert(hashmap_isempty(tr->jobs));
45 }
46
47 static void transaction_find_jobs_that_matter_to_anchor(Job *j, unsigned generation) {
48 JobDependency *l;
49
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. */
54
55 j->matters_to_anchor = true;
56 j->generation = generation;
57
58 LIST_FOREACH(subject, l, j->subject_list) {
59
60 /* This link does not matter */
61 if (!l->matters)
62 continue;
63
64 /* This unit has already been marked */
65 if (l->object->generation == generation)
66 continue;
67
68 transaction_find_jobs_that_matter_to_anchor(l->object, generation);
69 }
70 }
71
72 static void transaction_merge_and_delete_job(Transaction *tr, Job *j, Job *other, JobType t) {
73 JobDependency *l, *last;
74
75 assert(j);
76 assert(other);
77 assert(j->unit == other->unit);
78 assert(!j->installed);
79
80 /* Merges 'other' into 'j' and then deletes 'other'. */
81
82 j->type = t;
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;
86
87 /* Patch us in as new owner of the JobDependency objects */
88 last = NULL;
89 LIST_FOREACH(subject, l, other->subject_list) {
90 assert(l->subject == other);
91 l->subject = j;
92 last = l;
93 }
94
95 /* Merge both lists */
96 if (last) {
97 last->subject_next = j->subject_list;
98 if (j->subject_list)
99 j->subject_list->subject_prev = last;
100 j->subject_list = other->subject_list;
101 }
102
103 /* Patch us in as new owner of the JobDependency objects */
104 last = NULL;
105 LIST_FOREACH(object, l, other->object_list) {
106 assert(l->object == other);
107 l->object = j;
108 last = l;
109 }
110
111 /* Merge both lists */
112 if (last) {
113 last->object_next = j->object_list;
114 if (j->object_list)
115 j->object_list->object_prev = last;
116 j->object_list = other->object_list;
117 }
118
119 /* Kill the other job */
120 other->subject_list = NULL;
121 other->object_list = NULL;
122 transaction_delete_job(tr, other, true);
123 }
124
125 _pure_ static bool job_is_conflicted_by(Job *j) {
126 JobDependency *l;
127
128 assert(j);
129
130 /* Returns true if this job is pulled in by a least one
131 * ConflictedBy dependency. */
132
133 LIST_FOREACH(object, l, j->object_list)
134 if (l->conflicts)
135 return true;
136
137 return false;
138 }
139
140 static int delete_one_unmergeable_job(Transaction *tr, Job *j) {
141 Job *k;
142
143 assert(j);
144
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. */
149
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) {
154 Job *d;
155
156 /* Is this one mergeable? Then skip it */
157 if (job_type_is_mergeable(j->type, k->type))
158 continue;
159
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) {
163
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. */
172
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)));
181
182 if (j->type == JOB_STOP) {
183
184 if (job_is_conflicted_by(j))
185 d = k;
186 else
187 d = j;
188
189 } else if (k->type == JOB_STOP) {
190
191 if (job_is_conflicted_by(k))
192 d = j;
193 else
194 d = k;
195 } else
196 d = j;
197
198 } else if (!j->matters_to_anchor)
199 d = j;
200 else if (!k->matters_to_anchor)
201 d = k;
202 else
203 return -ENOEXEC;
204
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);
212 return 0;
213 }
214
215 return -EINVAL;
216 }
217
218 static int transaction_merge_jobs(Transaction *tr, sd_bus_error *e) {
219 Job *j;
220 Iterator i;
221 int r;
222
223 assert(tr);
224
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) {
228 JobType t;
229 Job *k;
230
231 t = j->type;
232 LIST_FOREACH(transaction, k, j->transaction_next) {
233 if (job_type_merge_and_collapse(&t, k->type, j->unit) >= 0)
234 continue;
235
236 /* OK, we could not merge all jobs for this
237 * action. Let's see if we can get rid of one
238 * of them */
239
240 r = delete_one_unmergeable_job(tr, j);
241 if (r >= 0)
242 /* Ok, we managed to drop one, now
243 * let's ask our callers to call us
244 * again after garbage collecting */
245 return -EAGAIN;
246
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),
253 k->unit->id);
254 }
255 }
256
257 /* Second step, merge the jobs. */
258 HASHMAP_FOREACH(j, tr->jobs, i) {
259 JobType t = j->type;
260 Job *k;
261
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);
265
266 while ((k = j->transaction_next)) {
267 if (tr->anchor_job == k) {
268 transaction_merge_and_delete_job(tr, k, j, t);
269 j = k;
270 } else
271 transaction_merge_and_delete_job(tr, j, k, t);
272 }
273
274 assert(!j->transaction_next);
275 assert(!j->transaction_prev);
276 }
277
278 return 0;
279 }
280
281 static void transaction_drop_redundant(Transaction *tr) {
282 Job *j;
283 Iterator i;
284
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. */
288
289 assert(tr);
290
291 rescan:
292 HASHMAP_FOREACH(j, tr->jobs, i) {
293 Job *k;
294
295 LIST_FOREACH(transaction, k, j) {
296
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)))
300 goto next_unit;
301 }
302
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);
305 goto rescan;
306 next_unit:;
307 }
308 }
309
310 _pure_ static bool unit_matters_to_anchor(Unit *u, Job *j) {
311 assert(u);
312 assert(!j->transaction_prev);
313
314 /* Checks whether at least one of the jobs for this unit
315 * matters to the anchor. */
316
317 LIST_FOREACH(transaction, j, j)
318 if (j->matters_to_anchor)
319 return true;
320
321 return false;
322 }
323
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;
327
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)) {
331 return mfree(ans);
332 }
333
334 sprintf(ans + size, "%s%s", unit_log_field, *unit_id);
335 if (*(unit_id+1))
336 ans[size + next] = '\n';
337 size += next + 1;
338 }
339
340 return ans;
341 }
342
343 static int transaction_verify_order_one(Transaction *tr, Job *j, Job *from, unsigned generation, sd_bus_error *e) {
344 Iterator i;
345 Unit *u;
346 void *v;
347 int r;
348
349 assert(tr);
350 assert(j);
351 assert(!j->transaction_prev);
352
353 /* Does a recursive sweep through the ordering graph, looking
354 * for a cycle. If we find a cycle we try to break it. */
355
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;
361
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. */
365 if (!j->marker)
366 return 0;
367
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
372 * there. */
373
374 for (k = from; k; k = ((k->generation == generation && k->marker != k) ? k->marker : NULL)) {
375
376 /* For logging below */
377 if (strv_push_pair(&array, k->unit->id, (char*) job_type_to_string(k->type)) < 0)
378 log_oom();
379
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. */
382 delete = k;
383
384 /* Check if this in fact was the beginning of the cycle */
385 if (k == j)
386 break;
387 }
388
389 unit_ids = merge_unit_ids(j->manager->unit_log_field, array); /* ignore error */
390
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",
395 j->unit->id,
396 unit_id == array ? "ordering cycle" : "dependency",
397 *unit_id, *job_type,
398 unit_ids);
399
400 if (delete) {
401 const char *status;
402 /* logging for j not k here to provide a consistent narrative */
403 log_struct(LOG_ERR,
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),
407 unit_ids);
408
409 if (log_get_show_color())
410 status = ANSI_HIGHLIGHT_RED " SKIP " ANSI_NORMAL;
411 else
412 status = " SKIP ";
413
414 unit_status_printf(delete->unit, status,
415 "Ordering cycle found, skipping %s");
416 transaction_delete_unit(tr, delete->unit);
417 return -EAGAIN;
418 }
419
420 log_struct(LOG_ERR,
421 "MESSAGE=%s: Unable to break cycle starting with %s/%s",
422 j->unit->id, j->unit->id, job_type_to_string(j->type),
423 unit_ids);
424
425 return sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC,
426 "Transaction order is cyclic. See system logs for details.");
427 }
428
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
432 * ourselves. */
433 j->marker = from ? from : j;
434 j->generation = generation;
435
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) {
439 Job *o;
440
441 /* Is there a job for this unit? */
442 o = hashmap_get(tr->jobs, u);
443 if (!o) {
444 /* Ok, there is no job for this in the
445 * transaction, but maybe there is already one
446 * running? */
447 o = u->job;
448 if (!o)
449 continue;
450 }
451
452 r = transaction_verify_order_one(tr, o, j, generation, e);
453 if (r < 0)
454 return r;
455 }
456
457 /* Ok, let's backtrack, and remember that this entry is not on
458 * our path anymore. */
459 j->marker = NULL;
460
461 return 0;
462 }
463
464 static int transaction_verify_order(Transaction *tr, unsigned *generation, sd_bus_error *e) {
465 Job *j;
466 int r;
467 Iterator i;
468 unsigned g;
469
470 assert(tr);
471 assert(generation);
472
473 /* Check if the ordering graph is cyclic. If it is, try to fix
474 * that up by dropping one of the jobs. */
475
476 g = (*generation)++;
477
478 HASHMAP_FOREACH(j, tr->jobs, i) {
479 r = transaction_verify_order_one(tr, j, NULL, g, e);
480 if (r < 0)
481 return r;
482 }
483
484 return 0;
485 }
486
487 static void transaction_collect_garbage(Transaction *tr) {
488 Iterator i;
489 Job *j;
490
491 assert(tr);
492
493 /* Drop jobs that are not required by any other job */
494
495 rescan:
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"); */
502 continue;
503 }
504
505 /* log_debug("Garbage collecting job %s/%s", j->unit->id, job_type_to_string(j->type)); */
506 transaction_delete_job(tr, j, true);
507 goto rescan;
508 }
509 }
510
511 static int transaction_is_destructive(Transaction *tr, JobMode mode, sd_bus_error *e) {
512 Iterator i;
513 Job *j;
514
515 assert(tr);
516
517 /* Checks whether applying this transaction means that
518 * existing jobs would be replaced */
519
520 HASHMAP_FOREACH(j, tr->jobs, i) {
521
522 /* Assume merged */
523 assert(!j->transaction_prev);
524 assert(!j->transaction_next);
525
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 is destructive.");
530 }
531
532 return 0;
533 }
534
535 static void transaction_minimize_impact(Transaction *tr) {
536 Job *j;
537 Iterator i;
538
539 assert(tr);
540
541 /* Drops all unnecessary jobs that reverse already active jobs
542 * or that stop a running service. */
543
544 rescan:
545 HASHMAP_FOREACH(j, tr->jobs, i) {
546 LIST_FOREACH(transaction, j, j) {
547 bool stops_running_service, changes_existing_job;
548
549 /* If it matters, we shouldn't drop it */
550 if (j->matters_to_anchor)
551 continue;
552
553 /* Would this stop a running service?
554 * Would this change an existing job?
555 * If so, let's drop this entry */
556
557 stops_running_service =
558 j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
559
560 changes_existing_job =
561 j->unit->job &&
562 job_type_is_conflicting(j->type, j->unit->job->type);
563
564 if (!stops_running_service && !changes_existing_job)
565 continue;
566
567 if (stops_running_service)
568 log_unit_debug(j->unit,
569 "%s/%s would stop a running service.",
570 j->unit->id, job_type_to_string(j->type));
571
572 if (changes_existing_job)
573 log_unit_debug(j->unit,
574 "%s/%s would change existing job.",
575 j->unit->id, job_type_to_string(j->type));
576
577 /* Ok, let's get rid of this */
578 log_unit_debug(j->unit,
579 "Deleting %s/%s to minimize impact.",
580 j->unit->id, job_type_to_string(j->type));
581
582 transaction_delete_job(tr, j, true);
583 goto rescan;
584 }
585 }
586 }
587
588 static int transaction_apply(Transaction *tr, Manager *m, JobMode mode) {
589 Iterator i;
590 Job *j;
591 int r;
592
593 /* Moves the transaction jobs to the set of active jobs */
594
595 if (IN_SET(mode, JOB_ISOLATE, JOB_FLUSH)) {
596
597 /* When isolating first kill all installed jobs which
598 * aren't part of the new transaction */
599 HASHMAP_FOREACH(j, m->jobs, i) {
600 assert(j->installed);
601
602 if (j->unit->ignore_on_isolate)
603 continue;
604
605 if (hashmap_get(tr->jobs, j->unit))
606 continue;
607
608 /* Not invalidating recursively. Avoids triggering
609 * OnFailure= actions of dependent jobs. Also avoids
610 * invalidating our iterator. */
611 job_finish_and_invalidate(j, JOB_CANCELED, false, false);
612 }
613 }
614
615 HASHMAP_FOREACH(j, tr->jobs, i) {
616 /* Assume merged */
617 assert(!j->transaction_prev);
618 assert(!j->transaction_next);
619
620 r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j);
621 if (r < 0)
622 goto rollback;
623 }
624
625 while ((j = hashmap_steal_first(tr->jobs))) {
626 Job *installed_job;
627
628 /* Clean the job dependencies */
629 transaction_unlink_job(tr, j, false);
630
631 installed_job = job_install(j);
632 if (installed_job != j) {
633 /* j has been merged into a previously installed job */
634 if (tr->anchor_job == j)
635 tr->anchor_job = installed_job;
636 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
637 job_free(j);
638 j = installed_job;
639 }
640
641 job_add_to_run_queue(j);
642 job_add_to_dbus_queue(j);
643 job_start_timer(j, false);
644 job_shutdown_magic(j);
645 }
646
647 return 0;
648
649 rollback:
650
651 HASHMAP_FOREACH(j, tr->jobs, i)
652 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
653
654 return r;
655 }
656
657 int transaction_activate(Transaction *tr, Manager *m, JobMode mode, sd_bus_error *e) {
658 Iterator i;
659 Job *j;
660 int r;
661 unsigned generation = 1;
662
663 assert(tr);
664
665 /* This applies the changes recorded in tr->jobs to
666 * the actual list of jobs, if possible. */
667
668 /* Reset the generation counter of all installed jobs. The detection of cycles
669 * looks at installed jobs. If they had a non-zero generation from some previous
670 * walk of the graph, the algorithm would break. */
671 HASHMAP_FOREACH(j, m->jobs, i)
672 j->generation = 0;
673
674 /* First step: figure out which jobs matter */
675 transaction_find_jobs_that_matter_to_anchor(tr->anchor_job, generation++);
676
677 /* Second step: Try not to stop any running services if
678 * we don't have to. Don't try to reverse running
679 * jobs if we don't have to. */
680 if (mode == JOB_FAIL)
681 transaction_minimize_impact(tr);
682
683 /* Third step: Drop redundant jobs */
684 transaction_drop_redundant(tr);
685
686 for (;;) {
687 /* Fourth step: Let's remove unneeded jobs that might
688 * be lurking. */
689 if (mode != JOB_ISOLATE)
690 transaction_collect_garbage(tr);
691
692 /* Fifth step: verify order makes sense and correct
693 * cycles if necessary and possible */
694 r = transaction_verify_order(tr, &generation, e);
695 if (r >= 0)
696 break;
697
698 if (r != -EAGAIN)
699 return log_warning_errno(r, "Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error_message(e, r));
700
701 /* Let's see if the resulting transaction ordering
702 * graph is still cyclic... */
703 }
704
705 for (;;) {
706 /* Sixth step: let's drop unmergeable entries if
707 * necessary and possible, merge entries we can
708 * merge */
709 r = transaction_merge_jobs(tr, e);
710 if (r >= 0)
711 break;
712
713 if (r != -EAGAIN)
714 return log_warning_errno(r, "Requested transaction contains unmergeable jobs: %s", bus_error_message(e, r));
715
716 /* Seventh step: an entry got dropped, let's garbage
717 * collect its dependencies. */
718 if (mode != JOB_ISOLATE)
719 transaction_collect_garbage(tr);
720
721 /* Let's see if the resulting transaction still has
722 * unmergeable entries ... */
723 }
724
725 /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
726 transaction_drop_redundant(tr);
727
728 /* Ninth step: check whether we can actually apply this */
729 r = transaction_is_destructive(tr, mode, e);
730 if (r < 0)
731 return log_notice_errno(r, "Requested transaction contradicts existing jobs: %s", bus_error_message(e, r));
732
733 /* Tenth step: apply changes */
734 r = transaction_apply(tr, m, mode);
735 if (r < 0)
736 return log_warning_errno(r, "Failed to apply transaction: %m");
737
738 assert(hashmap_isempty(tr->jobs));
739
740 if (!hashmap_isempty(m->jobs)) {
741 /* Are there any jobs now? Then make sure we have the
742 * idle pipe around. We don't really care too much
743 * whether this works or not, as the idle pipe is a
744 * feature for cosmetics, not actually useful for
745 * anything beyond that. */
746
747 if (m->idle_pipe[0] < 0 && m->idle_pipe[1] < 0 &&
748 m->idle_pipe[2] < 0 && m->idle_pipe[3] < 0) {
749 (void) pipe2(m->idle_pipe, O_NONBLOCK|O_CLOEXEC);
750 (void) pipe2(m->idle_pipe + 2, O_NONBLOCK|O_CLOEXEC);
751 }
752 }
753
754 return 0;
755 }
756
757 static Job* transaction_add_one_job(Transaction *tr, JobType type, Unit *unit, bool *is_new) {
758 Job *j, *f;
759
760 assert(tr);
761 assert(unit);
762
763 /* Looks for an existing prospective job and returns that. If
764 * it doesn't exist it is created and added to the prospective
765 * jobs list. */
766
767 f = hashmap_get(tr->jobs, unit);
768
769 LIST_FOREACH(transaction, j, f) {
770 assert(j->unit == unit);
771
772 if (j->type == type) {
773 if (is_new)
774 *is_new = false;
775 return j;
776 }
777 }
778
779 j = job_new(unit, type);
780 if (!j)
781 return NULL;
782
783 j->generation = 0;
784 j->marker = NULL;
785 j->matters_to_anchor = false;
786 j->irreversible = tr->irreversible;
787
788 LIST_PREPEND(transaction, f, j);
789
790 if (hashmap_replace(tr->jobs, unit, f) < 0) {
791 LIST_REMOVE(transaction, f, j);
792 job_free(j);
793 return NULL;
794 }
795
796 if (is_new)
797 *is_new = true;
798
799 /* log_debug("Added job %s/%s to transaction.", unit->id, job_type_to_string(type)); */
800
801 return j;
802 }
803
804 static void transaction_unlink_job(Transaction *tr, Job *j, bool delete_dependencies) {
805 assert(tr);
806 assert(j);
807
808 if (j->transaction_prev)
809 j->transaction_prev->transaction_next = j->transaction_next;
810 else if (j->transaction_next)
811 hashmap_replace(tr->jobs, j->unit, j->transaction_next);
812 else
813 hashmap_remove_value(tr->jobs, j->unit, j);
814
815 if (j->transaction_next)
816 j->transaction_next->transaction_prev = j->transaction_prev;
817
818 j->transaction_prev = j->transaction_next = NULL;
819
820 while (j->subject_list)
821 job_dependency_free(j->subject_list);
822
823 while (j->object_list) {
824 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
825
826 job_dependency_free(j->object_list);
827
828 if (other && delete_dependencies) {
829 log_unit_debug(other->unit,
830 "Deleting job %s/%s as dependency of job %s/%s",
831 other->unit->id, job_type_to_string(other->type),
832 j->unit->id, job_type_to_string(j->type));
833 transaction_delete_job(tr, other, delete_dependencies);
834 }
835 }
836 }
837
838 void transaction_add_propagate_reload_jobs(Transaction *tr, Unit *unit, Job *by, bool ignore_order, sd_bus_error *e) {
839 Iterator i;
840 JobType nt;
841 Unit *dep;
842 void *v;
843 int r;
844
845 assert(tr);
846 assert(unit);
847
848 HASHMAP_FOREACH_KEY(v, dep, unit->dependencies[UNIT_PROPAGATES_RELOAD_TO], i) {
849 nt = job_type_collapse(JOB_TRY_RELOAD, dep);
850 if (nt == JOB_NOP)
851 continue;
852
853 r = transaction_add_job_and_dependencies(tr, nt, dep, by, false, false, false, ignore_order, e);
854 if (r < 0) {
855 log_unit_warning(dep,
856 "Cannot add dependency reload job, ignoring: %s",
857 bus_error_message(e, r));
858 sd_bus_error_free(e);
859 }
860 }
861 }
862
863 int transaction_add_job_and_dependencies(
864 Transaction *tr,
865 JobType type,
866 Unit *unit,
867 Job *by,
868 bool matters,
869 bool conflicts,
870 bool ignore_requirements,
871 bool ignore_order,
872 sd_bus_error *e) {
873
874 bool is_new;
875 Iterator i;
876 Unit *dep;
877 Job *ret;
878 void *v;
879 int r;
880
881 assert(tr);
882 assert(type < _JOB_TYPE_MAX);
883 assert(type < _JOB_TYPE_MAX_IN_TRANSACTION);
884 assert(unit);
885
886 /* Before adding jobs for this unit, let's ensure that its state has been loaded
887 * This matters when jobs are spawned as part of coldplugging itself (see e. g. path_coldplug()).
888 * This way, we "recursively" coldplug units, ensuring that we do not look at state of
889 * not-yet-coldplugged units. */
890 if (MANAGER_IS_RELOADING(unit->manager))
891 unit_coldplug(unit);
892
893 /* log_debug("Pulling in %s/%s from %s/%s", */
894 /* unit->id, job_type_to_string(type), */
895 /* by ? by->unit->id : "NA", */
896 /* by ? job_type_to_string(by->type) : "NA"); */
897
898 /* Safety check that the unit is a valid state, i.e. not in UNIT_STUB or UNIT_MERGED which should only be set
899 * temporarily. */
900 if (!IN_SET(unit->load_state, UNIT_LOADED, UNIT_ERROR, UNIT_NOT_FOUND, UNIT_BAD_SETTING, UNIT_MASKED))
901 return sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED, "Unit %s is not loaded properly.", unit->id);
902
903 if (type != JOB_STOP) {
904 r = bus_unit_validate_load_state(unit, e);
905 if (r < 0)
906 return r;
907 }
908
909 if (!unit_job_is_applicable(unit, type))
910 return sd_bus_error_setf(e, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE,
911 "Job type %s is not applicable for unit %s.",
912 job_type_to_string(type), unit->id);
913
914 /* First add the job. */
915 ret = transaction_add_one_job(tr, type, unit, &is_new);
916 if (!ret)
917 return -ENOMEM;
918
919 ret->ignore_order = ret->ignore_order || ignore_order;
920
921 /* Then, add a link to the job. */
922 if (by) {
923 if (!job_dependency_new(by, ret, matters, conflicts))
924 return -ENOMEM;
925 } else {
926 /* If the job has no parent job, it is the anchor job. */
927 assert(!tr->anchor_job);
928 tr->anchor_job = ret;
929 }
930
931 if (is_new && !ignore_requirements && type != JOB_NOP) {
932 Set *following;
933
934 /* If we are following some other unit, make sure we
935 * add all dependencies of everybody following. */
936 if (unit_following_set(ret->unit, &following) > 0) {
937 SET_FOREACH(dep, following, i) {
938 r = transaction_add_job_and_dependencies(tr, type, dep, ret, false, false, false, ignore_order, e);
939 if (r < 0) {
940 log_unit_full(dep,
941 r == -ERFKILL ? LOG_INFO : LOG_WARNING,
942 r, "Cannot add dependency job, ignoring: %s",
943 bus_error_message(e, r));
944 sd_bus_error_free(e);
945 }
946 }
947
948 set_free(following);
949 }
950
951 /* Finally, recursively add in all dependencies. */
952 if (IN_SET(type, JOB_START, JOB_RESTART)) {
953 HASHMAP_FOREACH_KEY(v, dep, ret->unit->dependencies[UNIT_REQUIRES], i) {
954 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, true, false, false, ignore_order, e);
955 if (r < 0) {
956 if (r != -EBADR) /* job type not applicable */
957 goto fail;
958
959 sd_bus_error_free(e);
960 }
961 }
962
963 HASHMAP_FOREACH_KEY(v, dep, ret->unit->dependencies[UNIT_BINDS_TO], i) {
964 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, true, false, false, ignore_order, e);
965 if (r < 0) {
966 if (r != -EBADR) /* job type not applicable */
967 goto fail;
968
969 sd_bus_error_free(e);
970 }
971 }
972
973 HASHMAP_FOREACH_KEY(v, dep, ret->unit->dependencies[UNIT_WANTS], i) {
974 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, false, false, false, ignore_order, e);
975 if (r < 0) {
976 /* unit masked, job type not applicable and unit not found are not considered as errors. */
977 log_unit_full(dep,
978 IN_SET(r, -ERFKILL, -EBADR, -ENOENT) ? LOG_DEBUG : LOG_WARNING,
979 r, "Cannot add dependency job, ignoring: %s",
980 bus_error_message(e, r));
981 sd_bus_error_free(e);
982 }
983 }
984
985 HASHMAP_FOREACH_KEY(v, dep, ret->unit->dependencies[UNIT_REQUISITE], i) {
986 r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, ret, true, false, false, ignore_order, e);
987 if (r < 0) {
988 if (r != -EBADR) /* job type not applicable */
989 goto fail;
990
991 sd_bus_error_free(e);
992 }
993 }
994
995 HASHMAP_FOREACH_KEY(v, dep, ret->unit->dependencies[UNIT_CONFLICTS], i) {
996 r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, true, true, false, ignore_order, e);
997 if (r < 0) {
998 if (r != -EBADR) /* job type not applicable */
999 goto fail;
1000
1001 sd_bus_error_free(e);
1002 }
1003 }
1004
1005 HASHMAP_FOREACH_KEY(v, dep, ret->unit->dependencies[UNIT_CONFLICTED_BY], i) {
1006 r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, false, false, false, ignore_order, e);
1007 if (r < 0) {
1008 log_unit_warning(dep,
1009 "Cannot add dependency job, ignoring: %s",
1010 bus_error_message(e, r));
1011 sd_bus_error_free(e);
1012 }
1013 }
1014
1015 }
1016
1017 if (IN_SET(type, JOB_STOP, JOB_RESTART)) {
1018 static const UnitDependency propagate_deps[] = {
1019 UNIT_REQUIRED_BY,
1020 UNIT_REQUISITE_OF,
1021 UNIT_BOUND_BY,
1022 UNIT_CONSISTS_OF,
1023 };
1024
1025 JobType ptype;
1026 unsigned j;
1027
1028 /* We propagate STOP as STOP, but RESTART only
1029 * as TRY_RESTART, in order not to start
1030 * dependencies that are not around. */
1031 ptype = type == JOB_RESTART ? JOB_TRY_RESTART : type;
1032
1033 for (j = 0; j < ELEMENTSOF(propagate_deps); j++)
1034 HASHMAP_FOREACH_KEY(v, dep, ret->unit->dependencies[propagate_deps[j]], i) {
1035 JobType nt;
1036
1037 nt = job_type_collapse(ptype, dep);
1038 if (nt == JOB_NOP)
1039 continue;
1040
1041 r = transaction_add_job_and_dependencies(tr, nt, dep, ret, true, false, false, ignore_order, e);
1042 if (r < 0) {
1043 if (r != -EBADR) /* job type not applicable */
1044 goto fail;
1045
1046 sd_bus_error_free(e);
1047 }
1048 }
1049 }
1050
1051 if (type == JOB_RELOAD)
1052 transaction_add_propagate_reload_jobs(tr, ret->unit, ret, ignore_order, e);
1053
1054 /* JOB_VERIFY_STARTED require no dependency handling */
1055 }
1056
1057 return 0;
1058
1059 fail:
1060 return r;
1061 }
1062
1063 int transaction_add_isolate_jobs(Transaction *tr, Manager *m) {
1064 Iterator i;
1065 Unit *u;
1066 char *k;
1067 int r;
1068
1069 assert(tr);
1070 assert(m);
1071
1072 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
1073
1074 /* ignore aliases */
1075 if (u->id != k)
1076 continue;
1077
1078 if (u->ignore_on_isolate)
1079 continue;
1080
1081 /* No need to stop inactive jobs */
1082 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) && !u->job)
1083 continue;
1084
1085 /* Is there already something listed for this? */
1086 if (hashmap_get(tr->jobs, u))
1087 continue;
1088
1089 r = transaction_add_job_and_dependencies(tr, JOB_STOP, u, tr->anchor_job, true, false, false, false, NULL);
1090 if (r < 0)
1091 log_unit_warning_errno(u, r, "Cannot add isolate job, ignoring: %m");
1092 }
1093
1094 return 0;
1095 }
1096
1097 Transaction *transaction_new(bool irreversible) {
1098 Transaction *tr;
1099
1100 tr = new0(Transaction, 1);
1101 if (!tr)
1102 return NULL;
1103
1104 tr->jobs = hashmap_new(NULL);
1105 if (!tr->jobs)
1106 return mfree(tr);
1107
1108 tr->irreversible = irreversible;
1109
1110 return tr;
1111 }
1112
1113 void transaction_free(Transaction *tr) {
1114 assert(hashmap_isempty(tr->jobs));
1115 hashmap_free(tr->jobs);
1116 free(tr);
1117 }