Serenity Operating System
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

LibCore: Implement Core::ProcessStatisticsReader on OpenBSD

+135 -2
+7
Applications/SystemMonitor/Makefile
··· 15 15 16 16 LIB_DEPS = GUI Gfx Protocol PCIDB IPC Thread Pthread Core 17 17 18 + ifeq ($(HOSTED),1) 19 + UNAME_S := $(shell uname -s) 20 + ifeq ($(UNAME_S),OpenBSD) 21 + LDFLAGS += -lkvm 22 + endif 23 + endif 24 + 18 25 include ../../Makefile.common
+7
DevTools/HackStudio/Makefile
··· 19 19 20 20 LIB_DEPS = GUI Web VT Protocol Markdown Gfx IPC Thread Pthread Core JS 21 21 22 + ifeq ($(HOSTED),1) 23 + UNAME_S := $(shell uname -s) 24 + ifeq ($(UNAME_S),OpenBSD) 25 + LDFLAGS += -lkvm 26 + endif 27 + endif 28 + 22 29 include ../../Makefile.common
+114 -2
Libraries/LibCore/ProcessStatisticsReader.cpp
··· 32 32 #include <pwd.h> 33 33 #include <stdio.h> 34 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 + 35 51 namespace Core { 36 52 37 53 HashMap<uid_t, String> ProcessStatisticsReader::s_usernames; 38 54 39 55 HashMap<pid_t, Core::ProcessStatistics> ProcessStatisticsReader::get_all() 40 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 41 154 auto file = Core::File::construct("/proc/all"); 42 155 if (!file->open(Core::IODevice::ReadOnly)) { 43 156 fprintf(stderr, "ProcessStatisticsReader: Failed to open /proc/all: %s\n", file->error_string()); 44 157 return {}; 45 158 } 46 - 47 - HashMap<pid_t, Core::ProcessStatistics> map; 48 159 49 160 auto file_contents = file->read_all(); 50 161 auto json = JsonValue::from_string(file_contents); ··· 103 214 process.username = username_from_uid(process.uid); 104 215 map.set(process.pid, process); 105 216 }); 217 + #endif 106 218 107 219 return map; 108 220 }
+7
MenuApplets/CPUGraph/Makefile
··· 4 4 5 5 LIB_DEPS = GUI IPC Gfx Thread Pthread Core 6 6 7 + ifeq ($(HOSTED),1) 8 + UNAME_S := $(shell uname -s) 9 + ifeq ($(UNAME_S),OpenBSD) 10 + LDFLAGS += -lkvm 11 + endif 12 + endif 13 + 7 14 include ../../Makefile.common