util-linux/backport-login-Restore-tty-size-after-calling-vhangup.patch
2023-11-28 16:33:43 +08:00

59 lines
1.8 KiB
Diff

From 7e58b71dfa9bf27f574fd79424f56206f44fa806 Mon Sep 17 00:00:00 2001
From: Daan De Meyer <daan.j.demeyer@gmail.com>
Date: Sat, 30 Oct 2021 15:56:14 +0100
Subject: [PATCH] login: Restore tty size after calling vhangup()
If login receives the tty to work on via stdin, stdout and stderr,
login might end up closing the remaining open file descriptors to
the tty just before it calls vhangup(). When the last open file
descriptors to a tty are closed, it's configured size is reset to
0x0. To avoid this from happening, save the size before closing
the stdin, stdout and stderr file descriptors and reapply the size
after the tty is re-opened.
Fixes #1484
---
login-utils/login.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/login-utils/login.c b/login-utils/login.c
index c08d380..648d3d2 100644
--- a/login-utils/login.c
+++ b/login-utils/login.c
@@ -362,6 +362,7 @@ static void init_tty(struct login_context *cxt)
{
struct stat st;
struct termios tt, ttt;
+ struct winsize ws;
cxt->tty_mode = (mode_t) getlogindefs_num("TTYPERM", TTY_MODE);
@@ -392,6 +393,12 @@ static void init_tty(struct login_context *cxt)
}
#endif
+ /* The TTY size might be reset to 0x0 by the kernel when we close the stdin/stdout/stderr file
+ * descriptors so let's save the size now so we can reapply it later */
+ memset(&ws, 0, sizeof(struct winsize));
+ if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0)
+ syslog(LOG_WARNING, _("TIOCGWINSZ ioctl failed: %m"));
+
tcgetattr(0, &tt);
ttt = tt;
ttt.c_cflag &= ~HUPCL;
@@ -423,6 +430,11 @@ static void init_tty(struct login_context *cxt)
/* restore tty modes */
tcsetattr(0, TCSAFLUSH, &tt);
+
+ /* Restore tty size */
+ if (ws.ws_row > 0 || ws.ws_col > 0)
+ if (ioctl(STDIN_FILENO, TIOCSWINSZ, &ws) < 0)
+ syslog(LOG_WARNING, _("TIOCSWINSZ ioctl failed: %m"));
}
--
2.27.0