Unless the
Fa noclose
argument is non-zero,
daemon ();
will close the first three file descriptors and redirect them to
/dev/null
Normally, these correspond to standard input, standard output, and
standard error.
However, if any of those file descriptors refer to something else, they
will still be closed, resulting in incorrect behavior of the calling program.
This can happen if any of standard input, standard output, or standard
error have been closed before the program was run.
Programs using
daemon ();
should therefore either call
daemon ();
before opening any files or sockets, or verify that any file
descriptors obtained have values greater than 2.
The
daemon ();
function temporarily ignores
SIGHUP
while calling
setsid(2)
to prevent a parent session group leader's calls to
fork(2)
and then
_exit2
from prematurely terminating the child process.