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