]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/core/manager.c
Merge branch 'hostnamectl-dot-v2'
[thirdparty/systemd.git] / src / core / manager.c
index afd911d899cf3ec1eaf9cb1e3a9397cc33e32132..ba107d461568d1f48f41ca9603f6a08f901476c7 100644 (file)
@@ -19,7 +19,6 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <assert.h>
 #include <errno.h>
 #include <string.h>
 #include <signal.h>
 #include <unistd.h>
 #include <sys/inotify.h>
 #include <sys/epoll.h>
-#include <sys/poll.h>
 #include <sys/reboot.h>
 #include <sys/ioctl.h>
 #include <linux/kd.h>
-#include <termios.h>
 #include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <dirent.h>
 #include <sys/timerfd.h>
 
 #endif
 
 #include "sd-daemon.h"
-#include "sd-id128.h"
 #include "sd-messages.h"
 
-#include "manager.h"
-#include "transaction.h"
 #include "hashmap.h"
 #include "macro.h"
 #include "strv.h"
 #include "mkdir.h"
 #include "ratelimit.h"
 #include "locale-setup.h"
-#include "mount-setup.h"
 #include "unit-name.h"
 #include "missing.h"
+#include "rm-rf.h"
 #include "path-lookup.h"
 #include "special.h"
 #include "exit-status.h"
 #include "virt.h"
 #include "watchdog.h"
-#include "cgroup-util.h"
 #include "path-util.h"
 #include "audit-fd.h"
 #include "boot-timestamps.h"
 #include "bus-common-errors.h"
 #include "bus-error.h"
 #include "bus-util.h"
+#include "bus-kernel.h"
+#include "time-util.h"
+#include "process-util.h"
+#include "terminal-util.h"
+#include "signal-util.h"
 #include "dbus.h"
 #include "dbus-unit.h"
 #include "dbus-job.h"
 #include "dbus-manager.h"
-#include "bus-kernel.h"
-#include "time-util.h"
+#include "manager.h"
+#include "transaction.h"
 
 /* Initial delay and the interval for printing status messages about running jobs */
 #define JOBS_IN_PROGRESS_WAIT_USEC (5*USEC_PER_SEC)
@@ -90,22 +86,29 @@ static int manager_dispatch_time_change_fd(sd_event_source *source, int fd, uint
 static int manager_dispatch_idle_pipe_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
 static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t usec, void *userdata);
 static int manager_dispatch_run_queue(sd_event_source *source, void *userdata);
+static int manager_run_generators(Manager *m);
+static void manager_undo_generators(Manager *m);
 
-static int manager_watch_jobs_in_progress(Manager *m) {
+static void manager_watch_jobs_in_progress(Manager *m) {
         usec_t next;
+        int r;
 
         assert(m);
 
         if (m->jobs_in_progress_event_source)
-                return 0;
+                return;
 
         next = now(CLOCK_MONOTONIC) + JOBS_IN_PROGRESS_WAIT_USEC;
-        return sd_event_add_time(
+        r = sd_event_add_time(
                         m->event,
                         &m->jobs_in_progress_event_source,
                         CLOCK_MONOTONIC,
                         next, 0,
                         manager_dispatch_jobs_in_progress, m);
+        if (r < 0)
+                return;
+
+        (void) sd_event_source_set_description(m->jobs_in_progress_event_source, "manager-jobs-in-progress");
 }
 
 #define CYLON_BUFFER_EXTRA (2*(sizeof(ANSI_RED_ON)-1) + sizeof(ANSI_HIGHLIGHT_RED_ON)-1 + 2*(sizeof(ANSI_HIGHLIGHT_OFF)-1))
@@ -162,6 +165,7 @@ static void manager_print_jobs_in_progress(Manager *m) {
         uint64_t x;
 
         assert(m);
+        assert(m->n_running_jobs > 0);
 
         manager_flip_auto_status(m, true);
 
@@ -183,9 +187,10 @@ static void manager_print_jobs_in_progress(Manager *m) {
 
         m->jobs_in_progress_iteration++;
 
-        if (m->n_running_jobs > 1)
+        if (m->n_running_jobs > 1) {
                 if (asprintf(&job_of_n, "(%u of %u) ", counter, m->n_running_jobs) < 0)
                         job_of_n = NULL;
+        }
 
         format_timespan(time, sizeof(time), now(CLOCK_MONOTONIC) - j->begin_usec, 1*USEC_PER_SEC);
         if (job_get_timeout(j, &x) > 0)
@@ -197,7 +202,6 @@ static void manager_print_jobs_in_progress(Manager *m) {
                               job_type_to_string(j->type),
                               unit_description(j->unit),
                               time, limit);
-
 }
 
 static int have_ask_password(void) {
@@ -280,6 +284,8 @@ static int manager_check_ask_password(Manager *m) {
                         return -errno;
                 }
 
+                (void) sd_event_source_set_description(m->ask_password_event_source, "manager-ask-password");
+
                 /* Queries might have been added meanwhile... */
                 manager_dispatch_ask_password_fd(m->ask_password_event_source,
                                                  m->ask_password_inotify_fd, EPOLLIN, m);
@@ -303,6 +309,8 @@ static int manager_watch_idle_pipe(Manager *m) {
         if (r < 0)
                 return log_error_errno(r, "Failed to watch idle pipe: %m");
 
+        (void) sd_event_source_set_description(m->idle_pipe_event_source, "manager-idle-pipe");
+
         return 0;
 }
 
@@ -345,6 +353,8 @@ static int manager_setup_time_change(Manager *m) {
         if (r < 0)
                 return log_error_errno(r, "Failed to create time change event source: %m");
 
+        (void) sd_event_source_set_description(m->time_change_event_source, "manager-time-change");
+
         log_debug("Set up TFD_TIMER_CANCEL_ON_SET timerfd.");
 
         return 0;
@@ -455,6 +465,8 @@ static int manager_setup_signals(Manager *m) {
         if (r < 0)
                 return r;
 
+        (void) sd_event_source_set_description(m->signal_event_source, "manager-signal");
+
         /* Process signals a bit earlier than the rest of things, but
          * later than notify_fd processing, so that the notify
          * processing can still figure out to which process/service a
@@ -463,7 +475,7 @@ static int manager_setup_signals(Manager *m) {
         if (r < 0)
                 return r;
 
-        if (m->running_as == SYSTEMD_SYSTEM)
+        if (m->running_as == MANAGER_SYSTEM)
                 return enable_special_signals(m);
 
         return 0;
@@ -489,7 +501,7 @@ static void manager_clean_environment(Manager *m) {
 static int manager_default_environment(Manager *m) {
         assert(m);
 
-        if (m->running_as == SYSTEMD_SYSTEM) {
+        if (m->running_as == MANAGER_SYSTEM) {
                 /* The system manager always starts with a clean
                  * environment for its children. It does not import
                  * the kernel or the parents exported variables.
@@ -517,20 +529,32 @@ static int manager_default_environment(Manager *m) {
         return 0;
 }
 
-int manager_new(SystemdRunningAs running_as, bool test_run, Manager **_m) {
+
+int manager_new(ManagerRunningAs running_as, bool test_run, Manager **_m) {
+
+        static const char * const unit_log_fields[_MANAGER_RUNNING_AS_MAX] = {
+                [MANAGER_SYSTEM] = "UNIT=",
+                [MANAGER_USER] = "USER_UNIT=",
+        };
+
+        static const char * const unit_log_format_strings[_MANAGER_RUNNING_AS_MAX] = {
+                [MANAGER_SYSTEM] = "UNIT=%s",
+                [MANAGER_USER] = "USER_UNIT=%s",
+        };
+
         Manager *m;
         int r;
 
         assert(_m);
         assert(running_as >= 0);
-        assert(running_as < _SYSTEMD_RUNNING_AS_MAX);
+        assert(running_as < _MANAGER_RUNNING_AS_MAX);
 
         m = new0(Manager, 1);
         if (!m)
                 return -ENOMEM;
 
 #ifdef ENABLE_EFI
-        if (running_as == SYSTEMD_SYSTEM && detect_container(NULL) <= 0)
+        if (running_as == MANAGER_SYSTEM && detect_container(NULL) <= 0)
                 boot_timestamps(&m->userspace_timestamp, &m->firmware_timestamp, &m->loader_timestamp);
 #endif
 
@@ -538,6 +562,10 @@ int manager_new(SystemdRunningAs running_as, bool test_run, Manager **_m) {
         m->exit_code = _MANAGER_EXIT_CODE_INVALID;
         m->default_timer_accuracy_usec = USEC_PER_MINUTE;
 
+        /* Prepare log fields we can use for structured logging */
+        m->unit_log_field = unit_log_fields[running_as];
+        m->unit_log_format_string = unit_log_format_strings[running_as];
+
         m->idle_pipe[0] = m->idle_pipe[1] = m->idle_pipe[2] = m->idle_pipe[3] = -1;
 
         m->pin_cgroupfs_fd = m->notify_fd = m->signal_fd = m->time_change_fd = m->dev_autofs_fd = m->private_listen_fd = m->kdbus_fd = m->utab_inotify_fd = -1;
@@ -548,6 +576,9 @@ int manager_new(SystemdRunningAs running_as, bool test_run, Manager **_m) {
 
         m->test_run = test_run;
 
+        /* Reboot immediately if the user hits C-A-D more often than 7x per 2s */
+        RATELIMIT_INIT(m->ctrl_alt_del_ratelimit, 2 * USEC_PER_SEC, 7);
+
         r = manager_default_environment(m);
         if (r < 0)
                 goto fail;
@@ -592,6 +623,8 @@ int manager_new(SystemdRunningAs running_as, bool test_run, Manager **_m) {
         if (r < 0)
                 goto fail;
 
+        (void) sd_event_source_set_description(m->run_queue_event_source, "manager-run-queue");
+
         r = manager_setup_signals(m);
         if (r < 0)
                 goto fail;
@@ -632,7 +665,7 @@ static int manager_setup_notify(Manager *m) {
 
         if (m->notify_fd < 0) {
                 _cleanup_close_ int fd = -1;
-                union sockaddr_union sa =  {
+                union sockaddr_union sa = {
                         .sa.sa_family = AF_UNIX,
                 };
                 static const int one = 1;
@@ -646,7 +679,7 @@ static int manager_setup_notify(Manager *m) {
                 if (fd < 0)
                         return log_error_errno(errno, "Failed to allocate notification socket: %m");
 
-                if (m->running_as == SYSTEMD_SYSTEM)
+                if (m->running_as == MANAGER_SYSTEM)
                         m->notify_socket = strdup("/run/systemd/notify");
                 else {
                         const char *e;
@@ -690,32 +723,31 @@ static int manager_setup_notify(Manager *m) {
                 r = sd_event_source_set_priority(m->notify_event_source, -7);
                 if (r < 0)
                         return log_error_errno(r, "Failed to set priority of notify event source: %m");
+
+                (void) sd_event_source_set_description(m->notify_event_source, "manager-notify");
         }
 
         return 0;
 }
 
 static int manager_setup_kdbus(Manager *m) {
-#ifdef ENABLE_KDBUS
         _cleanup_free_ char *p = NULL;
 
         assert(m);
 
         if (m->test_run || m->kdbus_fd >= 0)
                 return 0;
-
-        if (getpid() == 1)
-                bus_kernel_fix_attach_mask();
+        if (!is_kdbus_available())
+                return -ESOCKTNOSUPPORT;
 
         m->kdbus_fd = bus_kernel_create_bus(
-                        m->running_as == SYSTEMD_SYSTEM ? "system" : "user",
-                        m->running_as == SYSTEMD_SYSTEM, &p);
+                        m->running_as == MANAGER_SYSTEM ? "system" : "user",
+                        m->running_as == MANAGER_SYSTEM, &p);
 
         if (m->kdbus_fd < 0)
                 return log_debug_errno(m->kdbus_fd, "Failed to set up kdbus: %m");
 
         log_debug("Successfully set up kdbus on %s", p);
-#endif
 
         return 0;
 }
@@ -731,9 +763,9 @@ static int manager_connect_bus(Manager *m, bool reexecuting) {
         try_bus_connect =
                 m->kdbus_fd >= 0 ||
                 reexecuting ||
-                (m->running_as == SYSTEMD_USER && getenv("DBUS_SESSION_BUS_ADDRESS"));
+                (m->running_as == MANAGER_USER && getenv("DBUS_SESSION_BUS_ADDRESS"));
 
-        /* Try to connect to the busses, if possible. */
+        /* Try to connect to the buses, if possible. */
         return bus_init(m, try_bus_connect);
 }
 
@@ -840,7 +872,8 @@ static unsigned manager_dispatch_gc_queue(Manager *m) {
 
                 if (u->gc_marker == gc_marker + GC_OFFSET_BAD ||
                     u->gc_marker == gc_marker + GC_OFFSET_UNSURE) {
-                        log_unit_debug(u->id, "Collecting %s", u->id);
+                        if (u->id)
+                                log_unit_debug(u, "Collecting.");
                         u->gc_marker = gc_marker + GC_OFFSET_BAD;
                         unit_add_to_cleanup_queue(u);
                 }
@@ -956,8 +989,8 @@ int manager_enumerate(Manager *m) {
         for (c = 0; c < _UNIT_TYPE_MAX; c++) {
                 int q;
 
-                if (unit_vtable[c]->supported && !unit_vtable[c]->supported(m)) {
-                        log_info("Unit type .%s is not supported on this system.", unit_type_to_string(c));
+                if (!unit_type_supported(c)) {
+                        log_debug("Unit type .%s is not supported on this system.", unit_type_to_string(c));
                         continue;
                 }
 
@@ -973,28 +1006,25 @@ int manager_enumerate(Manager *m) {
         return r;
 }
 
-static int manager_coldplug(Manager *m) {
-        int r = 0;
+static void manager_coldplug(Manager *m) {
         Iterator i;
         Unit *u;
         char *k;
+        int r;
 
         assert(m);
 
         /* Then, let's set up their initial state. */
         HASHMAP_FOREACH_KEY(u, k, m->units, i) {
-                int q;
 
                 /* ignore aliases */
                 if (u->id != k)
                         continue;
 
-                q = unit_coldplug(u);
-                if (q < 0)
-                        r = q;
+                r = unit_coldplug(u);
+                if (r < 0)
+                        log_warning_errno(r, "We couldn't coldplug %s, proceeding anyway: %m", u->id);
         }
-
-        return r;
 }
 
 static void manager_build_unit_path_cache(Manager *m) {
@@ -1028,7 +1058,7 @@ static void manager_build_unit_path_cache(Manager *m) {
                 while ((de = readdir(d))) {
                         char *p;
 
-                        if (ignore_file(de->d_name))
+                        if (hidden_file(de->d_name))
                                 continue;
 
                         p = strjoin(streq(*i, "/") ? "" : *i, "/", de->d_name, NULL);
@@ -1084,8 +1114,10 @@ int manager_startup(Manager *m, FILE *serialization, FDSet *fds) {
         assert(m);
 
         dual_timestamp_get(&m->generators_start_timestamp);
-        manager_run_generators(m);
+        r = manager_run_generators(m);
         dual_timestamp_get(&m->generators_finish_timestamp);
+        if (r < 0)
+                return r;
 
         r = lookup_paths_init(
                         &m->lookup_paths, m->running_as, true,
@@ -1136,9 +1168,7 @@ int manager_startup(Manager *m, FILE *serialization, FDSet *fds) {
         bus_track_coldplug(m, &m->subscribed, &m->deserialized_subscribed);
 
         /* Third, fire things up! */
-        q = manager_coldplug(m);
-        if (q < 0 && r == 0)
-                r = q;
+        manager_coldplug(m);
 
         if (serialization) {
                 assert(m->n_reloading > 0);
@@ -1168,11 +1198,9 @@ int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool ove
         if (mode == JOB_ISOLATE && !unit->allow_isolate)
                 return sd_bus_error_setf(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated.");
 
-        log_unit_debug(unit->id,
-                       "Trying to enqueue job %s/%s/%s", unit->id,
-                       job_type_to_string(type), job_mode_to_string(mode));
+        log_unit_debug(unit, "Trying to enqueue job %s/%s/%s", unit->id, job_type_to_string(type), job_mode_to_string(mode));
 
-        job_type_collapse(&type, unit);
+        type = job_type_collapse(type, unit);
 
         tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY);
         if (!tr)
@@ -1194,7 +1222,7 @@ int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool ove
         if (r < 0)
                 goto tr_abort;
 
-        log_unit_debug(unit->id,
+        log_unit_debug(unit,
                        "Enqueued job %s/%s as %u", unit->id,
                        job_type_to_string(type), (unsigned) tr->anchor_job->id);
 
@@ -1290,7 +1318,7 @@ int manager_load_unit_prepare(
 
         t = unit_name_to_type(name);
 
-        if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, TEMPLATE_INVALID))
+        if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
                 return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is not valid.", name);
 
         ret = manager_get_unit(m, name);
@@ -1450,7 +1478,7 @@ static unsigned manager_dispatch_dbus_queue(Manager *m) {
         return n;
 }
 
-static void manager_invoke_notify_message(Manager *m, Unit *u, pid_t pid, char *buf, size_t n) {
+static void manager_invoke_notify_message(Manager *m, Unit *u, pid_t pid, char *buf, size_t n, FDSet *fds) {
         _cleanup_strv_free_ char **tags = NULL;
 
         assert(m);
@@ -1464,15 +1492,16 @@ static void manager_invoke_notify_message(Manager *m, Unit *u, pid_t pid, char *
                 return;
         }
 
-        log_unit_debug(u->id, "Got notification message for unit %s", u->id);
-
         if (UNIT_VTABLE(u)->notify_message)
-                UNIT_VTABLE(u)->notify_message(u, pid, tags);
+                UNIT_VTABLE(u)->notify_message(u, pid, tags, fds);
+        else
+                log_unit_debug(u, "Got notification message for unit. Ignoring.");
 }
 
 static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
         Manager *m = userdata;
         ssize_t n;
+        int r;
 
         assert(m);
         assert(m->notify_fd == fd);
@@ -1483,73 +1512,99 @@ static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t
         }
 
         for (;;) {
-                char buf[4096];
+                _cleanup_fdset_free_ FDSet *fds = NULL;
+                char buf[NOTIFY_BUFFER_MAX+1];
                 struct iovec iovec = {
                         .iov_base = buf,
                         .iov_len = sizeof(buf)-1,
                 };
-                bool found = false;
-
                 union {
                         struct cmsghdr cmsghdr;
-                        uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
+                        uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
+                                    CMSG_SPACE(sizeof(int) * NOTIFY_FD_MAX)];
                 } control = {};
-
                 struct msghdr msghdr = {
                         .msg_iov = &iovec,
                         .msg_iovlen = 1,
                         .msg_control = &control,
                         .msg_controllen = sizeof(control),
                 };
-                struct ucred *ucred;
+                struct cmsghdr *cmsg;
+                struct ucred *ucred = NULL;
+                bool found = false;
                 Unit *u1, *u2, *u3;
+                int *fd_array = NULL;
+                unsigned n_fds = 0;
 
-                n = recvmsg(m->notify_fd, &msghdr, MSG_DONTWAIT);
-                if (n <= 0) {
-                        if (n == 0)
-                                return -EIO;
-
+                n = recvmsg(m->notify_fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
+                if (n < 0) {
                         if (errno == EAGAIN || errno == EINTR)
                                 break;
 
                         return -errno;
                 }
 
-                if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
-                    control.cmsghdr.cmsg_level != SOL_SOCKET ||
-                    control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
-                    control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
-                        log_warning("Received notify message without credentials. Ignoring.");
+                CMSG_FOREACH(cmsg, &msghdr) {
+                        if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
+
+                                fd_array = (int*) CMSG_DATA(cmsg);
+                                n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
+
+                        } else if (cmsg->cmsg_level == SOL_SOCKET &&
+                                   cmsg->cmsg_type == SCM_CREDENTIALS &&
+                                   cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred))) {
+
+                                ucred = (struct ucred*) CMSG_DATA(cmsg);
+                        }
+                }
+
+                if (n_fds > 0) {
+                        assert(fd_array);
+
+                        r = fdset_new_array(&fds, fd_array, n_fds);
+                        if (r < 0) {
+                                close_many(fd_array, n_fds);
+                                return log_oom();
+                        }
+                }
+
+                if (!ucred || ucred->pid <= 0) {
+                        log_warning("Received notify message without valid credentials. Ignoring.");
                         continue;
                 }
 
-                ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
+                if ((size_t) n >= sizeof(buf)) {
+                        log_warning("Received notify message exceeded maximum size. Ignoring.");
+                        continue;
+                }
 
-                assert((size_t) n < sizeof(buf));
                 buf[n] = 0;
 
                 /* Notify every unit that might be interested, but try
                  * to avoid notifying the same one multiple times. */
                 u1 = manager_get_unit_by_pid(m, ucred->pid);
                 if (u1) {
-                        manager_invoke_notify_message(m, u1, ucred->pid, buf, n);
+                        manager_invoke_notify_message(m, u1, ucred->pid, buf, n, fds);
                         found = true;
                 }
 
                 u2 = hashmap_get(m->watch_pids1, LONG_TO_PTR(ucred->pid));
                 if (u2 && u2 != u1) {
-                        manager_invoke_notify_message(m, u2, ucred->pid, buf, n);
+                        manager_invoke_notify_message(m, u2, ucred->pid, buf, n, fds);
                         found = true;
                 }
 
                 u3 = hashmap_get(m->watch_pids2, LONG_TO_PTR(ucred->pid));
                 if (u3 && u3 != u2 && u3 != u1) {
-                        manager_invoke_notify_message(m, u3, ucred->pid, buf, n);
+                        manager_invoke_notify_message(m, u3, ucred->pid, buf, n, fds);
                         found = true;
                 }
 
                 if (!found)
                         log_warning("Cannot find unit for notify message of PID "PID_FMT".", ucred->pid);
+
+                if (fdset_size(fds) > 0)
+                        log_warning("Got auxiliary fds with notification message, closing all.");
         }
 
         return 0;
@@ -1560,7 +1615,7 @@ static void invoke_sigchld_event(Manager *m, Unit *u, siginfo_t *si) {
         assert(u);
         assert(si);
 
-        log_unit_debug(u->id, "Child "PID_FMT" belongs to %s", si->si_pid, u->id);
+        log_unit_debug(u, "Child "PID_FMT" belongs to %s", si->si_pid, u->id);
 
         unit_unwatch_pid(u, si->si_pid);
         UNIT_VTABLE(u)->sigchld_event(u, si->si_pid, si->si_code, si->si_status);
@@ -1632,11 +1687,11 @@ static int manager_start_target(Manager *m, const char *name, JobMode mode) {
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         int r;
 
-        log_unit_debug(name, "Activating special unit %s", name);
+        log_debug("Activating special unit %s", name);
 
         r = manager_add_job_by_name(m, JOB_START, name, mode, true, &error, NULL);
         if (r < 0)
-                log_unit_error(name, "Failed to enqueue %s job: %s", name, bus_error_message(&error, r));
+                log_error("Failed to enqueue %s job: %s", name, bus_error_message(&error, r));
 
         return r;
 }
@@ -1646,6 +1701,7 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
         ssize_t n;
         struct signalfd_siginfo sfsi;
         bool sigchld = false;
+        int r;
 
         assert(m);
         assert(m->signal_fd == fd);
@@ -1669,7 +1725,7 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
                 }
 
                 log_received_signal(sfsi.ssi_signo == SIGCHLD ||
-                                    (sfsi.ssi_signo == SIGTERM && m->running_as == SYSTEMD_USER)
+                                    (sfsi.ssi_signo == SIGTERM && m->running_as == MANAGER_USER)
                                     ? LOG_DEBUG : LOG_INFO,
                                     &sfsi);
 
@@ -1680,7 +1736,7 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
                         break;
 
                 case SIGTERM:
-                        if (m->running_as == SYSTEMD_SYSTEM) {
+                        if (m->running_as == MANAGER_SYSTEM) {
                                 /* This is for compatibility with the
                                  * original sysvinit */
                                 m->exit_code = MANAGER_REEXECUTE;
@@ -1690,8 +1746,20 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
                         /* Fall through */
 
                 case SIGINT:
-                        if (m->running_as == SYSTEMD_SYSTEM) {
-                                manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE_IRREVERSIBLY);
+                        if (m->running_as == MANAGER_SYSTEM) {
+
+                                /* If the user presses C-A-D more than
+                                 * 7 times within 2s, we reboot
+                                 * immediately. */
+
+                                if (ratelimit_test(&m->ctrl_alt_del_ratelimit))
+                                        manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE_IRREVERSIBLY);
+                                else {
+                                        log_notice("Ctrl-Alt-Del was pressed more than 7 times within 2s, rebooting immediately.");
+                                        status_printf(NULL, true, false, "Ctrl-Alt-Del was pressed more than 7 times within 2s, rebooting immediately.");
+                                        m->exit_code = MANAGER_REBOOT;
+                                }
+
                                 break;
                         }
 
@@ -1704,14 +1772,14 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
                         break;
 
                 case SIGWINCH:
-                        if (m->running_as == SYSTEMD_SYSTEM)
+                        if (m->running_as == MANAGER_SYSTEM)
                                 manager_start_target(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE);
 
                         /* This is a nop on non-init */
                         break;
 
                 case SIGPWR:
-                        if (m->running_as == SYSTEMD_SYSTEM)
+                        if (m->running_as == MANAGER_SYSTEM)
                                 manager_start_target(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE);
 
                         /* This is a nop on non-init */
@@ -1742,20 +1810,16 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
 
                         f = open_memstream(&dump, &size);
                         if (!f) {
-                                log_warning("Failed to allocate memory stream.");
+                                log_warning_errno(errno, "Failed to allocate memory stream: %m");
                                 break;
                         }
 
                         manager_dump_units(m, f, "\t");
                         manager_dump_jobs(m, f, "\t");
 
-                        if (ferror(f)) {
-                                log_warning("Failed to write status stream");
-                                break;
-                        }
-
-                        if (fflush(f)) {
-                                log_warning("Failed to flush status stream");
+                        r = fflush_and_check(f);
+                        if (r < 0) {
+                                log_warning_errno(r, "Failed to write status stream: %m");
                                 break;
                         }
 
@@ -1825,7 +1889,7 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
                                 break;
 
                         case 24:
-                                if (m->running_as == SYSTEMD_USER) {
+                                if (m->running_as == MANAGER_USER) {
                                         m->exit_code = MANAGER_EXIT;
                                         return 0;
                                 }
@@ -1943,7 +2007,7 @@ int manager_loop(Manager *m) {
         while (m->exit_code == MANAGER_OK) {
                 usec_t wait_usec;
 
-                if (m->runtime_watchdog > 0 && m->running_as == SYSTEMD_SYSTEM)
+                if (m->runtime_watchdog > 0 && m->running_as == MANAGER_SYSTEM)
                         watchdog_ping();
 
                 if (!ratelimit_test(&rl)) {
@@ -1969,7 +2033,7 @@ int manager_loop(Manager *m) {
                         continue;
 
                 /* Sleep for half the watchdog time */
-                if (m->runtime_watchdog > 0 && m->running_as == SYSTEMD_SYSTEM) {
+                if (m->runtime_watchdog > 0 && m->running_as == MANAGER_SYSTEM) {
                         wait_usec = m->runtime_watchdog / 2;
                         if (wait_usec <= 0)
                                 wait_usec = 1;
@@ -2038,7 +2102,7 @@ void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
 #ifdef HAVE_AUDIT
         _cleanup_free_ char *p = NULL;
         const char *msg;
-        int audit_fd;
+        int audit_fd, r;
 
         audit_fd = get_audit_fd();
         if (audit_fd < 0)
@@ -2049,20 +2113,19 @@ void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
         if (m->n_reloading > 0)
                 return;
 
-        if (m->running_as != SYSTEMD_SYSTEM)
+        if (m->running_as != MANAGER_SYSTEM)
                 return;
 
         if (u->type != UNIT_SERVICE)
                 return;
 
-        p = unit_name_to_prefix_and_instance(u->id);
-        if (!p) {
-                log_oom();
+        r = unit_name_to_prefix_and_instance(u->id, &p);
+        if (r < 0) {
+                log_error_errno(r, "Failed to extract prefix and instance of unit name: %m");
                 return;
         }
 
-        msg = strappenda("unit=", p);
-
+        msg = strjoina("unit=", p);
         if (audit_log_user_comm_message(audit_fd, type, msg, "systemd", NULL, NULL, NULL, success) < 0) {
                 if (errno == EPERM)
                         /* We aren't allowed to send audit messages?
@@ -2087,7 +2150,7 @@ void manager_send_unit_plymouth(Manager *m, Unit *u) {
         if (m->n_reloading > 0)
                 return;
 
-        if (m->running_as != SYSTEMD_SYSTEM)
+        if (m->running_as != MANAGER_SYSTEM)
                 return;
 
         if (detect_container(NULL) > 0)
@@ -2149,7 +2212,7 @@ int manager_open_serialization(Manager *m, FILE **_f) {
 
         assert(_f);
 
-        path = m->running_as == SYSTEMD_SYSTEM ? "/run/systemd" : "/tmp";
+        path = m->running_as == MANAGER_SYSTEM ? "/run/systemd" : "/tmp";
         fd = open_tmpfile(path, O_RDWR|O_CLOEXEC);
         if (fd < 0)
                 return -errno;
@@ -2180,7 +2243,7 @@ int manager_serialize(Manager *m, FILE *f, FDSet *fds, bool switching_root) {
 
         m->n_reloading ++;
 
-        fprintf(f, "current-job-id=%i\n", m->current_job_id);
+        fprintf(f, "current-job-id=%"PRIu32"\n", m->current_job_id);
         fprintf(f, "taint-usr=%s\n", yes_no(m->taint_usr));
         fprintf(f, "n-installed-jobs=%u\n", m->n_installed_jobs);
         fprintf(f, "n-failed-jobs=%u\n", m->n_failed_jobs);
@@ -2355,11 +2418,9 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
                         _cleanup_free_ char *uce = NULL;
                         char **e;
 
-                        uce = cunescape(l+4);
-                        if (!uce) {
-                                r = -ENOMEM;
+                        r = cunescape(l + 4, UNESCAPE_RELAX, &uce);
+                        if (r < 0)
                                 goto finish;
-                        }
 
                         e = strv_env_set(m->environment, uce);
                         if (!e) {
@@ -2486,7 +2547,9 @@ int manager_reload(Manager *m) {
         lookup_paths_free(&m->lookup_paths);
 
         /* Find new unit paths */
-        manager_run_generators(m);
+        q = manager_run_generators(m);
+        if (q < 0 && r >= 0)
+                r = q;
 
         q = lookup_paths_init(
                         &m->lookup_paths, m->running_as, true,
@@ -2494,19 +2557,19 @@ int manager_reload(Manager *m) {
                         m->generator_unit_path,
                         m->generator_unit_path_early,
                         m->generator_unit_path_late);
-        if (q < 0)
+        if (q < 0 && r >= 0)
                 r = q;
 
         manager_build_unit_path_cache(m);
 
         /* First, enumerate what we can from all config files */
         q = manager_enumerate(m);
-        if (q < 0)
+        if (q < 0 && r >= 0)
                 r = q;
 
         /* Second, deserialize our stored data */
         q = manager_deserialize(m, f, fds);
-        if (q < 0)
+        if (q < 0 && r >= 0)
                 r = q;
 
         fclose(f);
@@ -2514,13 +2577,11 @@ int manager_reload(Manager *m) {
 
         /* Re-register notify_fd as event source */
         q = manager_setup_notify(m);
-        if (q < 0)
+        if (q < 0 && r >= 0)
                 r = q;
 
         /* Third, fire things up! */
-        q = manager_coldplug(m);
-        if (q < 0)
-                r = q;
+        manager_coldplug(m);
 
         assert(m->n_reloading > 0);
         m->n_reloading--;
@@ -2567,7 +2628,7 @@ static void manager_notify_finished(Manager *m) {
         if (m->test_run)
                 return;
 
-        if (m->running_as == SYSTEMD_SYSTEM && detect_container(NULL) <= 0) {
+        if (m->running_as == MANAGER_SYSTEM && detect_container(NULL) <= 0) {
 
                 /* Note that m->kernel_usec.monotonic is always at 0,
                  * and m->firmware_usec.monotonic and
@@ -2635,13 +2696,21 @@ void manager_check_finished(Manager *m) {
 
         assert(m);
 
-        if (m->n_running_jobs == 0)
-                m->jobs_in_progress_event_source = sd_event_source_unref(m->jobs_in_progress_event_source);
+        if (m->n_reloading > 0)
+                return;
+
+        /* Verify that we are actually running currently. Initially
+         * the exit code is set to invalid, and during operation it is
+         * then set to MANAGER_OK */
+        if (m->exit_code != MANAGER_OK)
+                return;
 
         if (hashmap_size(m->jobs) > 0) {
 
                 if (m->jobs_in_progress_event_source)
-                        sd_event_source_set_time(m->jobs_in_progress_event_source, now(CLOCK_MONOTONIC) + JOBS_IN_PROGRESS_WAIT_USEC);
+                        /* Ignore any failure, this is only for feedback */
+                        (void) sd_event_source_set_time(m->jobs_in_progress_event_source,
+                                                        now(CLOCK_MONOTONIC) + JOBS_IN_PROGRESS_WAIT_USEC);
 
                 return;
         }
@@ -2684,7 +2753,7 @@ static int create_generator_dir(Manager *m, char **generator, const char *name)
         if (*generator)
                 return 0;
 
-        if (m->running_as == SYSTEMD_SYSTEM && getpid() == 1) {
+        if (m->running_as == MANAGER_SYSTEM && getpid() == 1) {
                 /* systemd --system, not running --test */
 
                 p = strappend("/run/systemd/", name);
@@ -2697,7 +2766,7 @@ static int create_generator_dir(Manager *m, char **generator, const char *name)
                         free(p);
                         return r;
                 }
-        } else if (m->running_as == SYSTEMD_USER) {
+        } else if (m->running_as == MANAGER_USER) {
                 const char *s = NULL;
 
                 s = getenv("XDG_RUNTIME_DIR");
@@ -2747,28 +2816,33 @@ static void trim_generator_dir(Manager *m, char **generator) {
         return;
 }
 
-void manager_run_generators(Manager *m) {
-        _cleanup_closedir_ DIR *d = NULL;
-        const char *generator_path;
+static int manager_run_generators(Manager *m) {
+        _cleanup_strv_free_ char **paths = NULL;
         const char *argv[5];
+        char **path;
         int r;
 
         assert(m);
 
         if (m->test_run)
-                return;
+                return 0;
 
-        generator_path = m->running_as == SYSTEMD_SYSTEM ? SYSTEM_GENERATOR_PATH : USER_GENERATOR_PATH;
-        d = opendir(generator_path);
-        if (!d) {
-                if (errno == ENOENT)
-                        return;
+        paths = generator_paths(m->running_as);
+        if (!paths)
+                return log_oom();
 
-                log_error_errno(errno, "Failed to enumerate generator directory %s: %m",
-                          generator_path);
-                return;
+        /* Optimize by skipping the whole process by not creating output directories
+         * if no generators are found. */
+        STRV_FOREACH(path, paths) {
+                r = access(*path, F_OK);
+                if (r == 0)
+                        goto found;
+                if (errno != ENOENT)
+                        log_warning_errno(errno, "Failed to open generator directory %s: %m", *path);
         }
+        return 0;
 
+ found:
         r = create_generator_dir(m, &m->generator_unit_path, "generator");
         if (r < 0)
                 goto finish;
@@ -2788,12 +2862,13 @@ void manager_run_generators(Manager *m) {
         argv[4] = NULL;
 
         RUN_WITH_UMASK(0022)
-                execute_directory(generator_path, d, DEFAULT_TIMEOUT_USEC, (char**) argv);
+                execute_directories((const char* const*) paths, DEFAULT_TIMEOUT_USEC, (char**) argv);
 
 finish:
         trim_generator_dir(m, &m->generator_unit_path);
         trim_generator_dir(m, &m->generator_unit_path_early);
         trim_generator_dir(m, &m->generator_unit_path_late);
+        return r;
 }
 
 static void remove_generator_dir(Manager *m, char **generator) {
@@ -2804,13 +2879,13 @@ static void remove_generator_dir(Manager *m, char **generator) {
                 return;
 
         strv_remove(m->lookup_paths.unit_path, *generator);
-        rm_rf(*generator, false, true, false);
+        (void) rm_rf(*generator, REMOVE_ROOT);
 
         free(*generator);
         *generator = NULL;
 }
 
-void manager_undo_generators(Manager *m) {
+static void manager_undo_generators(Manager *m) {
         assert(m);
 
         remove_generator_dir(m, &m->generator_unit_path);
@@ -2878,7 +2953,7 @@ void manager_recheck_journal(Manager *m) {
 
         assert(m);
 
-        if (m->running_as != SYSTEMD_SYSTEM)
+        if (m->running_as != MANAGER_SYSTEM)
                 return;
 
         u = manager_get_unit(m, SPECIAL_JOURNALD_SOCKET);
@@ -2902,7 +2977,7 @@ void manager_set_show_status(Manager *m, ShowStatus mode) {
         assert(m);
         assert(IN_SET(mode, SHOW_STATUS_AUTO, SHOW_STATUS_NO, SHOW_STATUS_YES, SHOW_STATUS_TEMPORARY));
 
-        if (m->running_as != SYSTEMD_SYSTEM)
+        if (m->running_as != MANAGER_SYSTEM)
                 return;
 
         m->show_status = mode;
@@ -2916,7 +2991,7 @@ void manager_set_show_status(Manager *m, ShowStatus mode) {
 static bool manager_get_show_status(Manager *m, StatusType type) {
         assert(m);
 
-        if (m->running_as != SYSTEMD_SYSTEM)
+        if (m->running_as != MANAGER_SYSTEM)
                 return false;
 
         if (m->no_console_output)
@@ -2932,15 +3007,13 @@ static bool manager_get_show_status(Manager *m, StatusType type) {
         if (m->show_status > 0)
                 return true;
 
-        /* If Plymouth is running make sure we show the status, so
-         * that there's something nice to see when people press Esc */
-        return plymouth_running();
+        return false;
 }
 
 void manager_set_first_boot(Manager *m, bool b) {
         assert(m);
 
-        if (m->running_as != SYSTEMD_SYSTEM)
+        if (m->running_as != MANAGER_SYSTEM)
                 return;
 
         m->first_boot = b;
@@ -2972,15 +3045,16 @@ void manager_status_printf(Manager *m, StatusType type, const char *status, cons
 int manager_get_unit_by_path(Manager *m, const char *path, const char *suffix, Unit **_found) {
         _cleanup_free_ char *p = NULL;
         Unit *found;
+        int r;
 
         assert(m);
         assert(path);
         assert(suffix);
         assert(_found);
 
-        p = unit_name_from_path(path, suffix);
-        if (!p)
-                return -ENOMEM;
+        r = unit_name_from_path(path, suffix, &p);
+        if (r < 0)
+                return r;
 
         found = manager_get_unit(m, p);
         if (!found) {
@@ -3007,11 +3081,29 @@ Set *manager_get_units_requiring_mounts_for(Manager *m, const char *path) {
 const char *manager_get_runtime_prefix(Manager *m) {
         assert(m);
 
-        return m->running_as == SYSTEMD_SYSTEM ?
+        return m->running_as == MANAGER_SYSTEM ?
                "/run" :
                getenv("XDG_RUNTIME_DIR");
 }
 
+void manager_update_failed_units(Manager *m, Unit *u, bool failed) {
+        unsigned size;
+
+        assert(m);
+        assert(u->manager == m);
+
+        size = set_size(m->failed_units);
+
+        if (failed) {
+                if (set_put(m->failed_units, u) < 0)
+                        log_oom();
+        } else
+                set_remove(m->failed_units, u);
+
+        if (set_size(m->failed_units) != size)
+                bus_manager_send_change_signal(m);
+}
+
 ManagerState manager_state(Manager *m) {
         Unit *u;