Use clock MONOTONIC to timeout, not localtime, #177514

This commit is contained in:
Roy Marples 2007-05-11 08:58:19 +00:00
parent 7cdd8d4a0e
commit 46cd245ee3
2 changed files with 27 additions and 15 deletions

View File

@ -567,26 +567,38 @@ void rc_schedule_clear (const char *service)
} }
librc_hidden_def(rc_schedule_clear) librc_hidden_def(rc_schedule_clear)
static time_t get_uptime(void)
{
#ifdef __linux__
struct sysinfo info;
sysinfo (&info);
return (time_t) info.uptime;
#else
struct timespec tp;
if (clock_gettime (CLOCK_MONOTONIC, &tp) == -1) {
eerror ("clock_gettime: %s", strerror (errno));
return -1;
}
return tp.tv_sec;
#endif
}
bool rc_wait_service (const char *service) bool rc_wait_service (const char *service)
{ {
char *svc; char *svc;
char *base; char *base;
char *fifo; char *fifo;
struct timeval tv; struct timeval tv;
struct timeval stopat; time_t start = get_uptime ();
struct timeval now;
bool retval = false; bool retval = false;
bool forever = false; bool forever = false;
if (! service) if (! service)
return (false); return (false);
if (gettimeofday (&stopat, NULL) != 0) {
eerror ("gettimeofday: %s", strerror (errno));
return (false);
}
stopat.tv_sec += WAIT_MAX;
svc = rc_xstrdup (service); svc = rc_xstrdup (service);
base = basename (svc); base = basename (svc);
fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", base, (char *) NULL); fifo = rc_strcatpaths (RC_SVCDIR, "exclusive", base, (char *) NULL);
@ -612,13 +624,8 @@ bool rc_wait_service (const char *service)
} }
if (! forever) { if (! forever) {
/* Don't hang around forever */ time_t now = get_uptime();
if (gettimeofday (&now, NULL) != 0) { if (now - start > WAIT_MAX)
eerror ("gettimeofday: %s", strerror (errno));
break;
}
if (timercmp (&now, &stopat, >))
break; break;
} }
} }

View File

@ -15,6 +15,10 @@
#include <sys/utsname.h> #include <sys/utsname.h>
#include <sys/wait.h> #include <sys/wait.h>
#ifdef __linux__
#include <sys/sysinfo.h>
#endif
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
@ -26,6 +30,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
#if defined(__DragonFly__) || defined(__FreeBSD__) || \ #if defined(__DragonFly__) || defined(__FreeBSD__) || \