Blame support/support_openpty.c

Packit 6c4009
/* Open a pseudoterminal.
Packit 6c4009
   Copyright (C) 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 <support/tty.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/support.h>
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <termios.h>
Packit 6c4009
#include <sys/ioctl.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
/* As ptsname, but allocates space for an appropriately-sized string
Packit 6c4009
   using malloc.  */
Packit 6c4009
static char *
Packit 6c4009
xptsname (int fd)
Packit 6c4009
{
Packit 6c4009
  int rv;
Packit 6c4009
  size_t buf_len = 128;
Packit 6c4009
  char *buf = xmalloc (buf_len);
Packit 6c4009
  for (;;)
Packit 6c4009
    {
Packit 6c4009
      rv = ptsname_r (fd, buf, buf_len);
Packit 6c4009
      if (rv)
Packit 6c4009
        FAIL_EXIT1 ("ptsname_r: %s", strerror (errno));
Packit 6c4009
Packit 6c4009
      if (memchr (buf, '\0', buf_len))
Packit 6c4009
        return buf; /* ptsname succeeded and the buffer was not truncated */
Packit 6c4009
Packit 6c4009
      buf_len *= 2;
Packit 6c4009
      buf = xrealloc (buf, buf_len);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
support_openpty (int *a_outer, int *a_inner, char **a_name,
Packit 6c4009
                 const struct termios *termp,
Packit 6c4009
                 const struct winsize *winp)
Packit 6c4009
{
Packit 6c4009
  int outer = -1, inner = -1;
Packit 6c4009
  char *namebuf = 0;
Packit 6c4009
Packit 6c4009
  outer = posix_openpt (O_RDWR | O_NOCTTY);
Packit 6c4009
  if (outer == -1)
Packit 6c4009
    FAIL_EXIT1 ("posix_openpt: %s", strerror (errno));
Packit 6c4009
Packit 6c4009
  if (grantpt (outer))
Packit 6c4009
    FAIL_EXIT1 ("grantpt: %s", strerror (errno));
Packit 6c4009
Packit 6c4009
  if (unlockpt (outer))
Packit 6c4009
    FAIL_EXIT1 ("unlockpt: %s", strerror (errno));
Packit 6c4009
Packit 6c4009
Packit 6c4009
#ifdef TIOCGPTPEER
Packit 6c4009
  inner = ioctl (outer, TIOCGPTPEER, O_RDWR | O_NOCTTY);
Packit 6c4009
#endif
Packit 6c4009
  if (inner == -1)
Packit 6c4009
    {
Packit 6c4009
      /* The kernel might not support TIOCGPTPEER, fall back to open
Packit 6c4009
         by name.  */
Packit 6c4009
      namebuf = xptsname (outer);
Packit 6c4009
      inner = open (namebuf, O_RDWR | O_NOCTTY);
Packit 6c4009
      if (inner == -1)
Packit 6c4009
        FAIL_EXIT1 ("%s: %s", namebuf, strerror (errno));
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (termp)
Packit 6c4009
    {
Packit 6c4009
      if (tcsetattr (inner, TCSAFLUSH, termp))
Packit 6c4009
        FAIL_EXIT1 ("tcsetattr: %s", strerror (errno));
Packit 6c4009
    }
Packit 6c4009
#ifdef TIOCSWINSZ
Packit 6c4009
  if (winp)
Packit 6c4009
    {
Packit 6c4009
      if (ioctl (inner, TIOCSWINSZ, winp))
Packit 6c4009
        FAIL_EXIT1 ("TIOCSWINSZ: %s", strerror (errno));
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  if (a_name)
Packit 6c4009
    {
Packit 6c4009
      if (!namebuf)
Packit 6c4009
        namebuf = xptsname (outer);
Packit 6c4009
      *a_name = namebuf;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    free (namebuf);
Packit 6c4009
  *a_outer = outer;
Packit 6c4009
  *a_inner = inner;
Packit 6c4009
}