13.7 Summary
Daemons are processes that run in the background
independent of control from all terminals. Many network servers run
as daemons. All output from a daemon is normally sent to the
syslogd daemon by calling the syslog function.
The administrator then has complete control over what happens to
these messages, based on the daemon that sent the message and the
severity of the message.
To start an arbitrary program and have it run as
a daemon requires a few steps: Call fork to run in the
background, call setsid to create a new POSIX session and
become the session leader, fork again to avoid obtaining a
new controlling terminal, change the working directory and the file
mode creation mask, and close all unneeded files. Our
daemon_init function handles all these details.
Many Unix servers are started by the
inetd daemon. It handles all the required daemonization
steps, and when the actual server is started, the socket is open on
standard input, standard output, and standard error. This lets us
omit calls to socket, bind, listen, and
accept, since all these steps are handled by
inetd.
|