]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/transaction.c
basic/log: add the log_struct terminator to macro
[thirdparty/systemd.git] / src / core / transaction.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <fcntl.h>
9 #include <unistd.h>
10
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"
17
18 static void transaction_unlink_job(Transaction *tr, Job *j, bool delete_dependencies);
19
20 static void transaction_delete_job(Transaction *tr, Job *j, bool delete_dependencies) {
21 assert(tr);
22 assert(j);
23
24 /* Deletes one job from the transaction */
25
26 transaction_unlink_job(tr, j, delete_dependencies);
27
28 job_free(j);
29 }
30
31 static void transaction_delete_unit(Transaction *tr, Unit *u) {
32 Job *j;
33
34 /* Deletes all jobs associated with a certain unit from the
35 * transaction */
36
37 while ((j = hashmap_get(tr->jobs, u)))
38 transaction_delete_job(tr, j, true);
39 }
40
41 void transaction_abort(Transaction *tr) {
42 Job *j;
43
44 assert(tr);
45
46 while ((j = hashmap_first(tr->jobs)))
47 transaction_delete_job(tr, j, false);
48
49 assert(hashmap_isempty(tr->jobs));
50 }
51
52 static void transaction_find_jobs_that_matter_to_anchor(Job *j, unsigned generation) {
53 JobDependency *l;
54
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. */
59
60 j->matters_to_anchor = true;
61 j->generation = generation;
62
63 LIST_FOREACH(subject, l, j->subject_list) {
64
65 /* This link does not matter */
66 if (!l->matters)
67 continue;
68
69 /* This unit has already been marked */
70 if (l->object->generation == generation)
71 continue;
72
73 transaction_find_jobs_that_matter_to_anchor(l->object, generation);
74 }
75 }
76
77 static void transaction_merge_and_delete_job(Transaction *tr, Job *j, Job *other, JobType t) {
78 JobDependency *l, *last;
79
80 assert(j);
81 assert(other);
82 assert(j->unit == other->unit);
83 assert(!j->installed);
84
85 /* Merges 'other' into 'j' and then deletes 'other'. */
86
87 j->type = t;
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;
91
92 /* Patch us in as new owner of the JobDependency objects */
93 last = NULL;
94 LIST_FOREACH(subject, l, other->subject_list) {
95 assert(l->subject == other);
96 l->subject = j;
97 last = l;
98 }
99
100 /* Merge both lists */
101 if (last) {
102 last->subject_next = j->subject_list;
103 if (j->subject_list)
104 j->subject_list->subject_prev = last;
105 j->subject_list = other->subject_list;
106 }
107
108 /* Patch us in as new owner of the JobDependency objects */
109 last = NULL;
110 LIST_FOREACH(object, l, other->object_list) {
111 assert(l->object == other);
112 l->object = j;
113 last = l;
114 }
115
116 /* Merge both lists */
117 if (last) {
118 last->object_next = j->object_list;
119 if (j->object_list)
120 j->object_list->object_prev = last;
121 j->object_list = other->object_list;
122 }
123
124 /* Kill the other job */
125 other->subject_list = NULL;
126 other->object_list = NULL;
127 transaction_delete_job(tr, other, true);
128 }
129
130 _pure_ static bool job_is_conflicted_by(Job *j) {
131 JobDependency *l;
132
133 assert(j);
134
135 /* Returns true if this job is pulled in by a least one
136 * ConflictedBy dependency. */
137
138 LIST_FOREACH(object, l, j->object_list)
139 if (l->conflicts)
140 return true;
141
142 return false;
143 }
144
145 static int delete_one_unmergeable_job(Transaction *tr, Job *j) {
146 Job *k;
147
148 assert(j);
149
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. */
154
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) {
159 Job *d;
160
161 /* Is this one mergeable? Then skip it */
162 if (job_type_is_mergeable(j->type, k->type))
163 continue;
164
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) {
168
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. */
177
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)));
186
187 if (j->type == JOB_STOP) {
188
189 if (job_is_conflicted_by(j))
190 d = k;
191 else
192 d = j;
193
194 } else if (k->type == JOB_STOP) {
195
196 if (job_is_conflicted_by(k))
197 d = j;
198 else
199 d = k;
200 } else
201 d = j;
202
203 } else if (!j->matters_to_anchor)
204 d = j;
205 else if (!k->matters_to_anchor)
206 d = k;
207 else
208 return -ENOEXEC;
209
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);
217 return 0;
218 }
219
220 return -EINVAL;
221 }
222
223 static int transaction_merge_jobs(Transaction *tr, sd_bus_error *e) {
224 Job *j;
225 Iterator i;
226 int r;
227
228 assert(tr);
229
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) {
233 JobType t;
234 Job *k;
235
236 t = j->type;
237 LIST_FOREACH(transaction, k, j->transaction_next) {
238 if (job_type_merge_and_collapse(&t, k->type, j->unit) >= 0)
239 continue;
240
241 /* OK, we could not merge all jobs for this
242 * action. Let's see if we can get rid of one
243 * of them */
244
245 r = delete_one_unmergeable_job(tr, j);
246 if (r >= 0)
247 /* Ok, we managed to drop one, now
248 * let's ask our callers to call us
249 * again after garbage collecting */
250 return -EAGAIN;
251
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),
258 k->unit->id);
259 }
260 }
261
262 /* Second step, merge the jobs. */
263 HASHMAP_FOREACH(j, tr->jobs, i) {
264 JobType t = j->type;
265 Job *k;
266
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);
270
271 while ((k = j->transaction_next)) {
272 if (tr->anchor_job == k) {
273 transaction_merge_and_delete_job(tr, k, j, t);
274 j = k;
275 } else
276 transaction_merge_and_delete_job(tr, j, k, t);
277 }
278
279 assert(!j->transaction_next);
280 assert(!j->transaction_prev);
281 }
282
283 return 0;
284 }
285
286 static void transaction_drop_redundant(Transaction *tr) {
287 Job *j;
288 Iterator i;
289
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. */
293
294 assert(tr);
295
296 rescan:
297 HASHMAP_FOREACH(j, tr->jobs, i) {
298 Job *k;
299
300 LIST_FOREACH(transaction, k, j) {
301
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)))
305 goto next_unit;
306 }
307
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);
310 goto rescan;
311 next_unit:;
312 }
313 }
314
315 _pure_ static bool unit_matters_to_anchor(Unit *u, Job *j) {
316 assert(u);
317 assert(!j->transaction_prev);
318
319 /* Checks whether at least one of the jobs for this unit
320 * matters to the anchor. */
321
322 LIST_FOREACH(transaction, j, j)
323 if (j->matters_to_anchor)
324 return true;
325
326 return false;
327 }
328
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;
332
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)) {
336 return mfree(ans);
337 }
338
339 sprintf(ans + size, "%s%s", unit_log_field, *unit_id);
340 if (*(unit_id+1))
341 ans[size + next] = '\n';
342 size += next + 1;
343 }
344
345 return ans;
346 }
347
348 static int transaction_verify_order_one(Transaction *tr, Job *j, Job *from, unsigned generation, sd_bus_error *e) {
349 Iterator i;
350 Unit *u;
351 void *v;
352 int r;
353
354 assert(tr);
355 assert(j);
356 assert(!j->transaction_prev);
357
358 /* Does a recursive sweep through the ordering graph, looking
359 * for a cycle. If we find a cycle we try to break it. */
360
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;
366
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. */
370 if (!j->marker)
371 return 0;
372
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
377 * there. */
378
379 for (k = from; k; k = ((k->generation == generation && k->marker != k) ? k->marker : NULL)) {
380
381 /* For logging below */
382 if (strv_push_pair(&array, k->unit->id, (char*) job_type_to_string(k->type)) < 0)
383 log_oom();
384
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. */
387 delete = k;
388
389 /* Check if this in fact was the beginning of the cycle */
390 if (k == j)
391 break;
392 }
393
394 unit_ids = merge_unit_ids(j->manager->unit_log_field, array); /* ignore error */
395
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",
400 j->unit->id,
401 unit_id == array ? "ordering cycle" : "dependency",
402 *unit_id, *job_type,
403 unit_ids);
404
405 if (delete) {
406 const char *status;
407 /* logging for j not k here to provide a consistent narrative */
408 log_struct(LOG_ERR,
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),
412 unit_ids);
413
414 if (log_get_show_color())
415 status = ANSI_HIGHLIGHT_RED " SKIP " ANSI_NORMAL;
416 else
417 status = " SKIP ";
418
419 unit_status_printf(delete->unit, status,
420 "Ordering cycle found, skipping %s");
421 transaction_delete_unit(tr, delete->unit);
422 return -EAGAIN;
423 }
424
425 log_struct(LOG_ERR,
426 "MESSAGE=%s: Unable to break cycle starting with %s/%s",
427 j->unit->id, j->unit->id, job_type_to_string(j->type),
428 unit_ids);
429
430 return sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC,
431 "Transaction order is cyclic. See system logs for details.");
432 }
433
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
437 * ourselves. */
438 j->marker = from ? from : j;
439 j->generation = generation;
440
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) {
444 Job *o;
445
446 /* Is there a job for this unit? */
447 o = hashmap_get(tr->jobs, u);
448 if (!o) {
449 /* Ok, there is no job for this in the
450 * transaction, but maybe there is already one
451 * running? */
452 o = u->job;
453 if (!o)
454 continue;
455 }
456
457 r = transaction_verify_order_one(tr, o, j, generation, e);
458 if (r < 0)
459 return r;
460 }
461
462 /* Ok, let's backtrack, and remember that this entry is not on
463 * our path anymore. */
464 j->marker = NULL;
465
466 return 0;
467 }
468
469 static int transaction_verify_order(Transaction *tr, unsigned *generation, sd_bus_error *e) {
470 Job *j;
471 int r;
472 Iterator i;
473 unsigned g;
474
475 assert(tr);
476 assert(generation);
477
478 /* Check if the ordering graph is cyclic. If it is, try to fix
479 * that up by dropping one of the jobs. */
480
481 g = (*generation)++;
482
483 HASHMAP_FOREACH(j, tr->jobs, i) {
484 r = transaction_verify_order_one(tr, j, NULL, g, e);
485 if (r < 0)
486 return r;
487 }
488
489 return 0;
490 }
491
492 static void transaction_collect_garbage(Transaction *tr) {
493 Iterator i;
494 Job *j;
495
496 assert(tr);
497
498 /* Drop jobs that are not required by any other job */
499
500 rescan:
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"); */
507 continue;
508 }
509
510 /* log_debug("Garbage collecting job %s/%s", j->unit->id, job_type_to_string(j->type)); */
511 transaction_delete_job(tr, j, true);
512 goto rescan;
513 }
514 }
515
516 static int transaction_is_destructive(Transaction *tr, JobMode mode, sd_bus_error *e) {
517 Iterator i;
518 Job *j;
519
520 assert(tr);
521
522 /* Checks whether applying this transaction means that
523 * existing jobs would be replaced */
524
525 HASHMAP_FOREACH(j, tr->jobs, i) {
526
527 /* Assume merged */
528 assert(!j->transaction_prev);
529 assert(!j->transaction_next);
530
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.");
535 }
536
537 return 0;
538 }
539
540 static void transaction_minimize_impact(Transaction *tr) {
541 Job *j;
542 Iterator i;
543
544 assert(tr);
545
546 /* Drops all unnecessary jobs that reverse already active jobs
547 * or that stop a running service. */
548
549 rescan:
550 HASHMAP_FOREACH(j, tr->jobs, i) {
551 LIST_FOREACH(transaction, j, j) {
552 bool stops_running_service, changes_existing_job;
553
554 /* If it matters, we shouldn't drop it */
555 if (j->matters_to_anchor)
556 continue;
557
558 /* Would this stop a running service?
559 * Would this change an existing job?
560 * If so, let's drop this entry */
561
562 stops_running_service =
563 j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
564
565 changes_existing_job =
566 j->unit->job &&
567 job_type_is_conflicting(j->type, j->unit->job->type);
568
569 if (!stops_running_service && !changes_existing_job)
570 continue;
571
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));
576
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));
581
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));
586
587 transaction_delete_job(tr, j, true);
588 goto rescan;
589 }
590 }
591 }
592
593 static int transaction_apply(Transaction *tr, Manager *m, JobMode mode) {
594 Iterator i;
595 Job *j;
596 int r;
597
598 /* Moves the transaction jobs to the set of active jobs */
599
600 if (IN_SET(mode, JOB_ISOLATE, JOB_FLUSH)) {
601
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);
606
607 if (j->unit->ignore_on_isolate)
608 continue;
609
610 if (hashmap_get(tr->jobs, j->unit))
611 continue;
612
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);
617 }
618 }
619
620 HASHMAP_FOREACH(j, tr->jobs, i) {
621 /* Assume merged */
622 assert(!j->transaction_prev);
623 assert(!j->transaction_next);
624
625 r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j);
626 if (r < 0)
627 goto rollback;
628 }
629
630 while ((j = hashmap_steal_first(tr->jobs))) {
631 Job *installed_job;
632
633 /* Clean the job dependencies */
634 transaction_unlink_job(tr, j, false);
635
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));
642 job_free(j);
643 j = installed_job;
644 }
645
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);
650 }
651
652 return 0;
653
654 rollback:
655
656 HASHMAP_FOREACH(j, tr->jobs, i)
657 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
658
659 return r;
660 }
661
662 int transaction_activate(Transaction *tr, Manager *m, JobMode mode, sd_bus_error *e) {
663 Iterator i;
664 Job *j;
665 int r;
666 unsigned generation = 1;
667
668 assert(tr);
669
670 /* This applies the changes recorded in tr->jobs to
671 * the actual list of jobs, if possible. */
672
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)
677 j->generation = 0;
678
679 /* First step: figure out which jobs matter */
680 transaction_find_jobs_that_matter_to_anchor(tr->anchor_job, generation++);
681
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);
687
688 /* Third step: Drop redundant jobs */
689 transaction_drop_redundant(tr);
690
691 for (;;) {
692 /* Fourth step: Let's remove unneeded jobs that might
693 * be lurking. */
694 if (mode != JOB_ISOLATE)
695 transaction_collect_garbage(tr);
696
697 /* Fifth step: verify order makes sense and correct
698 * cycles if necessary and possible */
699 r = transaction_verify_order(tr, &generation, e);
700 if (r >= 0)
701 break;
702
703 if (r != -EAGAIN) {
704 log_warning("Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error_message(e, r));
705 return r;
706 }
707
708 /* Let's see if the resulting transaction ordering
709 * graph is still cyclic... */
710 }
711
712 for (;;) {
713 /* Sixth step: let's drop unmergeable entries if
714 * necessary and possible, merge entries we can
715 * merge */
716 r = transaction_merge_jobs(tr, e);
717 if (r >= 0)
718 break;
719
720 if (r != -EAGAIN) {
721 log_warning("Requested transaction contains unmergeable jobs: %s", bus_error_message(e, r));
722 return r;
723 }
724
725 /* Seventh step: an entry got dropped, let's garbage
726 * collect its dependencies. */
727 if (mode != JOB_ISOLATE)
728 transaction_collect_garbage(tr);
729
730 /* Let's see if the resulting transaction still has
731 * unmergeable entries ... */
732 }
733
734 /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
735 transaction_drop_redundant(tr);
736
737 /* Ninth step: check whether we can actually apply this */
738 r = transaction_is_destructive(tr, mode, e);
739 if (r < 0) {
740 log_notice("Requested transaction contradicts existing jobs: %s", bus_error_message(e, r));
741 return r;
742 }
743
744 /* Tenth step: apply changes */
745 r = transaction_apply(tr, m, mode);
746 if (r < 0)
747 return log_warning_errno(r, "Failed to apply transaction: %m");
748
749 assert(hashmap_isempty(tr->jobs));
750
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. */
757
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);
762 }
763 }
764
765 return 0;
766 }
767
768 static Job* transaction_add_one_job(Transaction *tr, JobType type, Unit *unit, bool *is_new) {
769 Job *j, *f;
770
771 assert(tr);
772 assert(unit);
773
774 /* Looks for an existing prospective job and returns that. If
775 * it doesn't exist it is created and added to the prospective
776 * jobs list. */
777
778 f = hashmap_get(tr->jobs, unit);
779
780 LIST_FOREACH(transaction, j, f) {
781 assert(j->unit == unit);
782
783 if (j->type == type) {
784 if (is_new)
785 *is_new = false;
786 return j;
787 }
788 }
789
790 j = job_new(unit, type);
791 if (!j)
792 return NULL;
793
794 j->generation = 0;
795 j->marker = NULL;
796 j->matters_to_anchor = false;
797 j->irreversible = tr->irreversible;
798
799 LIST_PREPEND(transaction, f, j);
800
801 if (hashmap_replace(tr->jobs, unit, f) < 0) {
802 LIST_REMOVE(transaction, f, j);
803 job_free(j);
804 return NULL;
805 }
806
807 if (is_new)
808 *is_new = true;
809
810 /* log_debug("Added job %s/%s to transaction.", unit->id, job_type_to_string(type)); */
811
812 return j;
813 }
814
815 static void transaction_unlink_job(Transaction *tr, Job *j, bool delete_dependencies) {
816 assert(tr);
817 assert(j);
818
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);
823 else
824 hashmap_remove_value(tr->jobs, j->unit, j);
825
826 if (j->transaction_next)
827 j->transaction_next->transaction_prev = j->transaction_prev;
828
829 j->transaction_prev = j->transaction_next = NULL;
830
831 while (j->subject_list)
832 job_dependency_free(j->subject_list);
833
834 while (j->object_list) {
835 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
836
837 job_dependency_free(j->object_list);
838
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);
845 }
846 }
847 }
848
849 void transaction_add_propagate_reload_jobs(Transaction *tr, Unit *unit, Job *by, bool ignore_order, sd_bus_error *e) {
850 Iterator i;
851 JobType nt;
852 Unit *dep;
853 void *v;
854 int r;
855
856 assert(tr);
857 assert(unit);
858
859 HASHMAP_FOREACH_KEY(v, dep, unit->dependencies[UNIT_PROPAGATES_RELOAD_TO], i) {
860 nt = job_type_collapse(JOB_TRY_RELOAD, dep);
861 if (nt == JOB_NOP)
862 continue;
863
864 r = transaction_add_job_and_dependencies(tr, nt, dep, by, false, false, false, ignore_order, e);
865 if (r < 0) {
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);
870 }
871 }
872 }
873
874 int transaction_add_job_and_dependencies(
875 Transaction *tr,
876 JobType type,
877 Unit *unit,
878 Job *by,
879 bool matters,
880 bool conflicts,
881 bool ignore_requirements,
882 bool ignore_order,
883 sd_bus_error *e) {
884
885 bool is_new;
886 Iterator i;
887 Unit *dep;
888 Job *ret;
889 void *v;
890 int r;
891
892 assert(tr);
893 assert(type < _JOB_TYPE_MAX);
894 assert(type < _JOB_TYPE_MAX_IN_TRANSACTION);
895 assert(unit);
896
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))
902 unit_coldplug(unit);
903
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"); */
908
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);
911
912 if (type != JOB_STOP) {
913 r = bus_unit_check_load_state(unit, e);
914 if (r < 0)
915 return r;
916 }
917
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);
922
923 /* First add the job. */
924 ret = transaction_add_one_job(tr, type, unit, &is_new);
925 if (!ret)
926 return -ENOMEM;
927
928 ret->ignore_order = ret->ignore_order || ignore_order;
929
930 /* Then, add a link to the job. */
931 if (by) {
932 if (!job_dependency_new(by, ret, matters, conflicts))
933 return -ENOMEM;
934 } else {
935 /* If the job has no parent job, it is the anchor job. */
936 assert(!tr->anchor_job);
937 tr->anchor_job = ret;
938 }
939
940 if (is_new && !ignore_requirements && type != JOB_NOP) {
941 Set *following;
942
943 /* If we are following some other unit, make sure we
944 * add all dependencies of everybody following. */
945 if (unit_following_set(ret->unit, &following) > 0) {
946 SET_FOREACH(dep, following, i) {
947 r = transaction_add_job_and_dependencies(tr, type, dep, ret, false, false, false, ignore_order, e);
948 if (r < 0) {
949 log_unit_full(dep,
950 r == -ERFKILL ? LOG_INFO : LOG_WARNING,
951 r, "Cannot add dependency job, ignoring: %s",
952 bus_error_message(e, r));
953 sd_bus_error_free(e);
954 }
955 }
956
957 set_free(following);
958 }
959
960 /* Finally, recursively add in all dependencies. */
961 if (IN_SET(type, JOB_START, JOB_RESTART)) {
962 HASHMAP_FOREACH_KEY(v, dep, ret->unit->dependencies[UNIT_REQUIRES], i) {
963 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, true, false, false, ignore_order, e);
964 if (r < 0) {
965 if (r != -EBADR) /* job type not applicable */
966 goto fail;
967
968 sd_bus_error_free(e);
969 }
970 }
971
972 HASHMAP_FOREACH_KEY(v, dep, ret->unit->dependencies[UNIT_BINDS_TO], i) {
973 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, true, false, false, ignore_order, e);
974 if (r < 0) {
975 if (r != -EBADR) /* job type not applicable */
976 goto fail;
977
978 sd_bus_error_free(e);
979 }
980 }
981
982 HASHMAP_FOREACH_KEY(v, dep, ret->unit->dependencies[UNIT_WANTS], i) {
983 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, false, false, false, ignore_order, e);
984 if (r < 0) {
985 /* unit masked, job type not applicable and unit not found are not considered as errors. */
986 log_unit_full(dep,
987 IN_SET(r, -ERFKILL, -EBADR, -ENOENT) ? LOG_DEBUG : LOG_WARNING,
988 r, "Cannot add dependency job, ignoring: %s",
989 bus_error_message(e, r));
990 sd_bus_error_free(e);
991 }
992 }
993
994 HASHMAP_FOREACH_KEY(v, dep, ret->unit->dependencies[UNIT_REQUISITE], i) {
995 r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, ret, true, false, false, ignore_order, e);
996 if (r < 0) {
997 if (r != -EBADR) /* job type not applicable */
998 goto fail;
999
1000 sd_bus_error_free(e);
1001 }
1002 }
1003
1004 HASHMAP_FOREACH_KEY(v, dep, ret->unit->dependencies[UNIT_CONFLICTS], i) {
1005 r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, true, true, false, ignore_order, e);
1006 if (r < 0) {
1007 if (r != -EBADR) /* job type not applicable */
1008 goto fail;
1009
1010 sd_bus_error_free(e);
1011 }
1012 }
1013
1014 HASHMAP_FOREACH_KEY(v, dep, ret->unit->dependencies[UNIT_CONFLICTED_BY], i) {
1015 r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, false, false, false, ignore_order, e);
1016 if (r < 0) {
1017 log_unit_warning(dep,
1018 "Cannot add dependency job, ignoring: %s",
1019 bus_error_message(e, r));
1020 sd_bus_error_free(e);
1021 }
1022 }
1023
1024 }
1025
1026 if (IN_SET(type, JOB_STOP, JOB_RESTART)) {
1027 static const UnitDependency propagate_deps[] = {
1028 UNIT_REQUIRED_BY,
1029 UNIT_REQUISITE_OF,
1030 UNIT_BOUND_BY,
1031 UNIT_CONSISTS_OF,
1032 };
1033
1034 JobType ptype;
1035 unsigned j;
1036
1037 /* We propagate STOP as STOP, but RESTART only
1038 * as TRY_RESTART, in order not to start
1039 * dependencies that are not around. */
1040 ptype = type == JOB_RESTART ? JOB_TRY_RESTART : type;
1041
1042 for (j = 0; j < ELEMENTSOF(propagate_deps); j++)
1043 HASHMAP_FOREACH_KEY(v, dep, ret->unit->dependencies[propagate_deps[j]], i) {
1044 JobType nt;
1045
1046 nt = job_type_collapse(ptype, dep);
1047 if (nt == JOB_NOP)
1048 continue;
1049
1050 r = transaction_add_job_and_dependencies(tr, nt, dep, ret, true, false, false, ignore_order, e);
1051 if (r < 0) {
1052 if (r != -EBADR) /* job type not applicable */
1053 goto fail;
1054
1055 sd_bus_error_free(e);
1056 }
1057 }
1058 }
1059
1060 if (type == JOB_RELOAD)
1061 transaction_add_propagate_reload_jobs(tr, ret->unit, ret, ignore_order, e);
1062
1063 /* JOB_VERIFY_STARTED require no dependency handling */
1064 }
1065
1066 return 0;
1067
1068 fail:
1069 return r;
1070 }
1071
1072 int transaction_add_isolate_jobs(Transaction *tr, Manager *m) {
1073 Iterator i;
1074 Unit *u;
1075 char *k;
1076 int r;
1077
1078 assert(tr);
1079 assert(m);
1080
1081 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
1082
1083 /* ignore aliases */
1084 if (u->id != k)
1085 continue;
1086
1087 if (u->ignore_on_isolate)
1088 continue;
1089
1090 /* No need to stop inactive jobs */
1091 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) && !u->job)
1092 continue;
1093
1094 /* Is there already something listed for this? */
1095 if (hashmap_get(tr->jobs, u))
1096 continue;
1097
1098 r = transaction_add_job_and_dependencies(tr, JOB_STOP, u, tr->anchor_job, true, false, false, false, NULL);
1099 if (r < 0)
1100 log_unit_warning_errno(u, r, "Cannot add isolate job, ignoring: %m");
1101 }
1102
1103 return 0;
1104 }
1105
1106 Transaction *transaction_new(bool irreversible) {
1107 Transaction *tr;
1108
1109 tr = new0(Transaction, 1);
1110 if (!tr)
1111 return NULL;
1112
1113 tr->jobs = hashmap_new(NULL);
1114 if (!tr->jobs)
1115 return mfree(tr);
1116
1117 tr->irreversible = irreversible;
1118
1119 return tr;
1120 }
1121
1122 void transaction_free(Transaction *tr) {
1123 assert(hashmap_isempty(tr->jobs));
1124 hashmap_free(tr->jobs);
1125 free(tr);
1126 }