Use daemon(3) to daemonise rather than writing our own code. Since we
use the mode of daemon(3) which closes the standard 3 file descriptors,
if the user requests logging to one of those we dup(2) the original
first. We must also take care to always close any newly-opened file
descriptors before execve(2) of the child.
Add a feature which allows arbitrary checks to be performed whenever the child
process is about to be respawned. Currently implemented checks allow running of
a command (via system(3)) or testing the existence of a file.
This was developed to be used to stop respawns of a daemon that was started in
response to a udev hotplug event, but it is generally applicable to many other
scenarios.
exit(3) is not async-signal safe (presumably due to atexit(3) processing),
so use _exit(2) instead. This was clearly an omission from the original
code which even had a comment to the effect that _exit(2) needed to be
used. In all likelihood this never caused any problems as atexit(3)
functionality is not used.
Treat SIGINT similarly to SIGTERM; exit when it is received. This gives the
desired behaviour of having a foreground daemonitor be stopped by pressing
ctrl-C on its controlling tty.