BSD 4_3_Reno release
[unix-history] / usr / src / usr.sbin / inetd / inetd.c
index 0a5a52d..6973d0c 100644 (file)
@@ -2,17 +2,19 @@
  * Copyright (c) 1983 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1983 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms are permitted provided
+ * that: (1) source distributions retain this entire copyright notice and
+ * comment, and (2) distributions including binaries display the following
+ * acknowledgement:  ``This product includes software developed by the
+ * University of California, Berkeley and its contributors'' in the
+ * documentation or other materials provided with the distribution and in
+ * all advertising materials mentioning features or use of this software.
+ * Neither the name of the University nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -22,7 +24,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)inetd.c    5.14 (Berkeley) %G%";
+static char sccsid[] = "@(#)inetd.c    5.25 (Berkeley) 6/29/90";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -56,7 +58,7 @@ static char sccsid[] = "@(#)inetd.c   5.14 (Berkeley) %G%";
  *     wait/nowait                     single-threaded/multi-threaded
  *     user                            user to run daemon as
  *     server program                  full path name
  *     wait/nowait                     single-threaded/multi-threaded
  *     user                            user to run daemon as
  *     server program                  full path name
- *     server program arguments        maximum of MAXARGS (5)
+ *     server program arguments        maximum of MAXARGS (20)
  *
  * Comment lines are indicated by a `#' in column 1.
  */
  *
  * Comment lines are indicated by a `#' in column 1.
  */
@@ -78,7 +80,8 @@ static char sccsid[] = "@(#)inetd.c   5.14 (Berkeley) %G%";
 #include <syslog.h>
 #include <pwd.h>
 #include <stdio.h>
 #include <syslog.h>
 #include <pwd.h>
 #include <stdio.h>
-#include <strings.h>
+#include <string.h>
+#include "pathnames.h"
 
 #define        TOOMANY         40              /* don't start more than TOOMANY */
 #define        CNT_INTVL       60              /* servers in CNT_INTVL sec. */
 
 #define        TOOMANY         40              /* don't start more than TOOMANY */
 #define        CNT_INTVL       60              /* servers in CNT_INTVL sec. */
@@ -88,7 +91,7 @@ static char sccsid[] = "@(#)inetd.c   5.14 (Berkeley) %G%";
 
 extern int errno;
 
 
 extern int errno;
 
-int    reapchild(), retry();
+void   config(), reapchild(), retry();
 char   *index();
 char   *malloc();
 
 char   *index();
 char   *malloc();
 
@@ -108,7 +111,7 @@ struct      servtab {
        char    *se_user;               /* user name to run as */
        struct  biltin *se_bi;          /* if built-in, description */
        char    *se_server;             /* server program */
        char    *se_user;               /* user name to run as */
        struct  biltin *se_bi;          /* if built-in, description */
        char    *se_server;             /* server program */
-#define MAXARGV 5
+#define        MAXARGV 20
        char    *se_argv[MAXARGV+1];    /* program arguments */
        int     se_fd;                  /* open descriptor */
        struct  sockaddr_in se_ctrladdr;/* bound address */
        char    *se_argv[MAXARGV+1];    /* program arguments */
        int     se_fd;                  /* open descriptor */
        struct  sockaddr_in se_ctrladdr;/* bound address */
@@ -151,7 +154,7 @@ struct biltin {
 };
 
 #define NUMINT (sizeof(intab) / sizeof(struct inent))
 };
 
 #define NUMINT (sizeof(intab) / sizeof(struct inent))
-char   *CONFIG = "/etc/inetd.conf";
+char   *CONFIG = _PATH_INETDCONF;
 char   **Argv;
 char   *LastArg;
 
 char   **Argv;
 char   *LastArg;
 
@@ -191,24 +194,8 @@ main(argc, argv, envp)
 
        if (argc > 0)
                CONFIG = argv[0];
 
        if (argc > 0)
                CONFIG = argv[0];
-       if (debug == 0) {
-               if (fork())
-                       exit(0);
-               for (tmpint = 0; tmpint < 10; tmpint++)
-                       (void) close(tmpint);
-               (void) open("/", O_RDONLY);
-               (void) dup2(0, 1);
-               (void) dup2(0, 2);
-               tmpint = open("/dev/tty", O_RDWR);
-               if (tmpint > 0) {
-                       ioctl(tmpint, TIOCNOTTY, (char *)0);
-                       close(tmpint);
-               }
-               (void) setpgrp(0, 0);
-               (void) signal(SIGTSTP, SIG_IGN);
-               (void) signal(SIGTTIN, SIG_IGN);
-               (void) signal(SIGTTOU, SIG_IGN);
-       }
+       if (debug == 0)
+               daemon(0, 0);
        openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
        bzero((char *)&sv, sizeof(sv));
        sv.sv_mask = SIGBLOCK;
        openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
        bzero((char *)&sv, sizeof(sv));
        sv.sv_mask = SIGBLOCK;
@@ -253,7 +240,7 @@ main(argc, argv, envp)
                n--;
                if (debug)
                        fprintf(stderr, "someone wants %s\n", sep->se_service);
                n--;
                if (debug)
                        fprintf(stderr, "someone wants %s\n", sep->se_service);
-               if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
+               if (sep->se_socktype == SOCK_STREAM) {
                        ctrl = accept(sep->se_fd, (struct sockaddr *)0,
                            (int *)0);
                        if (debug)
                        ctrl = accept(sep->se_fd, (struct sockaddr *)0,
                            (int *)0);
                        if (debug)
@@ -261,7 +248,8 @@ main(argc, argv, envp)
                        if (ctrl < 0) {
                                if (errno == EINTR)
                                        continue;
                        if (ctrl < 0) {
                                if (errno == EINTR)
                                        continue;
-                               syslog(LOG_WARNING, "accept: %m");
+                               syslog(LOG_WARNING, "accept (for %s): %m",
+                                       sep->se_service);
                                continue;
                        }
                } else
                                continue;
                        }
                } else
@@ -301,7 +289,7 @@ main(argc, argv, envp)
                        pid = fork();
                }
                if (pid < 0) {
                        pid = fork();
                }
                if (pid < 0) {
-                       if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
+                       if (sep->se_socktype == SOCK_STREAM)
                                close(ctrl);
                        sigsetmask(0L);
                        sleep(1);
                                close(ctrl);
                        sigsetmask(0L);
                        sleep(1);
@@ -314,17 +302,8 @@ main(argc, argv, envp)
                }
                sigsetmask(0L);
                if (pid == 0) {
                }
                sigsetmask(0L);
                if (pid == 0) {
-                       if (debug) {
-                               if (dofork &&
-                                   (tmpint = open("/dev/tty", O_RDWR)) > 0) {
-                                       ioctl(tmpint, TIOCNOTTY, 0);
-                                       close(tmpint);
-                               }
-                               (void) setpgrp(0, 0);
-                               (void) signal(SIGTSTP, SIG_IGN);
-                               (void) signal(SIGTTIN, SIG_IGN);
-                               (void) signal(SIGTTOU, SIG_IGN);
-                       }
+                       if (debug && dofork)
+                               setsid();
                        if (dofork)
                                for (tmpint = getdtablesize(); --tmpint > 2; )
                                        if (tmpint != ctrl)
                        if (dofork)
                                for (tmpint = getdtablesize(); --tmpint > 2; )
                                        if (tmpint != ctrl)
@@ -359,12 +338,13 @@ main(argc, argv, envp)
                                _exit(1);
                        }
                }
                                _exit(1);
                        }
                }
-               if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
+               if (sep->se_socktype == SOCK_STREAM)
                        close(ctrl);
            }
        }
 }
 
                        close(ctrl);
            }
        }
 }
 
+void
 reapchild()
 {
        union wait status;
 reapchild()
 {
        union wait status;
@@ -393,6 +373,7 @@ reapchild()
        }
 }
 
        }
 }
 
+void
 config()
 {
        register struct servtab *sep, *cp, **sepp;
 config()
 {
        register struct servtab *sep, *cp, **sepp;
@@ -414,7 +395,14 @@ config()
                        int i;
 
                        omask = sigblock(SIGBLOCK);
                        int i;
 
                        omask = sigblock(SIGBLOCK);
-                       if (cp->se_bi == 0)
+                       /*
+                        * sep->se_wait may be holding the pid of a daemon
+                        * that we're waiting for.  If so, don't overwrite
+                        * it unless the config file explicitly says don't 
+                        * wait.
+                        */
+                       if (cp->se_bi == 0 && 
+                           (sep->se_wait == 1 || cp->se_wait == 0))
                                sep->se_wait = cp->se_wait;
 #define SWAP(a, b) { char *c = a; a = b; b = c; }
                        if (cp->se_user)
                                sep->se_wait = cp->se_wait;
 #define SWAP(a, b) { char *c = a; a = b; b = c; }
                        if (cp->se_user)
@@ -473,6 +461,7 @@ config()
        (void) sigsetmask(omask);
 }
 
        (void) sigsetmask(omask);
 }
 
+void
 retry()
 {
        register struct servtab *sep;
 retry()
 {
        register struct servtab *sep;
@@ -651,7 +640,7 @@ again:
        while (*cp == ' ' || *cp == '\t')
                cp++;
        if (*cp == '\0') {
        while (*cp == ' ' || *cp == '\t')
                cp++;
        if (*cp == '\0') {
-               char c;
+               int c;
 
                c = getc(fconfig);
                (void) ungetc(c, fconfig);
 
                c = getc(fconfig);
                (void) ungetc(c, fconfig);
@@ -725,13 +714,14 @@ setproctitle(a, s)
 /*
  * Internet services provided internally by inetd:
  */
 /*
  * Internet services provided internally by inetd:
  */
+#define        BUFSIZE 4096
 
 /* ARGSUSED */
 echo_stream(s, sep)            /* Echo service -- echo data back */
        int s;
        struct servtab *sep;
 {
 
 /* ARGSUSED */
 echo_stream(s, sep)            /* Echo service -- echo data back */
        int s;
        struct servtab *sep;
 {
-       char buffer[BUFSIZ];
+       char buffer[BUFSIZE];
        int i;
 
        setproctitle(sep->se_service, s);
        int i;
 
        setproctitle(sep->se_service, s);
@@ -746,7 +736,7 @@ echo_dg(s, sep)                     /* Echo service -- echo data back */
        int s;
        struct servtab *sep;
 {
        int s;
        struct servtab *sep;
 {
-       char buffer[BUFSIZ];
+       char buffer[BUFSIZE];
        int i, size;
        struct sockaddr sa;
 
        int i, size;
        struct sockaddr sa;
 
@@ -761,7 +751,7 @@ discard_stream(s, sep)              /* Discard service -- ignore data */
        int s;
        struct servtab *sep;
 {
        int s;
        struct servtab *sep;
 {
-       char buffer[BUFSIZ];
+       char buffer[BUFSIZE];
 
        setproctitle(sep->se_service, s);
        while (1) {
 
        setproctitle(sep->se_service, s);
        while (1) {
@@ -778,7 +768,7 @@ discard_dg(s, sep)          /* Discard service -- ignore data */
        int s;
        struct servtab *sep;
 {
        int s;
        struct servtab *sep;
 {
-       char buffer[BUFSIZ];
+       char buffer[BUFSIZE];
 
        (void) read(s, buffer, sizeof(buffer));
 }
 
        (void) read(s, buffer, sizeof(buffer));
 }