1 |
|
|
/* $OpenBSD: machine.c,v 1.89 2017/05/30 06:01:30 tedu Exp $ */ |
2 |
|
|
|
3 |
|
|
/*- |
4 |
|
|
* Copyright (c) 1994 Thorsten Lockert <tholo@sigmasoft.com> |
5 |
|
|
* All rights reserved. |
6 |
|
|
* |
7 |
|
|
* Redistribution and use in source and binary forms, with or without |
8 |
|
|
* modification, are permitted provided that the following conditions |
9 |
|
|
* are met: |
10 |
|
|
* 1. Redistributions of source code must retain the above copyright |
11 |
|
|
* notice, this list of conditions and the following disclaimer. |
12 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
|
|
* notice, this list of conditions and the following disclaimer in the |
14 |
|
|
* documentation and/or other materials provided with the distribution. |
15 |
|
|
* 3. The name of the author may not be used to endorse or promote products |
16 |
|
|
* derived from this software without specific prior written permission. |
17 |
|
|
* |
18 |
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
19 |
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
20 |
|
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
21 |
|
|
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 |
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 |
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
24 |
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
25 |
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
26 |
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
27 |
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 |
|
|
* |
29 |
|
|
* AUTHOR: Thorsten Lockert <tholo@sigmasoft.com> |
30 |
|
|
* Adapted from BSD4.4 by Christos Zoulas <christos@ee.cornell.edu> |
31 |
|
|
* Patch for process wait display by Jarl F. Greipsland <jarle@idt.unit.no> |
32 |
|
|
* Patch for -DORDER by Kenneth Stailey <kstailey@disclosure.com> |
33 |
|
|
* Patch for new swapctl(2) by Tobias Weingartner <weingart@openbsd.org> |
34 |
|
|
*/ |
35 |
|
|
|
36 |
|
|
#include <sys/param.h> /* DEV_BSIZE MAXCOMLEN PZERO */ |
37 |
|
|
#include <sys/types.h> |
38 |
|
|
#include <sys/signal.h> |
39 |
|
|
#include <sys/mount.h> |
40 |
|
|
#include <sys/proc.h> |
41 |
|
|
#include <sys/sched.h> |
42 |
|
|
#include <sys/swap.h> |
43 |
|
|
#include <sys/sysctl.h> |
44 |
|
|
|
45 |
|
|
#include <stdio.h> |
46 |
|
|
#include <stdlib.h> |
47 |
|
|
#include <string.h> |
48 |
|
|
#include <unistd.h> |
49 |
|
|
#include <err.h> |
50 |
|
|
#include <errno.h> |
51 |
|
|
|
52 |
|
|
#include "top.h" |
53 |
|
|
#include "display.h" |
54 |
|
|
#include "machine.h" |
55 |
|
|
#include "utils.h" |
56 |
|
|
|
57 |
|
|
static int swapmode(int *, int *); |
58 |
|
|
static char *state_abbr(struct kinfo_proc *); |
59 |
|
|
static char *format_comm(struct kinfo_proc *); |
60 |
|
|
static int cmd_matches(struct kinfo_proc *, char *); |
61 |
|
|
static char **get_proc_args(struct kinfo_proc *); |
62 |
|
|
|
63 |
|
|
/* get_process_info passes back a handle. This is what it looks like: */ |
64 |
|
|
|
65 |
|
|
struct handle { |
66 |
|
|
struct kinfo_proc **next_proc; /* points to next valid proc pointer */ |
67 |
|
|
int remaining; /* number of pointers remaining */ |
68 |
|
|
}; |
69 |
|
|
|
70 |
|
|
/* what we consider to be process size: */ |
71 |
|
|
#define PROCSIZE(pp) ((pp)->p_vm_tsize + (pp)->p_vm_dsize + (pp)->p_vm_ssize) |
72 |
|
|
|
73 |
|
|
/* |
74 |
|
|
* These definitions control the format of the per-process area |
75 |
|
|
*/ |
76 |
|
|
static char header[] = |
77 |
|
|
" PID X PRI NICE SIZE RES STATE WAIT TIME CPU COMMAND"; |
78 |
|
|
|
79 |
|
|
/* 0123456 -- field to fill in starts at header+6 */ |
80 |
|
|
#define UNAME_START 6 |
81 |
|
|
|
82 |
|
|
#define Proc_format \ |
83 |
|
|
"%5d %-8.8s %3d %4d %5s %5s %-9s %-7.7s %6s %5.2f%% %s" |
84 |
|
|
|
85 |
|
|
/* process state names for the "STATE" column of the display */ |
86 |
|
|
/* |
87 |
|
|
* the extra nulls in the string "run" are for adding a slash and the |
88 |
|
|
* processor number when needed |
89 |
|
|
*/ |
90 |
|
|
|
91 |
|
|
char *state_abbrev[] = { |
92 |
|
|
"", "start", "run", "sleep", "stop", "zomb", "dead", "onproc" |
93 |
|
|
}; |
94 |
|
|
|
95 |
|
|
/* these are for calculating cpu state percentages */ |
96 |
|
|
static int64_t **cp_time; |
97 |
|
|
static int64_t **cp_old; |
98 |
|
|
static int64_t **cp_diff; |
99 |
|
|
|
100 |
|
|
/* these are for detailing the process states */ |
101 |
|
|
int process_states[8]; |
102 |
|
|
char *procstatenames[] = { |
103 |
|
|
"", " starting, ", " running, ", " idle, ", |
104 |
|
|
" stopped, ", " zombie, ", " dead, ", " on processor, ", |
105 |
|
|
NULL |
106 |
|
|
}; |
107 |
|
|
|
108 |
|
|
/* these are for detailing the cpu states */ |
109 |
|
|
int64_t *cpu_states; |
110 |
|
|
char *cpustatenames[] = { |
111 |
|
|
"user", "nice", "system", "interrupt", "idle", NULL |
112 |
|
|
}; |
113 |
|
|
|
114 |
|
|
/* these are for detailing the memory statistics */ |
115 |
|
|
int memory_stats[10]; |
116 |
|
|
char *memorynames[] = { |
117 |
|
|
"Real: ", "K/", "K act/tot ", "Free: ", "K ", |
118 |
|
|
"Cache: ", "K ", |
119 |
|
|
"Swap: ", "K/", "K", |
120 |
|
|
NULL |
121 |
|
|
}; |
122 |
|
|
|
123 |
|
|
/* these are names given to allowed sorting orders -- first is default */ |
124 |
|
|
char *ordernames[] = { |
125 |
|
|
"cpu", "size", "res", "time", "pri", "pid", "command", NULL |
126 |
|
|
}; |
127 |
|
|
|
128 |
|
|
/* these are for keeping track of the proc array */ |
129 |
|
|
static int nproc; |
130 |
|
|
static int onproc = -1; |
131 |
|
|
static int pref_len; |
132 |
|
|
static struct kinfo_proc *pbase; |
133 |
|
|
static struct kinfo_proc **pref; |
134 |
|
|
|
135 |
|
|
/* these are for getting the memory statistics */ |
136 |
|
|
static int pageshift; /* log base 2 of the pagesize */ |
137 |
|
|
|
138 |
|
|
/* define pagetok in terms of pageshift */ |
139 |
|
|
#define pagetok(size) ((size) << pageshift) |
140 |
|
|
|
141 |
|
|
int ncpu; |
142 |
|
|
int fscale; |
143 |
|
|
|
144 |
|
|
unsigned int maxslp; |
145 |
|
|
|
146 |
|
|
int |
147 |
|
|
getfscale(void) |
148 |
|
|
{ |
149 |
|
|
int mib[] = { CTL_KERN, KERN_FSCALE }; |
150 |
|
|
size_t size = sizeof(fscale); |
151 |
|
|
|
152 |
|
|
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
153 |
|
|
&fscale, &size, NULL, 0) < 0) |
154 |
|
|
return (-1); |
155 |
|
|
return fscale; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
int |
159 |
|
|
getncpu(void) |
160 |
|
|
{ |
161 |
|
|
int mib[] = { CTL_HW, HW_NCPU }; |
162 |
|
|
int numcpu; |
163 |
|
|
size_t size = sizeof(numcpu); |
164 |
|
|
|
165 |
|
|
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), |
166 |
|
|
&numcpu, &size, NULL, 0) == -1) |
167 |
|
|
return (-1); |
168 |
|
|
|
169 |
|
|
return (numcpu); |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
int |
173 |
|
|
machine_init(struct statics *statics) |
174 |
|
|
{ |
175 |
|
|
int pagesize, cpu; |
176 |
|
|
|
177 |
|
|
ncpu = getncpu(); |
178 |
|
|
if (ncpu == -1) |
179 |
|
|
return (-1); |
180 |
|
|
if (getfscale() == -1) |
181 |
|
|
return (-1); |
182 |
|
|
cpu_states = calloc(ncpu, CPUSTATES * sizeof(int64_t)); |
183 |
|
|
if (cpu_states == NULL) |
184 |
|
|
err(1, NULL); |
185 |
|
|
cp_time = calloc(ncpu, sizeof(int64_t *)); |
186 |
|
|
cp_old = calloc(ncpu, sizeof(int64_t *)); |
187 |
|
|
cp_diff = calloc(ncpu, sizeof(int64_t *)); |
188 |
|
|
if (cp_time == NULL || cp_old == NULL || cp_diff == NULL) |
189 |
|
|
err(1, NULL); |
190 |
|
|
for (cpu = 0; cpu < ncpu; cpu++) { |
191 |
|
|
cp_time[cpu] = calloc(CPUSTATES, sizeof(int64_t)); |
192 |
|
|
cp_old[cpu] = calloc(CPUSTATES, sizeof(int64_t)); |
193 |
|
|
cp_diff[cpu] = calloc(CPUSTATES, sizeof(int64_t)); |
194 |
|
|
if (cp_time[cpu] == NULL || cp_old[cpu] == NULL || |
195 |
|
|
cp_diff[cpu] == NULL) |
196 |
|
|
err(1, NULL); |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
pbase = NULL; |
200 |
|
|
pref = NULL; |
201 |
|
|
onproc = -1; |
202 |
|
|
nproc = 0; |
203 |
|
|
|
204 |
|
|
/* |
205 |
|
|
* get the page size with "getpagesize" and calculate pageshift from |
206 |
|
|
* it |
207 |
|
|
*/ |
208 |
|
|
pagesize = getpagesize(); |
209 |
|
|
pageshift = 0; |
210 |
|
|
while (pagesize > 1) { |
211 |
|
|
pageshift++; |
212 |
|
|
pagesize >>= 1; |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
/* we only need the amount of log(2)1024 for our conversion */ |
216 |
|
|
pageshift -= LOG1024; |
217 |
|
|
|
218 |
|
|
/* fill in the statics information */ |
219 |
|
|
statics->procstate_names = procstatenames; |
220 |
|
|
statics->cpustate_names = cpustatenames; |
221 |
|
|
statics->memory_names = memorynames; |
222 |
|
|
statics->order_names = ordernames; |
223 |
|
|
return (0); |
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
char * |
227 |
|
|
format_header(char *second_field, int show_threads) |
228 |
|
|
{ |
229 |
|
|
char *field_name, *thread_field = " TID"; |
230 |
|
|
char *ptr; |
231 |
|
|
|
232 |
|
|
if (show_threads) |
233 |
|
|
field_name = thread_field; |
234 |
|
|
else |
235 |
|
|
field_name = second_field; |
236 |
|
|
|
237 |
|
|
ptr = header + UNAME_START; |
238 |
|
|
while (*field_name != '\0') |
239 |
|
|
*ptr++ = *field_name++; |
240 |
|
|
return (header); |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
void |
244 |
|
|
get_system_info(struct system_info *si) |
245 |
|
|
{ |
246 |
|
|
static int sysload_mib[] = {CTL_VM, VM_LOADAVG}; |
247 |
|
|
static int uvmexp_mib[] = {CTL_VM, VM_UVMEXP}; |
248 |
|
|
static int bcstats_mib[] = {CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT}; |
249 |
|
|
struct loadavg sysload; |
250 |
|
|
struct uvmexp uvmexp; |
251 |
|
|
struct bcachestats bcstats; |
252 |
|
|
double *infoloadp; |
253 |
|
|
size_t size; |
254 |
|
|
int i; |
255 |
|
|
int64_t *tmpstate; |
256 |
|
|
|
257 |
|
|
if (ncpu > 1) { |
258 |
|
|
int cp_time_mib[] = {CTL_KERN, KERN_CPTIME2, /*fillme*/0}; |
259 |
|
|
|
260 |
|
|
size = CPUSTATES * sizeof(int64_t); |
261 |
|
|
for (i = 0; i < ncpu; i++) { |
262 |
|
|
cp_time_mib[2] = i; |
263 |
|
|
tmpstate = cpu_states + (CPUSTATES * i); |
264 |
|
|
if (sysctl(cp_time_mib, 3, cp_time[i], &size, NULL, 0) < 0) |
265 |
|
|
warn("sysctl kern.cp_time2 failed"); |
266 |
|
|
/* convert cp_time2 counts to percentages */ |
267 |
|
|
(void) percentages(CPUSTATES, tmpstate, cp_time[i], |
268 |
|
|
cp_old[i], cp_diff[i]); |
269 |
|
|
} |
270 |
|
|
} else { |
271 |
|
|
int cp_time_mib[] = {CTL_KERN, KERN_CPTIME}; |
272 |
|
|
long cp_time_tmp[CPUSTATES]; |
273 |
|
|
|
274 |
|
|
size = sizeof(cp_time_tmp); |
275 |
|
|
if (sysctl(cp_time_mib, 2, cp_time_tmp, &size, NULL, 0) < 0) |
276 |
|
|
warn("sysctl kern.cp_time failed"); |
277 |
|
|
for (i = 0; i < CPUSTATES; i++) |
278 |
|
|
cp_time[0][i] = cp_time_tmp[i]; |
279 |
|
|
/* convert cp_time counts to percentages */ |
280 |
|
|
(void) percentages(CPUSTATES, cpu_states, cp_time[0], |
281 |
|
|
cp_old[0], cp_diff[0]); |
282 |
|
|
} |
283 |
|
|
|
284 |
|
|
size = sizeof(sysload); |
285 |
|
|
if (sysctl(sysload_mib, 2, &sysload, &size, NULL, 0) < 0) |
286 |
|
|
warn("sysctl failed"); |
287 |
|
|
infoloadp = si->load_avg; |
288 |
|
|
for (i = 0; i < 3; i++) |
289 |
|
|
*infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale; |
290 |
|
|
|
291 |
|
|
|
292 |
|
|
/* get total -- systemwide main memory usage structure */ |
293 |
|
|
size = sizeof(uvmexp); |
294 |
|
|
if (sysctl(uvmexp_mib, 2, &uvmexp, &size, NULL, 0) < 0) { |
295 |
|
|
warn("sysctl failed"); |
296 |
|
|
bzero(&uvmexp, sizeof(uvmexp)); |
297 |
|
|
} |
298 |
|
|
size = sizeof(bcstats); |
299 |
|
|
if (sysctl(bcstats_mib, 3, &bcstats, &size, NULL, 0) < 0) { |
300 |
|
|
warn("sysctl failed"); |
301 |
|
|
bzero(&bcstats, sizeof(bcstats)); |
302 |
|
|
} |
303 |
|
|
/* convert memory stats to Kbytes */ |
304 |
|
|
memory_stats[0] = -1; |
305 |
|
|
memory_stats[1] = pagetok(uvmexp.active); |
306 |
|
|
memory_stats[2] = pagetok(uvmexp.npages - uvmexp.free); |
307 |
|
|
memory_stats[3] = -1; |
308 |
|
|
memory_stats[4] = pagetok(uvmexp.free); |
309 |
|
|
memory_stats[5] = -1; |
310 |
|
|
memory_stats[6] = pagetok(bcstats.numbufpages); |
311 |
|
|
memory_stats[7] = -1; |
312 |
|
|
|
313 |
|
|
if (!swapmode(&memory_stats[8], &memory_stats[9])) { |
314 |
|
|
memory_stats[8] = 0; |
315 |
|
|
memory_stats[9] = 0; |
316 |
|
|
} |
317 |
|
|
|
318 |
|
|
/* set arrays and strings */ |
319 |
|
|
si->cpustates = cpu_states; |
320 |
|
|
si->memory = memory_stats; |
321 |
|
|
si->last_pid = -1; |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
static struct handle handle; |
325 |
|
|
|
326 |
|
|
struct kinfo_proc * |
327 |
|
|
getprocs(int op, int arg, int *cnt) |
328 |
|
|
{ |
329 |
|
|
size_t size; |
330 |
|
|
int mib[6] = {CTL_KERN, KERN_PROC, 0, 0, sizeof(struct kinfo_proc), 0}; |
331 |
|
|
static int maxslp_mib[] = {CTL_VM, VM_MAXSLP}; |
332 |
|
|
static struct kinfo_proc *procbase; |
333 |
|
|
int st; |
334 |
|
|
|
335 |
|
|
mib[2] = op; |
336 |
|
|
mib[3] = arg; |
337 |
|
|
|
338 |
|
|
size = sizeof(maxslp); |
339 |
|
|
if (sysctl(maxslp_mib, 2, &maxslp, &size, NULL, 0) < 0) { |
340 |
|
|
warn("sysctl vm.maxslp failed"); |
341 |
|
|
return (0); |
342 |
|
|
} |
343 |
|
|
retry: |
344 |
|
|
free(procbase); |
345 |
|
|
st = sysctl(mib, 6, NULL, &size, NULL, 0); |
346 |
|
|
if (st == -1) { |
347 |
|
|
/* _kvm_syserr(kd, kd->program, "kvm_getprocs"); */ |
348 |
|
|
return (0); |
349 |
|
|
} |
350 |
|
|
size = 5 * size / 4; /* extra slop */ |
351 |
|
|
if ((procbase = malloc(size)) == NULL) |
352 |
|
|
return (0); |
353 |
|
|
mib[5] = (int)(size / sizeof(struct kinfo_proc)); |
354 |
|
|
st = sysctl(mib, 6, procbase, &size, NULL, 0); |
355 |
|
|
if (st == -1) { |
356 |
|
|
if (errno == ENOMEM) |
357 |
|
|
goto retry; |
358 |
|
|
/* _kvm_syserr(kd, kd->program, "kvm_getprocs"); */ |
359 |
|
|
return (0); |
360 |
|
|
} |
361 |
|
|
*cnt = (int)(size / sizeof(struct kinfo_proc)); |
362 |
|
|
return (procbase); |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
static char ** |
366 |
|
|
get_proc_args(struct kinfo_proc *kp) |
367 |
|
|
{ |
368 |
|
|
static char **s; |
369 |
|
|
static size_t siz = 1023; |
370 |
|
|
int mib[4]; |
371 |
|
|
|
372 |
|
|
if (!s && !(s = malloc(siz))) |
373 |
|
|
err(1, NULL); |
374 |
|
|
|
375 |
|
|
mib[0] = CTL_KERN; |
376 |
|
|
mib[1] = KERN_PROC_ARGS; |
377 |
|
|
mib[2] = kp->p_pid; |
378 |
|
|
mib[3] = KERN_PROC_ARGV; |
379 |
|
|
for (;;) { |
380 |
|
|
size_t space = siz; |
381 |
|
|
if (sysctl(mib, 4, s, &space, NULL, 0) == 0) |
382 |
|
|
break; |
383 |
|
|
if (errno != ENOMEM) |
384 |
|
|
return NULL; |
385 |
|
|
siz *= 2; |
386 |
|
|
if ((s = realloc(s, siz)) == NULL) |
387 |
|
|
err(1, NULL); |
388 |
|
|
} |
389 |
|
|
return s; |
390 |
|
|
} |
391 |
|
|
|
392 |
|
|
static int |
393 |
|
|
cmd_matches(struct kinfo_proc *proc, char *term) |
394 |
|
|
{ |
395 |
|
|
extern int show_args; |
396 |
|
|
char **args = NULL; |
397 |
|
|
|
398 |
|
|
if (!term) { |
399 |
|
|
/* No command filter set */ |
400 |
|
|
return 1; |
401 |
|
|
} else { |
402 |
|
|
/* Filter set, process name needs to contain term */ |
403 |
|
|
if (strstr(proc->p_comm, term)) |
404 |
|
|
return 1; |
405 |
|
|
/* If showing arguments, search those as well */ |
406 |
|
|
if (show_args) { |
407 |
|
|
args = get_proc_args(proc); |
408 |
|
|
|
409 |
|
|
if (args == NULL) { |
410 |
|
|
/* Failed to get args, so can't search them */ |
411 |
|
|
return 0; |
412 |
|
|
} |
413 |
|
|
|
414 |
|
|
while (*args != NULL) { |
415 |
|
|
if (strstr(*args, term)) |
416 |
|
|
return 1; |
417 |
|
|
args++; |
418 |
|
|
} |
419 |
|
|
} |
420 |
|
|
} |
421 |
|
|
return 0; |
422 |
|
|
} |
423 |
|
|
|
424 |
|
|
caddr_t |
425 |
|
|
get_process_info(struct system_info *si, struct process_select *sel, |
426 |
|
|
int (*compare) (const void *, const void *)) |
427 |
|
|
{ |
428 |
|
|
int show_idle, show_system, show_threads, show_uid, show_pid, show_cmd; |
429 |
|
|
int hide_uid; |
430 |
|
|
int total_procs, active_procs; |
431 |
|
|
struct kinfo_proc **prefp, *pp; |
432 |
|
|
int what = KERN_PROC_KTHREAD; |
433 |
|
|
|
434 |
|
|
if (sel->threads) |
435 |
|
|
what |= KERN_PROC_SHOW_THREADS; |
436 |
|
|
|
437 |
|
|
if ((pbase = getprocs(what, 0, &nproc)) == NULL) { |
438 |
|
|
/* warnx("%s", kvm_geterr(kd)); */ |
439 |
|
|
quit(23); |
440 |
|
|
} |
441 |
|
|
if (nproc > onproc) |
442 |
|
|
pref = reallocarray(pref, (onproc = nproc), |
443 |
|
|
sizeof(struct kinfo_proc *)); |
444 |
|
|
if (pref == NULL) { |
445 |
|
|
warnx("Out of memory."); |
446 |
|
|
quit(23); |
447 |
|
|
} |
448 |
|
|
/* get a pointer to the states summary array */ |
449 |
|
|
si->procstates = process_states; |
450 |
|
|
|
451 |
|
|
/* set up flags which define what we are going to select */ |
452 |
|
|
show_idle = sel->idle; |
453 |
|
|
show_system = sel->system; |
454 |
|
|
show_threads = sel->threads; |
455 |
|
|
show_uid = sel->uid != (uid_t)-1; |
456 |
|
|
hide_uid = sel->huid != (uid_t)-1; |
457 |
|
|
show_pid = sel->pid != (pid_t)-1; |
458 |
|
|
show_cmd = sel->command != NULL; |
459 |
|
|
|
460 |
|
|
/* count up process states and get pointers to interesting procs */ |
461 |
|
|
total_procs = 0; |
462 |
|
|
active_procs = 0; |
463 |
|
|
memset((char *) process_states, 0, sizeof(process_states)); |
464 |
|
|
prefp = pref; |
465 |
|
|
for (pp = pbase; pp < &pbase[nproc]; pp++) { |
466 |
|
|
/* |
467 |
|
|
* Place pointers to each valid proc structure in pref[]. |
468 |
|
|
* Process slots that are actually in use have a non-zero |
469 |
|
|
* status field. Processes with P_SYSTEM set are system |
470 |
|
|
* processes---these get ignored unless show_system is set. |
471 |
|
|
*/ |
472 |
|
|
if (show_threads && pp->p_tid == -1) |
473 |
|
|
continue; |
474 |
|
|
if (pp->p_stat != 0 && |
475 |
|
|
(show_system || (pp->p_flag & P_SYSTEM) == 0) && |
476 |
|
|
(show_threads || (pp->p_flag & P_THREAD) == 0)) { |
477 |
|
|
total_procs++; |
478 |
|
|
process_states[(unsigned char) pp->p_stat]++; |
479 |
|
|
if ((pp->p_psflags & PS_ZOMBIE) == 0 && |
480 |
|
|
(show_idle || pp->p_pctcpu != 0 || |
481 |
|
|
pp->p_stat == SRUN) && |
482 |
|
|
(!hide_uid || pp->p_ruid != sel->huid) && |
483 |
|
|
(!show_uid || pp->p_ruid == sel->uid) && |
484 |
|
|
(!show_pid || pp->p_pid == sel->pid) && |
485 |
|
|
(!show_cmd || cmd_matches(pp, sel->command))) { |
486 |
|
|
*prefp++ = pp; |
487 |
|
|
active_procs++; |
488 |
|
|
} |
489 |
|
|
} |
490 |
|
|
} |
491 |
|
|
|
492 |
|
|
/* if requested, sort the "interesting" processes */ |
493 |
|
|
if (compare != NULL) |
494 |
|
|
qsort((char *) pref, active_procs, |
495 |
|
|
sizeof(struct kinfo_proc *), compare); |
496 |
|
|
/* remember active and total counts */ |
497 |
|
|
si->p_total = total_procs; |
498 |
|
|
si->p_active = pref_len = active_procs; |
499 |
|
|
|
500 |
|
|
/* pass back a handle */ |
501 |
|
|
handle.next_proc = pref; |
502 |
|
|
handle.remaining = active_procs; |
503 |
|
|
return ((caddr_t) & handle); |
504 |
|
|
} |
505 |
|
|
|
506 |
|
|
char fmt[MAX_COLS]; /* static area where result is built */ |
507 |
|
|
|
508 |
|
|
static char * |
509 |
|
|
state_abbr(struct kinfo_proc *pp) |
510 |
|
|
{ |
511 |
|
|
static char buf[10]; |
512 |
|
|
|
513 |
|
|
if (ncpu > 1 && pp->p_cpuid != KI_NOCPU) |
514 |
|
|
snprintf(buf, sizeof buf, "%s/%llu", |
515 |
|
|
state_abbrev[(unsigned char)pp->p_stat], pp->p_cpuid); |
516 |
|
|
else |
517 |
|
|
snprintf(buf, sizeof buf, "%s", |
518 |
|
|
state_abbrev[(unsigned char)pp->p_stat]); |
519 |
|
|
return buf; |
520 |
|
|
} |
521 |
|
|
|
522 |
|
|
static char * |
523 |
|
|
format_comm(struct kinfo_proc *kp) |
524 |
|
|
{ |
525 |
|
|
static char buf[MAX_COLS]; |
526 |
|
|
char **p, **s; |
527 |
|
|
extern int show_args; |
528 |
|
|
|
529 |
|
|
if (!show_args) |
530 |
|
|
return (kp->p_comm); |
531 |
|
|
|
532 |
|
|
s = get_proc_args(kp); |
533 |
|
|
if (s == NULL) |
534 |
|
|
return kp->p_comm; |
535 |
|
|
|
536 |
|
|
buf[0] = '\0'; |
537 |
|
|
for (p = s; *p != NULL; p++) { |
538 |
|
|
if (p != s) |
539 |
|
|
strlcat(buf, " ", sizeof(buf)); |
540 |
|
|
strlcat(buf, *p, sizeof(buf)); |
541 |
|
|
} |
542 |
|
|
if (buf[0] == '\0') |
543 |
|
|
return (kp->p_comm); |
544 |
|
|
return (buf); |
545 |
|
|
} |
546 |
|
|
|
547 |
|
|
char * |
548 |
|
|
format_next_process(caddr_t hndl, char *(*get_userid)(uid_t), pid_t *pid, |
549 |
|
|
int show_threads) |
550 |
|
|
{ |
551 |
|
|
char *p_wait; |
552 |
|
|
struct kinfo_proc *pp; |
553 |
|
|
struct handle *hp; |
554 |
|
|
int cputime; |
555 |
|
|
double pct; |
556 |
|
|
char buf[16]; |
557 |
|
|
|
558 |
|
|
/* find and remember the next proc structure */ |
559 |
|
|
hp = (struct handle *) hndl; |
560 |
|
|
pp = *(hp->next_proc++); |
561 |
|
|
hp->remaining--; |
562 |
|
|
|
563 |
|
|
cputime = pp->p_rtime_sec + ((pp->p_rtime_usec + 500000) / 1000000); |
564 |
|
|
|
565 |
|
|
/* calculate the base for cpu percentages */ |
566 |
|
|
pct = (double)pp->p_pctcpu / fscale; |
567 |
|
|
|
568 |
|
|
if (pp->p_wmesg[0]) |
569 |
|
|
p_wait = pp->p_wmesg; |
570 |
|
|
else |
571 |
|
|
p_wait = "-"; |
572 |
|
|
|
573 |
|
|
if (show_threads) |
574 |
|
|
snprintf(buf, sizeof(buf), "%8d", pp->p_tid); |
575 |
|
|
else |
576 |
|
|
snprintf(buf, sizeof(buf), "%s", (*get_userid)(pp->p_ruid)); |
577 |
|
|
|
578 |
|
|
/* format this entry */ |
579 |
|
|
snprintf(fmt, sizeof(fmt), Proc_format, pp->p_pid, buf, |
580 |
|
|
pp->p_priority - PZERO, pp->p_nice - NZERO, |
581 |
|
|
format_k(pagetok(PROCSIZE(pp))), |
582 |
|
|
format_k(pagetok(pp->p_vm_rssize)), |
583 |
|
|
(pp->p_stat == SSLEEP && pp->p_slptime > maxslp) ? |
584 |
|
|
"idle" : state_abbr(pp), |
585 |
|
|
p_wait, format_time(cputime), 100.0 * pct, |
586 |
|
|
printable(format_comm(pp))); |
587 |
|
|
|
588 |
|
|
*pid = pp->p_pid; |
589 |
|
|
/* return the result */ |
590 |
|
|
return (fmt); |
591 |
|
|
} |
592 |
|
|
|
593 |
|
|
/* comparison routine for qsort */ |
594 |
|
|
static unsigned char sorted_state[] = |
595 |
|
|
{ |
596 |
|
|
0, /* not used */ |
597 |
|
|
4, /* start */ |
598 |
|
|
5, /* run */ |
599 |
|
|
2, /* sleep */ |
600 |
|
|
3, /* stop */ |
601 |
|
|
1 /* zombie */ |
602 |
|
|
}; |
603 |
|
|
|
604 |
|
|
/* |
605 |
|
|
* proc_compares - comparison functions for "qsort" |
606 |
|
|
*/ |
607 |
|
|
|
608 |
|
|
/* |
609 |
|
|
* First, the possible comparison keys. These are defined in such a way |
610 |
|
|
* that they can be merely listed in the source code to define the actual |
611 |
|
|
* desired ordering. |
612 |
|
|
*/ |
613 |
|
|
|
614 |
|
|
#define ORDERKEY_PCTCPU \ |
615 |
|
|
if ((result = (int)(p2->p_pctcpu - p1->p_pctcpu)) == 0) |
616 |
|
|
#define ORDERKEY_CPUTIME \ |
617 |
|
|
if ((result = p2->p_rtime_sec - p1->p_rtime_sec) == 0) \ |
618 |
|
|
if ((result = p2->p_rtime_usec - p1->p_rtime_usec) == 0) |
619 |
|
|
#define ORDERKEY_STATE \ |
620 |
|
|
if ((result = sorted_state[(unsigned char)p2->p_stat] - \ |
621 |
|
|
sorted_state[(unsigned char)p1->p_stat]) == 0) |
622 |
|
|
#define ORDERKEY_PRIO \ |
623 |
|
|
if ((result = p2->p_priority - p1->p_priority) == 0) |
624 |
|
|
#define ORDERKEY_RSSIZE \ |
625 |
|
|
if ((result = p2->p_vm_rssize - p1->p_vm_rssize) == 0) |
626 |
|
|
#define ORDERKEY_MEM \ |
627 |
|
|
if ((result = PROCSIZE(p2) - PROCSIZE(p1)) == 0) |
628 |
|
|
#define ORDERKEY_PID \ |
629 |
|
|
if ((result = p1->p_pid - p2->p_pid) == 0) |
630 |
|
|
#define ORDERKEY_CMD \ |
631 |
|
|
if ((result = strcmp(p1->p_comm, p2->p_comm)) == 0) |
632 |
|
|
|
633 |
|
|
/* compare_cpu - the comparison function for sorting by cpu percentage */ |
634 |
|
|
static int |
635 |
|
|
compare_cpu(const void *v1, const void *v2) |
636 |
|
|
{ |
637 |
|
|
struct proc **pp1 = (struct proc **) v1; |
638 |
|
|
struct proc **pp2 = (struct proc **) v2; |
639 |
|
|
struct kinfo_proc *p1, *p2; |
640 |
|
|
int result; |
641 |
|
|
|
642 |
|
|
/* remove one level of indirection */ |
643 |
|
|
p1 = *(struct kinfo_proc **) pp1; |
644 |
|
|
p2 = *(struct kinfo_proc **) pp2; |
645 |
|
|
|
646 |
|
|
ORDERKEY_PCTCPU |
647 |
|
|
ORDERKEY_CPUTIME |
648 |
|
|
ORDERKEY_STATE |
649 |
|
|
ORDERKEY_PRIO |
650 |
|
|
ORDERKEY_RSSIZE |
651 |
|
|
ORDERKEY_MEM |
652 |
|
|
; |
653 |
|
|
return (result); |
654 |
|
|
} |
655 |
|
|
|
656 |
|
|
/* compare_size - the comparison function for sorting by total memory usage */ |
657 |
|
|
static int |
658 |
|
|
compare_size(const void *v1, const void *v2) |
659 |
|
|
{ |
660 |
|
|
struct proc **pp1 = (struct proc **) v1; |
661 |
|
|
struct proc **pp2 = (struct proc **) v2; |
662 |
|
|
struct kinfo_proc *p1, *p2; |
663 |
|
|
int result; |
664 |
|
|
|
665 |
|
|
/* remove one level of indirection */ |
666 |
|
|
p1 = *(struct kinfo_proc **) pp1; |
667 |
|
|
p2 = *(struct kinfo_proc **) pp2; |
668 |
|
|
|
669 |
|
|
ORDERKEY_MEM |
670 |
|
|
ORDERKEY_RSSIZE |
671 |
|
|
ORDERKEY_PCTCPU |
672 |
|
|
ORDERKEY_CPUTIME |
673 |
|
|
ORDERKEY_STATE |
674 |
|
|
ORDERKEY_PRIO |
675 |
|
|
; |
676 |
|
|
return (result); |
677 |
|
|
} |
678 |
|
|
|
679 |
|
|
/* compare_res - the comparison function for sorting by resident set size */ |
680 |
|
|
static int |
681 |
|
|
compare_res(const void *v1, const void *v2) |
682 |
|
|
{ |
683 |
|
|
struct proc **pp1 = (struct proc **) v1; |
684 |
|
|
struct proc **pp2 = (struct proc **) v2; |
685 |
|
|
struct kinfo_proc *p1, *p2; |
686 |
|
|
int result; |
687 |
|
|
|
688 |
|
|
/* remove one level of indirection */ |
689 |
|
|
p1 = *(struct kinfo_proc **) pp1; |
690 |
|
|
p2 = *(struct kinfo_proc **) pp2; |
691 |
|
|
|
692 |
|
|
ORDERKEY_RSSIZE |
693 |
|
|
ORDERKEY_MEM |
694 |
|
|
ORDERKEY_PCTCPU |
695 |
|
|
ORDERKEY_CPUTIME |
696 |
|
|
ORDERKEY_STATE |
697 |
|
|
ORDERKEY_PRIO |
698 |
|
|
; |
699 |
|
|
return (result); |
700 |
|
|
} |
701 |
|
|
|
702 |
|
|
/* compare_time - the comparison function for sorting by CPU time */ |
703 |
|
|
static int |
704 |
|
|
compare_time(const void *v1, const void *v2) |
705 |
|
|
{ |
706 |
|
|
struct proc **pp1 = (struct proc **) v1; |
707 |
|
|
struct proc **pp2 = (struct proc **) v2; |
708 |
|
|
struct kinfo_proc *p1, *p2; |
709 |
|
|
int result; |
710 |
|
|
|
711 |
|
|
/* remove one level of indirection */ |
712 |
|
|
p1 = *(struct kinfo_proc **) pp1; |
713 |
|
|
p2 = *(struct kinfo_proc **) pp2; |
714 |
|
|
|
715 |
|
|
ORDERKEY_CPUTIME |
716 |
|
|
ORDERKEY_PCTCPU |
717 |
|
|
ORDERKEY_STATE |
718 |
|
|
ORDERKEY_PRIO |
719 |
|
|
ORDERKEY_MEM |
720 |
|
|
ORDERKEY_RSSIZE |
721 |
|
|
; |
722 |
|
|
return (result); |
723 |
|
|
} |
724 |
|
|
|
725 |
|
|
/* compare_prio - the comparison function for sorting by CPU time */ |
726 |
|
|
static int |
727 |
|
|
compare_prio(const void *v1, const void *v2) |
728 |
|
|
{ |
729 |
|
|
struct proc **pp1 = (struct proc **) v1; |
730 |
|
|
struct proc **pp2 = (struct proc **) v2; |
731 |
|
|
struct kinfo_proc *p1, *p2; |
732 |
|
|
int result; |
733 |
|
|
|
734 |
|
|
/* remove one level of indirection */ |
735 |
|
|
p1 = *(struct kinfo_proc **) pp1; |
736 |
|
|
p2 = *(struct kinfo_proc **) pp2; |
737 |
|
|
|
738 |
|
|
ORDERKEY_PRIO |
739 |
|
|
ORDERKEY_PCTCPU |
740 |
|
|
ORDERKEY_CPUTIME |
741 |
|
|
ORDERKEY_STATE |
742 |
|
|
ORDERKEY_RSSIZE |
743 |
|
|
ORDERKEY_MEM |
744 |
|
|
; |
745 |
|
|
return (result); |
746 |
|
|
} |
747 |
|
|
|
748 |
|
|
static int |
749 |
|
|
compare_pid(const void *v1, const void *v2) |
750 |
|
|
{ |
751 |
|
|
struct proc **pp1 = (struct proc **) v1; |
752 |
|
|
struct proc **pp2 = (struct proc **) v2; |
753 |
|
|
struct kinfo_proc *p1, *p2; |
754 |
|
|
int result; |
755 |
|
|
|
756 |
|
|
/* remove one level of indirection */ |
757 |
|
|
p1 = *(struct kinfo_proc **) pp1; |
758 |
|
|
p2 = *(struct kinfo_proc **) pp2; |
759 |
|
|
|
760 |
|
|
ORDERKEY_PID |
761 |
|
|
ORDERKEY_PCTCPU |
762 |
|
|
ORDERKEY_CPUTIME |
763 |
|
|
ORDERKEY_STATE |
764 |
|
|
ORDERKEY_PRIO |
765 |
|
|
ORDERKEY_RSSIZE |
766 |
|
|
ORDERKEY_MEM |
767 |
|
|
; |
768 |
|
|
return (result); |
769 |
|
|
} |
770 |
|
|
|
771 |
|
|
static int |
772 |
|
|
compare_cmd(const void *v1, const void *v2) |
773 |
|
|
{ |
774 |
|
|
struct proc **pp1 = (struct proc **) v1; |
775 |
|
|
struct proc **pp2 = (struct proc **) v2; |
776 |
|
|
struct kinfo_proc *p1, *p2; |
777 |
|
|
int result; |
778 |
|
|
|
779 |
|
|
/* remove one level of indirection */ |
780 |
|
|
p1 = *(struct kinfo_proc **) pp1; |
781 |
|
|
p2 = *(struct kinfo_proc **) pp2; |
782 |
|
|
|
783 |
|
|
ORDERKEY_CMD |
784 |
|
|
ORDERKEY_PCTCPU |
785 |
|
|
ORDERKEY_CPUTIME |
786 |
|
|
ORDERKEY_STATE |
787 |
|
|
ORDERKEY_PRIO |
788 |
|
|
ORDERKEY_RSSIZE |
789 |
|
|
ORDERKEY_MEM |
790 |
|
|
; |
791 |
|
|
return (result); |
792 |
|
|
} |
793 |
|
|
|
794 |
|
|
|
795 |
|
|
int (*proc_compares[])(const void *, const void *) = { |
796 |
|
|
compare_cpu, |
797 |
|
|
compare_size, |
798 |
|
|
compare_res, |
799 |
|
|
compare_time, |
800 |
|
|
compare_prio, |
801 |
|
|
compare_pid, |
802 |
|
|
compare_cmd, |
803 |
|
|
NULL |
804 |
|
|
}; |
805 |
|
|
|
806 |
|
|
/* |
807 |
|
|
* proc_owner(pid) - returns the uid that owns process "pid", or -1 if |
808 |
|
|
* the process does not exist. |
809 |
|
|
* It is EXTREMELY IMPORTANT that this function work correctly. |
810 |
|
|
* If top runs setuid root (as in SVR4), then this function |
811 |
|
|
* is the only thing that stands in the way of a serious |
812 |
|
|
* security problem. It validates requests for the "kill" |
813 |
|
|
* and "renice" commands. |
814 |
|
|
*/ |
815 |
|
|
uid_t |
816 |
|
|
proc_owner(pid_t pid) |
817 |
|
|
{ |
818 |
|
|
struct kinfo_proc **prefp, *pp; |
819 |
|
|
int cnt; |
820 |
|
|
|
821 |
|
|
prefp = pref; |
822 |
|
|
cnt = pref_len; |
823 |
|
|
while (--cnt >= 0) { |
824 |
|
|
pp = *prefp++; |
825 |
|
|
if (pp->p_pid == pid) |
826 |
|
|
return ((uid_t)pp->p_ruid); |
827 |
|
|
} |
828 |
|
|
return (uid_t)(-1); |
829 |
|
|
} |
830 |
|
|
|
831 |
|
|
/* |
832 |
|
|
* swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org> |
833 |
|
|
* to be based on the new swapctl(2) system call. |
834 |
|
|
*/ |
835 |
|
|
static int |
836 |
|
|
swapmode(int *used, int *total) |
837 |
|
|
{ |
838 |
|
|
struct swapent *swdev; |
839 |
|
|
int nswap, rnswap, i; |
840 |
|
|
|
841 |
|
|
nswap = swapctl(SWAP_NSWAP, 0, 0); |
842 |
|
|
if (nswap == 0) |
843 |
|
|
return 0; |
844 |
|
|
|
845 |
|
|
swdev = calloc(nswap, sizeof(*swdev)); |
846 |
|
|
if (swdev == NULL) |
847 |
|
|
return 0; |
848 |
|
|
|
849 |
|
|
rnswap = swapctl(SWAP_STATS, swdev, nswap); |
850 |
|
|
if (rnswap == -1) { |
851 |
|
|
free(swdev); |
852 |
|
|
return 0; |
853 |
|
|
} |
854 |
|
|
|
855 |
|
|
/* if rnswap != nswap, then what? */ |
856 |
|
|
|
857 |
|
|
/* Total things up */ |
858 |
|
|
*total = *used = 0; |
859 |
|
|
for (i = 0; i < nswap; i++) { |
860 |
|
|
if (swdev[i].se_flags & SWF_ENABLE) { |
861 |
|
|
*used += (swdev[i].se_inuse / (1024 / DEV_BSIZE)); |
862 |
|
|
*total += (swdev[i].se_nblks / (1024 / DEV_BSIZE)); |
863 |
|
|
} |
864 |
|
|
} |
865 |
|
|
free(swdev); |
866 |
|
|
return 1; |
867 |
|
|
} |