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 static char** session_dirs(void) {
75 char *config_home = NULL, *data_home = NULL;
76 char **config_dirs = NULL, **data_dirs = NULL;
79 /* Implement the mechanisms defined in
81 * http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
83 * We look in both the config and the data dirs because we
84 * want to encourage that distributors ship their unit files
85 * as data, and allow overriding as configuration.
88 home = getenv("HOME");
90 if ((e = getenv("XDG_CONFIG_HOME"))) {
91 if (asprintf(&config_home, "%s/systemd/session", e) < 0)
95 if (asprintf(&config_home, "%s/.config/systemd/session", home) < 0)
99 if ((e = getenv("XDG_CONFIG_DIRS")))
100 config_dirs = strv_split(e, ":");
102 config_dirs = strv_new("/etc/xdg", NULL);
107 if ((e = getenv("XDG_DATA_HOME"))) {
108 if (asprintf(&data_home, "%s/systemd/session", e) < 0)
112 if (asprintf(&data_home, "%s/.local/share/systemd/session", home) < 0)
116 if ((e = getenv("XDG_DATA_DIRS")))
117 data_dirs = strv_split(e, ":");
119 data_dirs = strv_new("/usr/local/share", "/usr/share", NULL);
124 /* Now merge everything we found. */
126 if (!(t = strv_append(r, config_home)))
132 if (!(t = strv_merge_concat(r, config_dirs, "/systemd/session")))
137 if (!(t = strv_append(r, SESSION_CONFIG_UNIT_PATH)))
143 if (!(t = strv_append(r, data_home)))
149 if (!(t = strv_merge_concat(r, data_dirs, "/systemd/session")))
154 if (!(t = strv_append(r, SESSION_DATA_UNIT_PATH)))
159 if (!strv_path_make_absolute_cwd(r))
164 strv_free(config_dirs);
166 strv_free(data_dirs);
176 static int manager_find_paths(Manager *m) {
181 /* First priority is whatever has been passed to us via env
183 if ((e = getenv("SYSTEMD_UNIT_PATH")))
184 if (!(m->unit_path = split_path_and_make_absolute(e)))
187 if (strv_isempty(m->unit_path)) {
189 /* Nothing is set, so let's figure something out. */
190 strv_free(m->unit_path);
192 if (m->running_as == MANAGER_SESSION) {
193 if (!(m->unit_path = session_dirs()))
196 if (!(m->unit_path = strv_new(
197 SYSTEM_CONFIG_UNIT_PATH, /* /etc/systemd/system/ */
198 SYSTEM_DATA_UNIT_PATH, /* /lib/systemd/system/ */
203 /* FIXME: This should probably look for MANAGER_INIT, and exclude MANAGER_SYSTEM */
204 if (m->running_as != MANAGER_SESSION) {
205 /* /etc/init.d/ compativility does not matter to users */
207 if ((e = getenv("SYSTEMD_SYSVINIT_PATH")))
208 if (!(m->sysvinit_path = split_path_and_make_absolute(e)))
211 if (strv_isempty(m->sysvinit_path)) {
212 strv_free(m->sysvinit_path);
214 if (!(m->sysvinit_path = strv_new(
215 SYSTEM_SYSVINIT_PATH, /* /etc/init.d/ */
221 strv_uniq(m->unit_path);
222 strv_uniq(m->sysvinit_path);
224 assert(!strv_isempty(m->unit_path));
225 if (!(t = strv_join(m->unit_path, "\n\t")))
227 log_debug("Looking for unit files in:\n\t%s", t);
230 if (!strv_isempty(m->sysvinit_path)) {
232 if (!(t = strv_join(m->sysvinit_path, "\n\t")))
235 log_debug("Looking for SysV init scripts in:\n\t%s", t);
238 log_debug("Ignoring SysV init scripts.");
243 Manager* manager_new(void) {
246 if (!(m = new0(Manager, 1)))
250 m->running_as = MANAGER_INIT;
251 else if (getuid() == 0)
252 m->running_as = MANAGER_SYSTEM;
254 m->running_as = MANAGER_SESSION;
256 log_debug("systemd running in %s mode.", manager_running_as_to_string(m->running_as));
258 m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = -1;
259 m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
261 if (manager_find_paths(m) < 0)
264 if (!(m->units = hashmap_new(string_hash_func, string_compare_func)))
267 if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
270 if (!(m->transaction_jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
273 if (!(m->watch_pids = hashmap_new(trivial_hash_func, trivial_compare_func)))
276 if ((m->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
279 if (manager_setup_signals(m) < 0)
282 /* FIXME: this should be called only when the D-Bus bus daemon is running */
293 void manager_free(Manager *m) {
300 while ((j = hashmap_first(m->transaction_jobs)))
303 while ((u = hashmap_first(m->units)))
306 for (c = 0; c < _UNIT_TYPE_MAX; c++)
307 if (unit_vtable[c]->shutdown)
308 unit_vtable[c]->shutdown(m);
312 hashmap_free(m->units);
313 hashmap_free(m->jobs);
314 hashmap_free(m->transaction_jobs);
315 hashmap_free(m->watch_pids);
317 if (m->epoll_fd >= 0)
318 close_nointr(m->epoll_fd);
319 if (m->signal_watch.fd >= 0)
320 close_nointr(m->signal_watch.fd);
322 strv_free(m->unit_path);
323 strv_free(m->sysvinit_path);
328 int manager_coldplug(Manager *m) {
337 /* First, let's ask every type to load all units from
338 * disk/kernel that it might know */
339 for (c = 0; c < _UNIT_TYPE_MAX; c++)
340 if (unit_vtable[c]->enumerate)
341 if ((r = unit_vtable[c]->enumerate(m)) < 0)
344 manager_dispatch_load_queue(m);
346 /* Then, let's set up their initial state. */
347 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
353 if (UNIT_VTABLE(u)->coldplug)
354 if ((r = UNIT_VTABLE(u)->coldplug(u)) < 0)
361 static void transaction_delete_job(Manager *m, Job *j) {
365 /* Deletes one job from the transaction */
367 manager_transaction_unlink_job(m, j);
373 static void transaction_delete_unit(Manager *m, Unit *u) {
376 /* Deletes all jobs associated with a certain unit from the
379 while ((j = hashmap_get(m->transaction_jobs, u)))
380 transaction_delete_job(m, j);
383 static void transaction_clean_dependencies(Manager *m) {
389 /* Drops all dependencies of all installed jobs */
391 HASHMAP_FOREACH(j, m->jobs, i) {
392 while (j->subject_list)
393 job_dependency_free(j->subject_list);
394 while (j->object_list)
395 job_dependency_free(j->object_list);
398 assert(!m->transaction_anchor);
401 static void transaction_abort(Manager *m) {
406 while ((j = hashmap_first(m->transaction_jobs)))
408 transaction_delete_job(m, j);
412 assert(hashmap_isempty(m->transaction_jobs));
414 transaction_clean_dependencies(m);
417 static void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) {
422 /* A recursive sweep through the graph that marks all units
423 * that matter to the anchor job, i.e. are directly or
424 * indirectly a dependency of the anchor job via paths that
425 * are fully marked as mattering. */
430 l = m->transaction_anchor;
432 LIST_FOREACH(subject, l, l) {
434 /* This link does not matter */
438 /* This unit has already been marked */
439 if (l->object->generation == generation)
442 l->object->matters_to_anchor = true;
443 l->object->generation = generation;
445 transaction_find_jobs_that_matter_to_anchor(m, l->object, generation);
449 static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) {
450 JobDependency *l, *last;
454 assert(j->unit == other->unit);
455 assert(!j->installed);
457 /* Merges 'other' into 'j' and then deletes j. */
460 j->state = JOB_WAITING;
461 j->forced = j->forced || other->forced;
463 j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
465 /* Patch us in as new owner of the JobDependency objects */
467 LIST_FOREACH(subject, l, other->subject_list) {
468 assert(l->subject == other);
473 /* Merge both lists */
475 last->subject_next = j->subject_list;
477 j->subject_list->subject_prev = last;
478 j->subject_list = other->subject_list;
481 /* Patch us in as new owner of the JobDependency objects */
483 LIST_FOREACH(object, l, other->object_list) {
484 assert(l->object == other);
489 /* Merge both lists */
491 last->object_next = j->object_list;
493 j->object_list->object_prev = last;
494 j->object_list = other->object_list;
497 /* Kill the other job */
498 other->subject_list = NULL;
499 other->object_list = NULL;
500 transaction_delete_job(m, other);
503 static int delete_one_unmergeable_job(Manager *m, Job *j) {
508 /* Tries to delete one item in the linked list
509 * j->transaction_next->transaction_next->... that conflicts
510 * whith another one, in an attempt to make an inconsistent
511 * transaction work. */
513 /* We rely here on the fact that if a merged with b does not
514 * merge with c, either a or b merge with c neither */
515 LIST_FOREACH(transaction, j, j)
516 LIST_FOREACH(transaction, k, j->transaction_next) {
519 /* Is this one mergeable? Then skip it */
520 if (job_type_is_mergeable(j->type, k->type))
523 /* Ok, we found two that conflict, let's see if we can
524 * drop one of them */
525 if (!j->matters_to_anchor)
527 else if (!k->matters_to_anchor)
532 /* Ok, we can drop one, so let's do so. */
533 log_debug("Try to fix job merging by deleting job %s/%s", unit_id(d->unit), job_type_to_string(d->type));
534 transaction_delete_job(m, d);
541 static int transaction_merge_jobs(Manager *m) {
548 /* First step, check whether any of the jobs for one specific
549 * task conflict. If so, try to drop one of them. */
550 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
555 LIST_FOREACH(transaction, k, j->transaction_next) {
556 if ((r = job_type_merge(&t, k->type)) >= 0)
559 /* OK, we could not merge all jobs for this
560 * action. Let's see if we can get rid of one
563 if ((r = delete_one_unmergeable_job(m, j)) >= 0)
564 /* Ok, we managed to drop one, now
565 * let's ask our callers to call us
566 * again after garbage collecting */
569 /* We couldn't merge anything. Failure */
574 /* Second step, merge the jobs. */
575 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
579 /* Merge all transactions */
580 LIST_FOREACH(transaction, k, j->transaction_next)
581 assert_se(job_type_merge(&t, k->type) == 0);
583 /* If an active job is mergeable, merge it too */
584 if (j->unit->meta.job)
585 job_type_merge(&t, j->unit->meta.job->type); /* Might fail. Which is OK */
587 while ((k = j->transaction_next)) {
589 transaction_merge_and_delete_job(m, k, j, t);
592 transaction_merge_and_delete_job(m, j, k, t);
595 assert(!j->transaction_next);
596 assert(!j->transaction_prev);
602 static bool unit_matters_to_anchor(Unit *u, Job *j) {
604 assert(!j->transaction_prev);
606 /* Checks whether at least one of the jobs for this unit
607 * matters to the anchor. */
609 LIST_FOREACH(transaction, j, j)
610 if (j->matters_to_anchor)
616 static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation) {
623 assert(!j->transaction_prev);
625 /* Does a recursive sweep through the ordering graph, looking
626 * for a cycle. If we find cycle we try to break it. */
628 /* Did we find a cycle? */
629 if (j->marker && j->generation == generation) {
632 /* So, we already have been here. We have a
633 * cycle. Let's try to break it. We go backwards in
634 * our path and try to find a suitable job to
635 * remove. We use the marker to find our way back,
636 * since smart how we are we stored our way back in
639 log_debug("Found cycle on %s/%s", unit_id(j->unit), job_type_to_string(j->type));
641 for (k = from; k; k = (k->generation == generation ? k->marker : NULL)) {
643 log_debug("Walked on cycle path to %s/%s", unit_id(j->unit), job_type_to_string(j->type));
646 !unit_matters_to_anchor(k->unit, k)) {
647 /* Ok, we can drop this one, so let's
649 log_debug("Breaking order cycle by deleting job %s/%s", unit_id(k->unit), job_type_to_string(k->type));
650 transaction_delete_unit(m, k->unit);
654 /* Check if this in fact was the beginning of
660 log_debug("Unable to break cycle");
665 /* Make the marker point to where we come from, so that we can
666 * find our way backwards if we want to break a cycle */
668 j->generation = generation;
670 /* We assume that the the dependencies are bidirectional, and
671 * hence can ignore UNIT_AFTER */
672 SET_FOREACH(u, j->unit->meta.dependencies[UNIT_BEFORE], i) {
675 /* Is there a job for this unit? */
676 if (!(o = hashmap_get(m->transaction_jobs, u)))
678 /* Ok, there is no job for this in the
679 * transaction, but maybe there is already one
681 if (!(o = u->meta.job))
684 if ((r = transaction_verify_order_one(m, o, j, generation)) < 0)
688 /* Ok, let's backtrack, and remember that this entry is not on
689 * our path anymore. */
695 static int transaction_verify_order(Manager *m, unsigned *generation) {
703 /* Check if the ordering graph is cyclic. If it is, try to fix
704 * that up by dropping one of the jobs. */
706 HASHMAP_FOREACH(j, m->transaction_jobs, i)
707 if ((r = transaction_verify_order_one(m, j, NULL, (*generation)++)) < 0)
713 static void transaction_collect_garbage(Manager *m) {
718 /* Drop jobs that are not required by any other job */
726 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
730 log_debug("Garbage collecting job %s/%s", unit_id(j->unit), job_type_to_string(j->type));
731 transaction_delete_job(m, j);
739 static int transaction_is_destructive(Manager *m, JobMode mode) {
745 /* Checks whether applying this transaction means that
746 * existing jobs would be replaced */
748 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
751 assert(!j->transaction_prev);
752 assert(!j->transaction_next);
754 if (j->unit->meta.job &&
755 j->unit->meta.job != j &&
756 !job_type_is_superset(j->type, j->unit->meta.job->type))
763 static void transaction_minimize_impact(Manager *m) {
767 /* Drops all unnecessary jobs that reverse already active jobs
768 * or that stop a running service. */
776 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
777 LIST_FOREACH(transaction, j, j) {
778 bool stops_running_service, changes_existing_job;
780 /* If it matters, we shouldn't drop it */
781 if (j->matters_to_anchor)
784 /* Would this stop a running service?
785 * Would this change an existing job?
786 * If so, let's drop this entry */
788 stops_running_service =
789 j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
791 changes_existing_job =
792 j->unit->meta.job && job_type_is_conflicting(j->type, j->unit->meta.job->state);
794 if (!stops_running_service && !changes_existing_job)
797 if (stops_running_service)
798 log_debug("%s/%s would stop a running service.", unit_id(j->unit), job_type_to_string(j->type));
800 if (changes_existing_job)
801 log_debug("%s/%s would change existing job.", unit_id(j->unit), job_type_to_string(j->type));
803 /* Ok, let's get rid of this */
804 log_debug("Deleting %s/%s to minimize impact.", unit_id(j->unit), job_type_to_string(j->type));
806 transaction_delete_job(m, j);
818 static int transaction_apply(Manager *m, JobMode mode) {
823 /* Moves the transaction jobs to the set of active jobs */
825 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
827 assert(!j->transaction_prev);
828 assert(!j->transaction_next);
833 if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
837 while ((j = hashmap_steal_first(m->transaction_jobs))) {
841 if (j->unit->meta.job)
842 job_free(j->unit->meta.job);
844 j->unit->meta.job = j;
847 /* We're fully installed. Now let's free data we don't
850 assert(!j->transaction_next);
851 assert(!j->transaction_prev);
853 job_add_to_run_queue(j);
854 job_add_to_dbus_queue(j);
857 /* As last step, kill all remaining job dependencies. */
858 transaction_clean_dependencies(m);
864 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
868 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
874 static int transaction_activate(Manager *m, JobMode mode) {
876 unsigned generation = 1;
880 /* This applies the changes recorded in transaction_jobs to
881 * the actual list of jobs, if possible. */
883 /* First step: figure out which jobs matter */
884 transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
886 /* Second step: Try not to stop any running services if
887 * we don't have to. Don't try to reverse running
888 * jobs if we don't have to. */
889 transaction_minimize_impact(m);
892 /* Third step: Let's remove unneeded jobs that might
894 transaction_collect_garbage(m);
896 /* Fourth step: verify order makes sense and correct
897 * cycles if necessary and possible */
898 if ((r = transaction_verify_order(m, &generation)) >= 0)
902 log_debug("Requested transaction contains an unfixable cyclic ordering dependency: %s", strerror(-r));
906 /* Let's see if the resulting transaction ordering
907 * graph is still cyclic... */
911 /* Fifth step: let's drop unmergeable entries if
912 * necessary and possible, merge entries we can
914 if ((r = transaction_merge_jobs(m)) >= 0)
918 log_debug("Requested transaction contains unmergable jobs: %s", strerror(-r));
922 /* Sixth step: an entry got dropped, let's garbage
923 * collect its dependencies. */
924 transaction_collect_garbage(m);
926 /* Let's see if the resulting transaction still has
927 * unmergeable entries ... */
930 /* Seventh step: check whether we can actually apply this */
931 if (mode == JOB_FAIL)
932 if ((r = transaction_is_destructive(m, mode)) < 0) {
933 log_debug("Requested transaction contradicts existing jobs: %s", strerror(-r));
937 /* Eights step: apply changes */
938 if ((r = transaction_apply(m, mode)) < 0) {
939 log_debug("Failed to apply transaction: %s", strerror(-r));
943 assert(hashmap_isempty(m->transaction_jobs));
944 assert(!m->transaction_anchor);
949 transaction_abort(m);
953 static Job* transaction_add_one_job(Manager *m, JobType type, Unit *unit, bool force, bool *is_new) {
960 /* Looks for an axisting prospective job and returns that. If
961 * it doesn't exist it is created and added to the prospective
964 f = hashmap_get(m->transaction_jobs, unit);
966 LIST_FOREACH(transaction, j, f) {
967 assert(j->unit == unit);
969 if (j->type == type) {
976 if (unit->meta.job && unit->meta.job->type == type)
978 else if (!(j = job_new(m, type, unit)))
983 j->matters_to_anchor = false;
986 LIST_PREPEND(Job, transaction, f, j);
988 if ((r = hashmap_replace(m->transaction_jobs, unit, f)) < 0) {
999 void manager_transaction_unlink_job(Manager *m, Job *j) {
1003 if (j->transaction_prev)
1004 j->transaction_prev->transaction_next = j->transaction_next;
1005 else if (j->transaction_next)
1006 hashmap_replace(m->transaction_jobs, j->unit, j->transaction_next);
1008 hashmap_remove_value(m->transaction_jobs, j->unit, j);
1010 if (j->transaction_next)
1011 j->transaction_next->transaction_prev = j->transaction_prev;
1013 j->transaction_prev = j->transaction_next = NULL;
1015 while (j->subject_list)
1016 job_dependency_free(j->subject_list);
1018 while (j->object_list) {
1019 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
1021 job_dependency_free(j->object_list);
1024 log_debug("Deleting job %s/%s as dependency of job %s/%s",
1025 unit_id(other->unit), job_type_to_string(other->type),
1026 unit_id(j->unit), job_type_to_string(j->type));
1027 transaction_delete_job(m, other);
1032 static int transaction_add_job_and_dependencies(Manager *m, JobType type, Unit *unit, Job *by, bool matters, bool force, Job **_ret) {
1040 assert(type < _JOB_TYPE_MAX);
1043 if (unit->meta.load_state != UNIT_LOADED)
1046 if (!unit_job_is_applicable(unit, type))
1049 /* First add the job. */
1050 if (!(ret = transaction_add_one_job(m, type, unit, force, &is_new)))
1053 /* Then, add a link to the job. */
1054 if (!job_dependency_new(by, ret, matters))
1058 /* Finally, recursively add in all dependencies. */
1059 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
1060 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES], i)
1061 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
1063 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_SOFT_REQUIRES], i)
1064 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !force, force, NULL)) < 0 && r != -EBADR)
1066 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_WANTS], i)
1067 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, force, NULL)) < 0 && r != -EBADR)
1069 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE], i)
1070 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
1072 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_SOFT_REQUISITE], i)
1073 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, !force, force, NULL)) < 0 && r != -EBADR)
1075 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTS], i)
1076 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
1079 } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
1081 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRED_BY], i)
1082 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, force, NULL)) < 0 && r != -EBADR)
1086 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
1098 int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool force, Job **_ret) {
1103 assert(type < _JOB_TYPE_MAX);
1105 assert(mode < _JOB_MODE_MAX);
1107 log_debug("Trying to enqueue job %s/%s", unit_id(unit), job_type_to_string(type));
1109 if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, force, &ret)) < 0) {
1110 transaction_abort(m);
1114 if ((r = transaction_activate(m, mode)) < 0)
1117 log_debug("Enqueued job %s/%s as %u", unit_id(unit), job_type_to_string(type), (unsigned) ret->id);
1125 Job *manager_get_job(Manager *m, uint32_t id) {
1128 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
1131 Unit *manager_get_unit(Manager *m, const char *name) {
1135 return hashmap_get(m->units, name);
1138 unsigned manager_dispatch_load_queue(Manager *m) {
1144 /* Make sure we are not run recursively */
1145 if (m->dispatching_load_queue)
1148 m->dispatching_load_queue = true;
1150 /* Dispatches the load queue. Takes a unit from the queue and
1151 * tries to load its data until the queue is empty */
1153 while ((meta = m->load_queue)) {
1154 assert(meta->in_load_queue);
1156 unit_load(UNIT(meta));
1160 m->dispatching_load_queue = false;
1164 int manager_load_unit(Manager *m, const char *path, Unit **_ret) {
1173 /* This will load the service information files, but not actually
1174 * start any services or anything. */
1176 name = file_name_from_path(path);
1178 if ((ret = manager_get_unit(m, name))) {
1183 if (!(ret = unit_new(m)))
1186 if (is_path(path)) {
1187 if (!(ret->meta.fragment_path = strdup(path))) {
1193 if ((r = unit_add_name(ret, name)) < 0) {
1198 unit_add_to_load_queue(ret);
1199 unit_add_to_dbus_queue(ret);
1201 manager_dispatch_load_queue(m);
1207 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
1214 HASHMAP_FOREACH(j, s->jobs, i)
1215 job_dump(j, f, prefix);
1218 void manager_dump_units(Manager *s, FILE *f, const char *prefix) {
1226 HASHMAP_FOREACH_KEY(u, t, s->units, i)
1227 if (unit_id(u) == t)
1228 unit_dump(u, f, prefix);
1231 void manager_clear_jobs(Manager *m) {
1236 transaction_abort(m);
1238 while ((j = hashmap_first(m->jobs)))
1242 unsigned manager_dispatch_run_queue(Manager *m) {
1246 if (m->dispatching_run_queue)
1249 m->dispatching_run_queue = true;
1251 while ((j = m->run_queue)) {
1252 assert(j->installed);
1253 assert(j->in_run_queue);
1255 job_run_and_invalidate(j);
1259 m->dispatching_run_queue = false;
1263 unsigned manager_dispatch_dbus_queue(Manager *m) {
1270 if (m->dispatching_dbus_queue)
1273 m->dispatching_dbus_queue = true;
1275 while ((meta = m->dbus_unit_queue)) {
1276 Unit *u = (Unit*) meta;
1277 assert(u->meta.in_dbus_queue);
1279 bus_unit_send_change_signal(u);
1283 while ((j = m->dbus_job_queue)) {
1284 assert(j->in_dbus_queue);
1286 bus_job_send_change_signal(j);
1290 m->dispatching_dbus_queue = false;
1294 static int manager_dispatch_sigchld(Manager *m) {
1297 log_debug("dispatching SIGCHLD");
1304 if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG) < 0) {
1306 if (errno == ECHILD)
1315 if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED)
1318 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);
1320 if (!(u = hashmap_remove(m->watch_pids, UINT32_TO_PTR(si.si_pid))))
1323 UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status);
1329 static int manager_process_signal_fd(Manager *m, bool *quit) {
1331 struct signalfd_siginfo sfsi;
1332 bool sigchld = false;
1337 if ((n = read(m->signal_watch.fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
1342 if (errno == EAGAIN)
1348 switch (sfsi.ssi_signo) {
1357 if (m->running_as != MANAGER_INIT) {
1365 if ((r = manager_load_unit(m, SPECIAL_CTRL_ALT_DEL_TARGET, &target)) < 0)
1366 log_error("Failed to load ctrl-alt-del target: %s", strerror(-r));
1367 else if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, true, NULL)) < 0)
1368 log_error("Failed to enqueue ctrl-alt-del job: %s", strerror(-r));
1375 if (m->running_as == MANAGER_INIT) {
1379 if ((r = manager_load_unit(m, SPECIAL_KBREQUEST_TARGET, &target)) < 0)
1380 log_error("Failed to load kbrequest target: %s", strerror(-r));
1381 else if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, true, NULL)) < 0)
1382 log_error("Failed to enqueue kbrequest job: %s", strerror(-r));
1387 /* This is a nop on non-init systemd's */
1392 log_info("Got unhandled signal <%s>.", strsignal(sfsi.ssi_signo));
1397 return manager_dispatch_sigchld(m);
1402 static int process_event(Manager *m, struct epoll_event *ev, bool *quit) {
1409 assert(w = ev->data.ptr);
1415 /* An incoming signal? */
1416 if (ev->events != EPOLLIN)
1419 if ((r = manager_process_signal_fd(m, quit)) < 0)
1426 /* Some fd event, to be dispatched to the units */
1427 UNIT_VTABLE(w->data.unit)->fd_event(w->data.unit, w->fd, ev->events, w);
1434 /* Some timer event, to be dispatched to the units */
1435 if ((k = read(w->fd, &v, sizeof(v))) != sizeof(v)) {
1437 if (k < 0 && (errno == EINTR || errno == EAGAIN))
1440 return k < 0 ? -errno : -EIO;
1443 UNIT_VTABLE(w->data.unit)->timer_event(w->data.unit, v, w);
1448 /* Some mount table change, intended for the mount subsystem */
1449 mount_fd_event(m, ev->events);
1453 /* Some notification from udev, intended for the device subsystem */
1454 device_fd_event(m, ev->events);
1457 case WATCH_DBUS_WATCH:
1458 bus_watch_event(m, w, ev->events);
1461 case WATCH_DBUS_TIMEOUT:
1462 bus_timeout_event(m, w, ev->events);
1466 assert_not_reached("Unknown epoll event type.");
1472 int manager_loop(Manager *m) {
1476 RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 1000);
1481 struct epoll_event event;
1484 if (!ratelimit_test(&rl)) {
1485 /* Yay, something is going seriously wrong, pause a little */
1486 log_warning("Looping too fast. Throttling execution a little.");
1490 if (manager_dispatch_load_queue(m) > 0)
1493 if (manager_dispatch_run_queue(m) > 0)
1496 if (bus_dispatch(m) > 0)
1499 if (manager_dispatch_dbus_queue(m) > 0)
1502 if ((n = epoll_wait(m->epoll_fd, &event, 1, -1)) < 0) {
1504 if (errno == -EINTR)
1512 if ((r = process_event(m, &event, &quit)) < 0)
1520 int manager_get_unit_from_dbus_path(Manager *m, const char *s, Unit **_u) {
1528 if (!startswith(s, "/org/freedesktop/systemd1/unit/"))
1531 if (!(n = bus_path_unescape(s+31)))
1534 u = manager_get_unit(m, n);
1545 int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
1554 if (!startswith(s, "/org/freedesktop/systemd1/job/"))
1557 if ((r = safe_atou(s + 30, &id)) < 0)
1560 if (!(j = manager_get_job(m, id)))
1568 static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = {
1569 [MANAGER_INIT] = "init",
1570 [MANAGER_SYSTEM] = "system",
1571 [MANAGER_SESSION] = "session"
1574 DEFINE_STRING_TABLE_LOOKUP(manager_running_as, ManagerRunningAs);