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