From 5157579ff7f9ae0cfc3c7629e953da05bce340fc Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 9 Nov 2026 00:52:52 -0300 Subject: tests: at the end of their files: every top-level test block of the 46 files that had one mid-file (the locality analysis's 45 and test/hxdiff.zig; gui.zig, macos.zig, look, host_io, 9p_io, terminal, tui, mouse, fs, replica, layout among them, 11,429 test lines), with the functions only tests call, moved after the file's code in their order, a mechanical move (.scratch/suites/tests_to_end.py), zig fmt'd; the unit suites pass as before (tui 1486/1487, gui 1536/1537, core 956/956 on both), macos.zig only parse-checked here Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/9p_io.zig | 3063 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 1531 insertions(+), 1532 deletions(-) (limited to 'src/9p_io.zig') diff --git a/src/9p_io.zig b/src/9p_io.zig index 62c38521..b557594a 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -103,7 +103,6 @@ const Runner = if (supported) cloud9.serve.Runner(pardes.ctlfs, ninep.editor, .{ .listeners = 2, }) else void; - pub const Listener = struct { io: std.Io, /// The core that answers; `reset` puts a replacement in. @@ -1078,182 +1077,6 @@ fn sweepRegistry(io: std.Io, svc: [:0]const u8, sockets: []const u8) void { const testing = std.testing; -test "the socket name is a third prefix in the shared directory" { - var buf: [sun_path_len]u8 = undefined; - const p = socketPath(&buf, "/run/user/1000", "t9srv").?; - try testing.expectEqualStrings("/run/user/1000/pardes-9p-t9srv.sock", p); - try testing.expect(!std.mem.startsWith(u8, std.Io.Dir.path.basename(p), "pardes-detached-")); -} - -test "a name that is not one path component is no address at all" { - var buf: [sun_path_len]u8 = undefined; - try testing.expect(socketPath(&buf, "/run", "") == null); - try testing.expect(socketPath(&buf, "/run", "a/b") == null); - try testing.expect(socketPath(&buf, "/run", "a\x00b") == null); -} - -test "the startup sweep removes a dead pid's socket and leaves a live pid's, a named one and a plain file" { - if (comptime !supported) return error.SkipZigTest; - var dir_buf: [sun_path_len:0]u8 = undefined; - const base = socketDir(&dir_buf) orelse return error.SkipZigTest; - if (!ensureSocketDir(base)) return error.SkipZigTest; - var sub_buf: [sun_path_len:0]u8 = undefined; - const sub = std.fmt.bufPrintSentinel(&sub_buf, "{s}/deadsweep-{d}", .{ base, @as(u32, @intCast(libc.getpid())) }, 0) catch return error.SkipZigTest; - if (libc.mkdir(sub, 0o700) != 0) return error.SkipZigTest; - var paths: [4][sun_path_len:0]u8 = undefined; - // No process has this pid: kill(pid, 0) answers ESRCH. - const dead = try std.fmt.bufPrintSentinel(&paths[0], "{s}/" ++ prefix ++ "2147483647.sock", .{sub}, 0); - const live = try std.fmt.bufPrintSentinel(&paths[1], "{s}/" ++ prefix ++ "{d}.sock", .{ sub, @as(u32, @intCast(std.c.getppid())) }, 0); - const named = try std.fmt.bufPrintSentinel(&paths[2], "{s}/" ++ prefix ++ "work.sock", .{sub}, 0); - const plain = try std.fmt.bufPrintSentinel(&paths[3], "{s}/" ++ prefix ++ "2147483646.sock", .{sub}, 0); - defer { - for ([_][:0]const u8{ dead, live, named, plain }) |p| _ = libc.unlink(p); - _ = libc.rmdir(sub); - } - for ([_][:0]const u8{ dead, live, named }) |p| { - const fd = bindSocket(p); - if (fd < 0) return error.SkipZigTest; - _ = libc.close(fd); // closed: what a killed editor leaves - } - const fd = libc.open(plain, .{ .CREAT = true, .ACCMODE = .WRONLY }, @as(libc.mode_t, 0o600)); - if (fd < 0) return error.SkipZigTest; - _ = libc.close(fd); - sweepDeadSockets(testing.io, sub); - try testing.expect(statNoFollow(dead) == null); - try testing.expect(statNoFollow(live) != null); - try testing.expect(statNoFollow(named) != null); - try testing.expect(statNoFollow(plain) != null); -} - -test "the registry sweep takes the dead entries and leaves everything else" { - if (comptime !supported) return error.SkipZigTest; - // Under the real runtime directory, because a sun_path is 108 bytes - // and the test cache's temporary directories are longer than that. - var dir_buf: [sun_path_len:0]u8 = undefined; - const base = socketDir(&dir_buf) orelse return error.SkipZigTest; - if (!ensureSocketDir(base)) return error.SkipZigTest; - - var svc_buf: [sun_path_len:0]u8 = undefined; - const svc = std.fmt.bufPrintSentinel(&svc_buf, "{s}/sweep-{d}", .{ base, @as(u32, @intCast(libc.getpid())) }, 0) catch - return error.SkipZigTest; - if (libc.mkdir(svc, 0o700) != 0) return error.SkipZigTest; - - // The sockets live where the real ones do -- beside the registry, not in - // it, and named the way a session names them -- because the sweep only - // follows an entry to a target of exactly that shape and place. - var paths: [6][sun_path_len:0]u8 = undefined; - const pid: u32 = @intCast(libc.getpid()); - const live_sock = try std.fmt.bufPrintSentinel(&paths[0], "{s}/pardes-9p-sweeplive-{d}.sock", .{ base, pid }, 0); - const dead_sock = try std.fmt.bufPrintSentinel(&paths[1], "{s}/pardes-9p-sweepdead-{d}.sock", .{ base, pid }, 0); - const live = try std.fmt.bufPrintSentinel(&paths[2], "{s}/live", .{svc}, 0); - const dead = try std.fmt.bufPrintSentinel(&paths[3], "{s}/dead", .{svc}, 0); - const stranger = try std.fmt.bufPrintSentinel(&paths[4], "{s}/stranger", .{svc}, 0); - // A dead entry pointing at something that is NOT one of our sockets: the - // entry goes, the file it named must not. - const outsider_sock = try std.fmt.bufPrintSentinel(&paths[5], "{s}/sweep-outsider-{d}.sock", .{ base, pid }, 0); - var outsider_buf: [sun_path_len:0]u8 = undefined; - const outsider = try std.fmt.bufPrintSentinel(&outsider_buf, "{s}/outsider", .{svc}, 0); - // Live, its lock held, but bound and not listening: a connect is - // refused, on every system, as FreeBSD refuses one to a live session - // whose queue is full. Its lock is what says it lives. - var starting_paths: [2][sun_path_len:0]u8 = undefined; - const starting_sock = try std.fmt.bufPrintSentinel(&starting_paths[0], "{s}/pardes-9p-sweepstarting-{d}.sock", .{ base, pid }, 0); - const starting = try std.fmt.bufPrintSentinel(&starting_paths[1], "{s}/starting", .{svc}, 0); - var locks: [3][lock_path_len:0]u8 = undefined; - const dead_lock = lockPath(&locks[2], dead_sock).?; - defer { - for ([_][:0]const u8{ live_sock, dead_sock, live, dead, stranger, outsider_sock, outsider, starting_sock, starting, dead_lock }) |p| _ = libc.unlink(p); - _ = libc.rmdir(svc); - } - - const live_lock = claim(live_sock, &locks[0]) orelse return error.LockFailed; - defer release(live_lock, std.mem.sliceTo(&locks[0], 0)); - const listening = bindSocket(live_sock); - try testing.expect(listening >= 0); - defer _ = libc.close(listening); - try testing.expectEqual(@as(c_int, 0), libc.listen(listening, 1)); - - const starting_lock = claim(starting_sock, &locks[1]) orelse return error.LockFailed; - defer release(starting_lock, std.mem.sliceTo(&locks[1], 0)); - const bound = bindSocket(starting_sock); - try testing.expect(bound >= 0); - defer _ = libc.close(bound); - try testing.expectEqual(@as(c_int, 0), libc.symlink(starting_sock, starting)); - - // Bound and then dropped, its lock let go: the files stay, and nobody - // answers it -- exactly what a killed session leaves behind. - const abandoned = bindSocket(dead_sock); - try testing.expect(abandoned >= 0); - _ = libc.close(abandoned); - const dropped = libc.open(dead_lock, .{ .ACCMODE = .RDWR, .CREAT = true, .CLOEXEC = true }, @as(libc.mode_t, 0o600)); - try testing.expect(dropped >= 0); - try testing.expectEqual(@as(c_int, 0), libc.flock(dropped, std.posix.LOCK.EX)); - _ = libc.close(dropped); - - try testing.expectEqual(@as(c_int, 0), libc.symlink(live_sock, live)); - try testing.expectEqual(@as(c_int, 0), libc.symlink(dead_sock, dead)); - // Dead too, but its target is not one of our sockets by name, so the - // entry must go and the file it named must survive untouched. - const outside = bindSocket(outsider_sock); - try testing.expect(outside >= 0); - defer _ = libc.close(outside); - try testing.expectEqual(@as(c_int, 0), libc.symlink(outsider_sock, outsider)); - // Not a symlink, so not this program's to reason about, even though - // connecting to it is refused exactly like the dead socket. - try std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = stranger, .data = "" }); - - try testing.expectEqual(Probe.live, probe(live)); - try testing.expectEqual(Probe.stale, probe(dead)); - - sweepRegistry(testing.io, svc, base); - - try testing.expect(statNoFollow(dead) == null); - try testing.expect(statNoFollow(dead_sock) == null); - try testing.expect(statNoFollow(dead_lock) == null); - try testing.expect(statNoFollow(starting) != null); - try testing.expect(statNoFollow(starting_sock) != null); - try testing.expect(statNoFollow(live) != null); - try testing.expect(statNoFollow(live_sock) != null); - try testing.expect(statNoFollow(stranger) != null); - try testing.expect(statNoFollow(outsider) == null); - try testing.expect(statNoFollow(outsider_sock) != null); - try testing.expectEqual(Probe.live, probe(live)); -} - -fn bindSocket(path: [:0]const u8) c_int { - const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM, 0); - if (fd < 0) return fd; - var addr: libc.sockaddr.un = .{ .path = @splat(0) }; - @memcpy(addr.path[0 .. path.len + 1], path[0 .. path.len + 1]); - if (libc.bind(fd, @ptrCast(&addr), @sizeOf(@TypeOf(addr))) != 0) { - _ = libc.close(fd); - return -1; - } - return fd; -} - -test "TCP addresses are numeric and normalize mapped IPv4" { - const loopback = try networkAddress("tcp!127.0.0.1!5640", false); - try testing.expectEqualDeep(loopback, try networkAddress("tcp!::ffff:127.0.0.1!5640", false)); - try testing.expectEqualDeep(loopback, try networkAddress("tcp!::ffff:7f00:1!5640", false)); - try testing.expectEqualDeep(try networkAddress("tcp!::1!5640", false), try networkAddress("tcp!0:0:0:0:0:0:0:1!5640", false)); - try testing.expectEqual(@as(u16, 0), (try networkAddress("tcp!127.0.0.1!0", true)).getPort()); - for ([_][]const u8{ "tcp!localhost!5640", "tcp!127.0.0.1!0", "tcp!127.0.0.1!-1", "tcp!127.0.0.1!65536", "tcp!127.0.0.1!", "tcp!!5640" }) |dial| - try testing.expectError(error.BadDial, networkAddress(dial, false)); - try Client.validateDial("tcp!127.0.0.1!5640"); - try Client.validateDial("/tmp/pardes-owned.sock"); - try Client.validateDial("unix!/tmp/pardes-owned.sock"); - try testing.expectError(error.BadDial, Client.validateDial("unix!work")); - try testing.expectError(error.BadDial, Client.validateDial("nowhere!x")); - try Client.validateDial("/tmp/odd!name.sock"); - try testing.expectError(error.BadDial, Client.validateDial("rel/dir.sock")); - try testing.expectError(error.BadDial, Client.validateDial("unix!")); - try testing.expectError(error.BadDial, Client.validateDial("unix!/tmp/a\x00b")); - try testing.expectError(error.BadDial, Client.validateDial("tcp!localhost!5640")); - try testing.expectError(error.BadDial, Client.validateDial("/tmp/a\x00b")); - try testing.expectError(error.BadDial, Client.validateDial("quic!127.0.0.1!5640")); -} - extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8; /// A test's own runtime directory under /tmp, which XDG_RUNTIME_DIR names @@ -1279,1512 +1102,1688 @@ const TempRuntime = struct { } }; -test "a runtime directory that makes the socket's path pass sun_path is refused in words, the editor running on" { - if (comptime !supported) return error.SkipZigTest; - const p = try pardes.Pardes.init(testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - var rt: TempRuntime = .{}; - try rt.init("words"); - defer rt.deinit(); - var buf: [256]u8 = undefined; - try testing.expect(noSocket("x", &buf) == null); - // A directory of 91 bytes (the ledger's was 83, a pid's digits from the - // edge): with a 1-byte name and a pid the socket's path passes sun_path. - var long_buf: [128:0]u8 = undefined; - const long = try std.fmt.bufPrintSentinel(&long_buf, "{s}/{s}", .{ std.mem.sliceTo(&rt.directory, 0), &@as([66]u8, @splat('d')) }, 0); - try testing.expectEqual(@as(usize, 91), long.len); - try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", long, 1)); - const words = noSocket("x", &buf) orelse return error.NotRefused; - try testing.expect(std.mem.startsWith(u8, words, "9p: the socket path is ")); - try testing.expect(std.mem.endsWith(u8, words, " bytes, 108 is the most: set XDG_RUNTIME_DIR shorter")); - // The session's own words, where its pane shows them; no listener. - try testing.expect(start(testing.io, testing.allocator, p) == null); - const pane = p.panes[0].?; - try testing.expect(std.mem.startsWith(u8, pane.msg[0..pane.msg_len], "9p: the socket path is ")); -} - -test "a runtime directory open to others is refused in words, never a silent session with no socket" { - if (comptime !supported) return error.SkipZigTest; - const p = try pardes.Pardes.init(testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - var rt: TempRuntime = .{}; - try rt.init("open"); - defer rt.deinit(); - // `mkdir -m 700 -p dir/sub` leaves dir itself at the umask's 755. - try testing.expectEqual(@as(c_int, 0), libc.chmod(&rt.directory, 0o755)); - var buf: [256]u8 = undefined; - const words = noSocket("x", &buf) orelse return error.NotRefused; - try testing.expect(std.mem.indexOf(u8, words, "must be a directory of yours alone (mode 755") != null); - try testing.expect(start(testing.io, testing.allocator, p) == null); - const pane = p.panes[0].?; - try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "chmod 700 it") != null); - try testing.expectEqual(@as(c_int, 0), libc.chmod(&rt.directory, 0o700)); - try testing.expect(noSocket("x", &buf) == null); -} +extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; -test "a socket path too long for sun_path is no listener, and leaves the editor's turn as it was" { - if (comptime !supported) return error.SkipZigTest; - const p = try pardes.Pardes.init(testing.allocator, .{ .tty_only = true }); - defer p.deinit(); - var rt: TempRuntime = .{}; - try rt.init("long"); - defer rt.deinit(); - // A name that fits: the directory is one a listener takes. - var fits: [sun_path_len]u8 = undefined; - try testing.expect(socketPath(&fits, std.mem.sliceTo(&rt.directory, 0), "short") != null); - const long = &@as([120]u8, @splat('n')); - try testing.expectError(error.NameTooLong, listen(testing.io, testing.allocator, p, long, "", null)); - try testing.expect(pardes.turn.answer_held == null); - try testing.expect(pardes.turn.wake_parked == null); - try testing.expect(pardes.turn.io == null); +pub fn start(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes) ?*Listener { + var name: [16]u8 = undefined; + // unreachable: a u32 is at most 10 digits + const fallback = std.fmt.bufPrint(&name, "{d}", .{@as(u32, @intCast(libc.getpid()))}) catch unreachable; + return listen(io, gpa, core, core.opts.ninep_name, fallback, core.opts.ninep_tcp) catch { + if (refusal.len > 0) core.setMessage(0, refusal) else core.reportError(0, "9p listener", error.ListenFailed); + return null; + }; } -test "a listening editor posts itself into the 9P registry and unposts on stop" { - if (comptime !supported) return error.SkipZigTest; - const gpa = testing.allocator; - var rt: TempRuntime = .{}; - try rt.init("post"); - defer rt.deinit(); - const dir = std.mem.sliceTo(&rt.directory, 0); - - var entry_buf: [sun_path_len:0]u8 = undefined; - const entry = try std.fmt.bufPrintSentinel(&entry_buf, "{s}/9p/pardes/unit", .{dir}, 0); - var sock_buf: [sun_path_len:0]u8 = undefined; - const sock = try std.fmt.bufPrintSentinel(&sock_buf, "{s}/" ++ prefix ++ "unit.sock", .{dir}, 0); - - var link: [sun_path_len]u8 = undefined; - { - const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); - defer p.deinit(); - const l = try listen(testing.io, gpa, p, "unit", "", null); - defer l.deinit(gpa); - // The socket stays exactly where pardes has always bound it: - // adopting the registry moves nothing, it only advertises. - try testing.expect(statNoFollow(sock) != null); - // It appeared already listening, renamed into place from a name of - // its own, which is gone; its lock held all the while. - try testing.expect(transport.isListening(sock)); - try testing.expectEqual(Probe.live, lockState(sock)); - var tmp_buf: [sun_path_len:0]u8 = undefined; - try testing.expect(statNoFollow(try std.fmt.bufPrintSentinel(&tmp_buf, "{s}.{d}", .{ sock, libc.getpid() }, 0)) == null); - // And the registry holds a symlink to it one directory down, so - // several editors group under /mnt/9p/pardes/ instead of - // crowding the registry root — the layout zmx posts its - // sessions in. - const n = libc.readlink(entry, &link, link.len); - try testing.expect(n > 0); - try testing.expectEqualStrings(sock, link[0..@intCast(n)]); - // A host path no fid names is swept, but never while a request is - // out in a syscall: it may be that request's, mid-walk. - try p.fs.os_paths.append(gpa, .{ .node = @import("fs.zig").os_node | 1, .path = try gpa.dupe(u8, "/tmp") }); - pardes.turn.out = 1; - l.collectOs(); - pardes.turn.out = 0; - try testing.expectEqual(@as(usize, 1), p.fs.os_paths.items.len); - l.collectOs(); - try testing.expectEqual(@as(usize, 0), p.fs.os_paths.items.len); +/// What a pane shell is told about the editor above it, set into this +/// process's environment just before `forkpty` so the child inherits it. +/// +/// Two independent facts, and they are separate variables because they answer +/// separate questions. `PARDES_PID` says "you are inside this editor, and it +/// will take a Look from you": a session whose 9P listener never came up still +/// owns its children, so the child says so rather than looking, from its own +/// side, exactly like no pardes at all. `PARDES_9P` and `PARDES_PANE` say how +/// to reach it, and a `--nested` session exports them too — its socket stays +/// open to scripts and to mounted shells — while withholding `PARDES_PID`, +/// which is the whole of what `--nested` means. +/// +/// The socket is this process's own (`listen` keeps it, its listener's +/// path, until `deinit`), for a pane's shell and for a command run off the +/// loop (an Edit's filter) alike. Told to children through +/// host_io.ChildEnv, never set into this process's own environment (see +/// ChildEnv.exportVar for why). +pub fn exportEnv(serial: u32, adopts: bool) void { + const socket: ?[]const u8 = if (own_socket_len > 0) own_socket[0..own_socket_len] else null; + const ChildEnv = @import("host_io.zig").ChildEnv; + var pid_buf: [16]u8 = undefined; + ChildEnv.exportVar("PARDES_PID", if (adopts) std.fmt.bufPrint(&pid_buf, "{d}", .{@as(u32, @intCast(libc.getpid()))}) catch unreachable else null); // a u32 fits 16 + if (socket) |own| { + var buf: [16]u8 = undefined; + ChildEnv.exportVar("PARDES_9P", own); + exportMount(own); + ChildEnv.exportVar("PARDES_PANE", std.fmt.bufPrint(&buf, "{d}", .{serial}) catch unreachable); + return; } - // Stopping takes the name back out, so the next editor of that name - // is not refused by its own corpse; the lock goes with the socket. - try testing.expect(libc.readlink(entry, &link, link.len) < 0); - try testing.expectEqual(Probe.none, lockState(sock)); + ChildEnv.exportVar("PARDES_9P", null); + ChildEnv.exportVar("PARDES_PANE", null); + ChildEnv.exportVar("PARDES_MOUNT", null); } -test "Unix and TCP share one listener through reads writes reconnects and reset" { - if (comptime !supported) return error.SkipZigTest; - const gpa = testing.allocator; - var rt: TempRuntime = .{}; - try rt.init("tcp"); - defer rt.deinit(); - const replacement = try gpa.alloc(u8, 3 * msize + 27); - defer gpa.free(replacement); - @memset(replacement, 'x'); - @memcpy(replacement[0.."changed café λ\n".len], "changed café λ\n"); - replacement[replacement.len - 1] = '\n'; +/// $PARDES_MOUNT, the session's own directory, when the environment says +/// where it is -- never by looking at the mount, which this editor serves +/// (the self-mount deadlock). Under `9ns --mntgen` (NINE_MNTGEN=1) it is +/// `$NINE_MOUNT/pardes/`, the name the registry gives the socket. +/// Any other $NINE_MOUNT is a mount of another server, made before this +/// session had a socket: unknown, so unset, and a script falls back to +/// `9p -a "unix!$PARDES_9P"`. +fn exportMount(sock_path: []const u8) void { + const mntgen = if (libc.getenv("NINE_MNTGEN")) |v| std.mem.eql(u8, std.mem.span(v), "1") else false; + const root = if (libc.getenv("NINE_MOUNT")) |v| std.mem.span(v) else ""; + const base = std.Io.Dir.path.basename(sock_path); + const sock_prefix = "pardes-9p-"; + const suffix = ".sock"; + if (mntgen and root.len > 0 and std.mem.startsWith(u8, base, sock_prefix) and std.mem.endsWith(u8, base, suffix) and base.len > sock_prefix.len + suffix.len) { + var buf: [4096]u8 = undefined; + if (std.fmt.bufPrint(&buf, "{s}/pardes/{s}", .{ std.mem.trimEnd(u8, root, "/"), base[sock_prefix.len .. base.len - suffix.len] })) |mount| { + return @import("host_io.zig").ChildEnv.exportVar("PARDES_MOUNT", mount); + } else |_| {} + } + @import("host_io.zig").ChildEnv.exportVar("PARDES_MOUNT", null); +} - const Worker = struct { - dial: []const u8, - body_path: []const u8, - expected: []const u8, - replacement: []const u8, - done: std.atomic.Value(bool) = .init(false), - failure: ?anyerror = null, +pub const Client = struct { + pub const budget_ms: i64 = 2000; + /// Each write of a long one has this long to be answered (transact). + pub const write_budget_ms: i64 = 30_000; - fn run(w: *@This()) void { - defer w.done.store(true, .release); - w.check() catch |err| { - w.failure = err; - }; - } + pub const max_depth: usize = 2 * ninep.max_welem; - fn check(w: *@This()) !void { - const before = try Client.readLimit(testing.allocator, w.dial, w.body_path, w.body_path, w.expected.len); - defer testing.allocator.free(before); - try testing.expectEqualStrings(w.expected, before); - try testing.expectError(error.FileTooLarge, Client.readLimit(testing.allocator, w.dial, w.body_path, w.body_path, w.expected.len - 1)); - const listing = try Client.readLimit(testing.allocator, w.dial, "/pane", "/pane", 128); - defer testing.allocator.free(listing); - const exact_listing = try Client.readLimit(testing.allocator, w.dial, "/pane", "/pane", listing.len); - defer testing.allocator.free(exact_listing); - try testing.expectEqualStrings(listing, exact_listing); - try testing.expectError(error.FileTooLarge, Client.readLimit(testing.allocator, w.dial, "/pane", "/pane", listing.len - 1)); - try Client.write(testing.allocator, w.dial, w.body_path, w.replacement); - const after = try Client.read(testing.allocator, w.dial, w.body_path, w.body_path); - defer testing.allocator.free(after); - try testing.expectEqualStrings(w.replacement, after); - const screen = try Client.read(testing.allocator, w.dial, "/screen", "/screen"); - defer testing.allocator.free(screen); - const parsed = try std.json.parseFromSlice(struct { cols: u16, rows: u16 }, testing.allocator, screen, .{ .ignore_unknown_fields = true }); - defer parsed.deinit(); - try testing.expectEqual(@as(u16, 40), parsed.value.cols); - try testing.expectEqual(@as(u16, 12), parsed.value.rows); - } + const uname = "pardes"; + + const Dial = union(enum) { unix: [:0]const u8, tcp: std.Io.net.IpAddress }; + + pub const Error = error{ + PathTooDeep, + BadDial, + Dial, + Hangup, + Timeout, + Botch, + Remote, + IsDirectory, + NotFound, + FileTooLarge, }; - for ([_][]const u8{ "127.0.0.1", "::1" }) |host| { - const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("initial\n"); - while (p.nextEffect()) |_| {} - var bind_buf: [64]u8 = undefined; - const bind = try std.fmt.bufPrint(&bind_buf, "tcp!{s}!0", .{host}); - const l = try listen(testing.io, gpa, p, "roundtrip", "", bind); - defer { - l.reset(p); - l.deinit(gpa); - } - try testing.expect(l.path().len != 0); - try testing.expectEqual(@as(usize, max_conns), l.runner.conns.len); - const port = l.tcp_address.?.getPort(); - try testing.expect(port != 0); - var dial_buf: [64]u8 = undefined; - const network_dial = try std.fmt.bufPrint(&dial_buf, "tcp!{s}!{d}", .{ host, port }); - var body_buf: [64]u8 = undefined; - const body = try std.fmt.bufPrint(&body_buf, "/pane/{d}/body", .{pane.serial}); - for ([_][]const u8{ network_dial, l.path(), network_dial }, 0..) |dial, attempt| { - var worker: Worker = .{ .dial = dial, .body_path = body, .expected = if (attempt == 0) "initial\n" else replacement, .replacement = replacement }; - const thread = try std.Thread.spawn(.{}, Worker.run, .{&worker}); - defer thread.join(); - // The editor rests while the client works: its requests are - // answered on the runner's tasks, not by this thread. - const deadline = nowMs() + 3 * Client.budget_ms; - pardes.turn.rest(); - while (!worker.done.load(.acquire) and nowMs() < deadline) nap(1); - // The client has hung up, but its connection's task may not have - // seen that yet. The releases it then owes are refused once - // `reset` starts cutting (they name the old editor's opens), and - // this test's "replacement" is the same editor, so they would - // stay in `p.fs.opens`. Let the hangup pay them first, resting - // so the task can take the turn: that is what is under test here. - const settle_by = nowMs() + 3 * Client.budget_ms; - while (l.runner.count() != 0 and nowMs() < settle_by) nap(1); - pardes.turn.wake(); - try testing.expect(worker.done.load(.acquire)); - if (worker.failure) |err| return err; - try testing.expectEqual(@as(usize, 0), l.runner.count()); - l.reset(p); - try testing.expectEqual(@as(usize, 0), l.runner.count()); - for (p.fs.opens) |o| try testing.expect(o.node == 0); - } + pub fn read(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8) ![]u8 { + return readLimit(gpa, dial, path, display_path, limits.max_file_bytes); } -} -test "a change waits while the editor is out mid-step, a read does not, and the change lands when the editor rests" { - if (comptime !supported) return error.SkipZigTest; - const gpa = testing.allocator; - var rt: TempRuntime = .{}; - try rt.init("park"); - defer rt.deinit(); + pub fn readLimit(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8, max_bytes: usize) ![]u8 { + return fetch(false, gpa, dial, path, display_path, null, @min(max_bytes, limits.max_file_bytes)); + } - const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("before\n"); - while (p.nextEffect()) |_| {} - // This thread is the editor's: it has the turn from here on. - const l = try listen(testing.io, gpa, p, "park", "", null); - defer { - l.reset(p); - l.deinit(gpa); + /// readLimit through a mount: its connection kept for the next (`kept`). + pub fn readMount(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8, max_bytes: usize) ![]u8 { + return fetch(true, gpa, dial, path, display_path, null, @min(max_bytes, limits.max_file_bytes)); } - var body_buf: [64]u8 = undefined; - const body = try std.fmt.bufPrint(&body_buf, "/pane/{d}/body", .{pane.serial}); - const Writer = struct { - dial: []const u8, - path: []const u8, - done: std.atomic.Value(bool) = .init(false), - failure: ?anyerror = null, - fn run(w: *@This()) void { - defer w.done.store(true, .release); - Client.write(testing.allocator, w.dial, w.path, "after\n") catch |err| { - w.failure = err; - }; - } - }; - var writer: Writer = .{ .dial = l.path(), .path = body }; + /// One operation on `path`, a read of up to `read_limit` or a write of + /// `write_bytes`, on the connection kept for `dial` when `keep`: the + /// peer's words, when it refuses, kept for lastSaid. + fn fetch(keep: bool, gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8, write_bytes: ?[]const u8, read_limit: usize) ![]u8 { + if (comptime !supported) return error.Unsupported; + var names: [max_depth][]const u8 = undefined; + const n = try elements(path, &names); + var remote: RemoteError = .{}; + return fetchBytes(gpa, dial, names[0..n], &remote, write_bytes, display_path, read_limit, keep) catch |err| { + if (err == error.Remote) said = remote; + return err; + }; + } - // The editor goes out mid-step -- a Look resolving a path, say -- and a - // write arrives meanwhile. It must not land: the step still holds - // pointers into the pane. A read is answered all the same. - pardes.turn.yield(); - const thread = try std.Thread.spawn(.{}, Writer.run, .{&writer}); - defer thread.join(); - nap(150); - try testing.expect(!writer.done.load(.acquire)); - const seen = try Client.readLimit(gpa, l.path(), body, body, 64); - defer gpa.free(seen); - try testing.expectEqualStrings("before\n", seen); - try testing.expect(!writer.done.load(.acquire)); - pardes.turn.back(); - try testing.expectEqualStrings("before\n", pane.file.?.content); + /// The peer's own words for the last error.Remote a read or write here + /// got (its Rerror), for a caller that says why: this thread's, as the + /// editor's mounts and a Handover job each ask from their own. + threadlocal var said: RemoteError = .{}; + pub fn lastSaid() []const u8 { + return said.buf[0..said.len]; + } - // Resting is what lets it in: the parked write is retried, served, and - // the client hears back. - pardes.turn.rest(); - const deadline = nowMs() + Client.budget_ms; - while (!writer.done.load(.acquire) and nowMs() < deadline) nap(1); - pardes.turn.wake(); - try testing.expect(writer.done.load(.acquire)); - if (writer.failure) |err| return err; - // Its bytes are held until its open is let go (pane.zig `batchFor`), and - // the client lets it go without waiting: rested again until that lands, - // the body read only while the editor has the turn. - while (!std.mem.eql(u8, pane.file.?.content, "after\n") and nowMs() < deadline) { - pardes.turn.rest(); - nap(1); - pardes.turn.wake(); + /// Whether a peer answers at `dial`: a version and attach, and its root + /// read. Any answer from the peer, an error of its own included, is one. + pub fn probe(gpa: std.mem.Allocator, dial: []const u8) !void { + return probeWith(false, gpa, dial); } - try testing.expectEqualStrings("after\n", pane.file.?.content); -} -test "a mount's connection is kept between operations, and one the peer hung up is dialled again" { - if (comptime !supported) return error.SkipZigTest; - const gpa = testing.allocator; - var rt: TempRuntime = .{}; - try rt.init("kept"); - defer rt.deinit(); + /// probe for a Mount: the connection kept as the mount's. + pub fn probeMount(gpa: std.mem.Allocator, dial: []const u8) !void { + return probeWith(true, gpa, dial); + } - const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); - defer p.deinit(); - const pane = try p.setTestFile("before\n"); - while (p.nextEffect()) |_| {} - const l = try listen(testing.io, gpa, p, "kept", "", null); - defer { - l.reset(p); - l.deinit(gpa); + fn probeWith(keep: bool, gpa: std.mem.Allocator, dial: []const u8) !void { + const got = fetch(keep, gpa, dial, "", "", null, 0) catch |err| switch (err) { + error.IsDirectory, error.NotFound, error.Remote, error.FileTooLarge => return, + else => return err, + }; + gpa.free(got); } - var body_buf: [64]u8 = undefined; - const body = try std.fmt.bufPrint(&body_buf, "/pane/{d}/body", .{pane.serial}); - const long = try gpa.alloc(u8, 2 * msize + 5); - defer gpa.free(long); - @memset(long, 'k'); - long[long.len - 1] = '\n'; - const Worker = struct { - l: *Listener, - body: []const u8, - long: []const u8, - done: std.atomic.Value(bool) = .init(false), - failure: ?anyerror = null, - fn run(w: *@This()) void { - defer w.done.store(true, .release); - w.check() catch |err| { - w.failure = err; - }; - } - fn read(w: *@This(), want: []const u8) !void { - const got = try Client.readMount(testing.allocator, w.l.path(), w.body, w.body, limits.max_file_bytes); - defer testing.allocator.free(got); - try testing.expectEqualStrings(want, got); + pub fn write(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, bytes: []const u8) !void { + gpa.free(try fetch(false, gpa, dial, path, path, bytes, limits.max_file_bytes)); + } + + /// write through a mount: its connection kept for the next (`kept`). + pub fn writeMount(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, bytes: []const u8) !void { + gpa.free(try fetch(true, gpa, dial, path, path, bytes, limits.max_file_bytes)); + } + + const RemoteError = struct { + buf: [ninep.errmax]u8 = undefined, + len: usize = 0, + + fn set(r: *RemoteError, msg: []const u8) error{Remote} { + r.keep(msg); + return error.Remote; } - fn check(w: *@This()) !void { - defer Client.forget(w.l.path()); - try w.read("before\n"); - try w.read("before\n"); - // A write longer than a message, and a miss, on the same one. - try Client.writeMount(testing.allocator, w.l.path(), w.body, w.long); - try w.read(w.long); - try testing.expectError(error.NotFound, Client.readMount(testing.allocator, w.l.path(), "/pane/no-such", "/pane/no-such", 64)); - try testing.expectEqual(@as(u64, 1), w.l.accepts.load(.acquire)); - // The peer hangs up (a Restore cuts its clients): the next - // operation dials again and is answered. - for (&w.l.runner.conns) |*conn| if (conn.live()) conn.close(); - const by = nowMs() + Client.budget_ms; - while (w.l.runner.count() != 0 and nowMs() < by) nap(1); - try w.read(w.long); - try testing.expectEqual(@as(u64, 2), w.l.accepts.load(.acquire)); + + fn keep(r: *RemoteError, msg: []const u8) void { + r.len = @min(msg.len, r.buf.len); + @memcpy(r.buf[0..r.len], msg[0..r.len]); } }; - var worker: Worker = .{ .l = l, .body = body, .long = long }; - pardes.turn.rest(); - const thread = try std.Thread.spawn(.{}, Worker.run, .{&worker}); - const deadline = nowMs() + 3 * Client.budget_ms; - while (!worker.done.load(.acquire) and nowMs() < deadline) nap(1); - thread.join(); - // Forgotten: the kept connection is closed, and its task sees it. - const settle_by = nowMs() + Client.budget_ms; - while (l.runner.count() != 0 and nowMs() < settle_by) nap(1); - pardes.turn.wake(); - if (worker.failure) |err| return err; - try testing.expectEqual(@as(usize, 0), l.runner.count()); -} - -test "a connection holds up to 128 reads, and the next is refused in words" { - try testing.expectEqual(@as(usize, 128), ninep.max_held); - if (comptime supported) try testing.expectEqualStrings("too many reads waiting: 128", Runner.Engine.e_waiting); -} -test "a held read is answered when the log has news, and a flushed one spends nothing" { - if (comptime !supported) return error.SkipZigTest; - const gpa = testing.allocator; - var rt: TempRuntime = .{}; - try rt.init("held"); - defer rt.deinit(); + fn elements(path: []const u8, out: *[max_depth][]const u8) Error!usize { + var n: usize = 0; + var it = std.mem.tokenizeScalar(u8, path, '/'); + while (it.next()) |name| { + if (n == out.len) return Error.PathTooDeep; + out[n] = name; + n += 1; + } + return n; + } - const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); - defer p.deinit(); - _ = try p.setTestFile("held\n"); - p.update(.tick); // the pane is logged, before the log is opened - while (p.nextEffect()) |_| {} - const l = try listen(testing.io, gpa, p, "held", "", null); - defer { - l.reset(p); - l.deinit(gpa); + fn resolve(buf: *[sun_path_len]u8, dial: []const u8) error{BadDial}!Dial { + if (dial.len == 0) return error.BadDial; + if (std.mem.startsWith(u8, dial, "tcp!")) return .{ .tcp = try networkAddress(dial, false) }; + const explicit_unix = std.mem.startsWith(u8, dial, "unix!"); + // A network this does not dial (`nowhere!x`) is a malformed address, + // not a session named so. + if (!explicit_unix and std.mem.indexOfScalar(u8, dial, '/') == null and std.mem.indexOfScalar(u8, dial, '!') != null) return error.BadDial; + const path = if (explicit_unix) dial[5..] else dial; + if (explicit_unix and !std.mem.startsWith(u8, path, "/")) return error.BadDial; + if (std.mem.indexOfScalar(u8, path, '/') != null) { + // A socket's path is absolute: a relative one would mean wherever + // the editor happens to run. + if (path[0] != '/') return error.BadDial; + if (std.mem.indexOfScalar(u8, path, 0) != null) return error.BadDial; + return .{ .unix = std.fmt.bufPrintSentinel(buf, "{s}", .{path}, 0) catch return error.BadDial }; + } + var dir_buf: [sun_path_len:0]u8 = undefined; + const dir = socketDir(&dir_buf) orelse return error.BadDial; + return .{ .unix = socketPath(buf, dir, dial) orelse return error.BadDial }; } - // This thread is the editor's and plays the client too, resting while - // it does so that the runner's tasks can take the turn and answer. - const s = try gpa.create(Client.Session); - defer gpa.destroy(s); - s.* = .{ .fd = -1, .deadline = nowMs() + 3 * Client.budget_ms, .display_path = "/log" }; - var sock_buf: [sun_path_len]u8 = undefined; - pardes.turn.rest(); - defer pardes.turn.wake(); - s.fd = try Client.connect(try Client.resolve(&sock_buf, l.path()), s.deadline); - defer _ = libc.close(s.fd); - s.cl = .init(.{ .in = &s.in, .out = &s.out }); - var remote: Client.RemoteError = .{}; - _ = try s.ask(.{ .version = .{} }, &remote); - _ = try s.ask(.{ .attach = .{ .fid = 0, .uname = "held" } }, &remote); - _ = try s.ask(.{ .walk = .{ .fid = 0, .newfid = 1, .names = &.{"log"} } }, &remote); - _ = try s.ask(.{ .open = .{ .fid = 1, .mode = ninep.ordwr } }, &remote); - var frozen: u64 = 0; - while (true) { - const n = (try s.ask(.{ .read = .{ .fid = 1, .offset = frozen, .count = 4096 } }, &remote)).read.len; - if (n == 0) break; - frozen += n; + + pub fn validateDial(dial: []const u8) error{BadDial}!void { + var buf: [sun_path_len]u8 = undefined; + _ = try resolve(&buf, dial); } - _ = try s.ask(.{ .write = .{ .fid = 1, .offset = 0, .data = "follow\n" } }, &remote); - const heldNow = struct { - /// Waits until the core holds `n` reads. - fn count(core: *pardes.Pardes, n: usize) !void { - const deadline = nowMs() + Client.budget_ms; - while (nowMs() < deadline) { - pardes.turn.wake(); - var got: usize = 0; - for (core.fs.opens) |o| got += @intFromBool(o.held != null); - pardes.turn.rest(); - if (got == n) return; - nap(1); - } - return error.Timeout; + const Session = struct { + fd: c_int, + deadline: i64, + display_path: []const u8, + cl: ninep.Client = undefined, + in: [msize]u8 = undefined, + out: [msize]u8 = undefined, + stage: [msize]u8 = undefined, + /// Version and attach answered: fid 0 is the peer's root, and each + /// operation walks a fid of its own from it, so a kept connection + /// (`kept`) pays for neither again. + attached: bool = false, + root_qid: ninep.Qid = undefined, + next_fid: u32 = 1, + /// Tags whose answers nobody waits for (a clunk's): dropped by + /// whoever settles next. + unwaited: u32 = 0, + /// The dial a kept connection answers for. + dial_buf: [sun_path_len + 32]u8 = undefined, + dial_len: usize = 0, + + fn wait(s: *Session, events: i16) Error!void { + if (s.deadline - nowMs() <= 0) return Error.Timeout; + return transport.wait(s.fd, events, s.deadline) catch |err| switch (err) { + error.Timeout => Error.Timeout, + else => Error.Hangup, + }; } - }; - const serial = p.panes[0].?.serial; - var event_path: [32]u8 = undefined; - var event_names: [3][]const u8 = .{ "pane", try std.fmt.bufPrint(&event_path, "{d}", .{serial}), "event" }; - _ = try s.ask(.{ .walk = .{ .fid = 0, .newfid = 2, .names = &event_names } }, &remote); - _ = try s.ask(.{ .open = .{ .fid = 2, .mode = ninep.oread } }, &remote); - // Flushed while held, a read is gone from the engine without the core - // hearing of it. Its tag, asked again at once for a read of the pane's - // event, must not be answered with the log's next record, and that - // record must reach the log's next read. - const flushed = try s.cl.submit(.{ .read = .{ .fid = 1, .offset = 0, .count = 4096 } }); - try s.flush(); - try heldNow.count(p, 1); - _ = try s.cl.submit(.{ .flush = .{ .oldtag = flushed } }); - const interrupted = try s.settle(); - try testing.expectEqual(flushed, interrupted.tag); - try testing.expect(interrupted.result == .fail); - try testing.expect((try s.settle()).result == .flush); - const reused = try s.cl.submit(.{ .read = .{ .fid = 2, .offset = 0, .count = 4096 } }); - try testing.expectEqual(flushed, reused); - try s.flush(); - try heldNow.count(p, 2); - pardes.turn.wake(); - p.setMessage(0, "after the flush"); - pardes.turn.rest(); - try heldNow.count(p, 1); - pardes.turn.wake(); - p.fs.origin = 'K'; - pardes.ctlfs.events.noteAction(p, 0, .body_exec, 0, 4, 0, "held"); - pardes.turn.rest(); - const clicked = try s.settle(); - try testing.expectEqual(reused, clicked.tag); - try testing.expectEqualStrings("KX0 4 0 4 held\n", clicked.result.read); - try testing.expect(std.mem.indexOf(u8, (try s.ask(.{ .read = .{ .fid = 1, .offset = 0, .count = 4096 } }, &remote)).read, "after the flush") != null); + fn flush(s: *Session) Error!void { + while (s.cl.output().len != 0) { + if (nowMs() >= s.deadline) return Error.Timeout; + const bytes = s.cl.output(); + try s.wait(poll_out); + const sent = (transport.write(s.fd, bytes) catch return Error.Hangup) orelse continue; + s.cl.wrote(@intCast(sent)); + } + } - // Held, a read is answered by the record that arrives, on its own - // connection, with no retry of every parked request asked for; a - // second read on the same open meanwhile is refused, not orphaned. - const waiting = try s.cl.submit(.{ .read = .{ .fid = 1, .offset = 0, .count = 4096 } }); - try s.flush(); - try heldNow.count(p, 1); - const second = try s.cl.submit(.{ .read = .{ .fid = 1, .offset = 0, .count = 4096 } }); - const refused = try s.settle(); - try testing.expectEqual(second, refused.tag); - try testing.expectEqualStrings(pardes.ctlfs.e_in_use, refused.result.fail); - pardes.turn.wake(); - p.setMessage(0, "while held"); - const retry_all = pardes.turn.parked; - pardes.turn.rest(); - try testing.expect(!retry_all); - const answered = try s.settle(); - try testing.expectEqual(waiting, answered.tag); - try testing.expect(std.mem.indexOf(u8, answered.result.read, "while held") != null); + fn settle(s: *Session) Error!ninep.Client.Done { + while (true) { + if (nowMs() >= s.deadline) return Error.Timeout; + try s.flush(); + if (s.cl.take()) |done| { + if (done.tag < 32 and s.unwaited & bit(done.tag) != 0) { + s.unwaited &= ~bit(done.tag); + continue; + } + return done; + } + if (s.cl.dead) return Error.Botch; + const room = s.cl.in.len - s.cl.in_len; + if (room == 0) return Error.Botch; + try s.wait(poll_in); + const got = (transport.read(s.fd, s.stage[0..@min(room, s.stage.len)]) catch return Error.Hangup) orelse continue; + if (got == 0) return Error.Hangup; + const n = s.cl.push(s.stage[0..@intCast(got)]); + std.debug.assert(n == @as(usize, @intCast(got))); + } + } - // Clunked, the log's held read is interrupted by the engine and the - // record it would have had is spent on nobody. - _ = try s.cl.submit(.{ .read = .{ .fid = 1, .offset = 0, .count = 4096 } }); - try s.flush(); - try heldNow.count(p, 1); - s.drop(1); - s.drop(2); - try heldNow.count(p, 0); + fn ask(s: *Session, req: ninep.Client.Request, remote: *RemoteError) Error!ninep.Client.Result { + _ = s.cl.submit(req) catch return Error.Botch; + const done = try s.settle(); + if (done.result == .fail) return remote.set(done.result.fail); + if (std.mem.eql(u8, @tagName(done.result), @tagName(std.meta.activeTag(req)))) return done.result; + return Error.Botch; + } - // Records queued as a Restore comes reach a follower in the answer to - // the read it holds, all of them, before the hangup cuts it. (The - // interrupted read above answered first: one clunk's answer is unread.) - _ = try s.settle(); - _ = try s.ask(.{ .walk = .{ .fid = 0, .newfid = 3, .names = &.{"log"} } }, &remote); - _ = try s.ask(.{ .open = .{ .fid = 3, .mode = ninep.ordwr } }, &remote); - _ = try s.ask(.{ .write = .{ .fid = 3, .offset = 0, .data = "follow new\n" } }, &remote); - const last = try s.cl.submit(.{ .read = .{ .fid = 3, .offset = 0, .count = 4096 } }); - try s.flush(); - try heldNow.count(p, 1); - pardes.turn.wake(); - p.setMessage(0, "queued one"); - p.setMessage(0, "queued two"); - p.setMessage(0, "queued three"); - l.reset(p); - pardes.turn.rest(); - const cut = try s.settle(); - try testing.expectEqual(last, cut.tag); - for ([_][]const u8{ "queued one", "queued two", "queued three" }) |said| - try testing.expect(std.mem.indexOf(u8, cut.result.read, said) != null); -} + fn drop(s: *Session, fid: u32) void { + _ = s.cl.submit(.{ .clunk = .{ .fid = fid } }) catch return; + _ = s.settle() catch {}; + } -extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; + fn dropNoWait(s: *Session, fid: u32) void { + const tag = s.cl.submit(.{ .clunk = .{ .fid = fid } }) catch return; + s.unwaited |= bit(tag); + s.flush() catch {}; + } -pub fn start(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes) ?*Listener { - var name: [16]u8 = undefined; - // unreachable: a u32 is at most 10 digits - const fallback = std.fmt.bufPrint(&name, "{d}", .{@as(u32, @intCast(libc.getpid()))}) catch unreachable; - return listen(io, gpa, core, core.opts.ninep_name, fallback, core.opts.ninep_tcp) catch { - if (refusal.len > 0) core.setMessage(0, refusal) else core.reportError(0, "9p listener", error.ListenFailed); - return null; - }; -} + fn send(s: *Session, req: ninep.Client.Request) Error!u16 { + return s.cl.submit(req) catch Error.Botch; + } -/// What a pane shell is told about the editor above it, set into this -/// process's environment just before `forkpty` so the child inherits it. -/// -/// Two independent facts, and they are separate variables because they answer -/// separate questions. `PARDES_PID` says "you are inside this editor, and it -/// will take a Look from you": a session whose 9P listener never came up still -/// owns its children, so the child says so rather than looking, from its own -/// side, exactly like no pardes at all. `PARDES_9P` and `PARDES_PANE` say how -/// to reach it, and a `--nested` session exports them too — its socket stays -/// open to scripts and to mounted shells — while withholding `PARDES_PID`, -/// which is the whole of what `--nested` means. -/// -/// The socket is this process's own (`listen` keeps it, its listener's -/// path, until `deinit`), for a pane's shell and for a command run off the -/// loop (an Edit's filter) alike. Told to children through -/// host_io.ChildEnv, never set into this process's own environment (see -/// ChildEnv.exportVar for why). -pub fn exportEnv(serial: u32, adopts: bool) void { - const socket: ?[]const u8 = if (own_socket_len > 0) own_socket[0..own_socket_len] else null; - const ChildEnv = @import("host_io.zig").ChildEnv; - var pid_buf: [16]u8 = undefined; - ChildEnv.exportVar("PARDES_PID", if (adopts) std.fmt.bufPrint(&pid_buf, "{d}", .{@as(u32, @intCast(libc.getpid()))}) catch unreachable else null); // a u32 fits 16 - if (socket) |own| { - var buf: [16]u8 = undefined; - ChildEnv.exportVar("PARDES_9P", own); - exportMount(own); - ChildEnv.exportVar("PARDES_PANE", std.fmt.bufPrint(&buf, "{d}", .{serial}) catch unreachable); - return; - } - ChildEnv.exportVar("PARDES_9P", null); - ChildEnv.exportVar("PARDES_PANE", null); - ChildEnv.exportVar("PARDES_MOUNT", null); -} + fn newFid(s: *Session) u32 { + const fid = s.next_fid; + s.next_fid = if (fid >= 1 << 20) 1 else fid + 1; + return fid; + } -/// $PARDES_MOUNT, the session's own directory, when the environment says -/// where it is -- never by looking at the mount, which this editor serves -/// (the self-mount deadlock). Under `9ns --mntgen` (NINE_MNTGEN=1) it is -/// `$NINE_MOUNT/pardes/`, the name the registry gives the socket. -/// Any other $NINE_MOUNT is a mount of another server, made before this -/// session had a socket: unknown, so unset, and a script falls back to -/// `9p -a "unix!$PARDES_9P"`. -fn exportMount(sock_path: []const u8) void { - const mntgen = if (libc.getenv("NINE_MNTGEN")) |v| std.mem.eql(u8, std.mem.span(v), "1") else false; - const root = if (libc.getenv("NINE_MOUNT")) |v| std.mem.span(v) else ""; - const base = std.Io.Dir.path.basename(sock_path); - const sock_prefix = "pardes-9p-"; - const suffix = ".sock"; - if (mntgen and root.len > 0 and std.mem.startsWith(u8, base, sock_prefix) and std.mem.endsWith(u8, base, suffix) and base.len > sock_prefix.len + suffix.len) { - var buf: [4096]u8 = undefined; - if (std.fmt.bufPrint(&buf, "{s}/pardes/{s}", .{ std.mem.trimEnd(u8, root, "/"), base[sock_prefix.len .. base.len - suffix.len] })) |mount| { - return @import("host_io.zig").ChildEnv.exportVar("PARDES_MOUNT", mount); - } else |_| {} - } - @import("host_io.zig").ChildEnv.exportVar("PARDES_MOUNT", null); -} + /// A new connection's Tversion and Tattach, sent with the first + /// operation's requests behind them in one flight (`land`). + fn greet(s: *Session, f: *Flight) Error!void { + if (s.attached) return; + _ = f.add(.version, try s.send(.{ .version = .{} })); + // ponytail: cloud9's client submits nothing while a Tversion is + // out, so its msize is taken as asked until the Rversion lands + // (`land` puts it back); only a pardes is dialled here, and every + // one offers ninep.msize. A Twrite in this flight stays under the + // least a listener offers (`flight_write`). The fix is cloud9's + // client taking requests while it versions. + s.cl.versioning = false; + s.cl.msize = s.cl.asked; + _ = f.add(.attach, try s.send(.{ .attach = .{ .fid = 0, .uname = uname } })); + } -test "a pane's PARDES_MOUNT is the session's directory under 9ns --mntgen, and unset when not known" { - const names = [_][*:0]const u8{ "NINE_MNTGEN", "NINE_MOUNT", "PARDES_MOUNT" }; - var saved: [names.len]?[:0]u8 = @splat(null); - for (names, &saved) |name, *value| { - if (libc.getenv(name)) |old| value.* = try testing.allocator.dupeSentinel(u8, std.mem.span(old), 0); - } - defer for (names, saved) |name, value| { - if (value) |old| { - _ = setenv(name, old, 1); - testing.allocator.free(old); - } else _ = @import("host_io.zig").testUnsetenv(name); + /// The walk to a new fid at `names` (two Twalks past 16 names, the + /// second from the fid to itself), from the root. + fn walkNew(s: *Session, f: *Flight, names: []const []const u8) Error!u32 { + const fid = s.newFid(); + const first = names[0..@min(names.len, ninep.max_welem)]; + f.add(.walk, try s.send(.{ .walk = .{ .fid = 0, .newfid = fid, .names = first } })).asked = @intCast(first.len); + if (names.len > first.len) + f.add(.walk, try s.send(.{ .walk = .{ .fid = fid, .newfid = fid, .names = names[first.len..] } })).asked = @intCast(names.len - first.len); + return fid; + } + + /// Sends the flight and takes every answer of it, a read's bytes + /// into `taker` as they come (each valid only until the next), so + /// that the connection is left with nothing out but its clunks. A + /// refused Tversion is its words at once: nothing after it is + /// answered. + fn land(s: *Session, f: *Flight, taker: ?*Taker, remote: *RemoteError) Error!void { + if (f.get(.version) != null) s.cl.versioning = true; + while (f.open()) { + const done = try s.settle(); + const item = f.find(done.tag) orelse return Error.Botch; + item.done = true; + switch (done.result) { + .fail => |words| { + item.failed = true; + item.said.keep(words); + if (item.step == .version) return remote.set(words); + }, + .version => { + if (item.step != .version or s.cl.msize == 0) return Error.Botch; + }, + .attach => |qid| { + if (item.step != .attach) return Error.Botch; + s.root_qid = qid; + s.attached = true; + }, + .walk => |w| { + if (item.step != .walk) return Error.Botch; + item.n = w.nwqid; + if (w.nwqid > 0) item.qid = w.wqid[w.nwqid - 1]; + }, + .stat => |st| { + if (item.step != .stat) return Error.Botch; + item.n = st.length; + }, + .open => |o| { + if (item.step != .open) return Error.Botch; + item.qid = o.qid; + }, + .read => |data| { + if (item.step != .read) return Error.Botch; + item.n = data.len; + if (taker) |t| t.take(item, data); + // A read is timed by its progress, as a long write + // is: each answer gives the rest the budget again, so + // a large file over a slow link is not cut off. + s.deadline = @max(s.deadline, nowMs() +| budget_ms); + }, + .write => |count| { + if (item.step != .write) return Error.Botch; + item.n = count; + }, + else => return Error.Botch, + } + } + } }; - _ = setenv("NINE_MOUNT", "/mnt/9p", 1); - _ = setenv("NINE_MNTGEN", "1", 1); - exportMount("/run/user/1000/pardes-9p-work.sock"); - try testing.expectEqualStrings("/mnt/9p/pardes/work", @import("host_io.zig").ChildEnv.exported("PARDES_MOUNT").?); - // A mount of another server (9ns --unix): not this session's. - _ = @import("host_io.zig").testUnsetenv("NINE_MNTGEN"); - exportMount("/run/user/1000/pardes-9p-work.sock"); - try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_MOUNT") == null); -} -test "9P shell environment states being inside pardes apart from how to reach it" { - const names = [_][*:0]const u8{ "XDG_RUNTIME_DIR", "HOME", "PARDES_PID", "PARDES_9P", "PARDES_PANE" }; - var saved: [names.len]?[:0]u8 = @splat(null); - for (names, &saved) |name, *value| { - if (libc.getenv(name)) |old| value.* = try testing.allocator.dupeSentinel(u8, std.mem.span(old), 0); + fn bit(tag: u16) u32 { + return @as(u32, 1) << @intCast(tag); } - defer for (names, saved) |name, value| { - if (value) |old| { - _ = setenv(name, old, 1); - testing.allocator.free(old); - } else _ = @import("host_io.zig").testUnsetenv(name); - }; - var dir: [sun_path_len:0]u8 = undefined; - _ = setenv("XDG_RUNTIME_DIR", "/run/user/1000", 1); - try testing.expectEqualStrings("/run/user/1000", socketDir(&dir).?); - _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); - _ = setenv("HOME", "/home/example", 1); - try testing.expectEqualStrings("/home/example/.local/state/pardes", socketDir(&dir).?); - _ = @import("host_io.zig").testUnsetenv("HOME"); - try testing.expect(socketDir(&dir) == null); + /// The requests of one flight, each its own tag, sent back to back: a + /// pardes takes one connection's requests in order (cloud9's engine + /// starts a request once the one before it is answered or parked), so + /// a walk, the open of its fid and the first read or write need no + /// answer between them, and an operation costs one round trip. + const Flight = struct { + items: [ninep.Client.window]Item = undefined, + len: usize = 0, - // The socket a listener keeps while it lives. - const path = "/tmp/pardes-example.sock"; - @memcpy(own_socket[0..path.len], path); - own_socket[path.len] = 0; - own_socket_len = path.len; - defer own_socket_len = 0; - var own: [16]u8 = undefined; - const own_pid = try std.fmt.bufPrint(&own, "{d}", .{@as(u32, @intCast(libc.getpid()))}); + const Step = enum { version, attach, walk, stat, open, read, write }; + const Item = struct { + step: Step, + tag: u16, + done: bool = false, + failed: bool = false, + said: RemoteError = .{}, + /// walk: names asked; read: its offset and count. + asked: u16 = 0, + off: u64 = 0, + count: u32 = 0, + /// walk: qids answered; stat: the length; read, write: bytes. + n: u64 = 0, + qid: ninep.Qid = undefined, + }; - // A `--nested` session: reachable for scripts and mounts, but nobody's - // parent, so a pardes started in one of its shells runs a session of - // its own instead of handing its argument over. - exportEnv(7, false); - try testing.expectEqualStrings(path, @import("host_io.zig").ChildEnv.exported("PARDES_9P").?); - try testing.expectEqualStrings("7", @import("host_io.zig").ChildEnv.exported("PARDES_PANE").?); - try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PID") == null); + fn add(f: *Flight, step: Step, tag: u16) *Item { + f.items[f.len] = .{ .step = step, .tag = tag }; + f.len += 1; + return &f.items[f.len - 1]; + } - exportEnv(8, true); - try testing.expectEqualStrings("8", @import("host_io.zig").ChildEnv.exported("PARDES_PANE").?); - try testing.expectEqualStrings(own_pid, @import("host_io.zig").ChildEnv.exported("PARDES_PID").?); + fn find(f: *Flight, tag: u16) ?*Item { + for (f.items[0..f.len]) |*item| if (item.tag == tag and !item.done) return item; + return null; + } - // A session whose listener never came up is still the session this shell - // is inside: the pid stands on its own, and only the address is missing. - own_socket_len = 0; - exportEnv(0, true); - try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_9P") == null); - try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PANE") == null); - try testing.expectEqualStrings(own_pid, @import("host_io.zig").ChildEnv.exported("PARDES_PID").?); + fn get(f: *Flight, step: Step) ?*Item { + for (f.items[0..f.len]) |*item| if (item.step == step) return item; + return null; + } - exportEnv(0, false); - try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_9P") == null); - try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PANE") == null); - try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PID") == null); -} + fn open(f: *const Flight) bool { + for (f.items[0..f.len]) |item| if (!item.done) return true; + return false; + } -pub const Client = struct { - pub const budget_ms: i64 = 2000; - /// Each write of a long one has this long to be answered (transact). - pub const write_budget_ms: i64 = 30_000; + /// The first refusal in the order asked: a version's or an attach's + /// words, a walk short of its names, an open's refusal. + fn check(f: *Flight, remote: *RemoteError) Error!void { + for (f.items[0..f.len]) |*item| switch (item.step) { + .version, .attach, .open => if (item.failed) return remote.set(item.said.buf[0..item.said.len]), + .walk => { + if (item.failed) return remote.set(item.said.buf[0..item.said.len]); + if (item.n != item.asked) return Error.NotFound; + }, + .stat, .read, .write => {}, + }; + } - pub const max_depth: usize = 2 * ninep.max_welem; + /// The walked file's qid: the last walk's, or the root's for none. + fn qid(f: *Flight, root: ninep.Qid) ninep.Qid { + var at = root; + for (f.items[0..f.len]) |item| if (item.step == .walk and item.n > 0) { + at = item.qid; + }; + return at; + } - const uname = "pardes"; + /// Whether the walk made the fid, so that it is owed a clunk. + fn made(f: *Flight) bool { + const first = f.get(.walk) orelse return false; + return !first.failed and first.n == first.asked; + } + }; - const Dial = union(enum) { unix: [:0]const u8, tcp: std.Io.net.IpAddress }; + /// A write in a new connection's first flight is sent before the msize + /// is known: no larger than the least a pardes listener offers. + const flight_write = ninep.min_msize - 23; - pub const Error = error{ - PathTooDeep, - BadDial, - Dial, - Hangup, - Timeout, - Botch, - Remote, - IsDirectory, - NotFound, - FileTooLarge, + /// Reads ahead this many Treads at once (the window, less a clunk + /// still out and one spare). + const read_ahead = ninep.Client.window - 2; + + /// Version and attach on a new connection, and a walk to `names`, in + /// one flight: the fid there. + fn walkTo(s: *Session, names: []const []const u8, remote: *RemoteError) !u32 { + var f: Flight = .{}; + try s.greet(&f); + const fid = try s.walkNew(&f, names); + try s.land(&f, null, remote); + f.check(remote) catch |err| { + if (f.made()) s.dropNoWait(fid); + return err; + }; + return fid; + } + + /// A read's bytes taken in order: a file's as they are, a directory's + /// entries as rows of names. A read ahead that came back out of order, + /// or past one that came back short, is not taken: what it held is + /// read again from where the taken bytes end. + const Taker = struct { + out: *std.Io.Writer.Allocating, + display_path: []const u8, + limit: usize, + /// The flight with the walk, whose answer says what was reached: + /// it lands before any read (a walk never parks). + flight: *Flight, + s: *const Session, + /// Bytes taken from the start; a directory's next offset. + end: u64 = 0, + /// The read that ended at `end` came back short, or empty (the end). + short: bool = false, + empty: bool = false, + /// What taking said (too large, a bad entry): the operation's + /// answer once every read out has landed. + err: ?Error = null, + + fn take(t: *Taker, item: *Flight.Item, data: []const u8) void { + if (t.err != null or item.off != t.end or t.short) return; + t.put(data) catch |err| { + t.err = err; + return; + }; + t.end += data.len; + t.short = data.len < item.count; + t.empty = data.len == 0; + } + + fn directory(t: *const Taker) bool { + return t.flight.qid(t.s.root_qid).type & ninep.qtdir != 0; + } + + fn maxBytes(t: *const Taker) u64 { + return @min(t.limit, @as(usize, if (t.directory()) limits.max_stream_bytes else limits.max_file_bytes)); + } + + fn wireLimit(t: *const Taker) u64 { + return if (t.directory()) limits.max_stream_bytes else t.maxBytes(); + } + + fn put(t: *Taker, data: []const u8) Error!void { + if (t.end + data.len > t.wireLimit()) return Error.FileTooLarge; + if (t.directory()) { + var pos: usize = 0; + while (pos < data.len) { + if (data.len - pos < 2) return Error.Botch; + const len: usize = 2 + @as(usize, std.mem.readInt(u16, data[pos..][0..2], .little)); + if (len > data.len - pos) return Error.Botch; + const entry = ninep.Stat.decode(data[pos..][0..len]) catch return Error.Botch; + const display_dir = std.mem.trimEnd(u8, t.display_path, "/"); + const row_len = display_dir.len + entry.name.len + 2 + @as(usize, @intFromBool(entry.qid.type & ninep.qtdir != 0)); + if (row_len > t.maxBytes() - t.out.written().len) return Error.FileTooLarge; + t.out.writer.print("{s}/{s}", .{ display_dir, entry.name }) catch return Error.Botch; + if (entry.qid.type & ninep.qtdir != 0) t.out.writer.writeByte('/') catch return Error.Botch; + t.out.writer.writeByte('\n') catch return Error.Botch; + pos += len; + } + } else t.out.writer.writeAll(data) catch return Error.Botch; + } }; - pub fn read(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8) ![]u8 { - return readLimit(gpa, dial, path, display_path, limits.max_file_bytes); - } + /// One operation in as few round trips as its answers allow: the walk, + /// a read's stat, the open and the first read or write go in one flight + /// (behind a new connection's version and attach). A file of the + /// length its stat said is whole when a read comes back short at that + /// length; a longer one is read ahead, the Treads for the rest sent at + /// once. Anything else (a directory, a file of no stated length) reads + /// on until a read comes back empty, the only end 9P has. + fn transact( + s: *Session, + names: []const []const u8, + out: *std.Io.Writer.Allocating, + remote: *RemoteError, + write_bytes: ?[]const u8, + read_limit: usize, + ) !void { + const writing = write_bytes != null; + const mode: u8 = if (writing) ninep.owrite else ninep.oread; + const truncate = writing and names.len > 0 and + (std.mem.eql(u8, names[0], "os") or std.mem.eql(u8, names[names.len - 1], "body")); + const fresh = !s.attached; + var f: Flight = .{}; + try s.greet(&f); + const fid = try s.walkNew(&f, names); + if (!writing) _ = f.add(.stat, try s.send(.{ .stat = .{ .fid = fid } })); + _ = f.add(.open, try s.send(.{ .open = .{ .fid = fid, .mode = mode | if (truncate) ninep.otrunc else 0 } })); + const chunk_max: usize = if (fresh) flight_write else s.cl.maxWrite(); + const first: []const u8 = if (write_bytes) |bytes| bytes[0..@min(bytes.len, chunk_max)] else ""; + if (writing and first.len != 0) { + // Out of the client's buffer first (it holds one msize): still + // sent without waiting. + try s.flush(); + _ = f.add(.write, try s.send(.{ .write = .{ .fid = fid, .offset = 0, .data = first } })); + } + // A read's count before the msize is known is the asked msize's. + if (!writing) { + const item = f.add(.read, try s.send(.{ .read = .{ .fid = fid, .offset = 0, .count = s.cl.maxRead() } })); + item.count = s.cl.maxRead(); + } + var taker: Taker = .{ .out = out, .display_path = s.display_path, .limit = read_limit, .flight = &f, .s = s }; + try s.land(&f, &taker, remote); + defer if (f.made()) s.dropNoWait(fid); + try f.check(remote); + if (taker.err) |err| return err; + const directory = taker.directory(); - pub fn readLimit(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8, max_bytes: usize) ![]u8 { - return fetch(false, gpa, dial, path, display_path, null, @min(max_bytes, limits.max_file_bytes)); - } + if (write_bytes) |bytes| { + if (directory) return Error.IsDirectory; + var written: usize = 0; + if (f.get(.write)) |w| { + // Refused unopened, the open parked (a truncating one while + // the editor is out): sent again below, after it. + if (w.failed and !std.mem.eql(u8, w.said.buf[0..w.said.len], ninep.fs.e_bad_use)) return remote.set(w.said.buf[0..w.said.len]); + if (!w.failed) { + if (w.n == 0 or w.n > first.len) return Error.Botch; + written = w.n; + } + } + while (written < bytes.len) { + s.deadline = nowMs() +| write_budget_ms; + const chunk = bytes[written..][0..@min(bytes.len - written, s.cl.maxWrite())]; + const count = (try s.ask(.{ .write = .{ .fid = fid, .offset = written, .data = chunk } }, remote)).write; + if (count == 0 or count > chunk.len) return Error.Botch; + written += count; + // A long write is timed by its progress: each answer gives + // the next its own budget, so a large body (`pardes -`) goes + // in through one open, whole, however long it takes. The + // budget is a write's: a server putting in tens of MiB it + // held (pane.zig batchFor) answers that one write late. + } + return; + } - /// readLimit through a mount: its connection kept for the next (`kept`). - pub fn readMount(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8, max_bytes: usize) ![]u8 { - return fetch(true, gpa, dial, path, display_path, null, @min(max_bytes, limits.max_file_bytes)); + if (f.get(.read)) |r| if (r.failed and !std.mem.eql(u8, r.said.buf[0..r.said.len], ninep.fs.e_bad_use)) + return remote.set(r.said.buf[0..r.said.len]); + const length: u64 = if (f.get(.stat)) |st| (if (st.failed or directory) 0 else st.n) else 0; + const step: u64 = s.cl.maxRead(); + while (true) { + if (taker.empty) return; + if (taker.short and length != 0 and taker.end == length) return; + // The rest of a file of known length at once; a directory, a + // file of none, or the end to be seen, a read at a time. + // ponytail: a file of no stated length is not read ahead. + const want: u64 = if (directory or taker.short or length <= taker.end) 1 else @min(read_ahead, (length - taker.end) / step + 1); + var batch: Flight = .{}; + var off = taker.end; + for (0..want) |_| { + if (off > taker.wireLimit()) break; + const count: u32 = @intCast(@min(step, taker.wireLimit() + 1 - off)); + const item = batch.add(.read, try s.send(.{ .read = .{ .fid = fid, .offset = off, .count = count } })); + item.off = off; + item.count = count; + off += count; + } + taker.short = false; + try s.land(&batch, &taker, remote); + if (taker.err) |err| return err; + for (batch.items[0..batch.len]) |item| if (item.failed and item.off == taker.end and !taker.short) + return remote.set(item.said.buf[0..item.said.len]); + } } - /// One operation on `path`, a read of up to `read_limit` or a write of - /// `write_bytes`, on the connection kept for `dial` when `keep`: the - /// peer's words, when it refuses, kept for lastSaid. - fn fetch(keep: bool, gpa: std.mem.Allocator, dial: []const u8, path: []const u8, display_path: []const u8, write_bytes: ?[]const u8, read_limit: usize) ![]u8 { - if (comptime !supported) return error.Unsupported; - var names: [max_depth][]const u8 = undefined; - const n = try elements(path, &names); - var remote: RemoteError = .{}; - return fetchBytes(gpa, dial, names[0..n], &remote, write_bytes, display_path, read_limit, keep) catch |err| { - if (err == error.Remote) said = remote; - return err; + fn fetchBytes( + gpa: std.mem.Allocator, + dial: []const u8, + names: []const []const u8, + remote: *RemoteError, + write_bytes: ?[]const u8, + display_path: []const u8, + read_limit: usize, + keep: bool, + ) ![]u8 { + if (comptime !supported) return Error.Dial; + if (keep) if (kept.take(dial)) |s| { + if (fetchOn(s, gpa, names, remote, write_bytes, read_limit, display_path, true)) |bytes| return bytes else |err| switch (err) { + // Let go of by the peer since (its hangup still on the way): + // a read is asked again on a new connection. A write is not, + // as it may have been done. + error.Hangup, error.Botch => if (write_bytes != null) return err, + else => return err, + } }; + return fetchOn(try startSession(dial, display_path), gpa, names, remote, write_bytes, read_limit, display_path, keep); } - /// The peer's own words for the last error.Remote a read or write here - /// got (its Rerror), for a caller that says why: this thread's, as the - /// editor's mounts and a Handover job each ask from their own. - threadlocal var said: RemoteError = .{}; - pub fn lastSaid() []const u8 { - return said.buf[0..said.len]; - } - - /// Whether a peer answers at `dial`: a version and attach, and its root - /// read. Any answer from the peer, an error of its own included, is one. - pub fn probe(gpa: std.mem.Allocator, dial: []const u8) !void { - return probeWith(false, gpa, dial); + fn fetchOn( + s: *Session, + gpa: std.mem.Allocator, + names: []const []const u8, + remote: *RemoteError, + write_bytes: ?[]const u8, + read_limit: usize, + display_path: []const u8, + keep: bool, + ) ![]u8 { + s.deadline = nowMs() +| budget_ms; + s.display_path = display_path; + var clean = false; + defer endSession(s, keep and clean); + var out: std.Io.Writer.Allocating = .init(gpa); + errdefer out.deinit(); + transact(s, names, &out, remote, write_bytes, read_limit) catch |err| { + // Refused with every answer in: the connection is as good as before. + clean = switch (err) { + error.Remote, error.NotFound, error.IsDirectory, error.FileTooLarge => true, + else => false, + }; + return err; + }; + clean = true; + return out.toOwnedSlice(); } - /// probe for a Mount: the connection kept as the mount's. - pub fn probeMount(gpa: std.mem.Allocator, dial: []const u8) !void { - return probeWith(true, gpa, dial); + /// A new connection at `dial` whose requests have the usual deadline. + fn startSession(dial: []const u8, display_path: []const u8) !*Session { + var sock_buf: [sun_path_len]u8 = undefined; + const sock = try resolve(&sock_buf, dial); + const deadline = nowMs() +| budget_ms; + // Not the caller's allocator: a kept one outlives the call (an arena's). + const s = try std.heap.page_allocator.create(Session); + s.* = .{ .fd = -1, .deadline = deadline, .display_path = display_path }; + errdefer endSession(s, false); + s.fd = try connect(sock, deadline); + s.cl = .init(.{ .in = &s.in, .out = &s.out }); + if (dial.len <= s.dial_buf.len) { + @memcpy(s.dial_buf[0..dial.len], dial); + s.dial_len = dial.len; + } + return s; } - fn probeWith(keep: bool, gpa: std.mem.Allocator, dial: []const u8) !void { - const got = fetch(keep, gpa, dial, "", "", null, 0) catch |err| switch (err) { - error.IsDirectory, error.NotFound, error.Remote, error.FileTooLarge => return, - else => return err, - }; - gpa.free(got); + fn endSession(s: *Session, keep: bool) void { + if (keep and s.attached and s.dial_len != 0 and kept.put(s)) return; + if (s.fd >= 0) _ = libc.close(s.fd); + std.heap.page_allocator.destroy(s); } - pub fn write(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, bytes: []const u8) !void { - gpa.free(try fetch(false, gpa, dial, path, path, bytes, limits.max_file_bytes)); - } + /// Connections kept open between operations, a few dials' (a mount's, + /// as Plan 9's mount keeps its channel): each costs the peer one of its + /// `max_conns` while kept, so only mounts and the Mount probe keep one. + const kept = struct { + const max = 8; + var lock: std.atomic.Mutex = .unlocked; + var slots: [max]?*Session = @splat(null); - /// write through a mount: its connection kept for the next (`kept`). - pub fn writeMount(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, bytes: []const u8) !void { - gpa.free(try fetch(true, gpa, dial, path, path, bytes, limits.max_file_bytes)); - } + fn hold() void { + while (!lock.tryLock()) std.atomic.spinLoopHint(); + } - const RemoteError = struct { - buf: [ninep.errmax]u8 = undefined, - len: usize = 0, + /// The session kept for `dial`, out of the table, if it is still + /// there: what came while it waited (a clunk's answer) taken, and + /// a hangup or anything else seen as its end. + fn take(dial: []const u8) ?*Session { + hold(); + const s = for (&slots) |*slot| { + const got = slot.* orelse continue; + if (std.mem.eql(u8, got.dial_buf[0..got.dial_len], dial)) { + slot.* = null; + break got; + } + } else null; + lock.unlock(); + const found = s orelse return null; + if (usable(found)) return found; + endSession(found, false); + return null; + } - fn set(r: *RemoteError, msg: []const u8) error{Remote} { - r.keep(msg); - return error.Remote; + fn usable(s: *Session) bool { + while (true) { + var fds = [1]libc.pollfd{.{ .fd = s.fd, .events = poll_in, .revents = 0 }}; + if (libc.poll(&fds, 1, 0) < 0) return false; + if (fds[0].revents == 0) return true; + if (fds[0].revents & poll_in == 0) return false; + const room = s.cl.in.len - s.cl.in_len; + if (room == 0) return false; + const got = (transport.read(s.fd, s.stage[0..@min(room, s.stage.len)]) catch return false) orelse return true; + if (got == 0) return false; + _ = s.cl.push(s.stage[0..got]); + while (s.cl.take()) |done| { + if (done.tag >= 32 or s.unwaited & bit(done.tag) == 0) return false; + s.unwaited &= ~bit(done.tag); + } + if (s.cl.dead) return false; + } } - fn keep(r: *RemoteError, msg: []const u8) void { - r.len = @min(msg.len, r.buf.len); - @memcpy(r.buf[0..r.len], msg[0..r.len]); + fn put(s: *Session) bool { + hold(); + defer lock.unlock(); + for (&slots) |*slot| if (slot.* == null) { + slot.* = s; + return true; + }; + return false; } }; - fn elements(path: []const u8, out: *[max_depth][]const u8) Error!usize { - var n: usize = 0; - var it = std.mem.tokenizeScalar(u8, path, '/'); - while (it.next()) |name| { - if (n == out.len) return Error.PathTooDeep; - out[n] = name; - n += 1; + /// Lets go of a connection kept for `dial` (an Unmount): its mount is gone. + pub fn forget(dial: []const u8) void { + if (comptime !supported) return; + while (kept.take(dial)) |s| endSession(s, false); + } + + /// Writes `request`, then `more`, to `path` and reads the answer on the + /// same open, as /pane/new answers its own open: a session's last + /// answer, from another client, can never be what comes back. `exec + /// 3<>file`. `more` (`pardes -`'s text after its /pager line) goes in + /// Twrites at rising offsets, each answer giving the next its budget. + pub fn ask(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, request: []const u8, more: []const u8) ![]u8 { + if (comptime !supported) return error.Unsupported; + var names: [max_depth][]const u8 = undefined; + const n = try elements(path, &names); + var remote: RemoteError = .{}; + errdefer said = remote; + const s = try startSession(dial, path); + defer endSession(s, false); + const fid = try walkTo(s, names[0..n], &remote); + _ = try s.ask(.{ .open = .{ .fid = fid, .mode = ninep.ordwr } }, &remote); + var written: usize = 0; + while (written < request.len + more.len) { + const from = if (written < request.len) request[written..] else more[written - request.len ..]; + const chunk = from[0..@min(from.len, s.cl.maxWrite())]; + const count = (try s.ask(.{ .write = .{ .fid = fid, .offset = written, .data = chunk } }, &remote)).write; + if (count == 0 or count > chunk.len) return Error.Botch; + written += count; + s.deadline = nowMs() +| write_budget_ms; + } + var out: std.ArrayList(u8) = .empty; + errdefer out.deinit(gpa); + while (true) { + const data = (try s.ask(.{ .read = .{ .fid = fid, .offset = out.items.len, .count = s.cl.maxRead() } }, &remote)).read; + if (data.len == 0) return out.toOwnedSlice(gpa); + if (out.items.len + data.len > 4096) return Error.FileTooLarge; + try out.appendSlice(gpa, data); } - return n; } - fn resolve(buf: *[sun_path_len]u8, dial: []const u8) error{BadDial}!Dial { - if (dial.len == 0) return error.BadDial; - if (std.mem.startsWith(u8, dial, "tcp!")) return .{ .tcp = try networkAddress(dial, false) }; - const explicit_unix = std.mem.startsWith(u8, dial, "unix!"); - // A network this does not dial (`nowhere!x`) is a malformed address, - // not a session named so. - if (!explicit_unix and std.mem.indexOfScalar(u8, dial, '/') == null and std.mem.indexOfScalar(u8, dial, '!') != null) return error.BadDial; - const path = if (explicit_unix) dial[5..] else dial; - if (explicit_unix and !std.mem.startsWith(u8, path, "/")) return error.BadDial; - if (std.mem.indexOfScalar(u8, path, '/') != null) { - // A socket's path is absolute: a relative one would mean wherever - // the editor happens to run. - if (path[0] != '/') return error.BadDial; - if (std.mem.indexOfScalar(u8, path, 0) != null) return error.BadDial; - return .{ .unix = std.fmt.bufPrintSentinel(buf, "{s}", .{path}, 0) catch return error.BadDial }; + /// Opens `path` on one connection and writes `first` to it (/log's + /// `follow new`), all with the usual deadline; then, once `ready(ctx)` + /// has said it is not done already, reads it with no deadline, a read at + /// a time, until `record(ctx, bytes)` says done. An end of file or a + /// dropped connection is Hangup. What `pardes --wait` blocks on. + pub fn follow( + dial: []const u8, + path: []const u8, + first: []const u8, + ctx: anytype, + comptime ready: fn (@TypeOf(ctx)) bool, + comptime record: fn (@TypeOf(ctx), []const u8) bool, + ) !void { + if (comptime !supported) return error.Unsupported; + var names: [max_depth][]const u8 = undefined; + const n = try elements(path, &names); + var remote: RemoteError = .{}; + const s = try startSession(dial, path); + defer endSession(s, false); + const fid = try walkTo(s, names[0..n], &remote); + _ = try s.ask(.{ .open = .{ .fid = fid, .mode = ninep.ordwr } }, &remote); + _ = try s.ask(.{ .write = .{ .fid = fid, .offset = 0, .data = first } }, &remote); + if (ready(ctx)) return; + s.deadline = std.math.maxInt(i64); + while (true) { + const data = (try s.ask(.{ .read = .{ .fid = fid, .offset = 0, .count = s.cl.maxRead() } }, &remote)).read; + if (data.len == 0) return Error.Hangup; + if (record(ctx, data)) return; } - var dir_buf: [sun_path_len:0]u8 = undefined; - const dir = socketDir(&dir_buf) orelse return error.BadDial; - return .{ .unix = socketPath(buf, dir, dial) orelse return error.BadDial }; } - pub fn validateDial(dial: []const u8) error{BadDial}!void { - var buf: [sun_path_len]u8 = undefined; - _ = try resolve(&buf, dial); + fn connect(sock: Dial, deadline: i64) Error!c_int { + const address: transport.Address = switch (sock) { + .unix => |path| .{ .unix = path }, + .tcp => |ip| .{ .tcp = ip }, + }; + return transport.connectFd(address, deadline) catch return Error.Dial; } - const Session = struct { - fd: c_int, - deadline: i64, - display_path: []const u8, - cl: ninep.Client = undefined, - in: [msize]u8 = undefined, - out: [msize]u8 = undefined, - stage: [msize]u8 = undefined, - /// Version and attach answered: fid 0 is the peer's root, and each - /// operation walks a fid of its own from it, so a kept connection - /// (`kept`) pays for neither again. - attached: bool = false, - root_qid: ninep.Qid = undefined, - next_fid: u32 = 1, - /// Tags whose answers nobody waits for (a clunk's): dropped by - /// whoever settles next. - unwaited: u32 = 0, - /// The dial a kept connection answers for. - dial_buf: [sun_path_len + 32]u8 = undefined, - dial_len: usize = 0, + const poll_in: i16 = @intCast(libc.POLL.IN); + const poll_out: i16 = @intCast(libc.POLL.OUT); - fn wait(s: *Session, events: i16) Error!void { - if (s.deadline - nowMs() <= 0) return Error.Timeout; - return transport.wait(s.fd, events, s.deadline) catch |err| switch (err) { - error.Timeout => Error.Timeout, - else => Error.Hangup, - }; - } + test "a path becomes walk elements, normalised the way a shell would" { + var out: [max_depth][]const u8 = undefined; + try testing.expectEqual(@as(usize, 3), try elements("/pane/1/body", &out)); + try testing.expectEqualStrings("pane", out[0]); + try testing.expectEqualStrings("1", out[1]); + try testing.expectEqualStrings("body", out[2]); - fn flush(s: *Session) Error!void { - while (s.cl.output().len != 0) { - if (nowMs() >= s.deadline) return Error.Timeout; - const bytes = s.cl.output(); - try s.wait(poll_out); - const sent = (transport.write(s.fd, bytes) catch return Error.Hangup) orelse continue; - s.cl.wrote(@intCast(sent)); - } - } + try testing.expectEqual(@as(usize, 3), try elements("pane/1/body", &out)); + try testing.expectEqual(@as(usize, 3), try elements("//pane//1//body//", &out)); + try testing.expectEqual(@as(usize, 1), try elements("/index", &out)); - fn settle(s: *Session) Error!ninep.Client.Done { - while (true) { - if (nowMs() >= s.deadline) return Error.Timeout; - try s.flush(); - if (s.cl.take()) |done| { - if (done.tag < 32 and s.unwaited & bit(done.tag) != 0) { - s.unwaited &= ~bit(done.tag); - continue; - } - return done; - } - if (s.cl.dead) return Error.Botch; - const room = s.cl.in.len - s.cl.in_len; - if (room == 0) return Error.Botch; - try s.wait(poll_in); - const got = (transport.read(s.fd, s.stage[0..@min(room, s.stage.len)]) catch return Error.Hangup) orelse continue; - if (got == 0) return Error.Hangup; - const n = s.cl.push(s.stage[0..@intCast(got)]); - std.debug.assert(n == @as(usize, @intCast(got))); - } - } + try testing.expectEqual(@as(usize, 0), try elements("/", &out)); - fn ask(s: *Session, req: ninep.Client.Request, remote: *RemoteError) Error!ninep.Client.Result { - _ = s.cl.submit(req) catch return Error.Botch; - const done = try s.settle(); - if (done.result == .fail) return remote.set(done.result.fail); - if (std.mem.eql(u8, @tagName(done.result), @tagName(std.meta.activeTag(req)))) return done.result; - return Error.Botch; - } + var deep: [8 * max_depth]u8 = @splat('/'); + for (0..max_depth + 1) |i| deep[i * 2 + 1] = 'a'; + try testing.expectError(Error.PathTooDeep, elements(deep[0 .. (max_depth + 1) * 2], &out)); + } - fn drop(s: *Session, fid: u32) void { - _ = s.cl.submit(.{ .clunk = .{ .fid = fid } }) catch return; - _ = s.settle() catch {}; - } + test "a bare dial resolves to the socket --9p binds, and a path is taken as given" { + if (comptime !supported) return error.SkipZigTest; + var buf: [sun_path_len]u8 = undefined; - fn dropNoWait(s: *Session, fid: u32) void { - const tag = s.cl.submit(.{ .clunk = .{ .fid = fid } }) catch return; - s.unwaited |= bit(tag); - s.flush() catch {}; - } + const named = (try resolve(&buf, "work")).unix; + try testing.expect(std.mem.endsWith(u8, named, "/pardes-9p-work.sock")); + var expect: [sun_path_len]u8 = undefined; + var dir_buf: [sun_path_len:0]u8 = undefined; + const dir = socketDir(&dir_buf).?; + try testing.expectEqualStrings(socketPath(&expect, dir, "work").?, named); - fn send(s: *Session, req: ninep.Client.Request) Error!u16 { - return s.cl.submit(req) catch Error.Botch; - } + const path = (try resolve(&buf, "/tmp/somewhere.sock")).unix; + try testing.expectEqualStrings("/tmp/somewhere.sock", path); + try testing.expectEqualStrings("/tmp/somewhere.sock", (try resolve(&buf, "unix!/tmp/somewhere.sock")).unix); - fn newFid(s: *Session) u32 { - const fid = s.next_fid; - s.next_fid = if (fid >= 1 << 20) 1 else fid + 1; - return fid; - } + try testing.expectError(error.BadDial, resolve(&buf, "")); + try testing.expectError(error.BadDial, resolve(&buf, "/tmp/a\x00b")); + } - /// A new connection's Tversion and Tattach, sent with the first - /// operation's requests behind them in one flight (`land`). - fn greet(s: *Session, f: *Flight) Error!void { - if (s.attached) return; - _ = f.add(.version, try s.send(.{ .version = .{} })); - // ponytail: cloud9's client submits nothing while a Tversion is - // out, so its msize is taken as asked until the Rversion lands - // (`land` puts it back); only a pardes is dialled here, and every - // one offers ninep.msize. A Twrite in this flight stays under the - // least a listener offers (`flight_write`). The fix is cloud9's - // client taking requests while it versions. - s.cl.versioning = false; - s.cl.msize = s.cl.asked; - _ = f.add(.attach, try s.send(.{ .attach = .{ .fid = 0, .uname = uname } })); - } + test "a dial with nothing listening is one error and not a wait" { + if (comptime !supported) return error.SkipZigTest; + var names: [max_depth][]const u8 = undefined; + const n = try elements("/pane/1/body", &names); + var remote: RemoteError = .{}; + const before = nowMs(); + try testing.expectError( + Error.Dial, + fetchBytes(testing.allocator, "/tmp/pardes-9p-no-such-socket.sock", names[0..n], &remote, null, "/pane/1/body", limits.max_file_bytes, false), + ); + try testing.expect(nowMs() - before < budget_ms); + } - /// The walk to a new fid at `names` (two Twalks past 16 names, the - /// second from the fid to itself), from the root. - fn walkNew(s: *Session, f: *Flight, names: []const []const u8) Error!u32 { - const fid = s.newFid(); - const first = names[0..@min(names.len, ninep.max_welem)]; - f.add(.walk, try s.send(.{ .walk = .{ .fid = 0, .newfid = fid, .names = first } })).asked = @intCast(first.len); - if (names.len > first.len) - f.add(.walk, try s.send(.{ .walk = .{ .fid = fid, .newfid = fid, .names = names[first.len..] } })).asked = @intCast(names.len - first.len); - return fid; - } + test "one fetch has three msize buffers and bounded transport metadata" { + try testing.expectEqual(@as(usize, msize), @as(usize, (Session{ .fd = -1, .deadline = 0, .display_path = "" }).in.len)); + try testing.expect(@sizeOf(Session) <= 3 * msize + @sizeOf(ninep.Client) + 128 + @sizeOf(@FieldType(Session, "dial_buf"))); + try testing.expect(@sizeOf(ninep.Client) <= 512); + } - /// Sends the flight and takes every answer of it, a read's bytes - /// into `taker` as they come (each valid only until the next), so - /// that the connection is left with nothing out but its clunks. A - /// refused Tversion is its words at once: nothing after it is - /// answered. - fn land(s: *Session, f: *Flight, taker: ?*Taker, remote: *RemoteError) Error!void { - if (f.get(.version) != null) s.cl.versioning = true; - while (f.open()) { - const done = try s.settle(); - const item = f.find(done.tag) orelse return Error.Botch; - item.done = true; - switch (done.result) { - .fail => |words| { - item.failed = true; - item.said.keep(words); - if (item.step == .version) return remote.set(words); - }, - .version => { - if (item.step != .version or s.cl.msize == 0) return Error.Botch; - }, - .attach => |qid| { - if (item.step != .attach) return Error.Botch; - s.root_qid = qid; - s.attached = true; - }, - .walk => |w| { - if (item.step != .walk) return Error.Botch; - item.n = w.nwqid; - if (w.nwqid > 0) item.qid = w.wqid[w.nwqid - 1]; - }, - .stat => |st| { - if (item.step != .stat) return Error.Botch; - item.n = st.length; - }, - .open => |o| { - if (item.step != .open) return Error.Botch; - item.qid = o.qid; - }, - .read => |data| { - if (item.step != .read) return Error.Botch; - item.n = data.len; - if (taker) |t| t.take(item, data); - // A read is timed by its progress, as a long write - // is: each answer gives the rest the budget again, so - // a large file over a slow link is not cut off. - s.deadline = @max(s.deadline, nowMs() +| budget_ms); - }, - .write => |count| { - if (item.step != .write) return Error.Botch; - item.n = count; - }, - else => return Error.Botch, - } - } - } - }; + test "expired sessions do not send or consume buffered protocol work" { + var session: Session = .{ .fd = -1, .deadline = 0, .display_path = "" }; + session.cl = .init(.{ .in = &session.in, .out = &session.out }); + _ = try session.cl.submit(.{ .version = .{} }); + const queued = session.cl.output().len; + try testing.expect(queued > 0); + try testing.expectError(Error.Timeout, session.flush()); + try testing.expectEqual(queued, session.cl.output().len); + try testing.expectError(Error.Timeout, session.settle()); + try testing.expectError(Error.Timeout, session.wait(poll_in)); + } +}; - fn bit(tag: u16) u32 { - return @as(u32, 1) << @intCast(tag); +fn bindSocket(path: [:0]const u8) c_int { + const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM, 0); + if (fd < 0) return fd; + var addr: libc.sockaddr.un = .{ .path = @splat(0) }; + @memcpy(addr.path[0 .. path.len + 1], path[0 .. path.len + 1]); + if (libc.bind(fd, @ptrCast(&addr), @sizeOf(@TypeOf(addr))) != 0) { + _ = libc.close(fd); + return -1; } + return fd; +} - /// The requests of one flight, each its own tag, sent back to back: a - /// pardes takes one connection's requests in order (cloud9's engine - /// starts a request once the one before it is answered or parked), so - /// a walk, the open of its fid and the first read or write need no - /// answer between them, and an operation costs one round trip. - const Flight = struct { - items: [ninep.Client.window]Item = undefined, - len: usize = 0, +test "the socket name is a third prefix in the shared directory" { + var buf: [sun_path_len]u8 = undefined; + const p = socketPath(&buf, "/run/user/1000", "t9srv").?; + try testing.expectEqualStrings("/run/user/1000/pardes-9p-t9srv.sock", p); + try testing.expect(!std.mem.startsWith(u8, std.Io.Dir.path.basename(p), "pardes-detached-")); +} - const Step = enum { version, attach, walk, stat, open, read, write }; - const Item = struct { - step: Step, - tag: u16, - done: bool = false, - failed: bool = false, - said: RemoteError = .{}, - /// walk: names asked; read: its offset and count. - asked: u16 = 0, - off: u64 = 0, - count: u32 = 0, - /// walk: qids answered; stat: the length; read, write: bytes. - n: u64 = 0, - qid: ninep.Qid = undefined, - }; +test "a name that is not one path component is no address at all" { + var buf: [sun_path_len]u8 = undefined; + try testing.expect(socketPath(&buf, "/run", "") == null); + try testing.expect(socketPath(&buf, "/run", "a/b") == null); + try testing.expect(socketPath(&buf, "/run", "a\x00b") == null); +} - fn add(f: *Flight, step: Step, tag: u16) *Item { - f.items[f.len] = .{ .step = step, .tag = tag }; - f.len += 1; - return &f.items[f.len - 1]; - } +test "the startup sweep removes a dead pid's socket and leaves a live pid's, a named one and a plain file" { + if (comptime !supported) return error.SkipZigTest; + var dir_buf: [sun_path_len:0]u8 = undefined; + const base = socketDir(&dir_buf) orelse return error.SkipZigTest; + if (!ensureSocketDir(base)) return error.SkipZigTest; + var sub_buf: [sun_path_len:0]u8 = undefined; + const sub = std.fmt.bufPrintSentinel(&sub_buf, "{s}/deadsweep-{d}", .{ base, @as(u32, @intCast(libc.getpid())) }, 0) catch return error.SkipZigTest; + if (libc.mkdir(sub, 0o700) != 0) return error.SkipZigTest; + var paths: [4][sun_path_len:0]u8 = undefined; + // No process has this pid: kill(pid, 0) answers ESRCH. + const dead = try std.fmt.bufPrintSentinel(&paths[0], "{s}/" ++ prefix ++ "2147483647.sock", .{sub}, 0); + const live = try std.fmt.bufPrintSentinel(&paths[1], "{s}/" ++ prefix ++ "{d}.sock", .{ sub, @as(u32, @intCast(std.c.getppid())) }, 0); + const named = try std.fmt.bufPrintSentinel(&paths[2], "{s}/" ++ prefix ++ "work.sock", .{sub}, 0); + const plain = try std.fmt.bufPrintSentinel(&paths[3], "{s}/" ++ prefix ++ "2147483646.sock", .{sub}, 0); + defer { + for ([_][:0]const u8{ dead, live, named, plain }) |p| _ = libc.unlink(p); + _ = libc.rmdir(sub); + } + for ([_][:0]const u8{ dead, live, named }) |p| { + const fd = bindSocket(p); + if (fd < 0) return error.SkipZigTest; + _ = libc.close(fd); // closed: what a killed editor leaves + } + const fd = libc.open(plain, .{ .CREAT = true, .ACCMODE = .WRONLY }, @as(libc.mode_t, 0o600)); + if (fd < 0) return error.SkipZigTest; + _ = libc.close(fd); + sweepDeadSockets(testing.io, sub); + try testing.expect(statNoFollow(dead) == null); + try testing.expect(statNoFollow(live) != null); + try testing.expect(statNoFollow(named) != null); + try testing.expect(statNoFollow(plain) != null); +} - fn find(f: *Flight, tag: u16) ?*Item { - for (f.items[0..f.len]) |*item| if (item.tag == tag and !item.done) return item; - return null; - } +test "the registry sweep takes the dead entries and leaves everything else" { + if (comptime !supported) return error.SkipZigTest; + // Under the real runtime directory, because a sun_path is 108 bytes + // and the test cache's temporary directories are longer than that. + var dir_buf: [sun_path_len:0]u8 = undefined; + const base = socketDir(&dir_buf) orelse return error.SkipZigTest; + if (!ensureSocketDir(base)) return error.SkipZigTest; - fn get(f: *Flight, step: Step) ?*Item { - for (f.items[0..f.len]) |*item| if (item.step == step) return item; - return null; - } + var svc_buf: [sun_path_len:0]u8 = undefined; + const svc = std.fmt.bufPrintSentinel(&svc_buf, "{s}/sweep-{d}", .{ base, @as(u32, @intCast(libc.getpid())) }, 0) catch + return error.SkipZigTest; + if (libc.mkdir(svc, 0o700) != 0) return error.SkipZigTest; - fn open(f: *const Flight) bool { - for (f.items[0..f.len]) |item| if (!item.done) return true; - return false; - } + // The sockets live where the real ones do -- beside the registry, not in + // it, and named the way a session names them -- because the sweep only + // follows an entry to a target of exactly that shape and place. + var paths: [6][sun_path_len:0]u8 = undefined; + const pid: u32 = @intCast(libc.getpid()); + const live_sock = try std.fmt.bufPrintSentinel(&paths[0], "{s}/pardes-9p-sweeplive-{d}.sock", .{ base, pid }, 0); + const dead_sock = try std.fmt.bufPrintSentinel(&paths[1], "{s}/pardes-9p-sweepdead-{d}.sock", .{ base, pid }, 0); + const live = try std.fmt.bufPrintSentinel(&paths[2], "{s}/live", .{svc}, 0); + const dead = try std.fmt.bufPrintSentinel(&paths[3], "{s}/dead", .{svc}, 0); + const stranger = try std.fmt.bufPrintSentinel(&paths[4], "{s}/stranger", .{svc}, 0); + // A dead entry pointing at something that is NOT one of our sockets: the + // entry goes, the file it named must not. + const outsider_sock = try std.fmt.bufPrintSentinel(&paths[5], "{s}/sweep-outsider-{d}.sock", .{ base, pid }, 0); + var outsider_buf: [sun_path_len:0]u8 = undefined; + const outsider = try std.fmt.bufPrintSentinel(&outsider_buf, "{s}/outsider", .{svc}, 0); + // Live, its lock held, but bound and not listening: a connect is + // refused, on every system, as FreeBSD refuses one to a live session + // whose queue is full. Its lock is what says it lives. + var starting_paths: [2][sun_path_len:0]u8 = undefined; + const starting_sock = try std.fmt.bufPrintSentinel(&starting_paths[0], "{s}/pardes-9p-sweepstarting-{d}.sock", .{ base, pid }, 0); + const starting = try std.fmt.bufPrintSentinel(&starting_paths[1], "{s}/starting", .{svc}, 0); + var locks: [3][lock_path_len:0]u8 = undefined; + const dead_lock = lockPath(&locks[2], dead_sock).?; + defer { + for ([_][:0]const u8{ live_sock, dead_sock, live, dead, stranger, outsider_sock, outsider, starting_sock, starting, dead_lock }) |p| _ = libc.unlink(p); + _ = libc.rmdir(svc); + } - /// The first refusal in the order asked: a version's or an attach's - /// words, a walk short of its names, an open's refusal. - fn check(f: *Flight, remote: *RemoteError) Error!void { - for (f.items[0..f.len]) |*item| switch (item.step) { - .version, .attach, .open => if (item.failed) return remote.set(item.said.buf[0..item.said.len]), - .walk => { - if (item.failed) return remote.set(item.said.buf[0..item.said.len]); - if (item.n != item.asked) return Error.NotFound; - }, - .stat, .read, .write => {}, - }; - } + const live_lock = claim(live_sock, &locks[0]) orelse return error.LockFailed; + defer release(live_lock, std.mem.sliceTo(&locks[0], 0)); + const listening = bindSocket(live_sock); + try testing.expect(listening >= 0); + defer _ = libc.close(listening); + try testing.expectEqual(@as(c_int, 0), libc.listen(listening, 1)); + + const starting_lock = claim(starting_sock, &locks[1]) orelse return error.LockFailed; + defer release(starting_lock, std.mem.sliceTo(&locks[1], 0)); + const bound = bindSocket(starting_sock); + try testing.expect(bound >= 0); + defer _ = libc.close(bound); + try testing.expectEqual(@as(c_int, 0), libc.symlink(starting_sock, starting)); + + // Bound and then dropped, its lock let go: the files stay, and nobody + // answers it -- exactly what a killed session leaves behind. + const abandoned = bindSocket(dead_sock); + try testing.expect(abandoned >= 0); + _ = libc.close(abandoned); + const dropped = libc.open(dead_lock, .{ .ACCMODE = .RDWR, .CREAT = true, .CLOEXEC = true }, @as(libc.mode_t, 0o600)); + try testing.expect(dropped >= 0); + try testing.expectEqual(@as(c_int, 0), libc.flock(dropped, std.posix.LOCK.EX)); + _ = libc.close(dropped); - /// The walked file's qid: the last walk's, or the root's for none. - fn qid(f: *Flight, root: ninep.Qid) ninep.Qid { - var at = root; - for (f.items[0..f.len]) |item| if (item.step == .walk and item.n > 0) { - at = item.qid; - }; - return at; - } + try testing.expectEqual(@as(c_int, 0), libc.symlink(live_sock, live)); + try testing.expectEqual(@as(c_int, 0), libc.symlink(dead_sock, dead)); + // Dead too, but its target is not one of our sockets by name, so the + // entry must go and the file it named must survive untouched. + const outside = bindSocket(outsider_sock); + try testing.expect(outside >= 0); + defer _ = libc.close(outside); + try testing.expectEqual(@as(c_int, 0), libc.symlink(outsider_sock, outsider)); + // Not a symlink, so not this program's to reason about, even though + // connecting to it is refused exactly like the dead socket. + try std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = stranger, .data = "" }); - /// Whether the walk made the fid, so that it is owed a clunk. - fn made(f: *Flight) bool { - const first = f.get(.walk) orelse return false; - return !first.failed and first.n == first.asked; - } - }; + try testing.expectEqual(Probe.live, probe(live)); + try testing.expectEqual(Probe.stale, probe(dead)); - /// A write in a new connection's first flight is sent before the msize - /// is known: no larger than the least a pardes listener offers. - const flight_write = ninep.min_msize - 23; + sweepRegistry(testing.io, svc, base); - /// Reads ahead this many Treads at once (the window, less a clunk - /// still out and one spare). - const read_ahead = ninep.Client.window - 2; + try testing.expect(statNoFollow(dead) == null); + try testing.expect(statNoFollow(dead_sock) == null); + try testing.expect(statNoFollow(dead_lock) == null); + try testing.expect(statNoFollow(starting) != null); + try testing.expect(statNoFollow(starting_sock) != null); + try testing.expect(statNoFollow(live) != null); + try testing.expect(statNoFollow(live_sock) != null); + try testing.expect(statNoFollow(stranger) != null); + try testing.expect(statNoFollow(outsider) == null); + try testing.expect(statNoFollow(outsider_sock) != null); + try testing.expectEqual(Probe.live, probe(live)); +} - /// Version and attach on a new connection, and a walk to `names`, in - /// one flight: the fid there. - fn walkTo(s: *Session, names: []const []const u8, remote: *RemoteError) !u32 { - var f: Flight = .{}; - try s.greet(&f); - const fid = try s.walkNew(&f, names); - try s.land(&f, null, remote); - f.check(remote) catch |err| { - if (f.made()) s.dropNoWait(fid); - return err; - }; - return fid; - } +test "TCP addresses are numeric and normalize mapped IPv4" { + const loopback = try networkAddress("tcp!127.0.0.1!5640", false); + try testing.expectEqualDeep(loopback, try networkAddress("tcp!::ffff:127.0.0.1!5640", false)); + try testing.expectEqualDeep(loopback, try networkAddress("tcp!::ffff:7f00:1!5640", false)); + try testing.expectEqualDeep(try networkAddress("tcp!::1!5640", false), try networkAddress("tcp!0:0:0:0:0:0:0:1!5640", false)); + try testing.expectEqual(@as(u16, 0), (try networkAddress("tcp!127.0.0.1!0", true)).getPort()); + for ([_][]const u8{ "tcp!localhost!5640", "tcp!127.0.0.1!0", "tcp!127.0.0.1!-1", "tcp!127.0.0.1!65536", "tcp!127.0.0.1!", "tcp!!5640" }) |dial| + try testing.expectError(error.BadDial, networkAddress(dial, false)); + try Client.validateDial("tcp!127.0.0.1!5640"); + try Client.validateDial("/tmp/pardes-owned.sock"); + try Client.validateDial("unix!/tmp/pardes-owned.sock"); + try testing.expectError(error.BadDial, Client.validateDial("unix!work")); + try testing.expectError(error.BadDial, Client.validateDial("nowhere!x")); + try Client.validateDial("/tmp/odd!name.sock"); + try testing.expectError(error.BadDial, Client.validateDial("rel/dir.sock")); + try testing.expectError(error.BadDial, Client.validateDial("unix!")); + try testing.expectError(error.BadDial, Client.validateDial("unix!/tmp/a\x00b")); + try testing.expectError(error.BadDial, Client.validateDial("tcp!localhost!5640")); + try testing.expectError(error.BadDial, Client.validateDial("/tmp/a\x00b")); + try testing.expectError(error.BadDial, Client.validateDial("quic!127.0.0.1!5640")); +} - /// A read's bytes taken in order: a file's as they are, a directory's - /// entries as rows of names. A read ahead that came back out of order, - /// or past one that came back short, is not taken: what it held is - /// read again from where the taken bytes end. - const Taker = struct { - out: *std.Io.Writer.Allocating, - display_path: []const u8, - limit: usize, - /// The flight with the walk, whose answer says what was reached: - /// it lands before any read (a walk never parks). - flight: *Flight, - s: *const Session, - /// Bytes taken from the start; a directory's next offset. - end: u64 = 0, - /// The read that ended at `end` came back short, or empty (the end). - short: bool = false, - empty: bool = false, - /// What taking said (too large, a bad entry): the operation's - /// answer once every read out has landed. - err: ?Error = null, +test "a runtime directory that makes the socket's path pass sun_path is refused in words, the editor running on" { + if (comptime !supported) return error.SkipZigTest; + const p = try pardes.Pardes.init(testing.allocator, .{ .tty_only = true }); + defer p.deinit(); + var rt: TempRuntime = .{}; + try rt.init("words"); + defer rt.deinit(); + var buf: [256]u8 = undefined; + try testing.expect(noSocket("x", &buf) == null); + // A directory of 91 bytes (the ledger's was 83, a pid's digits from the + // edge): with a 1-byte name and a pid the socket's path passes sun_path. + var long_buf: [128:0]u8 = undefined; + const long = try std.fmt.bufPrintSentinel(&long_buf, "{s}/{s}", .{ std.mem.sliceTo(&rt.directory, 0), &@as([66]u8, @splat('d')) }, 0); + try testing.expectEqual(@as(usize, 91), long.len); + try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", long, 1)); + const words = noSocket("x", &buf) orelse return error.NotRefused; + try testing.expect(std.mem.startsWith(u8, words, "9p: the socket path is ")); + try testing.expect(std.mem.endsWith(u8, words, " bytes, 108 is the most: set XDG_RUNTIME_DIR shorter")); + // The session's own words, where its pane shows them; no listener. + try testing.expect(start(testing.io, testing.allocator, p) == null); + const pane = p.panes[0].?; + try testing.expect(std.mem.startsWith(u8, pane.msg[0..pane.msg_len], "9p: the socket path is ")); +} - fn take(t: *Taker, item: *Flight.Item, data: []const u8) void { - if (t.err != null or item.off != t.end or t.short) return; - t.put(data) catch |err| { - t.err = err; - return; - }; - t.end += data.len; - t.short = data.len < item.count; - t.empty = data.len == 0; - } +test "a runtime directory open to others is refused in words, never a silent session with no socket" { + if (comptime !supported) return error.SkipZigTest; + const p = try pardes.Pardes.init(testing.allocator, .{ .tty_only = true }); + defer p.deinit(); + var rt: TempRuntime = .{}; + try rt.init("open"); + defer rt.deinit(); + // `mkdir -m 700 -p dir/sub` leaves dir itself at the umask's 755. + try testing.expectEqual(@as(c_int, 0), libc.chmod(&rt.directory, 0o755)); + var buf: [256]u8 = undefined; + const words = noSocket("x", &buf) orelse return error.NotRefused; + try testing.expect(std.mem.indexOf(u8, words, "must be a directory of yours alone (mode 755") != null); + try testing.expect(start(testing.io, testing.allocator, p) == null); + const pane = p.panes[0].?; + try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "chmod 700 it") != null); + try testing.expectEqual(@as(c_int, 0), libc.chmod(&rt.directory, 0o700)); + try testing.expect(noSocket("x", &buf) == null); +} - fn directory(t: *const Taker) bool { - return t.flight.qid(t.s.root_qid).type & ninep.qtdir != 0; - } +test "a socket path too long for sun_path is no listener, and leaves the editor's turn as it was" { + if (comptime !supported) return error.SkipZigTest; + const p = try pardes.Pardes.init(testing.allocator, .{ .tty_only = true }); + defer p.deinit(); + var rt: TempRuntime = .{}; + try rt.init("long"); + defer rt.deinit(); + // A name that fits: the directory is one a listener takes. + var fits: [sun_path_len]u8 = undefined; + try testing.expect(socketPath(&fits, std.mem.sliceTo(&rt.directory, 0), "short") != null); + const long = &@as([120]u8, @splat('n')); + try testing.expectError(error.NameTooLong, listen(testing.io, testing.allocator, p, long, "", null)); + try testing.expect(pardes.turn.answer_held == null); + try testing.expect(pardes.turn.wake_parked == null); + try testing.expect(pardes.turn.io == null); +} - fn maxBytes(t: *const Taker) u64 { - return @min(t.limit, @as(usize, if (t.directory()) limits.max_stream_bytes else limits.max_file_bytes)); - } +test "a listening editor posts itself into the 9P registry and unposts on stop" { + if (comptime !supported) return error.SkipZigTest; + const gpa = testing.allocator; + var rt: TempRuntime = .{}; + try rt.init("post"); + defer rt.deinit(); + const dir = std.mem.sliceTo(&rt.directory, 0); - fn wireLimit(t: *const Taker) u64 { - return if (t.directory()) limits.max_stream_bytes else t.maxBytes(); - } + var entry_buf: [sun_path_len:0]u8 = undefined; + const entry = try std.fmt.bufPrintSentinel(&entry_buf, "{s}/9p/pardes/unit", .{dir}, 0); + var sock_buf: [sun_path_len:0]u8 = undefined; + const sock = try std.fmt.bufPrintSentinel(&sock_buf, "{s}/" ++ prefix ++ "unit.sock", .{dir}, 0); - fn put(t: *Taker, data: []const u8) Error!void { - if (t.end + data.len > t.wireLimit()) return Error.FileTooLarge; - if (t.directory()) { - var pos: usize = 0; - while (pos < data.len) { - if (data.len - pos < 2) return Error.Botch; - const len: usize = 2 + @as(usize, std.mem.readInt(u16, data[pos..][0..2], .little)); - if (len > data.len - pos) return Error.Botch; - const entry = ninep.Stat.decode(data[pos..][0..len]) catch return Error.Botch; - const display_dir = std.mem.trimEnd(u8, t.display_path, "/"); - const row_len = display_dir.len + entry.name.len + 2 + @as(usize, @intFromBool(entry.qid.type & ninep.qtdir != 0)); - if (row_len > t.maxBytes() - t.out.written().len) return Error.FileTooLarge; - t.out.writer.print("{s}/{s}", .{ display_dir, entry.name }) catch return Error.Botch; - if (entry.qid.type & ninep.qtdir != 0) t.out.writer.writeByte('/') catch return Error.Botch; - t.out.writer.writeByte('\n') catch return Error.Botch; - pos += len; - } - } else t.out.writer.writeAll(data) catch return Error.Botch; - } - }; + var link: [sun_path_len]u8 = undefined; + { + const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); + defer p.deinit(); + const l = try listen(testing.io, gpa, p, "unit", "", null); + defer l.deinit(gpa); + // The socket stays exactly where pardes has always bound it: + // adopting the registry moves nothing, it only advertises. + try testing.expect(statNoFollow(sock) != null); + // It appeared already listening, renamed into place from a name of + // its own, which is gone; its lock held all the while. + try testing.expect(transport.isListening(sock)); + try testing.expectEqual(Probe.live, lockState(sock)); + var tmp_buf: [sun_path_len:0]u8 = undefined; + try testing.expect(statNoFollow(try std.fmt.bufPrintSentinel(&tmp_buf, "{s}.{d}", .{ sock, libc.getpid() }, 0)) == null); + // And the registry holds a symlink to it one directory down, so + // several editors group under /mnt/9p/pardes/ instead of + // crowding the registry root — the layout zmx posts its + // sessions in. + const n = libc.readlink(entry, &link, link.len); + try testing.expect(n > 0); + try testing.expectEqualStrings(sock, link[0..@intCast(n)]); + // A host path no fid names is swept, but never while a request is + // out in a syscall: it may be that request's, mid-walk. + try p.fs.os_paths.append(gpa, .{ .node = @import("fs.zig").os_node | 1, .path = try gpa.dupe(u8, "/tmp") }); + pardes.turn.out = 1; + l.collectOs(); + pardes.turn.out = 0; + try testing.expectEqual(@as(usize, 1), p.fs.os_paths.items.len); + l.collectOs(); + try testing.expectEqual(@as(usize, 0), p.fs.os_paths.items.len); + } + // Stopping takes the name back out, so the next editor of that name + // is not refused by its own corpse; the lock goes with the socket. + try testing.expect(libc.readlink(entry, &link, link.len) < 0); + try testing.expectEqual(Probe.none, lockState(sock)); +} + +test "Unix and TCP share one listener through reads writes reconnects and reset" { + if (comptime !supported) return error.SkipZigTest; + const gpa = testing.allocator; + var rt: TempRuntime = .{}; + try rt.init("tcp"); + defer rt.deinit(); + const replacement = try gpa.alloc(u8, 3 * msize + 27); + defer gpa.free(replacement); + @memset(replacement, 'x'); + @memcpy(replacement[0.."changed café λ\n".len], "changed café λ\n"); + replacement[replacement.len - 1] = '\n'; - /// One operation in as few round trips as its answers allow: the walk, - /// a read's stat, the open and the first read or write go in one flight - /// (behind a new connection's version and attach). A file of the - /// length its stat said is whole when a read comes back short at that - /// length; a longer one is read ahead, the Treads for the rest sent at - /// once. Anything else (a directory, a file of no stated length) reads - /// on until a read comes back empty, the only end 9P has. - fn transact( - s: *Session, - names: []const []const u8, - out: *std.Io.Writer.Allocating, - remote: *RemoteError, - write_bytes: ?[]const u8, - read_limit: usize, - ) !void { - const writing = write_bytes != null; - const mode: u8 = if (writing) ninep.owrite else ninep.oread; - const truncate = writing and names.len > 0 and - (std.mem.eql(u8, names[0], "os") or std.mem.eql(u8, names[names.len - 1], "body")); - const fresh = !s.attached; - var f: Flight = .{}; - try s.greet(&f); - const fid = try s.walkNew(&f, names); - if (!writing) _ = f.add(.stat, try s.send(.{ .stat = .{ .fid = fid } })); - _ = f.add(.open, try s.send(.{ .open = .{ .fid = fid, .mode = mode | if (truncate) ninep.otrunc else 0 } })); - const chunk_max: usize = if (fresh) flight_write else s.cl.maxWrite(); - const first: []const u8 = if (write_bytes) |bytes| bytes[0..@min(bytes.len, chunk_max)] else ""; - if (writing and first.len != 0) { - // Out of the client's buffer first (it holds one msize): still - // sent without waiting. - try s.flush(); - _ = f.add(.write, try s.send(.{ .write = .{ .fid = fid, .offset = 0, .data = first } })); - } - // A read's count before the msize is known is the asked msize's. - if (!writing) { - const item = f.add(.read, try s.send(.{ .read = .{ .fid = fid, .offset = 0, .count = s.cl.maxRead() } })); - item.count = s.cl.maxRead(); - } - var taker: Taker = .{ .out = out, .display_path = s.display_path, .limit = read_limit, .flight = &f, .s = s }; - try s.land(&f, &taker, remote); - defer if (f.made()) s.dropNoWait(fid); - try f.check(remote); - if (taker.err) |err| return err; - const directory = taker.directory(); + const Worker = struct { + dial: []const u8, + body_path: []const u8, + expected: []const u8, + replacement: []const u8, + done: std.atomic.Value(bool) = .init(false), + failure: ?anyerror = null, - if (write_bytes) |bytes| { - if (directory) return Error.IsDirectory; - var written: usize = 0; - if (f.get(.write)) |w| { - // Refused unopened, the open parked (a truncating one while - // the editor is out): sent again below, after it. - if (w.failed and !std.mem.eql(u8, w.said.buf[0..w.said.len], ninep.fs.e_bad_use)) return remote.set(w.said.buf[0..w.said.len]); - if (!w.failed) { - if (w.n == 0 or w.n > first.len) return Error.Botch; - written = w.n; - } - } - while (written < bytes.len) { - s.deadline = nowMs() +| write_budget_ms; - const chunk = bytes[written..][0..@min(bytes.len - written, s.cl.maxWrite())]; - const count = (try s.ask(.{ .write = .{ .fid = fid, .offset = written, .data = chunk } }, remote)).write; - if (count == 0 or count > chunk.len) return Error.Botch; - written += count; - // A long write is timed by its progress: each answer gives - // the next its own budget, so a large body (`pardes -`) goes - // in through one open, whole, however long it takes. The - // budget is a write's: a server putting in tens of MiB it - // held (pane.zig batchFor) answers that one write late. - } - return; + fn run(w: *@This()) void { + defer w.done.store(true, .release); + w.check() catch |err| { + w.failure = err; + }; } - if (f.get(.read)) |r| if (r.failed and !std.mem.eql(u8, r.said.buf[0..r.said.len], ninep.fs.e_bad_use)) - return remote.set(r.said.buf[0..r.said.len]); - const length: u64 = if (f.get(.stat)) |st| (if (st.failed or directory) 0 else st.n) else 0; - const step: u64 = s.cl.maxRead(); - while (true) { - if (taker.empty) return; - if (taker.short and length != 0 and taker.end == length) return; - // The rest of a file of known length at once; a directory, a - // file of none, or the end to be seen, a read at a time. - // ponytail: a file of no stated length is not read ahead. - const want: u64 = if (directory or taker.short or length <= taker.end) 1 else @min(read_ahead, (length - taker.end) / step + 1); - var batch: Flight = .{}; - var off = taker.end; - for (0..want) |_| { - if (off > taker.wireLimit()) break; - const count: u32 = @intCast(@min(step, taker.wireLimit() + 1 - off)); - const item = batch.add(.read, try s.send(.{ .read = .{ .fid = fid, .offset = off, .count = count } })); - item.off = off; - item.count = count; - off += count; - } - taker.short = false; - try s.land(&batch, &taker, remote); - if (taker.err) |err| return err; - for (batch.items[0..batch.len]) |item| if (item.failed and item.off == taker.end and !taker.short) - return remote.set(item.said.buf[0..item.said.len]); + fn check(w: *@This()) !void { + const before = try Client.readLimit(testing.allocator, w.dial, w.body_path, w.body_path, w.expected.len); + defer testing.allocator.free(before); + try testing.expectEqualStrings(w.expected, before); + try testing.expectError(error.FileTooLarge, Client.readLimit(testing.allocator, w.dial, w.body_path, w.body_path, w.expected.len - 1)); + const listing = try Client.readLimit(testing.allocator, w.dial, "/pane", "/pane", 128); + defer testing.allocator.free(listing); + const exact_listing = try Client.readLimit(testing.allocator, w.dial, "/pane", "/pane", listing.len); + defer testing.allocator.free(exact_listing); + try testing.expectEqualStrings(listing, exact_listing); + try testing.expectError(error.FileTooLarge, Client.readLimit(testing.allocator, w.dial, "/pane", "/pane", listing.len - 1)); + try Client.write(testing.allocator, w.dial, w.body_path, w.replacement); + const after = try Client.read(testing.allocator, w.dial, w.body_path, w.body_path); + defer testing.allocator.free(after); + try testing.expectEqualStrings(w.replacement, after); + const screen = try Client.read(testing.allocator, w.dial, "/screen", "/screen"); + defer testing.allocator.free(screen); + const parsed = try std.json.parseFromSlice(struct { cols: u16, rows: u16 }, testing.allocator, screen, .{ .ignore_unknown_fields = true }); + defer parsed.deinit(); + try testing.expectEqual(@as(u16, 40), parsed.value.cols); + try testing.expectEqual(@as(u16, 12), parsed.value.rows); } - } + }; - fn fetchBytes( - gpa: std.mem.Allocator, - dial: []const u8, - names: []const []const u8, - remote: *RemoteError, - write_bytes: ?[]const u8, - display_path: []const u8, - read_limit: usize, - keep: bool, - ) ![]u8 { - if (comptime !supported) return Error.Dial; - if (keep) if (kept.take(dial)) |s| { - if (fetchOn(s, gpa, names, remote, write_bytes, read_limit, display_path, true)) |bytes| return bytes else |err| switch (err) { - // Let go of by the peer since (its hangup still on the way): - // a read is asked again on a new connection. A write is not, - // as it may have been done. - error.Hangup, error.Botch => if (write_bytes != null) return err, - else => return err, - } - }; - return fetchOn(try startSession(dial, display_path), gpa, names, remote, write_bytes, read_limit, display_path, keep); + for ([_][]const u8{ "127.0.0.1", "::1" }) |host| { + const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); + defer p.deinit(); + const pane = try p.setTestFile("initial\n"); + while (p.nextEffect()) |_| {} + var bind_buf: [64]u8 = undefined; + const bind = try std.fmt.bufPrint(&bind_buf, "tcp!{s}!0", .{host}); + const l = try listen(testing.io, gpa, p, "roundtrip", "", bind); + defer { + l.reset(p); + l.deinit(gpa); + } + try testing.expect(l.path().len != 0); + try testing.expectEqual(@as(usize, max_conns), l.runner.conns.len); + const port = l.tcp_address.?.getPort(); + try testing.expect(port != 0); + var dial_buf: [64]u8 = undefined; + const network_dial = try std.fmt.bufPrint(&dial_buf, "tcp!{s}!{d}", .{ host, port }); + var body_buf: [64]u8 = undefined; + const body = try std.fmt.bufPrint(&body_buf, "/pane/{d}/body", .{pane.serial}); + for ([_][]const u8{ network_dial, l.path(), network_dial }, 0..) |dial, attempt| { + var worker: Worker = .{ .dial = dial, .body_path = body, .expected = if (attempt == 0) "initial\n" else replacement, .replacement = replacement }; + const thread = try std.Thread.spawn(.{}, Worker.run, .{&worker}); + defer thread.join(); + // The editor rests while the client works: its requests are + // answered on the runner's tasks, not by this thread. + const deadline = nowMs() + 3 * Client.budget_ms; + pardes.turn.rest(); + while (!worker.done.load(.acquire) and nowMs() < deadline) nap(1); + // The client has hung up, but its connection's task may not have + // seen that yet. The releases it then owes are refused once + // `reset` starts cutting (they name the old editor's opens), and + // this test's "replacement" is the same editor, so they would + // stay in `p.fs.opens`. Let the hangup pay them first, resting + // so the task can take the turn: that is what is under test here. + const settle_by = nowMs() + 3 * Client.budget_ms; + while (l.runner.count() != 0 and nowMs() < settle_by) nap(1); + pardes.turn.wake(); + try testing.expect(worker.done.load(.acquire)); + if (worker.failure) |err| return err; + try testing.expectEqual(@as(usize, 0), l.runner.count()); + l.reset(p); + try testing.expectEqual(@as(usize, 0), l.runner.count()); + for (p.fs.opens) |o| try testing.expect(o.node == 0); + } } +} - fn fetchOn( - s: *Session, - gpa: std.mem.Allocator, - names: []const []const u8, - remote: *RemoteError, - write_bytes: ?[]const u8, - read_limit: usize, - display_path: []const u8, - keep: bool, - ) ![]u8 { - s.deadline = nowMs() +| budget_ms; - s.display_path = display_path; - var clean = false; - defer endSession(s, keep and clean); - var out: std.Io.Writer.Allocating = .init(gpa); - errdefer out.deinit(); - transact(s, names, &out, remote, write_bytes, read_limit) catch |err| { - // Refused with every answer in: the connection is as good as before. - clean = switch (err) { - error.Remote, error.NotFound, error.IsDirectory, error.FileTooLarge => true, - else => false, - }; - return err; - }; - clean = true; - return out.toOwnedSlice(); - } +test "a change waits while the editor is out mid-step, a read does not, and the change lands when the editor rests" { + if (comptime !supported) return error.SkipZigTest; + const gpa = testing.allocator; + var rt: TempRuntime = .{}; + try rt.init("park"); + defer rt.deinit(); - /// A new connection at `dial` whose requests have the usual deadline. - fn startSession(dial: []const u8, display_path: []const u8) !*Session { - var sock_buf: [sun_path_len]u8 = undefined; - const sock = try resolve(&sock_buf, dial); - const deadline = nowMs() +| budget_ms; - // Not the caller's allocator: a kept one outlives the call (an arena's). - const s = try std.heap.page_allocator.create(Session); - s.* = .{ .fd = -1, .deadline = deadline, .display_path = display_path }; - errdefer endSession(s, false); - s.fd = try connect(sock, deadline); - s.cl = .init(.{ .in = &s.in, .out = &s.out }); - if (dial.len <= s.dial_buf.len) { - @memcpy(s.dial_buf[0..dial.len], dial); - s.dial_len = dial.len; - } - return s; + const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); + defer p.deinit(); + const pane = try p.setTestFile("before\n"); + while (p.nextEffect()) |_| {} + // This thread is the editor's: it has the turn from here on. + const l = try listen(testing.io, gpa, p, "park", "", null); + defer { + l.reset(p); + l.deinit(gpa); } + var body_buf: [64]u8 = undefined; + const body = try std.fmt.bufPrint(&body_buf, "/pane/{d}/body", .{pane.serial}); - fn endSession(s: *Session, keep: bool) void { - if (keep and s.attached and s.dial_len != 0 and kept.put(s)) return; - if (s.fd >= 0) _ = libc.close(s.fd); - std.heap.page_allocator.destroy(s); - } + const Writer = struct { + dial: []const u8, + path: []const u8, + done: std.atomic.Value(bool) = .init(false), + failure: ?anyerror = null, + fn run(w: *@This()) void { + defer w.done.store(true, .release); + Client.write(testing.allocator, w.dial, w.path, "after\n") catch |err| { + w.failure = err; + }; + } + }; + var writer: Writer = .{ .dial = l.path(), .path = body }; - /// Connections kept open between operations, a few dials' (a mount's, - /// as Plan 9's mount keeps its channel): each costs the peer one of its - /// `max_conns` while kept, so only mounts and the Mount probe keep one. - const kept = struct { - const max = 8; - var lock: std.atomic.Mutex = .unlocked; - var slots: [max]?*Session = @splat(null); + // The editor goes out mid-step -- a Look resolving a path, say -- and a + // write arrives meanwhile. It must not land: the step still holds + // pointers into the pane. A read is answered all the same. + pardes.turn.yield(); + const thread = try std.Thread.spawn(.{}, Writer.run, .{&writer}); + defer thread.join(); + nap(150); + try testing.expect(!writer.done.load(.acquire)); + const seen = try Client.readLimit(gpa, l.path(), body, body, 64); + defer gpa.free(seen); + try testing.expectEqualStrings("before\n", seen); + try testing.expect(!writer.done.load(.acquire)); + pardes.turn.back(); + try testing.expectEqualStrings("before\n", pane.file.?.content); - fn hold() void { - while (!lock.tryLock()) std.atomic.spinLoopHint(); - } + // Resting is what lets it in: the parked write is retried, served, and + // the client hears back. + pardes.turn.rest(); + const deadline = nowMs() + Client.budget_ms; + while (!writer.done.load(.acquire) and nowMs() < deadline) nap(1); + pardes.turn.wake(); + try testing.expect(writer.done.load(.acquire)); + if (writer.failure) |err| return err; + // Its bytes are held until its open is let go (pane.zig `batchFor`), and + // the client lets it go without waiting: rested again until that lands, + // the body read only while the editor has the turn. + while (!std.mem.eql(u8, pane.file.?.content, "after\n") and nowMs() < deadline) { + pardes.turn.rest(); + nap(1); + pardes.turn.wake(); + } + try testing.expectEqualStrings("after\n", pane.file.?.content); +} - /// The session kept for `dial`, out of the table, if it is still - /// there: what came while it waited (a clunk's answer) taken, and - /// a hangup or anything else seen as its end. - fn take(dial: []const u8) ?*Session { - hold(); - const s = for (&slots) |*slot| { - const got = slot.* orelse continue; - if (std.mem.eql(u8, got.dial_buf[0..got.dial_len], dial)) { - slot.* = null; - break got; - } - } else null; - lock.unlock(); - const found = s orelse return null; - if (usable(found)) return found; - endSession(found, false); - return null; - } +test "a mount's connection is kept between operations, and one the peer hung up is dialled again" { + if (comptime !supported) return error.SkipZigTest; + const gpa = testing.allocator; + var rt: TempRuntime = .{}; + try rt.init("kept"); + defer rt.deinit(); - fn usable(s: *Session) bool { - while (true) { - var fds = [1]libc.pollfd{.{ .fd = s.fd, .events = poll_in, .revents = 0 }}; - if (libc.poll(&fds, 1, 0) < 0) return false; - if (fds[0].revents == 0) return true; - if (fds[0].revents & poll_in == 0) return false; - const room = s.cl.in.len - s.cl.in_len; - if (room == 0) return false; - const got = (transport.read(s.fd, s.stage[0..@min(room, s.stage.len)]) catch return false) orelse return true; - if (got == 0) return false; - _ = s.cl.push(s.stage[0..got]); - while (s.cl.take()) |done| { - if (done.tag >= 32 or s.unwaited & bit(done.tag) == 0) return false; - s.unwaited &= ~bit(done.tag); - } - if (s.cl.dead) return false; - } - } + const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); + defer p.deinit(); + const pane = try p.setTestFile("before\n"); + while (p.nextEffect()) |_| {} + const l = try listen(testing.io, gpa, p, "kept", "", null); + defer { + l.reset(p); + l.deinit(gpa); + } + var body_buf: [64]u8 = undefined; + const body = try std.fmt.bufPrint(&body_buf, "/pane/{d}/body", .{pane.serial}); + const long = try gpa.alloc(u8, 2 * msize + 5); + defer gpa.free(long); + @memset(long, 'k'); + long[long.len - 1] = '\n'; - fn put(s: *Session) bool { - hold(); - defer lock.unlock(); - for (&slots) |*slot| if (slot.* == null) { - slot.* = s; - return true; + const Worker = struct { + l: *Listener, + body: []const u8, + long: []const u8, + done: std.atomic.Value(bool) = .init(false), + failure: ?anyerror = null, + fn run(w: *@This()) void { + defer w.done.store(true, .release); + w.check() catch |err| { + w.failure = err; }; - return false; + } + fn read(w: *@This(), want: []const u8) !void { + const got = try Client.readMount(testing.allocator, w.l.path(), w.body, w.body, limits.max_file_bytes); + defer testing.allocator.free(got); + try testing.expectEqualStrings(want, got); + } + fn check(w: *@This()) !void { + defer Client.forget(w.l.path()); + try w.read("before\n"); + try w.read("before\n"); + // A write longer than a message, and a miss, on the same one. + try Client.writeMount(testing.allocator, w.l.path(), w.body, w.long); + try w.read(w.long); + try testing.expectError(error.NotFound, Client.readMount(testing.allocator, w.l.path(), "/pane/no-such", "/pane/no-such", 64)); + try testing.expectEqual(@as(u64, 1), w.l.accepts.load(.acquire)); + // The peer hangs up (a Restore cuts its clients): the next + // operation dials again and is answered. + for (&w.l.runner.conns) |*conn| if (conn.live()) conn.close(); + const by = nowMs() + Client.budget_ms; + while (w.l.runner.count() != 0 and nowMs() < by) nap(1); + try w.read(w.long); + try testing.expectEqual(@as(u64, 2), w.l.accepts.load(.acquire)); } }; + var worker: Worker = .{ .l = l, .body = body, .long = long }; + pardes.turn.rest(); + const thread = try std.Thread.spawn(.{}, Worker.run, .{&worker}); + const deadline = nowMs() + 3 * Client.budget_ms; + while (!worker.done.load(.acquire) and nowMs() < deadline) nap(1); + thread.join(); + // Forgotten: the kept connection is closed, and its task sees it. + const settle_by = nowMs() + Client.budget_ms; + while (l.runner.count() != 0 and nowMs() < settle_by) nap(1); + pardes.turn.wake(); + if (worker.failure) |err| return err; + try testing.expectEqual(@as(usize, 0), l.runner.count()); +} - /// Lets go of a connection kept for `dial` (an Unmount): its mount is gone. - pub fn forget(dial: []const u8) void { - if (comptime !supported) return; - while (kept.take(dial)) |s| endSession(s, false); - } +test "a connection holds up to 128 reads, and the next is refused in words" { + try testing.expectEqual(@as(usize, 128), ninep.max_held); + if (comptime supported) try testing.expectEqualStrings("too many reads waiting: 128", Runner.Engine.e_waiting); +} - /// Writes `request`, then `more`, to `path` and reads the answer on the - /// same open, as /pane/new answers its own open: a session's last - /// answer, from another client, can never be what comes back. `exec - /// 3<>file`. `more` (`pardes -`'s text after its /pager line) goes in - /// Twrites at rising offsets, each answer giving the next its budget. - pub fn ask(gpa: std.mem.Allocator, dial: []const u8, path: []const u8, request: []const u8, more: []const u8) ![]u8 { - if (comptime !supported) return error.Unsupported; - var names: [max_depth][]const u8 = undefined; - const n = try elements(path, &names); - var remote: RemoteError = .{}; - errdefer said = remote; - const s = try startSession(dial, path); - defer endSession(s, false); - const fid = try walkTo(s, names[0..n], &remote); - _ = try s.ask(.{ .open = .{ .fid = fid, .mode = ninep.ordwr } }, &remote); - var written: usize = 0; - while (written < request.len + more.len) { - const from = if (written < request.len) request[written..] else more[written - request.len ..]; - const chunk = from[0..@min(from.len, s.cl.maxWrite())]; - const count = (try s.ask(.{ .write = .{ .fid = fid, .offset = written, .data = chunk } }, &remote)).write; - if (count == 0 or count > chunk.len) return Error.Botch; - written += count; - s.deadline = nowMs() +| write_budget_ms; - } - var out: std.ArrayList(u8) = .empty; - errdefer out.deinit(gpa); - while (true) { - const data = (try s.ask(.{ .read = .{ .fid = fid, .offset = out.items.len, .count = s.cl.maxRead() } }, &remote)).read; - if (data.len == 0) return out.toOwnedSlice(gpa); - if (out.items.len + data.len > 4096) return Error.FileTooLarge; - try out.appendSlice(gpa, data); - } - } +test "a held read is answered when the log has news, and a flushed one spends nothing" { + if (comptime !supported) return error.SkipZigTest; + const gpa = testing.allocator; + var rt: TempRuntime = .{}; + try rt.init("held"); + defer rt.deinit(); - /// Opens `path` on one connection and writes `first` to it (/log's - /// `follow new`), all with the usual deadline; then, once `ready(ctx)` - /// has said it is not done already, reads it with no deadline, a read at - /// a time, until `record(ctx, bytes)` says done. An end of file or a - /// dropped connection is Hangup. What `pardes --wait` blocks on. - pub fn follow( - dial: []const u8, - path: []const u8, - first: []const u8, - ctx: anytype, - comptime ready: fn (@TypeOf(ctx)) bool, - comptime record: fn (@TypeOf(ctx), []const u8) bool, - ) !void { - if (comptime !supported) return error.Unsupported; - var names: [max_depth][]const u8 = undefined; - const n = try elements(path, &names); - var remote: RemoteError = .{}; - const s = try startSession(dial, path); - defer endSession(s, false); - const fid = try walkTo(s, names[0..n], &remote); - _ = try s.ask(.{ .open = .{ .fid = fid, .mode = ninep.ordwr } }, &remote); - _ = try s.ask(.{ .write = .{ .fid = fid, .offset = 0, .data = first } }, &remote); - if (ready(ctx)) return; - s.deadline = std.math.maxInt(i64); - while (true) { - const data = (try s.ask(.{ .read = .{ .fid = fid, .offset = 0, .count = s.cl.maxRead() } }, &remote)).read; - if (data.len == 0) return Error.Hangup; - if (record(ctx, data)) return; - } + const p = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); + defer p.deinit(); + _ = try p.setTestFile("held\n"); + p.update(.tick); // the pane is logged, before the log is opened + while (p.nextEffect()) |_| {} + const l = try listen(testing.io, gpa, p, "held", "", null); + defer { + l.reset(p); + l.deinit(gpa); } - - fn connect(sock: Dial, deadline: i64) Error!c_int { - const address: transport.Address = switch (sock) { - .unix => |path| .{ .unix = path }, - .tcp => |ip| .{ .tcp = ip }, - }; - return transport.connectFd(address, deadline) catch return Error.Dial; + // This thread is the editor's and plays the client too, resting while + // it does so that the runner's tasks can take the turn and answer. + const s = try gpa.create(Client.Session); + defer gpa.destroy(s); + s.* = .{ .fd = -1, .deadline = nowMs() + 3 * Client.budget_ms, .display_path = "/log" }; + var sock_buf: [sun_path_len]u8 = undefined; + pardes.turn.rest(); + defer pardes.turn.wake(); + s.fd = try Client.connect(try Client.resolve(&sock_buf, l.path()), s.deadline); + defer _ = libc.close(s.fd); + s.cl = .init(.{ .in = &s.in, .out = &s.out }); + var remote: Client.RemoteError = .{}; + _ = try s.ask(.{ .version = .{} }, &remote); + _ = try s.ask(.{ .attach = .{ .fid = 0, .uname = "held" } }, &remote); + _ = try s.ask(.{ .walk = .{ .fid = 0, .newfid = 1, .names = &.{"log"} } }, &remote); + _ = try s.ask(.{ .open = .{ .fid = 1, .mode = ninep.ordwr } }, &remote); + var frozen: u64 = 0; + while (true) { + const n = (try s.ask(.{ .read = .{ .fid = 1, .offset = frozen, .count = 4096 } }, &remote)).read.len; + if (n == 0) break; + frozen += n; } + _ = try s.ask(.{ .write = .{ .fid = 1, .offset = 0, .data = "follow\n" } }, &remote); - const poll_in: i16 = @intCast(libc.POLL.IN); - const poll_out: i16 = @intCast(libc.POLL.OUT); + const heldNow = struct { + /// Waits until the core holds `n` reads. + fn count(core: *pardes.Pardes, n: usize) !void { + const deadline = nowMs() + Client.budget_ms; + while (nowMs() < deadline) { + pardes.turn.wake(); + var got: usize = 0; + for (core.fs.opens) |o| got += @intFromBool(o.held != null); + pardes.turn.rest(); + if (got == n) return; + nap(1); + } + return error.Timeout; + } + }; + const serial = p.panes[0].?.serial; + var event_path: [32]u8 = undefined; + var event_names: [3][]const u8 = .{ "pane", try std.fmt.bufPrint(&event_path, "{d}", .{serial}), "event" }; + _ = try s.ask(.{ .walk = .{ .fid = 0, .newfid = 2, .names = &event_names } }, &remote); + _ = try s.ask(.{ .open = .{ .fid = 2, .mode = ninep.oread } }, &remote); - test "a path becomes walk elements, normalised the way a shell would" { - var out: [max_depth][]const u8 = undefined; - try testing.expectEqual(@as(usize, 3), try elements("/pane/1/body", &out)); - try testing.expectEqualStrings("pane", out[0]); - try testing.expectEqualStrings("1", out[1]); - try testing.expectEqualStrings("body", out[2]); + // Flushed while held, a read is gone from the engine without the core + // hearing of it. Its tag, asked again at once for a read of the pane's + // event, must not be answered with the log's next record, and that + // record must reach the log's next read. + const flushed = try s.cl.submit(.{ .read = .{ .fid = 1, .offset = 0, .count = 4096 } }); + try s.flush(); + try heldNow.count(p, 1); + _ = try s.cl.submit(.{ .flush = .{ .oldtag = flushed } }); + const interrupted = try s.settle(); + try testing.expectEqual(flushed, interrupted.tag); + try testing.expect(interrupted.result == .fail); + try testing.expect((try s.settle()).result == .flush); + const reused = try s.cl.submit(.{ .read = .{ .fid = 2, .offset = 0, .count = 4096 } }); + try testing.expectEqual(flushed, reused); + try s.flush(); + try heldNow.count(p, 2); + pardes.turn.wake(); + p.setMessage(0, "after the flush"); + pardes.turn.rest(); + try heldNow.count(p, 1); + pardes.turn.wake(); + p.fs.origin = 'K'; + pardes.ctlfs.events.noteAction(p, 0, .body_exec, 0, 4, 0, "held"); + pardes.turn.rest(); + const clicked = try s.settle(); + try testing.expectEqual(reused, clicked.tag); + try testing.expectEqualStrings("KX0 4 0 4 held\n", clicked.result.read); + try testing.expect(std.mem.indexOf(u8, (try s.ask(.{ .read = .{ .fid = 1, .offset = 0, .count = 4096 } }, &remote)).read, "after the flush") != null); - try testing.expectEqual(@as(usize, 3), try elements("pane/1/body", &out)); - try testing.expectEqual(@as(usize, 3), try elements("//pane//1//body//", &out)); - try testing.expectEqual(@as(usize, 1), try elements("/index", &out)); + // Held, a read is answered by the record that arrives, on its own + // connection, with no retry of every parked request asked for; a + // second read on the same open meanwhile is refused, not orphaned. + const waiting = try s.cl.submit(.{ .read = .{ .fid = 1, .offset = 0, .count = 4096 } }); + try s.flush(); + try heldNow.count(p, 1); + const second = try s.cl.submit(.{ .read = .{ .fid = 1, .offset = 0, .count = 4096 } }); + const refused = try s.settle(); + try testing.expectEqual(second, refused.tag); + try testing.expectEqualStrings(pardes.ctlfs.e_in_use, refused.result.fail); + pardes.turn.wake(); + p.setMessage(0, "while held"); + const retry_all = pardes.turn.parked; + pardes.turn.rest(); + try testing.expect(!retry_all); + const answered = try s.settle(); + try testing.expectEqual(waiting, answered.tag); + try testing.expect(std.mem.indexOf(u8, answered.result.read, "while held") != null); - try testing.expectEqual(@as(usize, 0), try elements("/", &out)); + // Clunked, the log's held read is interrupted by the engine and the + // record it would have had is spent on nobody. + _ = try s.cl.submit(.{ .read = .{ .fid = 1, .offset = 0, .count = 4096 } }); + try s.flush(); + try heldNow.count(p, 1); + s.drop(1); + s.drop(2); + try heldNow.count(p, 0); - var deep: [8 * max_depth]u8 = @splat('/'); - for (0..max_depth + 1) |i| deep[i * 2 + 1] = 'a'; - try testing.expectError(Error.PathTooDeep, elements(deep[0 .. (max_depth + 1) * 2], &out)); + // Records queued as a Restore comes reach a follower in the answer to + // the read it holds, all of them, before the hangup cuts it. (The + // interrupted read above answered first: one clunk's answer is unread.) + _ = try s.settle(); + _ = try s.ask(.{ .walk = .{ .fid = 0, .newfid = 3, .names = &.{"log"} } }, &remote); + _ = try s.ask(.{ .open = .{ .fid = 3, .mode = ninep.ordwr } }, &remote); + _ = try s.ask(.{ .write = .{ .fid = 3, .offset = 0, .data = "follow new\n" } }, &remote); + const last = try s.cl.submit(.{ .read = .{ .fid = 3, .offset = 0, .count = 4096 } }); + try s.flush(); + try heldNow.count(p, 1); + pardes.turn.wake(); + p.setMessage(0, "queued one"); + p.setMessage(0, "queued two"); + p.setMessage(0, "queued three"); + l.reset(p); + pardes.turn.rest(); + const cut = try s.settle(); + try testing.expectEqual(last, cut.tag); + for ([_][]const u8{ "queued one", "queued two", "queued three" }) |said| + try testing.expect(std.mem.indexOf(u8, cut.result.read, said) != null); +} + +test "a pane's PARDES_MOUNT is the session's directory under 9ns --mntgen, and unset when not known" { + const names = [_][*:0]const u8{ "NINE_MNTGEN", "NINE_MOUNT", "PARDES_MOUNT" }; + var saved: [names.len]?[:0]u8 = @splat(null); + for (names, &saved) |name, *value| { + if (libc.getenv(name)) |old| value.* = try testing.allocator.dupeSentinel(u8, std.mem.span(old), 0); } + defer for (names, saved) |name, value| { + if (value) |old| { + _ = setenv(name, old, 1); + testing.allocator.free(old); + } else _ = @import("host_io.zig").testUnsetenv(name); + }; + _ = setenv("NINE_MOUNT", "/mnt/9p", 1); + _ = setenv("NINE_MNTGEN", "1", 1); + exportMount("/run/user/1000/pardes-9p-work.sock"); + try testing.expectEqualStrings("/mnt/9p/pardes/work", @import("host_io.zig").ChildEnv.exported("PARDES_MOUNT").?); + // A mount of another server (9ns --unix): not this session's. + _ = @import("host_io.zig").testUnsetenv("NINE_MNTGEN"); + exportMount("/run/user/1000/pardes-9p-work.sock"); + try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_MOUNT") == null); +} - test "a bare dial resolves to the socket --9p binds, and a path is taken as given" { - if (comptime !supported) return error.SkipZigTest; - var buf: [sun_path_len]u8 = undefined; +test "9P shell environment states being inside pardes apart from how to reach it" { + const names = [_][*:0]const u8{ "XDG_RUNTIME_DIR", "HOME", "PARDES_PID", "PARDES_9P", "PARDES_PANE" }; + var saved: [names.len]?[:0]u8 = @splat(null); + for (names, &saved) |name, *value| { + if (libc.getenv(name)) |old| value.* = try testing.allocator.dupeSentinel(u8, std.mem.span(old), 0); + } + defer for (names, saved) |name, value| { + if (value) |old| { + _ = setenv(name, old, 1); + testing.allocator.free(old); + } else _ = @import("host_io.zig").testUnsetenv(name); + }; - const named = (try resolve(&buf, "work")).unix; - try testing.expect(std.mem.endsWith(u8, named, "/pardes-9p-work.sock")); - var expect: [sun_path_len]u8 = undefined; - var dir_buf: [sun_path_len:0]u8 = undefined; - const dir = socketDir(&dir_buf).?; - try testing.expectEqualStrings(socketPath(&expect, dir, "work").?, named); + var dir: [sun_path_len:0]u8 = undefined; + _ = setenv("XDG_RUNTIME_DIR", "/run/user/1000", 1); + try testing.expectEqualStrings("/run/user/1000", socketDir(&dir).?); + _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR"); + _ = setenv("HOME", "/home/example", 1); + try testing.expectEqualStrings("/home/example/.local/state/pardes", socketDir(&dir).?); + _ = @import("host_io.zig").testUnsetenv("HOME"); + try testing.expect(socketDir(&dir) == null); - const path = (try resolve(&buf, "/tmp/somewhere.sock")).unix; - try testing.expectEqualStrings("/tmp/somewhere.sock", path); - try testing.expectEqualStrings("/tmp/somewhere.sock", (try resolve(&buf, "unix!/tmp/somewhere.sock")).unix); + // The socket a listener keeps while it lives. + const path = "/tmp/pardes-example.sock"; + @memcpy(own_socket[0..path.len], path); + own_socket[path.len] = 0; + own_socket_len = path.len; + defer own_socket_len = 0; + var own: [16]u8 = undefined; + const own_pid = try std.fmt.bufPrint(&own, "{d}", .{@as(u32, @intCast(libc.getpid()))}); - try testing.expectError(error.BadDial, resolve(&buf, "")); - try testing.expectError(error.BadDial, resolve(&buf, "/tmp/a\x00b")); - } + // A `--nested` session: reachable for scripts and mounts, but nobody's + // parent, so a pardes started in one of its shells runs a session of + // its own instead of handing its argument over. + exportEnv(7, false); + try testing.expectEqualStrings(path, @import("host_io.zig").ChildEnv.exported("PARDES_9P").?); + try testing.expectEqualStrings("7", @import("host_io.zig").ChildEnv.exported("PARDES_PANE").?); + try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PID") == null); - test "a dial with nothing listening is one error and not a wait" { - if (comptime !supported) return error.SkipZigTest; - var names: [max_depth][]const u8 = undefined; - const n = try elements("/pane/1/body", &names); - var remote: RemoteError = .{}; - const before = nowMs(); - try testing.expectError( - Error.Dial, - fetchBytes(testing.allocator, "/tmp/pardes-9p-no-such-socket.sock", names[0..n], &remote, null, "/pane/1/body", limits.max_file_bytes, false), - ); - try testing.expect(nowMs() - before < budget_ms); - } + exportEnv(8, true); + try testing.expectEqualStrings("8", @import("host_io.zig").ChildEnv.exported("PARDES_PANE").?); + try testing.expectEqualStrings(own_pid, @import("host_io.zig").ChildEnv.exported("PARDES_PID").?); - test "one fetch has three msize buffers and bounded transport metadata" { - try testing.expectEqual(@as(usize, msize), @as(usize, (Session{ .fd = -1, .deadline = 0, .display_path = "" }).in.len)); - try testing.expect(@sizeOf(Session) <= 3 * msize + @sizeOf(ninep.Client) + 128 + @sizeOf(@FieldType(Session, "dial_buf"))); - try testing.expect(@sizeOf(ninep.Client) <= 512); - } + // A session whose listener never came up is still the session this shell + // is inside: the pid stands on its own, and only the address is missing. + own_socket_len = 0; + exportEnv(0, true); + try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_9P") == null); + try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PANE") == null); + try testing.expectEqualStrings(own_pid, @import("host_io.zig").ChildEnv.exported("PARDES_PID").?); - test "expired sessions do not send or consume buffered protocol work" { - var session: Session = .{ .fd = -1, .deadline = 0, .display_path = "" }; - session.cl = .init(.{ .in = &session.in, .out = &session.out }); - _ = try session.cl.submit(.{ .version = .{} }); - const queued = session.cl.output().len; - try testing.expect(queued > 0); - try testing.expectError(Error.Timeout, session.flush()); - try testing.expectEqual(queued, session.cl.output().len); - try testing.expectError(Error.Timeout, session.settle()); - try testing.expectError(Error.Timeout, session.wait(poll_in)); - } -}; + exportEnv(0, false); + try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_9P") == null); + try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PANE") == null); + try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PID") == null); +} -- cgit v1.3