Skip to content

Commit 2ef3bc8

Browse files
committed
Merge topic 'execute_process-OUTPUT_FILE-cloexec' into release-3.29
60af429 execute_process: Restore CLOEXEC on OUTPUT_FILE and ERROR_FILE descriptors Acked-by: Kitware Robot <[email protected]> Merge-request: !10014
2 parents d1d20ed + 60af429 commit 2ef3bc8

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

Source/cmExecuteProcessCommand.cxx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717

1818
#include <cm3p/uv.h>
1919

20+
#ifndef _WIN32
21+
# include <fcntl.h>
22+
23+
# include "cm_fileno.hxx"
24+
#endif
25+
2026
#include "cmArgumentParser.h"
2127
#include "cmExecutionStatus.h"
2228
#include "cmList.h"
@@ -35,6 +41,20 @@ bool cmExecuteProcessCommandIsWhitespace(char c)
3541
return (cmIsSpace(c) || c == '\n' || c == '\r');
3642
}
3743

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+
3858
void cmExecuteProcessCommandFixText(std::vector<char>& output,
3959
bool strip_trailing_whitespace);
4060
void cmExecuteProcessCommandAppend(std::vector<char>& output, const char* data,
@@ -178,7 +198,7 @@ bool cmExecuteProcessCommand(std::vector<std::string> const& args,
178198
// Check the output variables.
179199
std::unique_ptr<FILE, int (*)(FILE*)> inputFile(nullptr, fclose);
180200
if (!inputFilename.empty()) {
181-
inputFile.reset(cmsys::SystemTools::Fopen(inputFilename, "rb"));
201+
inputFile.reset(FopenCLOEXEC(inputFilename, "rb"));
182202
if (inputFile) {
183203
builder.SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT,
184204
inputFile.get());
@@ -189,7 +209,7 @@ bool cmExecuteProcessCommand(std::vector<std::string> const& args,
189209

190210
std::unique_ptr<FILE, int (*)(FILE*)> outputFile(nullptr, fclose);
191211
if (!outputFilename.empty()) {
192-
outputFile.reset(cmsys::SystemTools::Fopen(outputFilename, "wb"));
212+
outputFile.reset(FopenCLOEXEC(outputFilename, "wb"));
193213
if (outputFile) {
194214
builder.SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT,
195215
outputFile.get());
@@ -211,7 +231,7 @@ bool cmExecuteProcessCommand(std::vector<std::string> const& args,
211231
outputFile.get());
212232
}
213233
} else {
214-
errorFile.reset(cmsys::SystemTools::Fopen(errorFilename, "wb"));
234+
errorFile.reset(FopenCLOEXEC(errorFilename, "wb"));
215235
if (errorFile) {
216236
builder.SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR,
217237
errorFile.get());

0 commit comments

Comments
 (0)