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