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.zig77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/host_io.zig b/src/host_io.zig
index bef88172..c593551b 100644
--- a/src/host_io.zig
+++ b/src/host_io.zig
@@ -1795,6 +1795,83 @@ pub fn shellCwd(pid: libc.pid_t, buf: []u8) ?[]const u8 {
}
}
+/// 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.
+/// One that exec'd into something else is a wrapper whose own directory
+/// never moves (fish's `exec 9ns --mntgen -- fish`): the shell is the
+/// foreground group when that is the same shell, and while a job holds the
+/// foreground there is no answer, the last one standing.
+pub fn ttyCwd(shell_pid: libc.pid_t, master_fd: c_int, shell_exe: []const u8, buf: []u8) ?[]const u8 {
+ var exe_buf: [std.fs.max_path_bytes]u8 = undefined;
+ var shell_buf: [std.fs.max_path_bytes]u8 = undefined;
+ const shell = realShell(shell_exe, &shell_buf) orelse return shellCwd(shell_pid, buf);
+ const exe = procExe(shell_pid, &exe_buf) orelse return shellCwd(shell_pid, buf);
+ if (std.mem.eql(u8, exe, shell)) return shellCwd(shell_pid, buf);
+ const fg = tcgetpgrp(master_fd);
+ if (fg <= 0 or fg == shell_pid) return null;
+ const fg_exe = procExe(fg, &exe_buf) orelse return null;
+ return if (std.mem.eql(u8, fg_exe, shell)) shellCwd(fg, buf) else null;
+}
+
+/// `shell_exe` as /proc/<pid>/exe would read it: its links followed.
+fn realShell(shell_exe: []const u8, buf: *[std.fs.max_path_bytes]u8) ?[]const u8 {
+ var z: [std.fs.max_path_bytes:0]u8 = undefined;
+ const path = std.fmt.bufPrintSentinel(&z, "{s}", .{shell_exe}, 0) catch return null;
+ const r = libc.realpath(path, buf) orelse return null;
+ return std.mem.span(@as([*:0]const u8, @ptrCast(r)));
+}
+
+test "a terminal is where its shell is: under a wrapper the foreground shell, never a job's directory" {
+ if (comptime builtin.os.tag != .linux) return error.SkipZigTest;
+ if (!haveFile("/bin/sh") or !haveFile("/usr/bin/timeout")) return error.SkipZigTest;
+ const io = std.testing.io;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.createDirPath(io, "start/a/b");
+ var path_buf: [4096]u8 = undefined;
+ const start = path_buf[0..try tmp.dir.realPathFile(io, "start", &path_buf)];
+ var inner_buf: [4096]u8 = undefined;
+ const inner = inner_buf[0..try tmp.dir.realPathFile(io, "start/a/b", &inner_buf)];
+ const rcs: Shell.PromptFiles = .{};
+ var result: [4096]u8 = undefined;
+ // A wrapper exec'd over the shell (timeout standing in for 9ns) runs
+ // another, two directories down, in the foreground.
+ {
+ const child = try forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80, null);
+ defer {
+ _ = libc.kill(child.pid, libc.SIG.KILL);
+ _ = libc.waitpid(child.pid, null, 0);
+ _ = libc.close(child.file.handle);
+ }
+ var sh: TestShell = .{ .master = child.file.handle, .pid = child.pid };
+ try std.testing.expect(writeFd(child.file.handle, "exec timeout --foreground 30 /bin/sh -c 'cd a/b && printf \"\\160ardes-in-\\n\" && exec /bin/sh -i'\n"));
+ try std.testing.expect(sh.waitText("pardes-in-", 5_000));
+ // printf runs before the inner shell takes the foreground.
+ var tries: usize = 0;
+ while (ttyCwd(child.pid, child.file.handle, "/bin/sh", &result) == null and tries < 50) : (tries += 1) {
+ const ts: libc.timespec = .{ .sec = 0, .nsec = 20 * std.time.ns_per_ms };
+ _ = libc.nanosleep(&ts, null);
+ }
+ try std.testing.expectEqualStrings(start, shellCwd(child.pid, &result) orelse return error.MissingCwd);
+ try std.testing.expectEqualStrings(inner, ttyCwd(child.pid, child.file.handle, "/bin/sh", &result) orelse return error.MissingCwd);
+ }
+ // A job of the shell's own, gone into a/b, leaves the pane where the
+ // shell is.
+ {
+ const child = try forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80, null);
+ defer {
+ _ = libc.kill(child.pid, libc.SIG.KILL);
+ _ = libc.waitpid(child.pid, null, 0);
+ _ = libc.close(child.file.handle);
+ }
+ var sh: TestShell = .{ .master = child.file.handle, .pid = child.pid };
+ try std.testing.expect(writeFd(child.file.handle, "set -m; (cd a/b && printf '\\160ardes-job\\n' && exec sleep 30)\n"));
+ try std.testing.expect(sh.waitText("pardes-job", 5_000));
+ try std.testing.expectEqualStrings(start, ttyCwd(child.pid, child.file.handle, "/bin/sh", &result) orelse return error.MissingCwd);
+ }
+}
+
test "shell cwd rejects truncation and preserves an owned child path longer than 1024 bytes" {
if (comptime builtin.os.tag != .linux) return error.SkipZigTest;
if (!haveFile("/bin/sh")) return error.SkipZigTest;