From 0d650e7958d011dc165fbad7477e2eff842598ea Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 25 Apr 2019 13:23:18 -0400 Subject: [PATCH] Move responsibility for checking if events are setup into periodic.c We have checks in various places in mainlook.c to make sure that events are initialized before we invoke any periodic_foo() functions on them. But now that each subsystem will own its own periodic events, it will be cleaner if we don't assume that they are all setup or not. --- src/core/mainloop/mainloop.c | 27 ++++++++++----------------- src/core/mainloop/periodic.c | 10 ++++++++-- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/core/mainloop/mainloop.c b/src/core/mainloop/mainloop.c index b32532c762..ddcc3bcd76 100644 --- a/src/core/mainloop/mainloop.c +++ b/src/core/mainloop/mainloop.c @@ -1587,6 +1587,13 @@ STATIC void teardown_periodic_events(void) { periodic_events_destroy_all(); + check_descriptor_event = NULL; + dirvote_event = NULL; + fetch_networkstatus_event = NULL; + launch_descriptor_fetches_event = NULL; + check_dns_honesty_event = NULL; + save_state_event = NULL; + prune_old_routers_event = NULL; periodic_events_initialized = 0; } @@ -1621,13 +1628,6 @@ rescan_periodic_events(const or_options_t *options) { tor_assert(options); - /* Avoid scanning the event list if we haven't initialized it yet. This is - * particularly useful for unit tests in order to avoid initializing main - * loop events everytime. */ - if (!periodic_events_initialized) { - return; - } - periodic_events_rescan_by_roles(get_my_roles(options), net_is_disabled()); } @@ -1636,13 +1636,7 @@ rescan_periodic_events(const or_options_t *options) void periodic_events_on_new_options(const or_options_t *options) { - /* Only if we've already initialized the events, rescan the list which will - * enable or disable events depending on our roles. This will be called at - * bootup and we don't want this function to initialize the events because - * they aren't set up at this stage. */ - if (periodic_events_initialized) { - rescan_periodic_events(options); - } + rescan_periodic_events(options); } /** @@ -2107,7 +2101,7 @@ dirvote_callback(time_t now, const or_options_t *options) void reschedule_dirvote(const or_options_t *options) { - if (periodic_events_initialized && authdir_mode_v3(options)) { + if (authdir_mode_v3(options) && dirvote_event) { periodic_event_reschedule(dirvote_event); } } @@ -2753,8 +2747,7 @@ dns_servers_relaunch_checks(void) { if (server_mode(get_options())) { dns_reset_correctness_checks(); - if (periodic_events_initialized) { - tor_assert(check_dns_honesty_event); + if (check_dns_honesty_event) { periodic_event_reschedule(check_dns_honesty_event); } } diff --git a/src/core/mainloop/periodic.c b/src/core/mainloop/periodic.c index 706dbc1b5e..a4a03ed297 100644 --- a/src/core/mainloop/periodic.c +++ b/src/core/mainloop/periodic.c @@ -100,8 +100,8 @@ periodic_event_dispatch(mainloop_event_t *ev, void *data) void periodic_event_reschedule(periodic_event_item_t *event) { - /* Don't reschedule a disabled event. */ - if (periodic_event_is_enabled(event)) { + /* Don't reschedule a disabled or uninitialized event. */ + if (event->ev && periodic_event_is_enabled(event)) { periodic_event_set_interval(event, 1); } } @@ -243,6 +243,9 @@ periodic_events_reset_all(void) return; SMARTLIST_FOREACH_BEGIN(the_periodic_events, periodic_event_item_t *, item) { + if (!item->ev) + continue; + periodic_event_reschedule(item); } SMARTLIST_FOREACH_END(item); } @@ -277,6 +280,9 @@ periodic_events_rescan_by_roles(int roles, bool net_disabled) return; SMARTLIST_FOREACH_BEGIN(the_periodic_events, periodic_event_item_t *, item) { + if (!item->ev) + continue; + int enable = !!(item->roles & roles); /* Handle the event flags. */