summaryrefslogtreecommitdiffstats
path: root/src/build_log.cc
diff options
context:
space:
mode:
authorEvan Martin <martine@danga.com>2011-06-30 17:31:25 (GMT)
committerEvan Martin <martine@danga.com>2011-06-30 17:31:25 (GMT)
commit5b25c9b8235468b0858cefedf68fa9a03b47ef55 (patch)
tree8ad2d99b786e84acf5d1a01a1ece9c38aebf4c31 /src/build_log.cc
parentdf560ebcacc7d69c532add1c4b834efafda55db7 (diff)
downloadNinja-5b25c9b8235468b0858cefedf68fa9a03b47ef55.zip
Ninja-5b25c9b8235468b0858cefedf68fa9a03b47ef55.tar.gz
Ninja-5b25c9b8235468b0858cefedf68fa9a03b47ef55.tar.bz2
don't leak the build_log file descriptor into subprocesses
Closes issue #74.
Diffstat (limited to 'src/build_log.cc')
-rw-r--r--src/build_log.cc17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/build_log.cc b/src/build_log.cc
index 8c45c77..c3ae1ab 100644
--- a/src/build_log.cc
+++ b/src/build_log.cc
@@ -15,6 +15,7 @@
#include "build_log.h"
#include <errno.h>
+#include <fcntl.h>
#include <stdio.h>
#include <string.h>
@@ -34,6 +35,21 @@ namespace {
const char kFileSignature[] = "# ninja log v2\n";
const int kCurrentVersion = 2;
+void SetCloseOnExec(FILE* file) {
+#ifndef _WIN32
+ int flags = fcntl(fileno(file), F_GETFD);
+ if (flags < 0) {
+ perror("fcntl(F_GETFD)");
+ } else {
+ if (fcntl(fileno(file), F_SETFD, flags | FD_CLOEXEC) < 0)
+ perror("fcntl(F_SETFD)");
+ }
+#else
+ // On Windows, handles must be explicitly marked to be passed to a
+ // spawned process, so there's nothing to do here.
+#endif // WIN32
+}
+
}
BuildLog::BuildLog()
@@ -55,6 +71,7 @@ bool BuildLog::OpenForWrite(const string& path, string* err) {
return false;
}
setvbuf(log_file_, NULL, _IOLBF, BUFSIZ);
+ SetCloseOnExec(log_file_);
if (ftell(log_file_) == 0) {
if (fwrite(kFileSignature, sizeof(kFileSignature) - 1, 1, log_file_) < 1) {