From 7b456ce548030ff362d3a2be04b1e5c2c89e0dcb Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Thu, 14 Aug 2008 11:19:02 +0200 Subject: bugfix: imfile could cause a segfault upon rsyslogd HUP and termination Thanks to lperr for an excellent bug report that helped detect this problem. --- plugins/imfile/imfile.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'plugins/imfile') diff --git a/plugins/imfile/imfile.c b/plugins/imfile/imfile.c index 75e54f04..6b7f516e 100644 --- a/plugins/imfile/imfile.c +++ b/plugins/imfile/imfile.c @@ -182,8 +182,9 @@ static void pollFileCancelCleanup(void *pArg) /* poll a file, need to check file rollover etc. open file if not open */ static rsRetVal pollFile(fileInfo_t *pThis, int *pbHadFileData) { - DEFiRet; cstr_t *pCStr = NULL; + int bMustPopCleanup = 0; + DEFiRet; ASSERT(pbHadFileData != NULL); @@ -192,6 +193,8 @@ static rsRetVal pollFile(fileInfo_t *pThis, int *pbHadFileData) } pthread_cleanup_push(pollFileCancelCleanup, &pCStr); + bMustPopCleanup = 1; + /* loop below will be exited when strmReadLine() returns EOF */ while(1) { CHKiRet(strmReadLine(pThis->pStrm, &pCStr)); @@ -199,9 +202,11 @@ static rsRetVal pollFile(fileInfo_t *pThis, int *pbHadFileData) CHKiRet(enqLine(pThis, pCStr)); /* process line */ rsCStrDestruct(&pCStr); /* discard string (must be done by us!) */ } - pthread_cleanup_pop(0); finalize_it: + if(bMustPopCleanup) + pthread_cleanup_pop(0); + if(pCStr != NULL) { rsCStrDestruct(&pCStr); } -- cgit From 3aa86ed554aeb05c386e99d605ddc220250d35d2 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Fri, 15 Aug 2008 12:21:26 +0200 Subject: fixed cross-platform compile problem introduced with recent change ...which fixed the imfile segfault issue. --- plugins/imfile/imfile.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'plugins/imfile') diff --git a/plugins/imfile/imfile.c b/plugins/imfile/imfile.c index 6b7f516e..2df1aaf7 100644 --- a/plugins/imfile/imfile.c +++ b/plugins/imfile/imfile.c @@ -183,18 +183,18 @@ static void pollFileCancelCleanup(void *pArg) static rsRetVal pollFile(fileInfo_t *pThis, int *pbHadFileData) { cstr_t *pCStr = NULL; - int bMustPopCleanup = 0; DEFiRet; ASSERT(pbHadFileData != NULL); + /* Note: we must do pthread_cleanup_push() immediately, because the POXIS macros + * otherwise do not work if I include the _cleanup_pop() inside an if... -- rgerhards, 2008-08-14 + */ + pthread_cleanup_push(pollFileCancelCleanup, &pCStr); if(pThis->pStrm == NULL) { CHKiRet(openFile(pThis)); /* open file */ } - pthread_cleanup_push(pollFileCancelCleanup, &pCStr); - bMustPopCleanup = 1; - /* loop below will be exited when strmReadLine() returns EOF */ while(1) { CHKiRet(strmReadLine(pThis->pStrm, &pCStr)); @@ -204,8 +204,15 @@ static rsRetVal pollFile(fileInfo_t *pThis, int *pbHadFileData) } finalize_it: - if(bMustPopCleanup) - pthread_cleanup_pop(0); + /*EMPTY - just to keep the compiler happy, do NOT remove*/; + /* Note: the problem above is that pthread:cleanup_pop() is a macro which + * evaluates to something like "} while(0);". So the code would become + * "finalize_it: }", that is a label without a statement. The C standard does + * not permit this. So we add an empty statement "finalize_it: ; }" and + * everybody is happy. Note that without the ;, an error is reported only + * on some platforms/compiler versions. -- rgerhards, 2008-08-15 + */ + pthread_cleanup_pop(0); if(pCStr != NULL) { rsCStrDestruct(&pCStr); -- cgit