summaryrefslogtreecommitdiff
path: root/src/daemon.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon.zig')
-rw-r--r--src/daemon.zig145
1 files changed, 142 insertions, 3 deletions
diff --git a/src/daemon.zig b/src/daemon.zig
index 702d11b1..43f15c6c 100644
--- a/src/daemon.zig
+++ b/src/daemon.zig
@@ -27,6 +27,7 @@ const libc = std.c;
const posix = std.posix;
const ninep_io = @import("9p_io.zig");
const wire = @import("detached/wire.zig");
+const upgrade = @import("upgrade.zig");
const log = std.log.scoped(.replica);
pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd;
@@ -355,6 +356,33 @@ pub fn adopt() void {
tell("adopt", &.{});
}
+/// This session is about to exec another build (upgrade.zig): the daemon
+/// is asked to move too, and every replica told why it is let go.
+/// ponytail: a pong its thread writes at the same moment may split the
+/// words; the client reconnects either way.
+pub fn upgraded() void {
+ tell("upgrade", &.{});
+ var out: [600]u8 = undefined;
+ const said = wire.encodeServer(&out, .{ .refused = "the session is upgrading to another build of pardes: connect again with that build" }) catch return;
+ for (&replica_fds) |*slot| {
+ const fd = slot.load(.acquire);
+ if (fd < 0) continue;
+ _ = writeAll(fd, said);
+ _ = libc.shutdown(fd, 2);
+ }
+}
+
+/// An upgraded session's link and stub as its old process left them: its
+/// build's hello said again on the link (the daemon keeps its masters as
+/// they were), and the stub's death a HUP again.
+pub fn resumeLink(fd: c_int, stub_fd: c_int, k: Kind, named: []const u8, detached: bool) void {
+ if (comptime !supported) return;
+ stub = stub_fd;
+ if (stub >= 0) hupOnStubDeath(true);
+ setName(named);
+ if (fd >= 0 and hello(fd, k, libc.getpid(), name(), detached)) link = fd else if (fd >= 0) _ = libc.close(fd);
+}
+
pub const Note = union(enum) {
/// A terminal for a detached tty session (`pardes --attach`), and the
/// stub's pipe; for a window session, both -1: open a window.
@@ -422,6 +450,8 @@ fn writeAll(fd: c_int, bytes: []const u8) bool {
return true;
}
+var replica_fds: [max_replicas]std.atomic.Value(c_int) = @splat(.init(-1));
+
/// A replica the daemon handed over, its hello accepted: welcomed, its pings
/// answered, until it says bye or hangs up. ponytail: phase 5 proper makes it
/// a replica (the log, the dump, terminal state); this stub is what times a
@@ -429,6 +459,11 @@ fn writeAll(fd: c_int, bytes: []const u8) bool {
/// than its buffer ends it.
fn serveReplica(fd: c_int) void {
defer _ = libc.close(fd);
+ // Where an upgrade finds it to say why it ends (`upgraded`).
+ const slot = for (&replica_fds) |*r| {
+ if (r.cmpxchgStrong(-1, fd, .acq_rel, .acquire) == null) break r;
+ } else null;
+ defer if (slot) |r| r.store(-1, .release);
log.info("a replica connected to '{s}'", .{name()});
defer log.info("a replica of '{s}' left", .{name()});
var out: [wire.hello_max]u8 = undefined;
@@ -698,6 +733,8 @@ const Daemon = struct {
conns: [max_conns]Conn = @splat(.{}),
held: [max_held]Held = @splat(.{}),
replicas: [max_replicas]Replica = @splat(.{}),
+ /// A session asked it to move onto the build installed since (`reexec`).
+ upgrade_asked: bool = false,
fn idle(d: *const Daemon) bool {
for (d.conns) |c| if (c.fd >= 0) return false;
@@ -776,6 +813,10 @@ const Daemon = struct {
for (&d.held) |*h| if (h.fd >= 0 and h.owner == i) d.drop(h);
return;
}
+ if (std.mem.eql(u8, verb, "upgrade")) {
+ d.upgrade_asked = true;
+ return;
+ }
if (std.mem.eql(u8, verb, "adopt")) {
var line: [msg_max]u8 = undefined;
var w: std.Io.Writer = .fixed(&line);
@@ -920,6 +961,93 @@ const Daemon = struct {
_ = libc.close(fd);
}
+ /// Upgrade: this daemon exec'd as the pardes now at the path it was
+ /// started from, on the same listeners and lock, every session's link
+ /// and every master kept open, its registry on the new command line
+ /// (`restore`). A replica whose hello is still on its way is let go: it
+ /// dials again. Returns when it cannot be done, as it was.
+ fn reexec(d: *Daemon, replicas: c_int) void {
+ const path = upgrade.exe() orelse return;
+ var why: [700]u8 = undefined;
+ switch (upgrade.check(path, &why)) {
+ .no => |w| return log.warn("{s}", .{w}),
+ .ok => {},
+ }
+ var text: [flag_room]u8 = undefined;
+ var w: std.Io.Writer = .fixed(&text);
+ w.print("{s}replicas {d}\n", .{ upgrade.flag, replicas }) catch return;
+ for (&d.conns, 0..) |*c, i| if (c.fd >= 0) {
+ const k: []const u8 = if (c.kind) |k| @tagName(k) else "-";
+ w.print("c {d} {d} {s} {d} {d} {x} {s}\n", .{ i, c.fd, k, c.pid, @intFromBool(c.detached), c.semantics, c.named() }) catch return;
+ };
+ for (&d.held) |*h| if (h.fd >= 0) {
+ w.print("h {d} {d} {d}\n", .{ h.fd, h.pid, h.owner orelse 255 }) catch return;
+ };
+ w.writeByte(0) catch return;
+ for (&d.replicas) |*r| if (r.fd >= 0) {
+ _ = libc.close(r.fd);
+ r.* = .{};
+ };
+ const keep = struct {
+ fn all(dd: *Daemon, rep: c_int, on: bool) void {
+ const v: c_int = if (on) 0 else 1;
+ for ([_]c_int{ 3, 4, rep }) |fd| if (fd >= 0) {
+ _ = libc.fcntl(fd, libc.F.SETFD, v);
+ };
+ for (&dd.conns) |*c| if (c.fd >= 0) {
+ _ = libc.fcntl(c.fd, libc.F.SETFD, v);
+ };
+ for (&dd.held) |*h| if (h.fd >= 0) {
+ _ = libc.fcntl(h.fd, libc.F.SETFD, v);
+ };
+ }
+ }.all;
+ const argv = [_:null]?[*:0]const u8{ path, "--daemon", @ptrCast(&text), null };
+ keep(d, replicas, true);
+ _ = libc.execve(path, &argv, @ptrCast(libc.environ));
+ keep(d, replicas, false);
+ log.warn("upgrade: {s} would not start", .{path});
+ }
+
+ /// The registry an upgraded daemon was exec'd with (`reexec`); the
+ /// replicas' listener, its fd, or -1.
+ fn restore(d: *Daemon, text: []const u8) c_int {
+ var replicas: c_int = -1;
+ var lines = std.mem.splitScalar(u8, text, '\n');
+ while (lines.next()) |line| {
+ var words = std.mem.tokenizeScalar(u8, line, ' ');
+ const what = words.next() orelse continue;
+ if (std.mem.eql(u8, what, "replicas")) {
+ replicas = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch -1;
+ if (replicas >= 0) ninep_io.setCloexec(replicas);
+ } else if (std.mem.eql(u8, what, "c")) {
+ const i = std.fmt.parseInt(u8, words.next() orelse "", 10) catch continue;
+ const fd = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch continue;
+ if (i >= max_conns) continue;
+ const k = std.meta.stringToEnum(Kind, words.next() orelse "");
+ const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0;
+ const detached = std.mem.eql(u8, words.next() orelse "", "1");
+ const semantics = std.fmt.parseInt(u64, words.next() orelse "", 16) catch 0;
+ const rest = std.mem.trim(u8, words.rest(), " ");
+ ninep_io.setCloexec(fd);
+ const c = &d.conns[i];
+ c.* = .{ .fd = fd, .kind = k, .pid = pid, .detached = detached, .semantics = semantics };
+ c.name_len = @min(rest.len, c.name.len);
+ @memcpy(c.name[0..c.name_len], rest[0..c.name_len]);
+ } else if (std.mem.eql(u8, what, "h")) {
+ const fd = std.fmt.parseInt(c_int, words.next() orelse "", 10) catch continue;
+ const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0;
+ const owner = std.fmt.parseInt(u8, words.next() orelse "", 10) catch 255;
+ ninep_io.setCloexec(fd);
+ for (&d.held) |*h| if (h.fd < 0) {
+ h.* = .{ .fd = fd, .pid = pid, .owner = if (owner < max_conns) owner else null };
+ break;
+ };
+ }
+ }
+ return replicas;
+ }
+
fn accept(d: *Daemon, listener: c_int) void {
const fd = libc.accept4(listener, null, null, libc.SOCK.CLOEXEC);
if (fd < 0) return;
@@ -946,16 +1074,23 @@ fn listenReplicas() c_int {
return -1;
}
+/// Room for an upgrade's registry on a command line (`reexec`): every
+/// connection and master, each a line.
+const flag_room = 64 * 1024;
+
/// `pardes --daemon`: what `spawn` started, on the listener (fd 3) and the
-/// lock (fd 4). Returns only to exit, idle.
-pub fn serve() noreturn {
+/// lock (fd 4), or (`state`) an upgrade of one, its registry. Returns only
+/// to exit, idle.
+pub fn serve(state: ?[]const u8) noreturn {
const listener: c_int = 3;
ninep_io.setCloexec(3);
ninep_io.setCloexec(4);
const ignore: posix.Sigaction = .{ .handler = .{ .handler = posix.SIG.IGN }, .mask = posix.sigemptyset(), .flags = 0 };
for ([_]posix.SIG{ .PIPE, .HUP, .INT }) |sig| posix.sigaction(sig, &ignore, null);
- const replicas = listenReplicas();
+ upgrade.remember(&.{});
var d: Daemon = .{};
+ const given = if (state) |text| d.restore(text) else -1;
+ const replicas = if (given >= 0) given else listenReplicas();
const slots = 2 + max_conns + max_replicas + max_held;
var fds: [slots]libc.pollfd = undefined;
var conn_of: [slots]u8 = undefined;
@@ -1025,6 +1160,10 @@ pub fn serve() noreturn {
if (pfd.revents == 0 or d.held[i].fd != pfd.fd) continue;
d.drop(&d.held[i]);
}
+ if (d.upgrade_asked) {
+ d.upgrade_asked = false;
+ d.reexec(replicas);
+ }
}
var sock_buf: [sun_path_len:0]u8 = undefined;
_ = libc.close(listener);