1
0
Fork 0

fix file descriptor issue in LogAppenderFile::reopenAll (#4116)

This commit is contained in:
Jan 2017-12-21 14:11:29 +01:00 committed by GitHub
parent efa75d345c
commit 3b5e31252d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions

View File

@ -35,7 +35,7 @@
using namespace arangodb; using namespace arangodb;
using namespace arangodb::basics; using namespace arangodb::basics;
std::vector<std::pair<int, std::string>> LogAppenderFile::_fds = {}; std::vector<std::tuple<int, std::string, LogAppenderFile*>> LogAppenderFile::_fds = {};
LogAppenderStream::LogAppenderStream(std::string const& filename, LogAppenderStream::LogAppenderStream(std::string const& filename,
std::string const& filter, int fd) std::string const& filter, int fd)
@ -87,9 +87,9 @@ LogAppenderFile::LogAppenderFile(std::string const& filename, std::string const&
// logging to an actual file // logging to an actual file
size_t pos = 0; size_t pos = 0;
for (auto& it : _fds) { for (auto& it : _fds) {
if (it.second == _filename) { if (std::get<1>(it) == _filename) {
// already have an appender for the same file // already have an appender for the same file
_fd = it.first; _fd = std::get<0>(it);
break; break;
} }
++pos; ++pos;
@ -109,7 +109,7 @@ LogAppenderFile::LogAppenderFile(std::string const& filename, std::string const&
THROW_ARANGO_EXCEPTION(TRI_ERROR_CANNOT_WRITE_FILE); THROW_ARANGO_EXCEPTION(TRI_ERROR_CANNOT_WRITE_FILE);
} }
_fds.emplace_back(std::make_pair(fd, _filename)); _fds.emplace_back(std::make_tuple(fd, _filename, this));
_fd = fd; _fd = fd;
} }
} }
@ -160,8 +160,8 @@ std::string LogAppenderFile::details() {
void LogAppenderFile::reopenAll() { void LogAppenderFile::reopenAll() {
for (auto& it : _fds) { for (auto& it : _fds) {
int old = it.first; int old = std::get<0>(it);
std::string const& filename = it.second; std::string const& filename = std::get<1>(it);
if (filename.empty()) { if (filename.empty()) {
continue; continue;
@ -192,7 +192,10 @@ void LogAppenderFile::reopenAll() {
FileUtils::remove(backup); FileUtils::remove(backup);
} }
it.first = fd; // update the file descriptor in the map
std::get<0>(it) = fd;
// and also tell the appender of the file descriptor change
std::get<2>(it)->updateFd(fd);
if (old > STDERR_FILENO) { if (old > STDERR_FILENO) {
TRI_TRACKED_CLOSE_FILE(old); TRI_TRACKED_CLOSE_FILE(old);
@ -202,8 +205,11 @@ void LogAppenderFile::reopenAll() {
void LogAppenderFile::closeAll() { void LogAppenderFile::closeAll() {
for (auto& it : _fds) { for (auto& it : _fds) {
int fd = it.first; int fd = std::get<0>(it);
it.first = -1; // set the fd to "disabled"
std::get<0>(it) = -1;
// and also tell the appender of the file descriptor change
std::get<2>(it)->updateFd(-1);
if (fd > STDERR_FILENO) { if (fd > STDERR_FILENO) {
fsync(fd); fsync(fd);

View File

@ -38,6 +38,8 @@ class LogAppenderStream : public LogAppender {
virtual std::string details() override = 0; virtual std::string details() override = 0;
protected: protected:
void updateFd(int fd) { _fd = fd; }
virtual void writeLogMessage(LogLevel, char const*, size_t) = 0; virtual void writeLogMessage(LogLevel, char const*, size_t) = 0;
/// @brief maximum size for reusable log buffer /// @brief maximum size for reusable log buffer
@ -73,7 +75,7 @@ class LogAppenderFile : public LogAppenderStream {
static void closeAll(); static void closeAll();
private: private:
static std::vector<std::pair<int, std::string>> _fds; static std::vector<std::tuple<int, std::string, LogAppenderFile*>> _fds;
std::string _filename; std::string _filename;
}; };