chiark / gitweb /
a104e2f401c8689ec572558206ad37699a776020
[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
12 Manager* manager_new(void) {
13         Manager *m;
14
15         if (!(m = new0(Manager, 1)))
16                 return NULL;
17
18         if (!(m->names = hashmap_new(string_hash_func, string_compare_func)))
19                 goto fail;
20
21         if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
22                 goto fail;
23
24         if (!(m->transaction_jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
25                 goto fail;
26
27         return m;
28
29 fail:
30         manager_free(m);
31         return NULL;
32 }
33
34 void manager_free(Manager *m) {
35         Name *n;
36         Job *j;
37
38         assert(m);
39
40         while ((n = hashmap_first(m->names)))
41                 name_free(n);
42
43         while ((j = hashmap_steal_first(m->transaction_jobs)))
44                 job_free(j);
45
46         hashmap_free(m->names);
47         hashmap_free(m->jobs);
48         hashmap_free(m->transaction_jobs);
49
50         free(m);
51 }
52
53 static void transaction_abort(Manager *m) {
54         Job *j;
55
56         assert(m);
57
58         while ((j = hashmap_first(m->transaction_jobs)))
59                 if (j->linked)
60                         manager_transaction_delete_job(m, j);
61                 else
62                         job_free(j);
63
64         assert(hashmap_isempty(m->transaction_jobs));
65         assert(!m->transaction_anchor);
66 }
67
68 static void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) {
69         JobDependency *l;
70
71         assert(m);
72
73         for (l = j ? j->subject_list : m->transaction_anchor; l; l = l->subject_next) {
74
75                 /* This link does not matter */
76                 if (!l->matters)
77                         continue;
78
79                 /* This name has already been marked */
80                 if (l->object->generation == generation)
81                         continue;
82
83                 l->object->matters_to_anchor = true;
84                 l->object->generation = generation;
85
86                 transaction_find_jobs_that_matter_to_anchor(m, l->object, generation);
87         }
88 }
89
90 static bool types_match(JobType a, JobType b, JobType c, JobType d) {
91         return
92                 (a == c && b == d) ||
93                 (a == d && b == c);
94 }
95
96 static int types_merge(JobType *a, JobType b) {
97         if (*a == b)
98                 return 0;
99
100         if (types_match(*a, b, JOB_START, JOB_VERIFY_STARTED))
101                 *a = JOB_START;
102         else if (types_match(*a, b, JOB_START, JOB_RELOAD) ||
103                  types_match(*a, b, JOB_START, JOB_RELOAD_OR_START) ||
104                  types_match(*a, b, JOB_VERIFY_STARTED, JOB_RELOAD_OR_START) ||
105                  types_match(*a, b, JOB_RELOAD, JOB_RELOAD_OR_START))
106                 *a = JOB_RELOAD_OR_START;
107         else if (types_match(*a, b, JOB_START, JOB_RESTART) ||
108                  types_match(*a, b, JOB_START, JOB_TRY_RESTART) ||
109                  types_match(*a, b, JOB_VERIFY_STARTED, JOB_RESTART) ||
110                  types_match(*a, b, JOB_RELOAD, JOB_RESTART) ||
111                  types_match(*a, b, JOB_RELOAD_OR_START, JOB_RESTART) ||
112                  types_match(*a, b, JOB_RELOAD_OR_START, JOB_TRY_RESTART) ||
113                  types_match(*a, b, JOB_RESTART, JOB_TRY_RESTART))
114                 *a = JOB_RESTART;
115         else if (types_match(*a, b, JOB_VERIFY_STARTED, JOB_RELOAD))
116                 *a = JOB_RELOAD;
117         else if (types_match(*a, b, JOB_VERIFY_STARTED, JOB_TRY_RESTART) ||
118                  types_match(*a, b, JOB_RELOAD, JOB_TRY_RESTART))
119                 *a = JOB_TRY_RESTART;
120
121         return -EEXIST;
122 }
123
124 static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) {
125         JobDependency *l, *last;
126
127         assert(j);
128         assert(other);
129         assert(j->name == other->name);
130         assert(!j->linked);
131
132         j->type = t;
133         j->state = JOB_WAITING;
134
135         j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
136
137         /* Patch us in as new owner of the JobDependency objects */
138         last = NULL;
139         for (l = other->subject_list; l; l = l->subject_next) {
140                 assert(l->subject == other);
141                 l->subject = j;
142                 last = l;
143         }
144
145         /* Merge both lists */
146         if (last) {
147                 last->subject_next = j->subject_list;
148                 if (j->subject_list)
149                         j->subject_list->subject_prev = last;
150                 j->subject_list = other->subject_list;
151         }
152
153         /* Patch us in as new owner of the JobDependency objects */
154         last = NULL;
155         for (l = other->object_list; l; l = l->object_next) {
156                 assert(l->object == other);
157                 l->object = j;
158                 last = l;
159         }
160
161         /* Merge both lists */
162         if (last) {
163                 last->object_next = j->object_list;
164                 if (j->object_list)
165                         j->object_list->object_prev = last;
166                 j->object_list = other->object_list;
167         }
168
169
170         /* Kill the other job */
171         other->subject_list = NULL;
172         other->object_list = NULL;
173         manager_transaction_delete_job(m, other);
174 }
175
176 static int transaction_merge_jobs(Manager *m) {
177         Job *j;
178         void *state;
179         int r;
180
181         assert(m);
182
183         HASHMAP_FOREACH(j, m->transaction_jobs, state) {
184                 JobType t = j->type;
185                 Job *k;
186
187                 for (k = j->transaction_next; k; k = k->transaction_next)
188                         if ((r = types_merge(&t, k->type)) < 0)
189                                 return r;
190
191                 while ((k = j->transaction_next)) {
192                         if (j->linked) {
193                                 transaction_merge_and_delete_job(m, k, j, t);
194                                 j = k;
195                         } else
196                                 transaction_merge_and_delete_job(m, j, k, t);
197                 }
198
199                 assert(!j->transaction_next);
200                 assert(!j->transaction_prev);
201         }
202
203         return 0;
204 }
205
206 static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation) {
207         void *state;
208         Name *n;
209         int r;
210
211         assert(m);
212         assert(j);
213
214         /* Did we find a cycle? */
215         if (j->marker && j->generation == generation) {
216                 Job *k;
217
218                 /* So, we already have been here. We have a
219                  * cycle. Let's try to break it. We go backwards in our
220                  * path and try to find a suitable job to remove. */
221
222                 for (k = from; k; k = (k->generation == generation ? k->marker : NULL)) {
223                         if (!k->matters_to_anchor) {
224                                 manager_transaction_delete_job(m, k);
225                                 return -EAGAIN;
226                         }
227
228                         /* Check if this in fact was the beginning of
229                          * the cycle */
230                         if (k == j)
231                                 break;
232                 }
233
234                 return -ELOOP;
235         }
236
237         j->marker = from;
238         j->generation = generation;
239
240         /* We assume that the the dependencies are both-ways, and
241          * hence can ignore NAME_AFTER */
242
243         SET_FOREACH(n, j->name->meta.dependencies[NAME_BEFORE], state) {
244                 Job *o;
245
246                 if (!(o = hashmap_get(m->transaction_jobs, n)))
247                         if (!(o = n->meta.job))
248                                 continue;
249
250                 if ((r = transaction_verify_order_one(m, o, j, generation)) < 0)
251                         return r;
252         }
253
254         return 0;
255 }
256
257 static int transaction_verify_order(Manager *m, unsigned *generation) {
258         bool again;
259         assert(m);
260         assert(generation);
261
262         do {
263                 Job *j;
264                 int r;
265                 void *state;
266
267                 again = false;
268
269                 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
270
271                         /* Assume merged */
272                         assert(!j->transaction_next);
273                         assert(!j->transaction_prev);
274
275                         if ((r = transaction_verify_order_one(m, j, NULL, (*generation)++)) < 0)  {
276
277                                 /* There was a cycleq, but it was fixed,
278                                  * we need to restart our algorithm */
279                                 if (r == -EAGAIN) {
280                                         again = true;
281                                         break;
282                                 }
283
284                                 return r;
285                         }
286                 }
287         } while (again);
288
289         return 0;
290 }
291
292 static void transaction_collect_garbage(Manager *m) {
293         bool again;
294
295         assert(m);
296
297         do {
298                 void *state;
299                 Job *j;
300
301                 again = false;
302
303                 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
304                         if (j->object_list)
305                                 continue;
306
307                         manager_transaction_delete_job(m, j);
308                         again = true;
309                         break;
310                 }
311
312         } while (again);
313 }
314
315 static int transaction_is_destructive(Manager *m, JobMode mode) {
316         void *state;
317         Job *j;
318
319         assert(m);
320
321         /* Checks whether applying this transaction means that
322          * existing jobs would be replaced */
323
324         HASHMAP_FOREACH(j, m->transaction_jobs, state)
325                 if (j->name->meta.job && j->name->meta.job != j)
326                         return -EEXIST;
327
328         return 0;
329 }
330
331 static int transaction_apply(Manager *m, JobMode mode) {
332         void *state;
333         Job *j;
334         int r;
335
336         HASHMAP_FOREACH(j, m->transaction_jobs, state) {
337                 if (j->linked)
338                         continue;
339
340                 if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
341                         goto rollback;
342         }
343
344         while ((j = hashmap_steal_first(m->transaction_jobs))) {
345                 if (j->linked)
346                         continue;
347
348                 if (j->name->meta.job)
349                         job_free(j->name->meta.job);
350
351                 j->name->meta.job = j;
352                 j->linked = true;
353
354                 /* We're fully installed. Now let's free data we don't
355                  * need anymore. */
356
357                 assert(!j->transaction_next);
358                 assert(!j->transaction_prev);
359
360                 while (j->subject_list)
361                         job_dependency_free(j->subject_list);
362                 while (j->object_list)
363                         job_dependency_free(j->object_list);
364         }
365
366         return 0;
367
368 rollback:
369
370         HASHMAP_FOREACH(j, m->transaction_jobs, state) {
371                 if (j->linked)
372                         continue;
373
374                 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
375         }
376
377         return r;
378 }
379
380
381 static int transaction_activate(Manager *m, JobMode mode) {
382         int r;
383         unsigned generation = 1;
384
385         assert(m);
386
387         /* This applies the changes recorded in transaction_jobs to
388          * the actual list of jobs, if possible. */
389
390         /* First step: figure out which jobs matter */
391         transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
392
393         /* Second step: let's merge entries we can merge */
394         if ((r = transaction_merge_jobs(m)) < 0)
395                 goto rollback;
396
397         /* Third step: verify order makes sense */
398         if ((r = transaction_verify_order(m, &generation)) < 0)
399                 goto rollback;
400
401         /* Third step: do garbage colletion */
402         transaction_collect_garbage(m);
403
404         /* Fourth step: check whether we can actually apply this */
405         if (mode == JOB_FAIL)
406                 if ((r = transaction_is_destructive(m, mode)) < 0)
407                         goto rollback;
408
409         /* Fifth step: apply changes */
410         if ((r = transaction_apply(m, mode)) < 0)
411                 goto rollback;
412
413         assert(hashmap_isempty(m->transaction_jobs));
414         assert(!m->transaction_anchor);
415
416         return 0;
417
418 rollback:
419         transaction_abort(m);
420         return r;
421 }
422
423 static Job* transaction_add_one_job(Manager *m, JobType type, Name *name, bool *is_new) {
424         Job *j, *f;
425         int r;
426
427         assert(m);
428         assert(name);
429
430         /* Looks for an axisting prospective job and returns that. If
431          * it doesn't exist it is created and added to the prospective
432          * jobs list. */
433
434         f = hashmap_get(m->transaction_jobs, name);
435
436         for (j = f; j; j = j->transaction_next) {
437                 assert(j->name == name);
438
439                 if (j->type == type) {
440                         if (is_new)
441                                 *is_new = false;
442                         return j;
443                 }
444         }
445
446         if (name->meta.job && name->meta.job->type == type)
447                 j = name->meta.job;
448         else if (!(j = job_new(m, type, name)))
449                 return NULL;
450
451         if ((r = hashmap_replace(m->transaction_jobs, name, j)) < 0) {
452                 job_free(j);
453                 return NULL;
454         }
455
456         j->transaction_next = f;
457
458         if (f)
459                 f->transaction_prev = j;
460
461         j->generation = 0;
462         j->marker = NULL;
463         j->matters_to_anchor = false;
464
465         if (is_new)
466                 *is_new = true;
467
468         return j;
469 }
470
471 void manager_transaction_delete_job(Manager *m, Job *j) {
472         assert(m);
473         assert(j);
474
475         if (j->transaction_prev)
476                 j->transaction_prev->transaction_next = j->transaction_next;
477         else if (j->transaction_next)
478                 hashmap_replace(m->transaction_jobs, j->name, j->transaction_next);
479         else
480                 hashmap_remove_value(m->transaction_jobs, j->name, j);
481
482         if (j->transaction_next)
483                 j->transaction_next->transaction_prev = j->transaction_prev;
484
485         j->transaction_prev = j->transaction_next = NULL;
486
487         while (j->subject_list)
488                 job_dependency_free(j->subject_list);
489         while (j->object_list)
490                 job_dependency_free(j->object_list);
491 }
492
493 static int transaction_add_job_and_dependencies(Manager *m, JobType type, Name *name, Job *by, bool matters, bool force, Job **_ret) {
494         Job *ret;
495         void *state;
496         Name *dep;
497         int r;
498         bool is_new;
499
500         assert(m);
501         assert(type < _JOB_TYPE_MAX);
502         assert(name);
503
504         /* First add the job. */
505         if (!(ret = transaction_add_one_job(m, type, name, &is_new)))
506                 return -ENOMEM;
507
508         /* Then, add a link to the job. */
509         if (!job_dependency_new(by, ret, matters))
510                 return -ENOMEM;
511
512         if (is_new) {
513                 /* Finally, recursively add in all dependencies. */
514                 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
515                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRES], state)
516                                 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, force, NULL)) < 0)
517                                         goto fail;
518                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUIRES], state)
519                                 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !force, force, NULL)) < 0)
520                                         goto fail;
521                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_WANTS], state)
522                                 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, force, NULL)) < 0)
523                                         goto fail;
524                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUISITE], state)
525                                 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, true, force, NULL)) < 0)
526                                         goto fail;
527                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUISITE], state)
528                                 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, !force, force, NULL)) < 0)
529                                         goto fail;
530                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_CONFLICTS], state)
531                                 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, force, NULL)) < 0)
532                                         goto fail;
533
534                 } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
535
536                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRED_BY], state)
537                                 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, force, NULL)) < 0)
538                                         goto fail;
539                 }
540
541                 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
542         }
543
544         return 0;
545
546 fail:
547         return r;
548 }
549
550 int manager_add_job(Manager *m, JobType type, Name *name, JobMode mode, bool force, Job **_ret) {
551         int r;
552         Job *ret;
553
554         assert(m);
555         assert(type < _JOB_TYPE_MAX);
556         assert(name);
557         assert(mode < _JOB_MODE_MAX);
558
559         if ((r = transaction_add_job_and_dependencies(m, type, name, NULL, true, force, &ret))) {
560                 transaction_abort(m);
561                 return r;
562         }
563
564         if ((r = transaction_activate(m, mode)) < 0)
565                 return r;
566
567         if (_ret)
568                 *_ret = ret;
569
570         return 0;
571 }
572
573 Job *manager_get_job(Manager *m, uint32_t id) {
574         assert(m);
575
576         return hashmap_get(m->jobs, UINT32_TO_PTR(id));
577 }
578
579 Name *manager_get_name(Manager *m, const char *name) {
580         assert(m);
581         assert(name);
582
583         return hashmap_get(m->names, name);
584 }
585
586 static int dispatch_load_queue(Manager *m) {
587         Meta *meta;
588
589         assert(m);
590
591         /* Make sure we are not run recursively */
592         if (m->dispatching_load_queue)
593                 return 0;
594
595         m->dispatching_load_queue = true;
596
597         /* Dispatches the load queue. Takes a name from the queue and
598          * tries to load its data until the queue is empty */
599
600         while ((meta = m->load_queue)) {
601                 name_load(NAME(meta));
602                 LIST_REMOVE(Meta, m->load_queue, meta);
603         }
604
605         m->dispatching_load_queue = false;
606
607         return 0;
608 }
609
610 int manager_load_name(Manager *m, const char *name, Name **_ret) {
611         Name *ret;
612         NameType t;
613         int r;
614         char *n;
615
616         assert(m);
617         assert(name);
618         assert(_ret);
619
620         if (!name_is_valid(name))
621                 return -EINVAL;
622
623         /* This will load the service information files, but not actually
624          * start any services or anything */
625
626         if ((ret = manager_get_name(m, name)))
627                 goto finish;
628
629         if ((t = name_type_from_string(name)) == _NAME_TYPE_INVALID)
630                 return -EINVAL;
631
632         if (!(ret = name_new(m)))
633                 return -ENOMEM;
634
635         ret->meta.type = t;
636
637         if (!(n = strdup(name))) {
638                 name_free(ret);
639                 return -ENOMEM;
640         }
641
642         if (set_put(ret->meta.names, n) < 0) {
643                 name_free(ret);
644                 free(n);
645                 return -ENOMEM;
646         }
647
648         if ((r = name_link(ret)) < 0) {
649                 name_free(ret);
650                 return r;
651         }
652
653         /* At this point the new entry is created and linked. However,
654          * not loaded. Now load this entry and all its dependencies
655          * recursively */
656
657         dispatch_load_queue(m);
658
659 finish:
660
661         *_ret = ret;
662         return 0;
663 }
664
665 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
666         void *state;
667         Job *j;
668
669         assert(s);
670         assert(f);
671
672         HASHMAP_FOREACH(j, s->jobs, state)
673                 job_dump(j, f, prefix);
674 }
675
676 void manager_dump_names(Manager *s, FILE *f, const char *prefix) {
677         void *state;
678         Name *n;
679         const char *t;
680
681         assert(s);
682         assert(f);
683
684         HASHMAP_FOREACH_KEY(n, t, s->names, state)
685                 if (name_id(n) == t)
686                         name_dump(n, f, prefix);
687 }
688
689 void manager_clear_jobs(Manager *m) {
690         Job *j;
691
692         assert(m);
693
694         transaction_abort(m);
695
696         while ((j = hashmap_first(m->jobs)))
697                 job_free(j);
698 }