periodic.c 3.3 KB

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