From cb1101afd7dcab548680a142d4a4b375a5a39b08 Mon Sep 17 00:00:00 2001 From: Eitan Adler Date: Mon, 1 Jan 2018 22:33:57 +0000 Subject: [PATCH] shutdown: Assume absolute time is in the future The original bug describes it best: When an absolute time is specified to shutdown, the program's behavior depends on whether that time has passed during the current calendar day. POLA would suggest that for shutdown, whose time argument is always supposed to be in the future, absolute times specified without a specific date should refer to the next occurrence of that time, rather than erroring out if that time has already passed during the current day. PR: 32411 Submitted by: wollman@khavrinen.lcs.mit.edu Submitted on: 2001-11-30 20:30:01 UTC Reviewed by: asmodai (at time of bug submission) --- sbin/shutdown/shutdown.8 | 10 +++++++++- sbin/shutdown/shutdown.c | 22 +++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/sbin/shutdown/shutdown.8 b/sbin/shutdown/shutdown.8 index b388aaf92fe9..557a942bb406 100644 --- a/sbin/shutdown/shutdown.8 +++ b/sbin/shutdown/shutdown.8 @@ -28,7 +28,7 @@ .\" @(#)shutdown.8 8.2 (Berkeley) 4/27/95 .\" $FreeBSD$ .\" -.Dd October 23, 2017 +.Dd January 1, 2018 .Dt SHUTDOWN 8 .Os .Sh NAME @@ -138,6 +138,14 @@ suffix: .Dq Li min . .Dq Li h , .Dq Li hour . +.Pp +If an absolute time is specified, but not a date, +and that time today has already passed, +.Nm +will assume that the same time tomorrow was meant. +(If a complete date is specified which has already passed, +.Nm +will print an error and exit without shutting the system down.) .It Ar warning-message Any other arguments comprise the warning message that is broadcast to users currently logged into the system. diff --git a/sbin/shutdown/shutdown.c b/sbin/shutdown/shutdown.c index c8e77883f6be..622b99fa2fa3 100644 --- a/sbin/shutdown/shutdown.c +++ b/sbin/shutdown/shutdown.c @@ -431,7 +431,7 @@ getoffset(char *timearg) struct tm *lt; char *p; time_t now; - int this_year; + int this_year, maybe_today; char *timeunit; (void)time(&now); @@ -503,6 +503,7 @@ getoffset(char *timearg) badtime(); /* FALLTHROUGH */ case 6: + maybe_today = 0; lt->tm_mday = ATOI2(timearg); if (lt->tm_mday < 1 || lt->tm_mday > 31) badtime(); @@ -517,8 +518,23 @@ getoffset(char *timearg) lt->tm_sec = 0; if ((shuttime = mktime(lt)) == -1) badtime(); - if ((offset = shuttime - now) < 0) - errx(1, "that time is already past."); + + if ((offset = shuttime - now) < 0) { + if (!maybe_today) + errx(1, "that time is already past."); + + /* + * If the user only gave a time, assume that + * any time earlier than the current time + * was intended to be that time tomorrow. + */ + lt->tm_mday++; + if ((shuttime = mktime(lt)) == -1) + badtime(); + if ((offset = shuttime - now) < 0) { + errx(1, "tomorrow is before today?"); + } + } break; default: badtime();