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