-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathclose_handles.cpp
More file actions
226 lines (166 loc) · 5.45 KB
/
close_handles.cpp
File metadata and controls
226 lines (166 loc) · 5.45 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (c) 2022 Klemens D. Morgenstern (klemens dot morgenstern at gmx dot net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/process/v2/detail/config.hpp>
#if defined(BOOST_PROCESS_V2_POSIX)
#include <boost/process/v2/detail/last_error.hpp>
#include <boost/process/v2/posix/detail/close_handles.hpp>
#include <algorithm>
#include <memory>
// linux has close_range since 5.19
#if defined(__FreeBSD__)
// https://www.freebsd.org/cgi/man.cgi?query=close_range&apropos=0&sektion=0&manpath=FreeBSD+13.1-RELEASE+and+Ports&arch=default&format=html
// __FreeBSD__
//
// gives us
//
// int closefrom(int fd);
// int close_range(u_int lowfd, u_int highfd, int flags);
#include <unistd.h>
#define BOOST_PROCESS_V2_HAS_CLOSE_RANGE_AND_CLOSEFROM 1
#elif defined(__sun)
/*https://docs.oracle.com/cd/E36784_01/html/E36874/closefrom-3c.html
int fdwalk(int (*func)(void *, int), void *cd);
*/
#include <stdlib.h>
#define BOOST_PROCESS_V2_HAS_PDFORK 1
#elif defined(__linux__)
#include <linux/version.h>
// kernel has close_range
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,9,0) && !defined(BOOST_PROCESS_V2_POSIX_FORCE_DISABLE_CLOSE_RANGE)
// version is included by stdlib.h #include <gnu/libc-version.h>
#if (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 34) // glibc is compiled with close_range support
// https://man7.org/linux/man-pages/man2/close_range.2.html
#include <linux/close_range.h>
#define BOOST_PROCESS_V2_HAS_CLOSE_RANGE 1
#else
#include <sys/syscall.h>
#if defined(SYS_close_range)
#define BOOST_PROCESS_V2_HAS_CLOSE_RANGE 1
#if !defined(CLOSE_RANGE_UNSHARE)
#define CLOSE_RANGE_UNSHARE 2
#endif
int close_range(unsigned int first, unsigned int last, int flags)
{
return ::syscall(SYS_close_range, first, last, flags);
}
#else
#include <dirent.h>
#endif
#endif
#else
#include <dirent.h>
#endif
#else
#include <dirent.h>
#endif
BOOST_PROCESS_V2_BEGIN_NAMESPACE
namespace posix
{
namespace detail
{
#if defined(BOOST_PROCESS_V2_HAS_PDFORK)
void close_all(const std::vector<int> & whitelist, error_code & ec)
{
fdwalk(+[](void * p, int fd)
{
const auto & wl = *static_cast<const std::vector<int>*>(p);
if (std::find(wl.begin(), wl.end(), fd) == wl.end())
return ::close(fd);
else
return 0;
}, const_cast<void*>(static_cast<const void*>(&whitelist)) );
ec = BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error();
}
#elif defined(BOOST_PROCESS_V2_HAS_CLOSE_RANGE_AND_CLOSEFROM)
// freeBSD impl - whitelist must be ordered
void close_all(const std::vector<int> & whitelist, error_code & ec)
{
//the most common scenario is whitelist = {0,1,2}
if (!whitelist.empty())
{
if (whitelist.front() != 0)
::close_range(0, whitelist.front() - 1, 0);
for (std::size_t idx = 0u;
idx < (whitelist.size() - 1u);
idx++)
{
const auto mine = whitelist[idx];
const auto next = whitelist[idx + 1];
if ((mine + 1) != next && (mine != next))
{
::close_range(mine + 1, next - 1, 0);
}
}
::closefrom(whitelist.back() + 1);
}
else
::closefrom(0);
}
#elif defined(BOOST_PROCESS_V2_HAS_CLOSE_RANGE)
// linux impl - whitelist must be ordered
void close_all(const std::vector<int> & whitelist, error_code & /*ec*/)
{
// https://patchwork.kernel.org/project/linux-fsdevel/cover/20200602204219.186620-1-christian.brauner@ubuntu.com/
//the most common scenario is whitelist = {0,1,2}
if (!whitelist.empty())
{
if (whitelist.front() != 0)
::close_range(0, whitelist.front() - 1, CLOSE_RANGE_UNSHARE);
for (std::size_t idx = 0u;
idx < (whitelist.size() - 1u);
idx++)
{
const auto mine = whitelist[idx];
const auto next = whitelist[idx + 1];
if ((mine + 1) != next && (mine != next))
{
::close_range(mine + 1, next - 1, CLOSE_RANGE_UNSHARE);
}
}
::close_range(whitelist.back() + 1, std::numeric_limits<int>::max(), CLOSE_RANGE_UNSHARE);
}
else
::close_range(0, std::numeric_limits<int>::max(), CLOSE_RANGE_UNSHARE);
}
#else
// default one
void close_all(const std::vector<int> & whitelist, error_code & ec)
{
std::unique_ptr<DIR, void(*)(DIR*)> dir{::opendir("/dev/fd"), +[](DIR* p){::closedir(p);}};
if (dir.get() == nullptr)
{
dir = {::opendir("/proc/self/fd"), +[](DIR* p){::closedir(p);}};
if (dir.get() == nullptr)
{
ec = BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error();
return ;
}
}
auto dir_fd = dirfd(dir.get());
if (dir_fd == -1)
{
ec = BOOST_PROCESS_V2_NAMESPACE::detail::get_last_error();
return ;
}
struct ::dirent * ent_p;
while ((ent_p = ::readdir(dir.get())) != nullptr)
{
if (ent_p->d_name[0] == '.')
continue;
const auto conv = std::atoi(ent_p->d_name);
if (conv == 0 && (ent_p->d_name[0] != '0' && ent_p->d_name[1] != '\0'))
continue;
if (conv == dir_fd
|| (std::find(whitelist.begin(), whitelist.end(), conv) != whitelist.end()))
continue;
::close(conv);
}
}
#endif
}
}
BOOST_PROCESS_V2_END_NAMESPACE
#endif