]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/ttyname.c
Merge branch 'elf-move'
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / ttyname.c
1 /* Copyright (C) 1991-1993,1996-2002,2006,2009,2010
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 <limits.h>
21 #include <stddef.h>
22 #include <dirent.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <termios.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include <_itoa.h>
31 #include <kernel-features.h>
32
33 #if 0
34 /* Is this used anywhere? It is not exported. */
35 char *__ttyname;
36 #endif
37
38 static char *getttyname (const char *dev, dev_t mydev,
39 ino64_t myino, int save, int *dostat)
40 internal_function;
41
42
43 libc_freeres_ptr (static char *getttyname_name);
44
45 static char *
46 internal_function attribute_compat_text_section
47 getttyname (const char *dev, dev_t mydev, ino64_t myino, int save, int *dostat)
48 {
49 static size_t namelen;
50 struct stat64 st;
51 DIR *dirstream;
52 struct dirent64 *d;
53 size_t devlen = strlen (dev) + 1;
54
55 dirstream = __opendir (dev);
56 if (dirstream == NULL)
57 {
58 *dostat = -1;
59 return NULL;
60 }
61
62 /* Prepare for the loop. If we already have a buffer copy the directory
63 name we look at into it. */
64 if (devlen < namelen)
65 *((char *) __mempcpy (getttyname_name, dev, devlen - 1)) = '/';
66
67 while ((d = __readdir64 (dirstream)) != NULL)
68 if ((d->d_fileno == myino || *dostat)
69 && strcmp (d->d_name, "stdin")
70 && strcmp (d->d_name, "stdout")
71 && strcmp (d->d_name, "stderr"))
72 {
73 size_t dlen = _D_ALLOC_NAMLEN (d);
74 if (devlen + dlen > namelen)
75 {
76 free (getttyname_name);
77 namelen = 2 * (devlen + dlen); /* Big enough. */
78 getttyname_name = malloc (namelen);
79 if (! getttyname_name)
80 {
81 *dostat = -1;
82 /* Perhaps it helps to free the directory stream buffer. */
83 (void) __closedir (dirstream);
84 return NULL;
85 }
86 *((char *) __mempcpy (getttyname_name, dev, devlen - 1)) = '/';
87 }
88 memcpy (&getttyname_name[devlen], d->d_name, dlen);
89 if (__xstat64 (_STAT_VER, getttyname_name, &st) == 0
90 #ifdef _STATBUF_ST_RDEV
91 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
92 #else
93 && d->d_fileno == myino && st.st_dev == mydev
94 #endif
95 )
96 {
97 (void) __closedir (dirstream);
98 #if 0
99 __ttyname = getttyname_name;
100 #endif
101 __set_errno (save);
102 return getttyname_name;
103 }
104 }
105
106 (void) __closedir (dirstream);
107 __set_errno (save);
108 return NULL;
109 }
110
111
112 /* Static buffer in `ttyname'. */
113 libc_freeres_ptr (static char *ttyname_buf);
114
115
116 /* Return the pathname of the terminal FD is open on, or NULL on errors.
117 The returned storage is good only until the next call to this function. */
118 char *
119 ttyname (int fd)
120 {
121 static size_t buflen;
122 char procname[30];
123 struct stat64 st, st1;
124 int dostat = 0;
125 char *name;
126 int save = errno;
127 struct termios term;
128
129 /* isatty check, tcgetattr is used because it sets the correct
130 errno (EBADF resp. ENOTTY) on error. */
131 if (__builtin_expect (__tcgetattr (fd, &term) < 0, 0))
132 return NULL;
133
134 if (__fxstat64 (_STAT_VER, fd, &st) < 0)
135 return NULL;
136
137 /* We try using the /proc filesystem. */
138 *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0';
139
140 if (buflen == 0)
141 {
142 buflen = 4095;
143 ttyname_buf = (char *) malloc (buflen + 1);
144 if (ttyname_buf == NULL)
145 {
146 buflen = 0;
147 return NULL;
148 }
149 }
150
151 ssize_t len = __readlink (procname, ttyname_buf, buflen);
152 if (__builtin_expect (len == -1 && errno == ENOENT, 0))
153 {
154 __set_errno (EBADF);
155 return NULL;
156 }
157
158 if (__builtin_expect (len != -1
159 #ifndef __ASSUME_PROC_SELF_FD_SYMLINK
160 /* This is for Linux 2.0. */
161 && ttyname_buf[0] != '['
162 #endif
163 , 1))
164 {
165 if ((size_t) len >= buflen)
166 return NULL;
167
168 #define UNREACHABLE_LEN strlen ("(unreachable)")
169 if (len > UNREACHABLE_LEN
170 && memcmp (ttyname_buf, "(unreachable)", UNREACHABLE_LEN) == 0)
171 {
172 memmove (ttyname_buf, ttyname_buf + UNREACHABLE_LEN,
173 len - UNREACHABLE_LEN);
174 len -= UNREACHABLE_LEN;
175 }
176
177 /* readlink need not terminate the string. */
178 ttyname_buf[len] = '\0';
179
180 /* Verify readlink result, fall back on iterating through devices. */
181 if (ttyname_buf[0] == '/'
182 && __xstat64 (_STAT_VER, ttyname_buf, &st1) == 0
183 #ifdef _STATBUF_ST_RDEV
184 && S_ISCHR (st1.st_mode)
185 && st1.st_rdev == st.st_rdev
186 #else
187 && st1.st_ino == st.st_ino
188 && st1.st_dev == st.st_dev
189 #endif
190 )
191 return ttyname_buf;
192 }
193
194 if (__xstat64 (_STAT_VER, "/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode))
195 {
196 #ifdef _STATBUF_ST_RDEV
197 name = getttyname ("/dev/pts", st.st_rdev, st.st_ino, save, &dostat);
198 #else
199 name = getttyname ("/dev/pts", st.st_dev, st.st_ino, save, &dostat);
200 #endif
201 }
202 else
203 {
204 __set_errno (save);
205 name = NULL;
206 }
207
208 if (!name && dostat != -1)
209 {
210 #ifdef _STATBUF_ST_RDEV
211 name = getttyname ("/dev", st.st_rdev, st.st_ino, save, &dostat);
212 #else
213 name = getttyname ("/dev", st.st_dev, st.st_ino, save, &dostat);
214 #endif
215 }
216
217 if (!name && dostat != -1)
218 {
219 dostat = 1;
220 #ifdef _STATBUF_ST_RDEV
221 name = getttyname ("/dev", st.st_rdev, st.st_ino, save, &dostat);
222 #else
223 name = getttyname ("/dev", st.st_dev, st.st_ino, save, &dostat);
224 #endif
225 }
226
227 return name;
228 }