diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 02:40:21 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 757cdaa826131dc5ff2d3ff0b270b8abe041c81a (patch) | |
| tree | 96de2e4537bc009d502bdf74b453e6964a12dff0 | |
| parent | 83b9d45b8665a80bdb70934aa23bbde5cc03d202 (diff) | |
| download | pardes-757cdaa826131dc5ff2d3ff0b270b8abe041c81a.tar.gz pardes-757cdaa826131dc5ff2d3ff0b270b8abe041c81a.zip | |
plumber: external ports (docs/next-plumber.md, phase 3): every port a rule names has a file, plumb/<port> (0444), listed after rules and send in the order rules named them and never dropped (Plumb.ports, dumped: seeded at boot from the rules in force, grown by each apply, refused past 32), its node a third family in the tree beside panes and columns (Node.ofPort, Target.port; port_bit is 1 << 40, clear of the host tree's and the archive's high bits, which 1 << 57 hit); an open reads (Open.what.port, an events.Queue of its own, 64 KiB): the messages sent from then on, packed, one a read, a short read the front and the next the rest, a read with none held until one comes (p.fs.news, answered by the listener as event reads are); every open gets its copy (fanOut, plumber(4)'s queuesend), one over the queue is not sent to it and the log says so; a port pardes reads itself (edit, web, a loaded kind) hands its readers copies without asking anyone, so a look stays speculated whole; another port's readers are the authority's to know, asked through tape.authority (a replica's own look waits for the first answer and guesses the last), with none its rule set's start runs, or its client, the message held in p.fs.plumb_held for the port's first opener (Plumb.opened, plumber(4)'s queueheld); test: plumber.zig, two opens of plumb/edit each read a look's message while the file opens, an open made after reads nothing, a client's message reaches the first opener and the next goes to the reader with nothing started, a port is not written; monkey9p.zig gains the plumb/ files, rules and messages that start no program, the ports' reads as held reads, and plumb/rules' rest read at the close (whose refusal is the log's alone, as a line file's: restAtClose); replicas.zig: a look to a port a program reads, the second of a replica's own speculated whole and confirmed, digests equal; tree.zig's readdir test lists web and edit; the reference's tree and plumber section, fs-help and the changelog say so
| -rw-r--r-- | docs/typ/reference.typ | 27 | ||||
| -rw-r--r-- | src/CHANGELOG.md | 7 | ||||
| -rw-r--r-- | src/Plumb.zig | 121 | ||||
| -rw-r--r-- | src/config.zig | 1 | ||||
| -rw-r--r-- | src/fs-help.txt | 2 | ||||
| -rw-r--r-- | src/fs.zig | 5 | ||||
| -rw-r--r-- | src/ninep/ctl.zig | 2 | ||||
| -rw-r--r-- | src/ninep/events.zig | 4 | ||||
| -rw-r--r-- | src/ninep/tree.zig | 64 | ||||
| -rw-r--r-- | test/behaviour/plumber.zig | 41 | ||||
| -rw-r--r-- | test/monkey9p.zig | 25 | ||||
| -rw-r--r-- | test/replicas.zig | 47 |
12 files changed, 309 insertions, 37 deletions
diff --git a/docs/typ/reference.typ b/docs/typ/reference.typ index f1b4df76..4d077f71 100644 --- a/docs/typ/reference.typ +++ b/docs/typ/reference.typ @@ -41,6 +41,7 @@ tagexec a word clicked in the workspace tag @co plumb/ the plumber's: plumb(6) rules, and messages sent by them @plumber rules the rules in force; a write adds rule sets, > replaces them @plumber send write a message: six header lines, then its data @plumber + <port> a port the rules name: read, the messages sent to it @plumber col/ the columns @columns-and-tags <n>/ column <n>; rmdir closes it when empty @columns-and-tags tag its tag @@ -1038,7 +1039,8 @@ own: a file or directory opened, else the word searched for. (`plumbing` in the configuration directory, beside `init`; saved, its rules are in force), or without one `include basic`, pardes's own (`src/plumbing`, below). A write adds rule sets, each - read as its blank line comes, the rest at the close; `>` replaces them + read as its blank line comes, the rest at the close (refused there, it + is an `err` in the log alone; Plan 9's loses it); `>` replaces them all. One open writes at a time (`file in use`). Kept in a dump. - A rule set that does not read is refused at the write it is whole in, `plumb/rules:<line>: <why>` in rules.c's words (`unknown verb isnt`, @@ -1062,11 +1064,22 @@ own: a file or directory opened, else the word searched for. `$file` and `$dir` are where), `set`, `add` and `delete` (`delete` of an attribute not there fails, as plumber's code does), and the actions `plumb to <port>`, `plumb start <words>` and `plumb client <words>`. -- The ports pardes reads itself: `edit` (the data opened as a - #word("Look") opens it, at `addr=` when given), `web` (the host's - browser, a URL of at most 256 bytes) and each loaded kind by its name - (`image`, `pdf`). A message to another port runs its rule set's - `start` or `client`; to a port no rule names it goes nowhere. +- Each port a rule has named this session has a file, + #file("/plumb/<port>") (0444), listed after `rules` and `send` in the + order the rules named them, never dropped. An open of it reads the + messages sent to that port from then on, one a read in the six lines' + form, a read that finds none held until one comes; every open gets its + own copy (`cat /plumb/edit` beside the editor sees every look it opens), + a message over its 64 KiB queue is not sent to it (an `err` in the log + says so). Messages sent before the open are not its. +- The ports pardes reads itself, whatever else reads them: `edit` (the + data opened as a #word("Look") opens it, at `addr=` when given), `web` + (the host's browser, a URL of at most 256 bytes) and each loaded kind by + its name (`image`, `pdf`). A message to another port goes to its + readers; with none, its rule set's `start` runs (the message dropped), + or its `client`, the message held for the port's first reader (the + program it started, as a rule's `plumb client` means). To a port no + rule ever named it goes nowhere. - `plumb start` of a builtin or a kind's word runs it in place, at the looking pane (the pane with the keyboard for a #file("/plumb/send") message); one that cannot do what it is asked declines, saying @@ -1424,7 +1437,7 @@ with Client(sys.argv[1]) as c: # a socket path, or (ip, port) [Edit], [every acme command but `B <cmd`, `D <cmd` and the `'` address; no `\1`-`\9`, and an alternation takes the first branch that matches, not the longest; a failing `< | >` command changes nothing, where acme puts in what it wrote (#doc("fs", section: "edit"))], [`win`], [#word("Term") is a VT terminal pane; #file("pty/run") runs a line at its prompt and answers `exit N` and the output; with #word("Repl") bound, #word("Exec") on a source pane types the text into it], [a directory window], [as acme's: a text pane named `dir/` (#file("index") kind `text`, #file("ctl") isdir 1), its entries in columns sorted bytewise, dotfiles shown, a directory's marked `/`; blanks pad the columns where acme's tabs do; a #word("Look") at an entry is from that directory, `Get` or a look at it again reads it again, and an edit is kept until then rather than lost to a resize; never dirty, #word("Save") refuses it], - [the plumber], [no process: the plumber is the editor's own (#doc("reference", section: "plumber")): plumb(6)'s rules in #file("/plumb/rules") and the plumbing file #word("Plumb") opens, messages to #file("/plumb/send"); no submatches (`$1`-`$9`), no `click` from a #word("Look") (its own expansion stands), and no external port files yet], + [the plumber], [no process: the plumber is the editor's own (#doc("reference", section: "plumber")): plumb(6)'s rules in #file("/plumb/rules") and the plumbing file #word("Plumb") opens, messages to #file("/plumb/send"); no submatches (`$1`-`$9`), no `click` from a #word("Look") (its own expansion stands)], [#key("Esc") selects what was typed], [#key("Esc") leaves insert mode, or goes back a pane (#doc("tags", section: "modes"))], ) diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index 3e6c3d7f..823b0e13 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -2,6 +2,13 @@ ## Unreleased +- A port the plumbing rules name is a file a program reads, + `plumb/<port>`, as Plan 9's plumber serves one: every open reads its + own copy of what is sent there from then on (`cat plumb/edit` sees each + file a look opens), a read held until a message comes. A message to a + port no one reads runs its rule set's `start`, or with `plumb client` + waits for the port's first reader. + - A look asks the plumber first, as acme's does: once the pane's own readings are done and a name already open in a pane is focused, what is looked at is sent (`type text`, `src pardes`, `wdir` the pane's diff --git a/src/Plumb.zig b/src/Plumb.zig index f54ac1e2..e2edef77 100644 --- a/src/Plumb.zig +++ b/src/Plumb.zig @@ -12,6 +12,8 @@ const fs = @import("fs.zig"); const look = @import("look.zig"); const layout = @import("layout.zig"); const Kind = @import("Kind.zig"); +const tape = @import("tape.zig"); +const events = @import("ninep/events.zig"); const Regex = @import("regexp.zig").Regex; const Pardes = pardes.Pardes; @@ -23,6 +25,10 @@ text: std.ArrayList(u8) = .empty, /// are `include basic`'s, so a session starts with the defaults, and so /// does a dump made before the plumber. written: bool = false, +/// Every port a rule has named this session, in the order they were +/// named, never dropped (plumber(4)'s directory): plumb/<port>'s files, +/// a port's index its node. +ports: std.ArrayList([]u8) = .empty, /// The default rules, which `include basic` names, as Plan 9's names /// /lib/plumb/basic. @@ -45,6 +51,8 @@ pub const e_empty = "empty argument list"; pub fn deinit(pl: *Plumb, gpa: std.mem.Allocator) void { pl.text.deinit(gpa); + for (pl.ports.items) |name| gpa.free(name); + pl.ports.deinit(gpa); } // ---- the message ---- @@ -518,8 +526,10 @@ pub fn filePath(p: *Pardes, buf: *[4096]u8) ?[]const u8 { /// At boot, the plumbing file's rules in force, if it is there (the /// plumber reading $home/lib/plumbing); a refusal is said, as an init -/// line that fails is, and the defaults stay. +/// line that fails is, and the defaults stay. Either way the ports the +/// rules in force name have their files. pub fn boot(p: *Pardes) void { + if (rulesOf(p, p.scratch.allocator())) |rules| notePorts(p, rules.ports.items) catch {} else |_| {} var buf: [4096]u8 = undefined; const path = filePath(p, &buf) orelse return; const text = fs.read(p, path) catch return; @@ -537,6 +547,71 @@ pub fn saved(p: *Pardes, pane: usize, path: []const u8, text: []const u8) void { if (apply(p, false, "plumbing", text)) |why| p.reportFailure(pane, why); } +/// `names` added to the session's ports, those it has not yet, in order; +/// none when they would be more than max_ports. +fn notePorts(p: *Pardes, names: []const []const u8) error{ TooMany, OutOfMemory }!void { + var new: usize = 0; + for (names) |n| new += @intFromBool(portIndex(p, n) == null); + if (p.plumber.ports.items.len + new > max_ports) return error.TooMany; + try p.plumber.ports.ensureUnusedCapacity(p.gpa, new); + for (names) |n| if (portIndex(p, n) == null) p.plumber.ports.appendAssumeCapacity(try p.gpa.dupe(u8, n)); +} + +/// The port named `name`'s index, its file's node. +pub fn portIndex(p: *const Pardes, name: []const u8) ?u8 { + for (p.plumber.ports.items, 0..) |n, i| if (std.mem.eql(u8, n, name)) return @intCast(i); + return null; +} + +pub fn portName(p: *const Pardes, index: u8) ?[]const u8 { + return if (index < p.plumber.ports.items.len) p.plumber.ports.items[index] else null; +} + +/// Whether a program reads port `index` (an open of its file): the +/// authority's 9P state, asked through the tape. +fn portRead(p: *Pardes, index: u8) bool { + for (&p.fs.opens) |*o| if (o.node != 0 and o.what == .port and o.what.port.index == index) return true; + return false; +} + +/// `m` packed onto `q` (a reader's, the held ones'), after `lead` (a +/// held message's port); false when it is over what a queue holds. +fn enqueue(p: *Pardes, q: *events.Queue, lead: []const u8, m: *const Msg) bool { + var out: std.Io.Writer.Allocating = .init(p.scratch.allocator()); + out.writer.writeAll(lead) catch return false; + m.pack(&out.writer) catch return false; + if (out.written().len + 4 > q.cap) return false; + q.push(p.gpa, out.written()); + return true; +} + +/// A copy of `m` to every reader of port `index` (plumber(4)'s queuesend: +/// every fid open when it was sent), each read held for one answered. +fn fanOut(p: *Pardes, index: u8, m: *const Msg) void { + for (&p.fs.opens) |*o| if (o.node != 0 and o.what == .port and o.what.port.index == index) { + if (!enqueue(p, &o.what.port.queue, "", m)) { + var said: Kind.Buf(160) = .{}; + events.notePath(p, "err -", said.print("plumb/{s}: a message of {d} bytes is over a reader's {d} KiB, not sent to it", .{ portName(p, index).?, m.data.len, events.queue_cap / 1024 })); + } + }; + p.fs.news = true; +} + +/// A port's file opened (tree.zig): what `client` rules held for it, the +/// opener's (plumber(4)'s queueheld). +pub fn opened(p: *Pardes, index: u8, q: *events.Queue) void { + const held = &p.fs.plumb_held; + var kept: events.Queue = .{}; + while (held.peek()) |record| { + if (record[0] == index) { + q.push(p.gpa, record[1..]); + } else kept.push(p.gpa, record); + held.pop(); + } + held.deinit(p.gpa); + held.* = kept; +} + /// No rules at all: a `>` to plumb/rules, before what it writes. pub fn clear(p: *Pardes) void { p.plumber.text.clearRetainingCapacity(); @@ -560,6 +635,10 @@ pub fn apply(p: *Pardes, keep: bool, name: []const u8, more: []const u8) ?[]cons var out: std.Io.Writer.Allocating = .init(arena); ps.rules.print(&out.writer) catch break :said "out of memory"; if (out.written().len > max_text) break :said std.fmt.comptimePrint("rules too long; max {d} KiB", .{max_text / 1024}); + notePorts(p, ps.rules.ports.items) catch |err| break :said switch (err) { + error.TooMany => std.fmt.comptimePrint("too many ports; max {d}", .{max_ports}), + error.OutOfMemory => "out of memory", + }; p.plumber.text.clearRetainingCapacity(); p.plumber.text.appendSlice(p.gpa, out.written()) catch break :said "out of memory"; p.plumber.written = true; @@ -780,15 +859,28 @@ pub fn send(p: *Pardes, from: ?usize, msg: Msg, seen: ?Seen) Sent { const id = from orelse p.view().active; // dispose. if (m.dst.len == 0) return startup(&s, id, set orelse return .{ .refused = e_no_port }, &e); - if (std.meta.stringToEnum(Port, m.dst)) |port| return deliver(&s, id, port, &m); + const port = portIndex(p, m.dst); + // A port pardes reads itself: its readers' copies (a program reading + // plumb/edit sees every open, plumber(4)'s fan-out), the authority's + // alone to hand out, so asked of nobody; then pardes's own. + if (std.meta.stringToEnum(Port, m.dst)) |own| { + if (port) |i| fanOut(p, i, &m); + return deliver(&s, id, own, &m); + } if (p.kinds.byName(m.dst)) |k| { + if (port) |i| fanOut(p, i, &m); openInKind(&s, id, k, &m); return .done; } - // A port nobody reads: what the rule set starts, if it is one a rule - // names; one no rule names is no port, and the message goes nowhere, - // as fsys.c's dispose finds no Dirtab for it. - if (!s.rules.named(m.dst)) return .done; + // One no rule ever named is none, and the message goes nowhere, as + // fsys.c's dispose finds no Dirtab for it. Another goes to its readers, + // whom only the authority knows (its 9P opens); with none, what the + // rule set starts. + const i = port orelse return .done; + if (tape.authority(portRead, .{ p, i })) { + fanOut(p, i, &m); + return .done; + } return startup(&s, id, set orelse return .{ .refused = e_no_start }, &e); } @@ -814,10 +906,17 @@ fn startup(s: *Send, id: usize, set: Set, e: *const Exec) Sent { rest = rest[took..]; } if (argv.items.len == 0) return .{ .refused = e_empty }; - return if (run(s.p, id, e.m.wdir, argv.items)) |why| .{ .refused = why } else .done; + if (run(s.p, id, e.m.wdir, argv.items)) |why| return .{ .refused = why }; + // `client`: the message waits for the port's first reader, the program + // it started perhaps (plumber(4)'s hold); held messages past what the + // queue keeps push out the oldest. + if (act.verb == .client) if (portIndex(s.p, e.m.dst)) |i| { + if (!enqueue(s.p, &s.p.fs.plumb_held, &.{i}, e.m)) return .{ .refused = "plumb: message too big to hold for a client" }; + }; + return .done; } -/// What a `plumb start` (and, until ports are read, a `plumb client`) runs: +/// What a `plumb start` or a `plumb client` runs: /// the one place that decides it (question 4 of docs/next-plumber.md). /// A builtin or a kind's word runs in place, from the message's pane, as /// a line of it would; one that cannot do what the message asks declines @@ -1175,15 +1274,15 @@ test "plumb: dispose: no rule and no dst, a start, the web port, a port no rule // A URL to the web port: the host's opener. while (p.nextEffect()) |_| {} try testing.expect(send(p, null, .{ .type = "text", .data = "https://ziglang.org" }, null) == .done); - var opened = false; + var linked = false; while (p.nextEffect()) |e| switch (e) { .open_link => |u| { try testing.expectEqualStrings("https://ziglang.org", u.slice()); - opened = true; + linked = true; }, else => {}, }; - try testing.expect(opened); + try testing.expect(linked); } test "plumb: rules written replace or add, a refusal keeps them as they were, and a dump keeps them" { diff --git a/src/config.zig b/src/config.zig index 8a1528a8..0568fa26 100644 --- a/src/config.zig +++ b/src/config.zig @@ -1828,6 +1828,7 @@ test "reference.typ's #fstree is the tree the session serves, file for file" { const under = if (f.inPty() and f != .pty) "pane/<n>/pty/" else "pane/<n>/"; try served.put(gpa, try std.fmt.allocPrint(gpa, "{s}{s}{s}", .{ under, f.fileName(), if (f.isDir()) "/" else "" }), {}); }; + try served.put(gpa, "plumb/<port>", {}); try served.put(gpa, "col/<n>/", {}); for (std.enums.values(tree.ColFile)) |f| if (f != .dir) try served.put(gpa, try std.fmt.allocPrint(gpa, "col/<n>/{s}", .{f.fileName()}), {}); diff --git a/src/fs-help.txt b/src/fs-help.txt index 4e43a476..9a87843d 100644 --- a/src/fs-help.txt +++ b/src/fs-help.txt @@ -19,7 +19,7 @@ recent files opened lately, closed ones too: open|closed <path> dumps the dumps the state file keeps, the newest first: <label> <date> <size> <panes> layout a line per column: serial index x width current|notcurrent empty|full pane-serials; then active <serial> tag the workspace tag, tagexec a word as a click in it; col/<serial>/ a column's tag, ctl, exec; rmdir: empty only -plumb/ rules: the plumber's rules, plumb(6)'s (a write adds rule sets, > replaces); send: write a message, six lines then data +plumb/ rules: the plumber's rules, plumb(6)'s (a write adds rule sets, > replaces); send: write a message, six lines then data; <port>: read the messages sent to it pane/new open it to make a pane (<session dir>/+New) in the active column; the read answers its serial pane/<n>/ name body tag ctl addr dot limit data xdata sel dirty mark scroll errors event look exec tagexec rows; rmdir closes rows: a line a row, kind (m c p; d decl, u unmarked) line col end_line end_col source_column location_end code_start styles path; styles - or N:start+len=role,... (keyword string number comment, add del emph line, joined by .); a write at 0 replaces @@ -948,6 +948,10 @@ pub const Namespace = struct { /// The editor-wide event ring: panes made, renamed, saved and closed, and /// what the editor said. Recorded whether or not anyone reads /log. log: tree.events.Queue = .{ .cap = limits.log_bytes }, + /// Messages a plumbing rule's `client` holds for a port no one reads + /// yet (Plumb.zig), each its port's index then the message packed: the + /// port's first reader takes them (plumber(4)'s queueheld). + plumb_held: tree.events.Queue = .{}, /// What the last look or exec created or touched, answered by reading /// either of those files. results: [MAX_PANES]u32 = undefined, @@ -969,6 +973,7 @@ pub const Namespace = struct { for (st.closed_lines.items) |*line| line.bytes.deinit(gpa); st.closed_lines.deinit(gpa); st.log.deinit(gpa); + st.plumb_held.deinit(gpa); st.out.deinit(gpa); } diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 7bd586dc..7cdd3327 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -366,7 +366,7 @@ pub fn readResults(p: *Pardes, req: Req) Reply { const looking = if (tree.Node.target(req.node)) |t| switch (t) { .top => |f| f == .look, .pane => |pt| pt.file == .look, - .col => false, + .col, .port => false, } else false; for (kept) |serial| if (!looking or p.paneBySerial(serial) != null) out.print(p.gpa, "{d}\n", .{serial}) catch return Reply.fail(req.tag, E.NOMEM); diff --git a/src/ninep/events.zig b/src/ninep/events.zig index dd9b488c..5b5378b1 100644 --- a/src/ninep/events.zig +++ b/src/ninep/events.zig @@ -233,6 +233,10 @@ pub fn noteError(p: *Pardes, req: Req, reply: Reply) void { if ((f == .exec or f == .look) and p.fs.ran_at != 0) serial = name.print("{d}", .{p.fs.ran_at}); }, + .port => |i| { + serial = "-"; + file = name.print("plumb/{s}", .{pardes.Plumb.portName(p, i) orelse "?"}); + }, .col => |c| { serial = "-"; file = if (c.file == .dir) diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig index bd3f3b3b..e5e521c3 100644 --- a/src/ninep/tree.zig +++ b/src/ninep/tree.zig @@ -101,6 +101,7 @@ fn truncates(req: Req) bool { .pane => true, .col => |c| c.file == .tag, .top => |f| f == .tag or f == .rules, + .port => false, }; } @@ -156,6 +157,9 @@ pub const Open = struct { /// is not whole yet (rule sets with no blank line after them, a /// message short of its data). plumb, + /// An open reading a plumb port (Plumb.zig's ports, plumber(4)'s): + /// the messages sent to it since the open, packed, a read each. + port: struct { index: u8, queue: events.Queue = .{} }, } = .{ .snapshot = null }, /// What a write to a file of command lines left unfinished: a last line /// with no newline yet, or an Edit block still open. A write through a @@ -212,6 +216,7 @@ pub const Open = struct { gpa.free(run.line); }, .frozen => |bytes| gpa.free(bytes), + .port => |*port| port.queue.deinit(gpa), .event, .pty_data, .ctl, .lines, .writes, .pager, .plumb => {}, } o.pending.deinit(gpa); @@ -380,6 +385,10 @@ pub const ColFile = enum(u5) { } }; const col_bit: u59 = 1 << 58; +/// plumb/<port>: a port by its place in Plumb's list, which only grows; +/// past a pane serial's 32 bits and clear of the host tree's and the +/// archive's high bits (fs.os_node, sources.archive_node). +const port_bit: u59 = 1 << 40; pub const Node = packed struct(u64) { file: u5 = 0, @@ -394,11 +403,16 @@ pub const Node = packed struct(u64) { return @bitCast(Node{ .file = @backingInt(file), .serial = col_bit | @as(u59, serial) }); } + pub fn ofPort(index: u8) u64 { + return @bitCast(Node{ .serial = port_bit | @as(u59, index) }); + } + pub fn target(node: u64) ?Target { const n: Node = @bitCast(node); if (n.serial == 0) { return .{ .top = std.enums.fromInt(TopFile, n.file) orelse return null }; } + if (n.serial & port_bit != 0) return .{ .port = std.math.cast(u8, n.serial & ~port_bit) orelse return null }; if (n.serial & col_bit != 0) return .{ .col = .{ .serial = std.math.cast(u32, n.serial & ~col_bit) orelse return null, .file = std.enums.fromInt(ColFile, n.file) orelse return null, @@ -414,6 +428,7 @@ pub const Target = union(enum) { top: TopFile, pane: struct { serial: u32, file: PaneFile }, col: struct { serial: u32, file: ColFile }, + port: u8, }; /// Opening /pane/new makes a pane and reading the open fid answers its @@ -574,7 +589,7 @@ fn serve(p: *Pardes, req: Req) Reply { const target = Node.target(req.node) orelse return Reply.fail(req.tag, E.NOENT); if (req.op == .write or req.op == .setattr) p.fs.origin = switch (target) { .pane => |t| @as(u8, if (t.file == .body or t.file == .tag) 'E' else 'F'), - .top, .col => 'F', + .top, .col, .port => 'F', }; return switch (req.op) { .lookup => lookup(p, req, target), @@ -596,7 +611,7 @@ fn serve(p: *Pardes, req: Req) Reply { fn runAttr(p: *Pardes, req: Req, target: Target) ?Reply.Attr { const t = switch (target) { .pane => |t| t, - .top, .col => return null, + .top, .col, .port => return null, }; if (t.file != .pty_run) return null; const run = if (req.handle != 0) openOf(p, req) else for (&p.fs.opens) |*o| { @@ -608,6 +623,12 @@ fn runAttr(p: *Pardes, req: Req, target: Target) ?Reply.Attr { fn attrOf(p: *Pardes, target: Target) ?Reply.Attr { switch (target) { + .port => |i| return .{ + .name = Plumb.portName(p, i) orelse return null, + .node = Node.ofPort(i), + .mode = 0o444, + .mtime = p.fs.started, + }, .top => |f| return .{ .name = f.fileName(), .node = @backingInt(f), @@ -704,6 +725,7 @@ fn lookup(p: *Pardes, req: Req, target: Target) Reply { else => return Reply.fail(req.tag, E.NOTDIR), }, .col => |c| if (c.file == .dir) @backingInt(TopFile.col) else return Reply.fail(req.tag, E.NOTDIR), + .port => return Reply.fail(req.tag, E.NOTDIR), }; return handle(p, .{ .tag = req.tag, .op = .getattr, .node = parent }); } @@ -728,9 +750,10 @@ fn lookup(p: *Pardes, req: Req, target: Target) Reply { if (serial == 0) return Reply.fail(req.tag, E.NOENT); break :dir if (f == .pane) Node.of(serial, .dir) else Node.ofCol(serial, .dir); }, - .plumb => if (std.mem.eql(u8, name, "rules")) @backingInt(TopFile.rules) else if (std.mem.eql(u8, name, "send")) @backingInt(TopFile.send) else return Reply.fail(req.tag, E.NOENT), + .plumb => if (std.mem.eql(u8, name, "rules")) @backingInt(TopFile.rules) else if (std.mem.eql(u8, name, "send")) @backingInt(TopFile.send) else if (Plumb.portIndex(p, name)) |i| Node.ofPort(i) else return Reply.fail(req.tag, E.NOENT), else => return Reply.fail(req.tag, E.NOTDIR), }, + .port => return Reply.fail(req.tag, E.NOTDIR), .col => |c| col: { if (c.file != .dir) return Reply.fail(req.tag, E.NOTDIR); const f = std.meta.stringToEnum(ColFile, name) orelse return Reply.fail(req.tag, E.NOENT); @@ -790,8 +813,15 @@ fn stageDir(p: *Pardes, out: *std.ArrayList(u8), target: Target, off: u64) error try stageDirent(out, p.gpa, Node.of(serial, .dir), true, name); } }, - .plumb => inline for (.{ TopFile.rules, TopFile.send }) |t| { - if (skip > 0) skip -= 1 else try stageDirent(out, p.gpa, @backingInt(t), false, t.fileName()); + // plumber(4)'s order: rules, send, then the ports as rules + // named them. + .plumb => { + inline for (.{ TopFile.rules, TopFile.send }) |t| { + if (skip > 0) skip -= 1 else try stageDirent(out, p.gpa, @backingInt(t), false, t.fileName()); + } + for (p.plumber.ports.items, 0..) |name, i| { + if (skip > 0) skip -= 1 else try stageDirent(out, p.gpa, Node.ofPort(@intCast(i)), false, name); + } }, .col => for (0..p.view().ncol) |c| { if (skip > 0) { @@ -812,6 +842,7 @@ fn stageDir(p: *Pardes, out: *std.ArrayList(u8), target: Target, off: u64) error if (skip > 0) skip -= 1 else try stageDirent(out, p.gpa, Node.ofCol(c.serial, f), false, f.fileName()); } }, + .port => return error.NotDir, .pane => |t| { const id = p.paneBySerial(t.serial) orelse return error.FileNotFound; const terminal = p.panes[id].?.isTerminal(); @@ -872,6 +903,9 @@ fn open(p: *Pardes, req: Req, target: Target) Reply { .col => |c| if (layout.columnBySerial(p, c.serial) == null) return Reply.fail(req.tag, E.NOENT) else if (linesOpen(target, req)) .lines else if (c.file == .ctl) .{ .frozen = &.{} } else return .{ .tag = req.tag }, + // A reader of the port: what is sent to it from now on, and what a + // `client` rule held for its first. + .port => |i| if (req.omode & 3 != 0) return Reply.fail(req.tag, E.PERM) else .{ .port = .{ .index = i } }, .pane => |t| what: { const pn = p.panes[p.paneBySerial(t.serial) orelse return Reply.fail(req.tag, E.NOENT)].?; if (t.file.inPty() and !pn.isTerminal()) return Reply.fail(req.tag, E.NOENT); @@ -929,6 +963,7 @@ fn open(p: *Pardes, req: Req, target: Target) Reply { pn.fs.event_reader = pn.fs.event_reader or reader; }, .pty_data => pane_of.?.fs.pty_reader = true, + .port => |*port| Plumb.opened(p, port.index, &port.queue), .run, .ctl, .lines, .writes, .pager, .plumb => {}, } return .{ .tag = req.tag, .handle = @intCast(i + 1) }; @@ -940,7 +975,7 @@ fn remove(p: *Pardes, req: Req) Reply { const target = Node.target(req.node) orelse return Reply.fail(req.tag, E.NOENT); const t = switch (target) { .pane => |t| t, - .top => return Reply.fail(req.tag, E.PERM), + .top, .port => return Reply.fail(req.tag, E.PERM), .col => |c| return cols.remove(p, req, c.serial, c.file), }; if (t.file != .dir) return Reply.fail(req.tag, E.PERM); @@ -1027,7 +1062,7 @@ fn releaseHandle(p: *Pardes, req: Req) void { .ctl => if (pn.fs.lock == req.handle) { pn.fs.lock = null; }, - .snapshot, .log, .lines, .writes, .frozen, .pager, .plumb => {}, + .snapshot, .log, .lines, .writes, .frozen, .pager, .plumb, .port => {}, }; o.deinit(p.gpa); } @@ -1046,6 +1081,7 @@ fn setattr(p: *Pardes, req: Req, target: Target) Reply { }, .col => |c| if (c.file == .tag and cols.truncate(p, c.serial) != .ok) return Reply.fail(req.tag, E.NOENT), .top => |f| if (f == .tag and cols.truncate(p, null) != .ok) return Reply.fail(req.tag, E.NOMEM) else if (f == .rules) Plumb.clear(p), + .port => return Reply.fail(req.tag, E.PERM), }; return .{ .tag = req.tag, .attr = attrOf(p, target) orelse return Reply.fail(req.tag, E.NOENT) }; } @@ -1056,6 +1092,8 @@ fn read(p: *Pardes, req: Req, target: Target) Reply { if (open_record) |o| if (o.what == .pager) ctl.fillPager(p, o.what.pager, &o.pending) catch return Reply.fail(req.tag, E.NOMEM); // A listing reads what it was at the open. if (open_record) |o| if (o.what == .frozen) return window(p, req, o.what.frozen); + // A port: a message a read, held until one is sent. + if (open_record) |o| if (o.what == .port) return events.readQueue(p, req, &o.what.port.queue); const stream = if (open_record) |o| switch (o.what) { .log => o, .event => |reader| if (reader) o else null, @@ -1182,6 +1220,8 @@ fn readFile(p: *Pardes, req: Req, target: Target) Reply { .exec => ctl.readResults(p, req), else => Reply.fail(req.tag, E.PERM), }, + // Read through its open (read), never without one. + .port => return Reply.fail(req.tag, E.PERM), } } @@ -1193,6 +1233,7 @@ fn linesFile(target: Target) bool { // `name` too: one name, however its writes cut it, applied once // its newline comes, or with its write when that is whole. .pane => |t| t.file == .look or t.file == .exec or t.file == .ctl or t.file == .tagexec or t.file == .name or t.file == .rows, + .port => false, }; } @@ -1202,6 +1243,7 @@ fn resultsFile(target: Target) bool { .top => |f| f == .look or f == .exec or f == .tagexec or f == .pager, .col => |c| c.file == .exec, .pane => |t| t.file == .look or t.file == .exec or t.file == .tagexec, + .port => false, }; } @@ -1248,6 +1290,7 @@ fn ctlFile(target: Target) bool { .top => |f| f == .ctl, .col => |c| c.file == .ctl, .pane => |t| t.file == .ctl, + .port => false, }; } @@ -1398,6 +1441,7 @@ fn writeNow(p: *Pardes, req: Req, target: Target) Reply { .exec => cols.writeExec(p, req, c.serial), .dir => Reply.fail(req.tag, E.PERM), }, + .port => return Reply.fail(req.tag, E.PERM), } } @@ -1671,9 +1715,9 @@ test "readdir lists the root and a pane directory without creating anything" { @memcpy(plumb_bytes[0..plumb_reply.bytes.len], plumb_reply.bytes); var plumb_buf: [4]th.Dirent = undefined; const plumb = th.dirents(plumb_bytes[0..plumb_reply.bytes.len], &plumb_buf); - try testing.expectEqual(@as(usize, 2), plumb.len); - try testing.expectEqualStrings("rules", plumb[0].name); - try testing.expectEqualStrings("send", plumb[1].name); + // Then the ports the default rules name, in their order. + try testing.expectEqual(@as(usize, 4), plumb.len); + for ([_][]const u8{ "rules", "send", "web", "edit" }, plumb) |want, got| try testing.expectEqualStrings(want, got.name); var idbuf: [16]u8 = undefined; try testing.expect(th.nameAt(top, try std.fmt.bufPrint(&idbuf, "{d}", .{serial})) == null); try testing.expect(th.nameAt(top, "self") == null); diff --git a/test/behaviour/plumber.zig b/test/behaviour/plumber.zig index 28451222..4144901b 100644 --- a/test/behaviour/plumber.zig +++ b/test/behaviour/plumber.zig @@ -63,3 +63,44 @@ test "plumber: plumb/send takes a message in its six lines and data, written in try s.write("/plumb/send", s.at("test\n\n{s}\ntext\n\n{d}\nplumbed.txt:2", .{ dir, "plumbed.txt:2".len })); try std.testing.expect(has(try s.read("/index"), "/plumbed.txt")); } + +test "plumber: a port's file is read by programs: each open made before a send gets its own copy, one made after none, and a client rule's message waits for the port's first reader" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "seen.txt", .data = "one\ntwo\n" }); + var buf: [4096]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; + // A file for each port the rules name, read-only, as plumber(4)'s. + try std.testing.expectEqual(@as(u16, 0o444), (try s.stat("/plumb/web")).mode & 0o777); + try std.testing.expectEqual(@as(u16, 0o444), (try s.stat("/plumb/edit")).mode & 0o777); + // Two readers of edit, then a look: each reads the look's message, + // and the editor opens the file all the same. + const a = try s.hold("/plumb/edit", 0); + const b = try s.hold("/plumb/edit", 0); + try s.write("/look", s.at("{s}/seen.txt\n", .{dir})); + const want = s.at("pardes\nedit\n{s}\ntext\n\n{d}\n{s}/seen.txt", .{ "/", dir.len + "/seen.txt".len, dir }); + try std.testing.expectEqualStrings(want, try s.readFid(a)); + try std.testing.expectEqualStrings(want, try s.readFid(b)); + try std.testing.expect(has(try s.read("/index"), "/seen.txt")); + // An open made after the send has nothing: its read waits. + const late = try s.hold("/plumb/edit", 0); + try std.testing.expectError(error.Refused, s.readFid(late)); + s.clunk(late); + s.clunk(a); + s.clunk(b); + // A port only a program reads, its rule a client: the message waits + // for the port's first reader, the program started read nothing. + try s.write("/plumb/rules", "type is text\ndata matches 'letter-[0-9]+'\nplumb to seemail\nplumb client true\n\n"); + try s.write("/plumb/send", "test\n\n/\ntext\n\n9\nletter-42"); + const first = try s.hold("/plumb/seemail", 0); + try std.testing.expectEqualStrings("test\nseemail\n/\ntext\n\n9\nletter-42", try s.readFid(first)); + try std.testing.expectError(error.Refused, s.readFid(first)); + // Once read, a port's message goes to its readers, no program started. + try s.write("/plumb/send", "test\n\n/\ntext\n\n9\nletter-43"); + try std.testing.expectEqualStrings("test\nseemail\n/\ntext\n\n9\nletter-43", try s.readFid(first)); + s.clunk(first); + // Not to be written. + try std.testing.expectError(error.Refused, s.hold("/plumb/seemail", 1)); +} diff --git a/test/monkey9p.zig b/test/monkey9p.zig index c9dd5083..a4bf92a3 100644 --- a/test/monkey9p.zig +++ b/test/monkey9p.zig @@ -92,7 +92,7 @@ const builtins = [_][]const u8{ "Theme", "Get", "Put", "Undo", "Redo", "Zerox", "Snarf", "Paste", "Cut", "Sort", "Font", "Local", }; const pane_files = [_][]const u8{ "name", "body", "tag", "ctl", "addr", "dot", "limit", "data", "xdata", "sel", "dirty", "mark", "scroll", "errors", "event", "look", "exec", "tagexec", "pty", "pty/run", "pty/data", "pty/ctl", "pty/status" }; -const root_files = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "dumps", "look", "exec", "log", "screen", "listeners", "layout", "tag", "tagexec", "pane", "pane/new", "col", "" }; +const root_files = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "dumps", "look", "exec", "log", "screen", "listeners", "layout", "tag", "tagexec", "pane", "pane/new", "col", "plumb", "plumb/rules", "plumb/send", "plumb/edit", "plumb/web", "plumb/seemail", "" }; const long_x: [300]u8 = @splat('x'); const col_files = [_][]const u8{ "tag", "ctl", "exec" }; const writable = [_][]const u8{ "ctl", "exec", "look", "tag", "tagexec", "focus", "body", "data", "xdata", "addr", "dot", "limit", "name", "sel", "dirty", "mark", "scroll", "errors", "event", "pty/run", "pty/data", "pty/ctl", "log" }; @@ -199,7 +199,7 @@ fn pickCol(r: Random, a: Allocator) []const u8 { fn randomPath(r: Random, a: Allocator, write: bool) []const u8 { const x = chance(r); if (x < 0.6) return print(a, "/pane/{s}/{s}", .{ pickSerial(r, a), if (write) pick(r, &writable) else pick(r, &pane_files) }); - if (x < 0.75) return cat(a, &.{ "/", if (write) pick(r, &[_][]const u8{ "ctl", "exec", "look", "tag", "tagexec", "focus", "log" }) else pick(r, &root_files) }); + if (x < 0.75) return cat(a, &.{ "/", if (write) pick(r, &[_][]const u8{ "ctl", "exec", "look", "tag", "tagexec", "focus", "log", "plumb/rules", "plumb/send", "plumb/edit" }) else pick(r, &root_files) }); if (x < 0.85) return print(a, "/col/{s}/{s}", .{ pickCol(r, a), pick(r, &col_files) }); if (x < 0.93) return print(a, "/pane/{s}/{s}", .{ pickSerial(r, a), weirdName(r, a) }); var out: []const u8 = ""; @@ -230,6 +230,10 @@ fn dataFor(r: Random, a: Allocator, path: []const u8) []const u8 { return cat(a, &.{ shellSafe(a, pick(r, &[_][]const u8{ "alpha", "alpha.txt", "alpha.txt:2", "utf8.txt:#3", "nothere", "bad.txt", "crlf.txt:1", "gamma", "beta" })), "\n" }); if (is(b, &.{ "dirty", "mark", "scroll" })) return pick(r, &[_][]const u8{ "0", "1", "0\n", "1\n", "2\n", "", "yes\n" }); if (std.mem.eql(u8, b, "log")) return pick(r, &[_][]const u8{ "follow\n", "follow new\n", "follow", "nope\n" }); + // The plumber's: rule sets that start no program, half ones, bad ones; + // messages whole, short of their data, malformed. + if (std.mem.eql(u8, b, "rules")) return pick(r, &[_][]const u8{ "type is text\ndata matches 'letter-[0-9]+'\nplumb to seemail\n\n", "include basic\n", "type is text\narg isfile $data\nplumb to edit\n", "data isnt x\n", "plumb to seemail\n\n", "x='[a-z]+'\n", "" }); + if (std.mem.eql(u8, b, "send")) return pick(r, &[_][]const u8{ "monkey\n\n/\ntext\n\n9\nletter-42", "monkey\n\n/\ntext\n\n9\nalpha.txt", "monkey\nseemail\n/\ntext\n\n1\nx", "monkey\n\n/\ntext\n\n20\nshort", "a\nb\n" }); return payload(r, a, 200); } @@ -704,7 +708,8 @@ fn hostPath(path: []const u8) bool { fn isHeld(path: []const u8) bool { return std.mem.eql(u8, std.mem.trimEnd(u8, path, "/"), "/log") or std.mem.endsWith(u8, path, "/event") or - std.mem.endsWith(u8, path, "/pty/data") or std.mem.endsWith(u8, path, "/pty/run"); + std.mem.endsWith(u8, path, "/pty/data") or std.mem.endsWith(u8, path, "/pty/run") or + (std.mem.startsWith(u8, path, "/plumb/") and !std.mem.endsWith(u8, path, "/rules") and !std.mem.endsWith(u8, path, "/send")); } /// An open the rule names: pane/new's (it makes a pane) and a truncation @@ -931,7 +936,7 @@ fn exec(c: *Ctx, a: Allocator, res: *Res, op: Op) anyerror!void { } const closed = try clunk(c, a, res, &f, o.path); // What the open holds when it closes: the last line written, if unended. - if (opened and o.mode & 3 != 0 and sent.len > 0 and !std.mem.endsWith(u8, sent, "\n")) + if (opened and o.mode & 3 != 0 and restAtClose(o.path, sent)) res.add(a, .{ .kind = .close_runs, .path = o.path, .ok = closed }); }, .read => |o| { @@ -1014,7 +1019,7 @@ fn exec(c: *Ctx, a: Allocator, res: *Res, op: Op) anyerror!void { } var h = c.handles.orderedRemove(i % c.handles.items.len); const ok = try clunk(c, a, res, &h.fid, h.path); - if (h.fid.mode & 3 != 0 and h.last.len > 0 and !std.mem.endsWith(u8, h.last, "\n")) + if (h.fid.mode & 3 != 0 and restAtClose(h.path, h.last)) res.add(a, .{ .kind = .close_runs, .path = h.path, .ok = ok }); }, .restore => |data| { @@ -1322,7 +1327,15 @@ fn oneFailureRule(c: *Ctx, a: Allocator, res: *Res) ?[]const u8 { return null; } -const line_files = [_][]const u8{ "look", "exec", "tagexec", "ctl" }; +const line_files = [_][]const u8{ "look", "exec", "tagexec", "ctl", "rules" }; + +/// What an open that wrote `sent` last leaves to its close: a line with no +/// newline yet, or of plumb/rules' text a rule set with no blank line +/// after it (plumber(4)'s, read at the close). +fn restAtClose(path: []const u8, sent: []const u8) bool { + if (sent.len == 0) return false; + return !std.mem.endsWith(u8, sent, if (std.mem.endsWith(u8, path, "plumb/rules")) "\n\n" else "\n"); +} /// docs/typ/reference.typ: 'A write of command lines -- to look, exec, /// tagexec, a ctl of the root, a pane or a column, or a column's exec -- diff --git a/test/replicas.zig b/test/replicas.zig index 0f23d5a8..5e49a3ac 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -608,7 +608,52 @@ fn plumbCase(dir: []const u8) !bool { ok = false; } }; - std.debug.print("plumb: a URL and a commit hash looked at by the authority and as a replica's own input: {s}\n", .{if (ok) "the same session" else "FAILED"}); + // A rule to a port a program reads (plumb/seemail), of a URL, which a + // look reads no disk for: whether the port is read is the authority's + // 9P state, asked through the tape. A replica's own look waits for the + // first answer, then guesses the last; the second look speculates whole + // and is confirmed with no rollback. + { + // The rule from the session's plumbing file, read at boot. + var pbuf: [256]u8 = undefined; + try writeText(try std.fmt.bufPrintSentinel(&pbuf, "{s}/plumbing", .{dir}, 0), "type is text\ndata matches 'https://letters/.*'\nplumb to seemail\n\ninclude basic\n"); + const a = try replica.Authority.init(gpa, .{ .cols = 100, .rows = 30, .boot = .tty, .config_dir = dir }, host.host(), true); + defer a.deinit(); + const first = try gpa.dupe(u8, try a.unit(0, 0)); + defer gpa.free(first); + const rp = try replica.Replica.init(gpa, 1, first, true); + defer rp.deinit(); + rp.confirm(try a.local(.{ .command = open })); + rp.settle(); + if (pardes.Plumb.portIndex(rp.core(), "seemail") == null) { + std.debug.print("plumb: a look to a port: the plumbing file's port is not the replica's\n", .{}); + ok = false; + } + for (0..2) |n| { + const before = rp.stats; + const msg = try gpa.dupe(u8, try rp.input(.{ .command = "Look https://letters/42" })); + defer gpa.free(msg); + if (n == 1 and (rp.stats.stopped != before.stopped or rp.stats.speculated == before.speculated)) { + std.debug.print("plumb: a look to a port: the replica's second look did not speculate whole ({d} stopped, {d} applied)\n", .{ rp.stats.stopped - before.stopped, rp.stats.speculated - before.speculated }); + ok = false; + } + if (try a.pending()) |batch| rp.confirm(batch); + rp.confirm(try a.take(msg)); + rp.settle(); + } + if (rp.diverged) |why| { + std.debug.print("plumb: a look to a port: the replica diverged: {s}\n", .{why}); + ok = false; + } else if (!rp.quiet() or rp.digest() != replica.digestOf(a)) { + std.debug.print("plumb: a look to a port: the replica's digest is not the authority's\n", .{}); + ok = false; + } + if (rp.stats.mispredicted + rp.stats.foreign != 0) { + std.debug.print("plumb: a look to a port: {d} rollbacks\n", .{rp.stats.mispredicted + rp.stats.foreign}); + ok = false; + } + } + std.debug.print("plumb: a URL, a commit hash and a look to a port read by programs, by the authority and as a replica's own input: {s}\n", .{if (ok) "the same session" else "FAILED"}); return ok; } |
