From 60309004dfc57c3243abb2f01042950201596773 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Thu, 17 Apr 2008 12:46:57 +0200 Subject: completed better modularity of runtime - added the ability to specify an error log function for the runtime - removed dependency of core runtime on dirty.h Note that it is "better" modularity, not perfect. There is still work to do, but I think we can for the time being proceed with other things. --- dirty.h | 2 +- plugins/imklog/linux.c | 8 ++------ plugins/immark/immark.c | 2 +- plugins/imuxsock/imuxsock.c | 5 +---- runtime/cfsysline.c | 1 - runtime/errmsg.c | 17 ++++++++--------- runtime/glbl.c | 1 + runtime/modules.c | 1 - runtime/msg.c | 1 - runtime/obj.c | 2 +- runtime/queue.c | 1 - runtime/rsyslog.c | 20 ++++++++++++++++++++ runtime/rsyslog.h | 8 ++++---- runtime/srutils.c | 1 - runtime/stream.c | 1 - runtime/wti.c | 1 - runtime/wtp.c | 1 - tools/syslogd.c | 23 ++++++++++++++++++----- tools/syslogd.h | 1 - 19 files changed, 57 insertions(+), 40 deletions(-) diff --git a/dirty.h b/dirty.h index fdded6ed..fe188acd 100644 --- a/dirty.h +++ b/dirty.h @@ -41,7 +41,7 @@ #define MSG_DONT_PARSE_HOSTNAME 0 rsRetVal submitMsg(msg_t *pMsg); -rsRetVal logmsgInternal(int pri, char *msg, int flags); +rsRetVal logmsgInternal(int pri, uchar *msg, int flags); rsRetVal parseAndSubmitMessage(char *hname, char *msg, int len, int bParseHost, int flags, flowControl_t flowCtlType); /* TODO: the following 2 need to go in conf obj interface... */ diff --git a/plugins/imklog/linux.c b/plugins/imklog/linux.c index d00723dd..32cf70c4 100644 --- a/plugins/imklog/linux.c +++ b/plugins/imklog/linux.c @@ -146,9 +146,7 @@ static enum LOGSRC GetKernelLogSrc(void) if ( (kmsg = open(_PATH_KLOG, O_RDONLY)) < 0 ) { - char sz[512]; - snprintf(sz, sizeof(sz), "imklog: Cannot open proc file system, %d - %s.\n", errno, strerror(errno)); - logmsgInternal(LOG_SYSLOG|LOG_ERR, sz, ADDDATE); + imklogLogIntMsg(LOG_ERR, "imklog: Cannot open proc file system, %d.\n", errno); ksyslog(7, NULL, 0); /* TODO: check this, implement more */ return(none); } @@ -427,11 +425,9 @@ static void LogKernelLine(void) memset(log_buffer, '\0', sizeof(log_buffer)); if ( (rdcnt = ksyslog(2, log_buffer, sizeof(log_buffer)-1)) < 0 ) { - char sz[512]; if(errno == EINTR) return; - snprintf(sz, sizeof(sz), "imklog: Error return from sys_sycall: %d - %s\n", errno, strerror(errno)); - logmsgInternal(LOG_SYSLOG|LOG_ERR, sz, ADDDATE); + imklogLogIntMsg(LOG_ERR, "imklog Error return from sys_sycall: %d\n", errno); } else LogLine(log_buffer, rdcnt); diff --git a/plugins/immark/immark.c b/plugins/immark/immark.c index 1907bb25..ebdcabe9 100644 --- a/plugins/immark/immark.c +++ b/plugins/immark/immark.c @@ -75,7 +75,7 @@ CODESTARTrunInput * rgerhards, 2007-12-17 */ CHKiRet(thrdSleep(pThrd, iMarkMessagePeriod, 0)); /* seconds, micro seconds */ - logmsgInternal(LOG_INFO, "-- MARK --", ADDDATE|MARK); + logmsgInternal(LOG_INFO, (uchar*)"-- MARK --", ADDDATE|MARK); } finalize_it: return iRet; diff --git a/plugins/imuxsock/imuxsock.c b/plugins/imuxsock/imuxsock.c index 6c6b7f94..94f97eb5 100644 --- a/plugins/imuxsock/imuxsock.c +++ b/plugins/imuxsock/imuxsock.c @@ -103,8 +103,6 @@ static rsRetVal setSystemLogTimestampIgnore(void __attribute__((unused)) *pVal, */ static rsRetVal addLstnSocketName(void __attribute__((unused)) *pVal, uchar *pNewVal) { - char errStr[1024]; - if(nfunix < MAXFUNIX) { if(*pNewVal == ':') { funixParseHost[nfunix] = 1; @@ -116,9 +114,8 @@ static rsRetVal addLstnSocketName(void __attribute__((unused)) *pVal, uchar *pNe funixn[nfunix++] = pNewVal; } else { - snprintf(errStr, sizeof(errStr), "rsyslogd: Out of unix socket name descriptors, ignoring %s\n", + errmsg.LogError(NO_ERRCODE, "Out of unix socket name descriptors, ignoring %s\n", pNewVal); - logmsgInternal(LOG_SYSLOG|LOG_ERR, errStr, ADDDATE); } return RS_RET_OK; diff --git a/runtime/cfsysline.c b/runtime/cfsysline.c index cb86c7d4..ffc49057 100644 --- a/runtime/cfsysline.c +++ b/runtime/cfsysline.c @@ -33,7 +33,6 @@ #include #include -#include "dirty.h" /* TODO: when the module interface & library design is done, this should be able to go away */ #include "cfsysline.h" #include "obj.h" #include "errmsg.h" diff --git a/runtime/errmsg.c b/runtime/errmsg.c index b555d06a..1744c902 100644 --- a/runtime/errmsg.c +++ b/runtime/errmsg.c @@ -33,7 +33,6 @@ #include #include "rsyslog.h" -#include "dirty.h" #include "obj.h" #include "errmsg.h" #include "sysvar.h" @@ -84,14 +83,7 @@ LogError(int __attribute__((unused)) iErrCode, char *fmt, ... ) msg[sizeof(msg)/sizeof(char) - 1] = '\0'; /* just to be on the safe side... */ errno = 0; - /* we must check if the runtime is initialized, because else we can NOT - * submit internal errors. -- rgerhards, 2008-04-16 - * TODO: a better way is to set an error handler and check if it is NULL - */ - if(rsrtIsInit()) - logmsgInternal(LOG_SYSLOG|LOG_ERR, msg, ADDDATE); - else - fprintf(stderr, "rsyslog runtime error: %s\n", msg); + glblErrLogger((uchar*)msg); ENDfunc } @@ -126,5 +118,12 @@ BEGINAbstractObjClassInit(errmsg, 1, OBJ_IS_CORE_MODULE) /* class, version */ /* set our own handlers */ ENDObjClassInit(errmsg) +/* Exit the class. + * rgerhards, 2008-04-17 + */ +BEGINObjClassExit(errmsg, OBJ_IS_CORE_MODULE) /* class, version */ + /* release objects we no longer need */ +ENDObjClassExit(errmsg) + /* vi:set ai: */ diff --git a/runtime/glbl.c b/runtime/glbl.c index 432da93a..047fd611 100644 --- a/runtime/glbl.c +++ b/runtime/glbl.c @@ -82,6 +82,7 @@ SIMP_PROP(DisableDNS, bDisableDNS, int) SIMP_PROP(LocalDomain, LocalDomain, uchar*) SIMP_PROP(StripDomains, StripDomains, char**) SIMP_PROP(LocalHosts, LocalHosts, char**) + SIMP_PROP_SET(LocalHostName, LocalHostName, uchar*) #undef SIMP_PROP diff --git a/runtime/modules.c b/runtime/modules.c index 8ae9f038..c156fef2 100644 --- a/runtime/modules.c +++ b/runtime/modules.c @@ -49,7 +49,6 @@ #include #include -#include "dirty.h" #include "cfsysline.h" #include "modules.h" #include "errmsg.h" diff --git a/runtime/msg.c b/runtime/msg.c index 96bd8cc5..e72ef71b 100644 --- a/runtime/msg.c +++ b/runtime/msg.c @@ -36,7 +36,6 @@ #include #include #include "rsyslog.h" -#include "dirty.h" #include "srUtils.h" #include "stringbuf.h" #include "template.h" diff --git a/runtime/obj.c b/runtime/obj.c index 8f2f99e3..8ab606f9 100644 --- a/runtime/obj.c +++ b/runtime/obj.c @@ -1286,10 +1286,10 @@ objClassExit(void) /* TODO: implement the class exits! */ #if 0 - errmsgClassInit(pModInfo); cfsyslineInit(pModInfo); varClassInit(pModInfo); #endif + errmsgClassExit(); moduleClassExit(); RETiRet; } diff --git a/runtime/queue.c b/runtime/queue.c index c6b617a9..56711416 100644 --- a/runtime/queue.c +++ b/runtime/queue.c @@ -43,7 +43,6 @@ #include #include "rsyslog.h" -#include "dirty.h" #include "queue.h" #include "stringbuf.h" #include "srUtils.h" diff --git a/runtime/rsyslog.c b/runtime/rsyslog.c index d0eaa6f8..95ac23ef 100644 --- a/runtime/rsyslog.c +++ b/runtime/rsyslog.c @@ -56,6 +56,7 @@ * A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution. */ #include "config.h" +#include #include #include @@ -75,14 +76,33 @@ #include "queue.h" #include "conf.h" #include "glbl.h" +#include "errmsg.h" + +/* forward definitions */ +static rsRetVal dfltErrLogger(uchar *errMsg); /* globally visible static data - see comment in rsyslog.h for details */ uchar *glblModPath; /* module load path */ +rsRetVal (*glblErrLogger)(uchar*) = dfltErrLogger; /* the error logger to use by the errmsg module */ /* static data */ static int iRefCount = 0; /* our refcount - it MUST exist only once inside a process (not thread) thus it is perfectly OK to use a static. MUST be initialized to 0! */ +/* This is the default instance of the error logger. It simply writes the message + * to stderr. It is expected that this is replaced by the runtime user very early + * during startup (at least if the default is unsuitable). However, we provide a + * default so that we can log errors during the intial phase, most importantly + * during initialization. -- rgerhards. 2008-04-17 + */ +static rsRetVal dfltErrLogger(uchar *errMsg) +{ + DEFiRet; + fprintf(stderr, "rsyslog runtime error: %s\n", errMsg); + RETiRet; +} + + /* globally initialze the runtime system * NOTE: this is NOT thread safe and must not be called concurrently. If that * ever poses a problem, we may use proper mutex calls - not considered needed yet. diff --git a/runtime/rsyslog.h b/runtime/rsyslog.h index 5a3175e2..868bb564 100644 --- a/runtime/rsyslog.h +++ b/runtime/rsyslog.h @@ -59,12 +59,15 @@ /* define some base data types */ +typedef unsigned char uchar;/* get rid of the unhandy "unsigned char" */ typedef struct thrdInfo thrdInfo_t; typedef struct filed selector_t; /* TODO: this so far resides in syslogd.c, think about modularization */ typedef struct NetAddr netAddr_t; typedef struct msg msg_t; typedef struct interface_s interface_t; typedef struct objInfo_s objInfo_t; +typedef enum rsRetVal_ rsRetVal; /**< friendly type for global return value */ +typedef rsRetVal (*errLogFunc_t)(uchar*); /* this is a trick to store a function ptr to a function returning a function ptr... */ /* some universal 64 bit define... */ typedef long long int64; @@ -204,7 +207,6 @@ enum rsRetVal_ /** return value. All methods return this if not specified oth RS_RET_NO_RUN = 3, /**< operation successful, but function does not like to be executed */ RS_RET_OK = 0 /**< operation successful */ }; -typedef enum rsRetVal_ rsRetVal; /**< friendly type for global return value */ /* some helpful macros to work with srRetVals. * Be sure to call the to-be-returned variable always "iRet" and @@ -269,9 +271,6 @@ typedef enum rsObjectID rsObjID; #define RSFREEOBJ(x) {(x)->OID = OIDrsFreed; free(x);} #endif -/* get rid of the unhandy "unsigned char" - */ -typedef unsigned char uchar; /* for the time being, we do our own portability handling here. It * looks like autotools either does not yet support checks for it, or @@ -297,6 +296,7 @@ void dbgprintf(char *, ...) __attribute__((format(printf, 1, 2))); * add them. -- rgerhards, 2008-04-17 */ extern uchar *glblModPath; /* module load path */ +extern rsRetVal (*glblErrLogger)(uchar*); /* some runtime prototypes */ rsRetVal rsrtInit(char **ppErrObj, obj_if_t *pObjIF); diff --git a/runtime/srutils.c b/runtime/srutils.c index cf36493a..97cc3252 100644 --- a/runtime/srutils.c +++ b/runtime/srutils.c @@ -44,7 +44,6 @@ #define TRUE 1 #define FALSE 0 #include "srUtils.h" -#include "dirty.h" #include "obj.h" diff --git a/runtime/stream.c b/runtime/stream.c index 7274b807..3afa9fcd 100644 --- a/runtime/stream.c +++ b/runtime/stream.c @@ -41,7 +41,6 @@ #include #include "rsyslog.h" -#include "dirty.h" #include "stringbuf.h" #include "srUtils.h" #include "obj.h" diff --git a/runtime/wti.c b/runtime/wti.c index 88439049..0e04200c 100644 --- a/runtime/wti.c +++ b/runtime/wti.c @@ -40,7 +40,6 @@ #include #include "rsyslog.h" -#include "dirty.h" #include "stringbuf.h" #include "srUtils.h" #include "wtp.h" diff --git a/runtime/wtp.c b/runtime/wtp.c index 98f1bdbe..0658232b 100644 --- a/runtime/wtp.c +++ b/runtime/wtp.c @@ -41,7 +41,6 @@ #include #include "rsyslog.h" -#include "dirty.h" #include "stringbuf.h" #include "srUtils.h" #include "wtp.h" diff --git a/tools/syslogd.c b/tools/syslogd.c index 67c7d11b..f2b18a3d 100644 --- a/tools/syslogd.c +++ b/tools/syslogd.c @@ -847,6 +847,19 @@ finalize_it: RETiRet; } + +/* this is a special function used to submit an error message. This + * function is also passed to the runtime library as the generic error + * message handler. -- rgerhards, 2008-04-17 + */ +rsRetVal +submitErrMsg(uchar *msg) +{ + DEFiRet; + iRet = logmsgInternal(LOG_SYSLOG|LOG_ERR, msg, ADDDATE); + RETiRet; +} + /* rgerhards 2004-11-09: the following is a function that can be used * to log a message orginating from the syslogd itself. In sysklogd code, * this is done by simply calling logmsg(). However, logmsg() is changed in @@ -857,14 +870,14 @@ finalize_it: * think on the best way to do this. */ rsRetVal -logmsgInternal(int pri, char *msg, int flags) +logmsgInternal(int pri, uchar *msg, int flags) { DEFiRet; msg_t *pMsg; CHKiRet(msgConstruct(&pMsg)); - MsgSetUxTradMsg(pMsg, msg); - MsgSetRawMsg(pMsg, msg); + MsgSetUxTradMsg(pMsg, (char*)msg); + MsgSetRawMsg(pMsg, (char*)msg); MsgSetHOSTNAME(pMsg, (char*)glbl.GetLocalHostName()); MsgSetRcvFrom(pMsg, (char*)glbl.GetLocalHostName()); MsgSetTAG(pMsg, "rsyslogd:"); @@ -1877,7 +1890,7 @@ die(int sig) "\" x-pid=\"%d\" x-info=\"http://www.rsyslog.com\"]" " exiting on signal %d.", (int) myPid, sig); errno = 0; - logmsgInternal(LOG_SYSLOG|LOG_INFO, buf, ADDDATE); + logmsgInternal(LOG_SYSLOG|LOG_INFO, (uchar*)buf, ADDDATE); } /* drain queue (if configured so) and stop main queue worker thread pool */ @@ -2330,7 +2343,7 @@ init(void) " [origin software=\"rsyslogd\" " "swVersion=\"" VERSION \ "\" x-pid=\"%d\" x-info=\"http://www.rsyslog.com\"] restart", (int) myPid); - logmsgInternal(LOG_SYSLOG|LOG_INFO, bufStartUpMsg, ADDDATE); + logmsgInternal(LOG_SYSLOG|LOG_INFO, (uchar*)bufStartUpMsg, ADDDATE); memset(&sigAct, 0, sizeof (sigAct)); sigemptyset(&sigAct.sa_mask); diff --git a/tools/syslogd.h b/tools/syslogd.h index 01580a51..e866a16b 100644 --- a/tools/syslogd.h +++ b/tools/syslogd.h @@ -90,7 +90,6 @@ rsRetVal selectorAddList(selector_t *f); /* the following prototypes should go away once we have an input * module interface -- rgerhards, 2007-12-12 */ -rsRetVal logmsgInternal(int pri, char *msg, int flags); void logmsg(msg_t *pMsg, int flags); extern int NoHops; extern int send_to_all; -- cgit v1.2.3