From 1cf6e936e308166fffdc2d703c4ade2e8cbe5e51 Mon Sep 17 00:00:00 2001 From: Ludwig Nussel Date: Thu, 21 Oct 2021 15:00:59 +0200 Subject: [PATCH] agetty: use getttynam() if available /etc/ttys seems to be a rather archaic concept that is not meant to exist on Linux. Nevertheless it does. glibc has getttynam() which correctly parses /etc/ttys. So let's give it a try before falling back to the built in defaults. One can set the terminal type for a specific tty using e.g.: echo 'ttyS0 "" xterm' > /etc/ttys [kzak@redhat.com: - improve configure.ac part - log error on failed strdup()] Signed-off-by: Karel Zak --- configure.ac | 1 + term-utils/agetty.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/configure.ac b/configure.ac index 60e63592bd..edd29cbb43 100644 --- a/configure.ac +++ b/configure.ac @@ -535,6 +535,7 @@ AC_CHECK_FUNCS([ \ getrandom \ getrlimit \ getsgnam \ + getttynam \ inotify_init \ jrand48 \ lchown \ diff --git a/term-utils/agetty.c b/term-utils/agetty.c index d072d64d3a..55d3734613 100644 --- a/term-utils/agetty.c +++ b/term-utils/agetty.c @@ -56,6 +56,10 @@ # include #endif +#ifdef HAVE_GETTTYNAM +# include +#endif + #if defined(__FreeBSD_kernel__) # include # ifdef HAVE_UTMP_H @@ -1160,6 +1164,18 @@ static void open_tty(char *tty, struct termios *tp, struct options *op) if (tcgetattr(STDIN_FILENO, tp) < 0) log_err(_("%s: failed to get terminal attributes: %m"), tty); +#ifdef HAVE_GETTTYNAM + if (!op->term) { + struct ttyent *ent = getttynam(tty); + /* a bit nasty as it's never freed */ + if (ent && ent->ty_type) { + op->term = strdup(ent->ty_type); + if (!op->term) + log_err(_("failed to allocate memory: %m")); + } + } +#endif + #if defined (__s390__) || defined (__s390x__) if (!op->term) { /* -- 2.47.3