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