From 757cdaa826131dc5ff2d3ff0b270b8abe041c81a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 02:40:21 -0300 Subject: plumber: external ports (docs/next-plumber.md, phase 3): every port a rule names has a file, plumb/ (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 --- src/ninep/tree.zig | 64 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 10 deletions(-) (limited to 'src/ninep/tree.zig') 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/: 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); -- cgit v1.3