1 From 47831cc02ac0d71c335caecef1753f4c8861277c Mon Sep 17 00:00:00 2001
2 From: tamz <totemz@protonmail.com>
3 Date: Thu, 6 Jan 2022 11:56:58 +0100
4 Subject: [PATCH 1/1] agetty: resolve tty name even if stdin is specified
6 [kzak@redhat.com: - use "const" for options->tty (and friends)
7 as expected by get_terminal_name()]
9 Addresses: https://github.com/util-linux/util-linux/issues/1546
10 Signed-off-by: tamz <totemz@protonmail.com>
11 Signed-off-by: Karel Zak <kzak@redhat.com>
13 term-utils/agetty.c | 26 ++++++++++++++++++--------
14 1 file changed, 18 insertions(+), 8 deletions(-)
16 diff --git a/term-utils/agetty.c b/term-utils/agetty.c
17 index 55d373461..22850786d 100644
18 --- a/term-utils/agetty.c
19 +++ b/term-utils/agetty.c
20 @@ -190,8 +190,8 @@ struct options {
21 char *chroot; /* Chroot before the login */
22 char *login; /* login program */
23 char *logopt; /* options for login program */
24 - char *tty; /* name of tty */
25 - char *vcline; /* line of virtual console */
26 + const char *tty; /* name of tty */
27 + const char *vcline; /* line of virtual console */
28 char *term; /* terminal type */
29 char *initstring; /* modem init string */
30 char *issue; /* alternative issue file or directory */
31 @@ -203,6 +203,7 @@ struct options {
32 int numspeed; /* number of baud rates to try */
33 int clocal; /* CLOCAL_MODE_* */
34 int kbmode; /* Keyboard mode if virtual console */
35 + int tty_is_stdin; /* is the tty the standard input stream */
36 speed_t speeds[MAX_SPEED]; /* baud rates to be tried */
39 @@ -319,7 +320,7 @@ static void init_special_char(char* arg, struct options *op);
40 static void parse_args(int argc, char **argv, struct options *op);
41 static void parse_speeds(struct options *op, char *arg);
42 static void update_utmp(struct options *op);
43 -static void open_tty(char *tty, struct termios *tp, struct options *op);
44 +static void open_tty(const char *tty, struct termios *tp, struct options *op);
45 static void termio_init(struct options *op, struct termios *tp);
46 static void reset_vc(const struct options *op, struct termios *tp, int canon);
47 static void auto_baud(struct termios *tp);
48 @@ -922,6 +923,15 @@ static void parse_args(int argc, char **argv, struct options *op)
52 + /* resolve the tty path in case it was provided as stdin */
53 + if (strcmp(op->tty, "-") == 0) {
54 + op->tty_is_stdin = 1;
55 + int fd = get_terminal_name(NULL, &op->tty, NULL);
57 + log_warn(_("could not get terminal name: %d"), fd);
61 /* On virtual console remember the line which is used for */
62 if (strncmp(op->tty, "tty", 3) == 0 &&
63 strspn(op->tty + 3, "0123456789") == strlen(op->tty+3))
64 @@ -962,8 +972,8 @@ static void update_utmp(struct options *op)
67 pid_t sid = getsid(0);
68 - char *vcline = op->vcline;
69 - char *line = op->tty;
70 + const char *vcline = op->vcline;
71 + const char *line = op->tty;
75 @@ -1002,7 +1012,7 @@ static void update_utmp(struct options *op)
76 str2memcpy(ut.ut_id, vcline, sizeof(ut.ut_id));
78 size_t len = strlen(line);
81 if (len >= sizeof(ut.ut_id))
82 ptr = line + len - sizeof(ut.ut_id);
84 @@ -1030,7 +1040,7 @@ static void update_utmp(struct options *op)
85 #endif /* SYSV_STYLE */
87 /* Set up tty as stdin, stdout & stderr. */
88 -static void open_tty(char *tty, struct termios *tp, struct options *op)
89 +static void open_tty(const char *tty, struct termios *tp, struct options *op)
91 const pid_t pid = getpid();
93 @@ -1040,7 +1050,7 @@ static void open_tty(char *tty, struct termios *tp, struct options *op)
95 /* Set up new standard input, unless we are given an already opened port. */
97 - if (strcmp(tty, "-") != 0) {
98 + if (!op->tty_is_stdin) {
100 struct group *gr = NULL;