Sync usage with man page.
[netbsd-mini2440.git] / usr.sbin / rtadvd / timer.c
blob51d3545714696073ba2218306bf4dd91bd36d177
1 /* $NetBSD: timer.c,v 1.8 2002/07/10 21:11:44 itojun Exp $ */
2 /* $KAME: timer.c,v 1.11 2005/04/14 06:22:35 suz Exp $ */
4 /*
5 * Copyright (C) 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <sys/time.h>
35 #include <unistd.h>
36 #include <syslog.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <search.h>
40 #include "timer.h"
42 static struct rtadvd_timer timer_head;
44 #define MILLION 1000000
45 #define TIMEVAL_EQUAL(t1,t2) ((t1)->tv_sec == (t2)->tv_sec &&\
46 (t1)->tv_usec == (t2)->tv_usec)
48 static struct timeval tm_max = {0x7fffffff, 0x7fffffff};
50 void
51 rtadvd_timer_init()
53 memset(&timer_head, 0, sizeof(timer_head));
55 timer_head.next = timer_head.prev = &timer_head;
56 timer_head.tm = tm_max;
59 struct rtadvd_timer *
60 rtadvd_add_timer(struct rtadvd_timer *(*timeout) __P((void *)),
61 void (*update) __P((void *, struct timeval *)),
62 void *timeodata, void *updatedata)
64 struct rtadvd_timer *newtimer;
66 if ((newtimer = malloc(sizeof(*newtimer))) == NULL) {
67 syslog(LOG_ERR,
68 "<%s> can't allocate memory", __func__);
69 exit(1);
72 memset(newtimer, 0, sizeof(*newtimer));
74 if (timeout == NULL) {
75 syslog(LOG_ERR,
76 "<%s> timeout function unspecified", __func__);
77 exit(1);
79 newtimer->expire = timeout;
80 newtimer->update = update;
81 newtimer->expire_data = timeodata;
82 newtimer->update_data = updatedata;
83 newtimer->tm = tm_max;
85 /* link into chain */
86 insque(newtimer, &timer_head);
88 return(newtimer);
91 void
92 rtadvd_remove_timer(struct rtadvd_timer **timer)
94 remque(*timer);
95 free(*timer);
96 *timer = NULL;
99 void
100 rtadvd_set_timer(struct timeval *tm, struct rtadvd_timer *timer)
102 struct timeval now;
104 /* reset the timer */
105 gettimeofday(&now, NULL);
107 TIMEVAL_ADD(&now, tm, &timer->tm);
109 /* update the next expiration time */
110 if (TIMEVAL_LT(timer->tm, timer_head.tm))
111 timer_head.tm = timer->tm;
113 return;
117 * Check expiration for each timer. If a timer expires,
118 * call the expire function for the timer and update the timer.
119 * Return the next interval for select() call.
121 struct timeval *
122 rtadvd_check_timer()
124 static struct timeval returnval;
125 struct timeval now;
126 struct rtadvd_timer *tm = timer_head.next, *tm_next;
128 gettimeofday(&now, NULL);
130 timer_head.tm = tm_max;
132 for (tm = timer_head.next; tm != &timer_head; tm = tm_next) {
133 tm_next = tm->next;
135 if (TIMEVAL_LEQ(tm->tm, now)) {
136 if ((*tm->expire)(tm->expire_data) == NULL)
137 continue; /* the timer was removed */
138 if (tm->update)
139 (*tm->update)(tm->update_data, &tm->tm);
140 TIMEVAL_ADD(&tm->tm, &now, &tm->tm);
143 if (TIMEVAL_LT(tm->tm, timer_head.tm))
144 timer_head.tm = tm->tm;
147 if (TIMEVAL_EQUAL(&tm_max, &timer_head.tm)) {
148 /* no need to timeout */
149 return(NULL);
150 } else if (TIMEVAL_LT(timer_head.tm, now)) {
151 /* this may occur when the interval is too small */
152 returnval.tv_sec = returnval.tv_usec = 0;
153 } else
154 TIMEVAL_SUB(&timer_head.tm, &now, &returnval);
155 return(&returnval);
158 struct timeval *
159 rtadvd_timer_rest(struct rtadvd_timer *timer)
161 static struct timeval returnval, now;
163 gettimeofday(&now, NULL);
164 if (TIMEVAL_LEQ(timer->tm, now)) {
165 syslog(LOG_DEBUG,
166 "<%s> a timer must be expired, but not yet",
167 __func__);
168 returnval.tv_sec = returnval.tv_usec = 0;
170 else
171 TIMEVAL_SUB(&timer->tm, &now, &returnval);
173 return(&returnval);
176 /* result = a + b */
177 void
178 TIMEVAL_ADD(struct timeval *a, struct timeval *b, struct timeval *result)
180 long l;
182 if ((l = a->tv_usec + b->tv_usec) < MILLION) {
183 result->tv_usec = l;
184 result->tv_sec = a->tv_sec + b->tv_sec;
186 else {
187 result->tv_usec = l - MILLION;
188 result->tv_sec = a->tv_sec + b->tv_sec + 1;
193 * result = a - b
194 * XXX: this function assumes that a >= b.
196 void
197 TIMEVAL_SUB(struct timeval *a, struct timeval *b, struct timeval *result)
199 long l;
201 if ((l = a->tv_usec - b->tv_usec) >= 0) {
202 result->tv_usec = l;
203 result->tv_sec = a->tv_sec - b->tv_sec;
205 else {
206 result->tv_usec = MILLION + l;
207 result->tv_sec = a->tv_sec - b->tv_sec - 1;