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