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