-
Notifications
You must be signed in to change notification settings - Fork 4
/
ptyproxy.c
168 lines (146 loc) · 4.13 KB
/
ptyproxy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#define _GNU_SOURCE 1
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <limits.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <poll.h>
#include <sysexits.h>
#ifdef __linux__
#include <pty.h>
#elif defined(__FreeBSD__)
#include <libutil.h>
#elif defined(__APPLE__) || defined(__OpenBSD__) || defined(__NetBSD__)
#include <util.h>
#include <sys/ioctl.h>
#endif
int do_proxy(int argc, char **argv)
{
pid_t child;
int master;
int child_status;
int exit_status = 0;
struct winsize win;
struct termios termios;
struct termios orig_termios;
if (argc < 1)
return -1;
/* Get current win size */
ioctl(0, TIOCGWINSZ, &win);
/* Ensure that terminal echo is switched off so that we
do not get back from the spawned process the same
messages that we have sent it. */
if (tcgetattr(STDIN_FILENO, &termios) < 0) {
perror("tcgetattr");
return -1;
}
orig_termios = termios;
child = forkpty(&master, NULL, &termios, &win);
if (child == -1) {
perror("forkpty");
exit(1);
}
if (child == 0) {
if (execvp(argv[0], argv)) {
perror("execl");
return -1;
}
} else {
struct pollfd pfds[2];
int log_in, log_out;
pfds[0].fd = master;
pfds[0].events = POLLOUT;
pfds[0].revents = 0;
pfds[1].fd = STDIN_FILENO;
pfds[1].events = POLLIN;
pfds[1].revents = 0;
fcntl(master, F_SETFL, O_NONBLOCK);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
termios.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);
termios.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR |
INPCK | ISTRIP | IXON | PARMRK);
termios.c_oflag &= ~OPOST;
termios.c_cc[VMIN] = 1;
termios.c_cc[VTIME] = 0;
if (tcsetattr(STDIN_FILENO, TCSANOW, &termios) < 0) {
perror("tcsetattr");
exit_status = -1;
}
log_in = open("in.log", O_WRONLY | O_CREAT | O_TRUNC, 0644);
log_out = open("out.log", O_WRONLY | O_CREAT | O_TRUNC, 0644);
while (1) {
int res;
char buf[4096];
int nb_fds;
pid_t pid;
pid = waitpid(child, &child_status, WNOHANG);
if (pid == -1) {
perror("waitpid");
exit_status = -1;
break;
}
if (pid == child && WIFEXITED(child_status)) {
break;
}
nb_fds = poll(pfds, 2, -1);
if (nb_fds < 1) {
perror("poll");
exit_status = -1;
break;
}
/* master */
if (pfds[0].revents & POLLOUT) {
nb_fds--;
do {
res = read(master, buf, 4096);
if (res > 0) {
write(STDOUT_FILENO, buf, res);
write(log_out, buf, res);
}
} while (res == 4096);
pfds[0].revents &= ~POLLOUT;
}
/* stdin */
if (nb_fds && pfds[1].revents & POLLIN) {
do {
res = read(STDIN_FILENO, buf, 4096);
if (res > 0) {
write(master, buf, res);
write(log_in, buf, res);
}
} while (res == 4096);
pfds[1].revents &= ~POLLIN;
}
}
if (tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios) < 0) {
perror("tcsetattr");
exit_status = -1;
}
close(log_in);
close(log_out);
close(master);
}
return exit_status;
}
static void
usage(void)
{
fprintf(stderr,
"ptyproxy prog...\n"
"prog is the software program to proxy");
exit(EX_USAGE);
}
int main (int argc, char **argv)
{
if (argc <= 1) {
usage();
}
do_proxy(--argc, ++argv);
return EX_OK;
}