Home | History | Annotate | Line # | Download | only in kern
kern_sysctl.c revision 1.13
      1 /*	$NetBSD: kern_sysctl.c,v 1.13 1996/02/04 02:16:22 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Mike Karels at Berkeley Software Design, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
     39  */
     40 
     41 /*
     42  * sysctl system call.
     43  */
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/malloc.h>
     49 #include <sys/proc.h>
     50 #include <sys/file.h>
     51 #include <sys/vnode.h>
     52 #include <sys/unistd.h>
     53 #include <sys/buf.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/tty.h>
     56 #include <sys/disklabel.h>
     57 #include <vm/vm.h>
     58 #include <sys/sysctl.h>
     59 
     60 #include <sys/mount.h>
     61 #include <sys/syscallargs.h>
     62 
     63 #include <kern/kern_extern.h>
     64 
     65 /*
     66  * Locking and stats
     67  */
     68 static struct sysctl_lock {
     69 	int	sl_lock;
     70 	int	sl_want;
     71 	int	sl_locked;
     72 } memlock;
     73 
     74 int
     75 sys___sysctl(p, v, retval)
     76 	struct proc *p;
     77 	void *v;
     78 	register_t *retval;
     79 {
     80 	register struct sys___sysctl_args /* {
     81 		syscallarg(int *) name;
     82 		syscallarg(u_int) namelen;
     83 		syscallarg(void *) old;
     84 		syscallarg(size_t *) oldlenp;
     85 		syscallarg(void *) new;
     86 		syscallarg(size_t) newlen;
     87 	} */ *uap = v;
     88 	int error, dolock = 1;
     89 	size_t savelen = 0, oldlen = 0;
     90 	sysctlfn *fn;
     91 	int name[CTL_MAXNAME];
     92 
     93 	if (SCARG(uap, new) != NULL &&
     94 	    (error = suser(p->p_ucred, &p->p_acflag)))
     95 		return (error);
     96 	/*
     97 	 * all top-level sysctl names are non-terminal
     98 	 */
     99 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
    100 		return (EINVAL);
    101 	error = copyin(SCARG(uap, name), &name,
    102 		       SCARG(uap, namelen) * sizeof(int));
    103 	if (error)
    104 		return (error);
    105 
    106 	switch (name[0]) {
    107 	case CTL_KERN:
    108 		fn = kern_sysctl;
    109 		if (name[2] != KERN_VNODE)	/* XXX */
    110 			dolock = 0;
    111 		break;
    112 	case CTL_HW:
    113 		fn = hw_sysctl;
    114 		break;
    115 	case CTL_VM:
    116 		fn = vm_sysctl;
    117 		break;
    118 	case CTL_NET:
    119 		fn = net_sysctl;
    120 		break;
    121 #ifdef notyet
    122 	case CTL_FS:
    123 		fn = fs_sysctl;
    124 		break;
    125 #endif
    126 	case CTL_MACHDEP:
    127 		fn = cpu_sysctl;
    128 		break;
    129 #ifdef DEBUG
    130 	case CTL_DEBUG:
    131 		fn = debug_sysctl;
    132 		break;
    133 #endif
    134 	default:
    135 		return (EOPNOTSUPP);
    136 	}
    137 
    138 	if (SCARG(uap, oldlenp) &&
    139 	    (error = copyin(SCARG(uap, oldlenp), &oldlen, sizeof(oldlen))))
    140 		return (error);
    141 	if (SCARG(uap, old) != NULL) {
    142 		if (!useracc(SCARG(uap, old), oldlen, B_WRITE))
    143 			return (EFAULT);
    144 		while (memlock.sl_lock) {
    145 			memlock.sl_want = 1;
    146 			sleep((caddr_t)&memlock, PRIBIO+1);
    147 			memlock.sl_locked++;
    148 		}
    149 		memlock.sl_lock = 1;
    150 		if (dolock)
    151 			vslock(SCARG(uap, old), oldlen);
    152 		savelen = oldlen;
    153 	}
    154 	error = (*fn)(name + 1, SCARG(uap, namelen) - 1, SCARG(uap, old),
    155 	    &oldlen, SCARG(uap, new), SCARG(uap, newlen), p);
    156 	if (SCARG(uap, old) != NULL) {
    157 		if (dolock)
    158 			vsunlock(SCARG(uap, old), savelen);
    159 		memlock.sl_lock = 0;
    160 		if (memlock.sl_want) {
    161 			memlock.sl_want = 0;
    162 			wakeup((caddr_t)&memlock);
    163 		}
    164 	}
    165 	if (error)
    166 		return (error);
    167 	if (SCARG(uap, oldlenp))
    168 		error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen));
    169 	*retval = oldlen;
    170 	return (0);
    171 }
    172 
    173 /*
    174  * Attributes stored in the kernel.
    175  */
    176 char hostname[MAXHOSTNAMELEN];
    177 int hostnamelen;
    178 char domainname[MAXHOSTNAMELEN];
    179 int domainnamelen;
    180 long hostid;
    181 #ifdef INSECURE
    182 int securelevel = -1;
    183 #else
    184 int securelevel;
    185 #endif
    186 
    187 /*
    188  * kernel related system variables.
    189  */
    190 int
    191 kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    192 	int *name;
    193 	u_int namelen;
    194 	void *oldp;
    195 	size_t *oldlenp;
    196 	void *newp;
    197 	size_t newlen;
    198 	struct proc *p;
    199 {
    200 	int error, level, inthostid;
    201 	extern char ostype[], osrelease[], version[];
    202 
    203 	/* all sysctl names at this level are terminal */
    204 	if (namelen != 1 && !(name[0] == KERN_PROC || name[0] == KERN_PROF))
    205 		return (ENOTDIR);		/* overloaded */
    206 
    207 	switch (name[0]) {
    208 	case KERN_OSTYPE:
    209 		return (sysctl_rdstring(oldp, oldlenp, newp, ostype));
    210 	case KERN_OSRELEASE:
    211 		return (sysctl_rdstring(oldp, oldlenp, newp, osrelease));
    212 	case KERN_OSREV:
    213 		return (sysctl_rdint(oldp, oldlenp, newp, BSD));
    214 	case KERN_VERSION:
    215 		return (sysctl_rdstring(oldp, oldlenp, newp, version));
    216 	case KERN_MAXVNODES:
    217 		return(sysctl_int(oldp, oldlenp, newp, newlen, &desiredvnodes));
    218 	case KERN_MAXPROC:
    219 		return (sysctl_int(oldp, oldlenp, newp, newlen, &maxproc));
    220 	case KERN_MAXFILES:
    221 		return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles));
    222 	case KERN_ARGMAX:
    223 		return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX));
    224 	case KERN_SECURELVL:
    225 		level = securelevel;
    226 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) ||
    227 		    newp == NULL)
    228 			return (error);
    229 		if (level < securelevel && p->p_pid != 1)
    230 			return (EPERM);
    231 		securelevel = level;
    232 		return (0);
    233 	case KERN_HOSTNAME:
    234 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    235 		    hostname, sizeof(hostname));
    236 		if (newp && !error)
    237 			hostnamelen = newlen;
    238 		return (error);
    239 	case KERN_DOMAINNAME:
    240 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    241 		    domainname, sizeof(domainname));
    242 		if (newp && !error)
    243 			domainnamelen = newlen;
    244 		return (error);
    245 	case KERN_HOSTID:
    246 		inthostid = hostid;  /* XXX assumes sizeof long <= sizeof int */
    247 		error =  sysctl_int(oldp, oldlenp, newp, newlen, &inthostid);
    248 		hostid = inthostid;
    249 		return (error);
    250 	case KERN_CLOCKRATE:
    251 		return (sysctl_clockrate(oldp, oldlenp));
    252 	case KERN_BOOTTIME:
    253 		return (sysctl_rdstruct(oldp, oldlenp, newp, &boottime,
    254 		    sizeof(struct timeval)));
    255 	case KERN_VNODE:
    256 		return (sysctl_vnode(oldp, oldlenp));
    257 	case KERN_PROC:
    258 		return (sysctl_doproc(name + 1, namelen - 1, oldp, oldlenp));
    259 	case KERN_FILE:
    260 		return (sysctl_file(oldp, oldlenp));
    261 #ifdef GPROF
    262 	case KERN_PROF:
    263 		return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
    264 		    newp, newlen));
    265 #endif
    266 	case KERN_POSIX1:
    267 		return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION));
    268 	case KERN_NGROUPS:
    269 		return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX));
    270 	case KERN_JOB_CONTROL:
    271 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    272 	case KERN_SAVED_IDS:
    273 #ifdef _POSIX_SAVED_IDS
    274 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    275 #else
    276 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    277 #endif
    278 	case KERN_MAXPARTITIONS:
    279 		return (sysctl_rdint(oldp, oldlenp, newp, MAXPARTITIONS));
    280 	case KERN_RAWPARTITION:
    281 		return (sysctl_rdint(oldp, oldlenp, newp, RAW_PART));
    282 	default:
    283 		return (EOPNOTSUPP);
    284 	}
    285 	/* NOTREACHED */
    286 }
    287 
    288 /*
    289  * hardware related system variables.
    290  */
    291 int
    292 hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    293 	int *name;
    294 	u_int namelen;
    295 	void *oldp;
    296 	size_t *oldlenp;
    297 	void *newp;
    298 	size_t newlen;
    299 	struct proc *p;
    300 {
    301 	extern char machine[], cpu_model[];
    302 
    303 	/* all sysctl names at this level are terminal */
    304 	if (namelen != 1)
    305 		return (ENOTDIR);		/* overloaded */
    306 
    307 	switch (name[0]) {
    308 	case HW_MACHINE:
    309 		return (sysctl_rdstring(oldp, oldlenp, newp, machine));
    310 	case HW_MODEL:
    311 		return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
    312 	case HW_NCPU:
    313 		return (sysctl_rdint(oldp, oldlenp, newp, 1));	/* XXX */
    314 	case HW_BYTEORDER:
    315 		return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
    316 	case HW_PHYSMEM:
    317 		return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
    318 	case HW_USERMEM:
    319 		return (sysctl_rdint(oldp, oldlenp, newp,
    320 		    ctob(physmem - cnt.v_wire_count)));
    321 	case HW_PAGESIZE:
    322 		return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
    323 	default:
    324 		return (EOPNOTSUPP);
    325 	}
    326 	/* NOTREACHED */
    327 }
    328 
    329 #ifdef DEBUG
    330 /*
    331  * Debugging related system variables.
    332  */
    333 struct ctldebug debug0, debug1, debug2, debug3, debug4;
    334 struct ctldebug debug5, debug6, debug7, debug8, debug9;
    335 struct ctldebug debug10, debug11, debug12, debug13, debug14;
    336 struct ctldebug debug15, debug16, debug17, debug18, debug19;
    337 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
    338 	&debug0, &debug1, &debug2, &debug3, &debug4,
    339 	&debug5, &debug6, &debug7, &debug8, &debug9,
    340 	&debug10, &debug11, &debug12, &debug13, &debug14,
    341 	&debug15, &debug16, &debug17, &debug18, &debug19,
    342 };
    343 int
    344 debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    345 	int *name;
    346 	u_int namelen;
    347 	void *oldp;
    348 	size_t *oldlenp;
    349 	void *newp;
    350 	size_t newlen;
    351 	struct proc *p;
    352 {
    353 	struct ctldebug *cdp;
    354 
    355 	/* all sysctl names at this level are name and field */
    356 	if (namelen != 2)
    357 		return (ENOTDIR);		/* overloaded */
    358 	cdp = debugvars[name[0]];
    359 	if (cdp->debugname == 0)
    360 		return (EOPNOTSUPP);
    361 	switch (name[1]) {
    362 	case CTL_DEBUG_NAME:
    363 		return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
    364 	case CTL_DEBUG_VALUE:
    365 		return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
    366 	default:
    367 		return (EOPNOTSUPP);
    368 	}
    369 	/* NOTREACHED */
    370 }
    371 #endif /* DEBUG */
    372 
    373 /*
    374  * Validate parameters and get old / set new parameters
    375  * for an integer-valued sysctl function.
    376  */
    377 int
    378 sysctl_int(oldp, oldlenp, newp, newlen, valp)
    379 	void *oldp;
    380 	size_t *oldlenp;
    381 	void *newp;
    382 	size_t newlen;
    383 	int *valp;
    384 {
    385 	int error = 0;
    386 
    387 	if (oldp && *oldlenp < sizeof(int))
    388 		return (ENOMEM);
    389 	if (newp && newlen != sizeof(int))
    390 		return (EINVAL);
    391 	*oldlenp = sizeof(int);
    392 	if (oldp)
    393 		error = copyout(valp, oldp, sizeof(int));
    394 	if (error == 0 && newp)
    395 		error = copyin(newp, valp, sizeof(int));
    396 	return (error);
    397 }
    398 
    399 /*
    400  * As above, but read-only.
    401  */
    402 int
    403 sysctl_rdint(oldp, oldlenp, newp, val)
    404 	void *oldp;
    405 	size_t *oldlenp;
    406 	void *newp;
    407 	int val;
    408 {
    409 	int error = 0;
    410 
    411 	if (oldp && *oldlenp < sizeof(int))
    412 		return (ENOMEM);
    413 	if (newp)
    414 		return (EPERM);
    415 	*oldlenp = sizeof(int);
    416 	if (oldp)
    417 		error = copyout((caddr_t)&val, oldp, sizeof(int));
    418 	return (error);
    419 }
    420 
    421 /*
    422  * Validate parameters and get old / set new parameters
    423  * for a string-valued sysctl function.
    424  */
    425 int
    426 sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen)
    427 	void *oldp;
    428 	size_t *oldlenp;
    429 	void *newp;
    430 	size_t newlen;
    431 	char *str;
    432 	int maxlen;
    433 {
    434 	int len, error = 0;
    435 
    436 	len = strlen(str) + 1;
    437 	if (oldp && *oldlenp < len)
    438 		return (ENOMEM);
    439 	if (newp && newlen >= maxlen)
    440 		return (EINVAL);
    441 	if (oldp) {
    442 		*oldlenp = len;
    443 		error = copyout(str, oldp, len);
    444 	}
    445 	if (error == 0 && newp) {
    446 		error = copyin(newp, str, newlen);
    447 		str[newlen] = 0;
    448 	}
    449 	return (error);
    450 }
    451 
    452 /*
    453  * As above, but read-only.
    454  */
    455 int
    456 sysctl_rdstring(oldp, oldlenp, newp, str)
    457 	void *oldp;
    458 	size_t *oldlenp;
    459 	void *newp;
    460 	char *str;
    461 {
    462 	int len, error = 0;
    463 
    464 	len = strlen(str) + 1;
    465 	if (oldp && *oldlenp < len)
    466 		return (ENOMEM);
    467 	if (newp)
    468 		return (EPERM);
    469 	*oldlenp = len;
    470 	if (oldp)
    471 		error = copyout(str, oldp, len);
    472 	return (error);
    473 }
    474 
    475 /*
    476  * Validate parameters and get old / set new parameters
    477  * for a structure oriented sysctl function.
    478  */
    479 int
    480 sysctl_struct(oldp, oldlenp, newp, newlen, sp, len)
    481 	void *oldp;
    482 	size_t *oldlenp;
    483 	void *newp;
    484 	size_t newlen;
    485 	void *sp;
    486 	int len;
    487 {
    488 	int error = 0;
    489 
    490 	if (oldp && *oldlenp < len)
    491 		return (ENOMEM);
    492 	if (newp && newlen > len)
    493 		return (EINVAL);
    494 	if (oldp) {
    495 		*oldlenp = len;
    496 		error = copyout(sp, oldp, len);
    497 	}
    498 	if (error == 0 && newp)
    499 		error = copyin(newp, sp, len);
    500 	return (error);
    501 }
    502 
    503 /*
    504  * Validate parameters and get old parameters
    505  * for a structure oriented sysctl function.
    506  */
    507 int
    508 sysctl_rdstruct(oldp, oldlenp, newp, sp, len)
    509 	void *oldp;
    510 	size_t *oldlenp;
    511 	void *newp, *sp;
    512 	int len;
    513 {
    514 	int error = 0;
    515 
    516 	if (oldp && *oldlenp < len)
    517 		return (ENOMEM);
    518 	if (newp)
    519 		return (EPERM);
    520 	*oldlenp = len;
    521 	if (oldp)
    522 		error = copyout(sp, oldp, len);
    523 	return (error);
    524 }
    525 
    526 /*
    527  * Get file structures.
    528  */
    529 int
    530 sysctl_file(where, sizep)
    531 	char *where;
    532 	size_t *sizep;
    533 {
    534 	int buflen, error;
    535 	struct file *fp;
    536 	char *start = where;
    537 
    538 	buflen = *sizep;
    539 	if (where == NULL) {
    540 		/*
    541 		 * overestimate by 10 files
    542 		 */
    543 		*sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
    544 		return (0);
    545 	}
    546 
    547 	/*
    548 	 * first copyout filehead
    549 	 */
    550 	if (buflen < sizeof(filehead)) {
    551 		*sizep = 0;
    552 		return (0);
    553 	}
    554 	error = copyout((caddr_t)&filehead, where, sizeof(filehead));
    555 	if (error)
    556 		return (error);
    557 	buflen -= sizeof(filehead);
    558 	where += sizeof(filehead);
    559 
    560 	/*
    561 	 * followed by an array of file structures
    562 	 */
    563 	for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
    564 		if (buflen < sizeof(struct file)) {
    565 			*sizep = where - start;
    566 			return (ENOMEM);
    567 		}
    568 		error = copyout((caddr_t)fp, where, sizeof (struct file));
    569 		if (error)
    570 			return (error);
    571 		buflen -= sizeof(struct file);
    572 		where += sizeof(struct file);
    573 	}
    574 	*sizep = where - start;
    575 	return (0);
    576 }
    577 
    578 /*
    579  * try over estimating by 5 procs
    580  */
    581 #define KERN_PROCSLOP	(5 * sizeof (struct kinfo_proc))
    582 
    583 int
    584 sysctl_doproc(name, namelen, where, sizep)
    585 	int *name;
    586 	u_int namelen;
    587 	char *where;
    588 	size_t *sizep;
    589 {
    590 	register struct proc *p;
    591 	register struct kinfo_proc *dp = (struct kinfo_proc *)where;
    592 	register int needed = 0;
    593 	int buflen = where != NULL ? *sizep : 0;
    594 	int doingzomb;
    595 	struct eproc eproc;
    596 	int error = 0;
    597 
    598 	if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
    599 		return (EINVAL);
    600 	p = allproc.lh_first;
    601 	doingzomb = 0;
    602 again:
    603 	for (; p != 0; p = p->p_list.le_next) {
    604 		/*
    605 		 * Skip embryonic processes.
    606 		 */
    607 		if (p->p_stat == SIDL)
    608 			continue;
    609 		/*
    610 		 * TODO - make more efficient (see notes below).
    611 		 * do by session.
    612 		 */
    613 		switch (name[0]) {
    614 
    615 		case KERN_PROC_PID:
    616 			/* could do this with just a lookup */
    617 			if (p->p_pid != (pid_t)name[1])
    618 				continue;
    619 			break;
    620 
    621 		case KERN_PROC_PGRP:
    622 			/* could do this by traversing pgrp */
    623 			if (p->p_pgrp->pg_id != (pid_t)name[1])
    624 				continue;
    625 			break;
    626 
    627 		case KERN_PROC_TTY:
    628 			if ((p->p_flag & P_CONTROLT) == 0 ||
    629 			    p->p_session->s_ttyp == NULL ||
    630 			    p->p_session->s_ttyp->t_dev != (dev_t)name[1])
    631 				continue;
    632 			break;
    633 
    634 		case KERN_PROC_UID:
    635 			if (p->p_ucred->cr_uid != (uid_t)name[1])
    636 				continue;
    637 			break;
    638 
    639 		case KERN_PROC_RUID:
    640 			if (p->p_cred->p_ruid != (uid_t)name[1])
    641 				continue;
    642 			break;
    643 		}
    644 		if (buflen >= sizeof(struct kinfo_proc)) {
    645 			fill_eproc(p, &eproc);
    646 			error = copyout((caddr_t)p, &dp->kp_proc,
    647 					sizeof(struct proc));
    648 			if (error)
    649 				return (error);
    650 			error = copyout((caddr_t)&eproc, &dp->kp_eproc,
    651 					sizeof(eproc));
    652 			if (error)
    653 				return (error);
    654 			dp++;
    655 			buflen -= sizeof(struct kinfo_proc);
    656 		}
    657 		needed += sizeof(struct kinfo_proc);
    658 	}
    659 	if (doingzomb == 0) {
    660 		p = zombproc.lh_first;
    661 		doingzomb++;
    662 		goto again;
    663 	}
    664 	if (where != NULL) {
    665 		*sizep = (caddr_t)dp - where;
    666 		if (needed > *sizep)
    667 			return (ENOMEM);
    668 	} else {
    669 		needed += KERN_PROCSLOP;
    670 		*sizep = needed;
    671 	}
    672 	return (0);
    673 }
    674 
    675 /*
    676  * Fill in an eproc structure for the specified process.
    677  */
    678 void
    679 fill_eproc(p, ep)
    680 	register struct proc *p;
    681 	register struct eproc *ep;
    682 {
    683 	register struct tty *tp;
    684 
    685 	ep->e_paddr = p;
    686 	ep->e_sess = p->p_pgrp->pg_session;
    687 	ep->e_pcred = *p->p_cred;
    688 	ep->e_ucred = *p->p_ucred;
    689 	if (p->p_stat == SIDL || p->p_stat == SZOMB) {
    690 		ep->e_vm.vm_rssize = 0;
    691 		ep->e_vm.vm_tsize = 0;
    692 		ep->e_vm.vm_dsize = 0;
    693 		ep->e_vm.vm_ssize = 0;
    694 		/* ep->e_vm.vm_pmap = XXX; */
    695 	} else {
    696 		register struct vmspace *vm = p->p_vmspace;
    697 
    698 #ifdef pmap_resident_count
    699 		ep->e_vm.vm_rssize = pmap_resident_count(&vm->vm_pmap); /*XXX*/
    700 #else
    701 		ep->e_vm.vm_rssize = vm->vm_rssize;
    702 #endif
    703 		ep->e_vm.vm_tsize = vm->vm_tsize;
    704 		ep->e_vm.vm_dsize = vm->vm_dsize;
    705 		ep->e_vm.vm_ssize = vm->vm_ssize;
    706 		ep->e_vm.vm_pmap = vm->vm_pmap;
    707 	}
    708 	if (p->p_pptr)
    709 		ep->e_ppid = p->p_pptr->p_pid;
    710 	else
    711 		ep->e_ppid = 0;
    712 	ep->e_pgid = p->p_pgrp->pg_id;
    713 	ep->e_jobc = p->p_pgrp->pg_jobc;
    714 	if ((p->p_flag & P_CONTROLT) &&
    715 	     (tp = ep->e_sess->s_ttyp)) {
    716 		ep->e_tdev = tp->t_dev;
    717 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
    718 		ep->e_tsess = tp->t_session;
    719 	} else
    720 		ep->e_tdev = NODEV;
    721 	ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
    722 	if (SESS_LEADER(p))
    723 		ep->e_flag |= EPROC_SLEADER;
    724 	if (p->p_wmesg)
    725 		strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
    726 	ep->e_xsize = ep->e_xrssize = 0;
    727 	ep->e_xccount = ep->e_xswrss = 0;
    728 }
    729 
    730