/
fork_utils.cpp
52 lines (44 loc) · 1.27 KB
/
fork_utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "condor_common.h"
#include "condor_debug.h"
#include <set>
#include "fork_utils.h"
#if defined(LINUX)
bool
close_all_fds_quickly( const std::set<int> & exceptions ) {
DIR * dir = opendir( "/proc/self/fd" );
if( dir == NULL ) { return false; }
std::vector<int> fds;
char * endptr = NULL;
struct dirent * d = NULL;
while( (d = readdir(dir)) != NULL ) {
if( strcmp( d->d_name, "." ) == 0 || strcmp( d->d_name, ".." ) == 0 ) { continue; }
int fd = (int)strtol( d->d_name, & endptr, 10 );
ASSERT( * endptr == '\0' );
if(exceptions.count(fd) == 0) {
fds.push_back(fd);
}
}
// We buffer the FDs to close primarily to avoid disturbing the
// directory while we're iterating over it, but it also means that
// we don't accidentally close dir's underlying FD early. Since
// we're ignoring close() failures anyway, don't bother to exclude
// the FD we're about to close here from the loop below.
(void)closedir(dir);
for( auto i : fds ) {
(void)close( i );
}
return true;
}
#endif
void
close_all_fds( const std::set<int> & exceptions ) {
#if defined(LINUX)
if(close_all_fds_quickly( exceptions )) { return; }
#endif /* defined(LINUX) */
int limit = getdtablesize();
for (int jj=3; jj < limit; jj++) {
if(exceptions.count(jj) == 0) {
close(jj);
}
}
}