diff options
Diffstat (limited to 'src/upgrade.zig')
| -rw-r--r-- | src/upgrade.zig | 77 |
1 files changed, 47 insertions, 30 deletions
diff --git a/src/upgrade.zig b/src/upgrade.zig index d8f8f3c3..212a8176 100644 --- a/src/upgrade.zig +++ b/src/upgrade.zig @@ -2,15 +2,17 @@ //! 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 +//! At a step boundary a session commits its dump to the state file +//! (Db.zig, as `upgrade-<pid>`) 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. +//! shells adopted, the dump left for a Restore by hand. A record of an older +//! build names a `.dump` file instead (0.24 reads one, and deletes it once +//! loaded). const std = @import("std"); const builtin = @import("builtin"); const libc = std.c; @@ -114,8 +116,11 @@ pub const Record = struct { nttys: usize = 0, /// The path exec'd, the one a later Upgrade execs again. exe: []const u8 = "", - /// The session's dump. + /// The state file the session's dump is in (an older build's: the + /// `.dump` file it is). dump: []const u8 = "", + /// The dump's label there; none, `dump` is a file of its own. + label: []const u8 = "", /// Why the new build could not load it: a start that went back to the /// old binary says so. refused: []const u8 = "", @@ -158,6 +163,7 @@ pub const Record = struct { 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}); + if (rec.label.len > 0) try w.print("label {s}\n", .{rec.label}); try w.print("exe {s}\ndump {s}\n", .{ rec.exe, rec.dump }); } @@ -206,6 +212,8 @@ pub const Record = struct { rec.nttys += 1; } else if (std.mem.eql(u8, word, "refused")) { rec.refused = rest; + } else if (std.mem.eql(u8, word, "label")) { + rec.label = rest; } else if (std.mem.eql(u8, word, "exe")) { rec.exe = rest; } else if (std.mem.eql(u8, word, "dump")) { @@ -333,44 +341,44 @@ pub fn exec(gpa: std.mem.Allocator, path: ?[:0]const u8, old: c_int, rec: *const }; } -/// The dump of a session to `path` (0600, replaced): false when it could -/// not be written whole. -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); - if (@import("host_io.zig").writeFd(fd, bytes)) return true; - _ = libc.unlink(path); - return false; -} +/// The state file's path an upgrade's record names (`dumpInto`'s). +var state_path: [4200]u8 = undefined; -/// An Upgrade's dump, the terminal's and the window's alike: written -/// where a Restore finds it (dump.upgradePath, into `at`) and named in -/// `rec`, the binary it replaces held open for a new build that cannot -/// load it. Null when it is, else why in words (`why`'s). +/// An Upgrade's dump, the terminal's and the window's alike: committed to +/// the state file as `upgrade-<pid>` (its label kept in `at`), where a +/// Restore finds it, and named in `rec`, the binary it replaces held open +/// for a new build that cannot load it. Null when it is, else why in words +/// (`why`'s). pub fn dumpInto(core: *pardes.Pardes, rec: *Record, at: *[1024:0]u8, why: []u8) ?[]const u8 { - const path = pardes.dump.upgradePath(at, core.settings.dump_dir.get()) orelse return "Upgrade: no directory for the session's dump; this session stays as it is"; - const bytes = pardes.dump.dumpBytes(core) catch |err| { + const path = pardes.Db.statePath(&state_path, core.settings.dump_dir.get()) orelse return "Upgrade: no directory for the session's dump; this session stays as it is"; + var name: [64]u8 = undefined; + const label = std.fmt.bufPrint(&name, "upgrade-{d}", .{std.c.getpid()}) catch unreachable; + const bytes = pardes.dump.image(core, label) catch |err| { var words: [128]u8 = undefined; return std.fmt.bufPrint(why, "Upgrade: the session would not dump: {s}; it stays as it is", .{pardes.Messages.errorWords(err, &words)}) catch "Upgrade: the session would not dump"; }; defer core.gpa.free(bytes); - if (!writeDump(path, bytes)) return std.fmt.bufPrint(why, "Upgrade: the session's dump could not be written to {s}; it stays as it is", .{path[0..@min(path.len, 600)]}) catch "Upgrade: its dump could not be written"; + const put = core.db.putDump(core, bytes, label, at[0 .. at.len - 1]) catch |err| { + var words: [128]u8 = undefined; + return std.fmt.bufPrint(why, "Upgrade: the session's dump could not be written to {s} ({s}); it stays as it is", .{ path[0..@min(path.len, 600)], pardes.Messages.errorWords(err, &words) }) catch "Upgrade: its dump could not be written"; + }; + at[put.len] = 0; rec.dump = path; + rec.label = at[0..put.len]; rec.old = openSelf(); return null; } /// The daemon told, 9P's last answers sent (`fs`), and this process -/// exec'd as `path` with `rec`, whose dump `dumpInto` wrote at `at`. -/// Returns only when the exec failed, that dump removed and the binary it -/// replaces let go: why, in words (`why`'s). +/// exec'd as `path` with `rec`, whose dump `dumpInto` labelled `at`. +/// Returns only when the exec failed, the binary it replaces let go (the +/// dump stays, a dump among the state file's): why, in words (`why`'s). pub fn execAs(gpa: std.mem.Allocator, path: [:0]const u8, rec: *const Record, at: [*:0]const u8, fs: ?*@import("9p_io.zig").Listener, why: []u8) []const u8 { + _ = at; @import("daemon.zig").upgraded(); if (fs) |f| f.flushOut(); const err_words = exec(gpa, path, -1, rec); // Still here: the exec failed. - _ = libc.unlink(at); if (rec.old >= 0) _ = libc.close(rec.old); return std.fmt.bufPrint(why, "Upgrade: {s} would not start: {s}; this session stays as it is", .{ path[0..@min(path.len, 600)], err_words }) catch "Upgrade: the new pardes would not start"; } @@ -391,13 +399,17 @@ pub fn load(gpa: std.mem.Allocator, core_gpa: std.mem.Allocator, opts: pardes.Op 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; + const bytes = (if (rec.label.len > 0) pardes.Db.readDumpAt(gpa, rec.dump, rec.label) else 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; + // Restore's word for the dump left behind: its label, or its file. + const again_name = if (rec.label.len > 0) rec.label else rec.dump; if (loaded) |p| { - _ = libc.unlink(path); + // An older build's dump, a file of its own, goes once loaded; a + // label stays among the state file's dumps. + if (rec.label.len == 0) _ = libc.unlink(path); // The binary it replaced, let go: held, its file (deleted) would // keep its blocks for as long as this session runs. if (rec.old >= 0) _ = libc.close(rec.old); @@ -424,13 +436,13 @@ pub fn load(gpa: std.mem.Allocator, core_gpa: std.mem.Allocator, opts: pardes.Op 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"); + 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, again_name[0..@min(again_name.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" }; + var rec: Record = .{ .link = 5, .stub = 7, .old = 9, .window = true, .exe = "/a b/pardes", .dump = "/home/u/.local/share/pardes/pardes.xitdb", .label = "upgrade-1" }; rec.addPty(3, 1234, 11); rec.addPty(0, 99, 12); var tio: posix.termios = std.mem.zeroes(posix.termios); @@ -449,7 +461,12 @@ test "an upgrade's record reads back as it was written, and refuses one cut shor 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.expectEqualStrings("/home/u/.local/share/pardes/pardes.xitdb", back.dump); + try std.testing.expectEqualStrings("upgrade-1", back.label); + // An older build's: its dump a file, no label. + const older = try Record.decode("link 5\nexe /a\ndump /run/u/pardes-upgrade-1.dump\n"); + try std.testing.expectEqualStrings("/run/u/pardes-upgrade-1.dump", older.dump); + try std.testing.expectEqualStrings("", older.label); 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")); } |
