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