diff --git a/src/core/mainloop/mainloop.c b/src/core/mainloop/mainloop.c index cd955e0ca0..6b9c03798e 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, ¤t_second_last_changed, sizeof(last_updated)); monotime_coarse_get(¤t_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 2426baae34..d1989cb839 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 seconds_diff + * 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 4b008e4cfa..9a0fa410fd 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 16952bee3e..11b6fdf98e 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 x, independent of its type. */ +#define ABS(x) ( ((x)<0) ? -(x) : (x) ) + #endif /* !defined(TOR_INTMATH_CMP_H) */