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