summaryrefslogtreecommitdiff
path: root/src/host_io.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-05 00:57:44 -0300
committerGabriel Schneider <[email protected]>2026-11-05 01:27:20 -0300
commit0de5fd5523faf4028b5c5e334367d28b4079aec3 (patch)
tree5e059853da6a4a898291a429892cc66dd7ca31d9 /src/host_io.zig
parente50c1b3fc928bf860bc6e91b0a2fea469f7e41e7 (diff)
downloadpardes-0de5fd5523faf4028b5c5e334367d28b4079aec3.tar.gz
pardes-0de5fd5523faf4028b5c5e334367d28b4079aec3.zip
host_io: the shells a dead session left are one Adoptions table (Adopt's ask, adoptShell, a spawn's take, and the Adoption a terminal pane takes), where the tty and the window each kept the same three fields and the same adopt, adoptShell and spawn lookup
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/host_io.zig')
-rw-r--r--src/host_io.zig56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/host_io.zig b/src/host_io.zig
index 3a9a73fd..6ee38b6b 100644
--- a/src/host_io.zig
+++ b/src/host_io.zig
@@ -8,6 +8,7 @@ const ninep_io = @import("9p_io.zig");
const filesystem = @import("fs.zig");
const file_watch = @import("file_watch.zig");
const replica = @import("replica.zig");
+const daemon = @import("daemon.zig");
pub const Host = struct {
ctx: ?*anyopaque = null,
@@ -120,6 +121,61 @@ pub fn restoreCore(gpa: std.mem.Allocator, core: *pardes.Pardes, path: []const u
return nc;
}
+/// A shell of a dead session the daemon handed this one (Adopt), and the
+/// terminal pane made for it once there is one.
+pub const Adoption = struct { pid: libc.pid_t, fd: c_int, pane: ?u8 };
+
+/// The shells of dead sessions handed this one (Adopt), each waiting for
+/// the spawn of the terminal pane made for it (`take`).
+pub const Adoptions = struct {
+ list: [pardes.MAX_PANES]Adoption = undefined,
+ len: usize = 0,
+ /// Taken in since the last Adopt.
+ taken: usize = 0,
+
+ /// Adopt: the daemon is asked for the shells of sessions that died
+ /// holding them; each comes back to `adoptShell`.
+ pub fn ask(a: *Adoptions, core: *pardes.Pardes, pane: u8) void {
+ if (!daemon.kept()) return core.reportFailure(pane, "Adopt: no pardes daemon is keeping shells for this session");
+ a.taken = 0;
+ daemon.adopt();
+ }
+
+ /// One shell of a dead session (`pid` 0: the last of them was told):
+ /// a terminal pane made for it, as the Term word makes one, whose spawn
+ /// takes this pty instead of forking a shell (`take`).
+ pub fn adoptShell(a: *Adoptions, core: *pardes.Pardes, pid: libc.pid_t, fd: c_int) void {
+ if (pid == 0) {
+ var buf: [96]u8 = undefined;
+ return core.setMessage(core.active, if (a.taken == 0) "Adopt: no shell of a session that died is waiting" else std.fmt.bufPrint(&buf, "Adopt: {d} shell{s} taken in; what they showed is gone", .{ a.taken, if (a.taken == 1) "" else "s" }) catch "Adopt: shells taken in");
+ }
+ if (a.len < a.list.len) {
+ a.list[a.len] = .{ .pid = pid, .fd = fd, .pane = null };
+ a.len += 1;
+ if (core.adoptShell()) |id| {
+ a.list[a.len - 1].pane = id;
+ a.taken += 1;
+ return;
+ }
+ a.len -= 1;
+ }
+ // No room for it: its master goes, and the shell with this session.
+ _ = libc.close(fd);
+ var buf: [96]u8 = undefined;
+ core.setMessage(core.active, std.fmt.bufPrint(&buf, "Adopt: no room for shell {d}; it ends with this session", .{pid}) catch "Adopt: no room for a shell");
+ }
+
+ /// The adopted shell pane `pane`'s spawn takes, off the list.
+ pub fn take(a: *Adoptions, pane: u8) ?Adoption {
+ for (a.list[0..a.len], 0..) |it, i| if (it.pane == pane) {
+ std.mem.copyForwards(Adoption, a.list[i .. a.len - 1], a.list[i + 1 .. a.len]);
+ a.len -= 1;
+ return it;
+ };
+ return null;
+ }
+};
+
pub const Fallback = struct {
pub const dump_path = "pardes.dump";