turn on keep alives
[unix-history] / usr / src / libexec / telnetd / telnetd.c
index cf707df..1015310 100644 (file)
@@ -1,5 +1,5 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)telnetd.c  4.13 82/12/25";
+static char sccsid[] = "@(#)telnetd.c  4.19 83/05/03";
 #endif
 
 /*
 #endif
 
 /*
@@ -10,6 +10,8 @@ static char sccsid[] = "@(#)telnetd.c 4.13 82/12/25";
 
 #include <netinet/in.h>
 
 
 #include <netinet/in.h>
 
+#include <arpa/telnet.h>
+
 #include <stdio.h>
 #include <signal.h>
 #include <errno.h>
 #include <stdio.h>
 #include <signal.h>
 #include <errno.h>
@@ -17,8 +19,6 @@ static char sccsid[] = "@(#)telnetd.c 4.13 82/12/25";
 #include <wait.h>
 #include <netdb.h>
 
 #include <wait.h>
 #include <netdb.h>
 
-#include "telnet.h"
-
 #define        BELL            '\07'
 
 char   hisopts[256];
 #define        BELL            '\07'
 
 char   hisopts[256];
@@ -40,6 +40,7 @@ int   pcc, ncc;
 
 int    pty, net;
 int    inter;
 
 int    pty, net;
 int    inter;
+int    reapchild();
 extern int errno;
 char   line[] = "/dev/ptyp0";
 
 extern int errno;
 char   line[] = "/dev/ptyp0";
 
@@ -48,8 +49,7 @@ struct        sockaddr_in sin = { AF_INET };
 main(argc, argv)
        char *argv[];
 {
 main(argc, argv)
        char *argv[];
 {
-       int s, pid;
-       union wait status;
+       int s, pid, options;
        struct servent *sp;
 
        sp = getservbyname("telnet", "tcp");
        struct servent *sp;
 
        sp = getservbyname("telnet", "tcp");
@@ -59,6 +59,10 @@ main(argc, argv)
        }
        sin.sin_port = sp->s_port;
        argc--, argv++;
        }
        sin.sin_port = sp->s_port;
        argc--, argv++;
+       if (argc > 0 && !strcmp(*argv, "-d")) {
+               options |= SO_DEBUG;
+               argc--, argv++;
+       }
        if (argc > 0) {
                sin.sin_port = atoi(*argv);
                if (sin.sin_port <= 0) {
        if (argc > 0) {
                sin.sin_port = atoi(*argv);
                if (sin.sin_port <= 0) {
@@ -89,31 +93,47 @@ again:
                sleep(5);
                goto again;
        }
                sleep(5);
                goto again;
        }
+       if (options & SO_DEBUG)
+               if (setsockopt(s, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
+                       perror("telnetd: setsockopt (SO_DEBUG)");
+       if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, 0, 0) < 0)
+               perror("telnetd: setsockopt (SO_KEEPALIVE)");
        while (bind(s, (caddr_t)&sin, sizeof (sin), 0) < 0) {
                perror("telnetd: bind");
                sleep(5);
        }
        while (bind(s, (caddr_t)&sin, sizeof (sin), 0) < 0) {
                perror("telnetd: bind");
                sleep(5);
        }
+       sigset(SIGCHLD, reapchild);
        listen(s, 10);
        for (;;) {
                int s2;
 
                s2 = accept(s, (caddr_t)0, 0, 0);
                if (s2 < 0) {
        listen(s, 10);
        for (;;) {
                int s2;
 
                s2 = accept(s, (caddr_t)0, 0, 0);
                if (s2 < 0) {
+                       if (errno == EINTR)
+                               continue;
                        perror("telnetd: accept");
                        sleep(1);
                        continue;
                }
                if ((pid = fork()) < 0)
                        printf("Out of processes\n");
                        perror("telnetd: accept");
                        sleep(1);
                        continue;
                }
                if ((pid = fork()) < 0)
                        printf("Out of processes\n");
-               else if (pid == 0)
+               else if (pid == 0) {
+                       signal(SIGCHLD, SIG_IGN);
                        doit(s2);
                        doit(s2);
+               }
                close(s2);
                close(s2);
-               while (wait3(status, WNOHANG, 0) > 0)
-                       continue;
        }
        /*NOTREACHED*/
 }
 
        }
        /*NOTREACHED*/
 }
 
+reapchild()
+{
+       union wait status;
+
+       while (wait3(&status, WNOHANG, 0) > 0)
+               ;
+}
+
 int    cleanup();
 
 /*
 int    cleanup();
 
 /*
@@ -579,13 +599,10 @@ netflush()
 
 cleanup()
 {
 
 cleanup()
 {
-       int how = 2;
 
        rmut();
 
        rmut();
-#ifdef notdef
-       vhangup();
-#endif
-       ioctl(net, SIOCDONE, &how);
+       vhangup();      /* XXX */
+       shutdown(net, 2);
        kill(0, SIGKILL);
        exit(1);
 }
        kill(0, SIGKILL);
        exit(1);
 }