1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* event.c: Event functions. |
4 |
* |
5 |
* Copyright (c) 1998-2000 Regents of the University of California |
6 |
* Copyright (c) 2000-2014 ircd-hybrid development team |
7 |
* |
8 |
* Code borrowed from the squid web cache by Adrian Chadd. |
9 |
* Original header: |
10 |
* |
11 |
* DEBUG: section 41 Event Processing |
12 |
* AUTHOR: Henrik Nordstrom |
13 |
* |
14 |
* SQUID Internet Object Cache http://squid.nlanr.net/Squid/ |
15 |
* ---------------------------------------------------------- |
16 |
* |
17 |
* Squid is the result of efforts by numerous individuals from the |
18 |
* Internet community. Development is led by Duane Wessels of the |
19 |
* National Laboratory for Applied Network Research and funded by the |
20 |
* National Science Foundation. Squid is Copyrighted (C) 1998 by |
21 |
* the Regents of the University of California. Please see the |
22 |
* COPYRIGHT file for full details. Squid incorporates software |
23 |
* developed and/or copyrighted by other sources. Please see the |
24 |
* CREDITS file for full details. |
25 |
* |
26 |
* This program is free software; you can redistribute it and/or modify |
27 |
* it under the terms of the GNU General Public License as published by |
28 |
* the Free Software Foundation; either version 2 of the License, or |
29 |
* (at your option) any later version. |
30 |
* |
31 |
* This program is distributed in the hope that it will be useful, |
32 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
33 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
34 |
* GNU General Public License for more details. |
35 |
* |
36 |
* You should have received a copy of the GNU General Public License |
37 |
* along with this program; if not, write to the Free Software |
38 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
39 |
* USA |
40 |
* |
41 |
* $Id$ |
42 |
*/ |
43 |
|
44 |
/* |
45 |
* How it's used: |
46 |
* |
47 |
* Should be pretty self-explanatory. Events are added to the static |
48 |
* array event_table with a frequency time telling eventRun how often |
49 |
* to execute it. |
50 |
*/ |
51 |
|
52 |
#include "stdinc.h" |
53 |
#include "list.h" |
54 |
#include "ircd.h" |
55 |
#include "event.h" |
56 |
#include "client.h" |
57 |
#include "send.h" |
58 |
#include "memory.h" |
59 |
#include "log.h" |
60 |
#include "numeric.h" |
61 |
#include "rng_mt.h" |
62 |
|
63 |
|
64 |
static const char *last_event_ran = NULL; |
65 |
static struct ev_entry event_table[MAX_EVENTS]; |
66 |
static time_t event_time_min = -1; |
67 |
|
68 |
|
69 |
/* |
70 |
* int eventFind(EVH *func, void *arg) |
71 |
* |
72 |
* Input: Event function and the argument passed to it |
73 |
* Output: Index to the slow in the event_table |
74 |
* Side Effects: None |
75 |
*/ |
76 |
static int |
77 |
eventFind(EVH *func, void *arg) |
78 |
{ |
79 |
for (unsigned int i = 0; i < MAX_EVENTS; ++i) |
80 |
if ((event_table[i].func == func) && |
81 |
(event_table[i].arg == arg) && |
82 |
event_table[i].active) |
83 |
return i; |
84 |
|
85 |
return -1; |
86 |
} |
87 |
|
88 |
/* |
89 |
* void eventAdd(const char *name, EVH *func, void *arg, time_t when) |
90 |
* |
91 |
* Input: Name of event, function to call, arguments to pass, and frequency |
92 |
* of the event. |
93 |
* Output: None |
94 |
* Side Effects: Adds the event to the event list. |
95 |
*/ |
96 |
void |
97 |
eventAdd(const char *name, EVH *func, void *arg, time_t when) |
98 |
{ |
99 |
/* find first inactive index, or use next index */ |
100 |
for (unsigned int i = 0; i < MAX_EVENTS; ++i) |
101 |
{ |
102 |
if (event_table[i].active == 0) |
103 |
{ |
104 |
event_table[i].func = func; |
105 |
event_table[i].name = name; |
106 |
event_table[i].arg = arg; |
107 |
event_table[i].when = CurrentTime + when; |
108 |
event_table[i].frequency = when; |
109 |
event_table[i].active = 1; |
110 |
|
111 |
if ((event_table[i].when < event_time_min) || (event_time_min == -1)) |
112 |
event_time_min = event_table[i].when; |
113 |
|
114 |
return; |
115 |
} |
116 |
} |
117 |
} |
118 |
|
119 |
/* |
120 |
* void eventDelete(EVH *func, void *arg) |
121 |
* |
122 |
* Input: Function handler, argument that was passed. |
123 |
* Output: None |
124 |
* Side Effects: Removes the event from the event list |
125 |
*/ |
126 |
void |
127 |
eventDelete(EVH *func, void *arg) |
128 |
{ |
129 |
int i = eventFind(func, arg); |
130 |
|
131 |
if (i == -1) |
132 |
return; |
133 |
|
134 |
event_table[i].name = NULL; |
135 |
event_table[i].func = NULL; |
136 |
event_table[i].arg = NULL; |
137 |
event_table[i].active = 0; |
138 |
} |
139 |
|
140 |
/* |
141 |
* void eventAddIsh(const char *name, EVH *func, void *arg, time_t delta_isa) |
142 |
* |
143 |
* Input: Name of event, function to call, arguments to pass, and frequency |
144 |
* of the event. |
145 |
* Output: None |
146 |
* Side Effects: Adds the event to the event list within +- 1/3 of the |
147 |
* specified frequency. |
148 |
*/ |
149 |
void |
150 |
eventAddIsh(const char *name, EVH *func, void *arg, time_t delta_ish) |
151 |
{ |
152 |
if (delta_ish >= 3) |
153 |
{ |
154 |
const time_t two_third = (2 * delta_ish) / 3; |
155 |
delta_ish = two_third + ((genrand_int32() % 1000) * two_third) / 1000; |
156 |
/* |
157 |
* XXX I hate the above magic, I don't even know if its right. |
158 |
* Grr. -- adrian |
159 |
*/ |
160 |
} |
161 |
|
162 |
eventAdd(name, func, arg, delta_ish); |
163 |
} |
164 |
|
165 |
/* |
166 |
* void eventRun(void) |
167 |
* |
168 |
* Input: None |
169 |
* Output: None |
170 |
* Side Effects: Runs pending events in the event list |
171 |
*/ |
172 |
void |
173 |
eventRun(void) |
174 |
{ |
175 |
for (unsigned int i = 0; i < MAX_EVENTS; ++i) |
176 |
{ |
177 |
if (event_table[i].active && (event_table[i].when <= CurrentTime)) |
178 |
{ |
179 |
last_event_ran = event_table[i].name; |
180 |
event_table[i].func(event_table[i].arg); |
181 |
event_table[i].when = CurrentTime + event_table[i].frequency; |
182 |
event_time_min = -1; |
183 |
} |
184 |
} |
185 |
} |
186 |
|
187 |
/* |
188 |
* time_t eventNextTime(void) |
189 |
* |
190 |
* Input: None |
191 |
* Output: Specifies the next time eventRun() should be run |
192 |
* Side Effects: None |
193 |
*/ |
194 |
time_t |
195 |
eventNextTime(void) |
196 |
{ |
197 |
if (event_time_min == -1) |
198 |
for (unsigned int i = 0; i < MAX_EVENTS; ++i) |
199 |
if (event_table[i].active && ((event_table[i].when < event_time_min) || (event_time_min == -1))) |
200 |
event_time_min = event_table[i].when; |
201 |
|
202 |
return event_time_min; |
203 |
} |
204 |
|
205 |
/* |
206 |
* void eventInit(void) |
207 |
* |
208 |
* Input: None |
209 |
* Output: None |
210 |
* Side Effects: Initializes the event system. |
211 |
*/ |
212 |
void |
213 |
eventInit(void) |
214 |
{ |
215 |
last_event_ran = NULL; |
216 |
memset(event_table, 0, sizeof(event_table)); |
217 |
} |
218 |
|
219 |
/* |
220 |
* void show_events(struct Client *source_p) |
221 |
* |
222 |
* Input: Client requesting the event |
223 |
* Output: List of events |
224 |
* Side Effects: None |
225 |
*/ |
226 |
void |
227 |
show_events(struct Client *source_p) |
228 |
{ |
229 |
if (last_event_ran) |
230 |
{ |
231 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG|SND_EXPLICIT, |
232 |
"E :Last event to run: %s", |
233 |
last_event_ran); |
234 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG|SND_EXPLICIT, |
235 |
"E : "); |
236 |
} |
237 |
|
238 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG|SND_EXPLICIT, |
239 |
"E :Operation Next Execution"); |
240 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG|SND_EXPLICIT, |
241 |
"E :-------------------------------------------"); |
242 |
|
243 |
for (unsigned i = 0; i < MAX_EVENTS; ++i) |
244 |
if (event_table[i].active) |
245 |
sendto_one_numeric(source_p, &me, RPL_STATSDEBUG|SND_EXPLICIT, |
246 |
"E :%-28s %-4d seconds", |
247 |
event_table[i].name, |
248 |
(int)(event_table[i].when - CurrentTime)); |
249 |
} |
250 |
|
251 |
/* |
252 |
* void set_back_events(time_t by) |
253 |
* Input: Time to set back events by. |
254 |
* Output: None. |
255 |
* Side-effects: Sets back all events by "by" seconds. |
256 |
*/ |
257 |
void |
258 |
set_back_events(time_t by) |
259 |
{ |
260 |
event_time_min = -1; |
261 |
|
262 |
for (unsigned int i = 0; i < MAX_EVENTS; ++i) |
263 |
{ |
264 |
if (event_table[i].when > by) |
265 |
event_table[i].when -= by; |
266 |
else |
267 |
event_table[i].when = 0; |
268 |
} |
269 |
} |