1 /* 2 * PPP Timer Processing Module 3 * 4 * Written by Toshiharu OHNO ([email protected]) 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 * $Id: timer.c,v 1.27.2.9 1998/04/30 23:53:56 brian Exp $ 21 * 22 * TODO: 23 */ 24 25 #include <errno.h> 26 #include <signal.h> 27 #include <stdio.h> 28 #include <sys/time.h> 29 #include <termios.h> 30 #include <unistd.h> 31 32 #include "log.h" 33 #include "sig.h" 34 #include "timer.h" 35 #include "descriptor.h" 36 #include "prompt.h" 37 38 static struct pppTimer *TimerList = NULL; 39 40 static void StopTimerNoBlock(struct pppTimer *); 41 static void InitTimerService(void); 42 43 static const char * 44 tState2Nam(u_int state) 45 { 46 static const char *StateNames[] = { "stopped", "running", "expired" }; 47 48 if (state >= sizeof StateNames / sizeof StateNames[0]) 49 return "unknown"; 50 return StateNames[state]; 51 } 52 53 void 54 timer_Stop(struct pppTimer * tp) 55 { 56 int omask; 57 58 omask = sigblock(sigmask(SIGALRM)); 59 StopTimerNoBlock(tp); 60 sigsetmask(omask); 61 } 62 63 void 64 timer_Start(struct pppTimer * tp) 65 { 66 struct pppTimer *t, *pt; 67 u_long ticks = 0; 68 int omask; 69 70 omask = sigblock(sigmask(SIGALRM)); 71 72 if (tp->state != TIMER_STOPPED) 73 StopTimerNoBlock(tp); 74 75 if (tp->load == 0) { 76 log_Printf(LogTIMER, "%s timer[%p] has 0 load!\n", tp->name, tp); 77 sigsetmask(omask); 78 return; 79 } 80 pt = NULL; 81 for (t = TimerList; t; t = t->next) { 82 if (ticks + t->rest >= tp->load) 83 break; 84 ticks += t->rest; 85 pt = t; 86 } 87 88 tp->state = TIMER_RUNNING; 89 tp->rest = tp->load - ticks; 90 91 if (t) 92 log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p] before %s " 93 "timer[%p], delta = %ld\n", tp->name, tp, t->name, t, tp->rest); 94 else 95 log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p]\n", tp->name, tp); 96 97 /* Insert given *tp just before *t */ 98 tp->next = t; 99 if (pt) { 100 pt->next = tp; 101 } else { 102 InitTimerService(); 103 TimerList = tp; 104 } 105 if (t) 106 t->rest -= tp->rest; 107 108 sigsetmask(omask); 109 } 110 111 static void 112 StopTimerNoBlock(struct pppTimer * tp) 113 { 114 struct pppTimer *t, *pt; 115 116 /* 117 * A RUNNING timer must be removed from TimerList (->next list). 118 * A STOPPED timer isn't in any list, but may have a bogus [e]next field. 119 * An EXPIRED timer is in the ->enext list. 120 */ 121 if (tp->state != TIMER_RUNNING) { 122 tp->next = NULL; 123 tp->state = TIMER_STOPPED; 124 return; 125 } 126 pt = NULL; 127 for (t = TimerList; t != tp && t != NULL; t = t->next) 128 pt = t; 129 if (t) { 130 if (pt) { 131 pt->next = t->next; 132 } else { 133 TimerList = t->next; 134 if (TimerList == NULL) /* Last one ? */ 135 timer_TermService(); /* Terminate Timer Service */ 136 } 137 if (t->next) 138 t->next->rest += tp->rest; 139 } else 140 log_Printf(LogERROR, "Oops, %s timer not found!!\n", tp->name); 141 142 tp->next = NULL; 143 tp->state = TIMER_STOPPED; 144 } 145 146 static void 147 TimerService(void) 148 { 149 struct pppTimer *tp, *exp, *wt; 150 151 if (log_IsKept(LogTIMER)) { 152 static time_t t; 153 time_t n = time(NULL); /* Only show timers every second */ 154 155 if (n > t) 156 timer_Show(LogTIMER, NULL); 157 t = n; 158 } 159 tp = TimerList; 160 if (tp) { 161 tp->rest--; 162 if (tp->rest == 0) { 163 164 /* 165 * Multiple timers may expires at once. Create list of expired timers. 166 */ 167 exp = NULL; 168 do { 169 tp->state = TIMER_EXPIRED; 170 wt = tp->next; 171 tp->enext = exp; 172 exp = tp; 173 tp = wt; 174 } while (tp && (tp->rest == 0)); 175 176 TimerList = tp; 177 if (TimerList == NULL) /* No timers ? */ 178 timer_TermService(); /* Terminate Timer Service */ 179 180 /* 181 * Process all expired timers. 182 */ 183 while (exp) { 184 #ifdef notdef 185 timer_Stop(exp); 186 #endif 187 if (exp->func) 188 (*exp->func) (exp->arg); 189 190 /* 191 * Just Removing each item from expired list And exp->enext will be 192 * intialized at next expire in this funtion. 193 */ 194 exp = exp->enext; 195 } 196 } 197 } 198 } 199 200 void 201 timer_Show(int LogLevel, struct prompt *prompt) 202 { 203 struct pppTimer *pt; 204 int rest = 0; 205 206 #define SECS(val) ((val) / SECTICKS) 207 #define HSECS(val) (((val) % SECTICKS) * 100 / SECTICKS) 208 #define DISP \ 209 "%s timer[%p]: freq = %ld.%02lds, next = %d.%02ds, state = %s\n", \ 210 pt->name, pt, SECS(pt->load), HSECS(pt->load), SECS(rest), \ 211 HSECS(rest), tState2Nam(pt->state) 212 213 if (!prompt) 214 log_Printf(LogLevel, "---- Begin of Timer Service List---\n"); 215 216 for (pt = TimerList; pt; pt = pt->next) { 217 rest += pt->rest; 218 if (prompt) 219 prompt_Printf(prompt, DISP); 220 else 221 log_Printf(LogLevel, DISP); 222 } 223 224 if (!prompt) 225 log_Printf(LogLevel, "---- End of Timer Service List ---\n"); 226 } 227 228 static void 229 nointr_dosleep(u_int sec, u_int usec) 230 { 231 struct timeval to, st, et; 232 233 gettimeofday(&st, NULL); 234 et.tv_sec = st.tv_sec + sec; 235 et.tv_usec = st.tv_usec + usec; 236 to.tv_sec = sec; 237 to.tv_usec = usec; 238 for (;;) { 239 if (select(0, NULL, NULL, NULL, &to) == 0 || 240 errno != EINTR) { 241 break; 242 } else { 243 gettimeofday(&to, NULL); 244 if (to.tv_sec > et.tv_sec + 1 || 245 (to.tv_sec == et.tv_sec + 1 && to.tv_usec > et.tv_usec) || 246 to.tv_sec < st.tv_sec || 247 (to.tv_sec == st.tv_sec && to.tv_usec < st.tv_usec)) { 248 log_Printf(LogWARN, "Clock adjusted between %ld and %ld seconds " 249 "during sleep !\n", 250 to.tv_sec - st.tv_sec, sec + to.tv_sec - st.tv_sec); 251 st.tv_sec = to.tv_sec; 252 st.tv_usec = to.tv_usec; 253 et.tv_sec = st.tv_sec + sec; 254 et.tv_usec = st.tv_usec + usec; 255 to.tv_sec = sec; 256 to.tv_usec = usec; 257 } else if (to.tv_sec > et.tv_sec || 258 (to.tv_sec == et.tv_sec && to.tv_usec >= et.tv_usec)) { 259 break; 260 } else { 261 to.tv_sec = et.tv_sec - to.tv_sec; 262 if (et.tv_usec < to.tv_usec) { 263 to.tv_sec--; 264 to.tv_usec = 1000000 + et.tv_usec - to.tv_usec; 265 } else 266 to.tv_usec = et.tv_usec - to.tv_usec; 267 } 268 } 269 } 270 } 271 272 void 273 nointr_sleep(u_int sec) 274 { 275 nointr_dosleep(sec, 0); 276 } 277 278 void 279 nointr_usleep(u_int usec) 280 { 281 nointr_dosleep(0, usec); 282 } 283 284 static void 285 InitTimerService() 286 { 287 struct itimerval itimer; 288 289 sig_signal(SIGALRM, (void (*) (int)) TimerService); 290 itimer.it_interval.tv_sec = itimer.it_value.tv_sec = 0; 291 itimer.it_interval.tv_usec = itimer.it_value.tv_usec = TICKUNIT; 292 if (setitimer(ITIMER_REAL, &itimer, NULL) == -1) 293 log_Printf(LogERROR, "Unable to set itimer.\n"); 294 } 295 296 void 297 timer_TermService(void) 298 { 299 struct itimerval itimer; 300 301 itimer.it_interval.tv_usec = itimer.it_interval.tv_sec = 0; 302 itimer.it_value.tv_usec = itimer.it_value.tv_sec = 0; 303 if (setitimer(ITIMER_REAL, &itimer, NULL) == -1) 304 log_Printf(LogERROR, "Unable to set itimer.\n"); 305 sig_signal(SIGALRM, SIG_IGN); 306 } 307