From 7ea8037ee1a274cb450b0453736cc793c3022407 Mon Sep 17 00:00:00 2001 From: acesso Date: Thu, 22 Jan 2015 11:20:16 -0300 Subject: [PATCH] Use fcntl and FD_CLOEXEC if O_CLOEXEC is undefined Same reported at #87 but at src/shrpx_config.cc src/instead of util.cc --- src/shrpx_config.cc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index a752010a..ac32ae2d 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -242,8 +242,17 @@ read_tls_ticket_key_file(const std::vector &files) { } FILE *open_file_for_write(const char *filename) { +#if defined O_CLOEXEC auto fd = open(filename, O_WRONLY | O_CLOEXEC | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); +#else + auto fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); + + // We get race condition if execve is called at the same time. + if (fd != -1) { + util::make_socket_closeonexec(fd); + } +#endif if (fd == -1) { LOG(ERROR) << "Failed to open " << filename << " for writing. Cause: " << strerror(errno);