summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/9p_io.zig4
-rw-r--r--src/file_watch.zig17
-rw-r--r--src/host_io.zig49
-rw-r--r--src/main.zig10
-rw-r--r--src/memory.zig1
-rw-r--r--src/tty/tty.zig10
6 files changed, 74 insertions, 17 deletions
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);