summaryrefslogtreecommitdiff
path: root/runtime/daemon.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-20 08:36:06 -0300
committerGabriel Schneider <[email protected]>2026-11-20 08:36:06 -0300
commit446cde19a6b6fb7125fc749608cc12b7452dad6e (patch)
treea7c2792ecdb1833a2e5a3ef99b9fb6ce96c01a00 /runtime/daemon.zig
parent2c2800c93e5417297dd06d3e52d5d336d975560c (diff)
parentae3b9e9d19492c827f257da8a224a19d77de51c1 (diff)
downloadpardes-446cde19a6b6fb7125fc749608cc12b7452dad6e.tar.gz
pardes-446cde19a6b6fb7125fc749608cc12b7452dad6e.zip
integration: a session's first client over an inherited socket before its first effects, the pair under PARDES_PAIR, and a session's last word, on the 9P run's fixes and the build's second pass: pardes-first-client (new --detach forks and returns; Options.pair and the join by fd; pardes FILE as a client and a session under the variable; ended CODE and left NAME: a client's own Detach exits 0 with its line again) and pardes-merge33, merged without a textual conflict; tui core-test 858/1013 (155 skipped), shell-test 1053/1056, the window builds; the coordinator's gate in the ledger (reorg/merge34)
Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'runtime/daemon.zig')
-rw-r--r--runtime/daemon.zig57
1 files changed, 57 insertions, 0 deletions
diff --git a/runtime/daemon.zig b/runtime/daemon.zig
index 80e1cc09..a2e890f9 100644
--- a/runtime/daemon.zig
+++ b/runtime/daemon.zig
@@ -600,6 +600,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 +701,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 {