Lecture 10 Overview!

Similar documents
Overview. Daemon processes and advanced I/O. Source: Chapters 13&14 of Stevens book

Overview. Last Lecture. This Lecture. Daemon processes and advanced I/O functions

Motivation of VPN! Overview! VPN addressing and routing! Two basic techniques for VPN! ! How to guarantee privacy of network traffic?!

Lecture 5 Overview! Last Lecture! This Lecture! Next Lecture! I/O multiplexing! Source: Chapter 6 of Stevens book!

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

Network programming(ii) Lenuta Alboaie

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

Programming Internet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

EEC-484/584 Computer Networks

ISA 563: Fundamentals of Systems Programming

Never Lose a Syslog Message

Outline. Distributed Computing Systems. Socket Basics (1 of 2) Socket Basics (2 of 2) 3/28/2014

CS4700/CS5700 Fundamentals of Computer Networking

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

TELE 301 Lecture 8: Post

STUDY OF SOCKET PROGRAMMING

CS631 - Advanced Programming in the UNIX Environment. Dæmon processes, System Logging, Advanced I/O

Contents. Part 1. Introduction and TCP/IP 1. Foreword Preface. xix. I ntroduction 31

Lecture 3 Overview! Last Lecture! TCP/UDP and Sockets introduction!

Elementary TCP Sockets

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

TCP: Three-way handshake

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

Group-A Assignment No. 6

CptS 360 (System Programming) Unit 17: Network IPC (Sockets)

Standards / Extensions C or C++ Dependencies POSIX.1 XPG4 XPG4.2 Single UNIX Specification, Version 3

What could be done in the kernel to make strace happy

Programming with TCP/IP. Ram Dantu

Interprocess Communication. Interprocess Communication

Advanced Unix Concepts. Satyajit Rai

Expires: April 20, 2006 M. Tuexen Muenster Univ. of Applied Sciences T. Dreibholz University of Duisburg-Essen October 17, 2005

CS 351 Week 15. Course Review

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers

UNIX Network Programming

Ken French HELP Session 1 CS4514

NETWORK PROGRAMMING. Ipv4 and Ipv6 interoperability

Oral. Total. Dated Sign (2) (5) (3) (2)

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

Multiservice servers. Stefan D. Bruda. Winter 2018

CLIENT-SIDE PROGRAMMING

Tutorial on Socket Programming

FastFlow: targeting distributed systems Massimo Torquati

Delayline A Wide-Area Network Emulation Tool

sottotitolo Socket Programming Milano, XX mese 20XX A.A. 2016/17 Federico Reghenzani

Ports under 1024 are often considered special, and usually require special OS privileges to use.

CSE 124 Discussion Section Sockets Programming 10/10/17

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

CSE 333 Section 8 - Client-Side Networking

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation

Introduction! Overview! Signal-driven I/O for Sockets! Two different UDP servers!

Lenuta Alboaie Computer Networks

Sistemas Operativos /2016 Support Document N o 1. Files, Pipes, FIFOs, I/O Redirection, and Unix Sockets

Unix Network Programming Chapter 4. Elementary TCP Sockets 광운대학교컴퓨터과학과 정보통신연구실 석사과정안중현

Networked Applications: Sockets. End System: Computer on the Net

UDP CONNECT TO A SERVER

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

SOCKETS: INTRODUCTION

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

UNIX Sockets. COS 461 Precept 1

Chapter 3: Client-Server Paradigm and Middleware

IPv4 and ipv6 INTEROPERABILITY

Interprocess Communication

Socket programming in C

Any of the descriptors in the set {1, 4} have an exception condition pending

UNIX System Administration

Socket Programming. #In the name of Allah. Computer Engineering Department Sharif University of Technology CE443- Computer Networks

Interprocess Communication Mechanisms

shared storage These mechanisms have already been covered. examples: shared virtual memory message based signals

LINX(7) manual page. Name. Synopsis. Description. Linx Concepts. LINX(7) manual page. linx - LINX inter-process communication protocol

The User Datagram Protocol

Programming Ethernet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

Outline. Option Types. Socket Options SWE 545. Socket Options. Out-of-Band Data. Advanced Socket. Many socket options are Boolean flags

10. I/O System Library

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013

CMPSC 311 Such Final Very Exam

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

SCTP in Go. arxiv: v1 [cs.ni] 20 Nov 2017

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Operating Systems. Review ENCE 360

Introduction to Network Programming in C/C++

Processes. CS3026 Operating Systems Lecture 05

UNIX Kernel. UNIX History

Client Server Computing

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

Tcpdump. For this exercise you must again be root. Login and obtain root privileges: Note that we use three computers for this exercise.

CSE 333 SECTION 3. POSIX I/O Functions

Programming Ethernet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

The Embedded I/O Company TIP700-SW-82 Linux Device Driver User Manual TEWS TECHNOLOGIES GmbH TEWS TECHNOLOGIES LLC

Socket Programming for TCP and UDP

A Client-Server Exchange

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

INTRODUCTION TO THE UNIX FILE SYSTEM 1)

CS 640: Computer Networking

SOCKET. Valerio Di Valerio

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

Tunneling. Encapsulation and Tunneling. Performance implications of data transmission on high speed network devices

Introduction for SPI mapping Service Discovery Interoperability Testing. 20, Sep PWG Fumio Nagasaka

Transcription:

Lecture 10 Overview! Last Lecture! Wireless Sensor Networks! 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! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 1

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)! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 2

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! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 3

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.! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 4

level! Log messages have a level between 0 and 7! If no level is specified, LOG_NOTICE is the default! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 5

facility! Identify the type of process sending the message! If no facility is specified, LOG_USER is the default! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 6

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);! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 7

Options for openlog! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 8

daemon_init function! Control flow of daemon_init (refer to attachment 1: 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! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 9

inetd daemon! With systems before 4.3BSD, each service, such as FTP and Telnet, had a process associated with it, which is started at boot time.! All these daemons contained nearly identical startup code, first with respect to socket creation, and also with respect to becoming a daemon process (similar to the daemon_init function).! Each daemon took a slot in the process table, but each daemon was asleep more of the time.! Since 4.3BSD release, it was simplified by providing an Internet superserver: the inetd daemon! It simplifies writing daemon process since most of the startup details are handled by inetd. This obviates the need for each server to the daemon_init function.! It allows a single process (inetd) to be waiting for incoming client requests for multiple services, instead of one process for each service. This reduces the total number of processes in the system.! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 10

inetd daemon (cont.)! The inetd process establishes itself as a daemon using the techniques described in the daemon_init function. It then reads and processes its configuration file, typically /etc/inetd.conf. This file specifies the services that the superserver is to handle, and what to do when a service request arrives.! Some sample lines are:!! ftp stream tcp nowait root /usr/bin/ftpd ftpd -1 telnet stream tcp nowait root /usr/bin/telnetd telnetd login stream tcp nowait root /usr/bin/rlogind rlogind -s TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 11

inetd daemon! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 12

Service handled by inetd! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 13

Service handled by inetd (cont.)! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 14

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.! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 15

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)! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 16

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 Appendix 2: lib/ connect_timeo.c)! Block waiting for I/O in select, which has a time limit as an argument (refer to Appendix 3: lib/readable_timeo.c)! Use the newer SO_RCVTIMEO and SO_SNDTIMEO socket options (refer to Appendix 4: advio/dgclitimeo2.c)! The important point for the programmer is to find out how to test the timeout condition.! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 17

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! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 18

Constants for flags! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 19

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! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 20

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! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 21

Summary of flags!! 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! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 22

recvmsg! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 23

recvmsg (cont.)! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 24

Comparison of I/O functions! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 25

Ancillary data! struct cmsghdr {socklen_t cmsg_len; int cmsg_level; int cmsg_type; /* followed by char array */ }! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 26

Ancillary data (cont.)! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 27

Macros for ancillary data! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 28

Example for macros! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 29

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.! TELE402 2012 Lecture 10 Daemon processes and advanced I/O functions 30