1/* Copyright (C) 1991-2019 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#include "ttyname.h"
32
33#if 0
34/* Is this used anywhere? It is not exported. */
35char *__ttyname;
36#endif
37
38static char *getttyname (const char *dev, const struct stat64 *mytty,
39 int save, int *dostat);
40
41libc_freeres_ptr (static char *getttyname_name);
42
43static char *
44attribute_compat_text_section
45getttyname (const char *dev, const struct stat64 *mytty, 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 == mytty->st_ino || *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 && is_mytty (mytty, &st))
89 {
90 (void) __closedir (dirstream);
91#if 0
92 __ttyname = getttyname_name;
93#endif
94 __set_errno (save);
95 return getttyname_name;
96 }
97 }
98
99 (void) __closedir (dirstream);
100 __set_errno (save);
101 return NULL;
102}
103
104
105/* Static buffer in `ttyname'. */
106libc_freeres_ptr (static char *ttyname_buf);
107
108
109/* Return the pathname of the terminal FD is open on, or NULL on errors.
110 The returned storage is good only until the next call to this function. */
111char *
112ttyname (int fd)
113{
114 static size_t buflen;
115 char procname[30];
116 struct stat64 st, st1;
117 int dostat = 0;
118 int doispty = 0;
119 char *name;
120 int save = errno;
121 struct termios term;
122
123 /* isatty check, tcgetattr is used because it sets the correct
124 errno (EBADF resp. ENOTTY) on error. */
125 if (__glibc_unlikely (__tcgetattr (fd, &term) < 0))
126 return NULL;
127
128 if (__fxstat64 (_STAT_VER, fd, &st) < 0)
129 return NULL;
130
131 /* We try using the /proc filesystem. */
132 *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0';
133
134 if (buflen == 0)
135 {
136 buflen = 4095;
137 ttyname_buf = (char *) malloc (buflen + 1);
138 if (ttyname_buf == NULL)
139 {
140 buflen = 0;
141 return NULL;
142 }
143 }
144
145 ssize_t len = __readlink (procname, ttyname_buf, buflen);
146 if (__glibc_likely (len != -1))
147 {
148 if ((size_t) len >= buflen)
149 return NULL;
150
151#define UNREACHABLE_LEN strlen ("(unreachable)")
152 if (len > UNREACHABLE_LEN
153 && memcmp (ttyname_buf, "(unreachable)", UNREACHABLE_LEN) == 0)
154 {
155 memmove (ttyname_buf, ttyname_buf + UNREACHABLE_LEN,
156 len - UNREACHABLE_LEN);
157 len -= UNREACHABLE_LEN;
158 }
159
160 /* readlink need not terminate the string. */
161 ttyname_buf[len] = '\0';
162
163 /* Verify readlink result, fall back on iterating through devices. */
164 if (ttyname_buf[0] == '/'
165 && __xstat64 (_STAT_VER, ttyname_buf, &st1) == 0
166 && is_mytty (&st, &st1))
167 return ttyname_buf;
168
169 doispty = 1;
170 }
171
172 if (__xstat64 (_STAT_VER, "/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode))
173 {
174 name = getttyname ("/dev/pts", &st, save, &dostat);
175 }
176 else
177 {
178 __set_errno (save);
179 name = NULL;
180 }
181
182 if (!name && dostat != -1)
183 {
184 name = getttyname ("/dev", &st, save, &dostat);
185 }
186
187 if (!name && dostat != -1)
188 {
189 dostat = 1;
190 name = getttyname ("/dev", &st, save, &dostat);
191 }
192
193 if (!name && doispty && is_pty (&st))
194 {
195 /* We failed to figure out the TTY's name, but we can at least
196 signal that we did verify that it really is a PTY slave.
197 This happens when we have inherited the file descriptor from
198 a different mount namespace. */
199 __set_errno (ENODEV);
200 return NULL;
201 }
202
203 return name;
204}
205