How to setup services in Linux using inetd.conf

The file /etc/services is used to map port numbers and protocols to service names, and the file /etc/inetd.conf is used to map service names to server names. For example, if a TCP request comes in on port 23, /etc/services shows

telnet 23/tcp

The corresponding line in the /etc/inetd.conf file (in this case, taken from a machine running AIX version 5.1) is

telnet stream tcp6 nowait root /usr/sbin/telnetd telnetd -a

This tells inetd to launch the program /usr/sbin/telnetd with the command line arguments telnetd -a. inetd automatically hooks the socket to stdin, stdout, and stderr of the server program.

Generally TCP sockets are handled by spawning a separate server to handle each connection concurrently. UDP sockets are generally handled by a single server instance that handles all packets on that port.

Some simple services, such as echo, are handled directly by inetd, without spawning an external server.
[edit] Creating an inetd service

This is a simple inetd service, written in C. It expects a command line argument containing a filename for a log file, and then it logs all strings sent through the socket to the log file.

#include
#include

int main(int argc, char **argv)
{
const char *fn = argv[1];
FILE *fp = fopen(fn, “a+”);

if(fp == NULL)
exit(EXIT_FAILURE);

char str[4096];
//inetd passes its information to us in stdin.
while(fgets(str, sizeof(str), stdin)) {
fputs(str, fp);
fflush(fp);
}
fclose(fp);
return 0;
}

the example uses standard c file functions as can be observed and it responds to network traffic coming in on stdin. In this case, we want all messages logged to a single file, so we only want one instance of the service running to service all requests. This means UDP would be the correct protocol to use. First, an unused port number must be picked. In this sample, 9999 will be used. The /etc/services entry will look like this:

errorLogger 9999/udp

And the entry in /etc/inetd.conf will look like this:

errorLogger dgram udp wait root /usr/local/bin/errlogd errlogd /tmp/logfile.txt

This tells inetd to run the /usr/local/bin/errlogd program, with the commandline: errlogd /tmp/logfile.txt (refer to the inetd.conf man page for information on the other arguments). The first argument contains the filename to be used for the log file: /tmp/logfile.txt. inetd will run the service when needed, and attach port 9999 to the input and output streams, and all strings sent to that port will be logged to the file. By specifying wait, it tells inetd to only use one instance of the server to handle all requests.

Note: the functionality of the above example is usually implemented by using syslog and a process like syslogd. syslogd would normally be started in parallel with inetd, not as an inetd service.

Share
This entry was posted in Important for LPI 117-101. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>