summaryrefslogtreecommitdiff
path: root/runtime/daemon.zig
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/daemon.zig')
-rw-r--r--runtime/daemon.zig54
1 files changed, 34 insertions, 20 deletions
diff --git a/runtime/daemon.zig b/runtime/daemon.zig
index 9b566e60..1afdd878 100644
--- a/runtime/daemon.zig
+++ b/runtime/daemon.zig
@@ -279,9 +279,9 @@ fn spawn(listener: c_int, lock: c_int) bool {
/// Says who this session is. Its answer is not waited for: a daemon just
/// started is still being exec'd, and no startup waits on it. The reader
/// (`watch`) takes the `ok`, and only then is the session kept (`kept`).
-fn hello(fd: c_int, k: Kind, pid: libc.pid_t, named: []const u8, detached: bool) bool {
+fn hello(fd: c_int, k: Kind, pid: libc.pid_t, named: []const u8, s: State) bool {
var buf: [256]u8 = undefined;
- const words = std.fmt.bufPrint(&buf, "hello {d} {t} {d} {s} {x} {s}", .{ version, k, pid, if (detached) "detached" else "attached", wire.semantics, named }) catch return false;
+ const words = std.fmt.bufPrint(&buf, "hello {d} {t} {d} {t} {x} {s}", .{ version, k, pid, s, wire.semantics, named }) catch return false;
return send(fd, words, &.{});
}
@@ -300,9 +300,23 @@ 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.
+/// What a session's hello says of it besides its kind and name: a frontend
+/// of its own (`attached`) or none (`detached`); a pair's (PARDES_PAIR),
+/// whether its last client's close ends it (`pair`) or it is `kept`
+/// (started --detach, or a Detach typed in a client since): `ps` says it.
+/// Numbered as the daemon's record said a bool before (`reexec`).
+pub const State = enum(u8) { attached, detached, pair, kept };
var kind: Kind = .tty;
-var detached_now = std.atomic.Value(bool).init(false);
+var state_now = std.atomic.Value(State).init(.detached);
+
+pub fn sessionState() State {
+ return state_now.load(.acquire);
+}
+
+/// A Detach typed in a client: a pair's session is kept from now on.
+pub fn keepSession() void {
+ if (state_now.cmpxchgStrong(.pair, .kept, .acq_rel, .acquire) == null) tell("state kept", &.{});
+}
/// What it is called (its 9P name).
var name_buf: [128]u8 = undefined;
var name_len: usize = 0;
@@ -347,14 +361,14 @@ fn setName(named: []const u8) void {
/// Registers this session on `fd` (connected already, as a terminal's
/// stub does before it forks), or on a new connection when `fd` is null.
-pub fn register(fd: ?c_int, k: Kind, named: []const u8, detached: bool) bool {
+pub fn register(fd: ?c_int, k: Kind, named: []const u8, s: State) bool {
if (comptime !supported) return false;
setName(named);
kind = k;
- detached_now.store(detached, .release);
+ state_now.store(s, .release);
const c = fd orelse connect(true) orelse return false;
startPending(true);
- if (!hello(c, k, libc.getpid(), name(), detached)) {
+ if (!hello(c, k, libc.getpid(), name(), s)) {
_ = libc.close(c);
return false;
}
@@ -386,7 +400,7 @@ 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);
+ state_now.store(.detached, .release);
tell("state detached", &.{});
}
@@ -429,14 +443,14 @@ pub fn upgraded() void {
/// 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 {
+pub fn resumeLink(fd: c_int, stub_fd: c_int, k: Kind, named: []const u8, s: State) void {
if (comptime !supported) return;
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);
+ state_now.store(s, .release);
+ if (fd >= 0 and hello(fd, k, libc.getpid(), name(), s)) link = fd else if (fd >= 0) _ = libc.close(fd);
}
pub const Note = union(enum) {
@@ -518,7 +532,7 @@ fn redial(fd: c_int) bool {
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));
+ hello(fd, kind, libc.getpid(), name(), sessionState());
}
/// `bytes` sent whole on a socket, never raising SIGPIPE: false when the
@@ -778,7 +792,7 @@ const Conn = struct {
fd: c_int = -1,
kind: ?Kind = null,
pid: libc.pid_t = 0,
- detached: bool = false,
+ state: State = .attached,
/// Its build's wire.semantics: a replica's must be the same.
semantics: u64 = 0,
/// `pardes --kill` asked its end: its shells go with it, not kept.
@@ -871,7 +885,7 @@ const Daemon = struct {
var w: std.Io.Writer = .fixed(&line);
w.print("daemon {d}\n", .{libc.getpid()}) catch unreachable; // a pid fits msg_max
for (&d.conns, 0..) |*s, k| if (s.fd >= 0 and s.kind != null) {
- w.print("session {d} {t} {s} {s}\n", .{ s.pid, s.kind.?, if (s.detached) "detached" else "attached", s.named() }) catch break;
+ w.print("session {d} {t} {t} {s}\n", .{ s.pid, s.kind.?, s.state, s.named() }) catch break;
for (s.clients[0..s.nclients]) |pid| w.print("client {d}\n", .{pid}) catch break;
for (d.held) |h| if (h.fd >= 0 and h.owner == @as(u8, @intCast(k))) w.print("shell {d}\n", .{h.pid}) catch break;
};
@@ -896,7 +910,7 @@ const Daemon = struct {
const v = std.fmt.parseInt(u32, words.next() orelse "", 10) catch 0;
const k = std.meta.stringToEnum(Kind, words.next() orelse "");
const pid = std.fmt.parseInt(libc.pid_t, words.next() orelse "", 10) catch 0;
- const state = words.next() orelse "";
+ const said = words.next() orelse "";
const semantics = std.fmt.parseInt(u64, words.next() orelse "", 16) catch 0;
const rest = std.mem.trim(u8, words.rest(), " ");
if (v != version or k == null or pid <= 0) {
@@ -906,14 +920,14 @@ const Daemon = struct {
}
c.kind = k;
c.pid = pid;
- c.detached = std.mem.eql(u8, state, "detached");
+ c.state = std.meta.stringToEnum(State, said) orelse .attached;
c.semantics = semantics;
c.nclients = 0;
c.name_len = @min(rest.len, c.name.len);
@memcpy(c.name[0..c.name_len], rest[0..c.name_len]);
return d.reply(i, "ok", &.{});
},
- .state => c.detached = std.mem.eql(u8, words.next() orelse "", "detached"),
+ .state => c.state = std.meta.stringToEnum(State, words.next() orelse "") orelse .attached,
.clients => {
c.nclients = 0;
while (words.next()) |w| if (c.nclients < max_clients) {
@@ -1026,7 +1040,7 @@ const Daemon = struct {
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;
+ w.print("c {d} {d} {s} {d} {d} {x} {s}\n", .{ i, c.fd, k, c.pid, @intFromEnum(c.state), 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;
@@ -1076,12 +1090,12 @@ const Daemon = struct {
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 s = std.enums.fromInt(State, std.fmt.parseInt(u8, words.next() orelse "", 10) catch 0) orelse .attached;
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.* = .{ .fd = fd, .kind = k, .pid = pid, .state = s, .semantics = semantics };
c.nclients = 0;
c.name_len = @min(rest.len, c.name.len);
@memcpy(c.name[0..c.name_len], rest[0..c.name_len]);