1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/*
* 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.
*
* $Id: timer.c,v 1.31 1998/06/27 14:18:11 brian Exp $
*
* TODO:
*/
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <sys/time.h>
#include <termios.h>
#include "log.h"
#include "sig.h"
#include "timer.h"
#include "descriptor.h"
#include "prompt.h"
static struct pppTimer *TimerList = NULL;
static void StopTimerNoBlock(struct pppTimer *);
static const char *
tState2Nam(u_int state)
{
static const char *StateNames[] = { "stopped", "running", "expired" };
if (state >= sizeof StateNames / sizeof StateNames[0])
return "unknown";
return StateNames[state];
}
void
timer_Stop(struct pppTimer * tp)
{
int omask;
omask = sigblock(sigmask(SIGALRM));
StopTimerNoBlock(tp);
sigsetmask(omask);
}
void
timer_Start(struct pppTimer * tp)
{
struct pppTimer *t, *pt;
u_long ticks = 0;
int omask;
omask = sigblock(sigmask(SIGALRM));
if (tp->state != TIMER_STOPPED)
StopTimerNoBlock(tp);
if (tp->load == 0) {
log_Printf(LogTIMER, "%s timer[%p] has 0 load!\n", tp->name, tp);
sigsetmask(omask);
return;
}
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;
if (t)
log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p] before %s "
"timer[%p], delta = %ld\n", tp->name, tp, t->name, t, tp->rest);
else
log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p]\n", tp->name, tp);
/* Insert given *tp just before *t */
tp->next = t;
if (pt) {
pt->next = tp;
} else {
TimerList = tp;
timer_InitService(0); /* Start the Timer Service */
}
if (t)
t->rest -= tp->rest;
sigsetmask(omask);
}
static void
StopTimerNoBlock(struct pppTimer * tp)
{
struct pppTimer *t, *pt;
/*
* 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.
*/
if (tp->state != TIMER_RUNNING) {
tp->next = NULL;
tp->state = TIMER_STOPPED;
return;
}
pt = NULL;
for (t = TimerList; t != tp && t != NULL; t = t->next)
pt = t;
if (t) {
if (pt) {
pt->next = t->next;
} else {
TimerList = t->next;
if (TimerList == NULL) /* Last one ? */
timer_TermService(); /* Terminate Timer Service */
}
if (t->next)
t->next->rest += tp->rest;
} else
log_Printf(LogERROR, "Oops, %s timer not found!!\n", tp->name);
tp->next = NULL;
tp->state = TIMER_STOPPED;
}
static void
TimerService(void)
{
struct pppTimer *tp, *exp, *next;
if (log_IsKept(LogTIMER)) {
static time_t t; /* Only show timers globally every second */
time_t n = time(NULL);
if (n > t)
timer_Show(LogTIMER, NULL);
t = n;
}
tp = TimerList;
if (tp) {
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);
TimerList = tp;
if (TimerList != NULL) /* Any timers remaining ? */
timer_InitService(1); /* Restart the Timer Service */
else
timer_TermService(); /* Stop the Timer Service */
/* Process all expired timers */
while (exp) {
next = exp->enext;
exp->enext = NULL;
if (exp->func)
(*exp->func)(exp->arg);
exp = next;
}
}
}
void
timer_Show(int LogLevel, struct prompt *prompt)
{
struct pppTimer *pt;
int rest = 0;
#define SECS(val) ((val) / SECTICKS)
#define HSECS(val) (((val) % SECTICKS) * 100 / SECTICKS)
#define DISP \
"%s timer[%p]: freq = %ld.%02lds, next = %d.%02ds, state = %s\n", \
pt->name, pt, SECS(pt->load), HSECS(pt->load), SECS(rest), \
HSECS(rest), tState2Nam(pt->state)
if (!prompt)
log_Printf(LogLevel, "---- Begin of Timer Service List---\n");
for (pt = TimerList; pt; pt = pt->next) {
rest += pt->rest;
if (prompt)
prompt_Printf(prompt, DISP);
else
log_Printf(LogLevel, DISP);
}
if (!prompt)
log_Printf(LogLevel, "---- End of Timer Service List ---\n");
}
void
timer_InitService(int restart)
{
struct itimerval itimer;
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)
log_Printf(LogERROR, "Unable to set itimer (%s)\n", sys_errlist[errno]);
}
}
void
timer_TermService(void)
{
struct itimerval itimer;
itimer.it_interval.tv_usec = itimer.it_interval.tv_sec = 0;
itimer.it_value.tv_usec = itimer.it_value.tv_sec = 0;
if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
log_Printf(LogERROR, "Unable to set itimer (%s)\n", sys_errlist[errno]);
sig_signal(SIGALRM, SIG_IGN);
}
|