]> jfr.im git - solanum.git/blob - libratbox/src/event.c
Fix a couple more string leaks.
[solanum.git] / libratbox / src / event.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * event.c: Event functions.
4 *
5 * Copyright (C) 1998-2000 Regents of the University of California
6 * Copyright (C) 2001-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
8 *
9 * Code borrowed from the squid web cache by Adrian Chadd.
10 * Original header:
11 *
12 * DEBUG: section 41 Event Processing
13 * AUTHOR: Henrik Nordstrom
14 *
15 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
16 * ----------------------------------------------------------
17 *
18 * Squid is the result of efforts by numerous individuals from the
19 * Internet community. Development is led by Duane Wessels of the
20 * National Laboratory for Applied Network Research and funded by the
21 * National Science Foundation. Squid is Copyrighted (C) 1998 by
22 * the Regents of the University of California. Please see the
23 * COPYRIGHT file for full details. Squid incorporates software
24 * developed and/or copyrighted by other sources. Please see the
25 * CREDITS file for full details.
26 *
27 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation; either version 2 of the License, or
30 * (at your option) any later version.
31 *
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
40 * USA
41 *
42 * $Id: event.c 26272 2008-12-10 05:55:10Z androsyn $
43 */
44
45 #include <libratbox_config.h>
46 #include <ratbox_lib.h>
47 #include <commio-int.h>
48 #include <event-int.h>
49
50 #define EV_NAME_LEN 33
51 static char last_event_ran[EV_NAME_LEN];
52 static rb_dlink_list event_list;
53
54 static time_t event_time_min = -1;
55
56 /*
57 * struct ev_entry *
58 * rb_event_find(EVH *func, void *arg)
59 *
60 * Input: Event function and the argument passed to it
61 * Output: Index to the slow in the event_table
62 * Side Effects: None
63 */
64 static struct ev_entry *
65 rb_event_find(EVH * func, void *arg)
66 {
67 rb_dlink_node *ptr;
68 struct ev_entry *ev;
69 RB_DLINK_FOREACH(ptr, event_list.head)
70 {
71 ev = ptr->data;
72 if((ev->func == func) && (ev->arg == arg))
73 return ev;
74 }
75
76 return NULL;
77 }
78
79 /*
80 * struct ev_entry *
81 * rb_event_add(const char *name, EVH *func, void *arg, time_t when)
82 *
83 * Input: Name of event, function to call, arguments to pass, and frequency
84 * of the event.
85 * Output: None
86 * Side Effects: Adds the event to the event list.
87 */
88 struct ev_entry *
89 rb_event_add(const char *name, EVH * func, void *arg, time_t when)
90 {
91 struct ev_entry *ev;
92 ev = rb_malloc(sizeof(struct ev_entry));
93 ev->func = func;
94 ev->name = rb_strndup(name, EV_NAME_LEN);
95 ev->arg = arg;
96 ev->when = rb_current_time() + when;
97 ev->next = when;
98 ev->frequency = when;
99
100 if((ev->when < event_time_min) || (event_time_min == -1))
101 {
102 event_time_min = ev->when;
103 }
104 rb_dlinkAdd(ev, &ev->node, &event_list);
105 rb_io_sched_event(ev, when);
106 return ev;
107 }
108
109 struct ev_entry *
110 rb_event_addonce(const char *name, EVH * func, void *arg, time_t when)
111 {
112 struct ev_entry *ev;
113 ev = rb_malloc(sizeof(struct ev_entry));
114 ev->func = func;
115 ev->name = rb_strndup(name, EV_NAME_LEN);
116 ev->arg = arg;
117 ev->when = rb_current_time() + when;
118 ev->next = when;
119 ev->frequency = 0;
120
121 if((ev->when < event_time_min) || (event_time_min == -1))
122 event_time_min = ev->when;
123
124 rb_dlinkAdd(ev, &ev->node, &event_list);
125 rb_io_sched_event(ev, when);
126 return ev;
127 }
128
129 /*
130 * void rb_event_delete(struct ev_entry *ev)
131 *
132 * Input: pointer to ev_entry for the event
133 * Output: None
134 * Side Effects: Removes the event from the event list
135 */
136 void
137 rb_event_delete(struct ev_entry *ev)
138 {
139 if(ev == NULL)
140 return;
141
142 rb_dlinkDelete(&ev->node, &event_list);
143 rb_io_unsched_event(ev);
144 rb_free(ev->name);
145 rb_free(ev);
146 }
147
148 /*
149 * void rb_event_find_delete(EVH *func, void *arg)
150 *
151 * Input: pointer to func and data
152 * Output: None
153 * Side Effects: Removes the event from the event list
154 */
155 void
156 rb_event_find_delete(EVH * func, void *arg)
157 {
158 rb_event_delete(rb_event_find(func, arg));
159 }
160
161 /*
162 * struct ev_entry *
163 * rb_event_addish(const char *name, EVH *func, void *arg, time_t delta_isa)
164 *
165 * Input: Name of event, function to call, arguments to pass, and frequency
166 * of the event.
167 * Output: None
168 * Side Effects: Adds the event to the event list within +- 1/3 of the
169 * specified frequency.
170 */
171 struct ev_entry *
172 rb_event_addish(const char *name, EVH * func, void *arg, time_t delta_ish)
173 {
174 if(delta_ish >= 3.0)
175 {
176 const time_t two_third = (2 * delta_ish) / 3;
177 delta_ish = two_third + ((rand() % 1000) * two_third) / 1000;
178 /*
179 * XXX I hate the above magic, I don't even know if its right.
180 * Grr. -- adrian
181 */
182 }
183 return rb_event_add(name, func, arg, delta_ish);
184 }
185
186
187 void
188 rb_run_event(struct ev_entry *ev)
189 {
190 rb_strlcpy(last_event_ran, ev->name, sizeof(last_event_ran));
191 ev->func(ev->arg);
192 if(!ev->frequency)
193 {
194 rb_event_delete(ev);
195 return;
196 }
197 ev->when = rb_current_time() + ev->frequency;
198 if((ev->when < event_time_min) || (event_time_min == -1))
199 event_time_min = ev->when;
200 }
201
202 /*
203 * void rb_event_run(void)
204 *
205 * Input: None
206 * Output: None
207 * Side Effects: Runs pending events in the event list
208 */
209 void
210 rb_event_run(void)
211 {
212 rb_dlink_node *ptr, *next;
213 struct ev_entry *ev;
214
215 if(rb_io_supports_event())
216 return;
217
218 event_time_min = -1;
219 RB_DLINK_FOREACH_SAFE(ptr, next, event_list.head)
220 {
221 ev = ptr->data;
222 if(ev->when <= rb_current_time())
223 {
224 rb_strlcpy(last_event_ran, ev->name, sizeof(last_event_ran));
225 ev->func(ev->arg);
226
227 /* event is scheduled more than once */
228 if(ev->frequency)
229 {
230 ev->when = rb_current_time() + ev->frequency;
231 if((ev->when < event_time_min) || (event_time_min == -1))
232 event_time_min = ev->when;
233 }
234 else
235 {
236 rb_dlinkDelete(&ev->node, &event_list);
237 rb_free(ev);
238 }
239 }
240 else
241 {
242 if((ev->when < event_time_min) || (event_time_min == -1))
243 event_time_min = ev->when;
244 }
245 }
246 }
247
248 void
249 rb_event_io_register_all(void)
250 {
251 rb_dlink_node *ptr;
252 struct ev_entry *ev;
253
254 if(!rb_io_supports_event())
255 return;
256
257 RB_DLINK_FOREACH(ptr, event_list.head)
258 {
259 ev = ptr->data;
260 rb_io_sched_event(ev, ev->next);
261 }
262 }
263
264 /*
265 * void rb_event_init(void)
266 *
267 * Input: None
268 * Output: None
269 * Side Effects: Initializes the event system.
270 */
271 void
272 rb_event_init(void)
273 {
274 rb_strlcpy(last_event_ran, "NONE", sizeof(last_event_ran));
275 }
276
277 void
278 rb_dump_events(void (*func) (char *, void *), void *ptr)
279 {
280 int len;
281 char buf[512];
282 rb_dlink_node *dptr;
283 struct ev_entry *ev;
284 len = sizeof(buf);
285
286 rb_snprintf(buf, len, "Last event to run: %s", last_event_ran);
287 func(buf, ptr);
288
289 rb_strlcpy(buf, "Operation Next Execution", len);
290 func(buf, ptr);
291
292 RB_DLINK_FOREACH(dptr, event_list.head)
293 {
294 ev = dptr->data;
295 rb_snprintf(buf, len, "%-28s %-4ld seconds", ev->name,
296 ev->when - (long)rb_current_time());
297 func(buf, ptr);
298 }
299 }
300
301 /*
302 * void rb_set_back_events(time_t by)
303 * Input: Time to set back events by.
304 * Output: None.
305 * Side-effects: Sets back all events by "by" seconds.
306 */
307 void
308 rb_set_back_events(time_t by)
309 {
310 rb_dlink_node *ptr;
311 struct ev_entry *ev;
312 RB_DLINK_FOREACH(ptr, event_list.head)
313 {
314 ev = ptr->data;
315 if(ev->when > by)
316 ev->when -= by;
317 else
318 ev->when = 0;
319 }
320 }
321
322 void
323 rb_event_update(struct ev_entry *ev, time_t freq)
324 {
325 if(ev == NULL)
326 return;
327
328 ev->frequency = freq;
329
330 /* update when its scheduled to run if its higher
331 * than the new frequency
332 */
333 if((rb_current_time() + freq) < ev->when)
334 ev->when = rb_current_time() + freq;
335 return;
336 }
337
338 time_t
339 rb_event_next(void)
340 {
341 return event_time_min;
342 }