Don't expect select() to adjust the passed time when it's

interrupted with a SIGALRM.  In fact, select() sets the
passed time to zero, making the previous implementation
terminate always after 1/10th of a second !

Also, deal with someone changing the clock while we're
sleeping (and restart the whole sleep).

Dangers pointed out by: Theo de Raadt <deraadt@cvs.openbsd.org>
This commit is contained in:
Brian Somers 1997-12-28 21:55:05 +00:00
parent bf58fc2646
commit b1cbb71c91
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=32063
3 changed files with 46 additions and 53 deletions

View File

@ -17,7 +17,7 @@
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Id: main.c,v 1.112 1997/12/27 13:45:53 brian Exp $
* $Id: main.c,v 1.113 1997/12/28 02:46:26 brian Exp $
*
* TODO:
* o Add commands for traffic summary, version display, etc.
@ -915,16 +915,15 @@ DoLoop(void)
FD_SET(server, &rfds);
}
#ifndef SIGALRM
/*
* *** IMPORTANT ***
*
* CPU is serviced every TICKUNIT micro seconds. This value must be chosen
* with great care. If this values is too big, it results loss of
* characters from modem and poor responce. If this values is too small,
* ppp process eats many CPU time.
* with great care. If this values is too big, it results in loss of
* characters from the modem and poor response. If this value is too
* small, ppp eats too much CPU time.
*/
#ifndef SIGALRM
nointr_usleep(TICKUNIT);
usleep(TICKUNIT);
TimerService();
#else
handle_signals();

View File

@ -17,7 +17,7 @@
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Id: timer.c,v 1.23 1997/11/09 06:22:49 brian Exp $
* $Id: timer.c,v 1.24 1997/11/22 03:37:52 brian Exp $
*
* TODO:
*/
@ -207,66 +207,60 @@ ShowTimers()
}
#ifdef SIGALRM
u_int
nointr_sleep(u_int sec)
static void
nointr_dosleep(u_int sec, u_int usec)
{
struct timeval to, st, et;
long sld, nwd, std;
gettimeofday(&st, NULL);
et.tv_sec = st.tv_sec + sec;
et.tv_usec = st.tv_usec + usec;
to.tv_sec = sec;
to.tv_usec = 0;
std = st.tv_sec * 1000000 + st.tv_usec;
to.tv_usec = usec;
for (;;) {
if (select(0, NULL, NULL, NULL, &to) == 0 ||
errno != EINTR) {
break;
} else {
gettimeofday(&et, NULL);
sld = to.tv_sec * 1000000 + to.tv_sec;
nwd = et.tv_sec * 1000000 + et.tv_usec - std;
if (sld > nwd)
sld -= nwd;
else
sld = 1; /* Avoid both tv_sec/usec is 0 */
/* Calculate timeout value for select */
to.tv_sec = sld / 1000000;
to.tv_usec = sld % 1000000;
gettimeofday(&to, NULL);
if (to.tv_sec > et.tv_sec ||
(to.tv_sec == et.tv_sec && 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_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;
}
}
}
return (0L);
}
void
nointr_sleep(u_int sec)
{
nointr_dosleep(sec, 0);
}
void
nointr_usleep(u_int usec)
{
struct timeval to, st, et;
long sld, nwd, std;
gettimeofday(&st, NULL);
to.tv_sec = 0;
to.tv_usec = usec;
std = st.tv_sec * 1000000 + st.tv_usec;
for (;;) {
if (select(0, NULL, NULL, NULL, &to) == 0 ||
errno != EINTR) {
break;
} else {
gettimeofday(&et, NULL);
sld = to.tv_sec * 1000000 + to.tv_sec;
nwd = et.tv_sec * 1000000 + et.tv_usec - std;
if (sld > nwd)
sld -= nwd;
else
sld = 1; /* Avoid both tv_sec/usec is 0 */
/* Calculate timeout value for select */
to.tv_sec = sld / 1000000;
to.tv_usec = sld % 1000000;
}
}
nointr_dosleep(0, usec);
}
static void

View File

@ -15,7 +15,7 @@
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* $Id: timer.h,v 1.2 1997/10/26 12:42:13 brian Exp $
* $Id: timer.h,v 1.3 1997/11/22 03:37:52 brian Exp $
*
* TODO:
*/
@ -46,6 +46,6 @@ extern void TermTimerService(void);
extern void ShowTimers(void);
#ifdef SIGALRM
extern u_int nointr_sleep(u_int);
extern void nointr_sleep(u_int);
extern void nointr_usleep(u_int);
#endif