summaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-29 22:18:34 -0300
committerGabriel Schneider <[email protected]>2026-10-29 23:37:00 -0300
commitddcfd860ef05c37f5536038d8828cc81c0c4125c (patch)
tree447d95dc0769273f2919747e30f5a24faec3fc21 /src/main.zig
parent2d86687b98f389232b213dfa67cfa731b0280fbb (diff)
downloadpardes-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/main.zig')
-rw-r--r--src/main.zig42
1 files changed, 33 insertions, 9 deletions
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);