closefrom.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2004-2005 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. #ifndef HAVE_CLOSEFROM
  17. #include <sys/types.h>
  18. #include <sys/param.h>
  19. #include <unistd.h>
  20. #include <stdio.h>
  21. #ifdef HAVE_FCNTL_H
  22. # include <fcntl.h>
  23. #endif
  24. #include <limits.h>
  25. #include <stdlib.h>
  26. #include <stddef.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #ifdef HAVE_DIRENT_H
  30. # include <dirent.h>
  31. # define NAMLEN(dirent) strlen((dirent)->d_name)
  32. #else
  33. # define dirent direct
  34. # define NAMLEN(dirent) (dirent)->d_namlen
  35. # ifdef HAVE_SYS_NDIR_H
  36. # include <sys/ndir.h>
  37. # endif
  38. # ifdef HAVE_SYS_DIR_H
  39. # include <sys/dir.h>
  40. # endif
  41. # ifdef HAVE_NDIR_H
  42. # include <ndir.h>
  43. # endif
  44. #endif
  45. #include "tmux.h"
  46. #ifndef OPEN_MAX
  47. # define OPEN_MAX 256
  48. #endif
  49. #if 0
  50. __unused static const char rcsid[] = "$Sudo: closefrom.c,v 1.11 2006/08/17 15:26:54 millert Exp $";
  51. #endif /* lint */
  52. /*
  53. * Close all file descriptors greater than or equal to lowfd.
  54. */
  55. #ifdef HAVE_FCNTL_CLOSEM
  56. void
  57. closefrom(int lowfd)
  58. {
  59. (void) fcntl(lowfd, F_CLOSEM, 0);
  60. }
  61. #else
  62. void
  63. closefrom(int lowfd)
  64. {
  65. long fd, maxfd;
  66. #if defined(HAVE_DIRFD) && defined(HAVE_PROC_PID)
  67. char fdpath[PATH_MAX], *endp;
  68. struct dirent *dent;
  69. DIR *dirp;
  70. int len;
  71. /* Check for a /proc/$$/fd directory. */
  72. len = snprintf(fdpath, sizeof(fdpath), "/proc/%ld/fd", (long)getpid());
  73. if (len > 0 && (size_t)len <= sizeof(fdpath) && (dirp = opendir(fdpath))) {
  74. while ((dent = readdir(dirp)) != NULL) {
  75. fd = strtol(dent->d_name, &endp, 10);
  76. if (dent->d_name != endp && *endp == '\0' &&
  77. fd >= 0 && fd < INT_MAX && fd >= lowfd && fd != dirfd(dirp))
  78. (void) close((int) fd);
  79. }
  80. (void) closedir(dirp);
  81. } else
  82. #endif
  83. {
  84. /*
  85. * Fall back on sysconf() or getdtablesize(). We avoid checking
  86. * resource limits since it is possible to open a file descriptor
  87. * and then drop the rlimit such that it is below the open fd.
  88. */
  89. #ifdef HAVE_SYSCONF
  90. maxfd = sysconf(_SC_OPEN_MAX);
  91. #else
  92. maxfd = getdtablesize();
  93. #endif /* HAVE_SYSCONF */
  94. if (maxfd < 0)
  95. maxfd = OPEN_MAX;
  96. for (fd = lowfd; fd < maxfd; fd++)
  97. (void) close((int) fd);
  98. }
  99. }
  100. #endif /* !HAVE_FCNTL_CLOSEM */
  101. #endif /* HAVE_CLOSEFROM */