From 4ffe5c93b3087f245f5b57a822ab0ecf41cc085c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 10:48:56 -0300 Subject: sessions register with the daemon: a terminal's session forked off its stub, Detach and --attach in place for terminals and windows, pty masters kept, Adopt pardes in a terminal reaches (or starts) the daemon, opens /dev/tty and forks: the child is the session, in a session of its own (setsid), drawing on that terminal (tty.Term.fd, a raw vaxis Tty made from the fd); the parent stays as its stub, the job the shell waits on. Not the daemon spawning it from a passed fd, env and argv: the user's terminals each run in a 9ns user and mount namespace, which a session spawned by a per-user daemon would not share, and a fork costs one small process's page tables where the spawn cost an IPC and an exec. GUI windows and --detach sessions just register. tty: Detach (and a terminal's end: EOF, EIO, a hangup the stub passes on, taken on the signal thread) puts the terminal back, drops it and the stub's stdio, tells the stub (which ends, exit 0), and leaves the session running with nothing drawn (Host.watched), its messages on the clock and `size` its own. pardes --attach[=name] hands its terminal through the daemon; the session enters it as the first one, sized, queried and drawn whole, and that process is its stub until it leaves. A hangup never ends a kept session; TERM and INT do, telling the daemon so the shells go with it (one async-signal-safe send). gui: Detach closes the window (its swapchain or renderer) and keeps the device, atlas and pipelines; the loop sleeps on SDL's queue; pardes-gui --attach[=name] opens a window again. Closing a window still quits. --detach is a session started detached: the tty host with no terminal yet, the GUI with a hidden window closed at once. forkShell sends the daemon a dup of every master and the hosts tell it what they close, so a session killed outright leaves its shells running; the new Adopt word takes them in as terminal panes (the Tty word's pane, its spawn taking the pty, a 1x1 size then the pane's to make it redraw). The Attach word, which switched a session's own terminal to a detached session through the frames, goes, with its effect and request in the core. PARDES_DAEMON=off (the e2e harness's) runs a session as before. test/daemon.py: 50 detach/attach cycles at a level fd count with no stale socket, kill -9 and Adopt, and nothing left; --gui the same for a window. test/fs.py: a forked session's socket is found by its own pid, and a hangup leaves a kept session running. Co-Authored-By: Claude Opus 5.5 --- src/host_io.zig | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'src/host_io.zig') diff --git a/src/host_io.zig b/src/host_io.zig index af284e84..96c790d7 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -24,7 +24,11 @@ pub const Host = struct { /// 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: the terminal or window let go, the session left running + /// for the daemon to attach again (daemon.zig). detach: ?*const fn (ctx: ?*anyopaque) void = null, + /// Adopt: the shells a dead session left, asked of the daemon. + adopt: ?*const fn (ctx: ?*anyopaque, pane: u8) 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, @@ -143,6 +147,11 @@ pub const Inbox = struct { handed: struct { seq: u32, ok: bool, words: []u8 }, files_changed, fs_ready, + /// The daemon asks for the window again (`pardes-gui --attach`). + window, + /// A shell a dead session left, its pty's master (Adopt); pid 0 is + /// the last of them told. + adopted: struct { pid: libc.pid_t, fd: c_int }, pub fn deinit(m: Msg, gpa: std.mem.Allocator, lsp_allocator: std.mem.Allocator) void { switch (m) { @@ -155,7 +164,10 @@ pub const Inbox = struct { }, .search => |b| b.deinit(gpa), .handed => |h| gpa.free(h.words), - .eof, .files_changed, .fs_ready => {}, + .adopted => |a| if (a.fd >= 0) { + _ = libc.close(a.fd); + }, + .eof, .files_changed, .fs_ready, .window => {}, } } }; @@ -2097,6 +2109,9 @@ pub fn forkShell( }; } ninep_io.setCloexec(master); + // The daemon keeps a dup: this session killed outright never hangs the + // shell up (daemon.zig). + @import("daemon.zig").pty(pid, master); 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 }; } @@ -2208,6 +2223,26 @@ const proc_vnodepathinfo = extern struct { const PROC_PIDVNODEPATHINFO: c_int = 9; extern "c" fn proc_pidinfo(pid: c_int, flavor: c_int, arg: u64, buffer: *anyopaque, buffersize: c_int) c_int; +/// Whether an adopted shell marks its prompts: one pardes started with its +/// prompt files (bash, zsh, fish), which a pane's runs wait on. +pub fn marksPrompts(pid: libc.pid_t) bool { + var buf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; + const exe = std.fs.path.basename(processExe(pid, &buf) orelse return false); + for ([_][]const u8{ "bash", "zsh", "fish" }) |shell| if (std.mem.eql(u8, exe, shell)) return true; + return false; +} + +/// The program `pid` runs, where the system says (an adopted shell's, for +/// its pane's Tty word); null elsewhere. +pub fn processExe(pid: libc.pid_t, buf: []u8) ?[]const u8 { + if (comptime builtin.os.tag != .linux) return null; + var pbuf: [64]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&pbuf, "/proc/{d}/exe", .{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)]; +} + pub fn shellCwd(pid: libc.pid_t, buf: []u8) ?[]const u8 { switch (builtin.os.tag) { .linux => { -- cgit v1.3 From 6b89fed454fd56a3af4b576646538b45d3315146 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 12:32:23 -0300 Subject: FreeBSD: the tty pardes builds for it and runs there, terminals, 9P and the daemon included Cross-compiled with -Dtarget=x86_64-freebsd.15.0 (15 ships libutil.so.10; Zig's default 14.0 links .so.9) and run on the FreeBSD 15.1 box: - 9p_io serves there: fstatat, not Linux's statx. madvise is Linux's. - WINCH reaches the sigwait thread through a handler that does nothing: FreeBSD drops a signal ignored by default as it is sent. - A dead terminal answers a write with ENXIO, which std does not expect: a Debug build traced it to stderr, that same terminal, and recursed until the stack ran out (a session whose ssh dropped hung). No tracing there. - waitid's FreeBSD numbers and 64-bit id: a command pane's exit is seen. - No /proc: a shell's directory and program by sysctl, ttyTaken by the foreground group, the foreground job's name by its program. - File watching by FreeBSD 15's inotify. - ghostty's kitty loader: off Linux a stat comes before a t=f open (a FIFO hung it); FreeBSD's shm_open refuses O_NONBLOCK. - vaxis's test terminal has no read off Linux: no input thread for it. - test-install: the test-build programs installed into /test, to run where the build cannot. Co-Authored-By: Claude Opus 5.5 --- build.zig | 28 +++++++++++++++++++++++----- src/9p_io.zig | 4 ++-- src/file_watch.zig | 17 ++++++++++------- src/host_io.zig | 49 +++++++++++++++++++++++++++++++++++++++++++------ src/main.zig | 10 +++++++++- src/memory.zig | 1 + src/tty/tty.zig | 10 +++++++++- 7 files changed, 97 insertions(+), 22 deletions(-) (limited to 'src/host_io.zig') diff --git a/build.zig b/build.zig index 034d7284..b365d265 100644 --- a/build.zig +++ b/build.zig @@ -1929,6 +1929,19 @@ pub fn build(b: *std.Build) void { } } } + + // The test-build programs installed rather than run, for a machine the + // build cannot run them on (`-Dtarget=x86_64-freebsd.15.0`): each into + // /test, named for its root file. + const test_install = b.step("test-install", "install the test-build programs into /test, to run elsewhere"); + for (test_build.dependencies.items) |dep| { + const t = dep.cast(std.Build.Step.Compile) orelse continue; + const name = switch (t.root_module.root_source_file orelse continue) { + .src_path => |p| std.fs.path.stem(p.sub_path), + else => t.name, + }; + test_install.dependOn(&b.addInstallArtifact(t, .{ .dest_dir = .{ .override = .{ .custom = "test" } }, .dest_sub_path = name }).step); + } } fn testMatch(b: *std.Build, profiler: *std.Build.Step.Compile, filters: []const []const u8, step: *std.Build.Step) ?*std.Build.Step.Run { @@ -1964,7 +1977,8 @@ fn installHome(b: *std.Build, artifact: *std.Build.Step.Compile, home_bin: ?[]co /// file: ghostty opens first and checks after, and the open of a FIFO /// waits for a writer (pardes hung) while a device's open may do /// something. On Linux an O_PATH probe looks, which opens nothing, and -/// the same inode is reopened through /proc/self/fd. +/// the same inode is reopened through /proc/self/fd; elsewhere a stat by +/// path looks first. /// - shared memory is opened non-blocking and must be a regular file, so /// a FIFO in /dev/shm (or a symlink there to one) neither hangs nor maps. /// - an image may be no larger than a screen's storage @@ -1990,14 +2004,15 @@ fn patchGhosttyKitty(b: *std.Build, io: std.Io, dep: *std.Build.Dependency, vt: .{ " const fd = std.c.shm_open(pathz, @as(c_int, @bitCast(std.c.O{ .ACCMODE = .RDONLY })), @as(u16, 0));", \\ // pardes (build.zig): a FIFO there must not hang the open, and - \\ // on Linux nothing but a regular file is opened at all. + \\ // on Linux nothing but a regular file is opened at all. FreeBSD's + \\ // names are no files, and its shm_open refuses O_NONBLOCK. \\ if (comptime builtin.target.os.tag == .linux) if (path.len > 0 and path[0] == '/') { \\ var dev: [std.Io.Dir.max_path_bytes]u8 = undefined; \\ const full = std.fmt.bufPrint(&dev, "/dev/shm{s}", .{path}) catch return error.InvalidData; \\ const shm_stat = std.Io.Dir.cwd().statFile(io, full, .{}) catch return error.InvalidData; \\ if (shm_stat.kind != .file) return error.InvalidData; \\ }; - \\ const fd = std.c.shm_open(pathz, @as(c_int, @bitCast(std.c.O{ .ACCMODE = .RDONLY, .NONBLOCK = true })), @as(u16, 0)); + \\ const fd = std.c.shm_open(pathz, @as(c_int, @bitCast(std.c.O{ .ACCMODE = .RDONLY, .NONBLOCK = builtin.target.os.tag != .freebsd })), @as(u16, 0)); }, .{ " if (stat.size <= 0) return error.InvalidData;", @@ -2011,9 +2026,12 @@ fn patchGhosttyKitty(b: *std.Build, io: std.Io, dep: *std.Build.Dependency, vt: const helper = \\ \\/// pardes (build.zig): `path`, opened for reading only once it is known - \\/// to be a regular file. Elsewhere than Linux, ghostty's open as it was. + \\/// to be a regular file. Elsewhere than Linux, a stat by path first. \\fn pardesOpenRegular(io: std.Io, path: []const u8) !std.Io.File { - \\ if (comptime builtin.target.os.tag != .linux) return std.Io.Dir.cwd().openFile(io, path, .{}); + \\ if (comptime builtin.target.os.tag != .linux) { + \\ if ((try std.Io.Dir.cwd().statFile(io, path, .{})).kind != .file) return error.NotRegularFile; + \\ return std.Io.Dir.cwd().openFile(io, path, .{}); + \\ } \\ const probe = try std.Io.Dir.cwd().openFile(io, path, .{ .path_only = true }); \\ defer probe.close(io); \\ const stat = try probe.stat(io); diff --git a/src/9p_io.zig b/src/9p_io.zig index 2a0061fd..174d3f89 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -12,7 +12,7 @@ const quic = if (quic_enabled) @import("9p_quic.zig") else struct {}; const log = std.log.scoped(.ninep); pub const darwin = builtin.os.tag.isDarwin(); -pub const supported = builtin.os.tag == .linux or darwin; +pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd or darwin; pub const sun_path_len = @typeInfo(@FieldType(libc.sockaddr.un, "path")).array.len; pub fn setCloexec(fd: c_int) void { @@ -29,7 +29,7 @@ pub fn socketDir(buf: *[sun_path_len:0]u8) ?[:0]const u8 { pub const FileFacts = struct { mode: u32, uid: libc.uid_t }; pub fn statNoFollow(path: [:0]const u8) ?FileFacts { - if (comptime darwin) { + if (comptime builtin.os.tag != .linux) { var stat: libc.Stat = undefined; if (libc.fstatat(libc.AT.FDCWD, path, &stat, libc.AT.SYMLINK_NOFOLLOW) != 0) return null; return .{ .mode = stat.mode, .uid = stat.uid }; diff --git a/src/file_watch.zig b/src/file_watch.zig index e3fe9963..9b41d146 100644 --- a/src/file_watch.zig +++ b/src/file_watch.zig @@ -5,9 +5,10 @@ //! is their generation. Large PDFs use metadata which changes for in-place and //! rename-over saves, avoiding a second whole-document allocation. //! -//! Two kernels, one transaction: inotify on linux, kqueue on macos. `init`, -//! `wait`, `stop`, `drain`, `markDir` and `unmarkDir` are the whole of that -//! difference, and nothing above them knows which one it is on. +//! Two kernels, one transaction: inotify on linux (and FreeBSD 15's, the +//! same events), kqueue on macos. `init`, `wait`, `stop`, `drain`, `markDir` +//! and `unmarkDir` are the whole of that difference, and nothing above them +//! knows which one it is on. const std = @import("std"); const builtin = @import("builtin"); const libc = std.c; @@ -81,7 +82,7 @@ pub const Table = [shader_slot + pardes.config.Runtime.Post.max]?Watch; /// empty and `reloadPane` returns false for every slot. The core then keeps its /// own record of what was asked and simply never gets a `file_changed`, which /// is what a host with no watcher has always done here. -pub const supported = builtin.os.tag == .linux or builtin.os.tag == .macos; +pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd or builtin.os.tag == .macos; /// The wake filter's `ident` on macos. `ident` is a directory descriptor for /// every other filter on this queue, and no descriptor is `maxInt(usize)`, so @@ -113,6 +114,8 @@ pub fn init(polled: bool) c_int { linux.IN.CLOEXEC | linux.IN.NONBLOCK else linux.IN.CLOEXEC), + // FreeBSD's IN_CLOEXEC and IN_NONBLOCK are its O_ flags. + .freebsd => return libc.inotify_init1(if (polled) 0x00100000 | 0x4 else 0x00100000), .macos => { const kq = libc.kqueue(); if (kq < 0) return -1; @@ -193,7 +196,7 @@ pub fn stop(fd: c_int) void { pub fn drain(fd: c_int) bool { if (fd < 0) return false; switch (builtin.os.tag) { - .linux => { + .linux, .freebsd => { var buf: [4096]u8 = undefined; var seen = false; while (libc.read(fd, &buf, buf.len) > 0) seen = true; @@ -227,7 +230,7 @@ pub fn drain(fd: c_int) bool { /// and inode, because each EVFILT_VNODE filter needs a descriptor of its own. fn markDir(fd: c_int, dir_z: [:0]const u8, watches: *const Table) c_int { switch (builtin.os.tag) { - .linux => { + .linux, .freebsd => { // CLOSE_WRITE coalesces one writer's writes; MOVED_TO and CREATE // cover rename-over and delete-then-recreate saves; DELETE and // MOVED_FROM a file going away, said at once (File.deleted), not @@ -366,7 +369,7 @@ fn unmark(fd: c_int, old: Watch, drop_dir: bool) void { } if (!drop_dir) return; switch (builtin.os.tag) { - .linux => _ = libc.inotify_rm_watch(fd, old.wd), + .linux, .freebsd => _ = libc.inotify_rm_watch(fd, old.wd), .macos => _ = libc.close(old.wd), else => {}, } diff --git a/src/host_io.zig b/src/host_io.zig index 96c790d7..99ada59b 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -2235,6 +2235,10 @@ pub fn marksPrompts(pid: libc.pid_t) bool { /// The program `pid` runs, where the system says (an adopted shell's, for /// its pane's Tty word); null elsewhere. pub fn processExe(pid: libc.pid_t, buf: []u8) ?[]const u8 { + if (comptime builtin.os.tag == .freebsd) { + const n = kernProc(pid, 12, buf) orelse return null; // KERN_PROC_PATHNAME + return if (n > 1) buf[0 .. n - 1] else null; + } if (comptime builtin.os.tag != .linux) return null; var pbuf: [64]u8 = undefined; const path = std.fmt.bufPrintSentinel(&pbuf, "/proc/{d}/exe", .{pid}, 0) catch return null; @@ -2268,10 +2272,28 @@ pub fn shellCwd(pid: libc.pid_t, buf: []u8) ?[]const u8 { @memcpy(buf[0..path.len], path); return buf[0..path.len]; }, + .freebsd => { + // A struct kinfo_file (sys/user.h), its last 1024 bytes the path. + var kf: [1392]u8 align(8) = undefined; + const n = kernProc(pid, 42, &kf) orelse return null; // KERN_PROC_CWD + if (n <= 368) return null; + const path = std.mem.sliceTo(kf[368..n], 0); + if (path.len == 0 or path.len >= buf.len) return null; + @memcpy(buf[0..path.len], path); + return buf[0..path.len]; + }, else => return null, } } +/// FreeBSD's kern.proc.. into `buf`: the bytes it wrote. +fn kernProc(pid: libc.pid_t, what: c_int, buf: []u8) ?usize { + const mib = [_]c_int{ 1, 14, what, pid }; // CTL_KERN, KERN_PROC + var len: usize = buf.len; + if (libc.sysctl(&mib, mib.len, buf.ptr, &len, null, 0) != 0) return null; + return len; +} + /// Where a terminal is. The process forked for it, while it still runs the /// shell pardes started (`shell_exe`), as ever: a job it runs, in another /// directory (`(cd sub && make)`, a nested pardes), never names the pane. @@ -2478,6 +2500,13 @@ pub fn ttyTaken(shell_pid: libc.pid_t, master_fd: c_int) bool { if (fg == shell_pid) return false; return darwin.taken(shell_pid, fg); }, + // ponytail: any foreground job but the shell's own group, so a + // nested interactive shell counts as taken; walk the group + // (KERN_PROC_PGRP) as darwin does if that matters. + .freebsd => { + const fg = tcgetpgrp(master_fd); + return fg > 0 and fg != shell_pid; + }, else => return false, } } @@ -2586,10 +2615,16 @@ pub fn retireShell(pid: libc.pid_t) void { // it -- until the host, told through `takeExited`, reaps it in its own loop // with `exitStatus` and tells the core. -const waitid_p_pid: c_int = 1; -const waitid_wexited: c_int = 4; -const waitid_wnowait: c_int = if (builtin.os.tag == .linux) 0x01000000 else 0x20; -extern "c" fn waitid(idtype: c_int, id: c_uint, infop: *anyopaque, options: c_int) c_int; +const freebsd = builtin.os.tag == .freebsd; +const waitid_p_pid: c_int = if (freebsd) 0 else 1; +const waitid_wexited: c_int = if (freebsd) 16 else 4; +const waitid_wnowait: c_int = switch (builtin.os.tag) { + .linux => 0x01000000, + .freebsd => 8, + else => 0x20, +}; +// FreeBSD's id_t is 64 bits. +extern "c" fn waitid(idtype: c_int, id: if (freebsd) i64 else c_uint, infop: *anyopaque, options: c_int) c_int; var exited: [pardes.MAX_PANES * 2]std.atomic.Value(libc.pid_t) = @splat(.init(0)); var exit_lock: libc.pthread_mutex_t = .{}; @@ -2833,11 +2868,13 @@ pub fn killJob(shell_pid: libc.pid_t, master_fd: c_int) bool { } /// The command name of the tty's foreground job, when that is not the -/// shell: what `pty/run` says is running. Linux only (/proc//comm). +/// shell: what `pty/run` says is running. /proc//comm on Linux, the +/// program's name on FreeBSD. pub fn foregroundName(shell_pid: libc.pid_t, master_fd: c_int, buf: []u8) ?[]const u8 { - if (comptime builtin.os.tag != .linux) return null; + if (comptime builtin.os.tag != .linux and builtin.os.tag != .freebsd) return null; const fg = tcgetpgrp(master_fd); if (fg <= 0 or fg == shell_pid) return null; + if (comptime builtin.os.tag == .freebsd) return std.fs.path.basename(processExe(fg, buf) orelse return null); var name: [64:0]u8 = undefined; const path = std.fmt.bufPrintSentinel(&name, "/proc/{d}/comm", .{@as(u32, @intCast(fg))}, 0) catch return null; const comm = std.mem.trimEnd(u8, readProc(path, buf) orelse return null, "\n"); diff --git a/src/main.zig b/src/main.zig index 803ff4da..3d26d1fd 100644 --- a/src/main.zig +++ b/src/main.zig @@ -21,7 +21,15 @@ else // the thread starts, so std's 256 KiB made each 9P connection's thread cost // that much resident. A stack-overflow segfault on a thread still printed its // whole symbolized trace from 32 KiB up (16 KiB double-faulted): 64 KiB. -pub const std_options: std.Options = .{ .logFn = logFn, .signal_stack_size = 64 * 1024 }; +// +// FreeBSD answers a write to a terminal that is gone with ENXIO, an errno std +// does not expect: a Debug build traced it to stderr, that same terminal, +// and recursed until the stack ran out (a session whose ssh dropped hung). +pub const std_options: std.Options = .{ + .logFn = logFn, + .signal_stack_size = 64 * 1024, + .unexpected_error_tracing = (std.Options{}).unexpected_error_tracing and builtin.os.tag != .freebsd, +}; fn logFn( comptime level: std.log.Level, diff --git a/src/memory.zig b/src/memory.zig index cb75ee0e..47bd755d 100644 --- a/src/memory.zig +++ b/src/memory.zig @@ -442,6 +442,7 @@ pub fn trim() void { /// 71 MiB that way. Only the region given, never the whole process (a prctl /// would reach every shell and server pardes starts). pub fn sparse(region: []u8) void { + if (comptime builtin.os.tag != .linux) return; advise(region, std.posix.MADV.NOHUGEPAGE); } diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 37f66456..31f62d34 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -122,6 +122,8 @@ fn stopInput(loop: *Loop) void { } fn inputThread(loop: *Loop, cache: *vaxis.GraphemeCache) void { + // vaxis's test terminal reads nothing off Linux: it has no read at all. + if (comptime !@hasDecl(vaxis.Tty, "read")) return; inputReader(loop, loop.tty, cache); } @@ -1185,6 +1187,10 @@ pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void { // Kept by a daemon, a hangup is the terminal's end, not the session's: // winchWatch takes it, and the session carries on detached. if (daemon.link >= 0) posix.sigaddset(&winch_set, posix.SIG.HUP); + // FreeBSD drops a signal whose action is to be ignored (WINCH's + // default) as it is sent, sigwait or not: a handler keeps it. + if (comptime builtin.os.tag == .freebsd) + posix.sigaction(.WINCH, &.{ .handler = .{ .handler = keepSignal }, .mask = posix.sigemptyset(), .flags = 0 }, null); posix.sigprocmask(posix.SIG.BLOCK, &winch_set, null); var tty_buf: [0x10000]u8 = undefined; @@ -2450,7 +2456,7 @@ fn lspStatusSink(ctx: ?*anyopaque, text: []const u8) void { } fn watchFiles(io: std.Io, fd: c_int, loop: *Loop) anyerror!void { - if (comptime builtin.os.tag != .linux) { + if (comptime builtin.os.tag != .linux and builtin.os.tag != .freebsd) { while (file_watch.wait(fd)) loop.postEvent(.files_changed) catch break; return; } @@ -2465,6 +2471,8 @@ fn watchFiles(io: std.Io, fd: c_int, loop: *Loop) anyerror!void { } } +fn keepSignal(_: posix.SIG) callconv(.c) void {} + fn winchWatch(loop: *Loop, vx: *vaxis.Vaxis, tty: *vaxis.Tty) void { var set = posix.sigemptyset(); posix.sigaddset(&set, posix.SIG.WINCH); -- cgit v1.3 From cc26fe74e092803ac2977801bf6510e05b394c29 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 19:36:11 -0300 Subject: web: a dump is a .dump by name everywhere (the download, the embedded fixture, -l's help); the touch check resizes a pane by its grip, there being no rule between panes Co-Authored-By: Claude Opus 5.5 --- build.zig | 2 +- src/host_io.zig | 2 +- src/main.zig | 2 +- src/web.zig | 4 ++-- test/web-snapshots/touch.mjs | 9 +++++---- 5 files changed, 10 insertions(+), 9 deletions(-) (limited to 'src/host_io.zig') diff --git a/build.zig b/build.zig index 097fb1bf..c0235e41 100644 --- a/build.zig +++ b/build.zig @@ -1111,7 +1111,7 @@ pub fn build(b: *std.Build) void { if (is_web) { const dp = dump_path.?; - root_mod.addAnonymousImport("embedded.dump.zon", .{ + root_mod.addAnonymousImport("embedded.dump", .{ .root_source_file = if (std.fs.path.isAbsolute(dp)) .{ .cwd_relative = dp } else b.path(dp), }); } diff --git a/src/host_io.zig b/src/host_io.zig index 99ada59b..7b0fe9a2 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -70,7 +70,7 @@ pub fn testClock() ?u64 { } pub const Fallback = struct { - pub const dump_path = "pardes.dump.zon"; + pub const dump_path = "pardes.dump"; gpa: std.mem.Allocator, files: std.StringHashMapUnmanaged([]u8) = .empty, diff --git a/src/main.zig b/src/main.zig index 0708e9d7..ac2226ae 100644 --- a/src/main.zig +++ b/src/main.zig @@ -73,7 +73,7 @@ const help_text = \\ -n initial shell panes: 1 (default) or 3 (classic). \\ A FILE argument boots just that file instead. \\ --tty-toggle use Ctrl- to toggle tty/editor mode - \\ -l load a dump of another instance (see Dump) + \\ -l load a dump of another instance (see Dump) \\ -w, --wait inside a pardes pane, hand FILE to the outer \\ session and return only once the pane it opened \\ (or already had) is deleted: 0 then, 1 if the diff --git a/src/web.zig b/src/web.zig index fd6371c0..5e5a0d74 100644 --- a/src/web.zig +++ b/src/web.zig @@ -13,7 +13,7 @@ const pardes = @import("pardes.zig"); const mouse = @import("mouse.zig"); const gpa = std.heap.wasm_allocator; -const embedded_dump = @embedFile("embedded.dump.zon"); +const embedded_dump = @embedFile("embedded.dump"); const input_capacity = 64 * 1024; const attr_tagline: u16 = 1 << 7; @@ -484,7 +484,7 @@ fn writeFile(_: ?*anyopaque, _: u8, path: []const u8, bytes: []const u8) void { } fn writeDump(_: ?*anyopaque, bytes: []const u8) void { - const name = "pardes-dump.zon"; + const name = "pardes.dump"; host_download(name.ptr, name.len, bytes.ptr, bytes.len); } diff --git a/test/web-snapshots/touch.mjs b/test/web-snapshots/touch.mjs index 3793c0f9..6ced7a29 100644 --- a/test/web-snapshots/touch.mjs +++ b/test/web-snapshots/touch.mjs @@ -192,14 +192,15 @@ export default async function ({ grid, touch, wait, stable, delay, evaluate, mou assert.deepEqual(reflected.lines.slice(0, sharedRows), original.lines.slice(0, sharedRows), "Look must read the source pane body"); const center = Math.floor(frame.cols / 2); - const separator = Math.max(original.top, reflected.top) - 1; + // No rule between panes: the lower one's grip, its tag's gutter, moves it. + const separator = Math.max(original.top, reflected.top); const distance = original.top < reflected.top ? 4 : -4; - finger = await touch("down", 2, center, separator, null); + finger = await touch("down", 2, 0, separator, null); for (let step = 1; step <= 8; step++) { - finger = await touch("move", 2, center, separator + distance * step / 8, finger); + finger = await touch("move", 2, 0, separator + distance * step / 8, finger); await delay(20); } - await touch("up", 2, center, separator + distance, finger); + await touch("up", 2, 0, separator + distance, finger); await stable(100, 5000); frame = await grid(); assert.equal(tags(frame).length, 2); -- cgit v1.3 From ddcfd860ef05c37f5536038d8828cc81c0c4125c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 22:18:34 -0300 Subject: upgrade: the Upgrade word (SPC s U, a tag, 9P) moves a running session onto the pardes installed where it started, in place, by exec at a step boundary: its dump in the DumpDir, its pty masters, terminals, stubs and daemon link kept open and named on the new command line, every view and running shell its own again; the daemon re-execs too, its registry on its command line; a build that cannot load the dump falls back to the old binary, or starts afresh with the shells adopted and the dump kept (test/daemon.py --upgrade, --upgrade --gui) Co-Authored-By: Claude Opus 5.5 --- docs/typ/building.typ | 13 ++ docs/typ/builtins-gui.json | 1 + docs/typ/builtins-tty.json | 1 + docs/typ/guide.typ | 7 +- src/9p_io.zig | 33 ++-- src/builtins.zig | 13 ++ src/config.zig | 1 + src/daemon.zig | 145 ++++++++++++++++- src/dump.zig | 71 +++++++-- src/gui/gui.zig | 120 +++++++++++++- src/host_io.zig | 3 + src/main.zig | 42 +++-- src/pardes.zig | 8 + src/tty/tty.zig | 220 ++++++++++++++++++++++--- src/upgrade.zig | 389 +++++++++++++++++++++++++++++++++++++++++++++ test/daemon.py | 315 +++++++++++++++++++++++++++++++++++- 16 files changed, 1316 insertions(+), 66 deletions(-) create mode 100644 src/upgrade.zig (limited to 'src/host_io.zig') diff --git a/docs/typ/building.typ b/docs/typ/building.typ index e42702c6..d8c089f6 100644 --- a/docs/typ/building.typ +++ b/docs/typ/building.typ @@ -236,6 +236,19 @@ hands an accepted connection to its session (`SCM_RIGHTS`), which welcomes it and from then on is the one it talks to. Today the session answers pings and nothing more. +#word("Upgrade") (`src/upgrade.zig`) execs the path the session was started +from, at a step boundary, after asking it `--version`: the session's dump +goes to the DumpDir (`pardes-upgrade-.dump`), and its pty masters, +terminals, stubs and daemon link stay open across the exec, named in a +record on the new command line (`--upgraded=`) ahead of the arguments it +was started with. The pid stays, so the shells stay its children; the new +process loads the dump with every view and takes the fds back. One that +cannot load it execs the old binary again (kept open from +`/proc/self/exe`), or with none starts afresh with the shells adopted and +the dump left for a Restore. The daemon re-execs the same way, its +registry on its command line. `python3 -B test/daemon.py --upgrade +` (and `--upgrade --gui`) is the end-to-end test. + = 9P The wire format, the client and server connections, the file-server engine diff --git a/docs/typ/builtins-gui.json b/docs/typ/builtins-gui.json index 9db9e396..e731b75e 100644 --- a/docs/typ/builtins-gui.json +++ b/docs/typ/builtins-gui.json @@ -152,6 +152,7 @@ {"name": "Undo", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "acme's Undo and Redo: step the pane's body back or forward through its edits, from a tag, a pane ctl or exec."}, {"name": "Unmount", "leader": null, "also": [], "arg": true, "scope": "root", "setting": false, "choices": null, "doc": "Unmount the 9P tree mounted under /n/."}, {"name": "Up", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Move the keyboard to the pane above, and from a column's top pane to its tag."}, + {"name": "Upgrade", "leader": "SPC s U", "also": [], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Move this session onto the pardes installed since it started, in place: its text, undo, views, terminals with their running shells, and its terminals and windows come along."}, {"name": "Verbose", "leader": null, "also": [], "arg": true, "scope": "root", "setting": true, "choices": "on, off", "doc": "A builtin says its own name on the message row as it runs, on or off."}, {"name": "Vignette", "leader": null, "also": [], "arg": true, "scope": "root", "setting": true, "choices": "on, off, 0-3", "doc": "The window's corners fall into shade: a level, 0 (off) through 3."}, {"name": "Window", "leader": "SPC w", "also": ["C-w"], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Window mode: h/j/k/l move an outline between the panes; Enter or Esc focuses the outlined pane, `:` its tag, the letter shown on a pane that one; d is Del on it (then k or j between two), c Collapse."}, diff --git a/docs/typ/builtins-tty.json b/docs/typ/builtins-tty.json index 151c04a6..fe81b3fa 100644 --- a/docs/typ/builtins-tty.json +++ b/docs/typ/builtins-tty.json @@ -135,6 +135,7 @@ {"name": "Undo", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "acme's Undo and Redo: step the pane's body back or forward through its edits, from a tag, a pane ctl or exec."}, {"name": "Unmount", "leader": null, "also": [], "arg": true, "scope": "root", "setting": false, "choices": null, "doc": "Unmount the 9P tree mounted under /n/."}, {"name": "Up", "leader": null, "also": [], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Move the keyboard to the pane above, and from a column's top pane to its tag."}, + {"name": "Upgrade", "leader": "SPC s U", "also": [], "arg": false, "scope": "root", "setting": false, "choices": null, "doc": "Move this session onto the pardes installed since it started, in place: its text, undo, views, terminals with their running shells, and its terminals and windows come along."}, {"name": "Verbose", "leader": null, "also": [], "arg": true, "scope": "root", "setting": true, "choices": "on, off", "doc": "A builtin says its own name on the message row as it runs, on or off."}, {"name": "Window", "leader": "SPC w", "also": ["C-w"], "arg": false, "scope": "pane", "setting": false, "choices": null, "doc": "Window mode: h/j/k/l move an outline between the panes; Enter or Esc focuses the outlined pane, `:` its tag, the letter shown on a pane that one; d is Del on it (then k or j between two), c Collapse."}, {"name": "Wrap", "leader": "SPC t w", "also": [], "arg": true, "scope": "root", "setting": true, "choices": "on, off", "doc": "Long lines wrap onto the next row, on or off."}, diff --git a/docs/typ/guide.typ b/docs/typ/guide.typ index 0b721658..4430276f 100644 --- a/docs/typ/guide.typ +++ b/docs/typ/guide.typ @@ -358,7 +358,12 @@ one lets only that one go. A session is named by `--9p=` or `--detach=`, else its pid. A session killed outright leaves its shells running; #word("Adopt") in another takes them in as terminal panes, what they showed gone. One `pardes --daemon` per user keeps all of it, and -leaves once nothing is kept. +leaves once nothing is kept. After a rebuild, #word("Upgrade") +(#key("SPC s U")) moves a running session onto the pardes now installed +where it was started from, in place: its text and undo, its views, its +terminals with their running shells, and its terminals and windows come +along, and the daemon moves too. A build that cannot read the session +hands it back to the one that ran it. = Config diff --git a/src/9p_io.zig b/src/9p_io.zig index 9bcb2827..ef6385fc 100644 --- a/src/9p_io.zig +++ b/src/9p_io.zig @@ -902,6 +902,25 @@ pub const Listener = struct { l.posted_len = 0; } + /// What the connections have still to send let go, 200 ms at most, + /// before they are cut (a quit, an Upgrade's exec: its write's answer). + pub fn flushOut(l: *Listener) void { + if (comptime !supported) return; + const deadline = nowMs() +| 200; + while (nowMs() < deadline) { + const pending = for (&l.runner.conns) |*conn| { + if (!conn.live()) continue; + conn.lock(); + const n = conn.engine.output().len; + conn.unlock(); + if (n != 0) break true; + } else false; + if (!pending) break; + Client.nap(1); + } + Client.nap(1); // the last bytes leave the stream's own buffer + } + pub fn deinit(l: *Listener, gpa: std.mem.Allocator) void { l.stopping.store(true, .release); l.unpostFromRegistry(); @@ -922,19 +941,7 @@ pub const Listener = struct { l.edit_watchers.cancel(l.io); // An answer given as the editor quit (a Kill written to /ctl) // may still be on its way out: let it go before the cut. - const deadline = nowMs() +| 200; - while (nowMs() < deadline) { - const pending = for (&l.runner.conns) |*conn| { - if (!conn.live()) continue; - conn.lock(); - const n = conn.engine.output().len; - conn.unlock(); - if (n != 0) break true; - } else false; - if (!pending) break; - Client.nap(1); - } - Client.nap(1); // the last bytes leave the stream's own buffer + l.flushOut(); l.runner.stop(); pardes.turn.wake(); } diff --git a/src/builtins.zig b/src/builtins.zig index 0ad59fd4..1dc428da 100644 --- a/src/builtins.zig +++ b/src/builtins.zig @@ -762,6 +762,19 @@ pub const Adopt = struct { } }; +/// Move this session onto the pardes installed since it started, in place: +/// its text, undo, views, terminals with their running shells, and its +/// terminals and windows come along. The pardes daemon moves too. +pub const Upgrade = struct { + pub const scope: Scope = .session; + pub const enabled = pardes.can_attach; + pub fn run(c: Ctx) void { + if (comptime !enabled) unreachable; + const upgrade = c.p.host.vtable.upgrade orelse return c.p.reportFailure(c.id, "Upgrade: this pardes cannot move to another build in place"); + upgrade(c.p.host.ctx, @intCast(c.id)); + } +}; + /// Mount another pardes's 9P tree under /n/, given its dial address. pub const Mount = struct { pub const scope: Scope = .session; diff --git a/src/config.zig b/src/config.zig index 13404461..742e9010 100644 --- a/src/config.zig +++ b/src/config.zig @@ -219,6 +219,7 @@ pub const leader_path = paths: { if (pardes.can_attach) { table.set(.Detach, "sD"); table.set(.Adopt, null); + table.set(.Upgrade, "sU"); } if (builtins.Peek.enabled) { table.set(.Peek, null); diff --git a/src/daemon.zig b/src/daemon.zig index 702d11b1..43f15c6c 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -27,6 +27,7 @@ const libc = std.c; const posix = std.posix; const ninep_io = @import("9p_io.zig"); const wire = @import("detached/wire.zig"); +const upgrade = @import("upgrade.zig"); const log = std.log.scoped(.replica); pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd; @@ -355,6 +356,33 @@ pub fn adopt() void { tell("adopt", &.{}); } +/// This session is about to exec another build (upgrade.zig): the daemon +/// is asked to move too, and every replica told why it is let go. +/// ponytail: a pong its thread writes at the same moment may split the +/// words; the client reconnects either way. +pub fn upgraded() void { + tell("upgrade", &.{}); + var out: [600]u8 = undefined; + const said = wire.encodeServer(&out, .{ .refused = "the session is upgrading to another build of pardes: connect again with that build" }) catch return; + for (&replica_fds) |*slot| { + const fd = slot.load(.acquire); + if (fd < 0) continue; + _ = writeAll(fd, said); + _ = libc.shutdown(fd, 2); + } +} + +/// An upgraded session's link and stub as its old process left them: its +/// build's hello said again on the link (the daemon keeps its masters as +/// they were), and the stub's death a HUP again. +pub fn resumeLink(fd: c_int, stub_fd: c_int, k: Kind, named: []const u8, detached: bool) void { + if (comptime !supported) return; + stub = stub_fd; + if (stub >= 0) hupOnStubDeath(true); + setName(named); + if (fd >= 0 and hello(fd, k, libc.getpid(), name(), detached)) link = fd else if (fd >= 0) _ = libc.close(fd); +} + pub const Note = union(enum) { /// A terminal for a detached tty session (`pardes --attach`), and the /// stub's pipe; for a window session, both -1: open a window. @@ -422,6 +450,8 @@ fn writeAll(fd: c_int, bytes: []const u8) bool { return true; } +var replica_fds: [max_replicas]std.atomic.Value(c_int) = @splat(.init(-1)); + /// A replica the daemon handed over, its hello accepted: welcomed, its pings /// answered, until it says bye or hangs up. ponytail: phase 5 proper makes it /// a replica (the log, the dump, terminal state); this stub is what times a @@ -429,6 +459,11 @@ fn writeAll(fd: c_int, bytes: []const u8) bool { /// than its buffer ends it. fn serveReplica(fd: c_int) void { defer _ = libc.close(fd); + // Where an upgrade finds it to say why it ends (`upgraded`). + const slot = for (&replica_fds) |*r| { + if (r.cmpxchgStrong(-1, fd, .acq_rel, .acquire) == null) break r; + } else null; + defer if (slot) |r| r.store(-1, .release); log.info("a replica connected to '{s}'", .{name()}); defer log.info("a replica of '{s}' left", .{name()}); var out: [wire.hello_max]u8 = undefined; @@ -698,6 +733,8 @@ const Daemon = struct { conns: [max_conns]Conn = @splat(.{}), held: [max_held]Held = @splat(.{}), replicas: [max_replicas]Replica = @splat(.{}), + /// A session asked it to move onto the build installed since (`reexec`). + upgrade_asked: bool = false, fn idle(d: *const Daemon) bool { for (d.conns) |c| if (c.fd >= 0) return false; @@ -776,6 +813,10 @@ const Daemon = struct { for (&d.held) |*h| if (h.fd >= 0 and h.owner == i) d.drop(h); return; } + if (std.mem.eql(u8, verb, "upgrade")) { + d.upgrade_asked = true; + return; + } if (std.mem.eql(u8, verb, "adopt")) { var line: [msg_max]u8 = undefined; var w: std.Io.Writer = .fixed(&line); @@ -920,6 +961,93 @@ const Daemon = struct { _ = libc.close(fd); } + /// Upgrade: this daemon exec'd as the pardes now at the path it was + /// started from, on the same listeners and lock, every session's link + /// and every master kept open, its registry on the new command line + /// (`restore`). A replica whose hello is still on its way is let go: it + /// dials again. Returns when it cannot be done, as it was. + fn reexec(d: *Daemon, replicas: c_int) void { + const path = upgrade.exe() orelse return; + var why: [700]u8 = undefined; + switch (upgrade.check(path, &why)) { + .no => |w| return log.warn("{s}", .{w}), + .ok => {}, + } + var text: [flag_room]u8 = undefined; + var w: std.Io.Writer = .fixed(&text); + w.print("{s}replicas {d}\n", .{ upgrade.flag, replicas }) catch return; + for (&d.conns, 0..) |*c, i| if (c.fd >= 0) { + const k: []const u8 = if (c.kind) |k| @tagName(k) else "-"; + w.print("c {d} {d} {s} {d} {d} {x} {s}\n", .{ i, c.fd, k, c.pid, @intFromBool(c.detached), c.semantics, c.named() }) catch return; + }; + for (&d.held) |*h| if (h.fd >= 0) { + w.print("h {d} {d} {d}\n", .{ h.fd, h.pid, h.owner orelse 255 }) catch return; + }; + w.writeByte(0) catch return; + for (&d.replicas) |*r| if (r.fd >= 0) { + _ = libc.close(r.fd); + r.* = .{}; + }; + const keep = struct { + fn all(dd: *Daemon, rep: c_int, on: bool) void { + const v: c_int = if (on) 0 else 1; + for ([_]c_int{ 3, 4, rep }) |fd| if (fd >= 0) { + _ = libc.fcntl(fd, libc.F.SETFD, v); + }; + for (&dd.conns) |*c| if (c.fd >= 0) { + _ = libc.fcntl(c.fd, libc.F.SETFD, v); + }; + for (&dd.held) |*h| if (h.fd >= 0) { + _ = libc.fcntl(h.fd, libc.F.SETFD, v); + }; + } + }.all; + const argv = [_:null]?[*:0]const u8{ path, "--daemon", @ptrCast(&text), null }; + keep(d, replicas, true); + _ = libc.execve(path, &argv, @ptrCast(libc.environ)); + keep(d, replicas, false); + log.warn("upgrade: {s} would not start", .{path}); + } + + /// The registry an upgraded daemon was exec'd with (`reexec`); the + /// replicas' listener, its fd, or -1. + fn restore(d: *Daemon, text: []const u8) c_int { + var replicas: c_int = -1; + var lines = std.mem.splitScalar(u8, text, '\n'); + while (lines.next()) |line| { + var words = std.mem.tokenizeScalar(u8, line, ' '); + const what = words.next() orelse continue; + if (std.mem.eql(u8, what, "replicas")) { + replicas = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch -1; + if (replicas >= 0) ninep_io.setCloexec(replicas); + } else if (std.mem.eql(u8, what, "c")) { + const i = std.fmt.parseInt(u8, words.next() orelse "", 10) catch continue; + const fd = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch continue; + if (i >= max_conns) continue; + const k = std.meta.stringToEnum(Kind, words.next() orelse ""); + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; + const detached = std.mem.eql(u8, words.next() orelse "", "1"); + const semantics = std.fmt.parseInt(u64, words.next() orelse "", 16) catch 0; + const rest = std.mem.trim(u8, words.rest(), " "); + ninep_io.setCloexec(fd); + const c = &d.conns[i]; + c.* = .{ .fd = fd, .kind = k, .pid = pid, .detached = detached, .semantics = semantics }; + c.name_len = @min(rest.len, c.name.len); + @memcpy(c.name[0..c.name_len], rest[0..c.name_len]); + } else if (std.mem.eql(u8, what, "h")) { + const fd = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch continue; + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0; + const owner = std.fmt.parseInt(u8, words.next() orelse "", 10) catch 255; + ninep_io.setCloexec(fd); + for (&d.held) |*h| if (h.fd < 0) { + h.* = .{ .fd = fd, .pid = pid, .owner = if (owner < max_conns) owner else null }; + break; + }; + } + } + return replicas; + } + fn accept(d: *Daemon, listener: c_int) void { const fd = libc.accept4(listener, null, null, libc.SOCK.CLOEXEC); if (fd < 0) return; @@ -946,16 +1074,23 @@ fn listenReplicas() c_int { return -1; } +/// Room for an upgrade's registry on a command line (`reexec`): every +/// connection and master, each a line. +const flag_room = 64 * 1024; + /// `pardes --daemon`: what `spawn` started, on the listener (fd 3) and the -/// lock (fd 4). Returns only to exit, idle. -pub fn serve() noreturn { +/// lock (fd 4), or (`state`) an upgrade of one, its registry. Returns only +/// to exit, idle. +pub fn serve(state: ?[]const u8) noreturn { const listener: c_int = 3; ninep_io.setCloexec(3); ninep_io.setCloexec(4); const ignore: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.IGN }, .mask = posix.sigemptyset(), .flags = 0 }; for ([_]posix.SIG{ .PIPE, .HUP, .INT }) |sig| posix.sigaction(sig, &ignore, null); - const replicas = listenReplicas(); + upgrade.remember(&.{}); var d: Daemon = .{}; + const given = if (state) |text| d.restore(text) else -1; + const replicas = if (given >= 0) given else listenReplicas(); const slots = 2 + max_conns + max_replicas + max_held; var fds: [slots]libc.pollfd = undefined; var conn_of: [slots]u8 = undefined; @@ -1025,6 +1160,10 @@ pub fn serve() noreturn { if (pfd.revents == 0 or d.held[i].fd != pfd.fd) continue; d.drop(&d.held[i]); } + if (d.upgrade_asked) { + d.upgrade_asked = false; + d.reexec(replicas); + } } var sock_buf: [sun_path_len:0]u8 = undefined; _ = libc.close(listener); diff --git a/src/dump.zig b/src/dump.zig index 2cef46e2..cb639568 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -321,7 +321,7 @@ pub fn restore(p: *Pardes, bytes: []const u8, from: []const u8) !*Pardes { var opts = p.opts; opts.cols = p.screen_w; opts.rows = p.screen_h; - const replacement = try loadAndRevive(p.gpa, opts, bytes, p); + const replacement = try loadAndRevive(p.gpa, opts, bytes, p, null); replacement.pointRestoreAt(from); // The tape follows the replacement from here (it is the session). pardes.tape.adopt(replacement); @@ -332,7 +332,7 @@ pub fn restore(p: *Pardes, bytes: []const u8, from: []const u8) !*Pardes { pub fn initFromDump(gpa: std.mem.Allocator, opts: Options, bytes: []const u8) !*Pardes { pardes.tape.startFromEnv(); const taped = pardes.tape.enter(null, .{ .init_from_dump = .{ .opts = opts, .bytes = bytes } }); - const p = (if (std.mem.startsWith(u8, bytes, Handover.carried_magic)) carriedWindow(gpa, opts, bytes) else loadAndRevive(gpa, opts, bytes, null)) catch |err| { + const p = (if (std.mem.startsWith(u8, bytes, Handover.carried_magic)) carriedWindow(gpa, opts, bytes) else loadAndRevive(gpa, opts, bytes, null, null)) catch |err| { pardes.tape.leave(null, taped); return err; }; @@ -346,18 +346,39 @@ fn carriedWindow(gpa: std.mem.Allocator, opts: Options, bytes: []const u8) !*Par const p = try Pardes.create(gpa, opts); errdefer p.deinit(); _ = try Handover.adopt(p, bytes); - try reviveSession(p, null); + try reviveSession(p, null, false); return p; } -fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previous: ?*const Pardes) !*Pardes { +/// An upgraded session's dump, loaded as the session it was (upgrade.zig): +/// every view kept, its settings the dump's (the startup config is not run +/// again), and the terminals whose shells run on (`kept`) not revived: the +/// host gives each its pty back. Untaped: a recording ends at an upgrade. +pub fn resumeFromDump(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, kept: *const [MAX_PANES]bool) !*Pardes { + return loadAndRevive(gpa, opts, bytes, null, kept); +} + +/// Where an upgrade leaves its dump: the DumpDir (a relative Restore finds +/// it there by name), as `pardes-upgrade-.dump`; never $PARDES_DUMP, +/// which is a Dump's. +pub fn upgradePath(buf: *[1024:0]u8, dir_setting: []const u8) ?[:0]const u8 { + var dir_buf: [900]u8 = undefined; + const dir = directory(&dir_buf, dir_setting) orelse return null; + _ = std.Io.Dir.cwd().createDirPathStatus(std.Io.Threaded.global_single_threaded.io(), dir, .fromMode(0o755)) catch {}; + return std.fmt.bufPrintSentinel(buf, "{s}/pardes-upgrade-{d}.dump", .{ dir, std.c.getpid() }, 0) catch null; +} + +fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previous: ?*const Pardes, kept: ?*const [MAX_PANES]bool) !*Pardes { var notes: std.ArrayList(u8) = .empty; defer notes.deinit(gpa); var on_disk: OnDisk = @splat(false); const p = try loadKnowing(gpa, opts, bytes, ¬es, &on_disk); errdefer p.deinit(); - for (p.panes, on_disk, 0..) |slot, was_on_disk, i| if (slot) |pane| revivePane(p, i, pane, was_on_disk); - try reviveSession(p, previous); + for (p.panes, on_disk, 0..) |slot, was_on_disk, i| if (slot) |pane| { + if (kept) |k| if (k[i] and pane.terminal != null) continue; + revivePane(p, i, pane, was_on_disk); + }; + try reviveSession(p, previous, kept != null); // What another build's dump could not give, said where the user looks // (stderr is a tty session's screen). if (notes.items.len > 0) { @@ -370,11 +391,12 @@ fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previ /// A loaded core given to this host (with `revivePane` for each of its /// panes): the store and Recent read (a Restore's carried from the session -/// it replaces) and its ThemeFile asked for again. -fn reviveSession(p: *Pardes, previous: ?*const Pardes) !void { +/// it replaces) and its ThemeFile asked for again. An upgrade's (`resumed`) +/// keeps its views, whose clients come back with it, and its settings. +fn reviveSession(p: *Pardes, previous: ?*const Pardes, resumed: bool) !void { // ponytail: one client loads it, so it is one view, the live one's; a // replica joining (phase 5) takes `load`'s, every view. - views.only(p); + if (!resumed) views.only(p); if (previous) |old| { // Never a serial or a request number the old session gave out. p.next_serial = @max(p.next_serial, old.next_serial); @@ -403,7 +425,7 @@ fn reviveSession(p: *Pardes, previous: ?*const Pardes) !void { // The dump's screen until this host says its own (a resize): two // reflows would lose where a terminal's view was. p.sync(); - exec.applyStartupConfig(p); + if (!resumed) exec.applyStartupConfig(p); colors.finishThemeInitialization(p); p.sync(); p.presentation.enabled = true; @@ -640,6 +662,35 @@ test "a loaded terminal's history stays above its banner, the cursor where it wa try std.testing.expect(std.mem.indexOf(u8, text, "two") .? < std.mem.indexOf(u8, text, "restored history").?); } +test "an upgraded session's dump keeps its views and its running terminal as they were, and settings the startup config would change" { + if (comptime !pardes.panes.terminal.enabled or isolated) return error.SkipZigTest; + const gpa = std.testing.allocator; + const term = pardes.panes.terminal; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12, .startup_config = "Verbose off\n" }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + term.feedOutput(p, p.panes[0].?, "one\r\n\x1b[?1049h$ vim"); + try std.testing.expect(p.executeBuiltinLine(p.active, "Verbose on")); + _ = p.openView().?; + const bytes = try dumpBytes(p); + defer gpa.free(bytes); + var kept: [MAX_PANES]bool = @splat(false); + kept[0] = true; + const q = try resumeFromDump(gpa, .{ .tty_only = true, .cols = 40, .rows = 12, .startup_config = "Verbose off\n" }, bytes, &kept); + defer q.deinit(); + try std.testing.expectEqual(@as(usize, 2), views.count(q)); + try std.testing.expect(q.settings.verbose); + // The program's own screen, no banner, and no shell asked for. + try std.testing.expectEqual(term.gridCursor(p.panes[0].?), term.gridCursor(q.panes[0].?)); + try std.testing.expect(q.panes[0].?.terminal.?.vt.screens.active_key == .alternate); + while (q.nextEffect()) |e| try std.testing.expect(e != .spawn); + // Not kept, the same terminal is a new shell's under its history. + kept[0] = false; + const r = try resumeFromDump(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }, bytes, &kept); + defer r.deinit(); + try std.testing.expect(r.panes[0].?.terminal.?.vt.screens.active_key == .primary); +} + test "a session read by name, field by field as another build reads it, is the same session" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true }); diff --git a/src/gui/gui.zig b/src/gui/gui.zig index c550aa4c..24061251 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -23,6 +23,7 @@ const c_heap = @import("c_heap"); const ninep_io = @import("../9p_io.zig"); const daemon = @import("../daemon.zig"); +const upgrade = @import("../upgrade.zig"); const tracy = @import("../tracy.zig"); pub const c = @import("gui_c"); // SDL3 and font.h, translated by build.zig @@ -2376,7 +2377,9 @@ fn localSession( opts.cols = @intCast(@max(1, @divTrunc(@as(u32, @intCast(@max(pw, 1))), g.cell_w))); opts.rows = @intCast(@max(1, @divTrunc(@as(u32, @intCast(@max(ph, 1))), g.cell_h))); } - var core = if (opts.load_path) |lp| blk: { + var core = if (upgrade.resumed) |*rec| + try upgrade.load(gpa, allocs.pardes, opts, rec) + else if (opts.load_path) |lp| blk: { const bytes = try @import("../fs.zig").readFile(gpa, lp); defer gpa.free(bytes); break :blk try pardes.dump.initFromDump(allocs.pardes, opts, bytes); @@ -2457,6 +2460,12 @@ fn localSession( core.host = host; while (core.nextEffect()) |e| core.perform(e); + // An upgraded session's shells, once a fresh session's own has a + // directory to adopt them in. + if (upgrade.resumed) |*rec| { + shell.resumePtys(rec); + while (core.nextEffect()) |e| core.perform(e); + } for (&ptys, 0..) |*slot, id| if (slot.*) |*pt| { pt.reader = try queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, @intCast(id), gens[id]); }; @@ -2467,10 +2476,12 @@ fn localSession( if (fs) |f| try f.watch(&queue, wakeFs); _ = c.SDL_StartTextInput(g.window); + // An upgraded session's views, a window each again. + if (upgrade.resumed) |*rec| if (!rec.fresh and !headless) shell.resumeWindows(); // PARDES_TEST_VIEWS=N: N more windows from the start, each a view of the // session (views.zig), captured under view-; view 0 live after. - if (test_mode) if (std.c.getenv("PARDES_TEST_VIEWS")) |n| { - for (0..std.fmt.parseInt(u8, std.mem.span(n), 10) catch 0) |_| shell.openAnotherWindow(); + if (test_mode and upgrade.resumed == null) if (std.c.getenv("PARDES_TEST_VIEWS")) |n| { + for (0..std.fmt.parseInt(u8, std.mem.span(n), 10) catch 0) |_| shell.openAnotherWindow(null); core.toView(0); shell.follow(); }; @@ -2491,6 +2502,8 @@ fn localSession( while (!core.quit) { pardes.turn.bump(&pardes.turn.restores); try core.pump(host); + // Asked in the step (the word), done between steps: the session whole. + if (shell.upgrade_asked) |pane| shell.upgradeNow(pane); // A window asked for again, or an adopted shell, from the daemon's // thread through the queue; started after the first frame. daemon.watch(&queue, deliverNote); @@ -3787,6 +3800,8 @@ const Shell = struct { /// and the view whose window the Gui holds (`follow`). windows: [pardes.views.max]GuiWindow = undefined, gview: u8 = 0, + /// Upgrade was asked from this pane, to be done between steps (`upgradeNow`). + upgrade_asked: ?u8 = null, fn reap(s: *Shell) void { const now = std.Io.Clock.awake.now(s.queue.io).toMilliseconds(); @@ -3905,11 +3920,12 @@ const Shell = struct { } /// `pardes-gui --attach` while a window is open: another window, a view - /// of its own (views.zig), opened as the first was. - fn openAnotherWindow(s: *Shell) void { + /// of its own (views.zig), opened as the first was; or (`existing`) an + /// upgraded session's view's window again. + fn openAnotherWindow(s: *Shell, existing: ?u8) void { const g = s.gui orelse return; s.follow(); - const k = s.core.openView() orelse return log.err("--attach: this session has as many windows as it takes", .{}); + const k = existing orelse s.core.openView() orelse return log.err("--attach: this session has as many windows as it takes", .{}); const live = s.gview; copyWindow(&s.windows[live], g); freshWindow(g); @@ -3998,6 +4014,94 @@ const Shell = struct { daemon.adopt(); } + fn upgradeAsked(ctx: ?*anyopaque, pane: u8) void { + const s = shellOf(ctx); + if (comptime !upgrade.supported) return s.core.reportFailure(pane, "Upgrade: this system cannot move a session to another build in place"); + s.upgrade_asked = pane; + } + + /// Upgrade, between steps: this process exec'd as the pardes now at the + /// path it was started from, the session in its dump and its ptys and + /// daemon link kept open across (upgrade.zig); the new process opens + /// its windows again. Returns only when it could not be, as it was. + fn upgradeNow(s: *Shell, pane: u8) void { + s.upgrade_asked = null; + const core = s.core; + var why: [800]u8 = undefined; + const path = upgrade.exe() orelse return core.reportFailure(pane, "Upgrade: this process does not know the path it was started from"); + // 9P is served while the new binary is asked, and not after. + pardes.turn.rest(); + const checked = upgrade.check(path, &why); + pardes.turn.wake(); + switch (checked) { + .no => |w| return core.reportFailure(pane, w), + .ok => {}, + } + // Nothing more read from a pty, and what was read goes in: drained, + // the readers stopped (a reader waiting for room has it then), and + // what they pushed meanwhile drained. + s.drainQueue(); + for (s.ptys) |*slot| if (slot.*) |*pt| if (pt.reader) |*r| { + r.cancel(s.queue.io); + pt.reader = null; + }; + s.drainQueue(); + core.settle(); + var rec: upgrade.Record = .{ .link = daemon.link, .window = s.gui != null, .exe = path }; + for (s.ptys, 0..) |slot, id| if (slot) |pt| if (pt.fd >= 0) rec.addPty(id, pt.pid, pt.fd); + var at_buf: [1024:0]u8 = undefined; + const failed: []const u8 = failed: { + const at = pardes.dump.upgradePath(&at_buf, core.settings.dump_dir.get()) orelse break :failed "Upgrade: no directory for the session's dump; this session stays as it is"; + const bytes = pardes.dump.dumpBytes(core) catch |err| { + var words: [128]u8 = undefined; + break :failed std.fmt.bufPrint(&why, "Upgrade: the session would not dump: {s}; it stays as it is", .{pardes.Messages.errorWords(err, &words)}) catch "Upgrade: the session would not dump"; + }; + defer s.gpa.free(bytes); + if (!upgrade.writeDump(at, bytes)) break :failed std.fmt.bufPrint(&why, "Upgrade: the session's dump could not be written to {s}; it stays as it is", .{at[0..@min(at.len, 600)]}) catch "Upgrade: its dump could not be written"; + rec.dump = at; + rec.old = upgrade.openSelf(); + host_io.Shell.unlinkOwnedRcs(); + daemon.upgraded(); + if (s.fs) |f| f.flushOut(); + const err_words = upgrade.exec(s.gpa, path, -1, &rec); + // Still here: the exec failed. + _ = libc.unlink(at); + if (rec.old >= 0) _ = libc.close(rec.old); + break :failed std.fmt.bufPrint(&why, "Upgrade: {s} would not start: {s}; this session stays as it is", .{ path[0..@min(path.len, 600)], err_words }) catch "Upgrade: the new pardes would not start"; + }; + // As it was: its ptys read on. + for (s.ptys, 0..) |*slot, id| if (slot.*) |*pt| if (pt.fd >= 0 and pt.reader == null) { + pt.reader = s.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, @intCast(id), s.gens[id]) catch null; + }; + core.reportFailure(pane, failed); + } + + /// An upgraded session's shells, running on: each its pane's pty again, + /// or, when its dump would not load (`fresh`), adopted as new panes. + fn resumePtys(s: *Shell, rec: *const upgrade.Record) void { + for (rec.ptys[0..rec.nptys]) |k| { + const pane = if (rec.fresh) null else s.core.panes[k.pane]; + if (pane == null or !pane.?.isTerminal()) { + s.adoptShell(k.pid, k.fd); + continue; + } + s.ptys[k.pane] = .{ .fd = k.fd, .pid = k.pid, .serial = pane.?.serial, .cmd = .{ .watched = pane.?.command != null and host_io.watchExit(k.pid), .fd = k.fd } }; + } + } + + /// An upgraded session's views, a window each again: the Gui's is the + /// live view's, the others open as `--attach` opens one. + fn resumeWindows(s: *Shell) void { + const live = s.core.view; + s.gview = live; + for (0..pardes.views.max) |i| { + const k: u8 = @intCast(i); + if (k != live and pardes.views.isOpen(s.core, k)) s.openAnotherWindow(k); + } + s.core.toView(live); + s.follow(); + } + /// One shell of a dead session (`pid` 0: the last of them was told): /// a terminal pane made for it, as the Tty word makes one, whose spawn /// takes this pty (spawnPane). @@ -4038,6 +4142,7 @@ const Shell = struct { .watched = watched, .detach = detachWindow, .adopt = adopt, + .upgrade = upgradeAsked, .spawn = spawnPane, .pty_write = ptyWrite, .pty_resize = ptyResize, @@ -4072,6 +4177,7 @@ const Shell = struct { v.watched = null; v.detach = null; v.adopt = null; + v.upgrade = null; break :vt v; }; @@ -4167,7 +4273,7 @@ const Shell = struct { .files_changed => check_files = true, .fs_ready => {}, .window => { - if (s.gui != null) s.openAnotherWindow() else s.openTheWindow(); + if (s.gui != null) s.openAnotherWindow(null) else s.openTheWindow(); s.saw_event = true; }, .adopted => |a| { diff --git a/src/host_io.zig b/src/host_io.zig index 7b0fe9a2..46ee9afc 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -29,6 +29,9 @@ pub const Host = struct { detach: ?*const fn (ctx: ?*anyopaque) void = null, /// Adopt: the shells a dead session left, asked of the daemon. adopt: ?*const fn (ctx: ?*anyopaque, pane: u8) void = null, + /// Upgrade: this process exec'd as the pardes installed since, at + /// the step's end (upgrade.zig); `pane` hears why if it cannot be. + upgrade: ?*const fn (ctx: ?*anyopaque, pane: u8) 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, diff --git a/src/main.zig b/src/main.zig index ac2226ae..a6144475 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,6 +3,7 @@ const builtin = @import("builtin"); const pardes = @import("pardes.zig"); const ninep_io = @import("9p_io.zig"); const daemon = @import("daemon.zig"); +const upgrade = @import("upgrade.zig"); const is_emscripten = builtin.os.tag == .emscripten; @@ -346,8 +347,18 @@ fn nativeMain(std_init: std.process.Init) !void { var mounts_len: usize = 0; const arena = init.arena.allocator(); const args = try init.minimal.args.toSlice(arena); - // The per-user daemon a session started (daemon.zig): nothing else. - if (comptime daemon.supported) if (args.len == 2 and std.mem.eql(u8, args[1], "--daemon")) daemon.serve(); + // The per-user daemon a session started (daemon.zig), or an upgrade of + // one with its registry: nothing else. + if (comptime daemon.supported) if (args.len >= 2 and args.len <= 3 and std.mem.eql(u8, args[1], "--daemon")) + daemon.serve(if (args.len == 3 and std.mem.startsWith(u8, args[2], upgrade.flag)) args[2][upgrade.flag.len..] else null); + // An upgraded session's new process (upgrade.zig): its record, then the + // arguments it was started with, which configure it as before; its + // file, stdin, directory and daemon come from the record instead. + const upgraded = comptime upgrade.supported and (pardes.platform == .tty or pardes.platform == .gui); + if (upgraded) if (args.len > 1 and std.mem.startsWith(u8, args[1], upgrade.flag)) { + upgrade.resumed = upgrade.Record.decode(args[1][upgrade.flag.len..]) catch badArgs(init.io, "--upgraded: a record this build cannot read", .{}); + }; + const resuming = upgrade.resumed != null; var session_only = true; var new_session = false; var explicit_tty = false; @@ -355,7 +366,7 @@ fn nativeMain(std_init: std.process.Init) !void { var attach: ?[]const u8 = null; var positional: ?[:0]const u8 = null; var wait = false; - var i: usize = 1; + var i: usize = if (resuming) 2 else 1; while (i < args.len) : (i += 1) { const a = args[i]; if (std.mem.eql(u8, a, "--tty")) { @@ -496,7 +507,7 @@ fn nativeMain(std_init: std.process.Init) !void { badArgs(init.io, "--9p-quic opens a session's own listener, and --attach has none of its own", .{}); if (opts.mounts.len != 0 and attach != null) badArgs(init.io, "--mount configures a session's own core, and --attach has none of its own", .{}); - if (!new_session and !opts.nested and detach == null and attach == null) forwarding: { + if (!new_session and !opts.nested and detach == null and attach == null and !resuming) forwarding: { // Am I inside a pardes at all? That is $PARDES_PID and nothing else — // one variable, exported by every session that means to take its // children's Looks, so a session that could not open a socket is still @@ -677,6 +688,7 @@ fn nativeMain(std_init: std.process.Init) !void { if (wait) waitForDel(init.io, arena, parent.dial, path, made_serial); return; } + if (resuming) positional = null; // `pardes -` outside a session: an editor whose first pane holds stdin, // as `vim -` does; the terminal is /dev/tty, never stdin. if (positional) |a| if (std.mem.eql(u8, a, "-")) { @@ -726,11 +738,11 @@ fn nativeMain(std_init: std.process.Init) !void { opts.config_dir = found.dir; opts.store_path = storePath(init.io, arena, init.environ_map); if (found.dir) |d| @import("crash.zig").setDir(d); - if (attach) |name| attachTo(init.io, name); + if (attach) |name| if (!resuming) attachTo(init.io, name); if (detach) |name| { // Said plainly, never a Zig error trace: a name a live session's // socket holds already (a plain session is named by its pid too). - if (name.len != 0 and ninep_io.taken(name)) { + if (name.len != 0 and !resuming and ninep_io.taken(name)) { var said: [512]u8 = undefined; std.Io.File.stderr().writeStreamingAll(init.io, std.fmt.bufPrint(&said, "pardes: a session named {s} is already running\n", .{name}) catch "pardes: a session of that name is already running\n") catch {}; std.process.exit(1); @@ -743,7 +755,19 @@ fn nativeMain(std_init: std.process.Init) !void { } const kind: daemon.Kind = if (pardes.platform == .gui) .gui else .tty; var term: @import("tty/tty.zig").Term = if (detach != null) .none else .open; - if (daemon.wanted()) { + var headless = detach != null; + upgrade.remember(args); + if (upgrade.resumed) |*rec| { + // Close-on-exec again: a shell forked from here inherits none. + rec.closeOnExec(true); + opts.load_path = rec.dump; + term = .none; + for (rec.ttys[0..rec.nttys]) |t| if (t.primary) { + term = .{ .fd = .{ .fd = t.fd, .termios = t.termios } }; + }; + headless = if (pardes.platform == .gui) !rec.window else rec.nttys == 0; + daemon.resumeLink(rec.link, rec.stub, kind, opts.ninep_name, headless); + } else if (daemon.wanted()) { if (detach != null or pardes.platform == .gui) { _ = daemon.register(null, kind, opts.ninep_name, detach != null); } else if (pardes.platform == .tty) fork: { @@ -770,7 +794,7 @@ fn nativeMain(std_init: std.process.Init) !void { break :fork; }; _ = daemon.register(conn, .tty, opts.ninep_name, false); - term = .{ .fd = fd }; + term = .{ .fd = .{ .fd = fd } }; } } defer daemon.quit(); @@ -785,7 +809,7 @@ fn nativeMain(std_init: std.process.Init) !void { }, else => return err, }, - .gui => try @import("gui/gui.zig").run(init, opts, detach != null), + .gui => try @import("gui/gui.zig").run(init, opts, headless), .web, .macos, .esp32p4 => unreachable, } daemon.tellStubExit(0); diff --git a/src/pardes.zig b/src/pardes.zig index 0806cc78..081861ac 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -501,6 +501,7 @@ test { _ = @import("body_layer.zig"); _ = @import("draw.zig"); _ = @import("dump.zig"); // its tests ran nowhere before + _ = @import("upgrade.zig"); _ = @import("recent.zig"); _ = @import("Store.zig"); _ = @import("Presentation.zig"); @@ -6048,6 +6049,13 @@ pub const Pardes = struct { } } + /// What a step leaves queued, its inputs and its effects, taken now: + /// the session at a step boundary (an Upgrade's dump, upgrade.zig). + pub fn settle(p: *Pardes) void { + while (p.nextQueued()) |ev| p.update(ev); + while (p.nextEffect()) |e| p.perform(e); + } + pub fn pump(p: *Pardes, host: Host) !void { p.host = host; const taped = tape.enter(p, .pump); diff --git a/src/tty/tty.zig b/src/tty/tty.zig index a7048645..ef7688bc 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -16,6 +16,7 @@ const ninep_io = @import("../9p_io.zig"); const panel_compositor = @import("panel_compositor.zig"); const daemon = @import("../daemon.zig"); const host_io = @import("../host_io.zig"); +const upgrade = @import("../upgrade.zig"); extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; @@ -69,6 +70,16 @@ fn kittyShmName(buf: []u8, n: u32) ![:0]const u8 { return std.fmt.bufPrintSentinel(buf, "/pardes-{d}-{d}", .{ libc.getpid(), n }, 0); } +/// A terminal unlinks what it reads; whatever it never read (the probe +/// unanswered, a transmission lost with the terminal) is ours to remove. +fn unlinkKittyShm() void { + var name_buf: [64]u8 = undefined; + var n: u32 = 0; + while (n <= kitty_shm_serial) : (n += 1) { + if (kittyShmName(&name_buf, n)) |name| _ = libc.shm_unlink(name) else |_| {} + } +} + /// Write `bytes` to a fresh shm object; the terminal unlinks it after reading. fn kittyShmWrite(name: [:0]const u8, bytes: []const u8) bool { const fd = libc.shm_open(name, @bitCast(libc.O{ .ACCMODE = .RDWR, .CREAT = true, .EXCL = true }), @as(c_uint, 0o600)); @@ -1208,7 +1219,9 @@ fn nativePdfWheelTarget(core: *const pardes.Pardes, mouse: vaxis.Mouse) ?PdfWhee /// Where a session's terminal comes from: /dev/tty, as a plain `pardes` /// opens it; one open already (a session forked off its stub, /// daemon.forkSession); or none, until one is attached (`--detach`). -pub const Term = union(enum) { open, fd: posix.fd_t, none }; +/// The terminal a session starts on: /dev/tty, one its stub opened (an +/// upgraded session's is raw already, `termios` the cooked one), or none. +pub const Term = union(enum) { open, fd: struct { fd: posix.fd_t, termios: ?posix.termios = null }, none }; pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void { const io = init.io; @@ -1230,7 +1243,7 @@ pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void { defer screen.kitty.deinit(); switch (term) { .open => try screen.enter(try vaxis.Tty.init(io, &tty_buf)), - .fd => |fd| try screen.enter(try Screen.ttyOn(io, &tty_buf, fd)), + .fd => |t| try screen.enter(try Screen.ttyOn(io, &tty_buf, t.fd, t.termios)), .none => {}, } defer screen.leave(); @@ -1277,9 +1290,10 @@ const Screen = struct { /// `fd` made raw, as vaxis.Tty.init makes /dev/tty: the terminal of a /// forked session is no controlling terminal of its, which /dev/tty is. - fn ttyOn(io: std.Io, buf: []u8, fd: posix.fd_t) !vaxis.Tty { + /// An upgraded session's is raw already, `cooked` what it goes back to. + fn ttyOn(io: std.Io, buf: []u8, fd: posix.fd_t, cooked: ?posix.termios) !vaxis.Tty { if (comptime builtin.is_test) unreachable; // vaxis.Tty is its TestTty - const termios = try vaxis.Tty.makeRaw(fd); + const termios = cooked orelse try vaxis.Tty.makeRaw(fd); const file: std.Io.File = .{ .handle = fd, .flags = .{ .nonblocking = false } }; return .{ .io = io, .fd = file, .termios = termios, .tty_writer = file.writerStreaming(io, buf) }; } @@ -1416,7 +1430,9 @@ fn localSession( pardes.syntax.stop(); } - var core = if (options.load_path) |lp| blk: { + var core = if (upgrade.resumed) |*rec| + try upgrade.load(gpa, allocs.pardes, options, rec) + else if (options.load_path) |lp| blk: { const bytes = try filesystem.readFile(gpa, lp); defer gpa.free(bytes); break :blk try pardes.dump.initFromDump(allocs.pardes, options, bytes); @@ -1505,9 +1521,17 @@ fn localSession( const host = sh.host(); core.host = host; + // An upgraded session's terminals, its own again. + if (comptime !builtin.is_test) if (upgrade.resumed) |*rec| sh.resumeTerminals(rec, screen); // Nobody to see it, and nobody to size it: `size` sets it (ctl.zig). - core.unattended = !screen.on; + core.unattended = !sh.attached(); while (core.nextEffect()) |effect| core.perform(effect); + // ...and its shells, once a fresh session's own has a directory to + // adopt them in. + if (upgrade.resumed) |*rec| { + sh.resumePtys(rec); + while (core.nextEffect()) |effect| core.perform(effect); + } if (screen.on) try startInput(loop, screen); defer stopInput(io, &screen.thread); @@ -1520,15 +1544,15 @@ fn localSession( // PARDES_TEST_VIEWS: the terminals at these paths are views of the // session too, from its start, as `pardes --attach`ed ones are (the // snapshot harness's `start ... views=N`). - if (init.environ_map.get("PARDES_TEST_VIEWS")) |paths| { + if (upgrade.resumed == null) if (init.environ_map.get("PARDES_TEST_VIEWS")) |paths| { var it = std.mem.tokenizeScalar(u8, paths, ' '); while (it.next()) |path| { var path_buf: [256]u8 = undefined; const z = std.fmt.bufPrintSentinel(&path_buf, "{s}", .{path}, 0) catch continue; const fd = libc.open(z, .{ .ACCMODE = .RDWR, .NOCTTY = true, .CLOEXEC = true }, @as(libc.mode_t, 0)); - if (fd >= 0) sh.attachTerminal(fd, -1); + if (fd >= 0) sh.attachTerminal(fd, -1, null); } - } + }; sh.threads_ok = true; for (&sh.ptys, 0..) |*slot, id| if (slot.*) |*pt| { @@ -1536,18 +1560,12 @@ fn localSession( }; if (sh.inotify_fd >= 0) sh.watch_task = io.concurrent(watchFiles, .{ io, sh.inotify_fd, loop }) catch null; - // A terminal unlinks what it reads; whatever it never read (the probe - // unanswered, a transmission lost with the terminal) is ours to remove. - defer { - var name_buf: [64]u8 = undefined; - var n: u32 = 0; - while (n <= kitty_shm_serial) : (n += 1) { - if (kittyShmName(&name_buf, n)) |name| _ = libc.shm_unlink(name) else |_| {} - } - } + defer unlinkKittyShm(); while (!core.quit) { pardes.turn.bump(&pardes.turn.restores); try core.pump(host); + // Asked in the step (the word), done between steps: the session whole. + if (sh.upgrade_asked) |pane| sh.upgradeNow(pane); // A terminal (`pardes --attach`) or an adopted shell, handed on by // the daemon's thread to this loop; started after the first frame, // off a startup's way. @@ -1695,6 +1713,10 @@ const Shell = struct { drawn_ns: u64 = 0, input_ns: u64 = 0, focused_ns: u64 = 0, + /// Upgrade was asked from this pane, to be done between steps (`upgradeNow`). + upgrade_asked: ?u8 = null, + /// Its pty readers stopped for it: their end of file is not a shell's. + upgrading: bool = false, const Leaving = enum { detach, exit }; @@ -1723,6 +1745,7 @@ const Shell = struct { .watched = watched, .detach = detach, .adopt = adopt, + .upgrade = upgradeAsked, .spawn = spawn, .pty_write = ptyWrite, .pty_resize = ptyResize, @@ -1825,7 +1848,7 @@ const Shell = struct { /// `pardes --attach`'s terminal: for a detached session its own again, /// entered as the first one was; for one attached already, a view of /// its own (views.zig). Sized, asked what it can do, and drawn whole. - fn attachTerminal(s: *Shell, fd: posix.fd_t, stub: posix.fd_t) void { + fn attachTerminal(s: *Shell, fd: posix.fd_t, stub: posix.fd_t, cooked: ?posix.termios) void { const live = s.term() orelse return; const screen = if (!live.on) live else extra: { const sc = s.gpa.create(Screen) catch break :extra null; @@ -1840,7 +1863,7 @@ const Shell = struct { return refuse(stub, "that session has no room for another terminal"); }; const extra = screen != live; - const tty = Screen.ttyOn(s.io, screen.buf, fd) catch { + const tty = Screen.ttyOn(s.io, screen.buf, fd, cooked) catch { _ = libc.close(fd); if (extra) s.freeScreen(screen) else daemon.setState(true); return refuse(stub, "that terminal would not go raw"); @@ -1886,6 +1909,159 @@ const Shell = struct { daemon.adopt(); } + fn upgradeAsked(ctx: ?*anyopaque, pane: u8) void { + const s = of(ctx); + if (comptime !upgrade.supported) return s.core.reportFailure(pane, "Upgrade: this system cannot move a session to another build in place"); + s.upgrade_asked = pane; + } + + /// Upgrade, between steps: this process exec'd as the pardes now at the + /// path it was started from, the session in its dump, and its ptys, + /// terminals, stubs and daemon link kept open across (upgrade.zig). + /// Returns only when it could not be, the session going on as it was. + fn upgradeNow(s: *Shell, pane: u8) void { + if (comptime builtin.is_test) unreachable; // vaxis.Tty is its TestTty + s.upgrade_asked = null; + const core = s.core; + var why: [800]u8 = undefined; + const path = upgrade.exe() orelse return core.reportFailure(pane, "Upgrade: this process does not know the path it was started from"); + // 9P is served while the new binary is asked (its clients' clunks + // and reads after the word are answered), and not after. + pardes.turn.rest(); + const checked = upgrade.check(path, &why); + pardes.turn.wake(); + switch (checked) { + .no => |w| return core.reportFailure(pane, w), + .ok => {}, + } + // Nothing more read from a pty or a terminal, and what was read + // goes in; a reader's cancel posts an end of file, passed over. + s.upgrading = true; + defer s.upgrading = false; + for (&s.ptys) |*slot| if (slot.*) |*pt| pt.reader.cancel(s.io) catch {}; + for (s.terms) |slot| if (slot) |sc| stopInput(s.io, &sc.thread); + while (s.loop.tryEvent() catch null) |ev| _ = s.apply(ev); + core.settle(); + var rec: upgrade.Record = .{ .link = daemon.link, .stub = daemon.stub, .exe = path }; + for (s.ptys, 0..) |slot, id| if (slot) |pt| rec.addPty(id, pt.pid, pt.file.handle); + for (s.terms, 0..) |slot, k| if (slot) |sc| if (sc.on) rec.addTty(.{ + .view = @intCast(k), + .fd = sc.tty.fd.handle, + .stub = if (sc.primary) daemon.stub else sc.stub, + .primary = sc.primary, + .termios = sc.tty.termios, + }); + var popped = false; + var at_buf: [1024:0]u8 = undefined; + const failed: []const u8 = failed: { + const at = pardes.dump.upgradePath(&at_buf, core.settings.dump_dir.get()) orelse break :failed "Upgrade: no directory for the session's dump; this session stays as it is"; + const bytes = pardes.dump.dumpBytes(core) catch |err| { + var words: [128]u8 = undefined; + break :failed std.fmt.bufPrint(&why, "Upgrade: the session would not dump: {s}; it stays as it is", .{pardes.Messages.errorWords(err, &words)}) catch "Upgrade: the session would not dump"; + }; + defer s.gpa.free(bytes); + if (!upgrade.writeDump(at, bytes)) break :failed std.fmt.bufPrint(&why, "Upgrade: the session's dump could not be written to {s}; it stays as it is", .{at[0..@min(at.len, 600)]}) catch "Upgrade: its dump could not be written"; + rec.dump = at; + rec.old = upgrade.openSelf(); + // What this process put in its terminals that the next puts in + // again: its images, and its keyboard mode, a stack. + for (s.terms) |slot| if (slot) |sc| if (sc.on) { + clearNativeImages(&sc.kitty, &sc.vx, &sc.tty); + if (sc.vx.state.kitty_keyboard) sc.tty.writer().writeAll("\x1b[= 0) _ = libc.close(rec.old); + break :failed std.fmt.bufPrint(&why, "Upgrade: {s} would not start: {s}; this session stays as it is", .{ path[0..@min(path.len, 600)], err_words }) catch "Upgrade: the new pardes would not start"; + }; + // As it was: its ptys read on, its terminals' keys as they were. + for (&s.ptys, 0..) |*slot, id| if (slot.*) |*pt| { + pt.reader = s.io.concurrent(readPty, .{ s.io, s.gpa, pt.file, id, s.gens[id], s.loop }) catch pt.reader; + }; + for (s.terms) |slot| if (slot) |sc| if (sc.on) { + if (popped) sc.vx.enableDetectedFeatures(sc.tty.writer()) catch {}; + startInput(s.loop, sc) catch {}; + }; + core.reportFailure(pane, failed); + } + + /// An upgraded session's shells, running on: each its pane's pty again, + /// or, when its dump would not load (`fresh`), adopted as new panes. + fn resumePtys(s: *Shell, rec: *const upgrade.Record) void { + for (rec.ptys[0..rec.nptys]) |k| { + const pane = if (rec.fresh) null else s.core.panes[k.pane]; + if (pane == null or !pane.?.isTerminal()) { + s.adoptShell(k.pid, k.fd); + continue; + } + const file: std.Io.File = .{ .handle = k.fd, .flags = .{ .nonblocking = false } }; + s.ptys[k.pane] = .{ .file = file, .pid = k.pid, .reader = .{ .any_future = null, .result = {} }, .cmd = .{ .watched = pane.?.command != null and host_io.watchExit(k.pid), .fd = k.fd } }; + } + } + + /// An upgraded session's terminals, each its view's again (`primary`, + /// run's, entered already); in a fresh session each but the primary a + /// view of its own, as `--attach` makes one. A view left with none + /// closes. + fn resumeTerminals(s: *Shell, rec: *const upgrade.Record, primary: *Screen) void { + if (comptime builtin.is_test) unreachable; + const core = s.core; + if (rec.fresh) { + for (rec.ttys[0..rec.nttys]) |t| if (!t.primary) s.attachTerminal(t.fd, t.stub, t.termios); + return; + } + s.terms = @splat(null); + if (rec.nttys == 0) { + pardes.views.only(core); + s.terms[core.view] = primary; + return; + } + for (rec.ttys[0..rec.nttys]) |t| { + if (!pardes.views.isOpen(core, t.view) or s.terms[t.view] != null) { + if (t.primary) continue; + _ = libc.close(t.fd); + if (t.stub >= 0) daemon.tellDetached(t.stub); + continue; + } + const sc = if (t.primary) primary else extra: { + const sc = s.gpa.create(Screen) catch break :extra null; + const buf = s.gpa.alloc(u8, 0x10000) catch { + s.gpa.destroy(sc); + break :extra null; + }; + sc.* = .{ .io = s.io, .gpa = s.gpa, .env = primary.env, .buf = buf, .kitty = .init(s.gpa) }; + const tty = Screen.ttyOn(s.io, sc.buf, t.fd, t.termios) catch { + s.freeScreen(sc); + break :extra null; + }; + sc.enter(tty) catch { + s.freeScreen(sc); + break :extra null; + }; + sc.setStub(t.stub); + startInput(s.loop, sc) catch {}; + sc.vx.queryTerminalSend(sc.tty.writer()) catch {}; + break :extra sc; + } orelse { + _ = libc.close(t.fd); + if (t.stub >= 0) daemon.tellDetached(t.stub); + continue; + }; + sc.feed.view = t.view; + s.terms[t.view] = sc; + } + for (0..pardes.views.max) |k| if (pardes.views.isOpen(core, @intCast(k)) and s.terms[k] == null) core.closeView(@intCast(k)); + if (s.terms[core.view] == null) s.terms[core.view] = primary; + } + /// One shell of a dead session (`pid` 0: the last of them was told). fn adoptShell(s: *Shell, pid: posix.pid_t, fd: posix.fd_t) void { const core = s.core; @@ -2083,7 +2259,7 @@ const Shell = struct { core.quit = true; return true; }, - .attach => |a| s.attachTerminal(a.tty, a.stub), + .attach => |a| s.attachTerminal(a.tty, a.stub, null), .adopted => |a| s.adoptShell(a.pid, a.fd), .focus_in => core.update(.{ .focus = true }), .focus_out => { @@ -2102,7 +2278,7 @@ const Shell = struct { s.takeExits(); return true; }, - .pty_eof => |e| if (s.gens[e.id] == e.gen) { + .pty_eof => |e| if (s.gens[e.id] == e.gen and !s.upgrading) { if (s.ptys[e.id]) |*pt| { pt.reader.await(s.io) catch {}; // reader just finished; join it or its future leaks // A command's pty stays open until its child has exited: diff --git a/src/upgrade.zig b/src/upgrade.zig new file mode 100644 index 00000000..c49d2370 --- /dev/null +++ b/src/upgrade.zig @@ -0,0 +1,389 @@ +//! Upgrade (docs/next-daemon.md, "Phase 6: upgrade notes"): a running +//! session, and the daemon, moved onto the pardes installed since they +//! started, in place. +//! +//! At a step boundary a session writes its dump (phase 2) and execs the +//! path it was started from. Its pty masters, terminals, stubs and daemon +//! link stay open across the exec, named in a record on the new command +//! line: `--upgraded=` ahead of the arguments it was started with. +//! The pid stays, so its shells stay its children; the new process loads +//! the dump and takes those fds back (each host's `resume`). A new build +//! that cannot load the dump execs the old one again, from the copy this +//! process left open (Linux), and with none it starts afresh with the +//! shells adopted, the dump left for a Restore by hand. +const std = @import("std"); +const builtin = @import("builtin"); +const libc = std.c; +const posix = std.posix; +const pardes = @import("pardes.zig"); + +pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd; +pub const flag = "--upgraded="; + +/// What this build says it is (`pardes --version`'s first line). +pub const version_line = if (pardes.commit) |c| "pardes " ++ pardes.version ++ " (" ++ c ++ ")" else "pardes " ++ pardes.version; + +// ---- the path it was started from ---- + +/// The path this process was started from, what Upgrade execs: a reinstall +/// puts the new build there, while /proc/self/exe names the old file, +/// deleted by then. +var exe_buf: [4096:0]u8 = undefined; +var exe_len: usize = 0; +/// The arguments it was started with (an upgrade's record left out). +var given: []const [:0]const u8 = &.{}; + +/// Its path asked of the system now, at its start (one readlink), and its +/// arguments kept; an upgraded process takes the path its record names +/// (the system would name a fallback's old binary, deleted). +pub fn remember(args: []const [:0]const u8) void { + given = if (args.len > 1 and std.mem.startsWith(u8, args[1], flag)) args[2..] else if (args.len > 0) args[1..] else &.{}; + if (resumed) |*rec| if (rec.exe.len > 0 and rec.exe.len < exe_buf.len) { + @memcpy(exe_buf[0..rec.exe.len], rec.exe); + exe_buf[rec.exe.len] = 0; + exe_len = rec.exe.len; + return; + }; + const n = std.process.executablePath(std.Io.Threaded.global_single_threaded.io(), &exe_buf) catch 0; + exe_buf[n] = 0; + exe_len = n; +} + +pub fn exe() ?[:0]const u8 { + return if (exe_len == 0) null else exe_buf[0..exe_len :0]; +} + +// ---- the record ---- + +pub const Pty = struct { pane: u8, pid: libc.pid_t, fd: c_int }; +/// A terminal and its view: the primary one's stub is daemon.stub; its +/// termios is the cooked one it is put back to at the end (it is raw now). +pub const Tty = struct { view: u8, fd: c_int, stub: c_int, primary: bool, termios: posix.termios }; + +pub const Record = struct { + /// daemon.link and daemon.stub. + link: c_int = -1, + stub: c_int = -1, + /// The binary being replaced, open (Linux): a new one that cannot load + /// the dump execs it again. + old: c_int = -1, + /// A window was open (the GUI). + window: bool = false, + ptys: [pardes.MAX_PANES]Pty = undefined, + nptys: usize = 0, + ttys: [pardes.views.max]Tty = undefined, + nttys: usize = 0, + /// The path exec'd, the one a later Upgrade execs again. + exe: []const u8 = "", + /// The session's dump. + dump: []const u8 = "", + /// Why the new build could not load it: a start that went back to the + /// old binary says so. + refused: []const u8 = "", + /// This process's own: its dump would not load, so it started afresh + /// and adopts the shells. + fresh: bool = false, + + pub fn addPty(rec: *Record, pane: usize, pid: libc.pid_t, fd: c_int) void { + if (rec.nptys == rec.ptys.len or fd < 0) return; + rec.ptys[rec.nptys] = .{ .pane = @intCast(pane), .pid = pid, .fd = fd }; + rec.nptys += 1; + } + + pub fn addTty(rec: *Record, t: Tty) void { + if (rec.nttys == rec.ttys.len or t.fd < 0) return; + rec.ttys[rec.nttys] = t; + rec.nttys += 1; + } + + /// Every fd it names, close-on-exec (`on`) or kept across one. + pub fn closeOnExec(rec: *const Record, on: bool) void { + const set = struct { + fn f(fd: c_int, cloexec: bool) void { + if (fd >= 0) _ = libc.fcntl(fd, libc.F.SETFD, @as(c_int, if (cloexec) 1 else 0)); + } + }.f; + set(rec.link, on); + set(rec.stub, on); + set(rec.old, on); + for (rec.ptys[0..rec.nptys]) |p| set(p.fd, on); + for (rec.ttys[0..rec.nttys]) |t| { + set(t.fd, on); + set(t.stub, on); + } + } + + /// One `word value` a line, the dump's path last (its line is all of it). + pub fn encode(rec: *const Record, w: *std.Io.Writer) !void { + try w.print("link {d}\nstub {d}\nold {d}\nwindow {d}\n", .{ rec.link, rec.stub, rec.old, @intFromBool(rec.window) }); + for (rec.ptys[0..rec.nptys]) |p| try w.print("pty {d} {d} {d}\n", .{ p.pane, p.pid, p.fd }); + for (rec.ttys[0..rec.nttys]) |t| try w.print("tty {d} {d} {d} {d} {x}\n", .{ t.view, t.fd, t.stub, @intFromBool(t.primary), std.mem.asBytes(&t.termios) }); + if (rec.refused.len > 0) try w.print("refused {s}\n", .{rec.refused}); + try w.print("exe {s}\ndump {s}\n", .{ rec.exe, rec.dump }); + } + + /// `text`'s record; its strings are slices of it. + pub fn decode(text: []const u8) !Record { + var rec: Record = .{}; + var lines = std.mem.splitScalar(u8, text, '\n'); + while (lines.next()) |line| { + if (line.len == 0) continue; + const sp = std.mem.indexOfScalar(u8, line, ' ') orelse return error.BadRecord; + const word = line[0..sp]; + const rest = line[sp + 1 ..]; + var it = std.mem.tokenizeScalar(u8, rest, ' '); + if (std.mem.eql(u8, word, "link")) { + rec.link = try std.fmt.parseInt(c_int, rest, 10); + } else if (std.mem.eql(u8, word, "stub")) { + rec.stub = try std.fmt.parseInt(c_int, rest, 10); + } else if (std.mem.eql(u8, word, "old")) { + rec.old = try std.fmt.parseInt(c_int, rest, 10); + } else if (std.mem.eql(u8, word, "window")) { + rec.window = std.mem.eql(u8, rest, "1"); + } else if (std.mem.eql(u8, word, "pty")) { + if (rec.nptys == rec.ptys.len) return error.BadRecord; + const pane = try std.fmt.parseInt(u8, it.next() orelse return error.BadRecord, 10); + if (pane >= pardes.MAX_PANES) return error.BadRecord; + rec.ptys[rec.nptys] = .{ + .pane = pane, + .pid = try std.fmt.parseInt(libc.pid_t, it.next() orelse return error.BadRecord, 10), + .fd = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), + }; + rec.nptys += 1; + } else if (std.mem.eql(u8, word, "tty")) { + if (rec.nttys == rec.ttys.len) return error.BadRecord; + var t: Tty = .{ + .view = try std.fmt.parseInt(u8, it.next() orelse return error.BadRecord, 10), + .fd = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), + .stub = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), + .primary = std.mem.eql(u8, it.next() orelse return error.BadRecord, "1"), + .termios = undefined, + }; + const hex = it.next() orelse return error.BadRecord; + // Another build's termios is the kernel's, of one size. + if (hex.len != 2 * @sizeOf(posix.termios) or t.view >= pardes.views.max) return error.BadRecord; + _ = try std.fmt.hexToBytes(std.mem.asBytes(&t.termios), hex); + rec.ttys[rec.nttys] = t; + rec.nttys += 1; + } else if (std.mem.eql(u8, word, "refused")) { + rec.refused = rest; + } else if (std.mem.eql(u8, word, "exe")) { + rec.exe = rest; + } else if (std.mem.eql(u8, word, "dump")) { + rec.dump = rest; + } + // A word of a later build's record is passed over. + } + if (rec.dump.len == 0) return error.BadRecord; + return rec; + } +}; + +/// The record this process was started with (`--upgraded=`), set by main. +pub var resumed: ?Record = null; + +// ---- before: asking the new binary, and the exec ---- + +pub const Checked = union(enum) { ok: []const u8, no: []const u8 }; + +/// `path --version` run as it is: a binary not there, not a pardes, or one +/// that cannot start, found before anything is let go. Its first line +/// (`pardes `), or why not, in `buf`. +pub fn check(path: [:0]const u8, buf: []u8) Checked { + var pipe: [2]c_int = undefined; + if (libc.pipe(&pipe) != 0) return .{ .no = "Upgrade: no pipe to ask the new pardes its version" }; + const argv = [_:null]?[*:0]const u8{ path, "--version", null }; + const pid = libc.fork(); + if (pid < 0) { + _ = libc.close(pipe[0]); + _ = libc.close(pipe[1]); + return .{ .no = "Upgrade: no process to ask the new pardes its version" }; + } + if (pid == 0) { + // Only async-signal-safe calls between fork and exec. + _ = libc.dup2(pipe[1], 1); + const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR }, @as(libc.mode_t, 0)); + if (null_fd >= 0) { + _ = libc.dup2(null_fd, 0); + _ = libc.dup2(null_fd, 2); + } + _ = libc.execve(path, &argv, @ptrCast(libc.environ)); + libc._exit(127); + } + _ = libc.close(pipe[1]); + var len: usize = 0; + var waited = false; + while (len < buf.len) { + // A pardes answers --version in milliseconds; 10 s is one that hangs. + var fds = [_]libc.pollfd{.{ .fd = pipe[0], .events = libc.POLL.IN, .revents = 0 }}; + const ready = libc.poll(&fds, 1, 10_000); + if (ready < 0 and libc.errno(ready) == .INTR) continue; + if (ready <= 0) { + _ = libc.kill(pid, .KILL); + waited = true; + break; + } + const n = libc.read(pipe[0], buf[len..].ptr, buf.len - len); + if (n < 0 and libc.errno(n) == .INTR) continue; + if (n <= 0) break; + len += @intCast(n); + } + _ = libc.close(pipe[0]); + var status: c_int = 0; + while (libc.waitpid(pid, &status, 0) < 0 and libc.errno(-1) == .INTR) {} + const s: u32 = @bitCast(status); + const said = buf[0..len]; + const first = said[0 .. std.mem.indexOfScalar(u8, said, '\n') orelse said.len]; + if (!waited and libc.W.IFEXITED(s) and libc.W.EXITSTATUS(s) == 0 and std.mem.startsWith(u8, first, "pardes ")) return .{ .ok = first }; + // The words go where the line was: the rest of `buf`. + var tail: [96]u8 = undefined; + const how = if (waited) + "did not answer --version in 10 s" + else if (libc.W.IFEXITED(s) and libc.W.EXITSTATUS(s) == 127) + "would not start" + else if (libc.W.IFEXITED(s) and libc.W.EXITSTATUS(s) != 0) + std.fmt.bufPrint(&tail, "exited {d} at --version", .{libc.W.EXITSTATUS(s)}) catch "failed at --version" + else if (libc.W.IFSIGNALED(s)) + std.fmt.bufPrint(&tail, "died of signal {d} at --version", .{libc.W.TERMSIG(s)}) catch "died at --version" + else + "does not answer --version as a pardes does"; + return .{ .no = std.fmt.bufPrint(buf, "Upgrade: {s} {s}; this session stays as it is", .{ path[0..@min(path.len, 400)], how }) catch "Upgrade: the new pardes would not start; this session stays as it is" }; +} + +/// The binary this process runs, open, for a new one to fall back to: on +/// Linux the file even once deleted. ponytail: FreeBSD has no /proc by +/// default, so a new build there that cannot load a dump starts afresh. +pub fn openSelf() c_int { + if (comptime builtin.os.tag != .linux) return -1; + return libc.open("/proc/self/exe", .{ .ACCMODE = .RDONLY, .CLOEXEC = true }, @as(libc.mode_t, 0)); +} + +/// This process exec'd as `path` (or the binary open on `old`), the record +/// on its command line and the fds it names kept open. Returns only when +/// the exec failed, those fds close-on-exec again: why, in words. +pub fn exec(gpa: std.mem.Allocator, path: ?[:0]const u8, old: c_int, rec: *const Record) []const u8 { + var text: std.Io.Writer.Allocating = .init(gpa); + defer text.deinit(); + text.writer.writeAll(flag) catch return "no memory for its record"; + rec.encode(&text.writer) catch return "no memory for its record"; + text.writer.writeByte(0) catch return "no memory for its record"; + const record: [*:0]const u8 = @ptrCast(text.written().ptr); + const argv = gpa.allocSentinel(?[*:0]const u8, given.len + 2, null) catch return "no memory for its arguments"; + defer gpa.free(argv); + argv[0] = if (path) |p| p.ptr else if (exe()) |e| e.ptr else "pardes"; + argv[1] = record; + for (given, argv[2..]) |a, *slot| slot.* = a.ptr; + rec.closeOnExec(false); + const envp: [*:null]const ?[*:0]const u8 = @ptrCast(libc.environ); + const r = if (path) |p| + libc.execve(p, argv.ptr, envp) + else if (comptime builtin.os.tag == .linux) + libc.execveat(old, "", argv.ptr, envp, .{ .SYMLINK_NOFOLLOW = false, .EMPTY_PATH = true }) + else + -1; + const e = libc.errno(r); + rec.closeOnExec(true); + return switch (e) { + .NOENT => "it is not there", + .ACCES, .PERM => "it may not be run", + .NOEXEC => "it is not a program this system runs", + .@"2BIG" => "its record and arguments are too long", + .NOMEM => "no memory for it", + .TXTBSY => "it is still being written", + else => "the exec failed", + }; +} + +/// The dump of a session to `path` (0600, replaced): false when it could +/// not be written whole. +pub fn writeDump(path: [:0]const u8, bytes: []const u8) bool { + const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true, .CLOEXEC = true }, @as(libc.mode_t, 0o600)); + if (fd < 0) return false; + defer _ = libc.close(fd); + var at: usize = 0; + while (at < bytes.len) { + const n = libc.write(fd, bytes[at..].ptr, bytes.len - at); + if (n < 0 and libc.errno(n) == .INTR) continue; + if (n <= 0) { + _ = libc.unlink(path); + return false; + } + at += @intCast(n); + } + return true; +} + +// ---- after: the new process ---- + +/// The core an upgraded process starts with: the session its dump is. A +/// build that cannot load it execs the binary it replaced to load it; with +/// none, a fresh session (`fresh`) the host adopts the shells into, the +/// dump left where a Restore finds it. `PARDES_TEST_UPGRADE_REFUSE` refuses +/// a load as such a build would (test/daemon.py's failing upgrade), in the +/// first start only, or (`all`) in the start it falls back to as well. +pub fn load(gpa: std.mem.Allocator, core_gpa: std.mem.Allocator, opts: pardes.Options, rec: *Record) !*pardes.Pardes { + var kept: [pardes.MAX_PANES]bool = @splat(false); + for (rec.ptys[0..rec.nptys]) |p| kept[p.pane] = true; + var path_buf: [4096:0]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&path_buf, "{s}", .{rec.dump}, 0) catch ""; + const loaded = loaded: { + if (libc.getenv("PARDES_TEST_UPGRADE_REFUSE")) |how| if (rec.refused.len == 0 or std.mem.eql(u8, std.mem.span(how), "all")) + break :loaded error.RefusedByTest; + const bytes = pardes.filesystem.readFile(gpa, rec.dump) catch |err| break :loaded err; + defer gpa.free(bytes); + break :loaded pardes.dump.resumeFromDump(core_gpa, opts, bytes, &kept); + }; + var buf: [700]u8 = undefined; + if (loaded) |p| { + _ = libc.unlink(path); + p.setMessage(p.active, if (rec.refused.len > 0) + std.fmt.bufPrint(&buf, "Upgrade: the new build could not load this session ({s}); it goes on as {s}", .{ rec.refused[0..@min(rec.refused.len, 200)], version_line }) catch "Upgrade: the new build could not load this session; it goes on as before" + else + "Upgrade: now " ++ version_line); + return p; + } else |err| { + var words_buf: [128]u8 = undefined; + const why = if (err == error.RefusedByTest) "refused by PARDES_TEST_UPGRADE_REFUSE" else pardes.Messages.errorWords(err, &words_buf); + // The binary it replaced loads its own dump: tried once, never from + // a start that is that fallback already. + if (rec.old >= 0 and rec.refused.len == 0) { + var again = rec.*; + again.refused = why; + again.old = -1; + _ = exec(gpa, null, rec.old, &again); + } + if (rec.old >= 0) _ = libc.close(rec.old); + rec.old = -1; + rec.fresh = true; + var o = opts; + o.load_path = null; + const p = try pardes.Pardes.init(core_gpa, o); + p.setMessage(p.active, std.fmt.bufPrint(&buf, "Upgrade: no build could load the session ({s}); its shells are here, and Restore {s} brings the rest back", .{ why, rec.dump[0..@min(rec.dump.len, 400)] }) catch "Upgrade: no build could load the session; its dump is kept for a Restore"); + return p; + } +} + +test "an upgrade's record reads back as it was written, and refuses one cut short" { + var rec: Record = .{ .link = 5, .stub = 7, .old = 9, .window = true, .exe = "/a b/pardes", .dump = "/run/u/pardes-upgrade-1.dump" }; + rec.addPty(3, 1234, 11); + rec.addPty(0, 99, 12); + var tio: posix.termios = std.mem.zeroes(posix.termios); + std.mem.asBytes(&tio)[0] = 0x5a; + rec.addTty(.{ .view = 2, .fd = 13, .stub = 14, .primary = true, .termios = tio }); + var buf: [4096]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + try rec.encode(&w); + const back = try Record.decode(w.buffered()); + try std.testing.expectEqual(@as(c_int, 5), back.link); + try std.testing.expectEqual(@as(c_int, 9), back.old); + try std.testing.expect(back.window); + try std.testing.expectEqual(@as(usize, 2), back.nptys); + try std.testing.expectEqual(Pty{ .pane = 3, .pid = 1234, .fd = 11 }, back.ptys[0]); + try std.testing.expectEqual(@as(usize, 1), back.nttys); + try std.testing.expect(back.ttys[0].primary and back.ttys[0].view == 2 and back.ttys[0].stub == 14); + try std.testing.expectEqual(@as(u8, 0x5a), std.mem.asBytes(&back.ttys[0].termios)[0]); + try std.testing.expectEqualStrings("/a b/pardes", back.exe); + try std.testing.expectEqualStrings("/run/u/pardes-upgrade-1.dump", back.dump); + try std.testing.expectError(error.BadRecord, Record.decode("link 5\n")); + try std.testing.expectError(error.BadRecord, Record.decode("tty 0 1 2 1 abc\ndump x\n")); +} diff --git a/test/daemon.py b/test/daemon.py index 2be3f355..7d35e427 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -506,8 +506,321 @@ def gui(binary, cycles): shutil.rmtree(root, ignore_errors=True) +def screen_text(client): + shown = client.screen() + cells = ''.join(cell[0] for cell in shown['cells']) + return '\n'.join(cells[i:i + shown['cols']] for i in range(0, len(cells), shown['cols'])) + + +def reconnect(sock, timeout=20): + """The session's 9P again, once its upgraded process listens.""" + return wait_for('the upgraded session\'s 9P', lambda: dial(sock), timeout=timeout) + + +def dial(sock): + try: + return Client(sock) + except (OSError, EOFError): # mid-exec: refused, or cut after the accept + return None + + +def saying(sock, words, timeout=30): + """A connection to the session once its screen shows `words`: dialled + afresh each look, since the process it talks to may exec meanwhile.""" + last = [''] + + def look(): + c = dial(sock) + if c is None: + return None + try: + last[0] = screen_text(c) + if words in last[0]: + return c + except (OSError, EOFError): + pass + c.socket.close() + return None + try: + return wait_for(f'the session saying {words!r}', look, timeout=timeout) + except TimeoutError: + raise TimeoutError(f'the session saying {words!r}; it shows:\n{last[0]}') + + +def exe_of(pid): + """What /proc says the process runs: (its path, whether that file is the + one at the path now).""" + link = os.readlink(f'/proc/{pid}/exe') if procfs else ps('-o', 'comm=', '-p', str(pid)).decode().strip() + if not procfs: + return link, True + try: + same = os.stat(f'/proc/{pid}/exe').st_ino == os.stat(link).st_ino + except OSError: + same = False + return link, same + + +def ppid(pid): + if not procfs: + return int(ps('-o', 'ppid=', '-p', str(pid))) + return int(open(f'/proc/{pid}/stat').read().rsplit(')', 1)[1].split()[1]) + + +def install(binary, at): + """As `zig build` and install(1) put a build in place: a new file + renamed over the old, which a process running it keeps, deleted.""" + tmp = at.with_name(at.name + '.new') + subprocess.run(['cp', '--reflink=auto', os.fspath(binary), os.fspath(tmp)], check=True) + os.chmod(tmp, 0o755) + os.replace(tmp, at) + + +def upgrades(old, new): + """Upgrade (docs/next-daemon.md, phase 6): a tty session with an unsaved + edit, a shell running `sleep 1000; echo done` and two terminals (two + views) moved onto another build installed at the path it started from, + the daemon with it; then a build that refuses the dump, which falls back + to the running one, and one that refuses it everywhere, which starts + afresh with the shells adopted and the dump kept.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + scratch.mkdir(parents=True, exist_ok=True) + root = Path(tempfile.mkdtemp(prefix='rtu-', dir=scratch)) + env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and 'PAGER' not in k} + env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), + TERM='xterm-256color', SHELL=shutil.which('bash') or '/bin/bash', PARDES_NOTIME='1') + (root / 'config' / 'pardes').mkdir(parents=True) + (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n') + (root / 'bin').mkdir() + binary = root / 'bin' / 'pardes' + install(old, binary) + notes = root / 'notes.txt' + notes.write_text('one\ntwo\n') + started = [] + try: + term = Term() + stub = term.spawn([str(binary), '--tty', '--9p=work'], env) + started.append(stub.pid) + sock = root / 'pardes-9p-work.sock' + wait_for('the session\'s 9P socket', sock.exists) + client = Client(sock) + session = int(dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())['pid']) + started.append(session) + daemon = wait_for('the daemon', lambda: [p for p in processes(root) if cmdline(p)[1:2] == [b'--daemon']])[0] + started.append(daemon) + shell = terminals(client)[0] + shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) + started.append(shell_pid) + client.write(f'/pane/{shell}/look', f'{notes}\n'.encode()) + text = int(client.read('/look')) + client.write(f'/pane/{text}/body', b'unsaved\n') + assert client.read(f'/pane/{text}/body') == b'one\ntwo\nunsaved\n' + client.write(f'/pane/{shell}/pty/data', b'sleep 1000; echo done-after-upgrade\r') + sleeper = wait_for('the sleep', lambda: [p for p in processes(root) if cmdline(p)[:2] == [b'sleep', b'1000']])[0] + # A second terminal: a view of its own. + term2 = Term(30, 100) + stub2 = term2.spawn([str(binary), '--attach=work'], env) + started.append(stub2.pid) + wait_for('the second terminal\'s first frame', lambda: b'\x1b[?1049h' in term2.text()) + old_build = semantics(old) + replica = Replica(root / 'pardes-replica.sock') + assert replica.hello('work', old_build)[0] == 'welcome' + before = len(term.text()), len(term2.text()) + + install(new, binary) + assert exe_of(session)[1] is False, 'the session runs the old build, deleted' + client.write(f'/pane/{shell}/exec', b'Upgrade\n') + client.socket.close() + wait_for('the session on the new build', lambda: exe_of(session) == (str(binary), True), timeout=30) + client = reconnect(sock) + assert int(dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())['pid']) == session + wait_for('the daemon on the new build', lambda: exe_of(daemon) == (str(binary), True)) + assert alive(daemon) and stub.poll() is None and stub2.poll() is None + # A replica of the old build is told why it is let go. + tag, body = replica.read() + assert tag == 0x02 and b'upgrading to another build' in body, (tag, body) + replica.close() + # The edit and its undo. + assert client.read(f'/pane/{text}/body') == b'one\ntwo\nunsaved\n' + assert any(row.split()[0] == str(text).encode() and row.split()[2] == b'1' for row in client.read('/index').splitlines()), client.read('/index') + client.write(f'/pane/{text}/exec', b'Undo\n') + assert client.read(f'/pane/{text}/body') == b'one\ntwo\n', client.read(f'/pane/{text}/body') + # The shell, its job and its pty: the same, the session's still. + assert alive(shell_pid) and ppid(shell_pid) == session and alive(sleeper) + # Both terminals drawn again, each its own view. + wait_for('the first terminal redrawn', lambda: b'\x1b[?1049h' in term.text()[before[0]:]) + wait_for('the second terminal redrawn', lambda: b'\x1b[?1049h' in term2.text()[before[1]:]) + wait_for('the word said', lambda: 'Upgrade: now pardes' in screen_text(client)) + # The daemon lists the session under its new build. + new_build = semantics(new) + assert new_build != old_build + said = Replica(root / 'pardes-replica.sock').hello('work', new_build) + assert said == ('welcome', session, 'work'), said + said = Replica(root / 'pardes-replica.sock').hello('work', old_build) + assert said[0] == 'refused' and 'another build' in said[1], said + os.kill(sleeper, signal.SIGTERM) + wait_for('the shell carrying on', lambda: b'done-after-upgrade\n' in client.read(f'/pane/{shell}/body')) + print(f'upgrade: session {session}, its daemon {daemon}, shell {shell_pid} and two terminals on the new build; ' + 'the edit, its undo and the job kept') + + # A build that cannot load the dump: back to the one running. + refuser = root / 'refuser' + refuser.write_text('#!/bin/sh\n# A pardes whose load refuses the dump (upgrade.zig).\n' + f'case "$1" in --upgraded=*) PARDES_TEST_UPGRADE_REFUSE=1 exec {new} "$@";; esac\n' + f'exec {new} "$@"\n') + refuser.chmod(0o755) + running = os.stat(f'/proc/{session}/exe').st_ino + install(refuser, binary) + client.write(f'/pane/{shell}/exec', b'Upgrade\n') + client.socket.close() + client = saying(sock, 'it goes on as') + assert os.stat(f'/proc/{session}/exe').st_ino == running, 'back on the build that ran' + assert alive(shell_pid) and ppid(shell_pid) == session and stub.poll() is None + assert client.read(f'/pane/{text}/body') == b'one\ntwo\n' + assert run(client, shell, b'echo $$\n').split(b'\n')[1] == str(shell_pid).encode() + print('upgrade: a build that refused the dump fell back to the one running; the shell ran on') + + # One that refuses it everywhere: a fresh session, the shells adopted. + refuser.write_text(refuser.read_text().replace('=1 exec', '=all exec')) + install(refuser, binary) + client.write(f'/pane/{shell}/exec', b'Upgrade\n') + client.socket.close() + client = saying(sock, 'brings the rest back') + kept = root / '.local' / 'share' / 'pardes' / f'pardes-upgrade-{session}.dump' + assert kept.exists(), list((root / '.local' / 'share' / 'pardes').iterdir()) + assert alive(shell_pid) and ppid(shell_pid) == session + pids = {p: run(client, p, b'echo $$\n') for p in terminals(client)} + adopted = [p for p, out in pids.items() if out.split(b'\n')[1:2] == [str(shell_pid).encode()]] + assert adopted, ('the shell is a pane of the fresh session', shell_pid, pids, client.read('/index')) + print(f'upgrade: a build that refused it everywhere started afresh, shell {shell_pid} adopted, the dump kept') + + # The daemon, three times upgraded, still holds the shell's master: + # the session killed outright, another adopts it. + install(new, binary) + os.kill(session, signal.SIGKILL) + stub.wait(timeout=10) + stub2.wait(timeout=10) + client.socket.close() + term.close() + term2.close() + wait_for('the killed session gone', lambda: not alive(session)) + time.sleep(.3) + assert alive(shell_pid) and alive(daemon) + term = Term() + stub = term.spawn([str(binary), '--tty', '--9p=again'], env) + started.append(stub.pid) + sock = root / 'pardes-9p-again.sock' + wait_for('the next session', sock.exists) + client = Client(sock) + started.append(int(dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())['pid'])) + before = set(terminals(client)) + client.write(f'/pane/{min(before)}/exec', b'Adopt\n') + pane = wait_for('the adopted pane', lambda: set(terminals(client)) - before).pop() + assert run(client, pane, b'echo $$\n').split(b'\n')[1] == str(shell_pid).encode() + print(f'upgrade: the upgraded daemon kept shell {shell_pid} through kill -9 of its session, and it was adopted') + client.write(f'/pane/{pane}/exec', b'Exit\n') + stub.wait(timeout=10) + client.socket.close() + term.close() + wait_for('the shell hung up', lambda: not alive(shell_pid)) + wait_for('the daemon gone once idle', lambda: not alive(daemon), timeout=5) + left = [p for p in processes(root) if alive(p)] + assert not left, [cmdline(p) for p in left] + print('upgrade: nothing left behind') + finally: + for pid in started + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + + +def gui_upgrades(old, new): + """Upgrade in a window session (test mode: hidden windows, captured): + two windows, an unsaved edit and a shell's job, onto another build.""" + scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' + root = Path(tempfile.mkdtemp(prefix='rtgu-', dir=scratch)) + env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and 'PAGER' not in k} + display = Path(os.environ.get('WAYLAND_DISPLAY', 'wayland-0')) + if not display.is_absolute(): + display = Path(os.environ.get('XDG_RUNTIME_DIR', f'/run/user/{os.getuid()}')) / display + caps = root / 'caps' + caps.mkdir() + env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), + SHELL='/bin/bash', PARDES_NOTIME='1', PARDES_TEST='1', PARDES_TEST_CAPTURE_DIR=str(caps), + SDL_VIDEODRIVER='wayland', WAYLAND_DISPLAY=str(display), PARDES_DAEMON='on') + (root / 'config' / 'pardes').mkdir(parents=True) + (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n') + (root / 'bin').mkdir() + binary = root / 'bin' / 'pardes-gui' + install(old, binary) + notes = root / 'notes.txt' + notes.write_text('one\ntwo\n') + feed_r, feed_w = os.pipe() + proc = subprocess.Popen([str(binary), '--9p=win'], env=env, cwd=root, stdin=feed_r, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) + os.close(feed_r) + started = [proc.pid] + try: + sock = root / 'pardes-9p-win.sock' + wait_for('the window session', sock.exists, timeout=20) + client = Client(sock) + daemon = wait_for('the daemon', lambda: [p for p in processes(root) if cmdline(p)[1:2] == [b'--daemon']])[0] + started.append(daemon) + shell = terminals(client)[0] + shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1]) + started.append(shell_pid) + client.write(f'/pane/{shell}/look', f'{notes}\n'.encode()) + text = int(client.read('/look')) + client.write(f'/pane/{text}/body', b'unsaved\n') + client.write(f'/pane/{shell}/pty/data', b'sleep 1000; echo done-after-upgrade\r') + sleeper = wait_for('the sleep', lambda: [p for p in processes(root) if cmdline(p)[:2] == [b'sleep', b'1000']])[0] + attach = subprocess.run([str(binary), '--attach=win'], env=env, cwd=root, timeout=10, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + assert attach.returncode == 0, attach.stderr + frames = [caps / 'latest.ppm', caps / 'view-1' / 'latest.ppm'] + wait_for('both windows drawn', lambda: all(f.exists() for f in frames)) + install(new, binary) + time.sleep(.05) + mark = time.time() + client.write(f'/pane/{shell}/exec', b'Upgrade\n') + client.socket.close() + wait_for('the session on the new build', lambda: exe_of(proc.pid) == (str(binary), True), timeout=30) + client = reconnect(sock) + assert proc.poll() is None + wait_for('the daemon on the new build', lambda: exe_of(daemon) == (str(binary), True)) + assert client.read(f'/pane/{text}/body') == b'one\ntwo\nunsaved\n' + client.write(f'/pane/{text}/exec', b'Undo\n') + assert client.read(f'/pane/{text}/body') == b'one\ntwo\n' + assert alive(shell_pid) and ppid(shell_pid) == proc.pid and alive(sleeper) + wait_for('both windows drawn again', lambda: all(f.stat().st_mtime > mark for f in frames)) + said = Replica(root / 'pardes-replica.sock').hello('win', semantics(new)) + assert said == ('welcome', proc.pid, 'win'), said + os.kill(sleeper, signal.SIGTERM) + wait_for('the shell carrying on', lambda: b'done-after-upgrade\n' in client.read(f'/pane/{shell}/body')) + print(f'upgrade: window session {proc.pid}, its daemon, shell {shell_pid} and two windows on the new build; ' + 'the edit, its undo and the job kept') + client.write(f'/pane/{shell}/exec', b'Exit\n') + proc.wait(timeout=10) + client.socket.close() + wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=5) + print('upgrade: window session ended, nothing left behind') + finally: + os.close(feed_w) + for pid in started + processes(root): + try: + os.kill(pid, signal.SIGKILL) + except OSError: + pass + shutil.rmtree(root, ignore_errors=True) + + if __name__ == '__main__': - if sys.argv[1:2] == ['--ping']: + if sys.argv[1:3] == ['--upgrade', '--gui']: + gui_upgrades(Path(sys.argv[3]).resolve(), Path(sys.argv[4]).resolve()) + elif sys.argv[1:2] == ['--upgrade']: + upgrades(Path(sys.argv[2]).resolve(), Path(sys.argv[3]).resolve()) + elif sys.argv[1:2] == ['--ping']: ping(sys.argv[2], sys.argv[3], sys.argv[4], int(sys.argv[5]) if len(sys.argv) > 5 else 50) elif '--gui' in sys.argv: args = [a for a in sys.argv[1:] if a != '--gui'] -- cgit v1.3 From 370913ae34c69b7958911c6146612c77275f6ebf Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 22:56:07 -0300 Subject: upgrade: a newer build at the path a session started from is said at a focus-in, once a build, the path read just after the first frame (off a startup's way); an upgraded process lets the binary it replaced go (held, its deleted file kept its blocks) and sweeps its old image's prompt files (kept for an exec that failed); test/daemon.py --upgrade asks through /ctl, checks the fds and prompt files level, and runs on FreeBSD (procstat, the daemon's registry, no kept old binary there) Co-Authored-By: Claude Opus 5.5 --- src/daemon.zig | 2 +- src/gui/gui.zig | 7 +++-- src/host_io.zig | 6 ++-- src/tty/tty.zig | 7 +++-- src/upgrade.zig | 50 +++++++++++++++++++++++++----- test/daemon.py | 94 +++++++++++++++++++++++++++++++++++++++------------------ 6 files changed, 123 insertions(+), 43 deletions(-) (limited to 'src/host_io.zig') diff --git a/src/daemon.zig b/src/daemon.zig index 43f15c6c..785f4afd 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -1087,7 +1087,7 @@ pub fn serve(state: ?[]const u8) noreturn { ninep_io.setCloexec(4); const ignore: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.IGN }, .mask = posix.sigemptyset(), .flags = 0 }; for ([_]posix.SIG{ .PIPE, .HUP, .INT }) |sig| posix.sigaction(sig, &ignore, null); - upgrade.remember(&.{}); + upgrade.locate(); var d: Daemon = .{}; const given = if (state) |text| d.restore(text) else -1; const replicas = if (given >= 0) given else listenReplicas(); diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 24061251..e4d69281 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -2502,6 +2502,7 @@ fn localSession( while (!core.quit) { pardes.turn.bump(&pardes.turn.restores); try core.pump(host); + upgrade.locate(); // Asked in the step (the word), done between steps: the session whole. if (shell.upgrade_asked) |pane| shell.upgradeNow(pane); // A window asked for again, or an adopted shell, from the daemon's @@ -3138,7 +3139,10 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { in.close(); }, c.SDL_EVENT_WINDOW_CLOSE_REQUESTED => in.close(), - c.SDL_EVENT_WINDOW_FOCUS_GAINED => in.post(.{ .focus = true }), + c.SDL_EVENT_WINDOW_FOCUS_GAINED => { + in.post(.{ .focus = true }); + if (in.core) |core| upgrade.noticeNewer(core); + }, c.SDL_EVENT_WINDOW_FOCUS_LOST => in.post(.{ .focus = false }), c.SDL_EVENT_WINDOW_MOUSE_LEAVE => { g.pointer_present = false; @@ -4060,7 +4064,6 @@ const Shell = struct { if (!upgrade.writeDump(at, bytes)) break :failed std.fmt.bufPrint(&why, "Upgrade: the session's dump could not be written to {s}; it stays as it is", .{at[0..@min(at.len, 600)]}) catch "Upgrade: its dump could not be written"; rec.dump = at; rec.old = upgrade.openSelf(); - host_io.Shell.unlinkOwnedRcs(); daemon.upgraded(); if (s.fs) |f| f.flushOut(); const err_words = upgrade.exec(s.gpa, path, -1, &rec); diff --git a/src/host_io.zig b/src/host_io.zig index 46ee9afc..c627d4ab 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -977,8 +977,10 @@ pub const Shell = struct { 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; + // This pid's are this process's, but in an upgraded one: those + // were its old image's (upgrade.zig), and its own come after. + if (pid == std.c.getpid() and @import("upgrade.zig").resumed == null) continue; + if (pid != std.c.getpid() and 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; diff --git a/src/tty/tty.zig b/src/tty/tty.zig index ef7688bc..14dc8042 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -1564,6 +1564,7 @@ fn localSession( while (!core.quit) { pardes.turn.bump(&pardes.turn.restores); try core.pump(host); + upgrade.locate(); // Asked in the step (the word), done between steps: the session whole. if (sh.upgrade_asked) |pane| sh.upgradeNow(pane); // A terminal (`pardes --attach`) or an adopted shell, handed on by @@ -1973,7 +1974,6 @@ const Shell = struct { }; popped = true; unlinkKittyShm(); - host_io.Shell.unlinkOwnedRcs(); daemon.upgraded(); if (s.fs) |f| f.flushOut(); const err_words = upgrade.exec(s.gpa, path, -1, &rec); @@ -2261,7 +2261,10 @@ const Shell = struct { }, .attach => |a| s.attachTerminal(a.tty, a.stub, null), .adopted => |a| s.adoptShell(a.pid, a.fd), - .focus_in => core.update(.{ .focus = true }), + .focus_in => { + core.update(.{ .focus = true }); + upgrade.noticeNewer(core); + }, .focus_out => { core.update(.pointer_leave); core.update(.{ .focus = false }); diff --git a/src/upgrade.zig b/src/upgrade.zig index c49d2370..49e8aff5 100644 --- a/src/upgrade.zig +++ b/src/upgrade.zig @@ -32,21 +32,53 @@ var exe_buf: [4096:0]u8 = undefined; var exe_len: usize = 0; /// The arguments it was started with (an upgrade's record left out). var given: []const [:0]const u8 = &.{}; +/// The file at that path when it started, and the newer one last said. +var started_inode: ?u64 = null; +var told_inode: ?u64 = null; -/// Its path asked of the system now, at its start (one readlink), and its -/// arguments kept; an upgraded process takes the path its record names -/// (the system would name a fallback's old binary, deleted). +var located = false; + +/// Its arguments kept, at its start; an upgraded process's path is the one +/// its record names (the system would name a fallback's old binary, +/// deleted). pub fn remember(args: []const [:0]const u8) void { given = if (args.len > 1 and std.mem.startsWith(u8, args[1], flag)) args[2..] else if (args.len > 0) args[1..] else &.{}; if (resumed) |*rec| if (rec.exe.len > 0 and rec.exe.len < exe_buf.len) { @memcpy(exe_buf[0..rec.exe.len], rec.exe); exe_buf[rec.exe.len] = 0; exe_len = rec.exe.len; - return; }; - const n = std.process.executablePath(std.Io.Threaded.global_single_threaded.io(), &exe_buf) catch 0; - exe_buf[n] = 0; - exe_len = n; +} + +/// Its path asked of the system (a readlink) and the file there now (a +/// stat), once: after the first frame, off a startup's way (the hosts' +/// loops; nothing is reinstalled within a frame of a start). +pub fn locate() void { + if (located) return; + located = true; + if (exe_len == 0) { + const n = std.process.executablePath(std.Io.Threaded.global_single_threaded.io(), &exe_buf) catch 0; + exe_buf[n] = 0; + exe_len = n; + } + if (exe()) |path| started_inode = inodeOf(path); +} + +fn inodeOf(path: [:0]const u8) ?u64 { + const st = std.Io.Dir.cwd().statFile(std.Io.Threaded.global_single_threaded.io(), path, .{}) catch return null; + return @intCast(st.inode); +} + +/// A newer build installed at that path since this process started, said +/// in a line, once a build: asked at a focus-in, where one comes back from +/// rebuilding (a stat). ponytail: explicit only, no watch and no automatic +/// upgrade. +pub fn noticeNewer(core: *pardes.Pardes) void { + const was = started_inode orelse return; + const now = inodeOf(exe() orelse return) orelse return; + if (now == was or told_inode == now) return; + told_inode = now; + core.setMessage(core.active, "a newer pardes is installed: Upgrade (SPC s U) moves this session onto it"); } pub fn exe() ?[:0]const u8 { @@ -336,6 +368,10 @@ pub fn load(gpa: std.mem.Allocator, core_gpa: std.mem.Allocator, opts: pardes.Op var buf: [700]u8 = undefined; if (loaded) |p| { _ = libc.unlink(path); + // The binary it replaced, let go: held, its file (deleted) would + // keep its blocks for as long as this session runs. + if (rec.old >= 0) _ = libc.close(rec.old); + rec.old = -1; p.setMessage(p.active, if (rec.refused.len > 0) std.fmt.bufPrint(&buf, "Upgrade: the new build could not load this session ({s}); it goes on as {s}", .{ rec.refused[0..@min(rec.refused.len, 200)], version_line }) catch "Upgrade: the new build could not load this session; it goes on as before" else diff --git a/test/daemon.py b/test/daemon.py index 7d35e427..ce19b502 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -549,10 +549,11 @@ def saying(sock, words, timeout=30): def exe_of(pid): """What /proc says the process runs: (its path, whether that file is the - one at the path now).""" - link = os.readlink(f'/proc/{pid}/exe') if procfs else ps('-o', 'comm=', '-p', str(pid)).decode().strip() + one at the path now). FreeBSD's procstat names the path alone.""" if not procfs: - return link, True + rows = subprocess.run(['procstat', '-b', str(pid)], capture_output=True).stdout.decode().splitlines() + return (rows[-1].split()[-1] if len(rows) > 1 else ''), None + link = os.readlink(f'/proc/{pid}/exe') try: same = os.stat(f'/proc/{pid}/exe').st_ino == os.stat(link).st_ino except OSError: @@ -570,7 +571,10 @@ def install(binary, at): """As `zig build` and install(1) put a build in place: a new file renamed over the old, which a process running it keeps, deleted.""" tmp = at.with_name(at.name + '.new') - subprocess.run(['cp', '--reflink=auto', os.fspath(binary), os.fspath(tmp)], check=True) + if procfs: + subprocess.run(['cp', '--reflink=auto', os.fspath(binary), os.fspath(tmp)], check=True) + else: + shutil.copyfile(binary, tmp) os.chmod(tmp, 0o755) os.replace(tmp, at) @@ -627,18 +631,45 @@ def upgrades(old, new): before = len(term.text()), len(term2.text()) install(new, binary) - assert exe_of(session)[1] is False, 'the session runs the old build, deleted' - client.write(f'/pane/{shell}/exec', b'Upgrade\n') + assert not procfs or exe_of(session)[1] is False, 'the session runs the old build, deleted' + # Come back to the terminal: the newer build is said. + os.write(term.master, b'\x1b[I') + wait_for('the newer build said', lambda: 'a newer pardes is installed' in screen_text(client)) + fds_before = fds(session), fds(daemon) + rcs = sorted(Path('/tmp').glob(f'pardes-osc133-*-{session}-*')) + t = time.monotonic() + client.write('/ctl', b'Upgrade\n') client.socket.close() - wait_for('the session on the new build', lambda: exe_of(session) == (str(binary), True), timeout=30) + new_build = semantics(new) + assert new_build != old_build + if procfs: + wait_for('the session on the new build', lambda: exe_of(session) == (str(binary), True), timeout=30) + else: + wait_for('the session registered as the new build', + lambda: Replica(root / 'pardes-replica.sock').hello('work', new_build)[0] == 'welcome', timeout=30) client = reconnect(sock) + took = time.monotonic() - t assert int(dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())['pid']) == session - wait_for('the daemon on the new build', lambda: exe_of(daemon) == (str(binary), True)) + if procfs: + wait_for('the daemon on the new build', lambda: exe_of(daemon) == (str(binary), True)) + else: + wait_for('the daemon exec\'d again', lambda: any(a.startswith(b'--upgraded=') for a in cmdline(daemon))) assert alive(daemon) and stub.poll() is None and stub2.poll() is None # A replica of the old build is told why it is let go. tag, body = replica.read() assert tag == 0x02 and b'upgrading to another build' in body, (tag, body) replica.close() + # The old image's prompt files swept, the new one's made. + wait_for('the prompt files level', lambda: len(list(Path('/tmp').glob(f'pardes-osc133-*-{session}-*'))) == len(rcs)) + assert sorted(Path('/tmp').glob(f'pardes-osc133-*-{session}-*')) != rcs or not rcs + # Nothing kept that was not: the old binary let go, every fd back. + # (The replica's connection is the one fewer.) + level = fds_before[0] - 1, fds_before[1] + try: + wait_for('the fds level', lambda: (fds(session), fds(daemon)) == level, timeout=5) + except TimeoutError: + raise AssertionError(('fds', level, (fds(session), fds(daemon)), + os.listdir(f'/proc/{session}/fd') if procfs else None)) # The edit and its undo. assert client.read(f'/pane/{text}/body') == b'one\ntwo\nunsaved\n' assert any(row.split()[0] == str(text).encode() and row.split()[2] == b'1' for row in client.read('/index').splitlines()), client.read('/index') @@ -651,37 +682,39 @@ def upgrades(old, new): wait_for('the second terminal redrawn', lambda: b'\x1b[?1049h' in term2.text()[before[1]:]) wait_for('the word said', lambda: 'Upgrade: now pardes' in screen_text(client)) # The daemon lists the session under its new build. - new_build = semantics(new) - assert new_build != old_build said = Replica(root / 'pardes-replica.sock').hello('work', new_build) assert said == ('welcome', session, 'work'), said said = Replica(root / 'pardes-replica.sock').hello('work', old_build) assert said[0] == 'refused' and 'another build' in said[1], said os.kill(sleeper, signal.SIGTERM) wait_for('the shell carrying on', lambda: b'done-after-upgrade\n' in client.read(f'/pane/{shell}/body')) - print(f'upgrade: session {session}, its daemon {daemon}, shell {shell_pid} and two terminals on the new build; ' - 'the edit, its undo and the job kept') + print(f'upgrade: session {session}, its daemon {daemon}, shell {shell_pid} and two terminals on the new build ' + f'(the word to its 9P again: {took * 1000:.0f} ms); the edit, its undo and the job kept') - # A build that cannot load the dump: back to the one running. + # A build that cannot load the dump: back to the one running (a copy + # of it kept open from /proc/self/exe; FreeBSD keeps none). refuser = root / 'refuser' - refuser.write_text('#!/bin/sh\n# A pardes whose load refuses the dump (upgrade.zig).\n' - f'case "$1" in --upgraded=*) PARDES_TEST_UPGRADE_REFUSE=1 exec {new} "$@";; esac\n' - f'exec {new} "$@"\n') - refuser.chmod(0o755) - running = os.stat(f'/proc/{session}/exe').st_ino - install(refuser, binary) - client.write(f'/pane/{shell}/exec', b'Upgrade\n') - client.socket.close() - client = saying(sock, 'it goes on as') - assert os.stat(f'/proc/{session}/exe').st_ino == running, 'back on the build that ran' - assert alive(shell_pid) and ppid(shell_pid) == session and stub.poll() is None - assert client.read(f'/pane/{text}/body') == b'one\ntwo\n' - assert run(client, shell, b'echo $$\n').split(b'\n')[1] == str(shell_pid).encode() - print('upgrade: a build that refused the dump fell back to the one running; the shell ran on') + + def refusing(how): + refuser.write_text('#!/bin/sh\n# A pardes whose load refuses the dump (upgrade.zig).\n' + f'case "$1" in --upgraded=*) PARDES_TEST_UPGRADE_REFUSE={how} exec {new} "$@";; esac\n' + f'exec {new} "$@"\n') + refuser.chmod(0o755) + install(refuser, binary) + if procfs: + running = os.stat(f'/proc/{session}/exe').st_ino + refusing('1') + client.write(f'/pane/{shell}/exec', b'Upgrade\n') + client.socket.close() + client = saying(sock, 'it goes on as') + assert os.stat(f'/proc/{session}/exe').st_ino == running, 'back on the build that ran' + assert alive(shell_pid) and ppid(shell_pid) == session and stub.poll() is None + assert client.read(f'/pane/{text}/body') == b'one\ntwo\n' + assert run(client, shell, b'echo $$\n').split(b'\n')[1] == str(shell_pid).encode() + print('upgrade: a build that refused the dump fell back to the one running; the shell ran on') # One that refuses it everywhere: a fresh session, the shells adopted. - refuser.write_text(refuser.read_text().replace('=1 exec', '=all exec')) - install(refuser, binary) + refusing('all') client.write(f'/pane/{shell}/exec', b'Upgrade\n') client.socket.close() client = saying(sock, 'brings the rest back') @@ -712,6 +745,9 @@ def upgrades(old, new): wait_for('the next session', sock.exists) client = Client(sock) started.append(int(dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines())['pid'])) + # Its reader of the daemon starts after its first frame: an Adopt + # before that waits for an `ok` nobody reads, and is refused. + wait_for('the next session\'s first frame', lambda: b'\x1b[?1049h' in term.text()) before = set(terminals(client)) client.write(f'/pane/{min(before)}/exec', b'Adopt\n') pane = wait_for('the adopted pane', lambda: set(terminals(client)) - before).pop() -- cgit v1.3 From f8a56c962d5103b84453d4305cd27973a71d4aa3 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 02:01:30 -0300 Subject: replica clients join again: one that diverged (a digest the session's unit did not end on) and one the session let go to upgrade dial it again, the same join as the first, and say why in a line over their frame until a key; a client of another build is told "session upgraded to a newer pardes: upgrade this client too" tty.zig `remote` is a loop of joins over one terminal: the link ended, its reader joined, the replica let go, the session dialled again (an upgrade's exec and load, and its daemon's, retried for 20 s), its dump loaded, its view's size sent again. `Remote.notify` draws the line (`noticeLine`, the client's own, never the session's core). The welcome carries the session's semantics: the daemon judged the hello by the hash it last heard, which an upgrade under way may not have said yet. replica.zig `Peers.refuseAll`: before an Upgrade's dump each client is sent `refused` after what it was sent (up to 200 ms) and let go, its view with it. "A newer pardes is installed" is a Host call (`newer_build`) the core makes in its focus step, so replica clients take the line from the tape; the GUI still says it itself. PARDES_TEST_DIVERGE= puts a client's core off the session's once quiet after its n-th unit. test/daemon.py: the clients test has one rejoin and agree; the upgrade test has a client of the old build told to upgrade and one of the build running join again and agree. Co-Authored-By: Claude Opus 5.5 --- src/detached/wire.zig | 6 +- src/host_io.zig | 3 + src/pardes.zig | 8 +- src/replica.zig | 36 +++++- src/tty/tty.zig | 305 ++++++++++++++++++++++++++++++++++++-------------- src/upgrade.zig | 15 ++- test/daemon.py | 56 ++++++++- 7 files changed, 332 insertions(+), 97 deletions(-) (limited to 'src/host_io.zig') diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 347d45d1..78f6e76e 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -134,7 +134,7 @@ pub const ServerMsg = union(ServerTag) { /// Who the client is now (its id and view in the session), how long the /// dump that follows is, and the session's offset from UTC (a replica /// guesses the session's time of day). - welcome: struct { pid: u32, session: []const u8, client: u16 = 0, view: u8 = 0, dump_len: u64 = 0, gmtoff: i32 = 0, digest: u64 = 0 }, + welcome: struct { pid: u32, session: []const u8, client: u16 = 0, view: u8 = 0, dump_len: u64 = 0, gmtoff: i32 = 0, digest: u64 = 0, semantics: u64 = 0 }, refused: []const u8, pong: u64, dump: []const u8, @@ -164,6 +164,7 @@ pub fn decodeServer(tag: u8, payload: []const u8) Error!ServerMsg { .dump_len = try r.takeInt(u64, .little), .gmtoff = try r.takeInt(i32, .little), .digest = try r.takeInt(u64, .little), + .semantics = try r.takeInt(u64, .little), } }, .refused => .{ .refused = try r.take(try r.takeInt(u16, .little)) }, .pong => .{ .pong = try r.takeInt(u64, .little) }, @@ -188,6 +189,7 @@ pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 { try w.writeInt(u64, h.dump_len, .little); try w.writeInt(i32, h.gmtoff, .little); try w.writeInt(u64, h.digest, .little); + try w.writeInt(u64, h.semantics, .little); }, .refused => |words| try putSlice16(&w, words), .pong => |v| try w.writeInt(u64, v, .little), @@ -620,7 +622,7 @@ test "detached wire: every Event variant round-trips" { const bytes = try encodeServer(&buf, .{ .welcome = .{ .pid = 42, .session = "work" } }); const f = (try framed(bytes)).?; try testing.expectEqual(@as(u8, 0x01), f.tag); - try testing.expectEqualSlices(u8, &.{ 42, 0, 0, 0, 4, 0, 'w', 'o', 'r', 'k', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, f.payload); + try testing.expectEqualSlices(u8, &.{ 42, 0, 0, 0, 4, 0, 'w', 'o', 'r', 'k', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, f.payload); const back = (try decodeServer(f.tag, f.payload)).welcome; try testing.expectEqualStrings("work", back.session); try testing.expectEqual(@as(u32, 42), back.pid); diff --git a/src/host_io.zig b/src/host_io.zig index c627d4ab..b1ed8b4b 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -32,6 +32,9 @@ pub const Host = struct { /// Upgrade: this process exec'd as the pardes installed since, at /// the step's end (upgrade.zig); `pane` hears why if it cannot be. upgrade: ?*const fn (ctx: ?*anyopaque, pane: u8) void = null, + /// Whether a newer pardes is installed at the path this process + /// started from, to be said once (upgrade.zig): asked at a focus-in. + newer_build: ?*const fn (ctx: ?*anyopaque) bool = 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, diff --git a/src/pardes.zig b/src/pardes.zig index cc55d695..6e01f8cd 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -3670,6 +3670,9 @@ pub const CellPixels = if (pdf_enabled) struct { h: u16 = 16, } else struct {}; +/// Said at a focus-in when the Host's `newer_build` answers yes (upgrade.zig). +pub const newer_build_words = "a newer pardes is installed: Upgrade (SPC s U) moves this session onto it"; + pub const Event = union(enum) { key: Key, mouse: Mouse, @@ -6338,7 +6341,10 @@ pub const Pardes = struct { .command => |line| _ = p.executeBuiltinLine(p.active, line), .pinch => |scale| p.ov_pinch_scale = scale, .touch_scroll => |delta| p.ov_touch_scroll_delta = delta, - .focus => |focused| p.window_focused = focused, + .focus => |focused| { + p.window_focused = focused; + if (focused) if (p.host.vtable.newer_build) |f| if (f(p.host.ctx)) p.setMessage(p.active, newer_build_words); + }, .handed => |h| p.handover.finish(p, h.seq, h.to, h.failure), .pointer_leave => { p.pointer_inside = false; diff --git a/src/replica.zig b/src/replica.zig index 7bf0574f..95135307 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -1140,6 +1140,8 @@ pub const Peer = struct { limit: usize = 64 << 20, closing: bool = false, cut: bool = false, + /// Its writer has ended (what it was sent, written). + done: std.atomic.Value(bool) = .init(false), /// `parts` queued for it, in order; never waits. A client whose queue /// passes its limit is cut off: its link is shut down, and its reader @@ -1169,6 +1171,7 @@ pub const Peer = struct { fn writeLoop(peer: *Peer) void { const io = peer.peers.io; + defer peer.done.store(true, .release); while (true) { peer.lock.lockUncancelable(io); while (peer.out.items.len == 0 and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); @@ -1457,7 +1460,7 @@ pub const Peers = struct { var out: [wire.hello_max + 64]u8 = undefined; // The session's digest as dumped, for the joiner to check its load by. const digest = tape.digestOf(core); - const welcome = wire.encodeServer(&out, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = k, .dump_len = bytes.len, .gmtoff = gmtoff(), .digest = digest } }) catch unreachable; + const welcome = wire.encodeServer(&out, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = k, .dump_len = bytes.len, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics } }) catch unreachable; peer.send(&.{welcome}); var at: usize = 0; while (at < bytes.len) : (at += dump_piece) { @@ -1535,6 +1538,33 @@ pub const Peers = struct { ps.gpa.destroy(peer); } + /// Every client told `why` and let go, its view with it: an Upgrade's + /// exec is next (tty.zig `upgradeNow`). What each was sent goes first, + /// for up to 200 ms; a client then dials the session again. + pub fn refuseAll(ps: *Peers, core: *Pardes, why: []const u8) void { + if (ps.count() == 0) return; + var out: [600]u8 = undefined; + const words = wire.encodeServer(&out, .{ .refused = why }) catch return; + const io = ps.io; + for (ps.list) |slot| if (slot) |peer| { + peer.send(&.{words}); + peer.lock.lockUncancelable(io); + peer.closing = true; + peer.cond.signal(io); + peer.lock.unlock(io); + }; + const until = nowNs() + 200 * std.time.ns_per_ms; + while (nowNs() < until) { + const busy = for (ps.list) |slot| { + if (slot) |peer| if (!peer.done.load(.acquire)) break true; + } else false; + if (!busy) break; + const ts: libc.timespec = .{ .sec = 0, .nsec = 2 * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); + } + ps.dropAll(core); + } + /// Detach typed in client view `k`'s: its link ends (its reader says /// it is gone, and the next boundary lets it go). pub fn detach(ps: *Peers, k: u8) bool { @@ -1612,6 +1642,9 @@ pub const Joined = struct { dump: []u8, /// The session's digest as dumped (`Replica.loaded`). digest: u64 = 0, + /// Its build's (wire.semantics): the daemon judged the hello by what it + /// last heard, which an upgrade under way may not have said yet. + semantics: u64 = 0, /// What was read past the dump: the stream's (`readStream`). rest: Framer = .{}, }; @@ -1657,6 +1690,7 @@ pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8) ?J r.j.view = w.view; r.j.gmtoff = w.gmtoff; r.j.digest = w.digest; + r.j.semantics = w.semantics; r.j.dump = r.gpa.alloc(u8, @intCast(w.dump_len)) catch { r.failed = "out of memory for the session's dump"; return false; diff --git a/src/tty/tty.zig b/src/tty/tty.zig index 2eafc785..e62da55c 100644 --- a/src/tty/tty.zig +++ b/src/tty/tty.zig @@ -160,6 +160,8 @@ pub const Command = struct { unit: []u8, /// ...a terminal's delta or rows, before the units after it. screen: []u8, + /// ...why it let this client go (an Upgrade's exec), the stream's end. + refused: []u8, /// The session's digest, asked by PARDES_REPLICA_CHECK. checked: u64, link_gone, @@ -1295,6 +1297,59 @@ const Remote = struct { /// frame shows the inputs after it too). mispredicted: u32 = 0, visible: u32 = 0, + /// What the session said as it let this client go (owned, the C heap's). + refused: ?[]u8 = null, + /// A line over its frame, until its next key (`notify`): why it joined + /// again. The client's own, never the session's core. + notice_buf: [240]u8 = undefined, + notice_len: usize = 0, + /// PARDES_TEST_DIVERGE=: once quiet after its n-th unit, its core + /// is put off the session's (a digested counter moved), the next unit's + /// digest finds it, and it joins again (test/daemon.py). + test_diverge: u32 = 0, + /// Put off so in this join: its checks are not written (they differ). + put_off: bool = false, + + /// The replica the dial's dump makes, checked against the session as + /// it was dumped; null, and why in `why`. The dump is let go. + fn load(gpa: std.mem.Allocator, joined: *replica.Joined, o: pardes.Options, why: []u8) ?*replica.Replica { + defer { + gpa.free(joined.dump); + joined.dump = &.{}; + } + const r = replica.Replica.initDump(gpa, joined.client, joined.view, joined.dump, o) catch |err| { + _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {}; + return null; + }; + if (!r.loaded(joined.digest)) { + r.deinit(); + _ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {}; + return null; + } + return r; + } + + /// A join's link, from its start. + fn reset(c: *Remote, r: *replica.Replica, fd: posix.fd_t) void { + c.r = r; + c.fd = fd; + c.quit = false; + c.gone = false; + c.dirty = true; + c.owed = false; + c.changed = false; + c.asked = false; + c.mispredicted = 0; + c.put_off = false; + } + + fn notify(c: *Remote, comptime fmt: []const u8, args: anytype) void { + const text = std.fmt.bufPrint(&c.notice_buf, fmt, args) catch c.notice_buf[0..]; + for (text) |*b| if (b.* < 0x20 or b.* > 0x7e) { + b.* = '?'; + }; + c.notice_len = text.len; + } /// PARDES_REPLICA_STATS=: how its inputs and the session's units /// went, a line at its end (test/daemon.py, the gates). @@ -1324,7 +1379,7 @@ const Remote = struct { fn checked(c: *Remote, theirs: u64) void { c.asked = false; const path = c.check orelse return; - if (!c.r.quiet()) return; + if (!c.r.quiet() or c.put_off) return; const mine = c.r.digest(); var buf: [96]u8 = undefined; const line = (if (mine == theirs) std.fmt.bufPrint(&buf, "agree {x:0>16}\n", .{mine}) else std.fmt.bufPrint(&buf, "differ {x:0>16} {x:0>16}\n", .{ mine, theirs })) catch return; @@ -1383,7 +1438,12 @@ fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loo const bytes = std.heap.c_allocator.dupe(u8, payload) catch return false; l.postEvent(.{ .screen = bytes }) catch std.heap.c_allocator.free(bytes); }, - .refused => return false, + .refused => { + const words = (wire.decodeServer(tag, payload) catch return false).refused; + const bytes = std.heap.c_allocator.dupe(u8, words) catch return false; + l.postEvent(.{ .refused = bytes }) catch std.heap.c_allocator.free(bytes); + return false; + }, .pong, .welcome, .dump => {}, } return true; @@ -1402,14 +1462,16 @@ fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loo pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, session: []const u8) !void { const io = init.io; const gpa = init.gpa; - var why: [600]u8 = @splat(0); - var joined = replica.dial(gpa, path, session, &why) orelse { - var said: [700]u8 = undefined; - std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: --attach: {s}\n", .{std.mem.sliceTo(&why, 0)}) catch "pardes: --attach failed\n") catch {}; - std.process.exit(1); + const Said = struct { + fn no(o: std.Io, comptime fmt: []const u8, args: anytype) noreturn { + var said: [800]u8 = undefined; + std.Io.File.stderr().writeStreamingAll(o, std.fmt.bufPrint(&said, "pardes: --attach: " ++ fmt ++ "\n", args) catch "pardes: --attach failed\n") catch {}; + std.process.exit(1); + } }; - defer _ = libc.close(joined.fd); - defer joined.rest.deinit(gpa); + var why: [600]u8 = @splat(0); + var joined = replica.dial(gpa, path, session, &why) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); + if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{upgrade_words}); const allocs = pardes.memory.init(gpa); defer pardes.memory.deinit(); @@ -1424,19 +1486,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se var o = opts; o.image_allocator = allocs.image; o.pdf_allocator = allocs.pdf; - const r = replica.Replica.initDump(gpa, joined.client, joined.view, joined.dump, o) catch |err| { - gpa.free(joined.dump); - var said: [200]u8 = undefined; - std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: --attach: the session's dump would not load: {t}\n", .{err}) catch "pardes: --attach: the dump would not load\n") catch {}; - std.process.exit(1); - }; - gpa.free(joined.dump); - defer r.deinit(); - r.gmtoff = joined.gmtoff; - if (!r.loaded(joined.digest)) { - std.Io.File.stderr().writeStreamingAll(io, "pardes: --attach: the session's dump loaded as another session (a build's dump is not complete)\n") catch {}; - std.process.exit(1); - } + var r = Remote.load(gpa, &joined, o, &why) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); var winch_set = posix.sigemptyset(); posix.sigaddset(&winch_set, posix.SIG.WINCH); @@ -1449,7 +1499,6 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se try screen.enter(try vaxis.Tty.init(io, &tty_buf)); defer screen.leave(); defer screen.glide.deinit(gpa); - screen.feed.view = joined.view; var paste_buf: std.Io.Writer.Allocating = .init(gpa); defer paste_buf.deinit(); var loop: Loop = .init(io, &screen.tty, &screen.vx); @@ -1458,6 +1507,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se var link: Remote = .{ .r = r, .fd = joined.fd }; if (init.environ_map.get("PARDES_REPLICA_CHECK")) |file| link.check = try gpa.dupeSentinel(u8, file, 0); defer if (link.check) |file| gpa.free(file); + if (init.environ_map.get("PARDES_TEST_DIVERGE")) |n| link.test_diverge = std.fmt.parseInt(u32, n, 10) catch 0; var sh: Shell = .{ .io = io, .gpa = gpa, @@ -1471,77 +1521,130 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se .from = joined.view, .remote = &link, }; - sh.terms[joined.view] = &screen; try startInput(&loop, &screen); defer stopInput(io, &screen.thread); (try std.Thread.spawn(.{}, winchWatch, .{&loop})).detach(); - var reader_done: std.atomic.Value(bool) = .init(false); - const reader = try std.Thread.spawn(.{}, linkReader, .{ gpa, joined.fd, &joined.rest, &loop, &reader_done }); if (link.check != null) (try std.Thread.spawn(.{}, wakeWatch, .{ io, &loop })).detach(); try screen.vx.queryTerminalSend(screen.tty.writer()); - // Its size is its view's first input. - sh.winched(); - while (!link.quit) { - if (link.dirty) link.draw(&sh); - const first = (loop.tryEvent() catch null) orelse idle: { - // Idle, its frame drawn: the next key needs no copy taken. - r.ready(); - break :idle loop.nextEvent() catch break; - }; - var ev = first; - var batch: usize = 1; - while (true) { - _ = sh.apply(ev); - if (link.quit or (!sh.in_paste and batch >= 64)) break; - ev = (loop.tryEvent() catch null) orelse break; - batch += 1; + // A join a pass: the first, and one after the replica diverged or the + // session let it go to upgrade. The terminal stays as it is. + while (true) { + r.gmtoff = joined.gmtoff; + sh.core = r.core(); + sh.from = joined.view; + sh.terms = @splat(null); + sh.terms[joined.view] = &screen; + screen.feed.view = joined.view; + link.reset(r, joined.fd); + var reader_done: std.atomic.Value(bool) = .init(false); + const reader = try std.Thread.spawn(.{}, linkReader, .{ gpa, joined.fd, &joined.rest, &loop, &reader_done }); + // Its size is its view's first input. + screen.size = null; + sh.winched(); + screen.vx.queueRefresh(); + while (!link.quit) { + if (link.dirty) link.draw(&sh); + const first = (loop.tryEvent() catch null) orelse idle: { + // Idle, its frame drawn: the next key needs no copy taken. + r.ready(); + break :idle loop.nextEvent() catch break; + }; + var ev = first; + var batch: usize = 1; + while (true) { + _ = sh.apply(ev); + if (link.quit or (!sh.in_paste and batch >= 64)) break; + ev = (loop.tryEvent() catch null) orelse break; + batch += 1; + } + r.settle(); + if (screen.caps_pending and screen.vx.queries_done.load(.unordered)) { + screen.caps_pending = false; + screen.vx.enableDetectedFeatures(screen.tty.writer()) catch {}; + screen.vx.queueRefresh(); + link.dirty = true; + } + if (link.owed) link.dirty = true; + if (link.check != null) { + // Quiet a while after a change: asked once (a wake, then the + // next quiet one). + if (first == .wake and link.changed and !link.asked and r.quiet()) { + link.changed = false; + link.asked = replica.askCheck(joined.fd); + } else if (link.changed and r.quiet()) requestWake(io, 300); + } + if (r.diverged != null) link.quit = true; } - r.settle(); - if (screen.caps_pending and screen.vx.queries_done.load(.unordered)) { - screen.caps_pending = false; - screen.vx.enableDetectedFeatures(screen.tty.writer()) catch {}; - screen.vx.queueRefresh(); - link.dirty = true; - } - if (link.owed) link.dirty = true; - if (link.check != null) { - // Quiet a while after a change: asked once (a wake, then the - // next quiet one). - if (first == .wake and link.changed and !link.asked and r.quiet()) { - link.changed = false; - link.asked = replica.askCheck(joined.fd); - } else if (link.changed and r.quiet()) requestWake(io, 300); + if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file); + if (!link.gone) replica.bye(joined.fd); + // The link's reader ends with the link, posting as it goes: what it + // and the terminal posted is let go until it has. + _ = libc.shutdown(joined.fd, 2); + while (true) { + while (loop.tryEvent() catch null) |ev| switch (ev) { + .unit, .screen, .refused => |b| std.heap.c_allocator.free(b), + .paste => |b| gpa.free(@constCast(b)), + else => {}, + }; + if (reader_done.load(.acquire)) break; + std.Thread.yield() catch {}; } + reader.join(); + _ = libc.close(joined.fd); + joined.rest.deinit(gpa); + // Why it joins again, in a line over its frame until a key. if (r.diverged) |reason| { - screen.leave(); - var said: [700]u8 = undefined; - std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: --attach: this replica diverged from '{s}': {s}\n", .{ session, reason }) catch "pardes: --attach: diverged\n") catch {}; - std.process.exit(1); + link.notify("joined again: this client diverged from the session ({s})", .{reason}); + } else if (link.refused) |words| { + link.notify("joined again: {s}", .{words}); + std.heap.c_allocator.free(words); + link.refused = null; + } else { + r.deinit(); + if (link.gone) { + screen.leave(); + Said.no(io, "'{s}' let this client go (a Detach in it, or the session's end)", .{session}); + } + return; } - } - if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file); - if (!link.gone) replica.bye(joined.fd); - // The link's reader ends with the link, posting as it goes: what it - // and the terminal posted is let go until it has. - _ = libc.shutdown(joined.fd, 2); - while (true) { - while (loop.tryEvent() catch null) |ev| switch (ev) { - .unit, .screen => |b| std.heap.c_allocator.free(b), - .paste => |b| gpa.free(@constCast(b)), - else => {}, + r.deinit(); + // The session again: an upgrade's takes the time its exec and load + // take, and its daemon's. + var tries: u32 = 0; + joined = while (true) : (tries += 1) { + why = @splat(0); + if (replica.dial(gpa, path, session, &why)) |j| { + if (j.semantics == wire.semantics) break j; + replica.bye(j.fd); + _ = libc.close(j.fd); + var jj = j; + jj.rest.deinit(gpa); + gpa.free(j.dump); + screen.leave(); + Said.no(io, "{s}", .{upgrade_words}); + } + const words = std.mem.sliceTo(&why, 0); + if (std.mem.indexOf(u8, words, "runs another build") != null) { + screen.leave(); + Said.no(io, "{s}", .{upgrade_words}); + } + if (tries > 200) { + screen.leave(); + Said.no(io, "{s}", .{words}); + } + const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms }; + _ = libc.nanosleep(&ts, null); + }; + r = Remote.load(gpa, &joined, o, &why) orelse { + screen.leave(); + Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); }; - if (reader_done.load(.acquire)) break; - std.Thread.yield() catch {}; - } - reader.join(); - if (link.gone) { - screen.leave(); - var said: [300]u8 = undefined; - std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: --attach: '{s}' let this client go (a Detach in it, or the session's end)\n", .{session}) catch "pardes: --attach: the session let this client go\n") catch {}; } } +const upgrade_words = "session upgraded to a newer pardes: upgrade this client too"; + /// A terminal a session draws on, while it has one: entered (raw, the /// alternate screen, mouse and paste reports) and left (all of it put back, /// the terminal closed) at the session's start and end, and at a detach and @@ -2048,6 +2151,8 @@ const Shell = struct { /// (applied at once there, and sent to the session). fn feed(s: *Shell, ev: pardes.Event) void { const c = s.remote orelse return s.core.update(ev); + // Its notice read: a key of its own puts it away. + if (c.notice_len > 0 and (ev == .key or ev == .paste)) c.notice_len = 0; const msg = c.r.input(ev) catch return; if (!replica.sendInput(c.fd, msg)) c.quit = true; c.dirty = true; @@ -2093,6 +2198,7 @@ const Shell = struct { .detach = detach, .adopt = adopt, .upgrade = upgradeAsked, + .newer_build = newerBuild, .spawn = spawn, .pty_write = ptyWrite, .pty_resize = ptyResize, @@ -2268,6 +2374,12 @@ const Shell = struct { daemon.adopt(); } + /// A client's own build is not the session's to say (its core's host + /// is the session's, through the tape). + fn newerBuild(ctx: ?*anyopaque) bool { + return of(ctx).remote == null and upgrade.newer(); + } + fn upgradeAsked(ctx: ?*anyopaque, pane: u8) void { const s = of(ctx); if (comptime !upgrade.supported) return s.core.reportFailure(pane, "Upgrade: this system cannot move a session to another build in place"); @@ -2301,6 +2413,9 @@ const Shell = struct { for (s.terms) |slot| if (slot) |sc| stopInput(s.io, &sc.thread); while (s.loop.tryEvent() catch null) |ev| _ = s.apply(ev); core.settle(); + // Its replica clients told, their views gone before the dump: each + // dials the session again once it is back. + s.peers.refuseAll(core, "the session is upgrading to another build of pardes"); var rec: upgrade.Record = .{ .link = daemon.link, .stub = daemon.stub, .exe = path }; for (s.ptys, 0..) |slot, id| if (slot) |pt| rec.addPty(id, pt.pid, pt.file.handle); for (s.terms, 0..) |slot, k| if (slot) |sc| if (sc.on) rec.addTty(.{ @@ -2668,6 +2783,11 @@ const Shell = struct { .unit => |bytes| { if (s.remote) |c| { c.r.confirm(bytes); + if (c.test_diverge != 0 and c.r.stats.units >= c.test_diverge and c.r.quiet()) { + c.test_diverge = 0; + c.put_off = true; + c.r.core().next_serial +%= 1000; + } if (c.r.moved) c.dirty = true; c.r.moved = false; c.changed = true; @@ -2684,17 +2804,18 @@ const Shell = struct { std.heap.c_allocator.free(bytes); }, .checked => |d| if (s.remote) |c| c.checked(d), + .refused => |words| if (s.remote) |c| { + if (c.refused) |old| std.heap.c_allocator.free(old); + c.refused = words; + c.quit = true; + return true; + } else std.heap.c_allocator.free(words), .link_gone => if (s.remote) |c| { c.quit = true; c.gone = true; return true; }, - .focus_in => { - s.feed(.{ .focus = true }); - // A client's core is the session's: the notice is the session's to give. - // ponytail: not while clients replicate it (set outside a step). - if (s.remote == null and !s.peers.recording) upgrade.noticeNewer(core); - }, + .focus_in => s.feed(.{ .focus = true }), .focus_out => { s.feed(.pointer_leave); s.feed(.{ .focus = false }); @@ -2887,6 +3008,7 @@ const Shell = struct { const flashed = sc.flash.apply(s.frame.allocator(), shadowed, s.core, truecolor) catch shadowed; const trailed = sc.trail.apply(s.frame.allocator(), flashed, s.core, truecolor) catch flashed; paintCells(win, trailed.cells, trailed.cols, trailed.rows); + if (s.remote) |c| if (c.notice_len > 0) noticeLine(win, c.notice_buf[0..c.notice_len], trailed.cols, trailed.rows); tz_cells.end(); for (surface.images[0..surface.nimages]) |maybe| { const place = maybe orelse continue; @@ -3641,6 +3763,17 @@ test "a mouse's side buttons, xterm's 8 and 9, are Back and Forward, as SDL's X1 } } +/// A replica client's own line over its frame's last row (`Remote.notify`), +/// reversed. `text` is printable ASCII and outlives the render (vaxis keeps +/// the slices). +fn noticeLine(win: vaxis.Window, text: []const u8, cols: u16, rows: u16) void { + if (rows == 0) return; + for (0..cols) |x| win.writeCell(@intCast(x), rows - 1, .{ + .char = .{ .grapheme = if (x < text.len) text[x..][0..1] else " " }, + .style = .{ .reverse = true }, + }); +} + /// A blank whose ink alone changed is not sent: vaxis's render skips it /// (pardes's patch to Vaxis.zig in build.zig), since a blank's ink shows /// nowhere. Deciding that here cost every cell of every frame a look at the diff --git a/src/upgrade.zig b/src/upgrade.zig index 49e8aff5..37c79991 100644 --- a/src/upgrade.zig +++ b/src/upgrade.zig @@ -74,11 +74,18 @@ fn inodeOf(path: [:0]const u8) ?u64 { /// rebuilding (a stat). ponytail: explicit only, no watch and no automatic /// upgrade. pub fn noticeNewer(core: *pardes.Pardes) void { - const was = started_inode orelse return; - const now = inodeOf(exe() orelse return) orelse return; - if (now == was or told_inode == now) return; + if (newer()) core.setMessage(core.active, pardes.newer_build_words); +} + +/// Whether to say a newer build is installed now (`noticeNewer`): the +/// tty's Host answers the core's focus step with it, so its replica +/// clients take the line from the tape. +pub fn newer() bool { + const was = started_inode orelse return false; + const now = inodeOf(exe() orelse return false) orelse return false; + if (now == was or told_inode == now) return false; told_inode = now; - core.setMessage(core.active, "a newer pardes is installed: Upgrade (SPC s U) moves this session onto it"); + return true; } pub fn exe() ?[:0]const u8 { diff --git a/test/daemon.py b/test/daemon.py index b54b2b54..273c29e0 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -337,6 +337,26 @@ def clients(binary): and b'from-B' in nine.read(f'/pane/{pane}/body')) wait_for('client A agrees once quiet', lambda: agreed(root / 'a.check', 0)) wait_for('client B agrees once quiet', lambda: agreed(root / 'b.check', 0)) + # One put off the session's state (PARDES_TEST_DIVERGE): the next + # unit's digest finds it, it says so in a line and joins again, and + # its keys after go in and it agrees. + td = Term(24, 90) + terms.append(td) + d = td.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'd.check'), PARDES_TEST_DIVERGE='1')) + started.append(d.pid) + wait_for('client D\'s first frame', lambda: b'line 3 of the notes' in td.text(), timeout=20) + os.write(td.master, b'jj') + time.sleep(.5) + # Another's unit, replayed on D's core: its digest finds it. + os.write(ta.master, b'jj') + wait_for('client D diverged and joined again', lambda: b'joined again' in td.text(), timeout=20) + assert d.poll() is None + seen = len(checks(root / 'd.check')) + os.write(td.master, b'Oafter-rejoin-D\x1b') + wait_for('client D\'s keys after its rejoin', lambda: b'after-rejoin-D' in nine.read(f'/pane/{pane}/body')) + wait_for('client D agrees after its rejoin', lambda: agreed(root / 'd.check', seen)) + os.write(td.master, b' sD') + wait_for('client D let go', lambda: d.poll() is not None, timeout=10) # Killed outright: the session and the other client carry on. os.kill(b.pid, signal.SIGKILL) b.wait(timeout=10) @@ -373,6 +393,7 @@ def clients(binary): wait_for('the session\'s fds level', lambda: fds(sess.pid) == alone, timeout=10) print(f'daemon: two replica clients (one through a 40 ms relay) typed into a detached session; ' f'digests agreed {len(checks(root / "a.check"))} and {len(checks(root / "b.check"))} times; ' + f'one put off the session\'s state joined again and agreed {len(checks(root / "d.check"))} times; ' f'one killed, the other typed on and detached; a third typed into a shell, agreed ' f'{len(checks(root / "c.check"))} times, and was hung up') try: @@ -767,6 +788,12 @@ def upgrades(old, new): old_build = semantics(old) replica = Replica(root / 'pardes-replica.sock') assert replica.hello('work', old_build)[0] == 'welcome' + # A replica client of the old build: let go at the upgrade, it dials + # again and is told to upgrade too. + term3 = Term(24, 90) + stale = term3.spawn([str(old), f'--attach={root / "pardes-replica.sock"}:work'], env) + started.append(stale.pid) + wait_for('the old build\'s client drawn', lambda: b'\x1b[?1049h' in term3.text() and b'one' in term3.text()) before = len(term.text()), len(term2.text()) install(new, binary) @@ -794,16 +821,23 @@ def upgrades(old, new): else: wait_for('the daemon exec\'d again', lambda: any(a.startswith(b'--upgraded=') for a in cmdline(daemon))) assert alive(daemon) and stub.poll() is None and stub2.poll() is None - # A replica of the old build is told why it is let go. + # A replica of the old build is told why it is let go, after what it + # was sent (units, terminals). tag, body = replica.read() + while tag in (0x05, 0x06, 0x07): + tag, body = replica.read() assert tag == 0x02 and b'upgrading to another build' in body, (tag, body) replica.close() + stale.wait(timeout=30) + assert b'upgrade this client too' in term3.text(), term3.text()[-400:] + term3.close() + print('upgrade: a replica client of the old build was let go, dialled again and was told to upgrade too') # The old image's prompt files swept, the new one's made. wait_for('the prompt files level', lambda: len(list(Path('/tmp').glob(f'pardes-osc133-*-{session}-*'))) == len(rcs)) assert sorted(Path('/tmp').glob(f'pardes-osc133-*-{session}-*')) != rcs or not rcs # Nothing kept that was not: the old binary let go, every fd back. - # (The replica's connection is the one fewer.) - level = fds_before[0] - 1, fds_before[1] + # (The replicas' connections are two fewer.) + level = fds_before[0] - 2, fds_before[1] try: wait_for('the fds level', lambda: (fds(session), fds(daemon)) == level, timeout=5) except TimeoutError: @@ -842,10 +876,26 @@ def upgrades(old, new): install(refuser, binary) if procfs: running = os.stat(f'/proc/{session}/exe').st_ino + # A replica client of the build running: let go at the upgrade, + # it joins again once the session is back, says so, and agrees. + term4 = Term(24, 90) + again = term4.spawn([str(new), f'--attach={root / "pardes-replica.sock"}:work'], + dict(env, PARDES_REPLICA_CHECK=str(root / 'e.check'))) + started.append(again.pid) + wait_for('the client of the build running drawn', lambda: b'\x1b[?1049h' in term4.text() and b'one' in term4.text()) refusing('1') client.write(f'/pane/{shell}/exec', b'Upgrade\n') client.socket.close() client = saying(sock, 'it goes on as') + wait_for('the client joined again', lambda: b'joined again: the session is upgrading' in term4.text(), timeout=30) + assert again.poll() is None + seen = len(checks(root / 'e.check')) + os.write(term4.master, b'gg') + wait_for('the client agrees after it joined again', lambda: agreed(root / 'e.check', seen), timeout=20) + again.send_signal(signal.SIGHUP) + again.wait(timeout=10) + term4.close() + print('upgrade: a replica client of the build running joined again after the upgrade, said so and agreed') assert os.stat(f'/proc/{session}/exe').st_ino == running, 'back on the build that ran' assert alive(shell_pid) and ppid(shell_pid) == session and stub.poll() is None assert client.read(f'/pane/{text}/body') == b'one\ntwo\n' -- cgit v1.3