[or-cvs] Patch to localtime/gmtime handling: use the _r variants whe...

Nick Mathewson nickm at seul.org
Tue Feb 22 07:03:05 UTC 2005


Update of /home/or/cvsroot/tor/src/common
In directory moria.mit.edu:/tmp/cvs-serv7366/src/common

Modified Files:
	compat.c compat.h log.c tortls.c util.c 
Log Message:
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.

Index: compat.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/compat.c,v
retrieving revision 1.38
retrieving revision 1.39
diff -u -d -r1.38 -r1.39
--- compat.c	22 Feb 2005 04:55:19 -0000	1.38
+++ compat.c	22 Feb 2005 07:03:02 -0000	1.39
@@ -753,6 +753,40 @@
   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 @@
 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
 
 /**

Index: compat.h
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/compat.h,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -d -r1.21 -r1.22
--- compat.h	13 Feb 2005 22:32:25 -0000	1.21
+++ compat.h	22 Feb 2005 07:03:02 -0000	1.22
@@ -104,6 +104,18 @@
 
 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 @@
 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 @@
 #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
 

Index: log.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/log.c,v
retrieving revision 1.85
retrieving revision 1.86
diff -u -d -r1.85 -r1.86
--- log.c	4 Jan 2005 02:25:18 -0000	1.85
+++ log.c	22 Feb 2005 07:03:02 -0000	1.86
@@ -60,13 +60,14 @@
 {
   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));

Index: tortls.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/tortls.c,v
retrieving revision 1.84
retrieving revision 1.85
diff -u -d -r1.84 -r1.85
--- tortls.c	11 Feb 2005 01:41:19 -0000	1.84
+++ tortls.c	22 Feb 2005 07:03:02 -0000	1.85
@@ -644,6 +644,7 @@
   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 @@
   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);
 

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/util.c,v
retrieving revision 1.200
retrieving revision 1.201
diff -u -d -r1.200 -r1.201
--- util.c	22 Feb 2005 06:38:39 -0000	1.200
+++ util.c	22 Feb 2005 07:03:03 -0000	1.201
@@ -603,15 +603,17 @@
     "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 @@
 }
 
 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) {



More information about the tor-commits mailing list