-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathntmux.c
183 lines (159 loc) · 4.45 KB
/
ntmux.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright (c) 2014 Yoshinori Kohyama.
* Distributed under the BSD 3-Clause License.
* See the file LICENSE.md.
*/
#include <sys/ioctl.h> /* ioctl(2) */
#include <sys/select.h> /* select(2) */
#include <sys/socket.h> /* socket(2), getsockname(2), setsockopt(2),
bind(2), listen(2), accept(2) */
#include <sys/types.h> /* read(2) */
#include <sys/uio.h> /* read(2) */
#include <sys/wait.h> /* waitpid(2) */
#include <arpa/inet.h> /* htons(3) */
#include <err.h> /* err(3) */
#include <errno.h> /* EINITR */
#include <fcntl.h> /* fcntl(2) */
#include <signal.h> /* sigaction(2) */
#include <stdio.h> /* BUFSIZ */
#include <stdlib.h> /* system(3), atoi(3), exit(3), malloc(3), free(3) */
#include <string.h> /* memcpy(3), strcpy(3) */
#include <termios.h> /* tcgetattr(3) */
#include <unistd.h> /* pipe(2), fork(2), dup2(2), close(2), execvp(3),
read(2), write(2) */
#ifdef Linux
#include <pty.h> /* forkpty(3) */
#endif
#ifdef Darwin // Compiles to OSX
#include "util.h" /* forkpty(3) */
#endif
static void
reapchild(int signo)
{
int status;
while (waitpid(-1, &status, WNOHANG) > 0)
;
if (WIFEXITED(status))
_exit(WEXITSTATUS(status));
_exit(3);
}
static void
server(int port, int pty)
{
fd_set fds, tfds;
int nc = 0; /* num of clients */
int ss; /* server socket */
int cs; /* client socket */
struct _cs {
struct _cs *prev;
struct _cs *next;
int cs;
} css = {NULL, NULL, -1}; /* list of client sockets */
struct _cs *csp, *tcsp;
int m; /* max # of file descriters to be read */
int i, n, yes = 1;
struct sockaddr_in addr;
char buf[BUFSIZ];
socklen_t addrlen;
if ((ss = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err(1, "socket");
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons((unsigned short)port);
addrlen = sizeof(addr);
if (setsockopt(ss, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0)
err(1, "setsockopt");
if (bind(ss, (struct sockaddr *)&addr, addrlen) < 0)
err(1, "bind");
if (getsockname(ss, (struct sockaddr *)&addr, &addrlen))
err(1, "getsockname");
listen(ss, 5);
FD_ZERO(&fds);
FD_SET(0, &fds);
FD_SET(pty, &fds);
FD_SET(ss, &fds);
for (; ; ) {
memcpy(&tfds, &fds, sizeof(fd_set));
m = ((pty > ss)?pty:ss);
for (csp = css.next; csp != NULL; csp = csp->next)
if (csp->cs > m)
m = csp->cs;
if (select(m + 1, &tfds, NULL, NULL, NULL) < 0)
err(1, "select");
/* inputs from terminal */
if (FD_ISSET(0, &tfds)) {
n = read(0, buf, BUFSIZ);
write(pty, buf, n);
}
/* event from client sockets */
for (csp = css.next; csp != NULL; csp = csp->next) {
if (FD_ISSET(csp->cs, &tfds)) {
/* some bytes */
if ((n = read(csp->cs, buf, BUFSIZ)) > 0) {
write(pty, buf, n);
/* closed by client */
} else {
close(csp->cs);
FD_CLR(csp->cs, &fds);
csp->prev->next = csp->next;
if (csp->next != NULL)
csp->next->prev = csp->prev;
tcsp = csp;
csp = csp->prev;
free(tcsp);
}
}
}
/* outputs from command */
if (FD_ISSET(pty, &tfds)) {
n = read(pty, buf, BUFSIZ);
write(1, buf, n);
}
/* new client connection */
if (FD_ISSET(ss, &tfds)) {
if ((cs = accept(ss, (struct sockaddr *)&addr, &addrlen)) < 0) {
if (errno == EINTR)
continue;
err(1, "accept");
}
csp = (struct _cs *)malloc(sizeof(struct _cs));
csp->cs = cs;
FD_SET(csp->cs, &fds);
tcsp = css.next;
css.next = csp;
csp->prev = &css;
csp->next = tcsp;
}
}
}
int
main(int argc, char *argv[])
{
int port, master;
struct sigaction sa;
struct termios t;
struct winsize w;
if (argc < 3) {
fprintf(stderr, "Usage: ntmux <port> command args ...");
exit(1);
}
port = atoi(argv[1]);
sa.sa_handler = reapchild;
sigaction(SIGCHLD, &sa, NULL);
tcgetattr(0, &t);
ioctl(0, TIOCGWINSZ, &w);
switch (forkpty(&master, NULL, &t, &w)) {
case -1:
err(1, "forkpty");
case 0:
if (execvp(argv[2], argv + 2) < 0)
err(1, "execvp");
default:
t.c_lflag &= ~(ECHO|ICANON);
t.c_cc[VTIME] = 0;
t.c_cc[VMIN] = 1;
tcsetattr(0, TCSANOW, &t);
fcntl(0, F_SETFL, O_NONBLOCK);
server(port, master);
}
}