-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathshim.c
More file actions
371 lines (322 loc) · 9.26 KB
/
Copy pathshim.c
File metadata and controls
371 lines (322 loc) · 9.26 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#define _GNU_SOURCE 600
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define EPOLL_EVENTS 128
#define PTSNAME_SIZE 1024
static int write_all(int fd, const void *buf, size_t count) {
const void *p = buf;
size_t remain = count;
ssize_t n;
while (remain > 0) {
do {
n = write(fd, p, remain);
} while (n == -1 && errno == EINTR);
if (n <= 0) {
return -1;
}
p += n;
remain -= n;
}
return 0;
}
static int socket_listen(const char *port) {
struct addrinfo hints;
memset(&hints, 0, sizeof (struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
struct addrinfo *ainfo;
if (getaddrinfo(NULL, port, &hints, &ainfo) != 0) {
perror("getaddrinfo() failed");
return -1;
}
int sock;
struct addrinfo *rp;
for (rp = ainfo; rp != NULL; rp = rp->ai_next) {
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sock == -1) {
continue;
}
if (bind(sock, rp->ai_addr, rp->ai_addrlen) == 0) {
break;
}
perror("bind() failed");
close(sock);
}
freeaddrinfo(ainfo);
if (rp == NULL) {
return -1;
}
if (listen(sock, SOMAXCONN) != 0) {
return -1;
}
return sock;
}
struct pt_info {
int master_fd;
char slave_name[PTSNAME_SIZE];
};
static int epoll_add_fd(int epoll, int fd) {
struct epoll_event ev;
ev.data.fd = fd;
ev.events = EPOLLIN;
return epoll_ctl(epoll, EPOLL_CTL_ADD, fd, &ev);
}
// atsock - keeps track of the attached sockets
struct atsock {
int fd;
struct atsock *next;
};
struct atsock *atsock_new(int conn_fd) {
struct atsock *s = malloc(sizeof(struct atsock));
if (s != NULL) {
s->fd = conn_fd;
s->next = NULL;
}
return s;
}
struct atsock *atsock_save(struct atsock *head, struct atsock *new) {
assert(new != NULL);
if (head == NULL) {
return new;
}
struct atsock *cur = head;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = new;
return head;
}
struct atsock *atsock_erase(struct atsock *head, int conn_fd) {
assert(head != NULL);
if (head->fd == conn_fd) {
struct atsock *next = head->next;
free(head);
return next;
}
struct atsock *prev = head, *cur = head->next;
while (cur) {
if (cur->fd != conn_fd) {
prev = cur;
cur = cur->next;
continue;
}
prev->next = cur->next;
free(cur);
return head;
}
assert(0); // s not found in the list
return NULL;
}
static void run_master(struct pt_info *pti, const char *port) {
assert(pti != NULL);
int exit_code = EXIT_FAILURE;
int attach_sock = -1;
int epoll = epoll_create1(0);
if (epoll == -1) {
perror("epoll_create1() failed");
goto exit;
}
if (epoll_add_fd(epoll, pti->master_fd) != 0) {
perror("epoll_add_fd(master_fd) failed");
goto exit;
}
attach_sock = socket_listen(port);
if (attach_sock == -1) {
perror("socket_listen() failed");
goto exit;
}
if (epoll_add_fd(epoll, attach_sock) != 0) {
perror("epoll_add_fd(attach_sock) failed");
goto exit;
}
struct atsock *head = NULL;
struct epoll_event evlist[EPOLL_EVENTS];
while (1) {
int nready = epoll_wait(epoll, evlist, EPOLL_EVENTS, -1);
if (nready == -1 && errno == EINTR) {
continue;
}
if (nready == -1) {
perror("epoll_wait() failed");
break;
}
for (int i = 0; i < nready; i++) {
int fd = evlist[i].data.fd;
if (evlist[i].events & EPOLLIN) {
if (fd == pti->master_fd) {
// read from pty and forward data to each attached socket
char buf[1024];
int nread = read(fd, buf, 1023);
struct atsock *cur = head;
while (nread && cur) {
write_all(cur->fd, buf, nread);
cur = cur->next;
}
} else if (fd == attach_sock) {
int conn;
do {
conn = accept(fd, NULL, NULL);
} while (conn == -1 && errno == EINTR);
if (conn == -1) {
perror("accept() failed");
goto exit;
}
head = atsock_save(head, atsock_new(conn));
if (epoll_add_fd(epoll, conn) != 0) {
perror("epoll_add_fd(conn) failed");
goto exit;
}
printf("accepted new sock conn\n");
} else {
// read from attached socket and forward to pty
char buf[1024];
int nread = read(fd, buf, 1023);
if (nread == 0) {
head = atsock_erase(head, fd);
if (epoll_ctl(epoll, EPOLL_CTL_DEL, fd, NULL) != 0) {
perror("epoll_ctl(EPOLL_CTL_DEL) failed");
goto exit;
}
printf("disconnected sock\n");
} else {
if (write_all(pti->master_fd, buf, nread) != 0) {
perror("write_all(master_fd) failed");
goto exit;
}
}
}
} else if (evlist[i].events & (EPOLLHUP | EPOLLERR)) {
if (fd == attach_sock) {
attach_sock = -1;
if (epoll_ctl(epoll, EPOLL_CTL_DEL, fd, NULL) != 0) {
perror("epoll_ctl(EPOLL_CTL_DEL, attach_sock) failed");
goto exit;
}
printf("attach_sock failed\n");
} else if (fd == pti->master_fd) {
exit_code = EXIT_SUCCESS;
goto exit;
} else {
head = atsock_erase(head, fd);
if (epoll_ctl(epoll, EPOLL_CTL_DEL, fd, NULL) != 0) {
perror("epoll_ctl(EPOLL_CTL_DEL) failed");
goto exit;
}
printf("disconnected sock\n");
}
}
}
}
exit_code = EXIT_SUCCESS;
exit:
close(pti->master_fd);
while (head) {
close(head->fd);
head = atsock_erase(head, head->fd);
}
if (epoll != -1) {
close(epoll);
}
if (attach_sock != -1) {
close(attach_sock);
}
exit(exit_code);
}
static void run_slave(struct pt_info *pti, char *const command[]) {
assert(pti != NULL);
close(pti->master_fd);
setsid();
int fds = open(pti->slave_name, O_RDWR);
if (fds >= 0) {
dup2(fds, 0);
dup2(fds, 1);
dup2(fds, 2);
close(fds);
execv(command[0], command);
} else {
perror("open(pts_name) failed");
}
_exit(127);
}
static int create_pt(struct pt_info *p) {
errno = 0;
do {
if (p == NULL) {
errno = EINVAL;
break;
}
p->master_fd = posix_openpt(O_RDWR);
if (p->master_fd < 0) {
perror("posix_openpt() failed");
break;
}
if (grantpt(p->master_fd) != 0) {
perror("grantpt() failed");
break;
}
if (unlockpt(p->master_fd) != 0) {
perror("unlockpt() failed");
break;
}
if (ptsname_r(p->master_fd, p->slave_name, PTSNAME_SIZE) != 0) {
perror("ptsname_r() failed");
break;
}
} while(0);
if (errno && p && p->master_fd >= 0) {
close(p->master_fd);
}
return errno;
}
int main(int argc, char *argv[]) {
int pid = fork();
if (pid < 0) {
perror("fork() failed");
exit(EXIT_FAILURE);
} else if (pid) {
printf("Daemon PID is %d\n", pid);
exit(EXIT_SUCCESS);
}
int devnull = open("/dev/null", O_RDWR | O_CLOEXEC);
if (devnull < 0) {
perror("open('dev/null') failed");
exit(EXIT_FAILURE);
}
if (dup2(devnull, STDIN_FILENO) < 0) {
perror("dup2(devnul, STDIN) failed");
exit(EXIT_FAILURE);
}
if (dup2(devnull, STDOUT_FILENO) < 0) {
perror("dup2(devnul, STDOUT) failed");
exit(EXIT_FAILURE);
}
if (dup2(devnull, STDERR_FILENO) < 0) {
perror("dup2(devnul, STDERR) failed");
exit(EXIT_FAILURE);
}
setsid();
struct pt_info pti;
if (create_pt(&pti) != 0) {
exit(EXIT_FAILURE);
}
pid = fork();
if (pid < 0) {
perror("fork() failed");
exit(EXIT_FAILURE);
} else if (pid == 0) {
run_slave(&pti, argv + 2);
} else {
run_master(&pti, argv[1]);
}
return 0;
}