1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser 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>
31 #include <sys/reboot.h>
32 #include <sys/ioctl.h>
36 #include <sys/types.h>
44 #include <systemd/sd-daemon.h>
53 #include "ratelimit.h"
55 #include "mount-setup.h"
56 #include "unit-name.h"
57 #include "dbus-unit.h"
60 #include "path-lookup.h"
62 #include "bus-errors.h"
63 #include "exit-status.h"
67 /* As soon as 16 units are in our GC queue, make sure to run a gc sweep */
68 #define GC_QUEUE_ENTRIES_MAX 16
70 /* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */
71 #define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC)
73 /* Where clients shall send notification messages to */
74 #define NOTIFY_SOCKET_SYSTEM "/run/systemd/notify"
75 #define NOTIFY_SOCKET_USER "@/org/freedesktop/systemd1/notify"
77 static int manager_setup_notify(Manager *m) {
80 struct sockaddr_un un;
82 struct epoll_event ev;
88 m->notify_watch.type = WATCH_NOTIFY;
89 if ((m->notify_watch.fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
90 log_error("Failed to allocate notification socket: %m");
95 sa.sa.sa_family = AF_UNIX;
98 snprintf(sa.un.sun_path, sizeof(sa.un.sun_path), NOTIFY_SOCKET_USER "/%llu", random_ull());
100 unlink(NOTIFY_SOCKET_SYSTEM);
101 strncpy(sa.un.sun_path, NOTIFY_SOCKET_SYSTEM, sizeof(sa.un.sun_path));
104 if (sa.un.sun_path[0] == '@')
105 sa.un.sun_path[0] = 0;
108 r = bind(m->notify_watch.fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1));
112 log_error("bind() failed: %m");
116 if (setsockopt(m->notify_watch.fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
117 log_error("SO_PASSCRED failed: %m");
123 ev.data.ptr = &m->notify_watch;
125 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev) < 0)
128 if (sa.un.sun_path[0] == 0)
129 sa.un.sun_path[0] = '@';
131 if (!(m->notify_socket = strdup(sa.un.sun_path)))
134 log_debug("Using notification socket %s", m->notify_socket);
139 static int enable_special_signals(Manager *m) {
144 /* Enable that we get SIGINT on control-alt-del. In containers
145 * this will fail with EPERM, so ignore that. */
146 if (reboot(RB_DISABLE_CAD) < 0 && errno != EPERM)
147 log_warning("Failed to enable ctrl-alt-del handling: %m");
149 fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC);
151 /* Support systems without virtual console */
153 log_warning("Failed to open /dev/tty0: %m");
155 /* Enable that we get SIGWINCH on kbrequest */
156 if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0)
157 log_warning("Failed to enable kbrequest handling: %s", strerror(errno));
159 close_nointr_nofail(fd);
165 static int manager_setup_signals(Manager *m) {
167 struct epoll_event ev;
172 /* We are not interested in SIGSTOP and friends. */
174 sa.sa_handler = SIG_DFL;
175 sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
176 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
178 assert_se(sigemptyset(&mask) == 0);
180 sigset_add_many(&mask,
181 SIGCHLD, /* Child died */
182 SIGTERM, /* Reexecute daemon */
183 SIGHUP, /* Reload configuration */
184 SIGUSR1, /* systemd/upstart: reconnect to D-Bus */
185 SIGUSR2, /* systemd: dump status */
186 SIGINT, /* Kernel sends us this on control-alt-del */
187 SIGWINCH, /* Kernel sends us this on kbrequest (alt-arrowup) */
188 SIGPWR, /* Some kernel drivers and upsd send us this on power failure */
189 SIGRTMIN+0, /* systemd: start default.target */
190 SIGRTMIN+1, /* systemd: isolate rescue.target */
191 SIGRTMIN+2, /* systemd: isolate emergency.target */
192 SIGRTMIN+3, /* systemd: start halt.target */
193 SIGRTMIN+4, /* systemd: start poweroff.target */
194 SIGRTMIN+5, /* systemd: start reboot.target */
195 SIGRTMIN+6, /* systemd: start kexec.target */
196 SIGRTMIN+13, /* systemd: Immediate halt */
197 SIGRTMIN+14, /* systemd: Immediate poweroff */
198 SIGRTMIN+15, /* systemd: Immediate reboot */
199 SIGRTMIN+16, /* systemd: Immediate kexec */
200 SIGRTMIN+20, /* systemd: enable status messages */
201 SIGRTMIN+21, /* systemd: disable status messages */
202 SIGRTMIN+22, /* systemd: set log level to LOG_DEBUG */
203 SIGRTMIN+23, /* systemd: set log level to LOG_INFO */
204 SIGRTMIN+26, /* systemd: set log target to journal-or-kmsg */
205 SIGRTMIN+27, /* systemd: set log target to console */
206 SIGRTMIN+28, /* systemd: set log target to kmsg */
207 SIGRTMIN+29, /* systemd: set log target to syslog-or-kmsg */
209 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
211 m->signal_watch.type = WATCH_SIGNAL;
212 if ((m->signal_watch.fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0)
217 ev.data.ptr = &m->signal_watch;
219 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0)
222 if (m->running_as == MANAGER_SYSTEM)
223 return enable_special_signals(m);
228 static void manager_strip_environment(Manager *m) {
231 /* Remove variables from the inherited set that are part of
232 * the container interface:
233 * http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface */
234 strv_remove_prefix(m->environment, "container=");
235 strv_remove_prefix(m->environment, "container_");
237 /* Remove variables from the inherited set that are part of
238 * the initrd interface:
239 * http://www.freedesktop.org/wiki/Software/systemd/InitrdInterface */
240 strv_remove_prefix(m->environment, "RD_");
243 int manager_new(ManagerRunningAs running_as, Manager **_m) {
248 assert(running_as >= 0);
249 assert(running_as < _MANAGER_RUNNING_AS_MAX);
251 if (!(m = new0(Manager, 1)))
254 dual_timestamp_get(&m->startup_timestamp);
256 m->running_as = running_as;
257 m->name_data_slot = m->conn_data_slot = m->subscribed_data_slot = -1;
258 m->exit_code = _MANAGER_EXIT_CODE_INVALID;
259 m->pin_cgroupfs_fd = -1;
265 m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = m->dev_autofs_fd = m->swap_watch.fd = -1;
266 m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
268 m->environment = strv_copy(environ);
272 manager_strip_environment(m);
274 if (running_as == MANAGER_SYSTEM) {
275 m->default_controllers = strv_new("cpu", NULL);
276 if (!m->default_controllers)
280 if (!(m->units = hashmap_new(string_hash_func, string_compare_func)))
283 if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
286 if (!(m->transaction_jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
289 if (!(m->watch_pids = hashmap_new(trivial_hash_func, trivial_compare_func)))
292 if (!(m->cgroup_bondings = hashmap_new(string_hash_func, string_compare_func)))
295 if (!(m->watch_bus = hashmap_new(string_hash_func, string_compare_func)))
298 if ((m->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
301 if ((r = lookup_paths_init(&m->lookup_paths, m->running_as, true)) < 0)
304 if ((r = manager_setup_signals(m)) < 0)
307 if ((r = manager_setup_cgroup(m)) < 0)
310 if ((r = manager_setup_notify(m)) < 0)
313 /* Try to connect to the busses, if possible. */
314 if ((r = bus_init(m, running_as != MANAGER_SYSTEM)) < 0)
318 if ((m->audit_fd = audit_open()) < 0 &&
319 /* If the kernel lacks netlink or audit support,
320 * don't worry about it. */
321 errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
322 log_error("Failed to connect to audit log: %m");
325 m->taint_usr = dir_is_empty("/usr") > 0;
335 static unsigned manager_dispatch_cleanup_queue(Manager *m) {
341 while ((u = m->cleanup_queue)) {
342 assert(u->in_cleanup_queue);
352 GC_OFFSET_IN_PATH, /* This one is on the path we were traveling */
353 GC_OFFSET_UNSURE, /* No clue */
354 GC_OFFSET_GOOD, /* We still need this unit */
355 GC_OFFSET_BAD, /* We don't need this unit anymore */
359 static void unit_gc_sweep(Unit *u, unsigned gc_marker) {
366 if (u->gc_marker == gc_marker + GC_OFFSET_GOOD ||
367 u->gc_marker == gc_marker + GC_OFFSET_BAD ||
368 u->gc_marker == gc_marker + GC_OFFSET_IN_PATH)
371 if (u->in_cleanup_queue)
374 if (unit_check_gc(u))
377 u->gc_marker = gc_marker + GC_OFFSET_IN_PATH;
381 SET_FOREACH(other, u->dependencies[UNIT_REFERENCED_BY], i) {
382 unit_gc_sweep(other, gc_marker);
384 if (other->gc_marker == gc_marker + GC_OFFSET_GOOD)
387 if (other->gc_marker != gc_marker + GC_OFFSET_BAD)
394 /* We were unable to find anything out about this entry, so
395 * let's investigate it later */
396 u->gc_marker = gc_marker + GC_OFFSET_UNSURE;
397 unit_add_to_gc_queue(u);
401 /* We definitely know that this one is not useful anymore, so
402 * let's mark it for deletion */
403 u->gc_marker = gc_marker + GC_OFFSET_BAD;
404 unit_add_to_cleanup_queue(u);
408 u->gc_marker = gc_marker + GC_OFFSET_GOOD;
411 static unsigned manager_dispatch_gc_queue(Manager *m) {
418 if ((m->n_in_gc_queue < GC_QUEUE_ENTRIES_MAX) &&
419 (m->gc_queue_timestamp <= 0 ||
420 (m->gc_queue_timestamp + GC_QUEUE_USEC_MAX) > now(CLOCK_MONOTONIC)))
423 log_debug("Running GC...");
425 m->gc_marker += _GC_OFFSET_MAX;
426 if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX)
429 gc_marker = m->gc_marker;
431 while ((u = m->gc_queue)) {
432 assert(u->in_gc_queue);
434 unit_gc_sweep(u, gc_marker);
436 LIST_REMOVE(Unit, gc_queue, m->gc_queue, u);
437 u->in_gc_queue = false;
441 if (u->gc_marker == gc_marker + GC_OFFSET_BAD ||
442 u->gc_marker == gc_marker + GC_OFFSET_UNSURE) {
443 log_debug("Collecting %s", u->id);
444 u->gc_marker = gc_marker + GC_OFFSET_BAD;
445 unit_add_to_cleanup_queue(u);
449 m->n_in_gc_queue = 0;
450 m->gc_queue_timestamp = 0;
455 static void manager_clear_jobs_and_units(Manager *m) {
461 while ((j = hashmap_first(m->transaction_jobs)))
464 while ((u = hashmap_first(m->units)))
467 manager_dispatch_cleanup_queue(m);
469 assert(!m->load_queue);
470 assert(!m->run_queue);
471 assert(!m->dbus_unit_queue);
472 assert(!m->dbus_job_queue);
473 assert(!m->cleanup_queue);
474 assert(!m->gc_queue);
476 assert(hashmap_isempty(m->transaction_jobs));
477 assert(hashmap_isempty(m->jobs));
478 assert(hashmap_isempty(m->units));
481 void manager_free(Manager *m) {
486 manager_clear_jobs_and_units(m);
488 for (c = 0; c < _UNIT_TYPE_MAX; c++)
489 if (unit_vtable[c]->shutdown)
490 unit_vtable[c]->shutdown(m);
492 /* If we reexecute ourselves, we keep the root cgroup
494 manager_shutdown_cgroup(m, m->exit_code != MANAGER_REEXECUTE);
496 manager_undo_generators(m);
500 hashmap_free(m->units);
501 hashmap_free(m->jobs);
502 hashmap_free(m->transaction_jobs);
503 hashmap_free(m->watch_pids);
504 hashmap_free(m->watch_bus);
506 if (m->epoll_fd >= 0)
507 close_nointr_nofail(m->epoll_fd);
508 if (m->signal_watch.fd >= 0)
509 close_nointr_nofail(m->signal_watch.fd);
510 if (m->notify_watch.fd >= 0)
511 close_nointr_nofail(m->notify_watch.fd);
514 if (m->audit_fd >= 0)
515 audit_close(m->audit_fd);
518 free(m->notify_socket);
520 lookup_paths_free(&m->lookup_paths);
521 strv_free(m->environment);
523 strv_free(m->default_controllers);
525 hashmap_free(m->cgroup_bondings);
526 set_free_free(m->unit_path_cache);
531 int manager_enumerate(Manager *m) {
537 /* Let's ask every type to load all units from disk/kernel
538 * that it might know */
539 for (c = 0; c < _UNIT_TYPE_MAX; c++)
540 if (unit_vtable[c]->enumerate)
541 if ((q = unit_vtable[c]->enumerate(m)) < 0)
544 manager_dispatch_load_queue(m);
548 int manager_coldplug(Manager *m) {
556 /* Then, let's set up their initial state. */
557 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
563 if ((q = unit_coldplug(u)) < 0)
570 static void manager_build_unit_path_cache(Manager *m) {
577 set_free_free(m->unit_path_cache);
579 if (!(m->unit_path_cache = set_new(string_hash_func, string_compare_func))) {
580 log_error("Failed to allocate unit path cache.");
584 /* This simply builds a list of files we know exist, so that
585 * we don't always have to go to disk */
587 STRV_FOREACH(i, m->lookup_paths.unit_path) {
590 if (!(d = opendir(*i))) {
591 log_error("Failed to open directory: %m");
595 while ((de = readdir(d))) {
598 if (ignore_file(de->d_name))
601 p = join(streq(*i, "/") ? "" : *i, "/", de->d_name, NULL);
607 if ((r = set_put(m->unit_path_cache, p)) < 0) {
620 log_error("Failed to build unit path cache: %s", strerror(-r));
622 set_free_free(m->unit_path_cache);
623 m->unit_path_cache = NULL;
629 int manager_startup(Manager *m, FILE *serialization, FDSet *fds) {
634 manager_run_generators(m);
636 manager_build_unit_path_cache(m);
638 /* If we will deserialize make sure that during enumeration
639 * this is already known, so we increase the counter here
644 /* First, enumerate what we can from all config files */
645 r = manager_enumerate(m);
647 /* Second, deserialize if there is something to deserialize */
649 if ((q = manager_deserialize(m, serialization, fds)) < 0)
652 /* Third, fire things up! */
653 if ((q = manager_coldplug(m)) < 0)
657 assert(m->n_reloading > 0);
664 static void transaction_delete_job(Manager *m, Job *j, bool delete_dependencies) {
668 /* Deletes one job from the transaction */
670 manager_transaction_unlink_job(m, j, delete_dependencies);
676 static void transaction_delete_unit(Manager *m, Unit *u) {
679 /* Deletes all jobs associated with a certain unit from the
682 while ((j = hashmap_get(m->transaction_jobs, u)))
683 transaction_delete_job(m, j, true);
686 static void transaction_clean_dependencies(Manager *m) {
692 /* Drops all dependencies of all installed jobs */
694 HASHMAP_FOREACH(j, m->jobs, i) {
695 while (j->subject_list)
696 job_dependency_free(j->subject_list);
697 while (j->object_list)
698 job_dependency_free(j->object_list);
701 assert(!m->transaction_anchor);
704 static void transaction_abort(Manager *m) {
709 while ((j = hashmap_first(m->transaction_jobs)))
711 transaction_delete_job(m, j, true);
715 assert(hashmap_isempty(m->transaction_jobs));
717 transaction_clean_dependencies(m);
720 static void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) {
725 /* A recursive sweep through the graph that marks all units
726 * that matter to the anchor job, i.e. are directly or
727 * indirectly a dependency of the anchor job via paths that
728 * are fully marked as mattering. */
733 l = m->transaction_anchor;
735 LIST_FOREACH(subject, l, l) {
737 /* This link does not matter */
741 /* This unit has already been marked */
742 if (l->object->generation == generation)
745 l->object->matters_to_anchor = true;
746 l->object->generation = generation;
748 transaction_find_jobs_that_matter_to_anchor(m, l->object, generation);
752 static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) {
753 JobDependency *l, *last;
757 assert(j->unit == other->unit);
758 assert(!j->installed);
760 /* Merges 'other' into 'j' and then deletes j. */
763 j->state = JOB_WAITING;
764 j->override = j->override || other->override;
766 j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
768 /* Patch us in as new owner of the JobDependency objects */
770 LIST_FOREACH(subject, l, other->subject_list) {
771 assert(l->subject == other);
776 /* Merge both lists */
778 last->subject_next = j->subject_list;
780 j->subject_list->subject_prev = last;
781 j->subject_list = other->subject_list;
784 /* Patch us in as new owner of the JobDependency objects */
786 LIST_FOREACH(object, l, other->object_list) {
787 assert(l->object == other);
792 /* Merge both lists */
794 last->object_next = j->object_list;
796 j->object_list->object_prev = last;
797 j->object_list = other->object_list;
800 /* Kill the other job */
801 other->subject_list = NULL;
802 other->object_list = NULL;
803 transaction_delete_job(m, other, true);
805 static bool job_is_conflicted_by(Job *j) {
810 /* Returns true if this job is pulled in by a least one
811 * ConflictedBy dependency. */
813 LIST_FOREACH(object, l, j->object_list)
820 static int delete_one_unmergeable_job(Manager *m, Job *j) {
825 /* Tries to delete one item in the linked list
826 * j->transaction_next->transaction_next->... that conflicts
827 * with another one, in an attempt to make an inconsistent
828 * transaction work. */
830 /* We rely here on the fact that if a merged with b does not
831 * merge with c, either a or b merge with c neither */
832 LIST_FOREACH(transaction, j, j)
833 LIST_FOREACH(transaction, k, j->transaction_next) {
836 /* Is this one mergeable? Then skip it */
837 if (job_type_is_mergeable(j->type, k->type))
840 /* Ok, we found two that conflict, let's see if we can
841 * drop one of them */
842 if (!j->matters_to_anchor && !k->matters_to_anchor) {
844 /* Both jobs don't matter, so let's
845 * find the one that is smarter to
846 * remove. Let's think positive and
847 * rather remove stops then starts --
848 * except if something is being
849 * stopped because it is conflicted by
850 * another unit in which case we
851 * rather remove the start. */
853 log_debug("Looking at job %s/%s conflicted_by=%s", j->unit->id, job_type_to_string(j->type), yes_no(j->type == JOB_STOP && job_is_conflicted_by(j)));
854 log_debug("Looking at job %s/%s conflicted_by=%s", k->unit->id, job_type_to_string(k->type), yes_no(k->type == JOB_STOP && job_is_conflicted_by(k)));
856 if (j->type == JOB_STOP) {
858 if (job_is_conflicted_by(j))
863 } else if (k->type == JOB_STOP) {
865 if (job_is_conflicted_by(k))
872 } else if (!j->matters_to_anchor)
874 else if (!k->matters_to_anchor)
879 /* Ok, we can drop one, so let's do so. */
880 log_debug("Fixing conflicting jobs by deleting job %s/%s", d->unit->id, job_type_to_string(d->type));
881 transaction_delete_job(m, d, true);
888 static int transaction_merge_jobs(Manager *m, DBusError *e) {
895 /* First step, check whether any of the jobs for one specific
896 * task conflict. If so, try to drop one of them. */
897 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
902 LIST_FOREACH(transaction, k, j->transaction_next) {
903 if (job_type_merge(&t, k->type) >= 0)
906 /* OK, we could not merge all jobs for this
907 * action. Let's see if we can get rid of one
910 if ((r = delete_one_unmergeable_job(m, j)) >= 0)
911 /* Ok, we managed to drop one, now
912 * let's ask our callers to call us
913 * again after garbage collecting */
916 /* We couldn't merge anything. Failure */
917 dbus_set_error(e, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING, "Transaction contains conflicting jobs '%s' and '%s' for %s. Probably contradicting requirement dependencies configured.",
918 job_type_to_string(t), job_type_to_string(k->type), k->unit->id);
923 /* Second step, merge the jobs. */
924 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
928 /* Merge all transactions */
929 LIST_FOREACH(transaction, k, j->transaction_next)
930 assert_se(job_type_merge(&t, k->type) == 0);
932 /* If an active job is mergeable, merge it too */
934 job_type_merge(&t, j->unit->job->type); /* Might fail. Which is OK */
936 while ((k = j->transaction_next)) {
938 transaction_merge_and_delete_job(m, k, j, t);
941 transaction_merge_and_delete_job(m, j, k, t);
944 if (j->unit->job && !j->installed)
945 transaction_merge_and_delete_job(m, j, j->unit->job, t);
947 assert(!j->transaction_next);
948 assert(!j->transaction_prev);
954 static void transaction_drop_redundant(Manager *m) {
959 /* Goes through the transaction and removes all jobs that are
968 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
969 bool changes_something = false;
972 LIST_FOREACH(transaction, k, j) {
974 if (!job_is_anchor(k) &&
975 (k->installed || job_type_is_redundant(k->type, unit_active_state(k->unit))) &&
976 (!k->unit->job || !job_type_is_conflicting(k->type, k->unit->job->type)))
979 changes_something = true;
983 if (changes_something)
986 /* log_debug("Found redundant job %s/%s, dropping.", j->unit->id, job_type_to_string(j->type)); */
987 transaction_delete_job(m, j, false);
995 static bool unit_matters_to_anchor(Unit *u, Job *j) {
997 assert(!j->transaction_prev);
999 /* Checks whether at least one of the jobs for this unit
1000 * matters to the anchor. */
1002 LIST_FOREACH(transaction, j, j)
1003 if (j->matters_to_anchor)
1009 static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation, DBusError *e) {
1016 assert(!j->transaction_prev);
1018 /* Does a recursive sweep through the ordering graph, looking
1019 * for a cycle. If we find cycle we try to break it. */
1021 /* Have we seen this before? */
1022 if (j->generation == generation) {
1025 /* If the marker is NULL we have been here already and
1026 * decided the job was loop-free from here. Hence
1027 * shortcut things and return right-away. */
1031 /* So, the marker is not NULL and we already have been
1032 * here. We have a cycle. Let's try to break it. We go
1033 * backwards in our path and try to find a suitable
1034 * job to remove. We use the marker to find our way
1035 * back, since smart how we are we stored our way back
1037 log_warning("Found ordering cycle on %s/%s", j->unit->id, job_type_to_string(j->type));
1040 for (k = from; k; k = ((k->generation == generation && k->marker != k) ? k->marker : NULL)) {
1042 log_info("Walked on cycle path to %s/%s", k->unit->id, job_type_to_string(k->type));
1046 !unit_matters_to_anchor(k->unit, k)) {
1047 /* Ok, we can drop this one, so let's
1052 /* Check if this in fact was the beginning of
1060 log_warning("Breaking ordering cycle by deleting job %s/%s", delete->unit->id, job_type_to_string(delete->type));
1061 transaction_delete_unit(m, delete->unit);
1065 log_error("Unable to break cycle");
1067 dbus_set_error(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC, "Transaction order is cyclic. See system logs for details.");
1071 /* Make the marker point to where we come from, so that we can
1072 * find our way backwards if we want to break a cycle. We use
1073 * a special marker for the beginning: we point to
1075 j->marker = from ? from : j;
1076 j->generation = generation;
1078 /* We assume that the the dependencies are bidirectional, and
1079 * hence can ignore UNIT_AFTER */
1080 SET_FOREACH(u, j->unit->dependencies[UNIT_BEFORE], i) {
1083 /* Is there a job for this unit? */
1084 if (!(o = hashmap_get(m->transaction_jobs, u)))
1086 /* Ok, there is no job for this in the
1087 * transaction, but maybe there is already one
1092 if ((r = transaction_verify_order_one(m, o, j, generation, e)) < 0)
1096 /* Ok, let's backtrack, and remember that this entry is not on
1097 * our path anymore. */
1103 static int transaction_verify_order(Manager *m, unsigned *generation, DBusError *e) {
1112 /* Check if the ordering graph is cyclic. If it is, try to fix
1113 * that up by dropping one of the jobs. */
1115 g = (*generation)++;
1117 HASHMAP_FOREACH(j, m->transaction_jobs, i)
1118 if ((r = transaction_verify_order_one(m, j, NULL, g, e)) < 0)
1124 static void transaction_collect_garbage(Manager *m) {
1129 /* Drop jobs that are not required by any other job */
1137 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1138 if (j->object_list) {
1139 /* log_debug("Keeping job %s/%s because of %s/%s", */
1140 /* j->unit->id, job_type_to_string(j->type), */
1141 /* j->object_list->subject ? j->object_list->subject->unit->id : "root", */
1142 /* j->object_list->subject ? job_type_to_string(j->object_list->subject->type) : "root"); */
1146 /* log_debug("Garbage collecting job %s/%s", j->unit->id, job_type_to_string(j->type)); */
1147 transaction_delete_job(m, j, true);
1155 static int transaction_is_destructive(Manager *m, DBusError *e) {
1161 /* Checks whether applying this transaction means that
1162 * existing jobs would be replaced */
1164 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1167 assert(!j->transaction_prev);
1168 assert(!j->transaction_next);
1171 j->unit->job != j &&
1172 !job_type_is_superset(j->type, j->unit->job->type)) {
1174 dbus_set_error(e, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE, "Transaction is destructive.");
1182 static void transaction_minimize_impact(Manager *m) {
1186 /* Drops all unnecessary jobs that reverse already active jobs
1187 * or that stop a running service. */
1195 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1196 LIST_FOREACH(transaction, j, j) {
1197 bool stops_running_service, changes_existing_job;
1199 /* If it matters, we shouldn't drop it */
1200 if (j->matters_to_anchor)
1203 /* Would this stop a running service?
1204 * Would this change an existing job?
1205 * If so, let's drop this entry */
1207 stops_running_service =
1208 j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
1210 changes_existing_job =
1212 job_type_is_conflicting(j->type, j->unit->job->type);
1214 if (!stops_running_service && !changes_existing_job)
1217 if (stops_running_service)
1218 log_debug("%s/%s would stop a running service.", j->unit->id, job_type_to_string(j->type));
1220 if (changes_existing_job)
1221 log_debug("%s/%s would change existing job.", j->unit->id, job_type_to_string(j->type));
1223 /* Ok, let's get rid of this */
1224 log_debug("Deleting %s/%s to minimize impact.", j->unit->id, job_type_to_string(j->type));
1226 transaction_delete_job(m, j, true);
1238 static int transaction_apply(Manager *m, JobMode mode) {
1243 /* Moves the transaction jobs to the set of active jobs */
1245 if (mode == JOB_ISOLATE) {
1247 /* When isolating first kill all installed jobs which
1248 * aren't part of the new transaction */
1250 HASHMAP_FOREACH(j, m->jobs, i) {
1251 assert(j->installed);
1253 if (hashmap_get(m->transaction_jobs, j->unit))
1256 /* 'j' itself is safe to remove, but if other jobs
1257 are invalidated recursively, our iterator may become
1258 invalid and we need to start over. */
1259 if (job_finish_and_invalidate(j, JOB_CANCELED) > 0)
1264 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1266 assert(!j->transaction_prev);
1267 assert(!j->transaction_next);
1272 if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
1276 while ((j = hashmap_steal_first(m->transaction_jobs))) {
1278 /* log_debug("Skipping already installed job %s/%s as %u", j->unit->id, job_type_to_string(j->type), (unsigned) j->id); */
1283 job_free(j->unit->job);
1286 j->installed = true;
1287 m->n_installed_jobs ++;
1289 /* We're fully installed. Now let's free data we don't
1292 assert(!j->transaction_next);
1293 assert(!j->transaction_prev);
1295 job_add_to_run_queue(j);
1296 job_add_to_dbus_queue(j);
1299 log_debug("Installed new job %s/%s as %u", j->unit->id, job_type_to_string(j->type), (unsigned) j->id);
1302 /* As last step, kill all remaining job dependencies. */
1303 transaction_clean_dependencies(m);
1309 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1313 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
1319 static int transaction_activate(Manager *m, JobMode mode, DBusError *e) {
1321 unsigned generation = 1;
1325 /* This applies the changes recorded in transaction_jobs to
1326 * the actual list of jobs, if possible. */
1328 /* First step: figure out which jobs matter */
1329 transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
1331 /* Second step: Try not to stop any running services if
1332 * we don't have to. Don't try to reverse running
1333 * jobs if we don't have to. */
1334 if (mode == JOB_FAIL)
1335 transaction_minimize_impact(m);
1337 /* Third step: Drop redundant jobs */
1338 transaction_drop_redundant(m);
1341 /* Fourth step: Let's remove unneeded jobs that might
1343 if (mode != JOB_ISOLATE)
1344 transaction_collect_garbage(m);
1346 /* Fifth step: verify order makes sense and correct
1347 * cycles if necessary and possible */
1348 if ((r = transaction_verify_order(m, &generation, e)) >= 0)
1352 log_warning("Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error(e, r));
1356 /* Let's see if the resulting transaction ordering
1357 * graph is still cyclic... */
1361 /* Sixth step: let's drop unmergeable entries if
1362 * necessary and possible, merge entries we can
1364 if ((r = transaction_merge_jobs(m, e)) >= 0)
1368 log_warning("Requested transaction contains unmergeable jobs: %s", bus_error(e, r));
1372 /* Seventh step: an entry got dropped, let's garbage
1373 * collect its dependencies. */
1374 if (mode != JOB_ISOLATE)
1375 transaction_collect_garbage(m);
1377 /* Let's see if the resulting transaction still has
1378 * unmergeable entries ... */
1381 /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
1382 transaction_drop_redundant(m);
1384 /* Ninth step: check whether we can actually apply this */
1385 if (mode == JOB_FAIL)
1386 if ((r = transaction_is_destructive(m, e)) < 0) {
1387 log_notice("Requested transaction contradicts existing jobs: %s", bus_error(e, r));
1391 /* Tenth step: apply changes */
1392 if ((r = transaction_apply(m, mode)) < 0) {
1393 log_warning("Failed to apply transaction: %s", strerror(-r));
1397 assert(hashmap_isempty(m->transaction_jobs));
1398 assert(!m->transaction_anchor);
1403 transaction_abort(m);
1407 static Job* transaction_add_one_job(Manager *m, JobType type, Unit *unit, bool override, bool *is_new) {
1413 /* Looks for an existing prospective job and returns that. If
1414 * it doesn't exist it is created and added to the prospective
1417 f = hashmap_get(m->transaction_jobs, unit);
1419 LIST_FOREACH(transaction, j, f) {
1420 assert(j->unit == unit);
1422 if (j->type == type) {
1429 if (unit->job && unit->job->type == type)
1431 else if (!(j = job_new(m, type, unit)))
1436 j->matters_to_anchor = false;
1437 j->override = override;
1439 LIST_PREPEND(Job, transaction, f, j);
1441 if (hashmap_replace(m->transaction_jobs, unit, f) < 0) {
1449 /* log_debug("Added job %s/%s to transaction.", unit->id, job_type_to_string(type)); */
1454 void manager_transaction_unlink_job(Manager *m, Job *j, bool delete_dependencies) {
1458 if (j->transaction_prev)
1459 j->transaction_prev->transaction_next = j->transaction_next;
1460 else if (j->transaction_next)
1461 hashmap_replace(m->transaction_jobs, j->unit, j->transaction_next);
1463 hashmap_remove_value(m->transaction_jobs, j->unit, j);
1465 if (j->transaction_next)
1466 j->transaction_next->transaction_prev = j->transaction_prev;
1468 j->transaction_prev = j->transaction_next = NULL;
1470 while (j->subject_list)
1471 job_dependency_free(j->subject_list);
1473 while (j->object_list) {
1474 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
1476 job_dependency_free(j->object_list);
1478 if (other && delete_dependencies) {
1479 log_debug("Deleting job %s/%s as dependency of job %s/%s",
1480 other->unit->id, job_type_to_string(other->type),
1481 j->unit->id, job_type_to_string(j->type));
1482 transaction_delete_job(m, other, delete_dependencies);
1487 static int transaction_add_job_and_dependencies(
1495 bool ignore_requirements,
1506 assert(type < _JOB_TYPE_MAX);
1509 /* log_debug("Pulling in %s/%s from %s/%s", */
1510 /* unit->id, job_type_to_string(type), */
1511 /* by ? by->unit->id : "NA", */
1512 /* by ? job_type_to_string(by->type) : "NA"); */
1514 if (unit->load_state != UNIT_LOADED &&
1515 unit->load_state != UNIT_ERROR &&
1516 unit->load_state != UNIT_MASKED) {
1517 dbus_set_error(e, BUS_ERROR_LOAD_FAILED, "Unit %s is not loaded properly.", unit->id);
1521 if (type != JOB_STOP && unit->load_state == UNIT_ERROR) {
1522 dbus_set_error(e, BUS_ERROR_LOAD_FAILED,
1523 "Unit %s failed to load: %s. "
1524 "See system logs and 'systemctl status %s' for details.",
1526 strerror(-unit->load_error),
1531 if (type != JOB_STOP && unit->load_state == UNIT_MASKED) {
1532 dbus_set_error(e, BUS_ERROR_MASKED, "Unit %s is masked.", unit->id);
1536 if (!unit_job_is_applicable(unit, type)) {
1537 dbus_set_error(e, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE, "Job type %s is not applicable for unit %s.", job_type_to_string(type), unit->id);
1541 /* First add the job. */
1542 if (!(ret = transaction_add_one_job(m, type, unit, override, &is_new)))
1545 ret->ignore_order = ret->ignore_order || ignore_order;
1547 /* Then, add a link to the job. */
1548 if (!job_dependency_new(by, ret, matters, conflicts))
1551 if (is_new && !ignore_requirements) {
1554 /* If we are following some other unit, make sure we
1555 * add all dependencies of everybody following. */
1556 if (unit_following_set(ret->unit, &following) > 0) {
1557 SET_FOREACH(dep, following, i)
1558 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, false, override, false, false, ignore_order, e, NULL)) < 0) {
1559 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
1565 set_free(following);
1568 /* Finally, recursively add in all dependencies. */
1569 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
1570 SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRES], i)
1571 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1579 SET_FOREACH(dep, ret->unit->dependencies[UNIT_BIND_TO], i)
1580 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1589 SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
1590 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !override, override, false, false, ignore_order, e, NULL)) < 0) {
1591 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
1597 SET_FOREACH(dep, ret->unit->dependencies[UNIT_WANTS], i)
1598 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, false, false, false, ignore_order, e, NULL)) < 0) {
1599 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
1605 SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUISITE], i)
1606 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1615 SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUISITE_OVERRIDABLE], i)
1616 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, !override, override, false, false, ignore_order, e, NULL)) < 0) {
1617 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
1623 SET_FOREACH(dep, ret->unit->dependencies[UNIT_CONFLICTS], i)
1624 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, override, true, false, ignore_order, e, NULL)) < 0) {
1633 SET_FOREACH(dep, ret->unit->dependencies[UNIT_CONFLICTED_BY], i)
1634 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, false, override, false, false, ignore_order, e, NULL)) < 0) {
1635 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
1643 if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
1645 SET_FOREACH(dep, ret->unit->dependencies[UNIT_REQUIRED_BY], i)
1646 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1655 SET_FOREACH(dep, ret->unit->dependencies[UNIT_BOUND_BY], i)
1656 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1666 if (type == JOB_RELOAD || type == JOB_RELOAD_OR_START) {
1668 SET_FOREACH(dep, ret->unit->dependencies[UNIT_PROPAGATE_RELOAD_TO], i) {
1669 r = transaction_add_job_and_dependencies(m, JOB_RELOAD, dep, ret, false, override, false, false, ignore_order, e, NULL);
1672 log_warning("Cannot add dependency reload job for unit %s, ignoring: %s", dep->id, bus_error(e, r));
1680 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
1692 static int transaction_add_isolate_jobs(Manager *m) {
1700 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
1702 /* ignore aliases */
1706 if (u->ignore_on_isolate)
1709 /* No need to stop inactive jobs */
1710 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) && !u->job)
1713 /* Is there already something listed for this? */
1714 if (hashmap_get(m->transaction_jobs, u))
1717 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, u, NULL, true, false, false, false, false, NULL, NULL)) < 0)
1718 log_warning("Cannot add isolate job for unit %s, ignoring: %s", u->id, strerror(-r));
1724 int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool override, DBusError *e, Job **_ret) {
1729 assert(type < _JOB_TYPE_MAX);
1731 assert(mode < _JOB_MODE_MAX);
1733 if (mode == JOB_ISOLATE && type != JOB_START) {
1734 dbus_set_error(e, BUS_ERROR_INVALID_JOB_MODE, "Isolate is only valid for start.");
1738 if (mode == JOB_ISOLATE && !unit->allow_isolate) {
1739 dbus_set_error(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated.");
1743 log_debug("Trying to enqueue job %s/%s/%s", unit->id, job_type_to_string(type), job_mode_to_string(mode));
1745 if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, override, false,
1746 mode == JOB_IGNORE_DEPENDENCIES || mode == JOB_IGNORE_REQUIREMENTS,
1747 mode == JOB_IGNORE_DEPENDENCIES, e, &ret)) < 0) {
1748 transaction_abort(m);
1752 if (mode == JOB_ISOLATE)
1753 if ((r = transaction_add_isolate_jobs(m)) < 0) {
1754 transaction_abort(m);
1758 if ((r = transaction_activate(m, mode, e)) < 0)
1761 log_debug("Enqueued job %s/%s as %u", unit->id, job_type_to_string(type), (unsigned) ret->id);
1769 int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, bool override, DBusError *e, Job **_ret) {
1774 assert(type < _JOB_TYPE_MAX);
1776 assert(mode < _JOB_MODE_MAX);
1778 if ((r = manager_load_unit(m, name, NULL, NULL, &unit)) < 0)
1781 return manager_add_job(m, type, unit, mode, override, e, _ret);
1784 Job *manager_get_job(Manager *m, uint32_t id) {
1787 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
1790 Unit *manager_get_unit(Manager *m, const char *name) {
1794 return hashmap_get(m->units, name);
1797 unsigned manager_dispatch_load_queue(Manager *m) {
1803 /* Make sure we are not run recursively */
1804 if (m->dispatching_load_queue)
1807 m->dispatching_load_queue = true;
1809 /* Dispatches the load queue. Takes a unit from the queue and
1810 * tries to load its data until the queue is empty */
1812 while ((u = m->load_queue)) {
1813 assert(u->in_load_queue);
1819 m->dispatching_load_queue = false;
1823 int manager_load_unit_prepare(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
1829 assert(name || path);
1831 /* This will prepare the unit for loading, but not actually
1832 * load anything from disk. */
1834 if (path && !is_path(path)) {
1835 dbus_set_error(e, BUS_ERROR_INVALID_PATH, "Path %s is not absolute.", path);
1840 name = file_name_from_path(path);
1842 t = unit_name_to_type(name);
1844 if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid_no_type(name, false)) {
1845 dbus_set_error(e, BUS_ERROR_INVALID_NAME, "Unit name %s is not valid.", name);
1849 ret = manager_get_unit(m, name);
1855 ret = unit_new(m, unit_vtable[t]->object_size);
1860 ret->fragment_path = strdup(path);
1861 if (!ret->fragment_path) {
1867 if ((r = unit_add_name(ret, name)) < 0) {
1872 unit_add_to_load_queue(ret);
1873 unit_add_to_dbus_queue(ret);
1874 unit_add_to_gc_queue(ret);
1882 int manager_load_unit(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
1887 /* This will load the service information files, but not actually
1888 * start any services or anything. */
1890 if ((r = manager_load_unit_prepare(m, name, path, e, _ret)) != 0)
1893 manager_dispatch_load_queue(m);
1896 *_ret = unit_follow_merge(*_ret);
1901 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
1908 HASHMAP_FOREACH(j, s->jobs, i)
1909 job_dump(j, f, prefix);
1912 void manager_dump_units(Manager *s, FILE *f, const char *prefix) {
1920 HASHMAP_FOREACH_KEY(u, t, s->units, i)
1922 unit_dump(u, f, prefix);
1925 void manager_clear_jobs(Manager *m) {
1930 transaction_abort(m);
1932 while ((j = hashmap_first(m->jobs)))
1933 job_finish_and_invalidate(j, JOB_CANCELED);
1936 unsigned manager_dispatch_run_queue(Manager *m) {
1940 if (m->dispatching_run_queue)
1943 m->dispatching_run_queue = true;
1945 while ((j = m->run_queue)) {
1946 assert(j->installed);
1947 assert(j->in_run_queue);
1949 job_run_and_invalidate(j);
1953 m->dispatching_run_queue = false;
1957 unsigned manager_dispatch_dbus_queue(Manager *m) {
1964 if (m->dispatching_dbus_queue)
1967 m->dispatching_dbus_queue = true;
1969 while ((u = m->dbus_unit_queue)) {
1970 assert(u->in_dbus_queue);
1972 bus_unit_send_change_signal(u);
1976 while ((j = m->dbus_job_queue)) {
1977 assert(j->in_dbus_queue);
1979 bus_job_send_change_signal(j);
1983 m->dispatching_dbus_queue = false;
1987 static int manager_process_notify_fd(Manager *m) {
1994 struct msghdr msghdr;
1996 struct ucred *ucred;
1998 struct cmsghdr cmsghdr;
1999 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
2005 iovec.iov_base = buf;
2006 iovec.iov_len = sizeof(buf)-1;
2010 msghdr.msg_iov = &iovec;
2011 msghdr.msg_iovlen = 1;
2012 msghdr.msg_control = &control;
2013 msghdr.msg_controllen = sizeof(control);
2015 if ((n = recvmsg(m->notify_watch.fd, &msghdr, MSG_DONTWAIT)) <= 0) {
2019 if (errno == EAGAIN || errno == EINTR)
2025 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
2026 control.cmsghdr.cmsg_level != SOL_SOCKET ||
2027 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
2028 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
2029 log_warning("Received notify message without credentials. Ignoring.");
2033 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
2035 if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(ucred->pid))))
2036 if (!(u = cgroup_unit_by_pid(m, ucred->pid))) {
2037 log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid);
2041 assert((size_t) n < sizeof(buf));
2043 if (!(tags = strv_split(buf, "\n\r")))
2046 log_debug("Got notification message for unit %s", u->id);
2048 if (UNIT_VTABLE(u)->notify_message)
2049 UNIT_VTABLE(u)->notify_message(u, ucred->pid, tags);
2057 static int manager_dispatch_sigchld(Manager *m) {
2067 /* First we call waitd() for a PID and do not reap the
2068 * zombie. That way we can still access /proc/$PID for
2069 * it while it is a zombie. */
2070 if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) {
2072 if (errno == ECHILD)
2084 if (si.si_code == CLD_EXITED || si.si_code == CLD_KILLED || si.si_code == CLD_DUMPED) {
2087 get_process_comm(si.si_pid, &name);
2088 log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si.si_pid, strna(name));
2092 /* Let's flush any message the dying child might still
2093 * have queued for us. This ensures that the process
2094 * still exists in /proc so that we can figure out
2095 * which cgroup and hence unit it belongs to. */
2096 if ((r = manager_process_notify_fd(m)) < 0)
2099 /* And now figure out the unit this belongs to */
2100 if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(si.si_pid))))
2101 u = cgroup_unit_by_pid(m, si.si_pid);
2103 /* And now, we actually reap the zombie. */
2104 if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) {
2111 if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED)
2114 log_debug("Child %lu died (code=%s, status=%i/%s)",
2115 (long unsigned) si.si_pid,
2116 sigchld_code_to_string(si.si_code),
2118 strna(si.si_code == CLD_EXITED
2119 ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL)
2120 : signal_to_string(si.si_status)));
2125 log_debug("Child %lu belongs to %s", (long unsigned) si.si_pid, u->id);
2127 hashmap_remove(m->watch_pids, LONG_TO_PTR(si.si_pid));
2128 UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status);
2134 static int manager_start_target(Manager *m, const char *name, JobMode mode) {
2138 dbus_error_init(&error);
2140 log_debug("Activating special unit %s", name);
2142 if ((r = manager_add_job_by_name(m, JOB_START, name, mode, true, &error, NULL)) < 0)
2143 log_error("Failed to enqueue %s job: %s", name, bus_error(&error, r));
2145 dbus_error_free(&error);
2150 static int manager_process_signal_fd(Manager *m) {
2152 struct signalfd_siginfo sfsi;
2153 bool sigchld = false;
2158 if ((n = read(m->signal_watch.fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
2163 if (errno == EINTR || errno == EAGAIN)
2169 if (sfsi.ssi_pid > 0) {
2172 get_process_comm(sfsi.ssi_pid, &p);
2174 log_debug("Received SIG%s from PID %lu (%s).",
2175 signal_to_string(sfsi.ssi_signo),
2176 (unsigned long) sfsi.ssi_pid, strna(p));
2179 log_debug("Received SIG%s.", signal_to_string(sfsi.ssi_signo));
2181 switch (sfsi.ssi_signo) {
2188 if (m->running_as == MANAGER_SYSTEM) {
2189 /* This is for compatibility with the
2190 * original sysvinit */
2191 m->exit_code = MANAGER_REEXECUTE;
2198 if (m->running_as == MANAGER_SYSTEM) {
2199 manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE);
2203 /* Run the exit target if there is one, if not, just exit. */
2204 if (manager_start_target(m, SPECIAL_EXIT_TARGET, JOB_REPLACE) < 0) {
2205 m->exit_code = MANAGER_EXIT;
2212 if (m->running_as == MANAGER_SYSTEM)
2213 manager_start_target(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE);
2215 /* This is a nop on non-init */
2219 if (m->running_as == MANAGER_SYSTEM)
2220 manager_start_target(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE);
2222 /* This is a nop on non-init */
2228 u = manager_get_unit(m, SPECIAL_DBUS_SERVICE);
2230 if (!u || UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) {
2231 log_info("Trying to reconnect to bus...");
2235 if (!u || !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) {
2236 log_info("Loading D-Bus service...");
2237 manager_start_target(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE);
2248 if (!(f = open_memstream(&dump, &size))) {
2249 log_warning("Failed to allocate memory stream.");
2253 manager_dump_units(m, f, "\t");
2254 manager_dump_jobs(m, f, "\t");
2259 log_warning("Failed to write status stream");
2264 log_dump(LOG_INFO, dump);
2271 m->exit_code = MANAGER_RELOAD;
2276 /* Starting SIGRTMIN+0 */
2277 static const char * const target_table[] = {
2278 [0] = SPECIAL_DEFAULT_TARGET,
2279 [1] = SPECIAL_RESCUE_TARGET,
2280 [2] = SPECIAL_EMERGENCY_TARGET,
2281 [3] = SPECIAL_HALT_TARGET,
2282 [4] = SPECIAL_POWEROFF_TARGET,
2283 [5] = SPECIAL_REBOOT_TARGET,
2284 [6] = SPECIAL_KEXEC_TARGET
2287 /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */
2288 static const ManagerExitCode code_table[] = {
2290 [1] = MANAGER_POWEROFF,
2291 [2] = MANAGER_REBOOT,
2295 if ((int) sfsi.ssi_signo >= SIGRTMIN+0 &&
2296 (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) {
2297 int idx = (int) sfsi.ssi_signo - SIGRTMIN;
2298 manager_start_target(m, target_table[idx],
2299 (idx == 1 || idx == 2) ? JOB_ISOLATE : JOB_REPLACE);
2303 if ((int) sfsi.ssi_signo >= SIGRTMIN+13 &&
2304 (int) sfsi.ssi_signo < SIGRTMIN+13+(int) ELEMENTSOF(code_table)) {
2305 m->exit_code = code_table[sfsi.ssi_signo - SIGRTMIN - 13];
2309 switch (sfsi.ssi_signo - SIGRTMIN) {
2312 log_debug("Enabling showing of status.");
2313 manager_set_show_status(m, true);
2317 log_debug("Disabling showing of status.");
2318 manager_set_show_status(m, false);
2322 log_set_max_level(LOG_DEBUG);
2323 log_notice("Setting log level to debug.");
2327 log_set_max_level(LOG_INFO);
2328 log_notice("Setting log level to info.");
2332 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
2333 log_notice("Setting log target to journal-or-kmsg.");
2337 log_set_target(LOG_TARGET_CONSOLE);
2338 log_notice("Setting log target to console.");
2342 log_set_target(LOG_TARGET_KMSG);
2343 log_notice("Setting log target to kmsg.");
2347 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
2348 log_notice("Setting log target to syslog-or-kmsg.");
2352 log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi.ssi_signo));
2359 return manager_dispatch_sigchld(m);
2364 static int process_event(Manager *m, struct epoll_event *ev) {
2371 assert_se(w = ev->data.ptr);
2373 if (w->type == WATCH_INVALID)
2380 /* An incoming signal? */
2381 if (ev->events != EPOLLIN)
2384 if ((r = manager_process_signal_fd(m)) < 0)
2391 /* An incoming daemon notification event? */
2392 if (ev->events != EPOLLIN)
2395 if ((r = manager_process_notify_fd(m)) < 0)
2402 /* Some fd event, to be dispatched to the units */
2403 UNIT_VTABLE(w->data.unit)->fd_event(w->data.unit, w->fd, ev->events, w);
2406 case WATCH_UNIT_TIMER:
2407 case WATCH_JOB_TIMER: {
2411 /* Some timer event, to be dispatched to the units */
2412 if ((k = read(w->fd, &v, sizeof(v))) != sizeof(v)) {
2414 if (k < 0 && (errno == EINTR || errno == EAGAIN))
2417 return k < 0 ? -errno : -EIO;
2420 if (w->type == WATCH_UNIT_TIMER)
2421 UNIT_VTABLE(w->data.unit)->timer_event(w->data.unit, v, w);
2423 job_timer_event(w->data.job, v, w);
2428 /* Some mount table change, intended for the mount subsystem */
2429 mount_fd_event(m, ev->events);
2433 /* Some swap table change, intended for the swap subsystem */
2434 swap_fd_event(m, ev->events);
2438 /* Some notification from udev, intended for the device subsystem */
2439 device_fd_event(m, ev->events);
2442 case WATCH_DBUS_WATCH:
2443 bus_watch_event(m, w, ev->events);
2446 case WATCH_DBUS_TIMEOUT:
2447 bus_timeout_event(m, w, ev->events);
2451 log_error("event type=%i", w->type);
2452 assert_not_reached("Unknown epoll event type.");
2458 int manager_loop(Manager *m) {
2461 RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 50000);
2464 m->exit_code = MANAGER_RUNNING;
2466 /* Release the path cache */
2467 set_free_free(m->unit_path_cache);
2468 m->unit_path_cache = NULL;
2470 manager_check_finished(m);
2472 /* There might still be some zombies hanging around from
2473 * before we were exec()'ed. Leat's reap them */
2474 r = manager_dispatch_sigchld(m);
2478 while (m->exit_code == MANAGER_RUNNING) {
2479 struct epoll_event event;
2483 if (m->runtime_watchdog > 0 && m->running_as == MANAGER_SYSTEM)
2486 if (!ratelimit_test(&rl)) {
2487 /* Yay, something is going seriously wrong, pause a little */
2488 log_warning("Looping too fast. Throttling execution a little.");
2493 if (manager_dispatch_load_queue(m) > 0)
2496 if (manager_dispatch_run_queue(m) > 0)
2499 if (bus_dispatch(m) > 0)
2502 if (manager_dispatch_cleanup_queue(m) > 0)
2505 if (manager_dispatch_gc_queue(m) > 0)
2508 if (manager_dispatch_dbus_queue(m) > 0)
2511 if (swap_dispatch_reload(m) > 0)
2514 /* Sleep for half the watchdog time */
2515 if (m->runtime_watchdog > 0 && m->running_as == MANAGER_SYSTEM) {
2516 wait_msec = (int) (m->runtime_watchdog / 2 / USEC_PER_MSEC);
2522 n = epoll_wait(m->epoll_fd, &event, 1, wait_msec);
2534 r = process_event(m, &event);
2539 return m->exit_code;
2542 int manager_get_unit_from_dbus_path(Manager *m, const char *s, Unit **_u) {
2550 if (!startswith(s, "/org/freedesktop/systemd1/unit/"))
2553 if (!(n = bus_path_unescape(s+31)))
2556 u = manager_get_unit(m, n);
2567 int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
2576 if (!startswith(s, "/org/freedesktop/systemd1/job/"))
2579 if ((r = safe_atou(s + 30, &id)) < 0)
2582 if (!(j = manager_get_job(m, id)))
2590 void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
2595 if (m->audit_fd < 0)
2598 /* Don't generate audit events if the service was already
2599 * started and we're just deserializing */
2600 if (m->n_reloading > 0)
2603 if (m->running_as != MANAGER_SYSTEM)
2606 if (u->type != UNIT_SERVICE)
2609 if (!(p = unit_name_to_prefix_and_instance(u->id))) {
2610 log_error("Failed to allocate unit name for audit message: %s", strerror(ENOMEM));
2614 if (audit_log_user_comm_message(m->audit_fd, type, "", p, NULL, NULL, NULL, success) < 0) {
2615 if (errno == EPERM) {
2616 /* We aren't allowed to send audit messages?
2617 * Then let's not retry again. */
2618 audit_close(m->audit_fd);
2621 log_warning("Failed to send audit message: %m");
2629 void manager_send_unit_plymouth(Manager *m, Unit *u) {
2631 union sockaddr_union sa;
2633 char *message = NULL;
2635 /* Don't generate plymouth events if the service was already
2636 * started and we're just deserializing */
2637 if (m->n_reloading > 0)
2640 if (m->running_as != MANAGER_SYSTEM)
2643 if (u->type != UNIT_SERVICE &&
2644 u->type != UNIT_MOUNT &&
2645 u->type != UNIT_SWAP)
2648 /* We set SOCK_NONBLOCK here so that we rather drop the
2649 * message then wait for plymouth */
2650 if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
2651 log_error("socket() failed: %m");
2656 sa.sa.sa_family = AF_UNIX;
2657 strncpy(sa.un.sun_path+1, "/org/freedesktop/plymouthd", sizeof(sa.un.sun_path)-1);
2658 if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
2660 if (errno != EPIPE &&
2663 errno != ECONNREFUSED &&
2664 errno != ECONNRESET &&
2665 errno != ECONNABORTED)
2666 log_error("connect() failed: %m");
2671 if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->id) + 1), u->id, &n) < 0) {
2672 log_error("Out of memory");
2677 if (write(fd, message, n + 1) != n + 1) {
2679 if (errno != EPIPE &&
2682 errno != ECONNREFUSED &&
2683 errno != ECONNRESET &&
2684 errno != ECONNABORTED)
2685 log_error("Failed to write Plymouth message: %m");
2692 close_nointr_nofail(fd);
2697 void manager_dispatch_bus_name_owner_changed(
2700 const char* old_owner,
2701 const char *new_owner) {
2708 if (!(u = hashmap_get(m->watch_bus, name)))
2711 UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner);
2714 void manager_dispatch_bus_query_pid_done(
2725 if (!(u = hashmap_get(m->watch_bus, name)))
2728 UNIT_VTABLE(u)->bus_query_pid_done(u, name, pid);
2731 int manager_open_serialization(Manager *m, FILE **_f) {
2739 if (m->running_as == MANAGER_SYSTEM)
2740 asprintf(&path, "/run/systemd/dump-%lu-XXXXXX", (unsigned long) getpid());
2742 asprintf(&path, "/tmp/systemd-dump-%lu-XXXXXX", (unsigned long) getpid());
2747 saved_umask = umask(0077);
2748 fd = mkostemp(path, O_RDWR|O_CLOEXEC);
2758 log_debug("Serializing state to %s", path);
2761 if (!(f = fdopen(fd, "w+")))
2769 int manager_serialize(Manager *m, FILE *f, FDSet *fds) {
2781 fprintf(f, "current-job-id=%i\n", m->current_job_id);
2782 fprintf(f, "taint-usr=%s\n", yes_no(m->taint_usr));
2784 dual_timestamp_serialize(f, "initrd-timestamp", &m->initrd_timestamp);
2785 dual_timestamp_serialize(f, "startup-timestamp", &m->startup_timestamp);
2786 dual_timestamp_serialize(f, "finish-timestamp", &m->finish_timestamp);
2790 HASHMAP_FOREACH_KEY(u, t, m->units, i) {
2794 if (!unit_can_serialize(u))
2801 if ((r = unit_serialize(u, f, fds)) < 0) {
2807 assert(m->n_reloading > 0);
2813 r = bus_fdset_add_all(m, fds);
2820 int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
2826 log_debug("Deserializing state...");
2831 char line[LINE_MAX], *l;
2833 if (!fgets(line, sizeof(line), f)) {
2848 if (startswith(l, "current-job-id=")) {
2851 if (safe_atou32(l+15, &id) < 0)
2852 log_debug("Failed to parse current job id value %s", l+15);
2854 m->current_job_id = MAX(m->current_job_id, id);
2855 } else if (startswith(l, "taint-usr=")) {
2858 if ((b = parse_boolean(l+10)) < 0)
2859 log_debug("Failed to parse taint /usr flag %s", l+10);
2861 m->taint_usr = m->taint_usr || b;
2862 } else if (startswith(l, "initrd-timestamp="))
2863 dual_timestamp_deserialize(l+17, &m->initrd_timestamp);
2864 else if (startswith(l, "startup-timestamp="))
2865 dual_timestamp_deserialize(l+18, &m->startup_timestamp);
2866 else if (startswith(l, "finish-timestamp="))
2867 dual_timestamp_deserialize(l+17, &m->finish_timestamp);
2869 log_debug("Unknown serialization item '%s'", l);
2874 char name[UNIT_NAME_MAX+2];
2877 if (!fgets(name, sizeof(name), f)) {
2888 if ((r = manager_load_unit(m, strstrip(name), NULL, NULL, &u)) < 0)
2891 if ((r = unit_deserialize(u, f, fds)) < 0)
2901 assert(m->n_reloading > 0);
2907 int manager_reload(Manager *m) {
2914 if ((r = manager_open_serialization(m, &f)) < 0)
2919 if (!(fds = fdset_new())) {
2925 if ((r = manager_serialize(m, f, fds)) < 0) {
2930 if (fseeko(f, 0, SEEK_SET) < 0) {
2936 /* From here on there is no way back. */
2937 manager_clear_jobs_and_units(m);
2938 manager_undo_generators(m);
2940 /* Find new unit paths */
2941 lookup_paths_free(&m->lookup_paths);
2942 if ((q = lookup_paths_init(&m->lookup_paths, m->running_as, true)) < 0)
2945 manager_run_generators(m);
2947 manager_build_unit_path_cache(m);
2949 /* First, enumerate what we can from all config files */
2950 if ((q = manager_enumerate(m)) < 0)
2953 /* Second, deserialize our stored data */
2954 if ((q = manager_deserialize(m, f, fds)) < 0)
2960 /* Third, fire things up! */
2961 if ((q = manager_coldplug(m)) < 0)
2964 assert(m->n_reloading > 0);
2977 bool manager_is_booting_or_shutting_down(Manager *m) {
2982 /* Is the initial job still around? */
2983 if (manager_get_job(m, m->default_unit_job_id))
2986 /* Is there a job for the shutdown target? */
2987 u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET);
2994 void manager_reset_failed(Manager *m) {
3000 HASHMAP_FOREACH(u, m->units, i)
3001 unit_reset_failed(u);
3004 bool manager_unit_pending_inactive(Manager *m, const char *name) {
3010 /* Returns true if the unit is inactive or going down */
3011 if (!(u = manager_get_unit(m, name)))
3014 return unit_pending_inactive(u);
3017 void manager_check_finished(Manager *m) {
3018 char userspace[FORMAT_TIMESPAN_MAX], initrd[FORMAT_TIMESPAN_MAX], kernel[FORMAT_TIMESPAN_MAX], sum[FORMAT_TIMESPAN_MAX];
3019 usec_t kernel_usec, initrd_usec, userspace_usec, total_usec;
3023 if (dual_timestamp_is_set(&m->finish_timestamp))
3026 if (hashmap_size(m->jobs) > 0)
3029 dual_timestamp_get(&m->finish_timestamp);
3031 if (m->running_as == MANAGER_SYSTEM && detect_container(NULL) <= 0) {
3033 userspace_usec = m->finish_timestamp.monotonic - m->startup_timestamp.monotonic;
3034 total_usec = m->finish_timestamp.monotonic;
3036 if (dual_timestamp_is_set(&m->initrd_timestamp)) {
3038 kernel_usec = m->initrd_timestamp.monotonic;
3039 initrd_usec = m->startup_timestamp.monotonic - m->initrd_timestamp.monotonic;
3041 log_info("Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.",
3042 format_timespan(kernel, sizeof(kernel), kernel_usec),
3043 format_timespan(initrd, sizeof(initrd), initrd_usec),
3044 format_timespan(userspace, sizeof(userspace), userspace_usec),
3045 format_timespan(sum, sizeof(sum), total_usec));
3047 kernel_usec = m->startup_timestamp.monotonic;
3050 log_info("Startup finished in %s (kernel) + %s (userspace) = %s.",
3051 format_timespan(kernel, sizeof(kernel), kernel_usec),
3052 format_timespan(userspace, sizeof(userspace), userspace_usec),
3053 format_timespan(sum, sizeof(sum), total_usec));
3056 userspace_usec = initrd_usec = kernel_usec = 0;
3057 total_usec = m->finish_timestamp.monotonic - m->startup_timestamp.monotonic;
3059 log_debug("Startup finished in %s.",
3060 format_timespan(sum, sizeof(sum), total_usec));
3063 bus_broadcast_finished(m, kernel_usec, initrd_usec, userspace_usec, total_usec);
3066 "READY=1\nSTATUS=Startup finished in %s.",
3067 format_timespan(sum, sizeof(sum), total_usec));
3070 void manager_run_generators(Manager *m) {
3072 const char *generator_path;
3073 const char *argv[3];
3078 generator_path = m->running_as == MANAGER_SYSTEM ? SYSTEM_GENERATOR_PATH : USER_GENERATOR_PATH;
3079 if (!(d = opendir(generator_path))) {
3081 if (errno == ENOENT)
3084 log_error("Failed to enumerate generator directory: %m");
3088 if (!m->generator_unit_path) {
3090 char user_path[] = "/tmp/systemd-generator-XXXXXX";
3092 if (m->running_as == MANAGER_SYSTEM && getpid() == 1) {
3093 p = "/run/systemd/generator";
3095 if (mkdir_p(p, 0755) < 0) {
3096 log_error("Failed to create generator directory: %m");
3101 if (!(p = mkdtemp(user_path))) {
3102 log_error("Failed to create generator directory: %m");
3107 if (!(m->generator_unit_path = strdup(p))) {
3108 log_error("Failed to allocate generator unit path.");
3113 argv[0] = NULL; /* Leave this empty, execute_directory() will fill something in */
3114 argv[1] = m->generator_unit_path;
3118 execute_directory(generator_path, d, (char**) argv);
3121 if (rmdir(m->generator_unit_path) >= 0) {
3122 /* Uh? we were able to remove this dir? I guess that
3123 * means the directory was empty, hence let's shortcut
3126 free(m->generator_unit_path);
3127 m->generator_unit_path = NULL;
3131 if (!strv_find(m->lookup_paths.unit_path, m->generator_unit_path)) {
3134 if (!(l = strv_append(m->lookup_paths.unit_path, m->generator_unit_path))) {
3135 log_error("Failed to add generator directory to unit search path: %m");
3139 strv_free(m->lookup_paths.unit_path);
3140 m->lookup_paths.unit_path = l;
3142 log_debug("Added generator unit path %s to search path.", m->generator_unit_path);
3150 void manager_undo_generators(Manager *m) {
3153 if (!m->generator_unit_path)
3156 strv_remove(m->lookup_paths.unit_path, m->generator_unit_path);
3157 rm_rf(m->generator_unit_path, false, true, false);
3159 free(m->generator_unit_path);
3160 m->generator_unit_path = NULL;
3163 int manager_set_default_controllers(Manager *m, char **controllers) {
3168 if (!(l = strv_copy(controllers)))
3171 strv_free(m->default_controllers);
3172 m->default_controllers = l;
3177 void manager_recheck_journal(Manager *m) {
3182 if (m->running_as != MANAGER_SYSTEM)
3185 u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET);
3186 if (u && SOCKET(u)->state != SOCKET_RUNNING) {
3187 log_close_journal();
3191 u = manager_get_unit(m, SPECIAL_JOURNALD_SERVICE);
3192 if (u && SERVICE(u)->state != SERVICE_RUNNING) {
3193 log_close_journal();
3197 /* Hmm, OK, so the socket is fully up and the service is up
3198 * too, then let's make use of the thing. */
3202 void manager_set_show_status(Manager *m, bool b) {
3205 if (m->running_as != MANAGER_SYSTEM)
3211 touch("/run/systemd/show-status");
3213 unlink("/run/systemd/show-status");
3216 bool manager_get_show_status(Manager *m) {
3219 if (m->running_as != MANAGER_SYSTEM)
3225 /* If Plymouth is running make sure we show the status, so
3226 * that there's something nice to see when people press Esc */
3228 return plymouth_running();
3231 static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = {
3232 [MANAGER_SYSTEM] = "system",
3233 [MANAGER_USER] = "user"
3236 DEFINE_STRING_TABLE_LOOKUP(manager_running_as, ManagerRunningAs);