chiark / gitweb /
main: acquire console before execing crash shell
[elogind.git] / main.c
diff --git a/main.c b/main.c
index aa58040ff565ac5673fef0dd169e37522f470c66..6c730eb435ba4e7a567466ac291d835b5a35d15c 100644 (file)
--- a/main.c
+++ b/main.c
@@ -136,6 +136,23 @@ _noreturn static void crash(int sig) {
                 if ((pid = fork()) < 0)
                         log_error("Failed to fork off crash shell: %s", strerror(errno));
                 else if (pid == 0) {
+                        int fd;
+
+                        if ((fd = acquire_terminal("/dev/console", false, true)) < 0) {
+                                log_error("Failed to acquire terminal: %s", strerror(-fd));
+                                _exit(1);
+                        }
+
+                        if (dup2(fd, STDIN_FILENO) < 0 ||
+                            dup2(fd, STDOUT_FILENO) < 0 ||
+                            dup2(fd, STDERR_FILENO) < 0) {
+                                log_error("Failed to duplicate terminal fd: %s", strerror(errno));
+                                _exit(1);
+                        }
+
+                        if (fd >= 3)
+                                close_nointr_nofail(fd);
+
                         execl("/bin/sh", "/bin/sh", NULL);
 
                         log_error("execl() failed: %s", strerror(errno));
@@ -653,7 +670,7 @@ int main(int argc, char *argv[]) {
         }
 
         if ((r = manager_startup(m, serialization, fds)) < 0)
-                log_error("Failed to fully startup daemon: %s", strerror(-r));
+                log_error("Failed to fully start up daemon: %s", strerror(-r));
 
         if (fds) {
                 /* This will close all file descriptors that were opened, but