summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2005-02-22 07:03:03 +0000
committerNick Mathewson <nickm@torproject.org>2005-02-22 07:03:03 +0000
commit70c3580f81dd560072209fe32c740c33d3781c67 (patch)
treea914e5d669ef9bf6d7b3ed79a07157cd676c9c61 /src
parentd01718841e5bc3d365191e74bebbccee48c8b045 (diff)
downloadtor-70c3580f81dd560072209fe32c740c33d3781c67.tar.gz
tor-70c3580f81dd560072209fe32c740c33d3781c67.zip
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
Diffstat (limited to 'src')
-rw-r--r--src/common/compat.c39
-rw-r--r--src/common/compat.h38
-rw-r--r--src/common/log.c3
-rw-r--r--src/common/tortls.c3
-rw-r--r--src/common/util.c24
-rw-r--r--src/or/hibernate.c38
6 files changed, 101 insertions, 44 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index 5de2081ca5..d7ed324b49 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -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
/**
diff --git a/src/common/compat.h b/src/common/compat.h
index 8efc63e9d6..ec433ddd7b 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -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
diff --git a/src/common/log.c b/src/common/log.c
index 13a012f678..5d8e64f021 100644
--- a/src/common/log.c
+++ b/src/common/log.c
@@ -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));
diff --git a/src/common/tortls.c b/src/common/tortls.c
index 42074cc41a..9f7bf326d9 100644
--- a/src/common/tortls.c
+++ b/src/common/tortls.c
@@ -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);
diff --git a/src/common/util.c b/src/common/util.c
index 11c592ca96..2d7f6903f7 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -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) {
diff --git a/src/or/hibernate.c b/src/or/hibernate.c
index ffd9138a68..1867aec0d9 100644
--- a/src/or/hibernate.c
+++ b/src/or/hibernate.c
@@ -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