1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/*
htop - OpenBSDProcessTable.c
(C) 2014 Hisham H. Muhammad
(C) 2015 Michael McConville
Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/
#include "config.h" // IWYU pragma: keep
#include "openbsd/OpenBSDProcessTable.h"
#include <kvm.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/sched.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <uvm/uvmexp.h>
#include "CRT.h"
#include "Macros.h"
#include "Object.h"
#include "Process.h"
#include "ProcessTable.h"
#include "Settings.h"
#include "XUtils.h"
#include "openbsd/OpenBSDMachine.h"
#include "openbsd/OpenBSDProcess.h"
ProcessTable* ProcessTable_new(Machine* host, Hashtable* pidMatchList) {
OpenBSDProcessTable* this = xCalloc(1, sizeof(OpenBSDProcessTable));
Object_setClass(this, Class(ProcessTable));
ProcessTable* super = &this->super;
ProcessTable_init(super, Class(OpenBSDProcess), host, pidMatchList);
return this;
}
void ProcessTable_delete(Object* cast) {
OpenBSDProcessTable* this = (OpenBSDProcessTable*) cast;
ProcessTable_done(&this->super);
free(this);
}
static void OpenBSDProcessTable_updateCwd(const struct kinfo_proc* kproc, Process* proc) {
const int mib[] = { CTL_KERN, KERN_PROC_CWD, kproc->p_pid };
char buffer[2048];
size_t size = sizeof(buffer);
if (sysctl(mib, 3, buffer, &size, NULL, 0) != 0) {
free(proc->procCwd);
proc->procCwd = NULL;
return;
}
/* Kernel threads return an empty buffer */
if (buffer[0] == '\0') {
free(proc->procCwd);
proc->procCwd = NULL;
return;
}
free_and_xStrdup(&proc->procCwd, buffer);
}
static void OpenBSDProcessTable_updateProcessName(kvm_t* kd, const struct kinfo_proc* kproc, Process* proc) {
Process_updateComm(proc, kproc->p_comm);
/*
* Like OpenBSD's top(1), we try to fall back to the command name
* (argv[0]) if we fail to construct the full command.
*/
char** arg = kvm_getargv(kd, kproc, 500);
if (arg == NULL || *arg == NULL) {
Process_updateCmdline(proc, kproc->p_comm, 0, strlen(kproc->p_comm));
return;
}
size_t len = 0;
for (int i = 0; arg[i] != NULL; i++) {
len += strlen(arg[i]) + 1; /* room for arg and trailing space or NUL */
}
/* don't use xMalloc here - we want to handle huge argv's gracefully */
char* s;
if ((s = malloc(len)) == NULL) {
Process_updateCmdline(proc, kproc->p_comm, 0, strlen(kproc->p_comm));
return;
}
*s = '\0';
int start = 0;
int end = 0;
for (int i = 0; arg[i] != NULL; i++) {
size_t n = strlcat(s, arg[i], len);
if (i == 0) {
end = MINIMUM(n, len - 1);
/* check if cmdline ended earlier, e.g 'kdeinit5: Running...' */
for (int j = end; j > 0; j--) {
if (arg[0][j] == ' ' && arg[0][j - 1] != '\\') {
end = (arg[0][j - 1] == ':') ? (j - 1) : j;
}
}
}
/* the trailing space should get truncated anyway */
strlcat(s, " ", len);
}
Process_updateCmdline(proc, s, start, end);
free(s);
}
/*
* Taken from OpenBSD's ps(1).
*/
static double getpcpu(const OpenBSDMachine* ohost, const struct kinfo_proc* kp) {
if (ohost->fscale == 0)
return 0.0;
return 100.0 * (double)kp->p_pctcpu / ohost->fscale;
}
static void OpenBSDProcessTable_scanProcs(OpenBSDProcessTable* this) {
Machine* host = this->super.super.host;
OpenBSDMachine* ohost = (OpenBSDMachine*) host;
const Settings* settings = host->settings;
const bool hideKernelThreads = settings->hideKernelThreads;
const bool hideUserlandThreads = settings->hideUserlandThreads;
int count = 0;
const struct kinfo_proc* kprocs = kvm_getprocs(ohost->kd, KERN_PROC_KTHREAD | KERN_PROC_SHOW_THREADS, 0, sizeof(struct kinfo_proc), &count);
for (int i = 0; i < count; i++) {
const struct kinfo_proc* kproc = &kprocs[i];
/* Ignore main threads */
if (kproc->p_tid != -1) {
Process* containingProcess = ProcessTable_findProcess(&this->super, kproc->p_pid);
if (containingProcess) {
if (((OpenBSDProcess*)containingProcess)->addr == kproc->p_addr)
continue;
containingProcess->nlwp++;
}
}
bool preExisting = false;
Process* proc = ProcessTable_getProcess(&this->super, (kproc->p_tid == -1) ? kproc->p_pid : kproc->p_tid, &preExisting, OpenBSDProcess_new);
OpenBSDProcess* op = (OpenBSDProcess*) proc;
if (!preExisting) {
Process_setParent(proc, kproc->p_ppid);
Process_setThreadGroup(proc, kproc->p_pid);
proc->tpgid = kproc->p_tpgid;
proc->session = kproc->p_sid;
proc->pgrp = kproc->p__pgid;
proc->isKernelThread = proc->pgrp == 0;
proc->isUserlandThread = kproc->p_tid != -1;
proc->starttime_ctime = kproc->p_ustart_sec;
Process_fillStarttimeBuffer(proc);
ProcessTable_add(&this->super, proc);
OpenBSDProcessTable_updateProcessName(ohost->kd, kproc, proc);
if (settings->ss->flags & PROCESS_FLAG_CWD) {
OpenBSDProcessTable_updateCwd(kproc, proc);
}
proc->tty_nr = kproc->p_tdev;
const char* name = ((dev_t)kproc->p_tdev != NODEV) ? devname(kproc->p_tdev, S_IFCHR) : NULL;
if (!name || String_eq(name, "??")) {
free(proc->tty_name);
proc->tty_name = NULL;
} else {
free_and_xStrdup(&proc->tty_name, name);
}
} else {
if (settings->updateProcessNames) {
OpenBSDProcessTable_updateProcessName(ohost->kd, kproc, proc);
}
}
op->addr = kproc->p_addr;
proc->m_virt = kproc->p_vm_dsize * ohost->pageSizeKB;
proc->m_resident = kproc->p_vm_rssize * ohost->pageSizeKB;
proc->percent_mem = proc->m_resident / (float)host->totalMem * 100.0F;
proc->percent_cpu = CLAMP(getpcpu(ohost, kproc), 0.0F, host->activeCPUs * 100.0F);
Process_updateCPUFieldWidths(proc->percent_cpu);
proc->nice = kproc->p_nice - 20;
proc->time = 100 * (kproc->p_rtime_sec + ((kproc->p_rtime_usec + 500000) / 1000000));
proc->priority = kproc->p_priority - PZERO;
proc->processor = kproc->p_cpuid;
proc->minflt = kproc->p_uru_minflt;
proc->majflt = kproc->p_uru_majflt;
proc->nlwp = 1;
if (proc->st_uid != kproc->p_uid) {
proc->st_uid = kproc->p_uid;
proc->user = UsersTable_getRef(host->usersTable, proc->st_uid);
}
/* Taken from: https://github.com/openbsd/src/blob/6a38af0976a6870911f4b2de19d8da28723a5778/sys/sys/proc.h#L420 */
switch (kproc->p_stat) {
case SIDL: proc->state = IDLE; break;
case SRUN: proc->state = RUNNABLE; break;
case SSLEEP: proc->state = SLEEPING; break;
case SSTOP: proc->state = STOPPED; break;
case SZOMB: proc->state = ZOMBIE; break;
case SDEAD: proc->state = DEFUNCT; break;
case SONPROC: proc->state = RUNNING; break;
default: proc->state = UNKNOWN;
}
if (Process_isKernelThread(proc)) {
this->super.kernelThreads++;
} else if (Process_isUserlandThread(proc)) {
this->super.userlandThreads++;
}
this->super.totalTasks++;
if (proc->state == RUNNING) {
this->super.runningTasks++;
}
proc->super.show = ! ((hideKernelThreads && Process_isKernelThread(proc)) || (hideUserlandThreads && Process_isUserlandThread(proc)));
proc->super.updated = true;
}
}
void ProcessTable_goThroughEntries(ProcessTable* super) {
OpenBSDProcessTable* this = (OpenBSDProcessTable*) super;
OpenBSDProcessTable_scanProcs(this);
}
|