Next: , Previous: , Up: posix   [Index]

4.32 Turn the process into a daemon

The library (vicare posix daemonisations) implements facilities that turn a running process into a daemon; it is build on top of (vicare posix). A daemon process is a process that runs in the background with no controlling process, usually to provide networking service.

Process daemonisation is usually performed in the early phase of a process start up, before the any actual work is performed. The daemonisation involves forking a child process and exiting the parent process; this changes the ID of the surviving process.

The following bindings are exported by the library (vicare posix daemonisations).

Function: daemonise

Turn the current process into a daemon. If successful: return a fixnum representing the new process group ID, else raise an exception. Follow this procedure:

  1. Check the ID of the parent process: if it is 1 assume this process is already a daemon and return doing nothing else. 1 is the ID of the init process. getppid.
  2. Block all interprocess signals using signal-bub-init from (vicare posix). signal-bub-init.
  3. Fork the process using fork from (vicare posix); exit the parent process with status code 0. The daemon process is the child. fork.
  4. Change the current working directory to root. chdir.
  5. Set the current umask to zero. umask.
  6. Open a new file descriptor reading from and writing to the /dev/null device; close the file descriptors 0, 1 and 2 replacing them with the new file descriptor.
  7. Detach the process from the controlling terminal and become session leader; this is done with a call to setsid from (vicare posix). setsid.
  8. Unblock all interprocess signals using signal-bub-final from (vicare posix). signal-bub-final.

Next: , Previous: , Up: posix   [Index]