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));
1615 if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) {
1617 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRED_BY], i)
1618 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1627 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_BOUND_BY], i)
1628 if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) {
1638 if (type == JOB_RELOAD || type == JOB_RELOAD_OR_START) {
1640 SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_PROPAGATE_RELOAD_TO], i) {
1641 r = transaction_add_job_and_dependencies(m, JOB_RELOAD, dep, ret, false, override, false, false, ignore_order, e, NULL);
1644 log_warning("Cannot add dependency reload job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r));
1652 /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */
1664 static int transaction_add_isolate_jobs(Manager *m) {
1672 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
1674 /* ignore aliases */
1675 if (u->meta.id != k)
1678 if (u->meta.ignore_on_isolate)
1681 /* No need to stop inactive jobs */
1682 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) && !u->meta.job)
1685 /* Is there already something listed for this? */
1686 if (hashmap_get(m->transaction_jobs, u))
1689 if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, u, NULL, true, false, false, false, false, NULL, NULL)) < 0)
1690 log_warning("Cannot add isolate job for unit %s, ignoring: %s", u->meta.id, strerror(-r));
1696 int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool override, DBusError *e, Job **_ret) {
1701 assert(type < _JOB_TYPE_MAX);
1703 assert(mode < _JOB_MODE_MAX);
1705 if (mode == JOB_ISOLATE && type != JOB_START) {
1706 dbus_set_error(e, BUS_ERROR_INVALID_JOB_MODE, "Isolate is only valid for start.");
1710 if (mode == JOB_ISOLATE && !unit->meta.allow_isolate) {
1711 dbus_set_error(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated.");
1715 log_debug("Trying to enqueue job %s/%s/%s", unit->meta.id, job_type_to_string(type), job_mode_to_string(mode));
1717 if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, override, false,
1718 mode == JOB_IGNORE_DEPENDENCIES || mode == JOB_IGNORE_REQUIREMENTS,
1719 mode == JOB_IGNORE_DEPENDENCIES, e, &ret)) < 0) {
1720 transaction_abort(m);
1724 if (mode == JOB_ISOLATE)
1725 if ((r = transaction_add_isolate_jobs(m)) < 0) {
1726 transaction_abort(m);
1730 if ((r = transaction_activate(m, mode, e)) < 0)
1733 log_debug("Enqueued job %s/%s as %u", unit->meta.id, job_type_to_string(type), (unsigned) ret->id);
1741 int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, bool override, DBusError *e, Job **_ret) {
1746 assert(type < _JOB_TYPE_MAX);
1748 assert(mode < _JOB_MODE_MAX);
1750 if ((r = manager_load_unit(m, name, NULL, NULL, &unit)) < 0)
1753 return manager_add_job(m, type, unit, mode, override, e, _ret);
1756 Job *manager_get_job(Manager *m, uint32_t id) {
1759 return hashmap_get(m->jobs, UINT32_TO_PTR(id));
1762 Unit *manager_get_unit(Manager *m, const char *name) {
1766 return hashmap_get(m->units, name);
1769 unsigned manager_dispatch_load_queue(Manager *m) {
1775 /* Make sure we are not run recursively */
1776 if (m->dispatching_load_queue)
1779 m->dispatching_load_queue = true;
1781 /* Dispatches the load queue. Takes a unit from the queue and
1782 * tries to load its data until the queue is empty */
1784 while ((meta = m->load_queue)) {
1785 assert(meta->in_load_queue);
1787 unit_load((Unit*) meta);
1791 m->dispatching_load_queue = false;
1795 int manager_load_unit_prepare(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
1800 assert(name || path);
1802 /* This will prepare the unit for loading, but not actually
1803 * load anything from disk. */
1805 if (path && !is_path(path)) {
1806 dbus_set_error(e, BUS_ERROR_INVALID_PATH, "Path %s is not absolute.", path);
1811 name = file_name_from_path(path);
1813 if (!unit_name_is_valid(name, false)) {
1814 dbus_set_error(e, BUS_ERROR_INVALID_NAME, "Unit name %s is not valid.", name);
1818 if ((ret = manager_get_unit(m, name))) {
1823 if (!(ret = unit_new(m)))
1827 if (!(ret->meta.fragment_path = strdup(path))) {
1832 if ((r = unit_add_name(ret, name)) < 0) {
1837 unit_add_to_load_queue(ret);
1838 unit_add_to_dbus_queue(ret);
1839 unit_add_to_gc_queue(ret);
1847 int manager_load_unit(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
1852 /* This will load the service information files, but not actually
1853 * start any services or anything. */
1855 if ((r = manager_load_unit_prepare(m, name, path, e, _ret)) != 0)
1858 manager_dispatch_load_queue(m);
1861 *_ret = unit_follow_merge(*_ret);
1866 void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) {
1873 HASHMAP_FOREACH(j, s->jobs, i)
1874 job_dump(j, f, prefix);
1877 void manager_dump_units(Manager *s, FILE *f, const char *prefix) {
1885 HASHMAP_FOREACH_KEY(u, t, s->units, i)
1886 if (u->meta.id == t)
1887 unit_dump(u, f, prefix);
1890 void manager_clear_jobs(Manager *m) {
1895 transaction_abort(m);
1897 while ((j = hashmap_first(m->jobs)))
1898 job_finish_and_invalidate(j, JOB_CANCELED);
1901 unsigned manager_dispatch_run_queue(Manager *m) {
1905 if (m->dispatching_run_queue)
1908 m->dispatching_run_queue = true;
1910 while ((j = m->run_queue)) {
1911 assert(j->installed);
1912 assert(j->in_run_queue);
1914 job_run_and_invalidate(j);
1918 m->dispatching_run_queue = false;
1922 unsigned manager_dispatch_dbus_queue(Manager *m) {
1929 if (m->dispatching_dbus_queue)
1932 m->dispatching_dbus_queue = true;
1934 while ((meta = m->dbus_unit_queue)) {
1935 assert(meta->in_dbus_queue);
1937 bus_unit_send_change_signal((Unit*) meta);
1941 while ((j = m->dbus_job_queue)) {
1942 assert(j->in_dbus_queue);
1944 bus_job_send_change_signal(j);
1948 m->dispatching_dbus_queue = false;
1952 static int manager_process_notify_fd(Manager *m) {
1959 struct msghdr msghdr;
1961 struct ucred *ucred;
1963 struct cmsghdr cmsghdr;
1964 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
1970 iovec.iov_base = buf;
1971 iovec.iov_len = sizeof(buf)-1;
1975 msghdr.msg_iov = &iovec;
1976 msghdr.msg_iovlen = 1;
1977 msghdr.msg_control = &control;
1978 msghdr.msg_controllen = sizeof(control);
1980 if ((n = recvmsg(m->notify_watch.fd, &msghdr, MSG_DONTWAIT)) <= 0) {
1984 if (errno == EAGAIN || errno == EINTR)
1990 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
1991 control.cmsghdr.cmsg_level != SOL_SOCKET ||
1992 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
1993 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
1994 log_warning("Received notify message without credentials. Ignoring.");
1998 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
2000 if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(ucred->pid))))
2001 if (!(u = cgroup_unit_by_pid(m, ucred->pid))) {
2002 log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid);
2006 assert((size_t) n < sizeof(buf));
2008 if (!(tags = strv_split(buf, "\n\r")))
2011 log_debug("Got notification message for unit %s", u->meta.id);
2013 if (UNIT_VTABLE(u)->notify_message)
2014 UNIT_VTABLE(u)->notify_message(u, ucred->pid, tags);
2022 static int manager_dispatch_sigchld(Manager *m) {
2032 /* First we call waitd() for a PID and do not reap the
2033 * zombie. That way we can still access /proc/$PID for
2034 * it while it is a zombie. */
2035 if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) {
2037 if (errno == ECHILD)
2049 if (si.si_code == CLD_EXITED || si.si_code == CLD_KILLED || si.si_code == CLD_DUMPED) {
2052 get_process_comm(si.si_pid, &name);
2053 log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si.si_pid, strna(name));
2057 /* Let's flush any message the dying child might still
2058 * have queued for us. This ensures that the process
2059 * still exists in /proc so that we can figure out
2060 * which cgroup and hence unit it belongs to. */
2061 if ((r = manager_process_notify_fd(m)) < 0)
2064 /* And now figure out the unit this belongs to */
2065 if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(si.si_pid))))
2066 u = cgroup_unit_by_pid(m, si.si_pid);
2068 /* And now, we actually reap the zombie. */
2069 if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) {
2076 if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED)
2079 log_debug("Child %lu died (code=%s, status=%i/%s)",
2080 (long unsigned) si.si_pid,
2081 sigchld_code_to_string(si.si_code),
2083 strna(si.si_code == CLD_EXITED
2084 ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL)
2085 : signal_to_string(si.si_status)));
2090 log_debug("Child %lu belongs to %s", (long unsigned) si.si_pid, u->meta.id);
2092 hashmap_remove(m->watch_pids, LONG_TO_PTR(si.si_pid));
2093 UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status);
2099 static int manager_start_target(Manager *m, const char *name, JobMode mode) {
2103 dbus_error_init(&error);
2105 log_debug("Activating special unit %s", name);
2107 if ((r = manager_add_job_by_name(m, JOB_START, name, mode, true, &error, NULL)) < 0)
2108 log_error("Failed to enqueue %s job: %s", name, bus_error(&error, r));
2110 dbus_error_free(&error);
2115 static int manager_process_signal_fd(Manager *m) {
2117 struct signalfd_siginfo sfsi;
2118 bool sigchld = false;
2123 if ((n = read(m->signal_watch.fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
2128 if (errno == EINTR || errno == EAGAIN)
2134 if (sfsi.ssi_pid > 0) {
2137 get_process_comm(sfsi.ssi_pid, &p);
2139 log_debug("Received SIG%s from PID %lu (%s).",
2140 signal_to_string(sfsi.ssi_signo),
2141 (unsigned long) sfsi.ssi_pid, strna(p));
2144 log_debug("Received SIG%s.", signal_to_string(sfsi.ssi_signo));
2146 switch (sfsi.ssi_signo) {
2153 if (m->running_as == MANAGER_SYSTEM) {
2154 /* This is for compatibility with the
2155 * original sysvinit */
2156 m->exit_code = MANAGER_REEXECUTE;
2163 if (m->running_as == MANAGER_SYSTEM) {
2164 manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE);
2168 /* Run the exit target if there is one, if not, just exit. */
2169 if (manager_start_target(m, SPECIAL_EXIT_TARGET, JOB_REPLACE) < 0) {
2170 m->exit_code = MANAGER_EXIT;
2177 if (m->running_as == MANAGER_SYSTEM)
2178 manager_start_target(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE);
2180 /* This is a nop on non-init */
2184 if (m->running_as == MANAGER_SYSTEM)
2185 manager_start_target(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE);
2187 /* This is a nop on non-init */
2193 u = manager_get_unit(m, SPECIAL_DBUS_SERVICE);
2195 if (!u || UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) {
2196 log_info("Trying to reconnect to bus...");
2200 if (!u || !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) {
2201 log_info("Loading D-Bus service...");
2202 manager_start_target(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE);
2213 if (!(f = open_memstream(&dump, &size))) {
2214 log_warning("Failed to allocate memory stream.");
2218 manager_dump_units(m, f, "\t");
2219 manager_dump_jobs(m, f, "\t");
2224 log_warning("Failed to write status stream");
2229 log_dump(LOG_INFO, dump);
2236 m->exit_code = MANAGER_RELOAD;
2241 /* Starting SIGRTMIN+0 */
2242 static const char * const target_table[] = {
2243 [0] = SPECIAL_DEFAULT_TARGET,
2244 [1] = SPECIAL_RESCUE_TARGET,
2245 [2] = SPECIAL_EMERGENCY_TARGET,
2246 [3] = SPECIAL_HALT_TARGET,
2247 [4] = SPECIAL_POWEROFF_TARGET,
2248 [5] = SPECIAL_REBOOT_TARGET,
2249 [6] = SPECIAL_KEXEC_TARGET
2252 /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */
2253 static const ManagerExitCode code_table[] = {
2255 [1] = MANAGER_POWEROFF,
2256 [2] = MANAGER_REBOOT,
2260 if ((int) sfsi.ssi_signo >= SIGRTMIN+0 &&
2261 (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) {
2262 int idx = (int) sfsi.ssi_signo - SIGRTMIN;
2263 manager_start_target(m, target_table[idx],
2264 (idx == 1 || idx == 2) ? JOB_ISOLATE : JOB_REPLACE);
2268 if ((int) sfsi.ssi_signo >= SIGRTMIN+13 &&
2269 (int) sfsi.ssi_signo < SIGRTMIN+13+(int) ELEMENTSOF(code_table)) {
2270 m->exit_code = code_table[sfsi.ssi_signo - SIGRTMIN - 13];
2274 switch (sfsi.ssi_signo - SIGRTMIN) {
2277 log_debug("Enabling showing of status.");
2278 manager_set_show_status(m, true);
2282 log_debug("Disabling showing of status.");
2283 manager_set_show_status(m, false);
2287 log_set_max_level(LOG_DEBUG);
2288 log_notice("Setting log level to debug.");
2292 log_set_max_level(LOG_INFO);
2293 log_notice("Setting log level to info.");
2297 log_set_target(LOG_TARGET_CONSOLE);
2298 log_notice("Setting log target to console.");
2302 log_set_target(LOG_TARGET_KMSG);
2303 log_notice("Setting log target to kmsg.");
2307 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
2308 log_notice("Setting log target to syslog-or-kmsg.");
2312 log_warning("Got unhandled signal <%s>.", signal_to_string(sfsi.ssi_signo));
2319 return manager_dispatch_sigchld(m);
2324 static int process_event(Manager *m, struct epoll_event *ev) {
2331 assert_se(w = ev->data.ptr);
2333 if (w->type == WATCH_INVALID)
2340 /* An incoming signal? */
2341 if (ev->events != EPOLLIN)
2344 if ((r = manager_process_signal_fd(m)) < 0)
2351 /* An incoming daemon notification event? */
2352 if (ev->events != EPOLLIN)
2355 if ((r = manager_process_notify_fd(m)) < 0)
2362 /* Some fd event, to be dispatched to the units */
2363 UNIT_VTABLE(w->data.unit)->fd_event(w->data.unit, w->fd, ev->events, w);
2366 case WATCH_UNIT_TIMER:
2367 case WATCH_JOB_TIMER: {
2371 /* Some timer event, to be dispatched to the units */
2372 if ((k = read(w->fd, &v, sizeof(v))) != sizeof(v)) {
2374 if (k < 0 && (errno == EINTR || errno == EAGAIN))
2377 return k < 0 ? -errno : -EIO;
2380 if (w->type == WATCH_UNIT_TIMER)
2381 UNIT_VTABLE(w->data.unit)->timer_event(w->data.unit, v, w);
2383 job_timer_event(w->data.job, v, w);
2388 /* Some mount table change, intended for the mount subsystem */
2389 mount_fd_event(m, ev->events);
2393 /* Some swap table change, intended for the swap subsystem */
2394 swap_fd_event(m, ev->events);
2398 /* Some notification from udev, intended for the device subsystem */
2399 device_fd_event(m, ev->events);
2402 case WATCH_DBUS_WATCH:
2403 bus_watch_event(m, w, ev->events);
2406 case WATCH_DBUS_TIMEOUT:
2407 bus_timeout_event(m, w, ev->events);
2411 log_error("event type=%i", w->type);
2412 assert_not_reached("Unknown epoll event type.");
2418 int manager_loop(Manager *m) {
2421 RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 50000);
2424 m->exit_code = MANAGER_RUNNING;
2426 /* Release the path cache */
2427 set_free_free(m->unit_path_cache);
2428 m->unit_path_cache = NULL;
2430 manager_check_finished(m);
2432 /* There might still be some zombies hanging around from
2433 * before we were exec()'ed. Leat's reap them */
2434 if ((r = manager_dispatch_sigchld(m)) < 0)
2437 while (m->exit_code == MANAGER_RUNNING) {
2438 struct epoll_event event;
2441 if (!ratelimit_test(&rl)) {
2442 /* Yay, something is going seriously wrong, pause a little */
2443 log_warning("Looping too fast. Throttling execution a little.");
2447 if (manager_dispatch_load_queue(m) > 0)
2450 if (manager_dispatch_run_queue(m) > 0)
2453 if (bus_dispatch(m) > 0)
2456 if (manager_dispatch_cleanup_queue(m) > 0)
2459 if (manager_dispatch_gc_queue(m) > 0)
2462 if (manager_dispatch_dbus_queue(m) > 0)
2465 if (swap_dispatch_reload(m) > 0)
2468 if ((n = epoll_wait(m->epoll_fd, &event, 1, -1)) < 0) {
2478 if ((r = process_event(m, &event)) < 0)
2482 return m->exit_code;
2485 int manager_get_unit_from_dbus_path(Manager *m, const char *s, Unit **_u) {
2493 if (!startswith(s, "/org/freedesktop/systemd1/unit/"))
2496 if (!(n = bus_path_unescape(s+31)))
2499 u = manager_get_unit(m, n);
2510 int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
2519 if (!startswith(s, "/org/freedesktop/systemd1/job/"))
2522 if ((r = safe_atou(s + 30, &id)) < 0)
2525 if (!(j = manager_get_job(m, id)))
2533 void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
2538 if (m->audit_fd < 0)
2541 /* Don't generate audit events if the service was already
2542 * started and we're just deserializing */
2543 if (m->n_reloading > 0)
2546 if (m->running_as != MANAGER_SYSTEM)
2549 if (u->meta.type != UNIT_SERVICE)
2552 if (!(p = unit_name_to_prefix_and_instance(u->meta.id))) {
2553 log_error("Failed to allocate unit name for audit message: %s", strerror(ENOMEM));
2557 if (audit_log_user_comm_message(m->audit_fd, type, "", p, NULL, NULL, NULL, success) < 0) {
2558 log_warning("Failed to send audit message: %m");
2560 if (errno == EPERM) {
2561 /* We aren't allowed to send audit messages?
2562 * Then let's not retry again, to avoid
2563 * spamming the user with the same and same
2564 * messages over and over. */
2566 audit_close(m->audit_fd);
2576 void manager_send_unit_plymouth(Manager *m, Unit *u) {
2578 union sockaddr_union sa;
2580 char *message = NULL;
2582 /* Don't generate plymouth events if the service was already
2583 * started and we're just deserializing */
2584 if (m->n_reloading > 0)
2587 if (m->running_as != MANAGER_SYSTEM)
2590 if (u->meta.type != UNIT_SERVICE &&
2591 u->meta.type != UNIT_MOUNT &&
2592 u->meta.type != UNIT_SWAP)
2595 /* We set SOCK_NONBLOCK here so that we rather drop the
2596 * message then wait for plymouth */
2597 if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
2598 log_error("socket() failed: %m");
2603 sa.sa.sa_family = AF_UNIX;
2604 strncpy(sa.un.sun_path+1, "/org/freedesktop/plymouthd", sizeof(sa.un.sun_path)-1);
2605 if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
2607 if (errno != EPIPE &&
2610 errno != ECONNREFUSED &&
2611 errno != ECONNRESET &&
2612 errno != ECONNABORTED)
2613 log_error("connect() failed: %m");
2618 if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->meta.id) + 1), u->meta.id, &n) < 0) {
2619 log_error("Out of memory");
2624 if (write(fd, message, n + 1) != n + 1) {
2626 if (errno != EPIPE &&
2629 errno != ECONNREFUSED &&
2630 errno != ECONNRESET &&
2631 errno != ECONNABORTED)
2632 log_error("Failed to write Plymouth message: %m");
2639 close_nointr_nofail(fd);
2644 void manager_dispatch_bus_name_owner_changed(
2647 const char* old_owner,
2648 const char *new_owner) {
2655 if (!(u = hashmap_get(m->watch_bus, name)))
2658 UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner);
2661 void manager_dispatch_bus_query_pid_done(
2672 if (!(u = hashmap_get(m->watch_bus, name)))
2675 UNIT_VTABLE(u)->bus_query_pid_done(u, name, pid);
2678 int manager_open_serialization(Manager *m, FILE **_f) {
2686 if (m->running_as == MANAGER_SYSTEM)
2687 asprintf(&path, "/run/systemd/dump-%lu-XXXXXX", (unsigned long) getpid());
2689 asprintf(&path, "/tmp/systemd-dump-%lu-XXXXXX", (unsigned long) getpid());
2694 saved_umask = umask(0077);
2695 fd = mkostemp(path, O_RDWR|O_CLOEXEC);
2705 log_debug("Serializing state to %s", path);
2708 if (!(f = fdopen(fd, "w+")))
2716 int manager_serialize(Manager *m, FILE *f, FDSet *fds) {
2728 fprintf(f, "current-job-id=%i\n", m->current_job_id);
2729 fprintf(f, "taint-usr=%s\n", yes_no(m->taint_usr));
2731 dual_timestamp_serialize(f, "initrd-timestamp", &m->initrd_timestamp);
2732 dual_timestamp_serialize(f, "startup-timestamp", &m->startup_timestamp);
2733 dual_timestamp_serialize(f, "finish-timestamp", &m->finish_timestamp);
2737 HASHMAP_FOREACH_KEY(u, t, m->units, i) {
2738 if (u->meta.id != t)
2741 if (!unit_can_serialize(u))
2745 fputs(u->meta.id, f);
2748 if ((r = unit_serialize(u, f, fds)) < 0) {
2754 assert(m->n_reloading > 0);
2760 r = bus_fdset_add_all(m, fds);
2767 int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
2773 log_debug("Deserializing state...");
2778 char line[LINE_MAX], *l;
2780 if (!fgets(line, sizeof(line), f)) {
2795 if (startswith(l, "current-job-id=")) {
2798 if (safe_atou32(l+15, &id) < 0)
2799 log_debug("Failed to parse current job id value %s", l+15);
2801 m->current_job_id = MAX(m->current_job_id, id);
2802 } else if (startswith(l, "taint-usr=")) {
2805 if ((b = parse_boolean(l+10)) < 0)
2806 log_debug("Failed to parse taint /usr flag %s", l+10);
2808 m->taint_usr = m->taint_usr || b;
2809 } else if (startswith(l, "initrd-timestamp="))
2810 dual_timestamp_deserialize(l+17, &m->initrd_timestamp);
2811 else if (startswith(l, "startup-timestamp="))
2812 dual_timestamp_deserialize(l+18, &m->startup_timestamp);
2813 else if (startswith(l, "finish-timestamp="))
2814 dual_timestamp_deserialize(l+17, &m->finish_timestamp);
2816 log_debug("Unknown serialization item '%s'", l);
2821 char name[UNIT_NAME_MAX+2];
2824 if (!fgets(name, sizeof(name), f)) {
2835 if ((r = manager_load_unit(m, strstrip(name), NULL, NULL, &u)) < 0)
2838 if ((r = unit_deserialize(u, f, fds)) < 0)
2848 assert(m->n_reloading > 0);
2854 int manager_reload(Manager *m) {
2861 if ((r = manager_open_serialization(m, &f)) < 0)
2866 if (!(fds = fdset_new())) {
2872 if ((r = manager_serialize(m, f, fds)) < 0) {
2877 if (fseeko(f, 0, SEEK_SET) < 0) {
2883 /* From here on there is no way back. */
2884 manager_clear_jobs_and_units(m);
2885 manager_undo_generators(m);
2887 /* Find new unit paths */
2888 lookup_paths_free(&m->lookup_paths);
2889 if ((q = lookup_paths_init(&m->lookup_paths, m->running_as, true)) < 0)
2892 manager_run_generators(m);
2894 manager_build_unit_path_cache(m);
2896 /* First, enumerate what we can from all config files */
2897 if ((q = manager_enumerate(m)) < 0)
2900 /* Second, deserialize our stored data */
2901 if ((q = manager_deserialize(m, f, fds)) < 0)
2907 /* Third, fire things up! */
2908 if ((q = manager_coldplug(m)) < 0)
2911 assert(m->n_reloading > 0);
2924 bool manager_is_booting_or_shutting_down(Manager *m) {
2929 /* Is the initial job still around? */
2930 if (manager_get_job(m, 1))
2933 /* Is there a job for the shutdown target? */
2934 u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET);
2936 return !!u->meta.job;
2941 void manager_reset_failed(Manager *m) {
2947 HASHMAP_FOREACH(u, m->units, i)
2948 unit_reset_failed(u);
2951 bool manager_unit_pending_inactive(Manager *m, const char *name) {
2957 /* Returns true if the unit is inactive or going down */
2958 if (!(u = manager_get_unit(m, name)))
2961 return unit_pending_inactive(u);
2964 void manager_check_finished(Manager *m) {
2965 char userspace[FORMAT_TIMESPAN_MAX], initrd[FORMAT_TIMESPAN_MAX], kernel[FORMAT_TIMESPAN_MAX], sum[FORMAT_TIMESPAN_MAX];
2966 usec_t kernel_usec = 0, initrd_usec = 0, userspace_usec = 0, total_usec = 0;
2970 if (dual_timestamp_is_set(&m->finish_timestamp))
2973 if (hashmap_size(m->jobs) > 0)
2976 dual_timestamp_get(&m->finish_timestamp);
2978 if (m->running_as == MANAGER_SYSTEM && detect_container(NULL) <= 0) {
2980 userspace_usec = m->finish_timestamp.monotonic - m->startup_timestamp.monotonic;
2981 total_usec = m->finish_timestamp.monotonic;
2983 if (dual_timestamp_is_set(&m->initrd_timestamp)) {
2985 kernel_usec = m->initrd_timestamp.monotonic;
2986 initrd_usec = m->startup_timestamp.monotonic - m->initrd_timestamp.monotonic;
2988 log_info("Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.",
2989 format_timespan(kernel, sizeof(kernel), kernel_usec),
2990 format_timespan(initrd, sizeof(initrd), initrd_usec),
2991 format_timespan(userspace, sizeof(userspace), userspace_usec),
2992 format_timespan(sum, sizeof(sum), total_usec));
2994 kernel_usec = m->startup_timestamp.monotonic;
2997 log_info("Startup finished in %s (kernel) + %s (userspace) = %s.",
2998 format_timespan(kernel, sizeof(kernel), kernel_usec),
2999 format_timespan(userspace, sizeof(userspace), userspace_usec),
3000 format_timespan(sum, sizeof(sum), total_usec));
3003 userspace_usec = initrd_usec = kernel_usec = 0;
3004 total_usec = m->finish_timestamp.monotonic - m->startup_timestamp.monotonic;
3006 log_debug("Startup finished in %s.",
3007 format_timespan(sum, sizeof(sum), total_usec));
3010 bus_broadcast_finished(m, kernel_usec, initrd_usec, userspace_usec, total_usec);
3013 "READY=1\nSTATUS=Startup finished in %s.",
3014 format_timespan(sum, sizeof(sum), total_usec));
3017 void manager_run_generators(Manager *m) {
3019 const char *generator_path;
3020 const char *argv[3];
3025 generator_path = m->running_as == MANAGER_SYSTEM ? SYSTEM_GENERATOR_PATH : USER_GENERATOR_PATH;
3026 if (!(d = opendir(generator_path))) {
3028 if (errno == ENOENT)
3031 log_error("Failed to enumerate generator directory: %m");
3035 if (!m->generator_unit_path) {
3037 char user_path[] = "/tmp/systemd-generator-XXXXXX";
3039 if (m->running_as == MANAGER_SYSTEM && getpid() == 1) {
3040 p = "/run/systemd/generator";
3042 if (mkdir_p(p, 0755) < 0) {
3043 log_error("Failed to create generator directory: %m");
3048 if (!(p = mkdtemp(user_path))) {
3049 log_error("Failed to create generator directory: %m");
3054 if (!(m->generator_unit_path = strdup(p))) {
3055 log_error("Failed to allocate generator unit path.");
3060 argv[0] = NULL; /* Leave this empty, execute_directory() will fill something in */
3061 argv[1] = m->generator_unit_path;
3065 execute_directory(generator_path, d, (char**) argv);
3068 if (rmdir(m->generator_unit_path) >= 0) {
3069 /* Uh? we were able to remove this dir? I guess that
3070 * means the directory was empty, hence let's shortcut
3073 free(m->generator_unit_path);
3074 m->generator_unit_path = NULL;
3078 if (!strv_find(m->lookup_paths.unit_path, m->generator_unit_path)) {
3081 if (!(l = strv_append(m->lookup_paths.unit_path, m->generator_unit_path))) {
3082 log_error("Failed to add generator directory to unit search path: %m");
3086 strv_free(m->lookup_paths.unit_path);
3087 m->lookup_paths.unit_path = l;
3089 log_debug("Added generator unit path %s to search path.", m->generator_unit_path);
3097 void manager_undo_generators(Manager *m) {
3100 if (!m->generator_unit_path)
3103 strv_remove(m->lookup_paths.unit_path, m->generator_unit_path);
3104 rm_rf(m->generator_unit_path, false, true, false);
3106 free(m->generator_unit_path);
3107 m->generator_unit_path = NULL;
3110 int manager_set_default_controllers(Manager *m, char **controllers) {
3115 if (!(l = strv_copy(controllers)))
3118 strv_free(m->default_controllers);
3119 m->default_controllers = l;
3124 void manager_recheck_syslog(Manager *m) {
3129 if (m->running_as != MANAGER_SYSTEM)
3132 if ((u = manager_get_unit(m, SPECIAL_SYSLOG_SOCKET))) {
3135 state = SOCKET(u)->state;
3137 if (state != SOCKET_DEAD &&
3138 state != SOCKET_FAILED &&
3139 state != SOCKET_RUNNING) {
3141 /* Hmm, the socket is not set up, or is still
3142 * listening, let's better not try to use
3143 * it. Note that we have no problem if the
3144 * socket is completely down, since there
3145 * might be a foreign /dev/log socket around
3146 * and we want to make use of that.
3154 if ((u = manager_get_unit(m, SPECIAL_SYSLOG_TARGET)))
3155 if (TARGET(u)->state != TARGET_ACTIVE) {
3160 /* Hmm, OK, so the socket is either fully up, or fully down,
3161 * and the target is up, then let's make use of the socket */
3165 void manager_set_show_status(Manager *m, bool b) {
3168 if (m->running_as != MANAGER_SYSTEM)
3174 touch("/run/systemd/show-status");
3176 unlink("/run/systemd/show-status");
3179 bool manager_get_show_status(Manager *m) {
3182 if (m->running_as != MANAGER_SYSTEM)
3188 /* If Plymouth is running make sure we show the status, so
3189 * that there's something nice to see when people press Esc */
3191 return plymouth_running();
3194 static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = {
3195 [MANAGER_SYSTEM] = "system",
3196 [MANAGER_USER] = "user"
3199 DEFINE_STRING_TABLE_LOOKUP(manager_running_as, ManagerRunningAs);