2 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4 * Internet Initiative Japan, Inc (IIJ)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/usr.sbin/ppp/timer.c,v 1.38.2.3 2002/09/01 02:12:32 brian Exp $
29 * $DragonFly: src/usr.sbin/ppp/timer.c,v 1.3 2008/05/19 10:19:49 corecode Exp $
32 #include <sys/select.h>
45 #include "descriptor.h"
50 ((t).it_value.tv_sec * SECTICKS + (t).it_value.tv_usec / TICKUNIT + \
51 ((((t).it_value.tv_usec % TICKUNIT) >= (TICKUNIT >> 1)) ? 1 : 0))
53 static struct pppTimer *TimerList = NULL, *ExpiredList = NULL;
55 static void StopTimerNoBlock(struct pppTimer *);
58 tState2Nam(u_int state)
60 static const char * const StateNames[] = { "stopped", "running", "expired" };
62 if (state >= sizeof StateNames / sizeof StateNames[0])
64 return StateNames[state];
68 timer_Stop(struct pppTimer *tp)
73 sigaddset(&mask, SIGALRM);
74 sigprocmask(SIG_BLOCK, &mask, &omask);
76 sigprocmask(SIG_SETMASK, &omask, NULL);
80 timer_Start(struct pppTimer *tp)
82 struct itimerval itimer;
83 struct pppTimer *t, *pt;
88 sigaddset(&mask, SIGALRM);
89 sigprocmask(SIG_BLOCK, &mask, &omask);
91 if (tp->state != TIMER_STOPPED)
95 log_Printf(LogTIMER, "%s timer[%p] has 0 load!\n", tp->name, tp);
96 sigprocmask(SIG_SETMASK, &omask, NULL);
100 /* Adjust our first delta so that it reflects what's really happening */
101 if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0)
102 TimerList->rest = RESTVAL(itimer);
105 for (t = TimerList; t; t = t->next) {
106 if (ticks + t->rest >= tp->load)
112 tp->state = TIMER_RUNNING;
113 tp->rest = tp->load - ticks;
116 log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p] before %s "
117 "timer[%p], delta = %ld\n", tp->name, tp, t->name, t, tp->rest);
119 log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p]\n", tp->name, tp);
121 /* Insert given *tp just before *t */
127 timer_InitService(t != NULL); /* [re]Start the Timer Service */
132 sigprocmask(SIG_SETMASK, &omask, NULL);
136 StopTimerNoBlock(struct pppTimer *tp)
138 struct pppTimer *t, *pt;
141 * A RUNNING timer must be removed from TimerList (->next list).
142 * A STOPPED timer isn't in any list, but may have a bogus [e]next field.
143 * An EXPIRED timer is in the ->enext list.
146 if (tp->state == TIMER_STOPPED)
150 for (t = TimerList; t != tp && t != NULL; t = t->next)
158 if (TimerList == NULL) /* Last one ? */
159 timer_TermService(); /* Terminate Timer Service */
162 if (!pt) { /* t (tp) was the first in the list */
163 struct itimerval itimer;
165 if (getitimer(ITIMER_REAL, &itimer) == 0)
166 t->rest = RESTVAL(itimer);
168 t->next->rest += t->rest;
169 if (!pt) /* t->next is now the first in the list */
170 timer_InitService(1);
173 /* Search for any pending expired timers */
175 for (t = ExpiredList; t != tp && t != NULL; t = t->enext)
180 pt->enext = t->enext;
182 ExpiredList = t->enext;
183 } else if (tp->state == TIMER_RUNNING)
184 log_Printf(LogERROR, "Oops, %s timer not found!!\n", tp->name);
187 tp->next = tp->enext = NULL;
188 tp->state = TIMER_STOPPED;
194 struct pppTimer *tp, *exp, *next;
196 if (log_IsKept(LogTIMER)) {
197 static time_t t; /* Only show timers globally every second */
198 time_t n = time(NULL);
201 timer_Show(LogTIMER, NULL);
209 /* Multiple timers might expire at once. Create a list of expired timers */
212 tp->state = TIMER_EXPIRED;
217 } while (tp && tp->rest == 0);
220 if (TimerList != NULL) /* Any timers remaining ? */
221 timer_InitService(1); /* Restart the Timer Service */
223 timer_TermService(); /* Stop the Timer Service */
225 /* Process all expired timers */
227 ExpiredList = exp->enext;
230 (*exp->func)(exp->arg);
237 timer_Show(int LogLevel, struct prompt *prompt)
239 struct itimerval itimer;
243 /* Adjust our first delta so that it reflects what's really happening */
244 if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0)
245 TimerList->rest = RESTVAL(itimer);
247 #define SECS(val) ((val) / SECTICKS)
248 #define HSECS(val) (((val) % SECTICKS) * 100 / SECTICKS)
250 "%s timer[%p]: freq = %ld.%02lds, next = %lu.%02lus, state = %s\n", \
251 pt->name, pt, SECS(pt->load), HSECS(pt->load), SECS(rest), \
252 HSECS(rest), tState2Nam(pt->state)
255 log_Printf(LogLevel, "---- Begin of Timer Service List---\n");
257 for (pt = TimerList; pt; pt = pt->next) {
260 prompt_Printf(prompt, DISP);
262 log_Printf(LogLevel, DISP);
266 log_Printf(LogLevel, "---- End of Timer Service List ---\n");
270 timer_InitService(int restart)
272 struct itimerval itimer;
276 sig_signal(SIGALRM, (void (*)(int))TimerService);
277 itimer.it_interval.tv_sec = 0;
278 itimer.it_interval.tv_usec = 0;
279 itimer.it_value.tv_sec = TimerList->rest / SECTICKS;
280 itimer.it_value.tv_usec = (TimerList->rest % SECTICKS) * TICKUNIT;
281 if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
282 log_Printf(LogERROR, "Unable to set itimer (%s)\n", strerror(errno));
287 timer_TermService(void)
289 struct itimerval itimer;
291 itimer.it_interval.tv_usec = itimer.it_interval.tv_sec = 0;
292 itimer.it_value.tv_usec = itimer.it_value.tv_sec = 0;
293 if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
294 log_Printf(LogERROR, "Unable to set itimer (%s)\n", strerror(errno));
295 sig_signal(SIGALRM, SIG_IGN);