summaryrefslogtreecommitdiff
path: root/src/host_io.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-29 10:48:56 -0300
committerGabriel Schneider <[email protected]>2026-10-29 12:01:27 -0300
commit4ffe5c93b3087f245f5b57a822ab0ecf41cc085c (patch)
treeede4d5de12fe169838f1f5993a9896bd32f45715 /src/host_io.zig
parentcb2a6cd79e443d671f5255d262ea79b7e26552a5 (diff)
downloadpardes-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/host_io.zig')
-rw-r--r--src/host_io.zig37
1 files changed, 36 insertions, 1 deletions
diff --git a/src/host_io.zig b/src/host_io.zig
index af284e84..96c790d7 100644
--- a/src/host_io.zig
+++ b/src/host_io.zig
@@ -24,7 +24,11 @@ pub const Host = struct {
/// client attached draws none (a /screen read draws its own), and
/// the frame owed is drawn when one attaches. Absent: always.
watched: ?*const fn (ctx: ?*anyopaque) bool = null,
+ /// Detach: the terminal or window let go, the session left running
+ /// for the daemon to attach again (daemon.zig).
detach: ?*const fn (ctx: ?*anyopaque) void = null,
+ /// Adopt: the shells a dead session left, asked of the daemon.
+ adopt: ?*const fn (ctx: ?*anyopaque, pane: u8) void = null,
spawn: ?*const fn (ctx: ?*anyopaque, pane: u8, cwd: []const u8) void = null,
pty_write: ?*const fn (ctx: ?*anyopaque, pane: u8, bytes: []const u8) void = null,
pty_resize: ?*const fn (ctx: ?*anyopaque, pane: u8, cols: u16, rows: u16) void = null,
@@ -143,6 +147,11 @@ pub const Inbox = struct {
handed: struct { seq: u32, ok: bool, words: []u8 },
files_changed,
fs_ready,
+ /// The daemon asks for the window again (`pardes-gui --attach`).
+ window,
+ /// A shell a dead session left, its pty's master (Adopt); pid 0 is
+ /// the last of them told.
+ adopted: struct { pid: libc.pid_t, fd: c_int },
pub fn deinit(m: Msg, gpa: std.mem.Allocator, lsp_allocator: std.mem.Allocator) void {
switch (m) {
@@ -155,7 +164,10 @@ pub const Inbox = struct {
},
.search => |b| b.deinit(gpa),
.handed => |h| gpa.free(h.words),
- .eof, .files_changed, .fs_ready => {},
+ .adopted => |a| if (a.fd >= 0) {
+ _ = libc.close(a.fd);
+ },
+ .eof, .files_changed, .fs_ready, .window => {},
}
}
};
@@ -2097,6 +2109,9 @@ pub fn forkShell(
};
}
ninep_io.setCloexec(master);
+ // The daemon keeps a dup: this session killed outright never hangs the
+ // shell up (daemon.zig).
+ @import("daemon.zig").pty(pid, master);
if (core) |c| c.acknowledgeShell(pane, std.mem.span(spawn.path), one_line == null and spawn.argv[1] != null);
return .{ .file = .{ .handle = master, .flags = .{ .nonblocking = false } }, .pid = pid };
}
@@ -2208,6 +2223,26 @@ const proc_vnodepathinfo = extern struct {
const PROC_PIDVNODEPATHINFO: c_int = 9;
extern "c" fn proc_pidinfo(pid: c_int, flavor: c_int, arg: u64, buffer: *anyopaque, buffersize: c_int) c_int;
+/// Whether an adopted shell marks its prompts: one pardes started with its
+/// prompt files (bash, zsh, fish), which a pane's runs wait on.
+pub fn marksPrompts(pid: libc.pid_t) bool {
+ var buf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined;
+ const exe = std.fs.path.basename(processExe(pid, &buf) orelse return false);
+ for ([_][]const u8{ "bash", "zsh", "fish" }) |shell| if (std.mem.eql(u8, exe, shell)) return true;
+ return false;
+}
+
+/// The program `pid` runs, where the system says (an adopted shell's, for
+/// its pane's Tty word); null elsewhere.
+pub fn processExe(pid: libc.pid_t, buf: []u8) ?[]const u8 {
+ if (comptime builtin.os.tag != .linux) return null;
+ var pbuf: [64]u8 = undefined;
+ const path = std.fmt.bufPrintSentinel(&pbuf, "/proc/{d}/exe", .{pid}, 0) catch return null;
+ const n = libc.readlink(path, buf.ptr, buf.len);
+ if (n <= 0 or n >= buf.len) return null;
+ return buf[0..@intCast(n)];
+}
+
pub fn shellCwd(pid: libc.pid_t, buf: []u8) ?[]const u8 {
switch (builtin.os.tag) {
.linux => {