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 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>
31 #include <sys/reboot.h>
32 #include <sys/ioctl.h>
36 #include <sys/types.h>
44 #include <systemd/sd-daemon.h>
52 #include "ratelimit.h"
54 #include "mount-setup.h"
55 #include "unit-name.h"
56 #include "dbus-unit.h"
59 #include "path-lookup.h"
61 #include "bus-errors.h"
62 #include "exit-status.h"
65 /* As soon as 16 units are in our GC queue, make sure to run a gc sweep */
66 #define GC_QUEUE_ENTRIES_MAX 16
68 /* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */
69 #define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC)
71 /* Where clients shall send notification messages to */
72 #define NOTIFY_SOCKET_SYSTEM "/run/systemd/notify"
73 #define NOTIFY_SOCKET_USER "@/org/freedesktop/systemd1/notify"
75 static int manager_setup_notify(Manager *m) {
78 struct sockaddr_un un;
80 struct epoll_event ev;
86 m->notify_watch.type = WATCH_NOTIFY;
87 if ((m->notify_watch.fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
88 log_error("Failed to allocate notification socket: %m");
93 sa.sa.sa_family = AF_UNIX;
96 snprintf(sa.un.sun_path, sizeof(sa.un.sun_path), NOTIFY_SOCKET_USER "/%llu", random_ull());
98 unlink(NOTIFY_SOCKET_SYSTEM);
99 strncpy(sa.un.sun_path, NOTIFY_SOCKET_SYSTEM, sizeof(sa.un.sun_path));
102 if (sa.un.sun_path[0] == '@')
103 sa.un.sun_path[0] = 0;
106 r = bind(m->notify_watch.fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1));
110 log_error("bind() failed: %m");
114 if (setsockopt(m->notify_watch.fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
115 log_error("SO_PASSCRED failed: %m");
121 ev.data.ptr = &m->notify_watch;
123 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev) < 0)
126 if (sa.un.sun_path[0] == 0)
127 sa.un.sun_path[0] = '@';
129 if (!(m->notify_socket = strdup(sa.un.sun_path)))
132 log_debug("Using notification socket %s", m->notify_socket);
137 static int enable_special_signals(Manager *m) {
142 /* Enable that we get SIGINT on control-alt-del */
143 if (reboot(RB_DISABLE_CAD) < 0)
144 log_warning("Failed to enable ctrl-alt-del handling: %m");
146 if ((fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC)) < 0)
147 log_warning("Failed to open /dev/tty0: %m");
149 /* Enable that we get SIGWINCH on kbrequest */
150 if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0)
151 log_warning("Failed to enable kbrequest handling: %s", strerror(errno));
153 close_nointr_nofail(fd);
159 static int manager_setup_signals(Manager *m) {
161 struct epoll_event ev;
166 /* We are not interested in SIGSTOP and friends. */
168 sa.sa_handler = SIG_DFL;
169 sa.sa_flags = SA_NOCLDSTOP|SA_RESTART;
170 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
172 assert_se(sigemptyset(&mask) == 0);
174 sigset_add_many(&mask,
175 SIGCHLD, /* Child died */
176 SIGTERM, /* Reexecute daemon */
177 SIGHUP, /* Reload configuration */
178 SIGUSR1, /* systemd/upstart: reconnect to D-Bus */
179 SIGUSR2, /* systemd: dump status */
180 SIGINT, /* Kernel sends us this on control-alt-del */
181 SIGWINCH, /* Kernel sends us this on kbrequest (alt-arrowup) */
182 SIGPWR, /* Some kernel drivers and upsd send us this on power failure */
183 SIGRTMIN+0, /* systemd: start default.target */
184 SIGRTMIN+1, /* systemd: isolate rescue.target */
185 SIGRTMIN+2, /* systemd: isolate emergency.target */
186 SIGRTMIN+3, /* systemd: start halt.target */
187 SIGRTMIN+4, /* systemd: start poweroff.target */
188 SIGRTMIN+5, /* systemd: start reboot.target */
189 SIGRTMIN+6, /* systemd: start kexec.target */
190 SIGRTMIN+13, /* systemd: Immediate halt */
191 SIGRTMIN+14, /* systemd: Immediate poweroff */
192 SIGRTMIN+15, /* systemd: Immediate reboot */
193 SIGRTMIN+16, /* systemd: Immediate kexec */
194 SIGRTMIN+20, /* systemd: enable status messages */
195 SIGRTMIN+21, /* systemd: disable status messages */
196 SIGRTMIN+22, /* systemd: set log level to LOG_DEBUG */
197 SIGRTMIN+23, /* systemd: set log level to LOG_INFO */
198 SIGRTMIN+26, /* systemd: set log target to journal-or-kmsg */
199 SIGRTMIN+27, /* systemd: set log target to console */
200 SIGRTMIN+28, /* systemd: set log target to kmsg */
201 SIGRTMIN+29, /* systemd: set log target to syslog-or-kmsg */
203 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
205 m->signal_watch.type = WATCH_SIGNAL;
206 if ((m->signal_watch.fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0)
211 ev.data.ptr = &m->signal_watch;
213 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0)
216 if (m->running_as == MANAGER_SYSTEM)
217 return enable_special_signals(m);
222 int manager_new(ManagerRunningAs running_as, Manager **_m) {
227 assert(running_as >= 0);
228 assert(running_as < _MANAGER_RUNNING_AS_MAX);
230 if (!(m = new0(Manager, 1)))
233 dual_timestamp_get(&m->startup_timestamp);
235 m->running_as = running_as;
236 m->name_data_slot = m->conn_data_slot = m->subscribed_data_slot = -1;
237 m->exit_code = _MANAGER_EXIT_CODE_INVALID;
238 m->pin_cgroupfs_fd = -1;
244 m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = m->dev_autofs_fd = m->swap_watch.fd = -1;
245 m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
247 if (!(m->environment = strv_copy(environ)))
250 if (!(m->default_controllers = strv_new("cpu", NULL)))
253 if (!(m->units = hashmap_new(string_hash_func, string_compare_func)))
256 if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
259 if (!(m->transaction_jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
262 if (!(m->watch_pids = hashmap_new(trivial_hash_func, trivial_compare_func)))
265 if (!(m->cgroup_bondings = hashmap_new(string_hash_func, string_compare_func)))
268 if (!(m->watch_bus = hashmap_new(string_hash_func, string_compare_func)))
271 if ((m->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
274 if ((r = lookup_paths_init(&m->lookup_paths, m->running_as, true)) < 0)
277 if ((r = manager_setup_signals(m)) < 0)
280 if ((r = manager_setup_cgroup(m)) < 0)
283 if ((r = manager_setup_notify(m)) < 0)
286 /* Try to connect to the busses, if possible. */
287 if ((r = bus_init(m, running_as != MANAGER_SYSTEM)) < 0)
291 if ((m->audit_fd = audit_open()) < 0 &&
292 /* If the kernel lacks netlink or audit support,
293 * don't worry about it. */
294 errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
295 log_error("Failed to connect to audit log: %m");
298 m->taint_usr = dir_is_empty("/usr") > 0;
308 static unsigned manager_dispatch_cleanup_queue(Manager *m) {
314 while ((meta = m->cleanup_queue)) {
315 assert(meta->in_cleanup_queue);
317 unit_free((Unit*) meta);
325 GC_OFFSET_IN_PATH, /* This one is on the path we were traveling */
326 GC_OFFSET_UNSURE, /* No clue */
327 GC_OFFSET_GOOD, /* We still need this unit */
328 GC_OFFSET_BAD, /* We don't need this unit anymore */
332 static void unit_gc_sweep(Unit *u, unsigned gc_marker) {
339 if (u->meta.gc_marker == gc_marker + GC_OFFSET_GOOD ||
340 u->meta.gc_marker == gc_marker + GC_OFFSET_BAD ||
341 u->meta.gc_marker == gc_marker + GC_OFFSET_IN_PATH)
344 if (u->meta.in_cleanup_queue)
347 if (unit_check_gc(u))
350 u->meta.gc_marker = gc_marker + GC_OFFSET_IN_PATH;
354 SET_FOREACH(other, u->meta.dependencies[UNIT_REFERENCED_BY], i) {
355 unit_gc_sweep(other, gc_marker);
357 if (other->meta.gc_marker == gc_marker + GC_OFFSET_GOOD)
360 if (other->meta.gc_marker != gc_marker + GC_OFFSET_BAD)
367 /* We were unable to find anything out about this entry, so
368 * let's investigate it later */
369 u->meta.gc_marker = gc_marker + GC_OFFSET_UNSURE;
370 unit_add_to_gc_queue(u);
374 /* We definitely know that this one is not useful anymore, so
375 * let's mark it for deletion */
376 u->meta.gc_marker = gc_marker + GC_OFFSET_BAD;
377 unit_add_to_cleanup_queue(u);
381 u->meta.gc_marker = gc_marker + GC_OFFSET_GOOD;
384 static unsigned manager_dispatch_gc_queue(Manager *m) {
391 if ((m->n_in_gc_queue < GC_QUEUE_ENTRIES_MAX) &&
392 (m->gc_queue_timestamp <= 0 ||
393 (m->gc_queue_timestamp + GC_QUEUE_USEC_MAX) > now(CLOCK_MONOTONIC)))
396 log_debug("Running GC...");
398 m->gc_marker += _GC_OFFSET_MAX;
399 if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX)
402 gc_marker = m->gc_marker;
404 while ((meta = m->gc_queue)) {
405 assert(meta->in_gc_queue);
407 unit_gc_sweep((Unit*) meta, gc_marker);
409 LIST_REMOVE(Meta, gc_queue, m->gc_queue, meta);
410 meta->in_gc_queue = false;
414 if (meta->gc_marker == gc_marker + GC_OFFSET_BAD ||
415 meta->gc_marker == gc_marker + GC_OFFSET_UNSURE) {
416 log_debug("Collecting %s", meta->id);
417 meta->gc_marker = gc_marker + GC_OFFSET_BAD;
418 unit_add_to_cleanup_queue((Unit*) meta);
422 m->n_in_gc_queue = 0;
423 m->gc_queue_timestamp = 0;
428 static void manager_clear_jobs_and_units(Manager *m) {
434 while ((j = hashmap_first(m->transaction_jobs)))
437 while ((u = hashmap_first(m->units)))
440 manager_dispatch_cleanup_queue(m);
442 assert(!m->load_queue);
443 assert(!m->run_queue);
444 assert(!m->dbus_unit_queue);
445 assert(!m->dbus_job_queue);
446 assert(!m->cleanup_queue);
447 assert(!m->gc_queue);
449 assert(hashmap_isempty(m->transaction_jobs));
450 assert(hashmap_isempty(m->jobs));
451 assert(hashmap_isempty(m->units));
454 void manager_free(Manager *m) {
459 manager_clear_jobs_and_units(m);
461 for (c = 0; c < _UNIT_TYPE_MAX; c++)
462 if (unit_vtable[c]->shutdown)
463 unit_vtable[c]->shutdown(m);
465 /* If we reexecute ourselves, we keep the root cgroup
467 manager_shutdown_cgroup(m, m->exit_code != MANAGER_REEXECUTE);
469 manager_undo_generators(m);
473 hashmap_free(m->units);
474 hashmap_free(m->jobs);
475 hashmap_free(m->transaction_jobs);
476 hashmap_free(m->watch_pids);
477 hashmap_free(m->watch_bus);
479 if (m->epoll_fd >= 0)
480 close_nointr_nofail(m->epoll_fd);
481 if (m->signal_watch.fd >= 0)
482 close_nointr_nofail(m->signal_watch.fd);
483 if (m->notify_watch.fd >= 0)
484 close_nointr_nofail(m->notify_watch.fd);
487 if (m->audit_fd >= 0)
488 audit_close(m->audit_fd);
491 free(m->notify_socket);
493 lookup_paths_free(&m->lookup_paths);
494 strv_free(m->environment);
496 strv_free(m->default_controllers);
498 hashmap_free(m->cgroup_bondings);
499 set_free_free(m->unit_path_cache);
504 int manager_enumerate(Manager *m) {
510 /* Let's ask every type to load all units from disk/kernel
511 * that it might know */
512 for (c = 0; c < _UNIT_TYPE_MAX; c++)
513 if (unit_vtable[c]->enumerate)
514 if ((q = unit_vtable[c]->enumerate(m)) < 0)
517 manager_dispatch_load_queue(m);
521 int manager_coldplug(Manager *m) {
529 /* Then, let's set up their initial state. */
530 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
536 if ((q = unit_coldplug(u)) < 0)
543 static void manager_build_unit_path_cache(Manager *m) {
550 set_free_free(m->unit_path_cache);
552 if (!(m->unit_path_cache = set_new(string_hash_func, string_compare_func))) {
553 log_error("Failed to allocate unit path cache.");
557 /* This simply builds a list of files we know exist, so that
558 * we don't always have to go to disk */
560 STRV_FOREACH(i, m->lookup_paths.unit_path) {
563 if (!(d = opendir(*i))) {
564 log_error("Failed to open directory: %m");
568 while ((de = readdir(d))) {
571 if (ignore_file(de->d_name))
574 p = join(streq(*i, "/") ? "" : *i, "/", de->d_name, NULL);
580 if ((r = set_put(m->unit_path_cache, p)) < 0) {
593 log_error("Failed to build unit path cache: %s", strerror(-r));
595 set_free_free(m->unit_path_cache);
596 m->unit_path_cache = NULL;
602 int manager_startup(Manager *m, FILE *serialization, FDSet *fds) {
607 manager_run_generators(m);
609 manager_build_unit_path_cache(m);
611 /* If we will deserialize make sure that during enumeration
612 * this is already known, so we increase the counter here
617 /* First, enumerate what we can from all config files */
618 r = manager_enumerate(m);
620 /* Second, deserialize if there is something to deserialize */
622 if ((q = manager_deserialize(m, serialization, fds)) < 0)
625 /* Third, fire things up! */
626 if ((q = manager_coldplug(m)) < 0)
630 assert(m->n_reloading > 0);
637 static void transaction_delete_job(Manager *m, Job *j, bool delete_dependencies) {
641 /* Deletes one job from the transaction */
643 manager_transaction_unlink_job(m, j, delete_dependencies);
649 static void transaction_delete_unit(Manager *m, Unit *u) {
652 /* Deletes all jobs associated with a certain unit from the
655 while ((j = hashmap_get(m->transaction_jobs, u)))
656 transaction_delete_job(m, j, true);
659 static void transaction_clean_dependencies(Manager *m) {
665 /* Drops all dependencies of all installed jobs */
667 HASHMAP_FOREACH(j, m->jobs, i) {
668 while (j->subject_list)
669 job_dependency_free(j->subject_list);
670 while (j->object_list)
671 job_dependency_free(j->object_list);
674 assert(!m->transaction_anchor);
677 static void transaction_abort(Manager *m) {
682 while ((j = hashmap_first(m->transaction_jobs)))
684 transaction_delete_job(m, j, true);
688 assert(hashmap_isempty(m->transaction_jobs));
690 transaction_clean_dependencies(m);
693 static void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) {
698 /* A recursive sweep through the graph that marks all units
699 * that matter to the anchor job, i.e. are directly or
700 * indirectly a dependency of the anchor job via paths that
701 * are fully marked as mattering. */
706 l = m->transaction_anchor;
708 LIST_FOREACH(subject, l, l) {
710 /* This link does not matter */
714 /* This unit has already been marked */
715 if (l->object->generation == generation)
718 l->object->matters_to_anchor = true;
719 l->object->generation = generation;
721 transaction_find_jobs_that_matter_to_anchor(m, l->object, generation);
725 static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) {
726 JobDependency *l, *last;
730 assert(j->unit == other->unit);
731 assert(!j->installed);
733 /* Merges 'other' into 'j' and then deletes j. */
736 j->state = JOB_WAITING;
737 j->override = j->override || other->override;
739 j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
741 /* Patch us in as new owner of the JobDependency objects */
743 LIST_FOREACH(subject, l, other->subject_list) {
744 assert(l->subject == other);
749 /* Merge both lists */
751 last->subject_next = j->subject_list;
753 j->subject_list->subject_prev = last;
754 j->subject_list = other->subject_list;
757 /* Patch us in as new owner of the JobDependency objects */
759 LIST_FOREACH(object, l, other->object_list) {
760 assert(l->object == other);
765 /* Merge both lists */
767 last->object_next = j->object_list;
769 j->object_list->object_prev = last;
770 j->object_list = other->object_list;
773 /* Kill the other job */
774 other->subject_list = NULL;
775 other->object_list = NULL;
776 transaction_delete_job(m, other, true);
778 static bool job_is_conflicted_by(Job *j) {
783 /* Returns true if this job is pulled in by a least one
784 * ConflictedBy dependency. */
786 LIST_FOREACH(object, l, j->object_list)
793 static int delete_one_unmergeable_job(Manager *m, Job *j) {
798 /* Tries to delete one item in the linked list
799 * j->transaction_next->transaction_next->... that conflicts
800 * with another one, in an attempt to make an inconsistent
801 * transaction work. */
803 /* We rely here on the fact that if a merged with b does not
804 * merge with c, either a or b merge with c neither */
805 LIST_FOREACH(transaction, j, j)
806 LIST_FOREACH(transaction, k, j->transaction_next) {
809 /* Is this one mergeable? Then skip it */
810 if (job_type_is_mergeable(j->type, k->type))
813 /* Ok, we found two that conflict, let's see if we can
814 * drop one of them */
815 if (!j->matters_to_anchor && !k->matters_to_anchor) {
817 /* Both jobs don't matter, so let's
818 * find the one that is smarter to
819 * remove. Let's think positive and
820 * rather remove stops then starts --
821 * except if something is being
822 * stopped because it is conflicted by
823 * another unit in which case we
824 * rather remove the start. */
826 log_debug("Looking at job %s/%s conflicted_by=%s", j->unit->meta.id, job_type_to_string(j->type), yes_no(j->type == JOB_STOP && job_is_conflicted_by(j)));
827 log_debug("Looking at job %s/%s conflicted_by=%s", k->unit->meta.id, job_type_to_string(k->type), yes_no(k->type == JOB_STOP && job_is_conflicted_by(k)));
829 if (j->type == JOB_STOP) {
831 if (job_is_conflicted_by(j))
836 } else if (k->type == JOB_STOP) {
838 if (job_is_conflicted_by(k))
845 } else if (!j->matters_to_anchor)
847 else if (!k->matters_to_anchor)
852 /* Ok, we can drop one, so let's do so. */
853 log_debug("Fixing conflicting jobs by deleting job %s/%s", d->unit->meta.id, job_type_to_string(d->type));
854 transaction_delete_job(m, d, true);
861 static int transaction_merge_jobs(Manager *m, DBusError *e) {
868 /* First step, check whether any of the jobs for one specific
869 * task conflict. If so, try to drop one of them. */
870 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
875 LIST_FOREACH(transaction, k, j->transaction_next) {
876 if (job_type_merge(&t, k->type) >= 0)
879 /* OK, we could not merge all jobs for this
880 * action. Let's see if we can get rid of one
883 if ((r = delete_one_unmergeable_job(m, j)) >= 0)
884 /* Ok, we managed to drop one, now
885 * let's ask our callers to call us
886 * again after garbage collecting */
889 /* We couldn't merge anything. Failure */
890 dbus_set_error(e, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING, "Transaction contains conflicting jobs '%s' and '%s' for %s. Probably contradicting requirement dependencies configured.",
891 job_type_to_string(t), job_type_to_string(k->type), k->unit->meta.id);
896 /* Second step, merge the jobs. */
897 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
901 /* Merge all transactions */
902 LIST_FOREACH(transaction, k, j->transaction_next)
903 assert_se(job_type_merge(&t, k->type) == 0);
905 /* If an active job is mergeable, merge it too */
906 if (j->unit->meta.job)
907 job_type_merge(&t, j->unit->meta.job->type); /* Might fail. Which is OK */
909 while ((k = j->transaction_next)) {
911 transaction_merge_and_delete_job(m, k, j, t);
914 transaction_merge_and_delete_job(m, j, k, t);
917 if (j->unit->meta.job && !j->installed)
918 transaction_merge_and_delete_job(m, j, j->unit->meta.job, t);
920 assert(!j->transaction_next);
921 assert(!j->transaction_prev);
927 static void transaction_drop_redundant(Manager *m) {
932 /* Goes through the transaction and removes all jobs that are
941 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
942 bool changes_something = false;
945 LIST_FOREACH(transaction, k, j) {
947 if (!job_is_anchor(k) &&
948 (k->installed || job_type_is_redundant(k->type, unit_active_state(k->unit))) &&
949 (!k->unit->meta.job || !job_type_is_conflicting(k->type, k->unit->meta.job->type)))
952 changes_something = true;
956 if (changes_something)
959 /* log_debug("Found redundant job %s/%s, dropping.", j->unit->meta.id, job_type_to_string(j->type)); */
960 transaction_delete_job(m, j, false);
968 static bool unit_matters_to_anchor(Unit *u, Job *j) {
970 assert(!j->transaction_prev);
972 /* Checks whether at least one of the jobs for this unit
973 * matters to the anchor. */
975 LIST_FOREACH(transaction, j, j)
976 if (j->matters_to_anchor)
982 static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation, DBusError *e) {
989 assert(!j->transaction_prev);
991 /* Does a recursive sweep through the ordering graph, looking
992 * for a cycle. If we find cycle we try to break it. */
994 /* Have we seen this before? */
995 if (j->generation == generation) {
998 /* If the marker is NULL we have been here already and
999 * decided the job was loop-free from here. Hence
1000 * shortcut things and return right-away. */
1004 /* So, the marker is not NULL and we already have been
1005 * here. We have a cycle. Let's try to break it. We go
1006 * backwards in our path and try to find a suitable
1007 * job to remove. We use the marker to find our way
1008 * back, since smart how we are we stored our way back
1010 log_warning("Found ordering cycle on %s/%s", j->unit->meta.id, job_type_to_string(j->type));
1013 for (k = from; k; k = ((k->generation == generation && k->marker != k) ? k->marker : NULL)) {
1015 log_info("Walked on cycle path to %s/%s", k->unit->meta.id, job_type_to_string(k->type));
1019 !unit_matters_to_anchor(k->unit, k)) {
1020 /* Ok, we can drop this one, so let's
1025 /* Check if this in fact was the beginning of
1033 log_warning("Breaking ordering cycle by deleting job %s/%s", delete->unit->meta.id, job_type_to_string(delete->type));
1034 transaction_delete_unit(m, delete->unit);
1038 log_error("Unable to break cycle");
1040 dbus_set_error(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC, "Transaction order is cyclic. See system logs for details.");
1044 /* Make the marker point to where we come from, so that we can
1045 * find our way backwards if we want to break a cycle. We use
1046 * a special marker for the beginning: we point to
1048 j->marker = from ? from : j;
1049 j->generation = generation;
1051 /* We assume that the the dependencies are bidirectional, and
1052 * hence can ignore UNIT_AFTER */
1053 SET_FOREACH(u, j->unit->meta.dependencies[UNIT_BEFORE], i) {
1056 /* Is there a job for this unit? */
1057 if (!(o = hashmap_get(m->transaction_jobs, u)))
1059 /* Ok, there is no job for this in the
1060 * transaction, but maybe there is already one
1062 if (!(o = u->meta.job))
1065 if ((r = transaction_verify_order_one(m, o, j, generation, e)) < 0)
1069 /* Ok, let's backtrack, and remember that this entry is not on
1070 * our path anymore. */
1076 static int transaction_verify_order(Manager *m, unsigned *generation, DBusError *e) {
1085 /* Check if the ordering graph is cyclic. If it is, try to fix
1086 * that up by dropping one of the jobs. */
1088 g = (*generation)++;
1090 HASHMAP_FOREACH(j, m->transaction_jobs, i)
1091 if ((r = transaction_verify_order_one(m, j, NULL, g, e)) < 0)
1097 static void transaction_collect_garbage(Manager *m) {
1102 /* Drop jobs that are not required by any other job */
1110 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1111 if (j->object_list) {
1112 /* log_debug("Keeping job %s/%s because of %s/%s", */
1113 /* j->unit->meta.id, job_type_to_string(j->type), */
1114 /* j->object_list->subject ? j->object_list->subject->unit->meta.id : "root", */
1115 /* j->object_list->subject ? job_type_to_string(j->object_list->subject->type) : "root"); */
1119 /* log_debug("Garbage collecting job %s/%s", j->unit->meta.id, job_type_to_string(j->type)); */
1120 transaction_delete_job(m, j, true);
1128 static int transaction_is_destructive(Manager *m, DBusError *e) {
1134 /* Checks whether applying this transaction means that
1135 * existing jobs would be replaced */
1137 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1140 assert(!j->transaction_prev);
1141 assert(!j->transaction_next);
1143 if (j->unit->meta.job &&
1144 j->unit->meta.job != j &&
1145 !job_type_is_superset(j->type, j->unit->meta.job->type)) {
1147 dbus_set_error(e, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE, "Transaction is destructive.");
1155 static void transaction_minimize_impact(Manager *m) {
1159 /* Drops all unnecessary jobs that reverse already active jobs
1160 * or that stop a running service. */
1168 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1169 LIST_FOREACH(transaction, j, j) {
1170 bool stops_running_service, changes_existing_job;
1172 /* If it matters, we shouldn't drop it */
1173 if (j->matters_to_anchor)
1176 /* Would this stop a running service?
1177 * Would this change an existing job?
1178 * If so, let's drop this entry */
1180 stops_running_service =
1181 j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
1183 changes_existing_job =
1184 j->unit->meta.job &&
1185 job_type_is_conflicting(j->type, j->unit->meta.job->type);
1187 if (!stops_running_service && !changes_existing_job)
1190 if (stops_running_service)
1191 log_debug("%s/%s would stop a running service.", j->unit->meta.id, job_type_to_string(j->type));
1193 if (changes_existing_job)
1194 log_debug("%s/%s would change existing job.", j->unit->meta.id, job_type_to_string(j->type));
1196 /* Ok, let's get rid of this */
1197 log_debug("Deleting %s/%s to minimize impact.", j->unit->meta.id, job_type_to_string(j->type));
1199 transaction_delete_job(m, j, true);
1211 static int transaction_apply(Manager *m, JobMode mode) {
1216 /* Moves the transaction jobs to the set of active jobs */
1218 if (mode == JOB_ISOLATE) {
1220 /* When isolating first kill all installed jobs which
1221 * aren't part of the new transaction */
1223 HASHMAP_FOREACH(j, m->jobs, i) {
1224 assert(j->installed);
1226 if (hashmap_get(m->transaction_jobs, j->unit))
1229 /* 'j' itself is safe to remove, but if other jobs
1230 are invalidated recursively, our iterator may become
1231 invalid and we need to start over. */
1232 if (job_finish_and_invalidate(j, JOB_CANCELED) > 0)
1237 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1239 assert(!j->transaction_prev);
1240 assert(!j->transaction_next);
1245 if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
1249 while ((j = hashmap_steal_first(m->transaction_jobs))) {
1251 /* log_debug("Skipping already installed job %s/%s as %u", j->unit->meta.id, job_type_to_string(j->type), (unsigned) j->id); */
1255 if (j->unit->meta.job)
1256 job_free(j->unit->meta.job);
1258 j->unit->meta.job = j;
1259 j->installed = true;
1260 m->n_installed_jobs ++;
1262 /* We're fully installed. Now let's free data we don't
1265 assert(!j->transaction_next);
1266 assert(!j->transaction_prev);
1268 job_add_to_run_queue(j);
1269 job_add_to_dbus_queue(j);
1272 log_debug("Installed new job %s/%s as %u", j->unit->meta.id, job_type_to_string(j->type), (unsigned) j->id);
1275 /* As last step, kill all remaining job dependencies. */
1276 transaction_clean_dependencies(m);
1282 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1286 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
1292 static int transaction_activate(Manager *m, JobMode mode, DBusError *e) {
1294 unsigned generation = 1;
1298 /* This applies the changes recorded in transaction_jobs to
1299 * the actual list of jobs, if possible. */
1301 /* First step: figure out which jobs matter */
1302 transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
1304 /* Second step: Try not to stop any running services if
1305 * we don't have to. Don't try to reverse running
1306 * jobs if we don't have to. */
1307 if (mode == JOB_FAIL)
1308 transaction_minimize_impact(m);
1310 /* Third step: Drop redundant jobs */
1311 transaction_drop_redundant(m);
1314 /* Fourth step: Let's remove unneeded jobs that might
1316 if (mode != JOB_ISOLATE)
1317 transaction_collect_garbage(m);
1319 /* Fifth step: verify order makes sense and correct
1320 * cycles if necessary and possible */
1321 if ((r = transaction_verify_order(m, &generation, e)) >= 0)
1325 log_warning("Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error(e, r));
1329 /* Let's see if the resulting transaction ordering
1330 * graph is still cyclic... */
1334 /* Sixth step: let's drop unmergeable entries if
1335 * necessary and possible, merge entries we can
1337 if ((r = transaction_merge_jobs(m, e)) >= 0)
1341 log_warning("Requested transaction contains unmergeable jobs: %s", bus_error(e, r));
1345 /* Seventh step: an entry got dropped, let's garbage
1346 * collect its dependencies. */
1347 if (mode != JOB_ISOLATE)
1348 transaction_collect_garbage(m);
1350 /* Let's see if the resulting transaction still has
1351 * unmergeable entries ... */
1354 /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
1355 transaction_drop_redundant(m);
1357 /* Ninth step: check whether we can actually apply this */
1358 if (mode == JOB_FAIL)
1359 if ((r = transaction_is_destructive(m, e)) < 0) {
1360 log_notice("Requested transaction contradicts existing jobs: %s", bus_error(e, r));
1364 /* Tenth step: apply changes */
1365 if ((r = transaction_apply(m, mode)) < 0) {
1366 log_warning("Failed to apply transaction: %s", strerror(-r));
1370 assert(hashmap_isempty(m->transaction_jobs));
1371 assert(!m->transaction_anchor);
1376 transaction_abort(m);
1380 static Job* transaction_add_one_job(Manager *m, JobType type, Unit *unit, bool override, bool *is_new) {
1386 /* Looks for an existing prospective job and returns that. If
1387 * it doesn't exist it is created and added to the prospective
1390 f = hashmap_get(m->transaction_jobs, unit);
1392 LIST_FOREACH(transaction, j, f) {
1393 assert(j->unit == unit);
1395 if (j->type == type) {
1402 if (unit->meta.job && unit->meta.job->type == type)
1404 else if (!(j = job_new(m, type, unit)))
1409 j->matters_to_anchor = false;
1410 j->override = override;
1412 LIST_PREPEND(Job, transaction, f, j);
1414 if (hashmap_replace(m->transaction_jobs, unit, f) < 0) {
1422 /* log_debug("Added job %s/%s to transaction.", unit->meta.id, job_type_to_string(type)); */
1427 void manager_transaction_unlink_job(Manager *m, Job *j, bool delete_dependencies) {
1431 if (j->transaction_prev)
1432 j->transaction_prev->transaction_next = j->transaction_next;
1433 else if (j->transaction_next)
1434 hashmap_replace(m->transaction_jobs, j->unit, j->transaction_next);
1436 hashmap_remove_value(m->transaction_jobs, j->unit, j);
1438 if (j->transaction_next)
1439 j->transaction_next->transaction_prev = j->transaction_prev;
1441 j->transaction_prev = j->transaction_next = NULL;
1443 while (j->subject_list)
1444 job_dependency_free(j->subject_list);
1446 while (j->object_list) {
1447 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
1449 job_dependency_free(j->object_list);
1451 if (other && delete_dependencies) {
1452 log_debug("Deleting job %s/%s as dependency of job %s/%s",
1453 other->unit->meta.id, job_type_to_string(other->type),
1454 j->unit->meta.id, job_type_to_string(j->type));
1455 transaction_delete_job(m, other, delete_dependencies);
1460 static int transaction_add_job_and_dependencies(
1468 bool ignore_requirements,
1479 assert(type < _JOB_TYPE_MAX);
1482 /* log_debug("Pulling in %s/%s from %s/%s", */
1483 /* unit->meta.id, job_type_to_string(type), */
1484 /* by ? by->unit->meta.id : "NA", */
1485 /* by ? job_type_to_string(by->type) : "NA"); */
1487 if (unit->meta.load_state != UNIT_LOADED &&
1488 unit->meta.load_state != UNIT_ERROR &&
1489 unit->meta.load_state != UNIT_MASKED) {
1490 dbus_set_error(e, BUS_ERROR_LOAD_FAILED, "Unit %s is not loaded properly.", unit->meta.id);
1494 if (type != JOB_STOP && unit->meta.load_state == UNIT_ERROR) {
1495 dbus_set_error(e, BUS_ERROR_LOAD_FAILED,
1496 "Unit %s failed to load: %s. "
1497 "See system logs and 'systemctl status %s' for details.",
1499 strerror(-unit->meta.load_error),
1504 if (type != JOB_STOP && unit->meta.load_state == UNIT_MASKED) {
1505 dbus_set_error(e, BUS_ERROR_MASKED, "Unit %s is masked.", unit->meta.id);
1509 if (!unit_job_is_applicable(unit, type)) {
1510 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->meta.id);
1514 /* First add the job. */
1515 if (!(ret = transaction_add_one_job(m, type, unit, override, &is_new)))
1518 ret->ignore_order = ret->ignore_order || ignore_order;
1520 /* Then, add a link to the job. */
1521 if (!job_dependency_new(by, ret, matters, conflicts))
1524 if (is_new && !ignore_requirements) {
1527 /* If we are following some other unit, make sure we
1528 * add all dependencies of everybody following. */
1529 if (unit_following_set(ret->unit, &following) > 0) {
1530 SET_FOREACH(dep, following, i)
1531 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, false, override, false, false, ignore_order, e, NULL)) < 0) {
1532 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1538 set_free(following);
1541 /* Finally, recursively add in all dependencies. */
1542 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
1543 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES], i)
1544 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1552 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_BIND_TO], i)
1553 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1562 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
1563 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !override, override, false, false, ignore_order, e, NULL)) < 0) {
1564 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1570 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_WANTS], i)
1571 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, false, false, false, ignore_order, e, NULL)) < 0) {
1572 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1578 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE], i)
1579 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1588 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i)
1589 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, !override, override, false, false, ignore_order, e, NULL)) < 0) {
1590 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1596 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTS], i)
1597 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, override, true, false, ignore_order, e, NULL)) < 0) {
1606 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTED_BY], i)
1607 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, false, override, false, false, ignore_order, e, NULL)) < 0) {
1608 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1616 if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
1618 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRED_BY], i)
1619 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1628 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_BOUND_BY], i)
1629 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1639 if (type == JOB_RELOAD || type == JOB_RELOAD_OR_START) {
1641 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_PROPAGATE_RELOAD_TO], i) {
1642 r = transaction_add_job_and_dependencies(m, JOB_RELOAD, dep, ret, false, override, false, false, ignore_order, e, NULL);
1645 log_warning("Cannot add dependency reload job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1653 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
1665 static int transaction_add_isolate_jobs(Manager *m) {
1673 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
1675 /* ignore aliases */
1676 if (u->meta.id != k)
1679 if (u->meta.ignore_on_isolate)
1682 /* No need to stop inactive jobs */
1683 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) && !u->meta.job)
1686 /* Is there already something listed for this? */
1687 if (hashmap_get(m->transaction_jobs, u))
1690 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, u, NULL, true, false, false, false, false, NULL, NULL)) < 0)
1691 log_warning("Cannot add isolate job for unit %s, ignoring: %s", u->meta.id, strerror(-r));
1697 int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool override, DBusError *e, Job **_ret) {
1702 assert(type < _JOB_TYPE_MAX);
1704 assert(mode < _JOB_MODE_MAX);
1706 if (mode == JOB_ISOLATE && type != JOB_START) {
1707 dbus_set_error(e, BUS_ERROR_INVALID_JOB_MODE, "Isolate is only valid for start.");
1711 if (mode == JOB_ISOLATE && !unit->meta.allow_isolate) {
1712 dbus_set_error(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated.");
1716 log_debug("Trying to enqueue job %s/%s/%s", unit->meta.id, job_type_to_string(type), job_mode_to_string(mode));
1718 if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, override, false,
1719 mode == JOB_IGNORE_DEPENDENCIES || mode == JOB_IGNORE_REQUIREMENTS,
1720 mode == JOB_IGNORE_DEPENDENCIES, e, &ret)) < 0) {
1721 transaction_abort(m);
1725 if (mode == JOB_ISOLATE)
1726 if ((r = transaction_add_isolate_jobs(m)) < 0) {
1727 transaction_abort(m);
1731 if ((r = transaction_activate(m, mode, e)) < 0)
1734 log_debug("Enqueued job %s/%s as %u", unit->meta.id, job_type_to_string(type), (unsigned) ret->id);
1742 int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, bool override, DBusError *e, Job **_ret) {
1747 assert(type < _JOB_TYPE_MAX);
1749 assert(mode < _JOB_MODE_MAX);
1751 if ((r = manager_load_unit(m, name, NULL, NULL, &unit)) < 0)
1754 return manager_add_job(m, type, unit, mode, override, e, _ret);
1757 Job *manager_get_job(Manager *m, uint32_t id) {
1760 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
1763 Unit *manager_get_unit(Manager *m, const char *name) {
1767 return hashmap_get(m->units, name);
1770 unsigned manager_dispatch_load_queue(Manager *m) {
1776 /* Make sure we are not run recursively */
1777 if (m->dispatching_load_queue)
1780 m->dispatching_load_queue = true;
1782 /* Dispatches the load queue. Takes a unit from the queue and
1783 * tries to load its data until the queue is empty */
1785 while ((meta = m->load_queue)) {
1786 assert(meta->in_load_queue);
1788 unit_load((Unit*) meta);
1792 m->dispatching_load_queue = false;
1796 int manager_load_unit_prepare(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
1801 assert(name || path);
1803 /* This will prepare the unit for loading, but not actually
1804 * load anything from disk. */
1806 if (path && !is_path(path)) {
1807 dbus_set_error(e, BUS_ERROR_INVALID_PATH, "Path %s is not absolute.", path);
1812 name = file_name_from_path(path);
1814 if (!unit_name_is_valid(name, false)) {
1815 dbus_set_error(e, BUS_ERROR_INVALID_NAME, "Unit name %s is not valid.", name);
1819 if ((ret = manager_get_unit(m, name))) {
1824 if (!(ret = unit_new(m)))
1828 if (!(ret->meta.fragment_path = strdup(path))) {
1833 if ((r = unit_add_name(ret, name)) < 0) {
1838 unit_add_to_load_queue(ret);
1839 unit_add_to_dbus_queue(ret);
1840 unit_add_to_gc_queue(ret);
1848 int manager_load_unit(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
1853 /* This will load the service information files, but not actually
1854 * start any services or anything. */
1856 if ((r = manager_load_unit_prepare(m, name, path, e, _ret)) != 0)
1859 manager_dispatch_load_queue(m);
1862 *_ret = unit_follow_merge(*_ret);
1867 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
1874 HASHMAP_FOREACH(j, s->jobs, i)
1875 job_dump(j, f, prefix);
1878 void manager_dump_units(Manager *s, FILE *f, const char *prefix) {
1886 HASHMAP_FOREACH_KEY(u, t, s->units, i)
1887 if (u->meta.id == t)
1888 unit_dump(u, f, prefix);
1891 void manager_clear_jobs(Manager *m) {
1896 transaction_abort(m);
1898 while ((j = hashmap_first(m->jobs)))
1899 job_finish_and_invalidate(j, JOB_CANCELED);
1902 unsigned manager_dispatch_run_queue(Manager *m) {
1906 if (m->dispatching_run_queue)
1909 m->dispatching_run_queue = true;
1911 while ((j = m->run_queue)) {
1912 assert(j->installed);
1913 assert(j->in_run_queue);
1915 job_run_and_invalidate(j);
1919 m->dispatching_run_queue = false;
1923 unsigned manager_dispatch_dbus_queue(Manager *m) {
1930 if (m->dispatching_dbus_queue)
1933 m->dispatching_dbus_queue = true;
1935 while ((meta = m->dbus_unit_queue)) {
1936 assert(meta->in_dbus_queue);
1938 bus_unit_send_change_signal((Unit*) meta);
1942 while ((j = m->dbus_job_queue)) {
1943 assert(j->in_dbus_queue);
1945 bus_job_send_change_signal(j);
1949 m->dispatching_dbus_queue = false;
1953 static int manager_process_notify_fd(Manager *m) {
1960 struct msghdr msghdr;
1962 struct ucred *ucred;
1964 struct cmsghdr cmsghdr;
1965 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
1971 iovec.iov_base = buf;
1972 iovec.iov_len = sizeof(buf)-1;
1976 msghdr.msg_iov = &iovec;
1977 msghdr.msg_iovlen = 1;
1978 msghdr.msg_control = &control;
1979 msghdr.msg_controllen = sizeof(control);
1981 if ((n = recvmsg(m->notify_watch.fd, &msghdr, MSG_DONTWAIT)) <= 0) {
1985 if (errno == EAGAIN || errno == EINTR)
1991 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
1992 control.cmsghdr.cmsg_level != SOL_SOCKET ||
1993 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
1994 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
1995 log_warning("Received notify message without credentials. Ignoring.");
1999 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
2001 if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(ucred->pid))))
2002 if (!(u = cgroup_unit_by_pid(m, ucred->pid))) {
2003 log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid);
2007 assert((size_t) n < sizeof(buf));
2009 if (!(tags = strv_split(buf, "\n\r")))
2012 log_debug("Got notification message for unit %s", u->meta.id);
2014 if (UNIT_VTABLE(u)->notify_message)
2015 UNIT_VTABLE(u)->notify_message(u, ucred->pid, tags);
2023 static int manager_dispatch_sigchld(Manager *m) {
2033 /* First we call waitd() for a PID and do not reap the
2034 * zombie. That way we can still access /proc/$PID for
2035 * it while it is a zombie. */
2036 if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) {
2038 if (errno == ECHILD)
2050 if (si.si_code == CLD_EXITED || si.si_code == CLD_KILLED || si.si_code == CLD_DUMPED) {
2053 get_process_comm(si.si_pid, &name);
2054 log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si.si_pid, strna(name));
2058 /* Let's flush any message the dying child might still
2059 * have queued for us. This ensures that the process
2060 * still exists in /proc so that we can figure out
2061 * which cgroup and hence unit it belongs to. */
2062 if ((r = manager_process_notify_fd(m)) < 0)
2065 /* And now figure out the unit this belongs to */
2066 if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(si.si_pid))))
2067 u = cgroup_unit_by_pid(m, si.si_pid);
2069 /* And now, we actually reap the zombie. */
2070 if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) {
2077 if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED)
2080 log_debug("Child %lu died (code=%s, status=%i/%s)",
2081 (long unsigned) si.si_pid,
2082 sigchld_code_to_string(si.si_code),
2084 strna(si.si_code == CLD_EXITED
2085 ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL)
2086 : signal_to_string(si.si_status)));
2091 log_debug("Child %lu belongs to %s", (long unsigned) si.si_pid, u->meta.id);
2093 hashmap_remove(m->watch_pids, LONG_TO_PTR(si.si_pid));
2094 UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status);
2100 static int manager_start_target(Manager *m, const char *name, JobMode mode) {
2104 dbus_error_init(&error);
2106 log_debug("Activating special unit %s", name);
2108 if ((r = manager_add_job_by_name(m, JOB_START, name, mode, true, &error, NULL)) < 0)
2109 log_error("Failed to enqueue %s job: %s", name, bus_error(&error, r));
2111 dbus_error_free(&error);
2116 static int manager_process_signal_fd(Manager *m) {
2118 struct signalfd_siginfo sfsi;
2119 bool sigchld = false;
2124 if ((n = read(m->signal_watch.fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
2129 if (errno == EINTR || errno == EAGAIN)
2135 if (sfsi.ssi_pid > 0) {
2138 get_process_comm(sfsi.ssi_pid, &p);
2140 log_debug("Received SIG%s from PID %lu (%s).",
2141 signal_to_string(sfsi.ssi_signo),
2142 (unsigned long) sfsi.ssi_pid, strna(p));
2145 log_debug("Received SIG%s.", signal_to_string(sfsi.ssi_signo));
2147 switch (sfsi.ssi_signo) {
2154 if (m->running_as == MANAGER_SYSTEM) {
2155 /* This is for compatibility with the
2156 * original sysvinit */
2157 m->exit_code = MANAGER_REEXECUTE;
2164 if (m->running_as == MANAGER_SYSTEM) {
2165 manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE);
2169 /* Run the exit target if there is one, if not, just exit. */
2170 if (manager_start_target(m, SPECIAL_EXIT_TARGET, JOB_REPLACE) < 0) {
2171 m->exit_code = MANAGER_EXIT;
2178 if (m->running_as == MANAGER_SYSTEM)
2179 manager_start_target(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE);
2181 /* This is a nop on non-init */
2185 if (m->running_as == MANAGER_SYSTEM)
2186 manager_start_target(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE);
2188 /* This is a nop on non-init */
2194 u = manager_get_unit(m, SPECIAL_DBUS_SERVICE);
2196 if (!u || UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) {
2197 log_info("Trying to reconnect to bus...");
2201 if (!u || !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) {
2202 log_info("Loading D-Bus service...");
2203 manager_start_target(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE);
2214 if (!(f = open_memstream(&dump, &size))) {
2215 log_warning("Failed to allocate memory stream.");
2219 manager_dump_units(m, f, "\t");
2220 manager_dump_jobs(m, f, "\t");
2225 log_warning("Failed to write status stream");
2230 log_dump(LOG_INFO, dump);
2237 m->exit_code = MANAGER_RELOAD;
2242 /* Starting SIGRTMIN+0 */
2243 static const char * const target_table[] = {
2244 [0] = SPECIAL_DEFAULT_TARGET,
2245 [1] = SPECIAL_RESCUE_TARGET,
2246 [2] = SPECIAL_EMERGENCY_TARGET,
2247 [3] = SPECIAL_HALT_TARGET,
2248 [4] = SPECIAL_POWEROFF_TARGET,
2249 [5] = SPECIAL_REBOOT_TARGET,
2250 [6] = SPECIAL_KEXEC_TARGET
2253 /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */
2254 static const ManagerExitCode code_table[] = {
2256 [1] = MANAGER_POWEROFF,
2257 [2] = MANAGER_REBOOT,
2261 if ((int) sfsi.ssi_signo >= SIGRTMIN+0 &&
2262 (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) {
2263 int idx = (int) sfsi.ssi_signo - SIGRTMIN;
2264 manager_start_target(m, target_table[idx],
2265 (idx == 1 || idx == 2) ? JOB_ISOLATE : JOB_REPLACE);
2269 if ((int) sfsi.ssi_signo >= SIGRTMIN+13 &&
2270 (int) sfsi.ssi_signo < SIGRTMIN+13+(int) ELEMENTSOF(code_table)) {
2271 m->exit_code = code_table[sfsi.ssi_signo - SIGRTMIN - 13];
2275 switch (sfsi.ssi_signo - SIGRTMIN) {
2278 log_debug("Enabling showing of status.");
2279 manager_set_show_status(m, true);
2283 log_debug("Disabling showing of status.");
2284 manager_set_show_status(m, false);
2288 log_set_max_level(LOG_DEBUG);
2289 log_notice("Setting log level to debug.");
2293 log_set_max_level(LOG_INFO);
2294 log_notice("Setting log level to info.");
2298 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
2299 log_notice("Setting log target to journal-or-kmsg.");
2303 log_set_target(LOG_TARGET_CONSOLE);
2304 log_notice("Setting log target to console.");
2308 log_set_target(LOG_TARGET_KMSG);
2309 log_notice("Setting log target to kmsg.");
2313 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
2314 log_notice("Setting log target to syslog-or-kmsg.");
2318 log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi.ssi_signo));
2325 return manager_dispatch_sigchld(m);
2330 static int process_event(Manager *m, struct epoll_event *ev) {
2337 assert_se(w = ev->data.ptr);
2339 if (w->type == WATCH_INVALID)
2346 /* An incoming signal? */
2347 if (ev->events != EPOLLIN)
2350 if ((r = manager_process_signal_fd(m)) < 0)
2357 /* An incoming daemon notification event? */
2358 if (ev->events != EPOLLIN)
2361 if ((r = manager_process_notify_fd(m)) < 0)
2368 /* Some fd event, to be dispatched to the units */
2369 UNIT_VTABLE(w->data.unit)->fd_event(w->data.unit, w->fd, ev->events, w);
2372 case WATCH_UNIT_TIMER:
2373 case WATCH_JOB_TIMER: {
2377 /* Some timer event, to be dispatched to the units */
2378 if ((k = read(w->fd, &v, sizeof(v))) != sizeof(v)) {
2380 if (k < 0 && (errno == EINTR || errno == EAGAIN))
2383 return k < 0 ? -errno : -EIO;
2386 if (w->type == WATCH_UNIT_TIMER)
2387 UNIT_VTABLE(w->data.unit)->timer_event(w->data.unit, v, w);
2389 job_timer_event(w->data.job, v, w);
2394 /* Some mount table change, intended for the mount subsystem */
2395 mount_fd_event(m, ev->events);
2399 /* Some swap table change, intended for the swap subsystem */
2400 swap_fd_event(m, ev->events);
2404 /* Some notification from udev, intended for the device subsystem */
2405 device_fd_event(m, ev->events);
2408 case WATCH_DBUS_WATCH:
2409 bus_watch_event(m, w, ev->events);
2412 case WATCH_DBUS_TIMEOUT:
2413 bus_timeout_event(m, w, ev->events);
2417 log_error("event type=%i", w->type);
2418 assert_not_reached("Unknown epoll event type.");
2424 int manager_loop(Manager *m) {
2427 RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 50000);
2430 m->exit_code = MANAGER_RUNNING;
2432 /* Release the path cache */
2433 set_free_free(m->unit_path_cache);
2434 m->unit_path_cache = NULL;
2436 manager_check_finished(m);
2438 /* There might still be some zombies hanging around from
2439 * before we were exec()'ed. Leat's reap them */
2440 if ((r = manager_dispatch_sigchld(m)) < 0)
2443 while (m->exit_code == MANAGER_RUNNING) {
2444 struct epoll_event event;
2447 if (!ratelimit_test(&rl)) {
2448 /* Yay, something is going seriously wrong, pause a little */
2449 log_warning("Looping too fast. Throttling execution a little.");
2453 if (manager_dispatch_load_queue(m) > 0)
2456 if (manager_dispatch_run_queue(m) > 0)
2459 if (bus_dispatch(m) > 0)
2462 if (manager_dispatch_cleanup_queue(m) > 0)
2465 if (manager_dispatch_gc_queue(m) > 0)
2468 if (manager_dispatch_dbus_queue(m) > 0)
2471 if (swap_dispatch_reload(m) > 0)
2474 if ((n = epoll_wait(m->epoll_fd, &event, 1, -1)) < 0) {
2484 if ((r = process_event(m, &event)) < 0)
2488 return m->exit_code;
2491 int manager_get_unit_from_dbus_path(Manager *m, const char *s, Unit **_u) {
2499 if (!startswith(s, "/org/freedesktop/systemd1/unit/"))
2502 if (!(n = bus_path_unescape(s+31)))
2505 u = manager_get_unit(m, n);
2516 int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
2525 if (!startswith(s, "/org/freedesktop/systemd1/job/"))
2528 if ((r = safe_atou(s + 30, &id)) < 0)
2531 if (!(j = manager_get_job(m, id)))
2539 void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
2544 if (m->audit_fd < 0)
2547 /* Don't generate audit events if the service was already
2548 * started and we're just deserializing */
2549 if (m->n_reloading > 0)
2552 if (m->running_as != MANAGER_SYSTEM)
2555 if (u->meta.type != UNIT_SERVICE)
2558 if (!(p = unit_name_to_prefix_and_instance(u->meta.id))) {
2559 log_error("Failed to allocate unit name for audit message: %s", strerror(ENOMEM));
2563 if (audit_log_user_comm_message(m->audit_fd, type, "", p, NULL, NULL, NULL, success) < 0) {
2564 log_warning("Failed to send audit message: %m");
2566 if (errno == EPERM) {
2567 /* We aren't allowed to send audit messages?
2568 * Then let's not retry again, to avoid
2569 * spamming the user with the same and same
2570 * messages over and over. */
2572 audit_close(m->audit_fd);
2582 void manager_send_unit_plymouth(Manager *m, Unit *u) {
2584 union sockaddr_union sa;
2586 char *message = NULL;
2588 /* Don't generate plymouth events if the service was already
2589 * started and we're just deserializing */
2590 if (m->n_reloading > 0)
2593 if (m->running_as != MANAGER_SYSTEM)
2596 if (u->meta.type != UNIT_SERVICE &&
2597 u->meta.type != UNIT_MOUNT &&
2598 u->meta.type != UNIT_SWAP)
2601 /* We set SOCK_NONBLOCK here so that we rather drop the
2602 * message then wait for plymouth */
2603 if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
2604 log_error("socket() failed: %m");
2609 sa.sa.sa_family = AF_UNIX;
2610 strncpy(sa.un.sun_path+1, "/org/freedesktop/plymouthd", sizeof(sa.un.sun_path)-1);
2611 if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
2613 if (errno != EPIPE &&
2616 errno != ECONNREFUSED &&
2617 errno != ECONNRESET &&
2618 errno != ECONNABORTED)
2619 log_error("connect() failed: %m");
2624 if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->meta.id) + 1), u->meta.id, &n) < 0) {
2625 log_error("Out of memory");
2630 if (write(fd, message, n + 1) != n + 1) {
2632 if (errno != EPIPE &&
2635 errno != ECONNREFUSED &&
2636 errno != ECONNRESET &&
2637 errno != ECONNABORTED)
2638 log_error("Failed to write Plymouth message: %m");
2645 close_nointr_nofail(fd);
2650 void manager_dispatch_bus_name_owner_changed(
2653 const char* old_owner,
2654 const char *new_owner) {
2661 if (!(u = hashmap_get(m->watch_bus, name)))
2664 UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner);
2667 void manager_dispatch_bus_query_pid_done(
2678 if (!(u = hashmap_get(m->watch_bus, name)))
2681 UNIT_VTABLE(u)->bus_query_pid_done(u, name, pid);
2684 int manager_open_serialization(Manager *m, FILE **_f) {
2692 if (m->running_as == MANAGER_SYSTEM)
2693 asprintf(&path, "/run/systemd/dump-%lu-XXXXXX", (unsigned long) getpid());
2695 asprintf(&path, "/tmp/systemd-dump-%lu-XXXXXX", (unsigned long) getpid());
2700 saved_umask = umask(0077);
2701 fd = mkostemp(path, O_RDWR|O_CLOEXEC);
2711 log_debug("Serializing state to %s", path);
2714 if (!(f = fdopen(fd, "w+")))
2722 int manager_serialize(Manager *m, FILE *f, FDSet *fds) {
2734 fprintf(f, "current-job-id=%i\n", m->current_job_id);
2735 fprintf(f, "taint-usr=%s\n", yes_no(m->taint_usr));
2737 dual_timestamp_serialize(f, "initrd-timestamp", &m->initrd_timestamp);
2738 dual_timestamp_serialize(f, "startup-timestamp", &m->startup_timestamp);
2739 dual_timestamp_serialize(f, "finish-timestamp", &m->finish_timestamp);
2743 HASHMAP_FOREACH_KEY(u, t, m->units, i) {
2744 if (u->meta.id != t)
2747 if (!unit_can_serialize(u))
2751 fputs(u->meta.id, f);
2754 if ((r = unit_serialize(u, f, fds)) < 0) {
2760 assert(m->n_reloading > 0);
2766 r = bus_fdset_add_all(m, fds);
2773 int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
2779 log_debug("Deserializing state...");
2784 char line[LINE_MAX], *l;
2786 if (!fgets(line, sizeof(line), f)) {
2801 if (startswith(l, "current-job-id=")) {
2804 if (safe_atou32(l+15, &id) < 0)
2805 log_debug("Failed to parse current job id value %s", l+15);
2807 m->current_job_id = MAX(m->current_job_id, id);
2808 } else if (startswith(l, "taint-usr=")) {
2811 if ((b = parse_boolean(l+10)) < 0)
2812 log_debug("Failed to parse taint /usr flag %s", l+10);
2814 m->taint_usr = m->taint_usr || b;
2815 } else if (startswith(l, "initrd-timestamp="))
2816 dual_timestamp_deserialize(l+17, &m->initrd_timestamp);
2817 else if (startswith(l, "startup-timestamp="))
2818 dual_timestamp_deserialize(l+18, &m->startup_timestamp);
2819 else if (startswith(l, "finish-timestamp="))
2820 dual_timestamp_deserialize(l+17, &m->finish_timestamp);
2822 log_debug("Unknown serialization item '%s'", l);
2827 char name[UNIT_NAME_MAX+2];
2830 if (!fgets(name, sizeof(name), f)) {
2841 if ((r = manager_load_unit(m, strstrip(name), NULL, NULL, &u)) < 0)
2844 if ((r = unit_deserialize(u, f, fds)) < 0)
2854 assert(m->n_reloading > 0);
2860 int manager_reload(Manager *m) {
2867 if ((r = manager_open_serialization(m, &f)) < 0)
2872 if (!(fds = fdset_new())) {
2878 if ((r = manager_serialize(m, f, fds)) < 0) {
2883 if (fseeko(f, 0, SEEK_SET) < 0) {
2889 /* From here on there is no way back. */
2890 manager_clear_jobs_and_units(m);
2891 manager_undo_generators(m);
2893 /* Find new unit paths */
2894 lookup_paths_free(&m->lookup_paths);
2895 if ((q = lookup_paths_init(&m->lookup_paths, m->running_as, true)) < 0)
2898 manager_run_generators(m);
2900 manager_build_unit_path_cache(m);
2902 /* First, enumerate what we can from all config files */
2903 if ((q = manager_enumerate(m)) < 0)
2906 /* Second, deserialize our stored data */
2907 if ((q = manager_deserialize(m, f, fds)) < 0)
2913 /* Third, fire things up! */
2914 if ((q = manager_coldplug(m)) < 0)
2917 assert(m->n_reloading > 0);
2930 bool manager_is_booting_or_shutting_down(Manager *m) {
2935 /* Is the initial job still around? */
2936 if (manager_get_job(m, 1))
2939 /* Is there a job for the shutdown target? */
2940 u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET);
2942 return !!u->meta.job;
2947 void manager_reset_failed(Manager *m) {
2953 HASHMAP_FOREACH(u, m->units, i)
2954 unit_reset_failed(u);
2957 bool manager_unit_pending_inactive(Manager *m, const char *name) {
2963 /* Returns true if the unit is inactive or going down */
2964 if (!(u = manager_get_unit(m, name)))
2967 return unit_pending_inactive(u);
2970 void manager_check_finished(Manager *m) {
2971 char userspace[FORMAT_TIMESPAN_MAX], initrd[FORMAT_TIMESPAN_MAX], kernel[FORMAT_TIMESPAN_MAX], sum[FORMAT_TIMESPAN_MAX];
2972 usec_t kernel_usec = 0, initrd_usec = 0, userspace_usec = 0, total_usec = 0;
2976 if (dual_timestamp_is_set(&m->finish_timestamp))
2979 if (hashmap_size(m->jobs) > 0)
2982 dual_timestamp_get(&m->finish_timestamp);
2984 if (m->running_as == MANAGER_SYSTEM && detect_container(NULL) <= 0) {
2986 userspace_usec = m->finish_timestamp.monotonic - m->startup_timestamp.monotonic;
2987 total_usec = m->finish_timestamp.monotonic;
2989 if (dual_timestamp_is_set(&m->initrd_timestamp)) {
2991 kernel_usec = m->initrd_timestamp.monotonic;
2992 initrd_usec = m->startup_timestamp.monotonic - m->initrd_timestamp.monotonic;
2994 log_info("Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.",
2995 format_timespan(kernel, sizeof(kernel), kernel_usec),
2996 format_timespan(initrd, sizeof(initrd), initrd_usec),
2997 format_timespan(userspace, sizeof(userspace), userspace_usec),
2998 format_timespan(sum, sizeof(sum), total_usec));
3000 kernel_usec = m->startup_timestamp.monotonic;
3003 log_info("Startup finished in %s (kernel) + %s (userspace) = %s.",
3004 format_timespan(kernel, sizeof(kernel), kernel_usec),
3005 format_timespan(userspace, sizeof(userspace), userspace_usec),
3006 format_timespan(sum, sizeof(sum), total_usec));
3009 userspace_usec = initrd_usec = kernel_usec = 0;
3010 total_usec = m->finish_timestamp.monotonic - m->startup_timestamp.monotonic;
3012 log_debug("Startup finished in %s.",
3013 format_timespan(sum, sizeof(sum), total_usec));
3016 bus_broadcast_finished(m, kernel_usec, initrd_usec, userspace_usec, total_usec);
3019 "READY=1\nSTATUS=Startup finished in %s.",
3020 format_timespan(sum, sizeof(sum), total_usec));
3023 void manager_run_generators(Manager *m) {
3025 const char *generator_path;
3026 const char *argv[3];
3031 generator_path = m->running_as == MANAGER_SYSTEM ? SYSTEM_GENERATOR_PATH : USER_GENERATOR_PATH;
3032 if (!(d = opendir(generator_path))) {
3034 if (errno == ENOENT)
3037 log_error("Failed to enumerate generator directory: %m");
3041 if (!m->generator_unit_path) {
3043 char user_path[] = "/tmp/systemd-generator-XXXXXX";
3045 if (m->running_as == MANAGER_SYSTEM && getpid() == 1) {
3046 p = "/run/systemd/generator";
3048 if (mkdir_p(p, 0755) < 0) {
3049 log_error("Failed to create generator directory: %m");
3054 if (!(p = mkdtemp(user_path))) {
3055 log_error("Failed to create generator directory: %m");
3060 if (!(m->generator_unit_path = strdup(p))) {
3061 log_error("Failed to allocate generator unit path.");
3066 argv[0] = NULL; /* Leave this empty, execute_directory() will fill something in */
3067 argv[1] = m->generator_unit_path;
3071 execute_directory(generator_path, d, (char**) argv);
3074 if (rmdir(m->generator_unit_path) >= 0) {
3075 /* Uh? we were able to remove this dir? I guess that
3076 * means the directory was empty, hence let's shortcut
3079 free(m->generator_unit_path);
3080 m->generator_unit_path = NULL;
3084 if (!strv_find(m->lookup_paths.unit_path, m->generator_unit_path)) {
3087 if (!(l = strv_append(m->lookup_paths.unit_path, m->generator_unit_path))) {
3088 log_error("Failed to add generator directory to unit search path: %m");
3092 strv_free(m->lookup_paths.unit_path);
3093 m->lookup_paths.unit_path = l;
3095 log_debug("Added generator unit path %s to search path.", m->generator_unit_path);
3103 void manager_undo_generators(Manager *m) {
3106 if (!m->generator_unit_path)
3109 strv_remove(m->lookup_paths.unit_path, m->generator_unit_path);
3110 rm_rf(m->generator_unit_path, false, true, false);
3112 free(m->generator_unit_path);
3113 m->generator_unit_path = NULL;
3116 int manager_set_default_controllers(Manager *m, char **controllers) {
3121 if (!(l = strv_copy(controllers)))
3124 strv_free(m->default_controllers);
3125 m->default_controllers = l;
3130 void manager_recheck_journal(Manager *m) {
3135 if (m->running_as != MANAGER_SYSTEM)
3138 u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET);
3139 if (u && SOCKET(u)->state != SOCKET_RUNNING) {
3140 log_close_journal();
3144 u = manager_get_unit(m, SPECIAL_JOURNALD_SERVICE);
3145 if (u && SERVICE(u)->state != SERVICE_RUNNING) {
3146 log_close_journal();
3150 /* Hmm, OK, so the socket is fully up and the service is up
3151 * too, then let's make use of the thing. */
3155 void manager_set_show_status(Manager *m, bool b) {
3158 if (m->running_as != MANAGER_SYSTEM)
3164 touch("/run/systemd/show-status");
3166 unlink("/run/systemd/show-status");
3169 bool manager_get_show_status(Manager *m) {
3172 if (m->running_as != MANAGER_SYSTEM)
3178 /* If Plymouth is running make sure we show the status, so
3179 * that there's something nice to see when people press Esc */
3181 return plymouth_running();
3184 static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = {
3185 [MANAGER_SYSTEM] = "system",
3186 [MANAGER_USER] = "user"
3189 DEFINE_STRING_TABLE_LOOKUP(manager_running_as, ManagerRunningAs);