summaryrefslogtreecommitdiff
path: root/nonsystemd/util-linux/0001-agetty-resolve-tty-name-even-if-stdin-is-specified.patch
blob: 4f317b0347355818c0daca66ced500b34d4e6071 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
From 47831cc02ac0d71c335caecef1753f4c8861277c Mon Sep 17 00:00:00 2001
From: tamz <totemz@protonmail.com>
Date: Thu, 6 Jan 2022 11:56:58 +0100
Subject: [PATCH 1/1] agetty: resolve tty name even if stdin is specified

[kzak@redhat.com: - use "const" for options->tty (and friends)
                    as expected by get_terminal_name()]

Addresses: https://github.com/util-linux/util-linux/issues/1546
Signed-off-by: tamz <totemz@protonmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
---
 term-utils/agetty.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/term-utils/agetty.c b/term-utils/agetty.c
index 55d373461..22850786d 100644
--- a/term-utils/agetty.c
+++ b/term-utils/agetty.c
@@ -190,8 +190,8 @@ struct options {
 	char *chroot;			/* Chroot before the login */
 	char *login;			/* login program */
 	char *logopt;			/* options for login program */
-	char *tty;			/* name of tty */
-	char *vcline;			/* line of virtual console */
+	const char *tty;		/* name of tty */
+	const char *vcline;		/* line of virtual console */
 	char *term;			/* terminal type */
 	char *initstring;		/* modem init string */
 	char *issue;			/* alternative issue file or directory */
@@ -203,6 +203,7 @@ struct options {
 	int numspeed;			/* number of baud rates to try */
 	int clocal;			/* CLOCAL_MODE_* */
 	int kbmode;			/* Keyboard mode if virtual console */
+	int tty_is_stdin;		/* is the tty the standard input stream */
 	speed_t speeds[MAX_SPEED];	/* baud rates to be tried */
 };
 
@@ -319,7 +320,7 @@ static void init_special_char(char* arg, struct options *op);
 static void parse_args(int argc, char **argv, struct options *op);
 static void parse_speeds(struct options *op, char *arg);
 static void update_utmp(struct options *op);
-static void open_tty(char *tty, struct termios *tp, struct options *op);
+static void open_tty(const char *tty, struct termios *tp, struct options *op);
 static void termio_init(struct options *op, struct termios *tp);
 static void reset_vc(const struct options *op, struct termios *tp, int canon);
 static void auto_baud(struct termios *tp);
@@ -922,6 +923,15 @@ static void parse_args(int argc, char **argv, struct options *op)
 		}
 	}
 
+	/* resolve the tty path in case it was provided as stdin */
+	if (strcmp(op->tty, "-") == 0) {
+		op->tty_is_stdin = 1;
+		int fd = get_terminal_name(NULL, &op->tty, NULL);
+		if (fd < 0) {
+			log_warn(_("could not get terminal name: %d"), fd);
+		}
+	}
+
 	/* On virtual console remember the line which is used for */
 	if (strncmp(op->tty, "tty", 3) == 0 &&
 	    strspn(op->tty + 3, "0123456789") == strlen(op->tty+3))
@@ -962,8 +972,8 @@ static void update_utmp(struct options *op)
 	time_t t;
 	pid_t pid = getpid();
 	pid_t sid = getsid(0);
-	char *vcline = op->vcline;
-	char *line   = op->tty;
+	const char *vcline = op->vcline;
+	const char *line = op->tty;
 	struct utmpx *utp;
 
 	/*
@@ -1002,7 +1012,7 @@ static void update_utmp(struct options *op)
 			str2memcpy(ut.ut_id, vcline, sizeof(ut.ut_id));
 		else {
 			size_t len = strlen(line);
-			char * ptr;
+			const char * ptr;
 			if (len >= sizeof(ut.ut_id))
 				ptr = line + len - sizeof(ut.ut_id);
 			else
@@ -1030,7 +1040,7 @@ static void update_utmp(struct options *op)
 #endif				/* SYSV_STYLE */
 
 /* Set up tty as stdin, stdout & stderr. */
-static void open_tty(char *tty, struct termios *tp, struct options *op)
+static void open_tty(const char *tty, struct termios *tp, struct options *op)
 {
 	const pid_t pid = getpid();
 	int closed = 0;
@@ -1040,7 +1050,7 @@ static void open_tty(char *tty, struct termios *tp, struct options *op)
 
 	/* Set up new standard input, unless we are given an already opened port. */
 
-	if (strcmp(tty, "-") != 0) {
+	if (!op->tty_is_stdin) {
 		char buf[PATH_MAX+1];
 		struct group *gr = NULL;
 		struct stat st;
-- 
2.34.1