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