From d3a606e9d9a4979bed9edfb043e1c87fe481fa38 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 7 Jan 2015 00:25:10 +0900 Subject: [PATCH] nghttpx: open_file_for_write: Use O_CLOEXEC flag --- src/shrpx_config.cc | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index 2c102d8b..b7a865f9 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -193,15 +194,20 @@ bool is_secure(const char *filename) { } // namespace FILE *open_file_for_write(const char *filename) { - auto f = fopen(filename, "wb"); + auto fd = open(filename, O_WRONLY | O_CLOEXEC | O_CREAT | O_TRUNC, + S_IRUSR | S_IWUSR); + if (fd == -1) { + LOG(ERROR) << "Failed to open " << filename + << " for writing. Cause: " << strerror(errno); + return nullptr; + } + auto f = fdopen(fd, "wb"); if (f == nullptr) { LOG(ERROR) << "Failed to open " << filename << " for writing. Cause: " << strerror(errno); return nullptr; } - util::make_socket_closeonexec(fileno(f)); - return f; }