GET_PKT_DATA(p), GET_PKT_LEN(p));
SCMutexLock(&aft->file_ctx->fp_mutex);
- (void)MemBufferPrintToFPAsString(aft->buffer, aft->file_ctx->fp);
- fflush(aft->file_ctx->fp);
+ 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);
if (SCConfLogOpenGeneric(conf, file_ctx, DEFAULT_LOG_FILENAME) < 0) {
goto error;
}
+ OutputRegisterFileRotationFlag(&file_ctx->rotation_flag);
OutputCtx *output_ctx = SCMalloc(sizeof(OutputCtx));
if (unlikely(output_ctx == NULL))
LogFileFreeCtx(logfile_ctx);
return NULL;
}
+ OutputRegisterFileRotationFlag(&logfile_ctx->rotation_flag);
OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
if (unlikely(output_ctx == NULL))
#include "util-privs.h"
#include "util-signal.h"
#include "unix-manager.h"
+#include "output.h"
/** \todo Get the default log directory from some global resource. */
#define SC_PERF_DEFAULT_LOG_FILENAME "stats.log"
return log_filename;
}
+/**
+ * \brief Reopen the log file.
+ *
+ * \retval 1 if successful, otherwise 0.
+ */
+static int SCPerfFileReopen(SCPerfOPIfaceContext *sc_perf_op_ctx)
+{
+ fclose(sc_perf_op_ctx->fp);
+ if ((sc_perf_op_ctx->fp = fopen(sc_perf_op_ctx->file, "w+")) == NULL) {
+ SCLogError(SC_ERR_FOPEN, "Failed to reopen file \"%s\"."
+ "Stats logging will now be disabled.",
+ sc_perf_op_ctx->file);
+ return 0;
+ }
+ return 1;
+}
+
/**
* \brief Initializes the output interface context
*
exit(EXIT_FAILURE);
}
}
+ else {
+ /* File opened, register for rotation notification. */
+ OutputRegisterFileRotationFlag(&sc_perf_op_ctx->rotation_flag);
+ }
/* init the lock used by SCPerfClubTMInst */
if (SCMutexInit(&sc_perf_op_ctx->pctmi_lock, NULL) != 0) {
return 0;
}
+ if (sc_perf_op_ctx->rotation_flag) {
+ SCLogDebug("Rotating log file");
+ sc_perf_op_ctx->rotation_flag = 0;
+ if (!SCPerfFileReopen(sc_perf_op_ctx)) {
+ /* Rotation failed, error already logged. */
+ return 0;
+ }
+ }
+
memset(&tval, 0, sizeof(struct timeval));
gettimeofday(&tval, NULL);
SCPerfClubTMInst *pctmi;
SCMutex pctmi_lock;
+
+ /* Flag set on file rotation notification. */
+ int rotation_flag;
+
} SCPerfOPIfaceContext;
/* the initialization functions */
record, srcip, sp, dstip, dp);
SCMutexLock(&hlog->file_ctx->fp_mutex);
- (void)MemBufferPrintToFPAsString(aft->buffer, hlog->file_ctx->fp);
- fflush(hlog->file_ctx->fp);
+ hlog->file_ctx->Write((const char *)MEMBUFFER_BUFFER(aft->buffer),
+ MEMBUFFER_OFFSET(aft->buffer), hlog->file_ctx);
SCMutexUnlock(&hlog->file_ctx->fp_mutex);
}
srcip, sp, dstip, dp);
SCMutexLock(&hlog->file_ctx->fp_mutex);
- (void)MemBufferPrintToFPAsString(aft->buffer, hlog->file_ctx->fp);
- fflush(hlog->file_ctx->fp);
+ hlog->file_ctx->Write((const char *)MEMBUFFER_BUFFER(aft->buffer),
+ MEMBUFFER_OFFSET(aft->buffer), hlog->file_ctx);
SCMutexUnlock(&hlog->file_ctx->fp_mutex);
}
LogFileFreeCtx(file_ctx);
return NULL;
}
+ OutputRegisterFileRotationFlag(&file_ctx->rotation_flag);
LogDnsFileCtx *dnslog_ctx = SCMalloc(sizeof(LogDnsFileCtx));
if (unlikely(dnslog_ctx == NULL)) {
LogFileFreeCtx(logfile_ctx);
return NULL;
}
+ OutputRegisterFileRotationFlag(&logfile_ctx->rotation_flag);
OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
if (unlikely(output_ctx == NULL)) {
SCMutexLock(&dlt->file_ctx->fp_mutex);
+ if (dlt->file_ctx->rotation_flag) {
+ dlt->file_ctx->rotation_flag = 0;
+ if (SCConfLogReopen(dlt->file_ctx) != 0) {
+ /* Rotation failed, error already logged. */
+ return TM_ECODE_FAILED;
+ }
+ }
+
+ if (dlt->file_ctx == NULL) {
+ return TM_ECODE_FAILED;
+ }
+
char srcip[46] = "";
char dstip[46] = "";
static void LogFileWriteJsonRecord(LogFileLogThread *aft, const Packet *p, const File *ff, int ipver) {
SCMutexLock(&aft->file_ctx->fp_mutex);
+ /* As writes are done via the LogFileCtx, check for rotation here. */
+ if (aft->file_ctx->rotation_flag) {
+ aft->file_ctx->rotation_flag = 0;
+ if (SCConfLogReopen(aft->file_ctx) != 0) {
+ SCLogWarning(SC_ERR_FOPEN, "Failed to re-open log file. "
+ "Logging for this module will be disabled.");
+ }
+ }
+
+ /* Bail early if no file pointer to write to (in the unlikely
+ * event file rotation failed. */
+ if (aft->file_ctx->fp == NULL) {
+ return;
+ }
+
FILE *fp = aft->file_ctx->fp;
char timebuf[64];
AppProto alproto = FlowGetAppProtocol(p->flow);
LogFileFreeCtx(logfile_ctx);
return NULL;
}
+ OutputRegisterFileRotationFlag(&logfile_ctx->rotation_flag);
OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
if (unlikely(output_ctx == NULL))
aft->uri_cnt ++;
SCMutexLock(&hlog->file_ctx->fp_mutex);
- (void)MemBufferPrintToFPAsString(aft->buffer, hlog->file_ctx->fp);
- fflush(hlog->file_ctx->fp);
+ hlog->file_ctx->Write((const char *)MEMBUFFER_BUFFER(aft->buffer),
+ MEMBUFFER_OFFSET(aft->buffer), hlog->file_ctx);
SCMutexUnlock(&hlog->file_ctx->fp_mutex);
end:
LogFileFreeCtx(file_ctx);
return NULL;
}
+ OutputRegisterFileRotationFlag(&file_ctx->rotation_flag);
LogHttpFileCtx *httplog_ctx = SCMalloc(sizeof(LogHttpFileCtx));
if (unlikely(httplog_ctx == NULL)) {
if (SCConfLogOpenGeneric(conf, file_ctx, DEFAULT_LOG_FILENAME) < 0) {
goto filectx_error;
}
+ OutputRegisterFileRotationFlag(&file_ctx->rotation_flag);
LogTlsFileCtx *tlslog_ctx = SCCalloc(1, sizeof(LogTlsFileCtx));
if (unlikely(tlslog_ctx == NULL))
aft->tls_cnt++;
SCMutexLock(&hlog->file_ctx->fp_mutex);
- MemBufferPrintToFPAsString(aft->buffer, hlog->file_ctx->fp);
- fflush(hlog->file_ctx->fp);
+ hlog->file_ctx->Write((const char *)MEMBUFFER_BUFFER(aft->buffer),
+ MEMBUFFER_OFFSET(aft->buffer), hlog->file_ctx);
SCMutexUnlock(&hlog->file_ctx->fp_mutex);
/* we only log the state once */
syslog(alert_syslog_level, "%s", js_s);
} else if (json_out == ALERT_FILE) {
MemBufferWriteString(buffer, "%s\n", js_s);
- (void)MemBufferPrintToFPAsString(buffer, file_ctx->fp);
- fflush(file_ctx->fp);
+ file_ctx->Write((const char *)MEMBUFFER_BUFFER(buffer),
+ MEMBUFFER_OFFSET(buffer), file_ctx);
}
SCMutexUnlock(&file_ctx->fp_mutex);
free(js_s);
SCFree(output_ctx);
return NULL;
}
+ OutputRegisterFileRotationFlag(&json_ctx->file_ctx->rotation_flag);
const char *format_s = ConfNodeLookupChildValue(conf, "format");
if (format_s != NULL) {