ptsname.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #define _ISOC99_SOURCE
  17. #include <stdio.h>
  18. #include <errno.h>
  19. #include <paths.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/stat.h>
  24. #include <sys/sysmacros.h>
  25. #include <termios.h>
  26. #include <unistd.h>
  27. #include <bits/uClibc_uintmaxtostr.h>
  28. libc_hidden_proto(strcat)
  29. libc_hidden_proto(strcpy)
  30. libc_hidden_proto(strlen)
  31. libc_hidden_proto(isatty)
  32. libc_hidden_proto(ioctl)
  33. libc_hidden_proto(fstat)
  34. libc_hidden_proto(stat)
  35. #if !defined __UNIX98PTY_ONLY__
  36. /* Check if DEV corresponds to a master pseudo terminal device. */
  37. #define MASTER_P(Dev) \
  38. (major ((Dev)) == 2 \
  39. || (major ((Dev)) == 4 && minor ((Dev)) >= 128 && minor ((Dev)) < 192) \
  40. || (major ((Dev)) >= 128 && major ((Dev)) < 136))
  41. /* Check if DEV corresponds to a slave pseudo terminal device. */
  42. #define SLAVE_P(Dev) \
  43. (major ((Dev)) == 3 \
  44. || (major ((Dev)) == 4 && minor ((Dev)) >= 192 && minor ((Dev)) < 256) \
  45. || (major ((Dev)) >= 136 && major ((Dev)) < 144))
  46. /* Note that major number 4 corresponds to the old BSD style pseudo
  47. terminal devices. As of Linux 2.1.115 these are no longer
  48. supported. They have been replaced by major numbers 2 (masters)
  49. and 3 (slaves). */
  50. /* The are declared in getpt.c. */
  51. extern const char __libc_ptyname1[] attribute_hidden;
  52. extern const char __libc_ptyname2[] attribute_hidden;
  53. #endif
  54. /* Directory where we can find the slave pty nodes. */
  55. #define _PATH_DEVPTS "/dev/pts/"
  56. /* Store at most BUFLEN characters of the pathname of the slave pseudo
  57. terminal associated with the master FD is open on in BUF.
  58. Return 0 on success, otherwise an error number. */
  59. libc_hidden_proto(ptsname_r)
  60. int ptsname_r (int fd, char *buf, size_t buflen)
  61. {
  62. int save_errno = errno;
  63. #if !defined __UNIX98PTY_ONLY__
  64. struct stat st;
  65. #endif
  66. int ptyno;
  67. if (buf == NULL)
  68. {
  69. errno = EINVAL;
  70. return EINVAL;
  71. }
  72. #if !defined __UNIX98PTY_ONLY__
  73. if (!isatty (fd))
  74. {
  75. errno = ENOTTY;
  76. return ENOTTY;
  77. }
  78. #elif !defined TIOCGPTN
  79. # error "__UNIX98PTY_ONLY__ enabled but TIOCGPTN ioctl not supported by your kernel."
  80. #endif
  81. #ifdef TIOCGPTN
  82. if (ioctl (fd, TIOCGPTN, &ptyno) == 0)
  83. {
  84. /* Buffer we use to print the number in. */
  85. char numbuf[__BUFLEN_INT10TOSTR];
  86. static const char devpts[] = _PATH_DEVPTS;
  87. char *p;
  88. p = _int10tostr(&numbuf[sizeof numbuf - 1], ptyno);
  89. if (buflen < sizeof(devpts) + (size_t)(&numbuf[sizeof(numbuf) - 1] - p))
  90. {
  91. errno = ERANGE;
  92. return ERANGE;
  93. }
  94. strcpy (buf, devpts);
  95. strcat (buf, p);
  96. /* Note: Don't bother with stat on the slave name and checking the
  97. driver's major device number - the ioctl above succeeded so
  98. we know the fd was a Unix'98 master and the /dev/pts/ prefix
  99. is set by definition. If the name isn't really a slave PTY,
  100. the system is misconfigured anyway - something else will fail
  101. later.
  102. */
  103. errno = save_errno;
  104. return 0;
  105. }
  106. #endif
  107. #if defined __UNIX98PTY_ONLY__
  108. else
  109. {
  110. /* If the ioctl fails it wasn't a Unix 98 master PTY */
  111. errno = ENOTTY;
  112. return ENOTTY;
  113. }
  114. #else
  115. # if !defined TIOCGPTN
  116. else if (errno == EINVAL)
  117. # endif
  118. {
  119. char *p;
  120. if (buflen < strlen (_PATH_TTY) + 3)
  121. {
  122. errno = ERANGE;
  123. return ERANGE;
  124. }
  125. if (fstat (fd, &st) < 0)
  126. return errno;
  127. /* Check if FD really is a master pseudo terminal. */
  128. if (! MASTER_P (st.st_rdev))
  129. {
  130. errno = ENOTTY;
  131. return ENOTTY;
  132. }
  133. ptyno = minor (st.st_rdev);
  134. /* This is for the old BSD pseudo terminals. As of Linux
  135. 2.1.115 these are no longer supported. */
  136. if (major (st.st_rdev) == 4)
  137. ptyno -= 128;
  138. if (ptyno / 16 >= strlen (__libc_ptyname1))
  139. {
  140. errno = ENOTTY;
  141. return ENOTTY;
  142. }
  143. strcpy (buf, _PATH_TTY);
  144. p = buf + strlen (buf);
  145. p[0] = __libc_ptyname1[ptyno / 16];
  146. p[1] = __libc_ptyname2[ptyno % 16];
  147. p[2] = '\0';
  148. }
  149. if (stat(buf, &st) < 0)
  150. return errno;
  151. /* Check if the name we're about to return really corresponds to a
  152. slave pseudo terminal. */
  153. if (! S_ISCHR (st.st_mode) || ! SLAVE_P (st.st_rdev))
  154. {
  155. /* This really is a configuration problem. */
  156. errno = ENOTTY;
  157. return ENOTTY;
  158. }
  159. #endif
  160. errno = save_errno;
  161. return 0;
  162. }
  163. libc_hidden_def(ptsname_r)
  164. /* Return the pathname of the pseudo terminal slave assoicated with
  165. the master FD is open on, or NULL on errors.
  166. The returned storage is good until the next call to this function. */
  167. char *
  168. ptsname (int fd)
  169. {
  170. static char buffer[sizeof (_PATH_DEVPTS) + 20];
  171. return ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
  172. }