getty: attempt to steal ctty if don't already have it

This fixes the following classic problem

    login: root
    -sh: can't access tty; job control turned off

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2021-06-04 08:39:48 +02:00
parent 093fa0107a
commit dc36b313ff
+8
View File
@@ -198,6 +198,7 @@ static int getty(char *tty, speed_t speed, char *term, char *user)
{
const char cln[] = "\r\e[2K\n";
char name[33]; /* useradd(1) limit at 32 chars */
pid_t sid;
/*
* Clean up tty name.
@@ -219,6 +220,13 @@ restart:
} else
strlcpy(name, user, sizeof(name));
/* check current session associated with our tty */
sid = tcgetsid(0);
if (sid < 0 || getpid() != sid) {
if (ioctl(STDIN_FILENO, TIOCSCTTY, 1) == -1)
err(1, "failed stealing controlling TTY");
}
if (term && term[0])
setenv("TERM", term, 1);