diff options
Diffstat (limited to 'src/host_io.zig')
| -rw-r--r-- | src/host_io.zig | 1899 |
1 files changed, 1781 insertions, 118 deletions
diff --git a/src/host_io.zig b/src/host_io.zig index 55592957..af284e84 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -3,8 +3,10 @@ const std = @import("std"); const posix = std.posix; const libc = std.c; const pardes = @import("pardes.zig"); +const selection_pipe = @import("selection_pipe.zig"); const ninep_io = @import("9p_io.zig"); const filesystem = @import("fs.zig"); +const file_watch = @import("file_watch.zig"); pub const Host = struct { ctx: ?*anyopaque = null, @@ -12,15 +14,29 @@ pub const Host = struct { pub const VTable = struct { wait_input: ?*const fn (ctx: ?*anyopaque, timeout_ms: u32) void = null, + /// The shell's monotonic clock in ns: the only time the core sees. + /// Without one, core animation moves only by `.tick` events. + now: ?*const fn (ctx: ?*anyopaque) u64 = null, present: ?*const fn (ctx: ?*anyopaque, surface: *const pardes.Surface) void = null, post_present: ?*const fn (ctx: ?*anyopaque) void = null, poll_frame: ?*const fn (ctx: ?*anyopaque) void = null, + /// Whether anyone sees a frame drawn now. A detached session with no + /// client attached draws none (a /screen read draws its own), and + /// the frame owed is drawn when one attaches. Absent: always. + watched: ?*const fn (ctx: ?*anyopaque) bool = null, detach: ?*const fn (ctx: ?*anyopaque) void = null, spawn: ?*const fn (ctx: ?*anyopaque, pane: u8, cwd: []const u8) void = null, pty_write: ?*const fn (ctx: ?*anyopaque, pane: u8, bytes: []const u8) void = null, pty_resize: ?*const fn (ctx: ?*anyopaque, pane: u8, cols: u16, rows: u16) void = null, pty_signal: ?*const fn (ctx: ?*anyopaque, pane: u8, sig: pardes.PtySignal) void = null, + close_pty: ?*const fn (ctx: ?*anyopaque, pane: u8) void = null, tty_taken: ?*const fn (ctx: ?*anyopaque, pane: u8) bool = null, + /// The name of the program holding the tty, when it is not the + /// shell (host_io.foregroundName), in `buf`. + fg_name: ?*const fn (ctx: ?*anyopaque, pane: u8, buf: []u8) ?[]const u8 = null, + /// Answered at once, not queued as an effect: Kill asks while the + /// job it saw running is still the one in the tty (host_io.killJob). + kill_job: ?*const fn (ctx: ?*anyopaque, pane: u8) bool = null, gpio_toggle: ?*const fn (ctx: ?*anyopaque, pin: u16, was: *u8, now: *u8) bool = null, write_file: ?*const fn (ctx: ?*anyopaque, pane: u8, path: []const u8, bytes: []const u8) void = null, write_dump: ?*const fn (ctx: ?*anyopaque, bytes: []const u8) void = null, @@ -32,9 +48,23 @@ pub const Host = struct { open_link: ?*const fn (ctx: ?*anyopaque, url: []const u8) void = null, lsp: ?*const fn (ctx: ?*anyopaque, req: Lsp.Request) void = null, pipe: ?*const fn (ctx: ?*anyopaque, id: u32) void = null, + /// Start the streaming search of this generation on a worker + /// (pardes.Stream.start); absent, it runs on the loop. + search: ?*const fn (ctx: ?*anyopaque, gen: u32) void = null, + /// Carry the dump of handover `seq` to another window on a worker + /// (Handover.start), which answers through the inbox's `handed`. + hand_over: ?*const fn (ctx: ?*anyopaque, seq: u32, bytes: []const u8) void = null, }; }; +/// PARDES_TEST_CLOCK makes a shell's `Host.now` answer a virtual clock +/// instead of `std.Io.Clock.awake`, moved only when a timed wait runs out -- +/// straight to the core's next wake -- so the snapshot harness sees the same +/// frames whatever the machine's load. +pub fn testClock() ?u64 { + return if (libc.getenv("PARDES_TEST_CLOCK")) |v| (if (std.mem.span(v).len != 0) 0 else null) else null; +} + pub const Fallback = struct { pub const dump_path = "pardes.dump.zon"; @@ -91,6 +121,366 @@ pub const Fallback = struct { } }; +/// What a pixel host's loop takes from its workers, in order: each pane's +/// pty output (a reader waits while `output_capacity` is full, so output is +/// never dropped or reordered), its end of file, the language client's +/// answers and status and the `|` jobs' answers (those kept apart, so a +/// full output queue never holds them up), and file-watch news. Output and +/// end of file of a reader that was canceled or replaced, and an answer to +/// a language query since superseded, are dropped here. +pub const Inbox = struct { + pub const Msg = union(enum) { + output: struct { pane: u8, gen: u32, bytes: []u8 }, + eof: struct { pane: u8, gen: u32, failure: ?anyerror = null }, + lsp: struct { id: u32, rows: ?[]u8 }, + lsp_status: []u8, + pipe: selection_pipe.Response, + /// A streaming search's rows (pardes.Stream), of whatever generation: + /// the core drops a stale one. + search: pardes.Stream.Batch, + /// How a handover went (Handover.zig): `words` is where it went, or + /// why it did not; gpa-owned. + handed: struct { seq: u32, ok: bool, words: []u8 }, + files_changed, + fs_ready, + + pub fn deinit(m: Msg, gpa: std.mem.Allocator, lsp_allocator: std.mem.Allocator) void { + switch (m) { + .output => |o| gpa.free(o.bytes), + .lsp => |l| if (l.rows) |rows| lsp_allocator.free(rows), + .lsp_status => |t| lsp_allocator.free(t), + .pipe => |response_value| { + var response = response_value; + response.deinit(gpa); + }, + .search => |b| b.deinit(gpa), + .handed => |h| gpa.free(h.words), + .eof, .files_changed, .fs_ready => {}, + } + } + }; + + pub const capacity = 512; + pub const output_capacity = capacity - pardes.MAX_PANES; + + pub const Batch = struct { + items: [capacity + selection_pipe.Tasks.capacity + 2]Msg = undefined, + len: usize = 0, + + pub fn slice(batch: *Batch) []Msg { + return batch.items[0..batch.len]; + } + }; + + gpa: std.mem.Allocator, + lsp_allocator: std.mem.Allocator, + /// The query in flight; the UI thread's alone (spawnLsp, teardown). + lsp_task: ?Lsp.Task = null, + /// Wakes the host's loop after a push from another thread: an SDL + /// event, a macOS runtime callback. Null: nobody to wake (tests). + wake_fn: ?*const fn (?*anyopaque) void = null, + wake_ctx: ?*anyopaque = null, + /// The process's Io: its terminal readers wait for space on `space`. + io: std.Io, + mutex: std.Io.Mutex = .init, + space: std.Io.Condition = .init, + items: [capacity]Msg = undefined, + head: usize = 0, + len: usize = 0, + closed: bool = false, + files_changed: bool = false, + readers: [pardes.MAX_PANES]?u32 = @splat(null), + waiting: usize = 0, + lsp_id: ?u32 = null, + completions: [selection_pipe.Tasks.capacity + 1]Msg = undefined, + completion_len: usize = 0, + + pub fn lock(q: *Inbox) void { + q.mutex.lockUncancelable(q.io); + } + + pub fn unlock(q: *Inbox) void { + q.mutex.unlock(q.io); + } + + pub fn wake(q: *Inbox) void { + if (q.wake_fn) |f| f(q.wake_ctx); + } + + pub fn acceptReader(q: *Inbox, pane: u8, gen: u32) void { + q.lock(); + defer q.unlock(); + std.debug.assert(q.readers[pane] == null); + q.readers[pane] = gen; + } + + pub fn cancelReader(q: *Inbox, pane: u8, gen: u32) void { + q.lock(); + defer q.unlock(); + if (q.readers[pane] != gen) return; + q.readers[pane] = null; + var i: usize = 0; + while (i < q.len) { + const msg = q.items[(q.head + i) % q.items.len]; + const matches = switch (msg) { + .output => |o| o.pane == pane and o.gen == gen, + .eof => |e| e.pane == pane and e.gen == gen, + else => false, + }; + if (matches) q.removeAt(i).deinit(q.gpa, q.lsp_allocator) else i += 1; + } + q.space.broadcast(q.io); + } + + pub fn pushOutput(q: *Inbox, pane: u8, gen: u32, bytes: []u8) bool { + q.lock(); + while (!q.closed and q.readers[pane] == gen and q.len >= output_capacity) { + q.waiting += 1; + q.space.waitUncancelable(q.io, &q.mutex); + q.waiting -= 1; + } + if (q.closed or q.readers[pane] != gen) { + q.unlock(); + q.gpa.free(bytes); + return false; + } + q.items[(q.head + q.len) % q.items.len] = .{ .output = .{ .pane = pane, .gen = gen, .bytes = bytes } }; + q.len += 1; + q.unlock(); + q.wake(); + return true; + } + + fn removeAt(q: *Inbox, offset: usize) Msg { + const removed = q.items[(q.head + offset) % q.items.len]; + var i = offset; + while (i + 1 < q.len) : (i += 1) + q.items[(q.head + i) % q.items.len] = q.items[(q.head + i + 1) % q.items.len]; + q.len -= 1; + return removed; + } + + pub fn discardLsp(q: *Inbox) void { + for (q.completions[0..q.completion_len], 0..) |msg, i| if (msg == .lsp) { + msg.deinit(q.gpa, q.lsp_allocator); + q.completion_len -= 1; + std.mem.copyForwards(Msg, q.completions[i..q.completion_len], q.completions[i + 1 .. q.completion_len + 1]); + return; + }; + } + + pub fn push(q: *Inbox, m: Msg) void { + if (m == .output) { + _ = q.pushOutput(m.output.pane, m.output.gen, m.output.bytes); + return; + } + q.lock(); + if (q.closed) { + q.unlock(); + m.deinit(q.gpa, q.lsp_allocator); + return; + } + switch (m) { + .files_changed => q.files_changed = true, + .lsp, .pipe => { + if (m == .lsp) { + if (q.lsp_id != m.lsp.id) { + q.unlock(); + m.deinit(q.gpa, q.lsp_allocator); + return; + } + q.discardLsp(); + } + std.debug.assert(q.completion_len < q.completions.len); + q.completions[q.completion_len] = m; + q.completion_len += 1; + }, + else => { + if (m == .eof) { + if (q.readers[m.eof.pane] != m.eof.gen) { + q.unlock(); + return; + } + std.debug.assert(q.len < q.items.len); + } else if (q.len >= output_capacity) { + q.unlock(); + m.deinit(q.gpa, q.lsp_allocator); + return; + } + q.items[(q.head + q.len) % q.items.len] = m; + q.len += 1; + }, + } + q.unlock(); + q.wake(); + } + + pub fn take(q: *Inbox) Batch { + var batch: Batch = .{}; + q.takeInto(&batch); + return batch; + } + + /// `take` into a batch the caller keeps: an idle wakeup copies nothing + /// (the batch is some 20 KB, and was built and returned by value each + /// time). + pub fn takeInto(q: *Inbox, batch: *Batch) void { + batch.len = 0; + q.lock(); + defer q.unlock(); + if (q.len == 0 and !q.files_changed and q.completion_len == 0) return; + while (q.len > 0) { + batch.items[batch.len] = q.items[q.head]; + batch.len += 1; + q.head = (q.head + 1) % q.items.len; + q.len -= 1; + } + q.head = 0; + if (q.files_changed) { + batch.items[batch.len] = .files_changed; + batch.len += 1; + q.files_changed = false; + } + @memcpy(batch.items[batch.len..][0..q.completion_len], q.completions[0..q.completion_len]); + batch.len += q.completion_len; + q.completion_len = 0; + q.space.broadcast(q.io); + } + + pub fn discardCompletions(q: *Inbox) void { + q.lock(); + defer q.unlock(); + q.lsp_id = null; + for (q.completions[0..q.completion_len]) |msg| msg.deinit(q.gpa, q.lsp_allocator); + q.completion_len = 0; + } + + pub fn close(q: *Inbox) void { + q.lock(); + q.closed = true; + q.files_changed = false; + q.readers = @splat(null); + q.space.broadcast(q.io); + q.unlock(); + q.discardCompletions(); + q.lock(); + defer q.unlock(); + while (q.len > 0) { + const m = q.items[q.head]; + m.deinit(q.gpa, q.lsp_allocator); + q.head = (q.head + 1) % q.items.len; + q.len -= 1; + } + q.head = 0; + } + + pub fn deinit(q: *Inbox) void { + q.close(); + std.debug.assert(q.waiting == 0); + } + + /// A pane's reader: its pty's output pushed as it comes, then its end + /// of file; canceled (closePty), it pushes nothing more. + pub fn startReader(q: *Inbox, pty: std.Io.File, pane: u8, gen: u32) !std.Io.Future(void) { + q.acceptReader(pane, gen); + errdefer q.cancelReader(pane, gen); + return q.io.concurrent(readPty, .{ q, pty, pane, gen }); + } + + fn readPty(q: *Inbox, pty: std.Io.File, pane: u8, gen: u32) void { + var buf: [0x10000]u8 = undefined; + var failure: ?anyerror = null; + while (true) { + const n = pty.readStreaming(q.io, &.{&buf}) catch |err| switch (err) { + // A pty whose child has gone answers EIO: its end of file. + error.EndOfStream, error.InputOutput => break, + error.Canceled => return, + else => { + failure = err; + break; + }, + }; + if (n == 0) continue; + const bytes = q.gpa.dupe(u8, buf[0..n]) catch |err| { + failure = err; + break; + }; + if (!q.pushOutput(pane, gen, bytes)) return; + } + q.push(.{ .eof = .{ .pane = pane, .gen = gen, .failure = failure } }); + } + + /// Starts `e`, its answer let in and every older one's turned away. + pub fn startLsp(q: *Inbox, core: *pardes.Pardes, e: Lsp.Request) void { + q.lock(); + q.lsp_id = e.id; + q.discardLsp(); + q.unlock(); + // The old query's waits are cancelation points: its join is short, and + // what it still delivers carries an id the queue now rejects. + Lsp.start(q.io, q.lsp_allocator, core, &q.lsp_task, e, q, pushLspRows); + } + + fn pushLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void { + const q: *Inbox = @ptrCast(@alignCast(ctx orelse return)); + q.push(.{ .lsp = .{ .id = id, .rows = rows } }); + } + + /// The language client's status line (lsp.setStatusSink). + pub fn lspStatus(ctx: ?*anyopaque, text: []const u8) void { + const q: *Inbox = @ptrCast(@alignCast(ctx orelse return)); + const copy = q.lsp_allocator.dupe(u8, text) catch return; + q.push(.{ .lsp_status = copy }); + } + + /// A `|` job's answer (selection_pipe.start's sink). + pub fn pushPipe(ctx: ?*anyopaque, response: selection_pipe.Response) void { + const q: *Inbox = @ptrCast(@alignCast(ctx.?)); + q.push(.{ .pipe = response }); + } + + /// A handover's end (Handover.Job.post). + pub fn pushHanded(ctx: ?*anyopaque, seq: u32, ok: bool, words: []const u8) void { + const q: *Inbox = @ptrCast(@alignCast(ctx.?)); + const copy = q.gpa.dupe(u8, words) catch return; + q.push(.{ .handed = .{ .seq = seq, .ok = ok, .words = copy } }); + } + + /// A streaming search's batch (pardes.Stream.start's sink). Never + /// dropped for want of room, as output is not: the worker waits for + /// room, a wait its cancel ends (false: it stops). + pub fn pushSearch(ctx: ?*anyopaque, batch: pardes.Stream.Batch) bool { + const q: *Inbox = @ptrCast(@alignCast(ctx.?)); + q.lock(); + while (!q.closed and q.len >= output_capacity) { + q.waiting += 1; + q.space.wait(q.io, &q.mutex) catch { + q.waiting -= 1; + q.unlock(); + batch.deinit(q.gpa); + return false; + }; + q.waiting -= 1; + } + if (q.closed) { + q.unlock(); + batch.deinit(q.gpa); + return false; + } + q.items[(q.head + q.len) % q.items.len] = .{ .search = batch }; + q.len += 1; + q.unlock(); + q.wake(); + return true; + } + + /// A watched child exited (setExitWake): wake the loop, which takes it. + pub fn wakeForExit(ctx: ?*anyopaque) void { + const q: *Inbox = @ptrCast(@alignCast(ctx.?)); + q.wake(); + } +}; + pub const Lsp = struct { pub const Request = struct { id: u32, @@ -102,11 +492,13 @@ pub const Lsp = struct { pub const Task = struct { id: u32, - future: std.Io.Future(anyerror!void), + future: std.Io.Future(void), }; /// One language query, owned by the worker that runs it. pub const Job = struct { + callstack_depth: u8 = pardes.lsp.callstack_default_depth, + callstack_corners: pardes.lsp.CallstackCorners = .rounded, id: u32, kind: pardes.lsp.Kind, offset: u32, @@ -114,12 +506,19 @@ pub const Lsp = struct { source: [:0]u8, arg: []u8, root: []u8, + /// The other open files, for a query that can reach them. + others: []pardes.lsp.Doc = &.{}, pub fn free(job: *Job, gpa: std.mem.Allocator) void { gpa.free(job.path); gpa.free(job.source); gpa.free(job.arg); gpa.free(job.root); + for (job.others) |doc| { + gpa.free(doc.path); + gpa.free(doc.source); + } + gpa.free(job.others); gpa.destroy(job); } }; @@ -134,13 +533,16 @@ pub const Lsp = struct { const declared_path = if (file) |f| f.path else ""; const path = try gpa.dupe(u8, filesystem.localPath(declared_path) orelse declared_path); errdefer gpa.free(path); - const source = try gpa.dupeZ(u8, if (file) |f| f.content else ""); + const source = try gpa.dupeSentinel(u8, if (file) |f| f.content else "", 0); errdefer gpa.free(source); const arg = try gpa.dupe(u8, req.arg); errdefer gpa.free(arg); - const declared_root = if (file) |f| std.fs.path.dirname(f.path) orelse "/" else pane.cwdSlice(); + const declared_root = if (file) |f| pardes.panes.Dir.ofName(f.path) else pane.cwdSlice(); const root = try gpa.dupe(u8, filesystem.localPath(declared_root) orelse declared_root); + errdefer gpa.free(root); job.* = .{ + .callstack_depth = core.settings.callstack_depth, + .callstack_corners = core.settings.callstack_corners, .id = req.id, .kind = req.kind, .offset = req.offset, @@ -149,25 +551,86 @@ pub const Lsp = struct { .arg = arg, .root = root, }; + // ponytail: a copy of every open file per such query; a server + // already told of one is sent nothing more (syncDoc). + // Lspwhy too: the query it explains may be one of those, and its + // trace shows each file synced first. + if (pardes.lsp.reachesOtherFiles(req.kind) or req.kind == .explain) { + var others: std.ArrayList(pardes.lsp.Doc) = .empty; + errdefer { + for (others.items) |doc| { + gpa.free(doc.path); + gpa.free(doc.source); + } + others.deinit(gpa); + } + var copied: usize = 0; + for (core.panes, 0..) |slot, i| { + const other = slot orelse continue; + if (i == req.pane) continue; + const f = other.file orelse continue; + if (f.output != null) continue; + if (copied + f.content.len > pardes.memory.limits.lsp_snapshot_bytes) continue; + copied += f.content.len; + const local = filesystem.localPath(f.path) orelse continue; + const other_path = try gpa.dupe(u8, local); + const other_source = gpa.dupeSentinel(u8, f.content, 0) catch |err| { + gpa.free(other_path); + return err; + }; + try others.append(gpa, .{ .path = other_path, .source = other_source }); + } + job.others = try others.toOwnedSlice(gpa); + } return job; } // Null is failure; the receiver frees non-null rows with the job's allocator. pub const Deliver = *const fn (ctx: ?*anyopaque, id: u32, rows: ?[]u8) void; - pub fn work(gpa: std.mem.Allocator, job: *Job, ctx: ?*anyopaque, deliver: Deliver) void { + /// Start `req` on a worker that hands its rows to `deliver`, the query + /// in `task` canceled first. One that cannot start is answered (no rows) + /// and reported here. + pub fn start(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, task: *?Task, req: Request, ctx: ?*anyopaque, deliver: Deliver) void { + if (task.*) |*old| { + old.future.cancel(io); + task.* = null; + } + const job = snapshot(gpa, core, req) catch |err| { + core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); + return core.reportError(req.pane, "lsp", err); + }; + const future = io.concurrent(work, .{ io, gpa, job, ctx, deliver }) catch |err| { + job.free(gpa); + core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); + return core.reportError(req.pane, "lsp", err); + }; + task.* = .{ .id = req.id, .future = future }; + } + + pub fn work(io: std.Io, gpa: std.mem.Allocator, job: *Job, ctx: ?*anyopaque, deliver: Deliver) void { defer job.free(gpa); - var arena: std.heap.ArenaAllocator = .init(gpa); - defer arena.deinit(); + // The query's memory is the fixed region memory.zig reserved at + // startup: a hard bound, reused by each query in turn. + const region = pardes.memory.takeLspQuery() orelse { + deliver(ctx, job.id, null); + return; + }; + var arena: std.heap.FixedBufferAllocator = .init(region); + defer pardes.memory.giveLspQuery(arena.end_index); var out: std.Io.Writer.Allocating = .init(gpa); defer out.deinit(); - pardes.lsp.query(gpa, arena.allocator(), .{ + pardes.lsp.query(io, gpa, arena.allocator(), .{ .kind = job.kind, .path = job.path, .source = job.source, .offset = job.offset, .arg = job.arg, .root = job.root, + .others = job.others, + .callstack_depth = job.callstack_depth, + .callstack_corners = job.callstack_corners, + .budget = &arena, }, &out.writer) catch { deliver(ctx, job.id, null); return; @@ -184,6 +647,8 @@ pub const Lsp = struct { const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); defer core.deinit(); + core.settings.callstack_depth = 16; + core.settings.callstack_corners = .square; var needle: [6]u8 = "needle".*; const job = try snapshot(gpa, core, .{ .id = 7, @@ -194,6 +659,10 @@ pub const Lsp = struct { }); defer job.free(gpa); + core.settings.callstack_depth = 2; + core.settings.callstack_corners = .rounded; + try std.testing.expectEqual(pardes.lsp.CallstackCorners.square, job.callstack_corners); + try std.testing.expectEqual(@as(u8, 16), job.callstack_depth); try std.testing.expectEqual(@as(u32, 7), job.id); try std.testing.expectEqual(pardes.lsp.Kind.status, job.kind); try std.testing.expectEqualStrings("needle", job.arg); @@ -289,7 +758,7 @@ pub const Lsp = struct { .offset = 0, .arg = "", }); - work(gpa, job, null, Sink.take); + work(std.testing.io, gpa, job, null, Sink.take); // `status` is the one kind that answers with no file and no cursor, which // is what makes it assertable here without a language server on the box. @@ -322,7 +791,7 @@ pub const Lsp = struct { const path = try gpa.dupe(u8, "/file.zig"); gpa.free(pane.file.?.path); pane.file.?.path = path; - pane.cur_col = 6; + pane.body.cur_col = 6; const revision = pane.file.?.revision; const undo_len = pane.file.?.history.undo_len; var failing = std.testing.FailingAllocator.init(gpa, .{}); @@ -338,7 +807,7 @@ pub const Lsp = struct { .arg = "renamed", }); if (fail) failing.fail_index = failing.alloc_index; - work(failing.allocator(), job, &sink, Sink.take); + work(std.testing.io, failing.allocator(), job, &sink, Sink.take); try std.testing.expectEqual(fail, sink.failed); try std.testing.expect(core.lsp_wait == null); if (fail) { @@ -404,7 +873,7 @@ pub const Lsp = struct { defer core.deinit(); const job = try snapshot(gpa, core, .{ .id = 37, .kind = .status, .pane = @intCast(core.active), .offset = 0, .arg = "" }); var sink: Sink = .{}; - work(gpa, job, &sink, Sink.take); + work(std.testing.io, gpa, job, &sink, Sink.take); if (sink.rows) |rows| gpa.free(rows); try std.testing.expect(allocator.failed); try std.testing.expectEqual(@as(usize, 1), sink.calls); @@ -451,7 +920,76 @@ pub const Shell = struct { adoptSystemPath(); if (comptime builtin.os.tag.isDarwin()) _ = setenv("BASH_SILENCE_DEPRECATION_WARNING", "1", 1); - return PromptFiles.init(); + sweepDeadRcs(); + const rcs = PromptFiles.init(); + // A private file goes at a fatal signal too, not only at Exit. + for ([_]?[:0]const u8{ if (rcs.bash_owned) rcs.bashPath() else null, if (rcs.fish_owned) rcs.fishPath() else null }, 0..) |owned, i| { + const path = owned orelse continue; + @memcpy(fatal_rcs[i][0..path.len], path); + fatal_rcs[i][path.len] = 0; + fatal_rc_len[i] = path.len; + } + ninep_io.fatal_cleanup = unlinkOwnedRcs; + return rcs; + } + + /// This process's private prompt files, where a fatal signal's handler + /// can reach them (unlinkOwnedRcs). + var fatal_rcs: [2][rc_path_capacity:0]u8 = undefined; + var fatal_rc_len: [2]usize = .{ 0, 0 }; + + /// Removes this process's private prompt files. Async-signal-safe (two + /// unlinks): SIGTERM and SIGHUP call it before the signal's death. + pub fn unlinkOwnedRcs() void { + for (&fatal_rc_len, 0..) |*len, i| { + const n = len.*; + if (n == 0) continue; + len.* = 0; + _ = libc.unlink(fatal_rcs[i][0..n :0].ptr); + } + } + + /// Removes /tmp's `pardes-osc133-<shell>-<pid>-XXXXXX` files whose pid + /// is gone (kill(pid, 0) answers ESRCH): what a killed or crashed + /// editor left. A live pid's, one of another user's, or a name of + /// another shape is left alone. + fn sweepDeadRcs() void { + const dir = std.c.opendir("/tmp") orelse return; + defer _ = std.c.closedir(dir); + var dead: [64][64]u8 = undefined; + var dead_len: [64]usize = undefined; + var n: usize = 0; + while (std.c.readdir(dir)) |ent| { + const name = std.mem.sliceTo(@as([*:0]const u8, @ptrCast(&ent.name)), 0); + const pid = rcPid(name) orelse continue; + if (pid == std.c.getpid()) continue; + if (std.posix.errno(std.c.kill(pid, @fromBackingInt(@intCast(0)))) != .SRCH) continue; + if (name.len >= 64 or n == dead.len) continue; + @memcpy(dead[n][0..name.len], name); + dead_len[n] = name.len; + n += 1; + } + for (dead[0..n], dead_len[0..n]) |*entry, len| { + var path_buf: [80:0]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&path_buf, "/tmp/{s}", .{entry[0..len]}, 0) catch continue; + const facts = ninep_io.statNoFollow(path) orelse continue; + if (facts.mode & 0o170000 != 0o100000 or facts.uid != libc.getuid()) continue; + _ = libc.unlink(path.ptr); + } + } + + /// The pid in `pardes-osc133-<shell>-<pid>-XXXXXX`, or null. + fn rcPid(name: []const u8) ?std.c.pid_t { + const head = "pardes-osc133-"; + if (!std.mem.startsWith(u8, name, head)) return null; + var fields = std.mem.splitScalar(u8, name[head.len..], '-'); + _ = fields.next() orelse return null; // the shell + const digits = fields.next() orelse return null; + const rest = fields.next() orelse return null; + if (fields.next() != null or rest.len != 6 or digits.len == 0 or digits.len > 10) return null; + for (digits) |c| if (!std.ascii.isDigit(c)) return null; + const pid = std.fmt.parseInt(std.c.pid_t, digits, 10) catch return null; + return if (pid > 0) pid else null; } fn collectSystemPath(paths_file: [:0]const u8, paths_dir: []const u8, buf: []u8, len: *usize) void { @@ -586,11 +1124,73 @@ pub const Shell = struct { return .none; } + /// `family`, for a shell whose name does not say it (a renamed copy, a + /// link named otherwise): what it really is, by the name its links end + /// at, else by the program itself -- bash's carries BASH_VERSINFO, fish's + /// fish_prompt. ponytail: a string scan of at most 16 MiB, once a spawn + /// and only for a name that says nothing; a --version probe would fork. + fn familyOf(path: [*:0]const u8) Family { + const named = family(std.mem.span(path)); + if (named != .none) return named; + // A name a POSIX shell answers to is that shell, whatever the file: + // bash run as `sh` is sh, and reads no --rcfile. + const bin = std.mem.span(path); + const base = if (std.mem.lastIndexOfScalar(u8, bin, '/')) |s| bin[s + 1 ..] else bin; + for ([_][]const u8{ "sh", "dash", "zsh", "ksh", "mksh", "yash", "nu", "elvish", "xonsh", "tcsh", "csh" }) |known| + if (std.mem.eql(u8, base, known)) return .none; + var real: [std.fs.max_path_bytes]u8 = undefined; + if (libc.realpath(path, &real)) |r| { + const by_link = family(std.mem.span(@as([*:0]const u8, @ptrCast(r)))); + if (by_link != .none) return by_link; + } + const fd = libc.open(path, .{ .ACCMODE = .RDONLY }, @as(libc.mode_t, 0)); + if (fd < 0) return .none; + defer _ = libc.close(fd); + var chunk: [64 * 1024]u8 = undefined; + var carry: usize = 0; + var total: usize = 0; + while (total < 16 << 20) { + const n = libc.read(fd, chunk[carry..].ptr, chunk.len - carry); + if (n <= 0) break; + const have = carry + @as(usize, @intCast(n)); + total += @intCast(n); + // A script is no binary: its interpreter is what runs. + if (total == have and std.mem.startsWith(u8, chunk[0..have], "#!")) return .none; + if (std.mem.indexOf(u8, chunk[0..have], "BASH_VERSINFO") != null) return .bash; + if (std.mem.indexOf(u8, chunk[0..have], "fish_prompt") != null) return .fish; + // Keep the tail, so a marker cut by the chunk is still seen. + carry = @min(have, 16); + std.mem.copyForwards(u8, chunk[0..carry], chunk[have - carry .. have]); + } + return .none; + } + + test "a bash under another name is still told as bash" { + const bash: [:0]const u8 = "/usr/bin/bash"; + if (libc.access(bash, X_OK) != 0) return; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const bytes = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, bash, std.testing.allocator, .limited(16 << 20)); + defer std.testing.allocator.free(bytes); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "mysh", .data = bytes, .flags = .{ .permissions = .executable_file } }); + var dir_buf: [4096]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + var path: [4200:0]u8 = undefined; + const p = try std.fmt.bufPrintSentinel(&path, "{s}/mysh", .{dir}, 0); + try std.testing.expectEqual(Family.bash, familyOf(p.ptr)); + try std.testing.expectEqual(Family.none, familyOf("/bin/sh")); + } + const bash_rc = \\[ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc" - \\PS1='\[\e]133;A;cl=line\a\]'"$PS1"'\[\e]133;B\a\]' - \\PROMPT_COMMAND='printf "\e]133;D\a"'"${PROMPT_COMMAND:+;$PROMPT_COMMAND}" - \\trap 'printf "\e]133;C\a"' DEBUG + \\PS1='\[\e]133;A;cl=line;aid=pardes\a\]'"$PS1"'\[\e]133;B;aid=pardes\a\]' + \\PS2='\[\e]133;A;k=c;aid=pardes\a\]'"$PS2"'\[\e]133;B;aid=pardes\a\]' + \\PROMPT_COMMAND='printf "\e]133;D;%s;aid=pardes\a" "$?"'"${PROMPT_COMMAND:+;$PROMPT_COMMAND}" + \\if ((BASH_VERSINFO[0] * 100 + BASH_VERSINFO[1] >= 404)); then + \\ PS0='\e]133;C;aid=pardes\a'"$PS0" + \\else + \\ trap 'printf "\e]133;C;aid=pardes\a"' DEBUG + \\fi \\ ; @@ -598,48 +1198,74 @@ pub const Shell = struct { const fish_rc = \\functions -c fish_prompt __pardes_user_prompt \\function fish_prompt - \\ printf '\e]133;A;cl=line\a' + \\ printf '\e]133;A;cl=line;aid=pardes\a' \\ __pardes_user_prompt - \\ printf '\e]133;B\a' + \\ printf '\e]133;B;aid=pardes\a' \\end \\function __pardes_preexec --on-event fish_preexec - \\ printf '\e]133;C\a' + \\ printf '\e]133;C;aid=pardes\a' \\end \\function __pardes_postexec --on-event fish_postexec - \\ printf '\e]133;D\a' + \\ printf '\e]133;D;%s;aid=pardes\a' $status \\end \\ ; - const rc_path_capacity = 64; + const rc_path_capacity = 256; - // Private files live until host teardown. Lengths keep this value movable. + /// The prompt files a shell sources at start. Under $XDG_RUNTIME_DIR + /// (private to the user) there is one file per content, named by its + /// hash and shared by every pardes, so none is left behind by a pardes + /// that was killed: 72K of them piled up in /tmp when each host wrote + /// its own. Without that directory each host writes private mkstemp + /// files in /tmp and removes them at teardown, as before. pub const PromptFiles = struct { bash_path: [rc_path_capacity:0]u8 = @splat(0), - bash_len: u8 = 0, + bash_len: u16 = 0, + bash_owned: bool = false, fish_path: [rc_path_capacity:0]u8 = @splat(0), - fish_len: u8 = 0, - fish_command: [rc_path_capacity + "source ".len:0]u8 = @splat(0), - fish_command_len: u8 = 0, + fish_len: u16 = 0, + fish_owned: bool = false, + fish_command: [rc_path_capacity + "source ''".len:0]u8 = @splat(0), + fish_command_len: u16 = 0, pub fn init() PromptFiles { var rcs: PromptFiles = .{}; - rcs.bash_len = stage(&rcs.bash_path, "/tmp/pardes-osc133-bash-XXXXXX", bash_rc); - rcs.fish_len = stage(&rcs.fish_path, "/tmp/pardes-osc133-fish-XXXXXX", fish_rc); + const bash = place(&rcs.bash_path, "bash", bash_rc); + rcs.bash_len = bash.len; + rcs.bash_owned = bash.owned; + const fish = place(&rcs.fish_path, "fish", fish_rc); + rcs.fish_len = fish.len; + rcs.fish_owned = fish.owned; if (rcs.fishPath()) |path| { - const command = std.fmt.bufPrintSentinel(&rcs.fish_command, "source {s}", .{path}, 0) catch { - _ = libc.unlink(path.ptr); + // Quoted for fish; a path a quote would break is not used. + const command = if (std.mem.indexOfAny(u8, path, "'\\") != null) + error.NoSpaceLeft + else + std.fmt.bufPrintSentinel(&rcs.fish_command, "source '{s}'", .{path}, 0); + const ok = command catch { + if (rcs.fish_owned) _ = libc.unlink(path.ptr); rcs.fish_len = 0; return rcs; }; - rcs.fish_command_len = @intCast(command.len); + rcs.fish_command_len = @intCast(ok.len); } return rcs; } pub fn deinit(rcs: *PromptFiles) void { - if (rcs.bashPath()) |path| _ = libc.unlink(path.ptr); - if (rcs.fishPath()) |path| _ = libc.unlink(path.ptr); + // Removed here, not again by a signal's handler. + for (&fatal_rc_len, 0..) |*len, i| for ([_]?[:0]const u8{ rcs.bashPath(), rcs.fishPath() }) |mine| { + if (mine) |path| if (len.* == path.len and std.mem.eql(u8, fatal_rcs[i][0..len.*], path)) { + len.* = 0; + }; + }; + if (rcs.bashPath()) |path| if (rcs.bash_owned) { + _ = libc.unlink(path.ptr); + }; + if (rcs.fishPath()) |path| if (rcs.fish_owned) { + _ = libc.unlink(path.ptr); + }; rcs.bash_len = 0; rcs.fish_len = 0; rcs.fish_command_len = 0; @@ -661,8 +1287,36 @@ pub const Shell = struct { } }; + /// The shared file for `contents` under $XDG_RUNTIME_DIR, written + /// beside it and renamed into place when missing or different, so a + /// shell never reads half of one; failing that, a private /tmp file. + fn place(path_buf: *[rc_path_capacity:0]u8, name: []const u8, contents: []const u8) struct { len: u16, owned: bool } { + shared: { + const dir = std.mem.span(std.c.getenv("XDG_RUNTIME_DIR") orelse break :shared); + if (dir.len == 0 or dir[0] != '/') break :shared; + const final = std.fmt.bufPrintSentinel(path_buf, "{s}/pardes-osc133-{s}-{x:0>16}", .{ dir, name, std.hash.Wyhash.hash(0, contents) }, 0) catch break :shared; + var have: [2048]u8 = undefined; + if (readSmall(final, &have)) |got| if (std.mem.eql(u8, got, contents)) return .{ .len = @intCast(final.len), .owned = false }; + var tmp_buf: [rc_path_capacity:0]u8 = @splat(0); + var template_buf: [rc_path_capacity]u8 = undefined; + const tmp_template = std.fmt.bufPrint(&template_buf, "{s}.XXXXXX", .{final}) catch break :shared; + const n = stage(&tmp_buf, tmp_template, contents); + if (n == 0) break :shared; + if (libc.rename(tmp_buf[0..n :0].ptr, final.ptr) != 0) { + _ = libc.unlink(tmp_buf[0..n :0].ptr); + break :shared; + } + return .{ .len = @intCast(final.len), .owned = false }; + } + var tmp_template: [64]u8 = undefined; + // Its pid in the name: one a killed editor left is swept by the + // next (sweepDeadRcs). + const template = std.fmt.bufPrint(&tmp_template, "/tmp/pardes-osc133-{s}-{d}-XXXXXX", .{ name, @as(u32, @intCast(std.c.getpid())) }) catch return .{ .len = 0, .owned = false }; + return .{ .len = stage(path_buf, template, contents), .owned = true }; + } + // Publish a path only after its private 0600 file is fully written and closed. - fn stage(path_buf: *[rc_path_capacity:0]u8, template: []const u8, contents: []const u8) u8 { + fn stage(path_buf: *[rc_path_capacity:0]u8, template: []const u8, contents: []const u8) u16 { const path = std.fmt.bufPrintSentinel(path_buf, "{s}", .{template}, 0) catch return 0; const fd = mkstemp(path.ptr); if (fd < 0) return 0; @@ -720,27 +1374,49 @@ pub const Shell = struct { pub const Spawn = struct { path: [*:0]const u8, - argv: [4:null]?[*:0]const u8, + argv: [6:null]?[*:0]const u8, }; // Resolve in the parent. The path buffer and prompt files must survive through exec. pub fn resolve(bin: []const u8, buf: *[std.fs.max_path_bytes]u8, prompt_rcs: *const PromptFiles) Spawn { const path = find(bin, buf) orelse fallback(buf); - const marks: [2]?[*:0]const u8 = switch (family(std.mem.span(path))) { + const marks: [2]?[*:0]const u8 = switch (familyOf(path)) { .bash => if (prompt_rcs.bashPath()) |rc| .{ "--rcfile", rc.ptr } else .{ null, null }, .fish => if (prompt_rcs.fishCommand()) |command| .{ "-C", command.ptr } else .{ null, null }, .none => .{ null, null }, }; - return .{ .path = path, .argv = .{ path, marks[0], marks[1], null } }; + return .{ .path = path, .argv = .{ path, marks[0], marks[1], null, null, null } }; + } + + /// A command pane's shell runs its one line with job control on, so a + /// job the line puts in the background has a process group of its own. + /// The shell leads the terminal's session, and when it exits the kernel + /// hangs up the terminal's foreground group: with job control off that + /// is the shell's own, the job's too, and the job died with the command + /// (nohup'd or not; only setsid got out, and not always in time). A + /// shell pardes does not know runs the line as it is. + pub fn commandArgv(path: [*:0]const u8, line: [*:0]const u8) [6:null]?[*:0]const u8 { + const bin = std.mem.span(path); + const base = if (std.mem.lastIndexOfScalar(u8, bin, '/')) |s| bin[s + 1 ..] else bin; + const posix_shells = [_][]const u8{ "bash", "sh", "dash", "zsh", "ksh", "mksh", "yash" }; + for (posix_shells) |name| if (std.mem.eql(u8, base, name)) + return .{ path, "-m", "-c", line, null, null }; + if (family(bin) == .fish) return .{ path, "-C", "status job-control full", "-c", line, null }; + return .{ path, "-c", line, null, null, null }; } - fn find(bin: []const u8, buf: *[std.fs.max_path_bytes]u8) ?[*:0]const u8 { + /// A shell by its path or its name in the usual directories, or null. + /// A path may be under a mount this editor serves: the turn goes out + /// with the lookups (pardes.Turn.yield). + pub fn find(bin: []const u8, buf: *[std.fs.max_path_bytes]u8) ?[*:0]const u8 { if (bin.len == 0 or bin.len + 1 > buf.len) return null; + pardes.turn.yield(); + defer pardes.turn.back(); if (std.mem.indexOfScalar(u8, bin, '/') != null) { @memcpy(buf[0..bin.len], bin); buf[bin.len] = 0; const p: [*:0]const u8 = @ptrCast(buf); - return if (libc.access(p, X_OK) == 0) p else null; + return if (libc.access(p, X_OK) == 0 and !isDirectory(p)) p else null; } for (bin_dirs) |dir| { if (dir.len + bin.len + 1 > buf.len) continue; @@ -753,6 +1429,75 @@ pub const Shell = struct { return null; } + /// A directory passes access(X_OK) (it can be searched): no shell. + pub fn isDirectory(path: [*:0]const u8) bool { + const d = libc.opendir(path) orelse return false; + _ = libc.closedir(d); + return true; + } + + /// Why `bin` is no shell, for the words that name one (Tty, Shell); + /// null when it is one. + pub fn refusal(bin: []const u8, said: []u8) ?[]const u8 { + var buf: [std.fs.max_path_bytes]u8 = undefined; + if (find(bin, &buf)) |found| { + // A script whose `#!` names a program that is not there: its + // exec fails ENOENT as a missing shell's does, so told apart. + var interp: [256]u8 = undefined; + const missing = missingInterpreter(found, &interp) orelse return null; + return std.fmt.bufPrint(said, "interpreter {s} not found", .{missing}) catch "interpreter not found"; + } + if (std.mem.indexOfScalar(u8, bin, '/') != null and bin.len < buf.len) { + @memcpy(buf[0..bin.len], bin); + buf[bin.len] = 0; + if (isDirectory(@ptrCast(&buf))) return std.fmt.bufPrint(said, "not a shell: {s} is a directory", .{bin}) catch "not a shell: a directory"; + // There, but no program: said so, not "not found". + if (libc.access(@ptrCast(&buf), 0) == 0) return std.fmt.bufPrint(said, "not a shell: {s} is not executable", .{bin}) catch "not a shell: not executable"; + } + // "not found": the words a mount reads as ENOENT. + return std.fmt.bufPrint(said, "shell \"{s}\" not found (a name on the usual paths, or a path to one)", .{bin}) catch "no such shell"; + } + + /// The program a script's `#!` line names, when it is not there to + /// run; null for a script whose interpreter is there, or no script. + pub fn missingInterpreter(path: [*:0]const u8, out: []u8) ?[]const u8 { + pardes.turn.yield(); + defer pardes.turn.back(); + const fd = libc.open(path, .{ .ACCMODE = .RDONLY }, @as(libc.mode_t, 0)); + if (fd < 0) return null; + defer _ = libc.close(fd); + var head: [256]u8 = undefined; + const n = libc.read(fd, &head, head.len); + if (n < 2) return null; + const got = head[0..@intCast(n)]; + if (!std.mem.startsWith(u8, got, "#!")) return null; + const line = got[2 .. std.mem.indexOfScalar(u8, got, '\n') orelse got.len]; + var words = std.mem.tokenizeAny(u8, line, " \t\r"); + const interp = words.next() orelse return null; + if (interp.len + 1 > out.len) return null; + @memcpy(out[0..interp.len], interp); + out[interp.len] = 0; + if (libc.access(@ptrCast(out.ptr), X_OK) == 0) return null; + return out[0..interp.len]; + } + + test "a script whose #! names a program not there is told apart from a missing shell" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "bad", .data = "#!/no/such/interp -x\n", .flags = .{ .permissions = .executable_file } }); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "good", .data = "#!/bin/sh\n", .flags = .{ .permissions = .executable_file } }); + var dir_buf: [4096]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + var path: [4200]u8 = undefined; + var said: [320]u8 = undefined; + try std.testing.expectEqualStrings("interpreter /no/such/interp not found", refusal(try std.fmt.bufPrint(&path, "{s}/bad", .{dir}), &said).?); + try std.testing.expect(refusal(try std.fmt.bufPrint(&path, "{s}/good", .{dir}), &said) == null); + try std.testing.expect(std.mem.startsWith(u8, refusal(try std.fmt.bufPrint(&path, "{s}/none", .{dir}), &said).?, "shell \"")); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "plain", .data = "echo\n" }); + const plain = try std.fmt.bufPrint(&path, "{s}/plain", .{dir}); + try std.testing.expect(std.mem.endsWith(u8, refusal(plain, &said).?, "plain is not executable")); + } + fn fallback(buf: *[std.fs.max_path_bytes]u8) [*:0]const u8 { for (fallbacks) |f| { @memcpy(buf[0..f.len], f); @@ -763,6 +1508,20 @@ pub const Shell = struct { return @ptrCast(buf); } + test "a command pane's line runs with job control on in the shells that have it" { + const want = struct { + fn check(path: [*:0]const u8, words: []const []const u8) !void { + const argv = commandArgv(path, "sleep 9 &"); + for (words, 0..) |w, i| try std.testing.expectEqualStrings(w, std.mem.span(argv[i].?)); + try std.testing.expect(argv[words.len] == null); + } + }; + try want.check("/usr/bin/bash", &.{ "/usr/bin/bash", "-m", "-c", "sleep 9 &" }); + try want.check("/bin/sh", &.{ "/bin/sh", "-m", "-c", "sleep 9 &" }); + try want.check("/usr/bin/fish", &.{ "/usr/bin/fish", "-C", "status job-control full", "-c", "sleep 9 &" }); + try want.check("/usr/bin/nu", &.{ "/usr/bin/nu", "-c", "sleep 9 &" }); + } + test "a path is taken at its word, a name is looked up, and both pick their own marks" { if (builtin.os.tag == .windows) return; var buf: [std.fs.max_path_bytes]u8 = undefined; @@ -786,55 +1545,119 @@ pub const Shell = struct { try std.testing.expect(libc.access(gone.path, X_OK) == 0); } - test "prompt rc owners have private complete files and clean them up" { + test "a private prompt file names its pid, and a dead pid's is swept while a live one's stays" { if (builtin.os.tag == .windows) return; - var original = PromptFiles.init(); - var a = original; - original = .{}; - original.deinit(); - defer a.deinit(); - var b = PromptFiles.init(); - defer b.deinit(); - const a_bash = a.bashPath() orelse return error.TempCreateFailed; - const b_bash = b.bashPath() orelse return error.TempCreateFailed; - const a_fish = a.fishPath() orelse return error.TempCreateFailed; - try std.testing.expect(!std.mem.eql(u8, a_bash, b_bash)); - const fish_command = a.fishCommand() orelse return error.MissingFishCommand; - try std.testing.expectEqualStrings("source ", fish_command[0.."source ".len]); - try std.testing.expectEqualStrings(a_fish, fish_command["source ".len..]); - for ([_][]const u8{ a_bash, b_bash, a_fish }) |path| { - const stat = try std.Io.Dir.cwd().statFile(std.testing.io, path, .{}); - try std.testing.expectEqual(std.Io.File.Kind.file, stat.kind); - try std.testing.expectEqual(0, stat.permissions.toMode() & 0o077); + const dead = "/tmp/pardes-osc133-bash-2147483647-AbCdEf"; + var live_buf: [80:0]u8 = undefined; + const live = try std.fmt.bufPrintSentinel(&live_buf, "/tmp/pardes-osc133-bash-{d}-AbCdEf", .{@as(u32, @intCast(std.c.getppid()))}, 0); + defer for ([_][:0]const u8{ dead, live }) |path| { + _ = libc.unlink(path.ptr); + }; + for ([_][:0]const u8{ dead, live }) |path| { + const fd = libc.open(path, .{ .CREAT = true, .ACCMODE = .WRONLY }, @as(libc.mode_t, 0o600)); + if (fd < 0) return error.SkipZigTest; + _ = libc.close(fd); } - var fish_buf: [fish_rc.len]u8 = undefined; - try std.testing.expectEqualStrings(fish_rc, readSmall(a_fish, &fish_buf) orelse return error.ReadFailed); + sweepDeadRcs(); + try std.testing.expect(libc.access(dead, 0) < 0); + try std.testing.expect(libc.access(live, 0) == 0); + // The pid is read only from that shape: an older name has none. + try std.testing.expectEqual(@as(?std.c.pid_t, 42), rcPid("pardes-osc133-fish-42-Xy12Zq")); + try std.testing.expectEqual(@as(?std.c.pid_t, null), rcPid("pardes-osc133-bash-01z3XK")); + } - var buf: [bash_rc.len]u8 = undefined; - const fd = libc.open(a_bash.ptr, .{ .ACCMODE = .RDONLY }); - if (fd < 0) return error.OpenFailed; - defer _ = libc.close(fd); - var len: usize = 0; - while (len < buf.len) { - const n = libc.read(fd, buf[len..].ptr, buf.len - len); - if (n < 0) { - if (libc.errno(n) == .INTR) continue; - return error.ReadFailed; - } - if (n == 0) break; - len += @intCast(n); - } - try std.testing.expectEqualStrings(bash_rc, buf[0..len]); + test "prompt files are one per content under the runtime dir, or private and cleaned up without it" { + if (builtin.os.tag == .windows) return; + var saved_buf: [std.fs.max_path_bytes:0]u8 = @splat(0); + const saved: ?[:0]const u8 = if (std.c.getenv("XDG_RUNTIME_DIR")) |v| std.fmt.bufPrintSentinel(&saved_buf, "{s}", .{std.mem.span(v)}, 0) catch null else null; + defer _ = if (saved) |v| setenv("XDG_RUNTIME_DIR", v.ptr, 1) else testUnsetenv("XDG_RUNTIME_DIR"); + + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var dir_buf: [std.fs.max_path_bytes:0]u8 = @splat(0); + const dir_len = try tmp.dir.realPath(std.testing.io, &dir_buf); + _ = setenv("XDG_RUNTIME_DIR", dir_buf[0..dir_len :0].ptr, 1); - var removed: [rc_path_capacity:0]u8 = @splat(0); - @memcpy(removed[0..a_bash.len], a_bash); - removed[a_bash.len] = 0; + // Shared: the same file for every owner, left in place, rewritten + // whole when what is there is not its content. + var a = PromptFiles.init(); + var b = PromptFiles.init(); + const a_bash = a.bashPath() orelse return error.TempCreateFailed; + try std.testing.expectEqualStrings(a_bash, b.bashPath().?); + try std.testing.expect(std.mem.startsWith(u8, a_bash, dir_buf[0..dir_len])); + const fish_command = a.fishCommand() orelse return error.MissingFishCommand; + var want: [rc_path_capacity + 16]u8 = undefined; + try std.testing.expectEqualStrings(try std.fmt.bufPrint(&want, "source '{s}'", .{a.fishPath().?}), fish_command); + var buf: [2048]u8 = undefined; + try std.testing.expectEqualStrings(bash_rc, readSmall(a_bash, &buf) orelse return error.ReadFailed); + try std.testing.expectEqualStrings(fish_rc, readSmall(a.fishPath().?, &buf) orelse return error.ReadFailed); + const stat = try @import("fs.zig").statPath(std.testing.io, a_bash, .{}); + try std.testing.expectEqual(0, stat.permissions.toMode() & 0o077); + var kept: [rc_path_capacity:0]u8 = @splat(0); + @memcpy(kept[0..a_bash.len], a_bash); a.deinit(); - try std.testing.expect(libc.access(&removed, 0) < 0); - try std.testing.expectEqualStrings(bash_rc, readSmall(b_bash, &buf) orelse return error.ReadFailed); + b.deinit(); + try std.testing.expect(libc.access(&kept, 0) == 0); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = std.fs.path.basename(kept[0..a_bash.len]), .data = "half" }); + var c = PromptFiles.init(); + defer c.deinit(); + try std.testing.expectEqualStrings(bash_rc, readSmall(c.bashPath().?, &buf) orelse return error.ReadFailed); + + // No runtime dir: private files, one per owner, gone at teardown. + _ = testUnsetenv("XDG_RUNTIME_DIR"); + var d = PromptFiles.init(); + var e = PromptFiles.init(); + defer e.deinit(); + const d_bash = d.bashPath() orelse return error.TempCreateFailed; + try std.testing.expect(!std.mem.eql(u8, d_bash, e.bashPath().?)); + try std.testing.expect(std.mem.startsWith(u8, d_bash, "/tmp/pardes-osc133-bash-")); + try std.testing.expectEqual(@as(?std.c.pid_t, std.c.getpid()), rcPid(std.fs.path.basename(d_bash))); + @memcpy(kept[0..d_bash.len], d_bash); + kept[d_bash.len] = 0; + d.deinit(); + try std.testing.expect(libc.access(&kept, 0) < 0); } }; +/// In a child between fork and exec: every signal back to its default and +/// none blocked, whatever this process ignores or blocks (the tty's SIGWINCH, +/// a SIGHUP nohup ignored). A handler resets at exec by itself; an ignored +/// signal and the mask do not. Async-signal-safe: sigaction and sigprocmask. +pub fn resetChildSignals() void { + const default: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.DFL }, .mask = posix.sigemptyset(), .flags = 0 }; + var sig: u8 = 1; + while (sig < 65) : (sig += 1) { + if (sig == @backingInt(posix.SIG.KILL) or sig == @backingInt(posix.SIG.STOP)) continue; + _ = std.c.sigaction(@fromBackingInt(@intCast(sig)), &default, null); + } + const none = posix.sigemptyset(); + posix.sigprocmask(posix.SIG.SETMASK, &none, null); +} + +test "a child starts with every signal at its default and none blocked" { + var blocked = posix.sigemptyset(); + posix.sigaddset(&blocked, posix.SIG.WINCH); + posix.sigprocmask(posix.SIG.BLOCK, &blocked, null); + defer posix.sigprocmask(posix.SIG.UNBLOCK, &blocked, null); + const ignore: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.IGN }, .mask = posix.sigemptyset(), .flags = 0 }; + var was: posix.Sigaction = undefined; + posix.sigaction(posix.SIG.HUP, &ignore, &was); + defer posix.sigaction(posix.SIG.HUP, &was, null); + const pid = libc.fork(); + if (pid == 0) { + resetChildSignals(); + var now: posix.Sigaction = undefined; + posix.sigaction(posix.SIG.HUP, null, &now); + var mask = posix.sigemptyset(); + posix.sigprocmask(posix.SIG.BLOCK, null, &mask); + const ok = now.handler.handler == posix.SIG.DFL and !posix.sigismember(&mask, posix.SIG.WINCH); + libc._exit(if (ok) 0 else 1); + } + var status: c_int = 0; + _ = libc.waitpid(pid, &status, 0); + try std.testing.expectEqual(@as(c_int, 0), status); +} + extern "c" fn forkpty(amaster: *c_int, name: ?[*:0]u8, termp: ?*const anyopaque, winp: ?*const posix.winsize) c_int; extern "c" fn execv(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; extern "c" fn execve(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8, envp: [*:null]const ?[*:0]const u8) c_int; @@ -862,6 +1685,44 @@ pub const ChildEnv = struct { "TERM_PROGRAM=" ++ pardes.config.child_term_program, }; + /// What this editor tells its children of itself (exportPaneEnv, `$winid`), + /// kept here rather than set into this process's environment: std's Io read + /// that block at startup, and setenv/unsetenv rewrite it in place under it + /// (a spawn then scans a null inside it). `build` puts each exported one in + /// place of an inherited variable of its name; a cleared one hides it; one + /// never exported is inherited as it is. + pub const exported_names = [_][]const u8{ "PARDES_PID", "PARDES_9P", "PARDES_PANE", "PARDES_MOUNT", "winid" }; + var exported_state: [exported_names.len]enum { inherited, cleared, set } = @splat(.inherited); + var exported_line: [exported_names.len][pardes.memory.limits.host_path_cap + 64:0]u8 = undefined; + + /// Set (or, null, clear) what children are told for `var_name`. + pub fn exportVar(var_name: []const u8, value: ?[]const u8) void { + const i = exportedIndex(var_name).?; + const v = value orelse { + exported_state[i] = .cleared; + return; + }; + const line = std.fmt.bufPrintSentinel(&exported_line[i], "{s}={s}", .{ var_name, v }, 0) catch { + exported_state[i] = .cleared; + return; + }; + exported_state[i] = .set; + _ = line; + } + + /// What children are told for `var_name`, as `exportVar` left it; null + /// when cleared or never exported. + pub fn exported(var_name: []const u8) ?[]const u8 { + const i = exportedIndex(var_name) orelse return null; + if (exported_state[i] != .set) return null; + return std.mem.span(@as([*:0]const u8, &exported_line[i]))[var_name.len + 1 ..]; + } + + fn exportedIndex(var_name: []const u8) ?usize { + for (exported_names, 0..) |n, i| if (std.mem.eql(u8, n, var_name)) return i; + return null; + } + /// Wide enough for any real environment; `build` answers null rather than /// truncate, because a child missing half its variables is a worse bug /// than one missing TERM, and the caller then execs with `environ` intact. @@ -872,7 +1733,12 @@ pub const ChildEnv = struct { return line[0 .. std.mem.indexOfScalar(u8, line, '=') orelse line.len]; } - pub fn build(slots: *Slots) ?[*:null]const ?[*:0]const u8 { + /// Which pager a child is given where the user's environment names none: + /// a command pane's one-shot shell `cat` (its output is a pane already), + /// a terminal's shell `pardes -` (a +Pager pane) unless `Pager off`. + pub const Paging = enum { command, terminal, terminal_off }; + + pub fn build(slots: *Slots, paging: Paging) ?[*:null]const ?[*:0]const u8 { var n: usize = 0; var i: usize = 0; while (libc.environ[i]) |entry| : (i += 1) { @@ -881,10 +1747,33 @@ pub const ChildEnv = struct { if (std.mem.eql(u8, inherited, name(mine))) break true; } else false; if (shadowed) continue; - if (n + own.len + 1 > slots.len) return null; + if (exportedIndex(inherited)) |e| if (exported_state[e] != .inherited) continue; + if (n + own.len + exported_names.len + 1 > slots.len) return null; slots[n] = entry; n += 1; } + // No pager to wait in (less holds a run at its first screen for + // ever): a command pane's output is read whole, through cat; a + // terminal's goes to a +Pager pane. Only where the inherited + // environment chose none. + const defaults: []const [*:0]const u8 = switch (paging) { + .command => &cat_pagers, + .terminal => pardesPagers(), + .terminal_off => &.{}, + }; + for (defaults) |default| { + const set = for (libc.environ[0..i]) |entry| { + if (std.mem.eql(u8, name(entry.?), name(default))) break true; + } else false; + if (set) continue; + if (n + own.len + exported_names.len + 4 > slots.len) return null; + slots[n] = default; + n += 1; + } + for (&exported_line, exported_state) |*line, state| if (state == .set) { + slots[n] = line; + n += 1; + }; for (own) |mine| { slots[n] = mine; n += 1; @@ -892,8 +1781,58 @@ pub const ChildEnv = struct { slots[n] = null; return @ptrCast(slots); } + + const cat_pagers = [_][*:0]const u8{ "PAGER=cat", "GIT_PAGER=cat" }; + + /// `PAGER='<this pardes> -'`, its own path so no PATH lookup can find + /// another; plain `pardes -` where the path cannot be read or quoted. + fn pardesPagers() []const [*:0]const u8 { + const S = struct { + var pager: [std.fs.max_path_bytes + 32:0]u8 = undefined; + var git_pager: [std.fs.max_path_bytes + 32:0]u8 = undefined; + var systemd_pager: [std.fs.max_path_bytes + 32:0]u8 = undefined; + var entries: [4][*:0]const u8 = undefined; + var ready = false; + }; + if (!S.ready) { + var exe_buf: [std.fs.max_path_bytes]u8 = undefined; + var quoted_buf: [std.fs.max_path_bytes + 2]u8 = undefined; + const n = if (comptime builtin.os.tag == .linux) libc.readlink("/proc/self/exe", &exe_buf, exe_buf.len) else -1; + const exe: []const u8 = if (n > 0 and @as(usize, @intCast(n)) < exe_buf.len) exe_buf[0..@intCast(n)] else "pardes"; + const command = pagerCommand(exe, "ed_buf); + S.entries[0] = if (std.fmt.bufPrintSentinel(&S.pager, "PAGER={s} -", .{command}, 0)) |t| t.ptr else |_| "PAGER=pardes -"; + S.entries[1] = if (std.fmt.bufPrintSentinel(&S.git_pager, "GIT_PAGER={s} -", .{command}, 0)) |t| t.ptr else |_| "GIT_PAGER=pardes -"; + // systemd's tools (journalctl, systemctl) read their own, and + // in their secure mode run only less: this pager has no shell + // to escape to, so secure mode buys nothing. + S.entries[2] = if (std.fmt.bufPrintSentinel(&S.systemd_pager, "SYSTEMD_PAGER={s} -", .{command}, 0)) |t| t.ptr else |_| "SYSTEMD_PAGER=pardes -"; + S.entries[3] = "SYSTEMD_PAGERSECURE=0"; + S.ready = true; + } + return &S.entries; + } + + /// The pager's path as a command line takes it: as it is when it needs + /// no quoting (systemd splits on blanks and execs, never through sh), + /// in single quotes when it holds a blank or a shell character, plain + /// `pardes` when even that cannot hold it. + fn pagerCommand(exe: []const u8, buf: []u8) []const u8 { + const plain = for (exe) |c| { + if (!(std.ascii.isAlphanumeric(c) or std.mem.indexOfScalar(u8, "/._+-,@%=:", c) != null)) break false; + } else true; + if (plain) return exe; + if (std.mem.indexOfScalar(u8, exe, '\'') != null) return "pardes"; + return std.fmt.bufPrint(buf, "'{s}'", .{exe}) catch "pardes"; + } }; +test "the pager's path is quoted only when it needs it" { + var buf: [64]u8 = undefined; + try std.testing.expectEqualStrings("/usr/bin/pardes", ChildEnv.pagerCommand("/usr/bin/pardes", &buf)); + try std.testing.expectEqualStrings("'/opt/my apps/pardes'", ChildEnv.pagerCommand("/opt/my apps/pardes", &buf)); + try std.testing.expectEqualStrings("pardes", ChildEnv.pagerCommand("/odd'dir/pardes", &buf)); +} + test "the child environment replaces the launcher's terminal identity exactly once" { const saved: ?[]const u8 = if (libc.getenv("TERM")) |t| std.mem.span(t) else null; var saved_buf: [256]u8 = undefined; @@ -906,21 +1845,48 @@ test "the child environment replaces the launcher's terminal identity exactly on defer if (restore) |t| { _ = Shell.setenv("TERM", t.ptr, 1); } else { - _ = unsetenv("TERM"); + _ = testUnsetenv("TERM"); }; // A launcher TERM must be shadowed rather than duplicated, and the rest of // the environment must arrive untouched. try std.testing.expectEqual(@as(c_int, 0), Shell.setenv("TERM", "dumb", 1)); try std.testing.expectEqual(@as(c_int, 0), Shell.setenv("PARDES_CHILD_ENV_PROBE", "1", 1)); - defer _ = unsetenv("PARDES_CHILD_ENV_PROBE"); + defer _ = testUnsetenv("PARDES_CHILD_ENV_PROBE"); var slots: ChildEnv.Slots = undefined; - const envp = ChildEnv.build(&slots) orelse return error.EnvironmentTooLarge; + // Where the user named no pager: a terminal's shell pages into a + // +Pager pane (`pardes -`), or, Pager off, as the environment says; a + // command pane's through cat. A pager the user named is kept. + for ([_]ChildEnv.Paging{ .terminal, .terminal_off }) |paging| { + var shell_slots: ChildEnv.Slots = undefined; + const shell_envp = ChildEnv.build(&shell_slots, paging) orelse return error.EnvironmentTooLarge; + var said: usize = 0; + var systemd: usize = 0; + var j: usize = 0; + while (shell_envp[j]) |entry| : (j += 1) { + const line = std.mem.span(entry); + if (std.c.getenv("SYSTEMD_PAGER") == null and std.mem.startsWith(u8, line, "SYSTEMD_PAGER=")) systemd += 1; + if (std.c.getenv("SYSTEMD_PAGERSECURE") == null and std.mem.eql(u8, line, "SYSTEMD_PAGERSECURE=0")) systemd += 1; + if (std.mem.startsWith(u8, line, "PAGER=") and std.c.getenv("PAGER") == null) { + try std.testing.expect(paging == .terminal and std.mem.endsWith(u8, line, " -")); + said += 1; + } + } + if (std.c.getenv("PAGER") == null) try std.testing.expectEqual(@as(usize, if (paging == .terminal) 1 else 0), said); + // systemd's pager and its secure mode off, with ours only. + if (std.c.getenv("SYSTEMD_PAGER") == null and std.c.getenv("SYSTEMD_PAGERSECURE") == null) + try std.testing.expectEqual(@as(usize, if (paging == .terminal) 2 else 0), systemd); + } + const envp = ChildEnv.build(&slots, .command) orelse return error.EnvironmentTooLarge; var terms: usize = 0; var colorterms: usize = 0; var probes: usize = 0; var dumb = false; + var pagers: usize = 0; + var git_pagers: usize = 0; + var pager_cat = false; + var git_pager_cat = false; var i: usize = 0; while (envp[i]) |entry| : (i += 1) { const line = std.mem.span(entry); @@ -928,7 +1894,16 @@ test "the child environment replaces the launcher's terminal identity exactly on if (std.mem.startsWith(u8, line, "COLORTERM=")) colorterms += 1; if (std.mem.eql(u8, line, "TERM=dumb")) dumb = true; if (std.mem.eql(u8, line, "PARDES_CHILD_ENV_PROBE=1")) probes += 1; + if (std.mem.startsWith(u8, line, "PAGER=")) pagers += 1; + if (std.mem.startsWith(u8, line, "GIT_PAGER=")) git_pagers += 1; + if (std.mem.eql(u8, line, "PAGER=cat")) pager_cat = true; + if (std.mem.eql(u8, line, "GIT_PAGER=cat")) git_pager_cat = true; } + // A pager only where the user named none: cat, so nothing waits in less. + try std.testing.expectEqual(@as(usize, 1), pagers); + try std.testing.expectEqual(@as(usize, 1), git_pagers); + if (std.c.getenv("PAGER") == null) try std.testing.expect(pager_cat); + if (std.c.getenv("GIT_PAGER") == null) try std.testing.expect(git_pager_cat); try std.testing.expectEqual(@as(usize, 1), terms); try std.testing.expectEqual(@as(usize, 1), colorterms); try std.testing.expectEqual(@as(usize, 1), probes); @@ -940,6 +1915,49 @@ test "the child environment replaces the launcher's terminal identity exactly on extern "c" fn unsetenv(name: [*:0]const u8) c_int; +/// For tests: unset `name` without compacting the environment block std +/// captured at startup (std.testing.environ and Io's spawns read it later). A +/// new variable first moves glibc's `environ` to a heap copy, which unsetenv +/// then compacts instead. Runtime code never changes this process's +/// environment (ChildEnv.exportVar). +pub fn testUnsetenv(name: [*:0]const u8) c_int { + _ = Shell.setenv("PARDES_TEST_ENVIRON_COPIED", "1", 1); + return unsetenv(name); +} + +/// The environment a pane's command is given (exportPaneEnv, `$winid`, +/// ChildEnv), as `NAME=value` lines in `arena`, for a command that is not a +/// pane's: an Edit's filters, run from pane `serial`. +pub fn commandEnv(arena: std.mem.Allocator, serial: u32, adopts: bool) ![]const []const u8 { + ninep_io.exportSessionEnv(serial, adopts); + exportWinid(serial); + var slots: ChildEnv.Slots = undefined; + const envp = ChildEnv.build(&slots, .command) orelse return error.EnvironmentTooLarge; + var out: std.ArrayList([]const u8) = .empty; + var i: usize = 0; + while (envp[i]) |entry| : (i += 1) try out.append(arena, try arena.dupe(u8, std.mem.span(entry))); + return out.items; +} + +fn exportWinid(serial: u32) void { + var buf: [16]u8 = undefined; + ChildEnv.exportVar("winid", if (serial == 0) null else std.fmt.bufPrint(&buf, "{d}", .{serial}) catch null); +} + +/// The longest line a command pane runs (exec.zig refuses a longer one). +pub const exec_command_max = pardes.exec.command_max; + +/// A pane's pty size, in cells and in pixels: a program sizes its kitty +/// graphics by the pixels (yazi's previews; pardes nested in a pane). +pub fn ptyWinsize(core: ?*const pardes.Pardes, cols: u16, rows: u16) posix.winsize { + var ws: posix.winsize = .{ .row = rows, .col = cols, .xpixel = 0, .ypixel = 0 }; + if (comptime pardes.pdf_enabled) if (core) |c| { + ws.xpixel = std.math.cast(u16, @as(u32, cols) * c.cell_pixels.w) orelse 0; + ws.ypixel = std.math.cast(u16, @as(u32, rows) * c.cell_pixels.h) orelse 0; + }; + return ws; +} + pub fn forkShell( core: ?*pardes.Pardes, pane: usize, @@ -953,62 +1971,140 @@ pub fn forkShell( const native_cwd = filesystem.localPath(cwd) orelse cwd; if (std.mem.indexOfScalar(u8, native_cwd, 0) != null) return error.InvalidPath; var cwd_buf: [4096]u8 = undefined; + const serial = if (core) |c| if (c.panes[pane]) |pn| pn.serial else 0 else 0; const cwd_z: ?[:0]const u8 = if (native_cwd.len == 0) null else dir: { const path = std.fmt.bufPrintSentinel(&cwd_buf, "{s}", .{native_cwd}, 0) catch return error.NameTooLong; - const stat = try std.Io.Dir.cwd().statFile(std.Io.Threaded.global_single_threaded.io(), path, .{}); + // A shell's directory may be inside a mount this editor serves. + pardes.turn.yield(); + defer pardes.turn.back(); + const stat = try @import("fs.zig").statPath(std.Io.Threaded.global_single_threaded.io(), path, .{}); if (stat.kind != .directory) return error.NotDir; break :dir path; }; var master: c_int = -1; var path_buf: [std.fs.max_path_bytes]u8 = undefined; - const spawn = Shell.resolve(bin, &path_buf, prompt_rcs); + // A terminal opened on a shell of its own (`Tty fish`) runs that one, + // copied: looking it up gives the turn up, and the pane may go. + var own_buf: [256]u8 = undefined; + var own: ?[]const u8 = null; + if (core) |c| if (c.panes[pane]) |pn| if (pn.shell) |name| { + const n = @min(name.len, own_buf.len); + @memcpy(own_buf[0..n], name[0..n]); + own = own_buf[0..n]; + }; + var spawn = Shell.resolve(own orelse bin, &path_buf, prompt_rcs); + // The turn was given up for the stat and the lookup: a 9P client may + // have closed the pane, and another taken its slot, whose command this + // is not. + if (core) |c| if ((if (c.panes[pane]) |pn| pn.serial else 0) != serial) return error.PaneGone; + // A command pane's child is the shell running its one line, which has + // no prompt to mark (exec.zig runCommand). + const one_line = if (core) |c| if (c.panes[pane]) |pn| pn.command else null else null; + var command_buf: [exec_command_max + 1]u8 = undefined; + if (one_line) |line| { + if (line.len > exec_command_max or std.mem.indexOfScalar(u8, line, 0) != null) return error.InvalidCommand; + @memcpy(command_buf[0..line.len], line); + command_buf[line.len] = 0; + spawn.argv = Shell.commandArgv(spawn.path, command_buf[0..line.len :0].ptr); + } var helper_buf: [4096]u8 = undefined; - const mounted = if (core) |c| if (c.panes[pane]) |pn| pn.v9fs_on_spawn else false else false; + const mounted = one_line == null and if (core) |c| if (c.panes[pane]) |pn| pn.v9fs_on_spawn else false else false; const helper: ?[:0]u8 = if (mounted) blk: { if (comptime builtin.os.tag != .linux) return error.LinuxRequired; if (fs == null) return error.SessionSocketRequired; break :blk try @import("linux/v9fs.zig").helperPath(&helper_buf); } else null; + // The last argument is whether this session takes its children's Looks — + // every session does but a `--nested` one, and a fork with no core at all + // (the tests below) is nobody's parent. ninep_io.exportPaneEnv( fs, if (core) |c| (if (c.panes[pane]) |pn| pn.serial else 0) else 0, if (core) |c| !c.opts.nested else false, ); + // acme's `$winid`: the pane a command was run from, so a script it runs + // can address that pane; unset for a shell and a header tag's command. + exportWinid(if (core) |c| (if (c.panes[pane]) |pn| (if (pn.command != null) pn.command_from else 0) else 0) else 0); if (helper) |path| { const c = core.?; const pn = c.panes[pane].?; - const socket = libc.getenv("PARDES_9P") orelse return error.SessionSocketRequired; + const socket = ChildEnv.exported("PARDES_9P") orelse return error.SessionSocketRequired; var command: std.Io.Writer.Allocating = .init(c.gpa); defer command.deinit(); - try @import("linux/v9fs.zig").writeLaunchCommand(&command.writer, path, std.mem.span(socket), &spawn.argv); - if (!try pardes.panes.Terminal.queuePendingCommand(pn, command.written())) return error.ShellAlreadyStarted; + try @import("linux/v9fs.zig").writeLaunchCommand(&command.writer, path, socket, &spawn.argv); + if (!try pardes.panes.terminal.queuePendingCommand(pn, command.written())) return error.ShellAlreadyStarted; } // Built here and not after the fork: between fork and exec the child may // not call setenv, whose malloc can deadlock against a pty reader thread // that held the heap when the fork took its snapshot. var env_slots: ChildEnv.Slots = undefined; - const envp = ChildEnv.build(&env_slots); - const ws = posix.winsize{ .row = rows, .col = cols, .xpixel = 0, .ypixel = 0 }; + const pages = if (core) |c| if (c.panes[pane]) |pn| pn.command_pages else false else false; + const paging: ChildEnv.Paging = if (one_line != null and !pages) + .command + else if (core) |c| (if (c.settings.pager == .off) .terminal_off else .terminal) else .terminal; + const envp = ChildEnv.build(&env_slots, paging); + const ws = ptyWinsize(core, cols, rows); + // The child says here why it could not become the shell (its chdir or + // its exec failed, as for a script whose interpreter is not there): the + // pipe closes on exec, so the parent reads nothing when the shell runs, + // and a reason when it did not, before anyone is told it started. + var told: [2]c_int = .{ -1, -1 }; + // pipe and then CLOEXEC, not pipe2, which macOS has not. + if (libc.pipe(&told) != 0) return error.PipeFailed; + ninep_io.setCloexec(told[0]); + ninep_io.setCloexec(told[1]); const pid = forkpty(&master, null, null, &ws); - if (pid < 0) return error.ForkFailed; + if (pid < 0) { + _ = libc.close(told[0]); + _ = libc.close(told[1]); + return error.ForkFailed; + } if (pid == 0) { - var set = posix.sigemptyset(); - posix.sigaddset(&set, posix.SIG.WINCH); - posix.sigprocmask(posix.SIG.UNBLOCK, &set, null); - if (cwd_z) |path| if (chdir(path.ptr) != 0) _exit(126); + resetChildSignals(); + if (cwd_z) |path| if (chdir(path.ptr) != 0) { + const why = [_]u8{ 'c', @truncate(@backingInt(libc.errno(@as(c_int, -1)))) }; + _ = libc.write(told[1], &why, why.len); + _exit(126); + }; if (envp) |env| _ = execve(spawn.path, &spawn.argv, env); _ = execv(spawn.path, &spawn.argv); + const why = [_]u8{ 'x', @truncate(@backingInt(libc.errno(@as(c_int, -1)))) }; + _ = libc.write(told[1], &why, why.len); _exit(127); } + _ = libc.close(told[1]); + const failed = failed: { + var why: [2]u8 = undefined; + pardes.turn.yield(); + defer pardes.turn.back(); + const n = libc.read(told[0], &why, why.len); + _ = libc.close(told[0]); + if (n != 2) break :failed null; + break :failed why; + }; + if (failed) |why| { + _ = libc.close(master); + _ = libc.waitpid(pid, null, 0); + const e: libc.E = @fromBackingInt(@intCast(why[1])); + if (why[0] == 'c') return if (e == .NOENT or e == .NOTDIR) error.FileNotFound else error.AccessDenied; + return switch (e) { + // Itself, or its script's interpreter: told apart by whether it + // is there. + .NOENT => if (libc.access(spawn.path, 0) == 0) error.InterpreterNotFound else error.ShellNotFound, + .ACCES, .PERM => error.ShellNotExecutable, + .NOEXEC => error.ShellNotExecutable, + else => error.ShellDidNotStart, + }; + } ninep_io.setCloexec(master); - if (core) |c| c.acknowledgeShell(pane, std.mem.span(spawn.path), spawn.argv[1] != null); + if (core) |c| c.acknowledgeShell(pane, std.mem.span(spawn.path), one_line == null and spawn.argv[1] != null); return .{ .file = .{ .handle = master, .flags = .{ .nonblocking = false } }, .pid = pid }; } test "shell spawn rejects invalid directories before creating a child" { const rcs: Shell.PromptFiles = .{}; try std.testing.expectError(error.InvalidPath, forkShell(null, 0, &rcs, "/bin/sh", "/tmp\x00/ignored", 24, 80, null)); - const too_long = [_]u8{'x'} ** 4096; + const too_long = @as([4096]u8, @splat('x')); try std.testing.expectError(error.NameTooLong, forkShell(null, 0, &rcs, "/bin/sh", &too_long, 24, 80, null)); var tmp = std.testing.tmpDir(.{}); @@ -1021,7 +2117,7 @@ test "shell spawn rejects invalid directories before creating a child" { try std.testing.expectError(error.NotDir, forkShell(null, 0, &rcs, "/bin/sh", file, 24, 80, null)); const explicit_file = try std.fmt.bufPrint(&path_buf, "/n/os{s}/file", .{directory}); try std.testing.expectError(error.NotDir, forkShell(null, 0, &rcs, "/bin/sh", explicit_file, 24, 80, null)); - const missing = try std.fmt.bufPrint(&path_buf, "{s}/missing/" ++ ("child/" ** 50), .{directory}); + const missing = try std.fmt.bufPrint(&path_buf, "{s}/missing/" ++ (std.mem.asBytes(&@as([50][6]u8, @splat("child/".*)))), .{directory}); try std.testing.expect(missing.len > 256); try std.testing.expectError(error.FileNotFound, forkShell(null, 0, &rcs, "/bin/sh", missing, 24, 80, null)); } @@ -1041,7 +2137,7 @@ test "a forked shell is told which terminal it is talking to" { defer if (restore) |t| { _ = Shell.setenv("TERM", t.ptr, 1); } else { - _ = unsetenv("TERM"); + _ = testUnsetenv("TERM"); }; _ = Shell.setenv("TERM", "dumb", 1); @@ -1066,7 +2162,7 @@ test "shell spawn uses an explicit OS directory longer than 256 bytes" { if (!haveFile("/bin/sh")) return error.SkipZigTest; var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); - const sub_path = "nested-directory-with-more-than-forty-characters/" ** 7; + const sub_path = std.mem.asBytes(&@as([7][49]u8, @splat("nested-directory-with-more-than-forty-characters/".*))); try tmp.dir.createDirPath(std.testing.io, sub_path); var directory_buf: [4096]u8 = undefined; const directory = directory_buf[0..try tmp.dir.realPathFile(std.testing.io, sub_path, &directory_buf)]; @@ -1119,7 +2215,14 @@ pub fn shellCwd(pid: libc.pid_t, buf: []u8) ?[]const u8 { const path = std.fmt.bufPrintSentinel(&pbuf, "/proc/{d}/cwd", .{pid}, 0) catch return null; const n = libc.readlink(path, buf.ptr, buf.len); if (n <= 0 or n >= buf.len) return null; - return buf[0..@intCast(n)]; + const link = buf[0..@intCast(n)]; + // A directory removed under the shell reads `<path> (deleted)`: + // no directory to be named by, so the name it had stays, and a + // restart works there once it is back. + if (std.mem.endsWith(u8, link, " (deleted)")) { + _ = @import("fs.zig").statPath(std.Io.Threaded.global_single_threaded.io(), link, .{}) catch return null; + } + return link; }, .macos, .ios, .tvos, .watchos, .visionos => { var info: proc_vnodepathinfo = undefined; @@ -1134,13 +2237,88 @@ 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) + try std.testing.io.sleep(.fromMilliseconds(20), .awake); + 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; const io = std.testing.io; var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); - const nested = "nested-directory-with-more-than-forty-characters/" ** 24; + const nested = std.mem.asBytes(&@as([24][49]u8, @splat("nested-directory-with-more-than-forty-characters/".*))); try tmp.dir.createDirPath(io, nested); var path_buf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; const directory = path_buf[0..try tmp.dir.realPathFile(io, nested, &path_buf)]; @@ -1163,6 +2341,31 @@ test "shell cwd rejects truncation and preserves an owned child path longer than try std.testing.expectEqualStrings(directory, shellCwd(child.pid, &result) orelse return error.MissingCwd); } +test "a shell whose directory was removed is not named `... (deleted)`" { + if (comptime builtin.os.tag != .linux) return error.SkipZigTest; + if (!haveFile("/bin/sh")) return error.SkipZigTest; + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.createDirPath(io, "gone"); + var path_buf: [4096]u8 = undefined; + const directory = path_buf[0..try tmp.dir.realPathFile(io, "gone", &path_buf)]; + const rcs: Shell.PromptFiles = .{}; + const child = try forkShell(null, 0, &rcs, "/bin/sh", directory, 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, "printf '\\160ardes-cwd-ready\\n'\n")); + try std.testing.expect(sh.waitText("pardes-cwd-ready", 5_000)); + var result: [4096]u8 = undefined; + try std.testing.expectEqualStrings(directory, shellCwd(child.pid, &result) orelse return error.MissingCwd); + try tmp.dir.deleteDir(io, "gone"); + try std.testing.expect(shellCwd(child.pid, &result) == null); +} + extern "c" fn tcgetpgrp(fd: c_int) libc.pid_t; const occ_max_depth: u8 = 8; @@ -1215,6 +2418,10 @@ pub fn ttyTaken(shell_pid: libc.pid_t, master_fd: c_int) bool { const exe = procExe(node.pid, &probe.exe) orelse { if (offTty(node.pid, &probe.blob)) continue; + // Exiting (its exe gone, not a zombie yet), or not ours + // to read: outside the foreground group it holds + // nothing, as a killed background job does for a moment. + if (pgrp) |g| if (g != fg) continue; return true; }; if (!std.mem.eql(u8, exe, self_exe)) { @@ -1249,7 +2456,7 @@ pub fn ttyTaken(shell_pid: libc.pid_t, master_fd: c_int) bool { /// and what still catches `bash -c 'sleep 30'`: the group holds `sleep` too. /// /// Until this existed, `ttyTaken` answered `false` on darwin for every pane, -/// so `Pardes.takesCommandLine` was permanently true and every bare Escape in +/// so `exec.takesCommandLine` was permanently true and every bare Escape in /// a raw terminal ran the `Last` builtin instead of reaching the child. const darwin = struct { const proc_pgrp_only: u32 = 2; @@ -1318,7 +2525,224 @@ const darwin = struct { } }; -pub fn signalTty(shell_pid: libc.pid_t, master_fd: c_int, which: pardes.PtySignal) void { +/// A terminal pane's shell is done with (its pane gone, its pty closed or at +/// end of file): say hangup, for a shell that ignores the tty's, and see it +/// reaped, killed if it is still there 100 ms on. A thread does the waiting +/// so that no host loop needs a timer for it (macOS's has none); a thread +/// that cannot start kills and reaps it here. +/// ponytail: a shell still inside its 100 ms when the editor exits is left +/// to init; the gui and detached hosts keep their own retired lists, which +/// this could replace. +pub fn retireShell(pid: libc.pid_t) void { + if (pid <= 0) return; + _ = libc.kill(pid, libc.SIG.HUP); + if (libc.waitpid(pid, null, libc.W.NOHANG) != 0) return; + const thread = std.Thread.spawn(.{}, reapShell, .{ pid, 100 }) catch return reapShell(pid, 0); + thread.detach(); +} + +// ---- a command pane's child, watched to its exit ---- +// +// A command pane's command is done when its process exits, not when its pty +// reaches end of file: a job it put in the background can hold the pty open +// after it has gone, and a command can close its terminal and run on. A +// thread per command waits for the exit without reaping (WNOWAIT), so the +// pid is still the child's -- no Kill can reach another process that took +// 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; + +var exited: [pardes.MAX_PANES * 2]std.atomic.Value(libc.pid_t) = @splat(.init(0)); +var exit_lock: libc.pthread_mutex_t = .{}; +var exit_wake: ?struct { ctx: ?*anyopaque, f: *const fn (?*anyopaque) void } = null; + +/// Who wakes the host's loop when a watched child exits; null at shutdown, +/// after which a watcher still waiting tells nobody. +pub fn setExitWake(ctx: ?*anyopaque, f: ?*const fn (?*anyopaque) void) void { + _ = libc.pthread_mutex_lock(&exit_lock); + defer _ = libc.pthread_mutex_unlock(&exit_lock); + exit_wake = if (f) |wake| .{ .ctx = ctx, .f = wake } else null; +} + +/// Watches a command pane's child; false when no thread could be started, +/// and the host then reads its exit at end of file instead. +pub fn watchExit(pid: libc.pid_t) bool { + const thread = std.Thread.spawn(.{}, watchChild, .{pid}) catch return false; + thread.detach(); + return true; +} + +fn watchChild(pid: libc.pid_t) void { + var info: [128]u8 align(16) = undefined; + while (waitid(waitid_p_pid, @intCast(pid), &info, waitid_wexited | waitid_wnowait) < 0 and libc.errno(-1) == .INTR) {} + // A cell for it, waiting for one if the host is slow to take them. + place: while (true) { + for (&exited) |*cell| if (cell.cmpxchgStrong(0, pid, .acq_rel, .acquire) == null) break :place; + const ts: libc.timespec = .{ .sec = 0, .nsec = 5 * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); + } + wakeForExit(); +} + +fn wakeForExit() void { + _ = libc.pthread_mutex_lock(&exit_lock); + defer _ = libc.pthread_mutex_unlock(&exit_lock); + if (exit_wake) |wake| wake.f(wake.ctx); +} + +/// Where a host's command pane is between its child's exit and the end of +/// its output. The exit is told once the output before it is in: at the +/// pty's end of file; or, when something else still holds the pty (a job +/// the command left in the background), once nothing is waiting to be read +/// -- checked when the exit is seen and again after each chunk of output, +/// with no timer to miss. acme's waitthread tells an exit when it is +/// reaped (acme.c:587-700); a terminal also has output in flight. +pub const CommandWatch = struct { + watched: bool = false, + /// The master, to ask whether output is still waiting. + fd: c_int = -1, + eof: bool = false, + exited: bool = false, + told: bool = false, + status: ?u8 = null, + + /// Whether the exit is to be told now. + pub fn due(w: *const CommandWatch) bool { + if (!w.exited or w.told) return false; + if (w.eof) return true; + var pfd = [1]libc.pollfd{.{ .fd = w.fd, .events = libc.POLL.IN, .revents = 0 }}; + if (libc.poll(&pfd, 1, 0) < 0) return true; + // Every holder of its terminal gone: the end of file comes, after + // the rest of the output. + if (pfd[0].revents & libc.POLL.HUP != 0) return false; + return pfd[0].revents & libc.POLL.IN == 0; + } +}; + +/// A host's pty slot, whichever way it keeps it: an optional, or one whose +/// `fd` is -1 when empty. +fn SlotPty(comptime S: type) type { + return switch (@typeInfo(S)) { + .optional => |o| *o.child, + else => *S, + }; +} + +fn slotPty(slot: anytype) ?SlotPty(@TypeOf(slot.*)) { + return switch (@typeInfo(@TypeOf(slot.*))) { + .optional => if (slot.*) |*pt| pt else null, + else => if (slot.fd < 0) null else slot, + }; +} + +/// Each watched child that exited: reaped, and the core told once its +/// output is in, its pty closed (`close(ctx, id)`) if its end of file came +/// first. Every host runs this after an exit's wake and after each chunk +/// of a command's output; `ptys` is its slot array, each slot with a `pid` +/// and a `cmd`. +pub fn takeExits(core: *pardes.Pardes, ptys: anytype, ctx: anytype, comptime close: anytype) void { + while (takeExited()) |pid| for (ptys) |*slot| { + const pt = slotPty(slot) orelse continue; + if (!pt.cmd.watched or pt.pid != pid or pt.cmd.exited) continue; + pt.cmd.status = (reapExited(pid) orelse break).status; + pt.cmd.exited = true; + pt.pid = 0; // reaped: no signal or retire may reach whoever gets it next + break; + }; + for (ptys, 0..) |*slot, id| { + const pt = slotPty(slot) orelse continue; + if (!pt.cmd.watched or !pt.cmd.due()) continue; + pt.cmd.told = true; + const eof = pt.cmd.eof; + core.update(.{ .exited = .{ .pane = @intCast(id), .status = pt.cmd.status } }); + if (eof) close(ctx, id); + } +} + +/// A watched command's pty reached end of file: the core hears its output +/// is over; the pty is closed now if its exit was told, else when it is. +pub fn commandEof(core: *pardes.Pardes, ptys: anytype, id: usize, ctx: anytype, comptime close: anytype) void { + const pt = slotPty(&ptys[id]) orelse return; + pt.cmd.eof = true; + core.update(.{ .eof = .{ .pane = @intCast(id) } }); + if (pt.cmd.told) return close(ctx, id); + takeExits(core, ptys, ctx, close); +} + +/// Reaps `pid` if it has exited: its status (null: unknown, or not ours +/// to read), or null when it is still running -- a pid a watcher reported +/// may since have been reaped by another and taken by a new child. +pub fn reapExited(pid: libc.pid_t) ?struct { status: ?u8 } { + if (pid <= 0) return .{ .status = null }; + var status: c_int = 0; + const got = libc.waitpid(pid, &status, libc.W.NOHANG); + if (got == 0) return null; + if (got != pid) return .{ .status = null }; + const s: u32 = @bitCast(status); + if (libc.W.IFEXITED(s)) return .{ .status = @truncate(libc.W.EXITSTATUS(s)) }; + if (libc.W.IFSIGNALED(s)) return .{ .status = @truncate(128 + @as(u32, @backingInt(libc.W.TERMSIG(s)))) }; + return .{ .status = null }; +} + +/// A watched child that has exited, not yet reaped, or null. +pub fn takeExited() ?libc.pid_t { + for (&exited) |*cell| { + const pid = cell.swap(0, .acq_rel); + if (pid != 0) return pid; + } + return null; +} + +/// A pane's child at its pty's end of file: reaped, and how it exited +/// (its code, or 128 and the signal that ended it), if it is gone within +/// `wait_ms`; null if it is still running (a job it left holds nothing +/// open, but the child itself may be a moment behind its pty), which the +/// caller then retires as it would any shell. +pub fn exitStatus(pid: libc.pid_t, wait_ms: u32) ?u8 { + if (pid <= 0) return null; + var waited: u32 = 0; + while (true) : (waited += 2) { + var status: c_int = 0; + const got = libc.waitpid(pid, &status, libc.W.NOHANG); + if (got == pid) { + const s: u32 = @bitCast(status); + if (libc.W.IFEXITED(s)) return @truncate(libc.W.EXITSTATUS(s)); + if (libc.W.IFSIGNALED(s)) return @truncate(128 + @as(u32, @backingInt(libc.W.TERMSIG(s)))); + return null; + } + if (got < 0 or waited >= wait_ms) return null; + const ts: libc.timespec = .{ .sec = 0, .nsec = 2 * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); + } +} + +fn reapShell(pid: libc.pid_t, grace_ms: u32) void { + var waited: u32 = 0; + while (waited < grace_ms) : (waited += 5) { + // Not 0 is reaped, or someone else reaped it: either way not ours. + if (libc.waitpid(pid, null, libc.W.NOHANG) != 0) return; + const ts: libc.timespec = .{ .sec = 0, .nsec = 5 * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); + } + if (libc.waitpid(pid, null, libc.W.NOHANG) != 0) return; + _ = libc.kill(pid, libc.SIG.KILL); + while (libc.waitpid(pid, null, 0) < 0 and libc.errno(-1) == .INTR) {} +} + +/// The tty's foreground job is signalled. A command pane's shell (`command`) +/// is its command too: its line runs with job control on, so the job +/// running is in a group of its own, and the shell's group goes as well, or +/// the rest of the line would run on after the job it was on. A stop (TERM +/// or KILL) of a command reaches its `&` jobs too, each in a group of its +/// own in the shell's session: Kill leaves nothing the line started. +/// The shell goes first: a job that died before it would let it run the +/// line's next command in the gap, and the gap is a scan of all of /proc +/// (signalSession), long once pids have wrapped below the old processes'. +pub fn signalTty(shell_pid: libc.pid_t, master_fd: c_int, which: pardes.PtySignal, command: bool) void { const sig = switch (which) { .int => libc.SIG.INT, .term => libc.SIG.TERM, @@ -1327,11 +2751,62 @@ pub fn signalTty(shell_pid: libc.pid_t, master_fd: c_int, which: pardes.PtySigna .kill => libc.SIG.KILL, }; const fg = tcgetpgrp(master_fd); - if (fg > 0) { - _ = libc.kill(-fg, sig); - return; + if (command and shell_pid > 0 and fg != shell_pid) _ = libc.kill(-shell_pid, sig); + if (command and shell_pid > 0 and (which == .term or which == .kill)) signalSession(shell_pid, sig); + if (fg > 0) _ = libc.kill(-fg, sig); + if (fg <= 0 and !command and shell_pid > 0) _ = libc.kill(shell_pid, sig); +} + +/// Every process group in the session `sid` leads, but its own (which the +/// caller signals last). forkpty made the shell the session's leader, and +/// job control moves a job to a new group, never out of the session. +// ponytail: Linux only (a /proc scan); elsewhere a `&` job outlives Kill, +// add sysctl KERN_PROC_SESSION on macOS if that matters. +fn signalSession(sid: libc.pid_t, sig: libc.SIG) void { + if (comptime builtin.os.tag != .linux) return; + const d = libc.opendir("/proc") orelse return; + defer _ = libc.closedir(d); + var buf: [4096]u8 = undefined; + while (libc.readdir(d)) |ent| { + const name = std.mem.sliceTo(&ent.name, 0); + const pid = std.fmt.parseInt(libc.pid_t, name, 10) catch continue; + var path_buf: [64:0]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&path_buf, "/proc/{d}/stat", .{pid}, 0) catch continue; + const ids = parseGroupSession(readProc(path, &buf) orelse continue) orelse continue; + if (ids.session == sid and ids.pgrp != sid) _ = libc.kill(-ids.pgrp, sig); } - if (shell_pid > 0) _ = libc.kill(shell_pid, sig); +} + +fn parseGroupSession(stat: []const u8) ?struct { pgrp: libc.pid_t, session: libc.pid_t } { + const close = std.mem.lastIndexOfScalar(u8, stat, ')') orelse return null; + var fields = std.mem.tokenizeAny(u8, stat[close + 1 ..], " \t\n"); + _ = fields.next() orelse return null; // 3: state + _ = fields.next() orelse return null; // 4: ppid + const pgrp = std.fmt.parseInt(libc.pid_t, fields.next() orelse return null, 10) catch return null; + const session = std.fmt.parseInt(libc.pid_t, fields.next() orelse return null, 10) catch return null; + return .{ .pgrp = pgrp, .session = session }; +} + +/// Kill: SIGTERM to the job in the tty's foreground, never to the shell. +/// The shell leads its own group, and a job it runs has another; with job +/// control off (`set +m`, a shell run as `sh -c`) the job shares the shell's +/// group, and there is no job to signal apart from it: false. +pub fn killJob(shell_pid: libc.pid_t, master_fd: c_int) bool { + const fg = tcgetpgrp(master_fd); + if (fg <= 0 or fg == shell_pid) return false; + return libc.kill(-fg, libc.SIG.TERM) == 0; +} + +/// 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). +pub fn foregroundName(shell_pid: libc.pid_t, master_fd: c_int, buf: []u8) ?[]const u8 { + if (comptime builtin.os.tag != .linux) return null; + const fg = tcgetpgrp(master_fd); + if (fg <= 0 or fg == shell_pid) 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"); + return if (comm.len > 0) comm else null; } fn readProc(path: [*:0]const u8, buf: []u8) ?[]const u8 { @@ -1599,17 +3074,11 @@ fn haveFile(path: [*:0]const u8) bool { } fn nowMs() i64 { - var ts: libc.timespec = undefined; - _ = libc.clock_gettime(.MONOTONIC, &ts); - return @as(i64, @intCast(ts.sec)) * 1000 + @divFloor(@as(i64, @intCast(ts.nsec)), 1_000_000); + return std.Io.Clock.awake.now(std.testing.io).toMilliseconds(); } fn sleepMs(ms: i64) void { - const ts = libc.timespec{ - .sec = @intCast(@divFloor(ms, 1000)), - .nsec = @intCast(@mod(ms, 1000) * 1_000_000), - }; - _ = libc.nanosleep(&ts, null); + std.testing.io.sleep(.fromMilliseconds(ms), .awake) catch {}; } test "an idle prompt is free, a foreground job takes the tty, and Ctrl-C hands it back" { @@ -1628,6 +3097,147 @@ test "an idle prompt is free, a foreground job takes the tty, and Ctrl-C hands i try std.testing.expect(sh.waitText(test_prompt, 10_000)); } +test "Kill stops the foreground job, never the shell, and finds none with job control off" { + if (comptime !tty_probe_platform) return error.SkipZigTest; + var sh = TestShell.start() orelse return error.SkipZigTest; + defer sh.stop(); + + // At the prompt the shell is the foreground: there is nothing to stop. + try std.testing.expect(!killJob(sh.pid, sh.master)); + sleepMs(100); + try std.testing.expectEqual(@as(libc.pid_t, 0), libc.waitpid(sh.pid, null, libc.W.NOHANG)); + + sh.send("sleep 30\n"); + try std.testing.expect(sh.waitTaken(true, 10_000)); + sh.forget(); + try std.testing.expect(killJob(sh.pid, sh.master)); + try std.testing.expect(sh.waitTaken(false, 10_000)); + try std.testing.expect(sh.waitText(test_prompt, 10_000)); + try std.testing.expectEqual(@as(libc.pid_t, 0), libc.waitpid(sh.pid, null, libc.W.NOHANG)); + + // Job control off: the job runs in the shell's own group. + sh.forget(); + sh.send("set +m; sleep 30\n"); + sleepMs(300); + try std.testing.expect(!killJob(sh.pid, sh.master)); + sh.forget(); + sh.send("\x03"); + try std.testing.expect(sh.waitText(test_prompt, 10_000)); + try std.testing.expectEqual(@as(libc.pid_t, 0), libc.waitpid(sh.pid, null, libc.W.NOHANG)); +} + +test "a command pane's child is its shell running the one line, and its exit status is read at the end" { + if (comptime !tty_probe_platform) return error.SkipZigTest; + const core = try pardes.Pardes.init(std.testing.allocator, .{ .tty_only = true }); + defer core.deinit(); + while (core.nextEffect()) |_| {} + const id = core.freeSlot().?; + _ = try core.newCommand(id, "", "printf 'said '; exit 7"); + while (core.nextEffect()) |_| {} + const rcs: Shell.PromptFiles = .{}; + const child = try forkShell(core, id, &rcs, "/bin/sh", "", 24, 80, null); + defer _ = libc.close(child.file.handle); + var got: [256]u8 = undefined; + var n: usize = 0; + while (n < got.len) { + const r = libc.read(child.file.handle, got[n..].ptr, got.len - n); + if (r <= 0) break; // EIO once the child is gone: the pty's end + n += @intCast(r); + } + try std.testing.expect(std.mem.indexOf(u8, got[0..n], "said") != null); + try std.testing.expectEqual(@as(?u8, 7), exitStatus(child.pid, 2000)); +} + +test "a command's exit is its process's, told by its watcher, whether or not its pty has ended" { + if (comptime !tty_probe_platform) return error.SkipZigTest; + const core = try pardes.Pardes.init(std.testing.allocator, .{ .tty_only = true }); + defer core.deinit(); + while (core.nextEffect()) |_| {} + const rcs: Shell.PromptFiles = .{}; + const Case = struct { line: []const u8, status: u8 }; + // A job left in the background holds the pty open past the command's + // exit; a command that lets go of its terminal runs on past its end of + // file. Either way the exit is the process's. + for ([_]Case{ + .{ .line = "sleep 3 & exit 4", .status = 4 }, + .{ .line = "exec </dev/null >/dev/null 2>&1; sleep 0.3; exit 5", .status = 5 }, + }) |c| { + const id = core.freeSlot().?; + _ = try core.newCommand(id, "", c.line); + while (core.nextEffect()) |_| {} + const child = try forkShell(core, id, &rcs, "/bin/sh", "", 24, 80, null); + defer _ = libc.close(child.file.handle); + try std.testing.expect(watchExit(child.pid)); + var got: ?libc.pid_t = null; + var waited: i64 = 0; + while (got == null and waited < 5000) : (waited += 10) { + got = takeExited(); + if (got == null) sleepMs(10); + } + try std.testing.expectEqual(@as(?libc.pid_t, child.pid), got); + // Told before it is reaped: the pid is still the child's. + try std.testing.expectEqual(@as(?u8, c.status), reapExited(child.pid).?.status); + try core.removePane(id, null); + } +} + +test "a command's background job outlives it and its pty, and Kill stops its whole line, its & jobs too" { + if (comptime !tty_probe_platform) return error.SkipZigTest; + if (!haveFile("/bin/sh")) return error.SkipZigTest; + const core = try pardes.Pardes.init(std.testing.allocator, .{ .tty_only = true }); + defer core.deinit(); + while (core.nextEffect()) |_| {} + const rcs: Shell.PromptFiles = .{}; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var dir_buf: [4096]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + const Case = struct { line: []const u8, kill: bool, made: bool }; + for ([_]Case{ + // The shell leads the pty's session; its exit hangs up only the + // foreground group, which the job, in a group of its own, is not. + .{ .line = "(sleep 0.4; touch left) & exit 0", .kill = false, .made = true }, + // The job running is in its own group; the shell goes too. + .{ .line = "sleep 0.4; touch killed", .kill = true, .made = false }, + // Kill stops the whole command, its `&` jobs as well. + .{ .line = "(sleep 0.4; touch killed) & sleep 5", .kill = true, .made = false }, + }) |c| { + const id = core.freeSlot().?; + _ = try core.newCommand(id, dir, c.line); + while (core.nextEffect()) |_| {} + const child = try forkShell(core, id, &rcs, "/bin/sh", dir, 24, 80, null); + if (c.kill) { + sleepMs(150); + signalTty(child.pid, child.file.handle, .term, true); + } + _ = exitStatus(child.pid, 2000); + _ = libc.close(child.file.handle); // the pane closes: the pty hangs up + sleepMs(800); + const name = if (c.kill) "killed" else "left"; + const made = if (tmp.dir.statFile(std.testing.io, name, .{})) |_| true else |_| false; + try std.testing.expectEqual(c.made, made); + try core.removePane(id, null); + } +} + +test "a retired shell that ignores the hangup is killed and reaped, not left a zombie" { + const pid = libc.fork(); + if (pid == 0) { + const argv = [_:null]?[*:0]const u8{ "/bin/sh", "-c", "trap '' HUP; exec sleep 30" }; + const envp = [_:null]?[*:0]const u8{}; + _ = libc.execve("/bin/sh", &argv, &envp); + libc._exit(127); + } + try std.testing.expect(pid > 0); + sleepMs(100); // past the trap + retireShell(pid); + // A zombie still answers kill(pid, 0); only a reaped pid is gone. + var waited: i64 = 0; + while (libc.kill(pid, @fromBackingInt(@intCast(0))) == 0 and waited < 2000) : (waited += 10) sleepMs(10); + try std.testing.expect(libc.kill(pid, @fromBackingInt(@intCast(0))) != 0); + try std.testing.expect(waited >= 90); // it did ignore the hangup +} + test "a background job is not the tty's owner" { if (comptime !tty_probe_platform) return error.SkipZigTest; var sh = TestShell.start() orelse return error.SkipZigTest; @@ -1728,3 +3338,56 @@ test "a full-screen program takes the tty until it quits" { } if (ran == 0) return error.SkipZigTest; } + +// ---- what every host's vtable does alike ---- + +/// A Save's write: the file written through the core's filesystem, the +/// pane's watch told the new text is the editor's own (so it is no outside +/// change), and "saved" said on the pane. +pub fn writeFile(core: *pardes.Pardes, watches: *file_watch.Table, pane: u8, path: []const u8, bytes: []const u8) void { + filesystem.write(core, path, bytes) catch |err| + return core.saveFailed(pane, path, err); + if (core.panes[pane]) |pn| if (pn.file) |f| if (std.mem.eql(u8, f.path, path)) { + if (watches[pane]) |*w| if (w.serial == pn.serial) switch (w.generation) { + .text => w.generation = .{ .text = std.hash.Wyhash.hash(0, bytes) }, + .pdf => {}, + }; + }; + var mbuf: [256]u8 = undefined; + core.setMessage(pane, pardes.Messages.Message.stamp(&mbuf, "saved", path)); + // The init file, saved here (Config): its settings take effect now. + pardes.exec.reapplyInitFile(core, path, bytes); +} + +/// A Dump's write, into the DumpDir. +pub fn writeDump(core: *pardes.Pardes, bytes: []const u8) void { + var pbuf: [1024:0]u8 = undefined; + const path = pardes.dump.outPath(&pbuf, core.settings.dump_dir.get()) orelse return core.dumpFailed(core.settings.dump_dir.get(), error.NoDumpDirectory); + filesystem.write(core, path, bytes) catch |err| return core.dumpFailed(path, err); + core.setLastDump(path); +} + +/// DumpThemes: the built-in themes written below the config directory. +pub fn dumpThemes(core: *pardes.Pardes, io: std.Io, gpa: std.mem.Allocator, pane: u8) void { + const config_dir = core.opts.config_dir orelse return; + const out_dir = pardes.config.User.dumpThemes(io, gpa, config_dir, pardes.themes) catch |err| { + core.reportError(pane, "dump themes", err); + return; + }; + defer gpa.free(out_dir); + var mbuf: [256]u8 = undefined; + core.setMessage(pane, pardes.Messages.Message.stamp(&mbuf, "dumped themes", out_dir)); +} + +/// A retired shell reaped once it has exited, killed once its grace is up. +pub fn reapRetired(pid: *std.c.pid_t, kill_at: *i64, now: i64) void { + if (pid.* <= 0) return; + const result = std.c.waitpid(pid.*, null, std.c.W.NOHANG); + if (result > 0 or (result < 0 and std.c.errno(result) == .CHILD)) { + pid.* = 0; + kill_at.* = 0; + } else if (kill_at.* != 0 and now >= kill_at.*) { + _ = std.c.kill(pid.*, std.c.SIG.KILL); + kill_at.* = 0; + } +} |
