All loggers were wrapping just the write in a lock with some
updating a counter. This moves the lock into the write function.
The log_ctx alerts counter was also removed as many modules have
stopped using this and the alert count is available elsewhere.
Should satisfy Coverity CID
1400798:
CID
1400798 (#1 of 1): Data race condition (MISSING_LOCK) 2.
missing_lock: Accessing log_ctx->rotation_flag without holding lock
LogFileCtx_.fp_mutex. Elsewhere, "LogFileCtx_.rotation_flag" is accessed
with LogFileCtx_.fp_mutex held 4 out of 5 times.
Which appears to be a false positive as all calls to SCLogFileWrite
were done under lock, but this will make it more explicit.
}
}
- SCMutexLock(&aft->file_ctx->fp_mutex);
aft->file_ctx->Write((const char *)MEMBUFFER_BUFFER(aft->buffer),
MEMBUFFER_OFFSET(aft->buffer), aft->file_ctx);
- aft->file_ctx->alerts += p->alerts.cnt;
- SCMutexUnlock(&aft->file_ctx->fp_mutex);
return TM_ECODE_OK;
}
PrintRawDataToBuffer(aft->buffer->buffer, &aft->buffer->offset, aft->buffer->size,
GET_PKT_DATA(p), GET_PKT_LEN(p));
- SCMutexLock(&aft->file_ctx->fp_mutex);
aft->file_ctx->Write((const char *)MEMBUFFER_BUFFER(aft->buffer),
MEMBUFFER_OFFSET(aft->buffer), aft->file_ctx);
- aft->file_ctx->alerts += p->alerts.cnt;
- SCMutexUnlock(&aft->file_ctx->fp_mutex);
return TM_ECODE_OK;
}
if (aft == NULL) {
return;
}
-
- SCLogInfo("(%s) Alerts %" PRIu64 "", tv->name, aft->file_ctx->alerts);
}
static void AlertDebugLogDeInitCtx(OutputCtx *output_ctx)
static inline void AlertFastLogOutputAlert(AlertFastLogThread *aft, char *buffer,
int alert_size)
{
- SCMutex *file_lock = &aft->file_ctx->fp_mutex;
/* Output the alert string and count alerts. Only need to lock here. */
- SCMutexLock(file_lock);
- aft->file_ctx->alerts++;
aft->file_ctx->Write(buffer, alert_size, aft->file_ctx);
- SCMutexUnlock(file_lock);
}
int AlertFastLogger(ThreadVars *tv, void *data, const Packet *p)
if (p->alerts.cnt == 0)
return TM_ECODE_OK;
+ /* Not sure if this mutex is needed around calls to syslog. */
SCMutexLock(&ast->file_ctx->fp_mutex);
- ast->file_ctx->alerts += p->alerts.cnt;
-
for (i = 0; i < p->alerts.cnt; i++) {
const PacketAlert *pa = &p->alerts.alerts[i];
if (unlikely(pa->s == NULL)) {
SCMutexLock(&ast->file_ctx->fp_mutex);
- ast->file_ctx->alerts += p->alerts.cnt;
-
for (i = 0; i < p->alerts.cnt; i++) {
const PacketAlert *pa = &p->alerts.alerts[i];
if (unlikely(pa->s == NULL)) {
SCMutexLock(&ast->file_ctx->fp_mutex);
- ast->file_ctx->alerts += p->alerts.cnt;
char temp_buf_hdr[512];
char temp_buf_pkt[65] = "";
char temp_buf_tail[32];
if (ast == NULL) {
return;
}
-
- SCLogInfo("(%s) Alerts %" PRIu64 "", tv->name, ast->file_ctx->alerts);
}
static int AlertSyslogCondition(ThreadVars *tv, const Packet *p)
? true : false;
if (truncate || file_ctx->rotation_flag) {
if (Unified2AlertRotateFile(aun, truncate) < 0) {
- aun->unified2alert_ctx->file_ctx->alerts += i;
SCMutexUnlock(&file_ctx->fp_mutex);
return -1;
}
}
if (Unified2Write(aun) != 1) {
- file_ctx->alerts += i;
SCMutexUnlock(&file_ctx->fp_mutex);
return -1;
}
ret = Unified2PacketTypeAlert(aun, p, phdr->event_id, stream);
if (ret != 1) {
SCLogError(SC_ERR_FWRITE, "Error: fwrite failed: %s", strerror(errno));
- aun->unified2alert_ctx->file_ctx->alerts += i;
SCMutexUnlock(&file_ctx->fp_mutex);
return -1;
}
fflush(aun->unified2alert_ctx->file_ctx->fp);
- aun->unified2alert_ctx->file_ctx->alerts++;
SCMutexUnlock(&file_ctx->fp_mutex);
}
? true : false;
if (truncate || file_ctx->rotation_flag) {
if (Unified2AlertRotateFile(aun, truncate) < 0) {
- file_ctx->alerts += i;
SCMutexUnlock(&file_ctx->fp_mutex);
return -1;
}
}
if (Unified2Write(aun) != 1) {
- file_ctx->alerts += i;
SCMutexUnlock(&file_ctx->fp_mutex);
return -1;
}
(pa->flags & (PACKET_ALERT_FLAG_STATE_MATCH|PACKET_ALERT_FLAG_STREAM_MATCH) ? 1 : 0) : 0;
ret = Unified2PacketTypeAlert(aun, p, event_id, stream);
if (ret != 1) {
- aun->unified2alert_ctx->file_ctx->alerts += i;
SCMutexUnlock(&file_ctx->fp_mutex);
return -1;
}
fflush(aun->unified2alert_ctx->file_ctx->fp);
- aun->unified2alert_ctx->file_ctx->alerts++;
SCMutexUnlock(&file_ctx->fp_mutex);
}
" [**] %s [**] %s:%" PRIu16 " -> %s:%" PRIu16 "\n",
record, srcip, sp, dstip, dp);
- SCMutexLock(&hlog->file_ctx->fp_mutex);
hlog->file_ctx->Write((const char *)MEMBUFFER_BUFFER(aft->buffer),
MEMBUFFER_OFFSET(aft->buffer), hlog->file_ctx);
- SCMutexUnlock(&hlog->file_ctx->fp_mutex);
}
static void LogAnswer(LogDnsLogThread *aft, char *timebuf, char *srcip, char *dstip, Port sp, Port dp, DNSTransaction *tx, DNSAnswerEntry *entry)
" [**] %s:%" PRIu16 " -> %s:%" PRIu16 "\n",
srcip, sp, dstip, dp);
- SCMutexLock(&hlog->file_ctx->fp_mutex);
hlog->file_ctx->Write((const char *)MEMBUFFER_BUFFER(aft->buffer),
MEMBUFFER_OFFSET(aft->buffer), hlog->file_ctx);
- SCMutexUnlock(&hlog->file_ctx->fp_mutex);
}
static int LogDnsLogger(ThreadVars *tv, void *data, const Packet *p,
aft->uri_cnt ++;
- SCMutexLock(&hlog->file_ctx->fp_mutex);
hlog->file_ctx->Write((const char *)MEMBUFFER_BUFFER(aft->buffer),
MEMBUFFER_OFFSET(aft->buffer), hlog->file_ctx);
- SCMutexUnlock(&hlog->file_ctx->fp_mutex);
end:
SCReturnInt(0);
}
}
- SCMutexLock(&aft->statslog_ctx->file_ctx->fp_mutex);
aft->statslog_ctx->file_ctx->Write((const char *)MEMBUFFER_BUFFER(aft->buffer),
MEMBUFFER_OFFSET(aft->buffer), aft->statslog_ctx->file_ctx);
- SCMutexUnlock(&aft->statslog_ctx->file_ctx->fp_mutex);
MemBufferReset(aft->buffer);
PrintRawDataToBuffer(aft->buffer->buffer, &aft->buffer->offset,
aft->buffer->size, (uint8_t *)data,data_len);
- SCMutexLock(&td->file_ctx->fp_mutex);
td->file_ctx->Write((const char *)MEMBUFFER_BUFFER(aft->buffer),
MEMBUFFER_OFFSET(aft->buffer), td->file_ctx);
- SCMutexUnlock(&td->file_ctx->fp_mutex);
}
SCReturnInt(TM_ECODE_OK);
}
aft->tls_cnt++;
- SCMutexLock(&hlog->file_ctx->fp_mutex);
hlog->file_ctx->Write((const char *)MEMBUFFER_BUFFER(aft->buffer),
MEMBUFFER_OFFSET(aft->buffer), hlog->file_ctx);
- SCMutexUnlock(&hlog->file_ctx->fp_mutex);
return 0;
}
*/
static int SCLogFileWrite(const char *buffer, int buffer_len, LogFileCtx *log_ctx)
{
+ SCMutexLock(&log_ctx->fp_mutex);
+
/* Check for rotation. */
if (log_ctx->rotation_flag) {
log_ctx->rotation_flag = 0;
}
}
+ SCMutexUnlock(&log_ctx->fp_mutex);
+
return ret;
}
{
/* append \n for files only */
MemBufferWriteString(buffer, "\n");
- SCMutexLock(&file_ctx->fp_mutex);
file_ctx->Write((const char *)MEMBUFFER_BUFFER(buffer),
MEMBUFFER_OFFSET(buffer), file_ctx);
- SCMutexUnlock(&file_ctx->fp_mutex);
}
#ifdef HAVE_LIBHIREDIS
else if (file_ctx->type == LOGFILE_TYPE_REDIS) {
uint64_t size_limit; /**< file size limit */
uint64_t size_current; /**< file current size */
- /* Alerts on the module (not on the file) */
- uint64_t alerts;
/* flag to avoid multiple threads printing the same stats */
uint8_t flags;