Download presentation
Presentation is loading. Please wait.
Published byNoel Dalton Modified over 8 years ago
1
UNIX Network Programming1 Chapter 12. Daemon Processes and inetd Superserver
2
UNIX Network Programming2 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 cron 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
UNIX Network Programming3 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
UNIX Network Programming4 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_INFO|LOG_LOCAL2, “rename(%s, %s): %m”,file1,file2); #include void syslog(int priority, const char * message,... );
5
UNIX Network Programming5 12. 3 syslog function Log message have a level between 0 and 7.
6
UNIX Network Programming6 12. 3 syslog function A facility to identify the type of process sending the message.
7
UNIX Network Programming7 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. #include void openlog(const char * ident, int options, int facility ); void closelog(void);
8
UNIX Network Programming8 12.4 daemon_init Function #include"unp.h" #include #defineMAXFD64 extern intdaemon_proc;/* defined in error.c */ void daemon_init(const char *pname, int facility) { inti; pid_tpid; 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); }
9
UNIX Network Programming9 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 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
UNIX Network Programming10 12.5 inetd daemon Figure 12.7
11
UNIX Network Programming11 12.6 daemon_inetd Function Figure 12.11 #include "unp.h" #include 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
UNIX Network Programming12 12.6 daemon_inetd Function Figure 12.12 #include "unp.h" #include 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); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.