chiark / gitweb /
switch-root: reopen /dev/console before we switch root
authorLennart Poettering <lennart@poettering.net>
Tue, 10 Jul 2012 17:19:59 +0000 (19:19 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 10 Jul 2012 17:19:59 +0000 (19:19 +0200)
src/core/main.c
src/core/shutdown.c
src/shared/util.c
src/shared/util.h

index 6a2dbc2f54d864e7912a7ba74d5e2cd29e562307..6f6b565d68d9d5424bfaa3ccea2f9986347de8a6 100644 (file)
@@ -164,16 +164,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));
@@ -1677,6 +1672,9 @@ finish:
                  * rebooted while we do that */
                 watchdog_close(true);
 
+                /* Reopen the console */
+                make_console_stdio();
+
                 if (switch_root_dir) {
                         r = switch_root(switch_root_dir);
                         if (r < 0)
index baef66dd9d60ba79a6348c5643e06140eb6bd1c9..194a1ccf21221e712bb82cbdc5c53b0b3bdf1424 100644 (file)
@@ -267,9 +267,10 @@ static int prepare_new_root(void) {
 }
 
 static int pivot_to_new_root(void) {
-        int fd;
-
-        chdir("/run/initramfs");
+        if (chdir("/run/initramfs") < 0) {
+                log_error("Failed to change directory to /run/initramfs: %m");
+                return -errno;
+        }
 
         /*
           In case some evil process made "/" MS_SHARED
@@ -288,18 +289,11 @@ static int pivot_to_new_root(void) {
         }
 
         chroot(".");
-        log_info("Successfully changed into root pivot.");
 
-        fd = open("/dev/console", O_RDWR);
-        if (fd < 0)
-                log_error("Failed to open /dev/console: %m");
-        else {
-                make_stdio(fd);
+        setsid();
+        make_console_stdio();
 
-                /* Initialize the controlling terminal */
-                setsid();
-                ioctl(STDIN_FILENO, TIOCSCTTY, NULL);
-        }
+        log_info("Successfully changed into root pivot.");
 
         return 0;
 }
index 62121b59236abd407acc811157f62ef83cc078c6..63471899fd111f8d21da5c74baecfcd464fb130d 100644 (file)
@@ -2940,7 +2940,8 @@ int make_stdio(int fd) {
 int make_null_stdio(void) {
         int null_fd;
 
-        if ((null_fd = open("/dev/null", O_RDWR|O_NOCTTY)) < 0)
+        null_fd = open("/dev/null", O_RDWR|O_NOCTTY);
+        if (null_fd < 0)
                 return -errno;
 
         return make_stdio(null_fd);
@@ -5844,3 +5845,23 @@ void warn_melody(void) {
         ioctl(fd, KIOCSOUND, 0);
         close_nointr_nofail(fd);
 }
+
+int make_console_stdio(void) {
+        int fd, r;
+
+        /* Make /dev/console the controlling terminal and stdin/stdout/stderr */
+
+        fd = acquire_terminal("/dev/console", false, true, true, (usec_t) -1);
+        if (fd < 0) {
+                log_error("Failed to acquire terminal: %s", strerror(-fd));
+                return fd;
+        }
+
+        r = make_stdio(fd);
+        if (r < 0) {
+                log_error("Failed to duplicate terminal fd: %s", strerror(-r));
+                return r;
+        }
+
+        return 0;
+}
index 89e9a00afcc34b0d3571c3a651a54be10d1b0b6e..c8d048f9b28999110ed311776da7a73e90a24b3d 100644 (file)
@@ -278,6 +278,7 @@ char *format_timespan(char *buf, size_t l, usec_t t);
 
 int make_stdio(int fd);
 int make_null_stdio(void);
+int make_console_stdio(void);
 
 unsigned long long random_ull(void);