mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 23:17:46 +00:00
LibCore: Use Core::Stream
for ProcessStatisticsReader
This commit is contained in:
parent
e338a0656d
commit
8940f2da7f
18 changed files with 55 additions and 95 deletions
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue