diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-20 04:50:25 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-20 05:42:56 -0300 |
| commit | 32ac0d9d5f7de60f2045edbd1c15631306bd25d4 (patch) | |
| tree | be11dddf1bcafa3cdee710ff5553e42e4a7b4afa /runtime/main.zig | |
| parent | 821509e5cbe47e73ce4f4fcdc5d092f3c5e1c986 (diff) | |
| download | pardes-32ac0d9d5f7de60f2045edbd1c15631306bd25d4.tar.gz pardes-32ac0d9d5f7de60f2045edbd1c15631306bd25d4.zip | |
pair: PARDES_PAIR (a working name, one variable) makes `pardes FILE` in a terminal a session and its first client over a socket pair (the last step's design 1.1, Z1): the daemon dialled first as for a stub, the terminal's size read, a fork; the parent is the client (the shell's job, the terminal its own, so WINCH/HUP/TERM reach what draws), its first join on its end of the pair (runtime/main.zig `pair`, tui.runClient unchanged); the child is the session (setsid, 0/1/2 on /dev/null, detached, registered on the dialled connection), its end taken as its first client before its first effects (the previous change); with no daemon too, where a pair's session ends with its last link (runtime/session.zig); without the variable nothing changes (tui core-test 856/1009, shell-test 1049/1052, term 17/17, pane 183/184, kitty 45/45, behaviour 275/275 and --replay, process-test 0 of 144 but pagercolor, which passed alone, both soaks diverged 0 unequal 0 corpus 22 of 22, the bytes soak's one unlisted stop now:1 the base's; daemon.py plain, far, games, upgrade both ways pass); test/daemon.py `pair` (and --pair): the job, the session its child in a session of its own, listed with its client, drawn, typed, digests agreed, Exit ends both, with no daemon a client killed outright ends its session, nothing left; process-test with the variable: 17 of 144 fail as pairs (the harness dials the launched pid's socket, its goldens are an own frontend's frames, a Restore lets the client go with no daemon to rejoin through, its signal and no-terminal cases are the own path's), and one daemon-on case leaves its pair session running (a killed client is a loss: Z4's harness), test/process.zig passes PARDES_PAIR through for that run
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'runtime/main.zig')
| -rw-r--r-- | runtime/main.zig | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/runtime/main.zig b/runtime/main.zig index 24ed1456..0feb0d53 100644 --- a/runtime/main.zig +++ b/runtime/main.zig @@ -1573,6 +1573,11 @@ fn nativeMain(std_init: std.process.Init) !void { }; detached = if (pardes.platform == .gui) !rec.window else rec.nttys == 0; daemon.resumeLink(rec.link, rec.stub, kind, opts.ninep_name, detached); + } else if (pardes.platform == .tui and detach == null and std.c.getenv("PARDES_PAIR") != null) { + // The last step's way, behind its variable until it is the default: + // this terminal a client of a session forked off for it (`pair`). + pair(init, &opts); + term = .detached; } else if (daemon.wanted()) { if (detach != null or pardes.platform == .gui) { _ = daemon.register(null, kind, opts.ninep_name, detach != null); @@ -1709,6 +1714,54 @@ fn commOf(io: std.Io, pid: []const u8, buf: *[64]u8) []const u8 { return std.mem.trimEnd(u8, std.Io.Dir.cwd().readFile(io, path, buf) catch return "", "\n"); } +/// PARDES_PAIR: `pardes` in a terminal as a session and its first client +/// over a socket pair (docs/next-run.md; the last step's design, 1.1). The +/// daemon dialled first, as for a stub; then a fork: the parent the CLIENT +/// (the shell's job, the terminal its own; signals reach what draws), its +/// first join on its end of the pair; the child the SESSION (setsid, 0, 1 +/// and 2 on /dev/null, detached), its end of the pair its first client, +/// taken before its first effects (runtime/session.zig). Returns in the +/// session; the client exits. No fork to be had: returns, and this process +/// is the session, with no terminal: said by the caller's run. +fn pair(init: std.process.Init, opts: *pardes.Options) void { + const conn: ?c_int = if (daemon.wanted()) daemon.connect(true) else null; + var fds: [2]c_int = undefined; + if (std.c.socketpair(std.c.AF.UNIX, std.c.SOCK.STREAM | std.c.SOCK.CLOEXEC, 0, &fds) != 0) + die(init.io, "no socket pair for a session and its terminal", .{}); + // The session laid out at the terminal's size: no reflow at the join. + var ws: std.posix.winsize = undefined; + if (std.c.ioctl(0, std.posix.T.IOCGWINSZ, &ws) == 0 and ws.col > 0 and ws.row > 0) { + opts.cols = ws.col; + opts.rows = ws.row; + } + const pid = std.c.fork(); + if (pid < 0) die(init.io, "no fork for a session and its terminal", .{}); + if (pid == 0) { + _ = std.c.close(fds[1]); + _ = std.c.setsid(); + const null_fd = std.c.open("/dev/null", .{ .ACCMODE = .RDWR, .CLOEXEC = true }, @as(std.c.mode_t, 0)); + if (null_fd >= 0) for (0..3) |i| { + _ = std.c.dup2(null_fd, @intCast(i)); + }; + if (null_fd > 2) _ = std.c.close(null_fd); + opts.pair = fds[0]; + // The daemon is the client's to start; the hello is this one's. + daemon.startPending(false); + if (conn) |c| _ = daemon.register(c, .tty, opts.ninep_name, true); + return; + } + _ = std.c.close(fds[0]); + if (conn) |c| _ = std.c.close(c); + daemon.startPending(true); + var name_buf: [16]u8 = undefined; + const name = if (opts.ninep_name.len != 0) opts.ninep_name else std.fmt.bufPrint(&name_buf, "{d}", .{pid}) catch unreachable; // a pid fits 16 + var sock_buf: [ninep_io.sun_path_len:0]u8 = undefined; + var o = opts.*; + o.pair = fds[1]; + @import("dev").tui.runClient(init, o, daemon.replicaSocket(&sock_buf) orelse "", name, false) catch |err| die(init.io, "{t}", .{err}); + std.process.exit(0); +} + /// The replica client of this build's frontend: a terminal's (tui.zig) or a /// window's (gui.zig). fn clientOf(init: std.process.Init, opts: pardes.Options, path: []const u8, name: []const u8, far: bool) !void { |
