machine.c revision 37885
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.11 1998/06/21 18:00:34 dt 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    /* This does not count interrupts */
569    cputime = (PP(pp, p_runtime) / 1000 + 500) / 1000;
570
571    /* calculate the base for cpu percentages */
572    pct = pctdouble(PP(pp, p_pctcpu));
573
574    /* generate "STATE" field */
575    switch (PP(pp, p_stat)) {
576	case SRUN:
577	    if (smpmode && PP(pp, p_oncpu) >= 0)
578		sprintf(status, "CPU%d", PP(pp, p_oncpu));
579	    else
580		strcpy(status, "RUN");
581	    break;
582	case SSLEEP:
583	    if (PP(pp, p_wmesg) != NULL) {
584		sprintf(status, "%.6s", EP(pp, e_wmesg));
585		break;
586	    }
587	    /* fall through */
588	default:
589	    sprintf(status, "%.6s", state_abbrev[(unsigned char) PP(pp, p_stat)]);
590	    break;
591    }
592
593    /* format this entry */
594    sprintf(fmt,
595	    smpmode ? smp_Proc_format : up_Proc_format,
596	    PP(pp, p_pid),
597	    namelength, namelength,
598	    (*get_userid)(EP(pp, e_pcred.p_ruid)),
599	    PP(pp, p_priority) - PZERO,
600
601	    /*
602	     * normal time      -> nice value -20 - +20
603	     * real time 0 - 31 -> nice value -52 - -21
604	     * idle time 0 - 31 -> nice value +21 - +52
605	     */
606	    (PP(pp, p_rtprio.type) ==  RTP_PRIO_NORMAL ?
607	    	PP(pp, p_nice) - NZERO :
608	    	(PP(pp, p_rtprio.type) ==  RTP_PRIO_REALTIME ?
609		    (PRIO_MIN - 1 - RTP_PRIO_MAX + PP(pp, p_rtprio.prio)) :
610		    (PRIO_MAX + 1 + PP(pp, p_rtprio.prio)))),
611	    format_k2(PROCSIZE(pp)),
612	    format_k2(pagetok(VP(pp, vm_rssize))),
613	    status,
614	    smpmode ? PP(pp, p_lastcpu) : 0,
615	    format_time(cputime),
616	    10000.0 * weighted_cpu(pct, pp) / hz,
617	    10000.0 * pct / hz,
618	    cmdlength,
619	    printable(PP(pp, p_comm)));
620
621    /* return the result */
622    return(fmt);
623}
624
625
626/*
627 * check_nlist(nlst) - checks the nlist to see if any symbols were not
628 *		found.  For every symbol that was not found, a one-line
629 *		message is printed to stderr.  The routine returns the
630 *		number of symbols NOT found.
631 */
632
633static int check_nlist(nlst)
634
635register struct nlist *nlst;
636
637{
638    register int i;
639
640    /* check to see if we got ALL the symbols we requested */
641    /* this will write one line to stderr for every symbol not found */
642
643    i = 0;
644    while (nlst->n_name != NULL)
645    {
646	if (nlst->n_type == 0)
647	{
648	    /* this one wasn't found */
649	    (void) fprintf(stderr, "kernel: no symbol named `%s'\n",
650			   nlst->n_name);
651	    i = 1;
652	}
653	nlst++;
654    }
655
656    return(i);
657}
658
659
660/*
661 *  getkval(offset, ptr, size, refstr) - get a value out of the kernel.
662 *	"offset" is the byte offset into the kernel for the desired value,
663 *  	"ptr" points to a buffer into which the value is retrieved,
664 *  	"size" is the size of the buffer (and the object to retrieve),
665 *  	"refstr" is a reference string used when printing error meessages,
666 *	    if "refstr" starts with a '!', then a failure on read will not
667 *  	    be fatal (this may seem like a silly way to do things, but I
668 *  	    really didn't want the overhead of another argument).
669 *
670 */
671
672static int getkval(offset, ptr, size, refstr)
673
674unsigned long offset;
675int *ptr;
676int size;
677char *refstr;
678
679{
680    if (kvm_read(kd, offset, (char *) ptr, size) != size)
681    {
682	if (*refstr == '!')
683	{
684	    return(0);
685	}
686	else
687	{
688	    fprintf(stderr, "top: kvm_read for %s: %s\n",
689		refstr, strerror(errno));
690	    quit(23);
691	}
692    }
693    return(1);
694}
695
696/* comparison routine for qsort */
697
698/*
699 *  proc_compare - comparison function for "qsort"
700 *	Compares the resource consumption of two processes using five
701 *  	distinct keys.  The keys (in descending order of importance) are:
702 *  	percent cpu, cpu ticks, state, resident set size, total virtual
703 *  	memory usage.  The process states are ordered as follows (from least
704 *  	to most important):  WAIT, zombie, sleep, stop, start, run.  The
705 *  	array declaration below maps a process state index into a number
706 *  	that reflects this ordering.
707 */
708
709static unsigned char sorted_state[] =
710{
711    0,	/* not used		*/
712    3,	/* sleep		*/
713    1,	/* ABANDONED (WAIT)	*/
714    6,	/* run			*/
715    5,	/* start		*/
716    2,	/* zombie		*/
717    4	/* stop			*/
718};
719
720int
721proc_compare(pp1, pp2)
722
723struct proc **pp1;
724struct proc **pp2;
725
726{
727    register struct kinfo_proc *p1;
728    register struct kinfo_proc *p2;
729    register int result;
730    register pctcpu lresult;
731
732    /* remove one level of indirection */
733    p1 = *(struct kinfo_proc **) pp1;
734    p2 = *(struct kinfo_proc **) pp2;
735
736    /* compare percent cpu (pctcpu) */
737    if ((lresult = PP(p2, p_pctcpu) - PP(p1, p_pctcpu)) == 0)
738    {
739	/* use lifetime CPU usage to break the tie */
740	if ((result = PP(p2, p_runtime) - PP(p1, p_runtime)) == 0)
741	{
742	    /* use process state to break the tie */
743	    if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] -
744			  sorted_state[(unsigned char) PP(p1, p_stat)])  == 0)
745	    {
746		/* use priority to break the tie */
747		if ((result = PP(p2, p_priority) - PP(p1, p_priority)) == 0)
748		{
749		    /* use resident set size (rssize) to break the tie */
750		    if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0)
751		    {
752			/* use total memory to break the tie */
753			result = PROCSIZE(p2) - PROCSIZE(p1);
754		    }
755		}
756	    }
757	}
758    }
759    else
760    {
761	result = lresult < 0 ? -1 : 1;
762    }
763
764    return(result);
765}
766
767
768/*
769 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
770 *		the process does not exist.
771 *		It is EXTREMLY IMPORTANT that this function work correctly.
772 *		If top runs setuid root (as in SVR4), then this function
773 *		is the only thing that stands in the way of a serious
774 *		security problem.  It validates requests for the "kill"
775 *		and "renice" commands.
776 */
777
778int proc_owner(pid)
779
780int pid;
781
782{
783    register int cnt;
784    register struct kinfo_proc **prefp;
785    register struct kinfo_proc *pp;
786
787    prefp = pref;
788    cnt = pref_len;
789    while (--cnt >= 0)
790    {
791	pp = *prefp++;
792	if (PP(pp, p_pid) == (pid_t)pid)
793	{
794	    return((int)EP(pp, e_pcred.p_ruid));
795	}
796    }
797    return(-1);
798}
799
800
801/*
802 * swapmode is based on a program called swapinfo written
803 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
804 */
805
806#define	SVAR(var) __STRING(var)	/* to force expansion */
807#define	KGET(idx, var)							\
808	KGET1(idx, &var, sizeof(var), SVAR(var))
809#define	KGET1(idx, p, s, msg)						\
810	KGET2(nlst[idx].n_value, p, s, msg)
811#define	KGET2(addr, p, s, msg)						\
812	if (kvm_read(kd, (u_long)(addr), p, s) != s) {		        \
813		warnx("cannot read %s: %s", msg, kvm_geterr(kd));       \
814		return (0);                                             \
815       }
816#define	KGETRET(addr, p, s, msg)					\
817	if (kvm_read(kd, (u_long)(addr), p, s) != s) {			\
818		warnx("cannot read %s: %s", msg, kvm_geterr(kd));	\
819		return (0);						\
820	}
821
822
823int
824swapmode(retavail, retfree)
825	int *retavail;
826	int *retfree;
827{
828	char *header;
829	int hlen, nswap, nswdev, dmmax;
830	int i, div, avail, nfree, npfree, used;
831	struct swdevt *sw;
832	long blocksize, *perdev;
833	u_long ptr;
834	struct rlist head;
835#if __FreeBSD_version >= 220000
836	struct rlisthdr swaplist;
837#else
838	struct rlist *swaplist;
839#endif
840	struct rlist *swapptr;
841
842	/*
843	 * Counter for error messages. If we reach the limit,
844	 * stop reading information from swap devices and
845	 * return zero. This prevent endless 'bad address'
846	 * messages.
847	 */
848	static warning = 10;
849
850	if (warning <= 0) {
851	    /* a single warning */
852	    if (!warning) {
853		warning--;
854		fprintf(stderr,
855			"Too much errors, stop reading swap devices ...\n");
856		(void)sleep(3);
857	    }
858	    return(0);
859	}
860	warning--; /* decrease counter, see end of function */
861
862	KGET(VM_NSWAP, nswap);
863	if (!nswap) {
864		fprintf(stderr, "No swap space available\n");
865		return(0);
866	}
867
868	KGET(VM_NSWDEV, nswdev);
869	KGET(VM_DMMAX, dmmax);
870	KGET1(VM_SWAPLIST, &swaplist, sizeof(swaplist), "swaplist");
871	if ((sw = (struct swdevt *)malloc(nswdev * sizeof(*sw))) == NULL ||
872	    (perdev = (long *)malloc(nswdev * sizeof(*perdev))) == NULL)
873		err(1, "malloc");
874	KGET1(VM_SWDEVT, &ptr, sizeof ptr, "swdevt");
875	KGET2(ptr, sw, nswdev * sizeof(*sw), "*swdevt");
876
877	/* Count up swap space. */
878	nfree = 0;
879	memset(perdev, 0, nswdev * sizeof(*perdev));
880#if  __FreeBSD_version >= 220000
881	swapptr = swaplist.rlh_list;
882	while (swapptr) {
883#else
884	while (swaplist) {
885#endif
886		int	top, bottom, next_block;
887#if  __FreeBSD_version >= 220000
888		KGET2(swapptr, &head, sizeof(struct rlist), "swapptr");
889#else
890		KGET2(swaplist, &head, sizeof(struct rlist), "swaplist");
891#endif
892
893		top = head.rl_end;
894		bottom = head.rl_start;
895
896		nfree += top - bottom + 1;
897
898		/*
899		 * Swap space is split up among the configured disks.
900		 *
901		 * For interleaved swap devices, the first dmmax blocks
902		 * of swap space some from the first disk, the next dmmax
903		 * blocks from the next, and so on up to nswap blocks.
904		 *
905		 * The list of free space joins adjacent free blocks,
906		 * ignoring device boundries.  If we want to keep track
907		 * of this information per device, we'll just have to
908		 * extract it ourselves.
909		 */
910		while (top / dmmax != bottom / dmmax) {
911			next_block = ((bottom + dmmax) / dmmax);
912			perdev[(bottom / dmmax) % nswdev] +=
913				next_block * dmmax - bottom;
914			bottom = next_block * dmmax;
915		}
916		perdev[(bottom / dmmax) % nswdev] +=
917			top - bottom + 1;
918
919#if  __FreeBSD_version >= 220000
920		swapptr = head.rl_next;
921#else
922		swaplist = head.rl_next;
923#endif
924	}
925
926	header = getbsize(&hlen, &blocksize);
927	div = blocksize / 512;
928	avail = npfree = 0;
929	for (i = 0; i < nswdev; i++) {
930		int xsize, xfree;
931
932		/*
933		 * Don't report statistics for partitions which have not
934		 * yet been activated via swapon(8).
935		 */
936
937		xsize = sw[i].sw_nblks;
938		xfree = perdev[i];
939		used = xsize - xfree;
940		npfree++;
941		avail += xsize;
942	}
943
944	/*
945	 * If only one partition has been set up via swapon(8), we don't
946	 * need to bother with totals.
947	 */
948	*retavail = avail / 2;
949	*retfree = nfree / 2;
950	used = avail - nfree;
951	free(sw); free(perdev);
952
953	/* increase counter, no errors occurs */
954	warning++;
955
956	return  (int)(((double)used / (double)avail * 100.0) + 0.5);
957}
958