timer.c revision 37061
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.29 1998/06/15 19:06:56 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 *);
41
42static const char *
43tState2Nam(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
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 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    timer_InitService();
102    TimerList = tp;
103  }
104  if (t)
105    t->rest -= tp->rest;
106
107  sigsetmask(omask);
108}
109
110static void
111StopTimerNoBlock(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  if (tp->state != TIMER_RUNNING) {
121    tp->next = NULL;
122    tp->state = TIMER_STOPPED;
123    return;
124  }
125  pt = NULL;
126  for (t = TimerList; t != tp && t != NULL; t = t->next)
127    pt = t;
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    log_Printf(LogERROR, "Oops, %s timer not found!!\n", tp->name);
140
141  tp->next = NULL;
142  tp->state = TIMER_STOPPED;
143}
144
145static void
146TimerService(void)
147{
148  struct pppTimer *tp, *exp, *wt;
149
150  if (log_IsKept(LogTIMER)) {
151    static time_t t;		/* Only show timers globally every second */
152    time_t n = time(NULL);
153
154    if (n > t)
155      timer_Show(LogTIMER, NULL);
156    t = n;
157  }
158  tp = TimerList;
159  if (tp) {
160    tp->rest--;
161    if (tp->rest == 0) {
162
163      /*
164       * Multiple timers may expires at once. Create list of expired timers.
165       */
166      exp = NULL;
167      do {
168	tp->state = TIMER_EXPIRED;
169	wt = tp->next;
170	tp->enext = exp;
171	exp = tp;
172	tp = wt;
173      } while (tp && (tp->rest == 0));
174
175      TimerList = tp;
176      if (TimerList == NULL)	/* No timers ? */
177	timer_TermService();	/* Terminate Timer Service */
178
179      /*
180       * Process all expired timers.
181       */
182      while (exp) {
183#ifdef notdef
184	timer_Stop(exp);
185#endif
186	if (exp->func)
187	  (*exp->func) (exp->arg);
188
189	/*
190	 * Just Removing each item from expired list And exp->enext will be
191	 * intialized at next expire in this funtion.
192	 */
193	exp = exp->enext;
194      }
195    }
196  }
197}
198
199void
200timer_Show(int LogLevel, struct prompt *prompt)
201{
202  struct pppTimer *pt;
203  int rest = 0;
204
205#define SECS(val)	((val) / SECTICKS)
206#define HSECS(val)	(((val) % SECTICKS) * 100 / SECTICKS)
207#define DISP								\
208  "%s timer[%p]: freq = %ld.%02lds, next = %d.%02ds, state = %s\n",	\
209  pt->name, pt, SECS(pt->load), HSECS(pt->load), SECS(rest),		\
210  HSECS(rest), tState2Nam(pt->state)
211
212  if (!prompt)
213    log_Printf(LogLevel, "---- Begin of Timer Service List---\n");
214
215  for (pt = TimerList; pt; pt = pt->next) {
216    rest += pt->rest;
217    if (prompt)
218      prompt_Printf(prompt, DISP);
219    else
220      log_Printf(LogLevel, DISP);
221  }
222
223  if (!prompt)
224    log_Printf(LogLevel, "---- End of Timer Service List ---\n");
225}
226
227void
228timer_InitService()
229{
230  struct itimerval itimer;
231
232  sig_signal(SIGALRM, (void (*) (int)) TimerService);
233  itimer.it_interval.tv_sec = itimer.it_value.tv_sec = 0;
234  itimer.it_interval.tv_usec = itimer.it_value.tv_usec = TICKUNIT;
235  if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
236    log_Printf(LogERROR, "Unable to set itimer.\n");
237}
238
239void
240timer_TermService(void)
241{
242  struct itimerval itimer;
243
244  itimer.it_interval.tv_usec = itimer.it_interval.tv_sec = 0;
245  itimer.it_value.tv_usec = itimer.it_value.tv_sec = 0;
246  if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
247    log_Printf(LogERROR, "Unable to set itimer.\n");
248  sig_signal(SIGALRM, SIG_IGN);
249}
250