login.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Copyright (C) 1996-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <assert.h>
  15. #include <errno.h>
  16. #include <limits.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <stdlib.h>
  20. #include <utmp.h>
  21. #include <shlib-compat.h>
  22. /* Return the result of ttyname in the buffer pointed to by TTY, which should
  23. be of length BUF_LEN. If it is too long to fit in this buffer, a
  24. sufficiently long buffer is allocated using malloc, and returned in TTY.
  25. 0 is returned upon success, -1 otherwise. */
  26. static int
  27. tty_name (int fd, char **tty, size_t buf_len)
  28. {
  29. int rv; /* Return value. 0 = success. */
  30. char *buf = *tty; /* Buffer for ttyname, initially the user's. */
  31. for (;;)
  32. {
  33. char *new_buf;
  34. if (buf_len)
  35. {
  36. rv = __ttyname_r (fd, buf, buf_len);
  37. if (rv != 0 || memchr (buf, '\0', buf_len))
  38. /* We either got an error, or we succeeded and the
  39. returned name fit in the buffer. */
  40. break;
  41. /* Try again with a longer buffer. */
  42. buf_len += buf_len; /* Double it */
  43. }
  44. else
  45. /* No initial buffer; start out by mallocing one. */
  46. buf_len = 128; /* First time guess. */
  47. if (buf != *tty)
  48. /* We've already malloced another buffer at least once. */
  49. new_buf = realloc (buf, buf_len);
  50. else
  51. new_buf = malloc (buf_len);
  52. if (! new_buf)
  53. {
  54. rv = -1;
  55. __set_errno (ENOMEM);
  56. break;
  57. }
  58. buf = new_buf;
  59. }
  60. if (rv == 0)
  61. *tty = buf; /* Return buffer to the user. */
  62. else if (buf != *tty)
  63. free (buf); /* Free what we malloced when returning an error. */
  64. return rv;
  65. }
  66. void
  67. __login (const struct utmp *ut)
  68. {
  69. #ifdef PATH_MAX
  70. char _tty[PATH_MAX + UT_LINESIZE];
  71. #else
  72. char _tty[512 + UT_LINESIZE];
  73. #endif
  74. char *tty = _tty;
  75. int found_tty;
  76. const char *ttyp;
  77. struct utmp copy = *ut;
  78. /* Fill in those fields we supply. */
  79. copy.ut_type = USER_PROCESS;
  80. copy.ut_pid = getpid ();
  81. /* Seek tty. */
  82. found_tty = tty_name (STDIN_FILENO, &tty, sizeof (_tty));
  83. if (found_tty < 0)
  84. found_tty = tty_name (STDOUT_FILENO, &tty, sizeof (_tty));
  85. if (found_tty < 0)
  86. found_tty = tty_name (STDERR_FILENO, &tty, sizeof (_tty));
  87. if (found_tty >= 0)
  88. {
  89. /* We only want to insert the name of the tty without path.
  90. But take care of name like /dev/pts/3. */
  91. if (strncmp (tty, "/dev/", 5) == 0)
  92. ttyp = tty + 5; /* Skip the "/dev/". */
  93. else
  94. ttyp = basename (tty);
  95. /* Position to record for this tty. */
  96. strncpy (copy.ut_line, ttyp, UT_LINESIZE);
  97. /* Tell that we want to use the UTMP file. */
  98. if (__utmpname (_PATH_UTMP) == 0)
  99. {
  100. /* Open UTMP file. */
  101. __setutent ();
  102. /* Write the entry. */
  103. __pututline (&copy);
  104. /* Close UTMP file. */
  105. __endutent ();
  106. }
  107. if (tty != _tty)
  108. free (tty); /* Free buffer malloced by tty_name. */
  109. }
  110. else
  111. /* We provide a default value so that the output does not contain
  112. an random bytes in this field. */
  113. strncpy (copy.ut_line, "???", UT_LINESIZE);
  114. /* Update the WTMP file. Here we have to add a new entry. */
  115. __updwtmp (_PATH_WTMP, &copy);
  116. }
  117. versioned_symbol (libc, __login, login, GLIBC_2_34);
  118. libc_hidden_ver (__login, login)
  119. #if OTHER_SHLIB_COMPAT (libutil, GLIBC_2_0, GLIBC_2_34)
  120. compat_symbol (libutil, __login, login, GLIBC_2_0);
  121. #endif