From a4508479f504c9965f988cedd2125c1ab5aff67a Mon Sep 17 00:00:00 2001 From: Jesse Smith Date: Wed, 4 Apr 2018 23:12:25 -0300 Subject: [PATCH] Updated shutdown to check if time has lapsed when we did not notice. For example if the computer was in sleep mode pending a shutdown. The shutdown command now notices time has past and adjusts its countdown clock to compensate. Accurate to the nearest minute. Closes Savannah bug #36279 --- doc/Changelog | 6 ++++++ src/shutdown.c | 26 +++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/doc/Changelog b/doc/Changelog index 5481d5a..3e8f975 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -6,6 +6,12 @@ sysvinit (2.90) UNRELEASED; urgency=low comparison so the latter is wrote_utmp_rlevel. * Simplified logic in mountpoint.c when testing for same device or same inode. Thanks to David Binderman for pointing out the above three issues. + * When we run shutdown and then the computer is put to sleep, the + shutdown command recognizes time has passed and continues its + countdown taking the time lapse into consideration. This prevents + longer waits if we slept past the time we should have shutdown. + Accurate to the nearest minute. + Closes Savannah bug #36279. sysvinit (2.89) world; urgency=low diff --git a/src/shutdown.c b/src/shutdown.c index 1208c57..548b67f 100644 --- a/src/shutdown.c +++ b/src/shutdown.c @@ -59,7 +59,7 @@ #include "init.h" -char *Version = "@(#) shutdown 2.86-1 31-Jul-2004 miquels@cistron.nl"; +char *Version = "@(#) shutdown 2.90-1 31-Jul-2004 miquels@cistron.nl"; #define MESSAGELEN 256 @@ -483,7 +483,7 @@ int main(int argc, char **argv) struct tm *lt; struct stat st; struct utmp *ut; - time_t t; + time_t t, target_time; char *halttype; char *downusers[32]; char buf[128]; @@ -758,6 +758,17 @@ int main(int argc, char **argv) /* Shutdown NOW if time == 0 */ if (wt == 0) issue_shutdown(halttype); + /* Rather than loop and reduce wt (wait time) once per minute, + we shall check the current time against the target time. + Then calculate the remaining wating time based on the difference + between current time and target time. + This avoids missing shutdown time (target time) after the + computer has been asleep. -- Jesse + */ + /* target time, in seconds = current time + wait time */ + time(&t); + target_time = t + (60 * wt); + /* Give warnings on regular intervals and finally shutdown. */ if (wt < 15 && !needwarning(wt)) issue_warn(wt); while(wt) { @@ -767,7 +778,16 @@ int main(int argc, char **argv) } if (needwarning(wt)) issue_warn(wt); hardsleep(60); - wt--; + time(&t); /* get current time once per minute */ + if (t >= target_time) /* past the target */ + wt = 0; + else if ( (target_time - t) <= 60 ) /* less 1 min remains */ + { + hardsleep(target_time - t); + wt = 0; + } + else /* more thsn 1 min remains */ + wt = (int) (target_time - t) / 60; } issue_shutdown(halttype);