From 92db8115b77d2a63ceb8fa6741f5d7650c1ab299 Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 10 Dec 2016 01:43:43 +0200 Subject: [PATCH] option to prevent child processes from inheriting log file desciptors (#define SPDLOG_PREVENT_CHILD_FD) --- include/spdlog/details/os.h | 50 ++++++++++++++++++++++++------------- include/spdlog/tweakme.h | 6 ++++- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 56b8c330..e0f3d70f 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -11,10 +11,11 @@ #include #include #include +#include #include #include #include - +#include #ifdef _WIN32 @@ -26,34 +27,32 @@ #define WIN32_LEAN_AND_MEAN #endif #include -#include +#include // _get_pid support +#include // _get_osfhandle support #ifdef __MINGW32__ #include #endif -#include -#include +#else // unix -#elif __linux__ +#include +#include +#ifdef __linux__ #include //Use gettid() syscall under linux to get thread id -#include #elif __FreeBSD__ #include //Use thr_self() syscall under FreeBSD to get thread id -#include +#endif -#else -#include -#include - -#endif +#endif //unix #ifndef __has_feature // Clang - feature checking macros. #define __has_feature(x) 0 // Compatibility with non-clang compilers. #endif + namespace spdlog { namespace details @@ -143,6 +142,18 @@ inline bool operator!=(const std::tm& tm1, const std::tm& tm2) SPDLOG_CONSTEXPR static const char* eol = SPDLOG_EOL; SPDLOG_CONSTEXPR static int eol_size = sizeof(SPDLOG_EOL) - 1; +inline void prevent_child_fd(FILE *f) +{ +#ifdef _WIN32 + auto file_handle = (HANDLE)_get_osfhandle(_fileno(f)); + if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0)) + throw spdlog_ex("SetHandleInformation failed", errno); +#else + auto fd = fileno(f); + if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) + throw spdlog_ex("fcntl with FD_CLOEXEC failed", errno); +#endif +} //fopen_s on non windows for writing @@ -153,14 +164,19 @@ inline int fopen_s(FILE** fp, const filename_t& filename, const filename_t& mode *fp = _wfsopen((filename.c_str()), mode.c_str(), _SH_DENYWR); #else *fp = _fsopen((filename.c_str()), mode.c_str(), _SH_DENYWR); +#endif +#else //unix + *fp = fopen((filename.c_str()), mode.c_str()); +#endif + +#ifdef SPDLOG_PREVENT_CHILD_FD + if(*fp != nullptr) + prevent_child_fd(*fp); #endif - return *fp == nullptr; -#else - *fp = fopen((filename.c_str()), mode.c_str()); - return *fp == nullptr; -#endif + return *fp == nullptr; } + inline int remove(const filename_t &filename) { #if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h index 1af539bf..325fa5e6 100644 --- a/include/spdlog/tweakme.h +++ b/include/spdlog/tweakme.h @@ -100,4 +100,8 @@ // #define SPDLOG_ENABLE_SYSLOG /////////////////////////////////////////////////////////////////////////////// - +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to prevent child processes from inheriting log file desciptors +// +// #define SPDLOG_PREVENT_CHILD_FD +///////////////////////////////////////////////////////////////////////////////