Adds mutex around the write operation on the auditlogs

This commit is contained in:
Felipe Zimmerle 2016-01-20 10:21:21 -03:00
parent b4691aa748
commit 9343942398

View File

@ -23,6 +23,7 @@
#include <fcntl.h>
#include <fstream>
#include <mutex>
#include "audit_log/audit_log.h"
#include "modsecurity/transaction.h"
@ -33,6 +34,9 @@ namespace modsecurity {
namespace audit_log {
namespace writer {
std::mutex g_writeMutex;
Parallel::~Parallel() {
if (log1.is_open()) {
log1.close();
@ -91,6 +95,7 @@ bool Parallel::init() {
bool Parallel::write(Transaction *transaction, int parts) {
std::lock_guard<std::mutex> guard(g_writeMutex);
FILE *fp;
int fd;
std::string log = transaction->toJSON(parts);
@ -124,14 +129,17 @@ bool Parallel::write(Transaction *transaction, int parts) {
if (log1.is_open() && log2.is_open()) {
log2 << transaction->toOldAuditLogFormatIndex(fileName, log.length(),
md5(log));
log2.flush();
}
if (log1.is_open() && !log2.is_open()) {
log1 << transaction->toOldAuditLogFormatIndex(fileName, log.length(),
md5(log));
log1.flush();
}
if (!log1.is_open() && log2.is_open()) {
log2 << transaction->toOldAuditLogFormatIndex(fileName, log.length(),
md5(log));
log2.flush();
}
return true;