Unverified Commit a9c3630d authored by dominicpoeschko's avatar dominicpoeschko Committed by GitHub

Properly handling SPDLOG_PREVENT_CHILD_FD

Using the SPDLOG_PREVENT_CHILD_FD option there where still a race when
a other thread was using fork and exec in between the call to fopen and fcntl.

Using open and O_CLOEXEC when possible prevents this race.

I have no idea if this problem  exists on Windows.
parent f3d99f41
...@@ -153,10 +153,26 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename ...@@ -153,10 +153,26 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename
*fp = ::_fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO); *fp = ::_fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
#endif #endif
#else // unix #else // unix
#if defined(SPDLOG_PREVENT_CHILD_FD) && (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L)
// prevent child processes from inheriting log file descriptors direcly on open
// so there is no race with a possible fork and exec
const int mode_flag = mode == SPDLOG_FILENAME_T("ab") ? O_APPEND : O_TRUNC;
const int fd = ::open((filename.c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644));
if(fd == -1)
{
return false;
}
*fp = ::fdopen(fd, mode.c_str());
if (*fp == nullptr)
{
::close(fd);
}
#else
*fp = ::fopen((filename.c_str()), mode.c_str()); *fp = ::fopen((filename.c_str()), mode.c_str());
#endif #endif
#endif
#ifdef SPDLOG_PREVENT_CHILD_FD #if defined(SPDLOG_PREVENT_CHILD_FD) && !(defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L)
// prevent child processes from inheriting log file descriptors // prevent child processes from inheriting log file descriptors
if (*fp != nullptr) if (*fp != nullptr)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment