ptsname.c 5.7 KB

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