|
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 |
#if 0
|
|
Packit |
6c4009 |
/* Is this used anywhere? It is not exported. */
|
|
Packit |
6c4009 |
char *__ttyname;
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static char *getttyname (const char *dev, const struct stat64 *mytty,
|
|
Packit |
6c4009 |
int save, int *dostat);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
libc_freeres_ptr (static char *getttyname_name);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static char *
|
|
Packit |
6c4009 |
attribute_compat_text_section
|
|
Packit |
6c4009 |
getttyname (const char *dev, const struct stat64 *mytty, int save, int *dostat)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
static size_t namelen;
|
|
Packit |
6c4009 |
struct stat64 st;
|
|
Packit |
6c4009 |
DIR *dirstream;
|
|
Packit |
6c4009 |
struct dirent64 *d;
|
|
Packit |
6c4009 |
size_t devlen = strlen (dev) + 1;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
dirstream = __opendir (dev);
|
|
Packit |
6c4009 |
if (dirstream == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
*dostat = -1;
|
|
Packit |
6c4009 |
return NULL;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Prepare for the loop. If we already have a buffer copy the directory
|
|
Packit |
6c4009 |
name we look at into it. */
|
|
Packit |
6c4009 |
if (devlen < namelen)
|
|
Packit |
6c4009 |
*((char *) __mempcpy (getttyname_name, dev, devlen - 1)) = '/';
|
|
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 |
size_t dlen = _D_ALLOC_NAMLEN (d);
|
|
Packit |
6c4009 |
if (devlen + dlen > namelen)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
free (getttyname_name);
|
|
Packit |
6c4009 |
namelen = 2 * (devlen + dlen); /* Big enough. */
|
|
Packit |
6c4009 |
getttyname_name = malloc (namelen);
|
|
Packit |
6c4009 |
if (! getttyname_name)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
*dostat = -1;
|
|
Packit |
6c4009 |
/* Perhaps it helps to free the directory stream buffer. */
|
|
Packit |
6c4009 |
(void) __closedir (dirstream);
|
|
Packit |
6c4009 |
return NULL;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
*((char *) __mempcpy (getttyname_name, dev, devlen - 1)) = '/';
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
memcpy (&getttyname_name[devlen], d->d_name, dlen);
|
|
Packit |
6c4009 |
if (__xstat64 (_STAT_VER, getttyname_name, &st) == 0
|
|
Packit |
6c4009 |
&& is_mytty (mytty, &st))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
(void) __closedir (dirstream);
|
|
Packit |
6c4009 |
#if 0
|
|
Packit |
6c4009 |
__ttyname = getttyname_name;
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
__set_errno (save);
|
|
Packit |
6c4009 |
return getttyname_name;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
(void) __closedir (dirstream);
|
|
Packit |
6c4009 |
__set_errno (save);
|
|
Packit |
6c4009 |
return NULL;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Static buffer in `ttyname'. */
|
|
Packit |
6c4009 |
libc_freeres_ptr (static char *ttyname_buf);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Return the pathname of the terminal FD is open on, or NULL on errors.
|
|
Packit |
6c4009 |
The returned storage is good only until the next call to this function. */
|
|
Packit |
6c4009 |
char *
|
|
Packit |
6c4009 |
ttyname (int fd)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
static size_t buflen;
|
|
Packit |
6c4009 |
char procname[30];
|
|
Packit |
6c4009 |
struct stat64 st, st1;
|
|
Packit |
6c4009 |
int dostat = 0;
|
|
Packit |
6c4009 |
int doispty = 0;
|
|
Packit |
6c4009 |
char *name;
|
|
Packit |
6c4009 |
int save = errno;
|
|
Packit |
6c4009 |
struct termios term;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* isatty check, tcgetattr is used because it sets the correct
|
|
Packit |
6c4009 |
errno (EBADF resp. ENOTTY) on error. */
|
|
Packit |
6c4009 |
if (__glibc_unlikely (__tcgetattr (fd, &term) < 0))
|
|
Packit |
6c4009 |
return NULL;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (__fxstat64 (_STAT_VER, fd, &st) < 0)
|
|
Packit |
6c4009 |
return NULL;
|
|
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 |
if (buflen == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
buflen = 4095;
|
|
Packit |
6c4009 |
ttyname_buf = (char *) malloc (buflen + 1);
|
|
Packit |
6c4009 |
if (ttyname_buf == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
buflen = 0;
|
|
Packit |
6c4009 |
return NULL;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ssize_t len = __readlink (procname, ttyname_buf, buflen);
|
|
Packit |
6c4009 |
if (__glibc_likely (len != -1))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if ((size_t) len >= buflen)
|
|
Packit |
6c4009 |
return NULL;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define UNREACHABLE_LEN strlen ("(unreachable)")
|
|
Packit |
6c4009 |
if (len > UNREACHABLE_LEN
|
|
Packit |
6c4009 |
&& memcmp (ttyname_buf, "(unreachable)", UNREACHABLE_LEN) == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
memmove (ttyname_buf, ttyname_buf + UNREACHABLE_LEN,
|
|
Packit |
6c4009 |
len - UNREACHABLE_LEN);
|
|
Packit |
6c4009 |
len -= UNREACHABLE_LEN;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* readlink need not terminate the string. */
|
|
Packit |
6c4009 |
ttyname_buf[len] = '\0';
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Verify readlink result, fall back on iterating through devices. */
|
|
Packit |
6c4009 |
if (ttyname_buf[0] == '/'
|
|
Packit |
6c4009 |
&& __xstat64 (_STAT_VER, ttyname_buf, &st1) == 0
|
|
Packit |
6c4009 |
&& is_mytty (&st, &st1))
|
|
Packit |
6c4009 |
return ttyname_buf;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
doispty = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (__xstat64 (_STAT_VER, "/dev/pts", &st1) == 0 && S_ISDIR (st1.st_mode))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
name = getttyname ("/dev/pts", &st, save, &dostat);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
__set_errno (save);
|
|
Packit |
6c4009 |
name = NULL;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (!name && dostat != -1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
name = getttyname ("/dev", &st, save, &dostat);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (!name && dostat != -1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
dostat = 1;
|
|
Packit |
6c4009 |
name = getttyname ("/dev", &st, save, &dostat);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (!name && 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 NULL;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return name;
|
|
Packit |
6c4009 |
}
|