Home | History | Annotate | Line # | Download | only in kern
kern_sysctl.c revision 1.20.2.1
      1 /*	$NetBSD: kern_sysctl.c,v 1.20.2.1 1997/01/14 21:27:05 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_ROOT_DEVICE:
    304 		return (sysctl_rdstring(oldp, oldlenp, newp,
    305 		    root_device->dv_xname));
    306 	default:
    307 		return (EOPNOTSUPP);
    308 	}
    309 	/* NOTREACHED */
    310 }
    311 
    312 /*
    313  * hardware related system variables.
    314  */
    315 int
    316 hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    317 	int *name;
    318 	u_int namelen;
    319 	void *oldp;
    320 	size_t *oldlenp;
    321 	void *newp;
    322 	size_t newlen;
    323 	struct proc *p;
    324 {
    325 	extern char machine[], cpu_model[];
    326 
    327 	/* all sysctl names at this level are terminal */
    328 	if (namelen != 1)
    329 		return (ENOTDIR);		/* overloaded */
    330 
    331 	switch (name[0]) {
    332 	case HW_MACHINE:
    333 		return (sysctl_rdstring(oldp, oldlenp, newp, machine));
    334 	case HW_MODEL:
    335 		return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
    336 	case HW_NCPU:
    337 		return (sysctl_rdint(oldp, oldlenp, newp, 1));	/* XXX */
    338 	case HW_BYTEORDER:
    339 		return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
    340 	case HW_PHYSMEM:
    341 		return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
    342 	case HW_USERMEM:
    343 		return (sysctl_rdint(oldp, oldlenp, newp,
    344 		    ctob(physmem - cnt.v_wire_count)));
    345 	case HW_PAGESIZE:
    346 		return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
    347 	default:
    348 		return (EOPNOTSUPP);
    349 	}
    350 	/* NOTREACHED */
    351 }
    352 
    353 #ifdef DEBUG
    354 /*
    355  * Debugging related system variables.
    356  */
    357 struct ctldebug debug0, debug1, debug2, debug3, debug4;
    358 struct ctldebug debug5, debug6, debug7, debug8, debug9;
    359 struct ctldebug debug10, debug11, debug12, debug13, debug14;
    360 struct ctldebug debug15, debug16, debug17, debug18, debug19;
    361 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
    362 	&debug0, &debug1, &debug2, &debug3, &debug4,
    363 	&debug5, &debug6, &debug7, &debug8, &debug9,
    364 	&debug10, &debug11, &debug12, &debug13, &debug14,
    365 	&debug15, &debug16, &debug17, &debug18, &debug19,
    366 };
    367 int
    368 debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    369 	int *name;
    370 	u_int namelen;
    371 	void *oldp;
    372 	size_t *oldlenp;
    373 	void *newp;
    374 	size_t newlen;
    375 	struct proc *p;
    376 {
    377 	struct ctldebug *cdp;
    378 
    379 	/* all sysctl names at this level are name and field */
    380 	if (namelen != 2)
    381 		return (ENOTDIR);		/* overloaded */
    382 	cdp = debugvars[name[0]];
    383 	if (cdp->debugname == 0)
    384 		return (EOPNOTSUPP);
    385 	switch (name[1]) {
    386 	case CTL_DEBUG_NAME:
    387 		return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
    388 	case CTL_DEBUG_VALUE:
    389 		return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
    390 	default:
    391 		return (EOPNOTSUPP);
    392 	}
    393 	/* NOTREACHED */
    394 }
    395 #endif /* DEBUG */
    396 
    397 /*
    398  * Validate parameters and get old / set new parameters
    399  * for an integer-valued sysctl function.
    400  */
    401 int
    402 sysctl_int(oldp, oldlenp, newp, newlen, valp)
    403 	void *oldp;
    404 	size_t *oldlenp;
    405 	void *newp;
    406 	size_t newlen;
    407 	int *valp;
    408 {
    409 	int error = 0;
    410 
    411 	if (oldp && *oldlenp < sizeof(int))
    412 		return (ENOMEM);
    413 	if (newp && newlen != sizeof(int))
    414 		return (EINVAL);
    415 	*oldlenp = sizeof(int);
    416 	if (oldp)
    417 		error = copyout(valp, oldp, sizeof(int));
    418 	if (error == 0 && newp)
    419 		error = copyin(newp, valp, sizeof(int));
    420 	return (error);
    421 }
    422 
    423 /*
    424  * As above, but read-only.
    425  */
    426 int
    427 sysctl_rdint(oldp, oldlenp, newp, val)
    428 	void *oldp;
    429 	size_t *oldlenp;
    430 	void *newp;
    431 	int val;
    432 {
    433 	int error = 0;
    434 
    435 	if (oldp && *oldlenp < sizeof(int))
    436 		return (ENOMEM);
    437 	if (newp)
    438 		return (EPERM);
    439 	*oldlenp = sizeof(int);
    440 	if (oldp)
    441 		error = copyout((caddr_t)&val, oldp, sizeof(int));
    442 	return (error);
    443 }
    444 
    445 /*
    446  * Validate parameters and get old / set new parameters
    447  * for a string-valued sysctl function.
    448  */
    449 int
    450 sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen)
    451 	void *oldp;
    452 	size_t *oldlenp;
    453 	void *newp;
    454 	size_t newlen;
    455 	char *str;
    456 	int maxlen;
    457 {
    458 	int len, error = 0;
    459 
    460 	len = strlen(str) + 1;
    461 	if (oldp && *oldlenp < len)
    462 		return (ENOMEM);
    463 	if (newp && newlen >= maxlen)
    464 		return (EINVAL);
    465 	if (oldp) {
    466 		*oldlenp = len;
    467 		error = copyout(str, oldp, len);
    468 	}
    469 	if (error == 0 && newp) {
    470 		error = copyin(newp, str, newlen);
    471 		str[newlen] = 0;
    472 	}
    473 	return (error);
    474 }
    475 
    476 /*
    477  * As above, but read-only.
    478  */
    479 int
    480 sysctl_rdstring(oldp, oldlenp, newp, str)
    481 	void *oldp;
    482 	size_t *oldlenp;
    483 	void *newp;
    484 	char *str;
    485 {
    486 	int len, error = 0;
    487 
    488 	len = strlen(str) + 1;
    489 	if (oldp && *oldlenp < len)
    490 		return (ENOMEM);
    491 	if (newp)
    492 		return (EPERM);
    493 	*oldlenp = len;
    494 	if (oldp)
    495 		error = copyout(str, oldp, len);
    496 	return (error);
    497 }
    498 
    499 /*
    500  * Validate parameters and get old / set new parameters
    501  * for a structure oriented sysctl function.
    502  */
    503 int
    504 sysctl_struct(oldp, oldlenp, newp, newlen, sp, len)
    505 	void *oldp;
    506 	size_t *oldlenp;
    507 	void *newp;
    508 	size_t newlen;
    509 	void *sp;
    510 	int len;
    511 {
    512 	int error = 0;
    513 
    514 	if (oldp && *oldlenp < len)
    515 		return (ENOMEM);
    516 	if (newp && newlen > len)
    517 		return (EINVAL);
    518 	if (oldp) {
    519 		*oldlenp = len;
    520 		error = copyout(sp, oldp, len);
    521 	}
    522 	if (error == 0 && newp)
    523 		error = copyin(newp, sp, len);
    524 	return (error);
    525 }
    526 
    527 /*
    528  * Validate parameters and get old parameters
    529  * for a structure oriented sysctl function.
    530  */
    531 int
    532 sysctl_rdstruct(oldp, oldlenp, newp, sp, len)
    533 	void *oldp;
    534 	size_t *oldlenp;
    535 	void *newp, *sp;
    536 	int len;
    537 {
    538 	int error = 0;
    539 
    540 	if (oldp && *oldlenp < len)
    541 		return (ENOMEM);
    542 	if (newp)
    543 		return (EPERM);
    544 	*oldlenp = len;
    545 	if (oldp)
    546 		error = copyout(sp, oldp, len);
    547 	return (error);
    548 }
    549 
    550 /*
    551  * Get file structures.
    552  */
    553 int
    554 sysctl_file(where, sizep)
    555 	char *where;
    556 	size_t *sizep;
    557 {
    558 	int buflen, error;
    559 	struct file *fp;
    560 	char *start = where;
    561 
    562 	buflen = *sizep;
    563 	if (where == NULL) {
    564 		/*
    565 		 * overestimate by 10 files
    566 		 */
    567 		*sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
    568 		return (0);
    569 	}
    570 
    571 	/*
    572 	 * first copyout filehead
    573 	 */
    574 	if (buflen < sizeof(filehead)) {
    575 		*sizep = 0;
    576 		return (0);
    577 	}
    578 	error = copyout((caddr_t)&filehead, where, sizeof(filehead));
    579 	if (error)
    580 		return (error);
    581 	buflen -= sizeof(filehead);
    582 	where += sizeof(filehead);
    583 
    584 	/*
    585 	 * followed by an array of file structures
    586 	 */
    587 	for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
    588 		if (buflen < sizeof(struct file)) {
    589 			*sizep = where - start;
    590 			return (ENOMEM);
    591 		}
    592 		error = copyout((caddr_t)fp, where, sizeof (struct file));
    593 		if (error)
    594 			return (error);
    595 		buflen -= sizeof(struct file);
    596 		where += sizeof(struct file);
    597 	}
    598 	*sizep = where - start;
    599 	return (0);
    600 }
    601 
    602 /*
    603  * try over estimating by 5 procs
    604  */
    605 #define KERN_PROCSLOP	(5 * sizeof (struct kinfo_proc))
    606 
    607 int
    608 sysctl_doproc(name, namelen, where, sizep)
    609 	int *name;
    610 	u_int namelen;
    611 	char *where;
    612 	size_t *sizep;
    613 {
    614 	register struct proc *p;
    615 	register struct kinfo_proc *dp = (struct kinfo_proc *)where;
    616 	register int needed = 0;
    617 	int buflen = where != NULL ? *sizep : 0;
    618 	int doingzomb;
    619 	struct eproc eproc;
    620 	int error = 0;
    621 
    622 	if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
    623 		return (EINVAL);
    624 	p = allproc.lh_first;
    625 	doingzomb = 0;
    626 again:
    627 	for (; p != 0; p = p->p_list.le_next) {
    628 		/*
    629 		 * Skip embryonic processes.
    630 		 */
    631 		if (p->p_stat == SIDL)
    632 			continue;
    633 		/*
    634 		 * TODO - make more efficient (see notes below).
    635 		 * do by session.
    636 		 */
    637 		switch (name[0]) {
    638 
    639 		case KERN_PROC_PID:
    640 			/* could do this with just a lookup */
    641 			if (p->p_pid != (pid_t)name[1])
    642 				continue;
    643 			break;
    644 
    645 		case KERN_PROC_PGRP:
    646 			/* could do this by traversing pgrp */
    647 			if (p->p_pgrp->pg_id != (pid_t)name[1])
    648 				continue;
    649 			break;
    650 
    651 		case KERN_PROC_TTY:
    652 			if ((p->p_flag & P_CONTROLT) == 0 ||
    653 			    p->p_session->s_ttyp == NULL ||
    654 			    p->p_session->s_ttyp->t_dev != (dev_t)name[1])
    655 				continue;
    656 			break;
    657 
    658 		case KERN_PROC_UID:
    659 			if (p->p_ucred->cr_uid != (uid_t)name[1])
    660 				continue;
    661 			break;
    662 
    663 		case KERN_PROC_RUID:
    664 			if (p->p_cred->p_ruid != (uid_t)name[1])
    665 				continue;
    666 			break;
    667 		}
    668 		if (buflen >= sizeof(struct kinfo_proc)) {
    669 			fill_eproc(p, &eproc);
    670 			error = copyout((caddr_t)p, &dp->kp_proc,
    671 					sizeof(struct proc));
    672 			if (error)
    673 				return (error);
    674 			error = copyout((caddr_t)&eproc, &dp->kp_eproc,
    675 					sizeof(eproc));
    676 			if (error)
    677 				return (error);
    678 			dp++;
    679 			buflen -= sizeof(struct kinfo_proc);
    680 		}
    681 		needed += sizeof(struct kinfo_proc);
    682 	}
    683 	if (doingzomb == 0) {
    684 		p = zombproc.lh_first;
    685 		doingzomb++;
    686 		goto again;
    687 	}
    688 	if (where != NULL) {
    689 		*sizep = (caddr_t)dp - where;
    690 		if (needed > *sizep)
    691 			return (ENOMEM);
    692 	} else {
    693 		needed += KERN_PROCSLOP;
    694 		*sizep = needed;
    695 	}
    696 	return (0);
    697 }
    698 
    699 /*
    700  * Fill in an eproc structure for the specified process.
    701  */
    702 void
    703 fill_eproc(p, ep)
    704 	register struct proc *p;
    705 	register struct eproc *ep;
    706 {
    707 	register struct tty *tp;
    708 
    709 	ep->e_paddr = p;
    710 	ep->e_sess = p->p_pgrp->pg_session;
    711 	ep->e_pcred = *p->p_cred;
    712 	ep->e_ucred = *p->p_ucred;
    713 	if (p->p_stat == SIDL || p->p_stat == SZOMB) {
    714 		ep->e_vm.vm_rssize = 0;
    715 		ep->e_vm.vm_tsize = 0;
    716 		ep->e_vm.vm_dsize = 0;
    717 		ep->e_vm.vm_ssize = 0;
    718 		/* ep->e_vm.vm_pmap = XXX; */
    719 	} else {
    720 		register struct vmspace *vm = p->p_vmspace;
    721 
    722 #ifdef pmap_resident_count
    723 		ep->e_vm.vm_rssize = pmap_resident_count(&vm->vm_pmap); /*XXX*/
    724 #else
    725 		ep->e_vm.vm_rssize = vm->vm_rssize;
    726 #endif
    727 		ep->e_vm.vm_tsize = vm->vm_tsize;
    728 		ep->e_vm.vm_dsize = vm->vm_dsize;
    729 		ep->e_vm.vm_ssize = vm->vm_ssize;
    730 		ep->e_vm.vm_pmap = vm->vm_pmap;
    731 	}
    732 	if (p->p_pptr)
    733 		ep->e_ppid = p->p_pptr->p_pid;
    734 	else
    735 		ep->e_ppid = 0;
    736 	ep->e_pgid = p->p_pgrp->pg_id;
    737 	ep->e_jobc = p->p_pgrp->pg_jobc;
    738 	if ((p->p_flag & P_CONTROLT) &&
    739 	     (tp = ep->e_sess->s_ttyp)) {
    740 		ep->e_tdev = tp->t_dev;
    741 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
    742 		ep->e_tsess = tp->t_session;
    743 	} else
    744 		ep->e_tdev = NODEV;
    745 	ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
    746 	if (SESS_LEADER(p))
    747 		ep->e_flag |= EPROC_SLEADER;
    748 	if (p->p_wmesg)
    749 		strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
    750 	ep->e_xsize = ep->e_xrssize = 0;
    751 	ep->e_xccount = ep->e_xswrss = 0;
    752 }
    753 
    754