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