Stop using getimeofday for timeouts incase the clock changes.

This commit is contained in:
Roy Marples 2007-05-16 11:05:42 +00:00
parent 66d7150750
commit 1ac8d86ebe
2 changed files with 25 additions and 13 deletions

View File

@ -65,6 +65,7 @@ endif
ifeq ($(OS),Linux)
LDLIBS_RC = -ldl
LDLIBS_RS = -ldl
LDLIBS_SSD = -lrt
# Shouldn't need this, but it's the easiest workaround for silly
# Linux headers that don't work with -std=c99
override CPPFLAGS += -D_GNU_SOURCE
@ -76,7 +77,7 @@ endif
HAVE_PAM =
ifdef HAVE_PAM
CPPFLAGS_SSD = -DHAVE_PAM
LDLIBS_SSD = -lpam
LDLIBS_SSD += -lpam
endif
# We also define _BSD_SOURCE so both Linux and the BSDs get a few

View File

@ -30,6 +30,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifdef HAVE_PAM
@ -90,6 +91,20 @@ static void cleanup (void)
rc_strlist_free (newenv);
}
static int get_time(struct timeval *tp)
{
struct timespec ts;
if (clock_gettime (CLOCK_MONOTONIC, &ts) == -1) {
eerror ("clock_gettime: %s", strerror (errno));
return (-1);
}
tp->tv_sec = ts.tv_sec;
tp->tv_usec = ts.tv_nsec / 1000;
return (0);
}
static int parse_signal (const char *sig)
{
typedef struct signalpair
@ -374,10 +389,8 @@ static int run_stop_schedule (const char *exec, const char *cmd,
break;
}
if (gettimeofday (&stopat, NULL) != 0) {
eerror ("%s: gettimeofday: %s", progname, strerror (errno));
if (get_time (&stopat) != 0)
return (0);
}
stopat.tv_sec += item->value;
while (1) {
@ -396,10 +409,8 @@ static int run_stop_schedule (const char *exec, const char *cmd,
}
}
if (gettimeofday (&now, NULL) != 0) {
eerror ("%s: gettimeofday: %s", progname, strerror (errno));
if (get_time (&now) != 0)
return (0);
}
if (timercmp (&now, &stopat, >))
break;
}
@ -966,8 +977,8 @@ int main (int argc, char **argv)
struct timeval now;
bool retestpid = false;
if (gettimeofday (&stopat, NULL) != 0)
eerrorx ("%s: gettimeofday: %s", progname, strerror (errno));
if (get_time (&stopat) != 0)
exit (EXIT_FAILURE);
stopat.tv_usec += START_WAIT;
while (1) {
@ -981,9 +992,6 @@ int main (int argc, char **argv)
eerrorx ("%s: select: %s", progname, strerror (errno));
}
if (gettimeofday (&now, NULL) != 0)
eerrorx ("%s: gettimeofday: %s", progname, strerror (errno));
/* This is knarly.
If we backgrounded then we know the exact pid.
Otherwise if we have a pidfile then it *may* know the exact pid.
@ -1000,6 +1008,7 @@ int main (int argc, char **argv)
alive = true;
retestpid = true;
} else {
printf ("%d\n", get_pid (pidfile, true));
retestpid = false;
if (do_stop (NULL, NULL, pidfile, uid, 0,
true, false, true) > 0)
@ -1015,6 +1024,8 @@ int main (int argc, char **argv)
if (! alive)
eerrorx ("%s: %s died", progname, exec);
if (get_time (&now) != 0)
exit (EXIT_FAILURE);
if (timercmp (&now, &stopat, >))
break;
}