]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/ttyname.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / ttyname.c
CommitLineData
bfff8b1b 1/* Copyright (C) 1991-2017 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
6455d255
UD
31#if 0
32/* Is this used anywhere? It is not exported. */
c4563d2d 33char *__ttyname;
6455d255 34#endif
45139d5f 35
b8fd5502 36static char *getttyname (const char *dev, dev_t mydev,
8edf6e0d 37 ino64_t myino, int save, int *dostat)
45139d5f
UD
38 internal_function;
39
b8fd5502 40
c877418f 41libc_freeres_ptr (static char *getttyname_name);
b8fd5502 42
45139d5f 43static char *
f9d07577 44internal_function attribute_compat_text_section
8edf6e0d 45getttyname (const char *dev, dev_t mydev, ino64_t myino, int save, int *dostat)
45139d5f 46{
c4563d2d 47 static size_t namelen;
8edf6e0d 48 struct stat64 st;
45139d5f 49 DIR *dirstream;
2958e6cc 50 struct dirent64 *d;
45139d5f
UD
51 size_t devlen = strlen (dev) + 1;
52
4aebaa6b 53 dirstream = __opendir (dev);
45139d5f
UD
54 if (dirstream == NULL)
55 {
56 *dostat = -1;
57 return NULL;
58 }
59
d94760f9
UD
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
2958e6cc
UD
65 while ((d = __readdir64 (dirstream)) != NULL)
66 if ((d->d_fileno == myino || *dostat)
45139d5f
UD
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 {
b8fd5502 74 free (getttyname_name);
45139d5f 75 namelen = 2 * (devlen + dlen); /* Big enough. */
b8fd5502
UD
76 getttyname_name = malloc (namelen);
77 if (! getttyname_name)
45139d5f
UD
78 {
79 *dostat = -1;
80 /* Perhaps it helps to free the directory stream buffer. */
50304ef0 81 (void) __closedir (dirstream);
45139d5f
UD
82 return NULL;
83 }
b8fd5502 84 *((char *) __mempcpy (getttyname_name, dev, devlen - 1)) = '/';
45139d5f 85 }
b8fd5502 86 memcpy (&getttyname_name[devlen], d->d_name, dlen);
8edf6e0d 87 if (__xstat64 (_STAT_VER, getttyname_name, &st) == 0
45139d5f
UD
88#ifdef _STATBUF_ST_RDEV
89 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
90#else
2958e6cc 91 && d->d_fileno == myino && st.st_dev == mydev
45139d5f
UD
92#endif
93 )
94 {
50304ef0 95 (void) __closedir (dirstream);
6455d255 96#if 0
b8fd5502 97 __ttyname = getttyname_name;
6455d255 98#endif
45139d5f 99 __set_errno (save);
b8fd5502 100 return getttyname_name;
45139d5f
UD
101 }
102 }
103
50304ef0 104 (void) __closedir (dirstream);
45139d5f
UD
105 __set_errno (save);
106 return NULL;
107}
108
b8fd5502
UD
109
110/* Static buffer in `ttyname'. */
c877418f 111libc_freeres_ptr (static char *ttyname_buf);
b8fd5502
UD
112
113
45139d5f
UD
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 *
bcaad6ee 117ttyname (int fd)
45139d5f 118{
c4563d2d 119 static size_t buflen;
c5e340c7 120 char procname[30];
8edf6e0d 121 struct stat64 st, st1;
45139d5f 122 int dostat = 0;
7d1de115 123 char *name;
45139d5f 124 int save = errno;
f0d5e1f6 125 struct termios term;
45139d5f 126
f0d5e1f6
UD
127 /* isatty check, tcgetattr is used because it sets the correct
128 errno (EBADF resp. ENOTTY) on error. */
a1ffb40e 129 if (__glibc_unlikely (__tcgetattr (fd, &term) < 0))
f0d5e1f6 130 return NULL;
45139d5f 131
0e516e0e
MS
132 if (__fxstat64 (_STAT_VER, fd, &st) < 0)
133 return NULL;
134
c5e340c7
UD
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;
b8fd5502
UD
141 ttyname_buf = (char *) malloc (buflen + 1);
142 if (ttyname_buf == NULL)
c5e340c7
UD
143 {
144 buflen = 0;
145 return NULL;
146 }
147 }
148
f9d07577 149 ssize_t len = __readlink (procname, ttyname_buf, buflen);
a1ffb40e 150 if (__glibc_likely (len != -1))
7081e0a3 151 {
57b36a0a 152 if ((size_t) len >= buflen)
7081e0a3 153 return NULL;
0e516e0e
MS
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
7081e0a3 164 /* readlink need not terminate the string. */
b8fd5502 165 ttyname_buf[len] = '\0';
c5e340c7 166
0e516e0e
MS
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 }
45139d5f 180
8edf6e0d 181 if (__xstat64 (_STAT_VER, "/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode))
45139d5f
UD
182 {
183#ifdef _STATBUF_ST_RDEV
7d1de115 184 name = getttyname ("/dev/pts", st.st_rdev, st.st_ino, save, &dostat);
45139d5f 185#else
7d1de115 186 name = getttyname ("/dev/pts", st.st_dev, st.st_ino, save, &dostat);
45139d5f 187#endif
7d1de115
UD
188 }
189 else
190 {
191 __set_errno (save);
192 name = NULL;
45139d5f
UD
193 }
194
195 if (!name && dostat != -1)
196 {
197#ifdef _STATBUF_ST_RDEV
160698e2 198 name = getttyname ("/dev", st.st_rdev, st.st_ino, save, &dostat);
45139d5f 199#else
160698e2 200 name = getttyname ("/dev", st.st_dev, st.st_ino, save, &dostat);
45139d5f
UD
201#endif
202 }
203
204 if (!name && dostat != -1)
205 {
206 dostat = 1;
207#ifdef _STATBUF_ST_RDEV
160698e2 208 name = getttyname ("/dev", st.st_rdev, st.st_ino, save, &dostat);
45139d5f 209#else
160698e2 210 name = getttyname ("/dev", st.st_dev, st.st_ino, save, &dostat);
45139d5f
UD
211#endif
212 }
213
214 return name;
215}