closefrom.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2004 Todd C. Miller <Todd.Miller@courtesan.com>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "common.h"
  17. #ifndef HAVE_CLOSEFROM
  18. #include <sys/types.h>
  19. #include <sys/param.h>
  20. #include <unistd.h>
  21. #include <stdio.h>
  22. #include <limits.h>
  23. #include <stdlib.h>
  24. #include <stddef.h>
  25. #ifdef HAVE_DIRENT_H
  26. # include <dirent.h>
  27. # define NAMLEN(dirent) strlen((dirent)->d_name)
  28. #else
  29. # define dirent direct
  30. # define NAMLEN(dirent) (dirent)->d_namlen
  31. # ifdef HAVE_SYS_NDIR_H
  32. # include <sys/ndir.h>
  33. # endif
  34. # ifdef HAVE_SYS_DIR_H
  35. # include <sys/dir.h>
  36. # endif
  37. # ifdef HAVE_NDIR_H
  38. # include <ndir.h>
  39. # endif
  40. #endif
  41. #ifndef OPEN_MAX
  42. # define OPEN_MAX 256
  43. #endif
  44. RCSID("$Id$");
  45. #ifndef lint
  46. static const char rcsid[] = "$Sudo: closefrom.c,v 1.6 2004/06/01 20:51:56 millert Exp $";
  47. #endif /* lint */
  48. /*
  49. * Close all file descriptors greater than or equal to lowfd.
  50. */
  51. void
  52. closefrom(int lowfd)
  53. {
  54. long fd, maxfd;
  55. #if defined(HAVE_DIRFD) && defined(HAVE_PROC_PID)
  56. char fdpath[PATH_MAX], *endp;
  57. struct dirent *dent;
  58. DIR *dirp;
  59. int len;
  60. /* Check for a /proc/$$/fd directory. */
  61. len = snprintf(fdpath, sizeof(fdpath), "/proc/%ld/fd", (long)getpid());
  62. if (len != -1 && len <= sizeof(fdpath) && (dirp = opendir(fdpath))) {
  63. while ((dent = readdir(dirp)) != NULL) {
  64. fd = strtol(dent->d_name, &endp, 10);
  65. if (dent->d_name != endp && *endp == '\0' &&
  66. fd >= 0 && fd < INT_MAX && fd >= lowfd && fd != dirfd(dirp))
  67. (void) close((int) fd);
  68. }
  69. (void) closedir(dirp);
  70. } else
  71. #endif
  72. {
  73. /*
  74. * Fall back on sysconf() or getdtablesize(). We avoid checking
  75. * resource limits since it is possible to open a file descriptor
  76. * and then drop the rlimit such that it is below the open fd.
  77. */
  78. #ifdef HAVE_SYSCONF
  79. maxfd = sysconf(_SC_OPEN_MAX);
  80. #else
  81. maxfd = getdtablesize();
  82. #endif /* HAVE_SYSCONF */
  83. if (maxfd < 0)
  84. maxfd = OPEN_MAX;
  85. for (fd = lowfd; fd < maxfd; fd++)
  86. (void) close((int) fd);
  87. }
  88. }
  89. #endif /* HAVE_CLOSEFROM */