Blame login/openpty.c

Packit 6c4009
/* Copyright (C) 1998-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
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 <fcntl.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <pty.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <termios.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Return the result of ptsname_r in the buffer pointed to by PTS,
Packit 6c4009
   which should be of length BUF_LEN.  If it is too long to fit in
Packit 6c4009
   this buffer, a sufficiently long buffer is allocated using malloc,
Packit 6c4009
   and returned in PTS.  0 is returned upon success, -1 otherwise.  */
Packit 6c4009
static int
Packit 6c4009
pts_name (int fd, char **pts, size_t buf_len)
Packit 6c4009
{
Packit 6c4009
  int rv;
Packit 6c4009
  char *buf = *pts;
Packit 6c4009
Packit 6c4009
  for (;;)
Packit 6c4009
    {
Packit 6c4009
      char *new_buf;
Packit 6c4009
Packit 6c4009
      if (buf_len)
Packit 6c4009
	{
Packit 6c4009
	  rv = ptsname_r (fd, buf, buf_len);
Packit 6c4009
Packit 6c4009
	  if (rv != 0 || memchr (buf, '\0', buf_len))
Packit 6c4009
	    /* We either got an error, or we succeeded and the
Packit 6c4009
	       returned name fit in the buffer.  */
Packit 6c4009
	    break;
Packit 6c4009
Packit 6c4009
	  /* Try again with a longer buffer.  */
Packit 6c4009
	  buf_len += buf_len;	/* Double it */
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	/* No initial buffer; start out by mallocing one.  */
Packit 6c4009
	buf_len = 128;		/* First time guess.  */
Packit 6c4009
Packit 6c4009
      if (buf != *pts)
Packit 6c4009
	/* We've already malloced another buffer at least once.  */
Packit 6c4009
	new_buf = realloc (buf, buf_len);
Packit 6c4009
      else
Packit 6c4009
	new_buf = malloc (buf_len);
Packit 6c4009
      if (! new_buf)
Packit 6c4009
	{
Packit 6c4009
	  rv = -1;
Packit 6c4009
	  __set_errno (ENOMEM);
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
      buf = new_buf;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (rv == 0)
Packit 6c4009
    *pts = buf;		/* Return buffer to the user.  */
Packit 6c4009
  else if (buf != *pts)
Packit 6c4009
    free (buf);		/* Free what we malloced when returning an error.  */
Packit 6c4009
Packit 6c4009
  return rv;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Create pseudo tty master slave pair and set terminal attributes
Packit 6c4009
   according to TERMP and WINP.  Return handles for both ends in
Packit 6c4009
   AMASTER and ASLAVE, and return the name of the slave end in NAME.  */
Packit 6c4009
int
Packit 6c4009
openpty (int *amaster, int *aslave, char *name,
Packit 6c4009
	 const struct termios *termp, const struct winsize *winp)
Packit 6c4009
{
Packit 6c4009
#ifdef PATH_MAX
Packit 6c4009
  char _buf[PATH_MAX];
Packit 6c4009
#else
Packit 6c4009
  char _buf[512];
Packit 6c4009
#endif
Packit 6c4009
  char *buf = _buf;
Packit 6c4009
  int master, ret = -1, slave = -1;
Packit 6c4009
Packit 6c4009
  *buf = '\0';
Packit 6c4009
Packit 6c4009
  master = getpt ();
Packit 6c4009
  if (master == -1)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  if (grantpt (master))
Packit 6c4009
    goto on_error;
Packit 6c4009
Packit 6c4009
  if (unlockpt (master))
Packit 6c4009
    goto on_error;
Packit 6c4009
Packit 6c4009
#ifdef TIOCGPTPEER
Packit 6c4009
  /* Try to allocate slave fd solely based on master fd first. */
Packit 6c4009
  slave = ioctl (master, TIOCGPTPEER, O_RDWR | O_NOCTTY);
Packit 6c4009
#endif
Packit 6c4009
  if (slave == -1)
Packit 6c4009
    {
Packit 6c4009
      /* Fallback to path-based slave fd allocation in case kernel doesn't
Packit 6c4009
       * support TIOCGPTPEER.
Packit 6c4009
       */
Packit 6c4009
      if (pts_name (master, &buf, sizeof (_buf)))
Packit 6c4009
        goto on_error;
Packit 6c4009
Packit 6c4009
      slave = open (buf, O_RDWR | O_NOCTTY);
Packit 6c4009
      if (slave == -1)
Packit 6c4009
        goto on_error;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* XXX Should we ignore errors here?  */
Packit 6c4009
  if (termp)
Packit 6c4009
    tcsetattr (slave, TCSAFLUSH, termp);
Packit 6c4009
#ifdef TIOCSWINSZ
Packit 6c4009
  if (winp)
Packit 6c4009
    ioctl (slave, TIOCSWINSZ, winp);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  *amaster = master;
Packit 6c4009
  *aslave = slave;
Packit 6c4009
  if (name != NULL)
Packit 6c4009
    {
Packit 6c4009
      if (*buf == '\0')
Packit 6c4009
        if (pts_name (master, &buf, sizeof (_buf)))
Packit 6c4009
          goto on_error;
Packit 6c4009
Packit 6c4009
      strcpy (name, buf);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  ret = 0;
Packit 6c4009
Packit 6c4009
 on_error:
Packit 6c4009
  if (ret == -1) {
Packit 6c4009
    close (master);
Packit 6c4009
Packit 6c4009
    if (slave != -1)
Packit 6c4009
      close (slave);
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  if (buf != _buf)
Packit 6c4009
    free (buf);
Packit 6c4009
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
libutil_hidden_def (openpty)