chiark / gitweb /
switch-root: reopen /dev/console before we switch root
[elogind.git] / src / shared / util.c
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;
+}