From 857bb39d31e55df015d0b73913a09be847a2f8e7 Mon Sep 17 00:00:00 2001 From: Craig Small Date: Tue, 21 Jul 2015 22:25:06 +1000 Subject: [PATCH] w: Fix choice of current process 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 --- w.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/w.c b/w.c index 4c8a6193..b310c190 100644 --- 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; -- 2.40.0