tor  0.4.1.0-alpha-dev
periodic.c
Go to the documentation of this file.
1 /* Copyright (c) 2015-2019, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
14 #include "core/or/or.h"
16 #include "app/config/config.h"
17 #include "core/mainloop/mainloop.h"
18 #include "core/mainloop/periodic.h"
20 
25 static const int MAX_INTERVAL = 10 * 365 * 86400;
26 
29 static void
31  time_t next_interval)
32 {
33  tor_assert(next_interval < MAX_INTERVAL);
34  struct timeval tv;
35  tv.tv_sec = next_interval;
36  tv.tv_usec = 0;
37  mainloop_event_schedule(event->ev, &tv);
38 }
39 
42 static void
44 {
45  periodic_event_item_t *event = data;
46  tor_assert(ev == event->ev);
47 
48  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;
54 
55  if (!periodic_event_is_enabled(event)) {
56  /* The event got disabled from inside its callback, or before: no need to
57  * reschedule. */
58  return;
59  }
60 
61  /* update the last run time if action was taken */
62  if (r==0) {
63  log_err(LD_BUG, "Invalid return value for periodic event from %s.",
64  event->name);
65  tor_assert(r != 0);
66  } else if (r > 0) {
67  event->last_action_time = now;
68  /* If the event is meant to happen after ten years, that's likely
69  * a bug, and somebody gave an absolute time rather than an interval.
70  */
72  next_interval = r;
73  } else {
74  /* no action was taken, it is likely a precondition failed,
75  * we should reschedule for next second incase the precondition
76  * passes then */
77  next_interval = 1;
78  }
79 
80 // log_debug(LD_GENERAL, "Scheduling %s for %d seconds", event->name,
81 // next_interval);
82  struct timeval tv = { next_interval , 0 };
83  mainloop_event_schedule(ev, &tv);
84 }
85 
87 void
89 {
90  /* Don't reschedule a disabled event. */
91  if (periodic_event_is_enabled(event)) {
93  }
94 }
95 
97 void
99 {
100  if (event->ev) { /* Already setup? This is a bug */
101  log_err(LD_BUG, "Initial dispatch should only be done once.");
102  tor_assert(0);
103  }
104 
106  event);
107  tor_assert(event->ev);
108 }
109 
112 void
114 {
115  if (! event->ev) { /* Not setup? This is a bug */
116  log_err(LD_BUG, "periodic_event_launch without periodic_event_setup");
117  tor_assert(0);
118  }
119  /* Event already enabled? This is a bug */
120  if (periodic_event_is_enabled(event)) {
121  log_err(LD_BUG, "periodic_event_launch on an already enabled event");
122  tor_assert(0);
123  }
124 
125  // Initial dispatch
126  event->enabled = 1;
127  periodic_event_dispatch(event->ev, event);
128 }
129 
131 void
133 {
134  if (!event)
135  return;
136  mainloop_event_free(event->ev);
137  event->last_action_time = 0;
138 }
139 
143 void
145 {
146  tor_assert(event);
147  /* Safely and silently ignore if this event is already enabled. */
148  if (periodic_event_is_enabled(event)) {
149  return;
150  }
151 
152  tor_assert(event->ev);
153  event->enabled = 1;
154  mainloop_event_activate(event->ev);
155 }
156 
160 void
162 {
163  tor_assert(event);
164  /* Safely and silently ignore if this event is already disabled. */
165  if (!periodic_event_is_enabled(event)) {
166  return;
167  }
168  mainloop_event_cancel(event->ev);
169  event->enabled = 0;
170 }
171 
176 void
178 {
179  tor_assert(event);
180  if (!periodic_event_is_enabled(event))
181  return;
182 
183  periodic_event_disable(event);
184 
185  mainloop_event_activate(event->ev);
186 }
static const int MAX_INTERVAL
Definition: periodic.c:25
void periodic_event_enable(periodic_event_item_t *event)
Definition: periodic.c:144
void update_current_time(time_t now)
Definition: mainloop.c:2677
Header file for config.c.
static void periodic_event_set_interval(periodic_event_item_t *event, time_t next_interval)
Definition: periodic.c:30
Header file for mainloop.c.
void mainloop_event_activate(mainloop_event_t *event)
tor_assert(buffer)
struct mainloop_event_t * ev
Definition: periodic.h:61
void mainloop_event_cancel(mainloop_event_t *event)
Master header file for Tor-specific functionality.
void periodic_event_schedule_and_disable(periodic_event_item_t *event)
Definition: periodic.c:177
void periodic_event_reschedule(periodic_event_item_t *event)
Definition: periodic.c:88
void periodic_event_disable(periodic_event_item_t *event)
Definition: periodic.c:161
static void periodic_event_dispatch(mainloop_event_t *ev, void *data)
Definition: periodic.c:43
mainloop_event_t * mainloop_event_new(void(*cb)(mainloop_event_t *, void *), void *userdata)
void periodic_event_setup(periodic_event_item_t *event)
Definition: periodic.c:98
void periodic_event_launch(periodic_event_item_t *event)
Definition: periodic.c:113
void periodic_event_destroy(periodic_event_item_t *event)
Definition: periodic.c:132
int mainloop_event_schedule(mainloop_event_t *event, const struct timeval *tv)
Header for compat_libevent.c.
#define LD_BUG
Definition: log.h:82