diff options
author | Tim Schumacher <timschumi@gmx.de> | 2022-12-08 14:50:31 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-12-10 11:49:24 +0000 |
commit | 8940f2da7fec4b4e375777c861e65ff04465f255 (patch) | |
tree | d1ac58cae77af94ea378a93fb818e8787faed542 /Userland/Libraries | |
parent | e338a0656dd8e99e9ed64abf98d15e219adb9755 (diff) | |
download | serenity-8940f2da7fec4b4e375777c861e65ff04465f255.zip |
LibCore: Use `Core::Stream` for `ProcessStatisticsReader`
Diffstat (limited to 'Userland/Libraries')
4 files changed, 14 insertions, 32 deletions
diff --git a/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp b/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp index c5f79f6d2e..8f2ea44cc0 100644 --- a/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp +++ b/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp @@ -8,7 +8,6 @@ #include <AK/JsonArray.h> #include <AK/JsonObject.h> #include <AK/JsonValue.h> -#include <LibCore/File.h> #include <LibCore/ProcessStatisticsReader.h> #include <pwd.h> @@ -16,29 +15,14 @@ namespace Core { HashMap<uid_t, DeprecatedString> ProcessStatisticsReader::s_usernames; -Optional<AllProcessesStatistics> ProcessStatisticsReader::get_all(RefPtr<Core::File>& proc_all_file, bool include_usernames) +ErrorOr<AllProcessesStatistics> ProcessStatisticsReader::get_all(Core::Stream::SeekableStream& proc_all_file, bool include_usernames) { - if (proc_all_file) { - if (!proc_all_file->seek(0, Core::SeekMode::SetPosition)) { - warnln("ProcessStatisticsReader: Failed to refresh /sys/kernel/processes: {}", proc_all_file->error_string()); - return {}; - } - } else { - proc_all_file = Core::File::construct("/sys/kernel/processes"); - if (!proc_all_file->open(Core::OpenMode::ReadOnly)) { - warnln("ProcessStatisticsReader: Failed to open /sys/kernel/processes: {}", proc_all_file->error_string()); - return {}; - } - } + TRY(proc_all_file.seek(0, Core::Stream::SeekMode::SetPosition)); AllProcessesStatistics all_processes_statistics; - auto file_contents = proc_all_file->read_all(); - auto json = JsonValue::from_string(file_contents); - if (json.is_error()) - return {}; - - auto& json_obj = json.value().as_object(); + auto file_contents = TRY(proc_all_file.read_all()); + auto json_obj = TRY(JsonValue::from_string(file_contents)).as_object(); json_obj.get("processes"sv).as_array().for_each([&](auto& value) { const JsonObject& process_object = value.as_object(); Core::ProcessStatistics process; @@ -104,10 +88,10 @@ Optional<AllProcessesStatistics> ProcessStatisticsReader::get_all(RefPtr<Core::F return all_processes_statistics; } -Optional<AllProcessesStatistics> ProcessStatisticsReader::get_all(bool include_usernames) +ErrorOr<AllProcessesStatistics> ProcessStatisticsReader::get_all(bool include_usernames) { - RefPtr<Core::File> proc_all_file; - return get_all(proc_all_file, include_usernames); + auto proc_all_file = TRY(Core::Stream::File::open("/sys/kernel/processes"sv, Core::Stream::OpenMode::Read)); + return get_all(*proc_all_file, include_usernames); } DeprecatedString ProcessStatisticsReader::username_from_uid(uid_t uid) diff --git a/Userland/Libraries/LibCore/ProcessStatisticsReader.h b/Userland/Libraries/LibCore/ProcessStatisticsReader.h index 1de6a7c03b..a5d4c9a679 100644 --- a/Userland/Libraries/LibCore/ProcessStatisticsReader.h +++ b/Userland/Libraries/LibCore/ProcessStatisticsReader.h @@ -7,7 +7,7 @@ #pragma once #include <AK/DeprecatedString.h> -#include <LibCore/File.h> +#include <LibCore/Stream.h> #include <unistd.h> namespace Core { @@ -72,8 +72,8 @@ struct AllProcessesStatistics { class ProcessStatisticsReader { public: - static Optional<AllProcessesStatistics> get_all(RefPtr<Core::File>&, bool include_usernames = true); - static Optional<AllProcessesStatistics> get_all(bool include_usernames = true); + static ErrorOr<AllProcessesStatistics> get_all(Core::Stream::SeekableStream&, bool include_usernames = true); + static ErrorOr<AllProcessesStatistics> get_all(bool include_usernames = true); private: static DeprecatedString username_from_uid(uid_t); diff --git a/Userland/Libraries/LibCore/SessionManagement.cpp b/Userland/Libraries/LibCore/SessionManagement.cpp index acbe29e828..fb78e5dfae 100644 --- a/Userland/Libraries/LibCore/SessionManagement.cpp +++ b/Userland/Libraries/LibCore/SessionManagement.cpp @@ -22,14 +22,12 @@ static ErrorOr<Core::ProcessStatistics const*> get_proc(Core::AllProcessesStatis ErrorOr<pid_t> root_session_id(Optional<pid_t> force_sid) { - auto stats = Core::ProcessStatisticsReader::get_all(false); - if (!stats.has_value()) - return Error::from_string_literal("Failed to get all process statistics"); + auto stats = TRY(Core::ProcessStatisticsReader::get_all(false)); pid_t sid = (force_sid.has_value()) ? force_sid.value() : TRY(System::getsid()); while (true) { - pid_t parent = TRY(get_proc(stats.value(), sid))->ppid; - pid_t parent_sid = TRY(get_proc(stats.value(), parent))->sid; + pid_t parent = TRY(get_proc(stats, sid))->ppid; + pid_t parent_sid = TRY(get_proc(stats, parent))->sid; if (parent_sid == 0) break; diff --git a/Userland/Libraries/LibGUI/RunningProcessesModel.cpp b/Userland/Libraries/LibGUI/RunningProcessesModel.cpp index f4bc37124f..d0af1001a0 100644 --- a/Userland/Libraries/LibGUI/RunningProcessesModel.cpp +++ b/Userland/Libraries/LibGUI/RunningProcessesModel.cpp @@ -21,7 +21,7 @@ void RunningProcessesModel::update() m_processes.clear(); auto all_processes = Core::ProcessStatisticsReader::get_all(); - if (all_processes.has_value()) { + if (!all_processes.is_error()) { for (auto& it : all_processes.value().processes) { Process process; process.pid = it.pid; |