Transport Interfaces Programming Guide

inetd Daemon

One of the daemons provided with the system is inetd. It is invoked at start-up time, and gets the services for which it listens from the /etc/inetd.conf file. The daemon creates one socket for each service listed in /etc/inetd.conf, binding the appropriate port number to each socket. See the inetd(1M) man page for details.

inetd polls each socket, waiting for a connection request to the service corresponding to that socket. For SOCK_STREAM type sockets, inetd does an accept() on the listening socket, fork()s, dup()s the new socket to file descriptors 0 and 1 (stdin and stdout), closes other open file descriptors, and exec()s the appropriate server.

The primary benefit of inetd is that services that are not in use are not taking up machine resources. A secondary benefit is that inetd does most of the work to establish a connection. The server started by inetd has the socket connected to its client on file descriptors 0 and 1, and can immediately read(), write(), send(), or recv(). Servers can use buffered I/O as provided by the stdio conventions, as long as they use fflush() when appropriate.

getpeername() returns the address of the peer (process) connected to a socket; it is useful in servers started by inetd. For example, to log the Internet address in decimal dot notation (such as 128.32.0.4, which is conventional for representing an IP address of a client), an inetd server could use the following:


struct sockaddr_in name;
int namelen = sizeof name;
 ...
if (getpeername(0, (struct sockaddr *) &name, &namelen) < 0) {
		syslog(LOG_ERR, "getpeername: %m");
		exit(1);
} else
		syslog(LOG_INFO, "Connection from %s",
			inet_ntoa(name.sin_addr));
 ...