timer.c revision 96582
1120441Sbms/*- 2120441Sbms * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org> 3120441Sbms * based on work by Toshiharu OHNO <tony-o@iij.ad.jp> 4120441Sbms * Internet Initiative Japan, Inc (IIJ) 5120441Sbms * All rights reserved. 6120441Sbms * 7120441Sbms * Redistribution and use in source and binary forms, with or without 8120441Sbms * modification, are permitted provided that the following conditions 9120441Sbms * are met: 10120441Sbms * 1. Redistributions of source code must retain the above copyright 11120441Sbms * notice, this list of conditions and the following disclaimer. 12120441Sbms * 2. Redistributions in binary form must reproduce the above copyright 13120441Sbms * notice, this list of conditions and the following disclaimer in the 14120441Sbms * documentation and/or other materials provided with the distribution. 15120441Sbms * 16120441Sbms * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17120441Sbms * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18120441Sbms * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19120441Sbms * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20120441Sbms * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21120441Sbms * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22120441Sbms * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23120441Sbms * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24120441Sbms * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25120441Sbms * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26120441Sbms * SUCH DAMAGE. 27120441Sbms * 28120441Sbms * $FreeBSD: head/usr.sbin/ppp/timer.c 96582 2002-05-14 12:55:39Z brian $ 29120441Sbms */ 30131681Sru 31120441Sbms#include <errno.h> 32120441Sbms#include <signal.h> 33120441Sbms#include <stdio.h> 34120441Sbms#include <string.h> 35120441Sbms#include <termios.h> 36120441Sbms 37120441Sbms#include "log.h" 38120441Sbms#include "sig.h" 39120441Sbms#include "timer.h" 40120441Sbms#include "descriptor.h" 41120441Sbms#include "prompt.h" 42120441Sbms 43120441Sbms 44120441Sbms#define RESTVAL(t) \ 45120441Sbms ((t).it_value.tv_sec * SECTICKS + (t).it_value.tv_usec / TICKUNIT + \ 46120441Sbms ((((t).it_value.tv_usec % TICKUNIT) >= (TICKUNIT >> 1)) ? 1 : 0)) 47120441Sbms 48120441Sbmsstatic struct pppTimer *TimerList = NULL, *ExpiredList = NULL; 49120441Sbms 50120441Sbmsstatic void StopTimerNoBlock(struct pppTimer *); 51120441Sbms 52120441Sbmsstatic const char * 53120441SbmstState2Nam(u_int state) 54120441Sbms{ 55120441Sbms static const char * const StateNames[] = { "stopped", "running", "expired" }; 56120441Sbms 57120441Sbms if (state >= sizeof StateNames / sizeof StateNames[0]) 58267936Sbapt return "unknown"; 59 return StateNames[state]; 60} 61 62void 63timer_Stop(struct pppTimer *tp) 64{ 65 sigset_t mask, omask; 66 67 sigemptyset(&mask); 68 sigaddset(&mask, SIGALRM); 69 sigprocmask(SIG_BLOCK, &mask, &omask); 70 StopTimerNoBlock(tp); 71 sigprocmask(SIG_SETMASK, &omask, NULL); 72} 73 74void 75timer_Start(struct pppTimer *tp) 76{ 77 struct itimerval itimer; 78 struct pppTimer *t, *pt; 79 u_long ticks = 0; 80 sigset_t mask, omask; 81 82 sigemptyset(&mask); 83 sigaddset(&mask, SIGALRM); 84 sigprocmask(SIG_BLOCK, &mask, &omask); 85 86 if (tp->state != TIMER_STOPPED) 87 StopTimerNoBlock(tp); 88 89 if (tp->load == 0) { 90 log_Printf(LogTIMER, "%s timer[%p] has 0 load!\n", tp->name, tp); 91 sigprocmask(SIG_SETMASK, &omask, NULL); 92 return; 93 } 94 95 /* Adjust our first delta so that it reflects what's really happening */ 96 if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0) 97 TimerList->rest = RESTVAL(itimer); 98 99 pt = NULL; 100 for (t = TimerList; t; t = t->next) { 101 if (ticks + t->rest >= tp->load) 102 break; 103 ticks += t->rest; 104 pt = t; 105 } 106 107 tp->state = TIMER_RUNNING; 108 tp->rest = tp->load - ticks; 109 110 if (t) 111 log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p] before %s " 112 "timer[%p], delta = %ld\n", tp->name, tp, t->name, t, tp->rest); 113 else 114 log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p]\n", tp->name, tp); 115 116 /* Insert given *tp just before *t */ 117 tp->next = t; 118 if (pt) { 119 pt->next = tp; 120 } else { 121 TimerList = tp; 122 timer_InitService(t != NULL); /* [re]Start the Timer Service */ 123 } 124 if (t) 125 t->rest -= tp->rest; 126 127 sigprocmask(SIG_SETMASK, &omask, NULL); 128} 129 130static void 131StopTimerNoBlock(struct pppTimer *tp) 132{ 133 struct pppTimer *t, *pt; 134 135 /* 136 * A RUNNING timer must be removed from TimerList (->next list). 137 * A STOPPED timer isn't in any list, but may have a bogus [e]next field. 138 * An EXPIRED timer is in the ->enext list. 139 */ 140 141 if (tp->state == TIMER_STOPPED) 142 return; 143 144 pt = NULL; 145 for (t = TimerList; t != tp && t != NULL; t = t->next) 146 pt = t; 147 148 if (t) { 149 if (pt) 150 pt->next = t->next; 151 else { 152 TimerList = t->next; 153 if (TimerList == NULL) /* Last one ? */ 154 timer_TermService(); /* Terminate Timer Service */ 155 } 156 if (t->next) { 157 if (!pt) { /* t (tp) was the first in the list */ 158 struct itimerval itimer; 159 160 if (getitimer(ITIMER_REAL, &itimer) == 0) 161 t->rest = RESTVAL(itimer); 162 } 163 t->next->rest += t->rest; 164 if (!pt) /* t->next is now the first in the list */ 165 timer_InitService(1); 166 } 167 } else { 168 /* Search for any pending expired timers */ 169 pt = NULL; 170 for (t = ExpiredList; t != tp && t != NULL; t = t->enext) 171 pt = t; 172 173 if (t) { 174 if (pt) 175 pt->enext = t->enext; 176 else 177 ExpiredList = t->enext; 178 } else if (tp->state == TIMER_RUNNING) 179 log_Printf(LogERROR, "Oops, %s timer not found!!\n", tp->name); 180 } 181 182 tp->next = tp->enext = NULL; 183 tp->state = TIMER_STOPPED; 184} 185 186static void 187TimerService(void) 188{ 189 struct pppTimer *tp, *exp, *next; 190 191 if (log_IsKept(LogTIMER)) { 192 static time_t t; /* Only show timers globally every second */ 193 time_t n = time(NULL); 194 195 if (n > t) 196 timer_Show(LogTIMER, NULL); 197 t = n; 198 } 199 200 tp = TimerList; 201 if (tp) { 202 tp->rest = 0; 203 204 /* Multiple timers might expire at once. Create a list of expired timers */ 205 exp = NULL; 206 do { 207 tp->state = TIMER_EXPIRED; 208 next = tp->next; 209 tp->enext = exp; 210 exp = tp; 211 tp = next; 212 } while (tp && tp->rest == 0); 213 214 TimerList = tp; 215 if (TimerList != NULL) /* Any timers remaining ? */ 216 timer_InitService(1); /* Restart the Timer Service */ 217 else 218 timer_TermService(); /* Stop the Timer Service */ 219 220 /* Process all expired timers */ 221 while (exp) { 222 ExpiredList = exp->enext; 223 exp->enext = NULL; 224 if (exp->func) 225 (*exp->func)(exp->arg); 226 exp = ExpiredList; 227 } 228 } 229} 230 231void 232timer_Show(int LogLevel, struct prompt *prompt) 233{ 234 struct itimerval itimer; 235 struct pppTimer *pt; 236 u_long rest = 0; 237 238 /* Adjust our first delta so that it reflects what's really happening */ 239 if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0) 240 TimerList->rest = RESTVAL(itimer); 241 242#define SECS(val) ((val) / SECTICKS) 243#define HSECS(val) (((val) % SECTICKS) * 100 / SECTICKS) 244#define DISP \ 245 "%s timer[%p]: freq = %ld.%02lds, next = %lu.%02lus, state = %s\n", \ 246 pt->name, pt, SECS(pt->load), HSECS(pt->load), SECS(rest), \ 247 HSECS(rest), tState2Nam(pt->state) 248 249 if (!prompt) 250 log_Printf(LogLevel, "---- Begin of Timer Service List---\n"); 251 252 for (pt = TimerList; pt; pt = pt->next) { 253 rest += pt->rest; 254 if (prompt) 255 prompt_Printf(prompt, DISP); 256 else 257 log_Printf(LogLevel, DISP); 258 } 259 260 if (!prompt) 261 log_Printf(LogLevel, "---- End of Timer Service List ---\n"); 262} 263 264void 265timer_InitService(int restart) 266{ 267 struct itimerval itimer; 268 269 if (TimerList) { 270 if (!restart) 271 sig_signal(SIGALRM, (void (*)(int))TimerService); 272 itimer.it_interval.tv_sec = 0; 273 itimer.it_interval.tv_usec = 0; 274 itimer.it_value.tv_sec = TimerList->rest / SECTICKS; 275 itimer.it_value.tv_usec = (TimerList->rest % SECTICKS) * TICKUNIT; 276 if (setitimer(ITIMER_REAL, &itimer, NULL) == -1) 277 log_Printf(LogERROR, "Unable to set itimer (%s)\n", strerror(errno)); 278 } 279} 280 281void 282timer_TermService(void) 283{ 284 struct itimerval itimer; 285 286 itimer.it_interval.tv_usec = itimer.it_interval.tv_sec = 0; 287 itimer.it_value.tv_usec = itimer.it_value.tv_sec = 0; 288 if (setitimer(ITIMER_REAL, &itimer, NULL) == -1) 289 log_Printf(LogERROR, "Unable to set itimer (%s)\n", strerror(errno)); 290 sig_signal(SIGALRM, SIG_IGN); 291} 292