diff options
Diffstat (limited to 'src/host_io.zig')
| -rw-r--r-- | src/host_io.zig | 37 |
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 => { |
