daemon - basic daemonization function
#include <stdlib.h> int daemon(int nochdir, int noclose);
The daemon() function provides a means for applications to run in the background.
This function ensures that the process calling this function:
runs in the background
detaches from the controlling terminal
forms a new process group
is not a session group leader.
The arguments to this function are treated as boolean variables and are evaluated using negative logic.
If the nochdir argument is zero the working directory will be changed to the root directory (/); otherwise it will not be.
If the noclose argument is zero the descriptors 0, 1, and 2 (normally corresponding to standard input, output and error output, depending on the application) will be redirected to /dev/null; otherwise they will not be.
If daemon() is called with noclose set to 0 and fails to redirect descriptors 0, 1, and 2 to /dev/null, those descriptors are not guaranteed to be the same as before the call.
The main() function of a network server could look like this:
int background; /* background flag */ /* Load and verify the configuration. */ /* Go into background. */ if (background && daemon(0, 0) < 0) err(1, "daemon"); /* Process requests here. */
See attributes(7) for descriptions of the following attributes: