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 <kzak@redhat.com>
This commit is contained in:
parent
4459448fbd
commit
783b08fc1c
|
@ -264,7 +264,7 @@ static void login_options_to_argv(char *argv[], int *argc, char *str, char *user
|
||||||
static char *fakehost;
|
static char *fakehost;
|
||||||
|
|
||||||
#ifdef DEBUGGING
|
#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;
|
FILE *dbf;
|
||||||
#else
|
#else
|
||||||
#define debug(s) do { ; } while (0)
|
#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)
|
static void open_tty(char *tty, struct termios *tp, struct options *op)
|
||||||
{
|
{
|
||||||
const pid_t pid = getpid();
|
const pid_t pid = getpid();
|
||||||
int serial;
|
int serial, closed = 0;
|
||||||
|
|
||||||
/* Set up new standard input, unless we are given an already opened port. */
|
/* Set up new standard input, unless we are given an already opened port. */
|
||||||
|
|
||||||
|
sleep(10);
|
||||||
|
|
||||||
if (strcmp(tty, "-") != 0) {
|
if (strcmp(tty, "-") != 0) {
|
||||||
char buf[PATH_MAX+1];
|
char buf[PATH_MAX+1];
|
||||||
struct group *gr = NULL;
|
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);
|
log_warn("/dev/%s: cannot get controlling tty: %m", tty);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (op->flags & F_HANGUP) {
|
|
||||||
/*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
if (vhangup())
|
|
||||||
log_err("/dev/%s: vhangup() failed: %m", tty);
|
|
||||||
if (ioctl(fd, TIOCNOTTY))
|
|
||||||
debug("TIOCNOTTY ioctl failed\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
close(fd);
|
|
||||||
close(STDIN_FILENO);
|
close(STDIN_FILENO);
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
|
||||||
|
if (op->flags & F_HANGUP) {
|
||||||
|
|
||||||
|
if (ioctl(fd, TIOCNOTTY))
|
||||||
|
debug("TIOCNOTTY ioctl failed\n");
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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);
|
||||||
|
} else
|
||||||
|
close(fd);
|
||||||
|
|
||||||
debug("open(2)\n");
|
debug("open(2)\n");
|
||||||
if (open(buf, O_RDWR|O_NOCTTY|O_NONBLOCK, 0) != 0)
|
if (open(buf, O_RDWR|O_NOCTTY|O_NONBLOCK, 0) != 0)
|
||||||
log_err(_("/dev/%s: cannot open as standard input: %m"), tty);
|
log_err(_("/dev/%s: cannot open as standard input: %m"), tty);
|
||||||
|
|
||||||
if (((tid = tcgetsid(STDIN_FILENO)) < 0) || (pid != tid)) {
|
if (((tid = tcgetsid(STDIN_FILENO)) < 0) || (pid != tid)) {
|
||||||
if (ioctl(STDIN_FILENO, TIOCSCTTY, 1) == -1)
|
if (ioctl(STDIN_FILENO, TIOCSCTTY, 1) == -1)
|
||||||
log_warn("/dev/%s: cannot get controlling tty: %m", tty);
|
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);
|
log_warn("/dev/%s: cannot set process group: %m", tty);
|
||||||
|
|
||||||
/* Get rid of the present outputs. */
|
/* Get rid of the present outputs. */
|
||||||
close(STDOUT_FILENO);
|
if (!closed) {
|
||||||
close(STDERR_FILENO);
|
close(STDOUT_FILENO);
|
||||||
errno = 0;
|
close(STDERR_FILENO);
|
||||||
|
errno = 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* Set up standard output and standard error file descriptors. */
|
/* Set up standard output and standard error file descriptors. */
|
||||||
debug("duping\n");
|
debug("duping\n");
|
||||||
|
|
Loading…
Reference in New Issue