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 /src/Plumb.zig | |
| 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
Diffstat (limited to 'src/Plumb.zig')
| -rw-r--r-- | src/Plumb.zig | 121 |
1 files changed, 110 insertions, 11 deletions
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" { |
