]> granicus.if.org Git - procps-ng/commitdiff
w: Fix choice of current process
authorCraig Small <csmall@enc.com.au>
Tue, 21 Jul 2015 12:25:06 +0000 (22:25 +1000)
committerCraig Small <csmall@enc.com.au>
Tue, 21 Jul 2015 12:25:06 +0000 (22:25 +1000)
If there is a PID wrap-around w will choose the wrong process.
For example:
$ ps x -o pgrp,tpgid,start,tty,cmd | grep pts/3
 3834  3834 21:50:26 pts/3    ssh server
 4461  4461 21:57:14 pts/2    grep pts/3
23410  3834 21:07:17 pts/3    mutt
26071  3834   Jul 13 pts/3    /bin/bash

w will show the user as:
csmall   pts/3    my-laptop:S.1   13Jul15  5:54   1.36s  1.13s /bin/bash

So why?
w scans the process table and has two ways of finding the best match.
 #1 match things like terminal,username and process group, find oldest
 #2 match utmp pid to process tgid

The problem is that #2 trumped #1, which is fine when your login process
is numerically lower than your other processes. However in this case
26071 is larger and appears later in the readdir() than the correct
process, which is 3834.

The fix is not not overwrite best if it already exists.

Signed-off-by: Craig Small <csmall@enc.com.au>
w.c

diff --git a/w.c b/w.c
index 4c8a6193d929319e66b732bc5430706975ef9ef3..b310c190a433d5ce880fdadb75fbd4ab570040ec 100644 (file)
--- a/w.c
+++ b/w.c
@@ -347,7 +347,8 @@ static const proc_t *getproc(const utmp_t * restrict const u,
                const proc_t *restrict const tmp = *pptr;
                if (unlikely(tmp->tgid == u->ut_pid)) {
                        *found_utpid = 1;
-                       best = tmp;
+            if (!best)
+                best = tmp;
                }
                if (tmp->tty != line)
                        continue;