summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorRainer Gerhards <rgerhards@adiscon.com>2008-10-23 14:46:47 +0200
committerRainer Gerhards <rgerhards@adiscon.com>2008-10-23 14:46:47 +0200
commit6334d335d89ae5df344f833c5095e9dea2abf6fb (patch)
tree9cd1a8de9e3b94e72b38b0612ba4d90cadccd81d /tools
parentcf38fc81759b01af5125b1a05e0d6fe8e2e1bc21 (diff)
downloadrsyslog-6334d335d89ae5df344f833c5095e9dea2abf6fb.tar.gz
rsyslog-6334d335d89ae5df344f833c5095e9dea2abf6fb.tar.xz
rsyslog-6334d335d89ae5df344f833c5095e9dea2abf6fb.zip
added configuration directive "HUPisRestart"
...which enables to configure HUP to be either a full restart or "just" a leightweight way to close open files
Diffstat (limited to 'tools')
-rw-r--r--tools/omfile.c102
-rw-r--r--tools/syslogd.c59
2 files changed, 114 insertions, 47 deletions
diff --git a/tools/omfile.c b/tools/omfile.c
index 8144386f..00a82933 100644
--- a/tools/omfile.c
+++ b/tools/omfile.c
@@ -174,7 +174,7 @@ rsRetVal setDynaFileCacheSize(void __attribute__((unused)) *pVal, int iNewVal)
}
iDynaFileCacheSize = iNewVal;
- dbgprintf("DynaFileCacheSize changed to %d.\n", iNewVal);
+ DBGPRINTF("DynaFileCacheSize changed to %d.\n", iNewVal);
RETiRet;
}
@@ -244,7 +244,6 @@ static rsRetVal cflineParseOutchannel(instanceData *pData, uchar* p, omodStringR
*/
pData->f_sizeLimitCmd = (char*) pOch->cmdOnSizeLimit;
-RUNLOG_VAR("%p", pszTplName);
iRet = cflineParseTemplateName(&p, pOMSR, iEntry, iTplOpts,
(pszTplName == NULL) ? (uchar*)"RSYSLOG_FileFormat" : pszTplName);
@@ -327,7 +326,7 @@ static void dynaFileDelCacheEntry(dynaFileCacheEntry **pCache, int iEntry, int b
if(pCache[iEntry] == NULL)
FINALIZE;
- dbgprintf("Removed entry %d for file '%s' from dynaCache.\n", iEntry,
+ DBGPRINTF("Removed entry %d for file '%s' from dynaCache.\n", iEntry,
pCache[iEntry]->pName == NULL ? "[OPEN FAILED]" : (char*)pCache[iEntry]->pName);
/* if the name is NULL, this is an improperly initilized entry which
* needs to be discarded. In this case, neither the file is to be closed
@@ -349,9 +348,11 @@ finalize_it:
}
-/* This function frees the dynamic file name cache.
+/* This function frees all dynamic file name cache entries and closes the
+ * relevant files. Part of Shutdown and HUP processing.
+ * rgerhards, 2008-10-23
*/
-static void dynaFileFreeCache(instanceData *pData)
+static inline void dynaFileFreeCacheEntries(instanceData *pData)
{
register int i;
ASSERT(pData != NULL);
@@ -360,17 +361,36 @@ static void dynaFileFreeCache(instanceData *pData)
for(i = 0 ; i < pData->iCurrCacheSize ; ++i) {
dynaFileDelCacheEntry(pData->dynCache, i, 1);
}
+ ENDfunc;
+}
+
+
+/* This function frees the dynamic file name cache.
+ */
+static void dynaFileFreeCache(instanceData *pData)
+{
+ ASSERT(pData != NULL);
+ BEGINfunc;
+ dynaFileFreeCacheEntries(pData);
if(pData->dynCache != NULL)
d_free(pData->dynCache);
ENDfunc;
}
-/* This is a shared code for both static and dynamic files.
+/* This is now shared code for all types of files. It simply prepares
+ * file access, which, among others, means the the file wil be opened
+ * and any directories in between will be created (based on config, of
+ * course). -- rgerhards, 2008-10-22
*/
static void prepareFile(instanceData *pData, uchar *newFileName)
{
+ if(pData->fileType == eTypePIPE) {
+ pData->fd = open((char*) pData->f_fname, O_RDWR|O_NONBLOCK);
+ FINALIZE; /* we are done in this case */
+ }
+
if(access((char*)newFileName, F_OK) == 0) {
/* file already exists */
pData->fd = open((char*) newFileName, O_WRONLY|O_APPEND|O_CREAT|O_NOCTTY,
@@ -392,8 +412,7 @@ static void prepareFile(instanceData *pData, uchar *newFileName)
/* check and set uid/gid */
if(pData->fileUID != (uid_t)-1 || pData->fileGID != (gid_t) -1) {
/* we need to set owner/group */
- if(fchown(pData->fd, pData->fileUID,
- pData->fileGID) != 0) {
+ if(fchown(pData->fd, pData->fileUID, pData->fileGID) != 0) {
if(pData->bFailOnChown) {
int eSave = errno;
close(pData->fd);
@@ -409,6 +428,12 @@ static void prepareFile(instanceData *pData, uchar *newFileName)
}
}
}
+finalize_it:
+ if((pData->fd) != 0 && isatty(pData->fd)) {
+ DBGPRINTF("file %d is a tty file\n", pData->fd);
+ pData->fileType = eTypeTTY;
+ untty();
+ }
}
@@ -482,7 +507,7 @@ static int prepareDynFile(instanceData *pData, uchar *newFileName, unsigned iMsg
/* we need to allocate memory for the cache structure */
pCache[iFirstFree] = (dynaFileCacheEntry*) calloc(1, sizeof(dynaFileCacheEntry));
if(pCache[iFirstFree] == NULL) {
- dbgprintf("prepareDynfile(): could not alloc mem, discarding this request\n");
+ DBGPRINTF("prepareDynfile(): could not alloc mem, discarding this request\n");
return -1;
}
}
@@ -496,10 +521,11 @@ static int prepareDynFile(instanceData *pData, uchar *newFileName, unsigned iMsg
* message. Otherwise, we could run into a never-ending loop. The bad
* news is that we also lose errors on startup messages, but so it is.
*/
- if(iMsgOpts & INTERNAL_MSG)
- dbgprintf("Could not open dynaFile, discarding message\n");
- else
+ if(iMsgOpts & INTERNAL_MSG) {
+ DBGPRINTF("Could not open dynaFile, discarding message\n");
+ } else {
errmsg.LogError(0, NO_ERRCODE, "Could not open dynamic file '%s' - discarding message", (char*)newFileName);
+ }
dynaFileDelCacheEntry(pCache, iFirstFree, 1);
pData->iCurrElt = -1;
return -1;
@@ -509,8 +535,7 @@ static int prepareDynFile(instanceData *pData, uchar *newFileName, unsigned iMsg
pCache[iFirstFree]->pName = (uchar*)strdup((char*)newFileName); /* TODO: check for NULL (very unlikely) */
pCache[iFirstFree]->lastUsed = time(NULL);
pData->iCurrElt = iFirstFree;
- dbgprintf("Added new entry %d for file cache, file '%s'.\n",
- iFirstFree, newFileName);
+ DBGPRINTF("Added new entry %d for file cache, file '%s'.\n", iFirstFree, newFileName);
return 0;
}
@@ -533,6 +558,8 @@ static rsRetVal writeFile(uchar **ppString, unsigned iMsgOpts, instanceData *pDa
if(pData->bDynamicName) {
if(prepareDynFile(pData, ppString[1], iMsgOpts) != 0)
ABORT_FINALIZE(RS_RET_ERR);
+ } else if(pData->fd == -1) {
+ prepareFile(pData, pData->f_fname);
}
/* create the message based on format specified */
@@ -584,11 +611,10 @@ again:
ABORT_FINALIZE(RS_RET_OK);
(void) close(pData->fd);
- /*
- * Check for EBADF on TTY's due to vhangup()
+ /* Check for EBADF on TTY's due to vhangup()
* Linux uses EIO instead (mrn 12 May 96)
*/
- if ((pData->fileType == eTypeTTY || pData->fileType == eTypeCONSOLE)
+ if((pData->fileType == eTypeTTY || pData->fileType == eTypeCONSOLE)
#ifdef linux
&& e == EIO) {
#else
@@ -637,13 +663,8 @@ ENDtryResume
BEGINdoAction
CODESTARTdoAction
- dbgprintf(" (%s)\n", pData->f_fname);
- /* pData->fd == -1 is an indicator that the we couldn't
- * open the file at startup. For dynaFiles, this is ok,
- * all others are doomed.
- */
- if(pData->bDynamicName || (pData->fd != -1))
- iRet = writeFile(ppString, iMsgOpts, pData);
+ DBGPRINTF(" (%s)\n", pData->f_fname);
+ iRet = writeFile(ppString, iMsgOpts, pData);
ENDdoAction
@@ -726,7 +747,7 @@ CODESTARTparseSelectorAct
if((pData->dynCache = (dynaFileCacheEntry**)
calloc(iDynaFileCacheSize, sizeof(dynaFileCacheEntry*))) == NULL) {
iRet = RS_RET_OUT_OF_MEMORY;
- dbgprintf("Could not allocate memory for dynaFileCache - selector disabled.\n");
+ DBGPRINTF("Could not allocate memory for dynaFileCache - selector disabled.\n");
}
break;
@@ -760,23 +781,15 @@ CODESTARTparseSelectorAct
pData->dirUID = dirUID;
pData->dirGID = dirGID;
- if(pData->fileType == eTypePIPE) {
- pData->fd = open((char*) pData->f_fname, O_RDWR|O_NONBLOCK);
- } else {
- prepareFile(pData, pData->f_fname);
- }
+ prepareFile(pData, pData->f_fname);
- if ( pData->fd < 0 ){
+ if(pData->fd < 0 ) {
pData->fd = -1;
- dbgprintf("Error opening log file: %s\n", pData->f_fname);
+ DBGPRINTF("Error opening log file: %s\n", pData->f_fname);
errmsg.LogError(0, NO_ERRCODE, "%s", pData->f_fname);
break;
}
- if (isatty(pData->fd)) {
- pData->fileType = eTypeTTY;
- untty();
- }
- if (strcmp((char*) p, _PATH_CONSOLE) == 0)
+ if(strcmp((char*) p, _PATH_CONSOLE) == 0)
pData->fileType = eTypeCONSOLE;
break;
default:
@@ -811,6 +824,20 @@ static rsRetVal resetConfigVariables(uchar __attribute__((unused)) *pp, void __a
}
+BEGINdoHUP
+CODESTARTdoHUP
+ if(pData->bDynamicName) {
+ dynaFileFreeCacheEntries(pData);
+ pData->iCurrElt = -1; /* invalidate current element */
+ } else {
+ if(pData->fd != -1) {
+ close(pData->fd);
+ pData->fd = -1;
+ }
+ }
+ENDdoHUP
+
+
BEGINmodExit
CODESTARTmodExit
if(pszTplName != NULL)
@@ -821,6 +848,7 @@ ENDmodExit
BEGINqueryEtryPt
CODESTARTqueryEtryPt
CODEqueryEtryPt_STD_OMOD_QUERIES
+CODEqueryEtryPt_doHUP
ENDqueryEtryPt
diff --git a/tools/syslogd.c b/tools/syslogd.c
index 13696955..7145779d 100644
--- a/tools/syslogd.c
+++ b/tools/syslogd.c
@@ -220,7 +220,7 @@ static char *PidFile = _PATH_LOGPID; /* read-only after startup */
static pid_t myPid; /* our pid for use in self-generated messages, e.g. on startup */
/* mypid is read-only after the initial fork() */
-static int restart = 0; /* do restart (config read) - multithread safe */
+static int bHadHUP = 0; /* did we have a HUP? */
static int bParseHOSTNAMEandTAG = 1; /* global config var: should the hostname and tag be
* parsed inside message - rgerhards, 2006-03-13 */
@@ -2543,20 +2543,18 @@ static rsRetVal setMainMsgQueType(void __attribute__((unused)) *pVal, uchar *psz
* The following function is resposible for handling a SIGHUP signal. Since
* we are now doing mallocs/free as part of init we had better not being
* doing this during a signal handler. Instead this function simply sets
- * a flag variable which will tell the main loop to go through a restart.
+ * a flag variable which will tells the main loop to do "the right thing".
*/
void sighup_handler()
{
struct sigaction sigAct;
- restart = 1;
+ bHadHUP = 1;
memset(&sigAct, 0, sizeof (sigAct));
sigemptyset(&sigAct.sa_mask);
sigAct.sa_handler = sighup_handler;
sigaction(SIGHUP, &sigAct, NULL);
-
- return;
}
@@ -2578,6 +2576,49 @@ static void processImInternal(void)
}
+/* helper to doHUP(), this "HUPs" each action. The necessary locking
+ * is done inside the action class and nothing we need to take care of.
+ * rgerhards, 2008-10-22
+ */
+DEFFUNC_llExecFunc(doHUPActions)
+{
+ BEGINfunc
+ actionCallHUPHdlr((action_t*) pData);
+ ENDfunc
+ return RS_RET_OK; /* we ignore errors, we can not do anything either way */
+}
+
+
+/* This function processes a HUP after one has been detected. Note that this
+ * is *NOT* the sighup handler. The signal is recorded by the handler, that record
+ * detected inside the mainloop and then this function is called to do the
+ * real work. -- rgerhards, 2008-10-22
+ */
+static inline void
+doHUP(void)
+{
+ selector_t *f;
+ char buf[512];
+
+ snprintf(buf, sizeof(buf) / sizeof(char),
+ " [origin software=\"rsyslogd\" " "swVersion=\"" VERSION
+ "\" x-pid=\"%d\" x-info=\"http://www.rsyslog.com\"] rsyslogd was HUPed, type '%s'.",
+ (int) myPid, glbl.GetHUPisRestart() ? "restart" : "lightweight");
+ errno = 0;
+ logmsgInternal(NO_ERRCODE, LOG_SYSLOG|LOG_INFO, (uchar*)buf, 0);
+
+ if(glbl.GetHUPisRestart()) {
+ DBGPRINTF("Received SIGHUP, configured to be restart, reloading rsyslogd.\n");
+ init(); /* main queue is stopped as part of init() */
+ } else {
+ DBGPRINTF("Received SIGHUP, configured to be a non-restart type of HUP - notifying actions.\n");
+ for(f = Files; f != NULL ; f = f->f_next) {
+ llExecFunc(&f->llActList, doHUPActions, NULL);
+ }
+ }
+}
+
+
/* This is the main processing loop. It is called after successful initialization.
* When it returns, the syslogd terminates.
* Its sole function is to provide some housekeeping things. The real work is done
@@ -2634,11 +2675,9 @@ mainloop(void)
if(bReduceRepeatMsgs == 1)
doFlushRptdMsgs();
- if(restart) {
- dbgprintf("\nReceived SIGHUP, reloading rsyslogd.\n");
- /* main queue is stopped as part of init() */
- init();
- restart = 0;
+ if(bHadHUP) {
+ doHUP();
+ bHadHUP = 0;
continue;
}
}