1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
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)
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
17 *
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
28 * SUCH DAMAGE.
29 */
30
31 #include <errno.h>
32 #include <signal.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <sys/time.h>
37 #include <termios.h>
38
39 #include "log.h"
40 #include "sig.h"
41 #include "timer.h"
42 #include "descriptor.h"
43 #include "prompt.h"
44
45
46 #define RESTVAL(t) \
47 ((t).it_value.tv_sec * SECTICKS + (t).it_value.tv_usec / TICKUNIT + \
48 ((((t).it_value.tv_usec % TICKUNIT) >= (TICKUNIT >> 1)) ? 1 : 0))
49
50 static struct pppTimer *TimerList = NULL, *ExpiredList = NULL;
51
52 static void StopTimerNoBlock(struct pppTimer *);
53
54 static const char *
tState2Nam(u_int state)55 tState2Nam(u_int state)
56 {
57 static const char * const StateNames[] = { "stopped", "running", "expired" };
58
59 if (state >= sizeof StateNames / sizeof StateNames[0])
60 return "unknown";
61 return StateNames[state];
62 }
63
64 void
timer_Stop(struct pppTimer * tp)65 timer_Stop(struct pppTimer *tp)
66 {
67 sigset_t mask, omask;
68
69 sigemptyset(&mask);
70 sigaddset(&mask, SIGALRM);
71 sigprocmask(SIG_BLOCK, &mask, &omask);
72 StopTimerNoBlock(tp);
73 sigprocmask(SIG_SETMASK, &omask, NULL);
74 }
75
76 void
timer_Start(struct pppTimer * tp)77 timer_Start(struct pppTimer *tp)
78 {
79 struct itimerval itimer;
80 struct pppTimer *t, *pt;
81 u_long ticks = 0;
82 sigset_t mask, omask;
83 int i;
84
85 sigemptyset(&mask);
86 sigaddset(&mask, SIGALRM);
87 sigprocmask(SIG_BLOCK, &mask, &omask);
88
89 if (tp->state != TIMER_STOPPED)
90 StopTimerNoBlock(tp);
91
92 if (tp->load == 0) {
93 log_Printf(LogTIMER, "%s timer has 0 load!\n", tp->name);
94 sigprocmask(SIG_SETMASK, &omask, NULL);
95 return;
96 }
97
98 /*
99 * We just need to insert tp in the correct relative place. We don't
100 * need to adjust TimerList->rest (yet).
101 */
102 if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0)
103 ticks = RESTVAL(itimer) - TimerList->rest;
104
105 pt = NULL;
106 for (i = 0, t = TimerList; t; t = t->next, i++) {
107 if (ticks + t->rest >= tp->load)
108 break;
109 ticks += t->rest;
110 pt = t;
111 }
112
113 tp->state = TIMER_RUNNING;
114 tp->rest = tp->load - ticks;
115
116 if (t)
117 log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%d] before %s "
118 "timer[%d], delta = %ld\n", tp->name, i, t->name, i + 1,
119 tp->rest);
120 else
121 log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%d]\n", tp->name, i);
122
123 /* Insert given *tp just before *t */
124 tp->next = t;
125 if (pt) {
126 pt->next = tp;
127 } else {
128 TimerList = tp;
129 timer_InitService(t != NULL); /* [re]Start the Timer Service */
130 }
131 if (t)
132 t->rest -= tp->rest;
133
134 sigprocmask(SIG_SETMASK, &omask, NULL);
135 }
136
137 static void
StopTimerNoBlock(struct pppTimer * tp)138 StopTimerNoBlock(struct pppTimer *tp)
139 {
140 struct itimerval itimer;
141 struct pppTimer *t, *pt;
142
143 /*
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.
147 */
148
149 if (tp->state == TIMER_STOPPED)
150 return;
151
152 pt = NULL;
153 for (t = TimerList; t != tp && t != NULL; t = t->next)
154 pt = t;
155
156 if (t) {
157 if (pt)
158 pt->next = t->next;
159 else {
160 TimerList = t->next;
161 if (TimerList == NULL) /* Last one ? */
162 timer_TermService(); /* Terminate Timer Service */
163 }
164 if (t->next) {
165 if (!pt && getitimer(ITIMER_REAL, &itimer) == 0)
166 t->next->rest += RESTVAL(itimer); /* t (tp) was the first in the list */
167 else
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);
171 }
172 } else {
173 /* Search for any pending expired timers */
174 pt = NULL;
175 for (t = ExpiredList; t != tp && t != NULL; t = t->enext)
176 pt = t;
177
178 if (t) {
179 if (pt)
180 pt->enext = t->enext;
181 else
182 ExpiredList = t->enext;
183 } else if (tp->state == TIMER_RUNNING)
184 log_Printf(LogERROR, "Oops, %s timer not found!!\n", tp->name);
185 }
186
187 tp->next = tp->enext = NULL;
188 tp->state = TIMER_STOPPED;
189 }
190
191 static void
TimerService(void)192 TimerService(void)
193 {
194 struct pppTimer *tp, *exp, *next;
195
196 if (log_IsKept(LogTIMER)) {
197 static time_t t; /* Only show timers globally every second */
198 time_t n = time(NULL);
199
200 if (n > t)
201 timer_Show(LogTIMER, NULL);
202 t = n;
203 }
204
205 tp = TimerList;
206 if (tp) {
207 tp->rest = 0;
208
209 /* Multiple timers might expire at once. Create a list of expired timers */
210 exp = NULL;
211 do {
212 tp->state = TIMER_EXPIRED;
213 next = tp->next;
214 tp->enext = exp;
215 exp = tp;
216 tp = next;
217 } while (tp && tp->rest == 0);
218
219 TimerList = tp;
220 if (TimerList != NULL) /* Any timers remaining ? */
221 timer_InitService(1); /* Restart the Timer Service */
222 else
223 timer_TermService(); /* Stop the Timer Service */
224
225 /* Process all expired timers */
226 while (exp) {
227 ExpiredList = exp->enext;
228 exp->enext = NULL;
229 if (exp->func)
230 (*exp->func)(exp->arg);
231 exp = ExpiredList;
232 }
233 }
234 }
235
236 void
timer_Show(int LogLevel,struct prompt * prompt)237 timer_Show(int LogLevel, struct prompt *prompt)
238 {
239 struct itimerval itimer;
240 struct pppTimer *pt;
241 long rest;
242 int i;
243
244 /*
245 * Adjust the base time so that the deltas reflect what's really
246 * happening. Changing TimerList->rest might cause it to become zero
247 * (if getitimer() returns a value close to zero), and the
248 * timer_InitService() call will call setitimer() with zero it_value,
249 * stopping the itimer... so be careful!
250 */
251 if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0)
252 rest = RESTVAL(itimer) - TimerList->rest;
253 else
254 rest = 0;
255
256 #define SECS(val) ((val) / SECTICKS)
257 #define HSECS(val) (((val) % SECTICKS) * 100 / SECTICKS)
258 #define DISP \
259 "%s timer[%d]: freq = %ld.%02lds, next = %lu.%02lus, state = %s\n", \
260 pt->name, i, SECS(pt->load), HSECS(pt->load), SECS(rest), \
261 HSECS(rest), tState2Nam(pt->state)
262
263 if (!prompt)
264 log_Printf(LogLevel, "---- Begin of Timer Service List---\n");
265
266 for (pt = TimerList, i = 0; pt; pt = pt->next, i++) {
267 rest += pt->rest;
268 if (prompt)
269 prompt_Printf(prompt, DISP);
270 else
271 log_Printf(LogLevel, DISP);
272 }
273
274 if (!prompt)
275 log_Printf(LogLevel, "---- End of Timer Service List ---\n");
276 }
277
278 void
timer_InitService(int restart)279 timer_InitService(int restart)
280 {
281 struct itimerval itimer;
282
283 if (TimerList) {
284 if (!restart)
285 sig_signal(SIGALRM, (void (*)(int))TimerService);
286 itimer.it_interval.tv_sec = 0;
287 itimer.it_interval.tv_usec = 0;
288 itimer.it_value.tv_sec = TimerList->rest / SECTICKS;
289 itimer.it_value.tv_usec = (TimerList->rest % SECTICKS) * TICKUNIT;
290 if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
291 log_Printf(LogERROR, "Unable to set itimer (%s)\n", strerror(errno));
292 }
293 }
294
295 void
timer_TermService(void)296 timer_TermService(void)
297 {
298 struct itimerval itimer;
299
300 itimer.it_interval.tv_usec = itimer.it_interval.tv_sec = 0;
301 itimer.it_value.tv_usec = itimer.it_value.tv_sec = 0;
302 if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
303 log_Printf(LogERROR, "Unable to set itimer (%s)\n", strerror(errno));
304 sig_signal(SIGALRM, SIG_IGN);
305 }
306