chiark / gitweb /
fix job merging
[elogind.git] / manager.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #include <assert.h>
4 #include <errno.h>
5 #include <string.h>
6
7 #include "manager.h"
8 #include "hashmap.h"
9 #include "macro.h"
10 #include "strv.h"
11 #include "log.h"
12
13 Manager* manager_new(void) {
14         Manager *m;
15
16         if (!(m = new0(Manager, 1)))
17                 return NULL;
18
19         if (!(m->names = hashmap_new(string_hash_func, string_compare_func)))
20                 goto fail;
21
22         if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
23                 goto fail;
24
25         if (!(m->transaction_jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
26                 goto fail;
27
28         return m;
29
30 fail:
31         manager_free(m);
32         return NULL;
33 }
34
35 void manager_free(Manager *m) {
36         Name *n;
37         Job *j;
38
39         assert(m);
40
41         while ((n = hashmap_first(m->names)))
42                 name_free(n);
43
44         while ((j = hashmap_steal_first(m->transaction_jobs)))
45                 job_free(j);
46
47         hashmap_free(m->names);
48         hashmap_free(m->jobs);
49         hashmap_free(m->transaction_jobs);
50
51         free(m);
52 }
53
54 static void transaction_delete_job(Manager *m, Job *j) {
55         assert(m);
56         assert(j);
57
58         /* Deletes one job from the transaction */
59
60         manager_transaction_unlink_job(m, j);
61
62         if (!j->linked)
63                 job_free(j);
64 }
65
66 static void transaction_delete_name(Manager *m, Name *n) {
67         Job *j;
68
69         /* Deletes all jobs associated with a certain name from the
70          * transaction */
71
72         while ((j = hashmap_get(m->transaction_jobs, n)))
73                 transaction_delete_job(m, j);
74 }
75
76 static void transaction_abort(Manager *m) {
77         Job *j;
78
79         assert(m);
80
81         while ((j = hashmap_first(m->transaction_jobs)))
82                 if (j->linked)
83                         transaction_delete_job(m, j);
84                 else
85                         job_free(j);
86
87         assert(hashmap_isempty(m->transaction_jobs));
88         assert(!m->transaction_anchor);
89 }
90
91 static void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) {
92         JobDependency *l;
93
94         assert(m);
95
96         /* A recursive sweep through the graph that marks all names
97          * that matter to the anchor job, i.e. are directly or
98          * indirectly a dependency of the anchor job via paths that
99          * are fully marked as mattering. */
100
101         for (l = j ? j->subject_list : m->transaction_anchor; l; l = l->subject_next) {
102
103                 /* This link does not matter */
104                 if (!l->matters)
105                         continue;
106
107                 /* This name has already been marked */
108                 if (l->object->generation == generation)
109                         continue;
110
111                 l->object->matters_to_anchor = true;
112                 l->object->generation = generation;
113
114                 transaction_find_jobs_that_matter_to_anchor(m, l->object, generation);
115         }
116 }
117
118 static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) {
119         JobDependency *l, *last;
120
121         assert(j);
122         assert(other);
123         assert(j->name == other->name);
124         assert(!j->linked);
125
126         /* Merges 'other' into 'j' and then deletes j. */
127
128         j->type = t;
129         j->state = JOB_WAITING;
130
131         j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
132
133         /* Patch us in as new owner of the JobDependency objects */
134         last = NULL;
135         for (l = other->subject_list; l; l = l->subject_next) {
136                 assert(l->subject == other);
137                 l->subject = j;
138                 last = l;
139         }
140
141         /* Merge both lists */
142         if (last) {
143                 last->subject_next = j->subject_list;
144                 if (j->subject_list)
145                         j->subject_list->subject_prev = last;
146                 j->subject_list = other->subject_list;
147         }
148
149         /* Patch us in as new owner of the JobDependency objects */
150         last = NULL;
151         for (l = other->object_list; l; l = l->object_next) {
152                 assert(l->object == other);
153                 l->object = j;
154                 last = l;
155         }
156
157         /* Merge both lists */
158         if (last) {
159                 last->object_next = j->object_list;
160                 if (j->object_list)
161                         j->object_list->object_prev = last;
162                 j->object_list = other->object_list;
163         }
164
165         /* Kill the other job */
166         other->subject_list = NULL;
167         other->object_list = NULL;
168         transaction_delete_job(m, other);
169 }
170
171 static int delete_one_unmergable_job(Manager *m, Job *j) {
172         Job *k;
173
174         assert(j);
175
176         /* Tries to delete one item in the linked list
177          * j->transaction_next->transaction_next->... that conflicts
178          * whith another one, in an attempt to make an inconsistent
179          * transaction work. */
180
181         /* We rely here on the fact that if a merged with b does not
182          * merge with c, either a or b merge with c neither */
183         for (; j; j = j->transaction_next)
184                 for (k = j->transaction_next; k; k = k->transaction_next) {
185                         Job *d;
186
187                         /* Is this one mergeable? Then skip it */
188                         if (job_type_mergeable(j->type, k->type))
189                                 continue;
190
191                         /* Ok, we found two that conflict, let's see if we can
192                          * drop one of them */
193                         if (!j->matters_to_anchor)
194                                 d = j;
195                         else if (!k->matters_to_anchor)
196                                 d = k;
197                         else
198                                 return -ENOEXEC;
199
200                         /* Ok, we can drop one, so let's do so. */
201                         log_debug("Try to fix job merging by deleting job %s", name_id(d->name));
202                         transaction_delete_job(m, d);
203                         return 0;
204                 }
205
206         return -EINVAL;
207 }
208
209 static int transaction_merge_jobs(Manager *m) {
210         Job *j;
211         void *state;
212         int r;
213
214         assert(m);
215
216         /* First step, check whether any of the jobs for one specific
217          * task conflict. If so, try to drop one of them. */
218         HASHMAP_FOREACH(j, m->transaction_jobs, state) {
219                 JobType t;
220                 Job *k;
221
222                 t = j->type;
223                 for (k = j->transaction_next; k; k = k->transaction_next) {
224                         if ((r = job_type_merge(&t, k->type)) >= 0)
225                                 continue;
226
227                         /* OK, we could not merge all jobs for this
228                          * action. Let's see if we can get rid of one
229                          * of them */
230
231                         if ((r = delete_one_unmergable_job(m, j)) >= 0)
232                                 /* Ok, we managed to drop one, now
233                                  * let's ask our callers to call us
234                                  * again after garbage collecting */
235                                 return -EAGAIN;
236
237                         /* We couldn't merge anything. Failure */
238                         return r;
239                 }
240         }
241
242         /* Second step, merge the jobs. */
243         HASHMAP_FOREACH(j, m->transaction_jobs, state) {
244                 JobType t = j->type;
245                 Job *k;
246
247                 for (k = j->transaction_next; k; k = k->transaction_next)
248                         assert_se(job_type_merge(&t, k->type) == 0);
249
250                 while ((k = j->transaction_next)) {
251                         if (j->linked) {
252                                 transaction_merge_and_delete_job(m, k, j, t);
253                                 j = k;
254                         } else
255                                 transaction_merge_and_delete_job(m, j, k, t);
256                 }
257
258                 assert(!j->transaction_next);
259                 assert(!j->transaction_prev);
260         }
261
262         return 0;
263 }
264
265 static bool name_matters_to_anchor(Name *n, Job *j) {
266         assert(n);
267         assert(!j->transaction_prev);
268
269         /* Checks whether at least one of the jobs for this name
270          * matters to the anchor. */
271
272         for (; j; j = j->transaction_next)
273                 if (j->matters_to_anchor)
274                         return true;
275
276         return false;
277 }
278
279 static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation) {
280         void *state;
281         Name *n;
282         int r;
283
284         assert(m);
285         assert(j);
286         assert(!j->transaction_prev);
287
288         /* Does a recursive sweep through the ordering graph, looking
289          * for a cycle. If we find cycle we try to break it. */
290
291         /* Did we find a cycle? */
292         if (j->marker && j->generation == generation) {
293                 Job *k;
294
295                 /* So, we already have been here. We have a
296                  * cycle. Let's try to break it. We go backwards in
297                  * our path and try to find a suitable job to
298                  * remove. We use the marker to find our way back,
299                  * since smart how we are we stored our way back in
300                  * there. */
301
302                 for (k = from; k; k = (k->generation == generation ? k->marker : NULL)) {
303
304                         if (!k->linked &&
305                             !name_matters_to_anchor(k->name, k)) {
306                                 /* Ok, we can drop this one, so let's
307                                  * do so. */
308                                 log_debug("Breaking order cycle by deleting job %s", name_id(k->name));
309                                 transaction_delete_name(m, k->name);
310                                 return -EAGAIN;
311                         }
312
313                         /* Check if this in fact was the beginning of
314                          * the cycle */
315                         if (k == j)
316                                 break;
317                 }
318
319                 return -ENOEXEC;
320         }
321
322         /* Make the marker point to where we come from, so that we can
323          * find our way backwards if we want to break a cycle */
324         j->marker = from;
325         j->generation = generation;
326
327         /* We assume that the the dependencies are bidirectional, and
328          * hence can ignore NAME_AFTER */
329         SET_FOREACH(n, j->name->meta.dependencies[NAME_BEFORE], state) {
330                 Job *o;
331
332                 /* Is there a job for this name? */
333                 if (!(o = hashmap_get(m->transaction_jobs, n)))
334
335                         /* Ok, there is no job for this in the
336                          * transaction, but maybe there is already one
337                          * running? */
338                         if (!(o = n->meta.job))
339                                 continue;
340
341                 if ((r = transaction_verify_order_one(m, o, j, generation)) < 0)
342                         return r;
343         }
344
345         return 0;
346 }
347
348 static int transaction_verify_order(Manager *m, unsigned *generation) {
349         Job *j;
350         int r;
351         void *state;
352
353         assert(m);
354         assert(generation);
355
356         /* Check if the ordering graph is cyclic. If it is, try to fix
357          * that up by dropping one of the jobs. */
358
359         HASHMAP_FOREACH(j, m->transaction_jobs, state)
360                 if ((r = transaction_verify_order_one(m, j, NULL, (*generation)++)) < 0)
361                         return r;
362
363         return 0;
364 }
365
366 static void transaction_collect_garbage(Manager *m) {
367         bool again;
368
369         assert(m);
370
371         /* Drop jobs that are not required by any other job */
372
373         do {
374                 void *state;
375                 Job *j;
376
377                 again = false;
378
379                 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
380                         if (j->object_list)
381                                 continue;
382
383                         log_debug("Garbage collecting job %s", name_id(j->name));
384
385                         transaction_delete_job(m, j);
386                         again = true;
387                         break;
388                 }
389
390         } while (again);
391 }
392
393 static int transaction_is_destructive(Manager *m, JobMode mode) {
394         void *state;
395         Job *j;
396
397         assert(m);
398
399         /* Checks whether applying this transaction means that
400          * existing jobs would be replaced */
401
402         HASHMAP_FOREACH(j, m->transaction_jobs, state)
403                 if (j->name->meta.job &&
404                     j->name->meta.job != j &&
405                     !job_type_is_superset(j->type, j->name->meta.job->type))
406                         return -EEXIST;
407
408         return 0;
409 }
410
411 static int transaction_apply(Manager *m, JobMode mode) {
412         void *state;
413         Job *j;
414         int r;
415
416         /* Moves the transaction jobs to the set of active jobs */
417
418         HASHMAP_FOREACH(j, m->transaction_jobs, state) {
419                 if (j->linked)
420                         continue;
421
422                 if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
423                         goto rollback;
424         }
425
426         while ((j = hashmap_steal_first(m->transaction_jobs))) {
427                 if (j->linked)
428                         continue;
429
430                 if (j->name->meta.job)
431                         job_free(j->name->meta.job);
432
433                 j->name->meta.job = j;
434                 j->linked = true;
435
436                 /* We're fully installed. Now let's free data we don't
437                  * need anymore. */
438
439                 assert(!j->transaction_next);
440                 assert(!j->transaction_prev);
441
442                 while (j->subject_list)
443                         job_dependency_free(j->subject_list);
444                 while (j->object_list)
445                         job_dependency_free(j->object_list);
446         }
447
448         m->transaction_anchor = NULL;
449
450         return 0;
451
452 rollback:
453
454         HASHMAP_FOREACH(j, m->transaction_jobs, state) {
455                 if (j->linked)
456                         continue;
457
458                 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
459         }
460
461         return r;
462 }
463
464
465 static int transaction_activate(Manager *m, JobMode mode) {
466         int r;
467         unsigned generation = 1;
468
469         assert(m);
470
471         /* This applies the changes recorded in transaction_jobs to
472          * the actual list of jobs, if possible. */
473
474         /* First step: figure out which jobs matter */
475         transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
476
477         for (;;) {
478                 /* Second step: Let's remove unneeded jobs that might
479                  * be lurking. */
480                 transaction_collect_garbage(m);
481
482                 /* Third step: verify order makes sense and correct
483                  * cycles if necessary and possible */
484                 if ((r = transaction_verify_order(m, &generation)) >= 0)
485                         break;
486
487                 if (r != -EAGAIN)
488                         goto rollback;
489
490                 /* Let's see if the resulting transaction ordering
491                  * graph is still cyclic... */
492         }
493
494         for (;;) {
495                 /* Fourth step: let's drop unmergable entries if
496                  * necessary and possible, merge entries we can
497                  * merge */
498                 if ((r = transaction_merge_jobs(m)) >= 0)
499                         break;
500
501                 if (r != -EAGAIN)
502                         goto rollback;
503
504                 /* Fifth step: an entry got dropped, let's garbage
505                  * collect its dependencies. */
506                 transaction_collect_garbage(m);
507
508                 /* Let's see if the resulting transaction still has
509                  * unmergable entries ... */
510         }
511
512         /* Sith step: check whether we can actually apply this */
513         if (mode == JOB_FAIL)
514                 if ((r = transaction_is_destructive(m, mode)) < 0)
515                         goto rollback;
516
517         /* Seventh step: apply changes */
518         if ((r = transaction_apply(m, mode)) < 0)
519                 goto rollback;
520
521         assert(hashmap_isempty(m->transaction_jobs));
522         assert(!m->transaction_anchor);
523
524         return 0;
525
526 rollback:
527         transaction_abort(m);
528         return r;
529 }
530
531 static Job* transaction_add_one_job(Manager *m, JobType type, Name *name, bool *is_new) {
532         Job *j, *f;
533         int r;
534
535         assert(m);
536         assert(name);
537
538         /* Looks for an axisting prospective job and returns that. If
539          * it doesn't exist it is created and added to the prospective
540          * jobs list. */
541
542         f = hashmap_get(m->transaction_jobs, name);
543
544         for (j = f; j; j = j->transaction_next) {
545                 assert(j->name == name);
546
547                 if (j->type == type) {
548                         if (is_new)
549                                 *is_new = false;
550                         return j;
551                 }
552         }
553
554         if (name->meta.job && name->meta.job->type == type)
555                 j = name->meta.job;
556         else if (!(j = job_new(m, type, name)))
557                 return NULL;
558
559         if ((r = hashmap_replace(m->transaction_jobs, name, j)) < 0) {
560                 job_free(j);
561                 return NULL;
562         }
563
564         j->transaction_next = f;
565
566         if (f)
567                 f->transaction_prev = j;
568
569         j->generation = 0;
570         j->marker = NULL;
571         j->matters_to_anchor = false;
572
573         if (is_new)
574                 *is_new = true;
575
576         return j;
577 }
578
579 void manager_transaction_unlink_job(Manager *m, Job *j) {
580         assert(m);
581         assert(j);
582
583         if (j->transaction_prev)
584                 j->transaction_prev->transaction_next = j->transaction_next;
585         else if (j->transaction_next)
586                 hashmap_replace(m->transaction_jobs, j->name, j->transaction_next);
587         else
588                 hashmap_remove_value(m->transaction_jobs, j->name, j);
589
590         if (j->transaction_next)
591                 j->transaction_next->transaction_prev = j->transaction_prev;
592
593         j->transaction_prev = j->transaction_next = NULL;
594
595         while (j->subject_list)
596                 job_dependency_free(j->subject_list);
597
598         while (j->object_list) {
599                 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
600
601                 job_dependency_free(j->object_list);
602
603                 if (other) {
604                         log_debug("Deleting job %s as dependency of job %s", name_id(other->name), name_id(j->name));
605                         transaction_delete_job(m, other);
606                 }
607         }
608 }
609
610 static int transaction_add_job_and_dependencies(Manager *m, JobType type, Name *name, Job *by, bool matters, bool force, Job **_ret) {
611         Job *ret;
612         void *state;
613         Name *dep;
614         int r;
615         bool is_new;
616
617         assert(m);
618         assert(type < _JOB_TYPE_MAX);
619         assert(name);
620
621         if (name->meta.state != NAME_LOADED)
622                 return -EINVAL;
623
624         /* First add the job. */
625         if (!(ret = transaction_add_one_job(m, type, name, &is_new)))
626                 return -ENOMEM;
627
628         /* Then, add a link to the job. */
629         if (!job_dependency_new(by, ret, matters))
630                 return -ENOMEM;
631
632         if (is_new) {
633                 /* Finally, recursively add in all dependencies. */
634                 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
635                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRES], state)
636                                 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, force, NULL)) < 0)
637                                         goto fail;
638                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUIRES], state)
639                                 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !force, force, NULL)) < 0)
640                                         goto fail;
641                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_WANTS], state)
642                                 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, force, NULL)) < 0)
643                                         goto fail;
644                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUISITE], state)
645                                 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, true, force, NULL)) < 0)
646                                         goto fail;
647                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUISITE], state)
648                                 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, !force, force, NULL)) < 0)
649                                         goto fail;
650                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_CONFLICTS], state)
651                                 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, force, NULL)) < 0)
652                                         goto fail;
653
654                 } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
655
656                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRED_BY], state)
657                                 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, force, NULL)) < 0)
658                                         goto fail;
659                 }
660
661                 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
662         }
663
664         return 0;
665
666 fail:
667         return r;
668 }
669
670 int manager_add_job(Manager *m, JobType type, Name *name, JobMode mode, bool force, Job **_ret) {
671         int r;
672         Job *ret;
673
674         assert(m);
675         assert(type < _JOB_TYPE_MAX);
676         assert(name);
677         assert(mode < _JOB_MODE_MAX);
678
679         if ((r = transaction_add_job_and_dependencies(m, type, name, NULL, true, force, &ret))) {
680                 transaction_abort(m);
681                 return r;
682         }
683
684         if ((r = transaction_activate(m, mode)) < 0)
685                 return r;
686
687         if (_ret)
688                 *_ret = ret;
689
690         return 0;
691 }
692
693 Job *manager_get_job(Manager *m, uint32_t id) {
694         assert(m);
695
696         return hashmap_get(m->jobs, UINT32_TO_PTR(id));
697 }
698
699 Name *manager_get_name(Manager *m, const char *name) {
700         assert(m);
701         assert(name);
702
703         return hashmap_get(m->names, name);
704 }
705
706 static int dispatch_load_queue(Manager *m) {
707         Meta *meta;
708
709         assert(m);
710
711         /* Make sure we are not run recursively */
712         if (m->dispatching_load_queue)
713                 return 0;
714
715         m->dispatching_load_queue = true;
716
717         /* Dispatches the load queue. Takes a name from the queue and
718          * tries to load its data until the queue is empty */
719
720         while ((meta = m->load_queue)) {
721                 name_load(NAME(meta));
722                 LIST_REMOVE(Meta, m->load_queue, meta);
723         }
724
725         m->dispatching_load_queue = false;
726
727         return 0;
728 }
729
730 int manager_load_name(Manager *m, const char *name, Name **_ret) {
731         Name *ret;
732         NameType t;
733         int r;
734         char *n;
735
736         assert(m);
737         assert(name);
738         assert(_ret);
739
740         if (!name_is_valid(name))
741                 return -EINVAL;
742
743         /* This will load the service information files, but not actually
744          * start any services or anything */
745
746         if ((ret = manager_get_name(m, name)))
747                 goto finish;
748
749         if ((t = name_type_from_string(name)) == _NAME_TYPE_INVALID)
750                 return -EINVAL;
751
752         if (!(ret = name_new(m)))
753                 return -ENOMEM;
754
755         ret->meta.type = t;
756
757         if (!(n = strdup(name))) {
758                 name_free(ret);
759                 return -ENOMEM;
760         }
761
762         if ((r = set_put(ret->meta.names, n)) < 0) {
763                 name_free(ret);
764                 free(n);
765                 return r;
766         }
767
768         if ((r = name_link(ret)) < 0) {
769                 name_free(ret);
770                 return r;
771         }
772
773         /* At this point the new entry is created and linked. However,
774          * not loaded. Now load this entry and all its dependencies
775          * recursively */
776
777         dispatch_load_queue(m);
778
779 finish:
780
781         *_ret = ret;
782         return 0;
783 }
784
785 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
786         void *state;
787         Job *j;
788
789         assert(s);
790         assert(f);
791
792         HASHMAP_FOREACH(j, s->jobs, state)
793                 job_dump(j, f, prefix);
794 }
795
796 void manager_dump_names(Manager *s, FILE *f, const char *prefix) {
797         void *state;
798         Name *n;
799         const char *t;
800
801         assert(s);
802         assert(f);
803
804         HASHMAP_FOREACH_KEY(n, t, s->names, state)
805                 if (name_id(n) == t)
806                         name_dump(n, f, prefix);
807 }
808
809 void manager_clear_jobs(Manager *m) {
810         Job *j;
811
812         assert(m);
813
814         transaction_abort(m);
815
816         while ((j = hashmap_first(m->jobs)))
817                 job_free(j);
818 }