Daemon Processes, Syslog Facility & inetd Superservers
Idea in plain words
A daemon is a background process with no controlling terminal, typically running continuously to provide system services.
Exam tip
High-yield exam topic (High).
Explanation
Server software running in enterprise environments must dissociate from user sessions so that terminal logout does not trigger SIGHUP and terminate the service. The daemon_init sequence detaches the process, guarantees it is not a session leader, and establishes independent process groups. Because daemons lack a console, POSIX provides the syslog interface. The inetd (and modern systemd) superserver architecture prevents hundreds of rarely-used services from idling in memory, instead maintaining listening sockets on their behalf and executing the actual server binary via exec() upon connection arrival.
Key points
- Daemonizing steps: fork() and exit parent, setsid() to create a new session, change working directory to root '/', umask(0), and redirect stdin/stdout/stderr to /dev/null.
- Syslog facility: Daemons report errors using openlog(), syslog(LOG_ERR, ...), and closelog(), routing messages to /var/log/syslog via syslogd.
- inetd superserver: A single master daemon that listens on multiple ports listed in /etc/inetd.conf and forks the appropriate service daemon on demand.
- Advantages of inetd: Conserves system memory and process table slots by launching servers only when connection requests arrive.
Example
Example
Sending a log message from a network daemon: openlog('my_daemon', LOG_PID, LOG_DAEMON); syslog(LOG_NOTICE, 'Server started on port %d', 8080); closelog();
Code examples
void daemon_init(void) {
pid_t pid;
if ((pid = fork()) < 0) exit(1);
else if (pid != 0) exit(0); // Terminate parent
setsid(); // Become session leader
chdir("/"); // Prevent unmounting directory
umask(0);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
open("/dev/null", O_RDWR); // fd 0
dup(0); // fd 1
dup(0); // fd 2
}
Exam questions
- [8 Marks] Explain the step-by-step procedure to daemonize a network process under Unix/Linux with an annotated code implementation.
- [5 Marks] Discuss the working mechanism and advantages of the inetd superserver with reference to /etc/inetd.conf.