2 * Copyright (c) 2004-2005 Todd C. Miller <Todd.Miller@courtesan.com>
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.
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.
19 #ifndef HAVE_CLOSEFROM
21 #include <sys/types.h>
22 #include <sys/param.h>
35 # define NAMLEN(dirent) strlen((dirent)->d_name)
37 # define dirent direct
38 # define NAMLEN(dirent) (dirent)->d_namlen
39 # ifdef HAVE_SYS_NDIR_H
40 # include <sys/ndir.h>
42 # ifdef HAVE_SYS_DIR_H
55 __unused
static const char rcsid
[] = "$Sudo: closefrom.c,v 1.11 2006/08/17 15:26:54 millert Exp $";
59 * Close all file descriptors greater than or equal to lowfd.
61 #ifdef HAVE_FCNTL_CLOSEM
65 (void) fcntl(lowfd
, F_CLOSEM
, 0);
72 #if defined(HAVE_DIRFD) && defined(HAVE_PROC_PID)
73 char fdpath
[PATH_MAX
], *endp
;
78 /* Check for a /proc/$$/fd directory. */
79 len
= snprintf(fdpath
, sizeof(fdpath
), "/proc/%ld/fd", (long)getpid());
80 if (len
> 0 && (size_t)len
<= sizeof(fdpath
) && (dirp
= opendir(fdpath
))) {
81 while ((dent
= readdir(dirp
)) != NULL
) {
82 fd
= strtol(dent
->d_name
, &endp
, 10);
83 if (dent
->d_name
!= endp
&& *endp
== '\0' &&
84 fd
>= 0 && fd
< INT_MAX
&& fd
>= lowfd
&& fd
!= dirfd(dirp
))
85 (void) close((int) fd
);
87 (void) closedir(dirp
);
92 * Fall back on sysconf() or getdtablesize(). We avoid checking
93 * resource limits since it is possible to open a file descriptor
94 * and then drop the rlimit such that it is below the open fd.
97 maxfd
= sysconf(_SC_OPEN_MAX
);
99 maxfd
= getdtablesize();
100 #endif /* HAVE_SYSCONF */
104 for (fd
= lowfd
; fd
< maxfd
; fd
++)
105 (void) close((int) fd
);
108 #endif /* !HAVE_FCNTL_CLOSEM */
109 #endif /* HAVE_CLOSEFROM */