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 <stdio.h> 34 #include <string.h> 35 #include <sys/time.h> 36 #include <termios.h> 37 38 #include "log.h" 39 #include "sig.h" 40 #include "timer.h" 41 #include "descriptor.h" 42 #include "prompt.h" 43 44 45 #define RESTVAL(t) \ 46 ((t).it_value.tv_sec * SECTICKS + (t).it_value.tv_usec / TICKUNIT + \ 47 ((((t).it_value.tv_usec % TICKUNIT) >= (TICKUNIT >> 1)) ? 1 : 0)) 48 49 static struct pppTimer *TimerList = NULL, *ExpiredList = NULL; 50 51 static void StopTimerNoBlock(struct pppTimer *); 52 53 static const char * 54 tState2Nam(u_int state) 55 { 56 static const char * const StateNames[] = { "stopped", "running", "expired" }; 57 58 if (state >= sizeof StateNames / sizeof StateNames[0]) 59 return "unknown"; 60 return StateNames[state]; 61 } 62 63 void 64 timer_Stop(struct pppTimer *tp) 65 { 66 sigset_t mask, omask; 67 68 sigemptyset(&mask); 69 sigaddset(&mask, SIGALRM); 70 sigprocmask(SIG_BLOCK, &mask, &omask); 71 StopTimerNoBlock(tp); 72 sigprocmask(SIG_SETMASK, &omask, NULL); 73 } 74 75 void 76 timer_Start(struct pppTimer *tp) 77 { 78 struct itimerval itimer; 79 struct pppTimer *t, *pt; 80 u_long ticks = 0; 81 sigset_t mask, omask; 82 83 sigemptyset(&mask); 84 sigaddset(&mask, SIGALRM); 85 sigprocmask(SIG_BLOCK, &mask, &omask); 86 87 if (tp->state != TIMER_STOPPED) 88 StopTimerNoBlock(tp); 89 90 if (tp->load == 0) { 91 log_Printf(LogTIMER, "%s timer[%p] has 0 load!\n", tp->name, tp); 92 sigprocmask(SIG_SETMASK, &omask, NULL); 93 return; 94 } 95 96 /* Adjust our first delta so that it reflects what's really happening */ 97 if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0) 98 TimerList->rest = RESTVAL(itimer); 99 100 pt = NULL; 101 for (t = TimerList; t; t = t->next) { 102 if (ticks + t->rest >= tp->load) 103 break; 104 ticks += t->rest; 105 pt = t; 106 } 107 108 tp->state = TIMER_RUNNING; 109 tp->rest = tp->load - ticks; 110 111 if (t) 112 log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p] before %s " 113 "timer[%p], delta = %ld\n", tp->name, tp, t->name, t, tp->rest); 114 else 115 log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p]\n", tp->name, tp); 116 117 /* Insert given *tp just before *t */ 118 tp->next = t; 119 if (pt) { 120 pt->next = tp; 121 } else { 122 TimerList = tp; 123 timer_InitService(t != NULL); /* [re]Start the Timer Service */ 124 } 125 if (t) 126 t->rest -= tp->rest; 127 128 sigprocmask(SIG_SETMASK, &omask, NULL); 129 } 130 131 static void 132 StopTimerNoBlock(struct pppTimer *tp) 133 { 134 struct pppTimer *t, *pt; 135 136 /* 137 * A RUNNING timer must be removed from TimerList (->next list). 138 * A STOPPED timer isn't in any list, but may have a bogus [e]next field. 139 * An EXPIRED timer is in the ->enext list. 140 */ 141 142 if (tp->state == TIMER_STOPPED) 143 return; 144 145 pt = NULL; 146 for (t = TimerList; t != tp && t != NULL; t = t->next) 147 pt = t; 148 149 if (t) { 150 if (pt) 151 pt->next = t->next; 152 else { 153 TimerList = t->next; 154 if (TimerList == NULL) /* Last one ? */ 155 timer_TermService(); /* Terminate Timer Service */ 156 } 157 if (t->next) { 158 if (!pt) { /* t (tp) was the first in the list */ 159 struct itimerval itimer; 160 161 if (getitimer(ITIMER_REAL, &itimer) == 0) 162 t->rest = RESTVAL(itimer); 163 } 164 t->next->rest += t->rest; 165 if (!pt) /* t->next is now the first in the list */ 166 timer_InitService(1); 167 } 168 } else { 169 /* Search for any pending expired timers */ 170 pt = NULL; 171 for (t = ExpiredList; t != tp && t != NULL; t = t->enext) 172 pt = t; 173 174 if (t) { 175 if (pt) 176 pt->enext = t->enext; 177 else 178 ExpiredList = t->enext; 179 } else if (tp->state == TIMER_RUNNING) 180 log_Printf(LogERROR, "Oops, %s timer not found!!\n", tp->name); 181 } 182 183 tp->next = tp->enext = NULL; 184 tp->state = TIMER_STOPPED; 185 } 186 187 static void 188 TimerService(void) 189 { 190 struct pppTimer *tp, *exp, *next; 191 192 if (log_IsKept(LogTIMER)) { 193 static time_t t; /* Only show timers globally every second */ 194 time_t n = time(NULL); 195 196 if (n > t) 197 timer_Show(LogTIMER, NULL); 198 t = n; 199 } 200 201 tp = TimerList; 202 if (tp) { 203 tp->rest = 0; 204 205 /* Multiple timers might expire at once. Create a list of expired timers */ 206 exp = NULL; 207 do { 208 tp->state = TIMER_EXPIRED; 209 next = tp->next; 210 tp->enext = exp; 211 exp = tp; 212 tp = next; 213 } while (tp && tp->rest == 0); 214 215 TimerList = tp; 216 if (TimerList != NULL) /* Any timers remaining ? */ 217 timer_InitService(1); /* Restart the Timer Service */ 218 else 219 timer_TermService(); /* Stop the Timer Service */ 220 221 /* Process all expired timers */ 222 while (exp) { 223 ExpiredList = exp->enext; 224 exp->enext = NULL; 225 if (exp->func) 226 (*exp->func)(exp->arg); 227 exp = ExpiredList; 228 } 229 } 230 } 231 232 void 233 timer_Show(int LogLevel, struct prompt *prompt) 234 { 235 struct itimerval itimer; 236 struct pppTimer *pt; 237 u_long rest = 0; 238 239 /* Adjust our first delta so that it reflects what's really happening */ 240 if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0) 241 TimerList->rest = RESTVAL(itimer); 242 243 #define SECS(val) ((val) / SECTICKS) 244 #define HSECS(val) (((val) % SECTICKS) * 100 / SECTICKS) 245 #define DISP \ 246 "%s timer[%p]: freq = %ld.%02lds, next = %lu.%02lus, state = %s\n", \ 247 pt->name, pt, SECS(pt->load), HSECS(pt->load), SECS(rest), \ 248 HSECS(rest), tState2Nam(pt->state) 249 250 if (!prompt) 251 log_Printf(LogLevel, "---- Begin of Timer Service List---\n"); 252 253 for (pt = TimerList; pt; pt = pt->next) { 254 rest += pt->rest; 255 if (prompt) 256 prompt_Printf(prompt, DISP); 257 else 258 log_Printf(LogLevel, DISP); 259 } 260 261 if (!prompt) 262 log_Printf(LogLevel, "---- End of Timer Service List ---\n"); 263 } 264 265 void 266 timer_InitService(int restart) 267 { 268 struct itimerval itimer; 269 270 if (TimerList) { 271 if (!restart) 272 sig_signal(SIGALRM, (void (*)(int))TimerService); 273 itimer.it_interval.tv_sec = 0; 274 itimer.it_interval.tv_usec = 0; 275 itimer.it_value.tv_sec = TimerList->rest / SECTICKS; 276 itimer.it_value.tv_usec = (TimerList->rest % SECTICKS) * TICKUNIT; 277 if (setitimer(ITIMER_REAL, &itimer, NULL) == -1) 278 log_Printf(LogERROR, "Unable to set itimer (%s)\n", strerror(errno)); 279 } 280 } 281 282 void 283 timer_TermService(void) 284 { 285 struct itimerval itimer; 286 287 itimer.it_interval.tv_usec = itimer.it_interval.tv_sec = 0; 288 itimer.it_value.tv_usec = itimer.it_value.tv_sec = 0; 289 if (setitimer(ITIMER_REAL, &itimer, NULL) == -1) 290 log_Printf(LogERROR, "Unable to set itimer (%s)\n", strerror(errno)); 291 sig_signal(SIGALRM, SIG_IGN); 292 } 293