Title: Daemon Processes
1- Chapter 12.
- Daemon Processes
- and inetd Superserver
212.1 Introduction
- A daemon is a process that runs in the background
and is independent of control from all terminals. - There are numerous ways to start a daemon
- 1. the system initialization scripts (
/etc/rc ) - 2. the inetd superserver
- 3. cron deamon
- 4. the at command
- 5. from user terminals
- Since a daemon does not have a controlling
terminal, it needs some way to output message
when something happens, either normal
informational messages, or emergency messages
that need to be handled by an administrator.
312.2 syslogd daemon
- Berkeley-derived implementation of syslogd
perform the following actions upon startup. - 1. The configuration file is read, specifying
what to do with each type of log message that the
daemon can receive. - 2. A Unix domain socket is created and bound to
the pathname /var/run/log ( /dev/log on some
system). - 3. A UDP socket is created and bound to port 514
- 4. The pathname /dev/klog is opened. Any error
messages from within the kernel appear as input
on this device. - We could send log messages to the syslogd daemon
from our daemons by creating a Unix domain
datagram socket and sending our messages to the
pathname that the daemon has bound, but an easier
interface is the syslog function.
412. 3 syslog function
- the priority argument is a combination of a level
and a facility. - The message is like a format string to printf,
with the addition of a m specification, which is
replaced with the error message corresponding to
the current value of errno. - Ex) Syslog(LOG_INFOLOG_LOCAL2, rename(s, s)
m,file1,file2)
512. 3 syslog function
- Log message have a level between 0 and 7.
612. 3 syslog function
- A facility to identify the type of process
sending the message.
712. 3 syslog function
- Openlog and closelog
- openlog can be called before the first call to
syslog and closelog can be called when the
application is finished sending is finished log
messages.
812.4 daemon_init Function
- include "unp.h"
- include ltsyslog.hgt
- define MAXFD 64
- extern int daemon_proc / defined in error.c /
- void daemon_init(const char pname, int facility)
-
- int i
- pid_t pid
- if ( (pid Fork()) ! 0)
- exit(0) / parent terminates /
- / 1st child continues /
- setsid() / become session leader /
- Signal(SIGHUP, SIG_IGN)
- if ( (pid Fork()) ! 0) exit(0) / 1st child
terminates / - / 2nd child continues /
- daemon_proc 1 / for our err_XXX() functions
/ - chdir("/") / change working directory /
912.5 inetd Daemon
- A typical Unix systems problems
- 1. All these daemons contained nearly identical
startup code. - 2. Each daemon took a slot in the process table,
but each daemon was asleep most of the time. - inetd daemon fixes the two problems.
- 1. It simplifies writing daemon processes, since
most of the startup details are handled by inetd. - 2. It allow a single process(inetd) to be waiting
for incoming client requests for multiple
services, instead of one process for each service.
1012.5 inetd daemon
1112.6 daemon_inetd Function
include "unp.h" include
ltsyslog.hgt extern int daemon_proc /
defined in error.c / void daemon_inetd(const
char pname, int facility) daemon_proc
1 / for our err_XXX()
functions / openlog(pname, LOG_PID,
facility)
1212.6 daemon_inetd Function
include "unp.h" include
lttime.hgt int main(int argc, char argv)
socklen_t len struct sockaddr
cliaddr char
buffMAXLINE time_t
ticks daemon_inetd(argv0, 0)
cliaddr Malloc(MAXSOCKADDR) len
MAXSOCKADDR Getpeername(0, cliaddr, len)
err_msg("connection from s",
Sock_ntop(cliaddr, len)) ticks
time(NULL) snprintf(buff, sizeof(buff),
".24s\r\n", ctime(ticks)) Write(0, buff,
strlen(buff)) Close(0) / close TCP
connection / exit(0)