Maintain stack alignment in ____longjmp_chk on x86_64
[glibc.git] / sysdeps / unix / sysv / linux / ttyname_r.c
blob2fa7503471b0c7e04d222967f145045a970f5a8a
1 /* Copyright (C) 1991-1993,1995-2001,2003,2006,2010
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <limits.h>
22 #include <stddef.h>
23 #include <dirent.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <termios.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <stdlib.h>
31 #include <stdio-common/_itoa.h>
32 #include <kernel-features.h>
34 static int getttyname_r (char *buf, size_t buflen,
35 dev_t mydev, ino64_t myino, int save,
36 int *dostat) internal_function;
38 static int
39 internal_function attribute_compat_text_section
40 getttyname_r (char *buf, size_t buflen, dev_t mydev, ino64_t myino,
41 int save, int *dostat)
43 struct stat64 st;
44 DIR *dirstream;
45 struct dirent64 *d;
46 size_t devlen = strlen (buf);
48 dirstream = __opendir (buf);
49 if (dirstream == NULL)
51 *dostat = -1;
52 return errno;
55 while ((d = __readdir64 (dirstream)) != NULL)
56 if ((d->d_fileno == myino || *dostat)
57 && strcmp (d->d_name, "stdin")
58 && strcmp (d->d_name, "stdout")
59 && strcmp (d->d_name, "stderr"))
61 char *cp;
62 size_t needed = _D_EXACT_NAMLEN (d) + 1;
64 if (needed > buflen)
66 *dostat = -1;
67 (void) __closedir (dirstream);
68 __set_errno (ERANGE);
69 return ERANGE;
72 cp = __stpncpy (buf + devlen, d->d_name, needed);
73 cp[0] = '\0';
75 if (__xstat64 (_STAT_VER, buf, &st) == 0
76 #ifdef _STATBUF_ST_RDEV
77 && S_ISCHR (st.st_mode) && st.st_rdev == mydev
78 #else
79 && d->d_fileno == myino && st.st_dev == mydev
80 #endif
83 (void) __closedir (dirstream);
84 __set_errno (save);
85 return 0;
89 (void) __closedir (dirstream);
90 __set_errno (save);
91 /* It is not clear what to return in this case. `isatty' says FD
92 refers to a TTY but no entry in /dev has this inode. */
93 return ENOTTY;
96 /* Store at most BUFLEN character of the pathname of the terminal FD is
97 open on in BUF. Return 0 on success, otherwise an error number. */
98 int
99 __ttyname_r (int fd, char *buf, size_t buflen)
101 char procname[30];
102 struct stat64 st, st1;
103 int dostat = 0;
104 int save = errno;
106 /* Test for the absolute minimal size. This makes life easier inside
107 the loop. */
108 if (!buf)
110 __set_errno (EINVAL);
111 return EINVAL;
114 if (buflen < sizeof ("/dev/pts/"))
116 __set_errno (ERANGE);
117 return ERANGE;
120 /* isatty check, tcgetattr is used because it sets the correct
121 errno (EBADF resp. ENOTTY) on error. */
122 struct termios term;
123 if (__builtin_expect (__tcgetattr (fd, &term) < 0, 0))
124 return errno;
126 if (__fxstat64 (_STAT_VER, fd, &st) < 0)
127 return errno;
129 /* We try using the /proc filesystem. */
130 *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0';
132 ssize_t ret = __readlink (procname, buf, buflen - 1);
133 if (__builtin_expect (ret == -1 && errno == ENOENT, 0))
135 __set_errno (EBADF);
136 return EBADF;
139 if (__builtin_expect (ret == -1 && errno == ENAMETOOLONG, 0))
141 __set_errno (ERANGE);
142 return ERANGE;
145 if (__builtin_expect (ret != -1
146 #ifndef __ASSUME_PROC_SELF_FD_SYMLINK
147 /* This is for Linux 2.0. */
148 && buf[0] != '['
149 #endif
150 , 1))
152 #define UNREACHABLE_LEN strlen ("(unreachable)")
153 if (ret > UNREACHABLE_LEN
154 && memcmp (buf, "(unreachable)", UNREACHABLE_LEN) == 0)
156 memmove (buf, buf + UNREACHABLE_LEN, ret - UNREACHABLE_LEN);
157 ret -= UNREACHABLE_LEN;
160 /* readlink need not terminate the string. */
161 buf[ret] = '\0';
163 /* Verify readlink result, fall back on iterating through devices. */
164 if (buf[0] == '/'
165 && __xstat64 (_STAT_VER, buf, &st1) == 0
166 #ifdef _STATBUF_ST_RDEV
167 && S_ISCHR (st1.st_mode)
168 && st1.st_rdev == st.st_rdev
169 #else
170 && st1.st_ino == st.st_ino
171 && st1.st_dev == st.st_dev
172 #endif
174 return 0;
177 /* Prepare the result buffer. */
178 memcpy (buf, "/dev/pts/", sizeof ("/dev/pts/"));
179 buflen -= sizeof ("/dev/pts/") - 1;
181 if (__xstat64 (_STAT_VER, buf, &st1) == 0 && S_ISDIR (st1.st_mode))
183 #ifdef _STATBUF_ST_RDEV
184 ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, save,
185 &dostat);
186 #else
187 ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, save,
188 &dostat);
189 #endif
191 else
193 __set_errno (save);
194 ret = ENOENT;
197 if (ret && dostat != -1)
199 buf[sizeof ("/dev/") - 1] = '\0';
200 buflen += sizeof ("pts/") - 1;
201 #ifdef _STATBUF_ST_RDEV
202 ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, save,
203 &dostat);
204 #else
205 ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, save,
206 &dostat);
207 #endif
210 if (ret && dostat != -1)
212 buf[sizeof ("/dev/") - 1] = '\0';
213 dostat = 1;
214 #ifdef _STATBUF_ST_RDEV
215 ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino,
216 save, &dostat);
217 #else
218 ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino,
219 save, &dostat);
220 #endif
223 return ret;
226 weak_alias (__ttyname_r, ttyname_r)