machine.c revision 1.20
1/*	$OpenBSD: machine.c,v 1.20 2001/02/17 22:51:26 deraadt Exp $	*/
2
3/*
4 * top - a top users display for Unix
5 *
6 * SYNOPSIS:  For an OpenBSD system
7 *
8 * DESCRIPTION:
9 * This is the machine-dependent module for OpenBSD
10 * Tested on:
11 *	i386
12 *
13 * LIBS: -lkvm
14 *
15 * TERMCAP: -ltermlib
16 *
17 * CFLAGS: -DHAVE_GETOPT -DORDER
18 *
19 * AUTHOR:  Thorsten Lockert <tholo@sigmasoft.com>
20 *          Adapted from BSD4.4 by Christos Zoulas <christos@ee.cornell.edu>
21 *          Patch for process wait display by Jarl F. Greipsland <jarle@idt.unit.no>
22 *	    Patch for -DORDER by Kenneth Stailey <kstailey@disclosure.com>
23 *	    Patch for new swapctl(2) by Tobias Weingartner <weingart@openbsd.org>
24 */
25
26#include <sys/types.h>
27#include <sys/signal.h>
28#include <sys/param.h>
29
30#define DOSWAP
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <limits.h>
36#include <err.h>
37#include <nlist.h>
38#include <math.h>
39#include <kvm.h>
40#include <unistd.h>
41#include <sys/errno.h>
42#include <sys/sysctl.h>
43#include <sys/dir.h>
44#include <sys/dkstat.h>
45#include <sys/file.h>
46#include <sys/time.h>
47#include <sys/resource.h>
48
49#ifdef DOSWAP
50#include <sys/swap.h>
51#include <err.h>
52#endif
53
54static int check_nlist __P((struct nlist *));
55static int getkval __P((unsigned long, int *, int, char *));
56static int swapmode __P((int *, int *));
57
58#include "top.h"
59#include "display.h"
60#include "machine.h"
61#include "utils.h"
62
63/* get_process_info passes back a handle.  This is what it looks like: */
64
65struct handle {
66	struct kinfo_proc **next_proc;	/* points to next valid proc pointer */
67	int     remaining;	/* number of pointers remaining */
68};
69
70/* declarations for load_avg */
71#include "loadavg.h"
72
73#define PP(pp, field) ((pp)->kp_proc . field)
74#define EP(pp, field) ((pp)->kp_eproc . field)
75#define VP(pp, field) ((pp)->kp_eproc.e_vm . field)
76
77/* what we consider to be process size: */
78#define PROCSIZE(pp) (VP((pp), vm_tsize) + VP((pp), vm_dsize) + VP((pp), vm_ssize))
79
80/* definitions for indices in the nlist array */
81#define X_CP_TIME	0
82
83static struct nlist nlst[] = {
84	{"_cp_time"},		/* 0 */
85	{0}
86};
87/*
88 *  These definitions control the format of the per-process area
89 */
90
91static char header[] =
92"  PID X        PRI NICE  SIZE   RES STATE WAIT     TIME    CPU COMMAND";
93/* 0123456   -- field to fill in starts at header+6 */
94#define UNAME_START 6
95
96#define Proc_format \
97	"%5d %-8.8s %3d %4d %5s %5s %-5s %-6.6s %6s %5.2f%% %.14s"
98
99
100/* process state names for the "STATE" column of the display */
101/* the extra nulls in the string "run" are for adding a slash and
102   the processor number when needed */
103
104char *state_abbrev[] = {
105	"", "start", "run\0\0\0", "sleep", "stop", "zomb",
106};
107
108
109static kvm_t *kd;
110
111/* these are retrieved from the kernel in _init */
112
113static int stathz;
114
115/* these are offsets obtained via nlist and used in the get_ functions */
116
117static unsigned long cp_time_offset;
118
119/* these are for calculating cpu state percentages */
120static long cp_time[CPUSTATES];
121static long cp_old[CPUSTATES];
122static long cp_diff[CPUSTATES];
123
124/* these are for detailing the process states */
125int     process_states[7];
126char   *procstatenames[] = {
127	"", " starting, ", " running, ", " idle, ", " stopped, ", " zombie, ",
128	NULL
129};
130
131/* these are for detailing the cpu states */
132int     cpu_states[CPUSTATES];
133char   *cpustatenames[] = {
134	"user", "nice", "system", "interrupt", "idle", NULL
135};
136
137/* these are for detailing the memory statistics */
138int     memory_stats[8];
139char   *memorynames[] = {
140	"Real: ", "K/", "K act/tot  ", "Free: ", "K  ",
141#ifdef DOSWAP
142	"Swap: ", "K/", "K used/tot",
143#endif
144	NULL
145};
146
147#ifdef ORDER
148/* these are names given to allowed sorting orders -- first is default */
149char   *ordernames[] = {"cpu", "size", "res", "time", "pri", NULL};
150#endif
151
152/* these are for keeping track of the proc array */
153static int nproc;
154static int onproc = -1;
155static int pref_len;
156static struct kinfo_proc *pbase;
157static struct kinfo_proc **pref;
158
159/* these are for getting the memory statistics */
160static int pageshift;		/* log base 2 of the pagesize */
161
162/* define pagetok in terms of pageshift */
163#define pagetok(size) ((size) << pageshift)
164
165int
166getstathz()
167{
168	struct clockinfo cinf;
169	size_t  size = sizeof(cinf);
170	int     mib[2];
171
172	mib[0] = CTL_KERN;
173	mib[1] = KERN_CLOCKRATE;
174	if (sysctl(mib, 2, &cinf, &size, NULL, 0) == -1)
175		return (-1);
176	return (cinf.stathz);
177}
178
179int
180machine_init(statics)
181	struct statics *statics;
182{
183	char    errbuf[_POSIX2_LINE_MAX];
184	int pagesize, i = 0;
185
186	if ((kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL) {
187		warnx("%s", errbuf);
188		return (-1);
189	}
190	setegid(getgid());
191	setgid(getgid());
192
193	/* get the list of symbols we want to access in the kernel */
194	if (kvm_nlist(kd, nlst) < 0) {
195		warnx("nlist failed");
196		return (-1);
197	}
198	/* make sure they were all found */
199	if (i > 0 && check_nlist(nlst) > 0)
200		return (-1);
201
202	stathz = getstathz();
203	if (stathz == -1)
204		return (-1);
205
206	/* stash away certain offsets for later use */
207	cp_time_offset = nlst[X_CP_TIME].n_value;
208
209	pbase = NULL;
210	pref = NULL;
211	onproc = -1;
212	nproc = 0;
213
214	/* get the page size with "getpagesize" and calculate pageshift from
215	 * it */
216	pagesize = getpagesize();
217	pageshift = 0;
218	while (pagesize > 1) {
219		pageshift++;
220		pagesize >>= 1;
221	}
222
223	/* we only need the amount of log(2)1024 for our conversion */
224	pageshift -= LOG1024;
225
226	/* fill in the statics information */
227	statics->procstate_names = procstatenames;
228	statics->cpustate_names = cpustatenames;
229	statics->memory_names = memorynames;
230#ifdef ORDER
231	statics->order_names = ordernames;
232#endif
233	return (0);
234}
235
236char *
237format_header(uname_field)
238	char   *uname_field;
239{
240	char *ptr;
241
242	ptr = header + UNAME_START;
243	while (*uname_field != '\0') {
244		*ptr++ = *uname_field++;
245	}
246	return (header);
247}
248
249void
250get_system_info(si)
251	struct system_info *si;
252{
253	static int sysload_mib[] = {CTL_VM, VM_LOADAVG};
254	static int vmtotal_mib[] = {CTL_VM, VM_METER};
255	struct loadavg sysload;
256	struct vmtotal vmtotal;
257	double *infoloadp;
258	int total, i;
259	size_t  size;
260
261	/* get the cp_time array */
262	(void) getkval(cp_time_offset, (int *) cp_time, sizeof(cp_time),
263	    "_cp_time");
264
265	size = sizeof(sysload);
266	if (sysctl(sysload_mib, 2, &sysload, &size, NULL, 0) < 0) {
267		warn("sysctl failed");
268		bzero(&total, sizeof(total));
269	}
270	infoloadp = si->load_avg;
271	for (i = 0; i < 3; i++)
272		*infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale;
273
274	/* convert cp_time counts to percentages */
275	total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
276
277	/* get total -- systemwide main memory usage structure */
278	size = sizeof(vmtotal);
279	if (sysctl(vmtotal_mib, 2, &vmtotal, &size, NULL, 0) < 0) {
280		warn("sysctl failed");
281		bzero(&vmtotal, sizeof(vmtotal));
282	}
283	/* convert memory stats to Kbytes */
284	memory_stats[0] = -1;
285	memory_stats[1] = pagetok(vmtotal.t_arm);
286	memory_stats[2] = pagetok(vmtotal.t_rm);
287	memory_stats[3] = -1;
288	memory_stats[4] = pagetok(vmtotal.t_free);
289	memory_stats[5] = -1;
290#ifdef DOSWAP
291	if (!swapmode(&memory_stats[6], &memory_stats[7])) {
292		memory_stats[6] = 0;
293		memory_stats[7] = 0;
294	}
295#endif
296
297	/* set arrays and strings */
298	si->cpustates = cpu_states;
299	si->memory = memory_stats;
300	si->last_pid = -1;
301}
302
303static struct handle handle;
304
305caddr_t
306get_process_info(si, sel, compare)
307	struct system_info *si;
308	struct process_select *sel;
309	int (*compare) __P((const void *, const void *));
310
311{
312	int show_idle, show_system, show_uid, show_command;
313	int total_procs, active_procs, i;
314	struct kinfo_proc **prefp, *pp;
315
316	if ((pbase = kvm_getprocs(kd, KERN_PROC_KTHREAD, 0, &nproc)) == NULL) {
317		warnx("%s", kvm_geterr(kd));
318		quit(23);
319	}
320	if (nproc > onproc)
321		pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *)
322		    * (onproc = nproc));
323	if (pref == NULL) {
324		warnx("Out of memory.");
325		quit(23);
326	}
327	/* get a pointer to the states summary array */
328	si->procstates = process_states;
329
330	/* set up flags which define what we are going to select */
331	show_idle = sel->idle;
332	show_system = sel->system;
333	show_uid = sel->uid != -1;
334	show_command = sel->command != NULL;
335
336	/* count up process states and get pointers to interesting procs */
337	total_procs = 0;
338	active_procs = 0;
339	memset((char *) process_states, 0, sizeof(process_states));
340	prefp = pref;
341	for (pp = pbase, i = 0; i < nproc; pp++, i++) {
342		/*
343		 *  Place pointers to each valid proc structure in pref[].
344		 *  Process slots that are actually in use have a non-zero
345		 *  status field.  Processes with SSYS set are system
346		 *  processes---these get ignored unless show_sysprocs is set.
347		 */
348		if (PP(pp, p_stat) != 0 &&
349		    (show_system || ((PP(pp, p_flag) & P_SYSTEM) == 0))) {
350			total_procs++;
351			process_states[(unsigned char) PP(pp, p_stat)]++;
352			if ((PP(pp, p_stat) != SZOMB) &&
353			    (show_idle || (PP(pp, p_pctcpu) != 0) ||
354				(PP(pp, p_stat) == SRUN)) &&
355			    (!show_uid || EP(pp, e_pcred.p_ruid) == (uid_t) sel->uid)) {
356				*prefp++ = pp;
357				active_procs++;
358			}
359		}
360	}
361
362	/* if requested, sort the "interesting" processes */
363	if (compare != NULL) {
364		qsort((char *) pref, active_procs, sizeof(struct kinfo_proc *), compare);
365	}
366	/* remember active and total counts */
367	si->p_total = total_procs;
368	si->p_active = pref_len = active_procs;
369
370	/* pass back a handle */
371	handle.next_proc = pref;
372	handle.remaining = active_procs;
373	return ((caddr_t) & handle);
374}
375
376char    fmt[MAX_COLS];		/* static area where result is built */
377
378char *
379format_next_process(handle, get_userid)
380	caddr_t handle;
381	char *(*get_userid)();
382
383{
384	char waddr[sizeof(void *) * 2 + 3];	/* Hexify void pointer */
385	struct kinfo_proc *pp;
386	struct handle *hp;
387	char *p_wait;
388	int cputime;
389	double pct;
390
391	/* find and remember the next proc structure */
392	hp = (struct handle *) handle;
393	pp = *(hp->next_proc++);
394	hp->remaining--;
395
396	/* get the process's user struct and set cputime */
397	if ((PP(pp, p_flag) & P_INMEM) == 0) {
398		/*
399		 * Print swapped processes as <pname>
400		 */
401		char   *comm = PP(pp, p_comm);
402#define COMSIZ sizeof(PP(pp, p_comm))
403		char    buf[COMSIZ];
404		(void) strncpy(buf, comm, COMSIZ);
405		comm[0] = '<';
406		(void) strncpy(&comm[1], buf, COMSIZ - 2);
407		comm[COMSIZ - 2] = '\0';
408		(void) strncat(comm, ">", COMSIZ - 1);
409		comm[COMSIZ - 1] = '\0';
410	}
411	cputime = (PP(pp, p_uticks) + PP(pp, p_sticks) + PP(pp, p_iticks)) / stathz;
412
413	/* calculate the base for cpu percentages */
414	pct = pctdouble(PP(pp, p_pctcpu));
415
416	if (PP(pp, p_wchan))
417		if (PP(pp, p_wmesg))
418			p_wait = EP(pp, e_wmesg);
419		else {
420			snprintf(waddr, sizeof(waddr), "%lx",
421			    (unsigned long) (PP(pp, p_wchan)) & ~KERNBASE);
422			p_wait = waddr;
423		}
424	else
425		p_wait = "-";
426
427	/* format this entry */
428	snprintf(fmt, MAX_COLS,
429	    Proc_format,
430	    PP(pp, p_pid),
431	    (*get_userid) (EP(pp, e_pcred.p_ruid)),
432	    PP(pp, p_priority) - PZERO,
433	    PP(pp, p_nice) - NZERO,
434	    format_k(pagetok(PROCSIZE(pp))),
435	    format_k(pagetok(VP(pp, vm_rssize))),
436	    (PP(pp, p_stat) == SSLEEP && PP(pp, p_slptime) > MAXSLP)
437	    ? "idle" : state_abbrev[(unsigned char) PP(pp, p_stat)],
438	    p_wait,
439	    format_time(cputime),
440	    100.0 * pct,
441	    printable(PP(pp, p_comm)));
442
443	/* return the result */
444	return (fmt);
445}
446
447
448/*
449 * check_nlist(nlst) - checks the nlist to see if any symbols were not
450 *		found.  For every symbol that was not found, a one-line
451 *		message is printed to stderr.  The routine returns the
452 *		number of symbols NOT found.
453 */
454int
455check_nlist(nlst)
456	struct nlist *nlst;
457{
458	int i;
459
460	/* check to see if we got ALL the symbols we requested */
461	/* this will write one line to stderr for every symbol not found */
462
463	i = 0;
464	while (nlst->n_name != NULL) {
465		if (nlst->n_type == 0) {
466			/* this one wasn't found */
467			(void) fprintf(stderr, "kernel: no symbol named `%s'\n",
468			    nlst->n_name);
469			i = 1;
470		}
471		nlst++;
472	}
473
474	return (i);
475}
476
477
478/*
479 *  getkval(offset, ptr, size, refstr) - get a value out of the kernel.
480 *	"offset" is the byte offset into the kernel for the desired value,
481 *  	"ptr" points to a buffer into which the value is retrieved,
482 *  	"size" is the size of the buffer (and the object to retrieve),
483 *  	"refstr" is a reference string used when printing error meessages,
484 *	    if "refstr" starts with a '!', then a failure on read will not
485 *  	    be fatal (this may seem like a silly way to do things, but I
486 *  	    really didn't want the overhead of another argument).
487 *
488 */
489int
490getkval(offset, ptr, size, refstr)
491	unsigned long offset;
492	int    *ptr;
493	int     size;
494	char   *refstr;
495{
496	if (kvm_read(kd, offset, ptr, size) != size) {
497		if (*refstr == '!') {
498			return (0);
499		} else {
500			warn("kvm_read for %s", refstr);
501			quit(23);
502		}
503	}
504	return (1);
505}
506/* comparison routine for qsort */
507
508static unsigned char sorted_state[] =
509{
510	0,			/* not used		 */
511	4,			/* start		 */
512	5,			/* run			 */
513	2,			/* sleep		 */
514	3,			/* stop			 */
515	1			/* zombie		 */
516};
517#ifdef ORDER
518
519/*
520 *  proc_compares - comparison functions for "qsort"
521 */
522
523/*
524 * First, the possible comparison keys.  These are defined in such a way
525 * that they can be merely listed in the source code to define the actual
526 * desired ordering.
527 */
528
529
530#define ORDERKEY_PCTCPU \
531	if (lresult = (pctcpu)PP(p2, p_pctcpu) - (pctcpu)PP(p1, p_pctcpu), \
532           (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0)
533#define ORDERKEY_CPUTIME \
534	if ((result = PP(p2, p_rtime.tv_sec) - PP(p1, p_rtime.tv_sec)) == 0) \
535		if ((result = PP(p2, p_rtime.tv_usec) - \
536		     PP(p1, p_rtime.tv_usec)) == 0)
537#define ORDERKEY_STATE \
538	if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] - \
539                      sorted_state[(unsigned char) PP(p1, p_stat)])  == 0)
540#define ORDERKEY_PRIO \
541	if ((result = PP(p2, p_priority) - PP(p1, p_priority)) == 0)
542#define ORDERKEY_RSSIZE \
543	if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0)
544#define ORDERKEY_MEM \
545	if ((result = PROCSIZE(p2) - PROCSIZE(p1)) == 0)
546
547
548/* compare_cpu - the comparison function for sorting by cpu percentage */
549int
550compare_cpu(v1, v2)
551	const void *v1, *v2;
552{
553	struct proc **pp1 = (struct proc **) v1;
554	struct proc **pp2 = (struct proc **) v2;
555	struct kinfo_proc *p1;
556	struct kinfo_proc *p2;
557	int result;
558	pctcpu lresult;
559
560	/* remove one level of indirection */
561	p1 = *(struct kinfo_proc **) pp1;
562	p2 = *(struct kinfo_proc **) 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 */
575int
576compare_size(v1, v2)
577	const void *v1, *v2;
578{
579	struct proc **pp1 = (struct proc **) v1;
580	struct proc **pp2 = (struct proc **) v2;
581	struct kinfo_proc *p1;
582	struct kinfo_proc *p2;
583	int result;
584	pctcpu lresult;
585
586	/* remove one level of indirection */
587	p1 = *(struct kinfo_proc **) pp1;
588	p2 = *(struct kinfo_proc **) pp2;
589
590	ORDERKEY_MEM
591	    ORDERKEY_RSSIZE
592	    ORDERKEY_PCTCPU
593	    ORDERKEY_CPUTIME
594	    ORDERKEY_STATE
595	    ORDERKEY_PRIO
596	    ;
597	return (result);
598}
599
600/* compare_res - the comparison function for sorting by resident set size */
601int
602compare_res(v1, v2)
603	const void *v1, *v2;
604{
605	struct proc **pp1 = (struct proc **) v1;
606	struct proc **pp2 = (struct proc **) v2;
607	struct kinfo_proc *p1;
608	struct kinfo_proc *p2;
609	int result;
610	pctcpu lresult;
611
612	/* remove one level of indirection */
613	p1 = *(struct kinfo_proc **) pp1;
614	p2 = *(struct kinfo_proc **) pp2;
615
616	ORDERKEY_RSSIZE
617	    ORDERKEY_MEM
618	    ORDERKEY_PCTCPU
619	    ORDERKEY_CPUTIME
620	    ORDERKEY_STATE
621	    ORDERKEY_PRIO
622	    ;
623	return (result);
624}
625
626/* compare_time - the comparison function for sorting by CPU time */
627int
628compare_time(v1, v2)
629	const void *v1, *v2;
630{
631	struct proc **pp1 = (struct proc **) v1;
632	struct proc **pp2 = (struct proc **) v2;
633	struct kinfo_proc *p1;
634	struct kinfo_proc *p2;
635	int result;
636	pctcpu lresult;
637
638	/* remove one level of indirection */
639	p1 = *(struct kinfo_proc **) pp1;
640	p2 = *(struct kinfo_proc **) pp2;
641
642	ORDERKEY_CPUTIME
643	    ORDERKEY_PCTCPU
644	    ORDERKEY_STATE
645	    ORDERKEY_PRIO
646	    ORDERKEY_MEM
647	    ORDERKEY_RSSIZE
648	    ;
649	return (result);
650}
651
652/* compare_prio - the comparison function for sorting by CPU time */
653int
654compare_prio(v1, v2)
655	const void *v1, *v2;
656{
657	struct proc **pp1 = (struct proc **) v1;
658	struct proc **pp2 = (struct proc **) v2;
659	struct kinfo_proc *p1;
660	struct kinfo_proc *p2;
661	int result;
662	pctcpu lresult;
663
664	/* remove one level of indirection */
665	p1 = *(struct kinfo_proc **) pp1;
666	p2 = *(struct kinfo_proc **) pp2;
667
668	ORDERKEY_PRIO
669	    ORDERKEY_PCTCPU
670	    ORDERKEY_CPUTIME
671	    ORDERKEY_STATE
672	    ORDERKEY_RSSIZE
673	    ORDERKEY_MEM
674	    ;
675	return (result);
676}
677
678int     (*proc_compares[]) () = {
679	compare_cpu,
680	compare_size,
681	compare_res,
682	compare_time,
683	compare_prio,
684	NULL
685};
686#else
687/*
688 *  proc_compare - comparison function for "qsort"
689 *	Compares the resource consumption of two processes using five
690 *  	distinct keys.  The keys (in descending order of importance) are:
691 *  	percent cpu, cpu ticks, state, resident set size, total virtual
692 *  	memory usage.  The process states are ordered as follows (from least
693 *  	to most important):  zombie, sleep, stop, start, run.  The array
694 *  	declaration below maps a process state index into a number that
695 *  	reflects this ordering.
696 */
697int
698proc_compare(v1, v2)
699	const void *v1, *v2;
700{
701	struct proc **pp1 = (struct proc **) v1;
702	struct proc **pp2 = (struct proc **) v2;
703	struct kinfo_proc *p1;
704	struct kinfo_proc *p2;
705	int result;
706	pctcpu lresult;
707
708	/* remove one level of indirection */
709	p1 = *(struct kinfo_proc **) pp1;
710	p2 = *(struct kinfo_proc **) pp2;
711
712	/* compare percent cpu (pctcpu) */
713	if ((lresult = PP(p2, p_pctcpu) - PP(p1, p_pctcpu)) == 0) {
714		/* use CPU usage to break the tie */
715		if ((result = PP(p2, p_rtime).tv_sec - PP(p1, p_rtime).tv_sec) == 0) {
716			/* use process state to break the tie */
717			if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] -
718				sorted_state[(unsigned char) PP(p1, p_stat)]) == 0) {
719				/* use priority to break the tie */
720				if ((result = PP(p2, p_priority) - PP(p1, p_priority)) == 0) {
721					/* use resident set size (rssize) to
722					 * break the tie */
723					if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0) {
724						/* use total memory to break
725						 * the tie */
726						result = PROCSIZE(p2) - PROCSIZE(p1);
727					}
728				}
729			}
730		}
731	} else {
732		result = lresult < 0 ? -1 : 1;
733	}
734
735	return (result);
736}
737#endif
738
739/*
740 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
741 *		the process does not exist.
742 *		It is EXTREMLY IMPORTANT that this function work correctly.
743 *		If top runs setuid root (as in SVR4), then this function
744 *		is the only thing that stands in the way of a serious
745 *		security problem.  It validates requests for the "kill"
746 *		and "renice" commands.
747 */
748int
749proc_owner(pid)
750	pid_t   pid;
751{
752	struct kinfo_proc **prefp, *pp;
753	int cnt;
754
755	prefp = pref;
756	cnt = pref_len;
757	while (--cnt >= 0) {
758		pp = *prefp++;
759		if (PP(pp, p_pid) == pid) {
760			return ((int) EP(pp, e_pcred.p_ruid));
761		}
762	}
763	return (-1);
764}
765#ifdef DOSWAP
766/*
767 * swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
768 * to be based on the new swapctl(2) system call.
769 */
770static int
771swapmode(used, total)
772	int    *used;
773	int    *total;
774{
775	int     nswap, rnswap, i;
776	struct swapent *swdev;
777
778	nswap = swapctl(SWAP_NSWAP, 0, 0);
779	if (nswap == 0)
780		return 0;
781
782	swdev = malloc(nswap * sizeof(*swdev));
783	if (swdev == NULL)
784		return 0;
785
786	rnswap = swapctl(SWAP_STATS, swdev, nswap);
787	if (rnswap == -1)
788		return 0;
789
790	/* if rnswap != nswap, then what? */
791
792	/* Total things up */
793	*total = *used = 0;
794	for (i = 0; i < nswap; i++) {
795		if (swdev[i].se_flags & SWF_ENABLE) {
796			*used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
797			*total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
798		}
799	}
800
801	free(swdev);
802	return 1;
803}
804#endif
805