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