machine.c revision 37100
1/*
2 * top - a top users display for Unix
3 *
4 * SYNOPSIS:  For FreeBSD-2.x system
5 *
6 * DESCRIPTION:
7 * Originally written for BSD4.4 system by Christos Zoulas.
8 * Ported to FreeBSD 2.x by Steven Wallace && Wolfram Schneider
9 *
10 * This is the machine-dependent module for FreeBSD 2.2
11 * Works for:
12 *	FreeBSD 2.2, and probably FreeBSD 2.1.x
13 *
14 * LIBS: -lkvm
15 *
16 * AUTHOR:  Christos Zoulas <christos@ee.cornell.edu>
17 *          Steven Wallace  <swallace@freebsd.org>
18 *          Wolfram Schneider <wosch@FreeBSD.org>
19 *
20 * $Id: machine.c,v 1.10 1998/05/28 09:29:48 phk Exp $
21 */
22
23
24#include <sys/types.h>
25#include <sys/signal.h>
26#include <sys/param.h>
27
28#include "os.h"
29#include <stdio.h>
30#include <nlist.h>
31#include <math.h>
32#include <kvm.h>
33#include <pwd.h>
34#include <sys/errno.h>
35#include <sys/sysctl.h>
36#include <sys/dkstat.h>
37#include <sys/file.h>
38#include <sys/time.h>
39#include <sys/proc.h>
40#include <sys/user.h>
41#include <sys/vmmeter.h>
42#include <sys/resource.h>
43#include <sys/rtprio.h>
44
45/* Swap */
46#include <stdlib.h>
47#include <sys/rlist.h>
48#include <sys/conf.h>
49
50#include <osreldate.h> /* for changes in kernel structures */
51
52#include "top.h"
53#include "machine.h"
54
55static int check_nlist __P((struct nlist *));
56static int getkval __P((unsigned long, int *, int, char *));
57extern char* printable __P((char *));
58int swapmode __P((int *retavail, int *retfree));
59static int smpmode;
60static int namelength;
61static int cmdlength;
62
63
64/* get_process_info passes back a handle.  This is what it looks like: */
65
66struct handle
67{
68    struct kinfo_proc **next_proc;	/* points to next valid proc pointer */
69    int remaining;		/* number of pointers remaining */
70};
71
72/* declarations for load_avg */
73#include "loadavg.h"
74
75#define PP(pp, field) ((pp)->kp_proc . field)
76#define EP(pp, field) ((pp)->kp_eproc . field)
77#define VP(pp, field) ((pp)->kp_eproc.e_vm . field)
78
79/* define what weighted cpu is.  */
80#define weighted_cpu(pct, pp) (PP((pp), p_swtime) == 0 ? 0.0 : \
81			 ((pct) / (1.0 - exp(PP((pp), p_swtime) * logcpu))))
82
83/* what we consider to be process size: */
84#define PROCSIZE(pp) (VP((pp), vm_map.size) / 1024)
85
86/* definitions for indices in the nlist array */
87
88
89static struct nlist nlst[] = {
90#define X_CCPU		0
91    { "_ccpu" },		/* 0 */
92#define X_CP_TIME	1
93    { "_cp_time" },		/* 1 */
94#define X_HZ		2
95    { "_hz" },		        /* 2 */
96#define X_STATHZ	3
97    { "_stathz" },		/* 3 */
98#define X_AVENRUN	4
99    { "_averunnable" },		/* 4 */
100
101/* Swap */
102#define VM_SWAPLIST	5
103	{ "_swaplist" },/* list of free swap areas */
104#define VM_SWDEVT	6
105	{ "_swdevt" },	/* list of swap devices and sizes */
106#define VM_NSWAP	7
107	{ "_nswap" },	/* size of largest swap device */
108#define VM_NSWDEV	8
109	{ "_nswdev" },	/* number of swap devices */
110#define VM_DMMAX	9
111	{ "_dmmax" },	/* maximum size of a swap block */
112#define X_BUFSPACE	10
113	{ "_bufspace" },	/* K in buffer cache */
114#define X_CNT           11
115    { "_cnt" },		        /* struct vmmeter cnt */
116
117/* Last pid */
118#define X_LASTPID	12
119    { "_nextpid" },
120    { 0 }
121};
122
123/*
124 *  These definitions control the format of the per-process area
125 */
126
127static char smp_header[] =
128  "  PID %-*.*s PRI NICE  SIZE    RES STATE  C   TIME   WCPU    CPU COMMAND";
129
130#define smp_Proc_format \
131	"%5d %-*.*s %3d %3d%7s %6s %-6.6s %1x%7s %5.2f%% %5.2f%% %.*s"
132
133static char up_header[] =
134  "  PID %-*.*s PRI NICE  SIZE    RES STATE    TIME   WCPU    CPU COMMAND";
135
136#define up_Proc_format \
137	"%5d %-*.*s %3d %3d%7s %6s %-6.6s%.0d%7s %5.2f%% %5.2f%% %.*s"
138
139
140
141/* process state names for the "STATE" column of the display */
142/* the extra nulls in the string "run" are for adding a slash and
143   the processor number when needed */
144
145char *state_abbrev[] =
146{
147    "", "START", "RUN\0\0\0", "SLEEP", "STOP", "ZOMB",
148};
149
150
151static kvm_t *kd;
152
153/* values that we stash away in _init and use in later routines */
154
155static double logcpu;
156
157/* these are retrieved from the kernel in _init */
158
159static          long hz;
160static load_avg  ccpu;
161
162/* these are offsets obtained via nlist and used in the get_ functions */
163
164static unsigned long cp_time_offset;
165static unsigned long avenrun_offset;
166static unsigned long lastpid_offset;
167static long lastpid;
168static unsigned long cnt_offset;
169static unsigned long bufspace_offset;
170static long cnt;
171
172/* these are for calculating cpu state percentages */
173
174static long cp_time[CPUSTATES];
175static long cp_old[CPUSTATES];
176static long cp_diff[CPUSTATES];
177
178/* these are for detailing the process states */
179
180int process_states[6];
181char *procstatenames[] = {
182    "", " starting, ", " running, ", " sleeping, ", " stopped, ",
183    " zombie, ",
184    NULL
185};
186
187/* these are for detailing the cpu states */
188
189int cpu_states[CPUSTATES];
190char *cpustatenames[] = {
191    "user", "nice", "system", "interrupt", "idle", NULL
192};
193
194/* these are for detailing the memory statistics */
195
196int memory_stats[7];
197char *memorynames[] = {
198    "K Active, ", "K Inact, ", "K Wired, ", "K Cache, ", "K Buf, ", "K Free",
199    NULL
200};
201
202int swap_stats[7];
203char *swapnames[] = {
204/*   0           1            2           3            4       5 */
205    "K Total, ", "K Used, ", "K Free, ", "% Inuse, ", "K In, ", "K Out",
206    NULL
207};
208
209
210/* these are for keeping track of the proc array */
211
212static int nproc;
213static int onproc = -1;
214static int pref_len;
215static struct kinfo_proc *pbase;
216static struct kinfo_proc **pref;
217
218/* these are for getting the memory statistics */
219
220static int pageshift;		/* log base 2 of the pagesize */
221
222/* define pagetok in terms of pageshift */
223
224#define pagetok(size) ((size) << pageshift)
225
226/* useful externals */
227long percentages();
228
229int
230machine_init(statics)
231
232struct statics *statics;
233
234{
235    register int i = 0;
236    register int pagesize;
237    int modelen;
238    struct passwd *pw;
239
240    modelen = sizeof(smpmode);
241    if ((sysctlbyname("machdep.smp_active", &smpmode, &modelen, NULL, 0) < 0 &&
242         sysctlbyname("smp.smp_active", &smpmode, &modelen, NULL, 0) < 0) ||
243	modelen != sizeof(smpmode))
244	    smpmode = 0;
245
246    while ((pw = getpwent()) != NULL) {
247	if (strlen(pw->pw_name) > namelength)
248	    namelength = strlen(pw->pw_name);
249    }
250    if (namelength < 8)
251	namelength = 8;
252    if (namelength > 16)
253	namelength = 16;
254
255    if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL)
256	return -1;
257
258
259    /* get the list of symbols we want to access in the kernel */
260    (void) kvm_nlist(kd, nlst);
261    if (nlst[0].n_type == 0)
262    {
263	fprintf(stderr, "top: nlist failed\n");
264	return(-1);
265    }
266
267    /* make sure they were all found */
268    if (i > 0 && check_nlist(nlst) > 0)
269    {
270	return(-1);
271    }
272
273    /* get the symbol values out of kmem */
274    (void) getkval(nlst[X_STATHZ].n_value, (int *)(&hz), sizeof(hz), "!");
275    if (!hz) {
276	(void) getkval(nlst[X_HZ].n_value, (int *)(&hz), sizeof(hz),
277		       nlst[X_HZ].n_name);
278    }
279
280    (void) getkval(nlst[X_CCPU].n_value,   (int *)(&ccpu),	sizeof(ccpu),
281	    nlst[X_CCPU].n_name);
282
283    /* stash away certain offsets for later use */
284    cp_time_offset = nlst[X_CP_TIME].n_value;
285    avenrun_offset = nlst[X_AVENRUN].n_value;
286    lastpid_offset =  nlst[X_LASTPID].n_value;
287    cnt_offset = nlst[X_CNT].n_value;
288    bufspace_offset = nlst[X_BUFSPACE].n_value;
289
290    /* this is used in calculating WCPU -- calculate it ahead of time */
291    logcpu = log(loaddouble(ccpu));
292
293    pbase = NULL;
294    pref = NULL;
295    nproc = 0;
296    onproc = -1;
297    /* get the page size with "getpagesize" and calculate pageshift from it */
298    pagesize = getpagesize();
299    pageshift = 0;
300    while (pagesize > 1)
301    {
302	pageshift++;
303	pagesize >>= 1;
304    }
305
306    /* we only need the amount of log(2)1024 for our conversion */
307    pageshift -= LOG1024;
308
309    /* fill in the statics information */
310    statics->procstate_names = procstatenames;
311    statics->cpustate_names = cpustatenames;
312    statics->memory_names = memorynames;
313    statics->swap_names = swapnames;
314
315    /* all done! */
316    return(0);
317}
318
319char *format_header(uname_field)
320
321register char *uname_field;
322
323{
324    register char *ptr;
325    static char Header[128];
326
327    snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header,
328	     namelength, namelength, uname_field);
329
330    cmdlength = 80 - strlen(Header) + 6;
331
332    return Header;
333}
334
335static int swappgsin = -1;
336static int swappgsout = -1;
337extern struct timeval timeout;
338
339void
340get_system_info(si)
341
342struct system_info *si;
343
344{
345    long total;
346    load_avg avenrun[3];
347
348    /* get the cp_time array */
349    (void) getkval(cp_time_offset, (int *)cp_time, sizeof(cp_time),
350		   nlst[X_CP_TIME].n_name);
351    (void) getkval(avenrun_offset, (int *)avenrun, sizeof(avenrun),
352		   nlst[X_AVENRUN].n_name);
353
354    (void) getkval(lastpid_offset, (int *)(&lastpid), sizeof(lastpid),
355		   "!");
356
357    /* convert load averages to doubles */
358    {
359	register int i;
360	register double *infoloadp;
361	load_avg *avenrunp;
362
363#ifdef notyet
364	struct loadavg sysload;
365	int size;
366	getkerninfo(KINFO_LOADAVG, &sysload, &size, 0);
367#endif
368
369	infoloadp = si->load_avg;
370	avenrunp = avenrun;
371	for (i = 0; i < 3; i++)
372	{
373#ifdef notyet
374	    *infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale;
375#endif
376	    *infoloadp++ = loaddouble(*avenrunp++);
377	}
378    }
379
380    /* convert cp_time counts to percentages */
381    total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
382
383    /* sum memory & swap statistics */
384    {
385	struct vmmeter sum;
386	static unsigned int swap_delay = 0;
387	static int swapavail = 0;
388	static int swapfree = 0;
389	static int bufspace = 0;
390
391        (void) getkval(cnt_offset, (int *)(&sum), sizeof(sum),
392		   "_cnt");
393        (void) getkval(bufspace_offset, (int *)(&bufspace), sizeof(bufspace),
394		   "_bufspace");
395
396	/* convert memory stats to Kbytes */
397	memory_stats[0] = pagetok(sum.v_active_count);
398	memory_stats[1] = pagetok(sum.v_inactive_count);
399	memory_stats[2] = pagetok(sum.v_wire_count);
400	memory_stats[3] = pagetok(sum.v_cache_count);
401	memory_stats[4] = bufspace / 1024;
402	memory_stats[5] = pagetok(sum.v_free_count);
403	memory_stats[6] = -1;
404
405	/* first interval */
406        if (swappgsin < 0) {
407	    swap_stats[4] = 0;
408	    swap_stats[5] = 0;
409	}
410
411	/* compute differences between old and new swap statistic */
412	else {
413	    swap_stats[4] = pagetok(((sum.v_swappgsin - swappgsin)));
414	    swap_stats[5] = pagetok(((sum.v_swappgsout - swappgsout)));
415	}
416
417        swappgsin = sum.v_swappgsin;
418	swappgsout = sum.v_swappgsout;
419
420	/* call CPU heavy swapmode() only for changes */
421        if (swap_stats[4] > 0 || swap_stats[5] > 0 || swap_delay == 0) {
422	    swap_stats[3] = swapmode(&swapavail, &swapfree);
423	    swap_stats[0] = swapavail;
424	    swap_stats[1] = swapavail - swapfree;
425	    swap_stats[2] = swapfree;
426	}
427        swap_delay = 1;
428	swap_stats[6] = -1;
429    }
430
431    /* set arrays and strings */
432    si->cpustates = cpu_states;
433    si->memory = memory_stats;
434    si->swap = swap_stats;
435
436
437    if(lastpid > 0) {
438	si->last_pid = lastpid;
439    } else {
440	si->last_pid = -1;
441    }
442}
443
444static struct handle handle;
445
446caddr_t get_process_info(si, sel, compare)
447
448struct system_info *si;
449struct process_select *sel;
450int (*compare)();
451
452{
453    register int i;
454    register int total_procs;
455    register int active_procs;
456    register struct kinfo_proc **prefp;
457    register struct kinfo_proc *pp;
458
459    /* these are copied out of sel for speed */
460    int show_idle;
461    int show_system;
462    int show_uid;
463    int show_command;
464
465
466    pbase = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc);
467    if (nproc > onproc)
468	pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *)
469		* (onproc = nproc));
470    if (pref == NULL || pbase == NULL) {
471	(void) fprintf(stderr, "top: Out of memory.\n");
472	quit(23);
473    }
474    /* get a pointer to the states summary array */
475    si->procstates = process_states;
476
477    /* set up flags which define what we are going to select */
478    show_idle = sel->idle;
479    show_system = sel->system;
480    show_uid = sel->uid != -1;
481    show_command = sel->command != NULL;
482
483    /* count up process states and get pointers to interesting procs */
484    total_procs = 0;
485    active_procs = 0;
486    memset((char *)process_states, 0, sizeof(process_states));
487    prefp = pref;
488    for (pp = pbase, i = 0; i < nproc; pp++, i++)
489    {
490	/*
491	 *  Place pointers to each valid proc structure in pref[].
492	 *  Process slots that are actually in use have a non-zero
493	 *  status field.  Processes with P_SYSTEM set are system
494	 *  processes---these get ignored unless show_sysprocs is set.
495	 */
496	if (PP(pp, p_stat) != 0 &&
497	    (show_system || ((PP(pp, p_flag) & P_SYSTEM) == 0)))
498	{
499	    total_procs++;
500	    process_states[(unsigned char) PP(pp, p_stat)]++;
501	    if ((PP(pp, p_stat) != SZOMB) &&
502		(show_idle || (PP(pp, p_pctcpu) != 0) ||
503		 (PP(pp, p_stat) == SRUN)) &&
504		(!show_uid || EP(pp, e_pcred.p_ruid) == (uid_t)sel->uid))
505	    {
506		*prefp++ = pp;
507		active_procs++;
508	    }
509	}
510    }
511
512    /* if requested, sort the "interesting" processes */
513    if (compare != NULL)
514    {
515	qsort((char *)pref, active_procs, sizeof(struct kinfo_proc *), compare);
516    }
517
518    /* remember active and total counts */
519    si->p_total = total_procs;
520    si->p_active = pref_len = active_procs;
521
522    /* pass back a handle */
523    handle.next_proc = pref;
524    handle.remaining = active_procs;
525    return((caddr_t)&handle);
526}
527
528char fmt[128];		/* static area where result is built */
529
530char *format_next_process(handle, get_userid)
531
532caddr_t handle;
533char *(*get_userid)();
534
535{
536    register struct kinfo_proc *pp;
537    register long cputime;
538    register double pct;
539    struct handle *hp;
540    char status[16];
541
542    /* find and remember the next proc structure */
543    hp = (struct handle *)handle;
544    pp = *(hp->next_proc++);
545    hp->remaining--;
546
547
548    /* get the process's user struct and set cputime */
549    if ((PP(pp, p_flag) & P_INMEM) == 0) {
550	/*
551	 * Print swapped processes as <pname>
552	 */
553	char *comm = PP(pp, p_comm);
554#define COMSIZ sizeof(PP(pp, p_comm))
555	char buf[COMSIZ];
556	(void) strncpy(buf, comm, COMSIZ);
557	comm[0] = '<';
558	(void) strncpy(&comm[1], buf, COMSIZ - 2);
559	comm[COMSIZ - 2] = '\0';
560	(void) strncat(comm, ">", COMSIZ - 1);
561	comm[COMSIZ - 1] = '\0';
562    }
563
564#if 0
565    /* This does not produce the correct results */
566    cputime = PP(pp, p_uticks) + PP(pp, p_sticks) + PP(pp, p_iticks);
567#endif
568    cputime = PP(pp, p_runtime) / 1000000; /* This does not count interrupts */
569
570    /* calculate the base for cpu percentages */
571    pct = pctdouble(PP(pp, p_pctcpu));
572
573    /* generate "STATE" field */
574    switch (PP(pp, p_stat)) {
575	case SRUN:
576	    if (smpmode && PP(pp, p_oncpu) >= 0)
577		sprintf(status, "CPU%d", PP(pp, p_oncpu));
578	    else
579		strcpy(status, "RUN");
580	    break;
581	case SSLEEP:
582	    if (PP(pp, p_wmesg) != NULL) {
583		sprintf(status, "%.6s", EP(pp, e_wmesg));
584		break;
585	    }
586	    /* fall through */
587	default:
588	    sprintf(status, "%.6s", state_abbrev[(unsigned char) PP(pp, p_stat)]);
589	    break;
590    }
591
592    /* format this entry */
593    sprintf(fmt,
594	    smpmode ? smp_Proc_format : up_Proc_format,
595	    PP(pp, p_pid),
596	    namelength, namelength,
597	    (*get_userid)(EP(pp, e_pcred.p_ruid)),
598	    PP(pp, p_priority) - PZERO,
599
600	    /*
601	     * normal time      -> nice value -20 - +20
602	     * real time 0 - 31 -> nice value -52 - -21
603	     * idle time 0 - 31 -> nice value +21 - +52
604	     */
605	    (PP(pp, p_rtprio.type) ==  RTP_PRIO_NORMAL ?
606	    	PP(pp, p_nice) - NZERO :
607	    	(PP(pp, p_rtprio.type) ==  RTP_PRIO_REALTIME ?
608		    (PRIO_MIN - 1 - RTP_PRIO_MAX + PP(pp, p_rtprio.prio)) :
609		    (PRIO_MAX + 1 + PP(pp, p_rtprio.prio)))),
610	    format_k2(PROCSIZE(pp)),
611	    format_k2(pagetok(VP(pp, vm_rssize))),
612	    status,
613	    smpmode ? PP(pp, p_lastcpu) : 0,
614	    format_time(cputime),
615	    10000.0 * weighted_cpu(pct, pp) / hz,
616	    10000.0 * pct / hz,
617	    cmdlength,
618	    printable(PP(pp, p_comm)));
619
620    /* return the result */
621    return(fmt);
622}
623
624
625/*
626 * check_nlist(nlst) - checks the nlist to see if any symbols were not
627 *		found.  For every symbol that was not found, a one-line
628 *		message is printed to stderr.  The routine returns the
629 *		number of symbols NOT found.
630 */
631
632static int check_nlist(nlst)
633
634register struct nlist *nlst;
635
636{
637    register int i;
638
639    /* check to see if we got ALL the symbols we requested */
640    /* this will write one line to stderr for every symbol not found */
641
642    i = 0;
643    while (nlst->n_name != NULL)
644    {
645	if (nlst->n_type == 0)
646	{
647	    /* this one wasn't found */
648	    (void) fprintf(stderr, "kernel: no symbol named `%s'\n",
649			   nlst->n_name);
650	    i = 1;
651	}
652	nlst++;
653    }
654
655    return(i);
656}
657
658
659/*
660 *  getkval(offset, ptr, size, refstr) - get a value out of the kernel.
661 *	"offset" is the byte offset into the kernel for the desired value,
662 *  	"ptr" points to a buffer into which the value is retrieved,
663 *  	"size" is the size of the buffer (and the object to retrieve),
664 *  	"refstr" is a reference string used when printing error meessages,
665 *	    if "refstr" starts with a '!', then a failure on read will not
666 *  	    be fatal (this may seem like a silly way to do things, but I
667 *  	    really didn't want the overhead of another argument).
668 *
669 */
670
671static int getkval(offset, ptr, size, refstr)
672
673unsigned long offset;
674int *ptr;
675int size;
676char *refstr;
677
678{
679    if (kvm_read(kd, offset, (char *) ptr, size) != size)
680    {
681	if (*refstr == '!')
682	{
683	    return(0);
684	}
685	else
686	{
687	    fprintf(stderr, "top: kvm_read for %s: %s\n",
688		refstr, strerror(errno));
689	    quit(23);
690	}
691    }
692    return(1);
693}
694
695/* comparison routine for qsort */
696
697/*
698 *  proc_compare - comparison function for "qsort"
699 *	Compares the resource consumption of two processes using five
700 *  	distinct keys.  The keys (in descending order of importance) are:
701 *  	percent cpu, cpu ticks, state, resident set size, total virtual
702 *  	memory usage.  The process states are ordered as follows (from least
703 *  	to most important):  WAIT, zombie, sleep, stop, start, run.  The
704 *  	array declaration below maps a process state index into a number
705 *  	that reflects this ordering.
706 */
707
708static unsigned char sorted_state[] =
709{
710    0,	/* not used		*/
711    3,	/* sleep		*/
712    1,	/* ABANDONED (WAIT)	*/
713    6,	/* run			*/
714    5,	/* start		*/
715    2,	/* zombie		*/
716    4	/* stop			*/
717};
718
719int
720proc_compare(pp1, pp2)
721
722struct proc **pp1;
723struct proc **pp2;
724
725{
726    register struct kinfo_proc *p1;
727    register struct kinfo_proc *p2;
728    register int result;
729    register pctcpu lresult;
730
731    /* remove one level of indirection */
732    p1 = *(struct kinfo_proc **) pp1;
733    p2 = *(struct kinfo_proc **) pp2;
734
735    /* compare percent cpu (pctcpu) */
736    if ((lresult = PP(p2, p_pctcpu) - PP(p1, p_pctcpu)) == 0)
737    {
738	/* use lifetime CPU usage to break the tie */
739	if ((result = PP(p2, p_runtime) - PP(p1, p_runtime)) == 0)
740	{
741	    /* use process state to break the tie */
742	    if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] -
743			  sorted_state[(unsigned char) PP(p1, p_stat)])  == 0)
744	    {
745		/* use priority to break the tie */
746		if ((result = PP(p2, p_priority) - PP(p1, p_priority)) == 0)
747		{
748		    /* use resident set size (rssize) to break the tie */
749		    if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0)
750		    {
751			/* use total memory to break the tie */
752			result = PROCSIZE(p2) - PROCSIZE(p1);
753		    }
754		}
755	    }
756	}
757    }
758    else
759    {
760	result = lresult < 0 ? -1 : 1;
761    }
762
763    return(result);
764}
765
766
767/*
768 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
769 *		the process does not exist.
770 *		It is EXTREMLY IMPORTANT that this function work correctly.
771 *		If top runs setuid root (as in SVR4), then this function
772 *		is the only thing that stands in the way of a serious
773 *		security problem.  It validates requests for the "kill"
774 *		and "renice" commands.
775 */
776
777int proc_owner(pid)
778
779int pid;
780
781{
782    register int cnt;
783    register struct kinfo_proc **prefp;
784    register struct kinfo_proc *pp;
785
786    prefp = pref;
787    cnt = pref_len;
788    while (--cnt >= 0)
789    {
790	pp = *prefp++;
791	if (PP(pp, p_pid) == (pid_t)pid)
792	{
793	    return((int)EP(pp, e_pcred.p_ruid));
794	}
795    }
796    return(-1);
797}
798
799
800/*
801 * swapmode is based on a program called swapinfo written
802 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
803 */
804
805#define	SVAR(var) __STRING(var)	/* to force expansion */
806#define	KGET(idx, var)							\
807	KGET1(idx, &var, sizeof(var), SVAR(var))
808#define	KGET1(idx, p, s, msg)						\
809	KGET2(nlst[idx].n_value, p, s, msg)
810#define	KGET2(addr, p, s, msg)						\
811	if (kvm_read(kd, (u_long)(addr), p, s) != s) {		        \
812		warnx("cannot read %s: %s", msg, kvm_geterr(kd));       \
813		return (0);                                             \
814       }
815#define	KGETRET(addr, p, s, msg)					\
816	if (kvm_read(kd, (u_long)(addr), p, s) != s) {			\
817		warnx("cannot read %s: %s", msg, kvm_geterr(kd));	\
818		return (0);						\
819	}
820
821
822int
823swapmode(retavail, retfree)
824	int *retavail;
825	int *retfree;
826{
827	char *header;
828	int hlen, nswap, nswdev, dmmax;
829	int i, div, avail, nfree, npfree, used;
830	struct swdevt *sw;
831	long blocksize, *perdev;
832	u_long ptr;
833	struct rlist head;
834#if __FreeBSD_version >= 220000
835	struct rlisthdr swaplist;
836#else
837	struct rlist *swaplist;
838#endif
839	struct rlist *swapptr;
840
841	/*
842	 * Counter for error messages. If we reach the limit,
843	 * stop reading information from swap devices and
844	 * return zero. This prevent endless 'bad address'
845	 * messages.
846	 */
847	static warning = 10;
848
849	if (warning <= 0) {
850	    /* a single warning */
851	    if (!warning) {
852		warning--;
853		fprintf(stderr,
854			"Too much errors, stop reading swap devices ...\n");
855		(void)sleep(3);
856	    }
857	    return(0);
858	}
859	warning--; /* decrease counter, see end of function */
860
861	KGET(VM_NSWAP, nswap);
862	if (!nswap) {
863		fprintf(stderr, "No swap space available\n");
864		return(0);
865	}
866
867	KGET(VM_NSWDEV, nswdev);
868	KGET(VM_DMMAX, dmmax);
869	KGET1(VM_SWAPLIST, &swaplist, sizeof(swaplist), "swaplist");
870	if ((sw = (struct swdevt *)malloc(nswdev * sizeof(*sw))) == NULL ||
871	    (perdev = (long *)malloc(nswdev * sizeof(*perdev))) == NULL)
872		err(1, "malloc");
873	KGET1(VM_SWDEVT, &ptr, sizeof ptr, "swdevt");
874	KGET2(ptr, sw, nswdev * sizeof(*sw), "*swdevt");
875
876	/* Count up swap space. */
877	nfree = 0;
878	memset(perdev, 0, nswdev * sizeof(*perdev));
879#if  __FreeBSD_version >= 220000
880	swapptr = swaplist.rlh_list;
881	while (swapptr) {
882#else
883	while (swaplist) {
884#endif
885		int	top, bottom, next_block;
886#if  __FreeBSD_version >= 220000
887		KGET2(swapptr, &head, sizeof(struct rlist), "swapptr");
888#else
889		KGET2(swaplist, &head, sizeof(struct rlist), "swaplist");
890#endif
891
892		top = head.rl_end;
893		bottom = head.rl_start;
894
895		nfree += top - bottom + 1;
896
897		/*
898		 * Swap space is split up among the configured disks.
899		 *
900		 * For interleaved swap devices, the first dmmax blocks
901		 * of swap space some from the first disk, the next dmmax
902		 * blocks from the next, and so on up to nswap blocks.
903		 *
904		 * The list of free space joins adjacent free blocks,
905		 * ignoring device boundries.  If we want to keep track
906		 * of this information per device, we'll just have to
907		 * extract it ourselves.
908		 */
909		while (top / dmmax != bottom / dmmax) {
910			next_block = ((bottom + dmmax) / dmmax);
911			perdev[(bottom / dmmax) % nswdev] +=
912				next_block * dmmax - bottom;
913			bottom = next_block * dmmax;
914		}
915		perdev[(bottom / dmmax) % nswdev] +=
916			top - bottom + 1;
917
918#if  __FreeBSD_version >= 220000
919		swapptr = head.rl_next;
920#else
921		swaplist = head.rl_next;
922#endif
923	}
924
925	header = getbsize(&hlen, &blocksize);
926	div = blocksize / 512;
927	avail = npfree = 0;
928	for (i = 0; i < nswdev; i++) {
929		int xsize, xfree;
930
931		/*
932		 * Don't report statistics for partitions which have not
933		 * yet been activated via swapon(8).
934		 */
935
936		xsize = sw[i].sw_nblks;
937		xfree = perdev[i];
938		used = xsize - xfree;
939		npfree++;
940		avail += xsize;
941	}
942
943	/*
944	 * If only one partition has been set up via swapon(8), we don't
945	 * need to bother with totals.
946	 */
947	*retavail = avail / 2;
948	*retfree = nfree / 2;
949	used = avail - nfree;
950	free(sw); free(perdev);
951
952	/* increase counter, no errors occurs */
953	warning++;
954
955	return  (int)(((double)used / (double)avail * 100.0) + 0.5);
956}
957