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