]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 1991-2015 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 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://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 | |
bcaad6ee | 31 | static int getttyname_r (char *buf, size_t buflen, |
8edf6e0d | 32 | dev_t mydev, ino64_t myino, int save, |
bcaad6ee | 33 | int *dostat) internal_function; |
45139d5f UD |
34 | |
35 | static int | |
f9d07577 | 36 | internal_function attribute_compat_text_section |
8edf6e0d | 37 | getttyname_r (char *buf, size_t buflen, dev_t mydev, ino64_t myino, |
bcaad6ee | 38 | int save, int *dostat) |
45139d5f | 39 | { |
8edf6e0d | 40 | struct stat64 st; |
45139d5f | 41 | DIR *dirstream; |
2958e6cc | 42 | struct dirent64 *d; |
45139d5f UD |
43 | size_t devlen = strlen (buf); |
44 | ||
50304ef0 | 45 | dirstream = __opendir (buf); |
45139d5f UD |
46 | if (dirstream == NULL) |
47 | { | |
48 | *dostat = -1; | |
49 | return errno; | |
50 | } | |
51 | ||
2958e6cc UD |
52 | while ((d = __readdir64 (dirstream)) != NULL) |
53 | if ((d->d_fileno == myino || *dostat) | |
45139d5f UD |
54 | && strcmp (d->d_name, "stdin") |
55 | && strcmp (d->d_name, "stdout") | |
56 | && strcmp (d->d_name, "stderr")) | |
57 | { | |
58 | char *cp; | |
59 | size_t needed = _D_EXACT_NAMLEN (d) + 1; | |
60 | ||
61 | if (needed > buflen) | |
62 | { | |
63 | *dostat = -1; | |
50304ef0 | 64 | (void) __closedir (dirstream); |
45139d5f UD |
65 | __set_errno (ERANGE); |
66 | return ERANGE; | |
67 | } | |
68 | ||
69 | cp = __stpncpy (buf + devlen, d->d_name, needed); | |
70 | cp[0] = '\0'; | |
71 | ||
8edf6e0d | 72 | if (__xstat64 (_STAT_VER, buf, &st) == 0 |
45139d5f UD |
73 | #ifdef _STATBUF_ST_RDEV |
74 | && S_ISCHR (st.st_mode) && st.st_rdev == mydev | |
75 | #else | |
2958e6cc | 76 | && d->d_fileno == myino && st.st_dev == mydev |
45139d5f UD |
77 | #endif |
78 | ) | |
79 | { | |
50304ef0 | 80 | (void) __closedir (dirstream); |
45139d5f UD |
81 | __set_errno (save); |
82 | return 0; | |
83 | } | |
84 | } | |
85 | ||
50304ef0 | 86 | (void) __closedir (dirstream); |
45139d5f UD |
87 | __set_errno (save); |
88 | /* It is not clear what to return in this case. `isatty' says FD | |
89 | refers to a TTY but no entry in /dev has this inode. */ | |
90 | return ENOTTY; | |
91 | } | |
92 | ||
93 | /* Store at most BUFLEN character of the pathname of the terminal FD is | |
94 | open on in BUF. Return 0 on success, otherwise an error number. */ | |
95 | int | |
bcaad6ee | 96 | __ttyname_r (int fd, char *buf, size_t buflen) |
45139d5f | 97 | { |
c5e340c7 | 98 | char procname[30]; |
8edf6e0d | 99 | struct stat64 st, st1; |
45139d5f UD |
100 | int dostat = 0; |
101 | int save = errno; | |
45139d5f UD |
102 | |
103 | /* Test for the absolute minimal size. This makes life easier inside | |
104 | the loop. */ | |
105 | if (!buf) | |
106 | { | |
107 | __set_errno (EINVAL); | |
108 | return EINVAL; | |
109 | } | |
110 | ||
111 | if (buflen < sizeof ("/dev/pts/")) | |
112 | { | |
113 | __set_errno (ERANGE); | |
114 | return ERANGE; | |
115 | } | |
116 | ||
f0d5e1f6 UD |
117 | /* isatty check, tcgetattr is used because it sets the correct |
118 | errno (EBADF resp. ENOTTY) on error. */ | |
119 | struct termios term; | |
a1ffb40e | 120 | if (__glibc_unlikely (__tcgetattr (fd, &term) < 0)) |
f0d5e1f6 | 121 | return errno; |
f9d07577 | 122 | |
0e516e0e MS |
123 | if (__fxstat64 (_STAT_VER, fd, &st) < 0) |
124 | return errno; | |
125 | ||
2f7dc594 UD |
126 | /* We try using the /proc filesystem. */ |
127 | *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0'; | |
128 | ||
f9d07577 | 129 | ssize_t ret = __readlink (procname, buf, buflen - 1); |
a1ffb40e | 130 | if (__glibc_unlikely (ret == -1 && errno == ENAMETOOLONG)) |
2f7dc594 UD |
131 | { |
132 | __set_errno (ERANGE); | |
133 | return ERANGE; | |
134 | } | |
c5e340c7 | 135 | |
a1ffb40e | 136 | if (__glibc_likely (ret != -1)) |
7081e0a3 | 137 | { |
0e516e0e MS |
138 | #define UNREACHABLE_LEN strlen ("(unreachable)") |
139 | if (ret > UNREACHABLE_LEN | |
140 | && memcmp (buf, "(unreachable)", UNREACHABLE_LEN) == 0) | |
141 | { | |
142 | memmove (buf, buf + UNREACHABLE_LEN, ret - UNREACHABLE_LEN); | |
143 | ret -= UNREACHABLE_LEN; | |
144 | } | |
145 | ||
146 | /* readlink need not terminate the string. */ | |
7081e0a3 | 147 | buf[ret] = '\0'; |
c5e340c7 | 148 | |
0e516e0e MS |
149 | /* Verify readlink result, fall back on iterating through devices. */ |
150 | if (buf[0] == '/' | |
151 | && __xstat64 (_STAT_VER, buf, &st1) == 0 | |
152 | #ifdef _STATBUF_ST_RDEV | |
153 | && S_ISCHR (st1.st_mode) | |
154 | && st1.st_rdev == st.st_rdev | |
155 | #else | |
156 | && st1.st_ino == st.st_ino | |
157 | && st1.st_dev == st.st_dev | |
158 | #endif | |
159 | ) | |
160 | return 0; | |
161 | } | |
45139d5f UD |
162 | |
163 | /* Prepare the result buffer. */ | |
164 | memcpy (buf, "/dev/pts/", sizeof ("/dev/pts/")); | |
165 | buflen -= sizeof ("/dev/pts/") - 1; | |
166 | ||
8edf6e0d | 167 | if (__xstat64 (_STAT_VER, buf, &st1) == 0 && S_ISDIR (st1.st_mode)) |
45139d5f UD |
168 | { |
169 | #ifdef _STATBUF_ST_RDEV | |
7d1de115 UD |
170 | ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, save, |
171 | &dostat); | |
45139d5f | 172 | #else |
7d1de115 UD |
173 | ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, save, |
174 | &dostat); | |
45139d5f | 175 | #endif |
7d1de115 UD |
176 | } |
177 | else | |
178 | { | |
179 | __set_errno (save); | |
180 | ret = ENOENT; | |
45139d5f UD |
181 | } |
182 | ||
183 | if (ret && dostat != -1) | |
184 | { | |
185 | buf[sizeof ("/dev/") - 1] = '\0'; | |
186 | buflen += sizeof ("pts/") - 1; | |
187 | #ifdef _STATBUF_ST_RDEV | |
160698e2 | 188 | ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, save, |
45139d5f UD |
189 | &dostat); |
190 | #else | |
160698e2 | 191 | ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, save, |
45139d5f UD |
192 | &dostat); |
193 | #endif | |
194 | } | |
195 | ||
196 | if (ret && dostat != -1) | |
197 | { | |
198 | buf[sizeof ("/dev/") - 1] = '\0'; | |
199 | dostat = 1; | |
200 | #ifdef _STATBUF_ST_RDEV | |
160698e2 | 201 | ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, |
45139d5f UD |
202 | save, &dostat); |
203 | #else | |
160698e2 | 204 | ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, |
45139d5f UD |
205 | save, &dostat); |
206 | #endif | |
207 | } | |
208 | ||
209 | return ret; | |
210 | } | |
211 | ||
212 | weak_alias (__ttyname_r, ttyname_r) |