1 /*- 2 * Copyright (c) 1996 - 2001 Brian Somers <[email protected]> 3 * based on work by Toshiharu OHNO <[email protected]> 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 <termios.h> 36 37 #include "log.h" 38 #include "sig.h" 39 #include "timer.h" 40 #include "descriptor.h" 41 #include "prompt.h" 42 43 44 #define RESTVAL(t) \ 45 ((t).it_value.tv_sec * SECTICKS + (t).it_value.tv_usec / TICKUNIT + \ 46 ((((t).it_value.tv_usec % TICKUNIT) >= (TICKUNIT >> 1)) ? 1 : 0)) 47 48 static struct pppTimer *TimerList = NULL, *ExpiredList = NULL; 49 50 static void StopTimerNoBlock(struct pppTimer *); 51 52 static const char * 53 tState2Nam(u_int state) 54 { 55 static const char * const StateNames[] = { "stopped", "running", "expired" }; 56 57 if (state >= sizeof StateNames / sizeof StateNames[0]) 58 return "unknown"; 59 return StateNames[state]; 60 } 61 62 void 63 timer_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 74 void 75 timer_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 130 static void 131 StopTimerNoBlock(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 186 static void 187 TimerService(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 231 void 232 timer_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 264 void 265 timer_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 281 void 282 timer_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