1 /* $OpenBSD: timer.c,v 1.3 2006/06/02 20:31:48 moritz Exp $ */ 2 3 /* 4 * Copyright (c) 2005 H�kan Olsson. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* 29 * This code was written under funding by Multicom Security AB. 30 */ 31 32 33 #include <sys/types.h> 34 #include <sys/queue.h> 35 #include <sys/time.h> 36 37 #include <stdlib.h> 38 #include <string.h> 39 40 #include "sasyncd.h" 41 42 /* 43 * All events have a name, an expiration time and a function to be called 44 * at this time. The queue is always sorted so the next event to happen is 45 * first in the queue. 46 */ 47 struct event { 48 TAILQ_ENTRY (event) next; 49 struct timeval expire; 50 char *name; 51 void (*fun) (void *); 52 void *arg; 53 }; 54 55 static TAILQ_HEAD (event_head, event) events; 56 57 /* Initialize timer event queue. */ 58 void 59 timer_init(void) 60 { 61 TAILQ_INIT(&events); 62 } 63 64 /* 65 * Return the number of seconds until the next event happens. Used for 66 * the select() call in the main loop. 67 */ 68 void 69 timer_next_event(struct timeval *tv) 70 { 71 struct timeval now; 72 struct event *e = TAILQ_FIRST(&events); 73 74 if (e) { 75 gettimeofday(&now, 0); 76 if (timercmp(&now, &e->expire, >=)) 77 timerclear(tv); 78 else 79 timersub(&e->expire, &now, tv); 80 } else { 81 tv->tv_sec = 60; /* "Best guess". */ 82 tv->tv_usec = 0; 83 } 84 } 85 86 /* 87 * Whenever select() times out, we have an event that should happen and this 88 * routine gets called. Handle and remove all pending events. 89 */ 90 void 91 timer_run(void) 92 { 93 struct timeval now; 94 struct event *e; 95 96 gettimeofday(&now, 0); 97 for (e = TAILQ_FIRST(&events); e && timercmp(&now, &e->expire, >=); 98 e = TAILQ_FIRST(&events)) { 99 TAILQ_REMOVE(&events, e, next); 100 log_msg(6, "timer_run: event \"%s\"", 101 e->name ? e->name : "<unknown>"); 102 (*e->fun)(e->arg); 103 if (e->name) 104 free(e->name); 105 free(e); 106 } 107 } 108 109 /* Add a new event. */ 110 int 111 timer_add(char *name, u_int32_t when, void (*function)(void *), void *arg) 112 { 113 struct timeval now, tmp; 114 struct event *e, *new; 115 116 new = (struct event *)calloc(1, sizeof *new); 117 if (!new) { 118 log_err("timer_add: calloc (1, %u) failed", sizeof *new); 119 return -1; 120 } 121 122 new->name = strdup(name); /* We handle failures here. */ 123 new->fun = function; 124 new->arg = arg; 125 126 memset(&tmp, 0, sizeof tmp); 127 tmp.tv_sec = when; 128 gettimeofday(&now, 0); 129 timeradd(&now, &tmp, &new->expire); 130 131 log_msg(6, "timer_add: new event \"%s\" (expiring in %us)", 132 name ? name : "<unknown>", when); 133 134 /* Insert the new event in the queue so it's always sorted. */ 135 for (e = TAILQ_FIRST(&events); e; e = TAILQ_NEXT(e, next)) { 136 if (timercmp(&new->expire, &e->expire, >=)) 137 continue; 138 TAILQ_INSERT_BEFORE(e, new, next); 139 return 0; 140 } 141 TAILQ_INSERT_TAIL(&events, new, next); 142 return 0; 143 } 144