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