summaryrefslogtreecommitdiff
path: root/src/9p_io.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/9p_io.zig')
-rw-r--r--src/9p_io.zig1915
1 files changed, 1559 insertions, 356 deletions
diff --git a/src/9p_io.zig b/src/9p_io.zig
index 090eb838..a089a7cf 100644
--- a/src/9p_io.zig
+++ b/src/9p_io.zig
@@ -11,10 +11,7 @@ const quic = if (quic_enabled) @import("9p_quic.zig") else struct {};
const log = std.log.scoped(.ninep);
-pub const darwin = switch (builtin.os.tag) {
- .macos, .ios, .tvos, .watchos, .visionos => true,
- else => false,
-};
+pub const darwin = builtin.os.tag.isDarwin();
pub const supported = builtin.os.tag == .linux or darwin;
pub const sun_path_len = @typeInfo(@FieldType(libc.sockaddr.un, "path")).array.len;
@@ -47,24 +44,19 @@ pub fn statNoFollow(path: [:0]const u8) ?FileFacts {
pub fn ensureSocketDir(dir: [:0]const u8) bool {
if (dir.len == 0) return false;
- var partial: [sun_path_len:0]u8 = undefined;
- @memcpy(partial[0 .. dir.len + 1], dir[0 .. dir.len + 1]);
- for (1..dir.len) |i| {
- if (dir[i] != '/') continue;
- partial[i] = 0;
- _ = libc.mkdir(partial[0..i :0], 0o700);
- partial[i] = '/';
- }
- _ = libc.mkdir(dir, 0o700);
+ // 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: u32 = 8192;
+pub const msize = ninep.msize;
-pub const max_conns = 4;
+/// 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;
@@ -143,76 +135,17 @@ const Srv = ninep.Server(pardes.ctlfs, ninep.editor);
/// 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 hands every backend request to the editor's thread
-/// through `Slot` (see `tick`). QUIC keeps the poll loop below.
+/// 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. QUIC
+/// keeps the poll loop below, on the editor's thread.
const Runner = if (supported) cloud9.serve.Runner(pardes.ctlfs, ninep.editor, .{
.msize = msize,
.connections = max_conns,
.listeners = 2,
}) else void;
-/// Answers one backend request on the editor's thread.
-fn step(core: *pardes.Pardes, srv: *Srv, req: pardes.ctlfs.Req) void {
- core.update(.{ .fs_req = req });
- var answered = false;
- while (core.nextEffect()) |effect| {
- if (effect == .fs_reply) {
- const reply = effect.fs_reply;
- if (reply.tag == req.tag) answered = true;
- srv.reply(&reply, core.fsPayload(reply));
- } else core.perform(effect);
- }
- if (!answered) {
- const reply = pardes.ctlfs.Reply.fail(req.tag, pardes.ctlfs.E.IO);
- srv.reply(&reply, "");
- }
-}
-
-/// Pays the backend a request whose connection is gone; replies go nowhere.
-fn pay(core: *pardes.Pardes, req: pardes.ctlfs.Req) void {
- core.update(.{ .fs_req = req });
- while (core.nextEffect()) |effect| if (effect != .fs_reply) core.perform(effect);
-}
-
-/// One runner slot's mailbox: the requests its connection task handed over,
-/// answered by the editor's thread in `tick`. A hangup's releases (at most
-/// one per fid) and one outstanding request are the most it ever holds,
-/// because a closing connection waits for its entries to be paid before
-/// the slot is reused.
-const Slot = struct {
- const capacity = ninep.editor.fid_capacity + 2;
- const Entry = struct { gen: u32, req: pardes.ctlfs.Req };
-
- mutex: std.Io.Mutex = .init,
- queue: [capacity]Entry = undefined,
- head: usize = 0,
- len: usize = 0,
- /// Bumped when a connection takes the slot; entries carry the value.
- gen: u32 = 0,
- closing: bool = false,
- drained: std.Io.Event = .unset,
- /// Requests refused for want of room; never expected.
- refused: u32 = 0,
-
- fn push(s: *Slot, req: pardes.ctlfs.Req) bool {
- if (s.len == s.queue.len) {
- s.refused += 1;
- return false;
- }
- s.queue[(s.head + s.len) % capacity] = .{ .gen = s.gen, .req = req };
- s.len += 1;
- return true;
- }
-
- fn pop(s: *Slot) ?Entry {
- if (s.len == 0) return null;
- const e = s.queue[s.head];
- s.head = (s.head + 1) % capacity;
- s.len -= 1;
- return e;
- }
-};
-
const quic_slots = if (quic_enabled) max_conns else 0;
/// A QUIC connection on the poll path.
@@ -229,18 +162,43 @@ const accept_pause_ms: i64 = 100;
pub const Listener = struct {
io: std.Io,
- runner: Runner = undefined,
- slots: [max_conns]Slot = @splat(.{}),
+ /// 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),
- /// While `reset` runs: the generation each slot is paying off; anything
- /// newer waits for the replacement core.
- resetting: ?[max_conns]u32 = null,
+ /// 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,
quic: if (quic_enabled) ?quic.Listener else void = if (quic_enabled) null else {},
quic_address: ?std.Io.net.IpAddress = null,
paused_ms: i64 = 0,
path_buf: [sun_path_len]u8 = undefined,
path_len: usize = 0,
+ /// 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,
conns: [quic_slots]Conn = @splat(.{}),
control: [2]c_int = .{ -1, -1 },
watcher: ?std.Thread = null,
@@ -260,47 +218,285 @@ pub const Listener = struct {
return @ptrCast(@alignCast(ctx.?));
}
- /// The runner's handler: on the connection's task.
+ /// 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 = of(ctx);
+ _ = l.refused.fetchAdd(1, .acq_rel);
+ l.kick();
+ }
+
fn onOpened(ctx: ?*anyopaque, conn: *Runner.Conn) void {
const l = of(ctx);
- const s = &l.slots[conn.index];
- s.mutex.lockUncancelable(l.io);
- s.gen +%= 1;
- s.mutex.unlock(l.io);
+ 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 = of(ctx);
- const s = &l.slots[conn.index];
- s.mutex.lockUncancelable(l.io);
- const queued = s.push(req);
- s.mutex.unlock(l.io);
- if (!queued) {
- const reply = pardes.ctlfs.Reply.fail(req.tag, pardes.ctlfs.E.IO);
- conn.reply(&reply, "");
+ 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("ninep/ctl.zig").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, "");
}
- /// Holds the slot until the editor's thread has paid what the
- /// connection still owes, so a successor never inherits its entries.
- fn onClosed(ctx: ?*anyopaque, conn: *Runner.Conn) void {
+ /// 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 = of(ctx);
- const s = &l.slots[conn.index];
if (l.stopping.load(.acquire)) return;
- s.mutex.lockUncancelable(l.io);
- if (s.len == 0) {
- s.mutex.unlock(l.io);
+ const core = l.core;
+ const refused = l.refused.swap(0, .acq_rel);
+ if (refused != 0) {
+ var why: [64]u8 = undefined;
+ pardes.ctlfs.events.notePath(core, "err -", std.fmt.bufPrint(&why, "9p: too many connections ({d} turned away)", .{refused}) catch "9p: too many connections");
+ }
+ 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("edit_cmd.zig").cancel(core);
+ l.kick();
return;
}
- s.closing = true;
- s.drained.reset();
- s.mutex.unlock(l.io);
- l.kick();
- s.drained.wait(l.io) catch {};
}
+ /// 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 = of(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);
@@ -407,68 +603,47 @@ pub const Listener = struct {
c.draining = true;
}
- pub const Drained = struct { count: usize = 0, pending: bool = false };
+ /// What one QUIC poll pass did: `pending` says there is more to do at
+ /// once.
+ pub const Polled = struct { count: usize = 0, pending: bool = false };
- /// Answers what every connection has asked since the last call, retries
- /// their parked reads against the editor's current state and has the
- /// runner send the replies. Call it after each editor update, on the
- /// editor's thread; `pending` says QUIC has more to do right away.
- pub fn drain(l: *Listener, core: *pardes.Pardes) Drained {
- core.fs.socket_path = l.path();
- core.fs.tcp_address = l.tcp_address;
- core.fs.quic_address = l.quic_address;
- l.expire();
- var result: Drained = .{};
- if (comptime supported) {
- for (&l.slots, 0..) |*s, i| {
- const conn = &l.runner.conns[i];
- s.mutex.lockUncancelable(l.io);
- const only: ?u32 = if (l.resetting) |gens| gens[i] else null;
- var count: usize = 0;
- if (s.len != 0 or conn.live()) {
- conn.lock();
- while (s.len != 0) {
- if (only) |gen| if (s.queue[s.head].gen != gen) break;
- const e = s.pop().?;
- if (e.gen == s.gen) step(core, &conn.engine, e.req) else if (e.req.op == .release) pay(core, e.req);
- count += 1;
- }
- if (conn.live() and (only == null or only.? == s.gen)) {
- while (conn.engine.retry()) |req| {
- step(core, &conn.engine, req);
- count += 1;
- }
- while (conn.engine.next()) |req| {
- step(core, &conn.engine, req);
- count += 1;
- }
- }
- const owed = conn.engine.output().len != 0;
- conn.unlock();
- if (owed) conn.flush();
- }
- if (s.closing and s.len == 0) {
- s.closing = false;
- s.drained.set(l.io);
- }
- s.mutex.unlock(l.io);
- result.count += count;
- if (count != 0) l.collectOs(core);
- }
- }
+ /// Answers one QUIC request, on the editor's thread.
+ fn step(l: *Listener, srv: *Srv, req: pardes.ctlfs.Req) void {
+ l.core.fs.write_room = if (srv.protocol.msize > 24) srv.protocol.msize - 24 else 0;
+ const reply = l.core.serveFs(req);
+ srv.reply(&reply, l.core.fsPayload(reply));
+ if (req.op == .release) l.collectOs();
+ }
+
+ /// QUIC's events, accepts, reads, requests and replies, on the editor's
+ /// thread after each of its steps; `pending` says there is more to do
+ /// right away. Unix and TCP need none of this.
+ pub fn tick(l: *Listener) Polled {
+ var result: Polled = .{};
+ // Between the editor's steps, which is when the table may be swept:
+ // the editor's own directory listings leave nodes in it too.
+ l.collectOs();
if (comptime quic_enabled) {
+ if (l.quic) |*listener| listener.events() catch |err| {
+ log.warn("QUIC listener stopped: {s}", .{@errorName(err)});
+ for (&l.conns, 0..) |*conn, i| if (conn.quic != null) l.drop(@intCast(i));
+ listener.deinit();
+ l.quic = null;
+ l.quic_address = null;
+ };
+ l.accept();
+ for (0..quic_slots) |i| if (l.live(@intCast(i))) l.fill(@intCast(i));
+ l.expire();
for (&l.conns, 0..) |*conn, i| {
if (!l.live(@intCast(i)) and !conn.draining) continue;
var count: usize = 0;
while (conn.srv.retry()) |req| {
- step(core, &conn.srv, req);
- l.collectOs(core);
+ l.step(&conn.srv, req);
count += 1;
}
while (count < 64) {
const req = conn.srv.next() orelse break;
- step(core, &conn.srv, req);
- l.collectOs(core);
+ l.step(&conn.srv, req);
count += 1;
}
result.count += count;
@@ -478,64 +653,75 @@ pub const Listener = struct {
} else l.flush(@intCast(i));
if (conn.quic) |*connection| result.pending = result.pending or connection.pending();
}
+ l.arm();
}
return result;
}
- /// `drain` plus the QUIC listener's events, accepts and reads.
- pub fn tick(l: *Listener, core: *pardes.Pardes) Drained {
- if (comptime quic_enabled) {
- if (l.quic) |*listener| listener.events() catch |err| {
- log.warn("QUIC listener stopped: {s}", .{@errorName(err)});
- for (&l.conns, 0..) |*conn, i| if (conn.quic != null) l.drop(@intCast(i));
- listener.deinit();
- l.quic = null;
- l.quic_address = null;
- };
- l.accept();
- for (0..quic_slots) |i| if (l.live(@intCast(i))) l.fill(@intCast(i));
- }
- const result = l.drain(core);
- l.arm();
- return result;
- }
-
- /// Hangs every connection up and pays `core` what they owed, so that a
- /// replacement core starts with no client holding anything. A client
- /// that connects meanwhile is not answered until the caller has put
- /// the replacement in and calls `tick` again.
- pub fn reset(l: *Listener, core: *pardes.Pardes) void {
+ /// 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;
for (0..quic_slots) |i| l.drop(@intCast(i));
+ if (comptime quic_enabled) while (l.tick().pending) {};
+ l.core = replacement;
+ replacement.fs.socket_path = l.path();
+ replacement.fs.tcp_address = l.tcp_address;
+ replacement.fs.quic_address = l.quic_address;
if (comptime supported) {
- var gens: [max_conns]u32 = undefined;
- for (&l.slots, &gens) |*s, *gen| {
- s.mutex.lockUncancelable(l.io);
- gen.* = s.gen;
- s.mutex.unlock(l.io);
+ // 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();
+ const flush_by = nowMs() +| 200;
+ while (nowMs() < flush_by) {
+ const pending = l.restore_writer.load(.acquire) or for (&l.runner.conns) |*conn| {
+ if (!l.cutting(conn)) continue;
+ conn.lock();
+ const n = conn.engine.output().len;
+ conn.unlock();
+ if (n != 0) break true;
+ } else false;
+ if (!pending) break;
+ Client.nap(1);
}
- l.resetting = gens;
- defer l.resetting = null;
- l.runner.closeAll();
+ for (&l.runner.conns) |*conn| if (l.cutting(conn)) conn.close();
const deadline = nowMs() +| 2000;
- while (true) {
- const drained = l.drain(core);
- var idle = !drained.pending;
- for (&l.slots, &gens, 0..) |*s, gen, i| {
- s.mutex.lockUncancelable(l.io);
- if (s.len != 0 and s.queue[s.head].gen == gen) idle = false;
- if (s.gen == gen and l.runner.conns[i].live()) idle = false;
- s.mutex.unlock(l.io);
- }
- if (idle or nowMs() >= deadline) break;
+ while (nowMs() < deadline) {
+ const left = for (&l.runner.conns) |*conn| {
+ if (l.cutting(conn)) break true;
+ } else false;
+ if (!left) break;
Client.nap(1);
}
- } else while (l.drain(core).pending) {}
- l.collectOs(core);
+ pardes.turn.wake();
+ l.cut_before = 0;
+ }
+ l.collectOs();
for (&l.conns) |*conn| conn.accepted_ms = 0;
l.arm();
}
- fn collectOs(l: *Listener, core: *pardes.Pardes) void {
+ /// 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 {
+ const core = l.core;
var i: usize = 0;
while (i < core.fs.os_paths.items.len) {
const entry = core.fs.os_paths.items[i];
@@ -548,17 +734,10 @@ pub const Listener = struct {
}
}
- /// Whether any connection, or a release still to be paid, names `node`.
+ /// Whether any connection names `node`.
fn references(l: *Listener, node: u64) bool {
if (comptime supported) {
- for (&l.slots, 0..) |*s, i| {
- const conn = &l.runner.conns[i];
- s.mutex.lockUncancelable(l.io);
- defer s.mutex.unlock(l.io);
- var k: usize = 0;
- while (k < s.len) : (k += 1) {
- if (s.queue[(s.head + k) % Slot.capacity].req.node == node) return true;
- }
+ for (&l.runner.conns) |*conn| {
if (!conn.live()) continue;
conn.lock();
defer conn.unlock();
@@ -572,16 +751,12 @@ pub const Listener = struct {
return false;
}
- /// Where the runner's tasks report that a request waits for `tick`.
- pub fn setWake(l: *Listener, ctx: ?*anyopaque, wake: *const fn (?*anyopaque) void) void {
+ /// Where the runner's tasks report that a request changed the core,
+ /// and for QUIC a thread that watches its socket and calls `wake` when
+ /// `tick` has something to read.
+ pub fn watch(l: *Listener, ctx: ?*anyopaque, wake: *const fn (?*anyopaque) void) !void {
l.wake_ctx = ctx;
l.wake = wake;
- }
-
- /// `setWake`, and for QUIC a thread that watches its socket and calls
- /// `wake` when `tick` has something to read.
- pub fn watch(l: *Listener, ctx: ?*anyopaque, wake: *const fn (?*anyopaque) void) !void {
- l.setWake(ctx, wake);
if (comptime !quic_enabled) return;
if (l.quic == null or l.watcher != null) return;
if (libc.pipe(&l.control) != 0) return error.PipeFailed;
@@ -592,7 +767,7 @@ pub const Listener = struct {
}
for (l.control) |fd| {
setCloexec(fd);
- setNonblock(fd);
+ _ = setNonblock(fd);
}
l.arm();
l.watcher = try std.Thread.spawn(.{}, watchQuic, .{l});
@@ -635,8 +810,92 @@ pub const Listener = struct {
}
}
+ /// 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(&reg_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;
+ }
+
pub fn deinit(l: *Listener, gpa: std.mem.Allocator) void {
l.stopping.store(true, .release);
+ l.unpostFromRegistry();
if (l.watcher) |thread| {
l.watch_stop.store(true, .release);
_ = libc.write(l.control[1], "q", 1);
@@ -647,24 +906,169 @@ pub const Listener = struct {
if (comptime quic_enabled) {
if (l.quic) |*listener| listener.deinit();
}
- if (comptime supported) l.runner.stop();
+ 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.
+ const deadline = nowMs() +| 200;
+ while (nowMs() < deadline) {
+ const pending = 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;
+ Client.nap(1);
+ }
+ Client.nap(1); // the last bytes leave the stream's own buffer
+ 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]);
}
+ 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("memory.zig").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: [sun_path_len:0]u8 = undefined;
+var own_socket_len: usize = 0;
+
+/// Removes the socket this process listens on. Async-signal-safe (one
+/// unlink): 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]);
+}
+
+/// SIGTERM and SIGHUP with no handler of their own (a GUI's): the socket
+/// goes, then the signal's own death. A host that handles them itself (the
+/// terminal's Killed, the detached server'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 }) |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();
+ // 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;
+ _ = libc.unlink(path);
+ }
+}
+
+/// 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;
}
-pub fn listen(io: std.Io, gpa: std.mem.Allocator, named: []const u8, fallback: []const u8, tcp_dial: ?[]const u8, quic_dial: ?[]const u8) ?*Listener {
+/// 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, quic_dial: ?[]const u8) ?*Listener {
if (comptime !supported) return null;
var dir_buf: [sun_path_len:0]u8 = undefined;
const dir = socketDir(&dir_buf) orelse {
@@ -672,20 +1076,59 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, named: []const u8, fallback: [
return null;
};
if (!ensureSocketDir(dir)) return null;
+ // What dead editors left behind goes first, never a live one's.
+ sweepDeadSockets(io, dir);
+ 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.
+ var path_check: [sun_path_len]u8 = undefined;
+ if (socketPath(&path_check, dir, entry_name) == null) {
+ log.warn("no socket for {s}: the path is too long for a unix socket", .{entry_name});
+ return null;
+ }
const l = gpa.create(Listener) catch return null;
- l.* = .{ .io = io };
+ l.* = .{ .io = io, .core = core };
+ l.runner = allocRunner() orelse {
+ gpa.destroy(l);
+ return null;
+ };
+ 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, .closed = Listener.onClosed },
+ .handler = .{ .ctx = l, .serve = Listener.onServe, .opened = Listener.onOpened, .refused = Listener.onRefused },
.greet_timeout_ms = Listener.greet_deadline_ms,
});
- const p = socketPath(&l.path_buf, dir, if (named.len != 0) named else fallback) orelse {
- gpa.destroy(l);
- return null;
- };
- _ = l.runner.listen(.{ .unix = p }, max_conns) catch |err| retry: {
- const existing = std.Io.Dir.cwd().statFile(io, p, .{ .follow_symlinks = false }) catch null;
+ const p = socketPath(&l.path_buf, dir, entry_name).?;
+ // 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. (No room for the longer name: bound in place.)
+ var tmp_buf: [sun_path_len]u8 = undefined;
+ if (std.fmt.bufPrintSentinel(&tmp_buf, "{s}.{d}", .{ p, libc.getpid() }, 0) catch null) |tmp| {
+ const existing = @import("fs.zig").statPath(io, p, .{ .follow_symlinks = false }) catch null;
+ if (existing != null and (existing.?.kind != .unix_domain_socket or alive(p))) {
+ log.warn("something is already listening on {s}", .{p});
+ l.deinit(gpa);
+ return null;
+ }
+ _ = libc.unlink(tmp);
+ _ = l.runner.listen(.{ .unix = tmp }, max_conns) catch {
+ l.deinit(gpa);
+ return null;
+ };
+ if (libc.chmod(tmp, 0o600) != 0 or libc.rename(tmp, p) != 0) {
+ _ = libc.unlink(tmp);
+ l.deinit(gpa);
+ return null;
+ }
+ } else _ = l.runner.listen(.{ .unix = p }, max_conns) catch |err| retry: {
+ const existing = @import("fs.zig").statPath(io, p, .{ .follow_symlinks = false }) catch null;
if (err != error.AddressInUse or existing == null or existing.?.kind != .unix_domain_socket or alive(p)) {
log.warn("something is already listening on {s}", .{p});
l.deinit(gpa);
@@ -705,6 +1148,11 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, named: []const u8, fallback: [
l.deinit(gpa);
return null;
}
+ @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!")) {
l.deinit(gpa);
@@ -748,17 +1196,145 @@ pub fn listen(io: std.Io, gpa: std.mem.Allocator, named: []const u8, fallback: [
}
}
log.info("serving 9P2000 on {s}", .{p});
+ core.fs.socket_path = l.path();
+ core.fs.tcp_address = l.tcp_address;
+ core.fs.quic_address = l.quic_address;
return l;
}
const alive = transport.isListening;
-fn setNonblock(fd: c_int) void {
+/// What is at a socket path, in cloud9's three answers (`post.Probe`).
+/// Asked here with libc rather than through `cloud9.post.probe`, whose
+/// raw Linux syscalls darwin cannot compile, but the classification is
+/// that file's and must not become a second opinion: the one definite
+/// refusal is `.stale`, nothing at the path at all is `.none`, and
+/// everything else — connected, busy, refused permission, a surprise —
+/// is `.live`. Uncertainty belongs to the server that owns the socket,
+/// never to a sweeper deciding what to delete.
+const Probe = enum { none, stale, live };
+
+/// `SOCK.STREAM`, and the kernel's own non-blocking bit where there is
+/// one. Darwin's `SOCK.NONBLOCK` is a Zig shim for `std.posix.socket`
+/// to unpack, not an ABI value, so handing it to the raw libc call
+/// would ask a kernel that has never heard of it; there it is an
+/// `fcntl` instead.
+const probe_socket_kind: c_uint = libc.SOCK.STREAM | (if (darwin) 0 else libc.SOCK.NONBLOCK);
+
+fn probe(path: [:0]const u8) Probe {
+ if (path.len + 1 > sun_path_len) return .live; // cannot ask; assume occupied
+ var addr: libc.sockaddr.un = .{ .path = @splat(0) };
+ @memcpy(addr.path[0 .. path.len + 1], path[0 .. path.len + 1]);
+ const fd = libc.socket(libc.AF.UNIX, probe_socket_kind, 0);
+ if (fd < 0) return .live;
+ defer _ = libc.close(fd);
+
+ // Non-blocking is the whole safety of this function, so it is read
+ // back rather than assumed: a BLOCKING connect to a live server
+ // whose backlog is full parks in the kernel with no timeout to end
+ // it — measured, it simply never returns — and a sweep that parks
+ // takes the editor's startup with it. An fd that cannot be proven
+ // non-blocking is never connected at all, which lands on `.live`,
+ // the answer that deletes nothing.
+ if (comptime darwin) _ = setNonblock(fd);
+ if (!isNonblocking(fd)) return .live;
+
+ if (libc.connect(fd, @ptrCast(&addr), @sizeOf(@TypeOf(addr))) == 0) return .live;
+ // cloud9's `post.probe` mapping, answer for answer. The connection
+ // is never wanted: EAGAIN (a full Unix backlog) and EINPROGRESS say
+ // somebody is listening, which is all that was asked, and the fd
+ // closes without ever waiting for it to become writable.
+ return switch (libc.errno(-1)) {
+ .AGAIN, .INPROGRESS, .PERM, .ACCES => .live,
+ .CONNREFUSED => .stale,
+ .NOENT, .NOTDIR => .none,
+ else => .live,
+ };
+}
+
+/// 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 definite refusal counts as gone. 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;
+ // Ask again, immediately before deleting. The first probe was of the
+ // ENTRY and is by now several syscalls old; a socket that is bound but
+ // has not reached listen(2) yet answers ECONNREFUSED exactly like a
+ // dead one, and that window is every server's startup.
+ if (probe(target) != .stale) continue;
+ _ = libc.unlink(target);
+ }
+ if (reaped != 0) log.info("reaped {d} stale registry entries under {s}", .{ reaped, svc });
+}
+
+/// Whether it worked, because `probe` is not allowed to find out the
+/// hard way: a caller that needs the guarantee has to be able to check.
+fn setNonblock(fd: c_int) bool {
const flags = libc.fcntl(fd, libc.F.GETFL, @as(c_int, 0));
- if (flags < 0) return;
+ if (flags < 0) return false;
var o: libc.O = @bitCast(@as(u32, @bitCast(flags)));
o.NONBLOCK = true;
- _ = libc.fcntl(fd, libc.F.SETFL, @as(c_int, @bitCast(@as(u32, @bitCast(o)))));
+ return libc.fcntl(fd, libc.F.SETFL, @as(c_int, @bitCast(@as(u32, @bitCast(o))))) >= 0;
+}
+
+fn isNonblocking(fd: c_int) bool {
+ const flags = libc.fcntl(fd, libc.F.GETFL, @as(c_int, 0));
+ if (flags < 0) return false;
+ const o: libc.O = @bitCast(@as(u32, @bitCast(flags)));
+ return o.NONBLOCK;
}
const testing = std.testing;
@@ -777,6 +1353,158 @@ test "a name that is not one path component is no address at all" {
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);
+ defer {
+ for ([_][:0]const u8{ live_sock, dead_sock, live, dead, stranger, outsider_sock, outsider }) |p| _ = libc.unlink(p);
+ _ = libc.rmdir(svc);
+ }
+
+ 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));
+
+ // Bound and then dropped: the file stays, and nobody answers it —
+ // exactly what an aborted test leaves behind.
+ const abandoned = bindSocket(dead_sock);
+ try testing.expect(abandoned >= 0);
+ _ = libc.close(abandoned);
+
+ 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 = "" });
+
+ // The guarantee itself, asserted rather than inferred from the fact
+ // that the test finished: if a probe socket ever stops being
+ // non-blocking the connect below parks instead of failing, and a
+ // parked test costs whoever is building far more than a red one.
+ const checking = libc.socket(libc.AF.UNIX, probe_socket_kind, 0);
+ try testing.expect(checking >= 0);
+ if (comptime darwin) try testing.expect(setNonblock(checking));
+ try testing.expect(isNonblocking(checking));
+ _ = libc.close(checking);
+
+ // The listener's backlog is filled before anything is asked, because
+ // a full backlog is the one place a blocking connect parks forever
+ // (`unix_wait_for_peer`, no timeout) and a sweep that only answers
+ // while nobody is queued is the sweep that hangs a build. Nothing
+ // below accepts any of these, so the queue stays full throughout.
+ var queued: [8]c_int = @splat(-1);
+ defer for (queued) |fd| {
+ if (fd >= 0) _ = libc.close(fd);
+ };
+ for (&queued) |*slot| {
+ const fd = libc.socket(libc.AF.UNIX, probe_socket_kind, 0);
+ if (fd < 0) break;
+ if (comptime darwin) _ = setNonblock(fd);
+ var addr: libc.sockaddr.un = .{ .path = @splat(0) };
+ @memcpy(addr.path[0 .. live_sock.len + 1], live_sock[0 .. live_sock.len + 1]);
+ _ = libc.connect(fd, @ptrCast(&addr), @sizeOf(@TypeOf(addr)));
+ slot.* = fd;
+ }
+
+ const started = transport.nowMs();
+ try testing.expectEqual(Probe.live, probe(live));
+ try testing.expectEqual(Probe.stale, probe(dead));
+
+ sweepRegistry(testing.io, svc, base);
+
+ // Promptly, and not "eventually": a blocking probe never comes back
+ // at all, so any wall-clock bound at all is the assertion that
+ // matters. A second is several thousand times what three connects
+ // and a readdir cost.
+ try testing.expect(transport.nowMs() - started < 1000);
+
+ try testing.expect(statNoFollow(dead) == null);
+ try testing.expect(statNoFollow(dead_sock) == null);
+ try testing.expect(statNoFollow(live) != null);
+ try testing.expect(statNoFollow(live_sock) != null);
+ try testing.expect(statNoFollow(stranger) != null);
+ try testing.expect(statNoFollow(outsider) == null);
+ try testing.expect(statNoFollow(outsider_sock) != null);
+ try testing.expectEqual(Probe.live, probe(live));
+}
+
+fn bindSocket(path: [:0]const u8) c_int {
+ const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM, 0);
+ if (fd < 0) return fd;
+ var addr: libc.sockaddr.un = .{ .path = @splat(0) };
+ @memcpy(addr.path[0 .. path.len + 1], path[0 .. path.len + 1]);
+ if (libc.bind(fd, @ptrCast(&addr), @sizeOf(@TypeOf(addr))) != 0) {
+ _ = libc.close(fd);
+ return -1;
+ }
+ return fd;
+}
+
test "one connection's buffers are sized from the one msize constant" {
try testing.expect(msize >= ninep.min_msize);
try testing.expectEqual(@as(u32, msize), Runner.msize);
@@ -801,6 +1529,9 @@ test "TCP addresses are numeric and normalize mapped IPv4" {
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"));
@@ -810,35 +1541,97 @@ test "TCP addresses are numeric and normalize mapped IPv4" {
} else try testing.expectError(error.QuicUnavailable, Client.validateDial("quic!127.0.0.1!5640"));
}
-test "same-session TCP mounts compare canonical endpoints and local wildcard destinations" {
+extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8;
+extern "c" fn rmdir(path: [*:0]const u8) c_int;
+
+test "a socket path too long for sun_path is no listener, and leaves the editor's turn as it was" {
if (comptime !supported) return error.SkipZigTest;
- const Case = struct { bound: []const u8, dial: []const u8, same: bool };
- for ([_]Case{
- .{ .bound = "tcp!127.0.0.1!5640", .dial = "tcp!::ffff:127.0.0.1!5640", .same = true },
- .{ .bound = "tcp!::ffff:127.0.0.1!5640", .dial = "tcp!127.0.0.1!5640", .same = true },
- .{ .bound = "tcp!::1!5640", .dial = "tcp!0:0:0:0:0:0:0:1!5640", .same = true },
- .{ .bound = "tcp!127.0.0.1!5640", .dial = "tcp!0.0.0.0!5640", .same = true },
- .{ .bound = "tcp!::1!5640", .dial = "tcp!::!5640", .same = true },
- .{ .bound = "tcp!0.0.0.0!5640", .dial = "tcp!127.0.0.2!5640", .same = true },
- .{ .bound = "tcp!::!5640", .dial = "tcp!::1!5640", .same = true },
- .{ .bound = "tcp!127.0.0.1!5640", .dial = "tcp!127.0.0.1!5641", .same = false },
- .{ .bound = "tcp!0.0.0.0!5640", .dial = "tcp!192.0.2.1!5640", .same = false },
- .{ .bound = "tcp!::!5640", .dial = "tcp!2001:db8::1!5640", .same = false },
- .{ .bound = "tcp!::!5640", .dial = "tcp!127.0.0.1!5640", .same = false },
- }) |c| try testing.expectEqual(c.same, Client.sameSession(c.dial, "", try networkAddress(c.bound, true), null));
- try testing.expect(Client.sameSession("/tmp/pardes-owned.sock", "/tmp/pardes-owned.sock", null, null));
- try testing.expect(Client.sameSession("unix!/tmp/pardes-owned.sock", "/tmp/pardes-owned.sock", null, null));
- try testing.expect(!Client.sameSession("tcp!127.0.0.1!5640", "/tmp/pardes-owned.sock", null, null));
- if (quic_enabled) {
- const endpoint = try networkAddress("quic!127.0.0.1!5640", false);
- try testing.expect(Client.sameSession("quic!::ffff:127.0.0.1!5640", "", null, endpoint));
- try testing.expect(!Client.sameSession("tcp!127.0.0.1!5640", "", null, endpoint));
- try testing.expect(!Client.sameSession("quic!127.0.0.1!5640", "", endpoint, null));
- }
+ const p = try pardes.Pardes.init(testing.allocator, .{ .tty_only = true });
+ defer p.deinit();
+ var directory: [64:0]u8 = undefined;
+ _ = try std.fmt.bufPrintSentinel(&directory, "/tmp/pardes-long-XXXXXX", .{}, 0);
+ if (mkdtemp(&directory) == null) return error.TempDirectoryFailed;
+ const dir = std.mem.span(@as([*:0]const u8, &directory));
+ defer _ = rmdir(&directory);
+ // A name that fits: the directory is one a listener takes.
+ var fits: [sun_path_len]u8 = undefined;
+ try testing.expect(socketPath(&fits, dir, "short") != null);
+ const old = libc.getenv("XDG_RUNTIME_DIR");
+ var old_buf: [4096]u8 = undefined;
+ const old_copy = if (old) |o| try std.fmt.bufPrintSentinel(&old_buf, "{s}", .{std.mem.span(o)}, 0) else null;
+ var z: [4096]u8 = undefined;
+ _ = setenv("XDG_RUNTIME_DIR", try std.fmt.bufPrintSentinel(&z, "{s}", .{dir}, 0), 1);
+ defer if (old_copy) |o| {
+ _ = setenv("XDG_RUNTIME_DIR", o, 1);
+ } else {
+ _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR");
+ };
+ const long = &@as([120]u8, @splat('n'));
+ try testing.expect(listen(testing.io, testing.allocator, p, long, "", null, null) == null);
+ try testing.expect(pardes.turn.answer_held == null);
+ try testing.expect(pardes.turn.wake_parked == null);
+ try testing.expect(pardes.turn.io == null);
}
-extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8;
-extern "c" fn rmdir(path: [*:0]const u8) c_int;
+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 directory: [64:0]u8 = undefined;
+ _ = try std.fmt.bufPrintSentinel(&directory, "/tmp/pardes-post-XXXXXX", .{}, 0);
+ if (mkdtemp(&directory) == null) return error.TempDirectoryFailed;
+ const dir = std.mem.span(@as([*:0]const u8, &directory));
+ const old_runtime = if (libc.getenv("XDG_RUNTIME_DIR")) |v| try gpa.dupeSentinel(u8, std.mem.span(v), 0) else null;
+ defer {
+ if (old_runtime) |v| {
+ _ = setenv("XDG_RUNTIME_DIR", v, 1);
+ gpa.free(v);
+ } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR");
+ }
+ try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1));
+
+ var entry_buf: [sun_path_len:0]u8 = undefined;
+ const entry = try std.fmt.bufPrintSentinel(&entry_buf, "{s}/9p/pardes/unit", .{dir}, 0);
+ var svc_buf: [sun_path_len:0]u8 = undefined;
+ const svc = try std.fmt.bufPrintSentinel(&svc_buf, "{s}/9p/pardes", .{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);
+ defer {
+ _ = libc.unlink(entry);
+ _ = rmdir(svc);
+ var reg_buf: [sun_path_len:0]u8 = undefined;
+ if (std.fmt.bufPrintSentinel(&reg_buf, "{s}/9p", .{dir}, 0)) |reg| {
+ _ = rmdir(reg);
+ } else |_| {}
+ _ = libc.unlink(sock);
+ _ = rmdir(&directory);
+ }
+
+ 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 = listen(testing.io, gpa, p, "unit", "", null, null) orelse return error.ListenFailed;
+ 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.
+ try testing.expect(alive(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)]);
+ }
+ // Stopping takes the name back out, so the next editor of that name
+ // is not refused by its own corpse.
+ try testing.expect(libc.readlink(entry, &link, link.len) < 0);
+}
test "Unix TCP and QUIC share one listener through reads writes reconnects and reset" {
if (comptime !supported) return error.SkipZigTest;
@@ -846,13 +1639,14 @@ test "Unix TCP and QUIC share one listener through reads writes reconnects and r
var directory: [64:0]u8 = undefined;
_ = try std.fmt.bufPrintSentinel(&directory, "/tmp/pardes-tcp-XXXXXX", .{}, 0);
if (mkdtemp(&directory) == null) return error.TempDirectoryFailed;
- defer _ = rmdir(&directory);
- const old_runtime = if (libc.getenv("XDG_RUNTIME_DIR")) |v| try gpa.dupeZ(u8, std.mem.span(v)) else null;
+ // What the listener posted and left inside goes with it.
+ defer std.Io.Dir.cwd().deleteTree(testing.io, std.mem.sliceTo(&directory, 0)) catch |err| std.debug.print("deleteTree: {s}\n", .{@errorName(err)});
+ const old_runtime = if (libc.getenv("XDG_RUNTIME_DIR")) |v| try gpa.dupeSentinel(u8, std.mem.span(v), 0) else null;
defer {
if (old_runtime) |v| {
_ = setenv("XDG_RUNTIME_DIR", v, 1);
gpa.free(v);
- } else _ = unsetenv("XDG_RUNTIME_DIR");
+ } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR");
}
try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1));
const replacement = try gpa.alloc(u8, 3 * msize + 27);
@@ -909,14 +1703,14 @@ test "Unix TCP and QUIC share one listener through reads writes reconnects and r
var bind_buf: [64]u8 = undefined;
const bind = try std.fmt.bufPrint(&bind_buf, "{s}!{s}!0", .{ protocol, host });
const tcp = std.mem.eql(u8, protocol, "tcp");
- const l = listen(testing.io, gpa, "roundtrip", "", if (tcp) bind else null, if (tcp) null else bind) orelse return error.ListenFailed;
+ const l = listen(testing.io, gpa, p, "roundtrip", "", if (tcp) bind else null, if (tcp) null else bind) orelse return error.ListenFailed;
defer {
l.reset(p);
l.deinit(gpa);
}
try testing.expect(l.path().len != 0);
try testing.expectEqual(tcp, l.tcp_address != null);
- try testing.expectEqual(@as(usize, 4), l.runner.conns.len);
+ try testing.expectEqual(@as(usize, max_conns), l.runner.conns.len);
try testing.expect(l.watcher == null);
const port = (if (tcp) l.tcp_address else l.quic_address).?.getPort();
try testing.expect(port != 0);
@@ -928,20 +1722,262 @@ test "Unix TCP and QUIC share one listener through reads writes reconnects and r
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 = Client.nowMs() + 3 * Client.budget_ms;
+ pardes.turn.rest();
while (!worker.done.load(.acquire) and Client.nowMs() < deadline) {
- _ = l.tick(p);
+ if (quic_enabled) {
+ pardes.turn.wake();
+ _ = l.tick();
+ pardes.turn.rest();
+ }
Client.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 = Client.nowMs() + 3 * Client.budget_ms;
+ while (l.runner.count() != 0 and Client.nowMs() < settle_by) Client.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 (&l.slots) |*slot| try testing.expectEqual(@as(usize, 0), slot.len);
for (&l.conns, 0..) |conn, i| try testing.expect(!l.live(@intCast(i)) and !conn.draining);
- for (p.fs.snapshots) |snapshot| try testing.expect(snapshot.node == 0);
+ 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 directory: [64:0]u8 = undefined;
+ _ = try std.fmt.bufPrintSentinel(&directory, "/tmp/pardes-park-XXXXXX", .{}, 0);
+ if (mkdtemp(&directory) == null) return error.TempDirectoryFailed;
+ // What the listener posted and left inside goes with it.
+ defer std.Io.Dir.cwd().deleteTree(testing.io, std.mem.sliceTo(&directory, 0)) catch |err| std.debug.print("deleteTree: {s}\n", .{@errorName(err)});
+ const old_runtime = if (libc.getenv("XDG_RUNTIME_DIR")) |v| try gpa.dupeSentinel(u8, std.mem.span(v), 0) else null;
+ defer {
+ if (old_runtime) |v| {
+ _ = setenv("XDG_RUNTIME_DIR", v, 1);
+ gpa.free(v);
+ } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR");
+ }
+ try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1));
+
+ 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 = listen(testing.io, gpa, p, "park", "", null, null) orelse return error.ListenFailed;
+ 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();
+ Client.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 = Client.nowMs() + Client.budget_ms;
+ while (!writer.done.load(.acquire) and Client.nowMs() < deadline) Client.nap(1);
+ pardes.turn.wake();
+ try testing.expect(writer.done.load(.acquire));
+ if (writer.failure) |err| return err;
+ try testing.expectEqualStrings("after\n", pane.file.?.content);
+}
+
+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 directory: [64:0]u8 = undefined;
+ _ = try std.fmt.bufPrintSentinel(&directory, "/tmp/pardes-held-XXXXXX", .{}, 0);
+ if (mkdtemp(&directory) == null) return error.TempDirectoryFailed;
+ // What the listener posted and left inside goes with it.
+ defer std.Io.Dir.cwd().deleteTree(testing.io, std.mem.sliceTo(&directory, 0)) catch |err| std.debug.print("deleteTree: {s}\n", .{@errorName(err)});
+ const old_runtime = if (libc.getenv("XDG_RUNTIME_DIR")) |v| try gpa.dupeSentinel(u8, std.mem.span(v), 0) else null;
+ defer {
+ if (old_runtime) |v| {
+ _ = setenv("XDG_RUNTIME_DIR", v, 1);
+ gpa.free(v);
+ } else _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR");
+ }
+ try testing.expectEqual(@as(c_int, 0), setenv("XDG_RUNTIME_DIR", &directory, 1));
+
+ 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 = listen(testing.io, gpa, p, "held", "", null, null) orelse return error.ListenFailed;
+ 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 = Client.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 = Client.nowMs() + Client.budget_ms;
+ while (Client.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;
+ Client.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);
}
extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int;
@@ -949,76 +1985,157 @@ extern "c" fn unsetenv(name: [*:0]const u8) 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;
- const listener = listen(io, gpa, core.opts.ninep_name, fallback, core.opts.ninep_tcp, core.opts.ninep_quic) orelse {
+ return listen(io, gpa, core, core.opts.ninep_name, fallback, core.opts.ninep_tcp, core.opts.ninep_quic) orelse {
core.reportError(0, "9p listener", error.ListenFailed);
return null;
};
- core.fs.socket_path = listener.path();
- core.fs.tcp_address = listener.tcp_address;
- core.fs.quic_address = listener.quic_address;
- return listener;
}
-pub fn exportPaneEnv(listener: ?*const Listener, serial: u32, forward_look: bool) void {
- _ = unsetenv("PARDES_FORWARD_LOOK");
- if (listener) |l| exporting: {
- var sock: [sun_path_len]u8 = undefined;
- const path = std.fmt.bufPrintSentinel(&sock, "{s}", .{l.path()}, 0) catch break :exporting;
+/// 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.
+pub fn exportPaneEnv(listener: ?*const Listener, serial: u32, adopts: bool) void {
+ exportEnv(if (listener) |l| l.path() else null, serial, adopts);
+}
+
+/// exportPaneEnv for a command run off the loop (an Edit's filter), with
+/// this process's own socket: what a pane's command would be given.
+pub fn exportSessionEnv(serial: u32, adopts: bool) void {
+ exportEnv(if (own_socket_len > 0) own_socket[0..own_socket_len] else null, serial, adopts);
+}
+
+/// Told to children through host_io.ChildEnv, never set into this process's
+/// own environment (see ChildEnv.exportVar for why).
+fn exportEnv(socket: ?[]const u8, serial: u32, adopts: bool) void {
+ const ChildEnv = @import("host_io.zig").ChildEnv;
+ var pid_buf: [16]u8 = undefined;
+ ChildEnv.exportVar("PARDES_PID", if (adopts) std.fmt.bufPrint(&pid_buf, "{d}", .{@as(u32, @intCast(libc.getpid()))}) catch null else null);
+ if (socket) |own| {
var buf: [16]u8 = undefined;
- const id = std.fmt.bufPrintSentinel(&buf, "{d}", .{serial}, 0) catch break :exporting;
- if (setenv("PARDES_9P", path, 1) != 0) break :exporting;
- if (setenv("PARDES_PANE", id, 1) != 0) break :exporting;
- if (setenv("PARDES_FORWARD_LOOK", if (forward_look) "1" else "0", 1) == 0) return;
+ 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"`. A Tty9p shell sets its own (linux/v9fs.zig).
+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.fs.path.basename(sock_path);
+ const sock_prefix = "pardes-9p-";
+ const suffix = ".sock";
+ if (mntgen and root.len > 0 and std.mem.startsWith(u8, base, sock_prefix) and std.mem.endsWith(u8, base, suffix) and base.len > sock_prefix.len + suffix.len) {
+ var buf: [4096]u8 = undefined;
+ if (std.fmt.bufPrint(&buf, "{s}/pardes/{s}", .{ std.mem.trimEnd(u8, root, "/"), base[sock_prefix.len .. base.len - suffix.len] })) |mount| {
+ return @import("host_io.zig").ChildEnv.exportVar("PARDES_MOUNT", mount);
+ } else |_| {}
}
- _ = unsetenv("PARDES_9P");
- _ = unsetenv("PARDES_PANE");
- _ = setenv("PARDES_FORWARD_LOOK", "0", 1);
+ @import("host_io.zig").ChildEnv.exportVar("PARDES_MOUNT", null);
}
-test "9P shell environment preserves identity when nested Look forwarding is disabled" {
- const names = [_][*:0]const u8{ "XDG_RUNTIME_DIR", "HOME", "PARDES_9P", "PARDES_PANE", "PARDES_FORWARD_LOOK" };
+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.dupeZ(u8, std.mem.span(old));
+ 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 _ = unsetenv(name);
+ } else _ = @import("host_io.zig").testUnsetenv(name);
+ };
+ _ = setenv("NINE_MOUNT", "/mnt/9p", 1);
+ _ = setenv("NINE_MNTGEN", "1", 1);
+ exportMount("/run/user/1000/pardes-9p-work.sock");
+ try testing.expectEqualStrings("/mnt/9p/pardes/work", @import("host_io.zig").ChildEnv.exported("PARDES_MOUNT").?);
+ // A mount of another server (9ns --unix): not this session's.
+ _ = @import("host_io.zig").testUnsetenv("NINE_MNTGEN");
+ exportMount("/run/user/1000/pardes-9p-work.sock");
+ try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_MOUNT") == null);
+}
+
+test "9P shell environment states being inside pardes apart from how to reach it" {
+ const names = [_][*:0]const u8{ "XDG_RUNTIME_DIR", "HOME", "PARDES_PID", "PARDES_9P", "PARDES_PANE" };
+ var saved: [names.len]?[:0]u8 = @splat(null);
+ for (names, &saved) |name, *value| {
+ if (libc.getenv(name)) |old| value.* = try testing.allocator.dupeSentinel(u8, std.mem.span(old), 0);
+ }
+ defer for (names, saved) |name, value| {
+ if (value) |old| {
+ _ = setenv(name, old, 1);
+ testing.allocator.free(old);
+ } else _ = @import("host_io.zig").testUnsetenv(name);
};
var dir: [sun_path_len:0]u8 = undefined;
_ = setenv("XDG_RUNTIME_DIR", "/run/user/1000", 1);
try testing.expectEqualStrings("/run/user/1000", socketDir(&dir).?);
- _ = unsetenv("XDG_RUNTIME_DIR");
+ _ = @import("host_io.zig").testUnsetenv("XDG_RUNTIME_DIR");
_ = setenv("HOME", "/home/example", 1);
try testing.expectEqualStrings("/home/example/.local/state/pardes", socketDir(&dir).?);
- _ = unsetenv("HOME");
+ _ = @import("host_io.zig").testUnsetenv("HOME");
try testing.expect(socketDir(&dir) == null);
const listener = try testing.allocator.create(Listener);
defer testing.allocator.destroy(listener);
- listener.* = .{ .io = testing.io };
+ listener.* = .{ .io = testing.io, .core = undefined }; // only its path is read
const path = "/tmp/pardes-example.sock";
@memcpy(listener.path_buf[0..path.len], path);
listener.path_len = path.len;
+ 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.
exportPaneEnv(listener, 7, false);
- try testing.expectEqualStrings(path, std.mem.span(libc.getenv("PARDES_9P").?));
- try testing.expectEqualStrings("7", std.mem.span(libc.getenv("PARDES_PANE").?));
- try testing.expectEqualStrings("0", std.mem.span(libc.getenv("PARDES_FORWARD_LOOK").?));
+ try testing.expectEqualStrings(path, @import("host_io.zig").ChildEnv.exported("PARDES_9P").?);
+ try testing.expectEqualStrings("7", @import("host_io.zig").ChildEnv.exported("PARDES_PANE").?);
+ try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PID") == null);
+
exportPaneEnv(listener, 8, true);
- try testing.expectEqualStrings("8", std.mem.span(libc.getenv("PARDES_PANE").?));
- try testing.expectEqualStrings("1", std.mem.span(libc.getenv("PARDES_FORWARD_LOOK").?));
+ try testing.expectEqualStrings("8", @import("host_io.zig").ChildEnv.exported("PARDES_PANE").?);
+ try testing.expectEqualStrings(own_pid, @import("host_io.zig").ChildEnv.exported("PARDES_PID").?);
+
+ // 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.
+ exportPaneEnv(null, 0, true);
+ try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_9P") == null);
+ try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PANE") == null);
+ try testing.expectEqualStrings(own_pid, @import("host_io.zig").ChildEnv.exported("PARDES_PID").?);
+
exportPaneEnv(null, 0, false);
- try testing.expect(libc.getenv("PARDES_9P") == null);
- try testing.expect(libc.getenv("PARDES_PANE") == null);
- try testing.expectEqualStrings("0", std.mem.span(libc.getenv("PARDES_FORWARD_LOOK").?));
+ try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_9P") == null);
+ try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PANE") == null);
+ try testing.expect(@import("host_io.zig").ChildEnv.exported("PARDES_PID") == null);
}
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;
@@ -1048,20 +2165,39 @@ pub const Client = struct {
if (comptime !supported) return error.Unsupported;
var names: [max_depth][]const u8 = undefined;
const n = try elements(path, &names);
- var sock_buf: [sun_path_len]u8 = undefined;
- const sock = try resolve(&sock_buf, dial);
var remote: RemoteError = .{};
- return fetchBytes(gpa, sock, names[0..n], &remote, null, display_path, @min(max_bytes, limits.max_file_bytes));
+ return fetchBytes(gpa, dial, names[0..n], &remote, null, display_path, @min(max_bytes, limits.max_file_bytes)) 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.
+ 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 {
+ const got = readLimit(gpa, dial, "", "", 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 {
if (comptime !supported) return error.Unsupported;
var names: [max_depth][]const u8 = undefined;
const n = try elements(path, &names);
- var sock_buf: [sun_path_len]u8 = undefined;
- const sock = try resolve(&sock_buf, dial);
var remote: RemoteError = .{};
- const result = try fetchBytes(gpa, sock, names[0..n], &remote, bytes, path, limits.max_file_bytes);
+ const result = fetchBytes(gpa, dial, names[0..n], &remote, bytes, path, limits.max_file_bytes) catch |err| {
+ if (err == error.Remote) said = remote;
+ return err;
+ };
gpa.free(result);
}
@@ -1095,9 +2231,15 @@ pub const Client = struct {
return .{ .quic = 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 };
}
@@ -1111,35 +2253,6 @@ pub const Client = struct {
_ = try resolve(&buf, dial);
}
- pub fn sameSession(dial: []const u8, socket_path: []const u8, tcp_address: ?std.Io.net.IpAddress, quic_address: ?std.Io.net.IpAddress) bool {
- if (comptime !supported) return false;
- var buf: [sun_path_len]u8 = undefined;
- const address = resolve(&buf, dial) catch return false;
- switch (address) {
- .unix => |path| return socket_path.len != 0 and std.mem.eql(u8, path, socket_path),
- .tcp, .quic => |destination| {
- var ip = destination;
- switch (ip) {
- .ip4 => |v4| if (std.mem.allEqual(u8, &v4.bytes, 0)) {
- ip = .{ .ip4 = .loopback(v4.port) };
- },
- .ip6 => |v6| if (std.mem.allEqual(u8, &v6.bytes, 0)) {
- ip = .{ .ip6 = .loopback(v6.port) };
- },
- }
- const bound = canonicalIp((if (address == .tcp) tcp_address else quic_address) orelse return false);
- if (ip.getPort() != bound.getPort() or @as(std.Io.net.IpAddress.Family, ip) != @as(std.Io.net.IpAddress.Family, bound)) return false;
- if (ip.eql(&bound)) return true;
- const wildcard = switch (bound) {
- .ip4 => |v4| std.mem.allEqual(u8, &v4.bytes, 0),
- .ip6 => |v6| std.mem.allEqual(u8, &v6.bytes, 0),
- };
- if (wildcard) return localIp(ip);
- return false;
- },
- }
- }
-
const Session = struct {
fd: c_int,
quic: if (quic_enabled) ?quic.Connection else void = if (quic_enabled) null else {},
@@ -1244,14 +2357,8 @@ pub const Client = struct {
}
};
- fn transact(
- s: *Session,
- names: []const []const u8,
- out: *std.Io.Writer.Allocating,
- remote: *RemoteError,
- write_bytes: ?[]const u8,
- read_limit: usize,
- ) !void {
+ /// Version, attach and a walk to `names`: the fid there and its qid.
+ fn walkTo(s: *Session, names: []const []const u8, remote: *RemoteError) !struct { fid: u32, qid: ninep.Qid } {
_ = try s.ask(.{ .version = .{} }, remote);
if (s.cl.msize == 0) return Error.Botch;
@@ -1275,6 +2382,20 @@ pub const Client = struct {
next = if (next == 1) 2 else 1;
i += n;
}
+ return .{ .fid = cur, .qid = here };
+ }
+
+ fn transact(
+ s: *Session,
+ names: []const []const u8,
+ out: *std.Io.Writer.Allocating,
+ remote: *RemoteError,
+ write_bytes: ?[]const u8,
+ read_limit: usize,
+ ) !void {
+ const at = try walkTo(s, names, remote);
+ const cur = at.fid;
+ const here = at.qid;
defer s.dropNoWait(cur);
const directory = here.type & ninep.qtdir != 0;
@@ -1291,6 +2412,12 @@ pub const Client = struct {
const count = (try s.ask(.{ .write = .{ .fid = cur, .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.
+ s.deadline = nowMs() +| write_budget_ms;
}
return;
}
@@ -1325,7 +2452,7 @@ pub const Client = struct {
fn fetchBytes(
gpa: std.mem.Allocator,
- sock: Dial,
+ dial: []const u8,
names: []const []const u8,
remote: *RemoteError,
write_bytes: ?[]const u8,
@@ -1333,15 +2460,22 @@ pub const Client = struct {
read_limit: usize,
) ![]u8 {
if (comptime !supported) return Error.Dial;
+ const s = try startSession(gpa, dial, display_path);
+ defer endSession(gpa, s);
+ var out: std.Io.Writer.Allocating = .init(gpa);
+ errdefer out.deinit();
+ try transact(s, names, &out, remote, write_bytes, read_limit);
+ return out.toOwnedSlice();
+ }
+
+ /// A connected session at `dial` whose requests have the usual deadline.
+ fn startSession(gpa: std.mem.Allocator, 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;
const s = try gpa.create(Session);
s.* = .{ .fd = -1, .deadline = deadline, .display_path = display_path };
- defer {
- if (quic_enabled and s.quic != null) {
- s.quic.?.deinit();
- } else if (s.fd >= 0) _ = libc.close(s.fd);
- gpa.destroy(s);
- }
+ errdefer endSession(gpa, s);
if (sock == .quic) {
if (comptime quic_enabled) {
s.quic = quic.Connection.dial(sock.quic) catch return Error.Dial;
@@ -1349,11 +2483,80 @@ pub const Client = struct {
} else return Error.QuicUnavailable;
} else s.fd = try connect(sock, deadline);
s.cl = .init(.{ .in = &s.in, .out = &s.out });
+ return s;
+ }
- var out: std.Io.Writer.Allocating = .init(gpa);
- errdefer out.deinit();
- try transact(s, names, &out, remote, write_bytes, read_limit);
- return out.toOwnedSlice();
+ fn endSession(gpa: std.mem.Allocator, s: *Session) void {
+ if (quic_enabled and s.quic != null) {
+ s.quic.?.deinit();
+ } else if (s.fd >= 0) _ = libc.close(s.fd);
+ gpa.destroy(s);
+ }
+
+ /// 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(gpa, dial, path);
+ defer endSession(gpa, s);
+ const fid = (try walkTo(s, names[0..n], &remote)).fid;
+ _ = 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(
+ gpa: std.mem.Allocator,
+ 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(gpa, dial, path);
+ defer endSession(gpa, s);
+ const fid = (try walkTo(s, names[0..n], &remote)).fid;
+ _ = 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 {
@@ -1419,7 +2622,7 @@ pub const Client = struct {
const before = nowMs();
try testing.expectError(
Error.Dial,
- fetchBytes(testing.allocator, .{ .unix = "/tmp/pardes-9p-no-such-socket.sock" }, names[0..n], &remote, null, "/pane/1/body", limits.max_file_bytes),
+ fetchBytes(testing.allocator, "/tmp/pardes-9p-no-such-socket.sock", names[0..n], &remote, null, "/pane/1/body", limits.max_file_bytes),
);
try testing.expect(nowMs() - before < budget_ms);
}