periodic.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. static const int MAX_INTERVAL = 10 * 365 * 86400;
  13. /** DOCDOC */
  14. static void
  15. periodic_event_set_interval(periodic_event_item_t *event,
  16. time_t next_interval)
  17. {
  18. tor_assert(next_interval < MAX_INTERVAL);
  19. struct timeval tv;
  20. tv.tv_sec = next_interval;
  21. tv.tv_usec = 0;
  22. event_add(event->ev, &tv);
  23. }
  24. /** Wraps dispatches for periodic events, <b>data</b> will be a pointer to the
  25. * event that needs to be called */
  26. static void
  27. periodic_event_dispatch(evutil_socket_t fd, short what, void *data)
  28. {
  29. (void)fd;
  30. (void)what;
  31. periodic_event_item_t *event = data;
  32. time_t now = time(NULL);
  33. const or_options_t *options = get_options();
  34. int r = event->fn(now, options);
  35. int next_interval = 0;
  36. /* update the last run time if action was taken */
  37. if (r==0) {
  38. log_err(LD_BUG, "Invalid return value for periodic event from %s.",
  39. event->name);
  40. tor_assert(r != 0);
  41. } else if (r > 0) {
  42. event->last_action_time = now;
  43. /* If the event is meant to happen after ten years, that's likely
  44. * a bug, and somebody gave an absolute time rather than an interval.
  45. */
  46. tor_assert(r < MAX_INTERVAL);
  47. next_interval = r;
  48. } else {
  49. /* no action was taken, it is likely a precondition failed,
  50. * we should reschedule for next second incase the precondition
  51. * passes then */
  52. next_interval = 1;
  53. }
  54. struct timeval tv = { next_interval , 0 };
  55. event_add(event->ev, &tv);
  56. log_info(LD_GENERAL, "Dispatching %s", event->name);
  57. }
  58. /** DOCDOC */
  59. void
  60. periodic_event_reschedule(periodic_event_item_t *event)
  61. {
  62. periodic_event_set_interval(event, 1);
  63. }
  64. /** Handles initial dispatch for periodic events. It should happen 1 second
  65. * after the events are created to mimic behaviour before #3199's refactor */
  66. void
  67. periodic_event_launch(periodic_event_item_t *event)
  68. {
  69. if (event->ev) { /** Already setup? This is a bug */
  70. log_err(LD_BUG, "Initial dispatch should only be done once.");
  71. tor_assert(0);
  72. }
  73. event->ev = tor_event_new(tor_libevent_get_base(),
  74. -1, 0,
  75. periodic_event_dispatch,
  76. event);
  77. tor_assert(event->ev);
  78. // Initial dispatch
  79. periodic_event_dispatch(-1, EV_TIMEOUT, event);
  80. }
  81. /** DOCDOC */
  82. void
  83. periodic_event_destroy(periodic_event_item_t *event)
  84. {
  85. if (!event)
  86. return;
  87. tor_event_free(event->ev);
  88. event->last_action_time = 0;
  89. }