timer.c revision 37192
10Sduke/*
22362Sohair *		PPP Timer Processing Module
30Sduke *
40Sduke *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
50Sduke *
60Sduke *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
70Sduke *
80Sduke * Redistribution and use in source and binary forms are permitted
90Sduke * provided that the above copyright notice and this paragraph are
100Sduke * duplicated in all such forms and that any documentation,
110Sduke * advertising materials, and other materials related to such
120Sduke * distribution and use acknowledge that the software was developed
130Sduke * by the Internet Initiative Japan, Inc.  The name of the
140Sduke * IIJ may not be used to endorse or promote products derived
150Sduke * from this software without specific prior written permission.
160Sduke * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
170Sduke * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
180Sduke * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
192362Sohair *
202362Sohair * $Id: timer.c,v 1.30 1998/06/20 01:36:38 brian Exp $
212362Sohair *
220Sduke *  TODO:
230Sduke */
240Sduke
250Sduke#include <signal.h>
260Sduke#include <stdio.h>
270Sduke#include <sys/time.h>
280Sduke#include <termios.h>
290Sduke
300Sduke#include "log.h"
310Sduke#include "sig.h"
320Sduke#include "timer.h"
330Sduke#include "descriptor.h"
340Sduke#include "prompt.h"
350Sduke
360Sdukestatic struct pppTimer *TimerList = NULL;
37
38static void StopTimerNoBlock(struct pppTimer *);
39
40static const char *
41tState2Nam(u_int state)
42{
43  static const char *StateNames[] = { "stopped", "running", "expired" };
44
45  if (state >= sizeof StateNames / sizeof StateNames[0])
46    return "unknown";
47  return StateNames[state];
48}
49
50void
51timer_Stop(struct pppTimer * tp)
52{
53  int omask;
54
55  omask = sigblock(sigmask(SIGALRM));
56  StopTimerNoBlock(tp);
57  sigsetmask(omask);
58}
59
60void
61timer_Start(struct pppTimer * tp)
62{
63  struct pppTimer *t, *pt;
64  u_long ticks = 0;
65  int omask;
66
67  omask = sigblock(sigmask(SIGALRM));
68
69  if (tp->state != TIMER_STOPPED)
70    StopTimerNoBlock(tp);
71
72  if (tp->load == 0) {
73    log_Printf(LogTIMER, "%s timer[%p] has 0 load!\n", tp->name, tp);
74    sigsetmask(omask);
75    return;
76  }
77  pt = NULL;
78  for (t = TimerList; t; t = t->next) {
79    if (ticks + t->rest >= tp->load)
80      break;
81    ticks += t->rest;
82    pt = t;
83  }
84
85  tp->state = TIMER_RUNNING;
86  tp->rest = tp->load - ticks;
87
88  if (t)
89    log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p] before %s "
90              "timer[%p], delta = %ld\n", tp->name, tp, t->name, t, tp->rest);
91  else
92    log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p]\n", tp->name, tp);
93
94  /* Insert given *tp just before *t */
95  tp->next = t;
96  if (pt) {
97    pt->next = tp;
98  } else {
99    timer_InitService();
100    TimerList = tp;
101  }
102  if (t)
103    t->rest -= tp->rest;
104
105  sigsetmask(omask);
106}
107
108static void
109StopTimerNoBlock(struct pppTimer * tp)
110{
111  struct pppTimer *t, *pt;
112
113  /*
114   * A RUNNING timer must be removed from TimerList (->next list).
115   * A STOPPED timer isn't in any list, but may have a bogus [e]next field.
116   * An EXPIRED timer is in the ->enext list.
117   */
118  if (tp->state != TIMER_RUNNING) {
119    tp->next = NULL;
120    tp->state = TIMER_STOPPED;
121    return;
122  }
123  pt = NULL;
124  for (t = TimerList; t != tp && t != NULL; t = t->next)
125    pt = t;
126  if (t) {
127    if (pt) {
128      pt->next = t->next;
129    } else {
130      TimerList = t->next;
131      if (TimerList == NULL)	/* Last one ? */
132	timer_TermService();	/* Terminate Timer Service */
133    }
134    if (t->next)
135      t->next->rest += tp->rest;
136  } else
137    log_Printf(LogERROR, "Oops, %s timer not found!!\n", tp->name);
138
139  tp->next = NULL;
140  tp->state = TIMER_STOPPED;
141}
142
143static void
144TimerService(void)
145{
146  struct pppTimer *tp, *exp, *wt;
147
148  if (log_IsKept(LogTIMER)) {
149    static time_t t;		/* Only show timers globally every second */
150    time_t n = time(NULL);
151
152    if (n > t)
153      timer_Show(LogTIMER, NULL);
154    t = n;
155  }
156  tp = TimerList;
157  if (tp) {
158    tp->rest--;
159    if (tp->rest == 0) {
160
161      /*
162       * Multiple timers may expires at once. Create list of expired timers.
163       */
164      exp = NULL;
165      do {
166	tp->state = TIMER_EXPIRED;
167	wt = tp->next;
168	tp->enext = exp;
169	exp = tp;
170	tp = wt;
171      } while (tp && (tp->rest == 0));
172
173      TimerList = tp;
174      if (TimerList == NULL)	/* No timers ? */
175	timer_TermService();	/* Terminate Timer Service */
176
177      /*
178       * Process all expired timers.
179       */
180      while (exp) {
181#ifdef notdef
182	timer_Stop(exp);
183#endif
184	if (exp->func)
185	  (*exp->func) (exp->arg);
186
187	/*
188	 * Just Removing each item from expired list And exp->enext will be
189	 * intialized at next expire in this funtion.
190	 */
191	exp = exp->enext;
192      }
193    }
194  }
195}
196
197void
198timer_Show(int LogLevel, struct prompt *prompt)
199{
200  struct pppTimer *pt;
201  int rest = 0;
202
203#define SECS(val)	((val) / SECTICKS)
204#define HSECS(val)	(((val) % SECTICKS) * 100 / SECTICKS)
205#define DISP								\
206  "%s timer[%p]: freq = %ld.%02lds, next = %d.%02ds, state = %s\n",	\
207  pt->name, pt, SECS(pt->load), HSECS(pt->load), SECS(rest),		\
208  HSECS(rest), tState2Nam(pt->state)
209
210  if (!prompt)
211    log_Printf(LogLevel, "---- Begin of Timer Service List---\n");
212
213  for (pt = TimerList; pt; pt = pt->next) {
214    rest += pt->rest;
215    if (prompt)
216      prompt_Printf(prompt, DISP);
217    else
218      log_Printf(LogLevel, DISP);
219  }
220
221  if (!prompt)
222    log_Printf(LogLevel, "---- End of Timer Service List ---\n");
223}
224
225void
226timer_InitService()
227{
228  struct itimerval itimer;
229
230  sig_signal(SIGALRM, (void (*) (int)) TimerService);
231  itimer.it_interval.tv_sec = itimer.it_value.tv_sec = 0;
232  itimer.it_interval.tv_usec = itimer.it_value.tv_usec = TICKUNIT;
233  if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
234    log_Printf(LogERROR, "Unable to set itimer.\n");
235}
236
237void
238timer_TermService(void)
239{
240  struct itimerval itimer;
241
242  itimer.it_interval.tv_usec = itimer.it_interval.tv_sec = 0;
243  itimer.it_value.tv_usec = itimer.it_value.tv_sec = 0;
244  if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
245    log_Printf(LogERROR, "Unable to set itimer.\n");
246  sig_signal(SIGALRM, SIG_IGN);
247}
248