From: Jeff Lucovsky Date: Tue, 11 Aug 2020 12:17:12 +0000 (-0400) Subject: log: Log errors while writing log info X-Git-Tag: suricata-6.0.0-rc1~45 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=00f77f9643789fda6e4fa581fb43b02f4e90f093;p=thirdparty%2Fsuricata.git log: Log errors while writing log info This commit adds logic to log errors during output. Errors are logged once and the number of errors is maintained. --- diff --git a/src/util-logopenfile.c b/src/util-logopenfile.c index 3e85e605ac..1c1c4e4bdc 100644 --- a/src/util-logopenfile.c +++ b/src/util-logopenfile.c @@ -210,8 +210,17 @@ static int SCLogFileWriteNoLock(const char *buffer, int buffer_len, LogFileCtx * if (log_ctx->fp) { SCClearErrUnlocked(log_ctx->fp); - ret = SCFwriteUnlocked(buffer, buffer_len, 1, log_ctx->fp); - SCFflushUnlocked(log_ctx->fp); + if (1 != SCFwriteUnlocked(buffer, buffer_len, 1, log_ctx->fp)) { + /* Only the first error is logged */ + if (!log_ctx->output_errors) { + SCLogError(SC_ERR_LOG_OUTPUT, "%s error while writing to %s", + SCFerrorUnlocked(log_ctx->fp) ? strerror(errno) : "unknown error", + log_ctx->filename); + } + log_ctx->output_errors++; + } else { + SCFflushUnlocked(log_ctx->fp); + } } return ret; @@ -250,8 +259,17 @@ static int SCLogFileWrite(const char *buffer, int buffer_len, LogFileCtx *log_ct if (log_ctx->fp) { clearerr(log_ctx->fp); - ret = fwrite(buffer, buffer_len, 1, log_ctx->fp); - fflush(log_ctx->fp); + if (1 != fwrite(buffer, buffer_len, 1, log_ctx->fp)) { + /* Only the first error is logged */ + if (!log_ctx->output_errors) { + SCLogError(SC_ERR_LOG_OUTPUT, "%s error while writing to %s", + ferror(log_ctx->fp) ? strerror(errno) : "unknown error", + log_ctx->filename); + } + log_ctx->output_errors++; + } else { + fflush(log_ctx->fp); + } } } @@ -285,6 +303,11 @@ static void SCLogFileCloseNoLock(LogFileCtx *log_ctx) { if (log_ctx->fp) fclose(log_ctx->fp); + + if (log_ctx->output_errors) { + SCLogError(SC_ERR_LOG_OUTPUT, "There were %" PRIu64 " output errors to %s", + log_ctx->output_errors, log_ctx->filename); + } } static void SCLogFileClose(LogFileCtx *log_ctx) diff --git a/src/util-logopenfile.h b/src/util-logopenfile.h index 992c64f8d7..cc62802264 100644 --- a/src/util-logopenfile.h +++ b/src/util-logopenfile.h @@ -148,6 +148,8 @@ typedef struct LogFileCtx_ { /* Socket types may need to drop events to keep from blocking * Suricata. */ uint64_t dropped; + + uint64_t output_errors; } LogFileCtx; /* Min time (msecs) before trying to reconnect a Unix domain socket */