periodic.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright (c) 2015-2018, 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/or.h"
  13. #include "common/compat_libevent.h"
  14. #include "or/config.h"
  15. #include "or/main.h"
  16. #include "or/periodic.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. mainloop_event_schedule(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(mainloop_event_t *ev, void *data)
  38. {
  39. periodic_event_item_t *event = data;
  40. tor_assert(ev == event->ev);
  41. if (BUG(!periodic_event_is_enabled(event))) {
  42. return;
  43. }
  44. time_t now = time(NULL);
  45. update_current_time(now);
  46. const or_options_t *options = get_options();
  47. // log_debug(LD_GENERAL, "Dispatching %s", event->name);
  48. int r = event->fn(now, options);
  49. int next_interval = 0;
  50. if (!periodic_event_is_enabled(event)) {
  51. /* The event got disabled from inside its callback; no need to
  52. * reschedule. */
  53. return;
  54. }
  55. /* update the last run time if action was taken */
  56. if (r==0) {
  57. log_err(LD_BUG, "Invalid return value for periodic event from %s.",
  58. event->name);
  59. tor_assert(r != 0);
  60. } else if (r > 0) {
  61. event->last_action_time = now;
  62. /* If the event is meant to happen after ten years, that's likely
  63. * a bug, and somebody gave an absolute time rather than an interval.
  64. */
  65. tor_assert(r < MAX_INTERVAL);
  66. next_interval = r;
  67. } else {
  68. /* no action was taken, it is likely a precondition failed,
  69. * we should reschedule for next second incase the precondition
  70. * passes then */
  71. next_interval = 1;
  72. }
  73. // log_debug(LD_GENERAL, "Scheduling %s for %d seconds", event->name,
  74. // next_interval);
  75. struct timeval tv = { next_interval , 0 };
  76. mainloop_event_schedule(ev, &tv);
  77. }
  78. /** Schedules <b>event</b> to run as soon as possible from now. */
  79. void
  80. periodic_event_reschedule(periodic_event_item_t *event)
  81. {
  82. /* Don't reschedule a disabled event. */
  83. if (periodic_event_is_enabled(event)) {
  84. periodic_event_set_interval(event, 1);
  85. }
  86. }
  87. /** Initializes the libevent backend for a periodic event. */
  88. void
  89. periodic_event_setup(periodic_event_item_t *event)
  90. {
  91. if (event->ev) { /* Already setup? This is a bug */
  92. log_err(LD_BUG, "Initial dispatch should only be done once.");
  93. tor_assert(0);
  94. }
  95. event->ev = mainloop_event_new(periodic_event_dispatch,
  96. event);
  97. tor_assert(event->ev);
  98. }
  99. /** Handles initial dispatch for periodic events. It should happen 1 second
  100. * after the events are created to mimic behaviour before #3199's refactor */
  101. void
  102. periodic_event_launch(periodic_event_item_t *event)
  103. {
  104. if (! event->ev) { /* Not setup? This is a bug */
  105. log_err(LD_BUG, "periodic_event_launch without periodic_event_setup");
  106. tor_assert(0);
  107. }
  108. /* Event already enabled? This is a bug */
  109. if (periodic_event_is_enabled(event)) {
  110. log_err(LD_BUG, "periodic_event_launch on an already enabled event");
  111. tor_assert(0);
  112. }
  113. // Initial dispatch
  114. event->enabled = 1;
  115. periodic_event_dispatch(event->ev, event);
  116. }
  117. /** Release all storage associated with <b>event</b> */
  118. void
  119. periodic_event_destroy(periodic_event_item_t *event)
  120. {
  121. if (!event)
  122. return;
  123. mainloop_event_free(event->ev);
  124. event->last_action_time = 0;
  125. }
  126. /** Enable the given event which means the event is launched and then the
  127. * event's enabled flag is set. This can be called for an event that is
  128. * already enabled. */
  129. void
  130. periodic_event_enable(periodic_event_item_t *event)
  131. {
  132. tor_assert(event);
  133. /* Safely and silently ignore if this event is already enabled. */
  134. if (periodic_event_is_enabled(event)) {
  135. return;
  136. }
  137. periodic_event_launch(event);
  138. }
  139. /** Disable the given event which means the event is destroyed and then the
  140. * event's enabled flag is unset. This can be called for an event that is
  141. * already disabled. */
  142. void
  143. periodic_event_disable(periodic_event_item_t *event)
  144. {
  145. tor_assert(event);
  146. /* Safely and silently ignore if this event is already disabled. */
  147. if (!periodic_event_is_enabled(event)) {
  148. return;
  149. }
  150. mainloop_event_cancel(event->ev);
  151. event->enabled = 0;
  152. }