mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-10 05:03:43 +01:00
Patch to localtime/gmtime handling: use the _r variants where available. Use mutexes to fake _r where necessary. Make mutexes no-ops where no threading is enabled.
svn:r3653
This commit is contained in:
parent
d01718841e
commit
70c3580f81
@ -152,7 +152,7 @@ dnl These headers are not essential
|
||||
|
||||
AC_CHECK_HEADERS(stdint.h sys/types.h inttypes.h sys/param.h sys/wait.h sys/limits.h netinet/in.h arpa/inet.h machine/limits.h syslog.h sys/time.h sys/resource.h pthread.h stddef.h inttypes.h)
|
||||
|
||||
AC_CHECK_FUNCS(gettimeofday ftime socketpair uname inet_aton strptime getrlimit setrlimit strlcat strlcpy strtoull getpwnam ftello pthread_create getaddrinfo)
|
||||
AC_CHECK_FUNCS(gettimeofday ftime socketpair uname inet_aton strptime getrlimit setrlimit strlcat strlcpy strtoull getpwnam ftello pthread_create getaddrinfo localtime_r gmtime_r)
|
||||
AC_FUNC_FSEEKO
|
||||
|
||||
AC_CHECK_MEMBERS([struct timeval.tv_sec])
|
||||
|
@ -753,6 +753,40 @@ void tor_gettimeofday(struct timeval *timeval) {
|
||||
return;
|
||||
}
|
||||
|
||||
#ifndef HAVE_LOCALTIME_R
|
||||
struct tm *tor_localtime_r(const time_t *timep, struct tm *result)
|
||||
{
|
||||
struct tm *r;
|
||||
#ifdef TOR_IS_MULTITHREADED
|
||||
static tor_mutex_t *m=NULL;
|
||||
if (!m) { m=tor_mutex_new(); }
|
||||
#endif
|
||||
tor_assert(result);
|
||||
tor_mutex_acquire(m);
|
||||
r = localtime(timep);
|
||||
memcpy(result, r, sizeof(struct tm));
|
||||
tor_mutex_release(m);
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_GMTIME_R
|
||||
struct tm *tor_gmtime_r(const time_t *timep, struct tm *result)
|
||||
{
|
||||
struct tm *r;
|
||||
#ifdef TOR_IS_MULTITHREADED
|
||||
static tor_mutex_t *m=NULL;
|
||||
if (!m) { m=tor_mutex_new(); }
|
||||
#endif
|
||||
tor_assert(result);
|
||||
tor_mutex_acquire(m);
|
||||
r = gmtime(timep);
|
||||
memcpy(result, r, sizeof(struct tm));
|
||||
tor_mutex_release(m);
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_WIN32_THREADS
|
||||
struct tor_mutex_t {
|
||||
HANDLE handle;
|
||||
@ -833,11 +867,6 @@ tor_get_thread_id(void)
|
||||
struct tor_mutex_t {
|
||||
int _unused;
|
||||
};
|
||||
tor_mutex_t *tor_mutex_new(void) { return NULL; }
|
||||
void tor_mutex_acquire(tor_mutex_t *m) { }
|
||||
void tor_mutex_release(tor_mutex_t *m) { }
|
||||
void tor_mutex_free(tor_mutex_t *m) { }
|
||||
unsigned long tor_get_thread_id(void) { return 1; }
|
||||
#endif
|
||||
|
||||
/**
|
||||
|
@ -104,6 +104,18 @@ struct timeval {
|
||||
|
||||
void tor_gettimeofday(struct timeval *timeval);
|
||||
|
||||
#ifdef HAVE_LOCALTIME_R
|
||||
#define tor_localtime_r localtime_r
|
||||
#else
|
||||
struct tm *tor_localtime_r(const time_t *timep, struct tm *result);
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_GMTIME_R
|
||||
#define tor_gmtime_r gmtime_r
|
||||
#else
|
||||
struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
|
||||
#endif
|
||||
|
||||
/* ===== File compatibility */
|
||||
int replace_file(const char *from, const char *to);
|
||||
|
||||
@ -192,14 +204,6 @@ char *get_user_homedir(const char *username);
|
||||
int spawn_func(int (*func)(void *), void *data);
|
||||
void spawn_exit(void);
|
||||
|
||||
/* Because we use threads instead of processes on Windows, we need locking on
|
||||
* Windows. On Unixy platforms, these functions are no-ops. */
|
||||
typedef struct tor_mutex_t tor_mutex_t;
|
||||
tor_mutex_t *tor_mutex_new(void);
|
||||
void tor_mutex_acquire(tor_mutex_t *m);
|
||||
void tor_mutex_release(tor_mutex_t *m);
|
||||
void tor_mutex_free(tor_mutex_t *m);
|
||||
unsigned long tor_get_thread_id(void);
|
||||
|
||||
#if defined(MS_WINDOWS)
|
||||
#define USE_WIN32_THREADS
|
||||
@ -211,5 +215,23 @@ unsigned long tor_get_thread_id(void);
|
||||
#undef TOR_IS_MULTITHREADED
|
||||
#endif
|
||||
|
||||
/* Because we use threads instead of processes on Windows, we need locking on
|
||||
* Windows. On Unixy platforms, these functions are no-ops. */
|
||||
typedef struct tor_mutex_t tor_mutex_t;
|
||||
#ifdef TOR_IS_MULTITHREADED
|
||||
tor_mutex_t *tor_mutex_new(void);
|
||||
void tor_mutex_acquire(tor_mutex_t *m);
|
||||
void tor_mutex_release(tor_mutex_t *m);
|
||||
void tor_mutex_free(tor_mutex_t *m);
|
||||
unsigned long tor_get_thread_id(void);
|
||||
#else
|
||||
#define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int)))
|
||||
#define tor_mutex_acquire(m) do { } while (0)
|
||||
#define tor_mutex_release(m) do { } while (0)
|
||||
#define tor_mutex_free(m) do { tor_free(m); } while(0)
|
||||
#define tor_get_thread_id() (1UL)
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -60,13 +60,14 @@ _log_prefix(char *buf, size_t buf_len, int severity)
|
||||
{
|
||||
time_t t;
|
||||
struct timeval now;
|
||||
struct tm tm;
|
||||
size_t n;
|
||||
int r;
|
||||
|
||||
tor_gettimeofday(&now);
|
||||
t = (time_t)now.tv_sec;
|
||||
|
||||
n = strftime(buf, buf_len, "%b %d %H:%M:%S", localtime(&t));
|
||||
n = strftime(buf, buf_len, "%b %d %H:%M:%S", tor_localtime_r(&t, &tm));
|
||||
r = tor_snprintf(buf+n, buf_len-n,
|
||||
".%.3ld [%s] ",
|
||||
(long)now.tv_usec / 1000, sev_to_string(severity));
|
||||
|
@ -644,6 +644,7 @@ static void log_cert_lifetime(X509 *cert, const char *problem)
|
||||
char *s1=NULL, *s2=NULL;
|
||||
char mytime[33];
|
||||
time_t now = time(NULL);
|
||||
struct tm tm;
|
||||
|
||||
if (problem)
|
||||
log_fn(LOG_WARN,"Certificate %s: is your system clock set incorrectly?",
|
||||
@ -667,7 +668,7 @@ static void log_cert_lifetime(X509 *cert, const char *problem)
|
||||
BIO_get_mem_ptr(bio, &buf);
|
||||
s2 = tor_strndup(buf->data, buf->length);
|
||||
|
||||
strftime(mytime, 32, "%b %d %H:%M:%S %Y GMT", gmtime(&now));
|
||||
strftime(mytime, 32, "%b %d %H:%M:%S %Y GMT", tor_gmtime_r(&now, &tm));
|
||||
|
||||
log_fn(LOG_WARN, "(certificate lifetime runs from %s through %s. Your time is %s.)",s1,s2,mytime);
|
||||
|
||||
|
@ -603,15 +603,17 @@ static const char *MONTH_NAMES[] =
|
||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
|
||||
|
||||
void format_rfc1123_time(char *buf, time_t t) {
|
||||
struct tm *tm = gmtime(&t);
|
||||
struct tm tm;
|
||||
|
||||
strftime(buf, RFC1123_TIME_LEN+1, "___, %d ___ %Y %H:%M:%S GMT", tm);
|
||||
tor_assert(tm->tm_wday >= 0);
|
||||
tor_assert(tm->tm_wday <= 6);
|
||||
memcpy(buf, WEEKDAY_NAMES[tm->tm_wday], 3);
|
||||
tor_assert(tm->tm_wday >= 0);
|
||||
tor_assert(tm->tm_mon <= 11);
|
||||
memcpy(buf+8, MONTH_NAMES[tm->tm_mon], 3);
|
||||
tor_gmtime_r(&t, &tm);
|
||||
|
||||
strftime(buf, RFC1123_TIME_LEN+1, "___, %d ___ %Y %H:%M:%S GMT", &tm);
|
||||
tor_assert(tm.tm_wday >= 0);
|
||||
tor_assert(tm.tm_wday <= 6);
|
||||
memcpy(buf, WEEKDAY_NAMES[tm.tm_wday], 3);
|
||||
tor_assert(tm.tm_wday >= 0);
|
||||
tor_assert(tm.tm_mon <= 11);
|
||||
memcpy(buf+8, MONTH_NAMES[tm.tm_mon], 3);
|
||||
}
|
||||
|
||||
int parse_rfc1123_time(const char *buf, time_t *t) {
|
||||
@ -649,11 +651,13 @@ int parse_rfc1123_time(const char *buf, time_t *t) {
|
||||
}
|
||||
|
||||
void format_local_iso_time(char *buf, time_t t) {
|
||||
strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", localtime(&t));
|
||||
struct tm tm;
|
||||
strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_localtime_r(&t, &tm));
|
||||
}
|
||||
|
||||
void format_iso_time(char *buf, time_t t) {
|
||||
strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", gmtime(&t));
|
||||
struct tm tm;
|
||||
strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", tor_gmtime_r(&t, &tm));
|
||||
}
|
||||
|
||||
int parse_iso_time(const char *cp, time_t *t) {
|
||||
|
@ -237,13 +237,13 @@ static time_t
|
||||
edge_of_accounting_period_containing(time_t now, int get_end)
|
||||
{
|
||||
int before;
|
||||
struct tm *tm;
|
||||
tm = localtime(&now);
|
||||
struct tm tm;
|
||||
tor_localtime_r(&now, &tm);
|
||||
|
||||
/* Set 'before' to true iff the current time is before the hh:mm
|
||||
* changeover time for today. */
|
||||
before = tm->tm_hour < cfg_start_hour ||
|
||||
(tm->tm_hour == cfg_start_hour && tm->tm_min < cfg_start_min);
|
||||
before = tm.tm_hour < cfg_start_hour ||
|
||||
(tm.tm_hour == cfg_start_hour && tm.tm_min < cfg_start_min);
|
||||
|
||||
/* Dispatch by unit. First, find the start day of the given period;
|
||||
* then, if get_end is true, increment to the end day. */
|
||||
@ -251,14 +251,14 @@ edge_of_accounting_period_containing(time_t now, int get_end)
|
||||
{
|
||||
case UNIT_MONTH: {
|
||||
/* If this is before the Nth, we want the Nth of last month. */
|
||||
if (tm->tm_mday < cfg_start_day ||
|
||||
(tm->tm_mday < cfg_start_day && before)) {
|
||||
--tm->tm_mon;
|
||||
if (tm.tm_mday < cfg_start_day ||
|
||||
(tm.tm_mday < cfg_start_day && before)) {
|
||||
--tm.tm_mon;
|
||||
}
|
||||
/* Otherwise, the month is correct. */
|
||||
tm->tm_mday = cfg_start_day;
|
||||
tm.tm_mday = cfg_start_day;
|
||||
if (get_end)
|
||||
++tm->tm_mon;
|
||||
++tm.tm_mon;
|
||||
break;
|
||||
}
|
||||
case UNIT_WEEK: {
|
||||
@ -266,31 +266,31 @@ edge_of_accounting_period_containing(time_t now, int get_end)
|
||||
say Sunday==7; struct tm says Sunday==0.) */
|
||||
int wday = cfg_start_day % 7;
|
||||
/* How many days do we subtract from today to get to the right day? */
|
||||
int delta = (7+tm->tm_wday-wday)%7;
|
||||
int delta = (7+tm.tm_wday-wday)%7;
|
||||
/* If we are on the right day, but the changeover hasn't happened yet,
|
||||
* then subtract a whole week. */
|
||||
if (delta == 0 && before)
|
||||
delta = 7;
|
||||
tm->tm_mday -= delta;
|
||||
tm.tm_mday -= delta;
|
||||
if (get_end)
|
||||
tm->tm_mday += 7;
|
||||
tm.tm_mday += 7;
|
||||
break;
|
||||
}
|
||||
case UNIT_DAY:
|
||||
if (before)
|
||||
--tm->tm_mday;
|
||||
--tm.tm_mday;
|
||||
if (get_end)
|
||||
++tm->tm_mday;
|
||||
++tm.tm_mday;
|
||||
break;
|
||||
default:
|
||||
tor_assert(0);
|
||||
}
|
||||
|
||||
tm->tm_hour = cfg_start_hour;
|
||||
tm->tm_min = cfg_start_min;
|
||||
tm->tm_sec = 0;
|
||||
tm->tm_isdst = -1; /* Autodetect DST */
|
||||
return mktime(tm);
|
||||
tm.tm_hour = cfg_start_hour;
|
||||
tm.tm_min = cfg_start_min;
|
||||
tm.tm_sec = 0;
|
||||
tm.tm_isdst = -1; /* Autodetect DST */
|
||||
return mktime(&tm);
|
||||
}
|
||||
|
||||
/** Return the start of the accounting period containing the time
|
||||
|
Loading…
Reference in New Issue
Block a user