nghttpx: open_file_for_write: Use O_CLOEXEC flag

This commit is contained in:
Tatsuhiro Tsujikawa 2015-01-07 00:25:10 +09:00
parent 2fb3d5fd1f
commit d3a606e9d9
1 changed files with 9 additions and 3 deletions

View File

@ -29,6 +29,7 @@
#include <syslog.h> #include <syslog.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <cstring> #include <cstring>
@ -193,15 +194,20 @@ bool is_secure(const char *filename) {
} // namespace } // namespace
FILE *open_file_for_write(const char *filename) { 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) { if (f == nullptr) {
LOG(ERROR) << "Failed to open " << filename LOG(ERROR) << "Failed to open " << filename
<< " for writing. Cause: " << strerror(errno); << " for writing. Cause: " << strerror(errno);
return nullptr; return nullptr;
} }
util::make_socket_closeonexec(fileno(f));
return f; return f;
} }