Something went wrong. Try again.
Start a program in a session of its own without forking the caller (posix_spawn SETSID)
Something went wrong. Try again.
Files
- OCaml 86%
- C 14%
- Raku <1%
README.md
# daemon
Start a program in a session of its own, without forking the caller.
`Daemon.spawn` starts an executable as the leader of a new session and process
group with no controlling terminal, so it survives the caller's exit, the
caller's process group being signalled, and the caller's terminal going away.
The child comes from `posix_spawn(3)` with `POSIX_SPAWN_SETSID`, or from
`vfork(2)` and `execve(2)` where that call cannot keep the caller's other
descriptors out of the child, so the caller is never duplicated. A domain is
OCaml 5's unit of parallelism: a system thread with its own minor heap that
runs OCaml code in parallel with the others. `fork(2)` copies only the calling
thread, so a child forked while another domain runs could inherit the heap or
a lock in the middle of that domain's update; OCaml 5's `Unix.fork` therefore
fails for the rest of a process's life once any domain has been spawned, and
duplicating a multi-threaded runtime is a hazard where it is still allowed.
Requires OCaml >= 5.1. Supported on macOS and on Linux, the platforms which can
keep the caller's other descriptors out of the child.
## Security
- The child receives the three descriptors it is given as its standard input,
output and error, plus explicitly declared `extra_fds`, and no others, whatever the
close-on-exec state of the caller's descriptors and whatever another domain
opens while the call runs. Each mechanism settles the set to close in the
child, whose descriptor table is the copy taken when it was made:
`POSIX_SPAWN_CLOEXEC_DEFAULT` on macOS,
`posix_spawn_file_actions_addclosefrom_np` on glibc 2.34 and later, and
`close_range(2)` between a `vfork(2)` and the `execve(2)` on any other Linux
libc, where `posix_spawn` has no file action which closes a range and no hook
for one. A platform with none of the three fails to build rather than spawn
something less confined.
- Signal dispositions are reset to default and the signal mask emptied, so an
ignored or blocked signal in the caller does not silen…