periodic.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* Copyright (c) 2015-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file periodic.c
  5. *
  6. * \brief Generic backend for handling periodic events.
  7. *
  8. * The events in this module are used to track items that need
  9. * to fire once every N seconds, possibly picking a new interval each time
  10. * that they fire. See periodic_events[] in mainloop.c for examples.
  11. *
  12. * This module manages a global list of periodic_event_item_t objects,
  13. * each corresponding to a single event. To register an event, pass it to
  14. * periodic_events_register() when initializing your subsystem.
  15. *
  16. * Registering an event makes the periodic event subsystem know about it, but
  17. * doesn't cause the event to get created immediately. Before the event can
  18. * be started, periodic_event_connect_all() must be called by mainloop.c to
  19. * connect all the events to Libevent.
  20. *
  21. * We expect that periodic_event_item_t objects will be statically allocated;
  22. * we set them up and tear them down here, but we don't take ownership of
  23. * them.
  24. */
  25. #include "core/or/or.h"
  26. #include "lib/evloop/compat_libevent.h"
  27. #include "app/config/config.h"
  28. #include "core/mainloop/mainloop.h"
  29. #include "core/mainloop/periodic.h"
  30. #include "lib/evloop/compat_libevent.h"
  31. /** We disable any interval greater than this number of seconds, on the
  32. * grounds that it is probably an absolute time mistakenly passed in as a
  33. * relative time.
  34. */
  35. static const int MAX_INTERVAL = 10 * 365 * 86400;
  36. /**
  37. * Global list of periodic events that have been registered with
  38. * <b>periodic_event_register</a>.
  39. **/
  40. static smartlist_t *the_periodic_events = NULL;
  41. /** Set the event <b>event</b> to run in <b>next_interval</b> seconds from
  42. * now. */
  43. static void
  44. periodic_event_set_interval(periodic_event_item_t *event,
  45. time_t next_interval)
  46. {
  47. tor_assert(next_interval < MAX_INTERVAL);
  48. struct timeval tv;
  49. tv.tv_sec = next_interval;
  50. tv.tv_usec = 0;
  51. mainloop_event_schedule(event->ev, &tv);
  52. }
  53. /** Wraps dispatches for periodic events, <b>data</b> will be a pointer to the
  54. * event that needs to be called */
  55. static void
  56. periodic_event_dispatch(mainloop_event_t *ev, void *data)
  57. {
  58. periodic_event_item_t *event = data;
  59. tor_assert(ev == event->ev);
  60. time_t now = time(NULL);
  61. update_current_time(now);
  62. const or_options_t *options = get_options();
  63. // log_debug(LD_GENERAL, "Dispatching %s", event->name);
  64. int r = event->fn(now, options);
  65. int next_interval = 0;
  66. if (!periodic_event_is_enabled(event)) {
  67. /* The event got disabled from inside its callback, or before: no need to
  68. * reschedule. */
  69. return;
  70. }
  71. /* update the last run time if action was taken */
  72. if (r==0) {
  73. log_err(LD_BUG, "Invalid return value for periodic event from %s.",
  74. event->name);
  75. tor_assert(r != 0);
  76. } else if (r > 0) {
  77. event->last_action_time = now;
  78. /* If the event is meant to happen after ten years, that's likely
  79. * a bug, and somebody gave an absolute time rather than an interval.
  80. */
  81. tor_assert(r < MAX_INTERVAL);
  82. next_interval = r;
  83. } else {
  84. /* no action was taken, it is likely a precondition failed,
  85. * we should reschedule for next second incase the precondition
  86. * passes then */
  87. next_interval = 1;
  88. }
  89. // log_debug(LD_GENERAL, "Scheduling %s for %d seconds", event->name,
  90. // next_interval);
  91. struct timeval tv = { next_interval , 0 };
  92. mainloop_event_schedule(ev, &tv);
  93. }
  94. /** Schedules <b>event</b> to run as soon as possible from now. */
  95. void
  96. periodic_event_reschedule(periodic_event_item_t *event)
  97. {
  98. /* Don't reschedule a disabled or uninitialized event. */
  99. if (event->ev && periodic_event_is_enabled(event)) {
  100. periodic_event_set_interval(event, 1);
  101. }
  102. }
  103. /** Connects a periodic event to the Libevent backend. Does not launch the
  104. * event immediately. */
  105. void
  106. periodic_event_connect(periodic_event_item_t *event)
  107. {
  108. if (event->ev) { /* Already setup? This is a bug */
  109. log_err(LD_BUG, "Initial dispatch should only be done once.");
  110. tor_assert(0);
  111. }
  112. event->ev = mainloop_event_new(periodic_event_dispatch,
  113. event);
  114. tor_assert(event->ev);
  115. }
  116. /** Handles initial dispatch for periodic events. It should happen 1 second
  117. * after the events are created to mimic behaviour before #3199's refactor */
  118. void
  119. periodic_event_launch(periodic_event_item_t *event)
  120. {
  121. if (! event->ev) { /* Not setup? This is a bug */
  122. log_err(LD_BUG, "periodic_event_launch without periodic_event_connect");
  123. tor_assert(0);
  124. }
  125. /* Event already enabled? This is a bug */
  126. if (periodic_event_is_enabled(event)) {
  127. log_err(LD_BUG, "periodic_event_launch on an already enabled event");
  128. tor_assert(0);
  129. }
  130. // Initial dispatch
  131. event->enabled = 1;
  132. periodic_event_dispatch(event->ev, event);
  133. }
  134. /** Disconnect and unregister the periodic event in <b>event</b> */
  135. static void
  136. periodic_event_disconnect(periodic_event_item_t *event)
  137. {
  138. if (!event)
  139. return;
  140. mainloop_event_free(event->ev);
  141. event->last_action_time = 0;
  142. }
  143. /** Enable the given event by setting its "enabled" flag and scheduling it to
  144. * run immediately in the event loop. This can be called for an event that is
  145. * already enabled. */
  146. void
  147. periodic_event_enable(periodic_event_item_t *event)
  148. {
  149. tor_assert(event);
  150. /* Safely and silently ignore if this event is already enabled. */
  151. if (periodic_event_is_enabled(event)) {
  152. return;
  153. }
  154. tor_assert(event->ev);
  155. event->enabled = 1;
  156. mainloop_event_activate(event->ev);
  157. }
  158. /** Disable the given event which means the event is destroyed and then the
  159. * event's enabled flag is unset. This can be called for an event that is
  160. * already disabled. */
  161. void
  162. periodic_event_disable(periodic_event_item_t *event)
  163. {
  164. tor_assert(event);
  165. /* Safely and silently ignore if this event is already disabled. */
  166. if (!periodic_event_is_enabled(event)) {
  167. return;
  168. }
  169. mainloop_event_cancel(event->ev);
  170. event->enabled = 0;
  171. }
  172. /**
  173. * Disable an event, then schedule it to run once.
  174. * Do nothing if the event was already disabled.
  175. */
  176. void
  177. periodic_event_schedule_and_disable(periodic_event_item_t *event)
  178. {
  179. tor_assert(event);
  180. if (!periodic_event_is_enabled(event))
  181. return;
  182. periodic_event_disable(event);
  183. mainloop_event_activate(event->ev);
  184. }
  185. /**
  186. * Add <b>item</b> to the list of periodic events.
  187. *
  188. * Note that <b>item</b> should be statically allocated: we do not
  189. * take ownership of it.
  190. **/
  191. void
  192. periodic_events_register(periodic_event_item_t *item)
  193. {
  194. if (!the_periodic_events)
  195. the_periodic_events = smartlist_new();
  196. if (BUG(smartlist_contains(the_periodic_events, item)))
  197. return;
  198. smartlist_add(the_periodic_events, item);
  199. }
  200. /**
  201. * Make all registered periodic events connect to the libevent backend.
  202. */
  203. void
  204. periodic_events_connect_all(void)
  205. {
  206. if (! the_periodic_events)
  207. return;
  208. SMARTLIST_FOREACH_BEGIN(the_periodic_events, periodic_event_item_t *, item) {
  209. if (item->ev)
  210. continue;
  211. periodic_event_connect(item);
  212. } SMARTLIST_FOREACH_END(item);
  213. }
  214. /**
  215. * Reset all the registered periodic events so we'll do all our actions again
  216. * as if we just started up.
  217. *
  218. * Useful if our clock just moved back a long time from the future,
  219. * so we don't wait until that future arrives again before acting.
  220. */
  221. void
  222. periodic_events_reset_all(void)
  223. {
  224. if (! the_periodic_events)
  225. return;
  226. SMARTLIST_FOREACH_BEGIN(the_periodic_events, periodic_event_item_t *, item) {
  227. if (!item->ev)
  228. continue;
  229. periodic_event_reschedule(item);
  230. } SMARTLIST_FOREACH_END(item);
  231. }
  232. /**
  233. * Return the registered periodic event whose name is <b>name</b>.
  234. * Return NULL if no such event is found.
  235. */
  236. periodic_event_item_t *
  237. periodic_events_find(const char *name)
  238. {
  239. if (! the_periodic_events)
  240. return NULL;
  241. SMARTLIST_FOREACH_BEGIN(the_periodic_events, periodic_event_item_t *, item) {
  242. if (strcmp(name, item->name) == 0)
  243. return item;
  244. } SMARTLIST_FOREACH_END(item);
  245. return NULL;
  246. }
  247. /**
  248. * Start or stop registered periodic events, depending on our current set of
  249. * roles.
  250. *
  251. * Invoked when our list of roles, or the net_disabled flag has changed.
  252. **/
  253. void
  254. periodic_events_rescan_by_roles(int roles, bool net_disabled)
  255. {
  256. if (! the_periodic_events)
  257. return;
  258. SMARTLIST_FOREACH_BEGIN(the_periodic_events, periodic_event_item_t *, item) {
  259. if (!item->ev)
  260. continue;
  261. int enable = !!(item->roles & roles);
  262. /* Handle the event flags. */
  263. if (net_disabled &&
  264. (item->flags & PERIODIC_EVENT_FLAG_NEED_NET)) {
  265. enable = 0;
  266. }
  267. /* Enable the event if needed. It is safe to enable an event that was
  268. * already enabled. Same goes for disabling it. */
  269. if (enable) {
  270. log_debug(LD_GENERAL, "Launching periodic event %s", item->name);
  271. periodic_event_enable(item);
  272. } else {
  273. log_debug(LD_GENERAL, "Disabling periodic event %s", item->name);
  274. if (item->flags & PERIODIC_EVENT_FLAG_RUN_ON_DISABLE) {
  275. periodic_event_schedule_and_disable(item);
  276. } else {
  277. periodic_event_disable(item);
  278. }
  279. }
  280. } SMARTLIST_FOREACH_END(item);
  281. }
  282. /**
  283. * Invoked at shutdown: disconnect and unregister all periodic events.
  284. *
  285. * Does not free the periodic_event_item_t object themselves, because we do
  286. * not own them.
  287. */
  288. void
  289. periodic_events_disconnect_all(void)
  290. {
  291. if (! the_periodic_events)
  292. return;
  293. SMARTLIST_FOREACH_BEGIN(the_periodic_events, periodic_event_item_t *, item) {
  294. periodic_event_disconnect(item);
  295. } SMARTLIST_FOREACH_END(item);
  296. smartlist_free(the_periodic_events);
  297. }
  298. #define LONGEST_TIMER_PERIOD (30 * 86400)
  299. /** Helper: Return the number of seconds between <b>now</b> and <b>next</b>,
  300. * clipped to the range [1 second, LONGEST_TIMER_PERIOD].
  301. *
  302. * We use this to answer the question, "how many seconds is it from now until
  303. * next" in periodic timer callbacks. Don't use it for other purposes
  304. **/
  305. int
  306. safe_timer_diff(time_t now, time_t next)
  307. {
  308. if (next > now) {
  309. /* There were no computers at signed TIME_MIN (1902 on 32-bit systems),
  310. * and nothing that could run Tor. It's a bug if 'next' is around then.
  311. * On 64-bit systems with signed TIME_MIN, TIME_MIN is before the Big
  312. * Bang. We cannot extrapolate past a singularity, but there was probably
  313. * nothing that could run Tor then, either.
  314. **/
  315. tor_assert(next > TIME_MIN + LONGEST_TIMER_PERIOD);
  316. if (next - LONGEST_TIMER_PERIOD > now)
  317. return LONGEST_TIMER_PERIOD;
  318. return (int)(next - now);
  319. } else {
  320. return 1;
  321. }
  322. }