Rework tty setup in getty(8)

This commit is contained in:
sin 2014-11-12 11:49:44 +00:00
parent facc3a871f
commit 6da76775cb

29
getty.c
View File

@ -63,32 +63,31 @@ main(int argc, char *argv[])
eprintf("open %s:", tty); eprintf("open %s:", tty);
if (isatty(fd) == 0) if (isatty(fd) == 0)
eprintf("%s is not a tty\n", tty); eprintf("%s is not a tty\n", tty);
if (ioctl(fd, TIOCSCTTY, (void *)1) == 0)
vhangup();
else
weprintf("TIOCSCTTY: could not set controlling tty\n");
/* steal the controlling terminal if necessary */
if (ioctl(fd, TIOCSCTTY, (void *)1) != 0)
weprintf("TIOCSCTTY: could not set controlling tty\n");
vhangup();
close(fd); close(fd);
fd = open(tty, O_RDWR); fd = open(tty, O_RDWR);
if (fd < 0) if (fd < 0)
eprintf("open %s:", tty); eprintf("open %s:", tty);
if (dup2(fd, STDIN_FILENO) != STDIN_FILENO) dup2(fd, STDIN_FILENO);
eprintf("dup2:"); dup2(fd, STDOUT_FILENO);
if (dup2(fd, STDOUT_FILENO) != STDOUT_FILENO) dup2(fd, STDERR_FILENO);
eprintf("dup2:"); if (fchown(fd, 0, 0) < 0)
if (dup2(fd, STDERR_FILENO) != STDERR_FILENO) weprintf("fchown %s:", tty);
eprintf("dup2:"); if (fchmod(fd, 0600) < 0)
weprintf("fchmod %s:", tty);
if (fd > 2)
close(fd);
sa.sa_handler = SIG_DFL; sa.sa_handler = SIG_DFL;
sa.sa_flags = 0; sa.sa_flags = 0;
sigemptyset(&sa.sa_mask); sigemptyset(&sa.sa_mask);
sigaction(SIGHUP, &sa, NULL); sigaction(SIGHUP, &sa, NULL);
if (fchown(fd, 0, 0) < 0)
eprintf("fchown %s:", tty);
if (fchmod(fd, 0600) < 0)
eprintf("chmod %s:", tty);
/* Clear all utmp entries for this tty */ /* Clear all utmp entries for this tty */
fp = fopen(UTMP_PATH, "r+"); fp = fopen(UTMP_PATH, "r+");
if (fp) { if (fp) {