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