vfs: check userland buffers before reading them.
[haiku.git] / src / kits / network / netresolv / isc / ev_timers.c
blob12ac2cebca5fc6ea81444bd2543c21e7ac18ada5
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1995-1999 by Internet Software Consortium
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 /* ev_timers.c - implement timers for the eventlib
19 * vix 09sep95 [initial]
22 #if !defined(LINT) && !defined(CODECENTER)
23 static const char rcsid[] = "$Id: ev_timers.c,v 1.6 2005/04/27 04:56:36 sra Exp $";
24 #endif
26 /* Import. */
28 #include "port_before.h"
29 #include "fd_setsize.h"
31 #include <errno.h>
33 #include <isc/assertions.h>
34 #include <isc/eventlib.h>
35 #include "eventlib_p.h"
37 #include "port_after.h"
39 /* Constants. */
41 #define MILLION 1000000
42 #define BILLION 1000000000
44 /* Forward. */
46 static int due_sooner(void *, void *);
47 static void set_index(void *, int);
48 static void free_timer(void *, void *);
49 static void print_timer(void *, void *);
50 static void idle_timeout(evContext, void *, struct timespec, struct timespec);
52 /* Private type. */
54 typedef struct {
55 evTimerFunc func;
56 void * uap;
57 struct timespec lastTouched;
58 struct timespec max_idle;
59 evTimer * timer;
60 } idle_timer;
62 /* Public. */
64 struct timespec
65 evConsTime(time_t sec, long nsec) {
66 struct timespec x;
68 x.tv_sec = sec;
69 x.tv_nsec = nsec;
70 return (x);
73 struct timespec
74 evAddTime(struct timespec addend1, struct timespec addend2) {
75 struct timespec x;
77 x.tv_sec = addend1.tv_sec + addend2.tv_sec;
78 x.tv_nsec = addend1.tv_nsec + addend2.tv_nsec;
79 if (x.tv_nsec >= BILLION) {
80 x.tv_sec++;
81 x.tv_nsec -= BILLION;
83 return (x);
86 struct timespec
87 evSubTime(struct timespec minuend, struct timespec subtrahend) {
88 struct timespec x;
90 x.tv_sec = minuend.tv_sec - subtrahend.tv_sec;
91 if (minuend.tv_nsec >= subtrahend.tv_nsec)
92 x.tv_nsec = minuend.tv_nsec - subtrahend.tv_nsec;
93 else {
94 x.tv_nsec = BILLION - subtrahend.tv_nsec + minuend.tv_nsec;
95 x.tv_sec--;
97 return (x);
101 evCmpTime(struct timespec a, struct timespec b) {
102 long x = a.tv_sec - b.tv_sec;
104 if (x == 0L)
105 x = a.tv_nsec - b.tv_nsec;
106 return (x < 0L ? (-1) : x > 0L ? (1) : (0));
109 struct timespec
110 evNowTime() {
111 struct timeval now;
112 #ifdef CLOCK_REALTIME
113 struct timespec tsnow;
114 int m = CLOCK_REALTIME;
116 #ifdef CLOCK_MONOTONIC
117 if (__evOptMonoTime)
118 m = CLOCK_MONOTONIC;
119 #endif
120 if (clock_gettime(m, &tsnow) == 0)
121 return (tsnow);
122 #endif
123 if (gettimeofday(&now, NULL) < 0)
124 return (evConsTime(0, 0));
125 return (evTimeSpec(now));
128 struct timespec
129 evUTCTime() {
130 struct timeval now;
131 #ifdef CLOCK_REALTIME
132 struct timespec tsnow;
133 if (clock_gettime(CLOCK_REALTIME, &tsnow) == 0)
134 return (tsnow);
135 #endif
136 if (gettimeofday(&now, NULL) < 0)
137 return (evConsTime(0, 0));
138 return (evTimeSpec(now));
141 struct timespec
142 evLastEventTime(evContext opaqueCtx) {
143 evContext_p *ctx = opaqueCtx.opaque;
145 return (ctx->lastEventTime);
148 struct timespec
149 evTimeSpec(struct timeval tv) {
150 struct timespec ts;
152 ts.tv_sec = tv.tv_sec;
153 ts.tv_nsec = tv.tv_usec * 1000;
154 return (ts);
157 struct timeval
158 evTimeVal(struct timespec ts) {
159 struct timeval tv;
161 tv.tv_sec = ts.tv_sec;
162 tv.tv_usec = ts.tv_nsec / 1000;
163 return (tv);
167 evSetTimer(evContext opaqueCtx,
168 evTimerFunc func,
169 void *uap,
170 struct timespec due,
171 struct timespec inter,
172 evTimerID *opaqueID
174 evContext_p *ctx = opaqueCtx.opaque;
175 evTimer *id;
177 evPrintf(ctx, 1,
178 "evSetTimer(ctx %p, func %p, uap %p, due %ld.%09ld, inter %ld.%09ld)\n",
179 ctx, func, uap,
180 (long)due.tv_sec, due.tv_nsec,
181 (long)inter.tv_sec, inter.tv_nsec);
183 #ifdef __hpux
185 * tv_sec and tv_nsec are unsigned.
187 if (due.tv_nsec >= BILLION)
188 EV_ERR(EINVAL);
190 if (inter.tv_nsec >= BILLION)
191 EV_ERR(EINVAL);
192 #else
193 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
194 EV_ERR(EINVAL);
196 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
197 EV_ERR(EINVAL);
198 #endif
200 /* due={0,0} is a magic cookie meaning "now." */
201 if (due.tv_sec == (time_t)0 && due.tv_nsec == 0L)
202 due = evNowTime();
204 /* Allocate and fill. */
205 OKNEW(id);
206 id->func = func;
207 id->uap = uap;
208 id->due = due;
209 id->inter = inter;
211 if (heap_insert(ctx->timers, id) < 0)
212 return (-1);
214 /* Remember the ID if the caller provided us a place for it. */
215 if (opaqueID)
216 opaqueID->opaque = id;
218 if (ctx->debug > 7) {
219 evPrintf(ctx, 7, "timers after evSetTimer:\n");
220 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
223 return (0);
227 evClearTimer(evContext opaqueCtx, evTimerID id) {
228 evContext_p *ctx = opaqueCtx.opaque;
229 evTimer *del = id.opaque;
231 if (ctx->cur != NULL &&
232 ctx->cur->type == Timer &&
233 ctx->cur->u.timer.this == del) {
234 evPrintf(ctx, 8, "deferring delete of timer (executing)\n");
236 * Setting the interval to zero ensures that evDrop() will
237 * clean up the timer.
239 del->inter = evConsTime(0, 0);
240 return (0);
243 if (heap_element(ctx->timers, del->index) != del)
244 EV_ERR(ENOENT);
246 if (heap_delete(ctx->timers, del->index) < 0)
247 return (-1);
248 FREE(del);
250 if (ctx->debug > 7) {
251 evPrintf(ctx, 7, "timers after evClearTimer:\n");
252 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
255 return (0);
259 evConfigTimer(evContext opaqueCtx,
260 evTimerID id,
261 const char *param,
262 int value
264 evContext_p *ctx = opaqueCtx.opaque;
265 evTimer *timer = id.opaque;
266 int result=0;
268 UNUSED(value);
270 if (heap_element(ctx->timers, timer->index) != timer)
271 EV_ERR(ENOENT);
273 if (strcmp(param, "rate") == 0)
274 timer->mode |= EV_TMR_RATE;
275 else if (strcmp(param, "interval") == 0)
276 timer->mode &= ~EV_TMR_RATE;
277 else
278 EV_ERR(EINVAL);
280 return (result);
284 evResetTimer(evContext opaqueCtx,
285 evTimerID id,
286 evTimerFunc func,
287 void *uap,
288 struct timespec due,
289 struct timespec inter
291 evContext_p *ctx = opaqueCtx.opaque;
292 evTimer *timer = id.opaque;
293 struct timespec old_due;
294 int result=0;
296 if (heap_element(ctx->timers, timer->index) != timer)
297 EV_ERR(ENOENT);
299 #ifdef __hpux
301 * tv_sec and tv_nsec are unsigned.
303 if (due.tv_nsec >= BILLION)
304 EV_ERR(EINVAL);
306 if (inter.tv_nsec >= BILLION)
307 EV_ERR(EINVAL);
308 #else
309 if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
310 EV_ERR(EINVAL);
312 if (inter.tv_sec < 0 || inter.tv_nsec < 0 || inter.tv_nsec >= BILLION)
313 EV_ERR(EINVAL);
314 #endif
316 old_due = timer->due;
318 timer->func = func;
319 timer->uap = uap;
320 timer->due = due;
321 timer->inter = inter;
323 switch (evCmpTime(due, old_due)) {
324 case -1:
325 result = heap_increased(ctx->timers, timer->index);
326 break;
327 case 0:
328 result = 0;
329 break;
330 case 1:
331 result = heap_decreased(ctx->timers, timer->index);
332 break;
335 if (ctx->debug > 7) {
336 evPrintf(ctx, 7, "timers after evResetTimer:\n");
337 (void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
340 return (result);
344 evSetIdleTimer(evContext opaqueCtx,
345 evTimerFunc func,
346 void *uap,
347 struct timespec max_idle,
348 evTimerID *opaqueID
350 evContext_p *ctx = opaqueCtx.opaque;
351 idle_timer *tt;
353 /* Allocate and fill. */
354 OKNEW(tt);
355 tt->func = func;
356 tt->uap = uap;
357 tt->lastTouched = ctx->lastEventTime;
358 tt->max_idle = max_idle;
360 if (evSetTimer(opaqueCtx, idle_timeout, tt,
361 evAddTime(ctx->lastEventTime, max_idle),
362 max_idle, opaqueID) < 0) {
363 FREE(tt);
364 return (-1);
367 tt->timer = opaqueID->opaque;
369 return (0);
373 evClearIdleTimer(evContext opaqueCtx, evTimerID id) {
374 evTimer *del = id.opaque;
375 idle_timer *tt = del->uap;
377 FREE(tt);
378 return (evClearTimer(opaqueCtx, id));
382 evResetIdleTimer(evContext opaqueCtx,
383 evTimerID opaqueID,
384 evTimerFunc func,
385 void *uap,
386 struct timespec max_idle
388 evContext_p *ctx = opaqueCtx.opaque;
389 evTimer *timer = opaqueID.opaque;
390 idle_timer *tt = timer->uap;
392 tt->func = func;
393 tt->uap = uap;
394 tt->lastTouched = ctx->lastEventTime;
395 tt->max_idle = max_idle;
397 return (evResetTimer(opaqueCtx, opaqueID, idle_timeout, tt,
398 evAddTime(ctx->lastEventTime, max_idle),
399 max_idle));
403 evTouchIdleTimer(evContext opaqueCtx, evTimerID id) {
404 evContext_p *ctx = opaqueCtx.opaque;
405 evTimer *t = id.opaque;
406 idle_timer *tt = t->uap;
408 tt->lastTouched = ctx->lastEventTime;
410 return (0);
413 /* Public to the rest of eventlib. */
415 heap_context
416 evCreateTimers(const evContext_p *ctx) {
418 UNUSED(ctx);
420 return (heap_new(due_sooner, set_index, 2048));
423 void
424 evDestroyTimers(const evContext_p *ctx) {
425 (void) heap_for_each(ctx->timers, free_timer, NULL);
426 (void) heap_free(ctx->timers);
429 /* Private. */
431 static int
432 due_sooner(void *a, void *b) {
433 evTimer *a_timer, *b_timer;
435 a_timer = a;
436 b_timer = b;
437 return (evCmpTime(a_timer->due, b_timer->due) < 0);
440 static void
441 set_index(void *what, int index) {
442 evTimer *timer;
444 timer = what;
445 timer->index = index;
448 static void
449 free_timer(void *what, void *uap) {
450 evTimer *t = what;
452 UNUSED(uap);
454 FREE(t);
457 static void
458 print_timer(void *what, void *uap) {
459 evTimer *cur = what;
460 evContext_p *ctx = uap;
462 cur = what;
463 evPrintf(ctx, 7,
464 " func %p, uap %p, due %ld.%09ld, inter %ld.%09ld\n",
465 cur->func, cur->uap,
466 (long)cur->due.tv_sec, cur->due.tv_nsec,
467 (long)cur->inter.tv_sec, cur->inter.tv_nsec);
470 static void
471 idle_timeout(evContext opaqueCtx,
472 void *uap,
473 struct timespec due,
474 struct timespec inter
476 evContext_p *ctx = opaqueCtx.opaque;
477 idle_timer *this = uap;
478 struct timespec idle;
480 UNUSED(due);
481 UNUSED(inter);
483 idle = evSubTime(ctx->lastEventTime, this->lastTouched);
484 if (evCmpTime(idle, this->max_idle) >= 0) {
485 (this->func)(opaqueCtx, this->uap, this->timer->due,
486 this->max_idle);
488 * Setting the interval to zero will cause the timer to
489 * be cleaned up in evDrop().
491 this->timer->inter = evConsTime(0, 0);
492 FREE(this);
493 } else {
494 /* evDrop() will reschedule the timer. */
495 this->timer->inter = evSubTime(this->max_idle, idle);
499 /*! \file */