ttyname_r.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* Copyright (C) 1991-2019 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. <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <limits.h>
  16. #include <stddef.h>
  17. #include <dirent.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <termios.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <_itoa.h>
  25. #include "ttyname.h"
  26. static int getttyname_r (char *buf, size_t buflen,
  27. const struct stat64 *mytty, int save,
  28. int *dostat);
  29. static int
  30. attribute_compat_text_section
  31. getttyname_r (char *buf, size_t buflen, const struct stat64 *mytty,
  32. int save, int *dostat)
  33. {
  34. struct stat64 st;
  35. DIR *dirstream;
  36. struct dirent64 *d;
  37. size_t devlen = strlen (buf);
  38. dirstream = __opendir (buf);
  39. if (dirstream == NULL)
  40. {
  41. *dostat = -1;
  42. return errno;
  43. }
  44. while ((d = __readdir64 (dirstream)) != NULL)
  45. if ((d->d_fileno == mytty->st_ino || *dostat)
  46. && strcmp (d->d_name, "stdin")
  47. && strcmp (d->d_name, "stdout")
  48. && strcmp (d->d_name, "stderr"))
  49. {
  50. char *cp;
  51. size_t needed = _D_EXACT_NAMLEN (d) + 1;
  52. if (needed > buflen)
  53. {
  54. *dostat = -1;
  55. (void) __closedir (dirstream);
  56. __set_errno (ERANGE);
  57. return ERANGE;
  58. }
  59. cp = __stpncpy (buf + devlen, d->d_name, needed);
  60. cp[0] = '\0';
  61. if (__xstat64 (_STAT_VER, buf, &st) == 0
  62. && is_mytty (mytty, &st))
  63. {
  64. (void) __closedir (dirstream);
  65. __set_errno (save);
  66. return 0;
  67. }
  68. }
  69. (void) __closedir (dirstream);
  70. __set_errno (save);
  71. /* It is not clear what to return in this case. `isatty' says FD
  72. refers to a TTY but no entry in /dev has this inode. */
  73. return ENOTTY;
  74. }
  75. /* Store at most BUFLEN character of the pathname of the terminal FD is
  76. open on in BUF. Return 0 on success, otherwise an error number. */
  77. int
  78. __ttyname_r (int fd, char *buf, size_t buflen)
  79. {
  80. char procname[30];
  81. struct stat64 st, st1;
  82. int dostat = 0;
  83. int doispty = 0;
  84. int save = errno;
  85. /* Test for the absolute minimal size. This makes life easier inside
  86. the loop. */
  87. if (!buf)
  88. {
  89. __set_errno (EINVAL);
  90. return EINVAL;
  91. }
  92. if (buflen < sizeof ("/dev/pts/"))
  93. {
  94. __set_errno (ERANGE);
  95. return ERANGE;
  96. }
  97. /* isatty check, tcgetattr is used because it sets the correct
  98. errno (EBADF resp. ENOTTY) on error. */
  99. struct termios term;
  100. if (__glibc_unlikely (__tcgetattr (fd, &term) < 0))
  101. return errno;
  102. if (__fxstat64 (_STAT_VER, fd, &st) < 0)
  103. return errno;
  104. /* We try using the /proc filesystem. */
  105. *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0';
  106. ssize_t ret = __readlink (procname, buf, buflen - 1);
  107. if (__glibc_unlikely (ret == -1 && errno == ENAMETOOLONG))
  108. {
  109. __set_errno (ERANGE);
  110. return ERANGE;
  111. }
  112. if (__glibc_likely (ret != -1))
  113. {
  114. #define UNREACHABLE_LEN strlen ("(unreachable)")
  115. if (ret > UNREACHABLE_LEN
  116. && memcmp (buf, "(unreachable)", UNREACHABLE_LEN) == 0)
  117. {
  118. memmove (buf, buf + UNREACHABLE_LEN, ret - UNREACHABLE_LEN);
  119. ret -= UNREACHABLE_LEN;
  120. }
  121. /* readlink need not terminate the string. */
  122. buf[ret] = '\0';
  123. /* Verify readlink result, fall back on iterating through devices. */
  124. if (buf[0] == '/'
  125. && __xstat64 (_STAT_VER, buf, &st1) == 0
  126. && is_mytty (&st, &st1))
  127. return 0;
  128. doispty = 1;
  129. }
  130. /* Prepare the result buffer. */
  131. memcpy (buf, "/dev/pts/", sizeof ("/dev/pts/"));
  132. buflen -= sizeof ("/dev/pts/") - 1;
  133. if (__xstat64 (_STAT_VER, buf, &st1) == 0 && S_ISDIR (st1.st_mode))
  134. {
  135. ret = getttyname_r (buf, buflen, &st, save,
  136. &dostat);
  137. }
  138. else
  139. {
  140. __set_errno (save);
  141. ret = ENOENT;
  142. }
  143. if (ret && dostat != -1)
  144. {
  145. buf[sizeof ("/dev/") - 1] = '\0';
  146. buflen += sizeof ("pts/") - 1;
  147. ret = getttyname_r (buf, buflen, &st, save,
  148. &dostat);
  149. }
  150. if (ret && dostat != -1)
  151. {
  152. buf[sizeof ("/dev/") - 1] = '\0';
  153. dostat = 1;
  154. ret = getttyname_r (buf, buflen, &st,
  155. save, &dostat);
  156. }
  157. if (ret && doispty && is_pty (&st))
  158. {
  159. /* We failed to figure out the TTY's name, but we can at least
  160. signal that we did verify that it really is a PTY slave.
  161. This happens when we have inherited the file descriptor from
  162. a different mount namespace. */
  163. __set_errno (ENODEV);
  164. return ENODEV;
  165. }
  166. return ret;
  167. }
  168. weak_alias (__ttyname_r, ttyname_r)