commit a0380b705daceb69c29ccda3a2f1453b9dcbc40d
Author: Nick Mathewson <ni...@torproject.org>
Date:   Tue Nov 13 08:22:58 2018 -0500

    Move control_per_second_events() into a callback with its own role
    
    Part of making extra-dormant mode work; closes ticket 28421.
---
 src/core/mainloop/mainloop.c   | 29 +++++++++++++++++++++++------
 src/core/mainloop/periodic.h   |  5 +++--
 src/feature/control/control.c  |  2 +-
 src/test/test_periodic_event.c |  3 ++-
 4 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/src/core/mainloop/mainloop.c b/src/core/mainloop/mainloop.c
index 7e5e5d0ef..a9d5d8155 100644
--- a/src/core/mainloop/mainloop.c
+++ b/src/core/mainloop/mainloop.c
@@ -1366,6 +1366,7 @@ CALLBACK(save_stability);
 CALLBACK(save_state);
 CALLBACK(write_bridge_ns);
 CALLBACK(write_stats_file);
+CALLBACK(control_per_second_events);
 
 #undef CALLBACK
 
@@ -1439,6 +1440,9 @@ STATIC periodic_event_item_t periodic_events[] = {
   /* Directory server only. */
   CALLBACK(clean_consdiffmgr, DIRSERVER, 0),
 
+  /* Controller with per-second events only. */
+  CALLBACK(control_per_second_events, CONTROLEV, 0),
+
   END_OF_PERIODIC_EVENTS
 };
 #undef CALLBACK
@@ -1498,6 +1502,8 @@ get_my_roles(const or_options_t *options)
   int is_hidden_service = !!hs_service_get_num_services() ||
                           !!rend_num_services();
   int is_dirserver = dir_server_mode(options);
+  int sending_control_events = control_any_per_second_event_enabled();
+
   /* We also consider tor to have the role of a client if the ControlPort is
    * set because a lot of things can be done over the control port which
    * requires tor to have basic functionnalities. */
@@ -1516,6 +1522,7 @@ get_my_roles(const or_options_t *options)
   if (is_hidden_service) roles |= PERIODIC_EVENT_ROLE_HS_SERVICE;
   if (is_dirserver) roles |= PERIODIC_EVENT_ROLE_DIRSERVER;
   if (is_net_participant) roles |= PERIODIC_EVENT_ROLE_NET_PARTICIPANT;
+  if (sending_control_events) roles |= PERIODIC_EVENT_ROLE_CONTROLEV;
 
   return roles;
 }
@@ -2524,6 +2531,21 @@ hs_service_callback(time_t now, const or_options_t 
*options)
   return 1;
 }
 
+/*
+ * Periodic callback: Send once-per-second events to the controller(s).
+ * This is called every second.
+ */
+static int
+control_per_second_events_callback(time_t now, const or_options_t *options)
+{
+  (void) options;
+  (void) now;
+
+  control_per_second_events();
+
+  return 1;
+}
+
 /** Timer: used to invoke second_elapsed_callback() once per second. */
 static periodic_timer_t *second_timer = NULL;
 
@@ -2546,8 +2568,7 @@ reschedule_per_second_timer(void)
     tor_assert(second_timer);
   }
 
-  const bool run_per_second_events =
-    control_any_per_second_event_enabled() || ! net_is_completely_disabled();
+  const bool run_per_second_events = ! net_is_completely_disabled();
 
   if (run_per_second_events) {
     periodic_timer_launch(second_timer, &one_second);
@@ -2640,10 +2661,6 @@ second_elapsed_callback(periodic_timer_t *timer, void 
*arg)
    */
   update_current_time(now);
 
-  // TODO XXXX Turn this into a separate event.
-  /* Maybe some controller events are ready to fire */
-  control_per_second_events();
-
   run_scheduled_events(now);
 }
 
diff --git a/src/core/mainloop/periodic.h b/src/core/mainloop/periodic.h
index 23459ff2b..52d5450ee 100644
--- a/src/core/mainloop/periodic.h
+++ b/src/core/mainloop/periodic.h
@@ -15,9 +15,10 @@
 #define PERIODIC_EVENT_ROLE_BRIDGEAUTH  (1U << 4)
 #define PERIODIC_EVENT_ROLE_HS_SERVICE  (1U << 5)
 #define PERIODIC_EVENT_ROLE_DIRSERVER   (1U << 6)
+#define PERIODIC_EVENT_ROLE_CONTROLEV   (1U << 7)
 
-#define PERIODIC_EVENT_ROLE_NET_PARTICIPANT (1U << 7)
-#define PERIODIC_EVENT_ROLE_ALL         (1U << 8)
+#define PERIODIC_EVENT_ROLE_NET_PARTICIPANT (1U << 8)
+#define PERIODIC_EVENT_ROLE_ALL         (1U << 9)
 
 /* Helper macro to make it a bit less annoying to defined groups of roles that
  * are often used. */
diff --git a/src/feature/control/control.c b/src/feature/control/control.c
index b31b448e9..a5b6ab3bf 100644
--- a/src/feature/control/control.c
+++ b/src/feature/control/control.c
@@ -368,7 +368,7 @@ control_update_global_event_mask(void)
     control_get_bytes_rw_last_sec(&r, &w);
   }
   if (any_old_per_sec_events != control_any_per_second_event_enabled()) {
-    reschedule_per_second_timer();
+    rescan_periodic_events(get_options());
   }
 
 #undef NEWLY_ENABLED
diff --git a/src/test/test_periodic_event.c b/src/test/test_periodic_event.c
index f63adf8e3..6a3e320b2 100644
--- a/src/test/test_periodic_event.c
+++ b/src/test/test_periodic_event.c
@@ -172,7 +172,8 @@ test_pe_launch(void *arg)
 
   for (int i = 0; periodic_events[i].name; ++i) {
     periodic_event_item_t *item = &periodic_events[i];
-    tt_int_op(periodic_event_is_enabled(item), OP_EQ, 1);
+    tt_int_op(periodic_event_is_enabled(item), OP_EQ,
+              (item->roles != PERIODIC_EVENT_ROLE_CONTROLEV));
   }
 
  done:



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

Reply via email to