add EGP
[unix-history] / usr / src / libexec / rexecd / rexecd.c
index 539b036..cc6e64e 100644 (file)
@@ -1,26 +1,26 @@
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)rexecd.c   4.5 82/12/25";
+static char sccsid[] = "@(#)rexecd.c   4.11 (Berkeley) %G%";
 #endif
 
 #include <sys/ioctl.h>
 #include <sys/param.h>
 #include <sys/socket.h>
 #endif
 
 #include <sys/ioctl.h>
 #include <sys/param.h>
 #include <sys/socket.h>
+#include <sys/wait.h>
 
 #include <netinet/in.h>
 
 #include <stdio.h>
 #include <errno.h>
 #include <pwd.h>
 
 #include <netinet/in.h>
 
 #include <stdio.h>
 #include <errno.h>
 #include <pwd.h>
-#include <wait.h>
 #include <signal.h>
 #include <netdb.h>
 
 extern errno;
 #include <signal.h>
 #include <netdb.h>
 
 extern errno;
-struct sockaddr_in sin = { AF_INET };
 struct passwd *getpwnam();
 char   *crypt(), *rindex(), *sprintf();
 /* VARARGS 1 */
 int    error();
 struct passwd *getpwnam();
 char   *crypt(), *rindex(), *sprintf();
 /* VARARGS 1 */
 int    error();
+int    reapchild();
 /*
  * remote execute server:
  *     username\0
 /*
  * remote execute server:
  *     username\0
@@ -32,58 +32,24 @@ main(argc, argv)
        int argc;
        char **argv;
 {
        int argc;
        char **argv;
 {
-       union wait status;
-       int f;
        struct sockaddr_in from;
        struct sockaddr_in from;
-       struct servent *sp;
+       int fromlen;
 
 
-       sp = getservbyname("exec", "tcp");
-       if (sp == 0) {
-               fprintf(stderr, "tcp/exec: unknown service\n");
-               exit(1);
-       }
-       sin.sin_port = sp->s_port;
-#ifndef DEBUG
-       if (fork())
-               exit(0);
-       for (f = 0; f < 10; f++)
-               (void) close(f);
-       (void) open("/", 0);
-       (void) dup2(0, 1);
-       (void) dup2(0, 2);
-       { int t = open("/dev/tty", 2);
-         if (t >= 0) {
-               ioctl(t, TIOCNOTTY, (char *)0);
-               (void) close(t);
-         }
-       }
-#endif
-       argc--, argv++;
-       f = socket(AF_INET, SOCK_STREAM, 0, 0);
-       if (f < 0) {
-               perror("rexecd: socket");
-               exit(1);
-       }
-       if (bind(f, &sin, sizeof (sin), 0) < 0) {
-               perror("rexecd: bind:");
+       fromlen = sizeof (from);
+       if (getpeername(0, &from, &fromlen) < 0) {
+               fprintf(stderr, "%s: ", argv[0]);
+               perror("getpeername");
                exit(1);
        }
                exit(1);
        }
-       listen(f, 10);
-       for (;;) {
-               int s, len = sizeof (from);
+       doit(0, &from);
+}
 
 
-               s = accept(f, &from, &len, 0);
-               if (s < 0) {
-                       perror("rexecd: accept");
-                       sleep(1);
-                       continue;
-               }
-               if (fork() == 0)
-                       doit(s, &from);
-               (void) close(s);
-               while (wait3(status, WNOHANG, 0) > 0)
-                       continue;
-       }
+reapchild()
+{
+       union wait status;
+
+       while (wait3(&status, WNOHANG, 0) > 0)
+               ;
 }
 
 char   username[20] = "USER=";
 }
 
 char   username[20] = "USER=";
@@ -192,8 +158,7 @@ doit(f, fromp)
                                if (ready & (1<<pv[0])) {
                                        cc = read(pv[0], buf, sizeof (buf));
                                        if (cc <= 0) {
                                if (ready & (1<<pv[0])) {
                                        cc = read(pv[0], buf, sizeof (buf));
                                        if (cc <= 0) {
-                                               int done = 1+1;
-                                               ioctl(s, SIOCDONE, (char *)&done);
+                                               shutdown(s, 1+1);
                                                readfrom &= ~(1<<pv[0]);
                                        } else
                                                (void) write(s, buf, cc);
                                                readfrom &= ~(1<<pv[0]);
                                        } else
                                                (void) write(s, buf, cc);