From 791d0447aa2bdd4fe4ae0c76052e09acca4c61f2 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 14:27:42 -0300 Subject: daemon.zig: a forked session takes its stub's death, a SIGKILL too, as the terminal's HUP and detaches PR_SET_PDEATHSIG (FreeBSD: PROC_PDEATHSIG_CTL) in the session child, cleared again at Detach so the stub's own exit then is no hangup; a stub dead before it was set hangs up at once. It is the HUP the signal thread already takes (blocked in every thread, sigwaited), and setsid does not touch it. An --attach stub is no parent of its session: one killed so still leaves the session drawing until the terminal closes or Detach. test/daemon.py kills a session's stub outright under a shell that keeps the terminal (on FreeBSD the session leader's exit revokes it), waits for the session to put the terminal back and detach, and attaches it again; without the death signal it times out. Co-Authored-By: Claude Opus 5.5 --- src/daemon.zig | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/daemon.zig b/src/daemon.zig index c7729e59..0f770514 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -511,9 +511,25 @@ pub fn stubWait(session: libc.pid_t, child: bool, pipe: c_int) noreturn { /// The session's half of its stub: what `stubWait` reads. pub var stub: c_int = -1; -/// Detached: the stub is told (and goes), and the pipe let go. +extern "c" fn procctl(idtype: c_int, id: i64, cmd: c_int, data: ?*anyopaque) c_int; + +/// A HUP when this process's parent dies (a forked session's stub), or no +/// longer. Linux keeps it per thread: set and cleared on the main one. +fn hupOnStubDeath(on: bool) void { + var sig: c_int = if (on) @backingInt(posix.SIG.HUP) else 0; + switch (builtin.os.tag) { + .linux => _ = std.os.linux.prctl(@backingInt(std.os.linux.PR.SET_PDEATHSIG), @intCast(sig), 0, 0, 0), + // procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &sig) + .freebsd => _ = procctl(0, 0, 11, &sig), + else => {}, + } +} + +/// Detached: the stub is told (and goes), and the pipe let go. Its end is +/// no hangup now (hupOnStubDeath). pub fn tellStubDetached() void { if (stub < 0) return; + hupOnStubDeath(false); var buf: [160]u8 = undefined; const said = std.fmt.bufPrint(&buf, "d{s}", .{name()}) catch "d"; _ = libc.write(stub, said.ptr, said.len); @@ -549,6 +565,7 @@ pub fn forkSession(conn: c_int) ?void { if (libc.pipe(&pipe) != 0) return null; ninep_io.setCloexec(pipe[0]); ninep_io.setCloexec(pipe[1]); + const parent = libc.getpid(); const pid = libc.fork(); if (pid < 0) { _ = libc.close(pipe[0]); @@ -558,6 +575,11 @@ pub fn forkSession(conn: c_int) ?void { if (pid == 0) { _ = libc.close(pipe[0]); _ = libc.setsid(); + // The stub's death, however it comes (a SIGKILL too), is a HUP: + // the terminal's end, which the session's signal thread takes and + // detaches on. One already dead hangs up at once. + hupOnStubDeath(true); + if (libc.getppid() != parent) _ = libc.raise(.HUP); stub = pipe[1]; // The listener and lock are the daemon's, which the stub starts. startPending(false); -- cgit v1.3