periodic.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Copyright (c) 2015-2016, 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 by main.c 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 main.c for examples.
  11. */
  12. #include "or.h"
  13. #include "compat_libevent.h"
  14. #include "config.h"
  15. #include "periodic.h"
  16. #include <event2/event.h>
  17. /** We disable any interval greater than this number of seconds, on the
  18. * grounds that it is probably an absolute time mistakenly passed in as a
  19. * relative time.
  20. */
  21. static const int MAX_INTERVAL = 10 * 365 * 86400;
  22. /** Set the event <b>event</b> to run in <b>next_interval</b> seconds from
  23. * now. */
  24. static void
  25. periodic_event_set_interval(periodic_event_item_t *event,
  26. time_t next_interval)
  27. {
  28. tor_assert(next_interval < MAX_INTERVAL);
  29. struct timeval tv;
  30. tv.tv_sec = next_interval;
  31. tv.tv_usec = 0;
  32. event_add(event->ev, &tv);
  33. }
  34. /** Wraps dispatches for periodic events, <b>data</b> will be a pointer to the
  35. * event that needs to be called */
  36. static void
  37. periodic_event_dispatch(evutil_socket_t fd, short what, void *data)
  38. {
  39. (void)fd;
  40. (void)what;
  41. periodic_event_item_t *event = data;
  42. time_t now = time(NULL);
  43. const or_options_t *options = get_options();
  44. // log_debug(LD_GENERAL, "Dispatching %s", event->name);
  45. int r = event->fn(now, options);
  46. int next_interval = 0;
  47. /* update the last run time if action was taken */
  48. if (r==0) {
  49. log_err(LD_BUG, "Invalid return value for periodic event from %s.",
  50. event->name);
  51. tor_assert(r != 0);
  52. } else if (r > 0) {
  53. event->last_action_time = now;
  54. /* If the event is meant to happen after ten years, that's likely
  55. * a bug, and somebody gave an absolute time rather than an interval.
  56. */
  57. tor_assert(r < MAX_INTERVAL);
  58. next_interval = r;
  59. } else {
  60. /* no action was taken, it is likely a precondition failed,
  61. * we should reschedule for next second incase the precondition
  62. * passes then */
  63. next_interval = 1;
  64. }
  65. // log_debug(LD_GENERAL, "Scheduling %s for %d seconds", event->name,
  66. // next_interval);
  67. struct timeval tv = { next_interval , 0 };
  68. event_add(event->ev, &tv);
  69. }
  70. /** Schedules <b>event</b> to run as soon as possible from now. */
  71. void
  72. periodic_event_reschedule(periodic_event_item_t *event)
  73. {
  74. periodic_event_set_interval(event, 1);
  75. }
  76. /** Initializes the libevent backend for a periodic event. */
  77. void
  78. periodic_event_setup(periodic_event_item_t *event)
  79. {
  80. if (event->ev) { /* Already setup? This is a bug */
  81. log_err(LD_BUG, "Initial dispatch should only be done once.");
  82. tor_assert(0);
  83. }
  84. event->ev = tor_event_new(tor_libevent_get_base(),
  85. -1, 0,
  86. periodic_event_dispatch,
  87. event);
  88. tor_assert(event->ev);
  89. }
  90. /** Handles initial dispatch for periodic events. It should happen 1 second
  91. * after the events are created to mimic behaviour before #3199's refactor */
  92. void
  93. periodic_event_launch(periodic_event_item_t *event)
  94. {
  95. if (! event->ev) { /* Not setup? This is a bug */
  96. log_err(LD_BUG, "periodic_event_launch without periodic_event_setup");
  97. tor_assert(0);
  98. }
  99. // Initial dispatch
  100. periodic_event_dispatch(-1, EV_TIMEOUT, event);
  101. }
  102. /** Release all storage associated with <b>event</b> */
  103. void
  104. periodic_event_destroy(periodic_event_item_t *event)
  105. {
  106. if (!event)
  107. return;
  108. tor_event_free(event->ev);
  109. event->last_action_time = 0;
  110. }