diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-29 12:32:23 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-29 13:11:16 -0300 |
| commit | 6b89fed454fd56a3af4b576646538b45d3315146 (patch) | |
| tree | f4be48c98368bc8c85c461af97528743afc98f59 /src/host_io.zig | |
| parent | 34551a64366b3257ac577c5b68288f955c9eea80 (diff) | |
| download | pardes-6b89fed454fd56a3af4b576646538b45d3315146.tar.gz pardes-6b89fed454fd56a3af4b576646538b45d3315146.zip | |
FreeBSD: the tty pardes builds for it and runs there, terminals, 9P and the daemon included
Cross-compiled with -Dtarget=x86_64-freebsd.15.0 (15 ships libutil.so.10;
Zig's default 14.0 links .so.9) and run on the FreeBSD 15.1 box:
- 9p_io serves there: fstatat, not Linux's statx. madvise is Linux's.
- WINCH reaches the sigwait thread through a handler that does nothing:
FreeBSD drops a signal ignored by default as it is sent.
- A dead terminal answers a write with ENXIO, which std does not expect: a
Debug build traced it to stderr, that same terminal, and recursed until
the stack ran out (a session whose ssh dropped hung). No tracing there.
- waitid's FreeBSD numbers and 64-bit id: a command pane's exit is seen.
- No /proc: a shell's directory and program by sysctl, ttyTaken by the
foreground group, the foreground job's name by its program.
- File watching by FreeBSD 15's inotify.
- ghostty's kitty loader: off Linux a stat comes before a t=f open (a FIFO
hung it); FreeBSD's shm_open refuses O_NONBLOCK.
- vaxis's test terminal has no read off Linux: no input thread for it.
- test-install: the test-build programs installed into <prefix>/test, to
run where the build cannot.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/host_io.zig')
| -rw-r--r-- | src/host_io.zig | 49 |
1 files changed, 43 insertions, 6 deletions
diff --git a/src/host_io.zig b/src/host_io.zig index 96c790d7..99ada59b 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -2235,6 +2235,10 @@ pub fn marksPrompts(pid: libc.pid_t) bool { /// 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; @@ -2268,10 +2272,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. @@ -2478,6 +2500,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, } } @@ -2586,10 +2615,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 = .{}; @@ -2833,11 +2868,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"); |
