chiark / gitweb /
aa0cedf6eb097abb0cfbef2bb761eec932e4c859
[elogind.git] / src / core / transaction.c
1 #include "transaction.h"
2 #include "bus-errors.h"
3
4 static void transaction_unlink_job(Transaction *tr, Job *j, bool delete_dependencies);
5
6 static void transaction_delete_job(Transaction *tr, Job *j, bool delete_dependencies) {
7         assert(tr);
8         assert(j);
9
10         /* Deletes one job from the transaction */
11
12         transaction_unlink_job(tr, j, delete_dependencies);
13
14         if (!j->installed)
15                 job_free(j);
16 }
17
18 static void transaction_delete_unit(Transaction *tr, Unit *u) {
19         Job *j;
20
21         /* Deletes all jobs associated with a certain unit from the
22          * transaction */
23
24         while ((j = hashmap_get(tr->jobs, u)))
25                 transaction_delete_job(tr, j, true);
26 }
27
28 void transaction_abort(Transaction *tr) {
29         Job *j;
30
31         assert(tr);
32
33         while ((j = hashmap_first(tr->jobs)))
34                 transaction_delete_job(tr, j, true);
35
36         assert(hashmap_isempty(tr->jobs));
37 }
38
39 static void transaction_find_jobs_that_matter_to_anchor(Job *j, unsigned generation) {
40         JobDependency *l;
41
42         /* A recursive sweep through the graph that marks all units
43          * that matter to the anchor job, i.e. are directly or
44          * indirectly a dependency of the anchor job via paths that
45          * are fully marked as mattering. */
46
47         j->matters_to_anchor = true;
48         j->generation = generation;
49
50         LIST_FOREACH(subject, l, j->subject_list) {
51
52                 /* This link does not matter */
53                 if (!l->matters)
54                         continue;
55
56                 /* This unit has already been marked */
57                 if (l->object->generation == generation)
58                         continue;
59
60                 transaction_find_jobs_that_matter_to_anchor(l->object, generation);
61         }
62 }
63
64 static void transaction_merge_and_delete_job(Transaction *tr, Job *j, Job *other, JobType t) {
65         JobDependency *l, *last;
66
67         assert(j);
68         assert(other);
69         assert(j->unit == other->unit);
70         assert(!j->installed);
71
72         /* Merges 'other' into 'j' and then deletes 'other'. */
73
74         j->type = t;
75         j->state = JOB_WAITING;
76         j->override = j->override || other->override;
77
78         j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
79
80         /* Patch us in as new owner of the JobDependency objects */
81         last = NULL;
82         LIST_FOREACH(subject, l, other->subject_list) {
83                 assert(l->subject == other);
84                 l->subject = j;
85                 last = l;
86         }
87
88         /* Merge both lists */
89         if (last) {
90                 last->subject_next = j->subject_list;
91                 if (j->subject_list)
92                         j->subject_list->subject_prev = last;
93                 j->subject_list = other->subject_list;
94         }
95
96         /* Patch us in as new owner of the JobDependency objects */
97         last = NULL;
98         LIST_FOREACH(object, l, other->object_list) {
99                 assert(l->object == other);
100                 l->object = j;
101                 last = l;
102         }
103
104         /* Merge both lists */
105         if (last) {
106                 last->object_next = j->object_list;
107                 if (j->object_list)
108                         j->object_list->object_prev = last;
109                 j->object_list = other->object_list;
110         }
111
112         /* Kill the other job */
113         other->subject_list = NULL;
114         other->object_list = NULL;
115         transaction_delete_job(tr, other, true);
116 }
117
118 static bool job_is_conflicted_by(Job *j) {
119         JobDependency *l;
120
121         assert(j);
122
123         /* Returns true if this job is pulled in by a least one
124          * ConflictedBy dependency. */
125
126         LIST_FOREACH(object, l, j->object_list)
127                 if (l->conflicts)
128                         return true;
129
130         return false;
131 }
132
133 static int delete_one_unmergeable_job(Transaction *tr, Job *j) {
134         Job *k;
135
136         assert(j);
137
138         /* Tries to delete one item in the linked list
139          * j->transaction_next->transaction_next->... that conflicts
140          * with another one, in an attempt to make an inconsistent
141          * transaction work. */
142
143         /* We rely here on the fact that if a merged with b does not
144          * merge with c, either a or b merge with c neither */
145         LIST_FOREACH(transaction, j, j)
146                 LIST_FOREACH(transaction, k, j->transaction_next) {
147                         Job *d;
148
149                         /* Is this one mergeable? Then skip it */
150                         if (job_type_is_mergeable(j->type, k->type))
151                                 continue;
152
153                         /* Ok, we found two that conflict, let's see if we can
154                          * drop one of them */
155                         if (!j->matters_to_anchor && !k->matters_to_anchor) {
156
157                                 /* Both jobs don't matter, so let's
158                                  * find the one that is smarter to
159                                  * remove. Let's think positive and
160                                  * rather remove stops then starts --
161                                  * except if something is being
162                                  * stopped because it is conflicted by
163                                  * another unit in which case we
164                                  * rather remove the start. */
165
166                                 log_debug("Looking at job %s/%s conflicted_by=%s", j->unit->id, job_type_to_string(j->type), yes_no(j->type == JOB_STOP && job_is_conflicted_by(j)));
167                                 log_debug("Looking at job %s/%s conflicted_by=%s", k->unit->id, job_type_to_string(k->type), yes_no(k->type == JOB_STOP && job_is_conflicted_by(k)));
168
169                                 if (j->type == JOB_STOP) {
170
171                                         if (job_is_conflicted_by(j))
172                                                 d = k;
173                                         else
174                                                 d = j;
175
176                                 } else if (k->type == JOB_STOP) {
177
178                                         if (job_is_conflicted_by(k))
179                                                 d = j;
180                                         else
181                                                 d = k;
182                                 } else
183                                         d = j;
184
185                         } else if (!j->matters_to_anchor)
186                                 d = j;
187                         else if (!k->matters_to_anchor)
188                                 d = k;
189                         else
190                                 return -ENOEXEC;
191
192                         /* Ok, we can drop one, so let's do so. */
193                         log_debug("Fixing conflicting jobs by deleting job %s/%s", d->unit->id, job_type_to_string(d->type));
194                         transaction_delete_job(tr, d, true);
195                         return 0;
196                 }
197
198         return -EINVAL;
199 }
200
201 static int transaction_merge_jobs(Transaction *tr, DBusError *e) {
202         Job *j;
203         Iterator i;
204         int r;
205
206         assert(tr);
207
208         /* First step, check whether any of the jobs for one specific
209          * task conflict. If so, try to drop one of them. */
210         HASHMAP_FOREACH(j, tr->jobs, i) {
211                 JobType t;
212                 Job *k;
213
214                 t = j->type;
215                 LIST_FOREACH(transaction, k, j->transaction_next) {
216                         if (job_type_merge(&t, k->type) >= 0)
217                                 continue;
218
219                         /* OK, we could not merge all jobs for this
220                          * action. Let's see if we can get rid of one
221                          * of them */
222
223                         r = delete_one_unmergeable_job(tr, j);
224                         if (r >= 0)
225                                 /* Ok, we managed to drop one, now
226                                  * let's ask our callers to call us
227                                  * again after garbage collecting */
228                                 return -EAGAIN;
229
230                         /* We couldn't merge anything. Failure */
231                         dbus_set_error(e, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING, "Transaction contains conflicting jobs '%s' and '%s' for %s. Probably contradicting requirement dependencies configured.",
232                                        job_type_to_string(t), job_type_to_string(k->type), k->unit->id);
233                         return r;
234                 }
235         }
236
237         /* Second step, merge the jobs. */
238         HASHMAP_FOREACH(j, tr->jobs, i) {
239                 JobType t = j->type;
240                 Job *k;
241
242                 /* Merge all transactions */
243                 LIST_FOREACH(transaction, k, j->transaction_next)
244                         assert_se(job_type_merge(&t, k->type) == 0);
245
246                 while ((k = j->transaction_next)) {
247                         if (tr->anchor_job == k) {
248                                 transaction_merge_and_delete_job(tr, k, j, t);
249                                 j = k;
250                         } else
251                                 transaction_merge_and_delete_job(tr, j, k, t);
252                 }
253
254                 assert(!j->transaction_next);
255                 assert(!j->transaction_prev);
256         }
257
258         return 0;
259 }
260
261 static void transaction_drop_redundant(Transaction *tr) {
262         bool again;
263
264         assert(tr);
265
266         /* Goes through the transaction and removes all jobs that are
267          * a noop */
268
269         do {
270                 Job *j;
271                 Iterator i;
272
273                 again = false;
274
275                 HASHMAP_FOREACH(j, tr->jobs, i) {
276                         bool changes_something = false;
277                         Job *k;
278
279                         LIST_FOREACH(transaction, k, j) {
280
281                                 if (tr->anchor_job != k &&
282                                     (k->installed || job_type_is_redundant(k->type, unit_active_state(k->unit))) &&
283                                     (!k->unit->job || !job_type_is_conflicting(k->type, k->unit->job->type)))
284                                         continue;
285
286                                 changes_something = true;
287                                 break;
288                         }
289
290                         if (changes_something)
291                                 continue;
292
293                         /* log_debug("Found redundant job %s/%s, dropping.", j->unit->id, job_type_to_string(j->type)); */
294                         transaction_delete_job(tr, j, false);
295                         again = true;
296                         break;
297                 }
298
299         } while (again);
300 }
301
302 static bool unit_matters_to_anchor(Unit *u, Job *j) {
303         assert(u);
304         assert(!j->transaction_prev);
305
306         /* Checks whether at least one of the jobs for this unit
307          * matters to the anchor. */
308
309         LIST_FOREACH(transaction, j, j)
310                 if (j->matters_to_anchor)
311                         return true;
312
313         return false;
314 }
315
316 static int transaction_verify_order_one(Transaction *tr, Job *j, Job *from, unsigned generation, DBusError *e) {
317         Iterator i;
318         Unit *u;
319         int r;
320
321         assert(tr);
322         assert(j);
323         assert(!j->transaction_prev);
324
325         /* Does a recursive sweep through the ordering graph, looking
326          * for a cycle. If we find cycle we try to break it. */
327
328         /* Have we seen this before? */
329         if (j->generation == generation) {
330                 Job *k, *delete;
331
332                 /* If the marker is NULL we have been here already and
333                  * decided the job was loop-free from here. Hence
334                  * shortcut things and return right-away. */
335                 if (!j->marker)
336                         return 0;
337
338                 /* So, the marker is not NULL and we already have been
339                  * here. We have a cycle. Let's try to break it. We go
340                  * backwards in our path and try to find a suitable
341                  * job to remove. We use the marker to find our way
342                  * back, since smart how we are we stored our way back
343                  * in there. */
344                 log_warning("Found ordering cycle on %s/%s", j->unit->id, job_type_to_string(j->type));
345
346                 delete = NULL;
347                 for (k = from; k; k = ((k->generation == generation && k->marker != k) ? k->marker : NULL)) {
348
349                         log_info("Walked on cycle path to %s/%s", k->unit->id, job_type_to_string(k->type));
350
351                         if (!delete &&
352                             !k->installed &&
353                             !unit_matters_to_anchor(k->unit, k)) {
354                                 /* Ok, we can drop this one, so let's
355                                  * do so. */
356                                 delete = k;
357                         }
358
359                         /* Check if this in fact was the beginning of
360                          * the cycle */
361                         if (k == j)
362                                 break;
363                 }
364
365
366                 if (delete) {
367                         log_warning("Breaking ordering cycle by deleting job %s/%s", delete->unit->id, job_type_to_string(delete->type));
368                         transaction_delete_unit(tr, delete->unit);
369                         return -EAGAIN;
370                 }
371
372                 log_error("Unable to break cycle");
373
374                 dbus_set_error(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC, "Transaction order is cyclic. See system logs for details.");
375                 return -ENOEXEC;
376         }
377
378         /* Make the marker point to where we come from, so that we can
379          * find our way backwards if we want to break a cycle. We use
380          * a special marker for the beginning: we point to
381          * ourselves. */
382         j->marker = from ? from : j;
383         j->generation = generation;
384
385         /* We assume that the the dependencies are bidirectional, and
386          * hence can ignore UNIT_AFTER */
387         SET_FOREACH(u, j->unit->dependencies[UNIT_BEFORE], i) {
388                 Job *o;
389
390                 /* Is there a job for this unit? */
391                 o = hashmap_get(tr->jobs, u);
392                 if (!o) {
393                         /* Ok, there is no job for this in the
394                          * transaction, but maybe there is already one
395                          * running? */
396                         o = u->job;
397                         if (!o)
398                                 continue;
399                 }
400
401                 r = transaction_verify_order_one(tr, o, j, generation, e);
402                 if (r < 0)
403                         return r;
404         }
405
406         /* Ok, let's backtrack, and remember that this entry is not on
407          * our path anymore. */
408         j->marker = NULL;
409
410         return 0;
411 }
412
413 static int transaction_verify_order(Transaction *tr, unsigned *generation, DBusError *e) {
414         Job *j;
415         int r;
416         Iterator i;
417         unsigned g;
418
419         assert(tr);
420         assert(generation);
421
422         /* Check if the ordering graph is cyclic. If it is, try to fix
423          * that up by dropping one of the jobs. */
424
425         g = (*generation)++;
426
427         HASHMAP_FOREACH(j, tr->jobs, i)
428                 if ((r = transaction_verify_order_one(tr, j, NULL, g, e)) < 0)
429                         return r;
430
431         return 0;
432 }
433
434 static void transaction_collect_garbage(Transaction *tr) {
435         bool again;
436
437         assert(tr);
438
439         /* Drop jobs that are not required by any other job */
440
441         do {
442                 Iterator i;
443                 Job *j;
444
445                 again = false;
446
447                 HASHMAP_FOREACH(j, tr->jobs, i) {
448                         if (tr->anchor_job == j || j->object_list) {
449                                 /* log_debug("Keeping job %s/%s because of %s/%s", */
450                                 /*           j->unit->id, job_type_to_string(j->type), */
451                                 /*           j->object_list->subject ? j->object_list->subject->unit->id : "root", */
452                                 /*           j->object_list->subject ? job_type_to_string(j->object_list->subject->type) : "root"); */
453                                 continue;
454                         }
455
456                         /* log_debug("Garbage collecting job %s/%s", j->unit->id, job_type_to_string(j->type)); */
457                         transaction_delete_job(tr, j, true);
458                         again = true;
459                         break;
460                 }
461
462         } while (again);
463 }
464
465 static int transaction_is_destructive(Transaction *tr, DBusError *e) {
466         Iterator i;
467         Job *j;
468
469         assert(tr);
470
471         /* Checks whether applying this transaction means that
472          * existing jobs would be replaced */
473
474         HASHMAP_FOREACH(j, tr->jobs, i) {
475
476                 /* Assume merged */
477                 assert(!j->transaction_prev);
478                 assert(!j->transaction_next);
479
480                 if (j->unit->job &&
481                     j->unit->job != j &&
482                     !job_type_is_superset(j->type, j->unit->job->type)) {
483
484                         dbus_set_error(e, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE, "Transaction is destructive.");
485                         return -EEXIST;
486                 }
487         }
488
489         return 0;
490 }
491
492 static void transaction_minimize_impact(Transaction *tr) {
493         bool again;
494         assert(tr);
495
496         /* Drops all unnecessary jobs that reverse already active jobs
497          * or that stop a running service. */
498
499         do {
500                 Job *j;
501                 Iterator i;
502
503                 again = false;
504
505                 HASHMAP_FOREACH(j, tr->jobs, i) {
506                         LIST_FOREACH(transaction, j, j) {
507                                 bool stops_running_service, changes_existing_job;
508
509                                 /* If it matters, we shouldn't drop it */
510                                 if (j->matters_to_anchor)
511                                         continue;
512
513                                 /* Would this stop a running service?
514                                  * Would this change an existing job?
515                                  * If so, let's drop this entry */
516
517                                 stops_running_service =
518                                         j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
519
520                                 changes_existing_job =
521                                         j->unit->job &&
522                                         job_type_is_conflicting(j->type, j->unit->job->type);
523
524                                 if (!stops_running_service && !changes_existing_job)
525                                         continue;
526
527                                 if (stops_running_service)
528                                         log_debug("%s/%s would stop a running service.", j->unit->id, job_type_to_string(j->type));
529
530                                 if (changes_existing_job)
531                                         log_debug("%s/%s would change existing job.", j->unit->id, job_type_to_string(j->type));
532
533                                 /* Ok, let's get rid of this */
534                                 log_debug("Deleting %s/%s to minimize impact.", j->unit->id, job_type_to_string(j->type));
535
536                                 transaction_delete_job(tr, j, true);
537                                 again = true;
538                                 break;
539                         }
540
541                         if (again)
542                                 break;
543                 }
544
545         } while (again);
546 }
547
548 static int transaction_apply(Transaction *tr, Manager *m, JobMode mode) {
549         Iterator i;
550         Job *j;
551         int r;
552
553         /* Moves the transaction jobs to the set of active jobs */
554
555         if (mode == JOB_ISOLATE) {
556
557                 /* When isolating first kill all installed jobs which
558                  * aren't part of the new transaction */
559         rescan:
560                 HASHMAP_FOREACH(j, m->jobs, i) {
561                         assert(j->installed);
562
563                         if (hashmap_get(tr->jobs, j->unit))
564                                 continue;
565
566                         /* 'j' itself is safe to remove, but if other jobs
567                            are invalidated recursively, our iterator may become
568                            invalid and we need to start over. */
569                         if (job_finish_and_invalidate(j, JOB_CANCELED) > 0)
570                                 goto rescan;
571                 }
572         }
573
574         HASHMAP_FOREACH(j, tr->jobs, i) {
575                 /* Assume merged */
576                 assert(!j->transaction_prev);
577                 assert(!j->transaction_next);
578
579                 if (j->installed)
580                         continue;
581
582                 r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j);
583                 if (r < 0)
584                         goto rollback;
585         }
586
587         while ((j = hashmap_steal_first(tr->jobs))) {
588                 Job *installed_job;
589
590                 if (j->installed) {
591                         /* log_debug("Skipping already installed job %s/%s as %u", j->unit->id, job_type_to_string(j->type), (unsigned) j->id); */
592                         continue;
593                 }
594
595                 /* Clean the job dependencies */
596                 transaction_unlink_job(tr, j, false);
597
598                 installed_job = job_install(j);
599                 if (installed_job != j) {
600                         /* j has been merged into a previously installed job */
601                         if (tr->anchor_job == j)
602                                 tr->anchor_job = installed_job;
603                         hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
604                         job_free(j);
605                         j = installed_job;
606                 }
607
608                 job_add_to_run_queue(j);
609                 job_add_to_dbus_queue(j);
610                 job_start_timer(j);
611         }
612
613         return 0;
614
615 rollback:
616
617         HASHMAP_FOREACH(j, tr->jobs, i) {
618                 if (j->installed)
619                         continue;
620
621                 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
622         }
623
624         return r;
625 }
626
627 int transaction_activate(Transaction *tr, Manager *m, JobMode mode, DBusError *e) {
628         int r;
629         unsigned generation = 1;
630
631         assert(tr);
632
633         /* This applies the changes recorded in tr->jobs to
634          * the actual list of jobs, if possible. */
635
636         /* First step: figure out which jobs matter */
637         transaction_find_jobs_that_matter_to_anchor(tr->anchor_job, generation++);
638
639         /* Second step: Try not to stop any running services if
640          * we don't have to. Don't try to reverse running
641          * jobs if we don't have to. */
642         if (mode == JOB_FAIL)
643                 transaction_minimize_impact(tr);
644
645         /* Third step: Drop redundant jobs */
646         transaction_drop_redundant(tr);
647
648         for (;;) {
649                 /* Fourth step: Let's remove unneeded jobs that might
650                  * be lurking. */
651                 if (mode != JOB_ISOLATE)
652                         transaction_collect_garbage(tr);
653
654                 /* Fifth step: verify order makes sense and correct
655                  * cycles if necessary and possible */
656                 r = transaction_verify_order(tr, &generation, e);
657                 if (r >= 0)
658                         break;
659
660                 if (r != -EAGAIN) {
661                         log_warning("Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error(e, r));
662                         return r;
663                 }
664
665                 /* Let's see if the resulting transaction ordering
666                  * graph is still cyclic... */
667         }
668
669         for (;;) {
670                 /* Sixth step: let's drop unmergeable entries if
671                  * necessary and possible, merge entries we can
672                  * merge */
673                 r = transaction_merge_jobs(tr, e);
674                 if (r >= 0)
675                         break;
676
677                 if (r != -EAGAIN) {
678                         log_warning("Requested transaction contains unmergeable jobs: %s", bus_error(e, r));
679                         return r;
680                 }
681
682                 /* Seventh step: an entry got dropped, let's garbage
683                  * collect its dependencies. */
684                 if (mode != JOB_ISOLATE)
685                         transaction_collect_garbage(tr);
686
687                 /* Let's see if the resulting transaction still has
688                  * unmergeable entries ... */
689         }
690
691         /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
692         transaction_drop_redundant(tr);
693
694         /* Ninth step: check whether we can actually apply this */
695         if (mode == JOB_FAIL) {
696                 r = transaction_is_destructive(tr, e);
697                 if (r < 0) {
698                         log_notice("Requested transaction contradicts existing jobs: %s", bus_error(e, r));
699                         return r;
700                 }
701         }
702
703         /* Tenth step: apply changes */
704         r = transaction_apply(tr, m, mode);
705         if (r < 0) {
706                 log_warning("Failed to apply transaction: %s", strerror(-r));
707                 return r;
708         }
709
710         assert(hashmap_isempty(tr->jobs));
711
712         return 0;
713 }
714
715 static Job* transaction_add_one_job(Transaction *tr, JobType type, Unit *unit, bool override, bool *is_new) {
716         Job *j, *f;
717
718         assert(tr);
719         assert(unit);
720
721         /* Looks for an existing prospective job and returns that. If
722          * it doesn't exist it is created and added to the prospective
723          * jobs list. */
724
725         f = hashmap_get(tr->jobs, unit);
726
727         LIST_FOREACH(transaction, j, f) {
728                 assert(j->unit == unit);
729
730                 if (j->type == type) {
731                         if (is_new)
732                                 *is_new = false;
733                         return j;
734                 }
735         }
736
737         j = job_new(unit, type);
738         if (!j)
739                 return NULL;
740
741         j->generation = 0;
742         j->marker = NULL;
743         j->matters_to_anchor = false;
744         j->override = override;
745
746         LIST_PREPEND(Job, transaction, f, j);
747
748         if (hashmap_replace(tr->jobs, unit, f) < 0) {
749                 LIST_REMOVE(Job, transaction, f, j);
750                 job_free(j);
751                 return NULL;
752         }
753
754         if (is_new)
755                 *is_new = true;
756
757         /* log_debug("Added job %s/%s to transaction.", unit->id, job_type_to_string(type)); */
758
759         return j;
760 }
761
762 static void transaction_unlink_job(Transaction *tr, Job *j, bool delete_dependencies) {
763         assert(tr);
764         assert(j);
765
766         if (j->transaction_prev)
767                 j->transaction_prev->transaction_next = j->transaction_next;
768         else if (j->transaction_next)
769                 hashmap_replace(tr->jobs, j->unit, j->transaction_next);
770         else
771                 hashmap_remove_value(tr->jobs, j->unit, j);
772
773         if (j->transaction_next)
774                 j->transaction_next->transaction_prev = j->transaction_prev;
775
776         j->transaction_prev = j->transaction_next = NULL;
777
778         while (j->subject_list)
779                 job_dependency_free(j->subject_list);
780
781         while (j->object_list) {
782                 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
783
784                 job_dependency_free(j->object_list);
785
786                 if (other && delete_dependencies) {
787                         log_debug("Deleting job %s/%s as dependency of job %s/%s",
788                                   other->unit->id, job_type_to_string(other->type),
789                                   j->unit->id, job_type_to_string(j->type));
790                         transaction_delete_job(tr, other, delete_dependencies);
791                 }
792         }
793 }
794
795 int transaction_add_job_and_dependencies(
796                 Transaction *tr,
797                 JobType type,
798                 Unit *unit,
799                 Job *by,
800                 bool matters,
801                 bool override,
802                 bool conflicts,
803                 bool ignore_requirements,
804                 bool ignore_order,
805                 DBusError *e) {
806         Job *ret;
807         Iterator i;
808         Unit *dep;
809         int r;
810         bool is_new;
811
812         assert(tr);
813         assert(type < _JOB_TYPE_MAX);
814         assert(unit);
815
816         /* log_debug("Pulling in %s/%s from %s/%s", */
817         /*           unit->id, job_type_to_string(type), */
818         /*           by ? by->unit->id : "NA", */
819         /*           by ? job_type_to_string(by->type) : "NA"); */
820
821         if (unit->load_state != UNIT_LOADED &&
822             unit->load_state != UNIT_ERROR &&
823             unit->load_state != UNIT_MASKED) {
824                 dbus_set_error(e, BUS_ERROR_LOAD_FAILED, "Unit %s is not loaded properly.", unit->id);
825                 return -EINVAL;
826         }
827
828         if (type != JOB_STOP && unit->load_state == UNIT_ERROR) {
829                 dbus_set_error(e, BUS_ERROR_LOAD_FAILED,
830                                "Unit %s failed to load: %s. "
831                                "See system logs and 'systemctl status %s' for details.",
832                                unit->id,
833                                strerror(-unit->load_error),
834                                unit->id);
835                 return -EINVAL;
836         }
837
838         if (type != JOB_STOP && unit->load_state == UNIT_MASKED) {
839                 dbus_set_error(e, BUS_ERROR_MASKED, "Unit %s is masked.", unit->id);
840                 return -EINVAL;
841         }
842
843         if (!unit_job_is_applicable(unit, type)) {
844                 dbus_set_error(e, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE, "Job type %s is not applicable for unit %s.", job_type_to_string(type), unit->id);
845                 return -EBADR;
846         }
847
848         /* First add the job. */
849         ret = transaction_add_one_job(tr, type, unit, override, &is_new);
850         if (!ret)
851                 return -ENOMEM;
852
853         ret->ignore_order = ret->ignore_order || ignore_order;
854
855         /* Then, add a link to the job. */
856         if (by) {
857                 if (!job_dependency_new(by, ret, matters, conflicts))
858                         return -ENOMEM;
859         } else {
860                 /* If the job has no parent job, it is the anchor job. */
861                 assert(!tr->anchor_job);
862                 tr->anchor_job = ret;
863         }
864         if (is_new && !ignore_requirements) {
865                 Set *following;
866
867                 /* If we are following some other unit, make sure we
868                  * add all dependencies of everybody following. */
869                 if (unit_following_set(ret->unit, &following) > 0) {
870                         SET_FOREACH(dep, following, i) {
871                                 r = transaction_add_job_and_dependencies(tr, type, dep, ret, false, override, false, false, ignore_order, e);
872                                 if (r < 0) {
873                                         log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
874
875                                         if (e)
876                                                 dbus_error_free(e);
877                                 }
878                         }
879
880                         set_free(following);
881                 }
882
883                 /* Finally, recursively add in all dependencies. */
884                 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
885                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRES], i) {
886                                 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, true, override, false, false, ignore_order, e);
887                                 if (r < 0) {
888                                         if (r != -EBADR)
889                                                 goto fail;
890
891                                         if (e)
892                                                 dbus_error_free(e);
893                                 }
894                         }
895
896                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_BIND_TO], i) {
897                                 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, true, override, false, false, ignore_order, e);
898                                 if (r < 0) {
899                                         if (r != -EBADR)
900                                                 goto fail;
901
902                                         if (e)
903                                                 dbus_error_free(e);
904                                 }
905                         }
906
907                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRES_OVERRIDABLE], i) {
908                                 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, !override, override, false, false, ignore_order, e);
909                                 if (r < 0) {
910                                         log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
911
912                                         if (e)
913                                                 dbus_error_free(e);
914                                 }
915                         }
916
917                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_WANTS], i) {
918                                 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, false, false, false, false, ignore_order, e);
919                                 if (r < 0) {
920                                         log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
921
922                                         if (e)
923                                                 dbus_error_free(e);
924                                 }
925                         }
926
927                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUISITE], i) {
928                                 r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, ret, true, override, false, false, ignore_order, e);
929                                 if (r < 0) {
930                                         if (r != -EBADR)
931                                                 goto fail;
932
933                                         if (e)
934                                                 dbus_error_free(e);
935                                 }
936                         }
937
938                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUISITE_OVERRIDABLE], i) {
939                                 r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, ret, !override, override, false, false, ignore_order, e);
940                                 if (r < 0) {
941                                         log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
942
943                                         if (e)
944                                                 dbus_error_free(e);
945                                 }
946                         }
947
948                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_CONFLICTS], i) {
949                                 r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, true, override, true, false, ignore_order, e);
950                                 if (r < 0) {
951                                         if (r != -EBADR)
952                                                 goto fail;
953
954                                         if (e)
955                                                 dbus_error_free(e);
956                                 }
957                         }
958
959                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_CONFLICTED_BY], i) {
960                                 r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, false, override, false, false, ignore_order, e);
961                                 if (r < 0) {
962                                         log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
963
964                                         if (e)
965                                                 dbus_error_free(e);
966                                 }
967                         }
968
969                 }
970
971                 if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
972
973                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRED_BY], i) {
974                                 r = transaction_add_job_and_dependencies(tr, type, dep, ret, true, override, false, false, ignore_order, e);
975                                 if (r < 0) {
976                                         if (r != -EBADR)
977                                                 goto fail;
978
979                                         if (e)
980                                                 dbus_error_free(e);
981                                 }
982                         }
983
984                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_BOUND_BY], i) {
985                                 r = transaction_add_job_and_dependencies(tr, type, dep, ret, true, override, false, false, ignore_order, e);
986                                 if (r < 0) {
987                                         if (r != -EBADR)
988                                                 goto fail;
989
990                                         if (e)
991                                                 dbus_error_free(e);
992                                 }
993                         }
994                 }
995
996                 if (type == JOB_RELOAD || type == JOB_RELOAD_OR_START) {
997
998                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_PROPAGATE_RELOAD_TO], i) {
999                                 r = transaction_add_job_and_dependencies(tr, JOB_RELOAD, dep, ret, false, override, false, false, ignore_order, e);
1000                                 if (r < 0) {
1001                                         log_warning("Cannot add dependency reload job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
1002
1003                                         if (e)
1004                                                 dbus_error_free(e);
1005                                 }
1006                         }
1007                 }
1008
1009                 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
1010         }
1011
1012         return 0;
1013
1014 fail:
1015         return r;
1016 }
1017
1018 int transaction_add_isolate_jobs(Transaction *tr, Manager *m) {
1019         Iterator i;
1020         Unit *u;
1021         char *k;
1022         int r;
1023
1024         assert(tr);
1025         assert(m);
1026
1027         HASHMAP_FOREACH_KEY(u, k, m->units, i) {
1028
1029                 /* ignore aliases */
1030                 if (u->id != k)
1031                         continue;
1032
1033                 if (u->ignore_on_isolate)
1034                         continue;
1035
1036                 /* No need to stop inactive jobs */
1037                 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) && !u->job)
1038                         continue;
1039
1040                 /* Is there already something listed for this? */
1041                 if (hashmap_get(tr->jobs, u))
1042                         continue;
1043
1044                 r = transaction_add_job_and_dependencies(tr, JOB_STOP, u, tr->anchor_job, true, false, false, false, false, NULL);
1045                 if (r < 0)
1046                         log_warning("Cannot add isolate job for unit %s, ignoring: %s", u->id, strerror(-r));
1047         }
1048
1049         return 0;
1050 }
1051
1052 Transaction *transaction_new(void) {
1053         Transaction *tr;
1054
1055         tr = new0(Transaction, 1);
1056         if (!tr)
1057                 return NULL;
1058
1059         tr->jobs = hashmap_new(trivial_hash_func, trivial_compare_func);
1060         if (!tr->jobs) {
1061                 free(tr);
1062                 return NULL;
1063         }
1064
1065         return tr;
1066 }
1067
1068 void transaction_free(Transaction *tr) {
1069         assert(hashmap_isempty(tr->jobs));
1070         hashmap_free(tr->jobs);
1071         free(tr);
1072 }