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