xref: /dflybsd-src/usr.sbin/ppp/timer.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /*-
2*86d7f5d3SJohn Marino  * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3*86d7f5d3SJohn Marino  *          based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4*86d7f5d3SJohn Marino  *                           Internet Initiative Japan, Inc (IIJ)
5*86d7f5d3SJohn Marino  * All rights reserved.
6*86d7f5d3SJohn Marino  *
7*86d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
8*86d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
9*86d7f5d3SJohn Marino  * are met:
10*86d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
11*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
12*86d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
13*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
14*86d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
15*86d7f5d3SJohn Marino  *
16*86d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*86d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*86d7f5d3SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*86d7f5d3SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*86d7f5d3SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*86d7f5d3SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*86d7f5d3SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*86d7f5d3SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*86d7f5d3SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*86d7f5d3SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*86d7f5d3SJohn Marino  * SUCH DAMAGE.
27*86d7f5d3SJohn Marino  *
28*86d7f5d3SJohn Marino  * $FreeBSD: src/usr.sbin/ppp/timer.c,v 1.38.2.3 2002/09/01 02:12:32 brian Exp $
29*86d7f5d3SJohn Marino  * $DragonFly: src/usr.sbin/ppp/timer.c,v 1.3 2008/05/19 10:19:49 corecode Exp $
30*86d7f5d3SJohn Marino  */
31*86d7f5d3SJohn Marino 
32*86d7f5d3SJohn Marino #include <sys/select.h>
33*86d7f5d3SJohn Marino 
34*86d7f5d3SJohn Marino #include <errno.h>
35*86d7f5d3SJohn Marino #include <signal.h>
36*86d7f5d3SJohn Marino #include <stdarg.h>
37*86d7f5d3SJohn Marino #include <stdio.h>
38*86d7f5d3SJohn Marino #include <string.h>
39*86d7f5d3SJohn Marino #include <sys/time.h>
40*86d7f5d3SJohn Marino #include <termios.h>
41*86d7f5d3SJohn Marino 
42*86d7f5d3SJohn Marino #include "log.h"
43*86d7f5d3SJohn Marino #include "sig.h"
44*86d7f5d3SJohn Marino #include "timer.h"
45*86d7f5d3SJohn Marino #include "descriptor.h"
46*86d7f5d3SJohn Marino #include "prompt.h"
47*86d7f5d3SJohn Marino 
48*86d7f5d3SJohn Marino 
49*86d7f5d3SJohn Marino #define RESTVAL(t) \
50*86d7f5d3SJohn Marino     ((t).it_value.tv_sec * SECTICKS + (t).it_value.tv_usec / TICKUNIT + \
51*86d7f5d3SJohn Marino      ((((t).it_value.tv_usec % TICKUNIT) >= (TICKUNIT >> 1)) ? 1 : 0))
52*86d7f5d3SJohn Marino 
53*86d7f5d3SJohn Marino static struct pppTimer *TimerList = NULL, *ExpiredList = NULL;
54*86d7f5d3SJohn Marino 
55*86d7f5d3SJohn Marino static void StopTimerNoBlock(struct pppTimer *);
56*86d7f5d3SJohn Marino 
57*86d7f5d3SJohn Marino static const char *
tState2Nam(u_int state)58*86d7f5d3SJohn Marino tState2Nam(u_int state)
59*86d7f5d3SJohn Marino {
60*86d7f5d3SJohn Marino   static const char * const StateNames[] = { "stopped", "running", "expired" };
61*86d7f5d3SJohn Marino 
62*86d7f5d3SJohn Marino   if (state >= sizeof StateNames / sizeof StateNames[0])
63*86d7f5d3SJohn Marino     return "unknown";
64*86d7f5d3SJohn Marino   return StateNames[state];
65*86d7f5d3SJohn Marino }
66*86d7f5d3SJohn Marino 
67*86d7f5d3SJohn Marino void
timer_Stop(struct pppTimer * tp)68*86d7f5d3SJohn Marino timer_Stop(struct pppTimer *tp)
69*86d7f5d3SJohn Marino {
70*86d7f5d3SJohn Marino   sigset_t mask, omask;
71*86d7f5d3SJohn Marino 
72*86d7f5d3SJohn Marino   sigemptyset(&mask);
73*86d7f5d3SJohn Marino   sigaddset(&mask, SIGALRM);
74*86d7f5d3SJohn Marino   sigprocmask(SIG_BLOCK, &mask, &omask);
75*86d7f5d3SJohn Marino   StopTimerNoBlock(tp);
76*86d7f5d3SJohn Marino   sigprocmask(SIG_SETMASK, &omask, NULL);
77*86d7f5d3SJohn Marino }
78*86d7f5d3SJohn Marino 
79*86d7f5d3SJohn Marino void
timer_Start(struct pppTimer * tp)80*86d7f5d3SJohn Marino timer_Start(struct pppTimer *tp)
81*86d7f5d3SJohn Marino {
82*86d7f5d3SJohn Marino   struct itimerval itimer;
83*86d7f5d3SJohn Marino   struct pppTimer *t, *pt;
84*86d7f5d3SJohn Marino   u_long ticks = 0;
85*86d7f5d3SJohn Marino   sigset_t mask, omask;
86*86d7f5d3SJohn Marino 
87*86d7f5d3SJohn Marino   sigemptyset(&mask);
88*86d7f5d3SJohn Marino   sigaddset(&mask, SIGALRM);
89*86d7f5d3SJohn Marino   sigprocmask(SIG_BLOCK, &mask, &omask);
90*86d7f5d3SJohn Marino 
91*86d7f5d3SJohn Marino   if (tp->state != TIMER_STOPPED)
92*86d7f5d3SJohn Marino     StopTimerNoBlock(tp);
93*86d7f5d3SJohn Marino 
94*86d7f5d3SJohn Marino   if (tp->load == 0) {
95*86d7f5d3SJohn Marino     log_Printf(LogTIMER, "%s timer[%p] has 0 load!\n", tp->name, tp);
96*86d7f5d3SJohn Marino     sigprocmask(SIG_SETMASK, &omask, NULL);
97*86d7f5d3SJohn Marino     return;
98*86d7f5d3SJohn Marino   }
99*86d7f5d3SJohn Marino 
100*86d7f5d3SJohn Marino   /* Adjust our first delta so that it reflects what's really happening */
101*86d7f5d3SJohn Marino   if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0)
102*86d7f5d3SJohn Marino     TimerList->rest = RESTVAL(itimer);
103*86d7f5d3SJohn Marino 
104*86d7f5d3SJohn Marino   pt = NULL;
105*86d7f5d3SJohn Marino   for (t = TimerList; t; t = t->next) {
106*86d7f5d3SJohn Marino     if (ticks + t->rest >= tp->load)
107*86d7f5d3SJohn Marino       break;
108*86d7f5d3SJohn Marino     ticks += t->rest;
109*86d7f5d3SJohn Marino     pt = t;
110*86d7f5d3SJohn Marino   }
111*86d7f5d3SJohn Marino 
112*86d7f5d3SJohn Marino   tp->state = TIMER_RUNNING;
113*86d7f5d3SJohn Marino   tp->rest = tp->load - ticks;
114*86d7f5d3SJohn Marino 
115*86d7f5d3SJohn Marino   if (t)
116*86d7f5d3SJohn Marino     log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p] before %s "
117*86d7f5d3SJohn Marino               "timer[%p], delta = %ld\n", tp->name, tp, t->name, t, tp->rest);
118*86d7f5d3SJohn Marino   else
119*86d7f5d3SJohn Marino     log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p]\n", tp->name, tp);
120*86d7f5d3SJohn Marino 
121*86d7f5d3SJohn Marino   /* Insert given *tp just before *t */
122*86d7f5d3SJohn Marino   tp->next = t;
123*86d7f5d3SJohn Marino   if (pt) {
124*86d7f5d3SJohn Marino     pt->next = tp;
125*86d7f5d3SJohn Marino   } else {
126*86d7f5d3SJohn Marino     TimerList = tp;
127*86d7f5d3SJohn Marino     timer_InitService(t != NULL);	/* [re]Start the Timer Service */
128*86d7f5d3SJohn Marino   }
129*86d7f5d3SJohn Marino   if (t)
130*86d7f5d3SJohn Marino     t->rest -= tp->rest;
131*86d7f5d3SJohn Marino 
132*86d7f5d3SJohn Marino   sigprocmask(SIG_SETMASK, &omask, NULL);
133*86d7f5d3SJohn Marino }
134*86d7f5d3SJohn Marino 
135*86d7f5d3SJohn Marino static void
StopTimerNoBlock(struct pppTimer * tp)136*86d7f5d3SJohn Marino StopTimerNoBlock(struct pppTimer *tp)
137*86d7f5d3SJohn Marino {
138*86d7f5d3SJohn Marino   struct pppTimer *t, *pt;
139*86d7f5d3SJohn Marino 
140*86d7f5d3SJohn Marino   /*
141*86d7f5d3SJohn Marino    * A RUNNING timer must be removed from TimerList (->next list).
142*86d7f5d3SJohn Marino    * A STOPPED timer isn't in any list, but may have a bogus [e]next field.
143*86d7f5d3SJohn Marino    * An EXPIRED timer is in the ->enext list.
144*86d7f5d3SJohn Marino    */
145*86d7f5d3SJohn Marino 
146*86d7f5d3SJohn Marino   if (tp->state == TIMER_STOPPED)
147*86d7f5d3SJohn Marino     return;
148*86d7f5d3SJohn Marino 
149*86d7f5d3SJohn Marino   pt = NULL;
150*86d7f5d3SJohn Marino   for (t = TimerList; t != tp && t != NULL; t = t->next)
151*86d7f5d3SJohn Marino     pt = t;
152*86d7f5d3SJohn Marino 
153*86d7f5d3SJohn Marino   if (t) {
154*86d7f5d3SJohn Marino     if (pt)
155*86d7f5d3SJohn Marino       pt->next = t->next;
156*86d7f5d3SJohn Marino     else {
157*86d7f5d3SJohn Marino       TimerList = t->next;
158*86d7f5d3SJohn Marino       if (TimerList == NULL)	/* Last one ? */
159*86d7f5d3SJohn Marino 	timer_TermService();	/* Terminate Timer Service */
160*86d7f5d3SJohn Marino     }
161*86d7f5d3SJohn Marino     if (t->next) {
162*86d7f5d3SJohn Marino       if (!pt) {		/* t (tp) was the first in the list */
163*86d7f5d3SJohn Marino         struct itimerval itimer;
164*86d7f5d3SJohn Marino 
165*86d7f5d3SJohn Marino         if (getitimer(ITIMER_REAL, &itimer) == 0)
166*86d7f5d3SJohn Marino           t->rest = RESTVAL(itimer);
167*86d7f5d3SJohn Marino       }
168*86d7f5d3SJohn Marino       t->next->rest += t->rest;
169*86d7f5d3SJohn Marino       if (!pt)			/* t->next is now the first in the list */
170*86d7f5d3SJohn Marino         timer_InitService(1);
171*86d7f5d3SJohn Marino     }
172*86d7f5d3SJohn Marino   } else {
173*86d7f5d3SJohn Marino     /* Search for any pending expired timers */
174*86d7f5d3SJohn Marino     pt = NULL;
175*86d7f5d3SJohn Marino     for (t = ExpiredList; t != tp && t != NULL; t = t->enext)
176*86d7f5d3SJohn Marino       pt = t;
177*86d7f5d3SJohn Marino 
178*86d7f5d3SJohn Marino     if (t) {
179*86d7f5d3SJohn Marino       if (pt)
180*86d7f5d3SJohn Marino         pt->enext = t->enext;
181*86d7f5d3SJohn Marino       else
182*86d7f5d3SJohn Marino         ExpiredList = t->enext;
183*86d7f5d3SJohn Marino     } else if (tp->state == TIMER_RUNNING)
184*86d7f5d3SJohn Marino       log_Printf(LogERROR, "Oops, %s timer not found!!\n", tp->name);
185*86d7f5d3SJohn Marino   }
186*86d7f5d3SJohn Marino 
187*86d7f5d3SJohn Marino   tp->next = tp->enext = NULL;
188*86d7f5d3SJohn Marino   tp->state = TIMER_STOPPED;
189*86d7f5d3SJohn Marino }
190*86d7f5d3SJohn Marino 
191*86d7f5d3SJohn Marino static void
TimerService(void)192*86d7f5d3SJohn Marino TimerService(void)
193*86d7f5d3SJohn Marino {
194*86d7f5d3SJohn Marino   struct pppTimer *tp, *exp, *next;
195*86d7f5d3SJohn Marino 
196*86d7f5d3SJohn Marino   if (log_IsKept(LogTIMER)) {
197*86d7f5d3SJohn Marino     static time_t t;		/* Only show timers globally every second */
198*86d7f5d3SJohn Marino     time_t n = time(NULL);
199*86d7f5d3SJohn Marino 
200*86d7f5d3SJohn Marino     if (n > t)
201*86d7f5d3SJohn Marino       timer_Show(LogTIMER, NULL);
202*86d7f5d3SJohn Marino     t = n;
203*86d7f5d3SJohn Marino   }
204*86d7f5d3SJohn Marino 
205*86d7f5d3SJohn Marino   tp = TimerList;
206*86d7f5d3SJohn Marino   if (tp) {
207*86d7f5d3SJohn Marino     tp->rest = 0;
208*86d7f5d3SJohn Marino 
209*86d7f5d3SJohn Marino     /* Multiple timers might expire at once. Create a list of expired timers */
210*86d7f5d3SJohn Marino     exp = NULL;
211*86d7f5d3SJohn Marino     do {
212*86d7f5d3SJohn Marino       tp->state = TIMER_EXPIRED;
213*86d7f5d3SJohn Marino       next = tp->next;
214*86d7f5d3SJohn Marino       tp->enext = exp;
215*86d7f5d3SJohn Marino       exp = tp;
216*86d7f5d3SJohn Marino       tp = next;
217*86d7f5d3SJohn Marino     } while (tp && tp->rest == 0);
218*86d7f5d3SJohn Marino 
219*86d7f5d3SJohn Marino     TimerList = tp;
220*86d7f5d3SJohn Marino     if (TimerList != NULL)	/* Any timers remaining ? */
221*86d7f5d3SJohn Marino       timer_InitService(1);	/* Restart the Timer Service */
222*86d7f5d3SJohn Marino     else
223*86d7f5d3SJohn Marino       timer_TermService();	/* Stop the Timer Service */
224*86d7f5d3SJohn Marino 
225*86d7f5d3SJohn Marino     /* Process all expired timers */
226*86d7f5d3SJohn Marino     while (exp) {
227*86d7f5d3SJohn Marino       ExpiredList = exp->enext;
228*86d7f5d3SJohn Marino       exp->enext = NULL;
229*86d7f5d3SJohn Marino       if (exp->func)
230*86d7f5d3SJohn Marino         (*exp->func)(exp->arg);
231*86d7f5d3SJohn Marino       exp = ExpiredList;
232*86d7f5d3SJohn Marino     }
233*86d7f5d3SJohn Marino   }
234*86d7f5d3SJohn Marino }
235*86d7f5d3SJohn Marino 
236*86d7f5d3SJohn Marino void
timer_Show(int LogLevel,struct prompt * prompt)237*86d7f5d3SJohn Marino timer_Show(int LogLevel, struct prompt *prompt)
238*86d7f5d3SJohn Marino {
239*86d7f5d3SJohn Marino   struct itimerval itimer;
240*86d7f5d3SJohn Marino   struct pppTimer *pt;
241*86d7f5d3SJohn Marino   u_long rest = 0;
242*86d7f5d3SJohn Marino 
243*86d7f5d3SJohn Marino   /* Adjust our first delta so that it reflects what's really happening */
244*86d7f5d3SJohn Marino   if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0)
245*86d7f5d3SJohn Marino     TimerList->rest = RESTVAL(itimer);
246*86d7f5d3SJohn Marino 
247*86d7f5d3SJohn Marino #define SECS(val)	((val) / SECTICKS)
248*86d7f5d3SJohn Marino #define HSECS(val)	(((val) % SECTICKS) * 100 / SECTICKS)
249*86d7f5d3SJohn Marino #define DISP								\
250*86d7f5d3SJohn Marino   "%s timer[%p]: freq = %ld.%02lds, next = %lu.%02lus, state = %s\n",	\
251*86d7f5d3SJohn Marino   pt->name, pt, SECS(pt->load), HSECS(pt->load), SECS(rest),		\
252*86d7f5d3SJohn Marino   HSECS(rest), tState2Nam(pt->state)
253*86d7f5d3SJohn Marino 
254*86d7f5d3SJohn Marino   if (!prompt)
255*86d7f5d3SJohn Marino     log_Printf(LogLevel, "---- Begin of Timer Service List---\n");
256*86d7f5d3SJohn Marino 
257*86d7f5d3SJohn Marino   for (pt = TimerList; pt; pt = pt->next) {
258*86d7f5d3SJohn Marino     rest += pt->rest;
259*86d7f5d3SJohn Marino     if (prompt)
260*86d7f5d3SJohn Marino       prompt_Printf(prompt, DISP);
261*86d7f5d3SJohn Marino     else
262*86d7f5d3SJohn Marino       log_Printf(LogLevel, DISP);
263*86d7f5d3SJohn Marino   }
264*86d7f5d3SJohn Marino 
265*86d7f5d3SJohn Marino   if (!prompt)
266*86d7f5d3SJohn Marino     log_Printf(LogLevel, "---- End of Timer Service List ---\n");
267*86d7f5d3SJohn Marino }
268*86d7f5d3SJohn Marino 
269*86d7f5d3SJohn Marino void
timer_InitService(int restart)270*86d7f5d3SJohn Marino timer_InitService(int restart)
271*86d7f5d3SJohn Marino {
272*86d7f5d3SJohn Marino   struct itimerval itimer;
273*86d7f5d3SJohn Marino 
274*86d7f5d3SJohn Marino   if (TimerList) {
275*86d7f5d3SJohn Marino     if (!restart)
276*86d7f5d3SJohn Marino       sig_signal(SIGALRM, (void (*)(int))TimerService);
277*86d7f5d3SJohn Marino     itimer.it_interval.tv_sec = 0;
278*86d7f5d3SJohn Marino     itimer.it_interval.tv_usec = 0;
279*86d7f5d3SJohn Marino     itimer.it_value.tv_sec = TimerList->rest / SECTICKS;
280*86d7f5d3SJohn Marino     itimer.it_value.tv_usec = (TimerList->rest % SECTICKS) * TICKUNIT;
281*86d7f5d3SJohn Marino     if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
282*86d7f5d3SJohn Marino       log_Printf(LogERROR, "Unable to set itimer (%s)\n", strerror(errno));
283*86d7f5d3SJohn Marino   }
284*86d7f5d3SJohn Marino }
285*86d7f5d3SJohn Marino 
286*86d7f5d3SJohn Marino void
timer_TermService(void)287*86d7f5d3SJohn Marino timer_TermService(void)
288*86d7f5d3SJohn Marino {
289*86d7f5d3SJohn Marino   struct itimerval itimer;
290*86d7f5d3SJohn Marino 
291*86d7f5d3SJohn Marino   itimer.it_interval.tv_usec = itimer.it_interval.tv_sec = 0;
292*86d7f5d3SJohn Marino   itimer.it_value.tv_usec = itimer.it_value.tv_sec = 0;
293*86d7f5d3SJohn Marino   if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
294*86d7f5d3SJohn Marino     log_Printf(LogERROR, "Unable to set itimer (%s)\n", strerror(errno));
295*86d7f5d3SJohn Marino   sig_signal(SIGALRM, SIG_IGN);
296*86d7f5d3SJohn Marino }
297