1
0

closefrom.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef lint
  45. static const char rcsid[] = "$Sudo: closefrom.c,v 1.6 2004/06/01 20:51:56 millert Exp $";
  46. #endif /* lint */
  47. /*
  48. * Close all file descriptors greater than or equal to lowfd.
  49. */
  50. void
  51. closefrom(int lowfd)
  52. {
  53. long fd, maxfd;
  54. #if defined(HAVE_DIRFD) && defined(HAVE_PROC_PID)
  55. char fdpath[PATH_MAX], *endp;
  56. struct dirent *dent;
  57. DIR *dirp;
  58. int len;
  59. /* Check for a /proc/$$/fd directory. */
  60. len = snprintf(fdpath, sizeof(fdpath), "/proc/%ld/fd", (long)getpid());
  61. if (len != -1 && len <= sizeof(fdpath) && (dirp = opendir(fdpath))) {
  62. while ((dent = readdir(dirp)) != NULL) {
  63. fd = strtol(dent->d_name, &endp, 10);
  64. if (dent->d_name != endp && *endp == '\0' &&
  65. fd >= 0 && fd < INT_MAX && fd >= lowfd && fd != dirfd(dirp))
  66. (void) close((int) fd);
  67. }
  68. (void) closedir(dirp);
  69. } else
  70. #endif
  71. {
  72. /*
  73. * Fall back on sysconf() or getdtablesize(). We avoid checking
  74. * resource limits since it is possible to open a file descriptor
  75. * and then drop the rlimit such that it is below the open fd.
  76. */
  77. #ifdef HAVE_SYSCONF
  78. maxfd = sysconf(_SC_OPEN_MAX);
  79. #else
  80. maxfd = getdtablesize();
  81. #endif /* HAVE_SYSCONF */
  82. if (maxfd < 0)
  83. maxfd = OPEN_MAX;
  84. for (fd = lowfd; fd < maxfd; fd++)
  85. (void) close((int) fd);
  86. }
  87. }
  88. #endif /* HAVE_CLOSEFROM */