From bcbb2e8649fb36bb1e38449506fbb4411e5d3847 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 8 Oct 2014 23:44:38 +0900 Subject: [PATCH] src: Use fcntl and FD_CLOEXEC if O_CLOEXEC is undefined We may run into race condition if execve is called at the same time when fcntl is called. But we just does this for now to compile nghttp2 applications under older kernel. --- src/util.cc | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/util.cc b/src/util.cc index 14a7ed07..9fa93326 100644 --- a/src/util.cc +++ b/src/util.cc @@ -539,10 +539,21 @@ int reopen_log_file(const char *path) fd = open(path, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP); } -#else // !__ANDROID__ && !ANDROID +#elif defined O_CLOEXEC + auto fd = open(path, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP); -#endif // !__ANDROID__ && !ANDROID +#else // !O_CLOEXEC + + auto fd = open(path, O_WRONLY | O_APPEND | O_CREAT, + S_IRUSR | S_IWUSR | S_IRGRP); + + // We get race condition if execve is called at the same time. + if(fd != -1) { + fcntl(fd, F_SETFD, FD_CLOEXEC); + } + +#endif // !O_CLOEXEC if(fd == -1) { return -1;