aboutsummaryrefslogtreecommitdiff
path: root/src/core/mainloop
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-12-04 11:56:05 -0500
committerNick Mathewson <nickm@torproject.org>2018-12-04 11:59:11 -0500
commitb25b8150c282c28d3fa23330891fda7adbbfe584 (patch)
tree937e26f34f25dfdcb336a4705854847896d4b456 /src/core/mainloop
parent0d9dc13e087c01550e5d237f298d8a5de5994df0 (diff)
downloadtor-b25b8150c282c28d3fa23330891fda7adbbfe584.tar.gz
tor-b25b8150c282c28d3fa23330891fda7adbbfe584.zip
Remember in our state file how long we've spent since user activity
Rather than initializing the "Dormant" status to "off" and the "last activity" count to "now", initialize them based on our state file: stay dormant if we were dormant, or remember the amount of time we've spent inactive.
Diffstat (limited to 'src/core/mainloop')
-rw-r--r--src/core/mainloop/mainloop.c7
-rw-r--r--src/core/mainloop/netstatus.c41
-rw-r--r--src/core/mainloop/netstatus.h3
3 files changed, 45 insertions, 6 deletions
diff --git a/src/core/mainloop/mainloop.c b/src/core/mainloop/mainloop.c
index 2e2ae876d4..cd955e0ca0 100644
--- a/src/core/mainloop/mainloop.c
+++ b/src/core/mainloop/mainloop.c
@@ -1627,6 +1627,7 @@ 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
@@ -2818,12 +2819,6 @@ initialize_mainloop_events(void)
int
do_main_loop(void)
{
- /* For now, starting Tor always counts as user activity. Later, we might
- * have an option to control this.
- */
- reset_user_activity(approx_time());
- set_network_participation(true);
-
/* initialize the periodic events first, so that code that depends on the
* events being present does not assert.
*/
diff --git a/src/core/mainloop/netstatus.c b/src/core/mainloop/netstatus.c
index ed7c952dcd..59fd8f8037 100644
--- a/src/core/mainloop/netstatus.c
+++ b/src/core/mainloop/netstatus.c
@@ -10,6 +10,8 @@
#include "app/config/config.h"
#include "feature/hibernate/hibernate.h"
+#include "app/config/or_state_st.h"
+
/** Return true iff our network is in some sense disabled or shutting down:
* either we're hibernating, entering hibernation, or the network is turned
* off with DisableNetwork. */
@@ -31,6 +33,10 @@ net_is_completely_disabled(void)
/**
* The time at which we've last seen "user activity" -- that is, any activity
* that should keep us as a participant on the network.
+ *
+ * This is not actually the true time. We will adjust this forward if
+ * our clock jumps, or if Tor is shut down for a while, so that the time
+ * since our last activity remains as it was before the jump or shutdown.
*/
static time_t last_user_activity_seen = 0;
@@ -99,3 +105,38 @@ is_participating_on_network(void)
{
return participating_on_network;
}
+
+/**
+ * Update 'state' with the last time at which we were active on the network.
+ **/
+void
+netstatus_flush_to_state(or_state_t *state, time_t now)
+{
+ state->Dormant = ! participating_on_network;
+ if (participating_on_network) {
+ time_t sec_since_activity = MAX(0, now - last_user_activity_seen);
+ state->MinutesSinceUserActivity = (int)(sec_since_activity / 60);
+ } else {
+ state->MinutesSinceUserActivity = 0;
+ }
+}
+
+/**
+ * Update our current view of network participation from an or_state_t object.
+ **/
+void
+netstatus_load_from_state(const or_state_t *state, time_t now)
+{
+ time_t last_activity;
+ if (state->Dormant == -1) { // Initial setup.
+ last_activity = now;
+ participating_on_network = true;
+ } else if (state->Dormant) {
+ last_activity = 0;
+ participating_on_network = false;
+ } else {
+ last_activity = now - 60 * state->MinutesSinceUserActivity;
+ participating_on_network = true;
+ }
+ reset_user_activity(last_activity);
+}
diff --git a/src/core/mainloop/netstatus.h b/src/core/mainloop/netstatus.h
index 58c994fd14..4b008e4cfa 100644
--- a/src/core/mainloop/netstatus.h
+++ b/src/core/mainloop/netstatus.h
@@ -17,4 +17,7 @@ time_t get_last_user_activity_time(void);
void set_network_participation(bool participation);
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);
+
#endif