Skip to content

Commit

Permalink
clients/upsmon.c: monitor if our sleep during end of loop cycle took …
Browse files Browse the repository at this point in the history
…too long [networkupstools#1070]

Treat *any* clock jump same as OS sleep (act to avoid stale UPS info),
even if it was not a suspend/hibernate event but e.g. summer/winter
time change or just upsmon suspended by a debugger or blocked stdio.

Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Aug 21, 2024
1 parent eda3ff5 commit ef0151b
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion clients/upsmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -3371,6 +3371,8 @@ int main(int argc, char *argv[])
default: /* Odd... */
break;
}
/* Reset the value, regardless of support */
sleep_inhibitor_status = -2;

for (ups = firstups; ups != NULL; ups = ups->next) {
if (isPreparingForSleepSupported() && (sleep_inhibitor_status = isPreparingForSleep()) >= 0) {
Expand All @@ -3391,28 +3393,43 @@ int main(int argc, char *argv[])
waitpid(-1, NULL, WNOHANG);

if (isPreparingForSleepSupported()) {
time_t start, now;
time_t start, now, prev;
double dt = 0;

time(&start);
time(&now);
sleep_inhibitor_status = -2;

while (sleep_inhibitor_status != 1 && dt < sleepval) {
prev = now;
sleep(1);
sleep_inhibitor_status = isPreparingForSleep();
time(&now);
dt = difftime(now, start);
upsdebugx(7, "start=%" PRIiMAX " now=%" PRIiMAX " dt=%g sleepval=%u sleep_inhibitor_status=%d",
start, now, dt, sleepval, sleep_inhibitor_status);
if (dt > (sleepval + 5) || difftime(now, prev) > 5) {
upsdebugx(2, "It seems we have slept without warning or the system clock was changed");
if (sleep_inhibitor_status < 0)
sleep_inhibitor_status = 0; /* behave as woken up */
}
}
if (sleep_inhibitor_status >= 0) {
upsdebugx(2, "Aborting polling delay between main loop cycles because OS is preparing for sleep or just woke up");
goto end_loop_cycle;
}
} else {
/* sleep tight */
time_t start, now;

time(&start);
sleep(sleepval);
time(&now);

if (difftime(now, start) > (sleepval + 5)) {
upsdebugx(2, "It seems we have slept without warning or the system clock was changed");
sleep_inhibitor_status = 0; /* behave as woken up */
}
}
#else
maxhandle = 0;
Expand Down

0 comments on commit ef0151b

Please sign in to comment.