From 71219e4c74c2e69284804f41ff6e32fb9452815b Mon Sep 17 00:00:00 2001 From: Ken Steele Date: Fri, 30 Aug 2013 12:34:43 -0400 Subject: [PATCH] On Open BSD systems don't cache time. Open BSD doesn't support __thread, which is used for time caching, so don't do time chaching for BSD systems. --- src/util-time.c | 55 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/src/util-time.c b/src/util-time.c index 996d7a1934d4..20aec91d4419 100644 --- a/src/util-time.c +++ b/src/util-time.c @@ -35,19 +35,6 @@ static struct timeval current_time = { 0, 0 }; static SCSpinlock current_time_spinlock; static char live = TRUE; -/* Values for caching in CreateTimeString */ -#define MAX_LOCAL_TIME_STRING 128 -static __thread int mru_time_slot; /* Most recently used cached value */ -static __thread time_t last_local_time[2]; -static __thread short int cached_local_time_len[2]; -static __thread char cached_local_time[2][MAX_LOCAL_TIME_STRING]; - -/* Values for caching SCLocalTime() These cached values are - * independent from the CreateTimeString cached values. */ -static __thread int mru_tm_slot; /* Most recently used local tm */ -static __thread time_t cached_minute_start[2]; -static __thread struct tm cached_local_tm[2]; - void TimeInit(void) { SCSpinInit(¤t_time_spinlock, 0); @@ -133,6 +120,46 @@ void TimeSetIncrementTime(uint32_t tv_sec) TimeSet(&tv); } +/* + * Time Caching code + */ + +#if defined(__OpenBSD__) +/* OpenBSD does not support __thread, so don't use time caching on BSD + */ +struct tm *SCLocalTime(time_t timep, struct tm *result) +{ + return localtime_r(&timep, result); +} + +void CreateTimeString (const struct timeval *ts, char *str, size_t size) +{ + time_t time = ts->tv_sec; + struct tm local_tm; + struct tm *t = (struct tm*)SCLocalTime(time, &local_tm); + + snprintf(str, size, "%02d/%02d/%02d-%02d:%02d:%02d.%06u", + t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour, + t->tm_min, t->tm_sec, (uint32_t) ts->tv_usec); +} + +#else + +/* On systems supporting __thread, use Per-thread values for caching + * in CreateTimeString */ + +#define MAX_LOCAL_TIME_STRING 128 +static __thread int mru_time_slot; /* Most recently used cached value */ +static __thread time_t last_local_time[2]; +static __thread short int cached_local_time_len[2]; +static __thread char cached_local_time[2][MAX_LOCAL_TIME_STRING]; + +/* Per-thread values for caching SCLocalTime() These cached values are + * independent from the CreateTimeString cached values. */ +static __thread int mru_tm_slot; /* Most recently used local tm */ +static __thread time_t cached_minute_start[2]; +static __thread struct tm cached_local_tm[2]; + /** \brief Convert time_t into Year, month, day, hour and minutes. * \param timep Time in seconds since defined date. * \param result The structure into which the broken down time it put. @@ -245,3 +272,5 @@ void CreateTimeString (const struct timeval *ts, char *str, size_t size) "%02d.%06u", seconds, (uint32_t) ts->tv_usec); } + +#endif /* defined(__OpenBSD__) */