chiark / gitweb /
39cfe54b0a8064b3c7708481e0d86077405c99fd
[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                 /* If an active job is mergeable, merge it too */
247                 if (j->unit->job)
248                         job_type_merge(&t, j->unit->job->type); /* Might fail. Which is OK */
249
250                 while ((k = j->transaction_next)) {
251                         if (j->installed) {
252                                 transaction_merge_and_delete_job(tr, k, j, t);
253                                 j = k;
254                         } else
255                                 transaction_merge_and_delete_job(tr, j, k, t);
256                 }
257
258                 if (j->unit->job && !j->installed)
259                         transaction_merge_and_delete_job(tr, j, j->unit->job, t);
260
261                 assert(!j->transaction_next);
262                 assert(!j->transaction_prev);
263         }
264
265         return 0;
266 }
267
268 static void transaction_drop_redundant(Transaction *tr) {
269         bool again;
270
271         assert(tr);
272
273         /* Goes through the transaction and removes all jobs that are
274          * a noop */
275
276         do {
277                 Job *j;
278                 Iterator i;
279
280                 again = false;
281
282                 HASHMAP_FOREACH(j, tr->jobs, i) {
283                         bool changes_something = false;
284                         Job *k;
285
286                         LIST_FOREACH(transaction, k, j) {
287
288                                 if (tr->anchor_job != k &&
289                                     (k->installed || job_type_is_redundant(k->type, unit_active_state(k->unit))) &&
290                                     (!k->unit->job || !job_type_is_conflicting(k->type, k->unit->job->type)))
291                                         continue;
292
293                                 changes_something = true;
294                                 break;
295                         }
296
297                         if (changes_something)
298                                 continue;
299
300                         /* log_debug("Found redundant job %s/%s, dropping.", j->unit->id, job_type_to_string(j->type)); */
301                         transaction_delete_job(tr, j, false);
302                         again = true;
303                         break;
304                 }
305
306         } while (again);
307 }
308
309 static bool unit_matters_to_anchor(Unit *u, Job *j) {
310         assert(u);
311         assert(!j->transaction_prev);
312
313         /* Checks whether at least one of the jobs for this unit
314          * matters to the anchor. */
315
316         LIST_FOREACH(transaction, j, j)
317                 if (j->matters_to_anchor)
318                         return true;
319
320         return false;
321 }
322
323 static int transaction_verify_order_one(Transaction *tr, Job *j, Job *from, unsigned generation, DBusError *e) {
324         Iterator i;
325         Unit *u;
326         int r;
327
328         assert(tr);
329         assert(j);
330         assert(!j->transaction_prev);
331
332         /* Does a recursive sweep through the ordering graph, looking
333          * for a cycle. If we find cycle we try to break it. */
334
335         /* Have we seen this before? */
336         if (j->generation == generation) {
337                 Job *k, *delete;
338
339                 /* If the marker is NULL we have been here already and
340                  * decided the job was loop-free from here. Hence
341                  * shortcut things and return right-away. */
342                 if (!j->marker)
343                         return 0;
344
345                 /* So, the marker is not NULL and we already have been
346                  * here. We have a cycle. Let's try to break it. We go
347                  * backwards in our path and try to find a suitable
348                  * job to remove. We use the marker to find our way
349                  * back, since smart how we are we stored our way back
350                  * in there. */
351                 log_warning("Found ordering cycle on %s/%s", j->unit->id, job_type_to_string(j->type));
352
353                 delete = NULL;
354                 for (k = from; k; k = ((k->generation == generation && k->marker != k) ? k->marker : NULL)) {
355
356                         log_info("Walked on cycle path to %s/%s", k->unit->id, job_type_to_string(k->type));
357
358                         if (!delete &&
359                             !k->installed &&
360                             !unit_matters_to_anchor(k->unit, k)) {
361                                 /* Ok, we can drop this one, so let's
362                                  * do so. */
363                                 delete = k;
364                         }
365
366                         /* Check if this in fact was the beginning of
367                          * the cycle */
368                         if (k == j)
369                                 break;
370                 }
371
372
373                 if (delete) {
374                         log_warning("Breaking ordering cycle by deleting job %s/%s", delete->unit->id, job_type_to_string(delete->type));
375                         transaction_delete_unit(tr, delete->unit);
376                         return -EAGAIN;
377                 }
378
379                 log_error("Unable to break cycle");
380
381                 dbus_set_error(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC, "Transaction order is cyclic. See system logs for details.");
382                 return -ENOEXEC;
383         }
384
385         /* Make the marker point to where we come from, so that we can
386          * find our way backwards if we want to break a cycle. We use
387          * a special marker for the beginning: we point to
388          * ourselves. */
389         j->marker = from ? from : j;
390         j->generation = generation;
391
392         /* We assume that the the dependencies are bidirectional, and
393          * hence can ignore UNIT_AFTER */
394         SET_FOREACH(u, j->unit->dependencies[UNIT_BEFORE], i) {
395                 Job *o;
396
397                 /* Is there a job for this unit? */
398                 o = hashmap_get(tr->jobs, u);
399                 if (!o) {
400                         /* Ok, there is no job for this in the
401                          * transaction, but maybe there is already one
402                          * running? */
403                         o = u->job;
404                         if (!o)
405                                 continue;
406                 }
407
408                 r = transaction_verify_order_one(tr, o, j, generation, e);
409                 if (r < 0)
410                         return r;
411         }
412
413         /* Ok, let's backtrack, and remember that this entry is not on
414          * our path anymore. */
415         j->marker = NULL;
416
417         return 0;
418 }
419
420 static int transaction_verify_order(Transaction *tr, unsigned *generation, DBusError *e) {
421         Job *j;
422         int r;
423         Iterator i;
424         unsigned g;
425
426         assert(tr);
427         assert(generation);
428
429         /* Check if the ordering graph is cyclic. If it is, try to fix
430          * that up by dropping one of the jobs. */
431
432         g = (*generation)++;
433
434         HASHMAP_FOREACH(j, tr->jobs, i)
435                 if ((r = transaction_verify_order_one(tr, j, NULL, g, e)) < 0)
436                         return r;
437
438         return 0;
439 }
440
441 static void transaction_collect_garbage(Transaction *tr) {
442         bool again;
443
444         assert(tr);
445
446         /* Drop jobs that are not required by any other job */
447
448         do {
449                 Iterator i;
450                 Job *j;
451
452                 again = false;
453
454                 HASHMAP_FOREACH(j, tr->jobs, i) {
455                         if (tr->anchor_job == j || j->object_list) {
456                                 /* log_debug("Keeping job %s/%s because of %s/%s", */
457                                 /*           j->unit->id, job_type_to_string(j->type), */
458                                 /*           j->object_list->subject ? j->object_list->subject->unit->id : "root", */
459                                 /*           j->object_list->subject ? job_type_to_string(j->object_list->subject->type) : "root"); */
460                                 continue;
461                         }
462
463                         /* log_debug("Garbage collecting job %s/%s", j->unit->id, job_type_to_string(j->type)); */
464                         transaction_delete_job(tr, j, true);
465                         again = true;
466                         break;
467                 }
468
469         } while (again);
470 }
471
472 static int transaction_is_destructive(Transaction *tr, DBusError *e) {
473         Iterator i;
474         Job *j;
475
476         assert(tr);
477
478         /* Checks whether applying this transaction means that
479          * existing jobs would be replaced */
480
481         HASHMAP_FOREACH(j, tr->jobs, i) {
482
483                 /* Assume merged */
484                 assert(!j->transaction_prev);
485                 assert(!j->transaction_next);
486
487                 if (j->unit->job &&
488                     j->unit->job != j &&
489                     !job_type_is_superset(j->type, j->unit->job->type)) {
490
491                         dbus_set_error(e, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE, "Transaction is destructive.");
492                         return -EEXIST;
493                 }
494         }
495
496         return 0;
497 }
498
499 static void transaction_minimize_impact(Transaction *tr) {
500         bool again;
501         assert(tr);
502
503         /* Drops all unnecessary jobs that reverse already active jobs
504          * or that stop a running service. */
505
506         do {
507                 Job *j;
508                 Iterator i;
509
510                 again = false;
511
512                 HASHMAP_FOREACH(j, tr->jobs, i) {
513                         LIST_FOREACH(transaction, j, j) {
514                                 bool stops_running_service, changes_existing_job;
515
516                                 /* If it matters, we shouldn't drop it */
517                                 if (j->matters_to_anchor)
518                                         continue;
519
520                                 /* Would this stop a running service?
521                                  * Would this change an existing job?
522                                  * If so, let's drop this entry */
523
524                                 stops_running_service =
525                                         j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
526
527                                 changes_existing_job =
528                                         j->unit->job &&
529                                         job_type_is_conflicting(j->type, j->unit->job->type);
530
531                                 if (!stops_running_service && !changes_existing_job)
532                                         continue;
533
534                                 if (stops_running_service)
535                                         log_debug("%s/%s would stop a running service.", j->unit->id, job_type_to_string(j->type));
536
537                                 if (changes_existing_job)
538                                         log_debug("%s/%s would change existing job.", j->unit->id, job_type_to_string(j->type));
539
540                                 /* Ok, let's get rid of this */
541                                 log_debug("Deleting %s/%s to minimize impact.", j->unit->id, job_type_to_string(j->type));
542
543                                 transaction_delete_job(tr, j, true);
544                                 again = true;
545                                 break;
546                         }
547
548                         if (again)
549                                 break;
550                 }
551
552         } while (again);
553 }
554
555 static int transaction_apply(Transaction *tr, Manager *m, JobMode mode) {
556         Iterator i;
557         Job *j;
558         int r;
559
560         /* Moves the transaction jobs to the set of active jobs */
561
562         if (mode == JOB_ISOLATE) {
563
564                 /* When isolating first kill all installed jobs which
565                  * aren't part of the new transaction */
566         rescan:
567                 HASHMAP_FOREACH(j, m->jobs, i) {
568                         assert(j->installed);
569
570                         if (hashmap_get(tr->jobs, j->unit))
571                                 continue;
572
573                         /* 'j' itself is safe to remove, but if other jobs
574                            are invalidated recursively, our iterator may become
575                            invalid and we need to start over. */
576                         if (job_finish_and_invalidate(j, JOB_CANCELED) > 0)
577                                 goto rescan;
578                 }
579         }
580
581         HASHMAP_FOREACH(j, tr->jobs, i) {
582                 /* Assume merged */
583                 assert(!j->transaction_prev);
584                 assert(!j->transaction_next);
585
586                 if (j->installed)
587                         continue;
588
589                 r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j);
590                 if (r < 0)
591                         goto rollback;
592         }
593
594         while ((j = hashmap_steal_first(tr->jobs))) {
595                 Job *uj;
596                 if (j->installed) {
597                         /* log_debug("Skipping already installed job %s/%s as %u", j->unit->id, job_type_to_string(j->type), (unsigned) j->id); */
598                         continue;
599                 }
600
601                 uj = j->unit->job;
602                 if (uj) {
603                         job_uninstall(uj);
604                         job_free(uj);
605                 }
606
607                 j->unit->job = j;
608                 j->installed = true;
609                 m->n_installed_jobs ++;
610
611                 /* We're fully installed. Now let's free data we don't
612                  * need anymore. */
613
614                 assert(!j->transaction_next);
615                 assert(!j->transaction_prev);
616
617                 /* Clean the job dependencies */
618                 transaction_unlink_job(tr, j, false);
619
620                 job_add_to_run_queue(j);
621                 job_add_to_dbus_queue(j);
622                 job_start_timer(j);
623
624                 log_debug("Installed new job %s/%s as %u", j->unit->id, job_type_to_string(j->type), (unsigned) j->id);
625         }
626
627         return 0;
628
629 rollback:
630
631         HASHMAP_FOREACH(j, tr->jobs, i) {
632                 if (j->installed)
633                         continue;
634
635                 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
636         }
637
638         return r;
639 }
640
641 int transaction_activate(Transaction *tr, Manager *m, JobMode mode, DBusError *e) {
642         int r;
643         unsigned generation = 1;
644
645         assert(tr);
646
647         /* This applies the changes recorded in tr->jobs to
648          * the actual list of jobs, if possible. */
649
650         /* First step: figure out which jobs matter */
651         transaction_find_jobs_that_matter_to_anchor(tr->anchor_job, generation++);
652
653         /* Second step: Try not to stop any running services if
654          * we don't have to. Don't try to reverse running
655          * jobs if we don't have to. */
656         if (mode == JOB_FAIL)
657                 transaction_minimize_impact(tr);
658
659         /* Third step: Drop redundant jobs */
660         transaction_drop_redundant(tr);
661
662         for (;;) {
663                 /* Fourth step: Let's remove unneeded jobs that might
664                  * be lurking. */
665                 if (mode != JOB_ISOLATE)
666                         transaction_collect_garbage(tr);
667
668                 /* Fifth step: verify order makes sense and correct
669                  * cycles if necessary and possible */
670                 r = transaction_verify_order(tr, &generation, e);
671                 if (r >= 0)
672                         break;
673
674                 if (r != -EAGAIN) {
675                         log_warning("Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error(e, r));
676                         return r;
677                 }
678
679                 /* Let's see if the resulting transaction ordering
680                  * graph is still cyclic... */
681         }
682
683         for (;;) {
684                 /* Sixth step: let's drop unmergeable entries if
685                  * necessary and possible, merge entries we can
686                  * merge */
687                 r = transaction_merge_jobs(tr, e);
688                 if (r >= 0)
689                         break;
690
691                 if (r != -EAGAIN) {
692                         log_warning("Requested transaction contains unmergeable jobs: %s", bus_error(e, r));
693                         return r;
694                 }
695
696                 /* Seventh step: an entry got dropped, let's garbage
697                  * collect its dependencies. */
698                 if (mode != JOB_ISOLATE)
699                         transaction_collect_garbage(tr);
700
701                 /* Let's see if the resulting transaction still has
702                  * unmergeable entries ... */
703         }
704
705         /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
706         transaction_drop_redundant(tr);
707
708         /* Ninth step: check whether we can actually apply this */
709         if (mode == JOB_FAIL) {
710                 r = transaction_is_destructive(tr, e);
711                 if (r < 0) {
712                         log_notice("Requested transaction contradicts existing jobs: %s", bus_error(e, r));
713                         return r;
714                 }
715         }
716
717         /* Tenth step: apply changes */
718         r = transaction_apply(tr, m, mode);
719         if (r < 0) {
720                 log_warning("Failed to apply transaction: %s", strerror(-r));
721                 return r;
722         }
723
724         assert(hashmap_isempty(tr->jobs));
725
726         return 0;
727 }
728
729 static Job* transaction_add_one_job(Transaction *tr, JobType type, Unit *unit, bool override, bool *is_new) {
730         Job *j, *f;
731
732         assert(tr);
733         assert(unit);
734
735         /* Looks for an existing prospective job and returns that. If
736          * it doesn't exist it is created and added to the prospective
737          * jobs list. */
738
739         f = hashmap_get(tr->jobs, unit);
740
741         LIST_FOREACH(transaction, j, f) {
742                 assert(j->unit == unit);
743
744                 if (j->type == type) {
745                         if (is_new)
746                                 *is_new = false;
747                         return j;
748                 }
749         }
750
751         j = job_new(unit, type);
752         if (!j)
753                 return NULL;
754
755         j->generation = 0;
756         j->marker = NULL;
757         j->matters_to_anchor = false;
758         j->override = override;
759
760         LIST_PREPEND(Job, transaction, f, j);
761
762         if (hashmap_replace(tr->jobs, unit, f) < 0) {
763                 LIST_REMOVE(Job, transaction, f, j);
764                 job_free(j);
765                 return NULL;
766         }
767
768         if (is_new)
769                 *is_new = true;
770
771         /* log_debug("Added job %s/%s to transaction.", unit->id, job_type_to_string(type)); */
772
773         return j;
774 }
775
776 static void transaction_unlink_job(Transaction *tr, Job *j, bool delete_dependencies) {
777         assert(tr);
778         assert(j);
779
780         if (j->transaction_prev)
781                 j->transaction_prev->transaction_next = j->transaction_next;
782         else if (j->transaction_next)
783                 hashmap_replace(tr->jobs, j->unit, j->transaction_next);
784         else
785                 hashmap_remove_value(tr->jobs, j->unit, j);
786
787         if (j->transaction_next)
788                 j->transaction_next->transaction_prev = j->transaction_prev;
789
790         j->transaction_prev = j->transaction_next = NULL;
791
792         while (j->subject_list)
793                 job_dependency_free(j->subject_list);
794
795         while (j->object_list) {
796                 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
797
798                 job_dependency_free(j->object_list);
799
800                 if (other && delete_dependencies) {
801                         log_debug("Deleting job %s/%s as dependency of job %s/%s",
802                                   other->unit->id, job_type_to_string(other->type),
803                                   j->unit->id, job_type_to_string(j->type));
804                         transaction_delete_job(tr, other, delete_dependencies);
805                 }
806         }
807 }
808
809 int transaction_add_job_and_dependencies(
810                 Transaction *tr,
811                 JobType type,
812                 Unit *unit,
813                 Job *by,
814                 bool matters,
815                 bool override,
816                 bool conflicts,
817                 bool ignore_requirements,
818                 bool ignore_order,
819                 DBusError *e) {
820         Job *ret;
821         Iterator i;
822         Unit *dep;
823         int r;
824         bool is_new;
825
826         assert(tr);
827         assert(type < _JOB_TYPE_MAX);
828         assert(unit);
829
830         /* log_debug("Pulling in %s/%s from %s/%s", */
831         /*           unit->id, job_type_to_string(type), */
832         /*           by ? by->unit->id : "NA", */
833         /*           by ? job_type_to_string(by->type) : "NA"); */
834
835         if (unit->load_state != UNIT_LOADED &&
836             unit->load_state != UNIT_ERROR &&
837             unit->load_state != UNIT_MASKED) {
838                 dbus_set_error(e, BUS_ERROR_LOAD_FAILED, "Unit %s is not loaded properly.", unit->id);
839                 return -EINVAL;
840         }
841
842         if (type != JOB_STOP && unit->load_state == UNIT_ERROR) {
843                 dbus_set_error(e, BUS_ERROR_LOAD_FAILED,
844                                "Unit %s failed to load: %s. "
845                                "See system logs and 'systemctl status %s' for details.",
846                                unit->id,
847                                strerror(-unit->load_error),
848                                unit->id);
849                 return -EINVAL;
850         }
851
852         if (type != JOB_STOP && unit->load_state == UNIT_MASKED) {
853                 dbus_set_error(e, BUS_ERROR_MASKED, "Unit %s is masked.", unit->id);
854                 return -EINVAL;
855         }
856
857         if (!unit_job_is_applicable(unit, type)) {
858                 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);
859                 return -EBADR;
860         }
861
862         /* First add the job. */
863         ret = transaction_add_one_job(tr, type, unit, override, &is_new);
864         if (!ret)
865                 return -ENOMEM;
866
867         ret->ignore_order = ret->ignore_order || ignore_order;
868
869         /* Then, add a link to the job. */
870         if (by) {
871                 if (!job_dependency_new(by, ret, matters, conflicts))
872                         return -ENOMEM;
873         } else {
874                 /* If the job has no parent job, it is the anchor job. */
875                 assert(!tr->anchor_job);
876                 tr->anchor_job = ret;
877         }
878         if (is_new && !ignore_requirements) {
879                 Set *following;
880
881                 /* If we are following some other unit, make sure we
882                  * add all dependencies of everybody following. */
883                 if (unit_following_set(ret->unit, &following) > 0) {
884                         SET_FOREACH(dep, following, i) {
885                                 r = transaction_add_job_and_dependencies(tr, type, dep, ret, false, override, false, false, ignore_order, e);
886                                 if (r < 0) {
887                                         log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
888
889                                         if (e)
890                                                 dbus_error_free(e);
891                                 }
892                         }
893
894                         set_free(following);
895                 }
896
897                 /* Finally, recursively add in all dependencies. */
898                 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
899                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRES], i) {
900                                 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, true, override, false, false, ignore_order, e);
901                                 if (r < 0) {
902                                         if (r != -EBADR)
903                                                 goto fail;
904
905                                         if (e)
906                                                 dbus_error_free(e);
907                                 }
908                         }
909
910                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_BIND_TO], i) {
911                                 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, true, override, false, false, ignore_order, e);
912                                 if (r < 0) {
913                                         if (r != -EBADR)
914                                                 goto fail;
915
916                                         if (e)
917                                                 dbus_error_free(e);
918                                 }
919                         }
920
921                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRES_OVERRIDABLE], i) {
922                                 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, !override, override, false, false, ignore_order, e);
923                                 if (r < 0) {
924                                         log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
925
926                                         if (e)
927                                                 dbus_error_free(e);
928                                 }
929                         }
930
931                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_WANTS], i) {
932                                 r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, false, false, false, false, ignore_order, e);
933                                 if (r < 0) {
934                                         log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
935
936                                         if (e)
937                                                 dbus_error_free(e);
938                                 }
939                         }
940
941                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUISITE], i) {
942                                 r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, ret, true, override, false, false, ignore_order, e);
943                                 if (r < 0) {
944                                         if (r != -EBADR)
945                                                 goto fail;
946
947                                         if (e)
948                                                 dbus_error_free(e);
949                                 }
950                         }
951
952                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUISITE_OVERRIDABLE], i) {
953                                 r = transaction_add_job_and_dependencies(tr, JOB_VERIFY_ACTIVE, dep, ret, !override, override, false, false, ignore_order, e);
954                                 if (r < 0) {
955                                         log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
956
957                                         if (e)
958                                                 dbus_error_free(e);
959                                 }
960                         }
961
962                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_CONFLICTS], i) {
963                                 r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, true, override, true, false, ignore_order, e);
964                                 if (r < 0) {
965                                         if (r != -EBADR)
966                                                 goto fail;
967
968                                         if (e)
969                                                 dbus_error_free(e);
970                                 }
971                         }
972
973                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_CONFLICTED_BY], i) {
974                                 r = transaction_add_job_and_dependencies(tr, JOB_STOP, dep, ret, false, override, false, false, ignore_order, e);
975                                 if (r < 0) {
976                                         log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
977
978                                         if (e)
979                                                 dbus_error_free(e);
980                                 }
981                         }
982
983                 }
984
985                 if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
986
987                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRED_BY], i) {
988                                 r = transaction_add_job_and_dependencies(tr, type, dep, ret, true, override, false, false, ignore_order, e);
989                                 if (r < 0) {
990                                         if (r != -EBADR)
991                                                 goto fail;
992
993                                         if (e)
994                                                 dbus_error_free(e);
995                                 }
996                         }
997
998                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_BOUND_BY], i) {
999                                 r = transaction_add_job_and_dependencies(tr, type, dep, ret, true, override, false, false, ignore_order, e);
1000                                 if (r < 0) {
1001                                         if (r != -EBADR)
1002                                                 goto fail;
1003
1004                                         if (e)
1005                                                 dbus_error_free(e);
1006                                 }
1007                         }
1008                 }
1009
1010                 if (type == JOB_RELOAD || type == JOB_RELOAD_OR_START) {
1011
1012                         SET_FOREACH(dep, ret->unit->dependencies[UNIT_PROPAGATE_RELOAD_TO], i) {
1013                                 r = transaction_add_job_and_dependencies(tr, JOB_RELOAD, dep, ret, false, override, false, false, ignore_order, e);
1014                                 if (r < 0) {
1015                                         log_warning("Cannot add dependency reload job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
1016
1017                                         if (e)
1018                                                 dbus_error_free(e);
1019                                 }
1020                         }
1021                 }
1022
1023                 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
1024         }
1025
1026         return 0;
1027
1028 fail:
1029         return r;
1030 }
1031
1032 int transaction_add_isolate_jobs(Transaction *tr, Manager *m) {
1033         Iterator i;
1034         Unit *u;
1035         char *k;
1036         int r;
1037
1038         assert(tr);
1039         assert(m);
1040
1041         HASHMAP_FOREACH_KEY(u, k, m->units, i) {
1042
1043                 /* ignore aliases */
1044                 if (u->id != k)
1045                         continue;
1046
1047                 if (u->ignore_on_isolate)
1048                         continue;
1049
1050                 /* No need to stop inactive jobs */
1051                 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) && !u->job)
1052                         continue;
1053
1054                 /* Is there already something listed for this? */
1055                 if (hashmap_get(tr->jobs, u))
1056                         continue;
1057
1058                 r = transaction_add_job_and_dependencies(tr, JOB_STOP, u, tr->anchor_job, true, false, false, false, false, NULL);
1059                 if (r < 0)
1060                         log_warning("Cannot add isolate job for unit %s, ignoring: %s", u->id, strerror(-r));
1061         }
1062
1063         return 0;
1064 }
1065
1066 Transaction *transaction_new(void) {
1067         Transaction *tr;
1068
1069         tr = new0(Transaction, 1);
1070         if (!tr)
1071                 return NULL;
1072
1073         tr->jobs = hashmap_new(trivial_hash_func, trivial_compare_func);
1074         if (!tr->jobs) {
1075                 free(tr);
1076                 return NULL;
1077         }
1078
1079         return tr;
1080 }
1081
1082 void transaction_free(Transaction *tr) {
1083         assert(hashmap_isempty(tr->jobs));
1084         hashmap_free(tr->jobs);
1085         free(tr);
1086 }