diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-29 10:48:56 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-29 12:01:27 -0300 |
| commit | 4ffe5c93b3087f245f5b57a822ab0ecf41cc085c (patch) | |
| tree | ede4d5de12fe169838f1f5993a9896bd32f45715 /src/builtins.zig | |
| parent | cb2a6cd79e443d671f5255d262ea79b7e26552a5 (diff) | |
| download | pardes-4ffe5c93b3087f245f5b57a822ab0ecf41cc085c.tar.gz pardes-4ffe5c93b3087f245f5b57a822ab0ecf41cc085c.zip | |
sessions register with the daemon: a terminal's session forked off its stub, Detach and --attach in place for terminals and windows, pty masters kept, Adopt
pardes in a terminal reaches (or starts) the daemon, opens /dev/tty and forks: the child is the session, in a session of its own (setsid), drawing on that terminal (tty.Term.fd, a raw vaxis Tty made from the fd); the parent stays as its stub, the job the shell waits on. Not the daemon spawning it from a passed fd, env and argv: the user's terminals each run in a 9ns user and mount namespace, which a session spawned by a per-user daemon would not share, and a fork costs one small process's page tables where the spawn cost an IPC and an exec. GUI windows and --detach sessions just register.
tty: Detach (and a terminal's end: EOF, EIO, a hangup the stub passes on, taken on the signal thread) puts the terminal back, drops it and the stub's stdio, tells the stub (which ends, exit 0), and leaves the session running with nothing drawn (Host.watched), its messages on the clock and `size` its own. pardes --attach[=name] hands its terminal through the daemon; the session enters it as the first one, sized, queried and drawn whole, and that process is its stub until it leaves. A hangup never ends a kept session; TERM and INT do, telling the daemon so the shells go with it (one async-signal-safe send).
gui: Detach closes the window (its swapchain or renderer) and keeps the device, atlas and pipelines; the loop sleeps on SDL's queue; pardes-gui --attach[=name] opens a window again. Closing a window still quits.
--detach is a session started detached: the tty host with no terminal yet, the GUI with a hidden window closed at once. forkShell sends the daemon a dup of every master and the hosts tell it what they close, so a session killed outright leaves its shells running; the new Adopt word takes them in as terminal panes (the Tty word's pane, its spawn taking the pty, a 1x1 size then the pane's to make it redraw).
The Attach word, which switched a session's own terminal to a detached session through the frames, goes, with its effect and request in the core. PARDES_DAEMON=off (the e2e harness's) runs a session as before. test/daemon.py: 50 detach/attach cycles at a level fd count with no stale socket, kill -9 and Adopt, and nothing left; --gui the same for a window. test/fs.py: a forked session's socket is found by its own pid, and a hangup leaves a kept session running.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/builtins.zig')
| -rw-r--r-- | src/builtins.zig | 49 |
1 files changed, 13 insertions, 36 deletions
diff --git a/src/builtins.zig b/src/builtins.zig index 1f1e674e..3a66a316 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -737,51 +737,28 @@ pub const Restore = struct { } }; -/// Attach this terminal to a detached pardes session by name. -pub const Attach = struct { +/// Detach this pardes from its terminal or window, leaving the session +/// running: `pardes --attach` (or `pardes-gui --attach`) takes it back. +pub const Detach = struct { pub const scope: Scope = .session; - pub const takes_arg = true; pub const enabled = pardes.can_attach; pub fn run(c: Ctx) void { - if (comptime enabled) ask(c) else unreachable; - } - fn ask(c: Ctx) void { - const name = std.mem.trim(u8, c.arg orelse "", " \t\r\n"); - if (name.len > pardes.attach_name_max) - return c.p.reportError(c.id, comptime word(@This()), error.NameTooLong); - // A detached session has no terminal to move: bare, there is no - // "the one session there" but itself; a name is said for what it is. - if (c.p.opts.detached_session) { - if (name.len == 0) return c.p.reportFailure(c.id, "wrong #args in control message \"Attach\": a detached session names the session"); - // A session is its socket, `pardes-detached-<name>.sock` beside - // the 9P ones (detached/server.zig socketPath); not imported, as - // the core links no frontend's code. - const ninep_io = @import("9p_io.zig"); - var dir_buf: [ninep_io.sun_path_len:0]u8 = undefined; - var path_buf: [ninep_io.sun_path_len + 64:0]u8 = undefined; - const exists = if (ninep_io.socketDir(&dir_buf)) |dir| - (if (std.fmt.bufPrintSentinel(&path_buf, "{s}/pardes-detached-{s}.sock", .{ dir, name }, 0)) |path| ninep_io.statNoFollow(path) != null else |_| false) - else - false; - var said: [pardes.attach_name_max + 96]u8 = undefined; - return c.p.reportFailure(c.id, if (exists) - std.fmt.bufPrint(&said, "Attach: {s}: a detached session has no terminal to attach; Attach from a frontend", .{name}) catch "Attach: a detached session has no terminal to attach" - else - std.fmt.bufPrint(&said, "Attach: {s}: no such session", .{name}) catch "Attach: no such session"); - } - c.p.emit(.{ .attach = .{ .pane = @intCast(c.id), .name = .from(name) } }); + if (comptime enabled) + c.p.emit(.{ .detach = .{ .pane = @intCast(c.id) } }) + else + unreachable; } }; -/// Detach this pardes from its terminal, leaving the session running. -pub const Detach = struct { +/// Take in the shells of a session that died holding them, each as a +/// terminal pane here: the pardes daemon kept their terminals open. +pub const Adopt = struct { pub const scope: Scope = .session; pub const enabled = pardes.can_attach; pub fn run(c: Ctx) void { - if (comptime enabled) - c.p.emit(.{ .detach = .{ .pane = @intCast(c.id) } }) - else - unreachable; + if (comptime !enabled) unreachable; + const adopt = c.p.host.vtable.adopt orelse return c.p.reportFailure(c.id, "Adopt: no pardes daemon is keeping shells for this session"); + adopt(c.p.host.ctx, @intCast(c.id)); } }; |
