From 6a6e1b2cb3e328d5abd4435c1c2659f4e4e31eb8 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 16:55:47 -0300 Subject: the replica socket: the daemon listens on pardes-replica.sock, a stream `ssh -L` can forward, refuses another build's hello in words and hands the connection to its session, which welcomes it and answers pings The daemon binds `pardes-replica.sock` beside its own (SOCK_STREAM, so ssh can forward it; the SEQPACKET socket that carries fds stays local) once it holds its lock. A replica's first message is a hello (detached/wire.zig, now version 14): the protocol version at offset 0, the client's semantics hash and the session's name. The daemon reads exactly that much and no further, so whatever the client sends next is the session's, keeps up to 16 hellos still on their way (ponytail: one that never comes holds its slot until it hangs up), and refuses in words another protocol, a session nobody has, or another build: every session now says its own hash in its registration (daemon protocol 2), so a mismatch never reaches it. An accepted connection goes to the session over its link (SCM_RIGHTS) and the daemon keeps no copy; the session welcomes it with its pid and name on a thread of its own and answers each ping with a pong, until bye or a hangup. ponytail: that stub is all a session does with a replica until phase 5 proper; it is what times a round trip through the handoff and `ssh -L`. The semantics hash (wire.semantics) is phase 1's undone item: the protocol, the release and commit, the platform, the build options, every dependency the manifest pins by its content hash (grammars, ghostty, the Unicode tables: build.zig's new `deps` option) and the walker codec's assumptions (pointer width, byte order). Not the OS: a Linux client of a FreeBSD session is the main case. ponytail: an unstamped build has no commit, so two dev trees of one release with the same options are not told apart. `pardes --version` prints it on a second line. The hello lost its grid (a replica's view takes a resize like any other input). test/daemon.py: a hello of this build welcomed by the session itself (its pid, one fd more in the session and none in the daemon, and back once it hangs up), 50 pings, another build's hello and an unknown session's refused, and `--ping [n]`, which times a hello and n pings on any replica socket (a forwarded one, say). Co-Authored-By: Claude Opus 5.5 --- src/daemon.zig | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 162 insertions(+), 4 deletions(-) (limited to 'src/daemon.zig') diff --git a/src/daemon.zig b/src/daemon.zig index ac9e6eb4..702d11b1 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -16,14 +16,22 @@ //! //! Messages are SOCK_SEQPACKET datagrams: a line of words, and the fds it //! carries (SCM_RIGHTS). +//! +//! A replica (docs/next-daemon.md, phase 5) comes in on a stream socket of +//! its own, `pardes-replica.sock`, which `ssh -L` can forward: its hello +//! (detached/wire.zig) names a session, and once its build is the session's +//! the connection is handed to that session, which it talks to from then on. const std = @import("std"); const builtin = @import("builtin"); const libc = std.c; const posix = std.posix; const ninep_io = @import("9p_io.zig"); +const wire = @import("detached/wire.zig"); +const log = std.log.scoped(.replica); pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd; -const version = 1; +/// 2: a session's hello carries its semantics hash (wire.semantics). +const version = 2; pub const max_fds = 64; const msg_max = 4096; /// Idle this long, it exits: long enough for the session that started it to @@ -31,6 +39,9 @@ const msg_max = 4096; const linger_ms = 1000; const max_conns = 64; const max_held = 256; +/// Replicas whose hello is still on its way. ponytail: one that never sends +/// it holds its slot until it hangs up; past 16 a new one is closed at once. +const max_replicas = 16; const sun_path_len = ninep_io.sun_path_len; const nosignal: u32 = if (builtin.os.tag == .linux) libc.MSG.NOSIGNAL else 0; @@ -258,7 +269,7 @@ fn spawn(listener: c_int, lock: c_int) bool { /// (`watch`) takes the `ok`, and only then is the session kept (`kept`). fn hello(fd: c_int, k: Kind, pid: libc.pid_t, named: []const u8, detached: bool) bool { var buf: [256]u8 = undefined; - const words = std.fmt.bufPrint(&buf, "hello {d} {t} {d} {s} {s}", .{ version, k, pid, if (detached) "detached" else "attached", named }) catch return false; + const words = std.fmt.bufPrint(&buf, "hello {d} {t} {d} {s} {x} {s}", .{ version, k, pid, if (detached) "detached" else "attached", wire.semantics, named }) catch return false; return send(fd, words, &.{}); } @@ -376,6 +387,11 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no .stub = if (got.nfds >= 2) got.fds[1] else -1, } }); for (got.fds[@min(got.nfds, 2)..got.nfds]) |f| _ = libc.close(f); + } else if (std.mem.eql(u8, verb, "replica")) { + if (got.nfds >= 1) { + if (std.Thread.spawn(.{}, serveReplica, .{got.fds[0]})) |t| t.detach() else |_| _ = libc.close(got.fds[0]); + } + for (got.fds[@min(got.nfds, 1)..got.nfds]) |f| _ = libc.close(f); } else if (std.mem.eql(u8, verb, "adopted")) { var note: Note = .{ .adopted = .{ .pids = undefined, .fds = undefined, .n = 0 } }; const a = ¬e.adopted; @@ -395,6 +411,49 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no } } +fn writeAll(fd: c_int, bytes: []const u8) bool { + var at: usize = 0; + while (at < bytes.len) { + const n = libc.send(fd, bytes[at..].ptr, bytes.len - at, nosignal); + if (n < 0 and libc.errno(n) == .INTR) continue; + if (n <= 0) return false; + at += @intCast(n); + } + return true; +} + +/// A replica the daemon handed over, its hello accepted: welcomed, its pings +/// answered, until it says bye or hangs up. ponytail: phase 5 proper makes it +/// a replica (the log, the dump, terminal state); this stub is what times a +/// round trip through the daemon's handoff and `ssh -L`. A message larger +/// than its buffer ends it. +fn serveReplica(fd: c_int) void { + defer _ = libc.close(fd); + log.info("a replica connected to '{s}'", .{name()}); + defer log.info("a replica of '{s}' left", .{name()}); + var out: [wire.hello_max]u8 = undefined; + const pid: u32 = @intCast(libc.getpid()); + if (!writeAll(fd, wire.encodeServer(&out, .{ .welcome = .{ .pid = pid, .session = name() } }) catch return)) return; + var buf: [msg_max]u8 = undefined; + var len: usize = 0; + while (true) { + while (wire.framed(buf[0..len]) catch return) |f| { + switch (wire.decodeClient(f.tag, f.payload) catch return) { + .ping => |v| if (!writeAll(fd, wire.encodeServer(&out, .{ .pong = v }) catch return)) return, + .bye, .hello => return, + .event => {}, + } + std.mem.copyForwards(u8, &buf, buf[f.total..len]); + len -= f.total; + } + if (len == buf.len) return; + const n = libc.read(fd, buf[len..].ptr, buf.len - len); + if (n < 0 and libc.errno(n) == .INTR) continue; + if (n <= 0) return; + len += @intCast(n); + } +} + /// A session ended by a signal (TERM, HUP, INT): its shells go with it, as /// at a quit. Async-signal-safe: one send. One killed outright (KILL) or /// crashed says nothing, and its shells live on for an Adopt. @@ -610,6 +669,8 @@ const Conn = struct { kind: ?Kind = null, pid: libc.pid_t = 0, detached: bool = false, + /// Its build's wire.semantics: a replica's must be the same. + semantics: u64 = 0, name: [128]u8 = undefined, name_len: usize = 0, @@ -625,9 +686,18 @@ const Held = struct { owner: ?u8 = null, }; +/// A replica's connection until its hello is whole: read no further than +/// the hello, so what follows it is the session's to read. +const Replica = struct { + fd: c_int = -1, + buf: [wire.hello_max]u8 = undefined, + len: usize = 0, +}; + const Daemon = struct { conns: [max_conns]Conn = @splat(.{}), held: [max_held]Held = @splat(.{}), + replicas: [max_replicas]Replica = @splat(.{}), fn idle(d: *const Daemon) bool { for (d.conns) |c| if (c.fd >= 0) return false; @@ -668,6 +738,7 @@ const Daemon = struct { const k = std.meta.stringToEnum(Kind, words.next() orelse ""); const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; const state = words.next() orelse ""; + const semantics = std.fmt.parseInt(u64, words.next() orelse "", 16) catch 0; const rest = std.mem.trim(u8, words.rest(), " "); if (v != version or k == null or pid <= 0) { // Another build's: it runs without a daemon. @@ -677,6 +748,7 @@ const Daemon = struct { c.kind = k; c.pid = pid; c.detached = std.mem.eql(u8, state, "detached"); + c.semantics = semantics; c.name_len = @min(rest.len, c.name.len); @memcpy(c.name[0..c.name_len], rest[0..c.name_len]); return d.reply(i, "ok", &.{}); @@ -796,6 +868,58 @@ const Daemon = struct { return error.Said; } + /// More of a replica's hello; once it is whole, the connection refused + /// in words or handed to its session. + fn hear(d: *Daemon, r: *Replica) void { + const want = if (r.len < wire.header_len) wire.header_len else wire.header_len + @as(usize, std.mem.readInt(u32, r.buf[1..5], .little)); + if (want > r.buf.len) return refuse(r, "the first message is not a hello"); + const n = libc.read(r.fd, r.buf[r.len..want].ptr, want - r.len); + if (n < 0 and libc.errno(n) == .INTR) return; + if (n <= 0) { + _ = libc.close(r.fd); + r.* = .{}; + return; + } + r.len += @intCast(n); + if (r.len < wire.header_len or r.len < wire.header_len + std.mem.readInt(u32, r.buf[1..5], .little)) return; + const f = (wire.framed(r.buf[0..r.len]) catch null) orelse return refuse(r, "the first message is not a hello"); + if (f.tag != @backingInt(wire.ClientTag.hello)) return refuse(r, "the first message is not a hello"); + var why: [512]u8 = undefined; + const v = wire.helloVersion(f.payload) catch 0; + if (v != wire.version) return refuse(r, std.fmt.bufPrint(&why, "this client speaks replica protocol {d}, and this daemon {d}: run the same pardes at both ends", .{ v, wire.version }) catch "another protocol"); + const h = (wire.decodeClient(f.tag, f.payload) catch return refuse(r, "a malformed hello")).hello; + const target = for (&d.conns, 0..) |*c, i| { + if (c.fd >= 0 and c.kind != null and std.mem.eql(u8, c.named(), h.session)) break i; + } else return refuse(r, std.fmt.bufPrint(&why, "no session called '{s}'", .{h.session}) catch "no such session"); + const t = &d.conns[target]; + if (t.semantics != h.semantics) return refuse(r, std.fmt.bufPrint(&why, "session '{s}' runs another build of pardes (semantics {x:0>16}, this client's {x:0>16}): a replica needs the same build at both ends, as `pardes --version` shows", .{ h.session, t.semantics, h.semantics }) catch "another build"); + if (!send(t.fd, "replica", &.{r.fd})) { + d.hangUp(@intCast(target)); + return refuse(r, "that session does not answer"); + } + log.info("a replica handed to '{s}'", .{h.session}); + _ = libc.close(r.fd); + r.* = .{}; + } + + fn refuse(r: *Replica, why: []const u8) void { + var out: [600]u8 = undefined; + // A fresh connection's buffer is empty: this never blocks. + if (wire.encodeServer(&out, .{ .refused = why[0..@min(why.len, 512)] })) |bytes| _ = writeAll(r.fd, bytes) else |_| {} + _ = libc.close(r.fd); + r.* = .{}; + } + + fn acceptReplica(d: *Daemon, listener: c_int) void { + const fd = libc.accept4(listener, null, null, libc.SOCK.CLOEXEC); + if (fd < 0) return; + for (&d.replicas) |*r| if (r.fd < 0) { + r.* = .{ .fd = fd }; + return; + }; + _ = libc.close(fd); + } + fn accept(d: *Daemon, listener: c_int) void { const fd = libc.accept4(listener, null, null, libc.SOCK.CLOEXEC); if (fd < 0) return; @@ -807,6 +931,21 @@ const Daemon = struct { } }; +/// The replicas' stream socket, bound and listening; -1 when it cannot be +/// (a runtime directory too long for a socket's path: 104 bytes on FreeBSD). +/// Ours to replace: the daemon's lock is held. +fn listenReplicas() c_int { + var sock_buf: [sun_path_len:0]u8 = undefined; + const sock = place(&sock_buf, "pardes-replica.sock") orelse return -1; + _ = libc.unlink(sock); + const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0); + if (fd < 0) return -1; + const addr = address(sock); + if (libc.bind(fd, @ptrCast(&addr), @sizeOf(libc.sockaddr.un)) == 0 and libc.chmod(sock, 0o600) == 0 and libc.listen(fd, max_replicas) == 0) return fd; + _ = libc.close(fd); + return -1; +} + /// `pardes --daemon`: what `spawn` started, on the listener (fd 3) and the /// lock (fd 4). Returns only to exit, idle. pub fn serve() noreturn { @@ -815,22 +954,33 @@ pub fn serve() noreturn { ninep_io.setCloexec(4); const ignore: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.IGN }, .mask = posix.sigemptyset(), .flags = 0 }; for ([_]posix.SIG{ .PIPE, .HUP, .INT }) |sig| posix.sigaction(sig, &ignore, null); + const replicas = listenReplicas(); var d: Daemon = .{}; - const slots = 1 + max_conns + max_held; + const slots = 2 + max_conns + max_replicas + max_held; var fds: [slots]libc.pollfd = undefined; var conn_of: [slots]u8 = undefined; var held_of: [slots]u16 = undefined; + var replica_of: [slots]u8 = undefined; var buf: [msg_max]u8 = undefined; while (true) { var n: usize = 0; fds[n] = .{ .fd = listener, .events = libc.POLL.IN, .revents = 0 }; n += 1; + // -1 when there is none: poll skips a negative fd. + fds[n] = .{ .fd = replicas, .events = libc.POLL.IN, .revents = 0 }; + n += 1; const conns_at = n; for (&d.conns, 0..) |*c, i| if (c.fd >= 0) { fds[n] = .{ .fd = c.fd, .events = libc.POLL.IN, .revents = 0 }; conn_of[n] = @intCast(i); n += 1; }; + const replicas_at = n; + for (&d.replicas, 0..) |*r, i| if (r.fd >= 0) { + fds[n] = .{ .fd = r.fd, .events = libc.POLL.IN, .revents = 0 }; + replica_of[n] = @intCast(i); + n += 1; + }; const held_at = n; // No events asked of a master: its hangup (every slave closed, the // shell and all it ran gone) is reported anyway, and its output is @@ -858,7 +1008,12 @@ pub fn serve() noreturn { break; } if (fds[0].revents & libc.POLL.IN != 0) d.accept(listener); - for (fds[conns_at..held_at], conn_of[conns_at..held_at]) |pfd, i| { + if (fds[1].revents & libc.POLL.IN != 0) d.acceptReplica(replicas); + for (fds[replicas_at..held_at], replica_of[replicas_at..held_at]) |pfd, i| { + if (pfd.revents == 0 or d.replicas[i].fd != pfd.fd) continue; + d.hear(&d.replicas[i]); + } + for (fds[conns_at..replicas_at], conn_of[conns_at..replicas_at]) |pfd, i| { if (pfd.revents == 0 or d.conns[i].fd != pfd.fd) continue; const got = recv(pfd.fd, &buf) orelse { d.hangUp(i); @@ -874,6 +1029,9 @@ pub fn serve() noreturn { var sock_buf: [sun_path_len:0]u8 = undefined; _ = libc.close(listener); if (place(&sock_buf, "pardes-daemon.sock")) |sock| _ = libc.unlink(sock); + if (replicas >= 0) { + if (place(&sock_buf, "pardes-replica.sock")) |sock| _ = libc.unlink(sock); + } libc._exit(0); } -- cgit v1.3