1 | /* Copyright (C) 1998-2018 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 <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 | |
29 | #include <_itoa.h> |
30 | |
31 | /* Check if DEV corresponds to a master pseudo terminal device. */ |
32 | #define MASTER_P(Dev) \ |
33 | (__gnu_dev_major ((Dev)) == 2 \ |
34 | || (__gnu_dev_major ((Dev)) == 4 \ |
35 | && __gnu_dev_minor ((Dev)) >= 128 && __gnu_dev_minor ((Dev)) < 192) \ |
36 | || (__gnu_dev_major ((Dev)) >= 128 && __gnu_dev_major ((Dev)) < 136)) |
37 | |
38 | /* Check if DEV corresponds to a slave pseudo terminal device. */ |
39 | #define SLAVE_P(Dev) \ |
40 | (__gnu_dev_major ((Dev)) == 3 \ |
41 | || (__gnu_dev_major ((Dev)) == 4 \ |
42 | && __gnu_dev_minor ((Dev)) >= 192 && __gnu_dev_minor ((Dev)) < 256) \ |
43 | || (__gnu_dev_major ((Dev)) >= 136 && __gnu_dev_major ((Dev)) < 144)) |
44 | |
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 | |
50 | /* Directory where we can find the slave pty nodes. */ |
51 | #define _PATH_DEVPTS "/dev/pts/" |
52 | |
53 | /* The are declared in getpt.c. */ |
54 | extern const char __libc_ptyname1[] attribute_hidden; |
55 | extern const char __libc_ptyname2[] attribute_hidden; |
56 | |
57 | /* Static buffer for `ptsname'. */ |
58 | static char buffer[sizeof (_PATH_DEVPTS) + 20]; |
59 | |
60 | |
61 | /* Return the pathname of the pseudo terminal slave associated with |
62 | the master FD is open on, or NULL on errors. |
63 | The returned storage is good until the next call to this function. */ |
64 | char * |
65 | ptsname (int fd) |
66 | { |
67 | return __ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer; |
68 | } |
69 | |
70 | |
71 | int |
72 | __ptsname_internal (int fd, char *buf, size_t buflen, struct stat64 *stp) |
73 | { |
74 | int save_errno = errno; |
75 | unsigned int ptyno; |
76 | |
77 | if (!__isatty (fd)) |
78 | { |
79 | __set_errno (ENOTTY); |
80 | return ENOTTY; |
81 | } |
82 | |
83 | #ifdef TIOCGPTN |
84 | if (__ioctl (fd, TIOCGPTN, &ptyno) == 0) |
85 | { |
86 | /* Buffer we use to print the number in. For a maximum size for |
87 | `int' of 8 bytes we never need more than 20 digits. */ |
88 | char numbuf[21]; |
89 | const char *devpts = _PATH_DEVPTS; |
90 | const size_t devptslen = strlen (_PATH_DEVPTS); |
91 | char *p; |
92 | |
93 | numbuf[sizeof (numbuf) - 1] = '\0'; |
94 | p = _itoa_word (ptyno, &numbuf[sizeof (numbuf) - 1], 10, 0); |
95 | |
96 | if (buflen < devptslen + (&numbuf[sizeof (numbuf)] - p)) |
97 | { |
98 | __set_errno (ERANGE); |
99 | return ERANGE; |
100 | } |
101 | |
102 | memcpy (__stpcpy (buf, devpts), p, &numbuf[sizeof (numbuf)] - p); |
103 | } |
104 | else if (errno != EINVAL) |
105 | return errno; |
106 | else |
107 | #endif |
108 | { |
109 | char *p; |
110 | |
111 | if (buflen < strlen (_PATH_TTY) + 3) |
112 | { |
113 | __set_errno (ERANGE); |
114 | return ERANGE; |
115 | } |
116 | |
117 | if (__fxstat64 (_STAT_VER, fd, stp) < 0) |
118 | return errno; |
119 | |
120 | /* Check if FD really is a master pseudo terminal. */ |
121 | if (! MASTER_P (stp->st_rdev)) |
122 | { |
123 | __set_errno (ENOTTY); |
124 | return ENOTTY; |
125 | } |
126 | |
127 | ptyno = __gnu_dev_minor (stp->st_rdev); |
128 | |
129 | if (ptyno / 16 >= strlen (__libc_ptyname1)) |
130 | { |
131 | __set_errno (ENOTTY); |
132 | return ENOTTY; |
133 | } |
134 | |
135 | p = __stpcpy (buf, _PATH_TTY); |
136 | p[0] = __libc_ptyname1[ptyno / 16]; |
137 | p[1] = __libc_ptyname2[ptyno % 16]; |
138 | p[2] = '\0'; |
139 | } |
140 | |
141 | if (__xstat64 (_STAT_VER, buf, stp) < 0) |
142 | return errno; |
143 | |
144 | /* Check if the name we're about to return really corresponds to a |
145 | slave pseudo terminal. */ |
146 | if (! S_ISCHR (stp->st_mode) || ! SLAVE_P (stp->st_rdev)) |
147 | { |
148 | /* This really is a configuration problem. */ |
149 | __set_errno (ENOTTY); |
150 | return ENOTTY; |
151 | } |
152 | |
153 | __set_errno (save_errno); |
154 | return 0; |
155 | } |
156 | |
157 | |
158 | /* Store at most BUFLEN characters of the pathname of the slave pseudo |
159 | terminal associated with the master FD is open on in BUF. |
160 | Return 0 on success, otherwise an error number. */ |
161 | int |
162 | __ptsname_r (int fd, char *buf, size_t buflen) |
163 | { |
164 | struct stat64 st; |
165 | return __ptsname_internal (fd, buf, buflen, &st); |
166 | } |
167 | weak_alias (__ptsname_r, ptsname_r) |
168 | |