Standard C Library Functions daemon(3C) NAME daemon - basic daemonization function SYNOPSIS #include int daemon(int nochdir, int noclose); DESCRIPTION The daemon() function provides a way for applications to go into background. The function will ensure 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 options to the function are treated as boolean variables and are evaluated using negative logic. If the nochdir option is other than zero the working directory will not be changed to the root directory ("/"), otherwise it will be. If the noclose option is other than zero the descriptors 0,1,2 (normally corresponding to standard input, output and error output, depending on the application) will not be redirected to /dev/null, otherwise they will be. RETURN VALUES Upon successful completion, daemon() returns 0. Otherwise it returns -1 in which case errno will be set to the values specified in fork(2) and setsid(2). EXAMPLES 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. */ ATTRIBUTES See attributes(5) for descriptions of the following attri- butes: ____________________________________________________________ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | |_____________________________|_____________________________| | Interface Stability | Committed | |_____________________________|_____________________________| | MT-Level | Async-Signal-Safe | |_____________________________|_____________________________| SEE ALSO attributes(5), fork(2), Intro(2), setsid(2)