diff options
| -rw-r--r-- | runtime/daemon.zig | 7 | ||||
| -rw-r--r-- | runtime/upgrade.zig | 26 | ||||
| -rw-r--r-- | test/daemon.py | 66 |
3 files changed, 74 insertions, 25 deletions
diff --git a/runtime/daemon.zig b/runtime/daemon.zig index 595d5107..a964eb0b 100644 --- a/runtime/daemon.zig +++ b/runtime/daemon.zig @@ -241,9 +241,8 @@ extern "c" fn close_range(first: c_uint, last: c_uint, flags: c_int) c_int; /// twice), on the listener and the lock as fds 3 and 4. fn spawn(listener: c_int, lock: c_int) bool { var exe_buf: [4096:0]u8 = undefined; - const n = std.process.executablePath(std.Io.Threaded.global_single_threaded.io(), &exe_buf) catch return false; - exe_buf[n] = 0; - const argv = [_:null]?[*:0]const u8{ &exe_buf, "--daemon", null }; + const exe = upgrade.startPath(&exe_buf) orelse return false; + const argv = [_:null]?[*:0]const u8{ exe, "--daemon", null }; const first = libc.fork(); if (first < 0) return false; if (first == 0) { @@ -268,7 +267,7 @@ fn spawn(listener: c_int, lock: c_int) bool { }; if (null_fd > 4) _ = libc.close(null_fd); _ = libc.chdir("/"); - _ = libc.execve(&exe_buf, &argv, @ptrCast(libc.environ)); + _ = libc.execve(exe, &argv, @ptrCast(libc.environ)); libc._exit(127); } var status: c_int = 0; diff --git a/runtime/upgrade.zig b/runtime/upgrade.zig index f59117ee..510c50ea 100644 --- a/runtime/upgrade.zig +++ b/runtime/upgrade.zig @@ -58,18 +58,28 @@ pub fn remember(args: []const [:0]const u8) void { }; } -/// Its path asked of the system (a readlink) and the file there now (a +/// Its path asked of the system (a readlink) and the file it runs (a /// stat), once: after the first frame, off a startup's way (the hosts' -/// loops; nothing is reinstalled within a frame of a start). +/// loops). A build can be installed at that path before then (a pair's +/// session, whose first pass comes after its client's first frame; a +/// loaded machine): Linux then names the path "PATH (deleted)", and the +/// file at the path is not the one it started from, the one it runs is. 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); + if (exe_len == 0) exe_len = if (startPath(&exe_buf)) |path| path.len else 0; + started_inode = if (builtin.os.tag == .linux) inodeOf("/proc/self/exe") else if (exe()) |path| inodeOf(path) else null; +} + +/// The path this process was started from, as the system names it, less +/// the " (deleted)" Linux adds once the file there was replaced (a +/// reinstall): the path is the path (what an Upgrade execs, and a daemon +/// started from here: daemon.zig `spawn`). +pub fn startPath(buf: *[4096:0]u8) ?[:0]const u8 { + const n = std.process.executablePath(std.Io.Threaded.global_single_threaded.io(), buf) catch return null; + const len = if (std.mem.endsWith(u8, buf[0..n], " (deleted)")) n - " (deleted)".len else n; + buf[len] = 0; + return buf[0..len :0]; } fn inodeOf(path: [:0]const u8) ?u64 { diff --git a/test/daemon.py b/test/daemon.py index caa1272b..7f1adfdb 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -1332,12 +1332,18 @@ def pair_upgrade_views(binary): shutil.rmtree(root, ignore_errors=True) -def pair_exec_reaps(old, new): +def pair_exec_reaps(old, new, early=False): """PARDES_PAIR across builds: a pair started from `root/bin` (old), its session upgraded to the build installed there since (new): its client, the session's parent, execs that build (the blink) and is still its parent; Exit typed then: the client returns once the session is gone, - reaped, its status the session's (0), as before the exec.""" + reaped, its status the session's (0), as before the exec. `early`: old + and new the same build, installed again at once, before the session's + first pass round its loop, where it first looks its start path up + (upgrade.zig `locate`), and as the client starts the daemon from its + own (daemon.zig `spawn`): the path is the path, not Linux's "PATH + (deleted)", so the daemon starts and the session execs it; the client, + its build the session's still, joins again in place (no exec).""" scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn' scratch.mkdir(parents=True, exist_ok=True) root = Path(tempfile.mkdtemp(prefix='rpx-', dir=scratch)) @@ -1352,36 +1358,48 @@ def pair_exec_reaps(old, new): notes = root / 'notes.txt' notes.write_text(''.join(f'line {i} of the notes\n' for i in range(30))) terms = [] + sock = root / 'pardes-9p-px.sock' + c = session = None try: t = Term(30, 100) terms.append(t) c = t.spawn([str(binary), '--9p=px', str(notes)], env) - sock = root / 'pardes-9p-px.sock' + if early: + install(new, binary) wait_for('the socket', sock.exists) nine = Client(sock) session = int(dict(line.split(maxsplit=1) for line in nine.read('/status').decode().splitlines())['pid']) assert ppid(session) == c.pid wait_for('drawn', lambda: b'line 3 of the notes' in t.text(), timeout=30) - install(new, binary) + if not early: + install(new, binary) ino = sock.stat().st_ino # Before the Upgrade: the exec'd client can draw before its exec is seen. mark = len(t.text()) nine.write('/ctl', b'Upgrade\n') nine.socket.close() wait_for('the session exec\'d', lambda: sock.stat().st_ino != ino, timeout=60) - wait_for('the client on the new build', lambda: exe_of(c.pid) == (str(binary), True), timeout=30) + if early: + wait_for('joined again in place', lambda: b'joined again' in t.text()[mark:], timeout=30) + else: + wait_for('the client on the new build', lambda: on_build(c, binary), timeout=30) nine = reconnect(sock) assert ppid(session) == c.pid # The blink: its screen left and entered again, the notes drawn there. - wait_for('drawn again', lambda: (lambda x: x.rfind(b'\x1b[?1049h') > x.rfind(b'\x1b[?1049l') > mark - and b'line 3 of the notes' in x[x.rfind(b'\x1b[?1049h'):])(t.text()), timeout=30) + if not early: + wait_for('drawn again', lambda: b'line 3 of the notes' in (entered_again(t.text(), mark) or b''), timeout=30) nine.write('/ctl', b'Exit\n') nine.socket.close() assert c.wait(timeout=30) == 0, c.returncode # Its parent's wait returned: the session reaped, not a zombie left # to whoever takes it in. assert not os.path.exists(f'/proc/{session}') if procfs else not alive(session), open(f'/proc/{session}/stat').read() - print('pair exec reaps: a pair\'s client exec\'d at its session\'s Upgrade still reaps it at Exit, status 0') + print('pair exec reaps: a pair\'s client exec\'d at its session\'s Upgrade still reaps it at Exit, status 0' + + (' (the same build installed again before the session looked its path up: joined again in place)' if early else '')) + except (TimeoutError, AssertionError, OSError, EOFError, subprocess.TimeoutExpired) as err: + kept = keep_evidence('pair-exec', root, env, binary, sock, [('session', session)] if session else [], + [('client', t, c, root / 'client.check', root / 'client.stats')] if c else []) + raise AssertionError(f'{type(err).__name__}: {err} (kept: {kept})') from None finally: for p in processes(root): try: @@ -1439,8 +1457,8 @@ def far_exec(old, new): nine.socket.close() wait_for('the session exec\'d', lambda: sock.stat().st_ino != ino, timeout=60) nine = reconnect(sock) - wait_for('the far client on the new build', lambda: exe_of(c.pid) == (str(binary), True), timeout=30) - wait_for('the terminal entered again', lambda: t.text().rfind(b'\x1b[?1049h') > t.text().rfind(b'\x1b[?1049l') > mark, timeout=30) + wait_for('the far client on the new build', lambda: on_build(c, binary), timeout=30) + wait_for('the terminal entered again', lambda: entered_again(t.text(), mark), timeout=30) # Its environment read once the new image runs (it entered the # screen): an exec names the new file in /proc/PID/exe before it has # written where the environment is, and a read between gets nothing @@ -1518,7 +1536,7 @@ def pair_window_geometry(gui_binary, new_gui): nine.write('/ctl', b'Upgrade\n') nine.socket.close() wait_for('the session exec\'d', lambda: sock.stat().st_ino != ino, timeout=60) - wait_for('the window on the new build', lambda: exe_of(p.pid) == (str(gui), True), timeout=30) + wait_for('the window on the new build', lambda: on_build(p, gui), timeout=30) nine = reconnect(sock) time.sleep(3) assert grid(nine) == sized, ('the exec\'d window the size it had', grid(nine), sized, plain) @@ -3392,7 +3410,10 @@ def exe_of(pid): if not procfs: 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: + link = os.readlink(f'/proc/{pid}/exe') + except OSError: # gone, or a zombie: it runs nothing + return '', False try: same = os.stat(f'/proc/{pid}/exe').st_ino == os.stat(link).st_ino except OSError: @@ -3400,6 +3421,22 @@ def exe_of(pid): return link, same +def entered_again(text, mark): + """The blink of a client exec'd at an Upgrade: its terminal's screen + left at or after `mark` (the text's length before the Upgrade: when the + reader had read all the old client wrote, the leave is its very next + byte) and entered again. What was drawn since that enter, or None.""" + enter, leave = text.rfind(b'\x1b[?1049h'), text.rfind(b'\x1b[?1049l') + return text[enter:] if enter > leave >= mark else None + + +def on_build(proc, binary): + """Whether the test's child `proc` runs `binary` now (exe_of); one that + ended says so with its status at once, not as a wait run out.""" + assert proc.poll() is None, f'pid {proc.pid} ended ({proc.returncode}) before it ran {binary}' + return exe_of(proc.pid) == (str(binary), True) + + def ppid(pid): if not procfs: return int(ps('-o', 'ppid=', '-p', str(pid))) @@ -3888,7 +3925,7 @@ def upgrade_clients(old, new): wait_for('the terminal client on the new build', lambda: exe_of(tc.pid) == (str(binary), True), timeout=30) wait_for('the window client on the new build', lambda: exe_of(w.pid) == (str(gui_binary), True), timeout=30) # The blink: its screen left, and entered again. - wait_for('the terminal entered again', lambda: t.text().rfind(b'\x1b[?1049h') > t.text().rfind(b'\x1b[?1049l') > mark, timeout=30) + wait_for('the terminal entered again', lambda: entered_again(t.text(), mark), timeout=30) assert tc.poll() is None and w.p.poll() is None for (key, check), pane, theirs in zip(owners, mine, reversed(mine)): if knows_exec(new): @@ -4035,6 +4072,9 @@ if __name__ == '__main__': far_exec(old, new) else: print('far exec: skipped: the old build\'s far client, cut at the Upgrade with no last word, takes it for its session\'s end (no PARDES_JOIN_SIZE)') + # A reinstall of the new build before the pair's first look: the + # new build's own, whatever the old one does. + pair_exec_reaps(new, new, early=True) # With a pardes-gui beside each: the clients carried too. if all(a.with_name('pardes-gui').exists() for a in (old, new)): upgrade_clients(old, new) |
