diff options
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/daemon.zig | 124 | ||||
| -rw-r--r-- | runtime/session.zig | 6 |
2 files changed, 91 insertions, 39 deletions
diff --git a/runtime/daemon.zig b/runtime/daemon.zig index 1b9149b6..80e1cc09 100644 --- a/runtime/daemon.zig +++ b/runtime/daemon.zig @@ -12,7 +12,8 @@ //! - whether a terminal or a window shows it (`state`). //! It lists the sessions (`pardes ps`), hands a crashed session's masters //! to the one that asks for them (Adopt), and a client's connection to the -//! session it names (below). +//! session it names (below). A session whose daemon died dials the next +//! one and registers again (`readNotes`). //! //! Messages are SOCK_SEQPACKET datagrams: a line of words, and the fds it //! carries (SCM_RIGHTS). @@ -290,9 +291,17 @@ fn hello(fd: c_int, k: Kind, pid: libc.pid_t, named: []const u8, detached: bool) /// tell of their ptys; -1 with none (no daemon, or PARDES_DAEMON=off). pub var link: c_int = -1; /// The daemon said `ok` to the hello, and the link has not ended since; -/// `gone` once it has ended. +/// `gone` once it has ended for good (`readNotes`). var up = std.atomic.Value(bool).init(false); var gone = std.atomic.Value(bool).init(false); +/// The session is ending (`quit`): its link's end is no daemon's death. +var quitting = std.atomic.Value(bool).init(false); +/// A new daemon took the hello since the last one died: the rest of what +/// the dead one was told is to be told again (`relinked`). +var retell = std.atomic.Value(bool).init(false); +/// What the hello says besides the name: the kind, and whether detached. +var kind: Kind = .tty; +var detached_now = std.atomic.Value(bool).init(false); /// What it is called (its 9P name). var name_buf: [128]u8 = undefined; var name_len: usize = 0; @@ -328,6 +337,8 @@ fn setName(named: []const u8) void { pub fn register(fd: ?c_int, k: Kind, named: []const u8, detached: bool) bool { if (comptime !supported) return false; setName(named); + kind = k; + detached_now.store(detached, .release); const c = fd orelse connect(true) orelse return false; startPending(true); if (!hello(c, k, libc.getpid(), name(), detached)) { @@ -362,19 +373,33 @@ pub fn closed(pid: libc.pid_t) void { /// Its own frontend let go (Detach, its terminal's end): detached for /// good, an attach after it a client. pub fn setDetached() void { + detached_now.store(true, .release); tell("state detached", &.{}); } +/// The last `clients` line, kept to be told to a new daemon (`relinked`). +var clients_line: ["clients".len + max_clients * " -2147483648".len]u8 = undefined; +var clients_len: usize = 0; + /// This session's replica clients now, each its process's pid (0 when its /// socket's peer is not known), for `--ps`. pub fn clients(pids: []const libc.pid_t) void { - var buf: ["clients".len + max_clients * " -2147483648".len]u8 = undefined; - var w: std.Io.Writer = .fixed(&buf); - w.writeAll("clients") catch unreachable; // a literal, its .len in buf's - for (pids[0..@min(pids.len, max_clients)]) |pid| w.print(" {d}", .{pid}) catch unreachable; // max_clients i32s, in buf's type + var w: std.Io.Writer = .fixed(&clients_line); + w.writeAll("clients") catch unreachable; // a literal, its .len in clients_line's + for (pids[0..@min(pids.len, max_clients)]) |pid| w.print(" {d}", .{pid}) catch unreachable; // max_clients i32s, in clients_line's type + clients_len = w.end; tell(w.buffered(), &.{}); } +/// Whether a new daemon took this session's hello since the last call +/// (the session loop's, each pass): its clients told it again here, its +/// ptys the caller's to tell (`pty`). +pub fn relinked() bool { + if (!retell.swap(false, .acq_rel)) return false; + if (clients_len > 0) tell(clients_line[0..clients_len], &.{}); + return true; +} + /// Asks for the masters of sessions that died holding them; they come back /// as an `adopted` note. pub fn adopt() void { @@ -396,10 +421,15 @@ pub fn resumeLink(fd: c_int, stub_fd: c_int, k: Kind, named: []const u8, detache stub = stub_fd; if (stub >= 0) hupOnStubDeath(true); setName(named); + kind = k; + detached_now.store(detached, .release); 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 new daemon took the hello (`readNotes`): the session loop is woken + /// to tell it the rest (`relinked`). + relinked, adopted: struct { pids: [max_fds]libc.pid_t, fds: [max_fds]c_int, n: usize }, /// A replica's connection, its hello taken (core/replica.zig `Peers`), /// and the view it joins again for, if it does. @@ -417,39 +447,58 @@ pub fn watch(ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, note: Note) v fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, note: Note) void) void { var buf: [msg_max]u8 = undefined; - // The end of the link (a daemon gone, a hello refused): not kept. - defer { - up.store(false, .release); - gone.store(true, .release); - } - while (recv(fd, &buf)) |got| { - var words = std.mem.tokenizeScalar(u8, got.words, ' '); - const verb = words.next() orelse ""; - if (std.meta.stringToEnum(enum { ok, replica, adopted }, verb)) |v| switch (v) { - .ok => up.store(true, .release), - .replica => { - const view = std.fmt.parseInt(u8, words.next() orelse "", 10) catch null; - if (got.nfds >= 1) deliver(ctx, .{ .replica = .{ .fd = got.fds[0], .view = view } }); - for (got.fds[@min(got.nfds, 1)..got.nfds]) |f| _ = libc.close(f); - }, - .adopted => { - var note: Note = .{ .adopted = .{ .pids = undefined, .fds = undefined, .n = 0 } }; - const a = ¬e.adopted; - for (got.fds[0..got.nfds]) |f| { - const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch { - _ = libc.close(f); - continue; - }; - a.pids[a.n] = pid; - a.fds[a.n] = f; - a.n += 1; - } - deliver(ctx, note); - }, - } else for (got.fds[0..got.nfds]) |f| { - _ = libc.close(f); + while (true) { + while (recv(fd, &buf)) |got| { + var words = std.mem.tokenizeScalar(u8, got.words, ' '); + const verb = words.next() orelse ""; + if (std.meta.stringToEnum(enum { ok, replica, adopted }, verb)) |v| switch (v) { + .ok => up.store(true, .release), + .replica => { + const view = std.fmt.parseInt(u8, words.next() orelse "", 10) catch null; + if (got.nfds >= 1) deliver(ctx, .{ .replica = .{ .fd = got.fds[0], .view = view } }); + for (got.fds[@min(got.nfds, 1)..got.nfds]) |f| _ = libc.close(f); + }, + .adopted => { + var note: Note = .{ .adopted = .{ .pids = undefined, .fds = undefined, .n = 0 } }; + const a = ¬e.adopted; + for (got.fds[0..got.nfds]) |f| { + const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch { + _ = libc.close(f); + continue; + }; + a.pids[a.n] = pid; + a.fds[a.n] = f; + a.n += 1; + } + deliver(ctx, note); + }, + } else for (got.fds[0..got.nfds]) |f| { + _ = libc.close(f); + } } + // The end of the link. One whose daemon took the hello (it died, or + // let the session go) is dialled again, a daemon started if none + // answers, and the hello said, here, off the session's way: the + // link's end is what wakes this, never a timer. One that never took + // it (another build's daemon), or the session's own end, is not: + // an end begun while it dialled finds the new link (`quit`). + if (!up.swap(false, .acq_rel) or quitting.load(.acquire) or !redial(fd) or quitting.load(.acquire)) break; + retell.store(true, .release); + deliver(ctx, .relinked); } + // Not kept, until the session's end. + gone.store(true, .release); +} + +/// A new connection in the place of `fd`, the link (its number kept, so +/// the session's own sends never race a new fd), and the hello said on it. +fn redial(fd: c_int) bool { + if (comptime !supported) return false; + const c = connect(true) orelse return false; + startPending(true); + defer _ = libc.close(c); + return libc.dup3(c, fd, @bitCast(@as(u32, @bitCast(libc.O{ .CLOEXEC = true })))) >= 0 and + hello(fd, kind, libc.getpid(), name(), detached_now.load(.acquire)); } /// `bytes` sent whole on a socket, never raising SIGPIPE: false when the @@ -485,6 +534,7 @@ pub fn hangUp() void { /// link closed, the reader joined. pub fn quit() void { if (link < 0) return; + quitting.store(true, .release); tell("quit", &.{}); _ = libc.shutdown(link, 2); if (reader) |r| r.join(); diff --git a/runtime/session.zig b/runtime/session.zig index 55b42b60..4172ba8d 100644 --- a/runtime/session.zig +++ b/runtime/session.zig @@ -24,7 +24,7 @@ //! - `sent()`: after each step's unit went to the clients. //! - `upgradeAsked()`: an Upgrade asked in the step, done (it execs). //! - `note(n)`: what the daemon says (an adopted shell, a client's -//! connection), onto its queue. +//! connection, a new daemon's hello taken), onto its queue. //! - `watched()`: the files it watches (a Restore stops watching them). //! - `restoring(nc)`, `restored(l)`: a Restore's new core, before it //! replaces the old one (its devices let go, what it is told of the @@ -139,8 +139,10 @@ pub fn Loop(comptime Frontend: type) type { } /// What the daemon says, onto the frontend's queue (once: its - /// reader is started by the first call). + /// reader is started by the first call); and a daemon started + /// since the last one died told the shells' masters it held. pub fn watch(l: *L) void { + if (daemon.relinked()) l.ptys.tellDaemon(); daemon.watch(l.fe, note); } |
