2019-06-29 10:12:40 +02:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
|
|
|
#include <LibCore/CFile.h>
|
|
|
|
#include <LibCore/CProcessStatisticsReader.h>
|
2019-05-16 18:47:47 +02:00
|
|
|
#include <pwd.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <stdio.h>
|
2019-05-16 18:47:47 +02:00
|
|
|
|
2019-07-10 13:56:28 +02:00
|
|
|
HashMap<uid_t, String> CProcessStatisticsReader::s_usernames;
|
2019-05-16 18:47:47 +02:00
|
|
|
|
2019-07-10 13:56:28 +02:00
|
|
|
HashMap<pid_t, CProcessStatistics> CProcessStatisticsReader::get_all()
|
2019-05-16 18:47:47 +02:00
|
|
|
{
|
|
|
|
CFile file("/proc/all");
|
|
|
|
if (!file.open(CIODevice::ReadOnly)) {
|
2019-07-10 15:32:07 +02:00
|
|
|
fprintf(stderr, "CProcessStatisticsReader: Failed to open /proc/all: %s\n", file.error_string());
|
2019-07-10 13:56:28 +02:00
|
|
|
return {};
|
2019-05-16 18:47:47 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 15:32:07 +02:00
|
|
|
HashMap<pid_t, CProcessStatistics> map;
|
|
|
|
|
2019-06-29 10:12:40 +02:00
|
|
|
auto file_contents = file.read_all();
|
|
|
|
auto json = JsonValue::from_string({ file_contents.data(), file_contents.size() });
|
|
|
|
json.as_array().for_each([&](auto& value) {
|
|
|
|
const JsonObject& process_object = value.as_object();
|
2019-05-16 18:47:47 +02:00
|
|
|
CProcessStatistics process;
|
2019-07-17 22:22:22 +02:00
|
|
|
|
|
|
|
// kernel data first
|
2019-07-03 21:17:35 +02:00
|
|
|
process.pid = process_object.get("pid").to_u32();
|
2019-07-17 22:22:22 +02:00
|
|
|
process.times_scheduled = process_object.get("times_scheduled").to_u32();
|
|
|
|
process.pgid = process_object.get("pgid").to_u32();
|
|
|
|
process.sid = process_object.get("sid").to_u32();
|
2019-07-03 21:17:35 +02:00
|
|
|
process.uid = process_object.get("uid").to_u32();
|
2019-07-17 22:22:22 +02:00
|
|
|
process.gid = process_object.get("gid").to_u32();
|
2019-06-29 10:12:40 +02:00
|
|
|
process.state = process_object.get("state").to_string();
|
2019-07-17 22:22:22 +02:00
|
|
|
process.ppid = process_object.get("ppid").to_u32();
|
|
|
|
process.nfds = process_object.get("nfds").to_u32();
|
2019-06-29 10:12:40 +02:00
|
|
|
process.name = process_object.get("name").to_string();
|
2019-07-17 22:22:22 +02:00
|
|
|
process.tty = process_object.get("tty").to_string();
|
|
|
|
process.amount_virtual = process_object.get("amount_virtual").to_u32();
|
|
|
|
process.amount_resident = process_object.get("amount_resident").to_u32();
|
|
|
|
process.amount_shared = process_object.get("amount_shared").to_u32();
|
|
|
|
process.ticks = process_object.get("ticks").to_u32();
|
|
|
|
process.priority = process_object.get("priority").to_string();
|
|
|
|
process.syscall_count = process_object.get("syscall_count").to_u32();
|
|
|
|
|
|
|
|
// and synthetic data last
|
|
|
|
process.username = username_from_uid(process.uid);
|
2019-05-16 18:47:47 +02:00
|
|
|
map.set(process.pid, process);
|
2019-06-29 10:12:40 +02:00
|
|
|
});
|
2019-07-10 13:56:28 +02:00
|
|
|
|
|
|
|
return map;
|
2019-05-16 18:47:47 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 13:56:28 +02:00
|
|
|
String CProcessStatisticsReader::username_from_uid(uid_t uid)
|
2019-05-16 18:47:47 +02:00
|
|
|
{
|
2019-07-10 13:56:28 +02:00
|
|
|
if (s_usernames.is_empty()) {
|
|
|
|
setpwent();
|
|
|
|
while (auto* passwd = getpwent())
|
|
|
|
s_usernames.set(passwd->pw_uid, passwd->pw_name);
|
|
|
|
endpwent();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = s_usernames.find(uid);
|
|
|
|
if (it != s_usernames.end())
|
2019-05-16 18:47:47 +02:00
|
|
|
return (*it).value;
|
2019-07-10 13:56:28 +02:00
|
|
|
return String::number(uid);
|
2019-05-16 18:47:47 +02:00
|
|
|
}
|
2019-07-10 13:56:28 +02:00
|
|
|
|