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>
32 #include <sys/reboot.h>
33 #include <sys/ioctl.h>
35 #include <libcgroup.h>
38 #include <sys/types.h>
47 #include "ratelimit.h"
49 #include "mount-setup.h"
50 #include "utmp-wtmp.h"
51 #include "unit-name.h"
52 #include "dbus-unit.h"
55 #include "path-lookup.h"
57 /* As soon as 16 units are in our GC queue, make sure to run a gc sweep */
58 #define GC_QUEUE_ENTRIES_MAX 16
60 /* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */
61 #define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC)
63 /* Where clients shall send notification messages to */
64 #define NOTIFY_SOCKET "/org/freedesktop/systemd1/notify"
66 static int manager_setup_notify(Manager *m) {
69 struct sockaddr_un un;
71 struct epoll_event ev;
77 m->notify_watch.type = WATCH_NOTIFY;
78 if ((m->notify_watch.fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
79 log_error("Failed to allocate notification socket: %m");
84 sa.sa.sa_family = AF_UNIX;
86 if (m->running_as == MANAGER_SESSION)
87 snprintf(sa.un.sun_path+1, sizeof(sa.un.sun_path)-1, NOTIFY_SOCKET "/%llu", random_ull());
89 strncpy(sa.un.sun_path+1, NOTIFY_SOCKET, sizeof(sa.un.sun_path)-1);
91 if (bind(m->notify_watch.fd, &sa.sa, sizeof(sa)) < 0) {
92 log_error("bind() failed: %m");
96 if (setsockopt(m->notify_watch.fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
97 log_error("SO_PASSCRED failed: %m");
103 ev.data.ptr = &m->notify_watch;
105 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev) < 0)
108 if (asprintf(&ne[0], "NOTIFY_SOCKET=@%s", sa.un.sun_path+1) < 0)
112 t = strv_env_merge(m->environment, ne, NULL);
118 strv_free(m->environment);
124 static int enable_special_signals(Manager *m) {
129 /* Enable that we get SIGINT on control-alt-del */
130 if (reboot(RB_DISABLE_CAD) < 0)
131 log_warning("Failed to enable ctrl-alt-del handling: %m");
133 if ((fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY)) < 0)
134 log_warning("Failed to open /dev/tty0: %m");
136 /* Enable that we get SIGWINCH on kbrequest */
137 if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0)
138 log_warning("Failed to enable kbrequest handling: %s", strerror(errno));
140 close_nointr_nofail(fd);
146 static int manager_setup_signals(Manager *m) {
148 struct epoll_event ev;
153 /* We are not interested in SIGSTOP and friends. */
155 sa.sa_handler = SIG_DFL;
156 sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
157 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
159 assert_se(sigemptyset(&mask) == 0);
160 assert_se(sigaddset(&mask, SIGCHLD) == 0);
161 assert_se(sigaddset(&mask, SIGTERM) == 0);
162 assert_se(sigaddset(&mask, SIGHUP) == 0);
163 assert_se(sigaddset(&mask, SIGUSR1) == 0);
164 assert_se(sigaddset(&mask, SIGUSR2) == 0);
165 assert_se(sigaddset(&mask, SIGINT) == 0); /* Kernel sends us this on control-alt-del */
166 assert_se(sigaddset(&mask, SIGWINCH) == 0); /* Kernel sends us this on kbrequest (alt-arrowup) */
167 assert_se(sigaddset(&mask, SIGPWR) == 0); /* Some kernel drivers and upsd send us this on power failure */
168 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
170 m->signal_watch.type = WATCH_SIGNAL;
171 if ((m->signal_watch.fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0)
176 ev.data.ptr = &m->signal_watch;
178 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0)
181 if (m->running_as == MANAGER_INIT)
182 return enable_special_signals(m);
187 int manager_new(ManagerRunningAs running_as, bool confirm_spawn, Manager **_m) {
192 assert(running_as >= 0);
193 assert(running_as < _MANAGER_RUNNING_AS_MAX);
195 if (!(m = new0(Manager, 1)))
198 timestamp_get(&m->startup_timestamp);
200 m->running_as = running_as;
201 m->confirm_spawn = confirm_spawn;
202 m->name_data_slot = -1;
203 m->exit_code = _MANAGER_EXIT_CODE_INVALID;
205 m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = m->dev_autofs_fd = -1;
206 m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
208 if (!(m->environment = strv_copy(environ)))
211 if (!(m->units = hashmap_new(string_hash_func, string_compare_func)))
214 if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
217 if (!(m->transaction_jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
220 if (!(m->watch_pids = hashmap_new(trivial_hash_func, trivial_compare_func)))
223 if (!(m->cgroup_bondings = hashmap_new(string_hash_func, string_compare_func)))
226 if (!(m->watch_bus = hashmap_new(string_hash_func, string_compare_func)))
229 if ((m->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
232 if ((r = lookup_paths_init(&m->lookup_paths, m->running_as)) < 0)
235 if ((r = manager_setup_signals(m)) < 0)
238 if ((r = manager_setup_cgroup(m)) < 0)
241 if ((r = manager_setup_notify(m)) < 0)
244 /* Try to connect to the busses, if possible. */
245 if ((r = bus_init_system(m)) < 0 ||
246 (r = bus_init_api(m)) < 0)
257 static unsigned manager_dispatch_cleanup_queue(Manager *m) {
263 while ((meta = m->cleanup_queue)) {
264 assert(meta->in_cleanup_queue);
266 unit_free(UNIT(meta));
274 GC_OFFSET_IN_PATH, /* This one is on the path we were travelling */
275 GC_OFFSET_UNSURE, /* No clue */
276 GC_OFFSET_GOOD, /* We still need this unit */
277 GC_OFFSET_BAD, /* We don't need this unit anymore */
281 static void unit_gc_sweep(Unit *u, unsigned gc_marker) {
288 if (u->meta.gc_marker == gc_marker + GC_OFFSET_GOOD ||
289 u->meta.gc_marker == gc_marker + GC_OFFSET_BAD ||
290 u->meta.gc_marker == gc_marker + GC_OFFSET_IN_PATH)
293 if (u->meta.in_cleanup_queue)
296 if (unit_check_gc(u))
299 u->meta.gc_marker = gc_marker + GC_OFFSET_IN_PATH;
303 SET_FOREACH(other, u->meta.dependencies[UNIT_REFERENCED_BY], i) {
304 unit_gc_sweep(other, gc_marker);
306 if (other->meta.gc_marker == gc_marker + GC_OFFSET_GOOD)
309 if (other->meta.gc_marker != gc_marker + GC_OFFSET_BAD)
316 /* We were unable to find anything out about this entry, so
317 * let's investigate it later */
318 u->meta.gc_marker = gc_marker + GC_OFFSET_UNSURE;
319 unit_add_to_gc_queue(u);
323 /* We definitely know that this one is not useful anymore, so
324 * let's mark it for deletion */
325 u->meta.gc_marker = gc_marker + GC_OFFSET_BAD;
326 unit_add_to_cleanup_queue(u);
330 u->meta.gc_marker = gc_marker + GC_OFFSET_GOOD;
333 static unsigned manager_dispatch_gc_queue(Manager *m) {
340 if ((m->n_in_gc_queue < GC_QUEUE_ENTRIES_MAX) &&
341 (m->gc_queue_timestamp <= 0 ||
342 (m->gc_queue_timestamp + GC_QUEUE_USEC_MAX) > now(CLOCK_MONOTONIC)))
345 log_debug("Running GC...");
347 m->gc_marker += _GC_OFFSET_MAX;
348 if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX)
351 gc_marker = m->gc_marker;
353 while ((meta = m->gc_queue)) {
354 assert(meta->in_gc_queue);
356 unit_gc_sweep(UNIT(meta), gc_marker);
358 LIST_REMOVE(Meta, gc_queue, m->gc_queue, meta);
359 meta->in_gc_queue = false;
363 if (meta->gc_marker == gc_marker + GC_OFFSET_BAD ||
364 meta->gc_marker == gc_marker + GC_OFFSET_UNSURE) {
365 log_debug("Collecting %s", meta->id);
366 meta->gc_marker = gc_marker + GC_OFFSET_BAD;
367 unit_add_to_cleanup_queue(UNIT(meta));
371 m->n_in_gc_queue = 0;
372 m->gc_queue_timestamp = 0;
377 static void manager_clear_jobs_and_units(Manager *m) {
383 while ((j = hashmap_first(m->transaction_jobs)))
386 while ((u = hashmap_first(m->units)))
389 manager_dispatch_cleanup_queue(m);
391 assert(!m->load_queue);
392 assert(!m->run_queue);
393 assert(!m->dbus_unit_queue);
394 assert(!m->dbus_job_queue);
395 assert(!m->cleanup_queue);
396 assert(!m->gc_queue);
398 assert(hashmap_isempty(m->transaction_jobs));
399 assert(hashmap_isempty(m->jobs));
400 assert(hashmap_isempty(m->units));
403 void manager_free(Manager *m) {
408 manager_clear_jobs_and_units(m);
410 for (c = 0; c < _UNIT_TYPE_MAX; c++)
411 if (unit_vtable[c]->shutdown)
412 unit_vtable[c]->shutdown(m);
414 /* If we reexecute ourselves, we keep the root cgroup
416 manager_shutdown_cgroup(m, m->exit_code != MANAGER_REEXECUTE);
421 hashmap_free(m->units);
422 hashmap_free(m->jobs);
423 hashmap_free(m->transaction_jobs);
424 hashmap_free(m->watch_pids);
425 hashmap_free(m->watch_bus);
427 if (m->epoll_fd >= 0)
428 close_nointr_nofail(m->epoll_fd);
429 if (m->signal_watch.fd >= 0)
430 close_nointr_nofail(m->signal_watch.fd);
431 if (m->notify_watch.fd >= 0)
432 close_nointr_nofail(m->notify_watch.fd);
434 lookup_paths_free(&m->lookup_paths);
435 strv_free(m->environment);
437 free(m->cgroup_controller);
438 free(m->cgroup_hierarchy);
440 hashmap_free(m->cgroup_bondings);
445 int manager_enumerate(Manager *m) {
451 /* Let's ask every type to load all units from disk/kernel
452 * that it might know */
453 for (c = 0; c < _UNIT_TYPE_MAX; c++)
454 if (unit_vtable[c]->enumerate)
455 if ((q = unit_vtable[c]->enumerate(m)) < 0)
458 manager_dispatch_load_queue(m);
462 int manager_coldplug(Manager *m) {
470 /* Then, let's set up their initial state. */
471 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
477 if ((q = unit_coldplug(u)) < 0)
484 int manager_startup(Manager *m, FILE *serialization, FDSet *fds) {
489 /* First, enumerate what we can from all config files */
490 r = manager_enumerate(m);
492 /* Second, deserialize if there is something to deserialize */
494 if ((q = manager_deserialize(m, serialization, fds)) < 0)
497 /* Third, fire things up! */
498 if ((q = manager_coldplug(m)) < 0)
501 /* Now that the initial devices are available, let's see if we
502 * can write the utmp file */
503 manager_write_utmp_reboot(m);
508 static void transaction_delete_job(Manager *m, Job *j, bool delete_dependencies) {
512 /* Deletes one job from the transaction */
514 manager_transaction_unlink_job(m, j, delete_dependencies);
520 static void transaction_delete_unit(Manager *m, Unit *u) {
523 /* Deletes all jobs associated with a certain unit from the
526 while ((j = hashmap_get(m->transaction_jobs, u)))
527 transaction_delete_job(m, j, true);
530 static void transaction_clean_dependencies(Manager *m) {
536 /* Drops all dependencies of all installed jobs */
538 HASHMAP_FOREACH(j, m->jobs, i) {
539 while (j->subject_list)
540 job_dependency_free(j->subject_list);
541 while (j->object_list)
542 job_dependency_free(j->object_list);
545 assert(!m->transaction_anchor);
548 static void transaction_abort(Manager *m) {
553 while ((j = hashmap_first(m->transaction_jobs)))
555 transaction_delete_job(m, j, true);
559 assert(hashmap_isempty(m->transaction_jobs));
561 transaction_clean_dependencies(m);
564 static void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) {
569 /* A recursive sweep through the graph that marks all units
570 * that matter to the anchor job, i.e. are directly or
571 * indirectly a dependency of the anchor job via paths that
572 * are fully marked as mattering. */
577 l = m->transaction_anchor;
579 LIST_FOREACH(subject, l, l) {
581 /* This link does not matter */
585 /* This unit has already been marked */
586 if (l->object->generation == generation)
589 l->object->matters_to_anchor = true;
590 l->object->generation = generation;
592 transaction_find_jobs_that_matter_to_anchor(m, l->object, generation);
596 static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) {
597 JobDependency *l, *last;
601 assert(j->unit == other->unit);
602 assert(!j->installed);
604 /* Merges 'other' into 'j' and then deletes j. */
607 j->state = JOB_WAITING;
608 j->override = j->override || other->override;
610 j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
612 /* Patch us in as new owner of the JobDependency objects */
614 LIST_FOREACH(subject, l, other->subject_list) {
615 assert(l->subject == other);
620 /* Merge both lists */
622 last->subject_next = j->subject_list;
624 j->subject_list->subject_prev = last;
625 j->subject_list = other->subject_list;
628 /* Patch us in as new owner of the JobDependency objects */
630 LIST_FOREACH(object, l, other->object_list) {
631 assert(l->object == other);
636 /* Merge both lists */
638 last->object_next = j->object_list;
640 j->object_list->object_prev = last;
641 j->object_list = other->object_list;
644 /* Kill the other job */
645 other->subject_list = NULL;
646 other->object_list = NULL;
647 transaction_delete_job(m, other, true);
650 static int delete_one_unmergeable_job(Manager *m, Job *j) {
655 /* Tries to delete one item in the linked list
656 * j->transaction_next->transaction_next->... that conflicts
657 * whith another one, in an attempt to make an inconsistent
658 * transaction work. */
660 /* We rely here on the fact that if a merged with b does not
661 * merge with c, either a or b merge with c neither */
662 LIST_FOREACH(transaction, j, j)
663 LIST_FOREACH(transaction, k, j->transaction_next) {
666 /* Is this one mergeable? Then skip it */
667 if (job_type_is_mergeable(j->type, k->type))
670 /* Ok, we found two that conflict, let's see if we can
671 * drop one of them */
672 if (!j->matters_to_anchor)
674 else if (!k->matters_to_anchor)
679 /* Ok, we can drop one, so let's do so. */
680 log_debug("Trying to fix job merging by deleting job %s/%s", d->unit->meta.id, job_type_to_string(d->type));
681 transaction_delete_job(m, d, true);
688 static int transaction_merge_jobs(Manager *m) {
695 /* First step, check whether any of the jobs for one specific
696 * task conflict. If so, try to drop one of them. */
697 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
702 LIST_FOREACH(transaction, k, j->transaction_next) {
703 if ((r = job_type_merge(&t, k->type)) >= 0)
706 /* OK, we could not merge all jobs for this
707 * action. Let's see if we can get rid of one
710 if ((r = delete_one_unmergeable_job(m, j)) >= 0)
711 /* Ok, we managed to drop one, now
712 * let's ask our callers to call us
713 * again after garbage collecting */
716 /* We couldn't merge anything. Failure */
721 /* Second step, merge the jobs. */
722 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
726 /* Merge all transactions */
727 LIST_FOREACH(transaction, k, j->transaction_next)
728 assert_se(job_type_merge(&t, k->type) == 0);
730 /* If an active job is mergeable, merge it too */
731 if (j->unit->meta.job)
732 job_type_merge(&t, j->unit->meta.job->type); /* Might fail. Which is OK */
734 while ((k = j->transaction_next)) {
736 transaction_merge_and_delete_job(m, k, j, t);
739 transaction_merge_and_delete_job(m, j, k, t);
742 assert(!j->transaction_next);
743 assert(!j->transaction_prev);
749 static void transaction_drop_redundant(Manager *m) {
754 /* Goes through the transaction and removes all jobs that are
763 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
764 bool changes_something = false;
767 LIST_FOREACH(transaction, k, j) {
769 if (!job_is_anchor(k) &&
770 job_type_is_redundant(k->type, unit_active_state(k->unit)))
773 changes_something = true;
777 if (changes_something)
780 log_debug("Found redundant job %s/%s, dropping.", j->unit->meta.id, job_type_to_string(j->type));
781 transaction_delete_job(m, j, false);
789 static bool unit_matters_to_anchor(Unit *u, Job *j) {
791 assert(!j->transaction_prev);
793 /* Checks whether at least one of the jobs for this unit
794 * matters to the anchor. */
796 LIST_FOREACH(transaction, j, j)
797 if (j->matters_to_anchor)
803 static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation) {
810 assert(!j->transaction_prev);
812 /* Does a recursive sweep through the ordering graph, looking
813 * for a cycle. If we find cycle we try to break it. */
815 /* Have we seen this before? */
816 if (j->generation == generation) {
819 /* If the marker is NULL we have been here already and
820 * decided the job was loop-free from here. Hence
821 * shortcut things and return right-away. */
825 /* So, the marker is not NULL and we already have been
826 * here. We have a cycle. Let's try to break it. We go
827 * backwards in our path and try to find a suitable
828 * job to remove. We use the marker to find our way
829 * back, since smart how we are we stored our way back
831 log_debug("Found ordering cycle on %s/%s", j->unit->meta.id, job_type_to_string(j->type));
833 for (k = from; k; k = ((k->generation == generation && k->marker != k) ? k->marker : NULL)) {
835 log_debug("Walked on cycle path to %s/%s", k->unit->meta.id, job_type_to_string(k->type));
838 !unit_matters_to_anchor(k->unit, k)) {
839 /* Ok, we can drop this one, so let's
841 log_debug("Breaking order cycle by deleting job %s/%s", k->unit->meta.id, job_type_to_string(k->type));
842 transaction_delete_unit(m, k->unit);
846 /* Check if this in fact was the beginning of
852 log_debug("Unable to break cycle");
857 /* Make the marker point to where we come from, so that we can
858 * find our way backwards if we want to break a cycle. We use
859 * a special marker for the beginning: we point to
861 j->marker = from ? from : j;
862 j->generation = generation;
864 /* We assume that the the dependencies are bidirectional, and
865 * hence can ignore UNIT_AFTER */
866 SET_FOREACH(u, j->unit->meta.dependencies[UNIT_BEFORE], i) {
869 /* Is there a job for this unit? */
870 if (!(o = hashmap_get(m->transaction_jobs, u)))
872 /* Ok, there is no job for this in the
873 * transaction, but maybe there is already one
875 if (!(o = u->meta.job))
878 if ((r = transaction_verify_order_one(m, o, j, generation)) < 0)
882 /* Ok, let's backtrack, and remember that this entry is not on
883 * our path anymore. */
889 static int transaction_verify_order(Manager *m, unsigned *generation) {
898 /* Check if the ordering graph is cyclic. If it is, try to fix
899 * that up by dropping one of the jobs. */
903 HASHMAP_FOREACH(j, m->transaction_jobs, i)
904 if ((r = transaction_verify_order_one(m, j, NULL, g)) < 0)
910 static void transaction_collect_garbage(Manager *m) {
915 /* Drop jobs that are not required by any other job */
923 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
927 log_debug("Garbage collecting job %s/%s", j->unit->meta.id, job_type_to_string(j->type));
928 transaction_delete_job(m, j, true);
936 static int transaction_is_destructive(Manager *m) {
942 /* Checks whether applying this transaction means that
943 * existing jobs would be replaced */
945 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
948 assert(!j->transaction_prev);
949 assert(!j->transaction_next);
951 if (j->unit->meta.job &&
952 j->unit->meta.job != j &&
953 !job_type_is_superset(j->type, j->unit->meta.job->type))
960 static void transaction_minimize_impact(Manager *m) {
964 /* Drops all unnecessary jobs that reverse already active jobs
965 * or that stop a running service. */
973 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
974 LIST_FOREACH(transaction, j, j) {
975 bool stops_running_service, changes_existing_job;
977 /* If it matters, we shouldn't drop it */
978 if (j->matters_to_anchor)
981 /* Would this stop a running service?
982 * Would this change an existing job?
983 * If so, let's drop this entry */
985 stops_running_service =
986 j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
988 changes_existing_job =
989 j->unit->meta.job && job_type_is_conflicting(j->type, j->unit->meta.job->state);
991 if (!stops_running_service && !changes_existing_job)
994 if (stops_running_service)
995 log_debug("%s/%s would stop a running service.", j->unit->meta.id, job_type_to_string(j->type));
997 if (changes_existing_job)
998 log_debug("%s/%s would change existing job.", j->unit->meta.id, job_type_to_string(j->type));
1000 /* Ok, let's get rid of this */
1001 log_debug("Deleting %s/%s to minimize impact.", j->unit->meta.id, job_type_to_string(j->type));
1003 transaction_delete_job(m, j, true);
1015 static int transaction_apply(Manager *m) {
1020 /* Moves the transaction jobs to the set of active jobs */
1022 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1024 assert(!j->transaction_prev);
1025 assert(!j->transaction_next);
1030 if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
1034 while ((j = hashmap_steal_first(m->transaction_jobs))) {
1038 if (j->unit->meta.job)
1039 job_free(j->unit->meta.job);
1041 j->unit->meta.job = j;
1042 j->installed = true;
1044 /* We're fully installed. Now let's free data we don't
1047 assert(!j->transaction_next);
1048 assert(!j->transaction_prev);
1050 job_add_to_run_queue(j);
1051 job_add_to_dbus_queue(j);
1054 /* As last step, kill all remaining job dependencies. */
1055 transaction_clean_dependencies(m);
1061 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1065 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
1071 static int transaction_activate(Manager *m, JobMode mode) {
1073 unsigned generation = 1;
1077 /* This applies the changes recorded in transaction_jobs to
1078 * the actual list of jobs, if possible. */
1080 /* First step: figure out which jobs matter */
1081 transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
1083 /* Second step: Try not to stop any running services if
1084 * we don't have to. Don't try to reverse running
1085 * jobs if we don't have to. */
1086 transaction_minimize_impact(m);
1088 /* Third step: Drop redundant jobs */
1089 transaction_drop_redundant(m);
1092 /* Fourth step: Let's remove unneeded jobs that might
1094 transaction_collect_garbage(m);
1096 /* Fifth step: verify order makes sense and correct
1097 * cycles if necessary and possible */
1098 if ((r = transaction_verify_order(m, &generation)) >= 0)
1102 log_debug("Requested transaction contains an unfixable cyclic ordering dependency: %s", strerror(-r));
1106 /* Let's see if the resulting transaction ordering
1107 * graph is still cyclic... */
1111 /* Sixth step: let's drop unmergeable entries if
1112 * necessary and possible, merge entries we can
1114 if ((r = transaction_merge_jobs(m)) >= 0)
1118 log_debug("Requested transaction contains unmergable jobs: %s", strerror(-r));
1122 /* Seventh step: an entry got dropped, let's garbage
1123 * collect its dependencies. */
1124 transaction_collect_garbage(m);
1126 /* Let's see if the resulting transaction still has
1127 * unmergeable entries ... */
1130 /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
1131 transaction_drop_redundant(m);
1133 /* Ninth step: check whether we can actually apply this */
1134 if (mode == JOB_FAIL)
1135 if ((r = transaction_is_destructive(m)) < 0) {
1136 log_debug("Requested transaction contradicts existing jobs: %s", strerror(-r));
1140 /* Tenth step: apply changes */
1141 if ((r = transaction_apply(m)) < 0) {
1142 log_debug("Failed to apply transaction: %s", strerror(-r));
1146 assert(hashmap_isempty(m->transaction_jobs));
1147 assert(!m->transaction_anchor);
1152 transaction_abort(m);
1156 static Job* transaction_add_one_job(Manager *m, JobType type, Unit *unit, bool override, bool *is_new) {
1163 /* Looks for an axisting prospective job and returns that. If
1164 * it doesn't exist it is created and added to the prospective
1167 f = hashmap_get(m->transaction_jobs, unit);
1169 LIST_FOREACH(transaction, j, f) {
1170 assert(j->unit == unit);
1172 if (j->type == type) {
1179 if (unit->meta.job && unit->meta.job->type == type)
1181 else if (!(j = job_new(m, type, unit)))
1186 j->matters_to_anchor = false;
1187 j->override = override;
1189 LIST_PREPEND(Job, transaction, f, j);
1191 if ((r = hashmap_replace(m->transaction_jobs, unit, f)) < 0) {
1199 log_debug("Added job %s/%s to transaction.", unit->meta.id, job_type_to_string(type));
1204 void manager_transaction_unlink_job(Manager *m, Job *j, bool delete_dependencies) {
1208 if (j->transaction_prev)
1209 j->transaction_prev->transaction_next = j->transaction_next;
1210 else if (j->transaction_next)
1211 hashmap_replace(m->transaction_jobs, j->unit, j->transaction_next);
1213 hashmap_remove_value(m->transaction_jobs, j->unit, j);
1215 if (j->transaction_next)
1216 j->transaction_next->transaction_prev = j->transaction_prev;
1218 j->transaction_prev = j->transaction_next = NULL;
1220 while (j->subject_list)
1221 job_dependency_free(j->subject_list);
1223 while (j->object_list) {
1224 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
1226 job_dependency_free(j->object_list);
1228 if (other && delete_dependencies) {
1229 log_debug("Deleting job %s/%s as dependency of job %s/%s",
1230 other->unit->meta.id, job_type_to_string(other->type),
1231 j->unit->meta.id, job_type_to_string(j->type));
1232 transaction_delete_job(m, other, delete_dependencies);
1237 static int transaction_add_job_and_dependencies(
1252 assert(type < _JOB_TYPE_MAX);
1255 if (unit->meta.load_state != UNIT_LOADED)
1258 if (!unit_job_is_applicable(unit, type))
1261 /* First add the job. */
1262 if (!(ret = transaction_add_one_job(m, type, unit, override, &is_new)))
1265 /* Then, add a link to the job. */
1266 if (!job_dependency_new(by, ret, matters))
1270 /* Finally, recursively add in all dependencies. */
1271 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
1272 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES], i)
1273 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, override, NULL)) < 0 && r != -EBADR)
1276 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
1277 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !override, override, NULL)) < 0 && r != -EBADR)
1278 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, strerror(-r));
1280 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_WANTS], i)
1281 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, false, NULL)) < 0)
1282 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, strerror(-r));
1284 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE], i)
1285 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, true, override, NULL)) < 0 && r != -EBADR)
1288 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i)
1289 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, !override, override, NULL)) < 0 && r != -EBADR)
1290 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, strerror(-r));
1292 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTS], i)
1293 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, override, NULL)) < 0 && r != -EBADR)
1296 } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
1298 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRED_BY], i)
1299 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, override, NULL)) < 0 && r != -EBADR)
1303 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
1315 static int transaction_add_isolate_jobs(Manager *m) {
1323 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
1325 /* ignore aliases */
1326 if (u->meta.id != k)
1329 if (UNIT_VTABLE(u)->no_isolate)
1332 /* No need to stop inactive jobs */
1333 if (unit_active_state(u) == UNIT_INACTIVE)
1336 /* Is there already something listed for this? */
1337 if (hashmap_get(m->transaction_jobs, u))
1340 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, u, NULL, true, false, NULL)) < 0)
1341 log_warning("Cannot add isolate job for unit %s, ignoring: %s", u->meta.id, strerror(-r));
1347 int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool override, Job **_ret) {
1352 assert(type < _JOB_TYPE_MAX);
1354 assert(mode < _JOB_MODE_MAX);
1356 if (mode == JOB_ISOLATE && type != JOB_START)
1359 log_debug("Trying to enqueue job %s/%s", unit->meta.id, job_type_to_string(type));
1361 if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, override, &ret)) < 0) {
1362 transaction_abort(m);
1366 if (mode == JOB_ISOLATE)
1367 if ((r = transaction_add_isolate_jobs(m)) < 0) {
1368 transaction_abort(m);
1372 if ((r = transaction_activate(m, mode)) < 0)
1375 log_debug("Enqueued job %s/%s as %u", unit->meta.id, job_type_to_string(type), (unsigned) ret->id);
1383 int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, bool override, Job **_ret) {
1388 assert(type < _JOB_TYPE_MAX);
1390 assert(mode < _JOB_MODE_MAX);
1392 if ((r = manager_load_unit(m, name, NULL, &unit)) < 0)
1395 return manager_add_job(m, type, unit, mode, override, _ret);
1398 Job *manager_get_job(Manager *m, uint32_t id) {
1401 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
1404 Unit *manager_get_unit(Manager *m, const char *name) {
1408 return hashmap_get(m->units, name);
1411 unsigned manager_dispatch_load_queue(Manager *m) {
1417 /* Make sure we are not run recursively */
1418 if (m->dispatching_load_queue)
1421 m->dispatching_load_queue = true;
1423 /* Dispatches the load queue. Takes a unit from the queue and
1424 * tries to load its data until the queue is empty */
1426 while ((meta = m->load_queue)) {
1427 assert(meta->in_load_queue);
1429 unit_load(UNIT(meta));
1433 m->dispatching_load_queue = false;
1437 int manager_load_unit_prepare(Manager *m, const char *name, const char *path, Unit **_ret) {
1442 assert(name || path);
1444 /* This will prepare the unit for loading, but not actually
1445 * load anything from disk. */
1447 if (path && !is_path(path))
1451 name = file_name_from_path(path);
1453 if (!unit_name_is_valid(name))
1456 if ((ret = manager_get_unit(m, name))) {
1461 if (!(ret = unit_new(m)))
1465 if (!(ret->meta.fragment_path = strdup(path))) {
1470 if ((r = unit_add_name(ret, name)) < 0) {
1475 unit_add_to_load_queue(ret);
1476 unit_add_to_dbus_queue(ret);
1477 unit_add_to_gc_queue(ret);
1485 int manager_load_unit(Manager *m, const char *name, const char *path, Unit **_ret) {
1490 /* This will load the service information files, but not actually
1491 * start any services or anything. */
1493 if ((r = manager_load_unit_prepare(m, name, path, _ret)) != 0)
1496 manager_dispatch_load_queue(m);
1499 *_ret = unit_follow_merge(*_ret);
1504 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
1511 HASHMAP_FOREACH(j, s->jobs, i)
1512 job_dump(j, f, prefix);
1515 void manager_dump_units(Manager *s, FILE *f, const char *prefix) {
1523 HASHMAP_FOREACH_KEY(u, t, s->units, i)
1524 if (u->meta.id == t)
1525 unit_dump(u, f, prefix);
1528 void manager_clear_jobs(Manager *m) {
1533 transaction_abort(m);
1535 while ((j = hashmap_first(m->jobs)))
1539 unsigned manager_dispatch_run_queue(Manager *m) {
1543 if (m->dispatching_run_queue)
1546 m->dispatching_run_queue = true;
1548 while ((j = m->run_queue)) {
1549 assert(j->installed);
1550 assert(j->in_run_queue);
1552 job_run_and_invalidate(j);
1556 m->dispatching_run_queue = false;
1560 unsigned manager_dispatch_dbus_queue(Manager *m) {
1567 if (m->dispatching_dbus_queue)
1570 m->dispatching_dbus_queue = true;
1572 while ((meta = m->dbus_unit_queue)) {
1573 assert(meta->in_dbus_queue);
1575 bus_unit_send_change_signal(UNIT(meta));
1579 while ((j = m->dbus_job_queue)) {
1580 assert(j->in_dbus_queue);
1582 bus_job_send_change_signal(j);
1586 m->dispatching_dbus_queue = false;
1590 static int manager_process_notify_fd(Manager *m) {
1597 struct msghdr msghdr;
1599 struct ucred *ucred;
1601 struct cmsghdr cmsghdr;
1602 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
1608 iovec.iov_base = buf;
1609 iovec.iov_len = sizeof(buf)-1;
1613 msghdr.msg_iov = &iovec;
1614 msghdr.msg_iovlen = 1;
1615 msghdr.msg_control = &control;
1616 msghdr.msg_controllen = sizeof(control);
1618 if ((n = recvmsg(m->notify_watch.fd, &msghdr, MSG_DONTWAIT)) <= 0) {
1622 if (errno == EAGAIN)
1628 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
1629 control.cmsghdr.cmsg_level != SOL_SOCKET ||
1630 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
1631 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
1632 log_warning("Received notify message without credentials. Ignoring.");
1636 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
1638 if (!(u = hashmap_get(m->watch_pids, UINT32_TO_PTR(ucred->pid))))
1639 if (!(u = cgroup_unit_by_pid(m, ucred->pid))) {
1640 log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid);
1645 if (!(tags = strv_split(buf, "\n\r")))
1648 log_debug("Got notification message for unit %s", u->meta.id);
1650 if (UNIT_VTABLE(u)->notify_message)
1651 UNIT_VTABLE(u)->notify_message(u, tags);
1659 static int manager_dispatch_sigchld(Manager *m) {
1669 /* First we call waitd() for a PID and do not reap the
1670 * zombie. That way we can still access /proc/$PID for
1671 * it while it is a zombie. */
1672 if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) {
1674 if (errno == ECHILD)
1686 if (si.si_code == CLD_EXITED || si.si_code == CLD_KILLED || si.si_code == CLD_DUMPED) {
1689 get_process_name(si.si_pid, &name);
1690 log_debug("Got SIGCHLD for process %llu (%s)", (unsigned long long) si.si_pid, strna(name));
1694 /* Let's flush any message the dying child might still
1695 * have queued for us. This ensures that the process
1696 * still exists in /proc so that we can figure out
1697 * which cgroup and hence unit it belongs to. */
1698 if ((r = manager_process_notify_fd(m)) < 0)
1701 /* And now figure out the unit this belongs to */
1702 if (!(u = hashmap_get(m->watch_pids, UINT32_TO_PTR(si.si_pid))))
1703 u = cgroup_unit_by_pid(m, si.si_pid);
1705 /* And now, we actually reap the zombie. */
1706 if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) {
1713 if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED)
1716 log_debug("Child %llu died (code=%s, status=%i/%s)",
1717 (long long unsigned) si.si_pid,
1718 sigchld_code_to_string(si.si_code),
1720 strna(si.si_code == CLD_EXITED ? exit_status_to_string(si.si_status) : strsignal(si.si_status)));
1725 log_debug("Child %llu belongs to %s", (long long unsigned) si.si_pid, u->meta.id);
1727 hashmap_remove(m->watch_pids, UINT32_TO_PTR(si.si_pid));
1728 UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status);
1734 static int manager_start_target(Manager *m, const char *name) {
1737 if ((r = manager_add_job_by_name(m, JOB_START, name, JOB_REPLACE, true, NULL)) < 0)
1738 log_error("Failed to enqueue %s job: %s", name, strerror(-r));
1743 static int manager_process_signal_fd(Manager *m) {
1745 struct signalfd_siginfo sfsi;
1746 bool sigchld = false;
1751 if ((n = read(m->signal_watch.fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
1756 if (errno == EAGAIN)
1762 switch (sfsi.ssi_signo) {
1769 if (m->running_as == MANAGER_INIT) {
1770 /* This is for compatibility with the
1771 * original sysvinit */
1772 m->exit_code = MANAGER_REEXECUTE;
1779 if (m->running_as == MANAGER_INIT) {
1780 manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET);
1784 /* Run the exit target if there is one, if not, just exit. */
1785 if (manager_start_target(m, SPECIAL_EXIT_SERVICE) < 0) {
1786 m->exit_code = MANAGER_EXIT;
1793 if (m->running_as == MANAGER_INIT)
1794 manager_start_target(m, SPECIAL_KBREQUEST_TARGET);
1796 /* This is a nop on non-init */
1800 if (m->running_as == MANAGER_INIT)
1801 manager_start_target(m, SPECIAL_SIGPWR_TARGET);
1803 /* This is a nop on non-init */
1809 u = manager_get_unit(m, SPECIAL_DBUS_SERVICE);
1811 if (!u || UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) {
1812 log_info("Trying to reconnect to bus...");
1817 if (!u || !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) {
1818 log_info("Loading D-Bus service...");
1819 manager_start_target(m, SPECIAL_DBUS_SERVICE);
1830 if (!(f = open_memstream(&dump, &size))) {
1831 log_warning("Failed to allocate memory stream.");
1835 manager_dump_units(m, f, "\t");
1836 manager_dump_jobs(m, f, "\t");
1841 log_warning("Failed to write status stream");
1846 log_dump(LOG_INFO, dump);
1853 m->exit_code = MANAGER_RELOAD;
1857 log_info("Got unhandled signal <%s>.", strsignal(sfsi.ssi_signo));
1862 return manager_dispatch_sigchld(m);
1867 static int process_event(Manager *m, struct epoll_event *ev) {
1874 assert(w = ev->data.ptr);
1880 /* An incoming signal? */
1881 if (ev->events != EPOLLIN)
1884 if ((r = manager_process_signal_fd(m)) < 0)
1891 /* An incoming daemon notification event? */
1892 if (ev->events != EPOLLIN)
1895 if ((r = manager_process_notify_fd(m)) < 0)
1902 /* Some fd event, to be dispatched to the units */
1903 UNIT_VTABLE(w->data.unit)->fd_event(w->data.unit, w->fd, ev->events, w);
1910 /* Some timer event, to be dispatched to the units */
1911 if ((k = read(w->fd, &v, sizeof(v))) != sizeof(v)) {
1913 if (k < 0 && (errno == EINTR || errno == EAGAIN))
1916 return k < 0 ? -errno : -EIO;
1919 UNIT_VTABLE(w->data.unit)->timer_event(w->data.unit, v, w);
1924 /* Some mount table change, intended for the mount subsystem */
1925 mount_fd_event(m, ev->events);
1929 /* Some notification from udev, intended for the device subsystem */
1930 device_fd_event(m, ev->events);
1933 case WATCH_DBUS_WATCH:
1934 bus_watch_event(m, w, ev->events);
1937 case WATCH_DBUS_TIMEOUT:
1938 bus_timeout_event(m, w, ev->events);
1942 assert_not_reached("Unknown epoll event type.");
1948 int manager_loop(Manager *m) {
1951 RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 1000);
1954 m->exit_code = MANAGER_RUNNING;
1956 /* There might still be some zombies hanging around from
1957 * before we were exec()'ed. Leat's reap them */
1958 if ((r = manager_dispatch_sigchld(m)) < 0)
1961 while (m->exit_code == MANAGER_RUNNING) {
1962 struct epoll_event event;
1965 if (!ratelimit_test(&rl)) {
1966 /* Yay, something is going seriously wrong, pause a little */
1967 log_warning("Looping too fast. Throttling execution a little.");
1971 if (manager_dispatch_load_queue(m) > 0)
1974 if (manager_dispatch_run_queue(m) > 0)
1977 if (bus_dispatch(m) > 0)
1980 if (manager_dispatch_cleanup_queue(m) > 0)
1983 if (manager_dispatch_gc_queue(m) > 0)
1986 if (manager_dispatch_dbus_queue(m) > 0)
1989 if ((n = epoll_wait(m->epoll_fd, &event, 1, -1)) < 0) {
1999 if ((r = process_event(m, &event)) < 0)
2003 return m->exit_code;
2006 int manager_get_unit_from_dbus_path(Manager *m, const char *s, Unit **_u) {
2014 if (!startswith(s, "/org/freedesktop/systemd1/unit/"))
2017 if (!(n = bus_path_unescape(s+31)))
2020 u = manager_get_unit(m, n);
2031 int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
2040 if (!startswith(s, "/org/freedesktop/systemd1/job/"))
2043 if ((r = safe_atou(s + 30, &id)) < 0)
2046 if (!(j = manager_get_job(m, id)))
2054 static bool manager_utmp_good(Manager *m) {
2059 if ((r = mount_path_is_mounted(m, _PATH_UTMPX)) <= 0) {
2062 log_warning("Failed to determine whether " _PATH_UTMPX " is mounted: %s", strerror(-r));
2070 void manager_write_utmp_reboot(Manager *m) {
2075 if (m->utmp_reboot_written)
2078 if (m->running_as != MANAGER_INIT)
2081 if (!manager_utmp_good(m))
2084 if ((r = utmp_put_reboot(m->startup_timestamp.realtime)) < 0) {
2086 if (r != -ENOENT && r != -EROFS)
2087 log_warning("Failed to write utmp/wtmp: %s", strerror(-r));
2092 m->utmp_reboot_written = true;
2095 void manager_write_utmp_runlevel(Manager *m, Unit *u) {
2101 if (u->meta.type != UNIT_TARGET)
2104 if (m->running_as != MANAGER_INIT)
2107 if (!manager_utmp_good(m))
2110 if ((runlevel = target_get_runlevel(TARGET(u))) <= 0)
2113 if ((r = utmp_put_runlevel(0, runlevel, 0)) < 0) {
2115 if (r != -ENOENT && r != -EROFS)
2116 log_warning("Failed to write utmp/wtmp: %s", strerror(-r));
2120 void manager_dispatch_bus_name_owner_changed(
2123 const char* old_owner,
2124 const char *new_owner) {
2131 if (!(u = hashmap_get(m->watch_bus, name)))
2134 UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner);
2137 void manager_dispatch_bus_query_pid_done(
2148 if (!(u = hashmap_get(m->watch_bus, name)))
2151 UNIT_VTABLE(u)->bus_query_pid_done(u, name, pid);
2154 int manager_open_serialization(FILE **_f) {
2162 if (asprintf(&path, "/dev/shm/systemd-%u.dump-XXXXXX", (unsigned) getpid()) < 0)
2165 saved_umask = umask(0077);
2166 fd = mkostemp(path, O_RDWR|O_CLOEXEC);
2176 log_debug("Serializing state to %s", path);
2179 if (!(f = fdopen(fd, "w+")) < 0)
2187 int manager_serialize(Manager *m, FILE *f, FDSet *fds) {
2197 HASHMAP_FOREACH_KEY(u, t, m->units, i) {
2198 if (u->meta.id != t)
2201 if (!unit_can_serialize(u))
2205 fputs(u->meta.id, f);
2208 if ((r = unit_serialize(u, f, fds)) < 0)
2218 int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
2224 log_debug("Deserializing state...");
2228 char name[UNIT_NAME_MAX+2];
2231 if (!fgets(name, sizeof(name), f)) {
2240 if ((r = manager_load_unit(m, strstrip(name), NULL, &u)) < 0)
2243 if ((r = unit_deserialize(u, f, fds)) < 0)
2253 int manager_reload(Manager *m) {
2260 if ((r = manager_open_serialization(&f)) < 0)
2263 if (!(fds = fdset_new())) {
2268 if ((r = manager_serialize(m, f, fds)) < 0)
2271 if (fseeko(f, 0, SEEK_SET) < 0) {
2276 /* From here on there is no way back. */
2277 manager_clear_jobs_and_units(m);
2279 /* Find new unit paths */
2280 lookup_paths_free(&m->lookup_paths);
2281 if ((q = lookup_paths_init(&m->lookup_paths, m->running_as)) < 0)
2284 /* First, enumerate what we can from all config files */
2285 if ((q = manager_enumerate(m)) < 0)
2288 /* Second, deserialize our stored data */
2289 if ((q = manager_deserialize(m, f, fds)) < 0)
2295 /* Third, fire things up! */
2296 if ((q = manager_coldplug(m)) < 0)
2309 static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = {
2310 [MANAGER_INIT] = "init",
2311 [MANAGER_SYSTEM] = "system",
2312 [MANAGER_SESSION] = "session"
2315 DEFINE_STRING_TABLE_LOOKUP(manager_running_as, ManagerRunningAs);