Inetd - Creating An Inetd Service

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; FILE *fp = fopen(fn, "a+"); if(fp == NULL) exit(EXIT_FAILURE); char str; //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 stdio functions 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 is the correct protocol to use. First, an unused port number must be selected. 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.

Read more about this topic:  Inetd

Famous quotes containing the words creating and/or service:

    Other roads do some violence to Nature, and bring the traveler to stare at her, but the river steals into the scenery it traverses without intrusion, silently creating and adorning it, and is as free to come and go as the zephyr.
    Henry David Thoreau (1817–1862)

    This was a great point gained; the archdeacon would certainly not come to morning service at Westminster Abbey, even though he were in London; and here the warden could rest quietly, and, when the time came, duly say his prayers.
    Anthony Trollope (1815–1882)