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.
|
|
|
|
*
|
1997-05-10 01:22:19 +00:00
|
|
|
* $Id: timer.c,v 1.15 1997/05/09 20:48:21 brian Exp $
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-01-31 06:29:58 +00:00
|
|
|
* TODO:
|
|
|
|
*/
|
|
|
|
#include "defs.h"
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include "timeout.h"
|
1995-05-30 03:57:47 +00:00
|
|
|
#ifdef SIGALRM
|
1995-02-26 12:18:08 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
1997-03-13 12:45:35 +00:00
|
|
|
#include "sig.h"
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
void StopTimerNoBlock( struct pppTimer *);
|
|
|
|
void ShowTimers(void);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
void
|
|
|
|
StopTimer( struct pppTimer *tp )
|
|
|
|
{
|
|
|
|
#ifdef SIGALRM
|
|
|
|
int omask;
|
|
|
|
omask = sigblock(sigmask(SIGALRM));
|
|
|
|
#endif
|
|
|
|
StopTimerNoBlock(tp);
|
|
|
|
#ifdef SIGALRM
|
|
|
|
sigsetmask(omask);
|
|
|
|
#endif
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
void
|
|
|
|
StartTimer(tp)
|
|
|
|
struct pppTimer *tp;
|
|
|
|
{
|
|
|
|
struct pppTimer *t, *pt;
|
|
|
|
u_long ticks = 0;
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifdef SIGALRM
|
|
|
|
int omask;
|
|
|
|
omask = sigblock(sigmask(SIGALRM));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (tp->state != TIMER_STOPPED) {
|
|
|
|
StopTimerNoBlock(tp);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
if (tp->load == 0) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
logprintf("timer %x has 0 load!\n", tp);
|
|
|
|
#endif
|
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) {
|
|
|
|
#ifdef DEBUG
|
1995-02-26 12:18:08 +00:00
|
|
|
logprintf("StartTimer: %x(%d): ticks: %d, rest: %d\n", t, t->state, ticks, t->rest);
|
1995-01-31 06:29:58 +00:00
|
|
|
#endif
|
|
|
|
if (ticks + t->rest >= tp->load)
|
|
|
|
break;
|
|
|
|
ticks += t->rest;
|
|
|
|
pt = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
tp->state = TIMER_RUNNING;
|
|
|
|
tp->rest = tp->load - ticks;
|
|
|
|
#ifdef DEBUG
|
|
|
|
logprintf("Inserting %x before %x, rest = %d\n", tp, t, tp->rest);
|
|
|
|
#endif
|
|
|
|
/* 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
|
|
|
|
|
|
|
#ifdef SIGALRM
|
|
|
|
sigsetmask(omask);
|
|
|
|
#endif
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1995-02-26 12:18:08 +00:00
|
|
|
StopTimerNoBlock(tp)
|
1995-01-31 06:29:58 +00:00
|
|
|
struct pppTimer *tp;
|
|
|
|
{
|
|
|
|
struct pppTimer *t, *pt;
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
/*
|
|
|
|
* A Running Timer should be removing TimerList,
|
|
|
|
* But STOPPED/EXPIRED is already removing TimerList.
|
|
|
|
* So just marked as TIMER_STOPPED.
|
|
|
|
* Do not change tp->enext!! (Might be Called by expired proc)
|
|
|
|
*/
|
|
|
|
#ifdef DEBUG
|
|
|
|
logprintf("StopTimer: %x, next = %x state=%x\n", tp, tp->next, tp->state);
|
|
|
|
#endif
|
1995-01-31 06:29:58 +00:00
|
|
|
if (tp->state != TIMER_RUNNING) {
|
1995-02-26 12:18:08 +00:00
|
|
|
tp->next = NULL;
|
|
|
|
tp->state = TIMER_STOPPED;
|
1995-01-31 06:29:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pt = NULL;
|
1995-02-26 12:18:08 +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;
|
1995-03-11 15:18:55 +00:00
|
|
|
if ( TimerList == NULL ) /* Last one ? */
|
|
|
|
TermTimerService(); /* Terminate Timer Service */
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
if (t->next)
|
|
|
|
t->next->rest += tp->rest;
|
1995-02-26 12:18:08 +00:00
|
|
|
} else {
|
|
|
|
logprintf("Oops, timer not found!!\n");
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
tp->next = NULL;
|
|
|
|
tp->state = TIMER_STOPPED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TimerService()
|
|
|
|
{
|
|
|
|
struct pppTimer *tp, *exp, *wt;
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
ShowTimers();
|
|
|
|
#endif
|
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) {
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
#ifdef DEBUG
|
|
|
|
logprintf("Add %x to exp\n", tp);
|
|
|
|
#endif
|
|
|
|
tp = wt;
|
|
|
|
} while (tp && (tp->rest == 0));
|
|
|
|
|
|
|
|
TimerList = tp;
|
1995-03-11 15:18:55 +00:00
|
|
|
if ( TimerList == NULL ) /* No timers ? */
|
|
|
|
TermTimerService(); /* Terminate Timer Service */
|
1995-01-31 06:29:58 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
logprintf("TimerService: next is %x(%d)\n",
|
|
|
|
TimerList, TimerList? TimerList->rest : 0);
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Process all expired timers.
|
|
|
|
*/
|
|
|
|
while (exp) {
|
|
|
|
#ifdef notdef
|
|
|
|
StopTimer(exp);
|
|
|
|
#endif
|
|
|
|
if (exp->func)
|
|
|
|
(*exp->func)(exp->arg);
|
1995-05-30 03:57:47 +00:00
|
|
|
/*
|
1995-02-26 12:18:08 +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
|
|
|
|
ShowTimers()
|
|
|
|
{
|
|
|
|
struct pppTimer *pt;
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
logprintf("---- Begin of Timer Service List---\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
for (pt = TimerList; pt; pt = pt->next)
|
1995-02-26 12:18:08 +00:00
|
|
|
logprintf("%x: load = %d, rest = %d, state =%x\n",
|
|
|
|
pt, pt->load, pt->rest, pt->state);
|
|
|
|
logprintf("---- End of Timer Service List ---\n");
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
|
|
|
|
#ifdef SIGALRM
|
1996-01-10 21:28:04 +00:00
|
|
|
u_int
|
|
|
|
sleep( u_int sec )
|
1995-02-26 12:18:08 +00:00
|
|
|
{
|
|
|
|
struct timeval to,st,et;
|
|
|
|
long sld, nwd, std;
|
|
|
|
|
|
|
|
gettimeofday( &st, NULL );
|
|
|
|
to.tv_sec = sec;
|
|
|
|
to.tv_usec = 0;
|
1995-05-30 03:57:47 +00:00
|
|
|
std = st.tv_sec * 1000000 + st.tv_usec;
|
1995-02-26 12:18:08 +00:00
|
|
|
for (;;) {
|
|
|
|
if ( select ( 0, NULL, NULL, NULL, &to) == 0 ||
|
|
|
|
errno != EINTR ) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
gettimeofday( &et, NULL );
|
|
|
|
sld = to.tv_sec * 1000000 + to.tv_sec;
|
|
|
|
nwd = et.tv_sec * 1000000 + et.tv_usec - std;
|
1995-05-30 03:57:47 +00:00
|
|
|
if ( sld > nwd )
|
1995-02-26 12:18:08 +00:00
|
|
|
sld -= nwd;
|
|
|
|
else
|
|
|
|
sld = 1; /* Avoid both tv_sec/usec is 0 */
|
|
|
|
|
|
|
|
/* Calculate timeout value for select */
|
|
|
|
to.tv_sec = sld / 1000000;
|
|
|
|
to.tv_usec = sld % 1000000;
|
|
|
|
}
|
|
|
|
}
|
1996-01-10 21:28:04 +00:00
|
|
|
return (0L);
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void usleep( u_int usec)
|
|
|
|
{
|
|
|
|
struct timeval to,st,et;
|
|
|
|
long sld, nwd, std;
|
|
|
|
|
|
|
|
gettimeofday( &st, NULL );
|
|
|
|
to.tv_sec = 0;
|
|
|
|
to.tv_usec = usec;
|
1995-05-30 03:57:47 +00:00
|
|
|
std = st.tv_sec * 1000000 + st.tv_usec;
|
1995-02-26 12:18:08 +00:00
|
|
|
for (;;) {
|
|
|
|
if ( select ( 0, NULL, NULL, NULL, &to) == 0 ||
|
|
|
|
errno != EINTR ) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
gettimeofday( &et, NULL );
|
|
|
|
sld = to.tv_sec * 1000000 + to.tv_sec;
|
|
|
|
nwd = et.tv_sec * 1000000 + et.tv_usec - std;
|
1995-05-30 03:57:47 +00:00
|
|
|
if ( sld > nwd )
|
1995-02-26 12:18:08 +00:00
|
|
|
sld -= nwd;
|
|
|
|
else
|
|
|
|
sld = 1; /* Avoid both tv_sec/usec is 0 */
|
|
|
|
|
|
|
|
/* Calculate timeout value for select */
|
|
|
|
to.tv_sec = sld / 1000000;
|
|
|
|
to.tv_usec = sld % 1000000;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-03-11 15:18:55 +00:00
|
|
|
|
|
|
|
void InitTimerService( void ) {
|
|
|
|
struct itimerval itimer;
|
|
|
|
|
1997-03-13 12:45:35 +00:00
|
|
|
pending_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-05-10 01:22:19 +00:00
|
|
|
if (setitimer(ITIMER_REAL, &itimer, NULL) == -1) {
|
|
|
|
logprintf("Unable to set itimer.\n");
|
|
|
|
}
|
1995-03-11 15:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TermTimerService( void ) {
|
|
|
|
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-05-10 01:22:19 +00:00
|
|
|
if (setitimer(ITIMER_REAL, &itimer, NULL) == -1) {
|
|
|
|
logprintf("Unable to set itimer.\n");
|
|
|
|
}
|
1997-03-13 12:45:35 +00:00
|
|
|
pending_signal(SIGALRM, SIG_IGN);
|
1995-03-11 15:18:55 +00:00
|
|
|
}
|
1995-02-26 12:18:08 +00:00
|
|
|
#endif
|