Blame SOURCES/0043-agetty-resolve-tty-name-even-if-stdin-is-specified.patch

321543
From b77ac3951932d2ea8bdba2b800380b3e70f8eca2 Mon Sep 17 00:00:00 2001
321543
From: tamz <totemz@protonmail.com>
321543
Date: Thu, 6 Jan 2022 11:56:58 +0100
321543
Subject: agetty: resolve tty name even if stdin is specified
321543
321543
[kzak@redhat.com: - use "const" for options->tty (and friends)
321543
                    as expected by get_terminal_name()]
321543
321543
Addresses: https://github.com/util-linux/util-linux/issues/1546
321543
Signed-off-by: tamz <totemz@protonmail.com>
321543
Signed-off-by: Karel Zak <kzak@redhat.com>
321543
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=2156946
321543
Upstream: http://github.com/util-linux/util-linux/commit/47831cc02ac0d71c335caecef1753f4c8861277c
321543
---
321543
 term-utils/agetty.c | 26 ++++++++++++++++++--------
321543
 1 file changed, 18 insertions(+), 8 deletions(-)
321543
321543
diff --git a/term-utils/agetty.c b/term-utils/agetty.c
321543
index 3b3d5101a..e65cbdeaf 100644
321543
--- a/term-utils/agetty.c
321543
+++ b/term-utils/agetty.c
321543
@@ -186,8 +186,8 @@ struct options {
321543
 	char *chroot;			/* Chroot before the login */
321543
 	char *login;			/* login program */
321543
 	char *logopt;			/* options for login program */
321543
-	char *tty;			/* name of tty */
321543
-	char *vcline;			/* line of virtual console */
321543
+	const char *tty;		/* name of tty */
321543
+	const char *vcline;		/* line of virtual console */
321543
 	char *term;			/* terminal type */
321543
 	char *initstring;		/* modem init string */
321543
 	char *issue;			/* alternative issue file or directory */
321543
@@ -199,6 +199,7 @@ struct options {
321543
 	int numspeed;			/* number of baud rates to try */
321543
 	int clocal;			/* CLOCAL_MODE_* */
321543
 	int kbmode;			/* Keyboard mode if virtual console */
321543
+	int tty_is_stdin;		/* is the tty the standard input stream */
321543
 	speed_t speeds[MAX_SPEED];	/* baud rates to be tried */
321543
 };
321543
 
321543
@@ -315,7 +316,7 @@ static void init_special_char(char* arg, struct options *op);
321543
 static void parse_args(int argc, char **argv, struct options *op);
321543
 static void parse_speeds(struct options *op, char *arg);
321543
 static void update_utmp(struct options *op);
321543
-static void open_tty(char *tty, struct termios *tp, struct options *op);
321543
+static void open_tty(const char *tty, struct termios *tp, struct options *op);
321543
 static void termio_init(struct options *op, struct termios *tp);
321543
 static void reset_vc(const struct options *op, struct termios *tp, int canon);
321543
 static void auto_baud(struct termios *tp);
321543
@@ -918,6 +919,15 @@ static void parse_args(int argc, char **argv, struct options *op)
321543
 		}
321543
 	}
321543
 
321543
+	/* resolve the tty path in case it was provided as stdin */
321543
+	if (strcmp(op->tty, "-") == 0) {
321543
+		op->tty_is_stdin = 1;
321543
+		int fd = get_terminal_name(NULL, &op->tty, NULL);
321543
+		if (fd < 0) {
321543
+			log_warn(_("could not get terminal name: %d"), fd);
321543
+		}
321543
+	}
321543
+
321543
 	/* On virtual console remember the line which is used for */
321543
 	if (strncmp(op->tty, "tty", 3) == 0 &&
321543
 	    strspn(op->tty + 3, "0123456789") == strlen(op->tty+3))
321543
@@ -958,8 +968,8 @@ static void update_utmp(struct options *op)
321543
 	time_t t;
321543
 	pid_t pid = getpid();
321543
 	pid_t sid = getsid(0);
321543
-	char *vcline = op->vcline;
321543
-	char *line   = op->tty;
321543
+	const char *vcline = op->vcline;
321543
+	const char *line = op->tty;
321543
 	struct utmpx *utp;
321543
 
321543
 	/*
321543
@@ -998,7 +1008,7 @@ static void update_utmp(struct options *op)
321543
 			str2memcpy(ut.ut_id, vcline, sizeof(ut.ut_id));
321543
 		else {
321543
 			size_t len = strlen(line);
321543
-			char * ptr;
321543
+			const char * ptr;
321543
 			if (len >= sizeof(ut.ut_id))
321543
 				ptr = line + len - sizeof(ut.ut_id);
321543
 			else
321543
@@ -1026,7 +1036,7 @@ static void update_utmp(struct options *op)
321543
 #endif				/* SYSV_STYLE */
321543
 
321543
 /* Set up tty as stdin, stdout & stderr. */
321543
-static void open_tty(char *tty, struct termios *tp, struct options *op)
321543
+static void open_tty(const char *tty, struct termios *tp, struct options *op)
321543
 {
321543
 	const pid_t pid = getpid();
321543
 	int closed = 0;
321543
@@ -1036,7 +1046,7 @@ static void open_tty(char *tty, struct termios *tp, struct options *op)
321543
 
321543
 	/* Set up new standard input, unless we are given an already opened port. */
321543
 
321543
-	if (strcmp(tty, "-") != 0) {
321543
+	if (!op->tty_is_stdin) {
321543
 		char buf[PATH_MAX+1];
321543
 		struct group *gr = NULL;
321543
 		struct stat st;
321543
-- 
321543
2.39.1
321543