Serenity Operating System
at portability 124 lines 6.0 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/JsonArray.h> 28#include <AK/JsonObject.h> 29#include <AK/JsonValue.h> 30#include <LibCore/File.h> 31#include <LibCore/ProcessStatisticsReader.h> 32#include <pwd.h> 33#include <stdio.h> 34 35namespace Core { 36 37HashMap<uid_t, String> ProcessStatisticsReader::s_usernames; 38 39HashMap<pid_t, Core::ProcessStatistics> ProcessStatisticsReader::get_all() 40{ 41 auto file = Core::File::construct("/proc/all"); 42 if (!file->open(Core::IODevice::ReadOnly)) { 43 fprintf(stderr, "CProcessStatisticsReader: Failed to open /proc/all: %s\n", file->error_string()); 44 return {}; 45 } 46 47 HashMap<pid_t, Core::ProcessStatistics> map; 48 49 auto file_contents = file->read_all(); 50 auto json = JsonValue::from_string({ file_contents.data(), (size_t)file_contents.size() }); 51 json.as_array().for_each([&](auto& value) { 52 const JsonObject& process_object = value.as_object(); 53 Core::ProcessStatistics process; 54 55 // kernel data first 56 process.pid = process_object.get("pid").to_u32(); 57 process.pgid = process_object.get("pgid").to_u32(); 58 process.pgp = process_object.get("pgp").to_u32(); 59 process.sid = process_object.get("sid").to_u32(); 60 process.uid = process_object.get("uid").to_u32(); 61 process.gid = process_object.get("gid").to_u32(); 62 process.ppid = process_object.get("ppid").to_u32(); 63 process.nfds = process_object.get("nfds").to_u32(); 64 process.name = process_object.get("name").to_string(); 65 process.tty = process_object.get("tty").to_string(); 66 process.pledge = process_object.get("pledge").to_string(); 67 process.veil = process_object.get("veil").to_string(); 68 process.amount_virtual = process_object.get("amount_virtual").to_u32(); 69 process.amount_resident = process_object.get("amount_resident").to_u32(); 70 process.amount_shared = process_object.get("amount_shared").to_u32(); 71 process.amount_dirty_private = process_object.get("amount_dirty_private").to_u32(); 72 process.amount_clean_inode = process_object.get("amount_clean_inode").to_u32(); 73 process.amount_purgeable_volatile = process_object.get("amount_purgeable_volatile").to_u32(); 74 process.amount_purgeable_nonvolatile = process_object.get("amount_purgeable_nonvolatile").to_u32(); 75 process.icon_id = process_object.get("icon_id").to_int(); 76 77 auto& thread_array = process_object.get_ptr("threads")->as_array(); 78 process.threads.ensure_capacity(thread_array.size()); 79 thread_array.for_each([&](auto& value) { 80 auto& thread_object = value.as_object(); 81 Core::ThreadStatistics thread; 82 thread.tid = thread_object.get("tid").to_u32(); 83 thread.times_scheduled = thread_object.get("times_scheduled").to_u32(); 84 thread.name = thread_object.get("name").to_string(); 85 thread.state = thread_object.get("state").to_string(); 86 thread.ticks = thread_object.get("ticks").to_u32(); 87 thread.priority = thread_object.get("priority").to_u32(); 88 thread.effective_priority = thread_object.get("effective_priority").to_u32(); 89 thread.syscall_count = thread_object.get("syscall_count").to_u32(); 90 thread.inode_faults = thread_object.get("inode_faults").to_u32(); 91 thread.zero_faults = thread_object.get("zero_faults").to_u32(); 92 thread.cow_faults = thread_object.get("cow_faults").to_u32(); 93 thread.unix_socket_read_bytes = thread_object.get("unix_socket_read_bytes").to_u32(); 94 thread.unix_socket_write_bytes = thread_object.get("unix_socket_write_bytes").to_u32(); 95 thread.ipv4_socket_read_bytes = thread_object.get("ipv4_socket_read_bytes").to_u32(); 96 thread.ipv4_socket_write_bytes = thread_object.get("ipv4_socket_write_bytes").to_u32(); 97 thread.file_read_bytes = thread_object.get("file_read_bytes").to_u32(); 98 thread.file_write_bytes = thread_object.get("file_write_bytes").to_u32(); 99 process.threads.append(move(thread)); 100 }); 101 102 // and synthetic data last 103 process.username = username_from_uid(process.uid); 104 map.set(process.pid, process); 105 }); 106 107 return map; 108} 109 110String ProcessStatisticsReader::username_from_uid(uid_t uid) 111{ 112 if (s_usernames.is_empty()) { 113 setpwent(); 114 while (auto* passwd = getpwent()) 115 s_usernames.set(passwd->pw_uid, passwd->pw_name); 116 endpwent(); 117 } 118 119 auto it = s_usernames.find(uid); 120 if (it != s_usernames.end()) 121 return (*it).value; 122 return String::number(uid); 123} 124}