]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
agetty: close tty before vhangup()
authorKarel Zak <kzak@redhat.com>
Thu, 12 Jul 2012 14:31:22 +0000 (16:31 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 12 Jul 2012 14:34:56 +0000 (16:34 +0200)
Let's close all tty file descriptors if called with --hangup option.

References: https://lkml.org/lkml/2012/6/5/145
Signed-off-by: Karel Zak <kzak@redhat.com>
term-utils/agetty.c

index b2cc24cca13a203857baceb4faeaefc75b600230..fab83b8ff993bb2819b199cefef514fc19b9014e 100644 (file)
@@ -264,7 +264,7 @@ static void login_options_to_argv(char *argv[], int *argc, char *str, char *user
 static char *fakehost;
 
 #ifdef DEBUGGING
-#define debug(s) do { fprintf(dbf,s); fflush(dbf); while (0)
+#define debug(s) do { fprintf(dbf,s); fflush(dbf); while (0)
 FILE *dbf;
 #else
 #define debug(s) do { ; } while (0)
@@ -869,10 +869,12 @@ static void update_utmp(struct options *op)
 static void open_tty(char *tty, struct termios *tp, struct options *op)
 {
        const pid_t pid = getpid();
-       int serial;
+       int serial, closed = 0;
 
        /* Set up new standard input, unless we are given an already opened port. */
 
+sleep(10);
+
        if (strcmp(tty, "-") != 0) {
                char buf[PATH_MAX+1];
                struct group *gr = NULL;
@@ -919,27 +921,33 @@ static void open_tty(char *tty, struct termios *tp, struct options *op)
                                log_warn("/dev/%s: cannot get controlling tty: %m", tty);
                }
 
+               close(STDIN_FILENO);
+               errno = 0;
+
                if (op->flags & F_HANGUP) {
+
+                       if (ioctl(fd, TIOCNOTTY))
+                               debug("TIOCNOTTY ioctl failed\n");
+
                        /*
-                        * vhangup() will replace all open file descriptors in the kernel
-                        * that point to our controlling tty by a dummy that will deny
-                        * further reading/writing to our device. It will also reset the
-                        * tty to sane defaults, so we don't have to modify the tty device
-                        * for sane settings. We also get a SIGHUP/SIGCONT.
+                        * Let's close all file decriptors before vhangup
+                        * https://lkml.org/lkml/2012/6/5/145
                         */
+                       close(fd);
+                       close(STDOUT_FILENO);
+                       close(STDERR_FILENO);
+                       errno = 0;
+                       closed = 1;
+
                        if (vhangup())
                                log_err("/dev/%s: vhangup() failed: %m", tty);
-                       if (ioctl(fd, TIOCNOTTY))
-                               debug("TIOCNOTTY ioctl failed\n");
-               }
-
-               close(fd);
-               close(STDIN_FILENO);
-               errno = 0;
+               } else
+                       close(fd);
 
                debug("open(2)\n");
                if (open(buf, O_RDWR|O_NOCTTY|O_NONBLOCK, 0) != 0)
                        log_err(_("/dev/%s: cannot open as standard input: %m"), tty);
+
                if (((tid = tcgetsid(STDIN_FILENO)) < 0) || (pid != tid)) {
                        if (ioctl(STDIN_FILENO, TIOCSCTTY, 1) == -1)
                                log_warn("/dev/%s: cannot get controlling tty: %m", tty);
@@ -961,9 +969,11 @@ static void open_tty(char *tty, struct termios *tp, struct options *op)
                log_warn("/dev/%s: cannot set process group: %m", tty);
 
        /* Get rid of the present outputs. */
-       close(STDOUT_FILENO);
-       close(STDERR_FILENO);
-       errno = 0;
+       if (!closed) {
+               close(STDOUT_FILENO);
+               close(STDERR_FILENO);
+               errno = 0;
+       }
 
        /* Set up standard output and standard error file descriptors. */
        debug("duping\n");