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. COMMENTS Running daemon as a service If the process calling daemon() is running as a smf(5) service, it should call daemon() as late as possible in the process of initializing in order to ensure that the process returns on the brink of providing the service (e.g. waiting for requests) and to return as much information as possible about eventual failures during initialization. Other options for setting up daemon environment The daemonized process might want to use the following: - contracts - tasks - privileges - umask setting If running under smf(5), process contracts, tasks and credential context are accounted for by smf(5) so this sort of daemon generally does not need to explicitly set them (unless being e.g. login application). Contracts are useful for providing fault separation, e.g. placing worker process groups started by the master listener process (the process calling daemon()) into a different contract(4) prevents them from being killed if the master process goes away. The daemon can be placed into a new task owned by a specific project(4) for better workload management. After going into background privileged daemons might want to permanently revoke some of the privileges(5) which they will not need anymore and drop some of the effective privileges until they are needed for specific action. Changing umask(2) is a good practice for daemons creating new files to ensure they are created with expected permissions. Since the file creation mask umask is inherited by child processes it is suitable to set it after calling daemon() and before forking child processes. 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), contract(4), fork(2), Intro(2), privileges(5), process(4), project(4), setsid(2), smf(5), umask(2)