diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-29 22:18:34 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-29 23:37:00 -0300 |
| commit | ddcfd860ef05c37f5536038d8828cc81c0c4125c (patch) | |
| tree | 447d95dc0769273f2919747e30f5a24faec3fc21 /src/gui/gui.zig | |
| parent | 2d86687b98f389232b213dfa67cfa731b0280fbb (diff) | |
| download | pardes-ddcfd860ef05c37f5536038d8828cc81c0c4125c.tar.gz pardes-ddcfd860ef05c37f5536038d8828cc81c0c4125c.zip | |
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 <[email protected]>
Diffstat (limited to 'src/gui/gui.zig')
| -rw-r--r-- | src/gui/gui.zig | 120 |
1 files changed, 113 insertions, 7 deletions
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-<k>; 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| { |
