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