chiark / gitweb /
main: switch to primary console vt on crash
[elogind.git] / manager.c
index 6c73acf403fc0abe43be94abea08ec05aaa3e495..0a108087172064f6ed14e169d605cd94e1cf38b5 100644 (file)
--- a/manager.c
+++ b/manager.c
@@ -27,6 +27,7 @@
 #include <sys/signalfd.h>
 #include <sys/wait.h>
 #include <unistd.h>
+#include <utmpx.h>
 #include <sys/poll.h>
 #include <sys/reboot.h>
 #include <sys/ioctl.h>
@@ -42,6 +43,7 @@
 #include "ratelimit.h"
 #include "cgroup.h"
 #include "mount-setup.h"
+#include "utmp-wtmp.h"
 
 static int manager_setup_signals(Manager *m) {
         sigset_t mask;
@@ -232,10 +234,24 @@ static int manager_find_paths(Manager *m) {
                                               NULL)))
                                 return -ENOMEM;
                 }
+
+                if ((e = getenv("SYSTEMD_SYSVRCND_PATH")))
+                        if (!(m->sysvrcnd_path = split_path_and_make_absolute(e)))
+                                return -ENOMEM;
+
+                if (strv_isempty(m->sysvrcnd_path)) {
+                        strv_free(m->sysvrcnd_path);
+
+                        if (!(m->sysvrcnd_path = strv_new(
+                                              SYSTEM_SYSVRCND_PATH,     /* /etc/rcN.d/ */
+                                              NULL)))
+                                return -ENOMEM;
+                }
         }
 
         strv_uniq(m->unit_path);
         strv_uniq(m->sysvinit_path);
+        strv_uniq(m->sysvrcnd_path);
 
         assert(!strv_isempty(m->unit_path));
         if (!(t = strv_join(m->unit_path, "\n\t")))
@@ -253,6 +269,16 @@ static int manager_find_paths(Manager *m) {
         } else
                 log_debug("Ignoring SysV init scripts.");
 
+        if (!strv_isempty(m->sysvrcnd_path)) {
+
+                if (!(t = strv_join(m->sysvrcnd_path, "\n\t")))
+                        return -ENOMEM;
+
+                log_debug("Looking for SysV rcN.d links in:\n\t%s", t);
+                free(t);
+        } else
+                log_debug("Ignoring SysV rcN.d links.");
+
         return 0;
 }
 
@@ -267,6 +293,8 @@ int manager_new(ManagerRunningAs running_as, Manager **_m) {
         if (!(m = new0(Manager, 1)))
                 return -ENOMEM;
 
+        m->boot_timestamp = now(CLOCK_REALTIME);
+
         m->running_as = running_as;
         m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = -1;
         m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
@@ -363,6 +391,7 @@ void manager_free(Manager *m) {
 
         strv_free(m->unit_path);
         strv_free(m->sysvinit_path);
+        strv_free(m->sysvrcnd_path);
 
         free(m->cgroup_controller);
         free(m->cgroup_hierarchy);
@@ -403,6 +432,10 @@ int manager_coldplug(Manager *m) {
                                 return r;
         }
 
+        /* Now that the initial devices are available, let's see if we
+         * can write the utmp file */
+        manager_write_utmp_reboot(m);
+
         return 0;
 }
 
@@ -1389,8 +1422,6 @@ unsigned manager_dispatch_dbus_queue(Manager *m) {
 static int manager_dispatch_sigchld(Manager *m) {
         assert(m);
 
-        log_debug("dispatching SIGCHLD");
-
         for (;;) {
                 siginfo_t si;
                 Unit *u;
@@ -1444,9 +1475,16 @@ static int manager_process_signal_fd(Manager *m, bool *quit) {
 
                 switch (sfsi.ssi_signo) {
 
-                case SIGCHLD:
+                case SIGCHLD: {
+                        char *name = NULL;
+
+                        get_process_name(sfsi.ssi_pid, &name);
+                        log_debug("Got SIGCHLD for process %llu (%s)", (unsigned long long) sfsi.ssi_pid, strna(name));
+                        free(name);
+
                         sigchld = true;
                         break;
+                }
 
                 case SIGINT:
                 case SIGTERM:
@@ -1674,6 +1712,72 @@ int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) {
         return 0;
 }
 
+static bool manager_utmp_good(Manager *m) {
+        int r;
+
+        assert(m);
+
+        if ((r = mount_path_is_mounted(m, _PATH_UTMPX)) <= 0) {
+
+                if (r < 0)
+                        log_warning("Failed to determine whether " _PATH_UTMPX " is mounted: %s", strerror(-r));
+
+                return false;
+        }
+
+        return true;
+}
+
+void manager_write_utmp_reboot(Manager *m) {
+        int r;
+
+        assert(m);
+
+        if (m->utmp_reboot_written)
+                return;
+
+        if (m->running_as != MANAGER_INIT)
+                return;
+
+        if (!manager_utmp_good(m))
+                return;
+
+        if ((r = utmp_put_reboot(m->boot_timestamp)) < 0) {
+
+                if (r != -ENOENT && r != -EROFS)
+                        log_warning("Failed to write utmp/wtmp: %s", strerror(-r));
+
+                return;
+        }
+
+        m->utmp_reboot_written = true;
+}
+
+void manager_write_utmp_runlevel(Manager *m, Unit *u) {
+        int runlevel, r;
+
+        assert(m);
+        assert(u);
+
+        if (u->meta.type != UNIT_TARGET)
+                return;
+
+        if (m->running_as != MANAGER_INIT)
+                return;
+
+        if (!manager_utmp_good(m))
+                return;
+
+        if ((runlevel = target_get_runlevel(TARGET(u))) <= 0)
+                return;
+
+        if ((r = utmp_put_runlevel(0, runlevel, 0)) < 0) {
+
+                if (r != -ENOENT && r != -EROFS)
+                        log_warning("Failed to write utmp/wtmp: %s", strerror(-r));
+        }
+}
+
 static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = {
         [MANAGER_INIT] = "init",
         [MANAGER_SYSTEM] = "system",