diff options
Diffstat (limited to 'runtime/upgrade.zig')
| -rw-r--r-- | runtime/upgrade.zig | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/runtime/upgrade.zig b/runtime/upgrade.zig index 2a8d6edf..f59117ee 100644 --- a/runtime/upgrade.zig +++ b/runtime/upgrade.zig @@ -19,9 +19,13 @@ const builtin = @import("builtin"); const libc = std.c; const posix = std.posix; const pardes = @import("core"); +const daemon = @import("daemon.zig"); pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd; pub const flag = "--upgraded="; +/// A window's pair's session's first word (main.zig `pair`): its first +/// client's fd, which an Upgrade does not name again (that client joined). +pub const paired_flag = "--paired="; /// 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; @@ -46,6 +50,7 @@ var located = false; /// 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 (given.len > 0 and std.mem.startsWith(u8, given[0], paired_flag)) given = given[1..]; 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; @@ -91,6 +96,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]; } @@ -128,6 +141,9 @@ pub const Record = struct { /// This process's own: its dump would not load, so it started afresh /// and adopts the shells. fresh: bool = false, + /// A pair's session's state (daemon.State): whether it is kept, which + /// the dump does not say. None: as its frontend says (attached or not). + state: ?daemon.State = null, 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; @@ -165,6 +181,7 @@ pub const Record = struct { 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}); + if (rec.state) |s| try w.print("state {t}\n", .{s}); try w.print("exe {s}\ndump {s}\n", .{ rec.exe, rec.dump }); } @@ -179,7 +196,7 @@ pub const Record = struct { const rest = line[sp + 1 ..]; var it = std.mem.tokenizeScalar(u8, rest, ' '); // A word of a later build's record is passed over. - switch (std.meta.stringToEnum(enum { link, stub, old, window, pty, tty, refused, label, exe, dump }, word) orelse continue) { + switch (std.meta.stringToEnum(enum { link, stub, old, window, pty, tty, refused, label, state, exe, dump }, word) orelse continue) { .link => rec.link = try std.fmt.parseInt(c_int, rest, 10), .stub => rec.stub = try std.fmt.parseInt(c_int, rest, 10), .old => rec.old = try std.fmt.parseInt(c_int, rest, 10), @@ -213,6 +230,7 @@ pub const Record = struct { }, .refused => rec.refused = rest, .label => rec.label = rest, + .state => rec.state = std.meta.stringToEnum(daemon.State, rest), .exe => rec.exe = rest, .dump => rec.dump = rest, } @@ -371,9 +389,14 @@ pub fn dumpInto(core: *pardes.Pardes, rec: *Record, at: *[1024:0]u8, why: []u8) /// 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(); + daemon.upgraded(); if (fs) |f| f.flushOut(); - const err_words = exec(gpa, path, -1, rec); + var with = rec.*; + with.state = switch (daemon.sessionState()) { + .pair, .kept => |s| s, + .attached, .detached => null, + }; + const err_words = exec(gpa, path, -1, &with); // Still here: the exec failed. 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"; @@ -441,7 +464,7 @@ pub fn load(gpa: std.mem.Allocator, core_gpa: std.mem.Allocator, opts: pardes.Op } 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 = "/home/u/.local/share/pardes/pardes.xitdb", .label = "upgrade-1" }; + 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", .state = .kept }; rec.addPty(3, 1234, 11); rec.addPty(0, 99, 12); var tio: posix.termios = std.mem.zeroes(posix.termios); @@ -462,10 +485,12 @@ test "an upgrade's record reads back as it was written, and refuses one cut shor try std.testing.expectEqualStrings("/a b/pardes", back.exe); 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. + try std.testing.expectEqual(daemon.State.kept, back.state.?); + // An older build's: its dump a file, no label, no state. 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.expect(older.state == null); 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")); } |
