OSDN Git Service

upg
[joborun/jobcore.git] / util-linux / 0001-agetty-resolve-tty-name-even-if-stdin-is-specified.patch
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
5
6 [kzak@redhat.com: - use "const" for options->tty (and friends)
7                     as expected by get_terminal_name()]
8
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>
12 ---
13  term-utils/agetty.c | 26 ++++++++++++++++++--------
14  1 file changed, 18 insertions(+), 8 deletions(-)
15
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 */
37  };
38  
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)
49                 }
50         }
51  
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);
56 +               if (fd < 0) {
57 +                       log_warn(_("could not get terminal name: %d"), fd);
58 +               }
59 +       }
60 +
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)
65         time_t t;
66         pid_t pid = getpid();
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;
72         struct utmpx *utp;
73  
74         /*
75 @@ -1002,7 +1012,7 @@ static void update_utmp(struct options *op)
76                         str2memcpy(ut.ut_id, vcline, sizeof(ut.ut_id));
77                 else {
78                         size_t len = strlen(line);
79 -                       char * ptr;
80 +                       const char * ptr;
81                         if (len >= sizeof(ut.ut_id))
82                                 ptr = line + len - sizeof(ut.ut_id);
83                         else
84 @@ -1030,7 +1040,7 @@ static void update_utmp(struct options *op)
85  #endif                         /* SYSV_STYLE */
86  
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)
90  {
91         const pid_t pid = getpid();
92         int closed = 0;
93 @@ -1040,7 +1050,7 @@ static void open_tty(char *tty, struct termios *tp, struct options *op)
94  
95         /* Set up new standard input, unless we are given an already opened port. */
96  
97 -       if (strcmp(tty, "-") != 0) {
98 +       if (!op->tty_is_stdin) {
99                 char buf[PATH_MAX+1];
100                 struct group *gr = NULL;
101                 struct stat st;
102 -- 
103 2.34.1
104