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