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