-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlx.c
More file actions
530 lines (473 loc) · 11.4 KB
/
lx.c
File metadata and controls
530 lines (473 loc) · 11.4 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <stdio.h>
#include <bio.h>
#include <regexp.h>
#include "procports.h"
#define FDOFF 20
#define CONNTIMEOUTMS 2000
void* emalloc(int n);
int edup(int a, int b);
int esnprint(char *tgt, int max, char *fmt, ...);
void _dbg(int iserror, char *format, ...);
void logsink(void *arg);
void usage(void);
void configlock(int take);
int notehandle(void*, char *note);
char* handlemsg(char *msg);
void lxsend(char *msg);
int getport(void);
void startfs(void);
void stopfs(void);
char* remote(void);
void vncviewer(int dpy);
void parseconf(char *path);
void config(int argc, char **argv);
struct {
int minport, maxport, fsport, mainfd, cmdlen,
checkcwd, debug, dbgfd, lckfd;
char *srvhost, *srvport, *cbhost, *defcmd, *defmounts,
*cwd, *mounts, *progname, *tmpdir;
char **cmd;
} g;
struct {
int pid; // only this thread handles notes
int count;
vlong lasttime;
} notectx;
// Writes to log file.
// Will be printed to stderr as well if it's an error msg or
// in debug mode.
void
_dbg(int iserror, char *format, ...)
{
va_list arg;
char buf[1000];
char *prefix = "client: ";
int prelen = strlen(prefix);
strcpy(buf, prefix);
va_start(arg, format);
vsnprint(buf + prelen, sizeof buf - prelen - 1, format, arg);
va_end(arg);
if(g.dbgfd != -1 && g.dbgfd != 2) fprint(g.dbgfd, buf);
if(g.debug || iserror) fprint(2, buf);
}
#define dbg(...) do{ _dbg(0, __VA_ARGS__); }while(0)
#define error(...) do{ _dbg(1, __VA_ARGS__); }while(0)
void*
emalloc(int n)
{
void *v = mallocz(n, 1);
if(v == nil) sysfatal("out of memory allocating %d", n);
return v;
}
int
edup(int a, int b)
{
int x = dup(a, b);
if(x < 0) sysfatal("dup: %r");
return x;
}
int
esnprint(char *tgt, int max, char *fmt, ...)
{
va_list arg;
int n;
va_start(arg, fmt);
n = vsnprint(tgt, max, fmt, arg);
if(n == max) sysfatal("formatted string too long: '%s'", fmt);
return n;
}
// Makes dir if doesn't exist, aborts on error
void
ensuredir(char *s, int mode)
{
int f;
if(access(s, AEXIST) == 0) return;
f = create(s, OREAD, DMDIR | mode);
if(f < 0) sysfatal("ensuredir: can't create %s: %r\n", s);
close(f);
}
void
logsink(void *arg)
{
int infd = *(int*)arg, outfd;
Ioproc *io = ioproc();
char outpath[1000];
esnprint(outpath, sizeof outpath, "%s/%s.%d.log",
g.tmpdir, g.srvhost, g.fsport);
outfd = create(outpath, OWRITE, 0600|DMAPPEND);
if(outfd < 0) sysfatal("cannot create %s: %r", outpath);
for(;;){
char buf[100];
int n = ioread(io, infd, buf, sizeof(buf)-1);
if (n < 0){
fprint(2, "logsink: read: %r\n");
break;
}
if(n == 0) break;
int w = iowrite(io, outfd, buf, n);
if(w != n)
fprint(2, "logsink: wrote %d instead of %d\n", w, n);
}
closeioproc(io);
}
// Sends message to server
void
lxsend(char *msg)
{
int n;
dbg("lxsend '%s'\n", msg);
if(g.mainfd < 0) return;
n = write(g.mainfd, msg, strlen(msg));
if(n != strlen(msg)) sysfatal("lxsend: unable to send '%s'", msg);
}
// Only notes against the main thread are handled
int
notehandle(void*, char *note)
{
long ns = 1000000000/3; // third of a second
vlong t;
if(getpid() != notectx.pid) return 1;
if(g.mainfd == -1 && strcmp(note, "alarm") == 0)
sysfatal("connection timed out");
if(strcmp(note, "interrupt") != 0) return 1;
dbg("notehandle: pid=%d note %s\n", getpid(), note);
t = nsec();
if(t - notectx.lasttime > ns){
notectx.count = 0;
notectx.lasttime = t;
}
notectx.count++;
switch(notectx.count){
case 1:
lxsend("int");
break;
case 2:
fprint(2, "sending SIGHUP\n");
lxsend("hup");
notectx.lasttime = t;
break;
case 3:
fprint(2, "sending SIGKILL\n");
lxsend("kill");
break;
}
return 1;
}
void
vncviewer(int dpy)
{
char vncaddr[50];
esnprint(vncaddr, sizeof(vncaddr)-1, "%s:%d", g.srvhost, dpy);
switch(fork()){
case -1:
sysfatal("vncviewer: fork: %r");
case 0:
dbg("vncviewer: vnc addr=%s\n", vncaddr);
edup(g.dbgfd, 1);
edup(g.dbgfd, 2);
execl("/bin/vncv", "vncv", vncaddr, NULL);
sysfatal("vncviewer: exec vncv %s: %r", vncaddr);
}
}
// Handles message from server
// Returns the exit message if the remote process exited, else nil
char*
handlemsg(char *msg)
{
char buf[200];
int dpy;
// dbg("handlemsg: received '%s'\n", msg);
if(strlen(msg) >= sizeof(buf)-1)
error("handlemsg: msg longer than %d\n", sizeof buf);
if(sscanf(msg, "vnc %d", &dpy) == 1)
vncviewer(dpy);
else if(sscanf(msg, "exit %s", buf) == 1)
return strdup((strcmp(msg, "exit 0") == 0) ? "" : msg);
else if(sscanf(msg, "fatal: %s", buf) == 1){
fprint(2, "%s: %s\n", g.progname, msg);
return strdup(msg);
}else
fprint(2, "%s: bug: malformed message: %s\n",
g.progname, msg);
return nil;
}
// Comms with server
// Returns the remote command exit message
char*
remote(void)
{
Ioproc *io = ioproc();
char *addr, *exitmsg = nil;
addr = netmkaddr(g.srvhost, nil, g.srvport);
g.mainfd = -1;
alarm(CONNTIMEOUTMS);
g.mainfd = dial(addr, nil, nil, nil);
if(g.mainfd < 0) sysfatal("remote: dial %s: %r", addr);
// push params and command
fprint(g.mainfd, "%q\n%d\n%d\n%q\n%d\n%q\n",
g.cbhost, g.fsport, g.debug, g.cwd, g.checkcwd, g.mounts);
for(int i = 0; i < g.cmdlen; i++)
fprint(g.mainfd, "%s\n", g.cmd[i]);
fprint(g.mainfd, "LXEND\n");
for(;;){
// dbg("remote: read loop\n");
char buf[200];
int n = ioread(io, g.mainfd, buf, sizeof(buf)-1);
if(n <= 0){
// Don't leave the loop if interrupted by a note,
char err[ERRMAX] = {0};
errstr(err, sizeof err);
dbg("remote: read err='%s'\n", err);
if(*err && strcmp(err, "interrupted")) break;
continue;
}
buf[n] = '\0';
if(n == sizeof(buf)) sysfatal("message too big: '%s'", buf);
dbg("remote: received '%s'\n", buf);
exitmsg = handlemsg(buf);
if(exitmsg != nil) break;
}
dbg("remote: post-read\n");
close(g.mainfd);
closeioproc(io);
return exitmsg;
}
// sysfatal if can't get port
int
getport(void)
{
int *busyports;
int port;
configlock(1);
busyports = portsbusy();
for(port = g.minport; port <= g.maxport+1; port++){
int busy = 0;
for(int *busyp = busyports; *busyp != -1; busyp++){
if(*busyp == port){
busy = 1;
break;
}
}
if(!busy) break;
}
free(busyports);
configlock(0);
if(port > g.maxport) sysfatal("no available port");
return port;
}
// Listen for exportfs connection
void
startfs(void)
{
char addr[32];
switch(fork()){
case -1:
sysfatal("startfs: fork: %r");
case 0:
esnprint(addr, sizeof(addr), "tcp!*!%d", g.fsport);
dbg("startfs: listen1 on %s\n", addr);
edup(g.dbgfd, 1);
edup(g.dbgfd, 2);
execl("/bin/aux/listen1", "aux/listen1", "-tv1", addr,
"/bin/exportfs", "-r", "/",
NULL);
sysfatal("startfs: exec: %r");
}
}
void
stopfs(void)
{
dbg("stopfs\n");
if(getpid() != notectx.pid) return;
if(g.fsport >= 0 && hanguplocalport(g.fsport) < 0)
sysfatal("cannot hangup exportfs conn: %r");
}
// Pass 1 to take, 0 to release
void
configlock(int take)
{
char path[40];
esnprint(path, sizeof(path), "%s/lock", g.tmpdir);
if(take){
assert(g.lckfd == -1);
// Try for 10s at least
for(int i = 0; i < 200; i++){
g.lckfd = create(path, OREAD, 0600|DMEXCL);
if(g.lckfd >= 0) break;
sleep(50);
}
if(g.lckfd < 0)
sysfatal("configlock: timed out waiting %s", path);
}else{
assert(g.lckfd != -1);
close(g.lckfd);
g.lckfd = -1;
}
}
// sysfatal on parsing error or missing config entry
void
parseconf(char *path)
{
char *line;
Reprog *rx = regcomp("^((([^=]+)(\\.))?([^.=]+))(=)(.*)$");
assert(rx);
// First pass for default values, second for host overrides
for(int pass = 1; pass <= 2; pass++){
Biobuf* bbuf = Bopen(path, OREAD);
if(bbuf == nil) sysfatal("config Bopen: %r");
while(line = Brdline(bbuf, '\n')){
Resub match[10];
int len;
char *host, *name, *val;
if(line[0] == '#') continue;
len = Blinelen(bbuf);
line[len-1] = '\0';
memset(match, 0, 10*sizeof(Resub));
if(regexec(rx, line, match, 8) != 1)
sysfatal("invalid line in %s: %s", path, line);
if(match[4].sp != nil) *match[4].sp = '\0';
*match[6].sp = '\0';
host = match[3].sp;
name = match[5].sp;
val = match[7].sp;
if(pass == 1 && host != nil)
continue;
if(pass == 2 &&
(host == nil || strcmp(host, g.srvhost) != 0))
continue;
if(strcmp(name, "default-host") == 0 && g.srvhost == nil)
g.srvhost = strdup(val);
else if(strcmp(name, "mounts") == 0)
g.defmounts = strdup(val);
else if(strcmp(name, "port") == 0)
g.srvport = strdup(val);
else if(strcmp(name, "minport") == 0)
g.minport = atoi(val);
else if(strcmp(name, "maxport") == 0)
g.maxport = atoi(val);
else if(strcmp(name, "command") == 0)
g.defcmd = strdup(val);
else if(strcmp(name, "callback-host") == 0)
g.cbhost = strdup(val);
}
Bterm(bbuf);
}
if(g.defmounts == nil) sysfatal("missing mounts config");
if(g.srvport == 0) sysfatal("missing port config");
if(g.minport == 0) sysfatal("missing minport config");
if(g.maxport == 0) sysfatal("missing maxport config");
if(g.defcmd == nil) sysfatal("missing command config");
// if(g.cbhost == nil) sysfatal("missing callback-host config");
free(rx);
}
// Parses command line and $home/lib/lx, populates global vars
void
config(int argc, char **argv)
{
char *home, *slash;
char path[100], cwd2[1000];
g.srvhost = nil;
ARGBEGIN {
default:
usage();
case 'd':
g.debug = 1;
break;
case 'D':
g.checkcwd = 1;
break;
case 'h':
g.srvhost = strdup(EARGF(usage()));
break;
case 'c':
g.cwd = strdup(EARGF(usage()));
break;
case 'm':
g.mounts = strdup(EARGF(usage()));
break;
} ARGEND
slash = strrchr(argv0, '/');
if(slash != nil)
g.progname = slash + 1;
else
g.progname = argv0;
if(g.cwd == nil){
if(getwd(cwd2, sizeof(cwd2)-1) == nil)
sysfatal("config getwd: %r");
g.cwd = strdup(cwd2);
}
home = getenv("home");
esnprint(path, sizeof(path), "%s/lib/%s", home, g.progname);
free(home);
parseconf(path);
if(g.cbhost == nil){
char buf[20];
int n, fd = open("/dev/sysname", OREAD);
if(fd < 0) sysfatal("cannot open /dev/sysname: %r");
n = read(fd, buf, sizeof buf - 1);
if(n < 0)
sysfatal("cannot read /dev/sysname: %r");
buf[n] = '\0';
g.cbhost = strdup(buf);
}
if(g.mounts == nil) g.mounts = g.defmounts;
if(argc > 0){
g.cmd = malloc(argc * sizeof(char*));
assert(g.cmd);
for(int i = 0; i < argc; i++)
g.cmd[i] = strdup(argv[i]);
g.cmdlen = argc;
}else{
// use default command
int maxargs = 100;
g.cmd = malloc((maxargs)*sizeof(char*));
assert(g.cmd);
g.cmdlen = tokenize(g.defcmd, g.cmd, maxargs);
if(g.cmdlen == maxargs)
sysfatal("more than %d args", maxargs-1);
}
assert(g.cmdlen);
}
void
usage(void)
{
fprint(2, "usage: %s [-Dd] [-c dir] [-h host] [-m mounts] cmd ...\n",
argv0);
threadexitsall("usage");
}
void
threadmain(int argc, char **argv)
{
char *exitmsg;
char tmp[100];
int in = FDOFF, out = in+1, err = in+2;
int logpipe[2];
if(rfork(RFFDG|RFNAMEG) < 0) sysfatal("threadmain: rfork: %r");
quotefmtinstall();
g.mainfd = -1;
g.lckfd = -1;
g.dbgfd = 2; // until log file is setup, log to stderr
assert(dup(0, in) == in);
assert(dup(1, out) == out);
assert(dup(2, err) == err);
config(argc, argv);
esnprint(tmp, sizeof tmp, "/root/tmp/%s.%s",
g.progname, getuser());
g.tmpdir = tmp;
ensuredir(g.tmpdir, 0700);
g.fsport = getport();
pipe(logpipe);
g.dbgfd = logpipe[0];
proccreate(logsink, &logpipe[1], 8*1024);
notectx.pid = getpid();
notectx.lasttime = 0;
startfs();
atexit(stopfs);
assert(atnotify(notehandle, 1) > 0);
exitmsg = remote();
threadexitsall(exitmsg);
}