diff options
Diffstat (limited to 'src/daemon.zig')
| -rw-r--r-- | src/daemon.zig | 24 |
1 files changed, 23 insertions, 1 deletions
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); |
