2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1996 - 2001, 2009 Brian Somers <brian@Awfulhak.org>
5 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
6 * Internet Initiative Japan, Inc (IIJ)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 #include "descriptor.h"
49 ((t).it_value.tv_sec * SECTICKS + (t).it_value.tv_usec / TICKUNIT + \
50 ((((t).it_value.tv_usec % TICKUNIT) >= (TICKUNIT >> 1)) ? 1 : 0))
52 static struct pppTimer *TimerList = NULL, *ExpiredList = NULL;
54 static void StopTimerNoBlock(struct pppTimer *);
57 tState2Nam(u_int state)
59 static const char * const StateNames[] = { "stopped", "running", "expired" };
61 if (state >= sizeof StateNames / sizeof StateNames[0])
63 return StateNames[state];
67 timer_Stop(struct pppTimer *tp)
72 sigaddset(&mask, SIGALRM);
73 sigprocmask(SIG_BLOCK, &mask, &omask);
75 sigprocmask(SIG_SETMASK, &omask, NULL);
79 timer_Start(struct pppTimer *tp)
81 struct itimerval itimer;
82 struct pppTimer *t, *pt;
87 sigaddset(&mask, SIGALRM);
88 sigprocmask(SIG_BLOCK, &mask, &omask);
90 if (tp->state != TIMER_STOPPED)
94 log_Printf(LogTIMER, "%s timer[%p] has 0 load!\n", tp->name, tp);
95 sigprocmask(SIG_SETMASK, &omask, NULL);
100 * We just need to insert tp in the correct relative place. We don't
101 * need to adjust TimerList->rest (yet).
103 if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0)
104 ticks = RESTVAL(itimer) - TimerList->rest;
107 for (t = TimerList; t; t = t->next) {
108 if (ticks + t->rest >= tp->load)
114 tp->state = TIMER_RUNNING;
115 tp->rest = tp->load - ticks;
118 log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p] before %s "
119 "timer[%p], delta = %ld\n", tp->name, tp, t->name, t, tp->rest);
121 log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p]\n", tp->name, tp);
123 /* Insert given *tp just before *t */
129 timer_InitService(t != NULL); /* [re]Start the Timer Service */
134 sigprocmask(SIG_SETMASK, &omask, NULL);
138 StopTimerNoBlock(struct pppTimer *tp)
140 struct itimerval itimer;
141 struct pppTimer *t, *pt;
144 * A RUNNING timer must be removed from TimerList (->next list).
145 * A STOPPED timer isn't in any list, but may have a bogus [e]next field.
146 * An EXPIRED timer is in the ->enext list.
149 if (tp->state == TIMER_STOPPED)
153 for (t = TimerList; t != tp && t != NULL; t = t->next)
161 if (TimerList == NULL) /* Last one ? */
162 timer_TermService(); /* Terminate Timer Service */
165 if (!pt && getitimer(ITIMER_REAL, &itimer) == 0)
166 t->next->rest += RESTVAL(itimer); /* t (tp) was the first in the list */
168 t->next->rest += t->rest;
169 if (!pt && t->next->rest > 0) /* 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;
244 * Adjust the base time so that the deltas reflect what's really
245 * happening. Changing TimerList->rest might cause it to become zero
246 * (if getitimer() returns a value close to zero), and the
247 * timer_InitService() call will call setitimer() with zero it_value,
248 * stopping the itimer... so be careful!
250 if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0)
251 rest = RESTVAL(itimer) - TimerList->rest;
255 #define SECS(val) ((val) / SECTICKS)
256 #define HSECS(val) (((val) % SECTICKS) * 100 / SECTICKS)
258 "%s timer[%p]: freq = %ld.%02lds, next = %lu.%02lus, state = %s\n", \
259 pt->name, pt, SECS(pt->load), HSECS(pt->load), SECS(rest), \
260 HSECS(rest), tState2Nam(pt->state)
263 log_Printf(LogLevel, "---- Begin of Timer Service List---\n");
265 for (pt = TimerList; pt; pt = pt->next) {
268 prompt_Printf(prompt, DISP);
270 log_Printf(LogLevel, DISP);
274 log_Printf(LogLevel, "---- End of Timer Service List ---\n");
278 timer_InitService(int restart)
280 struct itimerval itimer;
284 sig_signal(SIGALRM, (void (*)(int))TimerService);
285 itimer.it_interval.tv_sec = 0;
286 itimer.it_interval.tv_usec = 0;
287 itimer.it_value.tv_sec = TimerList->rest / SECTICKS;
288 itimer.it_value.tv_usec = (TimerList->rest % SECTICKS) * TICKUNIT;
289 if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
290 log_Printf(LogERROR, "Unable to set itimer (%s)\n", strerror(errno));
295 timer_TermService(void)
297 struct itimerval itimer;
299 itimer.it_interval.tv_usec = itimer.it_interval.tv_sec = 0;
300 itimer.it_value.tv_usec = itimer.it_value.tv_sec = 0;
301 if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
302 log_Printf(LogERROR, "Unable to set itimer (%s)\n", strerror(errno));
303 sig_signal(SIGALRM, SIG_IGN);