2018-07-18 12:12:47 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2013, Tan Chee Eng (@tan-ce)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pts.c
|
|
|
|
*
|
|
|
|
* Manages the pseudo-terminal driver on Linux/Android and provides some
|
|
|
|
* helper functions to handle raw input mode and terminal window resizing
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <termios.h>
|
|
|
|
|
2020-06-17 10:17:28 +02:00
|
|
|
#include <utils.hpp>
|
2019-02-10 09:57:51 +01:00
|
|
|
|
2020-03-09 09:50:30 +01:00
|
|
|
#include "pts.hpp"
|
2018-07-18 12:12:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper functions
|
|
|
|
*/
|
|
|
|
// Ensures all the data is written out
|
|
|
|
static int write_blocking(int fd, char *buf, ssize_t bufsz) {
|
2020-12-31 07:11:24 +01:00
|
|
|
ssize_t ret, written;
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
written = 0;
|
|
|
|
do {
|
|
|
|
ret = write(fd, buf + written, bufsz - written);
|
|
|
|
if (ret == -1) return -1;
|
|
|
|
written += ret;
|
|
|
|
} while (written < bufsz);
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
return 0;
|
2018-07-18 12:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pump data from input FD to output FD. If close_output is
|
|
|
|
* true, then close the output FD when we're done.
|
|
|
|
*/
|
2018-11-20 10:40:42 +01:00
|
|
|
static void pump(int input, int output, bool close_output = true) {
|
2020-12-31 07:11:24 +01:00
|
|
|
char buf[4096];
|
|
|
|
int len;
|
|
|
|
while ((len = read(input, buf, 4096)) > 0) {
|
|
|
|
if (write_blocking(output, buf, len) == -1) break;
|
|
|
|
}
|
|
|
|
close(input);
|
|
|
|
if (close_output) close(output);
|
2018-07-18 12:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void* pump_thread(void* data) {
|
2020-12-31 07:11:24 +01:00
|
|
|
int *fds = (int*) data;
|
|
|
|
pump(fds[0], fds[1]);
|
|
|
|
delete[] fds;
|
|
|
|
return nullptr;
|
2018-07-18 12:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pump_async(int input, int output) {
|
2020-12-31 07:11:24 +01:00
|
|
|
pthread_t writer;
|
|
|
|
int *fds = new int[2];
|
|
|
|
fds[0] = input;
|
|
|
|
fds[1] = output;
|
|
|
|
pthread_create(&writer, nullptr, pump_thread, fds);
|
2018-07-18 12:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pts_open
|
|
|
|
*
|
|
|
|
* Opens a pts device and returns the name of the slave tty device.
|
|
|
|
*
|
|
|
|
* Arguments
|
|
|
|
* slave_name the name of the slave device
|
|
|
|
* slave_name_size the size of the buffer passed via slave_name
|
|
|
|
*
|
|
|
|
* Return Values
|
|
|
|
* on failure either -2 or -1 (errno set) is returned.
|
|
|
|
* on success, the file descriptor of the master device is returned.
|
|
|
|
*/
|
|
|
|
int pts_open(char *slave_name, size_t slave_name_size) {
|
2020-12-31 07:11:24 +01:00
|
|
|
int fdm;
|
2017-04-14 21:21:31 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
// Open master ptmx device
|
|
|
|
fdm = open("/dev/ptmx", O_RDWR);
|
|
|
|
if (fdm == -1)
|
|
|
|
goto error;
|
2017-04-14 21:21:31 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
// Get the slave name
|
|
|
|
if (ptsname_r(fdm, slave_name, slave_name_size-1))
|
|
|
|
goto error;
|
2017-04-14 21:21:31 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
slave_name[slave_name_size - 1] = '\0';
|
2017-04-14 21:21:31 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
// Grant, then unlock
|
|
|
|
if (grantpt(fdm) == -1)
|
|
|
|
goto error;
|
2017-04-14 21:21:31 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
if (unlockpt(fdm) == -1)
|
|
|
|
goto error;
|
2017-04-14 21:21:31 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
return fdm;
|
2017-04-14 21:21:31 +02:00
|
|
|
error:
|
2020-12-31 07:11:24 +01:00
|
|
|
close(fdm);
|
|
|
|
PLOGE("pts_open");
|
|
|
|
return -1;
|
2018-07-18 12:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stores the previous termios of stdin
|
|
|
|
static struct termios old_stdin;
|
|
|
|
static int stdin_is_raw = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set_stdin_raw
|
|
|
|
*
|
2021-01-11 11:19:10 +01:00
|
|
|
* Changes stdin to raw unbuffered mode, disables echo,
|
2018-07-18 12:12:47 +02:00
|
|
|
* auto carriage return, etc.
|
|
|
|
*
|
|
|
|
* Return Value
|
|
|
|
* on failure -1, and errno is set
|
|
|
|
* on success 0
|
|
|
|
*/
|
|
|
|
int set_stdin_raw(void) {
|
2020-12-31 07:11:24 +01:00
|
|
|
struct termios new_termios;
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
// Save the current stdin termios
|
|
|
|
if (tcgetattr(STDIN_FILENO, &old_stdin) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
// Start from the current settings
|
|
|
|
new_termios = old_stdin;
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
// Make the terminal like an SSH or telnet client
|
|
|
|
new_termios.c_iflag |= IGNPAR;
|
|
|
|
new_termios.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
|
|
|
|
new_termios.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
|
|
|
|
new_termios.c_oflag &= ~OPOST;
|
|
|
|
new_termios.c_cc[VMIN] = 1;
|
|
|
|
new_termios.c_cc[VTIME] = 0;
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &new_termios) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
stdin_is_raw = 1;
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
return 0;
|
2018-07-18 12:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* restore_stdin
|
|
|
|
*
|
|
|
|
* Restore termios on stdin to the state it was before
|
|
|
|
* set_stdin_raw() was called. If set_stdin_raw() was
|
|
|
|
* never called, does nothing and doesn't return an error.
|
|
|
|
*
|
|
|
|
* This function is async-safe.
|
|
|
|
*
|
|
|
|
* Return Value
|
|
|
|
* on failure, -1 and errno is set
|
|
|
|
* on success, 0
|
|
|
|
*/
|
|
|
|
int restore_stdin(void) {
|
2020-12-31 07:11:24 +01:00
|
|
|
if (!stdin_is_raw) return 0;
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &old_stdin) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
stdin_is_raw = 0;
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
return 0;
|
2018-07-18 12:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flag indicating whether the sigwinch watcher should terminate.
|
2018-11-20 10:40:42 +01:00
|
|
|
static volatile bool close_sigwinch_watcher = false;
|
2018-07-18 12:12:47 +02:00
|
|
|
|
|
|
|
/**
|
2021-01-11 11:19:10 +01:00
|
|
|
* Thread process. Wait for a SIGWINCH to be received, then update
|
2018-07-18 12:12:47 +02:00
|
|
|
* the terminal size.
|
|
|
|
*/
|
|
|
|
static void *watch_sigwinch(void *data) {
|
2020-12-31 07:11:24 +01:00
|
|
|
sigset_t winch;
|
|
|
|
int *fds = (int *)data;
|
|
|
|
int sig;
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
sigemptyset(&winch);
|
|
|
|
sigaddset(&winch, SIGWINCH);
|
|
|
|
pthread_sigmask(SIG_UNBLOCK, &winch, nullptr);
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
do {
|
|
|
|
if (close_sigwinch_watcher)
|
|
|
|
break;
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
// Get the new terminal size
|
|
|
|
struct winsize w;
|
|
|
|
if (ioctl(fds[0], TIOCGWINSZ, &w) == -1)
|
|
|
|
continue;
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
// Set the new terminal size
|
|
|
|
ioctl(fds[1], TIOCSWINSZ, &w);
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
} while (sigwait(&winch, &sig) == 0);
|
|
|
|
delete[] fds;
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
return nullptr;
|
2018-07-18 12:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* watch_sigwinch_async
|
|
|
|
*
|
|
|
|
* After calling this function, if the application receives
|
2021-01-11 11:19:10 +01:00
|
|
|
* SIGWINCH, the terminal window size will be read from
|
2018-07-18 12:12:47 +02:00
|
|
|
* "input" and set on "output".
|
|
|
|
*
|
|
|
|
* NOTE: This function blocks SIGWINCH and spawns a thread.
|
|
|
|
* NOTE 2: This function must be called before any of the
|
|
|
|
* pump functions.
|
|
|
|
*
|
|
|
|
* Arguments
|
|
|
|
* master A file descriptor of the TTY window size to follow
|
|
|
|
* slave A file descriptor of the TTY window size which is
|
|
|
|
* to be set on SIGWINCH
|
|
|
|
*
|
|
|
|
* Return Value
|
|
|
|
* on failure, -1 and errno will be set. In this case, no
|
2021-01-11 11:19:10 +01:00
|
|
|
* thread has been spawned and SIGWINCH will not be
|
2018-07-18 12:12:47 +02:00
|
|
|
* blocked.
|
|
|
|
* on success, 0
|
|
|
|
*/
|
|
|
|
int watch_sigwinch_async(int master, int slave) {
|
2020-12-31 07:11:24 +01:00
|
|
|
pthread_t watcher;
|
|
|
|
int *fds = new int[2];
|
|
|
|
|
|
|
|
// Block SIGWINCH so sigwait can later receive it
|
|
|
|
sigset_t winch;
|
|
|
|
sigemptyset(&winch);
|
|
|
|
sigaddset(&winch, SIGWINCH);
|
|
|
|
if (pthread_sigmask(SIG_BLOCK, &winch, nullptr) == -1) {
|
|
|
|
delete[] fds;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize some variables, then start the thread
|
|
|
|
close_sigwinch_watcher = 0;
|
|
|
|
fds[0] = master;
|
|
|
|
fds[1] = slave;
|
|
|
|
int ret = pthread_create(&watcher, nullptr, &watch_sigwinch, fds);
|
|
|
|
if (ret != 0) {
|
|
|
|
delete[] fds;
|
|
|
|
errno = ret;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-07-18 12:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pump_stdin_async
|
|
|
|
*
|
|
|
|
* Forward data from STDIN to the given FD
|
|
|
|
* in a seperate thread
|
|
|
|
*/
|
|
|
|
void pump_stdin_async(int outfd) {
|
2020-12-31 07:11:24 +01:00
|
|
|
// Put stdin into raw mode
|
|
|
|
set_stdin_raw();
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
// Pump data from stdin to the PTY
|
|
|
|
pump_async(STDIN_FILENO, outfd);
|
2018-07-18 12:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pump_stdout_blocking
|
|
|
|
*
|
|
|
|
* Forward data from the FD to STDOUT.
|
|
|
|
* Returns when the remote end of the FD closes.
|
|
|
|
*
|
|
|
|
* Before returning, restores stdin settings.
|
|
|
|
*/
|
|
|
|
void pump_stdout_blocking(int infd) {
|
2020-12-31 07:11:24 +01:00
|
|
|
// Pump data from stdout to PTY
|
|
|
|
pump(infd, STDOUT_FILENO, false /* Don't close output when done */);
|
2018-07-18 12:12:47 +02:00
|
|
|
|
2020-12-31 07:11:24 +01:00
|
|
|
// Cleanup
|
|
|
|
restore_stdin();
|
|
|
|
close_sigwinch_watcher = true;
|
|
|
|
raise(SIGWINCH);
|
2018-07-18 12:12:47 +02:00
|
|
|
}
|