diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-20 04:16:02 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-20 04:25:21 -0300 |
| commit | 2fadb864051f5afc062606a898d821b702a1692c (patch) | |
| tree | 36a5164d90e991ae30a330d48fe26828332b63dd /runtime/daemon.zig | |
| parent | 6e8735895d04c0073eb28cc1a2f431a2ba587e9d (diff) | |
| download | pardes-2fadb864051f5afc062606a898d821b702a1692c.tar.gz pardes-2fadb864051f5afc062606a898d821b702a1692c.zip | |
launch: `pardes new --detach[=NAME]` forks the session off and returns once it is registered and listening (the last-step design's Z1, 2.2 F's second half): exit 0 and the name on stdout, the session in a session of its own (setsid, stdin /dev/null, its stdout and stderr the launch's until it runs, so a failed start still says why there and its status is the launch's), by the stub's own wait with one word more (`r<name>`, daemon.zig `launch`/`launched`, told from the session loop after its frontend's start); the flag alone (`pardes --detach=NAME`, what every harness and a supervisor runs) is still the session itself; test/daemon.py `launch` (plain and `--launch`) failing first on the base (not returned after 15 s: the launch was the session), then 0.12-0.22 s; help new and docs/typ/command-line.txt say so
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'runtime/daemon.zig')
| -rw-r--r-- | runtime/daemon.zig | 57 |
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 { |
