From d26bd3587e7ff44271d4fec7198852a9415cb786 Mon Sep 17 00:00:00 2001 From: Anoop Saldanha Date: Sun, 27 Dec 2009 17:53:14 +0530 Subject: [PATCH] fix for unclear error messages bug 15 --- src/alert-debuglog.c | 9 ++++++--- src/alert-fastlog.c | 9 ++++++--- src/alert-unified-alert.c | 28 +++++++++++++++++++--------- src/alert-unified-log.c | 8 ++++++-- src/alert-unified2-alert.c | 25 ++++++++++++++++--------- src/log-httplog.c | 9 ++++++--- src/util-error.c | 8 ++++++++ src/util-error.h | 7 +++++++ 8 files changed, 74 insertions(+), 29 deletions(-) diff --git a/src/alert-debuglog.c b/src/alert-debuglog.c index 880b419..af8ff55 100644 --- a/src/alert-debuglog.c +++ b/src/alert-debuglog.c @@ -193,7 +193,8 @@ TmEcode AlertDebuglogThreadInit(ThreadVars *t, void *initdata, void **data) if(initdata == NULL) { - printf("Error getting context for the file\n"); + SCLogError(SC_ERR_DEBUG_LOG_GENERIC_ERROR, "Error getting context for " + "DebugLog. \"initdata\" argument NULL"); return TM_ECODE_FAILED; } /** Use the Ouptut Context (file pointer and mutex) */ @@ -238,7 +239,8 @@ LogFileCtx *AlertDebuglogInitCtx(char *config_file) if(file_ctx == NULL) { - printf("AlertDebuglogInitCtx: Couldn't create new file_ctx\n"); + SCLogError(SC_ERR_DEBUG_LOG_GENERIC_ERROR, "AlertDebuglogInitCtx: Couldn't " + "create new file_ctx"); return NULL; } @@ -277,7 +279,8 @@ int AlertDebuglogOpenFileCtx(LogFileCtx *file_ctx, char *config_file) snprintf(log_path, PATH_MAX, "%s/%s", log_dir, DEFAULT_LOG_FILENAME); file_ctx->fp = fopen(log_path, "w"); if (file_ctx->fp == NULL) { - printf("ERROR: failed to open %s: %s\n", log_path, strerror(errno)); + SCLogError(SC_ERR_FOPEN, "ERROR: failed to open %s: %s", log_path, + strerror(errno)); return -1; } } diff --git a/src/alert-fastlog.c b/src/alert-fastlog.c index 0159edd..bf79eb9 100644 --- a/src/alert-fastlog.c +++ b/src/alert-fastlog.c @@ -161,7 +161,8 @@ TmEcode AlertFastlogThreadInit(ThreadVars *t, void *initdata, void **data) memset(aft, 0, sizeof(AlertFastlogThread)); if(initdata == NULL) { - printf("Error getting context for the file\n"); + SCLogError(SC_ERR_FAST_LOG_GENERIC_ERROR, "Error getting context for " + "AlertFastLog. \"initdata\" argument NULL"); return TM_ECODE_FAILED; } /** Use the Ouptut Context (file pointer and mutex) */ @@ -204,7 +205,8 @@ LogFileCtx *AlertFastlogInitCtx(char *config_file) if(file_ctx == NULL) { - printf("AlertFastlogInitCtx: Couldn't create new file_ctx\n"); + SCLogError(SC_ERR_FAST_LOG_GENERIC_ERROR, "AlertFastlogInitCtx: Couldn't " + "create new file_ctx"); return NULL; } @@ -244,7 +246,8 @@ int AlertFastlogOpenFileCtx(LogFileCtx *file_ctx, char *config_file) file_ctx->fp = fopen(log_path, "w"); if (file_ctx->fp == NULL) { - printf("ERROR: failed to open %s: %s\n", log_path, strerror(errno)); + SCLogError(SC_ERR_FOPEN, "ERROR: failed to open %s: %s", log_path, + strerror(errno)); return -1; } } diff --git a/src/alert-unified-alert.c b/src/alert-unified-alert.c index ec24d53..6466996 100644 --- a/src/alert-unified-alert.c +++ b/src/alert-unified-alert.c @@ -24,6 +24,8 @@ #include "util-unittest.h" #include "util-time.h" +#include "util-error.h" +#include "util-debug.h" #define DEFAULT_LOG_FILENAME "unified.alert" @@ -94,7 +96,8 @@ int AlertUnifiedAlertWriteFileHeader(ThreadVars *t, AlertUnifiedAlertThread *aun ret = fwrite(&hdr, sizeof(hdr), 1, aun->file_ctx->fp); if (ret != 1) { - printf("Error: fwrite failed: ret = %" PRId32 ", %s\n", ret, strerror(errno)); + SCLogError(SC_ERR_FWRITE, "Error: fwrite failed: ret = %" PRId32 ", %s", + ret, strerror(errno)); return -1; } fflush(aun->file_ctx->fp); @@ -114,15 +117,18 @@ int AlertUnifiedAlertCloseFile(ThreadVars *t, AlertUnifiedAlertThread *aun) { int AlertUnifiedAlertRotateFile(ThreadVars *t, AlertUnifiedAlertThread *aun) { if (AlertUnifiedAlertCloseFile(t,aun) < 0) { - printf("Error: AlertUnifiedAlertCloseFile failed\n"); + SCLogError(SC_ERR_UNIFIED_ALERT_GENERIC_ERROR, + "Error: AlertUnifiedAlertCloseFile failed"); return -1; } if (AlertUnifiedAlertOpenFileCtx(aun->file_ctx,aun->file_ctx->config_file) < 0) { - printf("Error: AlertUnifiedLogOpenFileCtx, open new log file failed\n"); + SCLogError(SC_ERR_UNIFIED_ALERT_GENERIC_ERROR, + "Error: AlertUnifiedLogOpenFileCtx, open new log file failed"); return -1; } if (AlertUnifiedAlertWriteFileHeader(t, aun) < 0) { - printf("Error: AlertUnifiedLogAppendFile, write unified header failed\n"); + SCLogError(SC_ERR_UNIFIED_ALERT_GENERIC_ERROR, "Error: " + "AlertUnifiedLogAppendFile, write unified header failed"); return -1; } @@ -181,7 +187,7 @@ TmEcode AlertUnifiedAlert (ThreadVars *tv, Packet *p, void *data, PacketQueue *p /* write and flush so it's written immediately */ ret = fwrite(&hdr, sizeof(hdr), 1, aun->file_ctx->fp); if (ret != 1) { - printf("Error: fwrite failed: %s\n", strerror(errno)); + SCLogError(SC_ERR_FWRITE, "Error: fwrite failed: %s", strerror(errno)); return TM_ECODE_FAILED; } /* force writing to disk so barnyard will not read half @@ -202,7 +208,8 @@ TmEcode AlertUnifiedAlertThreadInit(ThreadVars *t, void *initdata, void **data) if(initdata == NULL) { - printf("Error getting context for the file\n"); + SCLogError(SC_ERR_UNIFIED_ALERT_GENERIC_ERROR, "Error getting context for " + "UnifiedAlert. \"initdata\" argument NULL"); return TM_ECODE_FAILED; } /** Use the Ouptut Context (file pointer and mutex) */ @@ -212,7 +219,8 @@ TmEcode AlertUnifiedAlertThreadInit(ThreadVars *t, void *initdata, void **data) /** Write Unified header */ int ret = AlertUnifiedAlertWriteFileHeader(t, aun); if (ret != 0) { - printf("Error: AlertUnifiedLogWriteFileHeader failed.\n"); + SCLogError(SC_ERR_UNIFIED_ALERT_GENERIC_ERROR, + "Error: AlertUnifiedLogWriteFileHeader failed"); return TM_ECODE_FAILED; } @@ -255,7 +263,8 @@ LogFileCtx *AlertUnifiedAlertInitCtx(char *config_file) LogFileCtx *file_ctx = LogFileNewCtx(); if (file_ctx == NULL) { - printf("AlertUnifiedAlertInitCtx: Couldn't create new file_ctx\n"); + SCLogError(SC_ERR_UNIFIED_ALERT_GENERIC_ERROR, + "AlertUnifiedAlertInitCtx: Couldn't create new file_ctx"); return NULL; } @@ -307,7 +316,8 @@ int AlertUnifiedAlertOpenFileCtx(LogFileCtx *file_ctx, char *config_file) /* XXX filename & location */ file_ctx->fp = fopen(filename, "wb"); if (file_ctx->fp == NULL) { - printf("Error: fopen %s failed: %s\n", filename, strerror(errno)); /* XXX errno threadsafety? */ + SCLogError(SC_ERR_FOPEN, "ERROR: failed to open %s: %s", filename, + strerror(errno)); return -1; } } diff --git a/src/alert-unified-log.c b/src/alert-unified-log.c index edc04ed..fd3a0a5 100644 --- a/src/alert-unified-log.c +++ b/src/alert-unified-log.c @@ -24,6 +24,8 @@ #include "util-unittest.h" #include "util-time.h" +#include "util-debug.h" +#include "util-error.h" #define DEFAULT_LOG_FILENAME "unified.log" @@ -218,7 +220,8 @@ TmEcode AlertUnifiedLogThreadInit(ThreadVars *t, void *initdata, void **data) memset(aun, 0, sizeof(AlertUnifiedLogThread)); if(initdata == NULL) { - printf("Error getting context for the file\n"); + SCLogError(SC_ERR_UNIFIED_LOG_GENERIC_ERROR, "Error getting context for " + "UnifiedLog. \"initdata\" argument NULL"); return TM_ECODE_FAILED; } /** Use the Ouptut Context (file pointer and mutex) */ @@ -325,7 +328,8 @@ int AlertUnifiedLogOpenFileCtx(LogFileCtx *file_ctx, char *config_file) /* XXX filename & location */ file_ctx->fp = fopen(filename, "wb"); if (file_ctx->fp == NULL) { - printf("Error: fopen %s failed: %s\n", filename, strerror(errno)); /* XXX errno threadsafety? */ + SCLogError(SC_ERR_FOPEN, "ERROR: failed to open %s: %s", filename, + strerror(errno)); return -1; } } diff --git a/src/alert-unified2-alert.c b/src/alert-unified2-alert.c index c826ffb..957d19c 100644 --- a/src/alert-unified2-alert.c +++ b/src/alert-unified2-alert.c @@ -18,6 +18,7 @@ #include "alert-unified2-alert.h" #include "decode-ipv4.h" +#include "util-error.h" #include "util-debug.h" #include "util-time.h" #ifndef IPPROTO_SCTP @@ -152,11 +153,13 @@ int Unified2AlertCloseFile(ThreadVars *t, Unified2AlertThread *aun) { int Unified2AlertRotateFile(ThreadVars *t, Unified2AlertThread *aun) { if (Unified2AlertCloseFile(t,aun) < 0) { - printf("Error: Unified2AlertCloseFile failed\n"); + SCLogError(SC_ERR_UNIFIED2_ALERT_GENERIC_ERROR, + "Error: Unified2AlertCloseFile failed"); return -1; } if (Unified2AlertOpenFileCtx(aun->file_ctx,aun->file_ctx->config_file) < 0) { - printf("Error: Unified2AlertOpenFileCtx, open new log file failed\n"); + SCLogError(SC_ERR_UNIFIED2_ALERT_GENERIC_ERROR, + "Error: Unified2AlertOpenFileCtx, open new log file failed"); return -1; } return 0; @@ -236,7 +239,7 @@ int Unified2PacketTypeAlert (ThreadVars *t, Packet *p, void *data) ret = fwrite(write_buffer,len, 1, aun->file_ctx->fp); if (ret != 1) { - printf("Error: fwrite failed: %s\n", strerror(errno)); + SCLogError(SC_ERR_FWRITE, "Error: fwrite failed: %s", strerror(errno)); return -1; } @@ -346,7 +349,7 @@ int Unified2IPv6TypeAlert (ThreadVars *t, Packet *p, void *data, PacketQueue *pq ret = fwrite(write_buffer,len, 1, aun->file_ctx->fp); if (ret != 1) { - printf("Error: fwrite failed: %s\n", strerror(errno)); + SCLogError(SC_ERR_FWRITE, "Error: fwrite failed: %s", strerror(errno)); return -1; } @@ -457,7 +460,7 @@ int Unified2IPv4TypeAlert (ThreadVars *tv, Packet *p, void *data, PacketQueue *p ret = fwrite(write_buffer,len, 1, aun->file_ctx->fp); if (ret != 1) { - printf("Error: fwrite failed: %s\n", strerror(errno)); + SCLogError(SC_ERR_FWRITE, "Error: fwrite failed: %s", strerror(errno)); return -1; } @@ -488,7 +491,8 @@ TmEcode Unified2AlertThreadInit(ThreadVars *t, void *initdata, void **data) memset(aun, 0, sizeof(Unified2AlertThread)); if(initdata == NULL) { - printf("Error getting context for the file\n"); + SCLogError(SC_ERR_UNIFIED2_ALERT_GENERIC_ERROR, "Error getting context for " + "Unified2Alert. \"initdata\" argument NULL"); return TM_ECODE_FAILED; } /** Use the Ouptut Context (file pointer and mutex) */ @@ -542,7 +546,8 @@ LogFileCtx *Unified2AlertInitCtx(char *config_file) if(file_ctx == NULL) { - printf("Unified2AlertInitCtx: Couldn't create new file_ctx\n"); + SCLogError(SC_ERR_UNIFIED2_ALERT_GENERIC_ERROR, "Unified2AlertInitCtx: " + "Couldn't create new file_ctx"); return NULL; } @@ -594,7 +599,8 @@ int Unified2AlertOpenFileCtx(LogFileCtx *file_ctx, char *config_file) /* XXX filename & location */ file_ctx->fp = fopen(filename, "wb"); if (file_ctx->fp == NULL) { - printf("Error: fopen %s failed: %s\n", filename, strerror(errno)); /* XXX errno threadsafety? */ + SCLogError(SC_ERR_FOPEN, "ERROR: failed to open %s: %s", filename, + strerror(errno)); return -1; } } @@ -969,7 +975,8 @@ static int Unified2TestRotate01(void) goto error; if (strcmp(filename, lf->filename) == 0) { - printf("filename \"%s\" == \"%s\": ", filename, lf->filename); + SCLogError(SC_ERR_UNIFIED2_ALERT_GENERIC_ERROR, + "filename \"%s\" == \"%s\": ", filename, lf->filename); goto error; } diff --git a/src/log-httplog.c b/src/log-httplog.c index 6d1d544..3c5d9f1 100644 --- a/src/log-httplog.c +++ b/src/log-httplog.c @@ -184,7 +184,8 @@ TmEcode LogHttplogThreadInit(ThreadVars *t, void *initdata, void **data) if(initdata == NULL) { - printf("Error getting context for the file\n"); + SCLogError(SC_ERR_HTTP_LOG_GENERIC_ERROR, "Error getting context for " + "HTTPLog. \"initdata\" argument NULL"); return TM_ECODE_FAILED; } /** Use the Ouptut Context (file pointer and mutex) */ @@ -228,7 +229,8 @@ LogFileCtx *LogHttplogInitCtx(char *config_file) if(file_ctx == NULL) { - printf("LogHttplogInitCtx: Couldn't create new file_ctx\n"); + SCLogError(SC_ERR_HTTP_LOG_GENERIC_ERROR, "LogHttplogInitCtx: Couldn't " + "create new file_ctx"); return NULL; } @@ -268,7 +270,8 @@ int LogHttplogOpenFileCtx(LogFileCtx *file_ctx, char *config_file) file_ctx->fp = fopen(log_path, "w"); if (file_ctx->fp == NULL) { - printf("ERROR: failed to open %s: %s\n", log_path, strerror(errno)); + SCLogError(SC_ERR_FOPEN, "ERROR: failed to open %s: %s", log_path, + strerror(errno)); return -1; } if(file_ctx->config_file == NULL) diff --git a/src/util-error.c b/src/util-error.c index ecb5796..c3143d8 100644 --- a/src/util-error.c +++ b/src/util-error.c @@ -53,6 +53,14 @@ const char * SCErrorToString(SCError err) CASE_CODE (SC_ERR_REASSEMBLY_FAILED); CASE_CODE (SC_ERR_POOL_INIT_FAILED); CASE_CODE (SC_UNIMPLEMENTED); + CASE_CODE (SC_ERR_FAST_LOG_GENERIC_ERROR); + CASE_CODE (SC_ERR_DEBUG_LOG_GENERIC_ERROR); + CASE_CODE (SC_ERR_UNIFIED_LOG_GENERIC_ERROR); + CASE_CODE (SC_ERR_HTTP_LOG_GENERIC_ERROR); + CASE_CODE (SC_ERR_UNIFIED_ALERT_GENERIC_ERROR); + CASE_CODE (SC_ERR_UNIFIED2_ALERT_GENERIC_ERROR); + CASE_CODE (SC_ERR_FWRITE); + CASE_CODE (SC_ERR_FOPEN); default: return "UNKNOWN_ERROR"; } diff --git a/src/util-error.h b/src/util-error.h index 7b0ef4c..77f67f1 100644 --- a/src/util-error.h +++ b/src/util-error.h @@ -65,6 +65,13 @@ typedef enum { SC_NFQ_THREAD_INIT, SC_ERR_DAEMON, SC_UNIMPLEMENTED, + SC_ERR_FAST_LOG_GENERIC_ERROR, + SC_ERR_DEBUG_LOG_GENERIC_ERROR, + SC_ERR_UNIFIED_LOG_GENERIC_ERROR, + SC_ERR_HTTP_LOG_GENERIC_ERROR, + SC_ERR_UNIFIED_ALERT_GENERIC_ERROR, + SC_ERR_UNIFIED2_ALERT_GENERIC_ERROR, + SC_ERR_FWRITE, } SCError; const char *SCErrorToString(SCError); -- 1.5.5