summaryrefslogtreecommitdiff
path: root/runtime/daemon.zig
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/daemon.zig')
-rw-r--r--runtime/daemon.zig105
1 files changed, 91 insertions, 14 deletions
diff --git a/runtime/daemon.zig b/runtime/daemon.zig
index 80e1cc09..9b566e60 100644
--- a/runtime/daemon.zig
+++ b/runtime/daemon.zig
@@ -290,10 +290,11 @@ fn hello(fd: c_int, k: Kind, pid: libc.pid_t, named: []const u8, detached: bool)
/// This session's registration (`register`), which forkShell and the hosts
/// 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 for good (`readNotes`).
-var up = std.atomic.Value(bool).init(false);
-var gone = std.atomic.Value(bool).init(false);
+/// The hello's answer, as the reader took it (`readNotes`): `up`, the
+/// daemon said `ok` and the link has not ended since; `gone`, it has ended
+/// for good; `asked` until either (and while a new daemon is dialled).
+const Answer = enum(u32) { asked, up, gone };
+var answer = std.atomic.Value(Answer).init(.asked);
/// 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
@@ -306,6 +307,8 @@ var detached_now = std.atomic.Value(bool).init(false);
var name_buf: [128]u8 = undefined;
var name_len: usize = 0;
var reader: ?std.Thread = null;
+/// Where the reader hands each note (`notesTo`).
+var notes: ?struct { ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, note: Note) void } = null;
pub fn name() []const u8 {
return name_buf[0..name_len];
@@ -314,12 +317,22 @@ pub fn name() []const u8 {
/// Whether a daemon keeps this session: what Detach, and a terminal's end,
/// need, or the session would be left where nothing can attach to it. A
/// daemon just started answers the hello once it runs, so this waits for
-/// that answer (up to 2 s): never on a startup's path, only on those two.
+/// that answer, or the link's end, whenever they come: never on a startup's
+/// path, only on those two. The reader takes it: started here if the first
+/// frame has not started it yet.
pub fn kept() bool {
if (link < 0) return false;
- var waited: u32 = 0;
- while (!up.load(.acquire) and !gone.load(.acquire) and waited < 2000) : (waited += 1) sleepMs(1);
- return up.load(.acquire);
+ watch();
+ if (reader == null) return false;
+ const io = std.Io.Threaded.global_single_threaded.io();
+ while (answer.load(.acquire) == .asked) io.futexWaitUncancelable(Answer, &answer.raw, .asked);
+ return answer.load(.acquire) == .up;
+}
+
+/// The reader's word on the hello's answer, to `kept` waiting on it.
+fn settle(a: Answer) void {
+ answer.store(a, .release);
+ std.Io.Threaded.global_single_threaded.io().futexWake(Answer, &answer.raw, 1);
}
/// The name a session goes by: its 9P socket's (`--9p=`, `--detach=`), else
@@ -438,11 +451,18 @@ pub const Note = union(enum) {
pub const Joining = struct { fd: c_int, view: ?u8 = null };
-/// Hands every note the daemon sends on to `deliver`, on a thread of its
+/// Where every note the daemon sends goes, once `watch` has started the
+/// reader: the session's, before its first step.
+pub fn notesTo(ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, note: Note) void) void {
+ notes = .{ .ctx = ctx, .deliver = deliver };
+}
+
+/// Hands every note the daemon sends on (`notesTo`), on a thread of its
/// own, until the link ends (`quit`). Called each loop: started once.
-pub fn watch(ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, note: Note) void) void {
+pub fn watch() void {
+ const n = notes orelse return;
if (link < 0 or reader != null) return;
- reader = std.Thread.spawn(.{}, readNotes, .{ link, ctx, deliver }) catch null;
+ reader = std.Thread.spawn(.{}, readNotes, .{ link, n.ctx, n.deliver }) catch null;
}
fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, note: Note) void) void {
@@ -452,7 +472,7 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no
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),
+ .ok => settle(.up),
.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 } });
@@ -482,12 +502,12 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no
// 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;
+ if (answer.swap(.asked, .acq_rel) != .up 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);
+ settle(.gone);
}
/// A new connection in the place of `fd`, the link (its number kept, so
@@ -600,6 +620,12 @@ fn stubWait(session: libc.pid_t, pipe: c_int) noreturn {
const said = buf[0..len];
const out = std.Io.File.stderr();
const io = std.Io.Threaded.global_single_threaded.io();
+ // `new --detach`'s session runs (`launched`): its name, for a script.
+ if (said.len > 0 and said[0] == 'r') {
+ var text: [520]u8 = undefined;
+ std.Io.File.stdout().writeStreamingAll(io, std.fmt.bufPrint(&text, "{s}\n", .{said[1..]}) catch said[1..]) catch {};
+ libc._exit(0);
+ }
if (said.len > 0 and said[0] == 'd') {
var text: [700]u8 = undefined;
const who = said[1..];
@@ -695,6 +721,57 @@ pub fn forkSession(conn: c_int) ?void {
stubWait(pid, pipe[0]);
}
+/// `pardes new --detach`: the session forked off in a session of its own,
+/// its stdin /dev/null, and this process its stub until the session runs
+/// (`launched`): it then prints the name and exits 0, or with the
+/// session's status if it ended first. Returns in the session, called
+/// `named` (else by its pid); when no fork can be made, here, which is
+/// then the session.
+pub fn launch(named: []const u8) void {
+ var pipe: [2]c_int = undefined;
+ if (libc.pipe(&pipe) != 0) return;
+ ninep_io.setCloexec(pipe[0]);
+ ninep_io.setCloexec(pipe[1]);
+ const pid = libc.fork();
+ if (pid < 0) {
+ _ = libc.close(pipe[0]);
+ _ = libc.close(pipe[1]);
+ return;
+ }
+ if (pid == 0) {
+ _ = libc.close(pipe[0]);
+ _ = libc.setsid();
+ const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR, .CLOEXEC = true }, @as(libc.mode_t, 0));
+ if (null_fd >= 0) _ = libc.dup2(null_fd, 0);
+ if (null_fd > 0) _ = libc.close(null_fd);
+ stub = pipe[1];
+ launching = true;
+ setName(named);
+ return;
+ }
+ _ = libc.close(pipe[1]);
+ stubWait(pid, pipe[0]);
+}
+
+/// Forked by `launch`, and its stub not told yet.
+var launching = false;
+
+/// The session of a `new --detach` runs: registered (the daemon took its
+/// hello, when there is one) and listening. Its stub told, with its name;
+/// its stdout and stderr, still the launch's until now, let go.
+pub fn launched() void {
+ if (!launching) return;
+ launching = false;
+ _ = kept();
+ tellStub(stub, "r{s}", .{name()});
+ stub = -1;
+ const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR, .CLOEXEC = true }, @as(libc.mode_t, 0));
+ if (null_fd < 0) return;
+ _ = libc.dup2(null_fd, 1);
+ _ = libc.dup2(null_fd, 2);
+ if (null_fd > 2) _ = libc.close(null_fd);
+}
+
// ---- the daemon ----
const Conn = struct {