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