diff options
Diffstat (limited to 'src/upgrade.zig')
| -rw-r--r-- | src/upgrade.zig | 389 |
1 files changed, 389 insertions, 0 deletions
diff --git a/src/upgrade.zig b/src/upgrade.zig new file mode 100644 index 00000000..c49d2370 --- /dev/null +++ b/src/upgrade.zig @@ -0,0 +1,389 @@ +//! Upgrade (docs/next-daemon.md, "Phase 6: upgrade notes"): a running +//! session, and the daemon, moved onto the pardes installed since they +//! started, in place. +//! +//! At a step boundary a session writes its dump (phase 2) and execs the +//! path it was started from. Its pty masters, terminals, stubs and daemon +//! link stay open across the exec, named in a record on the new command +//! line: `--upgraded=<record>` ahead of the arguments it was started with. +//! The pid stays, so its shells stay its children; the new process loads +//! the dump and takes those fds back (each host's `resume`). A new build +//! that cannot load the dump execs the old one again, from the copy this +//! process left open (Linux), and with none it starts afresh with the +//! shells adopted, the dump left for a Restore by hand. +const std = @import("std"); +const builtin = @import("builtin"); +const libc = std.c; +const posix = std.posix; +const pardes = @import("pardes.zig"); + +pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd; +pub const flag = "--upgraded="; + +/// What this build says it is (`pardes --version`'s first line). +pub const version_line = if (pardes.commit) |c| "pardes " ++ pardes.version ++ " (" ++ c ++ ")" else "pardes " ++ pardes.version; + +// ---- the path it was started from ---- + +/// The path this process was started from, what Upgrade execs: a reinstall +/// puts the new build there, while /proc/self/exe names the old file, +/// deleted by then. +var exe_buf: [4096:0]u8 = undefined; +var exe_len: usize = 0; +/// The arguments it was started with (an upgrade's record left out). +var given: []const [:0]const u8 = &.{}; + +/// Its path asked of the system now, at its start (one readlink), and its +/// arguments kept; an upgraded process takes the path its record names +/// (the system would name a fallback's old binary, deleted). +pub fn remember(args: []const [:0]const u8) void { + given = if (args.len > 1 and std.mem.startsWith(u8, args[1], flag)) args[2..] else if (args.len > 0) args[1..] else &.{}; + if (resumed) |*rec| if (rec.exe.len > 0 and rec.exe.len < exe_buf.len) { + @memcpy(exe_buf[0..rec.exe.len], rec.exe); + exe_buf[rec.exe.len] = 0; + exe_len = rec.exe.len; + return; + }; + const n = std.process.executablePath(std.Io.Threaded.global_single_threaded.io(), &exe_buf) catch 0; + exe_buf[n] = 0; + exe_len = n; +} + +pub fn exe() ?[:0]const u8 { + return if (exe_len == 0) null else exe_buf[0..exe_len :0]; +} + +// ---- the record ---- + +pub const Pty = struct { pane: u8, pid: libc.pid_t, fd: c_int }; +/// A terminal and its view: the primary one's stub is daemon.stub; its +/// termios is the cooked one it is put back to at the end (it is raw now). +pub const Tty = struct { view: u8, fd: c_int, stub: c_int, primary: bool, termios: posix.termios }; + +pub const Record = struct { + /// daemon.link and daemon.stub. + link: c_int = -1, + stub: c_int = -1, + /// The binary being replaced, open (Linux): a new one that cannot load + /// the dump execs it again. + old: c_int = -1, + /// A window was open (the GUI). + window: bool = false, + ptys: [pardes.MAX_PANES]Pty = undefined, + nptys: usize = 0, + ttys: [pardes.views.max]Tty = undefined, + nttys: usize = 0, + /// The path exec'd, the one a later Upgrade execs again. + exe: []const u8 = "", + /// The session's dump. + dump: []const u8 = "", + /// Why the new build could not load it: a start that went back to the + /// old binary says so. + refused: []const u8 = "", + /// This process's own: its dump would not load, so it started afresh + /// and adopts the shells. + fresh: bool = false, + + pub fn addPty(rec: *Record, pane: usize, pid: libc.pid_t, fd: c_int) void { + if (rec.nptys == rec.ptys.len or fd < 0) return; + rec.ptys[rec.nptys] = .{ .pane = @intCast(pane), .pid = pid, .fd = fd }; + rec.nptys += 1; + } + + pub fn addTty(rec: *Record, t: Tty) void { + if (rec.nttys == rec.ttys.len or t.fd < 0) return; + rec.ttys[rec.nttys] = t; + rec.nttys += 1; + } + + /// Every fd it names, close-on-exec (`on`) or kept across one. + pub fn closeOnExec(rec: *const Record, on: bool) void { + const set = struct { + fn f(fd: c_int, cloexec: bool) void { + if (fd >= 0) _ = libc.fcntl(fd, libc.F.SETFD, @as(c_int, if (cloexec) 1 else 0)); + } + }.f; + set(rec.link, on); + set(rec.stub, on); + set(rec.old, on); + for (rec.ptys[0..rec.nptys]) |p| set(p.fd, on); + for (rec.ttys[0..rec.nttys]) |t| { + set(t.fd, on); + set(t.stub, on); + } + } + + /// One `word value` a line, the dump's path last (its line is all of it). + pub fn encode(rec: *const Record, w: *std.Io.Writer) !void { + try w.print("link {d}\nstub {d}\nold {d}\nwindow {d}\n", .{ rec.link, rec.stub, rec.old, @intFromBool(rec.window) }); + for (rec.ptys[0..rec.nptys]) |p| try w.print("pty {d} {d} {d}\n", .{ p.pane, p.pid, p.fd }); + for (rec.ttys[0..rec.nttys]) |t| try w.print("tty {d} {d} {d} {d} {x}\n", .{ t.view, t.fd, t.stub, @intFromBool(t.primary), std.mem.asBytes(&t.termios) }); + if (rec.refused.len > 0) try w.print("refused {s}\n", .{rec.refused}); + try w.print("exe {s}\ndump {s}\n", .{ rec.exe, rec.dump }); + } + + /// `text`'s record; its strings are slices of it. + pub fn decode(text: []const u8) !Record { + var rec: Record = .{}; + var lines = std.mem.splitScalar(u8, text, '\n'); + while (lines.next()) |line| { + if (line.len == 0) continue; + const sp = std.mem.indexOfScalar(u8, line, ' ') orelse return error.BadRecord; + const word = line[0..sp]; + const rest = line[sp + 1 ..]; + var it = std.mem.tokenizeScalar(u8, rest, ' '); + if (std.mem.eql(u8, word, "link")) { + rec.link = try std.fmt.parseInt(c_int, rest, 10); + } else if (std.mem.eql(u8, word, "stub")) { + rec.stub = try std.fmt.parseInt(c_int, rest, 10); + } else if (std.mem.eql(u8, word, "old")) { + rec.old = try std.fmt.parseInt(c_int, rest, 10); + } else if (std.mem.eql(u8, word, "window")) { + rec.window = std.mem.eql(u8, rest, "1"); + } else if (std.mem.eql(u8, word, "pty")) { + if (rec.nptys == rec.ptys.len) return error.BadRecord; + const pane = try std.fmt.parseInt(u8, it.next() orelse return error.BadRecord, 10); + if (pane >= pardes.MAX_PANES) return error.BadRecord; + rec.ptys[rec.nptys] = .{ + .pane = pane, + .pid = try std.fmt.parseInt(libc.pid_t, it.next() orelse return error.BadRecord, 10), + .fd = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), + }; + rec.nptys += 1; + } else if (std.mem.eql(u8, word, "tty")) { + if (rec.nttys == rec.ttys.len) return error.BadRecord; + var t: Tty = .{ + .view = try std.fmt.parseInt(u8, it.next() orelse return error.BadRecord, 10), + .fd = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), + .stub = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), + .primary = std.mem.eql(u8, it.next() orelse return error.BadRecord, "1"), + .termios = undefined, + }; + const hex = it.next() orelse return error.BadRecord; + // Another build's termios is the kernel's, of one size. + if (hex.len != 2 * @sizeOf(posix.termios) or t.view >= pardes.views.max) return error.BadRecord; + _ = try std.fmt.hexToBytes(std.mem.asBytes(&t.termios), hex); + rec.ttys[rec.nttys] = t; + rec.nttys += 1; + } else if (std.mem.eql(u8, word, "refused")) { + rec.refused = rest; + } else if (std.mem.eql(u8, word, "exe")) { + rec.exe = rest; + } else if (std.mem.eql(u8, word, "dump")) { + rec.dump = rest; + } + // A word of a later build's record is passed over. + } + if (rec.dump.len == 0) return error.BadRecord; + return rec; + } +}; + +/// The record this process was started with (`--upgraded=`), set by main. +pub var resumed: ?Record = null; + +// ---- before: asking the new binary, and the exec ---- + +pub const Checked = union(enum) { ok: []const u8, no: []const u8 }; + +/// `path --version` run as it is: a binary not there, not a pardes, or one +/// that cannot start, found before anything is let go. Its first line +/// (`pardes <version>`), or why not, in `buf`. +pub fn check(path: [:0]const u8, buf: []u8) Checked { + var pipe: [2]c_int = undefined; + if (libc.pipe(&pipe) != 0) return .{ .no = "Upgrade: no pipe to ask the new pardes its version" }; + const argv = [_:null]?[*:0]const u8{ path, "--version", null }; + const pid = libc.fork(); + if (pid < 0) { + _ = libc.close(pipe[0]); + _ = libc.close(pipe[1]); + return .{ .no = "Upgrade: no process to ask the new pardes its version" }; + } + if (pid == 0) { + // Only async-signal-safe calls between fork and exec. + _ = libc.dup2(pipe[1], 1); + const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR }, @as(libc.mode_t, 0)); + if (null_fd >= 0) { + _ = libc.dup2(null_fd, 0); + _ = libc.dup2(null_fd, 2); + } + _ = libc.execve(path, &argv, @ptrCast(libc.environ)); + libc._exit(127); + } + _ = libc.close(pipe[1]); + var len: usize = 0; + var waited = false; + while (len < buf.len) { + // A pardes answers --version in milliseconds; 10 s is one that hangs. + var fds = [_]libc.pollfd{.{ .fd = pipe[0], .events = libc.POLL.IN, .revents = 0 }}; + const ready = libc.poll(&fds, 1, 10_000); + if (ready < 0 and libc.errno(ready) == .INTR) continue; + if (ready <= 0) { + _ = libc.kill(pid, .KILL); + waited = true; + break; + } + const n = libc.read(pipe[0], buf[len..].ptr, buf.len - len); + if (n < 0 and libc.errno(n) == .INTR) continue; + if (n <= 0) break; + len += @intCast(n); + } + _ = libc.close(pipe[0]); + var status: c_int = 0; + while (libc.waitpid(pid, &status, 0) < 0 and libc.errno(-1) == .INTR) {} + const s: u32 = @bitCast(status); + const said = buf[0..len]; + const first = said[0 .. std.mem.indexOfScalar(u8, said, '\n') orelse said.len]; + if (!waited and libc.W.IFEXITED(s) and libc.W.EXITSTATUS(s) == 0 and std.mem.startsWith(u8, first, "pardes ")) return .{ .ok = first }; + // The words go where the line was: the rest of `buf`. + var tail: [96]u8 = undefined; + const how = if (waited) + "did not answer --version in 10 s" + else if (libc.W.IFEXITED(s) and libc.W.EXITSTATUS(s) == 127) + "would not start" + else if (libc.W.IFEXITED(s) and libc.W.EXITSTATUS(s) != 0) + std.fmt.bufPrint(&tail, "exited {d} at --version", .{libc.W.EXITSTATUS(s)}) catch "failed at --version" + else if (libc.W.IFSIGNALED(s)) + std.fmt.bufPrint(&tail, "died of signal {d} at --version", .{libc.W.TERMSIG(s)}) catch "died at --version" + else + "does not answer --version as a pardes does"; + return .{ .no = std.fmt.bufPrint(buf, "Upgrade: {s} {s}; this session stays as it is", .{ path[0..@min(path.len, 400)], how }) catch "Upgrade: the new pardes would not start; this session stays as it is" }; +} + +/// The binary this process runs, open, for a new one to fall back to: on +/// Linux the file even once deleted. ponytail: FreeBSD has no /proc by +/// default, so a new build there that cannot load a dump starts afresh. +pub fn openSelf() c_int { + if (comptime builtin.os.tag != .linux) return -1; + return libc.open("/proc/self/exe", .{ .ACCMODE = .RDONLY, .CLOEXEC = true }, @as(libc.mode_t, 0)); +} + +/// This process exec'd as `path` (or the binary open on `old`), the record +/// on its command line and the fds it names kept open. Returns only when +/// the exec failed, those fds close-on-exec again: why, in words. +pub fn exec(gpa: std.mem.Allocator, path: ?[:0]const u8, old: c_int, rec: *const Record) []const u8 { + var text: std.Io.Writer.Allocating = .init(gpa); + defer text.deinit(); + text.writer.writeAll(flag) catch return "no memory for its record"; + rec.encode(&text.writer) catch return "no memory for its record"; + text.writer.writeByte(0) catch return "no memory for its record"; + const record: [*:0]const u8 = @ptrCast(text.written().ptr); + const argv = gpa.allocSentinel(?[*:0]const u8, given.len + 2, null) catch return "no memory for its arguments"; + defer gpa.free(argv); + argv[0] = if (path) |p| p.ptr else if (exe()) |e| e.ptr else "pardes"; + argv[1] = record; + for (given, argv[2..]) |a, *slot| slot.* = a.ptr; + rec.closeOnExec(false); + const envp: [*:null]const ?[*:0]const u8 = @ptrCast(libc.environ); + const r = if (path) |p| + libc.execve(p, argv.ptr, envp) + else if (comptime builtin.os.tag == .linux) + libc.execveat(old, "", argv.ptr, envp, .{ .SYMLINK_NOFOLLOW = false, .EMPTY_PATH = true }) + else + -1; + const e = libc.errno(r); + rec.closeOnExec(true); + return switch (e) { + .NOENT => "it is not there", + .ACCES, .PERM => "it may not be run", + .NOEXEC => "it is not a program this system runs", + .@"2BIG" => "its record and arguments are too long", + .NOMEM => "no memory for it", + .TXTBSY => "it is still being written", + else => "the exec failed", + }; +} + +/// The dump of a session to `path` (0600, replaced): false when it could +/// not be written whole. +pub fn writeDump(path: [:0]const u8, bytes: []const u8) bool { + const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true, .CLOEXEC = true }, @as(libc.mode_t, 0o600)); + if (fd < 0) return false; + defer _ = libc.close(fd); + var at: usize = 0; + while (at < bytes.len) { + const n = libc.write(fd, bytes[at..].ptr, bytes.len - at); + if (n < 0 and libc.errno(n) == .INTR) continue; + if (n <= 0) { + _ = libc.unlink(path); + return false; + } + at += @intCast(n); + } + return true; +} + +// ---- after: the new process ---- + +/// The core an upgraded process starts with: the session its dump is. A +/// build that cannot load it execs the binary it replaced to load it; with +/// none, a fresh session (`fresh`) the host adopts the shells into, the +/// dump left where a Restore finds it. `PARDES_TEST_UPGRADE_REFUSE` refuses +/// a load as such a build would (test/daemon.py's failing upgrade), in the +/// first start only, or (`all`) in the start it falls back to as well. +pub fn load(gpa: std.mem.Allocator, core_gpa: std.mem.Allocator, opts: pardes.Options, rec: *Record) !*pardes.Pardes { + var kept: [pardes.MAX_PANES]bool = @splat(false); + for (rec.ptys[0..rec.nptys]) |p| kept[p.pane] = true; + var path_buf: [4096:0]u8 = undefined; + const path = std.fmt.bufPrintSentinel(&path_buf, "{s}", .{rec.dump}, 0) catch ""; + const loaded = loaded: { + if (libc.getenv("PARDES_TEST_UPGRADE_REFUSE")) |how| if (rec.refused.len == 0 or std.mem.eql(u8, std.mem.span(how), "all")) + break :loaded error.RefusedByTest; + const bytes = pardes.filesystem.readFile(gpa, rec.dump) catch |err| break :loaded err; + defer gpa.free(bytes); + break :loaded pardes.dump.resumeFromDump(core_gpa, opts, bytes, &kept); + }; + var buf: [700]u8 = undefined; + if (loaded) |p| { + _ = libc.unlink(path); + p.setMessage(p.active, if (rec.refused.len > 0) + std.fmt.bufPrint(&buf, "Upgrade: the new build could not load this session ({s}); it goes on as {s}", .{ rec.refused[0..@min(rec.refused.len, 200)], version_line }) catch "Upgrade: the new build could not load this session; it goes on as before" + else + "Upgrade: now " ++ version_line); + return p; + } else |err| { + var words_buf: [128]u8 = undefined; + const why = if (err == error.RefusedByTest) "refused by PARDES_TEST_UPGRADE_REFUSE" else pardes.Messages.errorWords(err, &words_buf); + // The binary it replaced loads its own dump: tried once, never from + // a start that is that fallback already. + if (rec.old >= 0 and rec.refused.len == 0) { + var again = rec.*; + again.refused = why; + again.old = -1; + _ = exec(gpa, null, rec.old, &again); + } + if (rec.old >= 0) _ = libc.close(rec.old); + rec.old = -1; + rec.fresh = true; + var o = opts; + o.load_path = null; + const p = try pardes.Pardes.init(core_gpa, o); + p.setMessage(p.active, std.fmt.bufPrint(&buf, "Upgrade: no build could load the session ({s}); its shells are here, and Restore {s} brings the rest back", .{ why, rec.dump[0..@min(rec.dump.len, 400)] }) catch "Upgrade: no build could load the session; its dump is kept for a Restore"); + return p; + } +} + +test "an upgrade's record reads back as it was written, and refuses one cut short" { + var rec: Record = .{ .link = 5, .stub = 7, .old = 9, .window = true, .exe = "/a b/pardes", .dump = "/run/u/pardes-upgrade-1.dump" }; + rec.addPty(3, 1234, 11); + rec.addPty(0, 99, 12); + var tio: posix.termios = std.mem.zeroes(posix.termios); + std.mem.asBytes(&tio)[0] = 0x5a; + rec.addTty(.{ .view = 2, .fd = 13, .stub = 14, .primary = true, .termios = tio }); + var buf: [4096]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + try rec.encode(&w); + const back = try Record.decode(w.buffered()); + try std.testing.expectEqual(@as(c_int, 5), back.link); + try std.testing.expectEqual(@as(c_int, 9), back.old); + try std.testing.expect(back.window); + try std.testing.expectEqual(@as(usize, 2), back.nptys); + try std.testing.expectEqual(Pty{ .pane = 3, .pid = 1234, .fd = 11 }, back.ptys[0]); + try std.testing.expectEqual(@as(usize, 1), back.nttys); + try std.testing.expect(back.ttys[0].primary and back.ttys[0].view == 2 and back.ttys[0].stub == 14); + try std.testing.expectEqual(@as(u8, 0x5a), std.mem.asBytes(&back.ttys[0].termios)[0]); + try std.testing.expectEqualStrings("/a b/pardes", back.exe); + try std.testing.expectEqualStrings("/run/u/pardes-upgrade-1.dump", back.dump); + try std.testing.expectError(error.BadRecord, Record.decode("link 5\n")); + try std.testing.expectError(error.BadRecord, Record.decode("tty 0 1 2 1 abc\ndump x\n")); +} |
