From 2dbc7e4ffff0eaa4b07bdc8ee3db9df3226da52f Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Tue, 23 May 2023 22:10:06 +0100 Subject: [PATCH] Utilities/w: Convert TTY string from `/var/run/utmp` to TTY pseudo name Previously, we were comparing the "tty" value from `/sys/kernel/processes` to the TTY value from `/var/run/utmp` directly. This caused the "WHAT" column to always show "N/A", because the former is the TTY pseudo name, while the latter is the full device name. --- Userland/Utilities/w.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/Userland/Utilities/w.cpp b/Userland/Utilities/w.cpp index 85ceaf6054..71c53d3c8c 100644 --- a/Userland/Utilities/w.cpp +++ b/Userland/Utilities/w.cpp @@ -15,6 +15,23 @@ #include #include #include +#include + +static ErrorOr tty_stat_to_pseudo_name(struct stat const& tty_stat) +{ + int tty_device_major = major(tty_stat.st_rdev); + int tty_device_minor = minor(tty_stat.st_rdev); + + if (tty_device_major == 201) { + return String::formatted("pts:{}", tty_device_minor); + } + + if (tty_device_major == 4) { + return String::formatted("tty:{}", tty_device_minor); + } + + return Error::from_string_literal("Unknown TTY device type"); +} ErrorOr serenity_main(Main::Arguments) { @@ -57,6 +74,7 @@ ErrorOr serenity_main(Main::Arguments) StringBuilder builder; String idle_string = "n/a"_short_string; + String what = "n/a"_short_string; auto maybe_stat = Core::System::stat(tty); if (!maybe_stat.is_error()) { auto stat = maybe_stat.release_value(); @@ -65,13 +83,14 @@ ErrorOr serenity_main(Main::Arguments) builder.appendff("{}s", idle_time); idle_string = TRY(builder.to_string()); } - } - String what = "n/a"_short_string; - - for (auto& process : process_statistics.processes) { - if (process.tty == tty && process.pid == process.pgid) - what = TRY(String::from_deprecated_string(process.name)); + auto tty_pseudo_name = TRY(tty_stat_to_pseudo_name(stat)); + for (auto& process : process_statistics.processes) { + if (tty_pseudo_name == process.tty.view() && process.pid == process.pgid) { + what = TRY(String::from_deprecated_string(process.name)); + break; + } + } } outln("{:10} {:12} {:16} {:6} {}", username, tty, login_at, idle_string, what);