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