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