diff options
Diffstat (limited to 'runtime/client.zig')
| -rw-r--r-- | runtime/client.zig | 54 |
1 files changed, 49 insertions, 5 deletions
diff --git a/runtime/client.zig b/runtime/client.zig index 1314d3fb..329bdab3 100644 --- a/runtime/client.zig +++ b/runtime/client.zig @@ -37,6 +37,7 @@ const std = @import("std"); const libc = std.c; const pardes = @import("core"); const replica = pardes.replica; +const upgrade = @import("upgrade.zig"); /// A wait with no wake of its own: until an input or a message. pub const forever: u32 = std.math.maxInt(u32); @@ -92,6 +93,8 @@ pub const Start = struct { s.o.distrusted = &s.distrusted; // Its frame timings, this process's (Pardes.frames). s.o.frames = &s.frames; + // Exec'd at an Upgrade (`execAgain`): for the view it had. + if (init.environ_map.get("PARDES_JOIN_VIEW")) |v| s.o.join_view = std.fmt.parseInt(u8, v, 10) catch null; s.r = replica.join(s.gpa, &s.joined, dial.path, dial.session, s.o, s.have, &s.why, null) orelse return false; return true; } @@ -111,8 +114,44 @@ pub const Start = struct { /// How a client's joins ended: it quit, the session let it go, it could /// not join again (`Start.said`), it left the session running (a Detach in -/// it), or the session ended, with its code. -pub const Ended = union(enum) { done, gone, failed, left, ended: u8 }; +/// it), the session ended, with its code, or it runs another build now and +/// one is installed at this client's start path since it started: the +/// frontend lets its screen go and execs that, for this view (`execAgain`). +pub const Ended = union(enum) { done, gone, failed, left, ended: u8, upgrade: u8 }; + +/// An Upgrade's client again (`Ended.upgrade`), its frontend's screen let +/// go: this process exec'd as the file at its start path, an attach of the +/// same session through the same socket (a far one's forward, its ssh this +/// process's child still), joining for `view` (PARDES_JOIN_VIEW; far, +/// PARDES_REPLICA_FAR). The terminal's blink: no record crosses the exec. +/// Returns only when the exec failed: why. +pub fn execAgain(s: *Start, view: u8) []const u8 { + const path = upgrade.exe() orelse return "Upgrade: this client does not know the path it was started from"; + var at_buf: [4200]u8 = undefined; + const at = std.fmt.bufPrintSentinel(&at_buf, "--attach={s}:{s}", .{ s.dial.path, s.dial.session }, 0) catch return "Upgrade: the session's socket's path is too long to attach again by"; + const argv = [_:null]?[*:0]const u8{ path, at }; + var view_buf: [32]u8 = undefined; + const joining = std.fmt.bufPrintSentinel(&view_buf, "PARDES_JOIN_VIEW={d}", .{view}, 0) catch unreachable; // a u8 fits 32 + var n: usize = 0; + while (libc.environ[n] != null) n += 1; + const envp = s.gpa.allocSentinel(?[*:0]const u8, n + 2, null) catch return "Upgrade: no memory for this client's environment"; + var k: usize = 0; + for (libc.environ[0..n]) |e| { + const entry = std.mem.span(e.?); + if (std.mem.startsWith(u8, entry, "PARDES_JOIN_VIEW=") or std.mem.startsWith(u8, entry, "PARDES_REPLICA_FAR=")) continue; + envp[k] = e; + k += 1; + } + envp[k] = joining.ptr; + k += 1; + if (s.have != null) { + envp[k] = "PARDES_REPLICA_FAR=1"; + k += 1; + } + envp[k] = null; + _ = libc.execve(path, &argv, envp.ptr); + return "Upgrade: the build at this client's start path would not start"; +} pub fn Loop(comptime Frontend: type) type { return struct { @@ -142,6 +181,8 @@ pub fn Loop(comptime Frontend: type) type { /// the session let it go to upgrade; the frontend stays as it is. pub fn run(l: *L, s: *Start) !Ended { const link = &l.link; + // Its start path's file, for an Upgrade's question (`upgrade`). + upgrade.locate(); while (true) { link.reset(s.r, &s.joined); l.fe.start(l); @@ -152,7 +193,7 @@ pub fn Loop(comptime Frontend: type) type { l.after(); } if (s.env.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file); - if (!link.gone) replica.bye(s.joined.fd, s.r.diverged != null); + if (!link.gone) replica.bye(s.joined.fd, .{ .again = s.r.diverged != null, .close = link.close }); // The link's reader ends with the link, posting as it goes: // what it posted is let go until it has (a terminal's queue // is bounded: a reader blocked on it would never end). @@ -177,8 +218,11 @@ pub fn Loop(comptime Frontend: type) type { } // The session again, the core it leaves held meanwhile: its // blobs are not sent again. - s.r = replica.join(s.gpa, &s.joined, s.dial.path, s.dial.session, s.o, s.have, &s.why, s.r) orelse - return if (replica.ended(&s.why)) .gone else .failed; + s.r = replica.join(s.gpa, &s.joined, s.dial.path, s.dial.session, s.o, s.have, &s.why, s.r) orelse { + if (replica.ended(&s.why)) return .gone; + if (replica.otherBuild(&s.why) and upgrade.replaced()) return .{ .upgrade = s.joined.view }; + return .failed; + }; } } |
