Back to index

glibc  2.9
ttyname_r.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,92,93,1995-2001,2003,2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <limits.h>
00021 #include <stddef.h>
00022 #include <dirent.h>
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025 #include <termios.h>
00026 #include <unistd.h>
00027 #include <string.h>
00028 #include <stdlib.h>
00029 
00030 #include <stdio-common/_itoa.h>
00031 #include <kernel-features.h>
00032 
00033 static int getttyname_r (char *buf, size_t buflen,
00034                       dev_t mydev, ino64_t myino, int save,
00035                       int *dostat) internal_function;
00036 
00037 static int
00038 internal_function attribute_compat_text_section
00039 getttyname_r (char *buf, size_t buflen, dev_t mydev, ino64_t myino,
00040              int save, int *dostat)
00041 {
00042   struct stat64 st;
00043   DIR *dirstream;
00044   struct dirent64 *d;
00045   size_t devlen = strlen (buf);
00046 
00047   dirstream = __opendir (buf);
00048   if (dirstream == NULL)
00049     {
00050       *dostat = -1;
00051       return errno;
00052     }
00053 
00054   while ((d = __readdir64 (dirstream)) != NULL)
00055     if ((d->d_fileno == myino || *dostat)
00056        && strcmp (d->d_name, "stdin")
00057        && strcmp (d->d_name, "stdout")
00058        && strcmp (d->d_name, "stderr"))
00059       {
00060        char *cp;
00061        size_t needed = _D_EXACT_NAMLEN (d) + 1;
00062 
00063        if (needed > buflen)
00064          {
00065            *dostat = -1;
00066            (void) __closedir (dirstream);
00067            __set_errno (ERANGE);
00068            return ERANGE;
00069          }
00070 
00071        cp = __stpncpy (buf + devlen, d->d_name, needed);
00072        cp[0] = '\0';
00073 
00074        if (__xstat64 (_STAT_VER, buf, &st) == 0
00075 #ifdef _STATBUF_ST_RDEV
00076            && S_ISCHR (st.st_mode) && st.st_rdev == mydev
00077 #else
00078            && d->d_fileno == myino && st.st_dev == mydev
00079 #endif
00080           )
00081          {
00082            (void) __closedir (dirstream);
00083            __set_errno (save);
00084            return 0;
00085          }
00086       }
00087 
00088   (void) __closedir (dirstream);
00089   __set_errno (save);
00090   /* It is not clear what to return in this case.  `isatty' says FD
00091      refers to a TTY but no entry in /dev has this inode.  */
00092   return ENOTTY;
00093 }
00094 
00095 /* Store at most BUFLEN character of the pathname of the terminal FD is
00096    open on in BUF.  Return 0 on success,  otherwise an error number.  */
00097 int
00098 __ttyname_r (int fd, char *buf, size_t buflen)
00099 {
00100   char procname[30];
00101   struct stat64 st, st1;
00102   int dostat = 0;
00103   int save = errno;
00104 
00105   /* Test for the absolute minimal size.  This makes life easier inside
00106      the loop.  */
00107   if (!buf)
00108     {
00109       __set_errno (EINVAL);
00110       return EINVAL;
00111     }
00112 
00113   if (buflen < sizeof ("/dev/pts/"))
00114     {
00115       __set_errno (ERANGE);
00116       return ERANGE;
00117     }
00118 
00119   /* isatty check, tcgetattr is used because it sets the correct
00120      errno (EBADF resp. ENOTTY) on error.  */
00121   struct termios term;
00122   if (__builtin_expect (__tcgetattr (fd, &term) < 0, 0))
00123     return errno;
00124 
00125   /* We try using the /proc filesystem.  */
00126   *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0';
00127 
00128   ssize_t ret = __readlink (procname, buf, buflen - 1);
00129   if (__builtin_expect (ret == -1 && errno == ENOENT, 0))
00130     {
00131       __set_errno (EBADF);
00132       return EBADF;
00133     }
00134 
00135   if (__builtin_expect (ret == -1 && errno == ENAMETOOLONG, 0))
00136     {
00137       __set_errno (ERANGE);
00138       return ERANGE;
00139     }
00140 
00141   if (__builtin_expect (ret != -1
00142 #ifndef __ASSUME_PROC_SELF_FD_SYMLINK
00143                      /* This is for Linux 2.0.  */
00144                      && buf[0] != '['
00145 #endif
00146                      , 1))
00147     {
00148       buf[ret] = '\0';
00149       return 0;
00150     }
00151 
00152   if (__fxstat64 (_STAT_VER, fd, &st) < 0)
00153     return errno;
00154 
00155   /* Prepare the result buffer.  */
00156   memcpy (buf, "/dev/pts/", sizeof ("/dev/pts/"));
00157   buflen -= sizeof ("/dev/pts/") - 1;
00158 
00159   if (__xstat64 (_STAT_VER, buf, &st1) == 0 && S_ISDIR (st1.st_mode))
00160     {
00161 #ifdef _STATBUF_ST_RDEV
00162       ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, save,
00163                        &dostat);
00164 #else
00165       ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, save,
00166                        &dostat);
00167 #endif
00168     }
00169   else
00170     {
00171       __set_errno (save);
00172       ret = ENOENT;
00173     }
00174 
00175   if (ret && dostat != -1)
00176     {
00177       buf[sizeof ("/dev/") - 1] = '\0';
00178       buflen += sizeof ("pts/") - 1;
00179 #ifdef _STATBUF_ST_RDEV
00180       ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino, save,
00181                        &dostat);
00182 #else
00183       ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino, save,
00184                        &dostat);
00185 #endif
00186     }
00187 
00188   if (ret && dostat != -1)
00189     {
00190       buf[sizeof ("/dev/") - 1] = '\0';
00191       dostat = 1;
00192 #ifdef _STATBUF_ST_RDEV
00193       ret = getttyname_r (buf, buflen, st.st_rdev, st.st_ino,
00194                        save, &dostat);
00195 #else
00196       ret = getttyname_r (buf, buflen, st.st_dev, st.st_ino,
00197                        save, &dostat);
00198 #endif
00199     }
00200 
00201   return ret;
00202 }
00203 
00204 weak_alias (__ttyname_r, ttyname_r)