1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* PPP Timer Processing Module
|
|
|
|
*
|
|
|
|
* Written by Toshiharu OHNO (tony-o@iij.ad.jp)
|
|
|
|
*
|
|
|
|
* Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that the above copyright notice and this paragraph are
|
|
|
|
* duplicated in all such forms and that any documentation,
|
|
|
|
* advertising materials, and other materials related to such
|
|
|
|
* distribution and use acknowledge that the software was developed
|
|
|
|
* by the Internet Initiative Japan, Inc. The name of the
|
|
|
|
* IIJ may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
1998-06-15 19:06:58 +00:00
|
|
|
* $Id: timer.c,v 1.28 1998/05/21 21:48:46 brian Exp $
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-01-31 06:29:58 +00:00
|
|
|
* TODO:
|
|
|
|
*/
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
#include <errno.h>
|
1998-04-03 19:21:56 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <sys/time.h>
|
1998-04-03 19:21:56 +00:00
|
|
|
#include <termios.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
1997-03-13 12:45:35 +00:00
|
|
|
#include "sig.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "timer.h"
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "descriptor.h"
|
|
|
|
#include "prompt.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1998-01-21 02:15:33 +00:00
|
|
|
static struct pppTimer *TimerList = NULL;
|
1997-03-13 12:45:35 +00:00
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void StopTimerNoBlock(struct pppTimer *);
|
|
|
|
static void InitTimerService(void);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-04-03 19:21:56 +00:00
|
|
|
static const char *
|
|
|
|
tState2Nam(u_int state)
|
|
|
|
{
|
|
|
|
static const char *StateNames[] = { "stopped", "running", "expired" };
|
|
|
|
|
|
|
|
if (state >= sizeof StateNames / sizeof StateNames[0])
|
|
|
|
return "unknown";
|
|
|
|
return StateNames[state];
|
|
|
|
}
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
void
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_Stop(struct pppTimer * tp)
|
1995-02-26 12:18:08 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
int omask;
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
omask = sigblock(sigmask(SIGALRM));
|
|
|
|
StopTimerNoBlock(tp);
|
|
|
|
sigsetmask(omask);
|
|
|
|
}
|
1997-06-09 03:27:43 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
void
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_Start(struct pppTimer * tp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct pppTimer *t, *pt;
|
|
|
|
u_long ticks = 0;
|
1997-08-25 00:29:32 +00:00
|
|
|
int omask;
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
omask = sigblock(sigmask(SIGALRM));
|
|
|
|
|
1998-04-21 01:02:32 +00:00
|
|
|
if (tp->state != TIMER_STOPPED)
|
1995-02-26 12:18:08 +00:00
|
|
|
StopTimerNoBlock(tp);
|
1998-04-21 01:02:32 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
if (tp->load == 0) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogTIMER, "%s timer[%p] has 0 load!\n", tp->name, tp);
|
1995-02-26 12:18:08 +00:00
|
|
|
sigsetmask(omask);
|
1995-01-31 06:29:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
pt = NULL;
|
|
|
|
for (t = TimerList; t; t = t->next) {
|
|
|
|
if (ticks + t->rest >= tp->load)
|
|
|
|
break;
|
|
|
|
ticks += t->rest;
|
|
|
|
pt = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
tp->state = TIMER_RUNNING;
|
|
|
|
tp->rest = tp->load - ticks;
|
1998-04-03 19:21:56 +00:00
|
|
|
|
|
|
|
if (t)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p] before %s "
|
1998-04-19 23:09:03 +00:00
|
|
|
"timer[%p], delta = %ld\n", tp->name, tp, t->name, t, tp->rest);
|
1998-04-03 19:21:56 +00:00
|
|
|
else
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p]\n", tp->name, tp);
|
1998-04-03 19:21:56 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
/* Insert given *tp just before *t */
|
|
|
|
tp->next = t;
|
|
|
|
if (pt) {
|
|
|
|
pt->next = tp;
|
1995-03-11 15:18:55 +00:00
|
|
|
} else {
|
|
|
|
InitTimerService();
|
1995-01-31 06:29:58 +00:00
|
|
|
TimerList = tp;
|
1995-03-11 15:18:55 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
if (t)
|
|
|
|
t->rest -= tp->rest;
|
1995-02-26 12:18:08 +00:00
|
|
|
|
|
|
|
sigsetmask(omask);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void
|
1997-08-25 00:29:32 +00:00
|
|
|
StopTimerNoBlock(struct pppTimer * tp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct pppTimer *t, *pt;
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
/*
|
1998-04-21 01:02:32 +00:00
|
|
|
* A RUNNING timer must be removed from TimerList (->next list).
|
|
|
|
* A STOPPED timer isn't in any list, but may have a bogus [e]next field.
|
|
|
|
* An EXPIRED timer is in the ->enext list.
|
1995-02-26 12:18:08 +00:00
|
|
|
*/
|
1995-01-31 06:29:58 +00:00
|
|
|
if (tp->state != TIMER_RUNNING) {
|
1997-08-25 00:29:32 +00:00
|
|
|
tp->next = NULL;
|
|
|
|
tp->state = TIMER_STOPPED;
|
1995-01-31 06:29:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
pt = NULL;
|
1997-08-25 00:29:32 +00:00
|
|
|
for (t = TimerList; t != tp && t != NULL; t = t->next)
|
1995-01-31 06:29:58 +00:00
|
|
|
pt = t;
|
|
|
|
if (t) {
|
1995-03-11 15:18:55 +00:00
|
|
|
if (pt) {
|
1995-01-31 06:29:58 +00:00
|
|
|
pt->next = t->next;
|
1995-03-11 15:18:55 +00:00
|
|
|
} else {
|
1995-01-31 06:29:58 +00:00
|
|
|
TimerList = t->next;
|
1997-08-25 00:29:32 +00:00
|
|
|
if (TimerList == NULL) /* Last one ? */
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_TermService(); /* Terminate Timer Service */
|
1995-03-11 15:18:55 +00:00
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
if (t->next)
|
|
|
|
t->next->rest += tp->rest;
|
1997-06-09 03:27:43 +00:00
|
|
|
} else
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "Oops, %s timer not found!!\n", tp->name);
|
1997-06-09 03:27:43 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
tp->next = NULL;
|
|
|
|
tp->state = TIMER_STOPPED;
|
|
|
|
}
|
|
|
|
|
1998-01-21 02:15:33 +00:00
|
|
|
static void
|
1998-04-18 23:17:26 +00:00
|
|
|
TimerService(void)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct pppTimer *tp, *exp, *wt;
|
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
if (log_IsKept(LogTIMER)) {
|
1998-06-15 19:06:58 +00:00
|
|
|
static time_t t; /* Only show timers globally every second */
|
|
|
|
time_t n = time(NULL);
|
1998-04-03 19:21:56 +00:00
|
|
|
|
|
|
|
if (n > t)
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_Show(LogTIMER, NULL);
|
1998-04-03 19:21:56 +00:00
|
|
|
t = n;
|
|
|
|
}
|
1996-01-10 21:28:04 +00:00
|
|
|
tp = TimerList;
|
|
|
|
if (tp) {
|
1995-01-31 06:29:58 +00:00
|
|
|
tp->rest--;
|
|
|
|
if (tp->rest == 0) {
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* Multiple timers may expires at once. Create list of expired timers.
|
|
|
|
*/
|
|
|
|
exp = NULL;
|
|
|
|
do {
|
|
|
|
tp->state = TIMER_EXPIRED;
|
|
|
|
wt = tp->next;
|
|
|
|
tp->enext = exp;
|
|
|
|
exp = tp;
|
|
|
|
tp = wt;
|
|
|
|
} while (tp && (tp->rest == 0));
|
|
|
|
|
|
|
|
TimerList = tp;
|
1997-08-25 00:29:32 +00:00
|
|
|
if (TimerList == NULL) /* No timers ? */
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_TermService(); /* Terminate Timer Service */
|
1997-08-25 00:29:32 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* Process all expired timers.
|
|
|
|
*/
|
|
|
|
while (exp) {
|
|
|
|
#ifdef notdef
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_Stop(exp);
|
1995-01-31 06:29:58 +00:00
|
|
|
#endif
|
|
|
|
if (exp->func)
|
1997-08-25 00:29:32 +00:00
|
|
|
(*exp->func) (exp->arg);
|
|
|
|
|
1995-05-30 03:57:47 +00:00
|
|
|
/*
|
1997-08-25 00:29:32 +00:00
|
|
|
* Just Removing each item from expired list And exp->enext will be
|
|
|
|
* intialized at next expire in this funtion.
|
|
|
|
*/
|
|
|
|
exp = exp->enext;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_Show(int LogLevel, struct prompt *prompt)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct pppTimer *pt;
|
1998-04-03 19:21:56 +00:00
|
|
|
int rest = 0;
|
|
|
|
|
|
|
|
#define SECS(val) ((val) / SECTICKS)
|
|
|
|
#define HSECS(val) (((val) % SECTICKS) * 100 / SECTICKS)
|
|
|
|
#define DISP \
|
1998-04-19 23:09:03 +00:00
|
|
|
"%s timer[%p]: freq = %ld.%02lds, next = %d.%02ds, state = %s\n", \
|
1998-04-03 19:21:56 +00:00
|
|
|
pt->name, pt, SECS(pt->load), HSECS(pt->load), SECS(rest), \
|
|
|
|
HSECS(rest), tState2Nam(pt->state)
|
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
if (!prompt)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogLevel, "---- Begin of Timer Service List---\n");
|
1998-04-03 19:21:56 +00:00
|
|
|
|
|
|
|
for (pt = TimerList; pt; pt = pt->next) {
|
|
|
|
rest += pt->rest;
|
1998-04-03 19:26:02 +00:00
|
|
|
if (prompt)
|
|
|
|
prompt_Printf(prompt, DISP);
|
|
|
|
else
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogLevel, DISP);
|
1997-10-23 21:32:48 +00:00
|
|
|
}
|
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
if (!prompt)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogLevel, "---- End of Timer Service List ---\n");
|
1997-10-23 21:32:48 +00:00
|
|
|
}
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void
|
|
|
|
InitTimerService()
|
1997-08-25 00:29:32 +00:00
|
|
|
{
|
1995-03-11 15:18:55 +00:00
|
|
|
struct itimerval itimer;
|
|
|
|
|
1998-05-01 19:26:12 +00:00
|
|
|
sig_signal(SIGALRM, (void (*) (int)) TimerService);
|
1995-03-11 15:18:55 +00:00
|
|
|
itimer.it_interval.tv_sec = itimer.it_value.tv_sec = 0;
|
|
|
|
itimer.it_interval.tv_usec = itimer.it_value.tv_usec = TICKUNIT;
|
1997-06-09 03:27:43 +00:00
|
|
|
if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "Unable to set itimer.\n");
|
1995-03-11 15:18:55 +00:00
|
|
|
}
|
|
|
|
|
1997-08-25 00:29:32 +00:00
|
|
|
void
|
1998-05-01 19:26:12 +00:00
|
|
|
timer_TermService(void)
|
1997-08-25 00:29:32 +00:00
|
|
|
{
|
1995-03-11 15:18:55 +00:00
|
|
|
struct itimerval itimer;
|
|
|
|
|
1997-05-09 20:48:21 +00:00
|
|
|
itimer.it_interval.tv_usec = itimer.it_interval.tv_sec = 0;
|
1995-03-11 15:18:55 +00:00
|
|
|
itimer.it_value.tv_usec = itimer.it_value.tv_sec = 0;
|
1997-06-09 03:27:43 +00:00
|
|
|
if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "Unable to set itimer.\n");
|
|
|
|
sig_signal(SIGALRM, SIG_IGN);
|
1995-03-11 15:18:55 +00:00
|
|
|
}
|