Blame sysdeps/unix/sysv/linux/ttyname_r.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <dirent.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <termios.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
#include <_itoa.h>
Packit 6c4009
Packit 6c4009
#include "ttyname.h"
Packit 6c4009
Packit 6c4009
static int getttyname_r (char *buf, size_t buflen,
Packit 6c4009
			 const struct stat64 *mytty, int save,
Packit 6c4009
			 int *dostat);
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
attribute_compat_text_section
Packit 6c4009
getttyname_r (char *buf, size_t buflen, const struct stat64 *mytty,
Packit 6c4009
	      int save, int *dostat)
Packit 6c4009
{
Packit 6c4009
  struct stat64 st;
Packit 6c4009
  DIR *dirstream;
Packit 6c4009
  struct dirent64 *d;
Packit 6c4009
  size_t devlen = strlen (buf);
Packit 6c4009
Packit 6c4009
  dirstream = __opendir (buf);
Packit 6c4009
  if (dirstream == NULL)
Packit 6c4009
    {
Packit 6c4009
      *dostat = -1;
Packit 6c4009
      return errno;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  while ((d = __readdir64 (dirstream)) != NULL)
Packit 6c4009
    if ((d->d_fileno == mytty->st_ino || *dostat)
Packit 6c4009
	&& strcmp (d->d_name, "stdin")
Packit 6c4009
	&& strcmp (d->d_name, "stdout")
Packit 6c4009
	&& strcmp (d->d_name, "stderr"))
Packit 6c4009
      {
Packit 6c4009
	char *cp;
Packit 6c4009
	size_t needed = _D_EXACT_NAMLEN (d) + 1;
Packit 6c4009
Packit 6c4009
	if (needed > buflen)
Packit 6c4009
	  {
Packit 6c4009
	    *dostat = -1;
Packit 6c4009
	    (void) __closedir (dirstream);
Packit 6c4009
	    __set_errno (ERANGE);
Packit 6c4009
	    return ERANGE;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
	cp = __stpncpy (buf + devlen, d->d_name, needed);
Packit 6c4009
	cp[0] = '\0';
Packit 6c4009
Packit 6c4009
	if (__xstat64 (_STAT_VER, buf, &st) == 0
Packit 6c4009
	    && is_mytty (mytty, &st))
Packit 6c4009
	  {
Packit 6c4009
	    (void) __closedir (dirstream);
Packit 6c4009
	    __set_errno (save);
Packit 6c4009
	    return 0;
Packit 6c4009
	  }
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  (void) __closedir (dirstream);
Packit 6c4009
  __set_errno (save);
Packit 6c4009
  /* It is not clear what to return in this case.  `isatty' says FD
Packit 6c4009
     refers to a TTY but no entry in /dev has this inode.  */
Packit 6c4009
  return ENOTTY;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Store at most BUFLEN character of the pathname of the terminal FD is
Packit 6c4009
   open on in BUF.  Return 0 on success,  otherwise an error number.  */
Packit 6c4009
int
Packit 6c4009
__ttyname_r (int fd, char *buf, size_t buflen)
Packit 6c4009
{
Packit 6c4009
  char procname[30];
Packit 6c4009
  struct stat64 st, st1;
Packit 6c4009
  int dostat = 0;
Packit 6c4009
  int doispty = 0;
Packit 6c4009
  int save = errno;
Packit 6c4009
Packit 6c4009
  /* Test for the absolute minimal size.  This makes life easier inside
Packit 6c4009
     the loop.  */
Packit 6c4009
  if (!buf)
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return EINVAL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (buflen < sizeof ("/dev/pts/"))
Packit 6c4009
    {
Packit 6c4009
      __set_errno (ERANGE);
Packit 6c4009
      return ERANGE;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* isatty check, tcgetattr is used because it sets the correct
Packit 6c4009
     errno (EBADF resp. ENOTTY) on error.  */
Packit 6c4009
  struct termios term;
Packit 6c4009
  if (__glibc_unlikely (__tcgetattr (fd, &term) < 0))
Packit 6c4009
    return errno;
Packit 6c4009
Packit 6c4009
  if (__fxstat64 (_STAT_VER, fd, &st) < 0)
Packit 6c4009
    return errno;
Packit 6c4009
Packit 6c4009
  /* We try using the /proc filesystem.  */
Packit 6c4009
  *_fitoa_word (fd, __stpcpy (procname, "/proc/self/fd/"), 10, 0) = '\0';
Packit 6c4009
Packit 6c4009
  ssize_t ret = __readlink (procname, buf, buflen - 1);
Packit 6c4009
  if (__glibc_unlikely (ret == -1 && errno == ENAMETOOLONG))
Packit 6c4009
    {
Packit 6c4009
      __set_errno (ERANGE);
Packit 6c4009
      return ERANGE;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (__glibc_likely (ret != -1))
Packit 6c4009
    {
Packit 6c4009
#define UNREACHABLE_LEN strlen ("(unreachable)")
Packit 6c4009
      if (ret > UNREACHABLE_LEN
Packit 6c4009
	  && memcmp (buf, "(unreachable)", UNREACHABLE_LEN) == 0)
Packit 6c4009
	{
Packit 6c4009
	  memmove (buf, buf + UNREACHABLE_LEN, ret - UNREACHABLE_LEN);
Packit 6c4009
	  ret -= UNREACHABLE_LEN;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* readlink need not terminate the string.  */
Packit 6c4009
      buf[ret] = '\0';
Packit 6c4009
Packit 6c4009
      /* Verify readlink result, fall back on iterating through devices.  */
Packit 6c4009
      if (buf[0] == '/'
Packit 6c4009
	  && __xstat64 (_STAT_VER, buf, &st1) == 0
Packit 6c4009
	  && is_mytty (&st, &st1))
Packit 6c4009
	return 0;
Packit 6c4009
Packit 6c4009
      doispty = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Prepare the result buffer.  */
Packit 6c4009
  memcpy (buf, "/dev/pts/", sizeof ("/dev/pts/"));
Packit 6c4009
  buflen -= sizeof ("/dev/pts/") - 1;
Packit 6c4009
Packit 6c4009
  if (__xstat64 (_STAT_VER, buf, &st1) == 0 && S_ISDIR (st1.st_mode))
Packit 6c4009
    {
Packit 6c4009
      ret = getttyname_r (buf, buflen, &st, save,
Packit 6c4009
			  &dostat);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      __set_errno (save);
Packit 6c4009
      ret = ENOENT;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (ret && dostat != -1)
Packit 6c4009
    {
Packit 6c4009
      buf[sizeof ("/dev/") - 1] = '\0';
Packit 6c4009
      buflen += sizeof ("pts/") - 1;
Packit 6c4009
      ret = getttyname_r (buf, buflen, &st, save,
Packit 6c4009
			  &dostat);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (ret && dostat != -1)
Packit 6c4009
    {
Packit 6c4009
      buf[sizeof ("/dev/") - 1] = '\0';
Packit 6c4009
      dostat = 1;
Packit 6c4009
      ret = getttyname_r (buf, buflen, &st,
Packit 6c4009
			  save, &dostat);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (ret && doispty && is_pty (&st))
Packit 6c4009
    {
Packit 6c4009
      /* We failed to figure out the TTY's name, but we can at least
Packit 6c4009
         signal that we did verify that it really is a PTY slave.
Packit 6c4009
         This happens when we have inherited the file descriptor from
Packit 6c4009
         a different mount namespace.  */
Packit 6c4009
      __set_errno (ENODEV);
Packit 6c4009
      return ENODEV;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
weak_alias (__ttyname_r, ttyname_r)