17
17
18
18
#include < cm3p/uv.h>
19
19
20
+ #ifndef _WIN32
21
+ # include < fcntl.h>
22
+
23
+ # include " cm_fileno.hxx"
24
+ #endif
25
+
20
26
#include " cmArgumentParser.h"
21
27
#include " cmExecutionStatus.h"
22
28
#include " cmList.h"
@@ -35,6 +41,20 @@ bool cmExecuteProcessCommandIsWhitespace(char c)
35
41
return (cmIsSpace (c) || c == ' \n ' || c == ' \r ' );
36
42
}
37
43
44
+ FILE* FopenCLOEXEC (std::string const & path, const char * mode)
45
+ {
46
+ FILE* f = cmsys::SystemTools::Fopen (path, mode);
47
+ #ifndef _WIN32
48
+ if (f) {
49
+ if (fcntl (cm_fileno (f), F_SETFD, FD_CLOEXEC) < 0 ) {
50
+ fclose (f);
51
+ f = nullptr ;
52
+ }
53
+ }
54
+ #endif
55
+ return f;
56
+ }
57
+
38
58
void cmExecuteProcessCommandFixText (std::vector<char >& output,
39
59
bool strip_trailing_whitespace);
40
60
void cmExecuteProcessCommandAppend (std::vector<char >& output, const char * data,
@@ -178,7 +198,7 @@ bool cmExecuteProcessCommand(std::vector<std::string> const& args,
178
198
// Check the output variables.
179
199
std::unique_ptr<FILE, int (*)(FILE*)> inputFile (nullptr , fclose);
180
200
if (!inputFilename.empty ()) {
181
- inputFile.reset (cmsys::SystemTools::Fopen (inputFilename, " rb" ));
201
+ inputFile.reset (FopenCLOEXEC (inputFilename, " rb" ));
182
202
if (inputFile) {
183
203
builder.SetExternalStream (cmUVProcessChainBuilder::Stream_INPUT,
184
204
inputFile.get ());
@@ -189,7 +209,7 @@ bool cmExecuteProcessCommand(std::vector<std::string> const& args,
189
209
190
210
std::unique_ptr<FILE, int (*)(FILE*)> outputFile (nullptr , fclose);
191
211
if (!outputFilename.empty ()) {
192
- outputFile.reset (cmsys::SystemTools::Fopen (outputFilename, " wb" ));
212
+ outputFile.reset (FopenCLOEXEC (outputFilename, " wb" ));
193
213
if (outputFile) {
194
214
builder.SetExternalStream (cmUVProcessChainBuilder::Stream_OUTPUT,
195
215
outputFile.get ());
@@ -211,7 +231,7 @@ bool cmExecuteProcessCommand(std::vector<std::string> const& args,
211
231
outputFile.get ());
212
232
}
213
233
} else {
214
- errorFile.reset (cmsys::SystemTools::Fopen (errorFilename, " wb" ));
234
+ errorFile.reset (FopenCLOEXEC (errorFilename, " wb" ));
215
235
if (errorFile) {
216
236
builder.SetExternalStream (cmUVProcessChainBuilder::Stream_ERROR,
217
237
errorFile.get ());
0 commit comments