ptsname.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* Copyright (C) 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <paths.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/stat.h>
  22. #include <sys/sysmacros.h>
  23. #include <termios.h>
  24. #include <unistd.h>
  25. #include <bits/uClibc_uintmaxtostr.h>
  26. #if !defined __UNIX98PTY_ONLY__
  27. /* Check if DEV corresponds to a master pseudo terminal device. */
  28. #define MASTER_P(Dev) \
  29. (major ((Dev)) == 2 \
  30. || (major ((Dev)) == 4 && minor ((Dev)) >= 128 && minor ((Dev)) < 192) \
  31. || (major ((Dev)) >= 128 && major ((Dev)) < 136))
  32. /* Check if DEV corresponds to a slave pseudo terminal device. */
  33. #define SLAVE_P(Dev) \
  34. (major ((Dev)) == 3 \
  35. || (major ((Dev)) == 4 && minor ((Dev)) >= 192 && minor ((Dev)) < 256) \
  36. || (major ((Dev)) >= 136 && major ((Dev)) < 144))
  37. /* Note that major number 4 corresponds to the old BSD style pseudo
  38. terminal devices. As of Linux 2.1.115 these are no longer
  39. supported. They have been replaced by major numbers 2 (masters)
  40. and 3 (slaves). */
  41. /* The are declared in getpt.c. */
  42. extern const char __libc_ptyname1[] attribute_hidden;
  43. extern const char __libc_ptyname2[] attribute_hidden;
  44. #endif
  45. /* Directory where we can find the slave pty nodes. */
  46. #define _PATH_DEVPTS "/dev/pts/"
  47. /* Store at most BUFLEN characters of the pathname of the slave pseudo
  48. terminal associated with the master FD is open on in BUF.
  49. Return 0 on success, otherwise an error number. */
  50. int ptsname_r (int fd, char *buf, size_t buflen)
  51. {
  52. int save_errno = errno;
  53. #if !defined __UNIX98PTY_ONLY__
  54. struct stat st;
  55. #endif
  56. int ptyno;
  57. if (buf == NULL)
  58. {
  59. errno = EINVAL;
  60. return EINVAL;
  61. }
  62. #if !defined __UNIX98PTY_ONLY__
  63. if (!isatty (fd))
  64. {
  65. errno = ENOTTY;
  66. return ENOTTY;
  67. }
  68. #elif !defined TIOCGPTN
  69. # error "__UNIX98PTY_ONLY__ enabled but TIOCGPTN ioctl not supported by your kernel."
  70. #endif
  71. #ifdef TIOCGPTN
  72. if (ioctl (fd, TIOCGPTN, &ptyno) == 0)
  73. {
  74. /* Buffer we use to print the number in. */
  75. char numbuf[__BUFLEN_INT10TOSTR];
  76. static const char devpts[] = _PATH_DEVPTS;
  77. char *p;
  78. p = _int10tostr(&numbuf[sizeof numbuf - 1], ptyno);
  79. if (buflen < sizeof(devpts) + (size_t)(&numbuf[sizeof(numbuf) - 1] - p))
  80. {
  81. errno = ERANGE;
  82. return ERANGE;
  83. }
  84. strcpy (buf, devpts);
  85. strcat (buf, p);
  86. /* Note: Don't bother with stat on the slave name and checking the
  87. driver's major device number - the ioctl above succeeded so
  88. we know the fd was a Unix'98 master and the /dev/pts/ prefix
  89. is set by definition. If the name isn't really a slave PTY,
  90. the system is misconfigured anyway - something else will fail
  91. later.
  92. */
  93. errno = save_errno;
  94. return 0;
  95. }
  96. #endif
  97. #if defined __UNIX98PTY_ONLY__
  98. else
  99. {
  100. /* If the ioctl fails it wasn't a Unix 98 master PTY */
  101. errno = ENOTTY;
  102. return ENOTTY;
  103. }
  104. #else
  105. # if defined TIOCGPTN
  106. else if (errno == EINVAL)
  107. # endif
  108. {
  109. char *p;
  110. if (buflen < strlen (_PATH_TTY) + 3)
  111. {
  112. errno = ERANGE;
  113. return ERANGE;
  114. }
  115. if (fstat (fd, &st) < 0)
  116. return errno;
  117. /* Check if FD really is a master pseudo terminal. */
  118. if (! MASTER_P (st.st_rdev))
  119. {
  120. errno = ENOTTY;
  121. return ENOTTY;
  122. }
  123. ptyno = minor (st.st_rdev);
  124. /* This is for the old BSD pseudo terminals. As of Linux
  125. 2.1.115 these are no longer supported. */
  126. if (major (st.st_rdev) == 4)
  127. ptyno -= 128;
  128. if (ptyno / 16 >= strlen (__libc_ptyname1))
  129. {
  130. errno = ENOTTY;
  131. return ENOTTY;
  132. }
  133. strcpy (buf, _PATH_TTY);
  134. p = buf + strlen (buf);
  135. p[0] = __libc_ptyname1[ptyno / 16];
  136. p[1] = __libc_ptyname2[ptyno % 16];
  137. p[2] = '\0';
  138. }
  139. if (stat(buf, &st) < 0)
  140. return errno;
  141. /* Check if the name we're about to return really corresponds to a
  142. slave pseudo terminal. */
  143. if (! S_ISCHR (st.st_mode) || ! SLAVE_P (st.st_rdev))
  144. {
  145. /* This really is a configuration problem. */
  146. errno = ENOTTY;
  147. return ENOTTY;
  148. }
  149. #endif
  150. errno = save_errno;
  151. return 0;
  152. }
  153. libc_hidden_def(ptsname_r)
  154. /* Return the pathname of the pseudo terminal slave assoicated with
  155. the master FD is open on, or NULL on errors.
  156. The returned storage is good until the next call to this function. */
  157. char *
  158. ptsname (int fd)
  159. {
  160. static char buffer[sizeof (_PATH_DEVPTS) + 20];
  161. return ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
  162. }