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