Home | History | Annotate | Line # | Download | only in rtadvd
timer.c revision 1.18.2.1
      1  1.18.2.1    martin /*	$NetBSD: timer.c,v 1.18.2.1 2020/04/08 14:09:22 martin Exp $	*/
      2       1.9    rpaulo /*	$KAME: timer.c,v 1.11 2005/04/14 06:22:35 suz Exp $	*/
      3       1.2    itojun 
      4       1.1    itojun /*
      5       1.1    itojun  * Copyright (C) 1998 WIDE Project.
      6       1.1    itojun  * All rights reserved.
      7      1.15       roy  *
      8       1.1    itojun  * Redistribution and use in source and binary forms, with or without
      9       1.1    itojun  * modification, are permitted provided that the following conditions
     10       1.1    itojun  * are met:
     11       1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     12       1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     13       1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     15       1.1    itojun  *    documentation and/or other materials provided with the distribution.
     16       1.1    itojun  * 3. Neither the name of the project nor the names of its contributors
     17       1.1    itojun  *    may be used to endorse or promote products derived from this software
     18       1.1    itojun  *    without specific prior written permission.
     19      1.15       roy  *
     20       1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21       1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22       1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23       1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24       1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25       1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26       1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27       1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28       1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29       1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30       1.1    itojun  * SUCH DAMAGE.
     31       1.1    itojun  */
     32       1.1    itojun 
     33      1.10       roy #include <sys/queue.h>
     34       1.1    itojun #include <sys/time.h>
     35       1.1    itojun 
     36      1.18       roy #include <assert.h>
     37      1.12       roy #include <limits.h>
     38       1.1    itojun #include <unistd.h>
     39       1.1    itojun #include <syslog.h>
     40      1.16       roy #include <stdbool.h>
     41       1.1    itojun #include <stdlib.h>
     42       1.1    itojun #include <string.h>
     43       1.1    itojun #include <search.h>
     44       1.1    itojun #include "timer.h"
     45      1.14  christos #include "logit.h"
     46      1.13     ozaki #include "prog_ops.h"
     47       1.1    itojun 
     48      1.10       roy struct rtadvd_timer_head_t ra_timer = TAILQ_HEAD_INITIALIZER(ra_timer);
     49      1.12       roy static struct timespec tm_limit = { LONG_MAX, 1000000000L - 1 };
     50      1.12       roy static struct timespec tm_max;
     51       1.1    itojun 
     52       1.1    itojun void
     53      1.10       roy rtadvd_timer_init(void)
     54       1.1    itojun {
     55       1.1    itojun 
     56      1.10       roy 	TAILQ_INIT(&ra_timer);
     57      1.10       roy 	tm_max = tm_limit;
     58       1.1    itojun }
     59       1.1    itojun 
     60       1.1    itojun struct rtadvd_timer *
     61      1.10       roy rtadvd_add_timer(struct rtadvd_timer *(*timeout) (void *),
     62      1.12       roy     void (*update) (void *, struct timespec *),
     63       1.9    rpaulo     void *timeodata, void *updatedata)
     64       1.1    itojun {
     65       1.1    itojun 	struct rtadvd_timer *newtimer;
     66       1.1    itojun 
     67      1.18       roy 	assert(timeout != NULL);
     68      1.18       roy 
     69       1.1    itojun 	if ((newtimer = malloc(sizeof(*newtimer))) == NULL) {
     70      1.18       roy 		logit(LOG_ERR, "%s: malloc: %m", __func__);
     71      1.17       roy 		exit(EXIT_FAILURE);
     72       1.1    itojun 	}
     73       1.1    itojun 
     74       1.1    itojun 	memset(newtimer, 0, sizeof(*newtimer));
     75       1.1    itojun 
     76       1.1    itojun 	newtimer->expire = timeout;
     77       1.1    itojun 	newtimer->update = update;
     78       1.1    itojun 	newtimer->expire_data = timeodata;
     79       1.1    itojun 	newtimer->update_data = updatedata;
     80       1.1    itojun 	newtimer->tm = tm_max;
     81       1.1    itojun 
     82       1.1    itojun 	/* link into chain */
     83      1.10       roy 	TAILQ_INSERT_TAIL(&ra_timer, newtimer, next);
     84       1.1    itojun 
     85       1.1    itojun 	return(newtimer);
     86       1.1    itojun }
     87       1.1    itojun 
     88       1.1    itojun void
     89       1.4    itojun rtadvd_remove_timer(struct rtadvd_timer **timer)
     90       1.4    itojun {
     91      1.10       roy 
     92      1.11       roy 	if (*timer) {
     93      1.11       roy 		TAILQ_REMOVE(&ra_timer, *timer, next);
     94      1.11       roy 		free(*timer);
     95      1.11       roy 		*timer = NULL;
     96      1.11       roy 	}
     97       1.4    itojun }
     98       1.4    itojun 
     99       1.4    itojun void
    100      1.12       roy rtadvd_set_timer(struct timespec *tm, struct rtadvd_timer *timer)
    101       1.1    itojun {
    102      1.12       roy 	struct timespec now;
    103       1.1    itojun 
    104       1.1    itojun 	/* reset the timer */
    105      1.13     ozaki 	prog_clock_gettime(CLOCK_MONOTONIC, &now);
    106      1.12       roy 	timespecadd(&now, tm, &timer->tm);
    107       1.1    itojun 
    108  1.18.2.1    martin 	/* update the next expiration time */
    109      1.12       roy 	if (timespeccmp(&timer->tm, &tm_max, <))
    110      1.10       roy 		tm_max = timer->tm;
    111      1.16       roy 
    112      1.16       roy 	/* enable the timer */
    113      1.16       roy 	timer->enabled = true;
    114       1.1    itojun }
    115       1.1    itojun 
    116       1.1    itojun /*
    117       1.9    rpaulo  * Check expiration for each timer. If a timer expires,
    118       1.1    itojun  * call the expire function for the timer and update the timer.
    119       1.1    itojun  * Return the next interval for select() call.
    120       1.1    itojun  */
    121      1.12       roy struct timespec *
    122      1.10       roy rtadvd_check_timer(void)
    123       1.1    itojun {
    124      1.12       roy 	static struct timespec returnval;
    125      1.12       roy 	struct timespec now;
    126      1.11       roy 	struct rtadvd_timer *tm, *tmn;
    127       1.1    itojun 
    128      1.13     ozaki 	prog_clock_gettime(CLOCK_MONOTONIC, &now);
    129      1.10       roy 	tm_max = tm_limit;
    130       1.1    itojun 
    131      1.11       roy 	TAILQ_FOREACH_SAFE(tm, &ra_timer, next, tmn) {
    132      1.16       roy 		if (!tm->enabled)
    133      1.16       roy 			continue;
    134      1.12       roy 		if (timespeccmp(&tm->tm, &now, <=)) {
    135       1.9    rpaulo 			if ((*tm->expire)(tm->expire_data) == NULL)
    136       1.9    rpaulo 				continue; /* the timer was removed */
    137       1.9    rpaulo 			if (tm->update)
    138       1.9    rpaulo 				(*tm->update)(tm->update_data, &tm->tm);
    139      1.12       roy 			timespecadd(&tm->tm, &now, &tm->tm);
    140       1.1    itojun 		}
    141      1.12       roy 		if (timespeccmp(&tm->tm, &tm_max, <))
    142      1.10       roy 			tm_max = tm->tm;
    143       1.1    itojun 	}
    144       1.1    itojun 
    145      1.12       roy 	if (timespeccmp(&tm_max, &tm_limit, ==))
    146       1.4    itojun 		return(NULL);
    147      1.12       roy 	if (timespeccmp(&tm_max, &now, <)) {
    148       1.1    itojun 		/* this may occur when the interval is too small */
    149      1.12       roy 		timespecclear(&returnval);
    150       1.5    itojun 	} else
    151      1.12       roy 		timespecsub(&tm_max, &now, &returnval);
    152       1.1    itojun 	return(&returnval);
    153       1.1    itojun }
    154       1.1    itojun 
    155      1.12       roy struct timespec *
    156       1.1    itojun rtadvd_timer_rest(struct rtadvd_timer *timer)
    157       1.1    itojun {
    158      1.12       roy 	static struct timespec returnval;
    159      1.12       roy 	struct timespec now;
    160       1.1    itojun 
    161      1.13     ozaki 	prog_clock_gettime(CLOCK_MONOTONIC, &now);
    162      1.12       roy 	if (timespeccmp(&timer->tm, &now, <=)) {
    163      1.16       roy 		if (timer->enabled)
    164      1.16       roy 			logit(LOG_DEBUG,
    165      1.16       roy 			       "<%s> a timer must be expired, but not yet",
    166      1.16       roy 			       __func__);
    167      1.12       roy 		returnval.tv_sec = 0;
    168      1.12       roy 		returnval.tv_nsec = 0;
    169       1.1    itojun 	}
    170       1.1    itojun 	else
    171      1.12       roy 		timespecsub(&timer->tm, &now, &returnval);
    172       1.1    itojun 
    173       1.1    itojun 	return(&returnval);
    174       1.1    itojun }
    175