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+27, /* systemd: set log target to console */
199 SIGRTMIN+28, /* systemd: set log target to kmsg */
200 SIGRTMIN+29, /* systemd: set log target to syslog-or-kmsg */
202 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
204 m->signal_watch.type = WATCH_SIGNAL;
205 if ((m->signal_watch.fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0)
210 ev.data.ptr = &m->signal_watch;
212 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0)
215 if (m->running_as == MANAGER_SYSTEM)
216 return enable_special_signals(m);
221 int manager_new(ManagerRunningAs running_as, Manager **_m) {
226 assert(running_as >= 0);
227 assert(running_as < _MANAGER_RUNNING_AS_MAX);
229 if (!(m = new0(Manager, 1)))
232 dual_timestamp_get(&m->startup_timestamp);
234 m->running_as = running_as;
235 m->name_data_slot = m->conn_data_slot = m->subscribed_data_slot = -1;
236 m->exit_code = _MANAGER_EXIT_CODE_INVALID;
237 m->pin_cgroupfs_fd = -1;
243 m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = m->dev_autofs_fd = m->swap_watch.fd = -1;
244 m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
246 if (!(m->environment = strv_copy(environ)))
249 if (!(m->default_controllers = strv_new("cpu", NULL)))
252 if (!(m->units = hashmap_new(string_hash_func, string_compare_func)))
255 if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
258 if (!(m->transaction_jobs = hashmap_new(trivial_hash_func, trivial_compare_func)))
261 if (!(m->watch_pids = hashmap_new(trivial_hash_func, trivial_compare_func)))
264 if (!(m->cgroup_bondings = hashmap_new(string_hash_func, string_compare_func)))
267 if (!(m->watch_bus = hashmap_new(string_hash_func, string_compare_func)))
270 if ((m->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
273 if ((r = lookup_paths_init(&m->lookup_paths, m->running_as, true)) < 0)
276 if ((r = manager_setup_signals(m)) < 0)
279 if ((r = manager_setup_cgroup(m)) < 0)
282 if ((r = manager_setup_notify(m)) < 0)
285 /* Try to connect to the busses, if possible. */
286 if ((r = bus_init(m, running_as != MANAGER_SYSTEM)) < 0)
290 if ((m->audit_fd = audit_open()) < 0 &&
291 /* If the kernel lacks netlink or audit support,
292 * don't worry about it. */
293 errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
294 log_error("Failed to connect to audit log: %m");
297 m->taint_usr = dir_is_empty("/usr") > 0;
307 static unsigned manager_dispatch_cleanup_queue(Manager *m) {
313 while ((meta = m->cleanup_queue)) {
314 assert(meta->in_cleanup_queue);
316 unit_free((Unit*) meta);
324 GC_OFFSET_IN_PATH, /* This one is on the path we were traveling */
325 GC_OFFSET_UNSURE, /* No clue */
326 GC_OFFSET_GOOD, /* We still need this unit */
327 GC_OFFSET_BAD, /* We don't need this unit anymore */
331 static void unit_gc_sweep(Unit *u, unsigned gc_marker) {
338 if (u->meta.gc_marker == gc_marker + GC_OFFSET_GOOD ||
339 u->meta.gc_marker == gc_marker + GC_OFFSET_BAD ||
340 u->meta.gc_marker == gc_marker + GC_OFFSET_IN_PATH)
343 if (u->meta.in_cleanup_queue)
346 if (unit_check_gc(u))
349 u->meta.gc_marker = gc_marker + GC_OFFSET_IN_PATH;
353 SET_FOREACH(other, u->meta.dependencies[UNIT_REFERENCED_BY], i) {
354 unit_gc_sweep(other, gc_marker);
356 if (other->meta.gc_marker == gc_marker + GC_OFFSET_GOOD)
359 if (other->meta.gc_marker != gc_marker + GC_OFFSET_BAD)
366 /* We were unable to find anything out about this entry, so
367 * let's investigate it later */
368 u->meta.gc_marker = gc_marker + GC_OFFSET_UNSURE;
369 unit_add_to_gc_queue(u);
373 /* We definitely know that this one is not useful anymore, so
374 * let's mark it for deletion */
375 u->meta.gc_marker = gc_marker + GC_OFFSET_BAD;
376 unit_add_to_cleanup_queue(u);
380 u->meta.gc_marker = gc_marker + GC_OFFSET_GOOD;
383 static unsigned manager_dispatch_gc_queue(Manager *m) {
390 if ((m->n_in_gc_queue < GC_QUEUE_ENTRIES_MAX) &&
391 (m->gc_queue_timestamp <= 0 ||
392 (m->gc_queue_timestamp + GC_QUEUE_USEC_MAX) > now(CLOCK_MONOTONIC)))
395 log_debug("Running GC...");
397 m->gc_marker += _GC_OFFSET_MAX;
398 if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX)
401 gc_marker = m->gc_marker;
403 while ((meta = m->gc_queue)) {
404 assert(meta->in_gc_queue);
406 unit_gc_sweep((Unit*) meta, gc_marker);
408 LIST_REMOVE(Meta, gc_queue, m->gc_queue, meta);
409 meta->in_gc_queue = false;
413 if (meta->gc_marker == gc_marker + GC_OFFSET_BAD ||
414 meta->gc_marker == gc_marker + GC_OFFSET_UNSURE) {
415 log_debug("Collecting %s", meta->id);
416 meta->gc_marker = gc_marker + GC_OFFSET_BAD;
417 unit_add_to_cleanup_queue((Unit*) meta);
421 m->n_in_gc_queue = 0;
422 m->gc_queue_timestamp = 0;
427 static void manager_clear_jobs_and_units(Manager *m) {
433 while ((j = hashmap_first(m->transaction_jobs)))
436 while ((u = hashmap_first(m->units)))
439 manager_dispatch_cleanup_queue(m);
441 assert(!m->load_queue);
442 assert(!m->run_queue);
443 assert(!m->dbus_unit_queue);
444 assert(!m->dbus_job_queue);
445 assert(!m->cleanup_queue);
446 assert(!m->gc_queue);
448 assert(hashmap_isempty(m->transaction_jobs));
449 assert(hashmap_isempty(m->jobs));
450 assert(hashmap_isempty(m->units));
453 void manager_free(Manager *m) {
458 manager_clear_jobs_and_units(m);
460 for (c = 0; c < _UNIT_TYPE_MAX; c++)
461 if (unit_vtable[c]->shutdown)
462 unit_vtable[c]->shutdown(m);
464 /* If we reexecute ourselves, we keep the root cgroup
466 manager_shutdown_cgroup(m, m->exit_code != MANAGER_REEXECUTE);
468 manager_undo_generators(m);
472 hashmap_free(m->units);
473 hashmap_free(m->jobs);
474 hashmap_free(m->transaction_jobs);
475 hashmap_free(m->watch_pids);
476 hashmap_free(m->watch_bus);
478 if (m->epoll_fd >= 0)
479 close_nointr_nofail(m->epoll_fd);
480 if (m->signal_watch.fd >= 0)
481 close_nointr_nofail(m->signal_watch.fd);
482 if (m->notify_watch.fd >= 0)
483 close_nointr_nofail(m->notify_watch.fd);
486 if (m->audit_fd >= 0)
487 audit_close(m->audit_fd);
490 free(m->notify_socket);
492 lookup_paths_free(&m->lookup_paths);
493 strv_free(m->environment);
495 strv_free(m->default_controllers);
497 hashmap_free(m->cgroup_bondings);
498 set_free_free(m->unit_path_cache);
503 int manager_enumerate(Manager *m) {
509 /* Let's ask every type to load all units from disk/kernel
510 * that it might know */
511 for (c = 0; c < _UNIT_TYPE_MAX; c++)
512 if (unit_vtable[c]->enumerate)
513 if ((q = unit_vtable[c]->enumerate(m)) < 0)
516 manager_dispatch_load_queue(m);
520 int manager_coldplug(Manager *m) {
528 /* Then, let's set up their initial state. */
529 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
535 if ((q = unit_coldplug(u)) < 0)
542 static void manager_build_unit_path_cache(Manager *m) {
549 set_free_free(m->unit_path_cache);
551 if (!(m->unit_path_cache = set_new(string_hash_func, string_compare_func))) {
552 log_error("Failed to allocate unit path cache.");
556 /* This simply builds a list of files we know exist, so that
557 * we don't always have to go to disk */
559 STRV_FOREACH(i, m->lookup_paths.unit_path) {
562 if (!(d = opendir(*i))) {
563 log_error("Failed to open directory: %m");
567 while ((de = readdir(d))) {
570 if (ignore_file(de->d_name))
573 p = join(streq(*i, "/") ? "" : *i, "/", de->d_name, NULL);
579 if ((r = set_put(m->unit_path_cache, p)) < 0) {
592 log_error("Failed to build unit path cache: %s", strerror(-r));
594 set_free_free(m->unit_path_cache);
595 m->unit_path_cache = NULL;
601 int manager_startup(Manager *m, FILE *serialization, FDSet *fds) {
606 manager_run_generators(m);
608 manager_build_unit_path_cache(m);
610 /* If we will deserialize make sure that during enumeration
611 * this is already known, so we increase the counter here
616 /* First, enumerate what we can from all config files */
617 r = manager_enumerate(m);
619 /* Second, deserialize if there is something to deserialize */
621 if ((q = manager_deserialize(m, serialization, fds)) < 0)
624 /* Third, fire things up! */
625 if ((q = manager_coldplug(m)) < 0)
629 assert(m->n_reloading > 0);
636 static void transaction_delete_job(Manager *m, Job *j, bool delete_dependencies) {
640 /* Deletes one job from the transaction */
642 manager_transaction_unlink_job(m, j, delete_dependencies);
648 static void transaction_delete_unit(Manager *m, Unit *u) {
651 /* Deletes all jobs associated with a certain unit from the
654 while ((j = hashmap_get(m->transaction_jobs, u)))
655 transaction_delete_job(m, j, true);
658 static void transaction_clean_dependencies(Manager *m) {
664 /* Drops all dependencies of all installed jobs */
666 HASHMAP_FOREACH(j, m->jobs, i) {
667 while (j->subject_list)
668 job_dependency_free(j->subject_list);
669 while (j->object_list)
670 job_dependency_free(j->object_list);
673 assert(!m->transaction_anchor);
676 static void transaction_abort(Manager *m) {
681 while ((j = hashmap_first(m->transaction_jobs)))
683 transaction_delete_job(m, j, true);
687 assert(hashmap_isempty(m->transaction_jobs));
689 transaction_clean_dependencies(m);
692 static void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) {
697 /* A recursive sweep through the graph that marks all units
698 * that matter to the anchor job, i.e. are directly or
699 * indirectly a dependency of the anchor job via paths that
700 * are fully marked as mattering. */
705 l = m->transaction_anchor;
707 LIST_FOREACH(subject, l, l) {
709 /* This link does not matter */
713 /* This unit has already been marked */
714 if (l->object->generation == generation)
717 l->object->matters_to_anchor = true;
718 l->object->generation = generation;
720 transaction_find_jobs_that_matter_to_anchor(m, l->object, generation);
724 static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) {
725 JobDependency *l, *last;
729 assert(j->unit == other->unit);
730 assert(!j->installed);
732 /* Merges 'other' into 'j' and then deletes j. */
735 j->state = JOB_WAITING;
736 j->override = j->override || other->override;
738 j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor;
740 /* Patch us in as new owner of the JobDependency objects */
742 LIST_FOREACH(subject, l, other->subject_list) {
743 assert(l->subject == other);
748 /* Merge both lists */
750 last->subject_next = j->subject_list;
752 j->subject_list->subject_prev = last;
753 j->subject_list = other->subject_list;
756 /* Patch us in as new owner of the JobDependency objects */
758 LIST_FOREACH(object, l, other->object_list) {
759 assert(l->object == other);
764 /* Merge both lists */
766 last->object_next = j->object_list;
768 j->object_list->object_prev = last;
769 j->object_list = other->object_list;
772 /* Kill the other job */
773 other->subject_list = NULL;
774 other->object_list = NULL;
775 transaction_delete_job(m, other, true);
777 static bool job_is_conflicted_by(Job *j) {
782 /* Returns true if this job is pulled in by a least one
783 * ConflictedBy dependency. */
785 LIST_FOREACH(object, l, j->object_list)
792 static int delete_one_unmergeable_job(Manager *m, Job *j) {
797 /* Tries to delete one item in the linked list
798 * j->transaction_next->transaction_next->... that conflicts
799 * with another one, in an attempt to make an inconsistent
800 * transaction work. */
802 /* We rely here on the fact that if a merged with b does not
803 * merge with c, either a or b merge with c neither */
804 LIST_FOREACH(transaction, j, j)
805 LIST_FOREACH(transaction, k, j->transaction_next) {
808 /* Is this one mergeable? Then skip it */
809 if (job_type_is_mergeable(j->type, k->type))
812 /* Ok, we found two that conflict, let's see if we can
813 * drop one of them */
814 if (!j->matters_to_anchor && !k->matters_to_anchor) {
816 /* Both jobs don't matter, so let's
817 * find the one that is smarter to
818 * remove. Let's think positive and
819 * rather remove stops then starts --
820 * except if something is being
821 * stopped because it is conflicted by
822 * another unit in which case we
823 * rather remove the start. */
825 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)));
826 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)));
828 if (j->type == JOB_STOP) {
830 if (job_is_conflicted_by(j))
835 } else if (k->type == JOB_STOP) {
837 if (job_is_conflicted_by(k))
844 } else if (!j->matters_to_anchor)
846 else if (!k->matters_to_anchor)
851 /* Ok, we can drop one, so let's do so. */
852 log_debug("Fixing conflicting jobs by deleting job %s/%s", d->unit->meta.id, job_type_to_string(d->type));
853 transaction_delete_job(m, d, true);
860 static int transaction_merge_jobs(Manager *m, DBusError *e) {
867 /* First step, check whether any of the jobs for one specific
868 * task conflict. If so, try to drop one of them. */
869 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
874 LIST_FOREACH(transaction, k, j->transaction_next) {
875 if (job_type_merge(&t, k->type) >= 0)
878 /* OK, we could not merge all jobs for this
879 * action. Let's see if we can get rid of one
882 if ((r = delete_one_unmergeable_job(m, j)) >= 0)
883 /* Ok, we managed to drop one, now
884 * let's ask our callers to call us
885 * again after garbage collecting */
888 /* We couldn't merge anything. Failure */
889 dbus_set_error(e, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING, "Transaction contains conflicting jobs '%s' and '%s' for %s. Probably contradicting requirement dependencies configured.",
890 job_type_to_string(t), job_type_to_string(k->type), k->unit->meta.id);
895 /* Second step, merge the jobs. */
896 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
900 /* Merge all transactions */
901 LIST_FOREACH(transaction, k, j->transaction_next)
902 assert_se(job_type_merge(&t, k->type) == 0);
904 /* If an active job is mergeable, merge it too */
905 if (j->unit->meta.job)
906 job_type_merge(&t, j->unit->meta.job->type); /* Might fail. Which is OK */
908 while ((k = j->transaction_next)) {
910 transaction_merge_and_delete_job(m, k, j, t);
913 transaction_merge_and_delete_job(m, j, k, t);
916 if (j->unit->meta.job && !j->installed)
917 transaction_merge_and_delete_job(m, j, j->unit->meta.job, t);
919 assert(!j->transaction_next);
920 assert(!j->transaction_prev);
926 static void transaction_drop_redundant(Manager *m) {
931 /* Goes through the transaction and removes all jobs that are
940 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
941 bool changes_something = false;
944 LIST_FOREACH(transaction, k, j) {
946 if (!job_is_anchor(k) &&
947 (k->installed || job_type_is_redundant(k->type, unit_active_state(k->unit))) &&
948 (!k->unit->meta.job || !job_type_is_conflicting(k->type, k->unit->meta.job->type)))
951 changes_something = true;
955 if (changes_something)
958 /* log_debug("Found redundant job %s/%s, dropping.", j->unit->meta.id, job_type_to_string(j->type)); */
959 transaction_delete_job(m, j, false);
967 static bool unit_matters_to_anchor(Unit *u, Job *j) {
969 assert(!j->transaction_prev);
971 /* Checks whether at least one of the jobs for this unit
972 * matters to the anchor. */
974 LIST_FOREACH(transaction, j, j)
975 if (j->matters_to_anchor)
981 static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation, DBusError *e) {
988 assert(!j->transaction_prev);
990 /* Does a recursive sweep through the ordering graph, looking
991 * for a cycle. If we find cycle we try to break it. */
993 /* Have we seen this before? */
994 if (j->generation == generation) {
997 /* If the marker is NULL we have been here already and
998 * decided the job was loop-free from here. Hence
999 * shortcut things and return right-away. */
1003 /* So, the marker is not NULL and we already have been
1004 * here. We have a cycle. Let's try to break it. We go
1005 * backwards in our path and try to find a suitable
1006 * job to remove. We use the marker to find our way
1007 * back, since smart how we are we stored our way back
1009 log_warning("Found ordering cycle on %s/%s", j->unit->meta.id, job_type_to_string(j->type));
1012 for (k = from; k; k = ((k->generation == generation && k->marker != k) ? k->marker : NULL)) {
1014 log_info("Walked on cycle path to %s/%s", k->unit->meta.id, job_type_to_string(k->type));
1018 !unit_matters_to_anchor(k->unit, k)) {
1019 /* Ok, we can drop this one, so let's
1024 /* Check if this in fact was the beginning of
1032 log_warning("Breaking ordering cycle by deleting job %s/%s", delete->unit->meta.id, job_type_to_string(delete->type));
1033 transaction_delete_unit(m, delete->unit);
1037 log_error("Unable to break cycle");
1039 dbus_set_error(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC, "Transaction order is cyclic. See system logs for details.");
1043 /* Make the marker point to where we come from, so that we can
1044 * find our way backwards if we want to break a cycle. We use
1045 * a special marker for the beginning: we point to
1047 j->marker = from ? from : j;
1048 j->generation = generation;
1050 /* We assume that the the dependencies are bidirectional, and
1051 * hence can ignore UNIT_AFTER */
1052 SET_FOREACH(u, j->unit->meta.dependencies[UNIT_BEFORE], i) {
1055 /* Is there a job for this unit? */
1056 if (!(o = hashmap_get(m->transaction_jobs, u)))
1058 /* Ok, there is no job for this in the
1059 * transaction, but maybe there is already one
1061 if (!(o = u->meta.job))
1064 if ((r = transaction_verify_order_one(m, o, j, generation, e)) < 0)
1068 /* Ok, let's backtrack, and remember that this entry is not on
1069 * our path anymore. */
1075 static int transaction_verify_order(Manager *m, unsigned *generation, DBusError *e) {
1084 /* Check if the ordering graph is cyclic. If it is, try to fix
1085 * that up by dropping one of the jobs. */
1087 g = (*generation)++;
1089 HASHMAP_FOREACH(j, m->transaction_jobs, i)
1090 if ((r = transaction_verify_order_one(m, j, NULL, g, e)) < 0)
1096 static void transaction_collect_garbage(Manager *m) {
1101 /* Drop jobs that are not required by any other job */
1109 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1110 if (j->object_list) {
1111 /* log_debug("Keeping job %s/%s because of %s/%s", */
1112 /* j->unit->meta.id, job_type_to_string(j->type), */
1113 /* j->object_list->subject ? j->object_list->subject->unit->meta.id : "root", */
1114 /* j->object_list->subject ? job_type_to_string(j->object_list->subject->type) : "root"); */
1118 /* log_debug("Garbage collecting job %s/%s", j->unit->meta.id, job_type_to_string(j->type)); */
1119 transaction_delete_job(m, j, true);
1127 static int transaction_is_destructive(Manager *m, DBusError *e) {
1133 /* Checks whether applying this transaction means that
1134 * existing jobs would be replaced */
1136 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1139 assert(!j->transaction_prev);
1140 assert(!j->transaction_next);
1142 if (j->unit->meta.job &&
1143 j->unit->meta.job != j &&
1144 !job_type_is_superset(j->type, j->unit->meta.job->type)) {
1146 dbus_set_error(e, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE, "Transaction is destructive.");
1154 static void transaction_minimize_impact(Manager *m) {
1158 /* Drops all unnecessary jobs that reverse already active jobs
1159 * or that stop a running service. */
1167 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1168 LIST_FOREACH(transaction, j, j) {
1169 bool stops_running_service, changes_existing_job;
1171 /* If it matters, we shouldn't drop it */
1172 if (j->matters_to_anchor)
1175 /* Would this stop a running service?
1176 * Would this change an existing job?
1177 * If so, let's drop this entry */
1179 stops_running_service =
1180 j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit));
1182 changes_existing_job =
1183 j->unit->meta.job &&
1184 job_type_is_conflicting(j->type, j->unit->meta.job->type);
1186 if (!stops_running_service && !changes_existing_job)
1189 if (stops_running_service)
1190 log_debug("%s/%s would stop a running service.", j->unit->meta.id, job_type_to_string(j->type));
1192 if (changes_existing_job)
1193 log_debug("%s/%s would change existing job.", j->unit->meta.id, job_type_to_string(j->type));
1195 /* Ok, let's get rid of this */
1196 log_debug("Deleting %s/%s to minimize impact.", j->unit->meta.id, job_type_to_string(j->type));
1198 transaction_delete_job(m, j, true);
1210 static int transaction_apply(Manager *m, JobMode mode) {
1215 /* Moves the transaction jobs to the set of active jobs */
1217 if (mode == JOB_ISOLATE) {
1219 /* When isolating first kill all installed jobs which
1220 * aren't part of the new transaction */
1222 HASHMAP_FOREACH(j, m->jobs, i) {
1223 assert(j->installed);
1225 if (hashmap_get(m->transaction_jobs, j->unit))
1228 /* 'j' itself is safe to remove, but if other jobs
1229 are invalidated recursively, our iterator may become
1230 invalid and we need to start over. */
1231 if (job_finish_and_invalidate(j, JOB_CANCELED) > 0)
1236 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1238 assert(!j->transaction_prev);
1239 assert(!j->transaction_next);
1244 if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0)
1248 while ((j = hashmap_steal_first(m->transaction_jobs))) {
1250 /* log_debug("Skipping already installed job %s/%s as %u", j->unit->meta.id, job_type_to_string(j->type), (unsigned) j->id); */
1254 if (j->unit->meta.job)
1255 job_free(j->unit->meta.job);
1257 j->unit->meta.job = j;
1258 j->installed = true;
1259 m->n_installed_jobs ++;
1261 /* We're fully installed. Now let's free data we don't
1264 assert(!j->transaction_next);
1265 assert(!j->transaction_prev);
1267 job_add_to_run_queue(j);
1268 job_add_to_dbus_queue(j);
1271 log_debug("Installed new job %s/%s as %u", j->unit->meta.id, job_type_to_string(j->type), (unsigned) j->id);
1274 /* As last step, kill all remaining job dependencies. */
1275 transaction_clean_dependencies(m);
1281 HASHMAP_FOREACH(j, m->transaction_jobs, i) {
1285 hashmap_remove(m->jobs, UINT32_TO_PTR(j->id));
1291 static int transaction_activate(Manager *m, JobMode mode, DBusError *e) {
1293 unsigned generation = 1;
1297 /* This applies the changes recorded in transaction_jobs to
1298 * the actual list of jobs, if possible. */
1300 /* First step: figure out which jobs matter */
1301 transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++);
1303 /* Second step: Try not to stop any running services if
1304 * we don't have to. Don't try to reverse running
1305 * jobs if we don't have to. */
1306 if (mode == JOB_FAIL)
1307 transaction_minimize_impact(m);
1309 /* Third step: Drop redundant jobs */
1310 transaction_drop_redundant(m);
1313 /* Fourth step: Let's remove unneeded jobs that might
1315 if (mode != JOB_ISOLATE)
1316 transaction_collect_garbage(m);
1318 /* Fifth step: verify order makes sense and correct
1319 * cycles if necessary and possible */
1320 if ((r = transaction_verify_order(m, &generation, e)) >= 0)
1324 log_warning("Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error(e, r));
1328 /* Let's see if the resulting transaction ordering
1329 * graph is still cyclic... */
1333 /* Sixth step: let's drop unmergeable entries if
1334 * necessary and possible, merge entries we can
1336 if ((r = transaction_merge_jobs(m, e)) >= 0)
1340 log_warning("Requested transaction contains unmergeable jobs: %s", bus_error(e, r));
1344 /* Seventh step: an entry got dropped, let's garbage
1345 * collect its dependencies. */
1346 if (mode != JOB_ISOLATE)
1347 transaction_collect_garbage(m);
1349 /* Let's see if the resulting transaction still has
1350 * unmergeable entries ... */
1353 /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */
1354 transaction_drop_redundant(m);
1356 /* Ninth step: check whether we can actually apply this */
1357 if (mode == JOB_FAIL)
1358 if ((r = transaction_is_destructive(m, e)) < 0) {
1359 log_notice("Requested transaction contradicts existing jobs: %s", bus_error(e, r));
1363 /* Tenth step: apply changes */
1364 if ((r = transaction_apply(m, mode)) < 0) {
1365 log_warning("Failed to apply transaction: %s", strerror(-r));
1369 assert(hashmap_isempty(m->transaction_jobs));
1370 assert(!m->transaction_anchor);
1375 transaction_abort(m);
1379 static Job* transaction_add_one_job(Manager *m, JobType type, Unit *unit, bool override, bool *is_new) {
1385 /* Looks for an existing prospective job and returns that. If
1386 * it doesn't exist it is created and added to the prospective
1389 f = hashmap_get(m->transaction_jobs, unit);
1391 LIST_FOREACH(transaction, j, f) {
1392 assert(j->unit == unit);
1394 if (j->type == type) {
1401 if (unit->meta.job && unit->meta.job->type == type)
1403 else if (!(j = job_new(m, type, unit)))
1408 j->matters_to_anchor = false;
1409 j->override = override;
1411 LIST_PREPEND(Job, transaction, f, j);
1413 if (hashmap_replace(m->transaction_jobs, unit, f) < 0) {
1421 /* log_debug("Added job %s/%s to transaction.", unit->meta.id, job_type_to_string(type)); */
1426 void manager_transaction_unlink_job(Manager *m, Job *j, bool delete_dependencies) {
1430 if (j->transaction_prev)
1431 j->transaction_prev->transaction_next = j->transaction_next;
1432 else if (j->transaction_next)
1433 hashmap_replace(m->transaction_jobs, j->unit, j->transaction_next);
1435 hashmap_remove_value(m->transaction_jobs, j->unit, j);
1437 if (j->transaction_next)
1438 j->transaction_next->transaction_prev = j->transaction_prev;
1440 j->transaction_prev = j->transaction_next = NULL;
1442 while (j->subject_list)
1443 job_dependency_free(j->subject_list);
1445 while (j->object_list) {
1446 Job *other = j->object_list->matters ? j->object_list->subject : NULL;
1448 job_dependency_free(j->object_list);
1450 if (other && delete_dependencies) {
1451 log_debug("Deleting job %s/%s as dependency of job %s/%s",
1452 other->unit->meta.id, job_type_to_string(other->type),
1453 j->unit->meta.id, job_type_to_string(j->type));
1454 transaction_delete_job(m, other, delete_dependencies);
1459 static int transaction_add_job_and_dependencies(
1467 bool ignore_requirements,
1478 assert(type < _JOB_TYPE_MAX);
1481 /* log_debug("Pulling in %s/%s from %s/%s", */
1482 /* unit->meta.id, job_type_to_string(type), */
1483 /* by ? by->unit->meta.id : "NA", */
1484 /* by ? job_type_to_string(by->type) : "NA"); */
1486 if (unit->meta.load_state != UNIT_LOADED &&
1487 unit->meta.load_state != UNIT_ERROR &&
1488 unit->meta.load_state != UNIT_MASKED) {
1489 dbus_set_error(e, BUS_ERROR_LOAD_FAILED, "Unit %s is not loaded properly.", unit->meta.id);
1493 if (type != JOB_STOP && unit->meta.load_state == UNIT_ERROR) {
1494 dbus_set_error(e, BUS_ERROR_LOAD_FAILED,
1495 "Unit %s failed to load: %s. "
1496 "See system logs and 'systemctl status %s' for details.",
1498 strerror(-unit->meta.load_error),
1503 if (type != JOB_STOP && unit->meta.load_state == UNIT_MASKED) {
1504 dbus_set_error(e, BUS_ERROR_MASKED, "Unit %s is masked.", unit->meta.id);
1508 if (!unit_job_is_applicable(unit, type)) {
1509 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);
1513 /* First add the job. */
1514 if (!(ret = transaction_add_one_job(m, type, unit, override, &is_new)))
1517 ret->ignore_order = ret->ignore_order || ignore_order;
1519 /* Then, add a link to the job. */
1520 if (!job_dependency_new(by, ret, matters, conflicts))
1523 if (is_new && !ignore_requirements) {
1526 /* If we are following some other unit, make sure we
1527 * add all dependencies of everybody following. */
1528 if (unit_following_set(ret->unit, &following) > 0) {
1529 SET_FOREACH(dep, following, i)
1530 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, false, override, false, false, ignore_order, e, NULL)) < 0) {
1531 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1537 set_free(following);
1540 /* Finally, recursively add in all dependencies. */
1541 if (type == JOB_START || type == JOB_RELOAD_OR_START) {
1542 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES], i)
1543 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1551 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_BIND_TO], i)
1552 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1561 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
1562 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !override, override, false, false, ignore_order, e, NULL)) < 0) {
1563 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1569 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_WANTS], i)
1570 if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, false, false, false, ignore_order, e, NULL)) < 0) {
1571 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1577 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE], i)
1578 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1587 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i)
1588 if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, !override, override, false, false, ignore_order, e, NULL)) < 0) {
1589 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1595 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTS], i)
1596 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, override, true, false, ignore_order, e, NULL)) < 0) {
1605 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTED_BY], i)
1606 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, false, override, false, false, ignore_order, e, NULL)) < 0) {
1607 log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1613 } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
1615 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRED_BY], i)
1616 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1625 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_BOUND_BY], i)
1626 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1636 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
1648 static int transaction_add_isolate_jobs(Manager *m) {
1656 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
1658 /* ignore aliases */
1659 if (u->meta.id != k)
1662 if (u->meta.ignore_on_isolate)
1665 /* No need to stop inactive jobs */
1666 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) && !u->meta.job)
1669 /* Is there already something listed for this? */
1670 if (hashmap_get(m->transaction_jobs, u))
1673 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, u, NULL, true, false, false, false, false, NULL, NULL)) < 0)
1674 log_warning("Cannot add isolate job for unit %s, ignoring: %s", u->meta.id, strerror(-r));
1680 int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool override, DBusError *e, Job **_ret) {
1685 assert(type < _JOB_TYPE_MAX);
1687 assert(mode < _JOB_MODE_MAX);
1689 if (mode == JOB_ISOLATE && type != JOB_START) {
1690 dbus_set_error(e, BUS_ERROR_INVALID_JOB_MODE, "Isolate is only valid for start.");
1694 if (mode == JOB_ISOLATE && !unit->meta.allow_isolate) {
1695 dbus_set_error(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated.");
1699 log_debug("Trying to enqueue job %s/%s/%s", unit->meta.id, job_type_to_string(type), job_mode_to_string(mode));
1701 if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, override, false,
1702 mode == JOB_IGNORE_DEPENDENCIES || mode == JOB_IGNORE_REQUIREMENTS,
1703 mode == JOB_IGNORE_DEPENDENCIES, e, &ret)) < 0) {
1704 transaction_abort(m);
1708 if (mode == JOB_ISOLATE)
1709 if ((r = transaction_add_isolate_jobs(m)) < 0) {
1710 transaction_abort(m);
1714 if ((r = transaction_activate(m, mode, e)) < 0)
1717 log_debug("Enqueued job %s/%s as %u", unit->meta.id, job_type_to_string(type), (unsigned) ret->id);
1725 int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, bool override, DBusError *e, Job **_ret) {
1730 assert(type < _JOB_TYPE_MAX);
1732 assert(mode < _JOB_MODE_MAX);
1734 if ((r = manager_load_unit(m, name, NULL, NULL, &unit)) < 0)
1737 return manager_add_job(m, type, unit, mode, override, e, _ret);
1740 Job *manager_get_job(Manager *m, uint32_t id) {
1743 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
1746 Unit *manager_get_unit(Manager *m, const char *name) {
1750 return hashmap_get(m->units, name);
1753 unsigned manager_dispatch_load_queue(Manager *m) {
1759 /* Make sure we are not run recursively */
1760 if (m->dispatching_load_queue)
1763 m->dispatching_load_queue = true;
1765 /* Dispatches the load queue. Takes a unit from the queue and
1766 * tries to load its data until the queue is empty */
1768 while ((meta = m->load_queue)) {
1769 assert(meta->in_load_queue);
1771 unit_load((Unit*) meta);
1775 m->dispatching_load_queue = false;
1779 int manager_load_unit_prepare(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
1784 assert(name || path);
1786 /* This will prepare the unit for loading, but not actually
1787 * load anything from disk. */
1789 if (path && !is_path(path)) {
1790 dbus_set_error(e, BUS_ERROR_INVALID_PATH, "Path %s is not absolute.", path);
1795 name = file_name_from_path(path);
1797 if (!unit_name_is_valid(name, false)) {
1798 dbus_set_error(e, BUS_ERROR_INVALID_NAME, "Unit name %s is not valid.", name);
1802 if ((ret = manager_get_unit(m, name))) {
1807 if (!(ret = unit_new(m)))
1811 if (!(ret->meta.fragment_path = strdup(path))) {
1816 if ((r = unit_add_name(ret, name)) < 0) {
1821 unit_add_to_load_queue(ret);
1822 unit_add_to_dbus_queue(ret);
1823 unit_add_to_gc_queue(ret);
1831 int manager_load_unit(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
1836 /* This will load the service information files, but not actually
1837 * start any services or anything. */
1839 if ((r = manager_load_unit_prepare(m, name, path, e, _ret)) != 0)
1842 manager_dispatch_load_queue(m);
1845 *_ret = unit_follow_merge(*_ret);
1850 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
1857 HASHMAP_FOREACH(j, s->jobs, i)
1858 job_dump(j, f, prefix);
1861 void manager_dump_units(Manager *s, FILE *f, const char *prefix) {
1869 HASHMAP_FOREACH_KEY(u, t, s->units, i)
1870 if (u->meta.id == t)
1871 unit_dump(u, f, prefix);
1874 void manager_clear_jobs(Manager *m) {
1879 transaction_abort(m);
1881 while ((j = hashmap_first(m->jobs)))
1882 job_finish_and_invalidate(j, JOB_CANCELED);
1885 unsigned manager_dispatch_run_queue(Manager *m) {
1889 if (m->dispatching_run_queue)
1892 m->dispatching_run_queue = true;
1894 while ((j = m->run_queue)) {
1895 assert(j->installed);
1896 assert(j->in_run_queue);
1898 job_run_and_invalidate(j);
1902 m->dispatching_run_queue = false;
1906 unsigned manager_dispatch_dbus_queue(Manager *m) {
1913 if (m->dispatching_dbus_queue)
1916 m->dispatching_dbus_queue = true;
1918 while ((meta = m->dbus_unit_queue)) {
1919 assert(meta->in_dbus_queue);
1921 bus_unit_send_change_signal((Unit*) meta);
1925 while ((j = m->dbus_job_queue)) {
1926 assert(j->in_dbus_queue);
1928 bus_job_send_change_signal(j);
1932 m->dispatching_dbus_queue = false;
1936 static int manager_process_notify_fd(Manager *m) {
1943 struct msghdr msghdr;
1945 struct ucred *ucred;
1947 struct cmsghdr cmsghdr;
1948 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
1954 iovec.iov_base = buf;
1955 iovec.iov_len = sizeof(buf)-1;
1959 msghdr.msg_iov = &iovec;
1960 msghdr.msg_iovlen = 1;
1961 msghdr.msg_control = &control;
1962 msghdr.msg_controllen = sizeof(control);
1964 if ((n = recvmsg(m->notify_watch.fd, &msghdr, MSG_DONTWAIT)) <= 0) {
1968 if (errno == EAGAIN || errno == EINTR)
1974 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
1975 control.cmsghdr.cmsg_level != SOL_SOCKET ||
1976 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
1977 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
1978 log_warning("Received notify message without credentials. Ignoring.");
1982 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
1984 if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(ucred->pid))))
1985 if (!(u = cgroup_unit_by_pid(m, ucred->pid))) {
1986 log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid);
1990 assert((size_t) n < sizeof(buf));
1992 if (!(tags = strv_split(buf, "\n\r")))
1995 log_debug("Got notification message for unit %s", u->meta.id);
1997 if (UNIT_VTABLE(u)->notify_message)
1998 UNIT_VTABLE(u)->notify_message(u, ucred->pid, tags);
2006 static int manager_dispatch_sigchld(Manager *m) {
2016 /* First we call waitd() for a PID and do not reap the
2017 * zombie. That way we can still access /proc/$PID for
2018 * it while it is a zombie. */
2019 if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) {
2021 if (errno == ECHILD)
2033 if (si.si_code == CLD_EXITED || si.si_code == CLD_KILLED || si.si_code == CLD_DUMPED) {
2036 get_process_comm(si.si_pid, &name);
2037 log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si.si_pid, strna(name));
2041 /* Let's flush any message the dying child might still
2042 * have queued for us. This ensures that the process
2043 * still exists in /proc so that we can figure out
2044 * which cgroup and hence unit it belongs to. */
2045 if ((r = manager_process_notify_fd(m)) < 0)
2048 /* And now figure out the unit this belongs to */
2049 if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(si.si_pid))))
2050 u = cgroup_unit_by_pid(m, si.si_pid);
2052 /* And now, we actually reap the zombie. */
2053 if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) {
2060 if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED)
2063 log_debug("Child %lu died (code=%s, status=%i/%s)",
2064 (long unsigned) si.si_pid,
2065 sigchld_code_to_string(si.si_code),
2067 strna(si.si_code == CLD_EXITED
2068 ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL)
2069 : signal_to_string(si.si_status)));
2074 log_debug("Child %lu belongs to %s", (long unsigned) si.si_pid, u->meta.id);
2076 hashmap_remove(m->watch_pids, LONG_TO_PTR(si.si_pid));
2077 UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status);
2083 static int manager_start_target(Manager *m, const char *name, JobMode mode) {
2087 dbus_error_init(&error);
2089 log_debug("Activating special unit %s", name);
2091 if ((r = manager_add_job_by_name(m, JOB_START, name, mode, true, &error, NULL)) < 0)
2092 log_error("Failed to enqueue %s job: %s", name, bus_error(&error, r));
2094 dbus_error_free(&error);
2099 static int manager_process_signal_fd(Manager *m) {
2101 struct signalfd_siginfo sfsi;
2102 bool sigchld = false;
2107 if ((n = read(m->signal_watch.fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
2112 if (errno == EINTR || errno == EAGAIN)
2118 if (sfsi.ssi_pid > 0) {
2121 get_process_comm(sfsi.ssi_pid, &p);
2123 log_debug("Received SIG%s from PID %lu (%s).",
2124 signal_to_string(sfsi.ssi_signo),
2125 (unsigned long) sfsi.ssi_pid, strna(p));
2128 log_debug("Received SIG%s.", signal_to_string(sfsi.ssi_signo));
2130 switch (sfsi.ssi_signo) {
2137 if (m->running_as == MANAGER_SYSTEM) {
2138 /* This is for compatibility with the
2139 * original sysvinit */
2140 m->exit_code = MANAGER_REEXECUTE;
2147 if (m->running_as == MANAGER_SYSTEM) {
2148 manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE);
2152 /* Run the exit target if there is one, if not, just exit. */
2153 if (manager_start_target(m, SPECIAL_EXIT_TARGET, JOB_REPLACE) < 0) {
2154 m->exit_code = MANAGER_EXIT;
2161 if (m->running_as == MANAGER_SYSTEM)
2162 manager_start_target(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE);
2164 /* This is a nop on non-init */
2168 if (m->running_as == MANAGER_SYSTEM)
2169 manager_start_target(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE);
2171 /* This is a nop on non-init */
2177 u = manager_get_unit(m, SPECIAL_DBUS_SERVICE);
2179 if (!u || UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) {
2180 log_info("Trying to reconnect to bus...");
2184 if (!u || !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) {
2185 log_info("Loading D-Bus service...");
2186 manager_start_target(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE);
2197 if (!(f = open_memstream(&dump, &size))) {
2198 log_warning("Failed to allocate memory stream.");
2202 manager_dump_units(m, f, "\t");
2203 manager_dump_jobs(m, f, "\t");
2208 log_warning("Failed to write status stream");
2213 log_dump(LOG_INFO, dump);
2220 m->exit_code = MANAGER_RELOAD;
2225 /* Starting SIGRTMIN+0 */
2226 static const char * const target_table[] = {
2227 [0] = SPECIAL_DEFAULT_TARGET,
2228 [1] = SPECIAL_RESCUE_TARGET,
2229 [2] = SPECIAL_EMERGENCY_TARGET,
2230 [3] = SPECIAL_HALT_TARGET,
2231 [4] = SPECIAL_POWEROFF_TARGET,
2232 [5] = SPECIAL_REBOOT_TARGET,
2233 [6] = SPECIAL_KEXEC_TARGET
2236 /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */
2237 static const ManagerExitCode code_table[] = {
2239 [1] = MANAGER_POWEROFF,
2240 [2] = MANAGER_REBOOT,
2244 if ((int) sfsi.ssi_signo >= SIGRTMIN+0 &&
2245 (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) {
2246 int idx = (int) sfsi.ssi_signo - SIGRTMIN;
2247 manager_start_target(m, target_table[idx],
2248 (idx == 1 || idx == 2) ? JOB_ISOLATE : JOB_REPLACE);
2252 if ((int) sfsi.ssi_signo >= SIGRTMIN+13 &&
2253 (int) sfsi.ssi_signo < SIGRTMIN+13+(int) ELEMENTSOF(code_table)) {
2254 m->exit_code = code_table[sfsi.ssi_signo - SIGRTMIN - 13];
2258 switch (sfsi.ssi_signo - SIGRTMIN) {
2261 log_debug("Enabling showing of status.");
2262 manager_set_show_status(m, true);
2266 log_debug("Disabling showing of status.");
2267 manager_set_show_status(m, false);
2271 log_set_max_level(LOG_DEBUG);
2272 log_notice("Setting log level to debug.");
2276 log_set_max_level(LOG_INFO);
2277 log_notice("Setting log level to info.");
2281 log_set_target(LOG_TARGET_CONSOLE);
2282 log_notice("Setting log target to console.");
2286 log_set_target(LOG_TARGET_KMSG);
2287 log_notice("Setting log target to kmsg.");
2291 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
2292 log_notice("Setting log target to syslog-or-kmsg.");
2296 log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi.ssi_signo));
2303 return manager_dispatch_sigchld(m);
2308 static int process_event(Manager *m, struct epoll_event *ev) {
2315 assert_se(w = ev->data.ptr);
2317 if (w->type == WATCH_INVALID)
2324 /* An incoming signal? */
2325 if (ev->events != EPOLLIN)
2328 if ((r = manager_process_signal_fd(m)) < 0)
2335 /* An incoming daemon notification event? */
2336 if (ev->events != EPOLLIN)
2339 if ((r = manager_process_notify_fd(m)) < 0)
2346 /* Some fd event, to be dispatched to the units */
2347 UNIT_VTABLE(w->data.unit)->fd_event(w->data.unit, w->fd, ev->events, w);
2350 case WATCH_UNIT_TIMER:
2351 case WATCH_JOB_TIMER: {
2355 /* Some timer event, to be dispatched to the units */
2356 if ((k = read(w->fd, &v, sizeof(v))) != sizeof(v)) {
2358 if (k < 0 && (errno == EINTR || errno == EAGAIN))
2361 return k < 0 ? -errno : -EIO;
2364 if (w->type == WATCH_UNIT_TIMER)
2365 UNIT_VTABLE(w->data.unit)->timer_event(w->data.unit, v, w);
2367 job_timer_event(w->data.job, v, w);
2372 /* Some mount table change, intended for the mount subsystem */
2373 mount_fd_event(m, ev->events);
2377 /* Some swap table change, intended for the swap subsystem */
2378 swap_fd_event(m, ev->events);
2382 /* Some notification from udev, intended for the device subsystem */
2383 device_fd_event(m, ev->events);
2386 case WATCH_DBUS_WATCH:
2387 bus_watch_event(m, w, ev->events);
2390 case WATCH_DBUS_TIMEOUT:
2391 bus_timeout_event(m, w, ev->events);
2395 log_error("event type=%i", w->type);
2396 assert_not_reached("Unknown epoll event type.");
2402 int manager_loop(Manager *m) {
2405 RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 50000);
2408 m->exit_code = MANAGER_RUNNING;
2410 /* Release the path cache */
2411 set_free_free(m->unit_path_cache);
2412 m->unit_path_cache = NULL;
2414 manager_check_finished(m);
2416 /* There might still be some zombies hanging around from
2417 * before we were exec()'ed. Leat's reap them */
2418 if ((r = manager_dispatch_sigchld(m)) < 0)
2421 while (m->exit_code == MANAGER_RUNNING) {
2422 struct epoll_event event;
2425 if (!ratelimit_test(&rl)) {
2426 /* Yay, something is going seriously wrong, pause a little */
2427 log_warning("Looping too fast. Throttling execution a little.");
2431 if (manager_dispatch_load_queue(m) > 0)
2434 if (manager_dispatch_run_queue(m) > 0)
2437 if (bus_dispatch(m) > 0)
2440 if (manager_dispatch_cleanup_queue(m) > 0)
2443 if (manager_dispatch_gc_queue(m) > 0)
2446 if (manager_dispatch_dbus_queue(m) > 0)
2449 if (swap_dispatch_reload(m) > 0)
2452 if ((n = epoll_wait(m->epoll_fd, &event, 1, -1)) < 0) {
2462 if ((r = process_event(m, &event)) < 0)
2466 return m->exit_code;
2469 int manager_get_unit_from_dbus_path(Manager *m, const char *s, Unit **_u) {
2477 if (!startswith(s, "/org/freedesktop/systemd1/unit/"))
2480 if (!(n = bus_path_unescape(s+31)))
2483 u = manager_get_unit(m, n);
2494 int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
2503 if (!startswith(s, "/org/freedesktop/systemd1/job/"))
2506 if ((r = safe_atou(s + 30, &id)) < 0)
2509 if (!(j = manager_get_job(m, id)))
2517 void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
2522 if (m->audit_fd < 0)
2525 /* Don't generate audit events if the service was already
2526 * started and we're just deserializing */
2527 if (m->n_reloading > 0)
2530 if (m->running_as != MANAGER_SYSTEM)
2533 if (u->meta.type != UNIT_SERVICE)
2536 if (!(p = unit_name_to_prefix_and_instance(u->meta.id))) {
2537 log_error("Failed to allocate unit name for audit message: %s", strerror(ENOMEM));
2541 if (audit_log_user_comm_message(m->audit_fd, type, "", p, NULL, NULL, NULL, success) < 0) {
2542 log_warning("Failed to send audit message: %m");
2544 if (errno == EPERM) {
2545 /* We aren't allowed to send audit messages?
2546 * Then let's not retry again, to avoid
2547 * spamming the user with the same and same
2548 * messages over and over. */
2550 audit_close(m->audit_fd);
2560 void manager_send_unit_plymouth(Manager *m, Unit *u) {
2562 union sockaddr_union sa;
2564 char *message = NULL;
2566 /* Don't generate plymouth events if the service was already
2567 * started and we're just deserializing */
2568 if (m->n_reloading > 0)
2571 if (m->running_as != MANAGER_SYSTEM)
2574 if (u->meta.type != UNIT_SERVICE &&
2575 u->meta.type != UNIT_MOUNT &&
2576 u->meta.type != UNIT_SWAP)
2579 /* We set SOCK_NONBLOCK here so that we rather drop the
2580 * message then wait for plymouth */
2581 if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
2582 log_error("socket() failed: %m");
2587 sa.sa.sa_family = AF_UNIX;
2588 strncpy(sa.un.sun_path+1, "/org/freedesktop/plymouthd", sizeof(sa.un.sun_path)-1);
2589 if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
2591 if (errno != EPIPE &&
2594 errno != ECONNREFUSED &&
2595 errno != ECONNRESET &&
2596 errno != ECONNABORTED)
2597 log_error("connect() failed: %m");
2602 if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->meta.id) + 1), u->meta.id, &n) < 0) {
2603 log_error("Out of memory");
2608 if (write(fd, message, n + 1) != n + 1) {
2610 if (errno != EPIPE &&
2613 errno != ECONNREFUSED &&
2614 errno != ECONNRESET &&
2615 errno != ECONNABORTED)
2616 log_error("Failed to write Plymouth message: %m");
2623 close_nointr_nofail(fd);
2628 void manager_dispatch_bus_name_owner_changed(
2631 const char* old_owner,
2632 const char *new_owner) {
2639 if (!(u = hashmap_get(m->watch_bus, name)))
2642 UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner);
2645 void manager_dispatch_bus_query_pid_done(
2656 if (!(u = hashmap_get(m->watch_bus, name)))
2659 UNIT_VTABLE(u)->bus_query_pid_done(u, name, pid);
2662 int manager_open_serialization(Manager *m, FILE **_f) {
2670 if (m->running_as == MANAGER_SYSTEM)
2671 asprintf(&path, "/run/systemd/dump-%lu-XXXXXX", (unsigned long) getpid());
2673 asprintf(&path, "/tmp/systemd-dump-%lu-XXXXXX", (unsigned long) getpid());
2678 saved_umask = umask(0077);
2679 fd = mkostemp(path, O_RDWR|O_CLOEXEC);
2689 log_debug("Serializing state to %s", path);
2692 if (!(f = fdopen(fd, "w+")))
2700 int manager_serialize(Manager *m, FILE *f, FDSet *fds) {
2712 fprintf(f, "current-job-id=%i\n", m->current_job_id);
2713 fprintf(f, "taint-usr=%s\n", yes_no(m->taint_usr));
2715 dual_timestamp_serialize(f, "initrd-timestamp", &m->initrd_timestamp);
2716 dual_timestamp_serialize(f, "startup-timestamp", &m->startup_timestamp);
2717 dual_timestamp_serialize(f, "finish-timestamp", &m->finish_timestamp);
2721 HASHMAP_FOREACH_KEY(u, t, m->units, i) {
2722 if (u->meta.id != t)
2725 if (!unit_can_serialize(u))
2729 fputs(u->meta.id, f);
2732 if ((r = unit_serialize(u, f, fds)) < 0) {
2738 assert(m->n_reloading > 0);
2744 r = bus_fdset_add_all(m, fds);
2751 int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
2757 log_debug("Deserializing state...");
2762 char line[LINE_MAX], *l;
2764 if (!fgets(line, sizeof(line), f)) {
2779 if (startswith(l, "current-job-id=")) {
2782 if (safe_atou32(l+15, &id) < 0)
2783 log_debug("Failed to parse current job id value %s", l+15);
2785 m->current_job_id = MAX(m->current_job_id, id);
2786 } else if (startswith(l, "taint-usr=")) {
2789 if ((b = parse_boolean(l+10)) < 0)
2790 log_debug("Failed to parse taint /usr flag %s", l+10);
2792 m->taint_usr = m->taint_usr || b;
2793 } else if (startswith(l, "initrd-timestamp="))
2794 dual_timestamp_deserialize(l+17, &m->initrd_timestamp);
2795 else if (startswith(l, "startup-timestamp="))
2796 dual_timestamp_deserialize(l+18, &m->startup_timestamp);
2797 else if (startswith(l, "finish-timestamp="))
2798 dual_timestamp_deserialize(l+17, &m->finish_timestamp);
2800 log_debug("Unknown serialization item '%s'", l);
2805 char name[UNIT_NAME_MAX+2];
2808 if (!fgets(name, sizeof(name), f)) {
2819 if ((r = manager_load_unit(m, strstrip(name), NULL, NULL, &u)) < 0)
2822 if ((r = unit_deserialize(u, f, fds)) < 0)
2832 assert(m->n_reloading > 0);
2838 int manager_reload(Manager *m) {
2845 if ((r = manager_open_serialization(m, &f)) < 0)
2850 if (!(fds = fdset_new())) {
2856 if ((r = manager_serialize(m, f, fds)) < 0) {
2861 if (fseeko(f, 0, SEEK_SET) < 0) {
2867 /* From here on there is no way back. */
2868 manager_clear_jobs_and_units(m);
2869 manager_undo_generators(m);
2871 /* Find new unit paths */
2872 lookup_paths_free(&m->lookup_paths);
2873 if ((q = lookup_paths_init(&m->lookup_paths, m->running_as, true)) < 0)
2876 manager_run_generators(m);
2878 manager_build_unit_path_cache(m);
2880 /* First, enumerate what we can from all config files */
2881 if ((q = manager_enumerate(m)) < 0)
2884 /* Second, deserialize our stored data */
2885 if ((q = manager_deserialize(m, f, fds)) < 0)
2891 /* Third, fire things up! */
2892 if ((q = manager_coldplug(m)) < 0)
2895 assert(m->n_reloading > 0);
2908 bool manager_is_booting_or_shutting_down(Manager *m) {
2913 /* Is the initial job still around? */
2914 if (manager_get_job(m, 1))
2917 /* Is there a job for the shutdown target? */
2918 u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET);
2920 return !!u->meta.job;
2925 void manager_reset_failed(Manager *m) {
2931 HASHMAP_FOREACH(u, m->units, i)
2932 unit_reset_failed(u);
2935 bool manager_unit_pending_inactive(Manager *m, const char *name) {
2941 /* Returns true if the unit is inactive or going down */
2942 if (!(u = manager_get_unit(m, name)))
2945 return unit_pending_inactive(u);
2948 void manager_check_finished(Manager *m) {
2949 char userspace[FORMAT_TIMESPAN_MAX], initrd[FORMAT_TIMESPAN_MAX], kernel[FORMAT_TIMESPAN_MAX], sum[FORMAT_TIMESPAN_MAX];
2950 usec_t kernel_usec = 0, initrd_usec = 0, userspace_usec = 0, total_usec = 0;
2954 if (dual_timestamp_is_set(&m->finish_timestamp))
2957 if (hashmap_size(m->jobs) > 0)
2960 dual_timestamp_get(&m->finish_timestamp);
2962 if (m->running_as == MANAGER_SYSTEM && detect_container(NULL) <= 0) {
2964 userspace_usec = m->finish_timestamp.monotonic - m->startup_timestamp.monotonic;
2965 total_usec = m->finish_timestamp.monotonic;
2967 if (dual_timestamp_is_set(&m->initrd_timestamp)) {
2969 kernel_usec = m->initrd_timestamp.monotonic;
2970 initrd_usec = m->startup_timestamp.monotonic - m->initrd_timestamp.monotonic;
2972 log_info("Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.",
2973 format_timespan(kernel, sizeof(kernel), kernel_usec),
2974 format_timespan(initrd, sizeof(initrd), initrd_usec),
2975 format_timespan(userspace, sizeof(userspace), userspace_usec),
2976 format_timespan(sum, sizeof(sum), total_usec));
2978 kernel_usec = m->startup_timestamp.monotonic;
2981 log_info("Startup finished in %s (kernel) + %s (userspace) = %s.",
2982 format_timespan(kernel, sizeof(kernel), kernel_usec),
2983 format_timespan(userspace, sizeof(userspace), userspace_usec),
2984 format_timespan(sum, sizeof(sum), total_usec));
2987 userspace_usec = initrd_usec = kernel_usec = 0;
2988 total_usec = m->finish_timestamp.monotonic - m->startup_timestamp.monotonic;
2990 log_debug("Startup finished in %s.",
2991 format_timespan(sum, sizeof(sum), total_usec));
2994 bus_broadcast_finished(m, kernel_usec, initrd_usec, userspace_usec, total_usec);
2997 "READY=1\nSTATUS=Startup finished in %s.",
2998 format_timespan(sum, sizeof(sum), total_usec));
3001 void manager_run_generators(Manager *m) {
3003 const char *generator_path;
3004 const char *argv[3];
3009 generator_path = m->running_as == MANAGER_SYSTEM ? SYSTEM_GENERATOR_PATH : USER_GENERATOR_PATH;
3010 if (!(d = opendir(generator_path))) {
3012 if (errno == ENOENT)
3015 log_error("Failed to enumerate generator directory: %m");
3019 if (!m->generator_unit_path) {
3021 char user_path[] = "/tmp/systemd-generator-XXXXXX";
3023 if (m->running_as == MANAGER_SYSTEM && getpid() == 1) {
3024 p = "/run/systemd/generator";
3026 if (mkdir_p(p, 0755) < 0) {
3027 log_error("Failed to create generator directory: %m");
3032 if (!(p = mkdtemp(user_path))) {
3033 log_error("Failed to create generator directory: %m");
3038 if (!(m->generator_unit_path = strdup(p))) {
3039 log_error("Failed to allocate generator unit path.");
3044 argv[0] = NULL; /* Leave this empty, execute_directory() will fill something in */
3045 argv[1] = m->generator_unit_path;
3049 execute_directory(generator_path, d, (char**) argv);
3052 if (rmdir(m->generator_unit_path) >= 0) {
3053 /* Uh? we were able to remove this dir? I guess that
3054 * means the directory was empty, hence let's shortcut
3057 free(m->generator_unit_path);
3058 m->generator_unit_path = NULL;
3062 if (!strv_find(m->lookup_paths.unit_path, m->generator_unit_path)) {
3065 if (!(l = strv_append(m->lookup_paths.unit_path, m->generator_unit_path))) {
3066 log_error("Failed to add generator directory to unit search path: %m");
3070 strv_free(m->lookup_paths.unit_path);
3071 m->lookup_paths.unit_path = l;
3073 log_debug("Added generator unit path %s to search path.", m->generator_unit_path);
3081 void manager_undo_generators(Manager *m) {
3084 if (!m->generator_unit_path)
3087 strv_remove(m->lookup_paths.unit_path, m->generator_unit_path);
3088 rm_rf(m->generator_unit_path, false, true, false);
3090 free(m->generator_unit_path);
3091 m->generator_unit_path = NULL;
3094 int manager_set_default_controllers(Manager *m, char **controllers) {
3099 if (!(l = strv_copy(controllers)))
3102 strv_free(m->default_controllers);
3103 m->default_controllers = l;
3108 void manager_recheck_syslog(Manager *m) {
3113 if (m->running_as != MANAGER_SYSTEM)
3116 if ((u = manager_get_unit(m, SPECIAL_SYSLOG_SOCKET))) {
3119 state = SOCKET(u)->state;
3121 if (state != SOCKET_DEAD &&
3122 state != SOCKET_FAILED &&
3123 state != SOCKET_RUNNING) {
3125 /* Hmm, the socket is not set up, or is still
3126 * listening, let's better not try to use
3127 * it. Note that we have no problem if the
3128 * socket is completely down, since there
3129 * might be a foreign /dev/log socket around
3130 * and we want to make use of that.
3138 if ((u = manager_get_unit(m, SPECIAL_SYSLOG_TARGET)))
3139 if (TARGET(u)->state != TARGET_ACTIVE) {
3144 /* Hmm, OK, so the socket is either fully up, or fully down,
3145 * and the target is up, then let's make use of the socket */
3149 void manager_set_show_status(Manager *m, bool b) {
3152 if (m->running_as != MANAGER_SYSTEM)
3158 touch("/run/systemd/show-status");
3160 unlink("/run/systemd/show-status");
3163 bool manager_get_show_status(Manager *m) {
3166 if (m->running_as != MANAGER_SYSTEM)
3172 /* If Plymouth is running make sure we show the status, so
3173 * that there's something nice to see when people press Esc */
3175 return plymouth_running();
3178 static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = {
3179 [MANAGER_SYSTEM] = "system",
3180 [MANAGER_USER] = "user"
3183 DEFINE_STRING_TABLE_LOOKUP(manager_running_as, ManagerRunningAs);