1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 * $FreeBSD: stable/11/usr.sbin/ppp/timer.c 330449 2018-03-05 07:26:05Z eadler $
31 */
32
33#include <errno.h>
34#include <signal.h>
35#include <stdarg.h>
36#include <stdio.h>
37#include <string.h>
38#include <sys/time.h>
39#include <termios.h>
40
41#include "log.h"
42#include "sig.h"
43#include "timer.h"
44#include "descriptor.h"
45#include "prompt.h"
46
47
48#define RESTVAL(t) \
49    ((t).it_value.tv_sec * SECTICKS + (t).it_value.tv_usec / TICKUNIT + \
50     ((((t).it_value.tv_usec % TICKUNIT) >= (TICKUNIT >> 1)) ? 1 : 0))
51
52static struct pppTimer *TimerList = NULL, *ExpiredList = NULL;
53
54static void StopTimerNoBlock(struct pppTimer *);
55
56static const char *
57tState2Nam(u_int state)
58{
59  static const char * const StateNames[] = { "stopped", "running", "expired" };
60
61  if (state >= sizeof StateNames / sizeof StateNames[0])
62    return "unknown";
63  return StateNames[state];
64}
65
66void
67timer_Stop(struct pppTimer *tp)
68{
69  sigset_t mask, omask;
70
71  sigemptyset(&mask);
72  sigaddset(&mask, SIGALRM);
73  sigprocmask(SIG_BLOCK, &mask, &omask);
74  StopTimerNoBlock(tp);
75  sigprocmask(SIG_SETMASK, &omask, NULL);
76}
77
78void
79timer_Start(struct pppTimer *tp)
80{
81  struct itimerval itimer;
82  struct pppTimer *t, *pt;
83  u_long ticks = 0;
84  sigset_t mask, omask;
85
86  sigemptyset(&mask);
87  sigaddset(&mask, SIGALRM);
88  sigprocmask(SIG_BLOCK, &mask, &omask);
89
90  if (tp->state != TIMER_STOPPED)
91    StopTimerNoBlock(tp);
92
93  if (tp->load == 0) {
94    log_Printf(LogTIMER, "%s timer[%p] has 0 load!\n", tp->name, tp);
95    sigprocmask(SIG_SETMASK, &omask, NULL);
96    return;
97  }
98
99  /*
100   * We just need to insert tp in the correct relative place.  We don't
101   * need to adjust TimerList->rest (yet).
102   */
103  if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0)
104    ticks = RESTVAL(itimer) - TimerList->rest;
105
106  pt = NULL;
107  for (t = TimerList; t; t = t->next) {
108    if (ticks + t->rest >= tp->load)
109      break;
110    ticks += t->rest;
111    pt = t;
112  }
113
114  tp->state = TIMER_RUNNING;
115  tp->rest = tp->load - ticks;
116
117  if (t)
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);
120  else
121    log_Printf(LogTIMER, "timer_Start: Inserting %s timer[%p]\n", tp->name, tp);
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
137static void
138StopTimerNoBlock(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
191static void
192TimerService(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
236void
237timer_Show(int LogLevel, struct prompt *prompt)
238{
239  struct itimerval itimer;
240  struct pppTimer *pt;
241  long rest;
242
243  /*
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!
249   */
250  if (TimerList && getitimer(ITIMER_REAL, &itimer) == 0)
251    rest = RESTVAL(itimer) - TimerList->rest;
252  else
253    rest = 0;
254
255#define SECS(val)	((val) / SECTICKS)
256#define HSECS(val)	(((val) % SECTICKS) * 100 / SECTICKS)
257#define DISP								\
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)
261
262  if (!prompt)
263    log_Printf(LogLevel, "---- Begin of Timer Service List---\n");
264
265  for (pt = TimerList; pt; pt = pt->next) {
266    rest += pt->rest;
267    if (prompt)
268      prompt_Printf(prompt, DISP);
269    else
270      log_Printf(LogLevel, DISP);
271  }
272
273  if (!prompt)
274    log_Printf(LogLevel, "---- End of Timer Service List ---\n");
275}
276
277void
278timer_InitService(int restart)
279{
280  struct itimerval itimer;
281
282  if (TimerList) {
283    if (!restart)
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));
291  }
292}
293
294void
295timer_TermService(void)
296{
297  struct itimerval itimer;
298
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);
304}
305