From fdf74e199de7d0fc648f912875323f3dc920ffc2 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 20:56:03 -0300 Subject: replica clients over a socket: the session streams each its dump and every unit after it (replica.zig `Peers`, a writer and a reader thread a client, units at each step boundary, a client's input stepped there in its own view), and `pardes --attach=:` and `--attach=:` (through `ssh -tt -L`) are a client in the tty host, its keys applied at once by its own replica and drawn by its own process wire.zig version 15: welcome (the client's id and view, the dump's length, the session's UTC offset and digest), dump pieces, units (with the authority's clocks), terminal messages both ways, a check. A unit carries one digest, at its end (`unit_digests`). The client: tty.zig `Shell.remote`, `feed` (a terminal's input to the replica and the session); the session's clock guessed from the last unit's (tape.zig `Guess`); a pump woken only by a client's post reads no new time (`mail_only`), and a unit of another's ending where the last ended leaves the speculation as it is (`neutral`); the rollback copy taken while idle (`Replica.ready`); views a frontend here does not draw left undrawn. A lagging client never holds the session: its queue is cut off past 64 MiB (PARDES_REPLICA_LAG_KB). Detach in a client ends that client; a Restore lets every client go. daemon.zig: a replica the daemon hands over is the session's loop's (a note), not a stub thread's. A window session refuses one in words. test/daemon.py `--clients`: two clients (one through the delay relay) type into a detached session and agree with it, one killed outright, one detached, a third typing into a shell. Co-Authored-By: Claude Opus 5.5 --- src/main.zig | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) (limited to 'src/main.zig') diff --git a/src/main.zig b/src/main.zig index a6144475..ab5047ce 100644 --- a/src/main.zig +++ b/src/main.zig @@ -108,6 +108,13 @@ const help_text = \\ the session detaches or ends \\ --attach= ...the session called (its --9p or \\ --detach name, else its pid) + \\ --attach=: a client of session on another machine, + \\ over `ssh -L` to its pardes-replica.sock + \\ ($PARDES_SSH: the ssh command, words; and + \\ $PARDES_REMOTE_RUNTIME: its runtime directory, + \\ if not this one's path). Its keys show at once + \\ --attach=: ...through that socket (a path, with a '/'), + \\ one forwarded already, or this machine's \\ --geometry=WxH[+X+Y] the window's size in points, and where it goes \\ where the display lets a window say (X11) \\ -h, --help show this help and exit @@ -738,6 +745,10 @@ fn nativeMain(std_init: std.process.Init) !void { opts.config_dir = found.dir; opts.store_path = storePath(init.io, arena, init.environ_map); if (found.dir) |d| @import("crash.zig").setDir(d); + if (attach) |name| if (std.mem.lastIndexOfScalar(u8, name, ':')) |colon| { + if (comptime pardes.platform != .tty) badArgs(init.io, "a window is no replica client yet: run `pardes --attach={s}` in a terminal", .{name}); + remoteAttach(init, opts, name[0..colon], name[colon + 1 ..]); + }; if (attach) |name| if (!resuming) attachTo(init.io, name); if (detach) |name| { // Said plainly, never a Zig error trace: a name a live session's @@ -815,6 +826,96 @@ fn nativeMain(std_init: std.process.Init) !void { daemon.tellStubExit(0); } +extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; + +/// `pardes --attach=:`: a replica client (tty.zig `remote`), +/// through the socket `where` names, or through `ssh -L` of host `where`'s +/// replica socket to one of this process's. +fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, name: []const u8) noreturn { + const io = init.io; + if (comptime pardes.platform != .tty) unreachable; + const Say = struct { + fn no(o: std.Io, comptime fmt: []const u8, args: anytype) noreturn { + var buf: [700]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(o, std.fmt.bufPrint(&buf, "pardes: --attach: " ++ fmt ++ "\n", args) catch "pardes: --attach failed\n") catch {}; + std.process.exit(1); + } + }; + if (std.mem.indexOfScalar(u8, where, '/') != null) { + @import("tty/tty.zig").remote(init, opts, where, name) catch |err| Say.no(io, "{t}", .{err}); + std.process.exit(0); + } + // ssh -L : , until the socket here answers. + var here_buf: [ninep_io.sun_path_len:0]u8 = undefined; + const tmp = std.mem.span(std.c.getenv("TMPDIR") orelse "/tmp"); + const here = std.fmt.bufPrintSentinel(&here_buf, "{s}/pardes-ssh-{d}.sock", .{ tmp, std.c.getpid() }, 0) catch Say.no(io, "TMPDIR is too long for a socket's path", .{}); + var there_buf: [ninep_io.sun_path_len + 64:0]u8 = undefined; + var dir_buf: [ninep_io.sun_path_len:0]u8 = undefined; + const dir = if (std.c.getenv("PARDES_REMOTE_RUNTIME")) |d| std.mem.span(d) else ninep_io.socketDir(&dir_buf) orelse Say.no(io, "no runtime directory to name the remote socket by: set PARDES_REMOTE_RUNTIME", .{}); + var forward_buf: [2 * ninep_io.sun_path_len + 64]u8 = undefined; + const there = std.fmt.bufPrintSentinel(&there_buf, "{s}/pardes-replica.sock", .{dir}, 0) catch unreachable; + const forward = std.fmt.bufPrintSentinel(&forward_buf, "{s}:{s}", .{ here, there }, 0) catch unreachable; + var host_buf: [256]u8 = undefined; + const host = std.fmt.bufPrintSentinel(&host_buf, "{s}", .{where}, 0) catch Say.no(io, "that host's name is too long", .{}); + var argv: [48:null]?[*:0]const u8 = @splat(null); + var n: usize = 0; + var words_buf: [1024]u8 = undefined; + var words = std.mem.tokenizeScalar(u8, std.mem.span(std.c.getenv("PARDES_SSH") orelse "ssh"), ' '); + var at: usize = 0; + while (words.next()) |w| { + if (n >= 40 or at + w.len + 1 > words_buf.len) Say.no(io, "PARDES_SSH has too many words", .{}); + @memcpy(words_buf[at..][0..w.len], w); + words_buf[at + w.len] = 0; + argv[n] = @ptrCast(&words_buf[at]); + n += 1; + at += w.len + 1; + } + // A terminal asked for and a command that waits, not -N: only an + // interactive connection gets TCP_NODELAY from ssh and sshd, and with + // Nagle each key in a burst waited for the last one's ACK, a round trip. + for ([_][*:0]const u8{ "-tt", "-q", "-o", "ExitOnForwardFailure=yes", "-L", forward.ptr, host.ptr, "exec sleep 2147483647" }) |a| { + argv[n] = a; + n += 1; + } + _ = std.c.unlink(here); + const pid = std.c.fork(); + if (pid < 0) Say.no(io, "no fork for ssh", .{}); + if (pid == 0) { + const null_fd = std.c.open("/dev/null", .{ .ACCMODE = .RDONLY }, @as(std.c.mode_t, 0)); + if (null_fd >= 0) _ = std.c.dup2(null_fd, 0); + _ = execvp(argv[0].?, &argv); + std.c._exit(127); + } + const Ssh = struct { + var child: std.c.pid_t = 0; + var path: [:0]const u8 = ""; + fn end() void { + _ = std.c.kill(child, .TERM); + _ = std.c.waitpid(child, null, 0); + _ = std.c.unlink(path); + } + }; + Ssh.child = pid; + Ssh.path = here; + var waited: u32 = 0; + while (std.c.access(here, std.c.F_OK) != 0) : (waited += 1) { + var status: c_int = 0; + if (std.c.waitpid(pid, &status, std.c.W.NOHANG) == pid) Say.no(io, "ssh to {s} ended before its forward was up", .{where}); + if (waited > 3000) { + Ssh.end(); + Say.no(io, "ssh to {s} set up no forward in 30 s", .{where}); + } + const ts: std.c.timespec = .{ .sec = 0, .nsec = 10 * std.time.ns_per_ms }; + _ = std.c.nanosleep(&ts, null); + } + @import("tty/tty.zig").remote(init, opts, here, name) catch |err| { + Ssh.end(); + Say.no(io, "{t}", .{err}); + }; + Ssh.end(); + std.process.exit(0); +} + /// `pardes --attach[=name]` in a terminal: this terminal handed (through /// the daemon) to the detached session of that name, or the only one, and /// this process its stub until the session leaves it. `pardes-gui -- cgit v1.3