1998-01-29 00:44:16 +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-04-30 23:53:56 +00:00
|
|
|
* $Id: timer.c,v 1.27.2.8 1998/04/21 01:02:32 brian Exp $
|
1998-01-29 00:44:16 +00:00
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
1998-04-03 19:21:56 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
1998-01-29 00:44:16 +00:00
|
|
|
#include <sys/time.h>
|
1998-04-03 19:21:56 +00:00
|
|
|
#include <termios.h>
|
1998-01-29 00:44:16 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "sig.h"
|
|
|
|
#include "timer.h"
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "descriptor.h"
|
|
|
|
#include "prompt.h"
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
static struct pppTimer *TimerList = NULL;
|
|
|
|
|
|
|
|
static void StopTimerNoBlock(struct pppTimer *);
|
|
|
|
static void InitTimerService(void);
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
void
|
|
|
|
StopTimer(struct pppTimer * tp)
|
|
|
|
{
|
|
|
|
int omask;
|
|
|
|
|
|
|
|
omask = sigblock(sigmask(SIGALRM));
|
|
|
|
StopTimerNoBlock(tp);
|
|
|
|
sigsetmask(omask);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StartTimer(struct pppTimer * tp)
|
|
|
|
{
|
|
|
|
struct pppTimer *t, *pt;
|
|
|
|
u_long ticks = 0;
|
|
|
|
int omask;
|
|
|
|
|
|
|
|
omask = sigblock(sigmask(SIGALRM));
|
|
|
|
|
1998-04-21 01:02:32 +00:00
|
|
|
if (tp->state != TIMER_STOPPED)
|
1998-01-29 00:44:16 +00:00
|
|
|
StopTimerNoBlock(tp);
|
1998-04-21 01:02:32 +00:00
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
if (tp->load == 0) {
|
1998-04-30 23:53:56 +00:00
|
|
|
LogPrintf(LogTIMER, "%s timer[%p] has 0 load!\n", tp->name, tp);
|
1998-01-29 00:44:16 +00:00
|
|
|
sigsetmask(omask);
|
|
|
|
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-04-30 23:53:56 +00:00
|
|
|
LogPrintf(LogTIMER, "StartTimer: 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-04-30 23:53:56 +00:00
|
|
|
LogPrintf(LogTIMER, "StartTimer: Inserting %s timer[%p]\n", tp->name, tp);
|
1998-04-03 19:21:56 +00:00
|
|
|
|
1998-01-29 00:44:16 +00:00
|
|
|
/* Insert given *tp just before *t */
|
|
|
|
tp->next = t;
|
|
|
|
if (pt) {
|
|
|
|
pt->next = tp;
|
|
|
|
} else {
|
|
|
|
InitTimerService();
|
|
|
|
TimerList = tp;
|
|
|
|
}
|
|
|
|
if (t)
|
|
|
|
t->rest -= tp->rest;
|
|
|
|
|
|
|
|
sigsetmask(omask);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
StopTimerNoBlock(struct pppTimer * tp)
|
|
|
|
{
|
|
|
|
struct pppTimer *t, *pt;
|
|
|
|
|
|
|
|
/*
|
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.
|
1998-01-29 00:44:16 +00:00
|
|
|
*/
|
|
|
|
if (tp->state != TIMER_RUNNING) {
|
|
|
|
tp->next = NULL;
|
|
|
|
tp->state = TIMER_STOPPED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pt = NULL;
|
|
|
|
for (t = TimerList; t != tp && t != NULL; t = t->next)
|
|
|
|
pt = t;
|
|
|
|
if (t) {
|
|
|
|
if (pt) {
|
|
|
|
pt->next = t->next;
|
|
|
|
} else {
|
|
|
|
TimerList = t->next;
|
|
|
|
if (TimerList == NULL) /* Last one ? */
|
|
|
|
TermTimerService(); /* Terminate Timer Service */
|
|
|
|
}
|
|
|
|
if (t->next)
|
|
|
|
t->next->rest += tp->rest;
|
|
|
|
} else
|
1998-04-17 22:04:36 +00:00
|
|
|
LogPrintf(LogERROR, "Oops, %s timer not found!!\n", tp->name);
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
tp->next = NULL;
|
|
|
|
tp->state = TIMER_STOPPED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-04-18 23:17:26 +00:00
|
|
|
TimerService(void)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
struct pppTimer *tp, *exp, *wt;
|
|
|
|
|
1998-04-30 23:53:56 +00:00
|
|
|
if (LogIsKept(LogTIMER)) {
|
1998-04-03 19:21:56 +00:00
|
|
|
static time_t t;
|
|
|
|
time_t n = time(NULL); /* Only show timers every second */
|
|
|
|
|
|
|
|
if (n > t)
|
1998-04-30 23:53:56 +00:00
|
|
|
ShowTimers(LogTIMER, NULL);
|
1998-04-03 19:21:56 +00:00
|
|
|
t = n;
|
|
|
|
}
|
1998-01-29 00:44:16 +00:00
|
|
|
tp = TimerList;
|
|
|
|
if (tp) {
|
|
|
|
tp->rest--;
|
|
|
|
if (tp->rest == 0) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
if (TimerList == NULL) /* No timers ? */
|
|
|
|
TermTimerService(); /* Terminate Timer Service */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process all expired timers.
|
|
|
|
*/
|
|
|
|
while (exp) {
|
|
|
|
#ifdef notdef
|
|
|
|
StopTimer(exp);
|
|
|
|
#endif
|
|
|
|
if (exp->func)
|
|
|
|
(*exp->func) (exp->arg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Just Removing each item from expired list And exp->enext will be
|
|
|
|
* intialized at next expire in this funtion.
|
|
|
|
*/
|
|
|
|
exp = exp->enext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-04-03 19:26:02 +00:00
|
|
|
ShowTimers(int LogLevel, struct prompt *prompt)
|
1998-01-29 00:44:16 +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-04-03 19:21:56 +00:00
|
|
|
LogPrintf(LogLevel, "---- Begin of Timer Service List---\n");
|
|
|
|
|
|
|
|
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-04-03 19:21:56 +00:00
|
|
|
LogPrintf(LogLevel, DISP);
|
|
|
|
}
|
1998-01-29 00:44:16 +00:00
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
if (!prompt)
|
1998-04-03 19:21:56 +00:00
|
|
|
LogPrintf(LogLevel, "---- End of Timer Service List ---\n");
|
1998-01-29 00:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nointr_dosleep(u_int sec, u_int usec)
|
|
|
|
{
|
|
|
|
struct timeval to, st, et;
|
|
|
|
|
|
|
|
gettimeofday(&st, NULL);
|
|
|
|
et.tv_sec = st.tv_sec + sec;
|
|
|
|
et.tv_usec = st.tv_usec + usec;
|
|
|
|
to.tv_sec = sec;
|
|
|
|
to.tv_usec = usec;
|
|
|
|
for (;;) {
|
|
|
|
if (select(0, NULL, NULL, NULL, &to) == 0 ||
|
|
|
|
errno != EINTR) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
gettimeofday(&to, NULL);
|
|
|
|
if (to.tv_sec > et.tv_sec + 1 ||
|
|
|
|
(to.tv_sec == et.tv_sec + 1 && to.tv_usec > et.tv_usec) ||
|
|
|
|
to.tv_sec < st.tv_sec ||
|
|
|
|
(to.tv_sec == st.tv_sec && to.tv_usec < st.tv_usec)) {
|
1998-04-19 23:09:03 +00:00
|
|
|
LogPrintf(LogWARN, "Clock adjusted between %ld and %ld seconds "
|
1998-01-29 00:44:16 +00:00
|
|
|
"during sleep !\n",
|
|
|
|
to.tv_sec - st.tv_sec, sec + to.tv_sec - st.tv_sec);
|
|
|
|
st.tv_sec = to.tv_sec;
|
|
|
|
st.tv_usec = to.tv_usec;
|
|
|
|
et.tv_sec = st.tv_sec + sec;
|
|
|
|
et.tv_usec = st.tv_usec + usec;
|
|
|
|
to.tv_sec = sec;
|
|
|
|
to.tv_usec = usec;
|
|
|
|
} else if (to.tv_sec > et.tv_sec ||
|
|
|
|
(to.tv_sec == et.tv_sec && to.tv_usec >= et.tv_usec)) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
to.tv_sec = et.tv_sec - to.tv_sec;
|
|
|
|
if (et.tv_usec < to.tv_usec) {
|
|
|
|
to.tv_sec--;
|
|
|
|
to.tv_usec = 1000000 + et.tv_usec - to.tv_usec;
|
|
|
|
} else
|
|
|
|
to.tv_usec = et.tv_usec - to.tv_usec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nointr_sleep(u_int sec)
|
|
|
|
{
|
|
|
|
nointr_dosleep(sec, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nointr_usleep(u_int usec)
|
|
|
|
{
|
|
|
|
nointr_dosleep(0, usec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitTimerService()
|
|
|
|
{
|
|
|
|
struct itimerval itimer;
|
|
|
|
|
|
|
|
pending_signal(SIGALRM, (void (*) (int)) TimerService);
|
|
|
|
itimer.it_interval.tv_sec = itimer.it_value.tv_sec = 0;
|
|
|
|
itimer.it_interval.tv_usec = itimer.it_value.tv_usec = TICKUNIT;
|
|
|
|
if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
|
|
|
|
LogPrintf(LogERROR, "Unable to set itimer.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TermTimerService(void)
|
|
|
|
{
|
|
|
|
struct itimerval itimer;
|
|
|
|
|
|
|
|
itimer.it_interval.tv_usec = itimer.it_interval.tv_sec = 0;
|
|
|
|
itimer.it_value.tv_usec = itimer.it_value.tv_sec = 0;
|
|
|
|
if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
|
|
|
|
LogPrintf(LogERROR, "Unable to set itimer.\n");
|
|
|
|
pending_signal(SIGALRM, SIG_IGN);
|
|
|
|
}
|