Daemon Processes - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Daemon Processes

Description:

... that runs in the background and is independent of control from all terminals. ... 5. from user terminals ... have a controlling terminal, it needs some way ... – PowerPoint PPT presentation

Number of Views:351
Avg rating:3.0/5.0
Slides: 13
Provided by: sna1
Category:

less

Transcript and Presenter's Notes

Title: Daemon Processes


1
  • Chapter 12.
  • Daemon Processes
  • and inetd Superserver

2
12.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.

3
12.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.

4
12. 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)

5
12. 3 syslog function
  • Log message have a level between 0 and 7.

6
12. 3 syslog function
  • A facility to identify the type of process
    sending the message.

7
12. 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.

8
12.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 /

9
12.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.

10
12.5 inetd daemon
  • Figure 12.7

11
12.6 daemon_inetd Function
  • Figure 12.11

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)
12
12.6 daemon_inetd Function
  • Figure 12.12

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)
Write a Comment
User Comments (0)
About PowerShow.com