Home | History | Annotate | Line # | Download | only in kern
kern_sysctl.c revision 1.18
      1 /*	$NetBSD: kern_sysctl.c,v 1.18 1996/07/17 21:54:04 explorer 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 = 0;
    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 	int old_autonicetime;
    199 	extern char ostype[], osrelease[], version[];
    200 
    201 	/* all sysctl names at this level are terminal */
    202 	if (namelen != 1 && !(name[0] == KERN_PROC || name[0] == KERN_PROF))
    203 		return (ENOTDIR);		/* overloaded */
    204 
    205 	switch (name[0]) {
    206 	case KERN_OSTYPE:
    207 		return (sysctl_rdstring(oldp, oldlenp, newp, ostype));
    208 	case KERN_OSRELEASE:
    209 		return (sysctl_rdstring(oldp, oldlenp, newp, osrelease));
    210 	case KERN_OSREV:
    211 		return (sysctl_rdint(oldp, oldlenp, newp, BSD));
    212 	case KERN_VERSION:
    213 		return (sysctl_rdstring(oldp, oldlenp, newp, version));
    214 	case KERN_MAXVNODES:
    215 		return(sysctl_int(oldp, oldlenp, newp, newlen, &desiredvnodes));
    216 	case KERN_MAXPROC:
    217 		return (sysctl_int(oldp, oldlenp, newp, newlen, &maxproc));
    218 	case KERN_MAXFILES:
    219 		return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles));
    220 	case KERN_ARGMAX:
    221 		return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX));
    222 	case KERN_SECURELVL:
    223 		level = securelevel;
    224 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) ||
    225 		    newp == NULL)
    226 			return (error);
    227 		if (level < securelevel && p->p_pid != 1)
    228 			return (EPERM);
    229 		securelevel = level;
    230 		return (0);
    231 	case KERN_HOSTNAME:
    232 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    233 		    hostname, sizeof(hostname));
    234 		if (newp && !error)
    235 			hostnamelen = newlen;
    236 		return (error);
    237 	case KERN_DOMAINNAME:
    238 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    239 		    domainname, sizeof(domainname));
    240 		if (newp && !error)
    241 			domainnamelen = newlen;
    242 		return (error);
    243 	case KERN_HOSTID:
    244 		inthostid = hostid;  /* XXX assumes sizeof long <= sizeof int */
    245 		error =  sysctl_int(oldp, oldlenp, newp, newlen, &inthostid);
    246 		hostid = inthostid;
    247 		return (error);
    248 	case KERN_CLOCKRATE:
    249 		return (sysctl_clockrate(oldp, oldlenp));
    250 	case KERN_BOOTTIME:
    251 		return (sysctl_rdstruct(oldp, oldlenp, newp, &boottime,
    252 		    sizeof(struct timeval)));
    253 	case KERN_VNODE:
    254 		return (sysctl_vnode(oldp, oldlenp));
    255 	case KERN_PROC:
    256 		return (sysctl_doproc(name + 1, namelen - 1, oldp, oldlenp));
    257 	case KERN_FILE:
    258 		return (sysctl_file(oldp, oldlenp));
    259 #ifdef GPROF
    260 	case KERN_PROF:
    261 		return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
    262 		    newp, newlen));
    263 #endif
    264 	case KERN_POSIX1:
    265 		return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION));
    266 	case KERN_NGROUPS:
    267 		return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX));
    268 	case KERN_JOB_CONTROL:
    269 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    270 	case KERN_SAVED_IDS:
    271 #ifdef _POSIX_SAVED_IDS
    272 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    273 #else
    274 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    275 #endif
    276 	case KERN_MAXPARTITIONS:
    277 		return (sysctl_rdint(oldp, oldlenp, newp, MAXPARTITIONS));
    278 	case KERN_RAWPARTITION:
    279 		return (sysctl_rdint(oldp, oldlenp, newp, RAW_PART));
    280 	case KERN_NTPTIME:
    281 		return (sysctl_ntptime(oldp, oldlenp));
    282 	case KERN_AUTONICETIME:
    283 	        old_autonicetime = autonicetime;
    284 	        error = sysctl_int(oldp, oldlenp, newp, newlen, &autonicetime);
    285 		if (autonicetime < 0)
    286  		        autonicetime = old_autonicetime;
    287 		return (error);
    288 	case KERN_AUTONICEVAL:
    289 		error = sysctl_int(oldp, oldlenp, newp, newlen, &autoniceval);
    290 		if (autoniceval < PRIO_MIN)
    291 			autoniceval = PRIO_MIN;
    292 		if (autoniceval > PRIO_MAX)
    293 			autoniceval = PRIO_MAX;
    294 		return (error);
    295 	default:
    296 		return (EOPNOTSUPP);
    297 	}
    298 	/* NOTREACHED */
    299 }
    300 
    301 /*
    302  * hardware related system variables.
    303  */
    304 int
    305 hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    306 	int *name;
    307 	u_int namelen;
    308 	void *oldp;
    309 	size_t *oldlenp;
    310 	void *newp;
    311 	size_t newlen;
    312 	struct proc *p;
    313 {
    314 	extern char machine[], cpu_model[];
    315 
    316 	/* all sysctl names at this level are terminal */
    317 	if (namelen != 1)
    318 		return (ENOTDIR);		/* overloaded */
    319 
    320 	switch (name[0]) {
    321 	case HW_MACHINE:
    322 		return (sysctl_rdstring(oldp, oldlenp, newp, machine));
    323 	case HW_MODEL:
    324 		return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
    325 	case HW_NCPU:
    326 		return (sysctl_rdint(oldp, oldlenp, newp, 1));	/* XXX */
    327 	case HW_BYTEORDER:
    328 		return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
    329 	case HW_PHYSMEM:
    330 		return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
    331 	case HW_USERMEM:
    332 		return (sysctl_rdint(oldp, oldlenp, newp,
    333 		    ctob(physmem - cnt.v_wire_count)));
    334 	case HW_PAGESIZE:
    335 		return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
    336 	default:
    337 		return (EOPNOTSUPP);
    338 	}
    339 	/* NOTREACHED */
    340 }
    341 
    342 #ifdef DEBUG
    343 /*
    344  * Debugging related system variables.
    345  */
    346 struct ctldebug debug0, debug1, debug2, debug3, debug4;
    347 struct ctldebug debug5, debug6, debug7, debug8, debug9;
    348 struct ctldebug debug10, debug11, debug12, debug13, debug14;
    349 struct ctldebug debug15, debug16, debug17, debug18, debug19;
    350 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
    351 	&debug0, &debug1, &debug2, &debug3, &debug4,
    352 	&debug5, &debug6, &debug7, &debug8, &debug9,
    353 	&debug10, &debug11, &debug12, &debug13, &debug14,
    354 	&debug15, &debug16, &debug17, &debug18, &debug19,
    355 };
    356 int
    357 debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    358 	int *name;
    359 	u_int namelen;
    360 	void *oldp;
    361 	size_t *oldlenp;
    362 	void *newp;
    363 	size_t newlen;
    364 	struct proc *p;
    365 {
    366 	struct ctldebug *cdp;
    367 
    368 	/* all sysctl names at this level are name and field */
    369 	if (namelen != 2)
    370 		return (ENOTDIR);		/* overloaded */
    371 	cdp = debugvars[name[0]];
    372 	if (cdp->debugname == 0)
    373 		return (EOPNOTSUPP);
    374 	switch (name[1]) {
    375 	case CTL_DEBUG_NAME:
    376 		return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
    377 	case CTL_DEBUG_VALUE:
    378 		return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
    379 	default:
    380 		return (EOPNOTSUPP);
    381 	}
    382 	/* NOTREACHED */
    383 }
    384 #endif /* DEBUG */
    385 
    386 /*
    387  * Validate parameters and get old / set new parameters
    388  * for an integer-valued sysctl function.
    389  */
    390 int
    391 sysctl_int(oldp, oldlenp, newp, newlen, valp)
    392 	void *oldp;
    393 	size_t *oldlenp;
    394 	void *newp;
    395 	size_t newlen;
    396 	int *valp;
    397 {
    398 	int error = 0;
    399 
    400 	if (oldp && *oldlenp < sizeof(int))
    401 		return (ENOMEM);
    402 	if (newp && newlen != sizeof(int))
    403 		return (EINVAL);
    404 	*oldlenp = sizeof(int);
    405 	if (oldp)
    406 		error = copyout(valp, oldp, sizeof(int));
    407 	if (error == 0 && newp)
    408 		error = copyin(newp, valp, sizeof(int));
    409 	return (error);
    410 }
    411 
    412 /*
    413  * As above, but read-only.
    414  */
    415 int
    416 sysctl_rdint(oldp, oldlenp, newp, val)
    417 	void *oldp;
    418 	size_t *oldlenp;
    419 	void *newp;
    420 	int val;
    421 {
    422 	int error = 0;
    423 
    424 	if (oldp && *oldlenp < sizeof(int))
    425 		return (ENOMEM);
    426 	if (newp)
    427 		return (EPERM);
    428 	*oldlenp = sizeof(int);
    429 	if (oldp)
    430 		error = copyout((caddr_t)&val, oldp, sizeof(int));
    431 	return (error);
    432 }
    433 
    434 /*
    435  * Validate parameters and get old / set new parameters
    436  * for a string-valued sysctl function.
    437  */
    438 int
    439 sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen)
    440 	void *oldp;
    441 	size_t *oldlenp;
    442 	void *newp;
    443 	size_t newlen;
    444 	char *str;
    445 	int maxlen;
    446 {
    447 	int len, error = 0;
    448 
    449 	len = strlen(str) + 1;
    450 	if (oldp && *oldlenp < len)
    451 		return (ENOMEM);
    452 	if (newp && newlen >= maxlen)
    453 		return (EINVAL);
    454 	if (oldp) {
    455 		*oldlenp = len;
    456 		error = copyout(str, oldp, len);
    457 	}
    458 	if (error == 0 && newp) {
    459 		error = copyin(newp, str, newlen);
    460 		str[newlen] = 0;
    461 	}
    462 	return (error);
    463 }
    464 
    465 /*
    466  * As above, but read-only.
    467  */
    468 int
    469 sysctl_rdstring(oldp, oldlenp, newp, str)
    470 	void *oldp;
    471 	size_t *oldlenp;
    472 	void *newp;
    473 	char *str;
    474 {
    475 	int len, error = 0;
    476 
    477 	len = strlen(str) + 1;
    478 	if (oldp && *oldlenp < len)
    479 		return (ENOMEM);
    480 	if (newp)
    481 		return (EPERM);
    482 	*oldlenp = len;
    483 	if (oldp)
    484 		error = copyout(str, oldp, len);
    485 	return (error);
    486 }
    487 
    488 /*
    489  * Validate parameters and get old / set new parameters
    490  * for a structure oriented sysctl function.
    491  */
    492 int
    493 sysctl_struct(oldp, oldlenp, newp, newlen, sp, len)
    494 	void *oldp;
    495 	size_t *oldlenp;
    496 	void *newp;
    497 	size_t newlen;
    498 	void *sp;
    499 	int len;
    500 {
    501 	int error = 0;
    502 
    503 	if (oldp && *oldlenp < len)
    504 		return (ENOMEM);
    505 	if (newp && newlen > len)
    506 		return (EINVAL);
    507 	if (oldp) {
    508 		*oldlenp = len;
    509 		error = copyout(sp, oldp, len);
    510 	}
    511 	if (error == 0 && newp)
    512 		error = copyin(newp, sp, len);
    513 	return (error);
    514 }
    515 
    516 /*
    517  * Validate parameters and get old parameters
    518  * for a structure oriented sysctl function.
    519  */
    520 int
    521 sysctl_rdstruct(oldp, oldlenp, newp, sp, len)
    522 	void *oldp;
    523 	size_t *oldlenp;
    524 	void *newp, *sp;
    525 	int len;
    526 {
    527 	int error = 0;
    528 
    529 	if (oldp && *oldlenp < len)
    530 		return (ENOMEM);
    531 	if (newp)
    532 		return (EPERM);
    533 	*oldlenp = len;
    534 	if (oldp)
    535 		error = copyout(sp, oldp, len);
    536 	return (error);
    537 }
    538 
    539 /*
    540  * Get file structures.
    541  */
    542 int
    543 sysctl_file(where, sizep)
    544 	char *where;
    545 	size_t *sizep;
    546 {
    547 	int buflen, error;
    548 	struct file *fp;
    549 	char *start = where;
    550 
    551 	buflen = *sizep;
    552 	if (where == NULL) {
    553 		/*
    554 		 * overestimate by 10 files
    555 		 */
    556 		*sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
    557 		return (0);
    558 	}
    559 
    560 	/*
    561 	 * first copyout filehead
    562 	 */
    563 	if (buflen < sizeof(filehead)) {
    564 		*sizep = 0;
    565 		return (0);
    566 	}
    567 	error = copyout((caddr_t)&filehead, where, sizeof(filehead));
    568 	if (error)
    569 		return (error);
    570 	buflen -= sizeof(filehead);
    571 	where += sizeof(filehead);
    572 
    573 	/*
    574 	 * followed by an array of file structures
    575 	 */
    576 	for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
    577 		if (buflen < sizeof(struct file)) {
    578 			*sizep = where - start;
    579 			return (ENOMEM);
    580 		}
    581 		error = copyout((caddr_t)fp, where, sizeof (struct file));
    582 		if (error)
    583 			return (error);
    584 		buflen -= sizeof(struct file);
    585 		where += sizeof(struct file);
    586 	}
    587 	*sizep = where - start;
    588 	return (0);
    589 }
    590 
    591 /*
    592  * try over estimating by 5 procs
    593  */
    594 #define KERN_PROCSLOP	(5 * sizeof (struct kinfo_proc))
    595 
    596 int
    597 sysctl_doproc(name, namelen, where, sizep)
    598 	int *name;
    599 	u_int namelen;
    600 	char *where;
    601 	size_t *sizep;
    602 {
    603 	register struct proc *p;
    604 	register struct kinfo_proc *dp = (struct kinfo_proc *)where;
    605 	register int needed = 0;
    606 	int buflen = where != NULL ? *sizep : 0;
    607 	int doingzomb;
    608 	struct eproc eproc;
    609 	int error = 0;
    610 
    611 	if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
    612 		return (EINVAL);
    613 	p = allproc.lh_first;
    614 	doingzomb = 0;
    615 again:
    616 	for (; p != 0; p = p->p_list.le_next) {
    617 		/*
    618 		 * Skip embryonic processes.
    619 		 */
    620 		if (p->p_stat == SIDL)
    621 			continue;
    622 		/*
    623 		 * TODO - make more efficient (see notes below).
    624 		 * do by session.
    625 		 */
    626 		switch (name[0]) {
    627 
    628 		case KERN_PROC_PID:
    629 			/* could do this with just a lookup */
    630 			if (p->p_pid != (pid_t)name[1])
    631 				continue;
    632 			break;
    633 
    634 		case KERN_PROC_PGRP:
    635 			/* could do this by traversing pgrp */
    636 			if (p->p_pgrp->pg_id != (pid_t)name[1])
    637 				continue;
    638 			break;
    639 
    640 		case KERN_PROC_TTY:
    641 			if ((p->p_flag & P_CONTROLT) == 0 ||
    642 			    p->p_session->s_ttyp == NULL ||
    643 			    p->p_session->s_ttyp->t_dev != (dev_t)name[1])
    644 				continue;
    645 			break;
    646 
    647 		case KERN_PROC_UID:
    648 			if (p->p_ucred->cr_uid != (uid_t)name[1])
    649 				continue;
    650 			break;
    651 
    652 		case KERN_PROC_RUID:
    653 			if (p->p_cred->p_ruid != (uid_t)name[1])
    654 				continue;
    655 			break;
    656 		}
    657 		if (buflen >= sizeof(struct kinfo_proc)) {
    658 			fill_eproc(p, &eproc);
    659 			error = copyout((caddr_t)p, &dp->kp_proc,
    660 					sizeof(struct proc));
    661 			if (error)
    662 				return (error);
    663 			error = copyout((caddr_t)&eproc, &dp->kp_eproc,
    664 					sizeof(eproc));
    665 			if (error)
    666 				return (error);
    667 			dp++;
    668 			buflen -= sizeof(struct kinfo_proc);
    669 		}
    670 		needed += sizeof(struct kinfo_proc);
    671 	}
    672 	if (doingzomb == 0) {
    673 		p = zombproc.lh_first;
    674 		doingzomb++;
    675 		goto again;
    676 	}
    677 	if (where != NULL) {
    678 		*sizep = (caddr_t)dp - where;
    679 		if (needed > *sizep)
    680 			return (ENOMEM);
    681 	} else {
    682 		needed += KERN_PROCSLOP;
    683 		*sizep = needed;
    684 	}
    685 	return (0);
    686 }
    687 
    688 /*
    689  * Fill in an eproc structure for the specified process.
    690  */
    691 void
    692 fill_eproc(p, ep)
    693 	register struct proc *p;
    694 	register struct eproc *ep;
    695 {
    696 	register struct tty *tp;
    697 
    698 	ep->e_paddr = p;
    699 	ep->e_sess = p->p_pgrp->pg_session;
    700 	ep->e_pcred = *p->p_cred;
    701 	ep->e_ucred = *p->p_ucred;
    702 	if (p->p_stat == SIDL || p->p_stat == SZOMB) {
    703 		ep->e_vm.vm_rssize = 0;
    704 		ep->e_vm.vm_tsize = 0;
    705 		ep->e_vm.vm_dsize = 0;
    706 		ep->e_vm.vm_ssize = 0;
    707 		/* ep->e_vm.vm_pmap = XXX; */
    708 	} else {
    709 		register struct vmspace *vm = p->p_vmspace;
    710 
    711 #ifdef pmap_resident_count
    712 		ep->e_vm.vm_rssize = pmap_resident_count(&vm->vm_pmap); /*XXX*/
    713 #else
    714 		ep->e_vm.vm_rssize = vm->vm_rssize;
    715 #endif
    716 		ep->e_vm.vm_tsize = vm->vm_tsize;
    717 		ep->e_vm.vm_dsize = vm->vm_dsize;
    718 		ep->e_vm.vm_ssize = vm->vm_ssize;
    719 		ep->e_vm.vm_pmap = vm->vm_pmap;
    720 	}
    721 	if (p->p_pptr)
    722 		ep->e_ppid = p->p_pptr->p_pid;
    723 	else
    724 		ep->e_ppid = 0;
    725 	ep->e_pgid = p->p_pgrp->pg_id;
    726 	ep->e_jobc = p->p_pgrp->pg_jobc;
    727 	if ((p->p_flag & P_CONTROLT) &&
    728 	     (tp = ep->e_sess->s_ttyp)) {
    729 		ep->e_tdev = tp->t_dev;
    730 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
    731 		ep->e_tsess = tp->t_session;
    732 	} else
    733 		ep->e_tdev = NODEV;
    734 	ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
    735 	if (SESS_LEADER(p))
    736 		ep->e_flag |= EPROC_SLEADER;
    737 	if (p->p_wmesg)
    738 		strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
    739 	ep->e_xsize = ep->e_xrssize = 0;
    740 	ep->e_xccount = ep->e_xswrss = 0;
    741 }
    742 
    743