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