summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2024-11-20 16:14:51 (GMT)
committerBrad King <brad.king@kitware.com>2024-11-20 16:14:51 (GMT)
commit2ef3bc896b9e8ad94150ab61cd395c122745ba11 (patch)
treeae3d8569591b7d0dd7f59ddd0d3b266d99eaf4f7
parentd1d20ed397ad46d6959ca6eabe628efae8cc6e10 (diff)
parent60af429c5d71d4e159f4f5c129a46482252a924c (diff)
downloadCMake-2ef3bc896b9e8ad94150ab61cd395c122745ba11.zip
CMake-2ef3bc896b9e8ad94150ab61cd395c122745ba11.tar.gz
CMake-2ef3bc896b9e8ad94150ab61cd395c122745ba11.tar.bz2
Merge topic 'execute_process-OUTPUT_FILE-cloexec' into release-3.29
60af429c5d execute_process: Restore CLOEXEC on OUTPUT_FILE and ERROR_FILE descriptors Acked-by: Kitware Robot <kwrobot@kitware.com> Merge-request: !10014
-rw-r--r--Source/cmExecuteProcessCommand.cxx26
1 files changed, 23 insertions, 3 deletions
diff --git a/Source/cmExecuteProcessCommand.cxx b/Source/cmExecuteProcessCommand.cxx
index 2b923df..9b9ef0b 100644
--- a/Source/cmExecuteProcessCommand.cxx
+++ b/Source/cmExecuteProcessCommand.cxx
@@ -17,6 +17,12 @@
#include <cm3p/uv.h>
+#ifndef _WIN32
+# include <fcntl.h>
+
+# include "cm_fileno.hxx"
+#endif
+
#include "cmArgumentParser.h"
#include "cmExecutionStatus.h"
#include "cmList.h"
@@ -35,6 +41,20 @@ bool cmExecuteProcessCommandIsWhitespace(char c)
return (cmIsSpace(c) || c == '\n' || c == '\r');
}
+FILE* FopenCLOEXEC(std::string const& path, const char* mode)
+{
+ FILE* f = cmsys::SystemTools::Fopen(path, mode);
+#ifndef _WIN32
+ if (f) {
+ if (fcntl(cm_fileno(f), F_SETFD, FD_CLOEXEC) < 0) {
+ fclose(f);
+ f = nullptr;
+ }
+ }
+#endif
+ return f;
+}
+
void cmExecuteProcessCommandFixText(std::vector<char>& output,
bool strip_trailing_whitespace);
void cmExecuteProcessCommandAppend(std::vector<char>& output, const char* data,
@@ -178,7 +198,7 @@ bool cmExecuteProcessCommand(std::vector<std::string> const& args,
// Check the output variables.
std::unique_ptr<FILE, int (*)(FILE*)> inputFile(nullptr, fclose);
if (!inputFilename.empty()) {
- inputFile.reset(cmsys::SystemTools::Fopen(inputFilename, "rb"));
+ inputFile.reset(FopenCLOEXEC(inputFilename, "rb"));
if (inputFile) {
builder.SetExternalStream(cmUVProcessChainBuilder::Stream_INPUT,
inputFile.get());
@@ -189,7 +209,7 @@ bool cmExecuteProcessCommand(std::vector<std::string> const& args,
std::unique_ptr<FILE, int (*)(FILE*)> outputFile(nullptr, fclose);
if (!outputFilename.empty()) {
- outputFile.reset(cmsys::SystemTools::Fopen(outputFilename, "wb"));
+ outputFile.reset(FopenCLOEXEC(outputFilename, "wb"));
if (outputFile) {
builder.SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT,
outputFile.get());
@@ -211,7 +231,7 @@ bool cmExecuteProcessCommand(std::vector<std::string> const& args,
outputFile.get());
}
} else {
- errorFile.reset(cmsys::SystemTools::Fopen(errorFilename, "wb"));
+ errorFile.reset(FopenCLOEXEC(errorFilename, "wb"));
if (errorFile) {
builder.SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR,
errorFile.get());