160 likes | 494 Views
Unix Network Programming Chapter 13: Daemon processes and the inetd superserver. 22.4.2005 Jani Peusaari. Contents. Daemons. Processes without a controlling terminal Generally started via startup scripts with superuser priviliges Perform administrative duties, networked or local services etc
E N D
Unix Network ProgrammingChapter 13: Daemon processes and the inetd superserver 22.4.2005 Jani Peusaari
Daemons • Processes without a controlling terminal • Generally started via startup scripts with superuser priviliges • Perform administrative duties, networked or local services etc • Output using syslog daemon, syslogd
Syslogd daemon • Collects kernel, service and user specific log information to system specific files • Used through UDP socket port 514 • Directly by sending a datagram • syslog function • UDP disabled by default, DoS possibilities
syslog function #include <syslog.h> void syslog(int priority, const char *message, …); • Priority is ORred from level and facility • Second argument is format (as in e.g. printf) with %m, error message (derived from errno) • In addition, openlog and closelog functions
Syslog Levels • Described in RFC 3164 • Seven levels • 0 is the highest, LOG_EMERG • 7 lowest, LOG_DEBUG • Level 5 (LOG_NOTICE) is the default • man syslog
Facilities • LOG_USER is the default • LOG_AUTH for security, LOG_DAEMON for system daemons etc • 8 local messages for user services (e.g. LOG_LOCAL0)
Why syslog • Daemons detach themselves, even if started from the console • No stdin, stdout, stderr • Different levels of output (Debug, notice, warning, emergency) • Collect messages in an uniform way • Portability, no need to know to which file to write messages to
Daemons • SIGHUP
How to make a daemon • Some systems have daemon() function • Fork -> Parent exits • Child becomes session leader • Ignore SIGHUP signal • Fork -> Child 1 exits • Change working directory (/), close file descriptors, std(in|out|err) to /dev/null
Inetd, xinetd • Many inet services (ftp, rlogin, etc) are needed, but are not used often • They all require similar functionality (daemonize, listen to sockets) • Inetd listens to the sockets, forks the service on their behalf • Only one process in the process list
Service types • Multi-threaded • Inetd forks a daemon with a new socket to service the client • Inetd listens to the original socket • Single-threaded • Inetd forks a daemon, and the daemon handles all incoming requests, old or new (Datagram services)
Benefits • Saves system resources, only one process listening to several sockets • Simplifies service creation, as inetd handles lots of common features on their behalf • Centralized access control to all inetd based services (xinetd) • Centralized logging (xinetd) • User services that are not in /etc/services (xinetd)