chiark / gitweb /
main: make sure we don't accidentally acquire a controlling terminal
[elogind.git] / main.c
diff --git a/main.c b/main.c
index b0b3cfb580cff56f29f93f399b09236717633a8a..1f407dce214977b81465db106ab188351341b1ea 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));
@@ -174,7 +191,7 @@ static int console_setup(bool do_reset) {
 
         release_terminal();
 
-        if ((tty_fd = open_terminal("/dev/console", O_WRONLY)) < 0) {
+        if ((tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY)) < 0) {
                 log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
                 r = -tty_fd;
                 goto finish;