1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/epoll.h>
27 #include <sys/signalfd.h>
38 #include "ratelimit.h"
40 static int manager_setup_signals(Manager *m) {
42 struct epoll_event ev;
46 assert_se(reset_all_signal_handlers() == 0);
48 assert_se(sigemptyset(&mask) == 0);
49 assert_se(sigaddset(&mask, SIGCHLD) == 0);
50 assert_se(sigaddset(&mask, SIGINT) == 0); /* Kernel sends us this on control-alt-del */
51 assert_se(sigaddset(&mask, SIGWINCH) == 0); /* Kernel sends us this on kbrequest (alt-arrowup) */
52 assert_se(sigaddset(&mask, SIGTERM) == 0);
53 assert_se(sigaddset(&mask, SIGHUP) == 0);
54 assert_se(sigaddset(&mask, SIGUSR1) == 0);
55 assert_se(sigaddset(&mask, SIGUSR2) == 0);
56 assert_se(sigaddset(&mask, SIGPIPE) == 0);
57 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
59 m->signal_watch.type = WATCH_SIGNAL;
60 if ((m->signal_watch.fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0)
65 ev.data.ptr = &m->signal_watch;
67 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0)
73 Manager* manager_new(void) {
76 if (!(m = new0(Manager, 1)))
80 m->running_as = MANAGER_INIT;
81 else if (getuid() == 0)
82 m->running_as = MANAGER_SYSTEM;
84 m->running_as = MANAGER_USER;
86 log_debug("systemd running in %s mode.", manager_running_as_to_string(m->running_as));
88 m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = -1;
89 m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
91 if (!(m->units = hashmap_new(string_hash_func, string_compare_func)))
94 if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
97 if (!(m->transaction_jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
100 if (!(m->watch_pids = hashmap_new(trivial_hash_func, trivial_compare_func)))
103 if ((m->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
106 if (manager_setup_signals(m) < 0)
109 /* FIXME: this should be called only when the D-Bus bus daemon is running */
120 void manager_free(Manager *m) {
127 while ((j = hashmap_first(m->transaction_jobs)))
130 while ((u = hashmap_first(m->units)))
133 for (c = 0; c < _UNIT_TYPE_MAX; c++)
134 if (unit_vtable[c]->shutdown)
135 unit_vtable[c]->shutdown(m);
139 hashmap_free(m->units);
140 hashmap_free(m->jobs);
141 hashmap_free(m->transaction_jobs);
142 hashmap_free(m->watch_pids);
144 if (m->epoll_fd >= 0)
145 close_nointr(m->epoll_fd);
146 if (m->signal_watch.fd >= 0)
147 close_nointr(m->signal_watch.fd);
152 int manager_coldplug(Manager *m) {
161 /* First, let's ask every type to load all units from
162 * disk/kernel that it might know */
163 for (c = 0; c < _UNIT_TYPE_MAX; c++)
164 if (unit_vtable[c]->enumerate)
165 if ((r = unit_vtable[c]->enumerate(m)) < 0)
168 manager_dispatch_load_queue(m);
170 /* Then, let's set up their initial state. */
171 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
177 if (UNIT_VTABLE(u)->coldplug)
178 if ((r = UNIT_VTABLE(u)->coldplug(u)) < 0)
185 static void transaction_delete_job(Manager *m, Job *j) {
189 /* Deletes one job from the transaction */
191 manager_transaction_unlink_job(m, j);
197 static void transaction_delete_unit(Manager *m, Unit *u) {
200 /* Deletes all jobs associated with a certain unit from the
203 while ((j = hashmap_get(m->transaction_jobs, u)))
204 transaction_delete_job(m, j);
207 static void transaction_clean_dependencies(Manager *m) {
213 /* Drops all dependencies of all installed jobs */
215 HASHMAP_FOREACH(j, m->jobs, i) {
216 while (j->subject_list)
217 job_dependency_free(j->subject_list);
218 while (j->object_list)
219 job_dependency_free(j->object_list);
222 assert(!m->transaction_anchor);
225 static void transaction_abort(Manager *m) {
230 while ((j = hashmap_first(m->transaction_jobs)))
232 transaction_delete_job(m, j);
236 assert(hashmap_isempty(m->transaction_jobs));
238 transaction_clean_dependencies(m);
241 static void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) {
246 /* A recursive sweep through the graph that marks all units
247 * that matter to the anchor job, i.e. are directly or
248 * indirectly a dependency of the anchor job via paths that
249 * are fully marked as mattering. */
254 l = m->transaction_anchor;
256 LIST_FOREACH(subject, l, l) {
258 /* This link does not matter */
262 /* This unit has already been marked */
263 if (l->object->generation == generation)
266 l->object->matters_to_anchor = true;
267 l->object->generation = generation;
269 transaction_find_jobs_that_matter_to_anchor(m, l->object, generation);
273 static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) {
274 JobDependency *l, *last;
278 assert(j->unit == other->unit);
279 assert(!j->installed);
281 /* Merges 'other' into 'j' and then deletes j. */
284 j->state = JOB_WAITING;
285 j->forced = j->forced || other->forced;
287 j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
289 /* Patch us in as new owner of the JobDependency objects */
291 LIST_FOREACH(subject, l, other->subject_list) {
292 assert(l->subject == other);
297 /* Merge both lists */
299 last->subject_next = j->subject_list;
301 j->subject_list->subject_prev = last;
302 j->subject_list = other->subject_list;
305 /* Patch us in as new owner of the JobDependency objects */
307 LIST_FOREACH(object, l, other->object_list) {
308 assert(l->object == other);
313 /* Merge both lists */
315 last->object_next = j->object_list;
317 j->object_list->object_prev = last;
318 j->object_list = other->object_list;
321 /* Kill the other job */
322 other->subject_list = NULL;
323 other->object_list = NULL;
324 transaction_delete_job(m, other);
327 static int delete_one_unmergeable_job(Manager *m, Job *j) {
332 /* Tries to delete one item in the linked list
333 * j->transaction_next->transaction_next->... that conflicts
334 * whith another one, in an attempt to make an inconsistent
335 * transaction work. */
337 /* We rely here on the fact that if a merged with b does not
338 * merge with c, either a or b merge with c neither */
339 LIST_FOREACH(transaction, j, j)
340 LIST_FOREACH(transaction, k, j->transaction_next) {
343 /* Is this one mergeable? Then skip it */
344 if (job_type_is_mergeable(j->type, k->type))
347 /* Ok, we found two that conflict, let's see if we can
348 * drop one of them */
349 if (!j->matters_to_anchor)
351 else if (!k->matters_to_anchor)
356 /* Ok, we can drop one, so let's do so. */
357 log_debug("Try to fix job merging by deleting job %s/%s", unit_id(d->unit), job_type_to_string(d->type));
358 transaction_delete_job(m, d);
365 static int transaction_merge_jobs(Manager *m) {
372 /* First step, check whether any of the jobs for one specific
373 * task conflict. If so, try to drop one of them. */
374 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
379 LIST_FOREACH(transaction, k, j->transaction_next) {
380 if ((r = job_type_merge(&t, k->type)) >= 0)
383 /* OK, we could not merge all jobs for this
384 * action. Let's see if we can get rid of one
387 if ((r = delete_one_unmergeable_job(m, j)) >= 0)
388 /* Ok, we managed to drop one, now
389 * let's ask our callers to call us
390 * again after garbage collecting */
393 /* We couldn't merge anything. Failure */
398 /* Second step, merge the jobs. */
399 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
403 /* Merge all transactions */
404 LIST_FOREACH(transaction, k, j->transaction_next)
405 assert_se(job_type_merge(&t, k->type) == 0);
407 /* If an active job is mergeable, merge it too */
408 if (j->unit->meta.job)
409 job_type_merge(&t, j->unit->meta.job->type); /* Might fail. Which is OK */
411 while ((k = j->transaction_next)) {
413 transaction_merge_and_delete_job(m, k, j, t);
416 transaction_merge_and_delete_job(m, j, k, t);
419 assert(!j->transaction_next);
420 assert(!j->transaction_prev);
426 static bool unit_matters_to_anchor(Unit *u, Job *j) {
428 assert(!j->transaction_prev);
430 /* Checks whether at least one of the jobs for this unit
431 * matters to the anchor. */
433 LIST_FOREACH(transaction, j, j)
434 if (j->matters_to_anchor)
440 static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation) {
447 assert(!j->transaction_prev);
449 /* Does a recursive sweep through the ordering graph, looking
450 * for a cycle. If we find cycle we try to break it. */
452 /* Did we find a cycle? */
453 if (j->marker && j->generation == generation) {
456 /* So, we already have been here. We have a
457 * cycle. Let's try to break it. We go backwards in
458 * our path and try to find a suitable job to
459 * remove. We use the marker to find our way back,
460 * since smart how we are we stored our way back in
463 log_debug("Found cycle on %s/%s", unit_id(j->unit), job_type_to_string(j->type));
465 for (k = from; k; k = (k->generation == generation ? k->marker : NULL)) {
467 log_debug("Walked on cycle path to %s/%s", unit_id(j->unit), job_type_to_string(j->type));
470 !unit_matters_to_anchor(k->unit, k)) {
471 /* Ok, we can drop this one, so let's
473 log_debug("Breaking order cycle by deleting job %s/%s", unit_id(k->unit), job_type_to_string(k->type));
474 transaction_delete_unit(m, k->unit);
478 /* Check if this in fact was the beginning of
484 log_debug("Unable to break cycle");
489 /* Make the marker point to where we come from, so that we can
490 * find our way backwards if we want to break a cycle */
492 j->generation = generation;
494 /* We assume that the the dependencies are bidirectional, and
495 * hence can ignore UNIT_AFTER */
496 SET_FOREACH(u, j->unit->meta.dependencies[UNIT_BEFORE], i) {
499 /* Is there a job for this unit? */
500 if (!(o = hashmap_get(m->transaction_jobs, u)))
502 /* Ok, there is no job for this in the
503 * transaction, but maybe there is already one
505 if (!(o = u->meta.job))
508 if ((r = transaction_verify_order_one(m, o, j, generation)) < 0)
512 /* Ok, let's backtrack, and remember that this entry is not on
513 * our path anymore. */
519 static int transaction_verify_order(Manager *m, unsigned *generation) {
527 /* Check if the ordering graph is cyclic. If it is, try to fix
528 * that up by dropping one of the jobs. */
530 HASHMAP_FOREACH(j, m->transaction_jobs, i)
531 if ((r = transaction_verify_order_one(m, j, NULL, (*generation)++)) < 0)
537 static void transaction_collect_garbage(Manager *m) {
542 /* Drop jobs that are not required by any other job */
550 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
554 log_debug("Garbage collecting job %s/%s", unit_id(j->unit), job_type_to_string(j->type));
555 transaction_delete_job(m, j);
563 static int transaction_is_destructive(Manager *m, JobMode mode) {
569 /* Checks whether applying this transaction means that
570 * existing jobs would be replaced */
572 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
575 assert(!j->transaction_prev);
576 assert(!j->transaction_next);
578 if (j->unit->meta.job &&
579 j->unit->meta.job != j &&
580 !job_type_is_superset(j->type, j->unit->meta.job->type))
587 static void transaction_minimize_impact(Manager *m) {
591 /* Drops all unnecessary jobs that reverse already active jobs
592 * or that stop a running service. */
600 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
601 LIST_FOREACH(transaction, j, j) {
602 bool stops_running_service, changes_existing_job;
604 /* If it matters, we shouldn't drop it */
605 if (j->matters_to_anchor)
608 /* Would this stop a running service?
609 * Would this change an existing job?
610 * If so, let's drop this entry */
612 stops_running_service =
613 j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
615 changes_existing_job =
616 j->unit->meta.job && job_type_is_conflicting(j->type, j->unit->meta.job->state);
618 if (!stops_running_service && !changes_existing_job)
621 if (stops_running_service)
622 log_debug("%s/%s would stop a running service.", unit_id(j->unit), job_type_to_string(j->type));
624 if (changes_existing_job)
625 log_debug("%s/%s would change existing job.", unit_id(j->unit), job_type_to_string(j->type));
627 /* Ok, let's get rid of this */
628 log_debug("Deleting %s/%s to minimize impact.", unit_id(j->unit), job_type_to_string(j->type));
630 transaction_delete_job(m, j);
642 static int transaction_apply(Manager *m, JobMode mode) {
647 /* Moves the transaction jobs to the set of active jobs */
649 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
651 assert(!j->transaction_prev);
652 assert(!j->transaction_next);
657 if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
661 while ((j = hashmap_steal_first(m->transaction_jobs))) {
665 if (j->unit->meta.job)
666 job_free(j->unit->meta.job);
668 j->unit->meta.job = j;
671 /* We're fully installed. Now let's free data we don't
674 assert(!j->transaction_next);
675 assert(!j->transaction_prev);
677 job_add_to_run_queue(j);
678 job_add_to_dbus_queue(j);
681 /* As last step, kill all remaining job dependencies. */
682 transaction_clean_dependencies(m);
688 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
692 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
698 static int transaction_activate(Manager *m, JobMode mode) {
700 unsigned generation = 1;
704 /* This applies the changes recorded in transaction_jobs to
705 * the actual list of jobs, if possible. */
707 /* First step: figure out which jobs matter */
708 transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
710 /* Second step: Try not to stop any running services if
711 * we don't have to. Don't try to reverse running
712 * jobs if we don't have to. */
713 transaction_minimize_impact(m);
716 /* Third step: Let's remove unneeded jobs that might
718 transaction_collect_garbage(m);
720 /* Fourth step: verify order makes sense and correct
721 * cycles if necessary and possible */
722 if ((r = transaction_verify_order(m, &generation)) >= 0)
726 log_debug("Requested transaction contains an unfixable cyclic ordering dependency: %s", strerror(-r));
730 /* Let's see if the resulting transaction ordering
731 * graph is still cyclic... */
735 /* Fifth step: let's drop unmergeable entries if
736 * necessary and possible, merge entries we can
738 if ((r = transaction_merge_jobs(m)) >= 0)
742 log_debug("Requested transaction contains unmergable jobs: %s", strerror(-r));
746 /* Sixth step: an entry got dropped, let's garbage
747 * collect its dependencies. */
748 transaction_collect_garbage(m);
750 /* Let's see if the resulting transaction still has
751 * unmergeable entries ... */
754 /* Seventh step: check whether we can actually apply this */
755 if (mode == JOB_FAIL)
756 if ((r = transaction_is_destructive(m, mode)) < 0) {
757 log_debug("Requested transaction contradicts existing jobs: %s", strerror(-r));
761 /* Eights step: apply changes */
762 if ((r = transaction_apply(m, mode)) < 0) {
763 log_debug("Failed to apply transaction: %s", strerror(-r));
767 assert(hashmap_isempty(m->transaction_jobs));
768 assert(!m->transaction_anchor);
773 transaction_abort(m);
777 static Job* transaction_add_one_job(Manager *m, JobType type, Unit *unit, bool force, bool *is_new) {
784 /* Looks for an axisting prospective job and returns that. If
785 * it doesn't exist it is created and added to the prospective
788 f = hashmap_get(m->transaction_jobs, unit);
790 LIST_FOREACH(transaction, j, f) {
791 assert(j->unit == unit);
793 if (j->type == type) {
800 if (unit->meta.job && unit->meta.job->type == type)
802 else if (!(j = job_new(m, type, unit)))
807 j->matters_to_anchor = false;
810 LIST_PREPEND(Job, transaction, f, j);
812 if ((r = hashmap_replace(m->transaction_jobs, unit, f)) < 0) {
823 void manager_transaction_unlink_job(Manager *m, Job *j) {
827 if (j->transaction_prev)
828 j->transaction_prev->transaction_next = j->transaction_next;
829 else if (j->transaction_next)
830 hashmap_replace(m->transaction_jobs, j->unit, j->transaction_next);
832 hashmap_remove_value(m->transaction_jobs, j->unit, j);
834 if (j->transaction_next)
835 j->transaction_next->transaction_prev = j->transaction_prev;
837 j->transaction_prev = j->transaction_next = NULL;
839 while (j->subject_list)
840 job_dependency_free(j->subject_list);
842 while (j->object_list) {
843 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
845 job_dependency_free(j->object_list);
848 log_debug("Deleting job %s/%s as dependency of job %s/%s",
849 unit_id(other->unit), job_type_to_string(other->type),
850 unit_id(j->unit), job_type_to_string(j->type));
851 transaction_delete_job(m, other);
856 static int transaction_add_job_and_dependencies(Manager *m, JobType type, Unit *unit, Job *by, bool matters, bool force, Job **_ret) {
864 assert(type < _JOB_TYPE_MAX);
867 if (unit->meta.load_state != UNIT_LOADED)
870 if (!unit_job_is_applicable(unit, type))
873 /* First add the job. */
874 if (!(ret = transaction_add_one_job(m, type, unit, force, &is_new)))
877 /* Then, add a link to the job. */
878 if (!job_dependency_new(by, ret, matters))
882 /* Finally, recursively add in all dependencies. */
883 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
884 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES], i)
885 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
887 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_SOFT_REQUIRES], i)
888 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !force, force, NULL)) < 0 && r != -EBADR)
890 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_WANTS], i)
891 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, force, NULL)) < 0 && r != -EBADR)
893 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE], i)
894 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
896 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_SOFT_REQUISITE], i)
897 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, !force, force, NULL)) < 0 && r != -EBADR)
899 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTS], i)
900 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
903 } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
905 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRED_BY], i)
906 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
910 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
922 int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool force, Job **_ret) {
927 assert(type < _JOB_TYPE_MAX);
929 assert(mode < _JOB_MODE_MAX);
931 log_debug("Trying to enqueue job %s/%s", unit_id(unit), job_type_to_string(type));
933 if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, force, &ret)) < 0) {
934 transaction_abort(m);
938 if ((r = transaction_activate(m, mode)) < 0)
941 log_debug("Enqueued job %s/%s as %u", unit_id(unit), job_type_to_string(type), (unsigned) ret->id);
949 Job *manager_get_job(Manager *m, uint32_t id) {
952 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
955 Unit *manager_get_unit(Manager *m, const char *name) {
959 return hashmap_get(m->units, name);
962 unsigned manager_dispatch_load_queue(Manager *m) {
968 /* Make sure we are not run recursively */
969 if (m->dispatching_load_queue)
972 m->dispatching_load_queue = true;
974 /* Dispatches the load queue. Takes a unit from the queue and
975 * tries to load its data until the queue is empty */
977 while ((meta = m->load_queue)) {
978 assert(meta->in_load_queue);
980 unit_load(UNIT(meta));
984 m->dispatching_load_queue = false;
988 int manager_load_unit(Manager *m, const char *path, Unit **_ret) {
997 /* This will load the service information files, but not actually
998 * start any services or anything. */
1000 name = file_name_from_path(path);
1002 if ((ret = manager_get_unit(m, name))) {
1007 if (!(ret = unit_new(m)))
1010 if (is_path(path)) {
1011 if (!(ret->meta.load_path = strdup(path))) {
1017 if ((r = unit_add_name(ret, name)) < 0) {
1022 unit_add_to_load_queue(ret);
1023 unit_add_to_dbus_queue(ret);
1025 manager_dispatch_load_queue(m);
1031 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
1038 HASHMAP_FOREACH(j, s->jobs, i)
1039 job_dump(j, f, prefix);
1042 void manager_dump_units(Manager *s, FILE *f, const char *prefix) {
1050 HASHMAP_FOREACH_KEY(u, t, s->units, i)
1051 if (unit_id(u) == t)
1052 unit_dump(u, f, prefix);
1055 void manager_clear_jobs(Manager *m) {
1060 transaction_abort(m);
1062 while ((j = hashmap_first(m->jobs)))
1066 unsigned manager_dispatch_run_queue(Manager *m) {
1070 if (m->dispatching_run_queue)
1073 m->dispatching_run_queue = true;
1075 while ((j = m->run_queue)) {
1076 assert(j->installed);
1077 assert(j->in_run_queue);
1079 job_run_and_invalidate(j);
1083 m->dispatching_run_queue = false;
1087 unsigned manager_dispatch_dbus_queue(Manager *m) {
1094 if (m->dispatching_dbus_queue)
1097 m->dispatching_dbus_queue = true;
1099 while ((meta = m->dbus_unit_queue)) {
1100 Unit *u = (Unit*) meta;
1101 assert(u->meta.in_dbus_queue);
1103 bus_unit_send_change_signal(u);
1107 while ((j = m->dbus_job_queue)) {
1108 assert(j->in_dbus_queue);
1110 bus_job_send_change_signal(j);
1114 m->dispatching_dbus_queue = false;
1118 static int manager_dispatch_sigchld(Manager *m) {
1121 log_debug("dispatching SIGCHLD");
1128 if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG) < 0) {
1130 if (errno == ECHILD)
1139 if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED)
1142 log_debug("child %llu died (code=%s, status=%i)", (long long unsigned) si.si_pid, sigchld_code_to_string(si.si_code), si.si_status);
1144 if (!(u = hashmap_remove(m->watch_pids, UINT32_TO_PTR(si.si_pid))))
1147 UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status);
1153 static int manager_process_signal_fd(Manager *m, bool *quit) {
1155 struct signalfd_siginfo sfsi;
1156 bool sigchld = false;
1161 if ((n = read(m->signal_watch.fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
1166 if (errno == EAGAIN)
1172 switch (sfsi.ssi_signo) {
1184 log_info("Got unhandled signal <%s>.", strsignal(sfsi.ssi_signo));
1189 return manager_dispatch_sigchld(m);
1194 static int process_event(Manager *m, struct epoll_event *ev, bool *quit) {
1201 assert(w = ev->data.ptr);
1207 /* An incoming signal? */
1208 if (ev->events != EPOLLIN)
1211 if ((r = manager_process_signal_fd(m, quit)) < 0)
1218 /* Some fd event, to be dispatched to the units */
1219 UNIT_VTABLE(w->data.unit)->fd_event(w->data.unit, w->fd, ev->events, w);
1226 /* Some timer event, to be dispatched to the units */
1227 if ((k = read(w->fd, &v, sizeof(v))) != sizeof(v)) {
1229 if (k < 0 && (errno == EINTR || errno == EAGAIN))
1232 return k < 0 ? -errno : -EIO;
1235 UNIT_VTABLE(w->data.unit)->timer_event(w->data.unit, v, w);
1240 /* Some mount table change, intended for the mount subsystem */
1241 mount_fd_event(m, ev->events);
1245 /* Some notification from udev, intended for the device subsystem */
1246 device_fd_event(m, ev->events);
1249 case WATCH_DBUS_WATCH:
1250 bus_watch_event(m, w, ev->events);
1253 case WATCH_DBUS_TIMEOUT:
1254 bus_timeout_event(m, w, ev->events);
1258 assert_not_reached("Unknown epoll event type.");
1264 int manager_loop(Manager *m) {
1268 RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 1000);
1273 struct epoll_event event;
1276 if (!ratelimit_test(&rl)) {
1277 /* Yay, something is going seriously wrong, pause a little */
1278 log_warning("Looping too fast. Throttling execution a little.");
1282 if (manager_dispatch_load_queue(m) > 0)
1285 if (manager_dispatch_run_queue(m) > 0)
1288 if (bus_dispatch(m) > 0)
1291 if (manager_dispatch_dbus_queue(m) > 0)
1294 if ((n = epoll_wait(m->epoll_fd, &event, 1, -1)) < 0) {
1296 if (errno == -EINTR)
1304 if ((r = process_event(m, &event, &quit)) < 0)
1312 int manager_get_unit_from_dbus_path(Manager *m, const char *s, Unit **_u) {
1320 if (!startswith(s, "/org/freedesktop/systemd1/unit/"))
1323 if (!(n = bus_path_unescape(s+31)))
1326 u = manager_get_unit(m, n);
1337 int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
1346 if (!startswith(s, "/org/freedesktop/systemd1/job/"))
1349 if ((r = safe_atou(s + 30, &id)) < 0)
1352 if (!(j = manager_get_job(m, id)))
1360 static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = {
1361 [MANAGER_INIT] = "init",
1362 [MANAGER_SYSTEM] = "system",
1363 [MANAGER_USER] = "user"
1366 DEFINE_STRING_TABLE_LOOKUP(manager_running_as, ManagerRunningAs);