We've explained above how to write a server program that does its own listening. Such a server must already be running in order for anyone to connect to it.
Another way to provide a service on an Internet port is to let the daemon program inetd do the listening. inetd is a program that runs all the time and waits (using select) for messages on a specified set of ports. When it receives a message, it accepts the connection (if the socket style calls for connections) and then forks a child process to run the corresponding server program. You specify the ports and their programs in the file /etc/inetd.conf.
Writing a server program to be run by inetd is very simple. Each time someone requests a connection to the appropriate port, a new server process starts. The connection already exists at this time; the socket is available as the standard input descriptor and as the standard output descriptor (descriptors 0 and 1) in the server process. Thus the server program can begin reading and writing data right away. Often the program needs only the ordinary I/O facilities; in fact, a general-purpose filter program that knows nothing about sockets can work as a byte stream server run by inetd.
You can also use inetd for servers that use connectionless communication styles. For these servers, inetd does not try to accept a connection since no connection is possible. It just starts the server program, which can read the incoming datagram packet from descriptor 0. The server program can handle one request and then exit, or you can choose to write it to keep reading more requests until no more arrive, and then exit. You must specify which of these two techniques the server uses when you configure inetd.
The file /etc/inetd.conf tells inetd which ports to listen to and what server programs to run for them. Normally each entry in the file is one line, but you can split it onto multiple lines provided all but the first line of the entry start with whitespace. Lines that start with # are comments.
Here are two standard entries in /etc/inetd.conf:
ftp stream tcp nowait root /libexec/ftpd ftpd talk dgram udp wait root /libexec/talkd talkd
An entry has this format:
servicestyleprotocolwaitusernameprogramarguments
The service field says which service this program provides. It should be the name of a service defined in /etc/services. inetd uses service to decide which port to listen on for this entry.
The fields style and protocol specify the communication style and the protocol to use for the listening socket. The style should be the name of a communication style, converted to lower case and with SOCK_ deleted--for example, stream or dgram. protocol should be one of the protocols listed in /etc/protocols. The typical protocol names are tcp for byte stream connections and udp for unreliable datagrams.
The wait field should be either wait or nowait. Use wait if style is a connectionless style and the server, once started, handles multiple requests as they come in. Use nowait if inetd should start a new process for each message or request that comes in. If style uses connections, then waitmust be nowait.
user is the user name that the server should run as. inetd runs as root, so it can set the user ID of its children arbitrarily. It's best to avoid using root for user if you can; but some servers, such as Telnet and FTP, read a username and password themselves. These servers need to be root initially so they can log in as commanded by the data coming over the network.
program together with arguments specifies the command to run to start the server. program should be an absolute file name specifying the executable file to run. arguments consists of any number of whitespace-separated words, which become the command-line arguments of program. The first word in arguments is argument zero, which should by convention be the program name itself (sans directories).
If you edit /etc/inetd.conf, you can tell inetd to reread the file and obey its new contents by sending the inetd process the SIGHUP signal. You'll have to use ps to determine the process ID of the inetd process as it is not fixed.