From: Joel Rosdahl Date: Fri, 31 Jul 2020 18:07:06 +0000 (+0200) Subject: Move set_cloexec_flag to Util X-Git-Tag: v4.0~240 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c0b71515672fbb5a025a64ea4ecf4f6125d2d072;p=thirdparty%2Fccache.git Move set_cloexec_flag to Util --- diff --git a/src/TemporaryFile.cpp b/src/TemporaryFile.cpp index 8dad5cf99..e057c795f 100644 --- a/src/TemporaryFile.cpp +++ b/src/TemporaryFile.cpp @@ -72,7 +72,7 @@ TemporaryFile::TemporaryFile(string_view path_prefix) fatal("Failed to create temporary file for {}: {}", path, strerror(errno)); } - set_cloexec_flag(*fd); + Util::set_cloexec_flag(*fd); #ifndef _WIN32 fchmod(*fd, 0666 & ~get_umask()); #endif diff --git a/src/Util.cpp b/src/Util.cpp index f18bed0d0..23bb4839a 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -1165,6 +1165,19 @@ send_to_stderr(const std::string& text, bool strip_colors) } } +void +set_cloexec_flag(int fd) +{ +#ifndef _WIN32 + int flags = fcntl(fd, F_GETFD, 0); + if (flags >= 0) { + fcntl(fd, F_SETFD, flags | FD_CLOEXEC); + } +#else + (void)fd; +#endif +} + void setenv(const std::string& name, const std::string& value) { diff --git a/src/Util.hpp b/src/Util.hpp index 2c4a8d249..b48ca5502 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -365,6 +365,9 @@ bool same_program_name(const std::string& program_name, // `strip_colors` is true. Throws `Error` on error. void send_to_stderr(const std::string& text, bool strip_colors); +// Set the FD_CLOEXEC on file descriptor `fd`. This is a NOP on Windows. +void set_cloexec_flag(int fd); + // Set environment variable `name` to `value`. void setenv(const std::string& name, const std::string& value); diff --git a/src/legacy_util.cpp b/src/legacy_util.cpp index bd1778443..b432f9b9c 100644 --- a/src/legacy_util.cpp +++ b/src/legacy_util.cpp @@ -31,19 +31,6 @@ # include #endif -void -set_cloexec_flag(int fd) -{ -#ifndef _WIN32 - int flags = fcntl(fd, F_GETFD, 0); - if (flags >= 0) { - fcntl(fd, F_SETFD, flags | FD_CLOEXEC); - } -#else - (void)fd; -#endif -} - double time_seconds() { diff --git a/src/legacy_util.hpp b/src/legacy_util.hpp index 182579acd..7f6a940b4 100644 --- a/src/legacy_util.hpp +++ b/src/legacy_util.hpp @@ -22,5 +22,4 @@ #include -void set_cloexec_flag(int fd); double time_seconds(); diff --git a/src/logging.cpp b/src/logging.cpp index 2ed326b3c..ed26788f7 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -89,7 +89,7 @@ init_log(const Config& config) logfile.open(logfile_path, "a"); #ifndef _WIN32 if (logfile) { - set_cloexec_flag(fileno(*logfile)); + Util::set_cloexec_flag(fileno(*logfile)); } #endif }