]>
Commit | Line | Data |
---|---|---|
2b778ceb | 1 | /* Copyright (C) 1991-2021 Free Software Foundation, Inc. |
45139d5f UD |
2 | This file is part of the GNU C Library. |
3 | ||
4 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
45139d5f UD |
8 | |
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
45139d5f | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
45139d5f UD |
17 | |
18 | #include <errno.h> | |
19 | #include <limits.h> | |
20 | #include <stddef.h> | |
21 | #include <dirent.h> | |
22 | #include <sys/types.h> | |
23 | #include <sys/stat.h> | |
f0d5e1f6 | 24 | #include <termios.h> |
45139d5f UD |
25 | #include <unistd.h> |
26 | #include <string.h> | |
27 | #include <stdlib.h> | |
28 | ||
eb96ffb0 | 29 | #include <_itoa.h> |
c5e340c7 | 30 | |
15e9a4f3 CB |
31 | #include "ttyname.h" |
32 | ||
bcaad6ee | 33 | static int getttyname_r (char *buf, size_t buflen, |
2fbce9c2 | 34 | const struct stat64 *mytty, int save, |
b41bd5bc | 35 | int *dostat); |
45139d5f UD |
36 | |
37 | static int | |
b41bd5bc | 38 | attribute_compat_text_section |
2fbce9c2 | 39 | getttyname_r (char *buf, size_t buflen, const struct stat64 *mytty, |
bcaad6ee | 40 | int save, int *dostat) |
45139d5f | 41 | { |
8edf6e0d | 42 | struct stat64 st; |
45139d5f | 43 | DIR *dirstream; |
2958e6cc | 44 | struct dirent64 *d; |
45139d5f UD |
45 | size_t devlen = strlen (buf); |
46 | ||
50304ef0 | 47 | dirstream = __opendir (buf); |
45139d5f UD |
48 | if (dirstream == NULL) |
49 | { | |
50 | *dostat = -1; | |
51 | return errno; | |
52 | } | |
53 | ||
2958e6cc | 54 | while ((d = __readdir64 (dirstream)) != NULL) |
2fbce9c2 | 55 | if ((d->d_fileno == mytty->st_ino || *dostat) |
45139d5f UD |
56 | && strcmp (d->d_name, "stdin") |
57 | && strcmp (d->d_name, "stdout") | |
58 | && strcmp (d->d_name, "stderr")) | |
59 | { | |
60 | char *cp; | |
61 | size_t needed = _D_EXACT_NAMLEN (d) + 1; | |
62 | ||
63 | if (needed > buflen) | |
64 | { | |
65 | *dostat = -1; | |
50304ef0 | 66 | (void) __closedir (dirstream); |
45139d5f UD |
67 | __set_errno (ERANGE); |
68 | return ERANGE; | |
69 | } | |
70 | ||
71 | cp = __stpncpy (buf + devlen, d->d_name, needed); | |
72 | cp[0] = '\0'; | |
73 | ||
04986243 | 74 | if (__stat64 (buf, &st) == 0 |
2fbce9c2 | 75 | && is_mytty (mytty, &st)) |
45139d5f | 76 | { |
50304ef0 | 77 | (void) __closedir (dirstream); |
45139d5f UD |
78 | __set_errno (save); |
79 | return 0; | |
80 | } | |
81 | } | |
82 | ||
50304ef0 | 83 | (void) __closedir (dirstream); |
45139d5f UD |
84 | __set_errno (save); |
85 | /* It is not clear what to return in this case. `isatty' says FD | |
86 | refers to a TTY but no entry in /dev has this inode. */ | |
87 | return ENOTTY; | |
88 | } | |
89 | ||
90 | /* Store at most BUFLEN character of the pathname of the terminal FD is | |
91 | open on in BUF. Return 0 on success, otherwise an error number. */ | |
92 | int | |
bcaad6ee | 93 | __ttyname_r (int fd, char *buf, size_t buflen) |
45139d5f | 94 | { |
c5e340c7 | 95 | char procname[30]; |
8edf6e0d | 96 | struct stat64 st, st1; |
45139d5f | 97 | int dostat = 0; |
a09dfc19 | 98 | int doispty = 0; |
45139d5f | 99 | int save = errno; |
45139d5f UD |
100 | |
101 | /* Test for the absolute minimal size. This makes life easier inside | |
102 | the loop. */ | |
103 | if (!buf) | |
104 | { | |
105 | __set_errno (EINVAL); | |
106 | return EINVAL; | |
107 | } | |
108 | ||
109 | if (buflen < sizeof ("/dev/pts/")) | |
110 | { | |
111 | __set_errno (ERANGE); | |
112 | return ERANGE; | |
113 | } | |
114 | ||
f0d5e1f6 UD |
115 | /* isatty check, tcgetattr is used because it sets the correct |
116 | errno (EBADF resp. ENOTTY) on error. */ | |
117 | struct termios term; | |
a1ffb40e | 118 | if (__glibc_unlikely (__tcgetattr (fd, &term) < 0)) |
f0d5e1f6 | 119 | return errno; |
f9d07577 | 120 | |
04986243 | 121 | if (__fstat64 (fd, &st) < 0) |
0e516e0e MS |
122 | return errno; |
123 | ||
2f7dc594 UD |
124 | /* We try using the /proc filesystem. */ |
125 | *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0'; | |
126 | ||
f9d07577 | 127 | ssize_t ret = __readlink (procname, buf, buflen - 1); |
a1ffb40e | 128 | if (__glibc_unlikely (ret == -1 && errno == ENAMETOOLONG)) |
2f7dc594 UD |
129 | { |
130 | __set_errno (ERANGE); | |
131 | return ERANGE; | |
132 | } | |
c5e340c7 | 133 | |
a1ffb40e | 134 | if (__glibc_likely (ret != -1)) |
7081e0a3 | 135 | { |
0e516e0e MS |
136 | #define UNREACHABLE_LEN strlen ("(unreachable)") |
137 | if (ret > UNREACHABLE_LEN | |
138 | && memcmp (buf, "(unreachable)", UNREACHABLE_LEN) == 0) | |
139 | { | |
140 | memmove (buf, buf + UNREACHABLE_LEN, ret - UNREACHABLE_LEN); | |
141 | ret -= UNREACHABLE_LEN; | |
142 | } | |
143 | ||
144 | /* readlink need not terminate the string. */ | |
7081e0a3 | 145 | buf[ret] = '\0'; |
c5e340c7 | 146 | |
0e516e0e MS |
147 | /* Verify readlink result, fall back on iterating through devices. */ |
148 | if (buf[0] == '/' | |
04986243 | 149 | && __stat64 (buf, &st1) == 0 |
2fbce9c2 | 150 | && is_mytty (&st, &st1)) |
0e516e0e | 151 | return 0; |
15e9a4f3 | 152 | |
a09dfc19 | 153 | doispty = 1; |
0e516e0e | 154 | } |
45139d5f UD |
155 | |
156 | /* Prepare the result buffer. */ | |
157 | memcpy (buf, "/dev/pts/", sizeof ("/dev/pts/")); | |
158 | buflen -= sizeof ("/dev/pts/") - 1; | |
159 | ||
04986243 | 160 | if (__stat64 (buf, &st1) == 0 && S_ISDIR (st1.st_mode)) |
45139d5f | 161 | { |
2fbce9c2 | 162 | ret = getttyname_r (buf, buflen, &st, save, |
7d1de115 | 163 | &dostat); |
7d1de115 UD |
164 | } |
165 | else | |
166 | { | |
167 | __set_errno (save); | |
168 | ret = ENOENT; | |
45139d5f UD |
169 | } |
170 | ||
171 | if (ret && dostat != -1) | |
172 | { | |
173 | buf[sizeof ("/dev/") - 1] = '\0'; | |
174 | buflen += sizeof ("pts/") - 1; | |
2fbce9c2 | 175 | ret = getttyname_r (buf, buflen, &st, save, |
45139d5f | 176 | &dostat); |
45139d5f UD |
177 | } |
178 | ||
179 | if (ret && dostat != -1) | |
180 | { | |
181 | buf[sizeof ("/dev/") - 1] = '\0'; | |
182 | dostat = 1; | |
2fbce9c2 | 183 | ret = getttyname_r (buf, buflen, &st, |
45139d5f | 184 | save, &dostat); |
45139d5f UD |
185 | } |
186 | ||
a09dfc19 LS |
187 | if (ret && doispty && is_pty (&st)) |
188 | { | |
189 | /* We failed to figure out the TTY's name, but we can at least | |
190 | signal that we did verify that it really is a PTY slave. | |
191 | This happens when we have inherited the file descriptor from | |
192 | a different mount namespace. */ | |
193 | __set_errno (ENODEV); | |
194 | return ENODEV; | |
195 | } | |
196 | ||
45139d5f UD |
197 | return ret; |
198 | } | |
199 | ||
200 | weak_alias (__ttyname_r, ttyname_r) |