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