Home | History | Annotate | Line # | Download | only in rtadvd
timer.c revision 1.17
      1 /*	$NetBSD: timer.c,v 1.17 2018/04/20 15:59:17 roy Exp $	*/
      2 /*	$KAME: timer.c,v 1.11 2005/04/14 06:22:35 suz Exp $	*/
      3 
      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.
     19  *
     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.
     31  */
     32 
     33 #include <sys/queue.h>
     34 #include <sys/time.h>
     35 
     36 #include <limits.h>
     37 #include <unistd.h>
     38 #include <syslog.h>
     39 #include <stdbool.h>
     40 #include <stdlib.h>
     41 #include <string.h>
     42 #include <search.h>
     43 #include "timer.h"
     44 #include "logit.h"
     45 #include "prog_ops.h"
     46 
     47 struct rtadvd_timer_head_t ra_timer = TAILQ_HEAD_INITIALIZER(ra_timer);
     48 static struct timespec tm_limit = { LONG_MAX, 1000000000L - 1 };
     49 static struct timespec tm_max;
     50 
     51 void
     52 rtadvd_timer_init(void)
     53 {
     54 
     55 	TAILQ_INIT(&ra_timer);
     56 	tm_max = tm_limit;
     57 }
     58 
     59 struct rtadvd_timer *
     60 rtadvd_add_timer(struct rtadvd_timer *(*timeout) (void *),
     61     void (*update) (void *, struct timespec *),
     62     void *timeodata, void *updatedata)
     63 {
     64 	struct rtadvd_timer *newtimer;
     65 
     66 	if ((newtimer = malloc(sizeof(*newtimer))) == NULL) {
     67 		logit(LOG_ERR,
     68 		       "<%s> can't allocate memory", __func__);
     69 		exit(EXIT_FAILURE);
     70 	}
     71 
     72 	memset(newtimer, 0, sizeof(*newtimer));
     73 
     74 	if (timeout == NULL) {
     75 		logit(LOG_ERR,
     76 		       "<%s> timeout function unspecified", __func__);
     77 		exit(EXIT_FAILURE);
     78 	}
     79 	newtimer->expire = timeout;
     80 	newtimer->update = update;
     81 	newtimer->expire_data = timeodata;
     82 	newtimer->update_data = updatedata;
     83 	newtimer->tm = tm_max;
     84 
     85 	/* link into chain */
     86 	TAILQ_INSERT_TAIL(&ra_timer, newtimer, next);
     87 
     88 	return(newtimer);
     89 }
     90 
     91 void
     92 rtadvd_remove_timer(struct rtadvd_timer **timer)
     93 {
     94 
     95 	if (*timer) {
     96 		TAILQ_REMOVE(&ra_timer, *timer, next);
     97 		free(*timer);
     98 		*timer = NULL;
     99 	}
    100 }
    101 
    102 void
    103 rtadvd_set_timer(struct timespec *tm, struct rtadvd_timer *timer)
    104 {
    105 	struct timespec now;
    106 
    107 	/* reset the timer */
    108 	prog_clock_gettime(CLOCK_MONOTONIC, &now);
    109 	timespecadd(&now, tm, &timer->tm);
    110 
    111 	/* upate the next expiration time */
    112 	if (timespeccmp(&timer->tm, &tm_max, <))
    113 		tm_max = timer->tm;
    114 
    115 	/* enable the timer */
    116 	timer->enabled = true;
    117 }
    118 
    119 /*
    120  * Check expiration for each timer. If a timer expires,
    121  * call the expire function for the timer and update the timer.
    122  * Return the next interval for select() call.
    123  */
    124 struct timespec *
    125 rtadvd_check_timer(void)
    126 {
    127 	static struct timespec returnval;
    128 	struct timespec now;
    129 	struct rtadvd_timer *tm, *tmn;
    130 
    131 	prog_clock_gettime(CLOCK_MONOTONIC, &now);
    132 	tm_max = tm_limit;
    133 
    134 	TAILQ_FOREACH_SAFE(tm, &ra_timer, next, tmn) {
    135 		if (!tm->enabled)
    136 			continue;
    137 		if (timespeccmp(&tm->tm, &now, <=)) {
    138 			if ((*tm->expire)(tm->expire_data) == NULL)
    139 				continue; /* the timer was removed */
    140 			if (tm->update)
    141 				(*tm->update)(tm->update_data, &tm->tm);
    142 			timespecadd(&tm->tm, &now, &tm->tm);
    143 		}
    144 		if (timespeccmp(&tm->tm, &tm_max, <))
    145 			tm_max = tm->tm;
    146 	}
    147 
    148 	if (timespeccmp(&tm_max, &tm_limit, ==))
    149 		return(NULL);
    150 	if (timespeccmp(&tm_max, &now, <)) {
    151 		/* this may occur when the interval is too small */
    152 		timespecclear(&returnval);
    153 	} else
    154 		timespecsub(&tm_max, &now, &returnval);
    155 	return(&returnval);
    156 }
    157 
    158 struct timespec *
    159 rtadvd_timer_rest(struct rtadvd_timer *timer)
    160 {
    161 	static struct timespec returnval;
    162 	struct timespec now;
    163 
    164 	prog_clock_gettime(CLOCK_MONOTONIC, &now);
    165 	if (timespeccmp(&timer->tm, &now, <=)) {
    166 		if (timer->enabled)
    167 			logit(LOG_DEBUG,
    168 			       "<%s> a timer must be expired, but not yet",
    169 			       __func__);
    170 		returnval.tv_sec = 0;
    171 		returnval.tv_nsec = 0;
    172 	}
    173 	else
    174 		timespecsub(&timer->tm, &now, &returnval);
    175 
    176 	return(&returnval);
    177 }
    178