refactor time-obtaining functions so that the can obtain time in UTC

This works where the time is picked up locally.
This commit is contained in:
Rainer Gerhards 2016-01-11 18:52:42 +01:00
parent 8e974a3679
commit e8c8e96e27
9 changed files with 50 additions and 34 deletions

View File

@ -210,7 +210,7 @@ doInjectMsg(int iNum, ratelimit_t *ratelimiter)
snprintf((char*)szMsg, sizeof(szMsg),
"<167>Mar 1 01:00:00 172.20.245.8 tag msgnum:%8.8d:", iNum);
datetime.getCurrTime(&stTime, &ttGenTime);
datetime.getCurrTime(&stTime, &ttGenTime, TIME_IN_LOCALTIME);
/* we now create our own message object and submit it to the queue */
CHKiRet(msgConstructWithTime(&pMsg, &stTime, ttGenTime));
MsgSetRawMsg(pMsg, (char*) szMsg, ustrlen(szMsg));

View File

@ -137,7 +137,7 @@ enqMsg(uchar *const __restrict__ msg, uchar* pszTag, const syslog_pri_t pri, str
if(tp == NULL) {
CHKiRet(msgConstruct(&pMsg));
} else {
datetime.timeval2syslogTime(tp, &st);
datetime.timeval2syslogTime(tp, &st, TIME_IN_LOCALTIME);
CHKiRet(msgConstructWithTime(&pMsg, &st, tp->tv_sec));
}
MsgSetFlowControlType(pMsg, eFLOWCTL_LIGHT_DELAY);

View File

@ -868,7 +868,7 @@ DataRcvdUncompressed(ptcpsess_t *pThis, char *pData, size_t iLen, struct syslogT
assert(iLen > 0);
if(ttGenTime == 0)
datetime.getCurrTime(stTime, &ttGenTime);
datetime.getCurrTime(stTime, &ttGenTime, TIME_IN_LOCALTIME);
multiSub.ppMsgs = pMsgs;
multiSub.maxElem = CONF_NUM_MULTISUB;
multiSub.nElem = 0;
@ -899,7 +899,7 @@ DataRcvdCompressed(ptcpsess_t *pThis, char *buf, size_t len)
// by simply updating the input and output sizes?
uint64_t outtotal;
datetime.getCurrTime(&stTime, &ttGenTime);
datetime.getCurrTime(&stTime, &ttGenTime, TIME_IN_LOCALTIME);
outtotal = 0;
if(!pThis->bzInitDone) {

View File

@ -509,7 +509,7 @@ processSocket(struct wrkrInfo_s *pWrkr, struct lstn_s *lstn, struct sockaddr_sto
}
if((runModConf->iTimeRequery == 0) || (iNbrTimeUsed++ % runModConf->iTimeRequery) == 0) {
datetime.getCurrTime(&stTime, &ttGenTime);
datetime.getCurrTime(&stTime, &ttGenTime, TIME_IN_LOCALTIME);
}
pWrkr->ctrMsgsRcvd += nelem;

View File

@ -791,9 +791,9 @@ SubmitMsg(uchar *pRcv, int lenRcv, lstn_t *pLstn, struct ucred *cred, struct tim
findRatelimiter(pLstn, cred, &ratelimiter); /* ignore error, better so than others... */
if(ts == NULL) {
datetime.getCurrTime(&st, &tt);
datetime.getCurrTime(&st, &tt, TIME_IN_LOCALTIME);
} else {
datetime.timeval2syslogTime(ts, &st);
datetime.timeval2syslogTime(ts, &st, TIME_IN_LOCALTIME);
tt = ts->tv_sec;
}

View File

@ -90,7 +90,7 @@ static const time_t yearInSecs[] = {
* Convert struct timeval to syslog_time
*/
void
timeval2syslogTime(struct timeval *tp, struct syslogTime *t)
timeval2syslogTime(struct timeval *tp, struct syslogTime *t, const int inUTC)
{
struct tm *tm;
struct tm tmBuf;
@ -98,7 +98,10 @@ timeval2syslogTime(struct timeval *tp, struct syslogTime *t)
time_t secs;
secs = tp->tv_sec;
tm = localtime_r(&secs, &tmBuf);
if(inUTC)
tm = gmtime_r(&secs, &tmBuf);
else
tm = localtime_r(&secs, &tmBuf);
t->year = tm->tm_year + 1900;
t->month = tm->tm_mon + 1;
@ -109,21 +112,26 @@ timeval2syslogTime(struct timeval *tp, struct syslogTime *t)
t->secfrac = tp->tv_usec;
t->secfracPrecision = 6;
# if __sun
/* Solaris uses a different method of exporting the time zone.
* It is UTC - localtime, which is the opposite sign of mins east of GMT.
*/
lBias = -(tm->tm_isdst ? altzone : timezone);
# elif defined(__hpux)
lBias = tz.tz_dsttime ? - tz.tz_minuteswest : 0;
# else
lBias = tm->tm_gmtoff;
# endif
if(lBias < 0) {
t->OffsetMode = '-';
lBias *= -1;
} else
if(inUTC) {
t->OffsetMode = '+';
lBias = 0;
} else {
# if __sun
/* Solaris uses a different method of exporting the time zone.
* It is UTC - localtime, which is the opposite sign of mins east of GMT.
*/
lBias = -(tm->tm_isdst ? altzone : timezone);
# elif defined(__hpux)
lBias = tz.tz_dsttime ? - tz.tz_minuteswest : 0;
# else
lBias = tm->tm_gmtoff;
# endif
if(lBias < 0) {
t->OffsetMode = '-';
lBias *= -1;
} else
t->OffsetMode = '+';
}
t->OffsetHour = lBias / 3600;
t->OffsetMinute = (lBias % 3600) / 60;
t->timeType = TIME_TYPE_RFC5424; /* we have a high precision timestamp */
@ -145,7 +153,7 @@ timeval2syslogTime(struct timeval *tp, struct syslogTime *t)
* in some situations to minimize time() calls (namely when doing
* output processing). This can be left NULL if not needed.
*/
static void getCurrTime(struct syslogTime *t, time_t *ttSeconds)
static void getCurrTime(struct syslogTime *t, time_t *ttSeconds, const int inUTC)
{
struct timeval tp;
# if defined(__hpux)
@ -164,7 +172,7 @@ static void getCurrTime(struct syslogTime *t, time_t *ttSeconds)
if(ttSeconds != NULL)
*ttSeconds = tp.tv_sec;
timeval2syslogTime(&tp, t);
timeval2syslogTime(&tp, t, inUTC);
}

View File

@ -32,7 +32,7 @@ typedef struct datetime_s {
/* interfaces */
BEGINinterface(datetime) /* name must also be changed in ENDinterface macro! */
void (*getCurrTime)(struct syslogTime *t, time_t *ttSeconds);
void (*getCurrTime)(struct syslogTime *t, time_t *ttSeconds, const int inUTC);
rsRetVal (*ParseTIMESTAMP3339)(struct syslogTime *pTime, uchar** ppszTS, int*);
rsRetVal (*ParseTIMESTAMP3164)(struct syslogTime *pTime, uchar** pszTS, int*, const int bParseTZ, const int bDetectYearAfterTime);
int (*formatTimestampToMySQL)(struct syslogTime *ts, char* pDst);
@ -42,13 +42,13 @@ BEGINinterface(datetime) /* name must also be changed in ENDinterface macro! */
int (*formatTimestampSecFrac)(struct syslogTime *ts, char* pBuf);
/* v3, 2009-11-12 */
time_t (*GetTime)(time_t *ttSeconds);
/* v6, 2011-06-20 */
void (*timeval2syslogTime)(struct timeval *tp, struct syslogTime *t);
/* v6, 2011-06-20 , v10, 2016-01-12*/
void (*timeval2syslogTime)(struct timeval *tp, struct syslogTime *t, const int inUTC);
/* v7, 2012-03-29 */
int (*formatTimestampUnix)(struct syslogTime *ts, char*pBuf);
time_t (*syslogTime2time_t)(struct syslogTime *ts);
ENDinterface(datetime)
#define datetimeCURR_IF_VERSION 9 /* increment whenever you change the interface structure! */
#define datetimeCURR_IF_VERSION 10 /* increment whenever you change the interface structure! */
/* interface changes:
* 1 - initial version
* 2 - not compatible to 1 - bugfix required ParseTIMESTAMP3164 to accept char ** as
@ -60,6 +60,8 @@ ENDinterface(datetime)
* 6 - see above
* 8 - ParseTIMESTAMP3164 has addtl parameter to permit TZ string parsing
* 9 - ParseTIMESTAMP3164 has addtl parameter to permit year parsing
* 10 - functions having addtl paramater inUTC to emit time in UTC:
* timeval2syslogTime, getCurrtime
*/
#define PARSE3164_TZSTRING 1
@ -68,6 +70,12 @@ ENDinterface(datetime)
#define PERMIT_YEAR_AFTER_TIME 1
#define NO_PERMIT_YEAR_AFTER_TIME 0
/* two defines for functions that create timestamps either in local
* time or UTC.
*/
#define TIME_IN_UTC 1
#define TIME_IN_LOCALTIME 0
/* prototypes */
PROTOTYPEObj(datetime);
void applyDfltTZ(struct syslogTime *pTime, char *tz);

View File

@ -852,7 +852,7 @@ rsRetVal msgConstruct(msg_t **ppThis)
* especially as I think there is no codepath currently where it would not be
* required (after I have cleaned up the pathes ;)). -- rgerhards, 2008-10-02
*/
datetime.getCurrTime(&((*ppThis)->tRcvdAt), &((*ppThis)->ttGenTime));
datetime.getCurrTime(&((*ppThis)->tRcvdAt), &((*ppThis)->ttGenTime), TIME_IN_LOCALTIME);
memcpy(&(*ppThis)->tTIMESTAMP, &(*ppThis)->tRcvdAt, sizeof(struct syslogTime));
finalize_it:
@ -2759,12 +2759,12 @@ static uchar *getNOW(eNOWType eNow, struct syslogTime *t)
}
if(t == NULL) { /* can happen if called via script engine */
datetime.getCurrTime(&tt, NULL);
datetime.getCurrTime(&tt, NULL, TIME_IN_LOCALTIME);
t = &tt;
}
if(t->year == 0) { /* not yet set! */
datetime.getCurrTime(t, NULL);
datetime.getCurrTime(t, NULL, TIME_IN_LOCALTIME);
}
switch(eNow) {

View File

@ -315,7 +315,7 @@ PrepareClose(tcps_sess_t *pThis)
* this case.
*/
DBGPRINTF("Extra data at end of stream in legacy syslog/tcp message - processing\n");
datetime.getCurrTime(&stTime, &ttGenTime);
datetime.getCurrTime(&stTime, &ttGenTime, TIME_IN_LOCALTIME);
defaultDoSubmitMessage(pThis, &stTime, ttGenTime, NULL);
}
@ -476,7 +476,7 @@ DataRcvd(tcps_sess_t *pThis, char *pData, size_t iLen)
assert(pData != NULL);
assert(iLen > 0);
datetime.getCurrTime(&stTime, &ttGenTime);
datetime.getCurrTime(&stTime, &ttGenTime, TIME_IN_LOCALTIME);
multiSub.ppMsgs = pMsgs;
multiSub.maxElem = NUM_MULTISUB;
multiSub.nElem = 0;