From dc36b313ff120430c0644b6c422969fd35674395 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 4 Jun 2021 08:39:48 +0200 Subject: [PATCH] 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 --- src/getty.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/getty.c b/src/getty.c index ab937486..b1313180 100644 --- a/src/getty.c +++ b/src/getty.c @@ -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);