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

Annotation of src/usr.bin/ssh/minfd.c, Revision 1.2

1.1       deraadt     1: /*
                      2:
                      3: 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:25:30 1995 ylo
                     12:
                     13: */
                     14:
                     15: #include "includes.h"
1.2     ! deraadt    16: RCSID("$Id: minfd.c,v 1.1 1999/09/26 20:53:36 deraadt Exp $");
1.1       deraadt    17:
                     18: #include <sys/resource.h> /* Needed by fdlim.h */
                     19: #include "fdlim.h"
                     20: #include "minfd.h"
                     21:
                     22: static int
                     23: _get_permanent_fd(const char *shellpath)
                     24: {
                     25:   const char *shell;
                     26:   struct passwd *pwd;
                     27:   int fdmin;
                     28:   int fdlim;
                     29:   int fd;
                     30:   int i;
                     31:
                     32:   if (!shellpath)
                     33:     {
                     34:       if (!shellpath)
                     35:        shellpath = getenv("SHELL");
                     36:       if (!shellpath)
                     37:        if ((pwd = getpwuid(getuid())))
                     38:          shellpath = pwd->pw_shell;
                     39:       if (!shellpath)
1.2     ! deraadt    40:        shellpath = _PATH_BSHELL;
1.1       deraadt    41:     }
                     42:   if ((shell = strrchr(shellpath, '/')))
                     43:     shell++;
                     44:   else
                     45:     shell = shellpath;
                     46:
                     47:   for (i = 0; strcmp(mafd[i].shell, shell); i++)
                     48:     if (i == MAFD_MAX - 1)
                     49:       return -1;
                     50:
                     51:   fdmin = mafd[i].fd;
                     52:   fdlim = fdlim_get(0);
                     53:
                     54:   if (fdmin < fdlim)
                     55:     {
                     56:       /* First try to find a file descriptor as high as possible without
                     57:         upping the limit */
                     58:       fd = fdlim - 1;
                     59:       while (fd >= fdmin)
                     60:        {
                     61:          if (fcntl(fd, F_GETFL, NULL) < 0)
                     62:            return fd;
                     63:          fd--;
                     64:        }
                     65:     }
                     66:
                     67:   fd = fdlim;
                     68:   for (;;)
                     69:     {
                     70:       if (fdlim_set(fd + 1) < 0)
                     71:        return -1;
                     72:       if (fcntl(fd, F_GETFL, NULL) < 0)
                     73:        break;
                     74:       fd++;
                     75:     }
                     76:   return fd;
                     77: }
                     78:
                     79: int
                     80: get_permanent_fd(const char *shellpath)
                     81: {
                     82:   static int fd = -2;
                     83:
                     84:   if (fd >= -1)
                     85:     return fd;
                     86:   fd = _get_permanent_fd(shellpath);
                     87:   if (fd < 0)
                     88:     fd = -1;
                     89:   return fd;
                     90: }