xref: /netbsd-src/usr.sbin/rtadvd/timer.c (revision eacf04d89a6611c02f6c2f70a76cd0ce69d75d44)
1*eacf04d8Schristos /*	$NetBSD: timer.c,v 1.20 2021/03/23 18:16:53 christos Exp $	*/
28c2379fdSrpaulo /*	$KAME: timer.c,v 1.11 2005/04/14 06:22:35 suz Exp $	*/
36a12600aSitojun 
4134b5f49Sitojun /*
5134b5f49Sitojun  * Copyright (C) 1998 WIDE Project.
6134b5f49Sitojun  * All rights reserved.
7134b5f49Sitojun  *
8134b5f49Sitojun  * Redistribution and use in source and binary forms, with or without
9134b5f49Sitojun  * modification, are permitted provided that the following conditions
10134b5f49Sitojun  * are met:
11134b5f49Sitojun  * 1. Redistributions of source code must retain the above copyright
12134b5f49Sitojun  *    notice, this list of conditions and the following disclaimer.
13134b5f49Sitojun  * 2. Redistributions in binary form must reproduce the above copyright
14134b5f49Sitojun  *    notice, this list of conditions and the following disclaimer in the
15134b5f49Sitojun  *    documentation and/or other materials provided with the distribution.
16134b5f49Sitojun  * 3. Neither the name of the project nor the names of its contributors
17134b5f49Sitojun  *    may be used to endorse or promote products derived from this software
18134b5f49Sitojun  *    without specific prior written permission.
19134b5f49Sitojun  *
20134b5f49Sitojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21134b5f49Sitojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22134b5f49Sitojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23134b5f49Sitojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24134b5f49Sitojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25134b5f49Sitojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26134b5f49Sitojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27134b5f49Sitojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28134b5f49Sitojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29134b5f49Sitojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30134b5f49Sitojun  * SUCH DAMAGE.
31134b5f49Sitojun  */
32134b5f49Sitojun 
334d6bb526Sroy #include <sys/queue.h>
34134b5f49Sitojun #include <sys/time.h>
35134b5f49Sitojun 
365f8de401Sroy #include <assert.h>
3776ec1114Sroy #include <limits.h>
38134b5f49Sitojun #include <unistd.h>
39134b5f49Sitojun #include <syslog.h>
403bff93a2Sroy #include <stdbool.h>
41134b5f49Sitojun #include <stdlib.h>
42134b5f49Sitojun #include <string.h>
43134b5f49Sitojun #include <search.h>
44134b5f49Sitojun #include "timer.h"
454c18e5f4Schristos #include "logit.h"
460bc5ae37Sozaki-r #include "prog_ops.h"
47134b5f49Sitojun 
484d6bb526Sroy struct rtadvd_timer_head_t ra_timer = TAILQ_HEAD_INITIALIZER(ra_timer);
4976ec1114Sroy static struct timespec tm_limit = { LONG_MAX, 1000000000L - 1 };
5076ec1114Sroy static struct timespec tm_max;
51134b5f49Sitojun 
52134b5f49Sitojun void
rtadvd_timer_init(void)534d6bb526Sroy rtadvd_timer_init(void)
54134b5f49Sitojun {
55134b5f49Sitojun 
564d6bb526Sroy 	TAILQ_INIT(&ra_timer);
574d6bb526Sroy 	tm_max = tm_limit;
58134b5f49Sitojun }
59134b5f49Sitojun 
60134b5f49Sitojun struct rtadvd_timer *
rtadvd_add_timer(struct rtadvd_timer * (* timeout)(void *),void (* update)(void *,struct timespec *),void * timeodata,void * updatedata)614d6bb526Sroy rtadvd_add_timer(struct rtadvd_timer *(*timeout) (void *),
6276ec1114Sroy     void (*update) (void *, struct timespec *),
63134b5f49Sitojun     void *timeodata, void *updatedata)
64134b5f49Sitojun {
65134b5f49Sitojun 	struct rtadvd_timer *newtimer;
66134b5f49Sitojun 
675f8de401Sroy 	assert(timeout != NULL);
685f8de401Sroy 
69134b5f49Sitojun 	if ((newtimer = malloc(sizeof(*newtimer))) == NULL) {
705f8de401Sroy 		logit(LOG_ERR, "%s: malloc: %m", __func__);
7127e3ea92Sroy 		exit(EXIT_FAILURE);
72134b5f49Sitojun 	}
73134b5f49Sitojun 
74134b5f49Sitojun 	memset(newtimer, 0, sizeof(*newtimer));
75134b5f49Sitojun 
76134b5f49Sitojun 	newtimer->expire = timeout;
77134b5f49Sitojun 	newtimer->update = update;
78134b5f49Sitojun 	newtimer->expire_data = timeodata;
79134b5f49Sitojun 	newtimer->update_data = updatedata;
80134b5f49Sitojun 	newtimer->tm = tm_max;
81134b5f49Sitojun 
82134b5f49Sitojun 	/* link into chain */
834d6bb526Sroy 	TAILQ_INSERT_TAIL(&ra_timer, newtimer, next);
84134b5f49Sitojun 
85134b5f49Sitojun 	return(newtimer);
86134b5f49Sitojun }
87134b5f49Sitojun 
88134b5f49Sitojun void
rtadvd_remove_timer(struct rtadvd_timer ** timer)8919a4d091Sitojun rtadvd_remove_timer(struct rtadvd_timer **timer)
9019a4d091Sitojun {
914d6bb526Sroy 
9283ab5659Sroy 	if (*timer) {
934d6bb526Sroy 		TAILQ_REMOVE(&ra_timer, *timer, next);
9419a4d091Sitojun 		free(*timer);
9519a4d091Sitojun 		*timer = NULL;
9619a4d091Sitojun 	}
9783ab5659Sroy }
9819a4d091Sitojun 
9919a4d091Sitojun void
rtadvd_set_timer(struct timespec * tm,struct rtadvd_timer * timer)10076ec1114Sroy rtadvd_set_timer(struct timespec *tm, struct rtadvd_timer *timer)
101134b5f49Sitojun {
10276ec1114Sroy 	struct timespec now;
103134b5f49Sitojun 
104134b5f49Sitojun 	/* reset the timer */
1050bc5ae37Sozaki-r 	prog_clock_gettime(CLOCK_MONOTONIC, &now);
10676ec1114Sroy 	timespecadd(&now, tm, &timer->tm);
107134b5f49Sitojun 
10876bb64e3Smsaitoh 	/* update the next expiration time */
10976ec1114Sroy 	if (timespeccmp(&timer->tm, &tm_max, <))
1104d6bb526Sroy 		tm_max = timer->tm;
1113bff93a2Sroy 
1123bff93a2Sroy 	/* enable the timer */
1133bff93a2Sroy 	timer->enabled = true;
114134b5f49Sitojun }
115134b5f49Sitojun 
116134b5f49Sitojun /*
1178c2379fdSrpaulo  * Check expiration for each timer. If a timer expires,
118134b5f49Sitojun  * call the expire function for the timer and update the timer.
119134b5f49Sitojun  * Return the next interval for select() call.
120134b5f49Sitojun  */
12176ec1114Sroy struct timespec *
rtadvd_check_timer(void)1224d6bb526Sroy rtadvd_check_timer(void)
123134b5f49Sitojun {
12476ec1114Sroy 	static struct timespec returnval;
12576ec1114Sroy 	struct timespec now;
12683ab5659Sroy 	struct rtadvd_timer *tm, *tmn;
127134b5f49Sitojun 
1280bc5ae37Sozaki-r 	prog_clock_gettime(CLOCK_MONOTONIC, &now);
1294d6bb526Sroy 	tm_max = tm_limit;
130134b5f49Sitojun 
13183ab5659Sroy 	TAILQ_FOREACH_SAFE(tm, &ra_timer, next, tmn) {
1323bff93a2Sroy 		if (!tm->enabled)
1333bff93a2Sroy 			continue;
13476ec1114Sroy 		if (timespeccmp(&tm->tm, &now, <=)) {
1358c2379fdSrpaulo 			if ((*tm->expire)(tm->expire_data) == NULL)
1368c2379fdSrpaulo 				continue; /* the timer was removed */
1378c2379fdSrpaulo 			if (tm->update)
138134b5f49Sitojun 				(*tm->update)(tm->update_data, &tm->tm);
13976ec1114Sroy 			timespecadd(&tm->tm, &now, &tm->tm);
140134b5f49Sitojun 		}
14176ec1114Sroy 		if (timespeccmp(&tm->tm, &tm_max, <))
1424d6bb526Sroy 			tm_max = tm->tm;
143134b5f49Sitojun 	}
144134b5f49Sitojun 
14576ec1114Sroy 	if (timespeccmp(&tm_max, &tm_limit, ==))
14619a4d091Sitojun 		return(NULL);
14776ec1114Sroy 	if (timespeccmp(&tm_max, &now, <)) {
148134b5f49Sitojun 		/* this may occur when the interval is too small */
14976ec1114Sroy 		timespecclear(&returnval);
15033413b28Sitojun 	} else
15176ec1114Sroy 		timespecsub(&tm_max, &now, &returnval);
152134b5f49Sitojun 	return(&returnval);
153134b5f49Sitojun }
154134b5f49Sitojun 
15576ec1114Sroy struct timespec *
rtadvd_timer_rest(struct rtadvd_timer * timer)156134b5f49Sitojun rtadvd_timer_rest(struct rtadvd_timer *timer)
157134b5f49Sitojun {
15876ec1114Sroy 	static struct timespec returnval;
15976ec1114Sroy 	struct timespec now;
160134b5f49Sitojun 
1610bc5ae37Sozaki-r 	prog_clock_gettime(CLOCK_MONOTONIC, &now);
16276ec1114Sroy 	if (timespeccmp(&timer->tm, &now, <=)) {
1633bff93a2Sroy 		if (timer->enabled)
1644c18e5f4Schristos 			logit(LOG_DEBUG,
165*eacf04d8Schristos 			       "%s: a timer must be expired, but not yet",
1665e4b1fc3Sitojun 			       __func__);
16776ec1114Sroy 		returnval.tv_sec = 0;
16876ec1114Sroy 		returnval.tv_nsec = 0;
169134b5f49Sitojun 	}
170134b5f49Sitojun 	else
17176ec1114Sroy 		timespecsub(&timer->tm, &now, &returnval);
172134b5f49Sitojun 
173134b5f49Sitojun 	return(&returnval);
174134b5f49Sitojun }
175