periodic.c 5.0 KB

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