periodic.c 3.4 KB

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