2001-06-13 21:52:19 +00:00
|
|
|
/*-
|
2009-05-26 07:32:08 +00:00
|
|
|
* Copyright (c) 1996 - 2001, 2009 Brian Somers <brian@Awfulhak.org>
|
2001-06-13 21:52:19 +00:00
|
|
|
* based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
|
|
|
|
* Internet Initiative Japan, Inc (IIJ)
|
|
|
|
* All rights reserved.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
2001-06-13 21:52:19 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
2001-06-13 21:52:19 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
1999-08-28 01:35:59 +00:00
|
|
|
* $FreeBSD$
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1998-12-14 19:24:30 +00:00
|
|
|
#include <errno.h>
|
1998-04-03 19:21:56 +00:00
|
|
|
#include <signal.h>
|
2002-08-27 20:11:58 +00:00
|
|
|
#include <stdarg.h>
|
1998-04-03 19:21:56 +00:00
|
|
|
#include <stdio.h>
|
1999-05-12 19:10:22 +00:00
|
|
|
#include <string.h>
|
2002-05-14 17:50:25 +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 "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
|
|
|
|
2000-03-14 01:46:34 +00:00
|
|
|
|
|
|
|
#define RESTVAL(t) \
|
|
|
|
((t).it_value.tv_sec * SECTICKS + (t).it_value.tv_usec / TICKUNIT + \
|
|
|
|
((((t).it_value.tv_usec % TICKUNIT) >= (TICKUNIT >> 1)) ? 1 : 0))
|
|
|
|
|
1999-10-15 20:44:34 +00:00
|
|
|
static struct pppTimer *TimerList = NULL, *ExpiredList = NULL;
|
1997-03-13 12:45:35 +00:00
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void StopTimerNoBlock(struct pppTimer *);
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1998-04-03 19:21:56 +00:00
|
|
|
static const char *
|
|
|
|
tState2Nam(u_int state)
|
|
|
|
{
|
1999-12-27 11:54:57 +00:00
|
|
|
static const char * const StateNames[] = { "stopped", "running", "expired" };
|
1998-04-03 19:21:56 +00:00
|
|
|
|
|
|
|
if (state >= sizeof StateNames / sizeof StateNames[0])
|
|
|
|
return "unknown";
|
|
|
|
return StateNames[state];
|
|
|
|
}
|
|
|
|
|
1995-02-26 12:18:08 +00:00
|
|
|
void
|
1999-05-09 20:02:29 +00:00
|
|
|
timer_Stop(struct pppTimer *tp)
|
1995-02-26 12:18:08 +00:00
|
|
|
{
|
2001-09-13 10:03:30 +00:00
|
|
|
sigset_t mask, omask;
|
1997-08-25 00:29:32 +00:00
|
|
|
|
2001-09-13 10:03:30 +00:00
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGALRM);
|
|
|
|
sigprocmask(SIG_BLOCK, &mask, &omask);
|
1995-02-26 12:18:08 +00:00
|
|
|
StopTimerNoBlock(tp);
|
2001-09-13 10:03:30 +00:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
1997-06-09 03:27:43 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
void
|
1999-05-09 20:02:29 +00:00
|
|
|
timer_Start(struct pppTimer *tp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1999-11-19 23:21:13 +00:00
|
|
|
struct itimerval itimer;
|
1995-01-31 06:29:58 +00:00
|
|
|
struct pppTimer *t, *pt;
|
|
|
|
u_long ticks = 0;
|
2001-09-13 10:03:30 +00:00
|
|
|
sigset_t mask, omask;
|
1997-08-25 00:29:32 +00:00
|
|
|
|
2001-09-13 10:03:30 +00:00
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGALRM);
|
|
|
|
sigprocmask(SIG_BLOCK, &mask, &omask);
|
1995-02-26 12:18:08 +00:00
|
|
|
|
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);
|
2001-09-13 10:03:30 +00:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
1995-01-31 06:29:58 +00:00
|
|
|
return;
|
|
|
|
}
|
1999-11-19 23:21:13 +00:00
|
|
|
|
2009-05-26 07:32:08 +00:00
|
|
|
/*
|
|
|
|
* We just need to insert tp in the correct relative place. We don't
|
|
|
|
* need to adjust TimerList->rest (yet).
|
|
|
|
*/
|
1999-11-19 23:21:13 +00:00
|
|
|
if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0)
|
2009-05-26 07:32:08 +00:00
|
|
|
ticks = RESTVAL(itimer) - TimerList->rest;
|
1999-11-19 23:21:13 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
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 {
|
1995-01-31 06:29:58 +00:00
|
|
|
TimerList = tp;
|
1999-11-19 23:21:13 +00:00
|
|
|
timer_InitService(t != NULL); /* [re]Start the Timer Service */
|
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
|
|
|
|
2001-09-13 10:03:30 +00:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, NULL);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static void
|
1999-05-09 20:02:29 +00:00
|
|
|
StopTimerNoBlock(struct pppTimer *tp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
2009-05-26 07:32:08 +00:00
|
|
|
struct itimerval itimer;
|
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
|
|
|
*/
|
1999-10-15 20:44:34 +00:00
|
|
|
|
|
|
|
if (tp->state == TIMER_STOPPED)
|
1995-01-31 06:29:58 +00:00
|
|
|
return;
|
1999-10-15 20:44:34 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
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;
|
1999-10-15 20:44:34 +00:00
|
|
|
|
1995-01-31 06:29:58 +00:00
|
|
|
if (t) {
|
2000-03-14 01:46:31 +00:00
|
|
|
if (pt)
|
1995-01-31 06:29:58 +00:00
|
|
|
pt->next = t->next;
|
2000-03-14 01:46:31 +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
|
|
|
}
|
2000-03-14 01:46:31 +00:00
|
|
|
if (t->next) {
|
2009-05-26 07:32:08 +00:00
|
|
|
if (!pt && getitimer(ITIMER_REAL, &itimer) == 0)
|
|
|
|
t->next->rest += RESTVAL(itimer); /* t (tp) was the first in the list */
|
|
|
|
else
|
|
|
|
t->next->rest += t->rest;
|
|
|
|
if (!pt && t->next->rest > 0) /* t->next is now the first in the list */
|
2000-03-14 01:46:31 +00:00
|
|
|
timer_InitService(1);
|
|
|
|
}
|
1999-10-15 20:44:34 +00:00
|
|
|
} else {
|
|
|
|
/* Search for any pending expired timers */
|
|
|
|
pt = NULL;
|
|
|
|
for (t = ExpiredList; t != tp && t != NULL; t = t->enext)
|
|
|
|
pt = t;
|
|
|
|
|
|
|
|
if (t) {
|
|
|
|
if (pt)
|
|
|
|
pt->enext = t->enext;
|
|
|
|
else
|
|
|
|
ExpiredList = t->enext;
|
|
|
|
} else if (tp->state == TIMER_RUNNING)
|
|
|
|
log_Printf(LogERROR, "Oops, %s timer not found!!\n", tp->name);
|
|
|
|
}
|
1997-06-09 03:27:43 +00:00
|
|
|
|
1999-10-15 20:44:34 +00:00
|
|
|
tp->next = tp->enext = NULL;
|
1995-01-31 06:29:58 +00:00
|
|
|
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
|
|
|
{
|
1998-12-14 19:24:30 +00:00
|
|
|
struct pppTimer *tp, *exp, *next;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
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;
|
|
|
|
}
|
1998-12-14 19:24:30 +00:00
|
|
|
|
1996-01-10 21:28:04 +00:00
|
|
|
tp = TimerList;
|
|
|
|
if (tp) {
|
1998-12-14 19:24:30 +00:00
|
|
|
tp->rest = 0;
|
|
|
|
|
|
|
|
/* Multiple timers might expire at once. Create a list of expired timers */
|
|
|
|
exp = NULL;
|
|
|
|
do {
|
|
|
|
tp->state = TIMER_EXPIRED;
|
|
|
|
next = tp->next;
|
|
|
|
tp->enext = exp;
|
|
|
|
exp = tp;
|
|
|
|
tp = next;
|
|
|
|
} while (tp && tp->rest == 0);
|
2002-06-15 08:03:30 +00:00
|
|
|
|
1998-12-14 19:24:30 +00:00
|
|
|
TimerList = tp;
|
|
|
|
if (TimerList != NULL) /* Any timers remaining ? */
|
|
|
|
timer_InitService(1); /* Restart the Timer Service */
|
|
|
|
else
|
|
|
|
timer_TermService(); /* Stop the Timer Service */
|
2002-06-15 08:03:30 +00:00
|
|
|
|
1998-12-14 19:24:30 +00:00
|
|
|
/* Process all expired timers */
|
|
|
|
while (exp) {
|
1999-10-15 20:44:34 +00:00
|
|
|
ExpiredList = exp->enext;
|
1998-12-14 19:24:30 +00:00
|
|
|
exp->enext = NULL;
|
|
|
|
if (exp->func)
|
|
|
|
(*exp->func)(exp->arg);
|
1999-10-15 20:44:34 +00:00
|
|
|
exp = ExpiredList;
|
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
|
|
|
{
|
1999-05-12 19:10:22 +00:00
|
|
|
struct itimerval itimer;
|
1995-01-31 06:29:58 +00:00
|
|
|
struct pppTimer *pt;
|
2009-05-26 07:32:08 +00:00
|
|
|
long rest;
|
1999-05-12 19:10:22 +00:00
|
|
|
|
2009-05-26 07:32:08 +00:00
|
|
|
/*
|
|
|
|
* Adjust the base time so that the deltas reflect what's really
|
|
|
|
* happening. Changing TimerList->rest might cause it to become zero
|
|
|
|
* (if getitimer() returns a value close to zero), and the
|
|
|
|
* timer_InitService() call will call setitimer() with zero it_value,
|
|
|
|
* stopping the itimer... so be careful!
|
|
|
|
*/
|
1999-05-12 19:10:22 +00:00
|
|
|
if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0)
|
2009-05-26 07:32:08 +00:00
|
|
|
rest = RESTVAL(itimer) - TimerList->rest;
|
|
|
|
else
|
|
|
|
rest = 0;
|
1998-04-03 19:21:56 +00:00
|
|
|
|
|
|
|
#define SECS(val) ((val) / SECTICKS)
|
|
|
|
#define HSECS(val) (((val) % SECTICKS) * 100 / SECTICKS)
|
|
|
|
#define DISP \
|
1999-05-12 19:10:22 +00:00
|
|
|
"%s timer[%p]: freq = %ld.%02lds, next = %lu.%02lus, 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
|
|
|
}
|
|
|
|
|
2002-06-15 08:03:30 +00:00
|
|
|
void
|
1998-12-14 19:24:30 +00:00
|
|
|
timer_InitService(int restart)
|
1997-08-25 00:29:32 +00:00
|
|
|
{
|
1995-03-11 15:18:55 +00:00
|
|
|
struct itimerval itimer;
|
|
|
|
|
1998-12-14 19:24:30 +00:00
|
|
|
if (TimerList) {
|
|
|
|
if (!restart)
|
|
|
|
sig_signal(SIGALRM, (void (*)(int))TimerService);
|
|
|
|
itimer.it_interval.tv_sec = 0;
|
|
|
|
itimer.it_interval.tv_usec = 0;
|
|
|
|
itimer.it_value.tv_sec = TimerList->rest / SECTICKS;
|
|
|
|
itimer.it_value.tv_usec = (TimerList->rest % SECTICKS) * TICKUNIT;
|
|
|
|
if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
|
1999-05-12 19:10:22 +00:00
|
|
|
log_Printf(LogERROR, "Unable to set itimer (%s)\n", strerror(errno));
|
1998-12-14 19:24:30 +00:00
|
|
|
}
|
1995-03-11 15:18:55 +00:00
|
|
|
}
|
|
|
|
|
2002-06-15 08:03:30 +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)
|
1999-05-12 19:10:22 +00:00
|
|
|
log_Printf(LogERROR, "Unable to set itimer (%s)\n", strerror(errno));
|
1998-05-01 19:26:12 +00:00
|
|
|
sig_signal(SIGALRM, SIG_IGN);
|
1995-03-11 15:18:55 +00:00
|
|
|
}
|