summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/Core.zig4
-rw-r--r--core/replica.zig12
-rw-r--r--dev/gui/gui.zig2
-rw-r--r--dev/tui/tui.zig10
-rw-r--r--runtime/client.zig52
-rw-r--r--runtime/upgrade.zig8
-rw-r--r--test/daemon.py15
7 files changed, 95 insertions, 8 deletions
diff --git a/core/Core.zig b/core/Core.zig
index 7ebcc020..c08eb883 100644
--- a/core/Core.zig
+++ b/core/Core.zig
@@ -2284,6 +2284,10 @@ pub const Options = struct {
/// first effects (runtime/session.zig); a client's session is on it,
/// its first join made there with no hello (replica.join).
pair: ?c_int = null,
+ /// A client exec'd at an Upgrade (runtime/client.zig `execAgain`,
+ /// PARDES_JOIN_VIEW): the view it had, which the session kept for it
+ /// (views.away), joined again for as a rejoin dials (replica.join).
+ join_view: ?u8 = null,
mounts: []const filesystem.Mount = &.{},
config_dir: ?[]const u8 = null,
/// Where what outlives the session is kept (Db.zig): the user's state
diff --git a/core/replica.zig b/core/replica.zig
index 8ca505ec..9edc96d2 100644
--- a/core/replica.zig
+++ b/core/replica.zig
@@ -2995,6 +2995,13 @@ pub fn ended(why: []const u8) bool {
/// The words a client goes with when its session runs another build now.
const upgrade_words = "session upgraded to a newer pardes: upgrade this client too";
+/// A join's failure that is its session's running another build now: a
+/// client whose own start path holds a newer one execs that instead
+/// (runtime/client.zig).
+pub fn otherBuild(why: []const u8) bool {
+ return std.mem.endsWith(u8, std.mem.sliceTo(why, 0), upgrade_words);
+}
+
/// A replica client's join, the terminal's and the window's alike (their
/// `runClient`): the session at `path` dialled (or, first, the one on its
/// pair, `o.pair`), its build checked to be this
@@ -3019,7 +3026,10 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o
};
if (have) |h| h.left();
prev.deinit();
- } else j.* = sameBuild(gpa, (if (o.pair) |fd| joinOn(gpa, fd, null, why, have, null) else dial(gpa, path, session, why, have, null)) orelse return null, why) orelse return null;
+ } else j.* = (if (o.join_view) |v|
+ redial(gpa, path, session, why, have, true, v)
+ else
+ sameBuild(gpa, (if (o.pair) |fd| joinOn(gpa, fd, null, why, have, null) else dial(gpa, path, session, why, have, null)) orelse return null, why)) orelse return null;
defer {
if (have) |h| h.store(gpa, j) else gpa.free(j.dump);
j.dump = &.{};
diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig
index 36243c71..60176642 100644
--- a/dev/gui/gui.zig
+++ b/dev/gui/gui.zig
@@ -1444,6 +1444,8 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_
.done => {},
.gone => Said.no(io, "'{s}' let this client go (a Detach in it, or the session's end)", .{to.session}),
.failed => Said.no(io, "{s}", .{start.said()}),
+ // A new process's window: the build at its start path, exec'd.
+ .upgrade => |view| Said.no(io, "{s}", .{client_loop.execAgain(&start, view)}),
.left => {
var said: [600]u8 = undefined;
std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes-gui: detached from '{s}', which is still running; come back with `pardes-gui attach {s}`\n", .{ to.session, to.session }) catch "pardes-gui: detached\n") catch {};
diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig
index bf051e9a..a3889d28 100644
--- a/dev/tui/tui.zig
+++ b/dev/tui/tui.zig
@@ -817,6 +817,12 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8,
screen.leave();
Said.no(io, "{s}", .{start.said()});
},
+ .upgrade => |view| {
+ // The blink: its screen left, the build at its start path
+ // exec'd, which enters it again.
+ screen.leave();
+ Said.no(io, "{s}", .{client_loop.execAgain(&start, view)});
+ },
}
}
@@ -1315,6 +1321,10 @@ const Shell = struct {
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");
+ // A client of a session no daemon keeps is a pair's first (no other
+ // can attach): let go, it has no socket to find the session again
+ // by, and the session would end with its last link.
+ if (s.peers.count() > 0 and daemon.link < 0) return core.reportFailure(pane, "Upgrade: no daemon keeps this session, so its client could not join it again; this session stays as it is");
// 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();
diff --git a/runtime/client.zig b/runtime/client.zig
index 1949d618..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);
@@ -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;
+ };
}
}
diff --git a/runtime/upgrade.zig b/runtime/upgrade.zig
index dcc705a5..37d4890b 100644
--- a/runtime/upgrade.zig
+++ b/runtime/upgrade.zig
@@ -95,6 +95,14 @@ pub fn newer() bool {
return true;
}
+/// Another file at its start path than the one it started from: a build
+/// installed there since (`locate` first). A client's question at an
+/// Upgrade (client.zig).
+pub fn replaced() bool {
+ const was = started_inode orelse return false;
+ return (inodeOf(exe() orelse return false) orelse return false) != was;
+}
+
pub fn exe() ?[:0]const u8 {
return if (exe_len == 0) null else exe_buf[0..exe_len :0];
}
diff --git a/test/daemon.py b/test/daemon.py
index 1daebea8..26508a0b 100644
--- a/test/daemon.py
+++ b/test/daemon.py
@@ -649,12 +649,16 @@ def pair(binary):
c2 = lone.spawn([binary, '--9p=lone', str(notes)], dict(env, PARDES_DAEMON='off'))
nine2, session2 = session_of('lone')
wait_for('the lone pair drawn', lambda: b'line 3 of the notes' in lone.text(), timeout=30)
+ # An Upgrade refused, with why: its client could not find it again.
+ nine2.write('/ctl', b'Upgrade\n')
+ wait_for('the Upgrade refused', lambda: 'its client could not join it again' in screen_text(nine2), timeout=20)
+ assert alive(session2) and c2.poll() is None
nine2.socket.close()
os.kill(c2.pid, signal.SIGKILL)
c2.wait(timeout=10)
wait_for('the lone session ended with its last link', lambda: not alive(session2), timeout=20)
wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=20)
- print(f'pair: with no daemon, client {c2.pid} killed outright, its session {session2} ended; nothing left behind')
+ print(f'pair: with no daemon, an Upgrade refused in words; client {c2.pid} killed outright, its session {session2} ended; nothing left behind')
finally:
for p in processes(root):
try:
@@ -2303,7 +2307,9 @@ def upgrades(old, new):
# its terminal and its stub's pipe let go once its stub is told
# (two fewer).
def level():
- gone = 2 if b'detached from' in term2.text() else 1
+ # (A client of a build that execs at an Upgrade comes back: its
+ # connection is the session's again.)
+ gone = 2 if b'detached from' in term2.text() else 0 if knows_exec(old) and stub2.poll() is None else 1
return fds_before[0] - 2 - gone + min(state_fds(session), 1) - state_before, fds_before[1]
try:
wait_for('the fds level', lambda: (fds(session), fds(daemon)) == level(), timeout=5)
@@ -2414,7 +2420,10 @@ def upgrades(old, new):
install(new, binary)
os.kill(session, signal.SIGKILL)
stub.wait(timeout=10)
- stub2.wait(timeout=10)
+ try:
+ stub2.wait(timeout=30)
+ except subprocess.TimeoutExpired:
+ raise AssertionError(('the second terminal\'s client on', cmdline(stub2.pid), term2.text()[-1200:]))
client.socket.close()
term.close()
term2.close()