summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gui/gui.zig19
-rw-r--r--src/tui/tui.zig19
-rw-r--r--src/upgrade.zig35
3 files changed, 36 insertions, 37 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index 3800c794..33922286 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -3749,24 +3749,7 @@ const Shell = struct {
var rec: upgrade.Record = .{ .link = daemon.link, .window = s.gui != null, .exe = path };
for (s.ptys, 0..) |slot, id| if (slot) |pt| if (pt.fd >= 0) rec.addPty(id, pt.pid, pt.fd);
var at_buf: [1024:0]u8 = undefined;
- const failed: []const u8 = failed: {
- const at = pardes.dump.upgradePath(&at_buf, core.settings.dump_dir.get()) orelse break :failed "Upgrade: no directory for the session's dump; this session stays as it is";
- const bytes = pardes.dump.dumpBytes(core) catch |err| {
- var words: [128]u8 = undefined;
- break :failed 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 s.gpa.free(bytes);
- if (!upgrade.writeDump(at, bytes)) break :failed std.fmt.bufPrint(&why, "Upgrade: the session's dump could not be written to {s}; it stays as it is", .{at[0..@min(at.len, 600)]}) catch "Upgrade: its dump could not be written";
- rec.dump = at;
- rec.old = upgrade.openSelf();
- daemon.upgraded();
- if (s.fs) |f| f.flushOut();
- const err_words = upgrade.exec(s.gpa, path, -1, &rec);
- // Still here: the exec failed.
- _ = libc.unlink(at);
- if (rec.old >= 0) _ = libc.close(rec.old);
- break :failed 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";
- };
+ const failed = upgrade.dumpInto(core, &rec, &at_buf, &why) orelse upgrade.execAs(s.gpa, path, &rec, &at_buf, s.fs, &why);
// As it was: its ptys read on.
for (s.ptys, 0..) |*slot, id| if (slot.*) |*pt| if (pt.fd >= 0 and pt.reader == null) {
pt.reader = s.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, @intCast(id), s.gens[id]) catch null;
diff --git a/src/tui/tui.zig b/src/tui/tui.zig
index ff0e8334..a698abf7 100644
--- a/src/tui/tui.zig
+++ b/src/tui/tui.zig
@@ -2123,16 +2123,7 @@ const Shell = struct {
});
var popped = false;
var at_buf: [1024:0]u8 = undefined;
- const failed: []const u8 = failed: {
- const at = pardes.dump.upgradePath(&at_buf, core.settings.dump_dir.get()) orelse break :failed "Upgrade: no directory for the session's dump; this session stays as it is";
- const bytes = pardes.dump.dumpBytes(core) catch |err| {
- var words: [128]u8 = undefined;
- break :failed 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 s.gpa.free(bytes);
- if (!upgrade.writeDump(at, bytes)) break :failed std.fmt.bufPrint(&why, "Upgrade: the session's dump could not be written to {s}; it stays as it is", .{at[0..@min(at.len, 600)]}) catch "Upgrade: its dump could not be written";
- rec.dump = at;
- rec.old = upgrade.openSelf();
+ const failed: []const u8 = upgrade.dumpInto(core, &rec, &at_buf, &why) orelse failed: {
// What this process put in its terminals that the next puts in
// again: its images, and its keyboard mode, a stack.
for (s.terms) |slot| if (slot) |sc| if (sc.on) {
@@ -2143,13 +2134,7 @@ const Shell = struct {
};
popped = true;
unlinkKittyShm();
- daemon.upgraded();
- if (s.fs) |f| f.flushOut();
- const err_words = upgrade.exec(s.gpa, path, -1, &rec);
- // Still here: the exec failed.
- _ = libc.unlink(at);
- if (rec.old >= 0) _ = libc.close(rec.old);
- break :failed 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";
+ break :failed upgrade.execAs(s.gpa, path, &rec, &at_buf, s.fs, &why);
};
// As it was: its ptys read on, its terminals' keys as they were.
for (&s.ptys, 0..) |*slot, id| if (slot.*) |*pt| {
diff --git a/src/upgrade.zig b/src/upgrade.zig
index 37c79991..2e5f683f 100644
--- a/src/upgrade.zig
+++ b/src/upgrade.zig
@@ -292,7 +292,7 @@ pub fn check(path: [:0]const u8, buf: []u8) Checked {
/// 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 {
+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));
}
@@ -335,7 +335,7 @@ 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.
-pub fn writeDump(path: [:0]const u8, bytes: []const u8) bool {
+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);
@@ -352,6 +352,37 @@ pub fn writeDump(path: [:0]const u8, bytes: []const u8) bool {
return true;
}
+/// 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).
+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| {
+ 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";
+ rec.dump = path;
+ 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).
+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 {
+ @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";
+}
+
// ---- after: the new process ----
/// The core an upgraded process starts with: the session its dump is. A