From: M Gordon (mfg@castle.ed.ac.uk)
Date: 06/14/93


From: mfg@castle.ed.ac.uk (M Gordon)
Subject: NET-2 : problems with inetd
Date: 14 Jun 1993 08:33:19 GMT

Kernel 0.99.10, libc 4.4

Most of net-2 seems to work - I've configured the loopback interface
(I don't have an ethernet card) and programs can talk to each other.
One big problem remains though - anything started from inetd dies
immediately, before main() as far as I can tell. I've run inetd with
the -d flag so I can see what it's doing, and checked the access time
on things such as /etc/in.telnetd, so I know inetd is starting them.
If I replace inetd with the program below, "telnet localhost" works
as expected.

Any ideas?

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int
main(int argc,char **argv)
{
    struct servent *sep;
    struct sockaddr_in my_addr,rem_addr;
    int my_len,rem_len;
    int s,ns;

    if ((s=socket(AF_INET,SOCK_STREAM,0))==-1) {
        perror("socket");
        exit(1);
    }
    
    if ((sep=getservbyname("telnet","tcp"))==NULL) {
        fprintf(stderr,"cannot find telnet service\n");
        exit(1);
    }
    my_addr.sin_family=AF_INET;
    my_addr.sin_port=sep->s_port;
    my_addr.sin_addr.s_addr=INADDR_ANY;
    my_len=sizeof(my_addr);

    if (bind(s,(struct sockaddr *)&my_addr,my_len)==-1) {
        perror("bind");
        exit(1);
    }

    listen(s,5);
    
    while (1) {
        rem_len=sizeof(rem_addr);
        if ((ns=accept(s,(struct sockaddr *)&rem_addr,&rem_len))==-1) {
            perror("accept");
            exit(1);
        }

        printf("accepted connection from %s\n",
               inet_ntoa(rem_addr.sin_addr));
        
        switch (fork()) {
          case -1:
            perror("fork");
            exit(1);
            
          case 0:
            dup2(ns,0);
            dup2(ns,1);
            dup2(ns,2);
            close(s);
            close(ns);
            execlp("/etc/in.telnetd","in.telnetd",(char *)NULL);
            perror("execlp");
            exit(1);
            
          default:
            close(ns);
            break;
        }
    }

    return(0);
}

-- 
Michael Gordon - mfg@castle.ed.ac.uk OR ee.ed.ac.uk
Computing Officer, EE Dept, Edinburgh University
It's not an optical illusion, it just looks like one