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