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 | |
| 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]>
| -rw-r--r-- | build.zig | 28 | ||||
| -rw-r--r-- | src/9p_io.zig | 4 | ||||
| -rw-r--r-- | src/file_watch.zig | 17 | ||||
| -rw-r--r-- | src/host_io.zig | 49 | ||||
| -rw-r--r-- | src/main.zig | 10 | ||||
| -rw-r--r-- | src/memory.zig | 1 | ||||
| -rw-r--r-- | src/tty/tty.zig | 10 |
7 files changed, 97 insertions, 22 deletions
@@ -1929,6 +1929,19 @@ pub fn build(b: *std.Build) void { } } } + + // The test-build programs installed rather than run, for a machine the + // build cannot run them on (`-Dtarget=x86_64-freebsd.15.0`): each into + // <prefix>/test, named for its root file. + const test_install = b.step("test-install", "install the test-build programs into <prefix>/test, to run elsewhere"); + for (test_build.dependencies.items) |dep| { + const t = dep.cast(std.Build.Step.Compile) orelse continue; + const name = switch (t.root_module.root_source_file orelse continue) { + .src_path => |p| std.fs.path.stem(p.sub_path), + else => t.name, + }; + test_install.dependOn(&b.addInstallArtifact(t, .{ .dest_dir = .{ .override = .{ .custom = "test" } }, .dest_sub_path = name }).step); + } } fn testMatch(b: *std.Build, profiler: *std.Build.Step.Compile, filters: []const []const u8, step: *std.Build.Step) ?*std.Build.Step.Run { @@ -1964,7 +1977,8 @@ fn installHome(b: *std.Build, artifact: *std.Build.Step.Compile, home_bin: ?[]co /// file: ghostty opens first and checks after, and the open of a FIFO /// waits for a writer (pardes hung) while a device's open may do /// something. On Linux an O_PATH probe looks, which opens nothing, and -/// the same inode is reopened through /proc/self/fd. +/// the same inode is reopened through /proc/self/fd; elsewhere a stat by +/// path looks first. /// - shared memory is opened non-blocking and must be a regular file, so /// a FIFO in /dev/shm (or a symlink there to one) neither hangs nor maps. /// - an image may be no larger than a screen's storage @@ -1990,14 +2004,15 @@ fn patchGhosttyKitty(b: *std.Build, io: std.Io, dep: *std.Build.Dependency, vt: .{ " const fd = std.c.shm_open(pathz, @as(c_int, @bitCast(std.c.O{ .ACCMODE = .RDONLY })), @as(u16, 0));", \\ // pardes (build.zig): a FIFO there must not hang the open, and - \\ // on Linux nothing but a regular file is opened at all. + \\ // on Linux nothing but a regular file is opened at all. FreeBSD's + \\ // names are no files, and its shm_open refuses O_NONBLOCK. \\ if (comptime builtin.target.os.tag == .linux) if (path.len > 0 and path[0] == '/') { \\ var dev: [std.Io.Dir.max_path_bytes]u8 = undefined; \\ const full = std.fmt.bufPrint(&dev, "/dev/shm{s}", .{path}) catch return error.InvalidData; \\ const shm_stat = std.Io.Dir.cwd().statFile(io, full, .{}) catch return error.InvalidData; \\ if (shm_stat.kind != .file) return error.InvalidData; \\ }; - \\ const fd = std.c.shm_open(pathz, @as(c_int, @bitCast(std.c.O{ .ACCMODE = .RDONLY, .NONBLOCK = true })), @as(u16, 0)); + \\ const fd = std.c.shm_open(pathz, @as(c_int, @bitCast(std.c.O{ .ACCMODE = .RDONLY, .NONBLOCK = builtin.target.os.tag != .freebsd })), @as(u16, 0)); }, .{ " if (stat.size <= 0) return error.InvalidData;", @@ -2011,9 +2026,12 @@ fn patchGhosttyKitty(b: *std.Build, io: std.Io, dep: *std.Build.Dependency, vt: const helper = \\ \\/// pardes (build.zig): `path`, opened for reading only once it is known - \\/// to be a regular file. Elsewhere than Linux, ghostty's open as it was. + \\/// to be a regular file. Elsewhere than Linux, a stat by path first. \\fn pardesOpenRegular(io: std.Io, path: []const u8) !std.Io.File { - \\ if (comptime builtin.target.os.tag != .linux) return std.Io.Dir.cwd().openFile(io, path, .{}); + \\ if (comptime builtin.target.os.tag != .linux) { + \\ if ((try std.Io.Dir.cwd().statFile(io, path, .{})).kind != .file) return error.NotRegularFile; + \\ return std.Io.Dir.cwd().openFile(io, path, .{}); + \\ } \\ const probe = try std.Io.Dir.cwd().openFile(io, path, .{ .path_only = true }); \\ defer probe.close(io); \\ const stat = try probe.stat(io); diff --git a/src/9p_io.zig b/src/9p_io.zig index 2a0061fd..174d3f89 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -12,7 +12,7 @@ const quic = if (quic_enabled) @import("9p_quic.zig") else struct {}; const log = std.log.scoped(.ninep); pub const darwin = builtin.os.tag.isDarwin(); -pub const supported = builtin.os.tag == .linux or darwin; +pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd or darwin; pub const sun_path_len = @typeInfo(@FieldType(libc.sockaddr.un, "path")).array.len; pub fn setCloexec(fd: c_int) void { @@ -29,7 +29,7 @@ pub fn socketDir(buf: *[sun_path_len:0]u8) ?[:0]const u8 { pub const FileFacts = struct { mode: u32, uid: libc.uid_t }; pub fn statNoFollow(path: [:0]const u8) ?FileFacts { - if (comptime darwin) { + if (comptime builtin.os.tag != .linux) { var stat: libc.Stat = undefined; if (libc.fstatat(libc.AT.FDCWD, path, &stat, libc.AT.SYMLINK_NOFOLLOW) != 0) return null; return .{ .mode = stat.mode, .uid = stat.uid }; diff --git a/src/file_watch.zig b/src/file_watch.zig index e3fe9963..9b41d146 100644 --- a/src/file_watch.zig +++ b/src/file_watch.zig @@ -5,9 +5,10 @@ //! is their generation. Large PDFs use metadata which changes for in-place and //! rename-over saves, avoiding a second whole-document allocation. //! -//! Two kernels, one transaction: inotify on linux, kqueue on macos. `init`, -//! `wait`, `stop`, `drain`, `markDir` and `unmarkDir` are the whole of that -//! difference, and nothing above them knows which one it is on. +//! Two kernels, one transaction: inotify on linux (and FreeBSD 15's, the +//! same events), kqueue on macos. `init`, `wait`, `stop`, `drain`, `markDir` +//! and `unmarkDir` are the whole of that difference, and nothing above them +//! knows which one it is on. const std = @import("std"); const builtin = @import("builtin"); const libc = std.c; @@ -81,7 +82,7 @@ pub const Table = [shader_slot + pardes.config.Runtime.Post.max]?Watch; /// empty and `reloadPane` returns false for every slot. The core then keeps its /// own record of what was asked and simply never gets a `file_changed`, which /// is what a host with no watcher has always done here. -pub const supported = builtin.os.tag == .linux or builtin.os.tag == .macos; +pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd or builtin.os.tag == .macos; /// The wake filter's `ident` on macos. `ident` is a directory descriptor for /// every other filter on this queue, and no descriptor is `maxInt(usize)`, so @@ -113,6 +114,8 @@ pub fn init(polled: bool) c_int { linux.IN.CLOEXEC | linux.IN.NONBLOCK else linux.IN.CLOEXEC), + // FreeBSD's IN_CLOEXEC and IN_NONBLOCK are its O_ flags. + .freebsd => return libc.inotify_init1(if (polled) 0x00100000 | 0x4 else 0x00100000), .macos => { const kq = libc.kqueue(); if (kq < 0) return -1; @@ -193,7 +196,7 @@ pub fn stop(fd: c_int) void { pub fn drain(fd: c_int) bool { if (fd < 0) return false; switch (builtin.os.tag) { - .linux => { + .linux, .freebsd => { var buf: [4096]u8 = undefined; var seen = false; while (libc.read(fd, &buf, buf.len) > 0) seen = true; @@ -227,7 +230,7 @@ pub fn drain(fd: c_int) bool { /// and inode, because each EVFILT_VNODE filter needs a descriptor of its own. fn markDir(fd: c_int, dir_z: [:0]const u8, watches: *const Table) c_int { switch (builtin.os.tag) { - .linux => { + .linux, .freebsd => { // CLOSE_WRITE coalesces one writer's writes; MOVED_TO and CREATE // cover rename-over and delete-then-recreate saves; DELETE and // MOVED_FROM a file going away, said at once (File.deleted), not @@ -366,7 +369,7 @@ fn unmark(fd: c_int, old: Watch, drop_dir: bool) void { } if (!drop_dir) return; switch (builtin.os.tag) { - .linux => _ = libc.inotify_rm_watch(fd, old.wd), + .linux, .freebsd => _ = libc.inotify_rm_watch(fd, old.wd), .macos => _ = libc.close(old.wd), else => {}, } 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"); diff --git a/src/main.zig b/src/main.zig index 803ff4da..3d26d1fd 100644 --- a/src/main.zig +++ b/src/main.zig @@ -21,7 +21,15 @@ else // the thread starts, so std's 256 KiB made each 9P connection's thread cost // that much resident. A stack-overflow segfault on a thread still printed its // whole symbolized trace from 32 KiB up (16 KiB double-faulted): 64 KiB. -pub const std_options: std.Options = .{ .logFn = logFn, .signal_stack_size = 64 * 1024 }; +// +// FreeBSD answers a write to a terminal that is gone with ENXIO, an errno 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). +pub const std_options: std.Options = .{ + .logFn = logFn, + .signal_stack_size = 64 * 1024, + .unexpected_error_tracing = (std.Options{}).unexpected_error_tracing and builtin.os.tag != .freebsd, +}; fn logFn( comptime level: std.log.Level, diff --git a/src/memory.zig b/src/memory.zig index cb75ee0e..47bd755d 100644 --- a/src/memory.zig +++ b/src/memory.zig @@ -442,6 +442,7 @@ pub fn trim() void { /// 71 MiB that way. Only the region given, never the whole process (a prctl /// would reach every shell and server pardes starts). pub fn sparse(region: []u8) void { + if (comptime builtin.os.tag != .linux) return; advise(region, std.posix.MADV.NOHUGEPAGE); } diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 37f66456..31f62d34 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -122,6 +122,8 @@ fn stopInput(loop: *Loop) void { } fn inputThread(loop: *Loop, cache: *vaxis.GraphemeCache) void { + // vaxis's test terminal reads nothing off Linux: it has no read at all. + if (comptime !@hasDecl(vaxis.Tty, "read")) return; inputReader(loop, loop.tty, cache); } @@ -1185,6 +1187,10 @@ pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void { // Kept by a daemon, a hangup is the terminal's end, not the session's: // winchWatch takes it, and the session carries on detached. if (daemon.link >= 0) posix.sigaddset(&winch_set, posix.SIG.HUP); + // FreeBSD drops a signal whose action is to be ignored (WINCH's + // default) as it is sent, sigwait or not: a handler keeps it. + if (comptime builtin.os.tag == .freebsd) + posix.sigaction(.WINCH, &.{ .handler = .{ .handler = keepSignal }, .mask = posix.sigemptyset(), .flags = 0 }, null); posix.sigprocmask(posix.SIG.BLOCK, &winch_set, null); var tty_buf: [0x10000]u8 = undefined; @@ -2450,7 +2456,7 @@ fn lspStatusSink(ctx: ?*anyopaque, text: []const u8) void { } fn watchFiles(io: std.Io, fd: c_int, loop: *Loop) anyerror!void { - if (comptime builtin.os.tag != .linux) { + if (comptime builtin.os.tag != .linux and builtin.os.tag != .freebsd) { while (file_watch.wait(fd)) loop.postEvent(.files_changed) catch break; return; } @@ -2465,6 +2471,8 @@ fn watchFiles(io: std.Io, fd: c_int, loop: *Loop) anyerror!void { } } +fn keepSignal(_: posix.SIG) callconv(.c) void {} + fn winchWatch(loop: *Loop, vx: *vaxis.Vaxis, tty: *vaxis.Tty) void { var set = posix.sigemptyset(); posix.sigaddset(&set, posix.SIG.WINCH); |
