Repository navigation
Expand file tree
/
Copy pathcommon.c
More file actions
362 lines (319 loc) · 7.13 KB
/
Copy pathcommon.c
File metadata and controls
362 lines (319 loc) · 7.13 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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <poll.h>
#include <errno.h>
#include "common.h"
void
panic(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(1);
}
int
hasinput(int fd)
{
fd_set fds;
struct timeval timeout;
if(fd < 0) return 0;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(fd, &fds);
return select(fd+1, &fds, NULL, NULL, &timeout) > 0;
}
int
readn(int fd, void *data, int n)
{
int m;
while(n > 0) {
m = read(fd, data, n);
if(m <= 0)
return -1;
data += m;
n -= m;
}
return 0;
}
/* bind to 127.0.0.1 instead of every interface. Off by default so remote
* panels and frontends keep working; DEBUG_PROTOCOL_SPEC §9 argues it should
* be the other way round on a machine that ships to hundreds of users. */
int netlocalonly;
int
socketlisten(int port)
{
int x;
struct sockaddr_in server;
int fd;
fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd < 0) {
perror("error: socket");
return -1;
}
x = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&x, sizeof x);
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(netlocalonly ? INADDR_LOOPBACK : INADDR_ANY);
server.sin_port = htons(port);
if(bind(fd, (struct sockaddr*)&server, sizeof(server)) < 0) {
close(fd);
perror("error: bind");
fprintf(stderr, "port %d\n", port);
return -1;
}
/* a backlog of 1 drops the second of two connections that arrive
* close together, which is exactly what a client that just crashed
* and is retrying looks like */
listen(fd, 8);
return fd;
}
/* Connect with a bounded wait, and hand back a blocking socket.
*
* A plain connect() to a host that drops SYNs takes the full TCP timeout,
* over two minutes. pdp1 dials from its command language, on the emulator
* thread, so a single `dpy <unreachable>' from any peer froze the machine
* and every port it serves for that long -- it looked like the listener
* had died. Everything anyone dials here is on the same machine or the
* same LAN, so a couple of seconds is already generous. */
enum { DIALMS = 2000 };
static int
connectto(int fd, const struct sockaddr *addr, socklen_t addrlen)
{
struct pollfd pfd;
int fl, err;
socklen_t errlen;
fl = fcntl(fd, F_GETFL, 0);
if(fl < 0 || fcntl(fd, F_SETFL, fl|O_NONBLOCK) < 0)
return -1;
if(connect(fd, addr, addrlen) < 0) {
if(errno != EINPROGRESS)
return -1;
pfd.fd = fd;
pfd.events = POLLOUT;
if(poll(&pfd, 1, DIALMS) != 1) {
errno = ETIMEDOUT;
return -1;
}
err = 0;
errlen = sizeof(err);
if(getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) < 0)
return -1;
if(err) {
errno = err;
return -1;
}
}
/* every caller expects a blocking fd back */
return fcntl(fd, F_SETFL, fl);
}
int
dial(const char *host, int port)
{
char portstr[32];
int sockfd;
struct addrinfo *result, *rp, hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
snprintf(portstr, 32, "%d", port);
if(getaddrinfo(host, portstr, &hints, &result)){
perror("error: getaddrinfo");
return -1;
}
for(rp = result; rp; rp = rp->ai_next){
sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(sockfd < 0)
continue;
if(connectto(sockfd, rp->ai_addr, rp->ai_addrlen) >= 0)
goto win;
close(sockfd);
}
freeaddrinfo(result);
perror("error");
return -1;
win:
freeaddrinfo(result);
return sockfd;
}
int
serve1(int port)
{
int sockfd, confd;
socklen_t len;
struct sockaddr_in client;
sockfd = socketlisten(port);
if(sockfd < 0)
return -1;
len = sizeof(client);
confd = accept(sockfd, (struct sockaddr*)&client, &len);
close(sockfd);
if(confd >= 0)
return confd;
perror("error: accept");
return -1;
}
// listen on multiple ports
void
serveN(struct PortHandler *ports, int nports, void *arg)
{
int i;
int confd;
struct pollfd pfds[100];
struct sockaddr_in client;
socklen_t len;
// create and listen on sockets for the given ports
for(i = 0; i < nports; i++) {
pfds[i].revents = 0;
pfds[i].events = 0;
pfds[i].fd = socketlisten(ports[i].port);
if(pfds[i].fd >= 0)
pfds[i].events = POLLIN;
}
// now poll to accept connections and hand off to handle function
for(;;) {
int ret = poll(pfds, nports, -1);
if(ret < 0)
break;
if(ret == 0)
continue;
for(i = 0; i < nports; i++) {
if(pfds[i].revents & POLLIN) {
len = sizeof(client);
confd = accept(pfds[i].fd, (struct sockaddr*)&client, &len);
if(confd >= 0)
ports[i].handle(confd, arg, ports[i].port);
} else if(pfds[i].revents) {
printf("weird stuff on port %d\n", ports[i].port);
}
}
}
for(i = 0; i < nports; i++)
close(pfds[i].fd);
}
void
nodelay(int fd)
{
int flag = 1;
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag));
}
void*
createseg(const char *name, size_t sz)
{
int fd;
void *p;
mode_t mask = umask(0);
fd = open(name, O_RDWR|O_CREAT, 0666);
umask(mask);
// if we try to open a /tmp file owned by another user
// with O_CREAT, the above will fail (even for root).
// so try again without O_CREAT
if(fd == -1)
fd = open(name, O_RDWR);
if(fd == -1) {
fprintf(stderr, "couldn't open file %s\n", name);
return nil;
}
ftruncate(fd, sz);
p = mmap(nil, sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if(p == MAP_FAILED) {
fprintf(stderr, "couldn't mmap file\n");
return nil;
}
return p;
}
void*
attachseg(const char *name, size_t sz)
{
int fd;
void *p;
fd = open(name, O_RDWR);
if(fd == -1)
return createseg(name, sz);
p = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if(p == MAP_FAILED)
return createseg(name, sz);
return p;
}
static struct timespec starttime;
void
inittime(void)
{
clock_gettime(CLOCK_MONOTONIC, &starttime);
}
u64
gettime(void)
{
struct timespec tm;
u64 t;
clock_gettime(CLOCK_MONOTONIC, &tm);
tm.tv_sec -= starttime.tv_sec;
t = tm.tv_nsec;
t += (u64)tm.tv_sec * 1000 * 1000 * 1000;
return t;
}
void
nsleep(u64 ns)
{
struct timespec tm;
tm.tv_sec = ns / (1000 * 1000 * 1000);
tm.tv_nsec = ns % (1000 * 1000 * 1000);
nanosleep(&tm, nil);
}
static int
isdelim(char c)
{
return c == '\0' || strchr(" \t\n;'\"", c) != nil;
}
/* have to free argv[0] and argv to clean up */
char**
split(char *line, int *pargc)
{
int argc, n;
char **argv, *lp, delim;
n = strlen(line)+1;
// just allocate enough
lp = (char*)malloc(2*n);
argv = (char**)malloc(sizeof(char*)*n);
argc = 0;
for(; *line; line++) {
while(isspace(*line)) line++;
if(*line == '\0')
break;
argv[argc++] = lp;
if(*line == '"' || *line == '\'') {
delim = *line++;
while(*line && *line != delim) {
if(*line == '\\')
line++;
*lp++ = *line++;
}
} else {
while(!isdelim(*line)) {
if(*line == '\\')
line++;
*lp++ = *line++;
}
}
*lp++ = '\0';
}
if(pargc) *pargc = argc;
argv[argc++] = nil;
return argv;
}