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