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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
|
//! The process runner of docs/next-tests.md: a real `pardes`, detached or
//! in a pty (test/e2e_harness.zig's Harness), and what a 9P client sees of
//! it over its socket, with T1's Session method names (test/session.zig), so
//! a test reads alike at either tier. The snap scripts, the process tests
//! (test/process/) and the window's run on it.
const std = @import("std");
const libc = std.c;
const eh = @import("e2e_harness.zig");
const Client = @import("9p_io").Client;
const c9 = @import("cloud9");
extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int;
extern "c" fn unsetenv(name: [*:0]const u8) c_int;
extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int;
extern "c" fn _exit(status: c_int) noreturn;
extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8;
extern "c" fn system(cmd: [*:0]const u8) c_int;
extern "c" fn realpath(path: [*:0]const u8, resolved: [*]u8) ?[*:0]u8;
/// One 9P2000 connection to a socket, blocking, as a client program holds
/// one: fids walked, opened, read, written, clunked and removed by path,
/// for the tests that need more than a file read or written whole (an open
/// held while others write, a directory's entries, a stat, a remove).
/// The last refusal's words are in `said`.
pub const Conn = struct {
fd: c_int,
cl: c9.Client,
in: [msize]u8 = undefined,
out: [msize]u8 = undefined,
next_fid: u32 = 1,
said_buf: [256]u8 = undefined,
said: []const u8 = "",
const msize = 64 * 1024;
pub const Fid = u32;
/// Connected to the unix socket at `path`, versioned and attached as
/// fid 0. Heap-held: its buffers are big and the client points into them.
pub fn dial(gpa: std.mem.Allocator, path: []const u8) !*Conn {
const c = try gpa.create(Conn);
errdefer gpa.destroy(c);
c.* = .{ .fd = -1, .cl = undefined };
c.cl = c9.Client.init(.{ .in = &c.in, .out = &c.out });
c.fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0);
if (c.fd < 0) return error.NoSocket;
errdefer _ = libc.close(c.fd);
var addr: libc.sockaddr.un = .{ .path = undefined };
if (path.len >= addr.path.len) return error.NameTooLong;
@memset(&addr.path, 0);
@memcpy(addr.path[0..path.len], path);
if (libc.connect(c.fd, @ptrCast(&addr), @sizeOf(libc.sockaddr.un)) != 0) return error.ConnectFailed;
_ = try c.call(.{ .version = .{ .msize = msize } });
_ = try c.call(.{ .attach = .{ .fid = 0, .uname = "test" } });
return c;
}
pub fn deinit(c: *Conn, gpa: std.mem.Allocator) void {
_ = libc.close(c.fd);
gpa.destroy(c);
}
fn call(c: *Conn, req: c9.Client.Request) !c9.Client.Result {
_ = c.cl.submit(req) catch return error.Protocol;
while (c.cl.output().len > 0) {
const out = c.cl.output();
const n = libc.write(c.fd, out.ptr, out.len);
if (n <= 0) return error.Hangup;
c.cl.wrote(@intCast(n));
}
while (true) {
if (c.cl.take()) |done| {
if (done.result == .fail) {
const words = done.result.fail;
const n = @min(words.len, c.said_buf.len);
@memcpy(c.said_buf[0..n], words[0..n]);
c.said = c.said_buf[0..n];
return error.Refused;
}
return done.result;
}
var buf: [msize]u8 = undefined;
const n = libc.read(c.fd, &buf, buf.len);
if (n <= 0) return error.Hangup;
var at: usize = 0;
while (at < n) at += c.cl.push(buf[at..@intCast(n)]);
}
}
/// A new fid walked to `path` from the root.
pub fn walk(c: *Conn, path: []const u8) !Fid {
var names: [c9.max_welem][]const u8 = undefined;
var n: usize = 0;
var it = std.mem.tokenizeScalar(u8, path, '/');
while (it.next()) |name| : (n += 1) {
if (n == names.len) return error.PathTooDeep;
names[n] = name;
}
const fid = c.next_fid;
c.next_fid += 1;
const r = try c.call(.{ .walk = .{ .fid = 0, .newfid = fid, .names = names[0..n] } });
if (r.walk.nwqid != n) return error.NotFound;
return fid;
}
/// `path` walked and opened (0 read, 1 write, 2 both; 16 truncates).
pub fn hold(c: *Conn, path: []const u8, mode: u8) !Fid {
const fid = try c.walk(path);
errdefer c.clunk(fid);
_ = try c.call(.{ .open = .{ .fid = fid, .mode = mode } });
return fid;
}
/// One read on an open fid; valid until the next call.
pub fn readFid(c: *Conn, fid: Fid, offset: u64, count: u32) ![]const u8 {
return (try c.call(.{ .read = .{ .fid = fid, .offset = offset, .count = @min(count, c.cl.maxRead()) } })).read;
}
/// One write on an open fid: the count the server took.
pub fn writeFid(c: *Conn, fid: Fid, offset: u64, data: []const u8) !u32 {
return (try c.call(.{ .write = .{ .fid = fid, .offset = offset, .data = data } })).write;
}
/// A fid let go; a refusal there (an Edit block left open) is in `said`.
pub fn clunk(c: *Conn, fid: Fid) void {
_ = c.call(.{ .clunk = .{ .fid = fid } }) catch {};
}
/// The whole file at `path`, read on an open of its own.
pub fn read(c: *Conn, gpa: std.mem.Allocator, path: []const u8) ![]u8 {
const fid = try c.hold(path, 0);
defer c.clunk(fid);
var out: std.ArrayList(u8) = .empty;
errdefer out.deinit(gpa);
while (true) {
const got = try c.readFid(fid, out.items.len, msize);
if (got.len == 0) break;
try out.appendSlice(gpa, got);
}
return out.toOwnedSlice(gpa);
}
/// `data` written to `path` in one write, the open clunked after; a
/// refusal by the write or by the clunk is `error.Refused`.
pub fn write(c: *Conn, path: []const u8, data: []const u8) !void {
const fid = try c.hold(path, 1);
_ = c.writeFid(fid, 0, data) catch |err| {
c.clunk(fid);
return err;
};
_ = c.call(.{ .clunk = .{ .fid = fid } }) catch |err| return err;
}
pub fn remove(c: *Conn, path: []const u8) !void {
const fid = try c.walk(path);
_ = try c.call(.{ .remove = .{ .fid = fid } });
}
/// What a stat of `path` answers; its strings valid until the next call.
pub fn stat(c: *Conn, path: []const u8) !c9.Stat {
const fid = try c.walk(path);
defer c.clunk(fid);
return (try c.call(.{ .stat = .{ .fid = fid } })).stat;
}
/// The names of a directory's entries.
pub fn list(c: *Conn, gpa: std.mem.Allocator, path: []const u8) ![]const []const u8 {
const fid = try c.hold(path, 0);
defer c.clunk(fid);
var names: std.ArrayList([]const u8) = .empty;
var off: u64 = 0;
while (true) {
const got = try c.readFid(fid, off, msize);
if (got.len == 0) break;
off += got.len;
var at: usize = 0;
while (at + 2 <= got.len) {
const len = @as(usize, std.mem.readInt(u16, got[at..][0..2], .little)) + 2;
const st = try c9.Stat.decode(got[at..][0..len]);
try names.append(gpa, try gpa.dupe(u8, st.name));
at += len;
}
}
return names.toOwnedSlice(gpa);
}
// fsb:
/// `dial`, versioned at `size` (a client of small frames, at least
/// 217, cloud9's least).
pub fn dialSized(gpa: std.mem.Allocator, path: []const u8, size: u32) !*Conn {
var addr: libc.sockaddr.un = .{ .path = undefined };
if (path.len >= addr.path.len) return error.NameTooLong;
@memset(&addr.path, 0);
@memcpy(addr.path[0..path.len], path);
return connectTo(gpa, libc.AF.UNIX, @ptrCast(&addr), @sizeOf(libc.sockaddr.un), size);
}
/// A connection over TCP to `host` (numeric, v4 or v6) at `port`.
pub fn dialTcp(gpa: std.mem.Allocator, host: [:0]const u8, port: u16) !*Conn {
if (std.mem.indexOfScalar(u8, host, ':') != null) {
var addr: libc.sockaddr.in6 = .{ .port = std.mem.nativeToBig(u16, port), .flowinfo = 0, .addr = undefined, .scope_id = 0 };
if (inet_pton(libc.AF.INET6, host, &addr.addr) != 1) return error.BadHost;
return connectTo(gpa, libc.AF.INET6, @ptrCast(&addr), @sizeOf(libc.sockaddr.in6), msize);
}
var addr: libc.sockaddr.in = .{ .port = std.mem.nativeToBig(u16, port), .addr = undefined };
if (inet_pton(libc.AF.INET, host, &addr.addr) != 1) return error.BadHost;
return connectTo(gpa, libc.AF.INET, @ptrCast(&addr), @sizeOf(libc.sockaddr.in), msize);
}
fn connectTo(gpa: std.mem.Allocator, family: c_uint, addr: *const libc.sockaddr, len: libc.socklen_t, size: u32) !*Conn {
const c = try gpa.create(Conn);
errdefer gpa.destroy(c);
c.* = .{ .fd = -1, .cl = undefined };
c.cl = c9.Client.init(.{ .in = &c.in, .out = &c.out });
c.fd = libc.socket(family, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0);
if (c.fd < 0) return error.NoSocket;
errdefer _ = libc.close(c.fd);
if (libc.connect(c.fd, addr, len) != 0) return error.ConnectFailed;
_ = try c.call(.{ .version = .{ .msize = size } });
_ = try c.call(.{ .attach = .{ .fid = 0, .uname = "test" } });
return c;
}
/// The frame size the server agreed to.
pub fn msizeAgreed(c: *const Conn) u32 {
return c.cl.msize;
}
/// `data` written to `path` truncated first (`>`), in as many writes
/// as it takes.
pub fn replace(c: *Conn, path: []const u8, data: []const u8) !void {
const fid = try c.hold(path, 1 | 16);
var off: usize = 0;
while (off < data.len) {
const n = c.writeFid(fid, off, data[off..@min(data.len, off + c.cl.maxWrite())]) catch |err| {
c.clunk(fid);
return err;
};
off += n;
}
_ = try c.call(.{ .clunk = .{ .fid = fid } });
}
/// A new pane, made by opening /pane/new; its serial.
pub fn newPane(c: *Conn, gpa: std.mem.Allocator) !u32 {
const got = try c.read(gpa, "/pane/new");
defer gpa.free(got);
return std.fmt.parseInt(u32, std.mem.trim(u8, got, "\n"), 10);
}
/// The serials /index lists, in its order.
pub fn serials(c: *Conn, gpa: std.mem.Allocator) ![]u32 {
const index = try c.read(gpa, "/index");
defer gpa.free(index);
var out: std.ArrayList(u32) = .empty;
var rows = std.mem.tokenizeScalar(u8, index, '\n');
while (rows.next()) |row| try out.append(gpa, try std.fmt.parseInt(u32, row[0 .. std.mem.indexOfScalar(u8, row, ' ') orelse row.len], 10));
return out.toOwnedSlice(gpa);
}
/// A Look at `path` from pane `source`, or from a scratch pane removed
/// after; the one pane it answers (test/fs.py's look).
pub fn look(c: *Conn, gpa: std.mem.Allocator, path: []const u8, source: ?u32) !u32 {
const from = source orelse try c.newPane(gpa);
var buf: [64]u8 = undefined;
const line = try std.fmt.allocPrint(gpa, "{s}\n", .{path});
defer gpa.free(line);
try c.write(try std.fmt.bufPrint(&buf, "/pane/{d}/look", .{from}), line);
const got = try c.read(gpa, "/look");
defer gpa.free(got);
const answer = std.mem.trim(u8, got, "\n");
const opened = std.fmt.parseInt(u32, answer, 10) catch {
std.debug.print("look {s}: answered {s}\n", .{ path, got });
return error.NotOneLook;
};
if (opened == from) return error.NotOneLook;
// A column holds only so many panes, each its tag and two rows.
if (source == null) try c.remove(try std.fmt.bufPrint(&buf, "/pane/{d}", .{from}));
return opened;
}
/// Pane `n`'s `file` read whole.
pub fn pane(c: *Conn, gpa: std.mem.Allocator, n: u32, file: []const u8) ![]u8 {
var buf: [64]u8 = undefined;
return c.read(gpa, try std.fmt.bufPrint(&buf, "/pane/{d}/{s}", .{ n, file }));
}
/// A line written to pane `n`'s exec, a middle click on it.
pub fn exec(c: *Conn, n: u32, line: []const u8) !void {
var buf: [64]u8 = undefined;
try c.write(try std.fmt.bufPrint(&buf, "/pane/{d}/exec", .{n}), line);
}
/// An exec that fails: refused, its words holding `words`.
pub fn execRefused(c: *Conn, n: u32, line: []const u8, words: []const u8) !void {
if (c.exec(n, line)) |_| {
std.debug.print("{s}: taken\n", .{line});
return error.Taken;
} else |err| if (err != error.Refused) return err;
try check(has(c.said, words), "{s}: refused {s}", .{ line, c.said });
}
pub fn removePane(c: *Conn, n: u32) !void {
var buf: [64]u8 = undefined;
try c.remove(try std.fmt.bufPrint(&buf, "/pane/{d}", .{n}));
}
};
/// A running pardes's file tree, dialled by its pid or socket path.
pub const Files = struct {
arena: std.heap.ArenaAllocator,
dial_buf: [4096]u8 = undefined,
dial_len: usize = 0,
pub fn init(gpa: std.mem.Allocator, to: []const u8) Files {
var f: Files = .{ .arena = .init(gpa) };
@memcpy(f.dial_buf[0..to.len], to);
f.dial_len = to.len;
return f;
}
pub fn deinit(f: *Files) void {
f.arena.deinit();
}
fn dial(f: *const Files) []const u8 {
return f.dial_buf[0..f.dial_len];
}
/// The whole file at `path`, kept until deinit.
pub fn read(f: *Files, path: []const u8) ![]const u8 {
const gpa = f.arena.child_allocator;
const bytes = try Client.read(gpa, f.dial(), path, path);
defer gpa.free(bytes);
return f.arena.allocator().dupe(u8, bytes);
}
pub fn write(f: *Files, path: []const u8, data: []const u8) !void {
try Client.write(f.arena.child_allocator, f.dial(), path, data);
}
pub fn expect(f: *Files, path: []const u8, want: []const u8) !void {
try std.testing.expectEqualStrings(want, try f.read(path));
}
/// Keys typed at the pane with the keyboard (`/kbdin`'s runes).
pub fn keys(f: *Files, runes: []const u8) !void {
try f.write("/kbdin", runes);
}
/// A click (1 left, 2 middle, 4 right) `into` cells into the first place
/// `text` is drawn.
pub fn click(f: *Files, text: []const u8, into: usize, buttons: u8) !void {
const col, const row = try f.where(text);
var line: [64]u8 = undefined;
const x = col + into;
try f.write("/mousein", try std.fmt.bufPrint(&line, "m {d} {d} {d}\nm {d} {d} 0\n", .{ x, row, buttons, x, row }));
}
/// The screen a frame draws, a line a row.
// shortcut: Session.screen's twin (test/session.zig, another module);
// share them when a third copy would be wanted.
pub fn screen(f: *Files) ![]const u8 {
const a = f.arena.allocator();
const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try f.read("/screen"), .{});
const cols: usize = @intCast(json.object.get("cols").?.integer);
var out: std.ArrayList(u8) = .empty;
for (json.object.get("cells").?.array.items, 0..) |cell, i| {
if (i > 0 and i % cols == 0) try out.append(a, '\n');
const g = cell.array.items[0].string;
try out.appendSlice(a, if (g.len == 0) " " else g);
}
return out.items;
}
/// Where `text` is first drawn, its column and row.
pub fn where(f: *Files, text: []const u8) !struct { usize, usize } {
var rows = std.mem.splitScalar(u8, try f.screen(), '\n');
var row: usize = 0;
while (rows.next()) |line| : (row += 1) {
if (std.mem.indexOf(u8, line, text)) |i| return .{ try std.unicode.utf8CountCodepoints(line[0..i]), row };
}
std.debug.print("not on the screen: {s}\n", .{text});
return error.NotOnScreen;
}
/// The serial of the pane with the keyboard.
pub fn focused(f: *Files) !u32 {
return std.fmt.parseInt(u32, std.mem.trim(u8, try f.read("/focus"), "\n"), 10);
}
/// A new pane, made by opening /pane/new; its serial.
pub fn newPane(f: *Files) !u32 {
return std.fmt.parseInt(u32, std.mem.trim(u8, try f.read("/pane/new"), "\n"), 10);
}
/// A path built from `args`, kept until deinit.
pub fn at(f: *Files, comptime fmt: []const u8, args: anytype) []const u8 {
return std.fmt.allocPrint(f.arena.allocator(), fmt, args) catch @panic("out of memory");
}
// fsb:
/// The ink /screen draws at a cell, as its JSON (`{"rgb":[1,2,3]}`).
pub fn fg(f: *Files, col: usize, row: usize) ![]const u8 {
const a = f.arena.allocator();
const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try f.read("/screen"), .{});
const cols: usize = @intCast(json.object.get("cols").?.integer);
const cell = json.object.get("cells").?.array.items[row * cols + col];
const style = json.object.get("styles").?.array.items[@intCast(cell.array.items[1].integer)];
return std.json.Stringify.valueAlloc(a, style.object.get("fg").?, .{});
}
};
/// The files of the app a pty harness started.
pub fn of(gpa: std.mem.Allocator, h: *const eh.Harness) Files {
var digits: [24]u8 = undefined;
return .init(gpa, std.fmt.bufPrint(&digits, "{d}", .{h.pid}) catch unreachable);
}
/// `pardes --detach=<name>` in a throwaway root that is its HOME, runtime
/// and config directory, as test/fs.py's session started one: no session
/// of the user's is reached. No daemon (PARDES_DAEMON=off, as the pty
/// harness starts one): the process is the session, and nothing outlives it.
pub const Detached = struct {
pid: libc.pid_t,
root_buf: [4096]u8 = undefined,
root_len: usize = 0,
socket_buf: [4096]u8 = undefined,
socket_len: usize = 0,
/// Whether deinit removes the root (one passed in is its owner's).
owns_root: bool = true,
/// The exit status, once it has exited (`exited`).
status: ?c_int = null,
files: Files,
pub const Options = struct {
/// Arguments before the file (`--mount=...`, `--9p=<name>`).
args: []const [:0]const u8 = &.{},
/// The file it opens; null: `<root>/<name>.txt`, holding "initial\n".
file: ?[]const u8 = null,
/// More environment, set after the root's.
env: []const [2][:0]const u8 = &.{},
/// Another session's root to run in, which both then share (its
/// store, its runtime directory); it is not removed.
root: ?[]const u8 = null,
/// The name its socket is linked as, when `--9p=<name>` names one.
socket: ?[]const u8 = null,
};
pub fn start(gpa: std.mem.Allocator, exe: [:0]const u8, name: []const u8, opts: Options) !Detached {
var d: Detached = .{ .pid = 0, .files = undefined };
if (opts.root) |shared| {
@memcpy(d.root_buf[0..shared.len], shared);
d.root_len = shared.len;
d.owns_root = false;
} else {
const template = "/tmp/pardes-process-XXXXXX";
@memcpy(d.root_buf[0..template.len], template);
d.root_buf[template.len] = 0;
_ = mkdtemp(d.root_buf[0..template.len :0]) orelse return error.NoTempDir;
d.root_len = template.len;
}
errdefer if (d.owns_root) d.removeRoot();
const dir = d.root();
var buf: [4096]u8 = undefined;
const fixture = if (opts.file) |f| try std.fmt.bufPrintSentinel(&buf, "{s}", .{f}, 0) else blk: {
const path = try std.fmt.bufPrintSentinel(&buf, "{s}/{s}.txt", .{ dir, name }, 0);
try eh.writeFile(path, "initial\n");
break :blk path;
};
// The session's socket, as its 9P service links it (src/9p_io.zig).
const sock_path = try std.fmt.bufPrint(&d.socket_buf, "{s}/9p/pardes/{s}", .{ dir, opts.socket orelse name });
d.socket_len = sock_path.len;
var arg_buf: [256]u8 = undefined;
const detach = try std.fmt.bufPrintSentinel(&arg_buf, "--detach={s}", .{name}, 0);
var env_buf: [3][4096]u8 = undefined;
const root_z = try std.fmt.bufPrintSentinel(&env_buf[0], "{s}", .{dir}, 0);
const config = try std.fmt.bufPrintSentinel(&env_buf[1], "{s}/config", .{dir}, 0);
const dump = try std.fmt.bufPrintSentinel(&env_buf[2], "{s}/{s}.dump.zon", .{ dir, name }, 0);
var err_buf: [4096]u8 = undefined;
const err_log = try std.fmt.bufPrintSentinel(&err_buf, "{s}/{s}.stderr", .{ dir, name }, 0);
// The build hands the binary over by a relative path, and the child
// runs in the root.
var exe_buf: [4096]u8 = undefined;
const exe_abs = realpath(exe, &exe_buf) orelse return error.NoBinary;
var argv: [24:null]?[*:0]const u8 = @splat(null);
if (opts.args.len + 3 > argv.len) return error.TooManyArgs;
argv[0] = exe_abs;
argv[1] = detach;
for (opts.args, 0..) |a, k| argv[2 + k] = a;
argv[2 + opts.args.len] = fixture;
const pid = libc.fork();
if (pid < 0) return error.ForkFailed;
if (pid == 0) {
// What a session of the caller's left in the environment, and a
// pager of theirs, are not the session under test's.
for ([_][*:0]const u8{ "PARDES_PID", "PARDES_9P", "PARDES_PANE", "PARDES_MOUNT", "PAGER" }) |v| _ = unsetenv(v);
for ([_][2][*:0]const u8{
.{ "HOME", root_z }, .{ "XDG_RUNTIME_DIR", root_z }, .{ "XDG_CONFIG_HOME", config },
.{ "PARDES_NOTIME", "1" }, .{ "PARDES_DUMP", dump }, .{ "PARDES_LSP_RS", "" },
.{ "PARDES_LSP_C", "" }, .{ "PARDES_LSP_GO", "" }, .{ "PARDES_LSP_TS", "" },
.{ "PARDES_LSP_PY", "" }, .{ "PARDES_DAEMON", "off" },
}) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126);
for (opts.env) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126);
if (libc.chdir(root_z) != 0) _exit(126);
const fd = libc.open(err_log, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644));
if (fd >= 0) _ = libc.dup2(fd, 2);
_ = execvp(exe_abs, &argv);
_exit(127);
}
d.pid = pid;
errdefer d.stop();
const deadline = eh.nowMs() + 10_000;
var sock_z: [4096]u8 = undefined;
const socket_z = try std.fmt.bufPrintSentinel(&sock_z, "{s}", .{sock_path}, 0);
while (libc.access(socket_z, 0) != 0) {
if (d.exited(0)) |status| {
var said: [4096]u8 = undefined;
const fd = libc.open(err_log, .{}, @as(libc.mode_t, 0));
const n = if (fd >= 0) libc.read(fd, &said, said.len) else 0;
if (fd >= 0) _ = libc.close(fd);
std.debug.print("the session exited, status {d}: {s}\n", .{ status, said[0..@intCast(@max(n, 0))] });
return error.SessionExited;
}
if (eh.nowMs() > deadline) return error.NoSocket;
sleepMs(5);
}
d.files = .init(gpa, sock_path);
return d;
}
pub fn root(d: *const Detached) []const u8 {
return d.root_buf[0..d.root_len];
}
/// The socket's path, which a held connection dials (`Conn.dial`).
pub fn socket(d: *const Detached) []const u8 {
return d.socket_buf[0..d.socket_len];
}
/// `data` written to `rel` under the root.
pub fn put(d: *const Detached, rel: []const u8, data: []const u8) !void {
var buf: [4096]u8 = undefined;
try eh.writeFile(try std.fmt.bufPrintSentinel(&buf, "{s}/{s}", .{ d.root(), rel }, 0), data);
}
/// The file `rel` under the root, whole.
pub fn get(d: *const Detached, gpa: std.mem.Allocator, rel: []const u8) ![]u8 {
var buf: [4096]u8 = undefined;
const path = try std.fmt.bufPrintSentinel(&buf, "{s}/{s}", .{ d.root(), rel }, 0);
const fd = libc.open(path, .{}, @as(libc.mode_t, 0));
if (fd < 0) return error.FileNotFound;
defer _ = libc.close(fd);
var out: std.ArrayList(u8) = .empty;
errdefer out.deinit(gpa);
var chunk: [16 * 1024]u8 = undefined;
while (true) {
const n = libc.read(fd, &chunk, chunk.len);
if (n < 0) return error.ReadFailed;
if (n == 0) break;
try out.appendSlice(gpa, chunk[0..@intCast(n)]);
}
return out.toOwnedSlice(gpa);
}
pub fn signal(d: *const Detached, sig: c_int) void {
if (d.pid > 0) _ = libc.kill(d.pid, @intCast(sig));
}
/// Its exit status once it has exited, waiting up to `ms`; null while
/// it runs. A status is the wait(2) word (WEXITSTATUS is `>> 8`).
pub fn exited(d: *Detached, ms: i64) ?c_int {
if (d.status) |s| return s;
if (d.pid <= 0) return null;
const deadline = eh.nowMs() + ms;
while (true) {
var status: c_int = 0;
if (libc.waitpid(d.pid, &status, std.c.W.NOHANG) == d.pid) {
d.status = status;
d.pid = 0;
return status;
}
if (eh.nowMs() >= deadline) return null;
sleepMs(10);
}
}
/// Ended (SIGTERM, then SIGKILL after 5 s), its root removed if it is
/// its own.
pub fn deinit(d: *Detached) void {
d.files.deinit();
d.stop();
if (d.owns_root) d.removeRoot();
}
fn removeRoot(d: *Detached) void {
var cmd: [4200]u8 = undefined;
if (std.fmt.bufPrintSentinel(&cmd, "rm -rf -- '{s}'", .{d.root()}, 0)) |c| {
_ = system(c);
} else |_| {}
}
fn stop(d: *Detached) void {
if (d.pid <= 0) return;
_ = libc.kill(d.pid, libc.SIG.TERM);
if (d.exited(5_000) == null) {
_ = libc.kill(d.pid, libc.SIG.KILL);
_ = d.exited(5_000);
}
d.pid = 0;
}
// fsb:
/// Whether `rel` under the root is there (a dangling link is not).
pub fn exists(d: *const Detached, rel: []const u8) bool {
var buf: [4096]u8 = undefined;
const path = std.fmt.bufPrintSentinel(&buf, "{s}/{s}", .{ d.root(), rel }, 0) catch return false;
return libc.access(path, 0) == 0;
}
/// `rel` under the root, absolute, kept until deinit.
pub fn abs(d: *Detached, rel: []const u8) [:0]const u8 {
return std.fmt.allocPrintSentinel(d.files.arena.allocator(), "{s}/{s}", .{ d.root(), rel }, 0) catch @panic("out of memory");
}
/// Another session in this root, in a pty, as fs.py's tty sessions:
/// `pardes --tty --9p=<name> <args> <root>/<name>.txt` (holding
/// "initial\n"), back once its socket (`abs("pardes-9p-<name>.sock")`)
/// is there.
pub fn startTty(d: *Detached, exe: [:0]const u8, name: []const u8, args: []const [:0]const u8) !Child {
const a = d.files.arena.allocator();
try d.put(try std.fmt.allocPrint(a, "{s}.txt", .{name}), "initial\n");
var argv: std.ArrayList([:0]const u8) = .empty;
try argv.appendSlice(a, &.{ exe, "--tty", try std.fmt.allocPrintSentinel(a, "--9p={s}", .{name}, 0) });
try argv.appendSlice(a, args);
try argv.append(a, d.abs(try std.fmt.allocPrint(a, "{s}.txt", .{name})));
const env = try a.dupe([2][:0]const u8, &.{.{ "PARDES_DUMP", d.abs(try std.fmt.allocPrint(a, "{s}.dump.zon", .{name})) }});
var c = try Child.spawn(argv.items, .{ .root = d.root(), .env = env, .pty = .{ .log = d.abs(try std.fmt.allocPrint(a, "{s}.pty", .{name})) } });
errdefer c.deinit();
try c.waitPath(d.abs(try std.fmt.allocPrint(a, "pardes-9p-{s}.sock", .{name})), 10_000);
return c;
}
};
fn sleepMs(ms: u32) void {
var ts: libc.timespec = .{ .sec = 0, .nsec = @as(isize, ms) * std.time.ns_per_ms };
_ = libc.nanosleep(&ts, null);
}
// fsb:
extern "c" fn readdir(dir: *libc.DIR) ?*libc.dirent;
extern "c" fn ioctl(fd: c_int, request: c_ulong, ...) c_int;
extern "c" fn openpty(master: *c_int, slave: *c_int, name: ?[*]u8, termp: ?*const anyopaque, winp: ?*const std.posix.winsize) c_int;
extern "c" fn signal(sig: c_int, handler: usize) usize;
extern "c" var environ: [*:null]?[*:0]u8;
extern "c" fn inet_pton(family: c_uint, src: [*:0]const u8, dst: *anyopaque) c_int;
extern "c" fn pipe2(fds: *[2]c_int, flags: c_int) c_int;
// shortcut: Linux's O_CLOEXEC, TIOCSCTTY and TCGETS, the only host these
// run on; take them from std for another.
const o_cloexec: c_int = 0o2000000;
/// A program run as a shell in `root` would run it: the caller's
/// environment without a session's (PARDES_*, a PAGER), HOME and the
/// runtime and config directories the root, no daemon (PARDES_DAEMON=off:
/// the process is the session) and no language servers, unless `env` says
/// otherwise. Its stderr is kept (`said`).
pub const Child = struct {
pid: libc.pid_t,
err_fd: c_int,
status: ?c_int = null,
/// A pty's ends, when on one, and the process copying its output to
/// `Pty.log`.
master: c_int = -1,
slave: c_int = -1,
drain: libc.pid_t = 0,
pub const Pty = struct {
/// Where everything the program draws goes.
log: [:0]const u8,
/// Made its controlling terminal; else a pty that is no one's.
ctty: bool = true,
};
pub const Options = struct {
/// HOME and the runtime and config directories; null: the caller's.
root: ?[]const u8 = null,
/// Where it runs; null: `root`, or here.
cwd: ?[]const u8 = null,
/// Set after the root's.
env: []const [2][:0]const u8 = &.{},
/// Unset after that.
unset: []const [:0]const u8 = &.{},
/// Its stdin, written whole and closed; null: /dev/null.
stdin: ?[]const u8 = null,
/// Started in a 24x80 pty, its stdin and stdout.
pty: ?Pty = null,
};
pub fn spawn(argv: []const [:0]const u8, opts: Options) !Child {
// A child gone before its stdin is written is EPIPE, not our end.
_ = signal(@intCast(@intFromEnum(libc.SIG.PIPE)), 1);
var args: [32:null]?[*:0]const u8 = @splat(null);
if (argv.len > args.len) return error.TooManyArgs;
for (argv, 0..) |a, i| args[i] = a;
var bufs: [4][4096]u8 = undefined;
// The build hands the binary over by a relative path, and the child
// runs elsewhere.
if (std.mem.indexOfScalar(u8, argv[0], '/') != null) args[0] = realpath(argv[0], &bufs[3]) orelse return error.NoBinary;
const root_z: ?[:0]const u8 = if (opts.root) |r| try std.fmt.bufPrintSentinel(&bufs[0], "{s}", .{r}, 0) else null;
const config = if (opts.root) |r| try std.fmt.bufPrintSentinel(&bufs[1], "{s}/config", .{r}, 0) else "";
const cwd: ?[:0]const u8 = if (opts.cwd) |c| try std.fmt.bufPrintSentinel(&bufs[2], "{s}", .{c}, 0) else root_z;
// Close-on-exec, so a later child holds none of these.
var err_pipe: [2]c_int = undefined;
if (pipe2(&err_pipe, o_cloexec) != 0) return error.NoPipe;
var in_pipe: [2]c_int = .{ -1, -1 };
if (opts.stdin != null and pipe2(&in_pipe, o_cloexec) != 0) return error.NoPipe;
var c: Child = .{ .pid = 0, .err_fd = err_pipe[0] };
if (opts.pty != null) {
var ws: std.posix.winsize = .{ .row = 24, .col = 80, .xpixel = 0, .ypixel = 0 };
if (openpty(&c.master, &c.slave, null, null, &ws) != 0) return error.NoPty;
for ([_]c_int{ c.master, c.slave }) |fd| _ = libc.fcntl(fd, libc.F.SETFD, @as(c_int, 1)); // FD_CLOEXEC
}
const pid = libc.fork();
if (pid < 0) return error.ForkFailed;
if (pid == 0) {
var i: usize = 0;
while (environ[i]) |kv| {
const entry = std.mem.span(kv);
const eq = std.mem.indexOfScalar(u8, entry, '=') orelse entry.len;
if (std.mem.startsWith(u8, entry, "PARDES_") or std.mem.indexOf(u8, entry[0..eq], "PAGER") != null) {
var name: [256]u8 = undefined;
const n = std.fmt.bufPrintSentinel(&name, "{s}", .{entry[0..@min(eq, 255)]}, 0) catch _exit(126);
_ = unsetenv(n);
i = 0;
} else i += 1;
}
if (root_z) |r| for ([_][2][*:0]const u8{
.{ "HOME", r }, .{ "XDG_RUNTIME_DIR", r }, .{ "XDG_CONFIG_HOME", config }, .{ "PARDES_NOTIME", "1" },
}) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126);
for ([_][2][*:0]const u8{
.{ "PARDES_DAEMON", "off" }, .{ "PARDES_LSP_RS", "" }, .{ "PARDES_LSP_C", "" }, .{ "PARDES_LSP_GO", "" },
.{ "PARDES_LSP_TS", "" }, .{ "PARDES_LSP_PY", "" },
}) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126);
if (opts.pty != null and setenv("TERM", "xterm-256color", 1) != 0) _exit(126);
for (opts.env) |kv| if (setenv(kv[0], kv[1], 1) != 0) _exit(126);
for (opts.unset) |name| _ = unsetenv(name);
if (cwd) |d| if (libc.chdir(d) != 0) _exit(126);
_ = libc.setsid();
if (opts.pty) |p| {
if (p.ctty) _ = ioctl(c.slave, 0x540E, @as(c_int, 0));
_ = libc.dup2(c.slave, 0);
_ = libc.dup2(c.slave, 1);
} else {
const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR }, @as(libc.mode_t, 0));
_ = libc.dup2(if (opts.stdin != null) in_pipe[0] else null_fd, 0);
_ = libc.dup2(null_fd, 1);
}
_ = libc.dup2(err_pipe[1], 2);
_ = execvp(args[0].?, &args);
_exit(127);
}
c.pid = pid;
_ = libc.close(err_pipe[1]);
if (opts.stdin) |bytes| {
_ = libc.close(in_pipe[0]);
var off: usize = 0;
while (off < bytes.len) {
const n = libc.write(in_pipe[1], bytes[off..].ptr, @min(bytes.len - off, 1 << 20));
if (n <= 0) break;
off += @intCast(n);
}
_ = libc.close(in_pipe[1]);
}
if (opts.pty) |p| {
const drain = libc.fork();
if (drain == 0) {
const fd = libc.open(p.log, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644));
var buf: [16 * 1024]u8 = undefined;
while (true) {
const n = libc.read(c.master, &buf, buf.len);
if (n <= 0) _exit(0);
_ = libc.write(fd, &buf, @intCast(n));
}
}
c.drain = drain;
}
return c;
}
/// Its wait(2) word once it has exited, waiting up to `ms`; null while it runs.
pub fn wait(c: *Child, ms: i64) ?c_int {
if (c.status) |s| return s;
const deadline = eh.nowMs() + ms;
while (true) {
var status: c_int = 0;
if (libc.waitpid(c.pid, &status, std.c.W.NOHANG) == c.pid) {
c.status = status;
return status;
}
if (eh.nowMs() >= deadline) return null;
sleepMs(1);
}
}
/// Everything it wrote to stderr, read to its end (its exit, or every
/// process holding it gone).
pub fn said(c: *Child, gpa: std.mem.Allocator) ![]u8 {
var out: std.ArrayList(u8) = .empty;
var buf: [4096]u8 = undefined;
while (true) {
const n = libc.read(c.err_fd, &buf, buf.len);
if (n <= 0) break;
try out.appendSlice(gpa, buf[0..@intCast(n)]);
}
return out.toOwnedSlice(gpa);
}
/// Waits up to `ms` for `path` (its socket) to be there; its stderr
/// said if it exits first.
pub fn waitPath(c: *Child, path: []const u8, ms: i64) !void {
var buf: [4096]u8 = undefined;
const z = try std.fmt.bufPrintSentinel(&buf, "{s}", .{path}, 0);
const deadline = eh.nowMs() + ms;
while (libc.access(z, 0) != 0) {
if (c.wait(0)) |status| {
const words = c.said(std.heap.page_allocator) catch "";
std.debug.print("exited, status {d}, before {s}: {s}\n", .{ status, path, words });
return error.Exited;
}
if (eh.nowMs() > deadline) return error.Timeout;
sleepMs(5);
}
}
pub fn kill(c: *const Child, sig: libc.SIG) void {
if (c.status == null) _ = libc.kill(c.pid, sig);
}
/// Ended (SIGTERM, SIGKILL after 5 s) and its fds closed.
pub fn deinit(c: *Child) void {
if (c.wait(0) == null) {
_ = libc.kill(c.pid, libc.SIG.TERM);
if (c.wait(5_000) == null) {
_ = libc.kill(c.pid, libc.SIG.KILL);
_ = c.wait(5_000);
}
}
if (c.drain > 0) {
_ = libc.kill(c.drain, libc.SIG.KILL);
var st: c_int = 0;
_ = libc.waitpid(c.drain, &st, 0);
}
for ([_]c_int{ c.err_fd, c.master, c.slave }) |fd| if (fd >= 0) {
_ = libc.close(fd);
};
}
};
/// What a run ended with: its exit code (a signal's is minus it, as
/// Python's returncode) and its stderr.
pub const Ran = struct { code: c_int, said: []u8 };
/// `argv` run to its end.
pub fn run(gpa: std.mem.Allocator, argv: []const [:0]const u8, opts: Child.Options) !Ran {
var c = try Child.spawn(argv, opts);
defer c.deinit();
const words = try c.said(gpa);
var status: c_int = 0;
_ = libc.waitpid(c.pid, &status, 0);
c.status = status;
return .{ .code = code(status), .said = words };
}
/// A wait(2) word as an exit code, a signal's as minus it.
pub fn code(status: c_int) c_int {
const sig = status & 0x7f;
return if (sig != 0) -sig else (status >> 8) & 0xff;
}
/// The names in directory `dir`, kept in `gpa`.
pub fn entries(gpa: std.mem.Allocator, dir: []const u8) ![]const []const u8 {
var buf: [4096]u8 = undefined;
const d = libc.opendir(try std.fmt.bufPrintSentinel(&buf, "{s}", .{dir}, 0)) orelse return error.NoDir;
defer _ = libc.closedir(d);
var names: std.ArrayList([]const u8) = .empty;
while (readdir(d)) |e| {
const name = std.mem.sliceTo(&e.name, 0);
if (std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) continue;
try names.append(gpa, try gpa.dupe(u8, name));
}
return names.toOwnedSlice(gpa);
}
/// Whether the line discipline of the terminal at `fd` is canonical.
pub fn canonical(fd: c_int) !bool {
var t: std.os.linux.termios = undefined;
if (ioctl(fd, 0x5401, &t) != 0) return error.NoTermios; // TCGETS
return t.lflag.ICANON;
}
/// Monotonic milliseconds.
pub const nowMs = eh.nowMs;
pub fn nap(ms: u32) void {
sleepMs(ms);
}
pub fn has(hay: []const u8, needle: []const u8) bool {
return std.mem.indexOf(u8, hay, needle) != null;
}
/// `ok`, or what is wrong said and the test failed.
pub fn check(ok: bool, comptime what: []const u8, args: anytype) !void {
if (ok) return;
std.debug.print(what ++ "\n", args);
return error.TestUnexpectedResult;
}
/// Whether `exe` was built with QUIC (-Dquic=true): refused for that, or
/// for --attach's own reason, which comes after.
pub fn quicIncluded(gpa: std.mem.Allocator, exe: [:0]const u8) !bool {
const r = try run(gpa, &.{ exe, "--attach=work", "--9p-quic=quic!127.0.0.1!0" }, .{});
defer gpa.free(r.said);
return !has(r.said, "QUIC is not included");
}
|