diff options
author | Andreas Kling <kling@serenityos.org> | 2020-09-06 19:05:08 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-09-06 19:05:08 +0200 |
commit | d531b4fa616d241c62336816714c37dea9207ae8 (patch) | |
tree | ff0ef07fef74a2fe53c5186fd48dcf894b5b26a6 /Userland | |
parent | 6d8c4af9c2c1ec86e526c7b73f23f0c5477ee338 (diff) | |
download | serenity-d531b4fa616d241c62336816714c37dea9207ae8.zip |
Userland: Show the current foreground process name in "w" output
Add a "WHAT" column that shows which command is currently executing in
each active session. Very neat :^)
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/w.cpp | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/Userland/w.cpp b/Userland/w.cpp index 55f86f6a61..4d2124ae60 100644 --- a/Userland/w.cpp +++ b/Userland/w.cpp @@ -1,6 +1,7 @@ #include <AK/JsonObject.h> #include <AK/JsonValue.h> #include <LibCore/File.h> +#include <LibCore/ProcessStatisticsReader.h> #include <pwd.h> #include <stdio.h> #include <sys/stat.h> @@ -28,6 +29,11 @@ int main() return 1; } + if (unveil("/proc", "r") < 0) { + perror("unveil"); + return 1; + } + unveil(nullptr, nullptr); auto file_or_error = Core::File::open("/var/run/utmp", Core::IODevice::ReadOnly); @@ -42,10 +48,12 @@ int main() return 1; } + auto process_statistics = Core::ProcessStatisticsReader::get_all(); + auto now = time(nullptr); - printf("\033[1m%-10s %-12s %-16s %-6s\033[0m\n", - "USER", "TTY", "FROM", "IDLE"); + printf("\033[1m%-10s %-12s %-16s %-6s %s\033[0m\n", + "USER", "TTY", "FROM", "IDLE", "WHAT"); json.value().as_object().for_each_member([&](auto& tty, auto& value) { const JsonObject& entry = value.as_object(); auto uid = entry.get("uid").to_u32(); @@ -71,11 +79,19 @@ int main() } } - printf("%-10s %-12s %-16s %-6s\n", + String what = "n/a"; + + for (auto& it : process_statistics) { + if (it.value.tty == tty && it.value.pid == it.value.pgid) + what = it.value.name; + } + + printf("%-10s %-12s %-16s %-6s %s\n", username.characters(), tty.characters(), from.characters(), - idle_string.characters()); + idle_string.characters(), + what.characters()); }); return 0; } |