Blame


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