diff --git a/src/alert-unified-alert.c b/src/alert-unified-alert.c index 871004b787..75898bd5dc 100644 --- a/src/alert-unified-alert.c +++ b/src/alert-unified-alert.c @@ -116,6 +116,11 @@ int AlertUnifiedAlertWriteFileHeader(LogFileCtx *file_ctx) { if (file_ctx->flags & LOGFILE_HEADER_WRITTEN) return 0; + if (file_ctx->fp == NULL) { + SCLogError(SC_ERR_FOPEN, "file pointer is NULL"); + return -1; + } + /** write the fileheader to the file so the reader can recognize it */ AlertUnifiedAlertFileHeader hdr; hdr.magic = ALERTUNIFIEDALERT_ALERTMAGIC; @@ -383,7 +388,7 @@ int AlertUnifiedAlertOpenFileCtx(LogFileCtx *file_ctx, const char *prefix) if (file_ctx->fp == NULL) { SCLogError(SC_ERR_FOPEN, "ERROR: failed to open %s: %s", filename, strerror(errno)); - ret = -1; + return TM_ECODE_FAILED; } file_ctx->flags = 0; diff --git a/src/alert-unified-log.c b/src/alert-unified-log.c index 24228516d0..71772b19e6 100644 --- a/src/alert-unified-log.c +++ b/src/alert-unified-log.c @@ -138,7 +138,8 @@ int AlertUnifiedLogWriteFileHeader(LogFileCtx *file_ctx) { ret = fwrite(&hdr, sizeof(hdr), 1, file_ctx->fp); if (ret != 1) { - printf("Error: fwrite failed: ret = %" PRId32 ", %s\n", ret, strerror(errno)); + SCLogError(SC_ERR_FWRITE, "fwrite failed: ret = %" PRId32 ", %s", ret, + strerror(errno)); return -1; } @@ -236,7 +237,7 @@ TmEcode AlertUnifiedLog (ThreadVars *tv, Packet *p, void *data, PacketQueue *pq) ret = fwrite(buf, buflen, 1, aun->file_ctx->fp); if (ret != 1) { - printf("Error: fwrite failed: %s\n", strerror(errno)); + SCLogError(SC_ERR_FWRITE, "fwrite failed: %s", strerror(errno)); aun->file_ctx->alerts += i; SCMutexUnlock(&aun->file_ctx->fp_mutex); return TM_ECODE_FAILED; @@ -262,13 +263,19 @@ TmEcode AlertUnifiedLogThreadInit(ThreadVars *t, void *initdata, void **data) memset(aun, 0, sizeof(AlertUnifiedLogThread)); if (initdata == NULL) { - SCLogDebug("Error getting context for UnifiedLog. \"initdata\" argument NULL"); + SCLogDebug("Error getting context for UnifiedLog. \"initdata\" argument NULL"); SCFree(aun); return TM_ECODE_FAILED; } /** Use the Ouptut Context (file pointer and mutex) */ aun->file_ctx = ((OutputCtx *)initdata)->data; + if (aun->file_ctx->fp == NULL) { + SCLogError (SC_ERR_OPENING_FILE, "Target file has not been opened, check" + " the write permission"); + return TM_ECODE_FAILED; + } + *data = (void *)aun; return TM_ECODE_OK; } @@ -307,7 +314,7 @@ OutputCtx *AlertUnifiedLogInitCtx(ConfNode *conf) LogFileCtx* file_ctx=LogFileNewCtx(); if (file_ctx == NULL) { - printf("AlertUnifiedLogInitCtx: Couldn't create new file_ctx\n"); + SCLogError(SC_ERR_MEM_ALLOC, "Couldn't create new file_ctx"); return NULL; } @@ -403,7 +410,7 @@ int AlertUnifiedLogOpenFileCtx(LogFileCtx *file_ctx, const char *prefix) if (file_ctx->fp == NULL) { SCLogError(SC_ERR_FOPEN, "ERROR: failed to open %s: %s", filename, strerror(errno)); - ret = -1; + return TM_ECODE_FAILED; } /** Write Unified header */ diff --git a/src/tm-modules.c b/src/tm-modules.c index 157f0e9fea..30dd36dc19 100644 --- a/src/tm-modules.c +++ b/src/tm-modules.c @@ -50,7 +50,7 @@ LogFileCtx *LogFileNewCtx() if(lf_ctx == NULL) { - printf("LogFileCtxNew: Couldn't SCMalloc \n"); + SCLogError(SC_ERR_MEM_ALLOC, "Couldn't SCMalloc"); return NULL; } memset(lf_ctx, 0, sizeof(LogFileCtx)); diff --git a/src/util-error.c b/src/util-error.c index 00bb1c2906..bf84f36b4d 100644 --- a/src/util-error.c +++ b/src/util-error.c @@ -128,6 +128,8 @@ const char * SCErrorToString(SCError err) CASE_CODE (SC_ERR_INVALID_YAML_CONF_ENTRY); CASE_CODE (SC_ERR_TMQ_ALREADY_REGISTERED); CASE_CODE (SC_ERR_CONFLICTING_RULE_KEYWORDS); + CASE_CODE (SC_ERR_INITIALIZATION); + CASE_CODE (SC_ERR_UNIFIED_LOG_FILE_HEADER); default: return "UNKNOWN_ERROR"; } diff --git a/src/util-error.h b/src/util-error.h index fafc13e0a6..7f203e00bf 100644 --- a/src/util-error.h +++ b/src/util-error.h @@ -145,6 +145,10 @@ typedef enum { SC_ERR_INVALID_YAML_CONF_ENTRY, SC_ERR_TMQ_ALREADY_REGISTERED, SC_ERR_CONFLICTING_RULE_KEYWORDS, + SC_ERR_UNIFIED_LOG_FILE_HEADER, /**< Error to indicate the unified file + header writing function has been + failed */ + } SCError; const char *SCErrorToString(SCError);