1 / 12

Chapter 12. Daemon Processes and inetd Superserver

Chapter 12. Daemon Processes and inetd Superserver. 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 )

peigi
Download Presentation

Chapter 12. Daemon Processes and inetd Superserver

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 12. Daemon Processes and inetd Superserver UNIX Network Programming

  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. UNIX Network Programming

  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. UNIX Network Programming

  4. 12. 3 syslog function #include <syslog.h> void syslog(int priority, const char *message, . . . ); • 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_INFO|LOG_LOCAL2, “rename(%s, %s): %m”,file1,file2); UNIX Network Programming

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

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

  7. 12. 3 syslog function #include <syslog.h> void openlog(const char *ident, int options, int facility); void closelog(void); • 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. UNIX Network Programming

  8. 12.4 daemon_init Function #include "unp.h" #include <syslog.h> #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 */ umask(0); /* clear our file mode creation mask */ for (i = 0; i < MAXFD; i++) close(i); openlog(pname, LOG_PID, facility); } UNIX Network Programming

  9. 12.5 inetd Daemon • A typical Unix system’s problems 1. All these daemons contained nearly identical startup code. 2. Each daemon took a slot in the processtable, 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. UNIX Network Programming

  10. 12.5 inetd daemon • Figure 12.7 UNIX Network Programming

  11. 12.6 daemon_inetd Function • Figure 12.11 #include "unp.h" #include <syslog.h> 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); } UNIX Network Programming

  12. 12.6 daemon_inetd Function • Figure 12.12 #include "unp.h" #include <time.h> int main(int argc, char **argv) { socklen_t len; struct sockaddr *cliaddr; char buff[MAXLINE]; time_t ticks; daemon_inetd(argv[0], 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); } UNIX Network Programming

More Related