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