Browse Source

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.
Nick Mathewson 5 years ago
parent
commit
b5c04173c8
4 changed files with 27 additions and 10 deletions
  1. 11 10
      src/core/mainloop/mainloop.c
  2. 12 0
      src/core/mainloop/netstatus.c
  3. 1 0
      src/core/mainloop/netstatus.h
  4. 3 0
      src/lib/intmath/cmp.h

+ 11 - 10
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;
   }

+ 12 - 0
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);
+}

+ 1 - 0
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

+ 3 - 0
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) */