Serenity Operating System
at hosted 236 lines 9.1 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 35#ifdef __OpenBSD__ 36#include <sys/ioctl.h> 37#include <sys/param.h> 38#define PLEDGENAMES 39#include <sys/pledge.h> 40#include <sys/proc.h> 41#include <sys/resource.h> 42#include <sys/stat.h> 43#include <sys/sysctl.h> 44#include <sys/time.h> 45#include <sys/types.h> 46#include <kvm.h> 47#include <limits.h> 48#include <string.h> 49#endif 50 51namespace Core { 52 53HashMap<uid_t, String> ProcessStatisticsReader::s_usernames; 54 55HashMap<pid_t, Core::ProcessStatistics> ProcessStatisticsReader::get_all() 56{ 57 HashMap<pid_t, Core::ProcessStatistics> map; 58 59#ifdef __OpenBSD__ 60 kvm_t *kd; 61 struct kinfo_proc *kp; 62 struct kinfo_file *kf; 63 char errbuf[_POSIX2_LINE_MAX]; 64 int nentries, i; 65 66 kd = kvm_openfiles(nullptr, nullptr, nullptr, KVM_NO_FILES, errbuf); 67 kp = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(*kp), &nentries); 68 if (kp == NULL) { 69 perror("kvm_getprocs"); 70 ASSERT_NOT_REACHED(); 71 } 72 73 for (i = 0; i < nentries; i++) { 74 Core::ProcessStatistics process; 75 char *ttname; 76 int nfiles, j; 77 78 memset(&process, 0, sizeof(process)); 79 80 process.pid = kp[i].p_pid; 81 process.pgid = kp[i].p__pgid; 82 process.pgp = kp[i].p_tpgid; 83 process.sid = kp[i].p_sid; 84 process.uid = kp[i].p_uid; 85 process.gid = kp[i].p_gid; 86 process.ppid = kp[i].p_gid; 87 88 if ((kf = kvm_getfiles(kd, KERN_FILE_BYPID, process.pid, sizeof(*kf), &nfiles)) != NULL) 89 process.nfds = nfiles; 90 91 process.name = String(kp[i].p_comm); 92 if ((int)kp[i].p_tdev != (int)NODEV && (ttname = devname(kp[i].p_tdev, S_IFCHR))) 93 process.tty = String(ttname); 94 95 StringBuilder pledge; 96 97 for (j = 0; pledgenames[j].bits != 0; j++) { 98 if (pledgenames[j].bits & kp[i].p_pledge) { 99 if (pledge.length()) 100 pledge.append(' '); 101 pledge.append(pledgenames[j].name); 102 } 103 } 104 105 process.pledge = pledge.to_string(); 106 107 if (kp[i].p_eflag & EPROC_UNVEIL) { 108 if (kp[i].p_eflag & EPROC_LKUNVEIL) 109 process.veil = String("Locked"); 110 else 111 process.veil = String("Dropped"); 112 } else 113 process.veil = String("None"); 114 115 process.amount_virtual = (kp[i].p_vm_dsize + kp[i].p_vm_ssize + kp[i].p_vm_tsize) * getpagesize(); 116 process.amount_resident = kp[i].p_vm_rssize * getpagesize(); 117 // no shared info 118 119 Core::ThreadStatistics thread; 120 memset(&thread, 0, sizeof(thread)); 121 122 thread.tid = kp[i].p_pid; 123 thread.name = String(kp[i].p_comm); 124 125 switch (kp[i].p_stat) { 126 case SSTOP: 127 thread.state = String("Stopped"); 128 break; 129 case SSLEEP: 130 if (kp[i].p_flag & P_SINTR) 131 thread.state = String("Sleeping"); 132 else 133 thread.state = String("Disk"); 134 break; 135 case SRUN: 136 case SIDL: 137 case SONPROC: 138 thread.state = "Runnable"; 139 break; 140 case SDEAD: 141 thread.state = "Dead"; 142 break; 143 default: 144 thread.state = "Invalid"; 145 } 146 147 process.threads.append(move(thread)); 148 149 process.username = username_from_uid(process.uid); 150 map.set(process.pid, process); 151 } 152 153#else 154 auto file = Core::File::construct("/proc/all"); 155 if (!file->open(Core::IODevice::ReadOnly)) { 156 fprintf(stderr, "ProcessStatisticsReader: Failed to open /proc/all: %s\n", file->error_string()); 157 return {}; 158 } 159 160 auto file_contents = file->read_all(); 161 auto json = JsonValue::from_string(file_contents); 162 json.as_array().for_each([&](auto& value) { 163 const JsonObject& process_object = value.as_object(); 164 Core::ProcessStatistics process; 165 166 // kernel data first 167 process.pid = process_object.get("pid").to_u32(); 168 process.pgid = process_object.get("pgid").to_u32(); 169 process.pgp = process_object.get("pgp").to_u32(); 170 process.sid = process_object.get("sid").to_u32(); 171 process.uid = process_object.get("uid").to_u32(); 172 process.gid = process_object.get("gid").to_u32(); 173 process.ppid = process_object.get("ppid").to_u32(); 174 process.nfds = process_object.get("nfds").to_u32(); 175 process.name = process_object.get("name").to_string(); 176 process.tty = process_object.get("tty").to_string(); 177 process.pledge = process_object.get("pledge").to_string(); 178 process.veil = process_object.get("veil").to_string(); 179 process.amount_virtual = process_object.get("amount_virtual").to_u32(); 180 process.amount_resident = process_object.get("amount_resident").to_u32(); 181 process.amount_shared = process_object.get("amount_shared").to_u32(); 182 process.amount_dirty_private = process_object.get("amount_dirty_private").to_u32(); 183 process.amount_clean_inode = process_object.get("amount_clean_inode").to_u32(); 184 process.amount_purgeable_volatile = process_object.get("amount_purgeable_volatile").to_u32(); 185 process.amount_purgeable_nonvolatile = process_object.get("amount_purgeable_nonvolatile").to_u32(); 186 process.icon_id = process_object.get("icon_id").to_int(); 187 188 auto& thread_array = process_object.get_ptr("threads")->as_array(); 189 process.threads.ensure_capacity(thread_array.size()); 190 thread_array.for_each([&](auto& value) { 191 auto& thread_object = value.as_object(); 192 Core::ThreadStatistics thread; 193 thread.tid = thread_object.get("tid").to_u32(); 194 thread.times_scheduled = thread_object.get("times_scheduled").to_u32(); 195 thread.name = thread_object.get("name").to_string(); 196 thread.state = thread_object.get("state").to_string(); 197 thread.ticks = thread_object.get("ticks").to_u32(); 198 thread.priority = thread_object.get("priority").to_u32(); 199 thread.effective_priority = thread_object.get("effective_priority").to_u32(); 200 thread.syscall_count = thread_object.get("syscall_count").to_u32(); 201 thread.inode_faults = thread_object.get("inode_faults").to_u32(); 202 thread.zero_faults = thread_object.get("zero_faults").to_u32(); 203 thread.cow_faults = thread_object.get("cow_faults").to_u32(); 204 thread.unix_socket_read_bytes = thread_object.get("unix_socket_read_bytes").to_u32(); 205 thread.unix_socket_write_bytes = thread_object.get("unix_socket_write_bytes").to_u32(); 206 thread.ipv4_socket_read_bytes = thread_object.get("ipv4_socket_read_bytes").to_u32(); 207 thread.ipv4_socket_write_bytes = thread_object.get("ipv4_socket_write_bytes").to_u32(); 208 thread.file_read_bytes = thread_object.get("file_read_bytes").to_u32(); 209 thread.file_write_bytes = thread_object.get("file_write_bytes").to_u32(); 210 process.threads.append(move(thread)); 211 }); 212 213 // and synthetic data last 214 process.username = username_from_uid(process.uid); 215 map.set(process.pid, process); 216 }); 217#endif 218 219 return map; 220} 221 222String ProcessStatisticsReader::username_from_uid(uid_t uid) 223{ 224 if (s_usernames.is_empty()) { 225 setpwent(); 226 while (auto* passwd = getpwent()) 227 s_usernames.set(passwd->pw_uid, passwd->pw_name); 228 endpwent(); 229 } 230 231 auto it = s_usernames.find(uid); 232 if (it != s_usernames.end()) 233 return (*it).value; 234 return String::number(uid); 235} 236}