From owner-freebsd-bugs@FreeBSD.ORG Sat Oct 24 17:20:04 2009 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id CFD7E106568D for ; Sat, 24 Oct 2009 17:20:04 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id A4DCD8FC28 for ; Sat, 24 Oct 2009 17:20:04 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.3/8.14.3) with ESMTP id n9OHK4J4071858 for ; Sat, 24 Oct 2009 17:20:04 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.3/8.14.3/Submit) id n9OHK45n071857; Sat, 24 Oct 2009 17:20:04 GMT (envelope-from gnats) Date: Sat, 24 Oct 2009 17:20:04 GMT Message-Id: <200910241720.n9OHK45n071857@freefall.freebsd.org> To: freebsd-bugs@FreeBSD.org From: "Mikhail T." Cc: Subject: Re: bin/139345: handle SIGINFO in sleep(1), etc. [patch] X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: "Mikhail T." List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 24 Oct 2009 17:20:05 -0000 The following reply was made to PR bin/139345; it has been noted by GNATS. From: "Mikhail T." To: FreeBSD-gnats-submit@FreeBSD.org, freebsd-bugs@FreeBSD.org Cc: Subject: Re: bin/139345: handle SIGINFO in sleep(1), etc. [patch] Date: Sat, 24 Oct 2009 12:57:16 -0400 This is a multi-part message in MIME format. --------------010206010208030009060605 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit An improved patch... -mi --------------010206010208030009060605 Content-Type: text/plain; name="sleep.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="sleep.patch" Index: sleep.c =================================================================== RCS file: /home/ncvs/src/bin/sleep/sleep.c,v retrieving revision 1.20 diff -U 2 -r1.20 sleep.c --- sleep.c 7 Aug 2005 09:11:38 -0000 1.20 +++ sleep.c 24 Oct 2009 16:54:40 -0000 @@ -43,7 +43,10 @@ #include +#include #include +#include #include #include +#include #include #include @@ -51,16 +54,22 @@ void usage(void); +volatile sig_atomic_t report_requested = 0; + +static void +report_request(int signum __unused) +{ + report_requested = 1; +} + int main(int argc, char *argv[]) { struct timespec time_to_sleep; - long l; + long l, original; int neg; char *p; - if (argc != 2) { + if (argc != 2) usage(); - return(1); - } p = argv[1]; @@ -86,13 +95,15 @@ if (isdigit((unsigned char)*p)) { l = strtol(p, &p, 10); + /* + * Avoid overflow when `seconds' is huge. This assumes + * that the maximum value for a time_t is <= INT_MAX. + */ + if (l > INT_MAX) { - /* - * Avoid overflow when `seconds' is huge. This assumes - * that the maximum value for a time_t is <= INT_MAX. - */ l = INT_MAX; } } else l = 0; + time_to_sleep.tv_sec = (time_t)l; @@ -105,12 +116,41 @@ if (isdigit((unsigned char)*++p)) time_to_sleep.tv_nsec += (*p - '0') * l; + else if(*p != '\0') + usage(); else break; l /= 10; } while (l); - } + } else if (*p != '\0') + usage(); - if (!neg && (time_to_sleep.tv_sec > 0 || time_to_sleep.tv_nsec > 0)) - (void)nanosleep(&time_to_sleep, (struct timespec *)NULL); + signal(SIGINFO, report_request); /* We don't care if it fails */ + if (!neg && (time_to_sleep.tv_sec > 0 || time_to_sleep.tv_nsec > 0)) { + original = time_to_sleep.tv_sec; + while (nanosleep(&time_to_sleep, &time_to_sleep)) { + /* + * Reporting does not bother with fractions + * of a second... + */ + if (report_requested) { + warnx("about %ld seconds left" + " out of the original %ld", + time_to_sleep.tv_sec, original); + report_requested = 0; + } else { + /* + * The old implementation would exit here, so + * that's what we are doing too. Removing + * the break below would change the behavior + * to "go back to sleep" -- the time_to_sleep + * already contains the proper values. + */ + warn("exiting prematurely after" + " %ld of the %ld seconds of sleep", + original-time_to_sleep.tv_sec, original); + break; + } + } + } return(0); @@ -123,3 +163,4 @@ write(STDERR_FILENO, msg, sizeof(msg) - 1); + exit(EX_USAGE); } --------------010206010208030009060605--