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