|
Packit Service |
82fcde |
/* Open a pseudoterminal.
|
|
Packit Service |
82fcde |
Copyright (C) 2018 Free Software Foundation, Inc.
|
|
Packit Service |
82fcde |
This file is part of the GNU C Library.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
82fcde |
modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License as published by the Free Software Foundation; either
|
|
Packit Service |
82fcde |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
82fcde |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
82fcde |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
82fcde |
Lesser General Public License for more details.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License along with the GNU C Library; if not, see
|
|
Packit Service |
82fcde |
<http://www.gnu.org/licenses/>. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <support/tty.h>
|
|
Packit Service |
82fcde |
#include <support/check.h>
|
|
Packit Service |
82fcde |
#include <support/support.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <errno.h>
|
|
Packit Service |
82fcde |
#include <stdlib.h>
|
|
Packit Service |
82fcde |
#include <string.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <fcntl.h>
|
|
Packit Service |
82fcde |
#include <termios.h>
|
|
Packit Service |
82fcde |
#include <sys/ioctl.h>
|
|
Packit Service |
82fcde |
#include <unistd.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* As ptsname, but allocates space for an appropriately-sized string
|
|
Packit Service |
82fcde |
using malloc. */
|
|
Packit Service |
82fcde |
static char *
|
|
Packit Service |
82fcde |
xptsname (int fd)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
int rv;
|
|
Packit Service |
82fcde |
size_t buf_len = 128;
|
|
Packit Service |
82fcde |
char *buf = xmalloc (buf_len);
|
|
Packit Service |
82fcde |
for (;;)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
rv = ptsname_r (fd, buf, buf_len);
|
|
Packit Service |
82fcde |
if (rv)
|
|
Packit Service |
82fcde |
FAIL_EXIT1 ("ptsname_r: %s", strerror (errno));
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (memchr (buf, '\0', buf_len))
|
|
Packit Service |
82fcde |
return buf; /* ptsname succeeded and the buffer was not truncated */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
buf_len *= 2;
|
|
Packit Service |
82fcde |
buf = xrealloc (buf, buf_len);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
void
|
|
Packit Service |
82fcde |
support_openpty (int *a_outer, int *a_inner, char **a_name,
|
|
Packit Service |
82fcde |
const struct termios *termp,
|
|
Packit Service |
82fcde |
const struct winsize *winp)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
int outer = -1, inner = -1;
|
|
Packit Service |
82fcde |
char *namebuf = 0;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
outer = posix_openpt (O_RDWR | O_NOCTTY);
|
|
Packit Service |
82fcde |
if (outer == -1)
|
|
Packit Service |
82fcde |
FAIL_EXIT1 ("posix_openpt: %s", strerror (errno));
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (grantpt (outer))
|
|
Packit Service |
82fcde |
FAIL_EXIT1 ("grantpt: %s", strerror (errno));
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (unlockpt (outer))
|
|
Packit Service |
82fcde |
FAIL_EXIT1 ("unlockpt: %s", strerror (errno));
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#ifdef TIOCGPTPEER
|
|
Packit Service |
82fcde |
inner = ioctl (outer, TIOCGPTPEER, O_RDWR | O_NOCTTY);
|
|
Packit Service |
82fcde |
#endif
|
|
Packit Service |
82fcde |
if (inner == -1)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
/* The kernel might not support TIOCGPTPEER, fall back to open
|
|
Packit Service |
82fcde |
by name. */
|
|
Packit Service |
82fcde |
namebuf = xptsname (outer);
|
|
Packit Service |
82fcde |
inner = open (namebuf, O_RDWR | O_NOCTTY);
|
|
Packit Service |
82fcde |
if (inner == -1)
|
|
Packit Service |
82fcde |
FAIL_EXIT1 ("%s: %s", namebuf, strerror (errno));
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (termp)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
if (tcsetattr (inner, TCSAFLUSH, termp))
|
|
Packit Service |
82fcde |
FAIL_EXIT1 ("tcsetattr: %s", strerror (errno));
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
#ifdef TIOCSWINSZ
|
|
Packit Service |
82fcde |
if (winp)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
if (ioctl (inner, TIOCSWINSZ, winp))
|
|
Packit Service |
82fcde |
FAIL_EXIT1 ("TIOCSWINSZ: %s", strerror (errno));
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
#endif
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (a_name)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
if (!namebuf)
|
|
Packit Service |
82fcde |
namebuf = xptsname (outer);
|
|
Packit Service |
82fcde |
*a_name = namebuf;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else
|
|
Packit Service |
82fcde |
free (namebuf);
|
|
Packit Service |
82fcde |
*a_outer = outer;
|
|
Packit Service |
82fcde |
*a_inner = inner;
|
|
Packit Service |
82fcde |
}
|