summaryrefslogtreecommitdiff
path: root/src/tty
diff options
context:
space:
mode:
Diffstat (limited to 'src/tty')
-rw-r--r--src/tty/tty.zig305
1 files changed, 219 insertions, 86 deletions
diff --git a/src/tty/tty.zig b/src/tty/tty.zig
index 2eafc785..e62da55c 100644
--- a/src/tty/tty.zig
+++ b/src/tty/tty.zig
@@ -160,6 +160,8 @@ pub const Command = struct {
unit: []u8,
/// ...a terminal's delta or rows, before the units after it.
screen: []u8,
+ /// ...why it let this client go (an Upgrade's exec), the stream's end.
+ refused: []u8,
/// The session's digest, asked by PARDES_REPLICA_CHECK.
checked: u64,
link_gone,
@@ -1295,6 +1297,59 @@ const Remote = struct {
/// frame shows the inputs after it too).
mispredicted: u32 = 0,
visible: u32 = 0,
+ /// What the session said as it let this client go (owned, the C heap's).
+ refused: ?[]u8 = null,
+ /// A line over its frame, until its next key (`notify`): why it joined
+ /// again. The client's own, never the session's core.
+ notice_buf: [240]u8 = undefined,
+ notice_len: usize = 0,
+ /// PARDES_TEST_DIVERGE=<n>: once quiet after its n-th unit, its core
+ /// is put off the session's (a digested counter moved), the next unit's
+ /// digest finds it, and it joins again (test/daemon.py).
+ test_diverge: u32 = 0,
+ /// Put off so in this join: its checks are not written (they differ).
+ put_off: bool = false,
+
+ /// The replica the dial's dump makes, checked against the session as
+ /// it was dumped; null, and why in `why`. The dump is let go.
+ fn load(gpa: std.mem.Allocator, joined: *replica.Joined, o: pardes.Options, why: []u8) ?*replica.Replica {
+ defer {
+ gpa.free(joined.dump);
+ joined.dump = &.{};
+ }
+ const r = replica.Replica.initDump(gpa, joined.client, joined.view, joined.dump, o) catch |err| {
+ _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {};
+ return null;
+ };
+ if (!r.loaded(joined.digest)) {
+ r.deinit();
+ _ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {};
+ return null;
+ }
+ return r;
+ }
+
+ /// A join's link, from its start.
+ fn reset(c: *Remote, r: *replica.Replica, fd: posix.fd_t) void {
+ c.r = r;
+ c.fd = fd;
+ c.quit = false;
+ c.gone = false;
+ c.dirty = true;
+ c.owed = false;
+ c.changed = false;
+ c.asked = false;
+ c.mispredicted = 0;
+ c.put_off = false;
+ }
+
+ fn notify(c: *Remote, comptime fmt: []const u8, args: anytype) void {
+ const text = std.fmt.bufPrint(&c.notice_buf, fmt, args) catch c.notice_buf[0..];
+ for (text) |*b| if (b.* < 0x20 or b.* > 0x7e) {
+ b.* = '?';
+ };
+ c.notice_len = text.len;
+ }
/// PARDES_REPLICA_STATS=<file>: how its inputs and the session's units
/// went, a line at its end (test/daemon.py, the gates).
@@ -1324,7 +1379,7 @@ const Remote = struct {
fn checked(c: *Remote, theirs: u64) void {
c.asked = false;
const path = c.check orelse return;
- if (!c.r.quiet()) return;
+ if (!c.r.quiet() or c.put_off) return;
const mine = c.r.digest();
var buf: [96]u8 = undefined;
const line = (if (mine == theirs) std.fmt.bufPrint(&buf, "agree {x:0>16}\n", .{mine}) else std.fmt.bufPrint(&buf, "differ {x:0>16} {x:0>16}\n", .{ mine, theirs })) catch return;
@@ -1383,7 +1438,12 @@ fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loo
const bytes = std.heap.c_allocator.dupe(u8, payload) catch return false;
l.postEvent(.{ .screen = bytes }) catch std.heap.c_allocator.free(bytes);
},
- .refused => return false,
+ .refused => {
+ const words = (wire.decodeServer(tag, payload) catch return false).refused;
+ const bytes = std.heap.c_allocator.dupe(u8, words) catch return false;
+ l.postEvent(.{ .refused = bytes }) catch std.heap.c_allocator.free(bytes);
+ return false;
+ },
.pong, .welcome, .dump => {},
}
return true;
@@ -1402,14 +1462,16 @@ fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loo
pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, session: []const u8) !void {
const io = init.io;
const gpa = init.gpa;
- var why: [600]u8 = @splat(0);
- var joined = replica.dial(gpa, path, session, &why) orelse {
- var said: [700]u8 = undefined;
- std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: --attach: {s}\n", .{std.mem.sliceTo(&why, 0)}) catch "pardes: --attach failed\n") catch {};
- std.process.exit(1);
+ const Said = struct {
+ fn no(o: std.Io, comptime fmt: []const u8, args: anytype) noreturn {
+ var said: [800]u8 = undefined;
+ std.Io.File.stderr().writeStreamingAll(o, std.fmt.bufPrint(&said, "pardes: --attach: " ++ fmt ++ "\n", args) catch "pardes: --attach failed\n") catch {};
+ std.process.exit(1);
+ }
};
- defer _ = libc.close(joined.fd);
- defer joined.rest.deinit(gpa);
+ var why: [600]u8 = @splat(0);
+ var joined = replica.dial(gpa, path, session, &why) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)});
+ if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{upgrade_words});
const allocs = pardes.memory.init(gpa);
defer pardes.memory.deinit();
@@ -1424,19 +1486,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se
var o = opts;
o.image_allocator = allocs.image;
o.pdf_allocator = allocs.pdf;
- const r = replica.Replica.initDump(gpa, joined.client, joined.view, joined.dump, o) catch |err| {
- gpa.free(joined.dump);
- var said: [200]u8 = undefined;
- std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: --attach: the session's dump would not load: {t}\n", .{err}) catch "pardes: --attach: the dump would not load\n") catch {};
- std.process.exit(1);
- };
- gpa.free(joined.dump);
- defer r.deinit();
- r.gmtoff = joined.gmtoff;
- if (!r.loaded(joined.digest)) {
- std.Io.File.stderr().writeStreamingAll(io, "pardes: --attach: the session's dump loaded as another session (a build's dump is not complete)\n") catch {};
- std.process.exit(1);
- }
+ var r = Remote.load(gpa, &joined, o, &why) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)});
var winch_set = posix.sigemptyset();
posix.sigaddset(&winch_set, posix.SIG.WINCH);
@@ -1449,7 +1499,6 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se
try screen.enter(try vaxis.Tty.init(io, &tty_buf));
defer screen.leave();
defer screen.glide.deinit(gpa);
- screen.feed.view = joined.view;
var paste_buf: std.Io.Writer.Allocating = .init(gpa);
defer paste_buf.deinit();
var loop: Loop = .init(io, &screen.tty, &screen.vx);
@@ -1458,6 +1507,7 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se
var link: Remote = .{ .r = r, .fd = joined.fd };
if (init.environ_map.get("PARDES_REPLICA_CHECK")) |file| link.check = try gpa.dupeSentinel(u8, file, 0);
defer if (link.check) |file| gpa.free(file);
+ if (init.environ_map.get("PARDES_TEST_DIVERGE")) |n| link.test_diverge = std.fmt.parseInt(u32, n, 10) catch 0;
var sh: Shell = .{
.io = io,
.gpa = gpa,
@@ -1471,77 +1521,130 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se
.from = joined.view,
.remote = &link,
};
- sh.terms[joined.view] = &screen;
try startInput(&loop, &screen);
defer stopInput(io, &screen.thread);
(try std.Thread.spawn(.{}, winchWatch, .{&loop})).detach();
- var reader_done: std.atomic.Value(bool) = .init(false);
- const reader = try std.Thread.spawn(.{}, linkReader, .{ gpa, joined.fd, &joined.rest, &loop, &reader_done });
if (link.check != null) (try std.Thread.spawn(.{}, wakeWatch, .{ io, &loop })).detach();
try screen.vx.queryTerminalSend(screen.tty.writer());
- // Its size is its view's first input.
- sh.winched();
- while (!link.quit) {
- if (link.dirty) link.draw(&sh);
- const first = (loop.tryEvent() catch null) orelse idle: {
- // Idle, its frame drawn: the next key needs no copy taken.
- r.ready();
- break :idle loop.nextEvent() catch break;
- };
- var ev = first;
- var batch: usize = 1;
- while (true) {
- _ = sh.apply(ev);
- if (link.quit or (!sh.in_paste and batch >= 64)) break;
- ev = (loop.tryEvent() catch null) orelse break;
- batch += 1;
- }
- r.settle();
- if (screen.caps_pending and screen.vx.queries_done.load(.unordered)) {
- screen.caps_pending = false;
- screen.vx.enableDetectedFeatures(screen.tty.writer()) catch {};
- screen.vx.queueRefresh();
- link.dirty = true;
+ // A join a pass: the first, and one after the replica diverged or the
+ // session let it go to upgrade. The terminal stays as it is.
+ while (true) {
+ r.gmtoff = joined.gmtoff;
+ sh.core = r.core();
+ sh.from = joined.view;
+ sh.terms = @splat(null);
+ sh.terms[joined.view] = &screen;
+ screen.feed.view = joined.view;
+ link.reset(r, joined.fd);
+ var reader_done: std.atomic.Value(bool) = .init(false);
+ const reader = try std.Thread.spawn(.{}, linkReader, .{ gpa, joined.fd, &joined.rest, &loop, &reader_done });
+ // Its size is its view's first input.
+ screen.size = null;
+ sh.winched();
+ screen.vx.queueRefresh();
+ while (!link.quit) {
+ if (link.dirty) link.draw(&sh);
+ const first = (loop.tryEvent() catch null) orelse idle: {
+ // Idle, its frame drawn: the next key needs no copy taken.
+ r.ready();
+ break :idle loop.nextEvent() catch break;
+ };
+ var ev = first;
+ var batch: usize = 1;
+ while (true) {
+ _ = sh.apply(ev);
+ if (link.quit or (!sh.in_paste and batch >= 64)) break;
+ ev = (loop.tryEvent() catch null) orelse break;
+ batch += 1;
+ }
+ r.settle();
+ if (screen.caps_pending and screen.vx.queries_done.load(.unordered)) {
+ screen.caps_pending = false;
+ screen.vx.enableDetectedFeatures(screen.tty.writer()) catch {};
+ screen.vx.queueRefresh();
+ link.dirty = true;
+ }
+ if (link.owed) link.dirty = true;
+ if (link.check != null) {
+ // Quiet a while after a change: asked once (a wake, then the
+ // next quiet one).
+ if (first == .wake and link.changed and !link.asked and r.quiet()) {
+ link.changed = false;
+ link.asked = replica.askCheck(joined.fd);
+ } else if (link.changed and r.quiet()) requestWake(io, 300);
+ }
+ if (r.diverged != null) link.quit = true;
}
- if (link.owed) link.dirty = true;
- if (link.check != null) {
- // Quiet a while after a change: asked once (a wake, then the
- // next quiet one).
- if (first == .wake and link.changed and !link.asked and r.quiet()) {
- link.changed = false;
- link.asked = replica.askCheck(joined.fd);
- } else if (link.changed and r.quiet()) requestWake(io, 300);
+ if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file);
+ if (!link.gone) replica.bye(joined.fd);
+ // The link's reader ends with the link, posting as it goes: what it
+ // and the terminal posted is let go until it has.
+ _ = libc.shutdown(joined.fd, 2);
+ while (true) {
+ while (loop.tryEvent() catch null) |ev| switch (ev) {
+ .unit, .screen, .refused => |b| std.heap.c_allocator.free(b),
+ .paste => |b| gpa.free(@constCast(b)),
+ else => {},
+ };
+ if (reader_done.load(.acquire)) break;
+ std.Thread.yield() catch {};
}
+ reader.join();
+ _ = libc.close(joined.fd);
+ joined.rest.deinit(gpa);
+ // Why it joins again, in a line over its frame until a key.
if (r.diverged) |reason| {
- screen.leave();
- var said: [700]u8 = undefined;
- std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: --attach: this replica diverged from '{s}': {s}\n", .{ session, reason }) catch "pardes: --attach: diverged\n") catch {};
- std.process.exit(1);
+ link.notify("joined again: this client diverged from the session ({s})", .{reason});
+ } else if (link.refused) |words| {
+ link.notify("joined again: {s}", .{words});
+ std.heap.c_allocator.free(words);
+ link.refused = null;
+ } else {
+ r.deinit();
+ if (link.gone) {
+ screen.leave();
+ Said.no(io, "'{s}' let this client go (a Detach in it, or the session's end)", .{session});
+ }
+ return;
}
- }
- if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file);
- if (!link.gone) replica.bye(joined.fd);
- // The link's reader ends with the link, posting as it goes: what it
- // and the terminal posted is let go until it has.
- _ = libc.shutdown(joined.fd, 2);
- while (true) {
- while (loop.tryEvent() catch null) |ev| switch (ev) {
- .unit, .screen => |b| std.heap.c_allocator.free(b),
- .paste => |b| gpa.free(@constCast(b)),
- else => {},
+ r.deinit();
+ // The session again: an upgrade's takes the time its exec and load
+ // take, and its daemon's.
+ var tries: u32 = 0;
+ joined = while (true) : (tries += 1) {
+ why = @splat(0);
+ if (replica.dial(gpa, path, session, &why)) |j| {
+ if (j.semantics == wire.semantics) break j;
+ replica.bye(j.fd);
+ _ = libc.close(j.fd);
+ var jj = j;
+ jj.rest.deinit(gpa);
+ gpa.free(j.dump);
+ screen.leave();
+ Said.no(io, "{s}", .{upgrade_words});
+ }
+ const words = std.mem.sliceTo(&why, 0);
+ if (std.mem.indexOf(u8, words, "runs another build") != null) {
+ screen.leave();
+ Said.no(io, "{s}", .{upgrade_words});
+ }
+ if (tries > 200) {
+ screen.leave();
+ Said.no(io, "{s}", .{words});
+ }
+ const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms };
+ _ = libc.nanosleep(&ts, null);
+ };
+ r = Remote.load(gpa, &joined, o, &why) orelse {
+ screen.leave();
+ Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)});
};
- if (reader_done.load(.acquire)) break;
- std.Thread.yield() catch {};
- }
- reader.join();
- if (link.gone) {
- screen.leave();
- var said: [300]u8 = undefined;
- std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: --attach: '{s}' let this client go (a Detach in it, or the session's end)\n", .{session}) catch "pardes: --attach: the session let this client go\n") catch {};
}
}
+const upgrade_words = "session upgraded to a newer pardes: upgrade this client too";
+
/// A terminal a session draws on, while it has one: entered (raw, the
/// alternate screen, mouse and paste reports) and left (all of it put back,
/// the terminal closed) at the session's start and end, and at a detach and
@@ -2048,6 +2151,8 @@ const Shell = struct {
/// (applied at once there, and sent to the session).
fn feed(s: *Shell, ev: pardes.Event) void {
const c = s.remote orelse return s.core.update(ev);
+ // Its notice read: a key of its own puts it away.
+ if (c.notice_len > 0 and (ev == .key or ev == .paste)) c.notice_len = 0;
const msg = c.r.input(ev) catch return;
if (!replica.sendInput(c.fd, msg)) c.quit = true;
c.dirty = true;
@@ -2093,6 +2198,7 @@ const Shell = struct {
.detach = detach,
.adopt = adopt,
.upgrade = upgradeAsked,
+ .newer_build = newerBuild,
.spawn = spawn,
.pty_write = ptyWrite,
.pty_resize = ptyResize,
@@ -2268,6 +2374,12 @@ const Shell = struct {
daemon.adopt();
}
+ /// A client's own build is not the session's to say (its core's host
+ /// is the session's, through the tape).
+ fn newerBuild(ctx: ?*anyopaque) bool {
+ return of(ctx).remote == null and upgrade.newer();
+ }
+
fn upgradeAsked(ctx: ?*anyopaque, pane: u8) void {
const s = of(ctx);
if (comptime !upgrade.supported) return s.core.reportFailure(pane, "Upgrade: this system cannot move a session to another build in place");
@@ -2301,6 +2413,9 @@ const Shell = struct {
for (s.terms) |slot| if (slot) |sc| stopInput(s.io, &sc.thread);
while (s.loop.tryEvent() catch null) |ev| _ = s.apply(ev);
core.settle();
+ // Its replica clients told, their views gone before the dump: each
+ // dials the session again once it is back.
+ s.peers.refuseAll(core, "the session is upgrading to another build of pardes");
var rec: upgrade.Record = .{ .link = daemon.link, .stub = daemon.stub, .exe = path };
for (s.ptys, 0..) |slot, id| if (slot) |pt| rec.addPty(id, pt.pid, pt.file.handle);
for (s.terms, 0..) |slot, k| if (slot) |sc| if (sc.on) rec.addTty(.{
@@ -2668,6 +2783,11 @@ const Shell = struct {
.unit => |bytes| {
if (s.remote) |c| {
c.r.confirm(bytes);
+ if (c.test_diverge != 0 and c.r.stats.units >= c.test_diverge and c.r.quiet()) {
+ c.test_diverge = 0;
+ c.put_off = true;
+ c.r.core().next_serial +%= 1000;
+ }
if (c.r.moved) c.dirty = true;
c.r.moved = false;
c.changed = true;
@@ -2684,17 +2804,18 @@ const Shell = struct {
std.heap.c_allocator.free(bytes);
},
.checked => |d| if (s.remote) |c| c.checked(d),
+ .refused => |words| if (s.remote) |c| {
+ if (c.refused) |old| std.heap.c_allocator.free(old);
+ c.refused = words;
+ c.quit = true;
+ return true;
+ } else std.heap.c_allocator.free(words),
.link_gone => if (s.remote) |c| {
c.quit = true;
c.gone = true;
return true;
},
- .focus_in => {
- s.feed(.{ .focus = true });
- // A client's core is the session's: the notice is the session's to give.
- // ponytail: not while clients replicate it (set outside a step).
- if (s.remote == null and !s.peers.recording) upgrade.noticeNewer(core);
- },
+ .focus_in => s.feed(.{ .focus = true }),
.focus_out => {
s.feed(.pointer_leave);
s.feed(.{ .focus = false });
@@ -2887,6 +3008,7 @@ const Shell = struct {
const flashed = sc.flash.apply(s.frame.allocator(), shadowed, s.core, truecolor) catch shadowed;
const trailed = sc.trail.apply(s.frame.allocator(), flashed, s.core, truecolor) catch flashed;
paintCells(win, trailed.cells, trailed.cols, trailed.rows);
+ if (s.remote) |c| if (c.notice_len > 0) noticeLine(win, c.notice_buf[0..c.notice_len], trailed.cols, trailed.rows);
tz_cells.end();
for (surface.images[0..surface.nimages]) |maybe| {
const place = maybe orelse continue;
@@ -3641,6 +3763,17 @@ test "a mouse's side buttons, xterm's 8 and 9, are Back and Forward, as SDL's X1
}
}
+/// A replica client's own line over its frame's last row (`Remote.notify`),
+/// reversed. `text` is printable ASCII and outlives the render (vaxis keeps
+/// the slices).
+fn noticeLine(win: vaxis.Window, text: []const u8, cols: u16, rows: u16) void {
+ if (rows == 0) return;
+ for (0..cols) |x| win.writeCell(@intCast(x), rows - 1, .{
+ .char = .{ .grapheme = if (x < text.len) text[x..][0..1] else " " },
+ .style = .{ .reverse = true },
+ });
+}
+
/// A blank whose ink alone changed is not sent: vaxis's render skips it
/// (pardes's patch to Vaxis.zig in build.zig), since a blank's ink shows
/// nowhere. Deciding that here cost every cell of every frame a look at the