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