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