From e45bbf5c66b5cb08c91bdb99ba1acf5f215c3112 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Fri, 14 Dec 2007 17:58:12 +0000 Subject: graceful termination now supported --- module-template.h | 12 ++++++++++++ modules.c | 3 +++ modules.h | 2 ++ plugins/immark/immark.c | 3 ++- syslogd.c | 13 ++++--------- threads.c | 20 +++++++++++++++++++- threads.h | 4 +++- 7 files changed, 45 insertions(+), 12 deletions(-) diff --git a/module-template.h b/module-template.h index a9535428..758e9710 100644 --- a/module-template.h +++ b/module-template.h @@ -376,6 +376,8 @@ static rsRetVal queryEtryPt(uchar *name, rsRetVal (**pEtryPoint)())\ CODEqueryEtryPt_STD_MOD_QUERIES \ else if(!strcmp((char*) name, "runInput")) {\ *pEtryPoint = runInput;\ + } else if(!strcmp((char*) name, "getTermSyncType")) {\ + *pEtryPoint = modGetTermSyncType;\ } /* modInit() @@ -467,6 +469,16 @@ static rsRetVal runInput(void)\ return iRet;\ } +/* method to return which termination sync method is used by this module. + */ +#define TERM_SYNC_TYPE(x) \ +static rsRetVal modGetTermSyncType(eTermSyncType_t *pTermSync)\ +{\ + assert(pTermSync != NULL); \ + *pTermSync = (x);\ + return RS_RET_OK;\ +} + /* * vi:set ai: */ diff --git a/modules.c b/modules.c index 9a3786f2..02ec366b 100644 --- a/modules.c +++ b/modules.c @@ -216,6 +216,7 @@ rsRetVal doModInit(rsRetVal (*modInit)(int, int*, rsRetVal(**)(), rsRetVal(*)()) DEFiRet; modInfo_t *pNew = NULL; rsRetVal (*modGetType)(eModType_t *pType); + rsRetVal (*modGetTermSyncType)(eTermSyncType_t *pType); assert(modInit != NULL); @@ -255,6 +256,8 @@ rsRetVal doModInit(rsRetVal (*modInit)(int, int*, rsRetVal(**)(), rsRetVal(*)()) /* ... and now the module-specific interfaces */ switch(pNew->eType) { case eMOD_IN: + CHKiRet((*pNew->modQueryEtryPt)((uchar*)"getTermSyncType", &modGetTermSyncType)); + CHKiRet((iRet = (*modGetTermSyncType)(&pNew->mod.im.eTermSyncType)) != RS_RET_OK); CHKiRet((*pNew->modQueryEtryPt)((uchar*)"runInput", &pNew->mod.im.runInput)); break; case eMOD_OUT: diff --git a/modules.h b/modules.h index 9a1a4d41..0dd4f651 100644 --- a/modules.h +++ b/modules.h @@ -32,6 +32,7 @@ #define MODULES_H_INCLUDED 1 #include "objomsr.h" +#include "threads.h" typedef enum eModType_ { eMOD_IN, /* input module */ @@ -76,6 +77,7 @@ typedef struct moduleInfo { /* TODO: pass pointer to msg submit function to IM rger, 2007-12-14 */ union { struct {/* data for input modules */ + eTermSyncType_t eTermSyncType; rsRetVal (*runInput)(void); /* function to gather input and submit to queue */ } im; struct {/* data for output modules */ diff --git a/plugins/immark/immark.c b/plugins/immark/immark.c index a43ea61e..3078958b 100644 --- a/plugins/immark/immark.c +++ b/plugins/immark/immark.c @@ -43,6 +43,7 @@ #include "module-template.h" MODULE_TYPE_INPUT +TERM_SYNC_TYPE(eTermSync_SIGNAL) /* Module static data */ /* TODO: this needs a lot of work ;) */ @@ -84,7 +85,7 @@ dbgprintf("immark post select, doing mark, bFinished: %d\n", bFinished); logmsgInternal(LOG_INFO, "-- MARK --", ADDDATE); //logmsgInternal(LOG_INFO, "-- MARK --", ADDDATE|MARK); } -fprintf(stderr, "immark: finished!\n"); +dbgprintf("immark: finished!\n"); return RS_RET_OK; ENDrunInput diff --git a/syslogd.c b/syslogd.c index a0e43587..53874f46 100644 --- a/syslogd.c +++ b/syslogd.c @@ -4246,7 +4246,7 @@ startInputModules(void) while(pMod != NULL) { /* activate here */ dbgprintf("thread creating...\n"); - thrdCreate(pMod->mod.im.runInput); + thrdCreate(pMod->mod.im.runInput, pMod->mod.im.eTermSyncType); pMod = modGetNxtType(pMod, eMOD_IN); } @@ -5323,7 +5323,8 @@ int decode(uchar *name, struct code *codetab) } extern void dbgprintf(char *fmt, ...) __attribute__((format(printf,1, 2))); -void dbgprintf(char *fmt, ...) +void +dbgprintf(char *fmt, ...) { static int bWasNL = FALSE; va_list ap; @@ -5342,7 +5343,7 @@ void dbgprintf(char *fmt, ...) * rgerhards, 2007-06-15 */ if(bWasNL) { - fprintf(stdout, "%8.8d: ", (unsigned int) pthread_self()); + fprintf(stdout, "%8.8x: ", (unsigned int) pthread_self()); } bWasNL = (*(fmt + strlen(fmt) - 1) == '\n') ? TRUE : FALSE; va_start(ap, fmt); @@ -6118,10 +6119,6 @@ static void mainThread() mainloop(); } -static void sigusr2Dummy(int sig) -{ -dbgprintf("sigusr2Dummy called!\n"); -} /* This is the main entry point into rsyslogd. Over time, we should try to * modularize it a bit more... @@ -6395,8 +6392,6 @@ int main(int argc, char **argv) sigaction(SIGALRM, &sigAct, NULL); sigAct.sa_handler = Debug ? debug_switch : SIG_IGN; sigaction(SIGUSR1, &sigAct, NULL); - sigAct.sa_handler = sigusr2Dummy; - sigaction(SIGUSR2, &sigAct, NULL); sigAct.sa_handler = SIG_IGN; sigaction(SIGPIPE, &sigAct, NULL); sigaction(SIGXFSZ, &sigAct, NULL); /* do not abort if 2gig file limit is hit */ diff --git a/threads.c b/threads.c index 96911403..139cf835 100644 --- a/threads.c +++ b/threads.c @@ -118,7 +118,7 @@ dbgprintf("thrdTerminateAll out\n"); * executing threads. It is added at the end of the list. * rgerhards, 2007-12-14 */ -rsRetVal thrdCreate(void* (*thrdMain)(void*)) +rsRetVal thrdCreate(void* (*thrdMain)(void*), eTermSyncType_t eTermSyncType) { DEFiRet; thrdInfo_t *pThis; @@ -127,6 +127,8 @@ rsRetVal thrdCreate(void* (*thrdMain)(void*)) assert(thrdMain != NULL); CHKiRet(thrdConstruct(&pThis)); + pThis->eTermTool = eTermSyncType; + pThis->bIsActive = 1; i = pthread_create(&pThis->thrdID, NULL, thrdMain, NULL); CHKiRet(llAppend(&llThrds, NULL, pThis)); @@ -135,15 +137,31 @@ finalize_it: } +/* This is a dummy handler. We user SIGUSR2 to interrupt blocking system calls + * if we are in termination mode 1. + */ +static void sigusr2Dummy(int sig) +{ + dbgprintf("sigusr2Dummy called!\n"); +} + + /* initialize the thread-support subsystem * must be called once at the start of the program */ rsRetVal thrdInit(void) { DEFiRet; + struct sigaction sigAct; iRet = llInit(&llThrds, thrdDestruct, NULL, NULL); + /* set up our termination subsystem */ + memset(&sigAct, 0, sizeof (sigAct)); + sigemptyset(&sigAct.sa_mask); + sigAct.sa_handler = sigusr2Dummy; + sigaction(SIGUSR2, &sigAct, NULL); + return iRet; } diff --git a/threads.h b/threads.h index 08b8481a..1075ec7f 100644 --- a/threads.h +++ b/threads.h @@ -49,9 +49,11 @@ typedef struct { } msgQueue; /* prototypes */ +rsRetVal thrdExit(void); +rsRetVal thrdInit(void); rsRetVal thrdTerminate(thrdInfo_t *pThis); rsRetVal thrdTerminateAll(void); -rsRetVal thrdCreate(void* (*thrdMain)(void*)); +rsRetVal thrdCreate(void* (*thrdMain)(void*), eTermSyncType_t eTermSyncType); msgQueue *queueInit (void); void queueDelete (msgQueue *q); void queueAdd (msgQueue *q, void* in); -- cgit v1.2.3