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