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