[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Change interaction between dormant mode and clock jumps.



commit b5c04173c88369d0c4cdaf8a34c2474dc25c79fa
Author: Nick Mathewson <nickm@xxxxxxxxxxxxxx>
Date:   Tue Dec 4 12:21:14 2018 -0500

    Change interaction between dormant mode and clock jumps.
    
    When the clock jumps, and we have a record of last user activity,
    adjust that record.  This way if I'm inactive for 10 minutes and
    then the laptop is sleeping for an hour, I'll still count as having
    been inactive for 10 minutes.
    
    Previously, we treat every jump as if it were activity, which is
    ridiculous, and would prevent a Tor instance with a jumpy clock from
    ever going dormant.
---
 src/core/mainloop/mainloop.c  | 21 +++++++++++----------
 src/core/mainloop/netstatus.c | 12 ++++++++++++
 src/core/mainloop/netstatus.h |  1 +
 src/lib/intmath/cmp.h         |  3 +++
 4 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/src/core/mainloop/mainloop.c b/src/core/mainloop/mainloop.c
index cd955e0ca..6b9c03798 100644
--- a/src/core/mainloop/mainloop.c
+++ b/src/core/mainloop/mainloop.c
@@ -1627,7 +1627,6 @@ schedule_rescan_periodic_events,(void))
 void
 rescan_periodic_events(const or_options_t *options)
 {
-  puts("RESCAN");
   tor_assert(options);
 
   /* Avoid scanning the event list if we haven't initialized it yet. This is
@@ -2687,6 +2686,17 @@ update_current_time(time_t now)
   memcpy(&last_updated, &current_second_last_changed, sizeof(last_updated));
   monotime_coarse_get(&current_second_last_changed);
 
+  /** How much clock jumping means that we should adjust our idea of when
+   * to go dormant? */
+#define NUM_JUMPED_SECONDS_BEFORE_NETSTATUS_UPDATE 20
+
+  /* Don't go dormant early or late just because we jumped in time. */
+  if (ABS(seconds_elapsed) >= NUM_JUMPED_SECONDS_BEFORE_NETSTATUS_UPDATE) {
+    if (is_participating_on_network()) {
+      netstatus_note_clock_jumped(seconds_elapsed);
+    }
+  }
+
   /** How much clock jumping do we tolerate? */
 #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
 
@@ -2697,10 +2707,6 @@ update_current_time(time_t now)
     // moving back in time is always a bad sign.
     circuit_note_clock_jumped(seconds_elapsed, false);
 
-    /* Don't go dormant just because we jumped in time. */
-    if (is_participating_on_network()) {
-      reset_user_activity(now);
-    }
   } else if (seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
     /* Compare the monotonic clock to the result of time(). */
     const int32_t monotime_msec_passed =
@@ -2722,11 +2728,6 @@ update_current_time(time_t now)
     if (clock_jumped || seconds_elapsed >= NUM_IDLE_SECONDS_BEFORE_WARN) {
       circuit_note_clock_jumped(seconds_elapsed, ! clock_jumped);
     }
-
-    /* Don't go dormant just because we jumped in time. */
-    if (is_participating_on_network()) {
-      reset_user_activity(now);
-    }
   } else if (seconds_elapsed > 0) {
     stats_n_seconds_working += seconds_elapsed;
   }
diff --git a/src/core/mainloop/netstatus.c b/src/core/mainloop/netstatus.c
index 2426baae3..d1989cb83 100644
--- a/src/core/mainloop/netstatus.c
+++ b/src/core/mainloop/netstatus.c
@@ -146,3 +146,15 @@ netstatus_load_from_state(const or_state_t *state, time_t now)
   }
   reset_user_activity(last_activity);
 }
+
+/**
+ * Adjust the time at which the user was last active by <b>seconds_diff</b>
+ * in response to a clock jump.
+ */
+void
+netstatus_note_clock_jumped(time_t seconds_diff)
+{
+  time_t last_active = get_last_user_activity_time();
+  if (last_active)
+    reset_user_activity(last_active + seconds_diff);
+}
diff --git a/src/core/mainloop/netstatus.h b/src/core/mainloop/netstatus.h
index 4b008e4cf..9a0fa410f 100644
--- a/src/core/mainloop/netstatus.h
+++ b/src/core/mainloop/netstatus.h
@@ -19,5 +19,6 @@ bool is_participating_on_network(void);
 
 void netstatus_flush_to_state(or_state_t *state, time_t now);
 void netstatus_load_from_state(const or_state_t *state, time_t now);
+void netstatus_note_clock_jumped(time_t seconds_diff);
 
 #endif
diff --git a/src/lib/intmath/cmp.h b/src/lib/intmath/cmp.h
index 16952bee3..11b6fdf98 100644
--- a/src/lib/intmath/cmp.h
+++ b/src/lib/intmath/cmp.h
@@ -36,4 +36,7 @@
     ((v) > (max)) ? (max) :                     \
     (v) )
 
+/** Give the absolute value of <b>x</b>, independent of its type. */
+#define ABS(x) ( ((x)<0) ? -(x) : (x) )
+
 #endif /* !defined(TOR_INTMATH_CMP_H) */



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits