summaryrefslogtreecommitdiff
path: root/src/host_io.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/host_io.zig')
-rw-r--r--src/host_io.zig100
1 files changed, 90 insertions, 10 deletions
diff --git a/src/host_io.zig b/src/host_io.zig
index af284e84..b1ed8b4b 100644
--- a/src/host_io.zig
+++ b/src/host_io.zig
@@ -24,7 +24,17 @@ 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,
+ /// Upgrade: this process exec'd as the pardes installed since, at
+ /// the step's end (upgrade.zig); `pane` hears why if it cannot be.
+ upgrade: ?*const fn (ctx: ?*anyopaque, pane: u8) void = null,
+ /// Whether a newer pardes is installed at the path this process
+ /// started from, to be said once (upgrade.zig): asked at a focus-in.
+ newer_build: ?*const fn (ctx: ?*anyopaque) bool = 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,
@@ -66,7 +76,7 @@ pub fn testClock() ?u64 {
}
pub const Fallback = struct {
- pub const dump_path = "pardes.dump.zon";
+ pub const dump_path = "pardes.dump";
gpa: std.mem.Allocator,
files: std.StringHashMapUnmanaged([]u8) = .empty,
@@ -143,6 +153,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 +170,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 => {},
}
}
};
@@ -962,8 +980,10 @@ pub const Shell = struct {
while (std.c.readdir(dir)) |ent| {
const name = std.mem.sliceTo(@as([*:0]const u8, @ptrCast(&ent.name)), 0);
const pid = rcPid(name) orelse continue;
- if (pid == std.c.getpid()) continue;
- if (std.posix.errno(std.c.kill(pid, @fromBackingInt(@intCast(0)))) != .SRCH) continue;
+ // This pid's are this process's, but in an upgraded one: those
+ // were its old image's (upgrade.zig), and its own come after.
+ if (pid == std.c.getpid() and @import("upgrade.zig").resumed == null) continue;
+ if (pid != std.c.getpid() and std.posix.errno(std.c.kill(pid, @fromBackingInt(@intCast(0)))) != .SRCH) continue;
if (name.len >= 64 or n == dead.len) continue;
@memcpy(dead[n][0..name.len], name);
dead_len[n] = name.len;
@@ -2097,6 +2117,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 +2231,30 @@ 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 == .freebsd) {
+ const n = kernProc(pid, 12, buf) orelse return null; // KERN_PROC_PATHNAME
+ return if (n > 1) buf[0 .. n - 1] else null;
+ }
+ 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 => {
@@ -2233,10 +2280,28 @@ pub fn shellCwd(pid: libc.pid_t, buf: []u8) ?[]const u8 {
@memcpy(buf[0..path.len], path);
return buf[0..path.len];
},
+ .freebsd => {
+ // A struct kinfo_file (sys/user.h), its last 1024 bytes the path.
+ var kf: [1392]u8 align(8) = undefined;
+ const n = kernProc(pid, 42, &kf) orelse return null; // KERN_PROC_CWD
+ if (n <= 368) return null;
+ const path = std.mem.sliceTo(kf[368..n], 0);
+ if (path.len == 0 or path.len >= buf.len) return null;
+ @memcpy(buf[0..path.len], path);
+ return buf[0..path.len];
+ },
else => return null,
}
}
+/// FreeBSD's kern.proc.<what>.<pid> into `buf`: the bytes it wrote.
+fn kernProc(pid: libc.pid_t, what: c_int, buf: []u8) ?usize {
+ const mib = [_]c_int{ 1, 14, what, pid }; // CTL_KERN, KERN_PROC
+ var len: usize = buf.len;
+ if (libc.sysctl(&mib, mib.len, buf.ptr, &len, null, 0) != 0) return null;
+ return len;
+}
+
/// Where a terminal is. The process forked for it, while it still runs the
/// shell pardes started (`shell_exe`), as ever: a job it runs, in another
/// directory (`(cd sub && make)`, a nested pardes), never names the pane.
@@ -2443,6 +2508,13 @@ pub fn ttyTaken(shell_pid: libc.pid_t, master_fd: c_int) bool {
if (fg == shell_pid) return false;
return darwin.taken(shell_pid, fg);
},
+ // ponytail: any foreground job but the shell's own group, so a
+ // nested interactive shell counts as taken; walk the group
+ // (KERN_PROC_PGRP) as darwin does if that matters.
+ .freebsd => {
+ const fg = tcgetpgrp(master_fd);
+ return fg > 0 and fg != shell_pid;
+ },
else => return false,
}
}
@@ -2551,10 +2623,16 @@ pub fn retireShell(pid: libc.pid_t) void {
// it -- until the host, told through `takeExited`, reaps it in its own loop
// with `exitStatus` and tells the core.
-const waitid_p_pid: c_int = 1;
-const waitid_wexited: c_int = 4;
-const waitid_wnowait: c_int = if (builtin.os.tag == .linux) 0x01000000 else 0x20;
-extern "c" fn waitid(idtype: c_int, id: c_uint, infop: *anyopaque, options: c_int) c_int;
+const freebsd = builtin.os.tag == .freebsd;
+const waitid_p_pid: c_int = if (freebsd) 0 else 1;
+const waitid_wexited: c_int = if (freebsd) 16 else 4;
+const waitid_wnowait: c_int = switch (builtin.os.tag) {
+ .linux => 0x01000000,
+ .freebsd => 8,
+ else => 0x20,
+};
+// FreeBSD's id_t is 64 bits.
+extern "c" fn waitid(idtype: c_int, id: if (freebsd) i64 else c_uint, infop: *anyopaque, options: c_int) c_int;
var exited: [pardes.MAX_PANES * 2]std.atomic.Value(libc.pid_t) = @splat(.init(0));
var exit_lock: libc.pthread_mutex_t = .{};
@@ -2798,11 +2876,13 @@ pub fn killJob(shell_pid: libc.pid_t, master_fd: c_int) bool {
}
/// The command name of the tty's foreground job, when that is not the
-/// shell: what `pty/run` says is running. Linux only (/proc/<pgrp>/comm).
+/// shell: what `pty/run` says is running. /proc/<pgrp>/comm on Linux, the
+/// program's name on FreeBSD.
pub fn foregroundName(shell_pid: libc.pid_t, master_fd: c_int, buf: []u8) ?[]const u8 {
- if (comptime builtin.os.tag != .linux) return null;
+ if (comptime builtin.os.tag != .linux and builtin.os.tag != .freebsd) return null;
const fg = tcgetpgrp(master_fd);
if (fg <= 0 or fg == shell_pid) return null;
+ if (comptime builtin.os.tag == .freebsd) return std.fs.path.basename(processExe(fg, buf) orelse return null);
var name: [64:0]u8 = undefined;
const path = std.fmt.bufPrintSentinel(&name, "/proc/{d}/comm", .{@as(u32, @intCast(fg))}, 0) catch return null;
const comm = std.mem.trimEnd(u8, readProc(path, buf) orelse return null, "\n");