Monday, December 25, 2006

Inetd

Recently while dabbling over socket programming, I came across a nifty feature known as inetd. It is basically a daemon that "listens" to the ports of the some well known servers and starts the server when it detects some request coming at that port.

For the uninitiated, a class of programs that work on the network are classified as "server-client" paradigm. The basic idea behind it is there would be a remote server program which would serve any request a client may send at it. For this kind of communication, a form of address is required. The address in this case is specified in the form of IP address and port number. Each Server has a port number and an IP address of the computer it is running on. The port number is pre determined and known by the client. The Server listens on the specified port for incoming requests. Whenever a client program sends any request to the server program (via its IP address, portnumber), the server processes the request and sends back the data to the client with the address specified by IP addres, portnumber.

Nowadays, a computer may run a number of servers, from http servers, mail servers and ftp servers. These servers need to running since the booting of the computer, listening for requests
Now the server uses up computer resources even when it is not performming any thing useful work instead just listening on its alloted port for incoming request.

The inetd deamon was built to address this situation. What the inetd does is instead of starting all the servers at boot time, only the inetd daemon is started. The inetd listens to all the ports that there are servers. It uses a API called SELECT, which allows a program to listen to a number of sockets simultaneously. The inetd runs in a loop and waits until the SELECT call returns. Whenever it detects an incoming connection, the SELECT call returns with the port number on which the incoming connection came. The inetd then understands which server to start based on the port number on which the request arrived.

What the inetd does is it "forks" and create a child process. The child process then exec the server process. Then the server process starts running. The server process uses the API getpeername() to get the address of the client process that is calling it. The inetd redirects the port of the incoming request to act as the standard input and standard output of the server process. It is to be noted that the file descriptors survive even across a exec call.

The server runs as an independent process, and when the server finishes serving the client, it ends and returns to the inetd process. The inetd again starts listenning to that port again and repeats the process for any incoming request.

Each server that runs are runned in the form of a deamon. So that it is detached from any terminal and runs independently. The process of making an running program into a daemon involves two forks and some clearn-up work. The process remains the same for all programs. Before inetd was introduced, all the servers had to independently make itself in to a daemon and then run. But with inetd, it is not required. Since inetd makes itself a daemon, all it child processes are not attached to any terminal, hence they are daemons automatically. So the programmer needs not worry about making there porgram into a daemon. The servers can now read and write as if it is reading and writing for the standard input and output. This makes the task of coding the servers eaiser.



This is just an introduction of inetd. This is intended for beginners. Please Write your comments telling me how you feel about this peice and how you want it to be improved. I would really appreciate you taking time out and writing your views

Thanks