1 Daemon Processes Computer Network Programming. 2 Motivation You wrote a server and you want to have it running all the time so that clients can connect.

Slides:



Advertisements
Similar presentations
Echo server The client reads a line of text from its standard input and writes the line to the server The server reads the line from its network input.
Advertisements

Categories of I/O Devices
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Pseudo Terminals Concept Application APIs. Overview A pseudo terminal (PTY) is a user level program that appears to be a terminal device to another program.
Using tcpdump. tcpdump is a powerful tool that allows us to sniff network packets and make some statistical analysis out of those dumps. tcpdump operates.
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
1 Elementary TCP Sockets socket function connect function bind function listen function accept function fork and exec functions Concurrent servers close.
Socket Programming Application Programming Interface.
Syslog and log files1-1 Syslog and Log Files  From logfiles, you can find m important information m History m Errors/warnings  Logging policies m Reset.
Process Relationships Terminal and Network Logins Process Groups and Sessions Job Control Relationships.
CS162B: Daemonization Jacob T.Chan. Foreground Process  Has input/output capabilities  These require users at the terminal  Lives as long as the terminal.
Lecture 19 Advanced Sockets Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Netprog: daemons and inetd1 Daemons & inetd Refs: Chapter 13.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
Socket Programming.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
TDC561 Network Programming Camelia Zlatea, PhD Week 2 – part II: Socket Application Programming Interface.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
Unix Network Programming Chapter 13: Daemon processes and the inetd superserver Jani Peusaari.
Daemon Processes and inetd Superserver
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Chapter 3 Unix Overview. Figure 3.1 Unix file system.
Chapter 26 Client Server Interaction Communication across a computer network requires a pair of application programs to cooperate. One application on one.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
UNIX Processes. The UNIX Process A process is an instance of a program in execution. Created by another parent process as its child. One process can be.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
Server Design Discuss Design issues for Servers Review Server Creation in Linux.
Agenda  Terminal Handling in Unix File Descriptors Opening/Assigning & Closing Sockets Types of Sockets – Internal(Local) vs. Network(Internet) Programming.
Elementary TCP Sockets
UNIX Network Programming1 UNIX Network Programming 2nd Edition.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Inetd...Server of Servers Looks at a number of ports Determines when a service is needed on any of those ports Calls the appropriate server Restarts new.
Linux Services Muhammad Amer. 2 xinetd Programs  In computer networking, xinetd, the eXtended InterNET Daemon, is an open-source super-server daemon.
Elementary TCP Sockets
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
SUSE Linux Enterprise Server Administration (Course 3037) Chapter 6 Manage Linux Processes and Services.
1 Server Design Discuss Design issues for Servers Review Server Creation in Windows.
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
 Advanced programming for the unix environment (chapters 7,8,9 of both editions + chapter 13(2 nd edition))
Nezer J. Zaidenberg.  Advanced programming for the unix environment (chapters about processes)
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Core System Services. INIT Daemon The init process is the patron of all processes. first process that gets started in any Linux/ UNIX -based system.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
TELE 402 Lecture 9: Daemon … 1 by Dr Z. Huang Overview Last Lecture –Broadcast and multicast This Lecture –Daemon processes and advanced I/O functions.
1 Daemons & inetd Refs: Chapter Daemons A daemon is a process that: –runs in the background –not associated with any terminal Unix systems typically.
Operating Systems Process Creation
2.1 Processes  process = abstraction of a running program.
Daemons Ying Zhang CMSC691X, Summer02. Outline  Introduction  Init and Cron  System daemons  Print daemons and NFS daemons  Time synchronization.
Introduction A Simple Daytime Client A Simple Daytime Server
Concurrent Servers. Idea Behind Concurrent Servers Server Client 1 Server 1 X.
UNIX Network Programming1 Chapter 12. Daemon Processes and inetd Superserver.
Signals and daemon processes Prepared by : Department of CSE Engineered for Tomorrow Course code: 10CS62.
CSC414 “Introduction to UNIX/ Linux” Lecture 3
Unix System Administration Chapter 31 Daemons. Out of the Goo, the Primordial Process l Init l Always the first process to run after system boot l Always.
2.1 Processes  process = abstraction of a running program  multiprogramming = CPU switches from running program to running program  pseudoparallelism.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
1 School of Computing Science Simon Fraser University CMPT 471: Computer Networking II Introduction Instructor: Dr. Mohamed Hefeeda.
Sockets and Beginning Network Programming
Process relationships
Processes in Unix, Linux, and Windows
UNIX Services and Daemons
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
2.1 Processes process = abstraction of a running program
CGS 3763 Operating Systems Concepts Spring 2013
Advanced Network Programming spring 2007
Daemons & inetd Refs: Chapter 12.
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
Presentation transcript:

1 Daemon Processes Computer Network Programming

2 Motivation You wrote a server and you want to have it running all the time so that clients can connect and use your service at any time. –Examples: Web serves, telnet servers, ftp servers, domain name servers, etc. You don’t want your server to be killed when you logout or when you press ^C. You want your server to be running in the background silently without disturbing others and without getting disturbed by others. You want to run it for a long-time: for days, months, years maybe. You want it to be started automatically when the system boots up.

3 Daemon Processes Solution: You need to run your server as a daemon! A daemon is a process that runs in the background and is independent of control from all terminals. A daemon process usually performs server tasks and administrative tasks There are daemon processes running in a Unix system: silently.

4 root Mar 22 ? 0:07 /usr/sbin/inetd -s root Mar 22 ? 0:01 /usr/lib/nfs/statd root Mar 22 ? 1:49 /usr/lib/autofs/automountd root Mar 22 ? 0:00 /usr/lib/nfs/lockd root Mar 22 ? 0:07 /usr/lib/utmpd root Mar 22 ? 0:22 /usr/sbin/syslogd -n -z 18 root Mar 22 ? 0:02 /usr/sbin/cron root Mar 22 ? 0:01 /usr/lib/snmp/snmpdx -y -c /etc/snmp/conf root Mar 22 ? 0:03 /usr/lib/nfs/mountd root Mar 22 ? 0:00 /usr/lib/nfs/nfsd -a 16 root Mar 22 ? 0:00 /usr/lib/dmi/snmpXdmid -s aspendos root Mar 26 ? 1:00 /usr/lib/lpsched root Mar 22 ? 0:00 /usr/lib/dmi/dmispd root Mar 22 ? 0:08 /usr/dt/bin/dtlogin -daemon root Mar 27 ? 0:00 in.rlogind root Mar 26 ? 0:00 /aspendosh/apache/bin/httpd root Mar 23 ? 0:18 /usr/dt/bin/rpc.ttdbserverd root :10:32 ? 0:00 in.telnetd ………. truncated output Some daemons running on a Unix system

5 Why independence from all terminals Assume you start your daemon from a terminal (xterm, dump terminal, etc). –You want to use the same terminal for other tasks. Hence you don’t want the daemon to print out messages to the screen while you are doing some other tasks –When the user presses interrupt keys (^C), you don’t want your daemon to be killed.

6 How to start a daemon –Start it from a system initialization script during system startup. –inetd, syslogd, sendmail, httpd are examples –inetd starts the daemon (service) –ftpd, telnetd, etc. are started from the inetd process. –cron daemon starts them at regular intervals –at command can be used to execute a server at a well-specified time in the future. –Can be started from user terminal: »to test the daemon »to restart a died daemon

7 syslogd daemon Used to log messages (error messages, information messages, all kinds of messages) Daemons usually don’t print messages on the screen. They send the messages to the syslogd and it logs everything or prints to console if necessary

8 syslogd Daemon Process Daemon Process Daemon kernel console Log file Udp Socket Unix Domain Socket Messages /dev/klog Uses select() to multiplex input from multiple sources - input from UDP socket (port 514) - input from Unix domain socket (/var/run/log) - input from Kernel (/dev/klog device) - output to log file(s) - output to console - output to user - output to an other syslogd Syslogs reads a configuration file /etc/syslog.conf to learn what to do with each kind of message

9 syslog() function A daemon does not have a controlling terminal (a keyboard/screen for example), hence it can not call printf.. It calls syslog() function to output a error messages or log messages to the syslogd and syslogd stores the message or writes it to the console. Syslog() function sends the message to the syslogd. void syslog(int priority, const char* message, ….); priority is combination of level and facility flags. message is a format string like printf(). then some arguments may follow

10 Level denotes the severity of the message: crital, error, info, debug message etc. Facility identifies the type of the process sending the message ftp daemon, kernel, mail system, a user process, local use, etc. Example: syslog (LOG_INFO | LOG_LOCAL2, “rename error: %s %s”, file1, file2) syslogd uses the configuration file /etc/syslog.conf and level and facility parameters to decide what to do with each incoming message. /etc/syslogd.conf file content *.err;kern.notice;auth.notice /dev/console *.err;kern.debug;daemon.notice;mail.crit /var/adm/message

11 static void err_doit(int errnoflag, int level, const char *fmt, va_list ap) { int errno_save, n; char buf[MAXLINE]; errno_save = errno; vsprintf(buf, fmt, ap); n = strlen(buf); if (errnoflag) snprintf(buf+n, sizeof(buf)-n, ": %s", strerror(errno_save)); strcat(buf, "\n"); if (daemon_proc) { syslog (level, buf); /* send to syslogd daemon for logging */ } else { fflush(stdout); /* print out to the screen */ fputs(buf, stdout); fflush(stdout); } return; } Use of syslog() function

12 How can we daemonize a process #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); }

13 A daemon process will never receive the following signals from the kernel: SIG_HUP, SIG_INT, SIH_WINCH. Usually SIG_HUP signal is given to the daemon by system administrators to make the daemon reread its confıguration file(s). Exampe: kill -HUP inetd

14 int main(int argc, char **argv) { int listenfd, connfd; socklen_t addrlen, len; struct sockaddr *cliaddr; char buff[MAXLINE]; time_t ticks; daemon_init(argv[0], 0); if (argc == 2) listenfd = Tcp_listen(NULL, argv[1], &addrlen); else if (argc == 3) listenfd = Tcp_listen(argv[1], argv[2], &addrlen); else err_quit("usage: daytimetcpsrv2 [ ] "); cliaddr = Malloc(addrlen); for ( ; ; ) { len = addrlen; connfd = Accept(listenfd, cliaddr, &len); err_msg("connection from %s", Sock_ntop(cliaddr, len)); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); Write(connfd, buff, strlen(buff)); Close(connfd); } Daytime tcp server as a daemon

15 inetd daemon internet superserver There are a lot of internet servers that will be running on a typical Unix system - FTP, telnet, rlogin, TFTP, daytime, time, echo Servers In earlier systems, each service had a process (daemon) associated with. - each process does the same startup tasks creating sockets, binding listening…. - each process took an entry in the process table - these processes are asleep most of the time waiting for requests. After 4.3 BSD Unix system, a single server inetd daemon is used to wait for requests for all of these services and then create the corresponding server process on demand.

16 inetd ftpstreamtcpnowaitroot/usr/sbin/in.ftpdin.ftpd telnetstreamtcpnowaitroot/usr/sbin/in.telnetdin.telnetd namedgramudpwaitroot/usr/sbin/in.tnamedin.tnamed shellstreamtcpnowaitroot/usr/sbin/in.rshdin.rshd loginstreamtcpnowaitroot/usr/sbin/in.rlogindin.rlogind tftpdgramudpwaitroot/usr/sbin/in.tftpdin.tftpd -s /tftpboot /etc/inetd.conf file ftpdtelnetdrlogind ……. fork() and exec() reads the configuration file upon startup Has a listening socket for each TCP service or a UDP socket for each UDP service and uses select()

17 inetd and its child services aspendos{korpe}:> ps -ef | grep 139 user ppid pid tty process root Mar 22 ? 0:07 /usr/sbin/inetd -s korpe :15:11 ? 0:00 in.ftpd root :14:16 ? 0:00 in.ftpd root :26:13 ? 0:00 in.rlogind root :13:55 ? 0:00 in.telnetd root Mar 26 ? 0:00 in.rlogind root :00:56 ? 0:00 in.telnetd root :13:18 ? 0:00 in.telnetd root :16:04 ? 0:00 in.telnetd We have a separate child server process for each client. The parent of these servers is inetd.

18 socket() steps performed by inetd bind() listen() (if TCP) select() accept() (if TCP) fork() close all descriptors other than socket Dup socket to descriptors 0, 1, 2 close socket setgid, setusid exec() server close connected socket (if TCP) For each service listed in the /etc/inetd.conf file parentchild

19 –For services that uses TCP, when inetd creates a child process, it does not wait until the child terminates before accepting a new service request. –The child uses a different connected socket than the listening socket of the inetd. –For services that uses UDP, when inetd creates a child process, inetd waits until the child terminates before accepting a new service request. –The child uses the same UDP socket that the inetd was waiting on for requests. Wait and Nowait

20 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); } Daytime Server as Daemon invoked by inetd extern int daemon_proc; /* defined in error.c */ void daemon_inetd(const char *pname, int facility) { daemon_proc = 1; openlog(pname, LOG_PID, facility); } We are writing to descriptor 0 since it corresponds to the socket. (normally it corresponds to stdin = keyboard)