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