summaryrefslogtreecommitdiff
path: root/usr.sbin/ppp/timer.c
blob: fcaa240b39cbe22d1611b3af58182f28c98c5319 (plain)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 *		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.5 1998/06/28 09:41:45 brian Exp $
 *
 *  TODO:
 */

#include <signal.h>
#ifdef SIGALRM
#include <errno.h>
#endif
#include <sys/time.h>
#include <unistd.h>

#include "command.h"
#include "mbuf.h"
#include "log.h"
#include "sig.h"
#include "timer.h"

static struct pppTimer *TimerList = NULL;

static void StopTimerNoBlock(struct pppTimer *);
static void InitTimerService(void);

void
StopTimer(struct pppTimer * tp)
{
#ifdef SIGALRM
  int omask;

  omask = sigblock(sigmask(SIGALRM));
#endif
  StopTimerNoBlock(tp);
#ifdef SIGALRM
  sigsetmask(omask);
#endif
}

void
StartTimer(struct pppTimer * tp)
{
  struct pppTimer *t, *pt;
  u_long ticks = 0;

#ifdef SIGALRM
  int omask;

  omask = sigblock(sigmask(SIGALRM));
#endif

  if (tp->state != TIMER_STOPPED) {
    StopTimerNoBlock(tp);
  }
  if (tp->load == 0) {
    LogPrintf(LogDEBUG, "timer %x has 0 load!\n", tp);
    sigsetmask(omask);
    return;
  }
  pt = NULL;
  for (t = TimerList; t; t = t->next) {
    LogPrintf(LogDEBUG, "StartTimer: %x(%d):  ticks: %d, rest: %d\n",
	      t, t->state, ticks, t->rest);
    if (ticks + t->rest >= tp->load)
      break;
    ticks += t->rest;
    pt = t;
  }

  tp->state = TIMER_RUNNING;
  tp->rest = tp->load - ticks;
  LogPrintf(LogDEBUG, "StartTimer: Inserting %x before %x, rest = %d\n",
	    tp, t, tp->rest);
  /* Insert given *tp just before *t */
  tp->next = t;
  if (pt) {
    pt->next = tp;
  } else {
    InitTimerService();
    TimerList = tp;
  }
  if (t)
    t->rest -= tp->rest;

#ifdef SIGALRM
  sigsetmask(omask);
#endif
}

static void
StopTimerNoBlock(struct pppTimer * tp)
{
  struct pppTimer *t, *pt;

  /*
   * A Running Timer should be removing TimerList, But STOPPED/EXPIRED is
   * already removing TimerList. So just marked as TIMER_STOPPED. Do not
   * change tp->enext!! (Might be Called by expired proc)
   */
  LogPrintf(LogDEBUG, "StopTimer: %x, next = %x state=%x\n",
	    tp, tp->next, tp->state);
  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 ? */
	TermTimerService();	/* Terminate Timer Service */
    }
    if (t->next)
      t->next->rest += tp->rest;
  } else
    LogPrintf(LogERROR, "Oops, timer not found!!\n");

  tp->next = NULL;
  tp->state = TIMER_STOPPED;
}

static void
TimerService(void)
{
  struct pppTimer *tp, *exp, *wt;

  if (LogIsKept(LogDEBUG))
    ShowTimers();
  tp = TimerList;
  if (tp) {
    tp->rest--;
    if (tp->rest == 0) {

      /*
       * Multiple timers may expires at once. Create list of expired timers.
       */
      exp = NULL;
      do {
	tp->state = TIMER_EXPIRED;
	wt = tp->next;
	tp->enext = exp;
	exp = tp;
	LogPrintf(LogDEBUG, "TimerService: Add %x to exp\n", tp);
	tp = wt;
      } while (tp && (tp->rest == 0));

      TimerList = tp;
      if (TimerList == NULL)	/* No timers ? */
	TermTimerService();	/* Terminate Timer Service */
      LogPrintf(LogDEBUG, "TimerService: next is %x(%d)\n",
		TimerList, TimerList ? TimerList->rest : 0);

      /*
       * Process all expired timers.
       */
      while (exp) {
#ifdef notdef
	StopTimer(exp);
#endif
	if (exp->func)
	  (*exp->func) (exp->arg);

	/*
	 * Just Removing each item from expired list And exp->enext will be
	 * intialized at next expire in this funtion.
	 */
	exp = exp->enext;
      }
    }
  }
}

void
ShowTimers()
{
  struct pppTimer *pt;

  LogPrintf(LogDEBUG, "---- Begin of Timer Service List---\n");
  for (pt = TimerList; pt; pt = pt->next)
    LogPrintf(LogDEBUG, "%x: load = %d, rest = %d, state =%x\n",
	      pt, pt->load, pt->rest, pt->state);
  LogPrintf(LogDEBUG, "---- End of Timer Service List ---\n");
}

#ifdef SIGALRM

static void
nointr_dosleep(u_int sec, u_int usec)
{
  struct timeval to, st, et;

  gettimeofday(&st, NULL);
  et.tv_sec = st.tv_sec + sec;
  et.tv_usec = st.tv_usec + usec;
  to.tv_sec = sec;
  to.tv_usec = usec;
  for (;;) {
    if (select(0, NULL, NULL, NULL, &to) == 0 ||
	errno != EINTR) {
      break;
    } else {
      gettimeofday(&to, NULL);
      if (to.tv_sec > et.tv_sec + 1 ||
          (to.tv_sec == et.tv_sec + 1 && to.tv_usec > et.tv_usec) ||
          to.tv_sec < st.tv_sec ||
          (to.tv_sec == st.tv_sec && to.tv_usec < st.tv_usec)) {
        LogPrintf(LogWARN, "Clock adjusted between %d and %d seconds "
                  "during sleep !\n",
                  to.tv_sec - st.tv_sec, sec + to.tv_sec - st.tv_sec);
        st.tv_sec = to.tv_sec;
        st.tv_usec = to.tv_usec;
        et.tv_sec = st.tv_sec + sec;
        et.tv_usec = st.tv_usec + usec;
        to.tv_sec = sec;
        to.tv_usec = usec;
      } else if (to.tv_sec > et.tv_sec ||
                 (to.tv_sec == et.tv_sec && to.tv_usec >= et.tv_usec)) {
        break;
      } else {
        to.tv_sec = et.tv_sec - to.tv_sec;
        if (et.tv_usec < to.tv_usec) {
          to.tv_sec--;
          to.tv_usec = 1000000 + et.tv_usec - to.tv_usec;
        } else
          to.tv_usec = et.tv_usec - to.tv_usec;
      }
    }
  }
}

void
nointr_sleep(u_int sec)
{
  nointr_dosleep(sec, 0);
}

void
nointr_usleep(u_int usec)
{
  nointr_dosleep(0, usec);
}

static void 
InitTimerService()
{
  struct itimerval itimer;

  pending_signal(SIGALRM, (void (*) (int)) TimerService);
  itimer.it_interval.tv_sec = itimer.it_value.tv_sec = 0;
  itimer.it_interval.tv_usec = itimer.it_value.tv_usec = TICKUNIT;
  if (setitimer(ITIMER_REAL, &itimer, NULL) == -1)
    LogPrintf(LogERROR, "Unable to set itimer.\n");
}

void 
TermTimerService(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)
    LogPrintf(LogERROR, "Unable to set itimer.\n");
  pending_signal(SIGALRM, SIG_IGN);
}

#endif