Bug 30680

Summary: uptime and w utilities lie about real uptime
Product: Base System Reporter: vova <vova>
Component: binAssignee: Crist J. Clark <cjc>
Status: Closed FIXED    
Severity: Affects Only Me CC: hackers
Priority: Normal    
Version: 5.0-CURRENT   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description vova 2001-09-20 12:30:03 UTC
	Just after very fast boot uptimealways shows more than 30 sec.

	looking to src/usr/bin/w/w.c:

        if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
            boottime.tv_sec != 0) {
                uptime = now - boottime.tv_sec;
                uptime += 30;
====================== ^^^^^
                days = uptime / 86400;
                uptime %= 86400;
                hrs = uptime / 3600;
                uptime %= 3600;
                mins = uptime / 60;
                secs = uptime % 60;
                (void)printf(" up");


why utility increases uptime on 30 seconds ??
Is any real reasons for it ?

How-To-Repeat: 	Boot and run uptime
Comment 1 dwmalone 2001-09-20 13:31:49 UTC
On Thu, Sep 20, 2001 at 03:21:08PM +0400, Vladimir B.Grebenschikov wrote:
> why utility increases uptime on 30 seconds ??
> Is any real reasons for it ?

It adds 30 because it wants to round the number of minutes to the
nearest minute, instead of rounding down. Unfortunately this isn't
a sensible thing to do if you are also going to display the number
of seconds.

	David.
Comment 2 Crist J. Clark 2001-09-20 20:04:50 UTC
On Thu, Sep 20, 2001 at 05:40:05AM -0700, David Malone wrote:
> The following reply was made to PR bin/30680; it has been noted by GNATS.
> 
> From: David Malone <dwmalone@maths.tcd.ie>
> To: "Vladimir B.Grebenschikov" <vova@express.ru>
> Cc: FreeBSD-gnats-submit@freebsd.org, freebsd-hackers@freebsd.org
> Subject: Re: bin/30680: uptime and w utilities lie about real uptime
> Date: Thu, 20 Sep 2001 13:31:49 +0100
> 
>  On Thu, Sep 20, 2001 at 03:21:08PM +0400, Vladimir B.Grebenschikov wrote:
>  > why utility increases uptime on 30 seconds ??
>  > Is any real reasons for it ?
>  
>  It adds 30 because it wants to round the number of minutes to the
>  nearest minute, instead of rounding down. Unfortunately this isn't
>  a sensible thing to do if you are also going to display the number
>  of seconds.

The only time this is noticed is when the time is printed in
seconds. This should fix it,

Index: src/usr.bin/w/w.c
===================================================================
RCS file: /export/ncvs/src/usr.bin/w/w.c,v
retrieving revision 1.48
diff -u -r1.48 w.c
--- src/usr.bin/w/w.c   2001/07/26 19:20:13     1.48
+++ src/usr.bin/w/w.c   2001/09/20 19:00:57
@@ -452,13 +452,15 @@
        if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
            boottime.tv_sec != 0) {
                uptime = now - boottime.tv_sec;
+               /* Round to nearest minute. */
                uptime += 30;
                days = uptime / 86400;
                uptime %= 86400;
                hrs = uptime / 3600;
                uptime %= 3600;
                mins = uptime / 60;
-               secs = uptime % 60;
+               /* Undo rounding to calculate uptime in seconds. */
+               secs = (uptime - 30) % 60;
                (void)printf(" up");
                if (days > 0)
                        (void)printf(" %d day%s,", days, days > 1 ? "s" : "");

I will commit the fix later unless someone has comments.
-- 
Crist J. Clark                           cjclark@alum.mit.edu
Comment 3 Crist J. Clark freebsd_committer freebsd_triage 2001-09-20 20:26:01 UTC
State Changed
From-To: open->analyzed

Fix being committed to CURRENT. 


Comment 4 Crist J. Clark freebsd_committer freebsd_triage 2001-09-20 20:26:01 UTC
Responsible Changed
From-To: freebsd-bugs->cjc

Made the fix in my repo doing some checks and will commit.
Comment 5 Bruce Evans 2001-09-20 21:10:26 UTC
On Thu, 20 Sep 2001, Crist J. Clark wrote:

>  On Thu, Sep 20, 2001 at 05:40:05AM -0700, David Malone wrote:
>  >  It adds 30 because it wants to round the number of minutes to the
>  >  nearest minute, instead of rounding down. Unfortunately this isn't
>  >  a sensible thing to do if you are also going to display the number
>  >  of seconds.
>
>  The only time this is noticed is when the time is printed in
>  seconds. This should fix it,
>
>  Index: src/usr.bin/w/w.c
>  ===================================================================
>  RCS file: /export/ncvs/src/usr.bin/w/w.c,v
>  retrieving revision 1.48
>  diff -u -r1.48 w.c
>  --- src/usr.bin/w/w.c   2001/07/26 19:20:13     1.48
>  +++ src/usr.bin/w/w.c   2001/09/20 19:00:57
>  @@ -452,13 +452,15 @@
>          if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
>              boottime.tv_sec != 0) {
>                  uptime = now - boottime.tv_sec;
>  +               /* Round to nearest minute. */
>                  uptime += 30;
>                  days = uptime / 86400;
>                  uptime %= 86400;
>                  hrs = uptime / 3600;
>                  uptime %= 3600;
>                  mins = uptime / 60;
>  -               secs = uptime % 60;
>  +               /* Undo rounding to calculate uptime in seconds. */
>  +               secs = (uptime - 30) % 60;
>                  (void)printf(" up");
>                  if (days > 0)
>                          (void)printf(" %d day%s,", days, days > 1 ? "s" : "");
>
>  I will commit the fix later unless someone has comments.

Just add 30 in the one place where it is needed:

		    mins = (uptime + 30) / 60;

Bruce
Comment 6 Crist J. Clark 2001-09-20 22:17:35 UTC
On Thu, Sep 20, 2001 at 01:20:01PM -0700, Bruce Evans wrote:
>  On Thu, 20 Sep 2001, Crist J. Clark wrote:
>  >  On Thu, Sep 20, 2001 at 05:40:05AM -0700, David Malone wrote:
>  >  >  It adds 30 because it wants to round the number of minutes to the
>  >  >  nearest minute, instead of rounding down. Unfortunately this isn't
>  >  >  a sensible thing to do if you are also going to display the number
>  >  >  of seconds.
>  >
>  >  The only time this is noticed is when the time is printed in
>  >  seconds. This should fix it,
>  >
>  >  Index: src/usr.bin/w/w.c
>  >  ===================================================================
>  >  RCS file: /export/ncvs/src/usr.bin/w/w.c,v
>  >  retrieving revision 1.48
>  >  diff -u -r1.48 w.c
>  >  --- src/usr.bin/w/w.c   2001/07/26 19:20:13     1.48
>  >  +++ src/usr.bin/w/w.c   2001/09/20 19:00:57
>  >  @@ -452,13 +452,15 @@
>  >          if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
>  >              boottime.tv_sec != 0) {
>  >                  uptime = now - boottime.tv_sec;
>  >  +               /* Round to nearest minute. */
>  >                  uptime += 30;
>  >                  days = uptime / 86400;
>  >                  uptime %= 86400;
>  >                  hrs = uptime / 3600;
>  >                  uptime %= 3600;
>  >                  mins = uptime / 60;
>  >  -               secs = uptime % 60;
>  >  +               /* Undo rounding to calculate uptime in seconds. */
>  >  +               secs = (uptime - 30) % 60;
>  >                  (void)printf(" up");
>  >                  if (days > 0)
>  >                          (void)printf(" %d day%s,", days, days > 1 ? "s" : "");
>  >
>  >  I will commit the fix later unless someone has comments.
>  
>  Just add 30 in the one place where it is needed:
>  
>  		    mins = (uptime + 30) / 60;

That won't actually work correctly. For example, if you were at,

  1 day 23:59:45

Just adding the 30 seconds when calculating 'mins' would produce,

  1 day 23:00

Instead of the correct,

  2 days 00:00

-- 
Crist J. Clark                           cjclark@alum.mit.edu
Comment 7 Crist J. Clark freebsd_committer freebsd_triage 2001-09-28 01:27:20 UTC
State Changed
From-To: analyzed->closed

Fix MFC'ed into STABLE.