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.zig49
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");