chiark / gitweb /
main: only reopen /dev/console if we switch-root to a custom init
[elogind.git] / src / core / main.c
index 7c66665e843957f931149bc12e166467f40ffae4..ae3769be5279ed08b2f79794e9cc0dc4072afcea 100644 (file)
@@ -51,6 +51,7 @@
 #include "path-util.h"
 #include "switch-root.h"
 #include "capability.h"
+#include "killall.h"
 
 #include "mount-setup.h"
 #include "loopback-setup.h"
@@ -164,16 +165,11 @@ _noreturn_ static void crash(int sig) {
                 sa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT|SA_RESTART;
                 assert_se(sigaction(SIGCHLD, &sa, NULL) == 0);
 
-                if ((pid = fork()) < 0)
+                pid = fork();
+                if (pid < 0)
                         log_error("Failed to fork off crash shell: %s", strerror(errno));
                 else if (pid == 0) {
-                        int fd, r;
-
-                        if ((fd = acquire_terminal("/dev/console", false, true, true, (usec_t) -1)) < 0)
-                                log_error("Failed to acquire terminal: %s", strerror(-fd));
-                        else if ((r = make_stdio(fd)) < 0)
-                                log_error("Failed to duplicate terminal fd: %s", strerror(-r));
-
+                        make_console_stdio();
                         execl("/bin/sh", "/bin/sh", NULL);
 
                         log_error("execl() failed: %s", strerror(errno));
@@ -1430,8 +1426,20 @@ int main(int argc, char *argv[]) {
                         goto finish;
         }
 
-        log_full(arg_running_as == MANAGER_SYSTEM ? LOG_INFO : LOG_DEBUG,
-                 PACKAGE_STRING " running in %s mode. (" SYSTEMD_FEATURES "; " DISTRIBUTION ")", manager_running_as_to_string(arg_running_as));
+        if (arg_running_as == MANAGER_SYSTEM) {
+                const char *virtualization = NULL;
+
+                log_info(PACKAGE_STRING " running in system mode. (" SYSTEMD_FEATURES "; " DISTRIBUTION ")");
+
+                detect_virtualization(&virtualization);
+                if (virtualization)
+                        log_info("Detected virtualization '%s'.", virtualization);
+
+                if (in_initrd())
+                        log_info("Running in initial RAM disk.");
+
+        } else
+                log_debug(PACKAGE_STRING " running in user mode. (" SYSTEMD_FEATURES "; " DISTRIBUTION ")");
 
         if (arg_running_as == MANAGER_SYSTEM && !skip_setup) {
                 locale_setup();
@@ -1666,6 +1674,11 @@ finish:
                 watchdog_close(true);
 
                 if (switch_root_dir) {
+                        /* Kill all remaining processes from the initrd */
+                        broadcast_signal(SIGTERM);
+                        broadcast_signal(SIGKILL);
+
+                        /* And switch root */
                         r = switch_root(switch_root_dir);
                         if (r < 0)
                                 log_error("Failed to switch root, ignoring: %s", strerror(-r));
@@ -1717,6 +1730,9 @@ finish:
                         fds = NULL;
                 }
 
+                /* Reopen the console */
+                make_console_stdio();
+
                 for (j = 1, i = 1; j < argc; j++)
                         args[i++] = argv[j];
                 args[i++] = NULL;