diff options
author | GuillaumeGas <guillaume@gas-ntic.fr> | 2019-05-16 18:47:47 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-05-16 18:47:47 +0200 |
commit | 0ba4ceb2cd1768336116368a360acc922091e63a (patch) | |
tree | 43156868d17b180bc5b751ce29bdfebc6f32a1cd /LibCore | |
parent | 174639b7f06ec9e5f09dfa7f59dc5b62d2940fc6 (diff) | |
download | serenity-0ba4ceb2cd1768336116368a360acc922091e63a.zip |
Added CProcessStatisticsReader to avoid having to parse /proc/all (#35)
* Added CProcessStatisticsReader to avoid having to parse /proc/all
* Took @awesomekling's feedbacks into account
* Fixed indentation and replaced tabs by spaces
Diffstat (limited to 'LibCore')
-rw-r--r-- | LibCore/CProcessStatisticsReader.cpp | 100 | ||||
-rw-r--r-- | LibCore/CProcessStatisticsReader.h | 29 | ||||
-rw-r--r-- | LibCore/Makefile | 3 |
3 files changed, 131 insertions, 1 deletions
diff --git a/LibCore/CProcessStatisticsReader.cpp b/LibCore/CProcessStatisticsReader.cpp new file mode 100644 index 0000000000..1a80f306c3 --- /dev/null +++ b/LibCore/CProcessStatisticsReader.cpp @@ -0,0 +1,100 @@ +#include "CProcessStatisticsReader.h" +#include "CFile.h" + +#include <stdio.h> +#include <pwd.h> + +CProcessStatisticsReader::CProcessStatisticsReader() +{ + setpwent(); + while (auto* passwd = getpwent()) + m_usernames.set(passwd->pw_uid, passwd->pw_name); + endpwent(); +} + +HashMap<pid_t, CProcessStatistics> CProcessStatisticsReader::get_map() +{ + HashMap<pid_t, CProcessStatistics> res; + update_map(res); + return res; +} + +void CProcessStatisticsReader::update_map(HashMap<pid_t, CProcessStatistics>& map) +{ + CFile file("/proc/all"); + if (!file.open(CIODevice::ReadOnly)) { + fprintf(stderr, "CProcessHelper : failed to open /proc/all: %s\n", file.error_string()); + return; + } + + for (;;) { + auto line = file.read_line(1024); + + if (line.is_empty()) + break; + + auto chomped = String((const char*)line.pointer(), line.size() - 1, Chomp); + auto parts = chomped.split_view(','); + + if (parts.size() < 18) + break; + + bool ok = false; + CProcessStatistics process; + + process.pid = parts[0].to_uint(ok); + if (!ok) { + fprintf(stderr, "CProcessHelper : couldn't convert %s to a valid pid\n", parts[0].characters()); + return; + } + + process.nsched = parts[1].to_uint(ok); + if (!ok) { + fprintf(stderr, "CProcessHelper : couldn't convert %s to a valid nsched value\n", parts[1].characters()); + return; + } + + uid_t uid = parts[5].to_uint(ok); + if (!ok) { + fprintf(stderr, "CProcessHelper : couldn't convert %s to a valid uid value\n", parts[5].characters()); + return; + } + + process.uid = uid; + process.username = get_username_from_uid(uid); + + process.priority = parts[16]; + + process.syscalls = parts[17].to_uint(ok); + if (!ok) { + fprintf(stderr, "CProcessHelper : couldn't convert %s to a valid syscalls count value\n", parts[17].characters()); + return; + } + + process.state = parts[7]; + + process.name = parts[11]; + process.linear = parts[12].to_uint(ok); + if (!ok) { + fprintf(stderr, "CProcessHelper : couldn't convert %s to a valid amount of linear address space used\n", parts[12].characters()); + return; + } + + process.physical = parts[13].to_uint(ok); + if (!ok) { + fprintf(stderr, "CProcessHelper : couldn't convert %s to a valid amount of physical address space used\n", parts[13].characters()); + return; + } + + map.set(process.pid, process); + } +} + +String CProcessStatisticsReader::get_username_from_uid(const uid_t uid) +{ + auto it = m_usernames.find(uid); + if (it != m_usernames.end()) + return (*it).value; + else + return String::format("%u", uid); +} diff --git a/LibCore/CProcessStatisticsReader.h b/LibCore/CProcessStatisticsReader.h new file mode 100644 index 0000000000..2ce97e9f68 --- /dev/null +++ b/LibCore/CProcessStatisticsReader.h @@ -0,0 +1,29 @@ +#pragma once + +#include <AK/AKString.h> +#include <AK/HashMap.h> + +struct CProcessStatistics { + pid_t pid; + unsigned nsched; + String name; + String state; + String username; + uid_t uid; + String priority; + size_t linear; + size_t physical; + unsigned syscalls; +}; + +class CProcessStatisticsReader { +public: + CProcessStatisticsReader(); + HashMap<pid_t, CProcessStatistics> get_map(); + +private: + void update_map(HashMap<pid_t, CProcessStatistics>& map); + String get_username_from_uid(const uid_t uid); + + HashMap<uid_t, String> m_usernames; +}; diff --git a/LibCore/Makefile b/LibCore/Makefile index 710a77ae65..a8a7fb00d6 100644 --- a/LibCore/Makefile +++ b/LibCore/Makefile @@ -16,7 +16,8 @@ OBJS = \ CTimer.o \ CEventLoop.o \ CConfigFile.o \ - CEvent.o + CEvent.o \ + CProcessStatisticsReader.o LIBRARY = libcore.a DEFINES += -DUSERLAND |