diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-18 20:52:50 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-18 21:22:58 -0300 |
| commit | a75956bb3443aa8aac778d2ff12c42f5ef99e0e9 (patch) | |
| tree | 17547cbf6606c8bd05be313cd8f835cb85d13e57 /runtime/9p_io.zig | |
| parent | 5b33c88476054fffc1044fff0526f7f0b28563cf (diff) | |
| download | pardes-a75956bb3443aa8aac778d2ff12c42f5ef99e0e9.tar.gz pardes-a75956bb3443aa8aac778d2ff12c42f5ef99e0e9.zip | |
runtime: main, daemon, upgrade, crash, addon_test, 9p_io, 9p and macos.zig into runtime/ (the reorganisation's tree, step 2a), skill.txt with main.zig, its one reader: module `runtime` (runtime/root.zig) and the core, each naming the other, a product's root (main.zig, macos.zig) a module of its own over both; the files name across by module (`@import("runtime").daemon`, `@import("core").look`), pardes.zig exporting what they name (ctl, edit_cmd, tracy, selection_pipe, and the frontends until they move); core-test and shell-test run each module's test program, so the same tests run (the ledger: every named test of every suite the base's, tui and gui); nothing else changes: frames byte-identical, the same counts (tui core-test 822/963, the one more an anonymous collector)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'runtime/9p_io.zig')
| -rw-r--r-- | runtime/9p_io.zig | 2789 |
1 files changed, 2789 insertions, 0 deletions
diff --git a/runtime/9p_io.zig b/runtime/9p_io.zig new file mode 100644 index 00000000..acc851c4 --- /dev/null +++ b/runtime/9p_io.zig @@ -0,0 +1,2789 @@ +const std = @import("std"); +/// libc: sockets, flock'd lock files, and the registry's symlinks, judged with readlink's own truncation and unlinked from signal handlers. +const libc = std.c; +const builtin = @import("builtin"); +const ninep = @import("9p.zig"); +const cloud9 = @import("cloud9"); +const transport = cloud9.transport; +const pardes = @import("core"); +const limits = @import("core").memory.limits; +const log = std.log.scoped(.ninep); + +const nowMs = transport.nowMs; + +fn nap(ms: c_int) void { + _ = libc.poll(&[0]libc.pollfd{}, 0, ms); +} + +pub const darwin = builtin.os.tag.isDarwin(); +pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd or darwin; +pub const sun_path_len = @typeInfo(@FieldType(libc.sockaddr.un, "path")).array.len; + +pub fn setCloexec(fd: c_int) void { + _ = libc.fcntl(fd, libc.F.SETFD, @as(c_int, 1)); +} + +pub fn socketDir(buf: []u8) ?[:0]const u8 { + if (libc.getenv("XDG_RUNTIME_DIR")) |path| + return std.fmt.bufPrintSentinel(buf, "{s}", .{std.mem.span(path)}, 0) catch null; + const home = libc.getenv("HOME") orelse return null; + return std.fmt.bufPrintSentinel(buf, "{s}/.local/state/pardes", .{std.mem.span(home)}, 0) catch null; +} + +pub const FileFacts = struct { mode: u32, uid: libc.uid_t }; + +pub fn statNoFollow(path: [:0]const u8) ?FileFacts { + if (comptime builtin.os.tag != .linux) { + var stat: libc.Stat = undefined; + if (libc.fstatat(libc.AT.FDCWD, path, &stat, libc.AT.SYMLINK_NOFOLLOW) != 0) return null; + return .{ .mode = stat.mode, .uid = stat.uid }; + } else { + const linux = std.os.linux; + var stat: linux.Statx = undefined; + const fields: linux.STATX = .{ .TYPE = true, .MODE = true, .UID = true }; + if (libc.statx(linux.AT.FDCWD, path, linux.AT.SYMLINK_NOFOLLOW, fields, &stat) != 0) return null; + return .{ .mode = stat.mode, .uid = stat.uid }; + } +} + +pub fn ensureSocketDir(dir: [:0]const u8) bool { + if (dir.len == 0) return false; + // A failure shows in the stat below. + _ = std.Io.Dir.cwd().createDirPathStatus(std.Io.Threaded.global_single_threaded.io(), dir, .fromMode(0o700)) catch {}; + const stat = statNoFollow(dir) orelse return false; + return stat.mode & 0o170000 == 0o040000 and stat.uid == libc.getuid() and stat.mode & 0o077 == 0; +} + +const prefix = "pardes-9p-"; + +pub const msize = ninep.msize; + +/// Scripts, a mount and an agent or two at once; each slot holds its +/// buffers (a few msize) whether used or not. +pub const max_conns = 16; + +extern "c" fn inet_pton(family: c_int, src: [*:0]const u8, dst: *anyopaque) c_int; + +pub fn networkAddress(dial: []const u8, allow_zero_port: bool) error{BadDial}!std.Io.net.IpAddress { + if (comptime !supported) return error.BadDial; + const host_start: usize = if (std.mem.startsWith(u8, dial, "tcp!")) 4 else return error.BadDial; + const split = std.mem.lastIndexOfScalar(u8, dial, '!') orelse return error.BadDial; + if (split <= host_start or split + 1 == dial.len) return error.BadDial; + const port_text = dial[split + 1 ..]; + for (port_text) |c| if (c < '0' or c > '9') return error.BadDial; + const port = std.fmt.parseInt(u16, port_text, 10) catch return error.BadDial; + if (port == 0 and !allow_zero_port) return error.BadDial; + const host = dial[host_start..split]; + if (std.mem.indexOfScalar(u8, host, 0) != null) return error.BadDial; + var host_buf: [46]u8 = undefined; + const host_z = std.fmt.bufPrintSentinel(&host_buf, "{s}", .{host}, 0) catch return error.BadDial; + var ip4: std.Io.net.Ip4Address = .{ .port = port, .bytes = undefined }; + if (inet_pton(libc.AF.INET, host_z, &ip4.bytes) == 1) return .{ .ip4 = ip4 }; + var ip6: std.Io.net.Ip6Address = .{ .port = port, .bytes = undefined }; + if (inet_pton(libc.AF.INET6, host_z, &ip6.bytes) == 1) return canonicalIp(.{ .ip6 = ip6 }); + return error.BadDial; +} + +fn canonicalIp(address: std.Io.net.IpAddress) std.Io.net.IpAddress { + if (address == .ip6) { + if (std.Io.net.Ip4Address.fromIp6(address.ip6)) |ip4| return .{ .ip4 = ip4 }; + } + return address; +} + +/// The Unix and TCP listeners run on cloud9's `std.Io` runner: it accepts, +/// reads frames, steps each connection's engine and writes replies on its +/// own tasks, and every backend request is answered right there, on the +/// connection's task, by taking the editor's turn (`pardes.turn`): while the +/// editor waits for input, or while a step of it is out in a syscall -- +/// which is what lets the editor read its own tree through a mount. +const Runner = if (supported) cloud9.serve.Runner(pardes.ctlfs, ninep.editor, .{ + .msize = msize, + .connections = max_conns, + .listeners = 2, +}) else void; + +pub const Listener = struct { + io: std.Io, + /// The core that answers; `reset` puts a replacement in. + core: *pardes.Pardes, + /// cloud9's runner, sized for every connection's buffers and parks (some + /// 136 MiB): pages of its own (`allocRunner`), written only as clients + /// take slots, so an idle session does not hold them resident. + runner: *Runner = undefined, + stopping: std.atomic.Value(bool) = .init(false), + /// The watchers of held Edit writes (watchEdit): canceled and awaited + /// by `deinit`, once the turn lets a waiting one through, so none wakes + /// into a freed Listener. + edit_watchers: std.Io.Group = .init, + /// A connection wrote the Restore and has yet to answer it. + restore_writer: std.atomic.Value(bool) = .init(false), + /// Clients turned away with every slot taken, since the log last said so. + refused: std.atomic.Value(u32) = .init(0), + /// Writes being served right now, a retry of a parked one included: a + /// held Edit write a retry has taken out of its park is not flushed + /// (watchEdit). + writes_out: std.atomic.Value(u32) = .init(0), + /// Connections accepted so far, and the count each slot's connection + /// was accepted at: a Restore cuts those accepted before it (`reset`), + /// and a new client in a freed slot has a later stamp. Written on the + /// connection's task as it opens, read with the turn. + accepts: std.atomic.Value(u64) = .init(0), + accepted: [max_conns]std.atomic.Value(u64) = @splat(.init(0)), + /// Connections accepted before this count are being cut by a Restore. + cut_before: u64 = 0, + tcp_address: ?std.Io.net.IpAddress = null, + path_buf: [sun_path_len]u8 = undefined, + path_len: usize = 0, + /// The socket's lock (`claim`), held for the listener's life; -1 when none. + lock: c_int = -1, + lock_buf: [lock_path_len:0]u8 = undefined, + /// The registry entry this editor posted + /// (`$XDG_RUNTIME_DIR/9p/pardes/<name>`), zero when it did not. + posted_buf: [sun_path_len]u8 = undefined, + posted_len: usize = 0, + wake_ctx: ?*anyopaque = null, + wake: ?*const fn (?*anyopaque) void = null, + + pub fn path(l: *const Listener) []const u8 { + return l.path_buf[0..l.path_len]; + } + + /// The runner's handler, on the connection's task: takes the turn and + /// answers. A request that would change a pane while the editor is out + /// in a syscall mid-step is parked in the engine instead, and retried + /// when the turn is next given up between steps (`wakeParked`). + /// On the accepting task, without the turn: counted, and logged with + /// the turn (`answerHeld`). + fn onRefused(ctx: ?*anyopaque) void { + const l: *Listener = @ptrCast(@alignCast(ctx.?)); + _ = l.refused.fetchAdd(1, .acq_rel); + l.kick(); + } + + fn onOpened(ctx: ?*anyopaque, conn: *Runner.Conn) void { + const l: *Listener = @ptrCast(@alignCast(ctx.?)); + l.accepted[conn.index].store(l.accepts.fetchAdd(1, .acq_rel) + 1, .release); + } + + /// A live connection accepted before the Restore under way. + fn cutting(l: *Listener, conn: *Runner.Conn) bool { + return conn.live() and l.accepted[conn.index].load(.acquire) <= l.cut_before and l.cut_before != 0; + } + + fn onServe(ctx: ?*anyopaque, conn: *Runner.Conn, req: pardes.ctlfs.Req) void { + const l: *Listener = @ptrCast(@alignCast(ctx.?)); + if (req.op == .write) _ = l.writes_out.fetchAdd(1, .acq_rel); + defer if (req.op == .write) { + _ = l.writes_out.fetchSub(1, .acq_rel); + }; + // Once `stop` has begun the editor is tearing down and may never + // rest again: answer without waiting on it. + if (l.stopping.load(.acquire)) { + const refused = pardes.ctlfs.Reply.fail(req.tag, pardes.ctlfs.E.IO); + return conn.reply(&refused, ""); + } + const quiet = pardes.turn.take(); + defer pardes.turn.give(); + // A connection a Restore is cutting (`reset`) is the old editor's + // client: its fids name the old editor's panes and opens. + if (l.cutting(conn)) { + const refused = pardes.ctlfs.Reply.fail(req.tag, pardes.ctlfs.E.IO); + return conn.reply(&refused, ""); + } + if (!quiet and pardes.ctlfs.needsQuiet(l.core, req)) { + pardes.turn.parked = true; + const later: pardes.ctlfs.Reply = .{ .tag = req.tag, .status = .again }; + return conn.reply(&later, ""); + } + const core = l.core; + core.fs.write_room = if (conn.engine.protocol.msize > 24) conn.engine.protocol.msize - 24 else 0; + const epoch = pardes.turn.epoch; + const restores = pardes.turn.restores; + // Asked first: a release that runs a held line has none after. + const changes = pardes.ctlfs.changesPane(core, req); + const fills = pardes.ctlfs.handsOff(core, req); + const reply = core.serveFs(req); + const edit_started = core.fs.edit_started; + core.fs.edit_started = false; + // The held Edit write asked again by a retry, its Edit still + // running: parked again, as it was. + if (reply.status == .again and req.op == .write) return conn.reply(&reply, ""); + if (req.op == .release and quiet) l.collectOs(); + // A close's held last line runs now when no step is out, so its err + // is logged before the close is answered, as before; with a step + // out (perhaps through a mount this close came by) it waits for + // that step, and the close is answered at once all the same. + if (req.op == .release and quiet) pardes.ctlfs.runClosedLines(core); + // A read with nothing yet stays parked in the engine, and the core + // keeps its ticket to answer it when what it waits on has something. + if (reply.status == .again and req.op == .read) { + // Only an open's record can hold a read; one that waits with + // none would sit parked until some unrelated write parks. + const o = pardes.ctlfs.openOf(core, req) orelse { + log.err("a read of node {x} waits with no open record to hold it", .{req.node}); + std.debug.assert(false); + return conn.reply(&reply, ""); + }; + // One read waits on an open at a time, as acme's window keeps + // one `eventx`; a second is refused rather than left parked + // where nothing would ever answer it. The first asked again by + // a retry is the same request, and holds its place. + if (o.held) |held| if (held.req.tag != req.tag) { + const other: *Runner.Conn = @ptrCast(@alignCast(held.asker)); + if (other.waiting(held.ticket)) + return conn.reply(&pardes.ctlfs.failText(req.tag, pardes.ctlfs.E.BUSY, pardes.ctlfs.e_in_use), ""); + }; + o.held = null; + const ticket = conn.hold(&reply) orelse return; + o.held = .{ .asker = conn, .req = req, .ticket = ticket }; + return; + } + if (!changes) return conn.reply(&reply, core.fsPayload(reply)); + // The editor draws the change and performs what it asked for; when + // it asked for something -- a save, a shell, a watch -- the answer + // waits until that is done, so `echo Save > exec` returns with the + // file written. A change carries no payload, so the reply is still + // whole after the wait. + l.kick(); + // A read that put a /pager open's text in (ctl.fillPager), or a + // close that handed its text or last line to the editor's step + // (ctl.fillClosedPagers, tree.runClosedLines), waits on nothing the + // editor performs: its answer goes now, while the turn still keeps + // a read's payload whole. Waiting would hang a close through a + // mount: 9ns answers nothing else on it while a clunk is out, and + // the editor's step may be out opening a file through that mount. + if (fills) return conn.reply(&reply, core.fsPayload(reply)); + // A refusal that left saves going (Putall's one refused among the + // rest) is answered once they have landed, its words copied out of + // the core's one buffer for them, which a request run meanwhile + // could write over. + if (reply.status == .err and core.effects_len != 0 and !core.quit) { + var kept: [320]u8 = undefined; + const words = kept[0..@min(reply.ename.len, kept.len)]; + @memcpy(words, reply.ename[0..words.len]); + var waited = reply; + waited.ename = words; + pardes.turn.awaitPast(&pardes.turn.epoch, epoch); + return conn.reply(&waited, ""); + } + // Any other refusal answers at once, for the same buffer's sake. + if (reply.status == .err) return conn.reply(&reply, ""); + // A write that quits the editor (Kill) is answered now: the editor + // is on its way out and will settle nothing this could wait for, + // and `deinit` lets the answer out before it cuts the connections. + if (core.quit) return conn.reply(&reply, ""); + // An Edit whose `<`, `|` or `>` commands run off the loop: the write + // is held, as a read with nothing yet is, and answered when the Edit + // is done (answerHeld), so the connection serves on meanwhile -- a + // filter that reads this session through a mount among what it + // serves. A flush of it, or a hang-up, stops the Edit (watchEdit). + if (edit_started and core.pipe.edit_run != null) { + const later: pardes.ctlfs.Reply = .{ .tag = req.tag, .status = .again }; + const ticket = conn.hold(&later) orelse return; + core.fs.edit_hold = .{ .asker = conn, .slot = ticket.slot, .seq = ticket.seq, .tag = req.tag, .node = req.node, .handle = req.handle, .written = reply.written }; + l.edit_watchers.concurrent(l.io, watchEdit, .{ l, conn, ticket }) catch {}; + return; + } + const restoring = core.restore_req != null; + // Set with the turn still held, so `reset` sees it and waits. + if (restoring) l.restore_writer.store(true, .release); + defer if (restoring) l.restore_writer.store(false, .release); + core.fs.late_failure_len = 0; + // What fails while this waits is this write's err, never a msg too. + core.fs.write_waits = true; + if (core.effects_len != 0) pardes.turn.awaitPast(&pardes.turn.epoch, epoch); + if (core.fs.lsp_answer_at) |n| { + core.fs.lsp_answer_at = null; + core.fs.lsp_result = null; + pardes.turn.awaitPast(&pardes.turn.lsp_answers, n); + // The answer filled a pane (a Rename's preview): that is what + // this write's open reads back, as look and exec answer. + if (l.core == core) if (core.fs.lsp_result) |serial| { + core.fs.lsp_result = null; + core.fs.results[0] = serial; + core.fs.results_len = 1; + if (pardes.ctlfs.openOf(core, req)) |o| { + o.results.list[0] = serial; + o.results.len = 1; + o.results.read = 0; + } + }; + } + core.fs.write_waits = false; + // ponytail: one slot, so a failure of another client's effects that + // settle in the same wait is told to this write too. + if (l.core == core and core.fs.late_failure_len != 0) { + const late = core.fs.late_failure[0..core.fs.late_failure_len]; + // The errno a builtin's failure in the same words has; what + // that leaves EIO and says `no space` (a full disk under a + // Save) is ENOSPC. + const said = @import("core").ctl.failureErrno(late); + const errno = if (said == pardes.ctlfs.E.IO and std.mem.indexOf(u8, late, "no space") != null) pardes.ctlfs.E.NOSPC else said; + const failed = pardes.ctlfs.failText(req.tag, errno, late); + // Its err record says it (the path in it), the one record: it + // was said with no msg while this waited (fs.write_waits). + pardes.ctlfs.events.noteError(core, req, failed); + return conn.reply(&failed, ""); + } + // The Restore's own write is answered once it is done, and before + // `reset` hangs every connection up, this one too, so the writer + // hears that it happened rather than a cut; a failed one leaves the + // connection and says why on the message row and in the log. + if (restoring) { + if (l.core == core) pardes.turn.awaitPast(&pardes.turn.restores, restores); + return conn.reply(&reply, ""); + } + // Once a wait returns `core` may be gone: a Restore meanwhile put a + // replacement in and is hanging this connection up. + if (l.core != core) return; + conn.reply(&reply, ""); + } + + /// With the turn, as it is given up: answers each read the core holds + /// whose file now has something, on the connection that asked, with no + /// retry of anything else parked there. Only while its ticket still + /// waits, asked in the same hold of the engine as the answer: Tflush + /// and clunk drop a park without a word to the backend, a `retry` takes + /// one out to ask again (and that asking answers it), and a record spent + /// on any of them would be lost to the reader that comes next. + fn answerHeld(ctx: ?*anyopaque) void { + if (comptime !supported) return; + const l: *Listener = @ptrCast(@alignCast(ctx.?)); + if (l.stopping.load(.acquire)) return; + const core = l.core; + const refused = l.refused.swap(0, .acq_rel); + if (refused != 0) { + var why: pardes.Kind.Buf(64) = .{}; + pardes.ctlfs.events.notePath(core, "err -", why.print("9p: too many connections ({d} turned away)", .{refused})); + } + if (!core.fs.news) return; + core.fs.news = false; + for (&core.fs.opens) |*o| { + const held = o.held orelse continue; + const conn: *Runner.Conn = @ptrCast(@alignCast(held.asker)); + if (!conn.answerWith(held.ticket, Held{ .core = core, .req = held.req }, Held.make)) o.held = null; + } + // An Edit's held write, its Edit done. Not waiting: a retry has it + // out, and its asking answers it (Pardes.serveFs); or it was + // flushed, and watchEdit lets the record go. + if (core.fs.edit_hold) |h| if (h.done) { + const conn: *Runner.Conn = @ptrCast(@alignCast(h.asker)); + _ = conn.answerWith(.{ .slot = h.slot, .seq = h.seq }, core, struct { + fn make(c: *pardes.Pardes) ?Runner.Conn.Answer { + return .{ .reply = pardes.filesystem.EditHold.answer(&c.fs) }; + } + }.make); + }; + } + + /// While an Edit's write is held: when nobody waits for it any more -- + /// flushed (an interrupted writer), its fid clunked, its connection + /// hung up -- the Edit is stopped, its commands killed, nothing + /// changed (edit_cmd.cancel). The engine tells the backend nothing of a + /// flush, so it is looked for, every 100 ms; a park out for a retry is + /// not waiting either, so only three looks in a row with no write being + /// served count. + fn watchEdit(l: *Listener, conn: *Runner.Conn, ticket: cloud9.fs.Ticket) error{Canceled}!void { + var misses: u8 = 0; + while (!l.stopping.load(.acquire)) { + try l.io.sleep(.fromMilliseconds(100), .awake); + _ = pardes.turn.take(); + defer pardes.turn.give(); + if (l.stopping.load(.acquire)) return; + const core = l.core; + const h = core.fs.edit_hold orelse return; + if (h.slot != ticket.slot or h.seq != ticket.seq or h.asker != @as(*anyopaque, conn)) return; + if (conn.waiting(ticket) or l.writes_out.load(.acquire) != 0) { + misses = 0; + continue; + } + misses += 1; + if (misses < 3) continue; + if (h.done) core.fs.edit_hold = null else @import("core").edit_cmd.cancel(core); + l.kick(); + return; + } + } + + /// A held read asked again, to make its answer while its park waits. + const Held = struct { + core: *pardes.Pardes, + req: pardes.ctlfs.Req, + + fn make(h: Held) ?Runner.Conn.Answer { + const reply = h.core.serveFs(h.req); + if (reply.status == .again) return null; + return .{ .reply = reply, .bytes = h.core.fsPayload(reply) }; + } + }; + + /// The turn was given up quiet with a request parked: every connection + /// retries what it parked. + fn wakeParked(ctx: ?*anyopaque) void { + const l: *Listener = @ptrCast(@alignCast(ctx.?)); + if (l.stopping.load(.acquire)) return; + if (comptime supported) l.runner.wakeAll(); + } + + /// A request changed the core: the editor wakes to draw it and perform + /// what it asked for. + fn kick(l: *Listener) void { + if (l.stopping.load(.acquire)) return; + if (l.wake) |f| f(l.wake_ctx); + } + + pub const greet_deadline_ms: i64 = 5000; + + /// What a frontend's poll of the listener did; the runner's own tasks + /// serve every connection, so there is never more to do at once. + pub const Polled = struct { count: usize = 0, pending: bool = false }; + + /// Between the editor's steps, which is when the host paths may be + /// swept: the editor's own directory listings leave nodes in it too. + pub fn tick(l: *Listener) Polled { + l.collectOs(); + return .{}; + } + + /// Hangs every connection up, so that `replacement` starts with no + /// client holding anything. From here requests are answered from the + /// replacement -- a client that connects meanwhile is a client of the + /// replacement -- and every task waiting on the old core is let go: the + /// Restore's own writer answers, the rest find the core changed and + /// answer nothing, their connections being hung up. The connections pay their releases on their own tasks, so + /// the editor rests while they do. + pub fn reset(l: *Listener, replacement: *pardes.Pardes) void { + // A follower of the log hears every record queued before the cut, in + // the answer to the read it holds; the read after it is cut. + l.core.fs.hanging_up = true; + l.core.fs.news = true; + answerHeld(l); + l.core.fs.hanging_up = false; + l.core = replacement; + replacement.fs.socket_path = l.path(); + replacement.fs.tcp_address = l.tcp_address; + if (comptime supported) { + // The Restore's writer answers as it wakes (`onServe`), and its + // answer gets 200 ms to leave before the cut; every other request + // from the old editor's clients meanwhile is refused, since the + // fids it names are the old editor's. A client that connects now + // is the replacement's, and stays. + l.cut_before = l.accepts.load(.acquire); + pardes.turn.bump(&pardes.turn.epoch); + pardes.turn.bump(&pardes.turn.restores); + pardes.turn.rest(); + l.flushOut(); + for (&l.runner.conns) |*conn| if (l.cutting(conn)) conn.close(); + const deadline = nowMs() +| 2000; + while (nowMs() < deadline) { + const left = for (&l.runner.conns) |*conn| { + if (l.cutting(conn)) break true; + } else false; + if (!left) break; + nap(1); + } + pardes.turn.wake(); + l.cut_before = 0; + } + l.collectOs(); + } + + /// Forgets the host paths no connection names any more. With the turn, + /// and only between the editor's steps: a step out in a syscall may be + /// reading one of these entries. + fn collectOs(l: *Listener) void { + // A request out in a host syscall may be using an entry's path, or + // made one that no fid names yet (a walk's next element): swept at + // the next quiet instead. Taken back after a step of its own, the + // editor's frame (tty pollFrame) came here with one out, and a walk + // of a directory just made was answered ENOENT. + if (pardes.turn.out != 0) return; + const core = l.core; + var i: usize = 0; + while (i < core.fs.os_paths.items.len) { + const entry = core.fs.os_paths.items[i]; + if (l.references(entry.node)) { + i += 1; + } else { + core.gpa.free(entry.path); + _ = core.fs.os_paths.swapRemove(i); + } + } + } + + /// Whether any connection names `node`. + fn references(l: *Listener, node: u64) bool { + if (comptime supported) { + for (&l.runner.conns) |*conn| { + if (!conn.live()) continue; + conn.lock(); + defer conn.unlock(); + if (conn.engine.references(node)) return true; + } + } + return false; + } + + /// Where the runner's tasks report that a request changed the core. + pub fn watch(l: *Listener, ctx: ?*anyopaque, wake: *const fn (?*anyopaque) void) !void { + l.wake_ctx = ctx; + l.wake = wake; + } + + /// Advertises this editor in the posted-9P registry so a + /// `9ns --mntgen` mount lists it and dials it on a walk: + /// `$XDG_RUNTIME_DIR/9p/pardes/<name>` is a symlink to the socket + /// pardes already binds. One directory for the program, one entry + /// per running editor — the layout zmx uses for its sessions, so + /// several editors group instead of crowding the registry root. + /// + /// Only the runtime-directory socket posts: an instance that fell + /// back to `~/.local/state/pardes` stays out of the user's + /// registry, the way a private `ZMX_DIR` does for zmx. + /// + /// The socket itself is still bound by `listen` above, not by + /// `cloud9.post`: `post` claims flat names only, and its claim + /// protocol derives its lock directory from the registry path, so + /// a name inside a subdirectory cannot go through it yet. See + /// docs/typ/building.typ. + fn postToRegistry(l: *Listener, name: []const u8) void { + if (comptime !supported) return; + if (name.len == 0 or std.mem.indexOfAny(u8, name, "/\x00") != null) return; + const xdg_c = libc.getenv("XDG_RUNTIME_DIR") orelse return; + const xdg = std.mem.span(xdg_c); + if (xdg.len == 0) return; + + var reg_buf: [sun_path_len:0]u8 = undefined; + const reg = std.fmt.bufPrintSentinel(®_buf, "{s}/9p", .{xdg}, 0) catch return; + _ = libc.mkdir(reg, 0o750); + var svc_buf: [sun_path_len:0]u8 = undefined; + const svc = std.fmt.bufPrintSentinel(&svc_buf, "{s}/9p/pardes", .{xdg}, 0) catch return; + if (libc.mkdir(svc, 0o750) != 0 and statNoFollow(svc) == null) { + log.warn("registry post skipped: cannot create {s}", .{svc}); + return; + } + sweepRegistry(l.io, svc, xdg); + var entry_buf: [sun_path_len:0]u8 = undefined; + const entry = std.fmt.bufPrintSentinel(&entry_buf, "{s}/{s}", .{ svc, name }, 0) catch { + log.warn("registry post skipped: name too long: {s}", .{name}); + return; + }; + const target = l.path_buf[0..l.path_len]; + + // Replace only what is provably not live: our own entry, or a + // dead predecessor's symlink. `probe` treats uncertainty as + // live, so it is only asked about an entry that *is* a symlink; + // anything else is left strictly alone and the symlink below + // simply fails. + var link_buf: [sun_path_len]u8 = undefined; + const n = libc.readlink(entry, &link_buf, link_buf.len); + if (n >= 0) { + const had = link_buf[0..@intCast(n)]; + if (!std.mem.eql(u8, had, target) and probe(entry) == .live) { + log.warn("registry entry pardes/{s} is live; not re-posted", .{name}); + return; + } + _ = libc.unlink(entry); + } + var target_z: [sun_path_len:0]u8 = undefined; + const target_zs = std.fmt.bufPrintSentinel(&target_z, "{s}", .{target}, 0) catch return; + if (libc.symlink(target_zs, entry) != 0) { + log.warn("registry post skipped: pardes/{s} is occupied", .{name}); + return; + } + @memcpy(l.posted_buf[0..entry.len], entry); + l.posted_len = entry.len; + log.info("posted pardes/{s} -> {s}", .{ name, target }); + } + + /// Removes the registry entry, but only while it is still ours: a + /// name another editor has since claimed is never unlinked. + fn unpostFromRegistry(l: *Listener) void { + if (comptime !supported) return; + if (l.posted_len == 0) return; + var z: [sun_path_len:0]u8 = undefined; + @memcpy(z[0..l.posted_len], l.posted_buf[0..l.posted_len]); + z[l.posted_len] = 0; + const entry = z[0..l.posted_len :0]; + var link_buf: [sun_path_len]u8 = undefined; + const n = libc.readlink(entry, &link_buf, link_buf.len); + if (n >= 0 and std.mem.eql(u8, link_buf[0..@intCast(n)], l.path_buf[0..l.path_len])) { + _ = libc.unlink(entry); + } + l.posted_len = 0; + } + + /// What the connections have still to send let go, 200 ms at most, + /// before they are cut (a quit, an Upgrade's exec, a Restore: its + /// write's answer, which a Restore's writer has yet to give while + /// `restore_writer` says so). + pub fn flushOut(l: *Listener) void { + if (comptime !supported) return; + const deadline = nowMs() +| 200; + while (nowMs() < deadline) { + const pending = l.restore_writer.load(.acquire) or for (&l.runner.conns) |*conn| { + if (!conn.live()) continue; + conn.lock(); + const n = conn.engine.output().len; + conn.unlock(); + if (n != 0) break true; + } else false; + if (!pending) break; + nap(1); + } + nap(1); // the last bytes leave the stream's own buffer + } + + pub fn deinit(l: *Listener, gpa: std.mem.Allocator) void { + l.stopping.store(true, .release); + l.unpostFromRegistry(); + if (comptime supported) { + // A connection task may be waiting for the turn; it answers + // nothing more once stopping, but the wait itself must end. + pardes.turn.rest(); + l.edit_watchers.cancel(l.io); + // An answer given as the editor quit (a Kill written to /ctl) + // may still be on its way out: let it go before the cut. + l.flushOut(); + l.runner.stop(); + pardes.turn.wake(); + } + pardes.turn.wake_parked = null; + pardes.turn.answer_held = null; + pardes.turn.stop(); + own_socket_len = 0; // removed here, not again by a signal + if (l.path_len != 0) { + var z: [sun_path_len:0]u8 = undefined; + @memcpy(z[0..l.path_len], l.path_buf[0..l.path_len]); + z[l.path_len] = 0; + _ = libc.unlink(z[0..l.path_len :0]); + } + // The lock after the socket: a socket there never lacks a held lock. + if (l.lock >= 0) release(l.lock, std.mem.sliceTo(&l.lock_buf, 0)); + freeRunner(l.runner); + gpa.destroy(l); + } +}; + +/// The runner's memory, from the page allocator with no fill: an Allocator's +/// `create` writes `undefined` over all of it in a safe build, which would make +/// every page resident at startup. +fn allocRunner() ?*Runner { + if (comptime !supported) return @constCast(&@as(Runner, {})); + const bytes = std.heap.page_allocator.rawAlloc(@sizeOf(Runner), .of(Runner), @returnAddress()) orelse return null; + @import("core").memory.sparse(bytes[0..@sizeOf(Runner)]); + return @ptrCast(@alignCast(bytes)); +} + +fn freeRunner(runner: *Runner) void { + if (comptime !supported) return; + std.heap.page_allocator.rawFree(@as([*]u8, @ptrCast(runner))[0..@sizeOf(Runner)], .of(Runner), @returnAddress()); +} + +/// The socket this process listens on, kept where a fatal signal's handler +/// can reach it without allocating (unlinkOwnSocket); empty when none. +var own_socket: [lock_path_len:0]u8 = undefined; +var own_socket_len: usize = 0; + +/// Removes the socket this process listens on, then its lock (the death +/// lets go of it). Async-signal-safe (two unlinks): SIGTERM and SIGHUP call +/// it before the signal's own death, so a killed editor leaves no socket +/// behind. +pub fn unlinkOwnSocket() void { + if (comptime !supported) return; + const n = own_socket_len; + if (n == 0) return; + own_socket_len = 0; + _ = libc.unlink(own_socket[0..n :0]); + own_socket[n..][0..lock_suffix.len].* = lock_suffix.*; + own_socket[n + lock_suffix.len] = 0; + _ = libc.unlink(own_socket[0 .. n + lock_suffix.len :0]); +} + +/// SIGTERM, SIGHUP and SIGINT with no handler of their own (a GUI's, a +/// detached session's): the socket goes, then the signal's own death. A +/// host that handles them itself (the terminal's Killed, SDL's quit) +/// removes it its own way. +fn armSocketCleanup() void { + const sig = std.posix.SIG; + const on: std.posix.Sigaction = .{ .handler = .{ .handler = onFatalSignal }, .mask = std.posix.sigemptyset(), .flags = std.posix.SA.RESETHAND }; + for ([_]std.posix.SIG{ sig.TERM, sig.HUP, sig.INT }) |s| { + var was: std.posix.Sigaction = undefined; + std.posix.sigaction(s, null, &was); + if (was.handler.handler == sig.DFL) std.posix.sigaction(s, &on, null); + } +} + +/// What else a fatal signal's handler removes (host_io's private prompt +/// files); async-signal-safe. +pub var fatal_cleanup: ?*const fn () void = null; + +fn onFatalSignal(s: std.posix.SIG) callconv(.c) void { + unlinkOwnSocket(); + if (fatal_cleanup) |cleanup| cleanup(); + // Its shells go with it, the daemon's dups let go. + @import("daemon.zig").hangUp(); + // SA_RESETHAND put the default back: the same signal, now fatal. + _ = std.c.raise(s); +} + +/// Removes `pardes-9p-<pid>.sock` files under `dir` whose pid is gone +/// (kill(pid, 0) answers ESRCH): what an editor killed by a signal it could +/// not catch, or a crash, left behind. A live pid's socket, a named +/// session's, anything not a socket of this user's, and one something +/// still answers on are left alone. +pub fn sweepDeadSockets(io: std.Io, dir: [:0]const u8) void { + if (comptime !supported) return; + var names: [8192]u8 = undefined; + const staged = stageNames(io, dir, &names, struct { + fn keep(name: []const u8) bool { + return deadSocketPid(name) != null; + } + }.keep); + var i: usize = 0; + while (i < staged) { + const name = names[i + 1 ..][0..names[i]]; + i += 1 + name.len; + const pid = deadSocketPid(name).?; + if (pid == libc.getpid()) continue; + if (std.posix.errno(std.c.kill(pid, @fromBackingInt(@intCast(0)))) != .SRCH) continue; + var path_buf: [sun_path_len:0]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&path_buf, "{s}/{s}", .{ dir, name }, 0) catch continue; + const facts = statNoFollow(path) orelse continue; + if (facts.mode & 0o170000 != 0o140000 or facts.uid != libc.getuid()) continue; + if (probe(path) == .live) continue; + removeDead(path); + } +} + +/// A socket's lock, `<socket>.lock` beside it: flocked by the socket's owner +/// from before it binds to after it unlinks (`claim`, `release`), so held +/// is alive and free is dead. A connect cannot say that: FreeBSD refuses one +/// to a listener whose queue is full exactly as to a socket nobody listens +/// on, and a starting owner's socket refuses until it listens. +const lock_suffix = ".lock"; +const lock_path_len = sun_path_len + lock_suffix.len; + +fn lockPath(buf: *[lock_path_len:0]u8, sock: []const u8) ?[:0]const u8 { + return std.fmt.bufPrintSentinel(buf, "{s}" ++ lock_suffix, .{sock}, 0) catch null; +} + +/// `sock`'s lock taken (its path in `buf`), made when it is missing: null +/// while someone else holds it. A probe holds it shared for a moment, so a +/// refusal is asked again for a while before it counts. One taken on a file +/// its owner unlinked as it let go is taken again on the one at the path. +fn claim(sock: []const u8, buf: *[lock_path_len:0]u8) ?c_int { + const path = lockPath(buf, sock) orelse return null; + var tries: u32 = 0; + while (tries < 50) : (tries += 1) { + const fd = libc.open(path, .{ .ACCMODE = .RDWR, .CREAT = true, .CLOEXEC = true }, @as(libc.mode_t, 0o600)); + if (fd < 0) return null; + if (libc.flock(fd, std.posix.LOCK.EX | std.posix.LOCK.NB) != 0) { + _ = libc.close(fd); + nap(1); + continue; + } + const io = std.Io.Threaded.global_single_threaded.io(); + const held = (std.Io.File{ .handle = fd, .flags = .{ .nonblocking = false } }).stat(io) catch null; + const named = std.Io.Dir.cwd().statFile(io, path, .{ .follow_symlinks = false }) catch null; + if (held != null and named != null and held.?.inode == named.?.inode) return fd; + _ = libc.close(fd); + } + return null; +} + +/// A claim let go, its lock file removed while still held: nobody else +/// unlinks it, and a claim on the unlinked file is taken again (`claim`). +fn release(fd: c_int, path: [:0]const u8) void { + _ = libc.unlink(path); + _ = libc.close(fd); +} + +/// What `sock`'s lock says of its owner: held is `.live`, free `.stale`, and +/// none at all `.none`. Anything unclear is `.live`. +fn lockState(sock: []const u8) Probe { + var buf: [lock_path_len:0]u8 = undefined; + const path = lockPath(&buf, sock) orelse return .live; + const fd = libc.open(path, .{ .ACCMODE = .RDONLY, .CLOEXEC = true }, @as(libc.mode_t, 0)); + if (fd < 0) return if (libc.errno(fd) == .NOENT) .none else .live; + defer _ = libc.close(fd); + return if (libc.flock(fd, std.posix.LOCK.SH | std.posix.LOCK.NB) == 0) .stale else .live; +} + +/// A dead owner's socket removed, and its lock, under that lock: no new +/// owner can come between the judging and the unlink, and one that took +/// it is gone. +fn removeDead(sock: [:0]const u8) void { + var buf: [lock_path_len:0]u8 = undefined; + const fd = claim(sock, &buf) orelse return; + _ = libc.unlink(sock); + release(fd, std.mem.sliceTo(&buf, 0)); +} + +/// The names in `dir` that `keep` takes (all with none), into `names` as a length byte and +/// the name each, until it is full; the bytes staged. They are staged +/// before anything is unlinked, so a sweep never asks a directory to keep +/// reading while it is being edited. +fn stageNames(io: std.Io, dir: [:0]const u8, names: []u8, comptime keep: ?fn ([]const u8) bool) usize { + const d = std.Io.Dir.openDirAbsolute(io, dir, .{ .iterate = true }) catch return 0; + defer d.close(io); + var read_buf: [std.Io.Dir.Iterator.reader_buffer_len]u8 align(@alignOf(usize)) = undefined; + var reader: std.Io.Dir.Reader = .init(d, &read_buf); + var staged: usize = 0; + while (true) { + const listed = (reader.next(io) catch break) orelse break; + if (listed.name.len == 0 or listed.name.len > 255) continue; + if (keep) |takes| if (!takes(listed.name)) continue; + if (staged + 1 + listed.name.len > names.len) break; + names[staged] = @intCast(listed.name.len); + @memcpy(names[staged + 1 ..][0..listed.name.len], listed.name); + staged += 1 + listed.name.len; + } + return staged; +} + +/// The pid a `pardes-9p-<pid>.sock` name carries, or null for any other. +fn deadSocketPid(name: []const u8) ?std.c.pid_t { + if (!std.mem.startsWith(u8, name, prefix) or !std.mem.endsWith(u8, name, ".sock")) return null; + const digits = name[prefix.len .. name.len - ".sock".len]; + if (digits.len == 0 or digits.len > 10) return null; + for (digits) |c| if (!std.ascii.isDigit(c)) return null; + const pid = std.fmt.parseInt(std.c.pid_t, digits, 10) catch return null; + return if (pid > 0) pid else null; +} + +pub fn socketPath(buf: *[sun_path_len]u8, dir: []const u8, name: []const u8) ?[:0]const u8 { + if (name.len == 0) return null; + if (std.mem.indexOfAny(u8, name, "/\x00") != null) return null; + return std.fmt.bufPrintSentinel(buf, "{s}/" ++ prefix ++ "{s}.sock", .{ dir, name }, 0) catch null; +} + +/// Serves `core`. From here on the calling thread has the editor's turn +/// (`pardes.turn`) and must give it up while it waits. +pub fn listen(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, named: []const u8, fallback: []const u8, tcp_dial: ?[]const u8) !*Listener { + if (comptime !supported) return error.Unsupported; + const entry_name = if (named.len != 0) named else fallback; + // Checked before anything is made: the turn's hooks point at the + // listener from here on, and a path too long for sun_path used to free + // it with them still set, so the editor's next rest called into freed + // memory. The name the socket is first bound at, `<path>.<pid>` (it is + // renamed into place once it listens), must fit too. + refusal = ""; + if (noSocket(entry_name, &refusal_buf)) |words| { + log.warn("{s}", .{words}); + refusal = words; + return error.NameTooLong; + } + var dir_buf: [sun_path_len:0]u8 = undefined; + const dir = socketDir(&dir_buf) orelse { + log.warn("no runtime directory for the socket", .{}); + return error.NoRuntimeDir; + }; + if (!ensureSocketDir(dir)) return error.NoRuntimeDir; + // What dead editors left behind goes first, never a live one's. + sweepDeadSockets(io, dir); + var tmp_buf: [sun_path_len]u8 = undefined; + if (std.mem.indexOfAny(u8, entry_name, "/\x00") != null or entry_name.len == 0) { + log.warn("no socket for {s}: not one path component", .{entry_name}); + return error.NameTooLong; + } + const l = try gpa.create(Listener); + l.* = .{ .io = io, .core = core }; + l.runner = allocRunner() orelse { + gpa.destroy(l); + return error.OutOfMemory; + }; + pardes.turn.start(io); + pardes.turn.wake_parked = Listener.wakeParked; + pardes.turn.answer_held = Listener.answerHeld; + pardes.turn.wake_ctx = l; + l.runner.init(.{ + .io = io, + .root = pardes.ctlfs.root, + .handler = .{ .ctx = l, .serve = Listener.onServe, .opened = Listener.onOpened, .refused = Listener.onRefused }, + .greet_timeout_ms = Listener.greet_deadline_ms, + }); + errdefer l.deinit(gpa); + const p = socketPath(&l.path_buf, dir, entry_name).?; + // The name's lock first, held from here to the end: whoever else holds + // it is alive, however its socket answers a connect. Held, a socket + // there is a dead owner's. + l.lock = claim(p, &l.lock_buf) orelse { + log.warn("something is already listening on {s}", .{p}); + return error.InUse; + }; + // Listened on under a name of its own, then renamed into place: a client + // that waits for the socket to appear finds it listening already. Bound + // at its own name, it was there a moment before listen(2), and a connect + // then was refused. + // unreachable: checked above, before anything was made + const tmp = std.fmt.bufPrintSentinel(&tmp_buf, "{s}.{d}", .{ p, libc.getpid() }, 0) catch unreachable; + const existing = @import("core").filesystem.statPath(io, p, .{ .follow_symlinks = false }) catch null; + if (existing != null and existing.?.kind != .unix_domain_socket) { + log.warn("something is already listening on {s}", .{p}); + return error.InUse; + } + _ = libc.unlink(tmp); + _ = try l.runner.listen(.{ .unix = tmp }, max_conns); + if (libc.chmod(tmp, 0o600) != 0 or libc.rename(tmp, p) != 0) { + _ = libc.unlink(tmp); + return error.ListenFailed; + } + l.path_len = p.len; + if (libc.chmod(p, 0o600) != 0) return error.ListenFailed; + @memcpy(own_socket[0..p.len], p); + own_socket[p.len] = 0; + own_socket_len = p.len; + armSocketCleanup(); + l.postToRegistry(entry_name); + if (tcp_dial) |dial| { + if (!std.mem.startsWith(u8, dial, "tcp!")) return error.BadDial; + const address = networkAddress(dial, true) catch { + log.warn("invalid TCP address {s}", .{dial}); + return error.BadDial; + }; + const bound = l.runner.listen(.{ .tcp = address }, max_conns) catch |err| { + log.warn("cannot listen on {s}: {s}", .{ dial, @errorName(err) }); + return err; + }; + l.tcp_address = canonicalIp(bound); + log.info("serving 9P2000 over TCP on {f}", .{l.tcp_address.?}); + } + log.info("serving 9P2000 on {s}", .{p}); + core.fs.socket_path = l.path(); + core.fs.tcp_address = l.tcp_address; + return l; +} + +/// `noSocket`'s words, kept for `start`'s message. +var refusal_buf: [256]u8 = undefined; +var refusal: []const u8 = ""; + +/// Why a session named `name` gets no socket, in words in `buf` (null: it +/// gets one, or there is no directory, said where it is looked for): the +/// name it is first bound at, `<socketDir>/pardes-9p-<name>.sock.<pid>` and +/// its 0, must fit sun_path, and the directory (made here when missing) +/// must be the user's alone. Asked at a detached start too, whose stderr +/// is the only place anyone reads it. +pub fn noSocket(name: []const u8, buf: []u8) ?[]const u8 { + var dir_buf: [4096]u8 = undefined; + const dir = socketDir(&dir_buf) orelse return null; + var digits: [16]u8 = undefined; + const pid = std.fmt.bufPrint(&digits, "{d}", .{libc.getpid()}) catch unreachable; // a pid fits 16 + const n = dir.len + "/".len + prefix.len + name.len + ".sock.".len + pid.len + 1; + if (n > sun_path_len) + return std.fmt.bufPrint(buf, "9p: the socket path is {d} bytes, {d} is the most: set XDG_RUNTIME_DIR shorter", .{ n, sun_path_len }) catch unreachable; // two numbers fit 256 + if (ensureSocketDir(dir)) return null; + const facts = statNoFollow(dir) orelse return std.fmt.bufPrint(buf, "9p: no socket: {s} is not there and could not be made", .{dir[0..@min(dir.len, 120)]}) catch unreachable; // 120 bytes of path fit 256 + return std.fmt.bufPrint(buf, "9p: no socket: {s} must be a directory of yours alone (mode {o}, owner {d}): chmod 700 it", .{ dir[0..@min(dir.len, 120)], facts.mode & 0o7777, facts.uid }) catch unreachable; // 120 bytes of path and three numbers fit 256 +} + +/// Whether a live session listens under `name` already: a second +/// `--detach=<name>` is refused before anything is made. +pub fn taken(name: []const u8) bool { + if (comptime !supported) return false; + var dir_buf: [sun_path_len:0]u8 = undefined; + const dir = socketDir(&dir_buf) orelse return false; + var buf: [sun_path_len]u8 = undefined; + const p = socketPath(&buf, dir, name) orelse return false; + return probe(p) == .live; +} + +/// What a socket's lock says of its owner (`lockState`): `.live`, `.stale`, +/// or `.none`, no lock at all. Uncertainty belongs to the server that owns +/// the socket, never to a sweeper deciding what to delete: anything unclear +/// is `.live`. +const Probe = enum { none, stale, live }; + +/// Whether whoever made the socket at `path` (or the one a registry entry +/// there names) is alive: its lock decides. With none, no owner holds one, +/// since each claims it before it binds and lets it go after it unlinks: +/// a socket there is a dead one's (a pardes from before the locks, +/// e9328a417, is one to restart). +fn probe(path: [:0]const u8) Probe { + var link_buf: [sun_path_len]u8 = undefined; + const n = libc.readlink(path, &link_buf, link_buf.len); + const sock = if (n > 0 and n < link_buf.len and link_buf[0] == '/') link_buf[0..@intCast(n)] else path; + const locked = lockState(sock); + return if (locked == .none) .stale else locked; +} + +/// Reaps the registry entries whose editor is gone, once per post. +/// +/// A clean exit unposts itself (`unpostFromRegistry`), so everything +/// left behind comes from an exit that could not: an aborted test, a +/// kill, a crash. No code in the dead process can ever run, so the cure +/// has to be somebody else's readdir, and the next editor to start is +/// the somebody. It is only ever a symlink that is followed or +/// unlinked, because that is the only thing `postToRegistry` makes and +/// anything else under a name belongs to whoever put it there; and only +/// a lock let go, or none at all, counts as gone (`probe`). The socket a stale entry points +/// at goes too, but not before a stat agrees it is a socket of ours: a +/// plain file answers a connect with the same refusal. +/// Is this the kind of path this editor is allowed to delete? A registry +/// entry is a symlink we wrote, but its target is just bytes on disk that +/// anyone could have pointed anywhere, so the sweep only ever follows one +/// into the directory our own sockets live in, and only to a name of the +/// shape we give them. Everything else gets its entry removed and its target +/// left strictly alone. +fn ourSocket(target: []const u8, sockets: []const u8) bool { + if (sockets.len == 0 or !std.mem.startsWith(u8, target, sockets)) return false; + if (target.len <= sockets.len or target[sockets.len] != '/') return false; + const base = target[sockets.len + 1 ..]; + if (std.mem.indexOfScalar(u8, base, '/') != null) return false; + return std.mem.startsWith(u8, base, "pardes-9p-") and std.mem.endsWith(u8, base, ".sock"); +} + +fn sweepRegistry(io: std.Io, svc: [:0]const u8, sockets: []const u8) void { + if (comptime !supported) return; + + var names: [4096]u8 = undefined; + const staged = stageNames(io, svc, &names, null); + + var reaped: usize = 0; + var i: usize = 0; + while (i < staged) { + const name = names[i + 1 ..][0..names[i]]; + i += 1 + name.len; + var entry_buf: [sun_path_len:0]u8 = undefined; + const entry = std.fmt.bufPrintSentinel(&entry_buf, "{s}/{s}", .{ svc, name }, 0) catch continue; + const facts = statNoFollow(entry) orelse continue; + if (facts.mode & 0o170000 != 0o120000) continue; // not a symlink: not ours to judge + const state = probe(entry); + if (state == .live) continue; + var link_buf: [sun_path_len]u8 = undefined; + const n = libc.readlink(entry, &link_buf, link_buf.len); + if (libc.unlink(entry) != 0) continue; + reaped += 1; + // A relative target would resolve against this editor's working + // directory, which says nothing about what the entry named, and a + // readlink that exactly filled the buffer was truncated, so the path + // it produced is some other file's. + if (state != .stale or n <= 0 or link_buf[0] != '/') continue; + if (@as(usize, @intCast(n)) >= link_buf.len) continue; + var target_buf: [sun_path_len:0]u8 = undefined; + const target = std.fmt.bufPrintSentinel(&target_buf, "{s}", .{link_buf[0..@intCast(n)]}, 0) catch continue; + if (!ourSocket(target, sockets)) continue; + const t = statNoFollow(target) orelse continue; + if (t.mode & 0o170000 != 0o140000 or t.uid != libc.getuid()) continue; + // Judged again, under its lock: the first probe was of the ENTRY and + // is by now several syscalls old, and a new owner may have claimed + // the name since. + removeDead(target); + } + if (reaped != 0) log.info("reaped {d} stale registry entries under {s}", .{ reaped, svc }); +} + +const testing = std.testing; + +extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8; + +/// A test's own runtime directory under /tmp, which XDG_RUNTIME_DIR names +/// until `deinit` puts the old value back and removes it, with what the +/// listener left in it. +const TempRuntime = struct { + directory: [64:0]u8 = undefined, + old: ?[:0]u8 = null, + + fn init(t: *TempRuntime, comptime name: []const u8) !void { + _ = try std.fmt.bufPrintSentinel(&t.directory, "/tmp/pardes-" ++ name ++ "-XXXXXX", .{}, 0); + if (mkdtemp(&t.directory) == null) return error.TempDirectoryFailed; + if (libc.getenv("XDG_RUNTIME_DIR")) |v| t.old = try testing.allocator.dupeSentinel(u8, std.mem.span(v), 0); + try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &t.directory, 1)); + } + + fn deinit(t: *TempRuntime) void { + if (t.old) |v| { + _ = setenv("XDG_RUNTIME_DIR", v, 1); + testing.allocator.free(v); + } else _ = @import("core").host_io.testUnsetenv("XDG_RUNTIME_DIR"); + std.Io.Dir.cwd().deleteTree(testing.io, std.mem.sliceTo(&t.directory, 0)) catch |err| std.debug.print("deleteTree: {s}\n", .{@errorName(err)}); + } +}; + +extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; + +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; + }; +} + +/// 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("core").host_io.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); +} + +/// $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/<name>`, 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("core").host_io.ChildEnv.exportVar("PARDES_MOUNT", mount); + } else |_| {} + } + @import("core").host_io.ChildEnv.exportVar("PARDES_MOUNT", 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; + + pub const max_depth: usize = 2 * ninep.max_welem; + + 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, + }; + + 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); + } + + 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)); + } + + /// 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)); + } + + /// 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 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); + } + + /// 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); + } + + 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); + } + + 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 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 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; + } + + 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 }; + } + + pub fn validateDial(dial: []const u8) error{BadDial}!void { + var buf: [sun_path_len]u8 = undefined; + _ = try resolve(&buf, 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, + + 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, + }; + } + + 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)); + } + } + + 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))); + } + } + + 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; + } + + fn drop(s: *Session, fid: u32) void { + _ = s.cl.submit(.{ .clunk = .{ .fid = fid } }) catch return; + _ = s.settle() catch {}; + } + + fn dropNoWait(s: *Session, fid: u32) void { + const tag = s.cl.submit(.{ .clunk = .{ .fid = fid } }) catch return; + s.unwaited |= bit(tag); + s.flush() catch {}; + } + + fn send(s: *Session, req: ninep.Client.Request) Error!u16 { + return s.cl.submit(req) catch Error.Botch; + } + + fn newFid(s: *Session) u32 { + const fid = s.next_fid; + s.next_fid = if (fid >= 1 << 20) 1 else fid + 1; + return fid; + } + + /// 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 } })); + } + + /// 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, + } + } + } + }; + + fn bit(tag: u16) u32 { + return @as(u32, 1) << @intCast(tag); + } + + /// 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, + + 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, + }; + + 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]; + } + + 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; + } + + fn get(f: *Flight, step: Step) ?*Item { + for (f.items[0..f.len]) |*item| if (item.step == step) return item; + return null; + } + + fn open(f: *const Flight) bool { + for (f.items[0..f.len]) |item| if (!item.done) return true; + return false; + } + + /// 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 => {}, + }; + } + + /// 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; + } + + /// 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; + } + }; + + /// 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; + + /// 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; + } + }; + + /// 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(); + + 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; + } + + 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 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); + } + + 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(); + } + + /// 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 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); + } + + /// 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); + + fn hold() void { + while (!lock.tryLock()) std.atomic.spinLoopHint(); + } + + /// 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 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 put(s: *Session) bool { + hold(); + defer lock.unlock(); + for (&slots) |*slot| if (slot.* == null) { + slot.* = s; + return true; + }; + return false; + } + }; + + /// 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); + } + } + + /// 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; + } + } + + 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 poll_in: i16 = @intCast(libc.POLL.IN); + const poll_out: i16 = @intCast(libc.POLL.OUT); + + 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]); + + 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)); + + try testing.expectEqual(@as(usize, 0), try elements("/", &out)); + + 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)); + } + + 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; + + 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); + + 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); + + try testing.expectError(error.BadDial, resolve(&buf, "")); + try testing.expectError(error.BadDial, resolve(&buf, "/tmp/a\x00b")); + } + + 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); + } + + 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); + } + + 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 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 "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)); +} + +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")); +} + +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); +} + +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); +} + +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("core").filesystem.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'; + + 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, + + fn run(w: *@This()) void { + defer w.done.store(true, .release); + w.check() catch |err| { + w.failure = err; + }; + } + + 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); + } + }; + + 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); + } + } +} + +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(); + + 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}); + + 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 }; + + // 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); + + // 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); +} + +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(); + + 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'; + + 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); + } + 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()); +} + +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(); + + 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); + } + // 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 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); + + // 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); + + // 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); + + // 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); + + // 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("core").host_io.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("core").host_io.ChildEnv.exported("PARDES_MOUNT").?); + // A mount of another server (9ns --unix): not this session's. + _ = @import("core").host_io.testUnsetenv("NINE_MNTGEN"); + exportMount("/run/user/1000/pardes-9p-work.sock"); + try testing.expect(@import("core").host_io.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); + } + defer for (names, saved) |name, value| { + if (value) |old| { + _ = setenv(name, old, 1); + testing.allocator.free(old); + } else _ = @import("core").host_io.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("core").host_io.testUnsetenv("XDG_RUNTIME_DIR"); + _ = setenv("HOME", "/home/example", 1); + try testing.expectEqualStrings("/home/example/.local/state/pardes", socketDir(&dir).?); + _ = @import("core").host_io.testUnsetenv("HOME"); + try testing.expect(socketDir(&dir) == null); + + // 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()))}); + + // 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("core").host_io.ChildEnv.exported("PARDES_9P").?); + try testing.expectEqualStrings("7", @import("core").host_io.ChildEnv.exported("PARDES_PANE").?); + try testing.expect(@import("core").host_io.ChildEnv.exported("PARDES_PID") == null); + + exportEnv(8, true); + try testing.expectEqualStrings("8", @import("core").host_io.ChildEnv.exported("PARDES_PANE").?); + try testing.expectEqualStrings(own_pid, @import("core").host_io.ChildEnv.exported("PARDES_PID").?); + + // 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("core").host_io.ChildEnv.exported("PARDES_9P") == null); + try testing.expect(@import("core").host_io.ChildEnv.exported("PARDES_PANE") == null); + try testing.expectEqualStrings(own_pid, @import("core").host_io.ChildEnv.exported("PARDES_PID").?); + + exportEnv(0, false); + try testing.expect(@import("core").host_io.ChildEnv.exported("PARDES_9P") == null); + try testing.expect(@import("core").host_io.ChildEnv.exported("PARDES_PANE") == null); + try testing.expect(@import("core").host_io.ChildEnv.exported("PARDES_PID") == null); +} |
