xref: /freebsd/usr.sbin/ppp/timer.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
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 static struct pppTimer *TimerList = NULL, *ExpiredList = 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 
121   if (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 
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     /* Search for any pending expired timers */
140     pt = NULL;
141     for (t = ExpiredList; t != tp && t != NULL; t = t->enext)
142       pt = t;
143 
144     if (t) {
145       if (pt)
146         pt->enext = t->enext;
147       else
148         ExpiredList = t->enext;
149     } else if (tp->state == TIMER_RUNNING)
150       log_Printf(LogERROR, "Oops, %s timer not found!!\n", tp->name);
151   }
152 
153   tp->next = tp->enext = NULL;
154   tp->state = TIMER_STOPPED;
155 }
156 
157 static void
158 TimerService(void)
159 {
160   struct pppTimer *tp, *exp, *next;
161 
162   if (log_IsKept(LogTIMER)) {
163     static time_t t;		/* Only show timers globally every second */
164     time_t n = time(NULL);
165 
166     if (n > t)
167       timer_Show(LogTIMER, NULL);
168     t = n;
169   }
170 
171   tp = TimerList;
172   if (tp) {
173     tp->rest = 0;
174 
175     /* Multiple timers might expire at once. Create a list of expired timers */
176     exp = NULL;
177     do {
178       tp->state = TIMER_EXPIRED;
179       next = tp->next;
180       tp->enext = exp;
181       exp = tp;
182       tp = next;
183     } while (tp && tp->rest == 0);
184 
185     TimerList = tp;
186     if (TimerList != NULL)	/* Any timers remaining ? */
187       timer_InitService(1);	/* Restart the Timer Service */
188     else
189       timer_TermService();	/* Stop the Timer Service */
190 
191     /* Process all expired timers */
192     while (exp) {
193       ExpiredList = exp->enext;
194       exp->enext = NULL;
195       if (exp->func)
196         (*exp->func)(exp->arg);
197       exp = ExpiredList;
198     }
199   }
200 }
201 
202 void
203 timer_Show(int LogLevel, struct prompt *prompt)
204 {
205   struct itimerval itimer;
206   struct pppTimer *pt;
207   u_long rest = 0;
208 
209   /* Adjust our first delta so that it reflects what's really happening */
210   if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0)
211     TimerList->rest = itimer.it_value.tv_sec * SECTICKS +
212                       itimer.it_value.tv_usec / TICKUNIT;
213 
214 #define SECS(val)	((val) / SECTICKS)
215 #define HSECS(val)	(((val) % SECTICKS) * 100 / SECTICKS)
216 #define DISP								\
217   "%s timer[%p]: freq = %ld.%02lds, next = %lu.%02lus, state = %s\n",	\
218   pt->name, pt, SECS(pt->load), HSECS(pt->load), SECS(rest),		\
219   HSECS(rest), tState2Nam(pt->state)
220 
221   if (!prompt)
222     log_Printf(LogLevel, "---- Begin of Timer Service List---\n");
223 
224   for (pt = TimerList; pt; pt = pt->next) {
225     rest += pt->rest;
226     if (prompt)
227       prompt_Printf(prompt, DISP);
228     else
229       log_Printf(LogLevel, DISP);
230   }
231 
232   if (!prompt)
233     log_Printf(LogLevel, "---- End of Timer Service List ---\n");
234 }
235 
236 void
237 timer_InitService(int restart)
238 {
239   struct itimerval itimer;
240 
241   if (TimerList) {
242     if (!restart)
243       sig_signal(SIGALRM, (void (*)(int))TimerService);
244     itimer.it_interval.tv_sec = 0;
245     itimer.it_interval.tv_usec = 0;
246     itimer.it_value.tv_sec = TimerList->rest / SECTICKS;
247     itimer.it_value.tv_usec = (TimerList->rest % SECTICKS) * TICKUNIT;
248     if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
249       log_Printf(LogERROR, "Unable to set itimer (%s)\n", strerror(errno));
250   }
251 }
252 
253 void
254 timer_TermService(void)
255 {
256   struct itimerval itimer;
257 
258   itimer.it_interval.tv_usec = itimer.it_interval.tv_sec = 0;
259   itimer.it_value.tv_usec = itimer.it_value.tv_sec = 0;
260   if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
261     log_Printf(LogERROR, "Unable to set itimer (%s)\n", strerror(errno));
262   sig_signal(SIGALRM, SIG_IGN);
263 }
264