summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-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
5 files changed, 82 insertions, 25 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) {