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.

Slides:



Advertisements
Similar presentations
Socket Options. abstraction Introduction getsockopt and setsockopt function socket state Generic socket option IPv4 socket option ICMPv6 socket option.
Advertisements

TELE 402 Lecture 11: Advanced UDP… 1 by Dr Z. Huang Overview Last Lecture –Nonblocking I/O and ioctl operations –Source: Chapter 16 & 17 of Stevens’ book.
I/O Multiplexing Road Map: 1. Motivation 2. Description of I/O multiplexing 3. Scenarios to use I/O multiplexing 4. I/O Models  Blocking I/O  Non-blocking.
Daemon Processes Long lived utility processes Often started at system boot and ended when system shuts down Run in the background with no controlling terminal.
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.
Netprog: daemons and inetd1 Daemons & inetd Refs: Chapter 13.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Socket Programming.
Tutorial 8 Socket Programming
1 Advanced I/O Computer Network Programming. 2 Regarding Project1 –Use RCS to store different revisions of your programs –this is a requirement! –you.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Unix Network Programming Chapter 13: Daemon processes and the inetd superserver Jani Peusaari.
Daemon Processes and inetd Superserver
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)
Operating Systems Sockets. Outline F Socket basics F TCP sockets F Socket details F Socket options F Final notes F Project 3.
Server Design Discuss Design issues for Servers Review Server Creation in Linux.
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
1 Introduction to Raw Sockets 2 IP address Port address MAC address TCP/IP Stack 67 Bootp DHCP OSPF protocol frame type UDP Port # TCP Port.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Assignment 3 A Client/Server Application: Chatroom.
Elementary TCP Sockets
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
Operating Systems Recitation 9, May 19-20, Iterative server Handle one connection request at a time. Connection requests stored in queue associated.
More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c.
Elementary TCP Sockets
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
Review: How to create a TCP end point? What is the right format for sin_port and sin_addr in the sockaddr_in data structure? How many different ways we.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
TELE 402 Lecture 4: I/O multi … 1 Overview Last Lecture –TCP socket and Client-Server example –Source: Chapters 4&5 of Stevens’ book This Lecture –I/O.
 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)
TELE 402 Lecture 10: Unix domain … 1 Overview Last Lecture –Daemon processes and advanced I/O functions This Lecture –Unix domain protocols and non-blocking.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
Advanced Sockets API-II Vinayak Jagtap
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
1 Daemons & inetd Refs: Chapter Daemons A daemon is a process that: –runs in the background –not associated with any terminal Unix systems typically.
Consider Letting inetd Launch Your Application. inetd daemon  Problems starting with /etc/rc(without inet daemon)  All the servers contains nearly identical.
TELE 402 Lecture 12: Signal-Driven I/O & Raw Socket 1 Overview Last Lecture –Advanced UDP sockets and threads –Source: Chapters 22&26 of Stevens’ book.
Today’s topic: UDP Reliable communication over UDP.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Review: –Concurrent server and multiplexed server How they work? Which one is better?
Sockets Introduction Socket address structures Value-result arguments
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
CMPT 471 Networking II Network Programming © Janice Regan,
UNIX Network Programming1 Chapter 13. Advanced I / O Functions.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Network Programming1 Chapter 12. Daemon Processes and inetd Superserver.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
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 UDP Sockets Programming Creating UDP sockets.Creating UDP sockets. –Client –Server Sending data.Sending data. Receiving data.Receiving data. Connected.
Error handling I/O Man pages
Socket Option.
Elementary UDP Sockets
Socket Programming in C
CHAPTER 8 ELEMENTARY UDP SOCKETS
UDP Sockets Programming
User Datagram Protocol (UDP)
Advanced Network Programming spring 2007
Daemons & inetd Refs: Chapter 12.
Advanced I/O Functions
Socket Programming with UDP
Presentation transcript:

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 –Source: Chapters 13&14 of Stevens’ book Next Lecture –Unix domain protocols and non-blocking I/O –Source: Chapters 15&16 of Stevens’ book

TELE 402 Lecture 9: Daemon … 2 by Dr Z. Huang daemon A daemon is a process that runs in the background and is independent of control from all terminals Reasons for daemons’ independence of terminals –Prevent daemons’ error message from appearing on a user’s terminal –Signals generated from terminal keys must not affect any daemons that were started from that terminal earlier Ways to start a daemon –Started by the system initialization scripts –Started by inetd superserver –Performed by cron daemon on a regular basis –Started from user terminals (foreground or background)

TELE 402 Lecture 9: Daemon … 3 by Dr Z. Huang syslogd daemon How it works? –Read the configuration file /etc/syslog.conf –A Unix domain socket is created and bound to the pathname /var/run/log –A UDP socket is created and bound to port 514 –The pathname /dev/klog is opened to read kernel error messages –Runs in an infinite loop that calls select, waiting for any one of the above descriptors to be readable, reads the log message, and does what the configuration file says to do with that message. –If the daemon receives the SIGHUP signal, it rereads the configuration file

TELE 402 Lecture 9: Daemon … 4 by Dr Z. Huang syslog function How to send log messages? –create a Unix domain datagram socket and send our messages to the pathname the daemon has bound, or send them to port 514 by a UDP socket syslog function –An easy interface to the syslogd daemon –void syslog(int priority, const char *message, …); –priority is a combination of a level and a facility shown later –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.

TELE 402 Lecture 9: Daemon … 5 by Dr Z. Huang level Log messages have a level between 0 and 7 If no level is specified, LOG_NOTICE is the default

TELE 402 Lecture 9: Daemon … 6 by Dr Z. Huang facility Identify the type of process sending the message If no facility is specified, LOG_USER is the default

TELE 402 Lecture 9: Daemon … 7 by Dr Z. Huang Example for syslog The following call could be issued by a mail daemon when a call to open unexpectedly fails: –syslog(LOG_INFO|LOG_MAIL, “open(%s ): %m”, file); –When an application calls syslog for the first time, it creates a Unix domain datagram socket and then calls connect to the well-known pathname of the socket created by the syslogd daemon. This socket remains open until the process terminates. Alternatively, the process can call openlog and closelog –void openlog(const char *ident, int options, int facility); –ident is a string that will be inserted in front of each log message –options is formed as the logical OR of one or more of the constants in Figure 12.3 –facility specifies a default facility –void closelog(void);

TELE 402 Lecture 9: Daemon … 8 by Dr Z. Huang Options for openlog

TELE 402 Lecture 9: Daemon … 9 by Dr Z. Huang daemon_init function Control flow of daemon_init (refer to lib/daemon_init.c) –fork: change the process into a child process –setsid: create a new session and the process becomes the session leader and group leader –Ignore SIGHUP and fork again, so that the daemon cannot automatically acquire a controlling terminal should it open a terminal device in the future. We must ignore SIGHUP because when the session leader terminates, all processes in the session are sent the SIGHUP signal –Set flag for error functions (err_XXX) so that they send error messages to syslogd. We cannot use printf to print any error message from a daemon (because of no controlling terminal) –Change working directory and clear file mode creation mask –Close any open descriptors –openlog for errors

TELE 402 Lecture 9: Daemon … 10 by Dr Z. Huang inetd daemon

TELE 402 Lecture 9: Daemon … 11 by Dr Z. Huang Service handled by inetd

TELE 402 Lecture 9: Daemon … 12 by Dr Z. Huang Service handled by inetd (cont.)

TELE 402 Lecture 9: Daemon … 13 by Dr Z. Huang Daemon invoked by inetd Control flow (refer to inetd/daytimetcpsrv3.c) –Set daemon flag so that err_XXX can print error messages to syslogd –openlog –Use getpeername to find out the peer address –Receive requests from the client by reading from the descriptor 0 –Send response to the client –Close connection.

TELE 402 Lecture 9: Daemon … 14 by Dr Z. Huang tcpd Control flow –Check the client IP address using getpeername –Check the service (port number) using getsockname –Compare the above with the corresponding entries in hosts.allow and hosts.deny files and then decide if the connection should be closed or not –If the connection is allowed, call exec to start the server (using argv)

TELE 402 Lecture 9: Daemon … 15 by Dr Z. Huang Socket timeouts Three ways to place a timeout on an I/O operation involving a socket –Call alarm, which generates the SIGALRM signal when the specified time has expired (refer to lib/connect_timeo.c and advio/dgclitimeo3.c) –Block waiting for I/O in select, which has a time limit as an argument (refer to lib/readable_timeo.c) –Use the newer SO_RCVTIMEO and SO_SNDTIMEO socket options (refer to advio/dgclitimeo2.c) The important point for the programmer is to find out how to test the timeout condition.

TELE 402 Lecture 9: Daemon … 16 by Dr Z. Huang Advanced I/O

TELE 402 Lecture 9: Daemon … 17 by Dr Z. Huang recv and send Prototype –ssize_t recv(int sockfd, void *buff, size_t nbytes, int flags); –ssize_t send(int sockfd, const void *buff, size_t nbytes, int flags); –Both return: number of bytes read or written if OK, -1 on error –The first three arguments are the same as the first three arguments to read and write –flags is either 0, or is formed by logically OR’ing one or more of the constants shown in Figure 13.6

TELE 402 Lecture 9: Daemon … 18 by Dr Z. Huang Constants for flags

TELE 402 Lecture 9: Daemon … 19 by Dr Z. Huang readv and writev Prototype –ssize_t readv(int filedes, const struct iovec *iov, int iovcnt); Called scatter read since the input data is scattered into multiple application buffers –ssize_t writev(int filedes, const struct iovec *iov, int iovcnt); Called gather write since multiple buffers are gathered for a single output operation. –Both return: number of bytes read or written if OK, -1 on error –struct iovec { void *iov_base; size_t iov_len;}; –iovcnt is the number of elements in the array of iovec structures

TELE 402 Lecture 9: Daemon … 20 by Dr Z. Huang recvmsg and sendmsg Prototype –ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags); –ssize_t sendmsg(int sockfd, struct msghdr *msg, int flags); –Both return: number of bytes read or written if OK, -1 on error –struct msghdr { void *msg_name; socklen_t msg_namelen; struct iovec *msg_iov; size_t msg_iovlen; void *msg_control; socklen_t msg_controllen; int msg_flags;} –msg_name and msg_namelen are used for unconnected UDP sockets to store a socket address and its length –msg_control and msg_controllen are used for optional ancillary data –msg_flags is used only by recvmsg. When recvmsg is called, the flags is copied into the msg_flags member and this value is used by the kernel to drive its receiving processing. This value is then updated based on the result of recvmsg –msg_flags is ignored by sendmsg since this function uses its argument flags

TELE 402 Lecture 9: Daemon … 21 by Dr Z. Huang Summary of flags

TELE 402 Lecture 9: Daemon … 22 by Dr Z. Huang recvmsg

TELE 402 Lecture 9: Daemon … 23 by Dr Z. Huang recvmsg (cont.)

TELE 402 Lecture 9: Daemon … 24 by Dr Z. Huang Comparison of I/O functions

TELE 402 Lecture 9: Daemon … 25 by Dr Z. Huang Ancillary data struct cmsghdr {socklen_t cmsg_len; int cmsg_level; int cmsg_type; /* followed by char array */ }

TELE 402 Lecture 9: Daemon … 26 by Dr Z. Huang Ancillary data (cont.)

TELE 402 Lecture 9: Daemon … 27 by Dr Z. Huang Macros for ancillary data

TELE 402 Lecture 9: Daemon … 28 by Dr Z. Huang Example for macros

TELE 402 Lecture 9: Daemon … 29 by Dr Z. Huang Miscellaneous Ways to find out how much data is queued –Use MSG_PEEK flag (MSG_DONTWAIT for nonblocking) –Use the FIONREAD command of ioctl Handle sockets with standard I/O library –Use fdopen to convert a socket (descriptor) into a FILE pointer Standard I/O uses three types of buffering –Fully buffered: I/O takes place only when the buffer is full, or the process calls fflush or exit –Line buffered: I/O takes place only when a new line is encountered, or the process calls fflush or exit –Unbuffered: I/O takes place each time a standard I/O output function is called Most Unix implements standard I/O based on the following rules –Standard error is always unbuffered; standard input and output, and other streams are fully buffered, unless they refer to a terminal device, in which case they are line buffered.

TELE 402 Lecture 9: Daemon … 30 by Dr Z. Huang T/TCP (TCP for Transactions)