util/log: fix log file rotation

Fix double log file rotation in presence of both rotation flag and rotate interval

Ticket: https://redmine.openinfosecfoundation.org/issues/8153
pull/14639/head
Abhijeet Singh 7 months ago committed by Victor Julien
parent 5f92a6cac3
commit b145e389ab

@ -206,30 +206,44 @@ static void SCLogFileFlush(LogFileCtx *log_ctx)
}
/**
* \brief Write buffer to log file.
* \retval 0 on failure; otherwise, the return value of fwrite_unlocked (number of
* characters successfully written).
* \brief Handle log file rotation checks and updates
* \param log_ctx Log file context
* \retval true if rotation occurred
*/
static int SCLogFileWriteNoLock(const char *buffer, int buffer_len, LogFileCtx *log_ctx)
static bool HandleLogRotation(LogFileCtx *log_ctx)
{
int ret = 0;
DEBUG_VALIDATE_BUG_ON(log_ctx->is_sock);
/* Check for rotation. */
if (log_ctx->rotation_flag) {
log_ctx->rotation_flag = 0;
SCConfLogReopen(log_ctx);
}
if (log_ctx->flags & LOGFILE_ROTATE_INTERVAL) {
if (log_ctx->flags & LOGFILE_ROTATE_INTERVAL) {
log_ctx->rotate_time = time(NULL) + log_ctx->rotate_interval;
}
return true;
} else if (log_ctx->flags & LOGFILE_ROTATE_INTERVAL) {
time_t now = time(NULL);
if (now >= log_ctx->rotate_time) {
SCConfLogReopen(log_ctx);
log_ctx->rotate_time = now + log_ctx->rotate_interval;
return true;
}
}
return false;
}
/**
* \brief Write buffer to log file.
* \retval 0 on failure; otherwise, the return value of fwrite_unlocked (number of
* characters successfully written).
*/
static int SCLogFileWriteNoLock(const char *buffer, int buffer_len, LogFileCtx *log_ctx)
{
int ret = 0;
DEBUG_VALIDATE_BUG_ON(log_ctx->is_sock);
HandleLogRotation(log_ctx);
if (log_ctx->fp) {
SCClearErrUnlocked(log_ctx->fp);
if (1 != SCFwriteUnlocked(buffer, buffer_len, 1, log_ctx->fp)) {

Loading…
Cancel
Save