From 4e6959077f2615aacc7d643484b2e17b6861a130 Mon Sep 17 00:00:00 2001 From: Dan Cross Date: Mon, 11 Jan 2021 14:33:02 +0000 Subject: [PATCH] w: Take into account terminal session ID. The 'w' command tries to figure out what process is running on a TTY line and emit that as part of its output. There are heuristics in the command to select the "most interesting" process. Unfortunately, at present these are not particularly interesting. In particular, if logged in over the network, one often sees the network daemon itself, which isn't terribly useful. For example: 2:32PM up 2 days, 16:34, 2 users, load averages: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE WHAT cross pts/0 gw.net 2:53AM - sshd: cross@pts/0 (sshd) user pts/2 vhf1.net 11:27AM - sshd: user@pts/2 (sshd) This patch changes the command to take the session ID associated with a terminal account, leading to the command making better choices: 2:36PM up 2 days, 16:38, 2 users, load averages: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE WHAT cross pts/0 gw.net 2:53AM - w user pts/2 vhf1.net 11:27AM - more /usr/include/sys/kinfo.h Also, skip the command if `kp_comm` is "login", which allows something interesting to be reported if the user is logged in via telnet (!!): 2:51PM up 2 days, 16:53, 3 users, load averages: 1.34, 0.43, 0.16 USER TTY FROM LOGIN@ IDLE WHAT cross pts/0 gw.user 2:53AM - w cross pts/1 vhf1.user 2:36PM - more /usr/include/sys/kinfo.h user p0 vhf1.user.inet.org 2:36PM - login [pam] (login) user% ./w 2:51PM up 2 days, 16:53, 3 users, load averages: 1.24, 0.42, 0.16 USER TTY FROM LOGIN@ IDLE WHAT cross pts/0 gw.user 2:53AM - ./w cross pts/1 vhf1.user 2:36PM - more /usr/include/sys/kinfo.h user p0 vhf1.user.inet.org 2:36PM - -bash (bash) user% Signed-off-by: Dan Cross --- usr.bin/w/w.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/usr.bin/w/w.c b/usr.bin/w/w.c index cafb3c91e2..e04fcc8387 100644 --- a/usr.bin/w/w.c +++ b/usr.bin/w/w.c @@ -269,8 +269,15 @@ main(int argc, char **argv) if ((kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nentries)) == NULL) err(1, "%s", kvm_geterr(kd)); for (i = 0; i < nentries; i++, kp++) { + /* + * login(1) is a special case. + */ + if (strcmp(kp->kp_comm, "login") == 0) + continue; if (kp->kp_stat == SIDL || kp->kp_stat == SZOMB) continue; + if (kp->kp_sid != kp->kp_tsid) + continue; for (ep = ehead; ep != NULL; ep = ep->next) { if (ep->tdev == kp->kp_tdev) { /* -- 2.28.0