|
/* |
|
* pseudoterminal_tcp.c - Illustrates how to use UNIX 98 pseudoterminals in |
|
* conjunction with TCP sockets |
|
* |
|
* Copyright (C) 2009 Javier Lopez-Gomez |
|
* |
|
* This program is free software; you can redistribute it and/or modify |
|
* it under the terms of the GNU General Public License as published by |
|
* the Free Software Foundation; either version 2 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* Compiler command line: `$ gcc -O2 -Wall -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 |
|
* \ -o pseudoterminal_tcp pseudoterminal_tcp.c` |
|
*/ |
|
|
|
#include <arpa/inet.h> |
|
#include <errno.h> |
|
#include <fcntl.h> |
|
#include <netinet/in.h> |
|
#include <netinet/ip.h> |
|
#include <signal.h> |
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
|
#include <sys/select.h> |
|
#include <sys/sendfile.h> |
|
#include <sys/socket.h> |
|
#include <sys/stat.h> |
|
#include <sys/types.h> |
|
#include <sys/wait.h> |
|
#include <termios.h> |
|
#include <unistd.h> |
|
|
|
#define BIND_ADDR INADDR_LOOPBACK |
|
#define BIND_PORT 2323 |
|
#define BACKLOG 50 |
|
#define BUF_SIZE 65536 |
|
#define SHELL "/bin/bash" |
|
|
|
#define FATAL(s) \ |
|
do { \ |
|
perror(s); \ |
|
exit(EXIT_FAILURE); \ |
|
} while (0) |
|
#undef max |
|
#define max(a, b) ((a) > (b) ? (a) : (b)) |
|
|
|
static pid_t _pid_shell; |
|
static _Bool _terminate = 0; |
|
|
|
// clang-format off |
|
/// \brief Spawn a process. If `sid != NULL`, the spawned process becomes the leader |
|
/// of a new session and its ID is returned in `*sid`. |
|
/// \param argv An array of pointers to NUL-terminated strings that represent the |
|
/// argument list (see exec(3)). |
|
/// \param fd_stdin (and `fd_(stdout|stderr)`) specify a file descriptor where stdin, |
|
/// stdout and stderr are redirected. |
|
// clang-format on |
|
pid_t spawn_process(char *const argv[], int fd_stdin, int fd_stdout, |
|
int fd_stderr, pid_t *sid) { |
|
pid_t child_pid; |
|
|
|
child_pid = fork(); |
|
if (child_pid == 0) { |
|
if (sid != NULL && (*sid = setsid()) == (pid_t)-1) |
|
FATAL("setsid"); |
|
|
|
if (dup2(fd_stdin, STDIN_FILENO) == -1 || |
|
dup2(fd_stdout, STDOUT_FILENO) == -1 || |
|
dup2(fd_stderr, STDERR_FILENO) == -1) |
|
FATAL("dup2"); |
|
|
|
execvp(argv[0], argv); |
|
FATAL("execvp"); |
|
} |
|
return child_pid; |
|
} |
|
|
|
/// \brief Relay data from / to `fd_tty` and `fd_socket` |
|
void do_relay(int fd_tty, int fd_socket) { |
|
fd_set readfds, writefds, exceptfds; |
|
int pipe_tty2socket[2], pipe_socket2tty[2]; |
|
size_t send_count = 0, recv_count = 0; |
|
|
|
if (pipe(pipe_tty2socket) == -1) |
|
FATAL("pipe"); |
|
if (pipe(pipe_socket2tty) == -1) |
|
FATAL("pipe"); |
|
|
|
while (!_terminate) { |
|
int r, nfds = 0; |
|
FD_ZERO(&readfds); |
|
FD_ZERO(&writefds); |
|
FD_ZERO(&exceptfds); |
|
FD_SET(fd_tty, &readfds); |
|
FD_SET(fd_socket, &readfds); |
|
FD_SET(fd_tty, &exceptfds); |
|
FD_SET(fd_socket, &exceptfds); |
|
nfds = max(fd_tty, fd_socket); |
|
r = select(nfds + 1, &readfds, &writefds, &exceptfds, NULL); |
|
|
|
if (r == -1) { |
|
if (errno == EINTR) |
|
continue; |
|
FATAL("select"); |
|
} |
|
|
|
if (FD_ISSET(fd_tty, &readfds)) { |
|
// FIXME: a simple `sendfile()` should do the job here; it, however, hangs |
|
// for some reason |
|
r = splice(fd_tty, NULL, pipe_tty2socket[1], NULL, BUF_SIZE, |
|
SPLICE_F_MOVE); |
|
if (r < 1) |
|
break; |
|
splice(pipe_tty2socket[0], NULL, fd_socket, NULL, r, SPLICE_F_MOVE); |
|
send_count += r; |
|
} |
|
if (FD_ISSET(fd_socket, &readfds)) { |
|
r = splice(fd_socket, NULL, pipe_socket2tty[1], NULL, BUF_SIZE, |
|
SPLICE_F_MOVE); |
|
if (r < 1) |
|
break; |
|
splice(pipe_socket2tty[0], NULL, fd_tty, NULL, r, |
|
SPLICE_F_MOVE | SPLICE_F_MOVE); |
|
recv_count += r; |
|
} |
|
fprintf(stderr, "\rUP [%ld bytes] | DOWN [%ld bytes]", send_count, |
|
recv_count); |
|
} |
|
|
|
close(pipe_tty2socket[0]); |
|
close(pipe_tty2socket[1]); |
|
close(pipe_socket2tty[0]); |
|
close(pipe_socket2tty[1]); |
|
} |
|
|
|
void handle_sigchld(int sig, siginfo_t *info, void *ucontext) { |
|
int wstatus; |
|
if (waitpid(info->si_pid, &wstatus, WNOHANG) == _pid_shell) |
|
_terminate = 1; |
|
} |
|
|
|
int main(int argc, char *argv[]) { |
|
struct sigaction sa; |
|
sa.sa_sigaction = &handle_sigchld; |
|
sa.sa_flags = SA_SIGINFO; |
|
sigaction(SIGCHLD, &sa, /*oldact=*/NULL); |
|
|
|
int fd_listen, fd_client; |
|
struct sockaddr_in sa_listen, sa_client; |
|
socklen_t sa_client_len; |
|
int fd_ptmx, fd_pts; |
|
char *ptspath; |
|
pid_t sid = 0; |
|
char *const shell_argv[] = {SHELL, NULL}; |
|
|
|
fd_listen = socket(AF_INET, SOCK_STREAM, 0); |
|
sa_listen.sin_family = AF_INET; |
|
sa_listen.sin_addr.s_addr = htonl(BIND_ADDR); |
|
sa_listen.sin_port = htons(BIND_PORT); |
|
if (bind(fd_listen, (struct sockaddr *)&sa_listen, |
|
sizeof(struct sockaddr_in)) == -1) |
|
FATAL("bind"); |
|
if (listen(fd_listen, BACKLOG) == -1) |
|
FATAL("listen"); |
|
fprintf(stderr, "listening on %s:%d\n", inet_ntoa(sa_listen.sin_addr), |
|
BIND_PORT); |
|
|
|
sa_client_len = sizeof(struct sockaddr_in); |
|
fd_client = accept(fd_listen, (struct sockaddr *)&sa_client, &sa_client_len); |
|
|
|
fd_ptmx = open("/dev/ptmx", O_RDWR); |
|
if (grantpt(fd_ptmx) == -1) |
|
FATAL("grantpt"); |
|
if (unlockpt(fd_ptmx) == -1) |
|
FATAL("unlockpt"); |
|
ptspath = ptsname(fd_ptmx); |
|
fd_pts = open(ptspath, O_RDWR); |
|
|
|
_pid_shell = spawn_process(shell_argv, fd_pts, fd_pts, fd_pts, &sid); |
|
fprintf(stderr, "_pid_shell=%d sid=%d ptspath=%s\n", _pid_shell, sid, |
|
ptspath); |
|
do_relay(fd_ptmx, fd_client); |
|
|
|
kill(_pid_shell, SIGTERM); |
|
close(fd_ptmx); |
|
shutdown(fd_client, SHUT_RDWR); |
|
close(fd_client); |
|
close(fd_listen); |
|
return EXIT_SUCCESS; |
|
} |