[prev in list] [next in list] [prev in thread] [next in thread] 

List:       opensolaris-code
Subject:    [osol-code] 'w' bug and proposed solution
From:       matty91 () gmail ! com (Matty)
Date:       2005-12-21 8:39:14
Message-ID: Pine.GSO.4.64.0512211059430.25670 () tigger
[Download RAW message or body]


Howdy,

While perusing some system information today, I came across a discrepancy
(which I would think is a bug -- at least according to the idle time 
description in the w manual pages) in the 'w' output:

$ date
Wed Dec 21 10:19:59 EST 2005

$ w
  10:19am  up 5 day(s), 19:52,  2 users,  load average: 0.02, 0.03, 0.04
User     tty           login@  idle   JCPU   PCPU  what
user1234 pts/1        10:19am    51                myapp

$ who -u
user1234   pts/1        Dec 21 10:19   .    10503      (1.2.3.4)

The user "user1234" logged in at 10:19, and according to w has been idle for 51 
minutes (since they have been logged in for one minute or less, they obviously 
cannot be idle for 51 minutes). A quick check of w.c reveals that idle time is 
calculated using the atime from a stat of /dev/pts/[1-9]+:

if (stat(ttyname, &stbuf) != -1) {
     lastaction = stbuf.st_atime;
     diff = now - lastaction;
     diff = DIV60(diff);

While who.c uses the mtime from a stat of /dev/pts/[1-9]+ (anyone happen 
to know why these utilities use different stat structure members to 
calculate idle time?):

if ((rc = stat(path, stbufp)) != -1) {
     idle = timnow - stbufp->st_mtime;

To understand when atime and mtime are updated, we took a gander at the stat(2) 
manual page:

st_atime        Time  when  file  data  was  last  accessed.
                 Changed by the following functions: creat(),
                 mknod(), pipe(), utime(2), and read(2).

st_mtime        Time when data was last modified. Changed by
                 the  following  functions: creat(), mknod(),
                 pipe(), utime(), and write(2).

While it's difficult to accurately predict idle time, I would like to propose 
one or more of the following solutions to fix this inconsistency (or to 
adjust the documentation to explain the inconsistency):

1. Adjust the w and who manual page to indicate that who assumes the user is 
not idle if the terminal device is written to, where w assumes the user is 
not idle if the terminal device is read from (the second description 
could help explain why the the value '51' is shown when /dev/pts/X is 
not read/creat/mknod/pipe/utime'ed for a lengthy period of time).

2. Change who.c to use atime so the data from both utilities matches up, and 
add an if statement to who.c/w.c to compare atime with the time the user 
logged in:

# From FreeBSD source code
touched = stp->st_atime;
if (touched < ep->utmp.ut_time) {
    /* tty untouched since before login */
    touched = ep->utmp.ut_time;
}
if ((ep->idle = now - touched) < 0)
    ep->idle = 0;

3. Do nothing since changing who.c/w.c could concievably break shell 
scripts that rely on the incorrect idle times.

Comments and suggestions are welcome,

Thanks,
Clay / Ryan

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic