1 /* Copyright (c) 2015-2017, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
7 * \brief Generic backend for handling periodic events.
9 * The events in this module are used by main.c to track items that need
10 * to fire once every N seconds, possibly picking a new interval each time
11 * that they fire. See periodic_events[] in main.c for examples.
15 #include "compat_libevent.h"
19 #include <event2/event.h>
21 /** We disable any interval greater than this number of seconds, on the
22 * grounds that it is probably an absolute time mistakenly passed in as a
25 static const int MAX_INTERVAL
= 10 * 365 * 86400;
27 /** Set the event <b>event</b> to run in <b>next_interval</b> seconds from
30 periodic_event_set_interval(periodic_event_item_t
*event
,
33 tor_assert(next_interval
< MAX_INTERVAL
);
35 tv
.tv_sec
= next_interval
;
37 event_add(event
->ev
, &tv
);
40 /** Wraps dispatches for periodic events, <b>data</b> will be a pointer to the
41 * event that needs to be called */
43 periodic_event_dispatch(evutil_socket_t fd
, short what
, void *data
)
47 periodic_event_item_t
*event
= data
;
49 time_t now
= time(NULL
);
50 const or_options_t
*options
= get_options();
51 // log_debug(LD_GENERAL, "Dispatching %s", event->name);
52 int r
= event
->fn(now
, options
);
53 int next_interval
= 0;
55 /* update the last run time if action was taken */
57 log_err(LD_BUG
, "Invalid return value for periodic event from %s.",
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.
65 tor_assert(r
< MAX_INTERVAL
);
68 /* no action was taken, it is likely a precondition failed,
69 * we should reschedule for next second incase the precondition
74 // log_debug(LD_GENERAL, "Scheduling %s for %d seconds", event->name,
76 struct timeval tv
= { next_interval
, 0 };
77 event_add(event
->ev
, &tv
);
80 /** Schedules <b>event</b> to run as soon as possible from now. */
82 periodic_event_reschedule(periodic_event_item_t
*event
)
84 periodic_event_set_interval(event
, 1);
87 /** Initializes the libevent backend for a periodic event. */
89 periodic_event_setup(periodic_event_item_t
*event
)
91 if (event
->ev
) { /* Already setup? This is a bug */
92 log_err(LD_BUG
, "Initial dispatch should only be done once.");
96 event
->ev
= tor_event_new(tor_libevent_get_base(),
98 periodic_event_dispatch
,
100 tor_assert(event
->ev
);
103 /** Handles initial dispatch for periodic events. It should happen 1 second
104 * after the events are created to mimic behaviour before #3199's refactor */
106 periodic_event_launch(periodic_event_item_t
*event
)
108 if (! event
->ev
) { /* Not setup? This is a bug */
109 log_err(LD_BUG
, "periodic_event_launch without periodic_event_setup");
114 periodic_event_dispatch(-1, EV_TIMEOUT
, event
);
117 /** Release all storage associated with <b>event</b> */
119 periodic_event_destroy(periodic_event_item_t
*event
)
123 tor_event_free(event
->ev
);
124 event
->last_action_time
= 0;