Commit 6e711ccb authored by Russ Fish's avatar Russ Fish
Browse files

More time hacks for Windows clock setbacks from UTC to local time.

parent 77845d20
......@@ -12,9 +12,9 @@ SLOTHD_DIR = unknownclient
TBLIB = $(OBJDIR)/lib/libtb/libtb.a
SBIN_SCRIPTS = sdisrunning sddeploy
SDPROGS = sdcollectd slothd
SDPROGS = sdcollectd$(EXE) slothd$(EXE)
SYSTEM := $(shell uname -s)
SYSTEM := $(shell uname -s)
include $(OBJDIR)/Makeconf
......@@ -34,6 +34,7 @@ ifeq ($(SYSTEM),CYGWIN_NT-5.1)
MLIBS += -liphlpapi
WINCLIENT = idlemon
WINPROGS = idlemon$(EXE)
WINCLIENTSRC = idlemon.c
CVR = /HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Run
IDLEKEY = $(CVR)/EmulabIdle
......@@ -86,4 +87,4 @@ endif
control-install:
clean:
rm -f *.o $(SDPROGS) *~ core *.core version.c
rm -f *.o $(SDPROGS) $(WINPROGS) *~ core *.core version.c
......@@ -25,7 +25,7 @@ int main(int argc, char **argv) {
int ch;
int debug = 0;
int input_occurred_fd;
time_t last_time = 0;
DWORD last_tick = 0;
while ((ch = getopt(argc, argv, "d")) != -1) {
switch (ch) {
......@@ -55,13 +55,19 @@ int main(int argc, char **argv) {
else {
/* Windows keeps time in millisecond ticks since boot time. */
DWORD windows_ticks = GetTickCount() - windows_input.dwTime;
time_t windows_time = time(0) - windows_ticks/1000;
if (debug)
printf("Windows input event time: %s", ctime(&windows_time));
DWORD windows_tick = windows_input.dwTime;
if (debug) {
time_t windows_time = time(0) - (GetTickCount() - windows_tick)/1000;
printf("Windows input event tick %d, time: %s",
windows_tick, ctime(&windows_time));
}
if (windows_time > last_time) {
last_time = windows_time;
/* Check time in ticks, because they're monotonic. The clock jumps back
* many hours (in the western hemisphere) as NTP changes from UTC time
* to local time.
*/
if (windows_tick > last_tick) {
last_tick = windows_tick;
/* It would be nice to use futimes(), but there's not one
* on Cygwin. Instead, use ftruncate() to set the modtime.
......
......@@ -73,7 +73,7 @@ int main(int argc, char **argv) {
int exitcode = -1;
u_int myabits, span;
time_t curtime;
time_t curtime, lasttime = 0;
static SLOTHD_OPTS mopts;
static SLOTHD_PARAMS mparms;
static SLOTHD_PACKET mpkt;
......@@ -105,6 +105,28 @@ int main(int argc, char **argv) {
lnotice(build_info);
for (;;) {
time(&curtime);
if (opts->debug) {
printf("The time is now %s", ctime(&curtime));
fflush(stdout);
}
if (curtime < lasttime) {
/*
* The clock was set back on us, most likely on Windows as NTP
* changed the clock from UTC time to local time. Set the
* timestamps back by the same amount to compensate.
*/
int delta = lasttime - curtime;
parms->startup -= delta;
mparms.lastrpt -= delta;
if (opts->debug) {
printf("The clock was set back by %d seconds.\n\n", delta);
fflush(stdout);
}
}
lasttime = curtime;
/* Collect current machine stats */
mparms.cnt++;
get_load(pkt);
......@@ -119,7 +141,7 @@ int main(int argc, char **argv) {
* 2) Its been over <reg_interval> seconds since the last report
*/
if ((!opkt->actbits && pkt->actbits) ||
(time(&curtime) >= mparms.lastrpt + mopts.reg_interval) ||
(curtime >= mparms.lastrpt + mopts.reg_interval) ||
parms->dolast) {
if (send_pkt(pkt)) {
mparms.lastrpt = curtime;
......@@ -496,14 +518,24 @@ void get_min_tty_idle(SLOTHD_PACKET *pkt) {
#ifndef __CYGWIN__
/* The time of last reading keyboard input. */
time_t tty_time = sb.st_atime;
if (opts->debug)
printf("Input on %s: %s", parms->ttys[i], ctime(&tty_time));
#else
/* We're modding time tag files Cygwin, not reading them. */
/* We're modding time tag files on Cygwin, not reading them. */
time_t tty_time = sb.st_mtime;
/* Ignore a time tag from the future when the clock gets set back. */
if ( tty_time > time(NULL) ) {
if (opts->debug)
printf("Ignored future time on %s: %s",
parms->ttys[i], ctime(&tty_time));
tty_time = mintime;
}
#endif
if (opts->debug)
printf("Input on %s: %s", parms->ttys[i], ctime(&tty_time));
if (tty_time > mintime) {
mintime = tty_time;
if (opts->debug)
printf("Input on %s: %s", parms->ttys[i], ctime(&mintime));
}
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment