Home | History | Annotate | Line # | Download | only in kern
kern_sysctl.c revision 1.12
      1 /*	$NetBSD: kern_sysctl.c,v 1.12 1995/10/07 06:28:27 mycroft 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 sysctlfn kern_sysctl;
     64 sysctlfn hw_sysctl;
     65 #ifdef DEBUG
     66 sysctlfn debug_sysctl;
     67 #endif
     68 extern sysctlfn vm_sysctl;
     69 extern sysctlfn fs_sysctl;
     70 extern sysctlfn net_sysctl;
     71 extern sysctlfn cpu_sysctl;
     72 
     73 /*
     74  * Locking and stats
     75  */
     76 static struct sysctl_lock {
     77 	int	sl_lock;
     78 	int	sl_want;
     79 	int	sl_locked;
     80 } memlock;
     81 
     82 int
     83 sys___sysctl(p, v, retval)
     84 	struct proc *p;
     85 	void *v;
     86 	register_t *retval;
     87 {
     88 	register struct sys___sysctl_args /* {
     89 		syscallarg(int *) name;
     90 		syscallarg(u_int) namelen;
     91 		syscallarg(void *) old;
     92 		syscallarg(size_t *) oldlenp;
     93 		syscallarg(void *) new;
     94 		syscallarg(size_t) newlen;
     95 	} */ *uap = v;
     96 	int error, dolock = 1;
     97 	size_t savelen, oldlen = 0;
     98 	sysctlfn *fn;
     99 	int name[CTL_MAXNAME];
    100 
    101 	if (SCARG(uap, new) != NULL &&
    102 	    (error = suser(p->p_ucred, &p->p_acflag)))
    103 		return (error);
    104 	/*
    105 	 * all top-level sysctl names are non-terminal
    106 	 */
    107 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
    108 		return (EINVAL);
    109 	if (error =
    110 	    copyin(SCARG(uap, name), &name, SCARG(uap, namelen) * sizeof(int)))
    111 		return (error);
    112 
    113 	switch (name[0]) {
    114 	case CTL_KERN:
    115 		fn = kern_sysctl;
    116 		if (name[2] != KERN_VNODE)	/* XXX */
    117 			dolock = 0;
    118 		break;
    119 	case CTL_HW:
    120 		fn = hw_sysctl;
    121 		break;
    122 	case CTL_VM:
    123 		fn = vm_sysctl;
    124 		break;
    125 	case CTL_NET:
    126 		fn = net_sysctl;
    127 		break;
    128 #ifdef notyet
    129 	case CTL_FS:
    130 		fn = fs_sysctl;
    131 		break;
    132 #endif
    133 	case CTL_MACHDEP:
    134 		fn = cpu_sysctl;
    135 		break;
    136 #ifdef DEBUG
    137 	case CTL_DEBUG:
    138 		fn = debug_sysctl;
    139 		break;
    140 #endif
    141 	default:
    142 		return (EOPNOTSUPP);
    143 	}
    144 
    145 	if (SCARG(uap, oldlenp) &&
    146 	    (error = copyin(SCARG(uap, oldlenp), &oldlen, sizeof(oldlen))))
    147 		return (error);
    148 	if (SCARG(uap, old) != NULL) {
    149 		if (!useracc(SCARG(uap, old), oldlen, B_WRITE))
    150 			return (EFAULT);
    151 		while (memlock.sl_lock) {
    152 			memlock.sl_want = 1;
    153 			sleep((caddr_t)&memlock, PRIBIO+1);
    154 			memlock.sl_locked++;
    155 		}
    156 		memlock.sl_lock = 1;
    157 		if (dolock)
    158 			vslock(SCARG(uap, old), oldlen);
    159 		savelen = oldlen;
    160 	}
    161 	error = (*fn)(name + 1, SCARG(uap, namelen) - 1, SCARG(uap, old),
    162 	    &oldlen, SCARG(uap, new), SCARG(uap, newlen), p);
    163 	if (SCARG(uap, old) != NULL) {
    164 		if (dolock)
    165 			vsunlock(SCARG(uap, old), savelen, B_WRITE);
    166 		memlock.sl_lock = 0;
    167 		if (memlock.sl_want) {
    168 			memlock.sl_want = 0;
    169 			wakeup((caddr_t)&memlock);
    170 		}
    171 	}
    172 	if (error)
    173 		return (error);
    174 	if (SCARG(uap, oldlenp))
    175 		error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen));
    176 	*retval = oldlen;
    177 	return (0);
    178 }
    179 
    180 /*
    181  * Attributes stored in the kernel.
    182  */
    183 char hostname[MAXHOSTNAMELEN];
    184 int hostnamelen;
    185 char domainname[MAXHOSTNAMELEN];
    186 int domainnamelen;
    187 long hostid;
    188 #ifdef INSECURE
    189 int securelevel = -1;
    190 #else
    191 int securelevel;
    192 #endif
    193 
    194 /*
    195  * kernel related system variables.
    196  */
    197 kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    198 	int *name;
    199 	u_int namelen;
    200 	void *oldp;
    201 	size_t *oldlenp;
    202 	void *newp;
    203 	size_t newlen;
    204 	struct proc *p;
    205 {
    206 	int error, level, inthostid;
    207 	extern char ostype[], osrelease[], version[];
    208 
    209 	/* all sysctl names at this level are terminal */
    210 	if (namelen != 1 && !(name[0] == KERN_PROC || name[0] == KERN_PROF))
    211 		return (ENOTDIR);		/* overloaded */
    212 
    213 	switch (name[0]) {
    214 	case KERN_OSTYPE:
    215 		return (sysctl_rdstring(oldp, oldlenp, newp, ostype));
    216 	case KERN_OSRELEASE:
    217 		return (sysctl_rdstring(oldp, oldlenp, newp, osrelease));
    218 	case KERN_OSREV:
    219 		return (sysctl_rdint(oldp, oldlenp, newp, BSD));
    220 	case KERN_VERSION:
    221 		return (sysctl_rdstring(oldp, oldlenp, newp, version));
    222 	case KERN_MAXVNODES:
    223 		return(sysctl_int(oldp, oldlenp, newp, newlen, &desiredvnodes));
    224 	case KERN_MAXPROC:
    225 		return (sysctl_int(oldp, oldlenp, newp, newlen, &maxproc));
    226 	case KERN_MAXFILES:
    227 		return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles));
    228 	case KERN_ARGMAX:
    229 		return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX));
    230 	case KERN_SECURELVL:
    231 		level = securelevel;
    232 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) ||
    233 		    newp == NULL)
    234 			return (error);
    235 		if (level < securelevel && p->p_pid != 1)
    236 			return (EPERM);
    237 		securelevel = level;
    238 		return (0);
    239 	case KERN_HOSTNAME:
    240 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    241 		    hostname, sizeof(hostname));
    242 		if (newp && !error)
    243 			hostnamelen = newlen;
    244 		return (error);
    245 	case KERN_DOMAINNAME:
    246 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    247 		    domainname, sizeof(domainname));
    248 		if (newp && !error)
    249 			domainnamelen = newlen;
    250 		return (error);
    251 	case KERN_HOSTID:
    252 		inthostid = hostid;  /* XXX assumes sizeof long <= sizeof int */
    253 		error =  sysctl_int(oldp, oldlenp, newp, newlen, &inthostid);
    254 		hostid = inthostid;
    255 		return (error);
    256 	case KERN_CLOCKRATE:
    257 		return (sysctl_clockrate(oldp, oldlenp));
    258 	case KERN_BOOTTIME:
    259 		return (sysctl_rdstruct(oldp, oldlenp, newp, &boottime,
    260 		    sizeof(struct timeval)));
    261 	case KERN_VNODE:
    262 		return (sysctl_vnode(oldp, oldlenp));
    263 	case KERN_PROC:
    264 		return (sysctl_doproc(name + 1, namelen - 1, oldp, oldlenp));
    265 	case KERN_FILE:
    266 		return (sysctl_file(oldp, oldlenp));
    267 #ifdef GPROF
    268 	case KERN_PROF:
    269 		return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
    270 		    newp, newlen));
    271 #endif
    272 	case KERN_POSIX1:
    273 		return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION));
    274 	case KERN_NGROUPS:
    275 		return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX));
    276 	case KERN_JOB_CONTROL:
    277 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    278 	case KERN_SAVED_IDS:
    279 #ifdef _POSIX_SAVED_IDS
    280 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    281 #else
    282 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    283 #endif
    284 	case KERN_MAXPARTITIONS:
    285 		return (sysctl_rdint(oldp, oldlenp, newp, MAXPARTITIONS));
    286 	case KERN_RAWPARTITION:
    287 		return (sysctl_rdint(oldp, oldlenp, newp, RAW_PART));
    288 	default:
    289 		return (EOPNOTSUPP);
    290 	}
    291 	/* NOTREACHED */
    292 }
    293 
    294 /*
    295  * hardware related system variables.
    296  */
    297 hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    298 	int *name;
    299 	u_int namelen;
    300 	void *oldp;
    301 	size_t *oldlenp;
    302 	void *newp;
    303 	size_t newlen;
    304 	struct proc *p;
    305 {
    306 	extern char machine[], cpu_model[];
    307 
    308 	/* all sysctl names at this level are terminal */
    309 	if (namelen != 1)
    310 		return (ENOTDIR);		/* overloaded */
    311 
    312 	switch (name[0]) {
    313 	case HW_MACHINE:
    314 		return (sysctl_rdstring(oldp, oldlenp, newp, machine));
    315 	case HW_MODEL:
    316 		return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
    317 	case HW_NCPU:
    318 		return (sysctl_rdint(oldp, oldlenp, newp, 1));	/* XXX */
    319 	case HW_BYTEORDER:
    320 		return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
    321 	case HW_PHYSMEM:
    322 		return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
    323 	case HW_USERMEM:
    324 		return (sysctl_rdint(oldp, oldlenp, newp,
    325 		    ctob(physmem - cnt.v_wire_count)));
    326 	case HW_PAGESIZE:
    327 		return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
    328 	default:
    329 		return (EOPNOTSUPP);
    330 	}
    331 	/* NOTREACHED */
    332 }
    333 
    334 #ifdef DEBUG
    335 /*
    336  * Debugging related system variables.
    337  */
    338 struct ctldebug debug0, debug1, debug2, debug3, debug4;
    339 struct ctldebug debug5, debug6, debug7, debug8, debug9;
    340 struct ctldebug debug10, debug11, debug12, debug13, debug14;
    341 struct ctldebug debug15, debug16, debug17, debug18, debug19;
    342 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
    343 	&debug0, &debug1, &debug2, &debug3, &debug4,
    344 	&debug5, &debug6, &debug7, &debug8, &debug9,
    345 	&debug10, &debug11, &debug12, &debug13, &debug14,
    346 	&debug15, &debug16, &debug17, &debug18, &debug19,
    347 };
    348 int
    349 debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    350 	int *name;
    351 	u_int namelen;
    352 	void *oldp;
    353 	size_t *oldlenp;
    354 	void *newp;
    355 	size_t newlen;
    356 	struct proc *p;
    357 {
    358 	struct ctldebug *cdp;
    359 
    360 	/* all sysctl names at this level are name and field */
    361 	if (namelen != 2)
    362 		return (ENOTDIR);		/* overloaded */
    363 	cdp = debugvars[name[0]];
    364 	if (cdp->debugname == 0)
    365 		return (EOPNOTSUPP);
    366 	switch (name[1]) {
    367 	case CTL_DEBUG_NAME:
    368 		return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
    369 	case CTL_DEBUG_VALUE:
    370 		return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
    371 	default:
    372 		return (EOPNOTSUPP);
    373 	}
    374 	/* NOTREACHED */
    375 }
    376 #endif /* DEBUG */
    377 
    378 /*
    379  * Validate parameters and get old / set new parameters
    380  * for an integer-valued sysctl function.
    381  */
    382 sysctl_int(oldp, oldlenp, newp, newlen, valp)
    383 	void *oldp;
    384 	size_t *oldlenp;
    385 	void *newp;
    386 	size_t newlen;
    387 	int *valp;
    388 {
    389 	int error = 0;
    390 
    391 	if (oldp && *oldlenp < sizeof(int))
    392 		return (ENOMEM);
    393 	if (newp && newlen != sizeof(int))
    394 		return (EINVAL);
    395 	*oldlenp = sizeof(int);
    396 	if (oldp)
    397 		error = copyout(valp, oldp, sizeof(int));
    398 	if (error == 0 && newp)
    399 		error = copyin(newp, valp, sizeof(int));
    400 	return (error);
    401 }
    402 
    403 /*
    404  * As above, but read-only.
    405  */
    406 sysctl_rdint(oldp, oldlenp, newp, val)
    407 	void *oldp;
    408 	size_t *oldlenp;
    409 	void *newp;
    410 	int val;
    411 {
    412 	int error = 0;
    413 
    414 	if (oldp && *oldlenp < sizeof(int))
    415 		return (ENOMEM);
    416 	if (newp)
    417 		return (EPERM);
    418 	*oldlenp = sizeof(int);
    419 	if (oldp)
    420 		error = copyout((caddr_t)&val, oldp, sizeof(int));
    421 	return (error);
    422 }
    423 
    424 /*
    425  * Validate parameters and get old / set new parameters
    426  * for a string-valued sysctl function.
    427  */
    428 sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen)
    429 	void *oldp;
    430 	size_t *oldlenp;
    431 	void *newp;
    432 	size_t newlen;
    433 	char *str;
    434 	int maxlen;
    435 {
    436 	int len, error = 0;
    437 
    438 	len = strlen(str) + 1;
    439 	if (oldp && *oldlenp < len)
    440 		return (ENOMEM);
    441 	if (newp && newlen >= maxlen)
    442 		return (EINVAL);
    443 	if (oldp) {
    444 		*oldlenp = len;
    445 		error = copyout(str, oldp, len);
    446 	}
    447 	if (error == 0 && newp) {
    448 		error = copyin(newp, str, newlen);
    449 		str[newlen] = 0;
    450 	}
    451 	return (error);
    452 }
    453 
    454 /*
    455  * As above, but read-only.
    456  */
    457 sysctl_rdstring(oldp, oldlenp, newp, str)
    458 	void *oldp;
    459 	size_t *oldlenp;
    460 	void *newp;
    461 	char *str;
    462 {
    463 	int len, error = 0;
    464 
    465 	len = strlen(str) + 1;
    466 	if (oldp && *oldlenp < len)
    467 		return (ENOMEM);
    468 	if (newp)
    469 		return (EPERM);
    470 	*oldlenp = len;
    471 	if (oldp)
    472 		error = copyout(str, oldp, len);
    473 	return (error);
    474 }
    475 
    476 /*
    477  * Validate parameters and get old / set new parameters
    478  * for a structure oriented sysctl function.
    479  */
    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 sysctl_rdstruct(oldp, oldlenp, newp, sp, len)
    508 	void *oldp;
    509 	size_t *oldlenp;
    510 	void *newp, *sp;
    511 	int len;
    512 {
    513 	int error = 0;
    514 
    515 	if (oldp && *oldlenp < len)
    516 		return (ENOMEM);
    517 	if (newp)
    518 		return (EPERM);
    519 	*oldlenp = len;
    520 	if (oldp)
    521 		error = copyout(sp, oldp, len);
    522 	return (error);
    523 }
    524 
    525 /*
    526  * Get file structures.
    527  */
    528 sysctl_file(where, sizep)
    529 	char *where;
    530 	size_t *sizep;
    531 {
    532 	int buflen, error;
    533 	struct file *fp;
    534 	char *start = where;
    535 
    536 	buflen = *sizep;
    537 	if (where == NULL) {
    538 		/*
    539 		 * overestimate by 10 files
    540 		 */
    541 		*sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
    542 		return (0);
    543 	}
    544 
    545 	/*
    546 	 * first copyout filehead
    547 	 */
    548 	if (buflen < sizeof(filehead)) {
    549 		*sizep = 0;
    550 		return (0);
    551 	}
    552 	if (error = copyout((caddr_t)&filehead, where, sizeof(filehead)))
    553 		return (error);
    554 	buflen -= sizeof(filehead);
    555 	where += sizeof(filehead);
    556 
    557 	/*
    558 	 * followed by an array of file structures
    559 	 */
    560 	for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
    561 		if (buflen < sizeof(struct file)) {
    562 			*sizep = where - start;
    563 			return (ENOMEM);
    564 		}
    565 		if (error = copyout((caddr_t)fp, where, sizeof (struct file)))
    566 			return (error);
    567 		buflen -= sizeof(struct file);
    568 		where += sizeof(struct file);
    569 	}
    570 	*sizep = where - start;
    571 	return (0);
    572 }
    573 
    574 /*
    575  * try over estimating by 5 procs
    576  */
    577 #define KERN_PROCSLOP	(5 * sizeof (struct kinfo_proc))
    578 
    579 sysctl_doproc(name, namelen, where, sizep)
    580 	int *name;
    581 	u_int namelen;
    582 	char *where;
    583 	size_t *sizep;
    584 {
    585 	register struct proc *p;
    586 	register struct kinfo_proc *dp = (struct kinfo_proc *)where;
    587 	register int needed = 0;
    588 	int buflen = where != NULL ? *sizep : 0;
    589 	int doingzomb;
    590 	struct eproc eproc;
    591 	int error = 0;
    592 
    593 	if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
    594 		return (EINVAL);
    595 	p = allproc.lh_first;
    596 	doingzomb = 0;
    597 again:
    598 	for (; p != 0; p = p->p_list.le_next) {
    599 		/*
    600 		 * Skip embryonic processes.
    601 		 */
    602 		if (p->p_stat == SIDL)
    603 			continue;
    604 		/*
    605 		 * TODO - make more efficient (see notes below).
    606 		 * do by session.
    607 		 */
    608 		switch (name[0]) {
    609 
    610 		case KERN_PROC_PID:
    611 			/* could do this with just a lookup */
    612 			if (p->p_pid != (pid_t)name[1])
    613 				continue;
    614 			break;
    615 
    616 		case KERN_PROC_PGRP:
    617 			/* could do this by traversing pgrp */
    618 			if (p->p_pgrp->pg_id != (pid_t)name[1])
    619 				continue;
    620 			break;
    621 
    622 		case KERN_PROC_TTY:
    623 			if ((p->p_flag & P_CONTROLT) == 0 ||
    624 			    p->p_session->s_ttyp == NULL ||
    625 			    p->p_session->s_ttyp->t_dev != (dev_t)name[1])
    626 				continue;
    627 			break;
    628 
    629 		case KERN_PROC_UID:
    630 			if (p->p_ucred->cr_uid != (uid_t)name[1])
    631 				continue;
    632 			break;
    633 
    634 		case KERN_PROC_RUID:
    635 			if (p->p_cred->p_ruid != (uid_t)name[1])
    636 				continue;
    637 			break;
    638 		}
    639 		if (buflen >= sizeof(struct kinfo_proc)) {
    640 			fill_eproc(p, &eproc);
    641 			if (error = copyout((caddr_t)p, &dp->kp_proc,
    642 			    sizeof(struct proc)))
    643 				return (error);
    644 			if (error = copyout((caddr_t)&eproc, &dp->kp_eproc,
    645 			    sizeof(eproc)))
    646 				return (error);
    647 			dp++;
    648 			buflen -= sizeof(struct kinfo_proc);
    649 		}
    650 		needed += sizeof(struct kinfo_proc);
    651 	}
    652 	if (doingzomb == 0) {
    653 		p = zombproc.lh_first;
    654 		doingzomb++;
    655 		goto again;
    656 	}
    657 	if (where != NULL) {
    658 		*sizep = (caddr_t)dp - where;
    659 		if (needed > *sizep)
    660 			return (ENOMEM);
    661 	} else {
    662 		needed += KERN_PROCSLOP;
    663 		*sizep = needed;
    664 	}
    665 	return (0);
    666 }
    667 
    668 /*
    669  * Fill in an eproc structure for the specified process.
    670  */
    671 void
    672 fill_eproc(p, ep)
    673 	register struct proc *p;
    674 	register struct eproc *ep;
    675 {
    676 	register struct tty *tp;
    677 
    678 	ep->e_paddr = p;
    679 	ep->e_sess = p->p_pgrp->pg_session;
    680 	ep->e_pcred = *p->p_cred;
    681 	ep->e_ucred = *p->p_ucred;
    682 	if (p->p_stat == SIDL || p->p_stat == SZOMB) {
    683 		ep->e_vm.vm_rssize = 0;
    684 		ep->e_vm.vm_tsize = 0;
    685 		ep->e_vm.vm_dsize = 0;
    686 		ep->e_vm.vm_ssize = 0;
    687 		/* ep->e_vm.vm_pmap = XXX; */
    688 	} else {
    689 		register struct vmspace *vm = p->p_vmspace;
    690 
    691 #ifdef pmap_resident_count
    692 		ep->e_vm.vm_rssize = pmap_resident_count(&vm->vm_pmap); /*XXX*/
    693 #else
    694 		ep->e_vm.vm_rssize = vm->vm_rssize;
    695 #endif
    696 		ep->e_vm.vm_tsize = vm->vm_tsize;
    697 		ep->e_vm.vm_dsize = vm->vm_dsize;
    698 		ep->e_vm.vm_ssize = vm->vm_ssize;
    699 		ep->e_vm.vm_pmap = vm->vm_pmap;
    700 	}
    701 	if (p->p_pptr)
    702 		ep->e_ppid = p->p_pptr->p_pid;
    703 	else
    704 		ep->e_ppid = 0;
    705 	ep->e_pgid = p->p_pgrp->pg_id;
    706 	ep->e_jobc = p->p_pgrp->pg_jobc;
    707 	if ((p->p_flag & P_CONTROLT) &&
    708 	     (tp = ep->e_sess->s_ttyp)) {
    709 		ep->e_tdev = tp->t_dev;
    710 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
    711 		ep->e_tsess = tp->t_session;
    712 	} else
    713 		ep->e_tdev = NODEV;
    714 	ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
    715 	if (SESS_LEADER(p))
    716 		ep->e_flag |= EPROC_SLEADER;
    717 	if (p->p_wmesg)
    718 		strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
    719 	ep->e_xsize = ep->e_xrssize = 0;
    720 	ep->e_xccount = ep->e_xswrss = 0;
    721 }
    722 
    723