diff options
author | Liav A <liavalb@gmail.com> | 2022-03-26 09:22:45 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-03-26 11:01:49 +0100 |
commit | d7c7e6e496594986ed52bb83be589e710d5f00da (patch) | |
tree | ab7f9a25eb703d86b00ba43afe52050cba0e7234 /Userland/Utilities | |
parent | 7053d7ece3c222aa74e7052ea4ca5bfbd9f40974 (diff) | |
download | serenity-d7c7e6e496594986ed52bb83be589e710d5f00da.zip |
Utilities/ps: Don't assume the kernel can provide real TTY paths
Diffstat (limited to 'Userland/Utilities')
-rw-r--r-- | Userland/Utilities/ps.cpp | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/Userland/Utilities/ps.cpp b/Userland/Utilities/ps.cpp index 73eca9db5b..7b6f934fc2 100644 --- a/Userland/Utilities/ps.cpp +++ b/Userland/Utilities/ps.cpp @@ -9,12 +9,33 @@ #include <LibCore/ProcessStatisticsReader.h> #include <LibCore/System.h> #include <LibMain/Main.h> +#include <sys/sysmacros.h> #include <unistd.h> +static ErrorOr<String> determine_tty_pseudo_name() +{ + struct stat tty_stat; + if (fstat(STDIN_FILENO, &tty_stat) < 0) { + int saved_errno = errno; + perror("fstat"); + return Error::from_errno(saved_errno); + } + 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); + } + VERIFY_NOT_REACHED(); +} + ErrorOr<int> serenity_main(Main::Arguments arguments) { TRY(Core::System::pledge("stdio rpath tty")); - String this_tty = ttyname(STDIN_FILENO); + + auto this_pseudo_tty_name = TRY(determine_tty_pseudo_name()); TRY(Core::System::pledge("stdio rpath")); TRY(Core::System::unveil("/proc/all", "r")); @@ -119,20 +140,17 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) for (auto const& process : processes) { auto tty = process.tty; - - if (!every_process_flag && tty != this_tty) + if (!every_process_flag && tty != this_pseudo_tty_name) continue; - if (tty.starts_with("/dev/")) - tty = tty.characters() + 5; - else - tty = "n/a"; - auto* state = process.threads.is_empty() ? "Zombie" : process.threads.first().state.characters(); Vector<String> row; TRY(row.try_resize(columns.size())); + if (tty == "") + tty = "n/a"; + if (uid_column != -1) row[uid_column] = process.username; if (pid_column != -1) |