[BACK]Return to gen_minfd.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/gen_minfd.c, Revision 1.1

1.1     ! deraadt     1: /*
        !             2:
        !             3: gen_minfd.c
        !             4:
        !             5: Author: David Mazieres <dm@lcs.mit.edu>
        !             6:        Contributed to be part of ssh.
        !             7:
        !             8: Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
        !             9:                    All rights reserved
        !            10:
        !            11: Created: Tue Aug 22 17:22:57 1995 ylo
        !            12: Last modified: Tue Aug 22 17:44:32 1995 ylo
        !            13:
        !            14: */
        !            15:
        !            16: #include "includes.h"
        !            17: #include <sys/resource.h>
        !            18: #include "fdlim.h"
        !            19:
        !            20: static char *self;
        !            21:
        !            22: static void
        !            23: child_test (char *fdstr)
        !            24: {
        !            25:   int fd = atoi (fdstr);
        !            26:   if (fcntl (fd, F_GETFL, NULL) < 0) {
        !            27:     exit (1);
        !            28:   }
        !            29:   exit (0);
        !            30: }
        !            31:
        !            32: static int
        !            33: run_child (char *shell, int fd)
        !            34: {
        !            35:   char cmd[128];
        !            36:   int pid;
        !            37:   int status;
        !            38:
        !            39:   if (dup2 (0, fd) < 0) {
        !            40:     perror ("dup2");
        !            41:     return (-1);
        !            42:   }
        !            43:
        !            44:   sprintf (cmd, "%s -fd %d", self, fd);
        !            45:
        !            46:   fflush (stdout);
        !            47:   pid = fork ();
        !            48:   if (! pid) {
        !            49:     close (1);  /* prevent any garbage from entering the output */
        !            50:     dup (2);
        !            51:     execlp (shell, shell, "-c", cmd, NULL);
        !            52:     exit (1);
        !            53:   }
        !            54:   close (fd);
        !            55:   if (wait (&status) != pid) {
        !            56:     fprintf (stderr, "wrong/no child??\n");
        !            57:     exit (1);
        !            58:   }
        !            59:   return (status ? -1 : 0);
        !            60: }
        !            61:
        !            62: static int
        !            63: do_shell (char *shell)
        !            64: {
        !            65:   int fd, min, max;
        !            66:
        !            67:   min = 3;
        !            68:   max = fdlim_get (0) - 1;
        !            69:   if (max < 0) {
        !            70:     printf ("fdlim_get: bad value\n");
        !            71:     exit (1);
        !            72:   }
        !            73:
        !            74:   if (run_child (shell, max)
        !            75:       && run_child (shell, --max))  /* bizarre ultrix weirdness */
        !            76:     return (-1);
        !            77:
        !            78:   while (min + 1 < max) {
        !            79:     fd = (min + max) / 2;
        !            80:     if (run_child (shell, fd))
        !            81:       min = fd;
        !            82:     else
        !            83:       max = fd;
        !            84:   }
        !            85:   return (max);
        !            86: }
        !            87:
        !            88: int
        !            89: main (int argc, char **argv)
        !            90: {
        !            91:   int fd;
        !            92:   int i;
        !            93:   char *p;
        !            94:
        !            95:   if (argc == 3 && !strcmp (argv[1], "-fd"))
        !            96:     child_test (argv[2]);
        !            97:   self = argv[0];
        !            98:
        !            99:   fd = fdlim_get (1);
        !           100:   if (fd < 0) {
        !           101:     fprintf (stderr, "fdlim_get: bad value\n");
        !           102:     exit (1);
        !           103:   }
        !           104:   fdlim_set (fd);
        !           105:   printf ("/* maximum file descriptors = %d */\n\n", fd);
        !           106:   printf ("struct Min_Auth_Fd {\n"
        !           107:          "  int fd;\n"
        !           108:          "  char shell[32];\n"
        !           109:          "};\n\n"
        !           110:          "static struct Min_Auth_Fd mafd[] = {\n");
        !           111:   for (i = 1; i < argc; i++) {
        !           112:     fd = do_shell (argv[i]);
        !           113:     if ((p = strrchr (argv[i], '/')))
        !           114:       p++;
        !           115:     else
        !           116:       p = argv[i];
        !           117:     if (fd > 0)
        !           118:       printf ("  { %d, \"%s\" },\n", fd, p);
        !           119:   }
        !           120:   printf ("  { 0, \"\" },\n};\n\n"
        !           121:          "#define MAFD_MAX (sizeof (mafd) / sizeof (mafd[0]) - 1)\n");
        !           122:   return (0);
        !           123: }