Move control_per_second_events() into a callback with its own role

Part of making extra-dormant mode work; closes ticket 28421.
This commit is contained in:
Nick Mathewson 2018-11-13 08:22:58 -05:00
parent db53bfe8f7
commit a0380b705d
4 changed files with 29 additions and 10 deletions

View File

@ -1366,6 +1366,7 @@ CALLBACK(save_stability);
CALLBACK(save_state); CALLBACK(save_state);
CALLBACK(write_bridge_ns); CALLBACK(write_bridge_ns);
CALLBACK(write_stats_file); CALLBACK(write_stats_file);
CALLBACK(control_per_second_events);
#undef CALLBACK #undef CALLBACK
@ -1439,6 +1440,9 @@ STATIC periodic_event_item_t periodic_events[] = {
/* Directory server only. */ /* Directory server only. */
CALLBACK(clean_consdiffmgr, DIRSERVER, 0), CALLBACK(clean_consdiffmgr, DIRSERVER, 0),
/* Controller with per-second events only. */
CALLBACK(control_per_second_events, CONTROLEV, 0),
END_OF_PERIODIC_EVENTS END_OF_PERIODIC_EVENTS
}; };
#undef CALLBACK #undef CALLBACK
@ -1498,6 +1502,8 @@ get_my_roles(const or_options_t *options)
int is_hidden_service = !!hs_service_get_num_services() || int is_hidden_service = !!hs_service_get_num_services() ||
!!rend_num_services(); !!rend_num_services();
int is_dirserver = dir_server_mode(options); 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 /* 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 * set because a lot of things can be done over the control port which
* requires tor to have basic functionnalities. */ * 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_hidden_service) roles |= PERIODIC_EVENT_ROLE_HS_SERVICE;
if (is_dirserver) roles |= PERIODIC_EVENT_ROLE_DIRSERVER; if (is_dirserver) roles |= PERIODIC_EVENT_ROLE_DIRSERVER;
if (is_net_participant) roles |= PERIODIC_EVENT_ROLE_NET_PARTICIPANT; if (is_net_participant) roles |= PERIODIC_EVENT_ROLE_NET_PARTICIPANT;
if (sending_control_events) roles |= PERIODIC_EVENT_ROLE_CONTROLEV;
return roles; return roles;
} }
@ -2524,6 +2531,21 @@ hs_service_callback(time_t now, const or_options_t *options)
return 1; 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. */ /** Timer: used to invoke second_elapsed_callback() once per second. */
static periodic_timer_t *second_timer = NULL; static periodic_timer_t *second_timer = NULL;
@ -2546,8 +2568,7 @@ reschedule_per_second_timer(void)
tor_assert(second_timer); tor_assert(second_timer);
} }
const bool run_per_second_events = const bool run_per_second_events = ! net_is_completely_disabled();
control_any_per_second_event_enabled() || ! net_is_completely_disabled();
if (run_per_second_events) { if (run_per_second_events) {
periodic_timer_launch(second_timer, &one_second); periodic_timer_launch(second_timer, &one_second);
@ -2640,10 +2661,6 @@ second_elapsed_callback(periodic_timer_t *timer, void *arg)
*/ */
update_current_time(now); 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); run_scheduled_events(now);
} }

View File

@ -15,9 +15,10 @@
#define PERIODIC_EVENT_ROLE_BRIDGEAUTH (1U << 4) #define PERIODIC_EVENT_ROLE_BRIDGEAUTH (1U << 4)
#define PERIODIC_EVENT_ROLE_HS_SERVICE (1U << 5) #define PERIODIC_EVENT_ROLE_HS_SERVICE (1U << 5)
#define PERIODIC_EVENT_ROLE_DIRSERVER (1U << 6) #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_NET_PARTICIPANT (1U << 8)
#define PERIODIC_EVENT_ROLE_ALL (1U << 8) #define PERIODIC_EVENT_ROLE_ALL (1U << 9)
/* Helper macro to make it a bit less annoying to defined groups of roles that /* Helper macro to make it a bit less annoying to defined groups of roles that
* are often used. */ * are often used. */

View File

@ -368,7 +368,7 @@ control_update_global_event_mask(void)
control_get_bytes_rw_last_sec(&r, &w); control_get_bytes_rw_last_sec(&r, &w);
} }
if (any_old_per_sec_events != control_any_per_second_event_enabled()) { if (any_old_per_sec_events != control_any_per_second_event_enabled()) {
reschedule_per_second_timer(); rescan_periodic_events(get_options());
} }
#undef NEWLY_ENABLED #undef NEWLY_ENABLED

View File

@ -172,7 +172,8 @@ test_pe_launch(void *arg)
for (int i = 0; periodic_events[i].name; ++i) { for (int i = 0; periodic_events[i].name; ++i) {
periodic_event_item_t *item = &periodic_events[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: done: