chiark / gitweb /
b1193b0ce3765ac302f3006f6cee62418829a97c
[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                 /* Merge all transactions */
248                 for (k = j->transaction_next; k; k = k->transaction_next)
249                         assert_se(job_type_merge(&t, k->type) == 0);
250
251                 /* If an active job is mergable, merge it too */
252                 if (j->name->meta.job)
253                         job_type_merge(&t, j->name->meta.job->type); /* Might fail. Which is OK */
254
255                 while ((k = j->transaction_next)) {
256                         if (j->linked) {
257                                 transaction_merge_and_delete_job(m, k, j, t);
258                                 j = k;
259                         } else
260                                 transaction_merge_and_delete_job(m, j, k, t);
261                 }
262
263                 assert(!j->transaction_next);
264                 assert(!j->transaction_prev);
265         }
266
267         return 0;
268 }
269
270 static bool name_matters_to_anchor(Name *n, Job *j) {
271         assert(n);
272         assert(!j->transaction_prev);
273
274         /* Checks whether at least one of the jobs for this name
275          * matters to the anchor. */
276
277         for (; j; j = j->transaction_next)
278                 if (j->matters_to_anchor)
279                         return true;
280
281         return false;
282 }
283
284 static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation) {
285         void *state;
286         Name *n;
287         int r;
288
289         assert(m);
290         assert(j);
291         assert(!j->transaction_prev);
292
293         /* Does a recursive sweep through the ordering graph, looking
294          * for a cycle. If we find cycle we try to break it. */
295
296         /* Did we find a cycle? */
297         if (j->marker && j->generation == generation) {
298                 Job *k;
299
300                 /* So, we already have been here. We have a
301                  * cycle. Let's try to break it. We go backwards in
302                  * our path and try to find a suitable job to
303                  * remove. We use the marker to find our way back,
304                  * since smart how we are we stored our way back in
305                  * there. */
306
307                 for (k = from; k; k = (k->generation == generation ? k->marker : NULL)) {
308
309                         if (!k->linked &&
310                             !name_matters_to_anchor(k->name, k)) {
311                                 /* Ok, we can drop this one, so let's
312                                  * do so. */
313                                 log_debug("Breaking order cycle by deleting job %s", name_id(k->name));
314                                 transaction_delete_name(m, k->name);
315                                 return -EAGAIN;
316                         }
317
318                         /* Check if this in fact was the beginning of
319                          * the cycle */
320                         if (k == j)
321                                 break;
322                 }
323
324                 return -ENOEXEC;
325         }
326
327         /* Make the marker point to where we come from, so that we can
328          * find our way backwards if we want to break a cycle */
329         j->marker = from;
330         j->generation = generation;
331
332         /* We assume that the the dependencies are bidirectional, and
333          * hence can ignore NAME_AFTER */
334         SET_FOREACH(n, j->name->meta.dependencies[NAME_BEFORE], state) {
335                 Job *o;
336
337                 /* Is there a job for this name? */
338                 if (!(o = hashmap_get(m->transaction_jobs, n)))
339
340                         /* Ok, there is no job for this in the
341                          * transaction, but maybe there is already one
342                          * running? */
343                         if (!(o = n->meta.job))
344                                 continue;
345
346                 if ((r = transaction_verify_order_one(m, o, j, generation)) < 0)
347                         return r;
348         }
349
350         return 0;
351 }
352
353 static int transaction_verify_order(Manager *m, unsigned *generation) {
354         Job *j;
355         int r;
356         void *state;
357
358         assert(m);
359         assert(generation);
360
361         /* Check if the ordering graph is cyclic. If it is, try to fix
362          * that up by dropping one of the jobs. */
363
364         HASHMAP_FOREACH(j, m->transaction_jobs, state)
365                 if ((r = transaction_verify_order_one(m, j, NULL, (*generation)++)) < 0)
366                         return r;
367
368         return 0;
369 }
370
371 static void transaction_collect_garbage(Manager *m) {
372         bool again;
373
374         assert(m);
375
376         /* Drop jobs that are not required by any other job */
377
378         do {
379                 void *state;
380                 Job *j;
381
382                 again = false;
383
384                 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
385                         if (j->object_list)
386                                 continue;
387
388                         log_debug("Garbage collecting job %s", name_id(j->name));
389
390                         transaction_delete_job(m, j);
391                         again = true;
392                         break;
393                 }
394
395         } while (again);
396 }
397
398 static int transaction_is_destructive(Manager *m, JobMode mode) {
399         void *state;
400         Job *j;
401
402         assert(m);
403
404         /* Checks whether applying this transaction means that
405          * existing jobs would be replaced */
406
407         HASHMAP_FOREACH(j, m->transaction_jobs, state) {
408
409                 /* Assume merged */
410                 assert(!j->transaction_prev);
411                 assert(!j->transaction_next);
412
413                 if (j->name->meta.job &&
414                     j->name->meta.job != j &&
415                     !job_type_is_superset(j->type, j->name->meta.job->type))
416                         return -EEXIST;
417         }
418
419         return 0;
420 }
421
422 static void transaction_minimize_impact(Manager *m) {
423         bool again;
424         assert(m);
425
426         /* Drops all unnecessary jobs that reverse already active jobs
427          * or that stop a running service. */
428
429         do {
430                 Job *j;
431                 void *state;
432
433                 again = false;
434
435                 HASHMAP_FOREACH(j, m->transaction_jobs, state) {
436                         for (; j; j = j->transaction_next) {
437
438                                 /* If it matters, we shouldn't drop it */
439                                 if (j->matters_to_anchor)
440                                         continue;
441
442                                 /* Would this stop a running service?
443                                  * Would this change an existing job?
444                                  * If so, let's drop this entry */
445                                 if ((j->type != JOB_STOP || name_is_dead(j->name)) &&
446                                     (!j->name->meta.job  || job_type_is_conflicting(j->type, j->name->meta.job->state)))
447                                         continue;
448
449                                 /* Ok, let's get rid of this */
450                                 transaction_delete_job(m, j);
451                                 again = true;
452                                 break;
453                         }
454
455                         if (again)
456                                 break;
457                 }
458
459         } while (again);
460 }
461
462 static int transaction_apply(Manager *m, JobMode mode) {
463         void *state;
464         Job *j;
465         int r;
466
467         /* Moves the transaction jobs to the set of active jobs */
468
469         HASHMAP_FOREACH(j, m->transaction_jobs, state) {
470                 /* Assume merged */
471                 assert(!j->transaction_prev);
472                 assert(!j->transaction_next);
473
474                 if (j->linked)
475                         continue;
476
477                 if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
478                         goto rollback;
479         }
480
481         while ((j = hashmap_steal_first(m->transaction_jobs))) {
482                 if (j->linked)
483                         continue;
484
485                 if (j->name->meta.job)
486                         job_free(j->name->meta.job);
487
488                 j->name->meta.job = j;
489                 j->linked = true;
490
491                 /* We're fully installed. Now let's free data we don't
492                  * need anymore. */
493
494                 assert(!j->transaction_next);
495                 assert(!j->transaction_prev);
496
497                 while (j->subject_list)
498                         job_dependency_free(j->subject_list);
499                 while (j->object_list)
500                         job_dependency_free(j->object_list);
501         }
502
503         m->transaction_anchor = NULL;
504
505         return 0;
506
507 rollback:
508
509         HASHMAP_FOREACH(j, m->transaction_jobs, state) {
510                 if (j->linked)
511                         continue;
512
513                 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
514         }
515
516         return r;
517 }
518
519 static int transaction_activate(Manager *m, JobMode mode) {
520         int r;
521         unsigned generation = 1;
522
523         assert(m);
524
525         /* This applies the changes recorded in transaction_jobs to
526          * the actual list of jobs, if possible. */
527
528         /* First step: figure out which jobs matter */
529         transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
530
531         /* Second step: Try not to stop any running services if
532          * we don't have to. Don't try to reverse running
533          * jobs if we don't have to. */
534         transaction_minimize_impact(m);
535
536         for (;;) {
537                 /* Third step: Let's remove unneeded jobs that might
538                  * be lurking. */
539                 transaction_collect_garbage(m);
540
541                 /* Fourth step: verify order makes sense and correct
542                  * cycles if necessary and possible */
543                 if ((r = transaction_verify_order(m, &generation)) >= 0)
544                         break;
545
546                 if (r != -EAGAIN)
547                         goto rollback;
548
549                 /* Let's see if the resulting transaction ordering
550                  * graph is still cyclic... */
551         }
552
553         for (;;) {
554                 /* Fifth step: let's drop unmergable entries if
555                  * necessary and possible, merge entries we can
556                  * merge */
557                 if ((r = transaction_merge_jobs(m)) >= 0)
558                         break;
559
560                 if (r != -EAGAIN)
561                         goto rollback;
562
563                 /* Sixth step: an entry got dropped, let's garbage
564                  * collect its dependencies. */
565                 transaction_collect_garbage(m);
566
567                 /* Let's see if the resulting transaction still has
568                  * unmergable entries ... */
569         }
570
571         /* Seventh step: check whether we can actually apply this */
572         if (mode == JOB_FAIL)
573                 if ((r = transaction_is_destructive(m, mode)) < 0)
574                         goto rollback;
575
576         /* Eights step: apply changes */
577         if ((r = transaction_apply(m, mode)) < 0)
578                 goto rollback;
579
580         assert(hashmap_isempty(m->transaction_jobs));
581         assert(!m->transaction_anchor);
582
583         return 0;
584
585 rollback:
586         transaction_abort(m);
587         return r;
588 }
589
590 static Job* transaction_add_one_job(Manager *m, JobType type, Name *name, bool *is_new) {
591         Job *j, *f;
592         int r;
593
594         assert(m);
595         assert(name);
596
597         /* Looks for an axisting prospective job and returns that. If
598          * it doesn't exist it is created and added to the prospective
599          * jobs list. */
600
601         f = hashmap_get(m->transaction_jobs, name);
602
603         for (j = f; j; j = j->transaction_next) {
604                 assert(j->name == name);
605
606                 if (j->type == type) {
607                         if (is_new)
608                                 *is_new = false;
609                         return j;
610                 }
611         }
612
613         if (name->meta.job && name->meta.job->type == type)
614                 j = name->meta.job;
615         else if (!(j = job_new(m, type, name)))
616                 return NULL;
617
618         if ((r = hashmap_replace(m->transaction_jobs, name, j)) < 0) {
619                 job_free(j);
620                 return NULL;
621         }
622
623         j->transaction_next = f;
624
625         if (f)
626                 f->transaction_prev = j;
627
628         j->generation = 0;
629         j->marker = NULL;
630         j->matters_to_anchor = false;
631
632         if (is_new)
633                 *is_new = true;
634
635         return j;
636 }
637
638 void manager_transaction_unlink_job(Manager *m, Job *j) {
639         assert(m);
640         assert(j);
641
642         if (j->transaction_prev)
643                 j->transaction_prev->transaction_next = j->transaction_next;
644         else if (j->transaction_next)
645                 hashmap_replace(m->transaction_jobs, j->name, j->transaction_next);
646         else
647                 hashmap_remove_value(m->transaction_jobs, j->name, j);
648
649         if (j->transaction_next)
650                 j->transaction_next->transaction_prev = j->transaction_prev;
651
652         j->transaction_prev = j->transaction_next = NULL;
653
654         while (j->subject_list)
655                 job_dependency_free(j->subject_list);
656
657         while (j->object_list) {
658                 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
659
660                 job_dependency_free(j->object_list);
661
662                 if (other) {
663                         log_debug("Deleting job %s as dependency of job %s", name_id(other->name), name_id(j->name));
664                         transaction_delete_job(m, other);
665                 }
666         }
667 }
668
669 static int transaction_add_job_and_dependencies(Manager *m, JobType type, Name *name, Job *by, bool matters, bool force, Job **_ret) {
670         Job *ret;
671         void *state;
672         Name *dep;
673         int r;
674         bool is_new;
675
676         assert(m);
677         assert(type < _JOB_TYPE_MAX);
678         assert(name);
679
680         if (name->meta.state != NAME_LOADED)
681                 return -EINVAL;
682
683         /* First add the job. */
684         if (!(ret = transaction_add_one_job(m, type, name, &is_new)))
685                 return -ENOMEM;
686
687         /* Then, add a link to the job. */
688         if (!job_dependency_new(by, ret, matters))
689                 return -ENOMEM;
690
691         if (is_new) {
692                 /* Finally, recursively add in all dependencies. */
693                 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
694                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRES], state)
695                                 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, force, NULL)) < 0)
696                                         goto fail;
697                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUIRES], state)
698                                 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !force, force, NULL)) < 0)
699                                         goto fail;
700                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_WANTS], state)
701                                 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, force, NULL)) < 0)
702                                         goto fail;
703                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUISITE], state)
704                                 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, true, force, NULL)) < 0)
705                                         goto fail;
706                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUISITE], state)
707                                 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, !force, force, NULL)) < 0)
708                                         goto fail;
709                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_CONFLICTS], state)
710                                 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, force, NULL)) < 0)
711                                         goto fail;
712
713                 } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
714
715                         SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRED_BY], state)
716                                 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, force, NULL)) < 0)
717                                         goto fail;
718                 }
719
720                 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
721         }
722
723         return 0;
724
725 fail:
726         return r;
727 }
728
729 int manager_add_job(Manager *m, JobType type, Name *name, JobMode mode, bool force, Job **_ret) {
730         int r;
731         Job *ret;
732
733         assert(m);
734         assert(type < _JOB_TYPE_MAX);
735         assert(name);
736         assert(mode < _JOB_MODE_MAX);
737
738         if ((r = transaction_add_job_and_dependencies(m, type, name, NULL, true, force, &ret))) {
739                 transaction_abort(m);
740                 return r;
741         }
742
743         if ((r = transaction_activate(m, mode)) < 0)
744                 return r;
745
746         if (_ret)
747                 *_ret = ret;
748
749         return 0;
750 }
751
752 Job *manager_get_job(Manager *m, uint32_t id) {
753         assert(m);
754
755         return hashmap_get(m->jobs, UINT32_TO_PTR(id));
756 }
757
758 Name *manager_get_name(Manager *m, const char *name) {
759         assert(m);
760         assert(name);
761
762         return hashmap_get(m->names, name);
763 }
764
765 static int dispatch_load_queue(Manager *m) {
766         Meta *meta;
767
768         assert(m);
769
770         /* Make sure we are not run recursively */
771         if (m->dispatching_load_queue)
772                 return 0;
773
774         m->dispatching_load_queue = true;
775
776         /* Dispatches the load queue. Takes a name from the queue and
777          * tries to load its data until the queue is empty */
778
779         while ((meta = m->load_queue)) {
780                 name_load(NAME(meta));
781                 LIST_REMOVE(Meta, m->load_queue, meta);
782         }
783
784         m->dispatching_load_queue = false;
785
786         return 0;
787 }
788
789 int manager_load_name(Manager *m, const char *name, Name **_ret) {
790         Name *ret;
791         NameType t;
792         int r;
793         char *n;
794
795         assert(m);
796         assert(name);
797         assert(_ret);
798
799         if (!name_is_valid(name))
800                 return -EINVAL;
801
802         /* This will load the service information files, but not actually
803          * start any services or anything */
804
805         if ((ret = manager_get_name(m, name)))
806                 goto finish;
807
808         if ((t = name_type_from_string(name)) == _NAME_TYPE_INVALID)
809                 return -EINVAL;
810
811         if (!(ret = name_new(m)))
812                 return -ENOMEM;
813
814         ret->meta.type = t;
815
816         if (!(n = strdup(name))) {
817                 name_free(ret);
818                 return -ENOMEM;
819         }
820
821         if ((r = set_put(ret->meta.names, n)) < 0) {
822                 name_free(ret);
823                 free(n);
824                 return r;
825         }
826
827         if ((r = name_link(ret)) < 0) {
828                 name_free(ret);
829                 return r;
830         }
831
832         /* At this point the new entry is created and linked. However,
833          * not loaded. Now load this entry and all its dependencies
834          * recursively */
835
836         dispatch_load_queue(m);
837
838 finish:
839
840         *_ret = ret;
841         return 0;
842 }
843
844 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
845         void *state;
846         Job *j;
847
848         assert(s);
849         assert(f);
850
851         HASHMAP_FOREACH(j, s->jobs, state)
852                 job_dump(j, f, prefix);
853 }
854
855 void manager_dump_names(Manager *s, FILE *f, const char *prefix) {
856         void *state;
857         Name *n;
858         const char *t;
859
860         assert(s);
861         assert(f);
862
863         HASHMAP_FOREACH_KEY(n, t, s->names, state)
864                 if (name_id(n) == t)
865                         name_dump(n, f, prefix);
866 }
867
868 void manager_clear_jobs(Manager *m) {
869         Job *j;
870
871         assert(m);
872
873         transaction_abort(m);
874
875         while ((j = hashmap_first(m->jobs)))
876                 job_free(j);
877 }