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