From 783b08fc1c6e5c906e26b1b4856d5df3a7b5a554 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 12 Jul 2012 16:31:22 +0200 Subject: agetty: close tty before vhangup() 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 --- term-utils/agetty.c | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) (limited to 'term-utils') diff --git a/term-utils/agetty.c b/term-utils/agetty.c index b2cc24cca..fab83b8ff 100644 --- a/term-utils/agetty.c +++ b/term-utils/agetty.c @@ -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"); -- cgit v1.2.3-55-g7522