-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptywrap.c
118 lines (100 loc) · 2.41 KB
/
ptywrap.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
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <pty.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#ifdef __linux__
#include <sys/syscall.h>
#include <linux/kcmp.h>
#endif
static int events[2];
static pid_t child;
static void copy(int src, int dst) {
ssize_t count, length, offset;
char buffer[PIPE_BUF];
while ((length = read(src, buffer, sizeof buffer))) {
if (length < 0 && errno != EINTR && errno != EAGAIN)
return;
offset = 0;
while (length > 0) {
if ((count = write(dst, buffer + offset, length)) > 0)
length -= count, offset += count;
if (count < 0 && errno != EINTR && errno != EAGAIN)
return;
}
}
}
static void reap(int signal) {
int status;
pid_t pid;
if (signal != SIGCHLD)
return;
while ((pid = waitpid(-1, &status, WNOHANG)) < 0)
if (errno != EINTR)
return;
if (pid != child)
return;
if (WIFSIGNALED(status))
status = 128 + WTERMSIG(status);
else
status = WEXITSTATUS(status);
while (write(events[1], &(unsigned char) { status }, 1))
if (errno != EINTR && errno != EAGAIN)
break;
child = -1;
}
static void wrap(int fd) {
unsigned char status = 1;
struct termios termios;
int master, slave;
if (openpty(&master, &slave, NULL, NULL, NULL) < 0)
err(1, "openpty");
if (tcgetattr(slave, &termios) < 0)
err(1, "tcgetattr");
cfmakeraw(&termios);
if (tcsetattr(slave, TCSANOW, &termios) < 0)
err(1, "tcsetattr");
child = fork();
switch (child) {
case -1:
err(1, "fork");
case 0:
if (dup2(slave, fd) < 0)
err(1, "dup2");
close(master);
close(slave);
return;
}
close(slave);
pipe(events);
signal(SIGCHLD, reap);
reap(SIGCHLD);
copy(master, fd);
while (read(events[0], &status, 1) < 0)
if (errno != EINTR && errno != EAGAIN)
break;
exit(status);
}
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Usage: %s CMD...\n", argv[0]);
return 64;
}
#ifdef __linux__
if (fcntl(1, F_GETFD) >= 0 && !isatty(1))
if (!syscall(SYS_kcmp, getpid(), getpid(), KCMP_FILE, 1, 2))
if (wrap(1), dup2(1, 2) < 0)
err(1, "dup2");
#endif
if (fcntl(1, F_GETFD) >= 0 && !isatty(1))
wrap(1);
if (fcntl(2, F_GETFD) >= 0 && !isatty(2))
wrap(2);
execvp(argv[1], argv + 1);
err(1, "exec %s", argv[1]);
}