ptsname.c 5.7 KB

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