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