0001-agetty-resolve-tty-name-even-if-stdin-is-specified.patch 3.8 KB

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