Home | History | Annotate | Line # | Download | only in kern
kern_sysctl.c revision 1.46
      1 /*	$NetBSD: kern_sysctl.c,v 1.46 1999/06/17 15:47:23 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.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_sysv.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 #include <uvm/uvm_extern.h>
     68 
     69 #include <sys/mount.h>
     70 #include <sys/syscallargs.h>
     71 
     72 
     73 #if defined(DDB)
     74 #include <ddb/ddbvar.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 		fn = uvm_sysctl;
    129 		break;
    130 	case CTL_NET:
    131 		fn = net_sysctl;
    132 		break;
    133 	case CTL_VFS:
    134 		fn = vfs_sysctl;
    135 		break;
    136 	case CTL_MACHDEP:
    137 		fn = cpu_sysctl;
    138 		break;
    139 #ifdef DEBUG
    140 	case CTL_DEBUG:
    141 		fn = debug_sysctl;
    142 		break;
    143 #endif
    144 #ifdef DDB
    145 	case CTL_DDB:
    146 		fn = ddb_sysctl;
    147 		break;
    148 #endif
    149 	default:
    150 		return (EOPNOTSUPP);
    151 	}
    152 
    153 	if (SCARG(uap, oldlenp) &&
    154 	    (error = copyin(SCARG(uap, oldlenp), &oldlen, sizeof(oldlen))))
    155 		return (error);
    156 	if (SCARG(uap, old) != NULL) {
    157 		if (!uvm_useracc(SCARG(uap, old), oldlen, B_WRITE))
    158 			return (EFAULT);
    159 		while (memlock.sl_lock) {
    160 			memlock.sl_want = 1;
    161 			sleep((caddr_t)&memlock, PRIBIO+1);
    162 			memlock.sl_locked++;
    163 		}
    164 		memlock.sl_lock = 1;
    165 		if (dolock) {
    166 			/*
    167 			 * XXX Um, this is kind of evil.  What should we
    168 			 * XXX be passing here?
    169 			 */
    170 			if (uvm_vslock(p, SCARG(uap, old), oldlen,
    171 			    VM_PROT_NONE) != KERN_SUCCESS) {
    172 				memlock.sl_lock = 0;
    173 				if (memlock.sl_want) {
    174 					memlock.sl_want = 0;
    175 					wakeup((caddr_t)&memlock);
    176 					return (EFAULT);
    177 				}
    178 			}
    179 		}
    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 			uvm_vsunlock(p, SCARG(uap, old), savelen);
    187 		memlock.sl_lock = 0;
    188 		if (memlock.sl_want) {
    189 			memlock.sl_want = 0;
    190 			wakeup((caddr_t)&memlock);
    191 		}
    192 	}
    193 	if (error)
    194 		return (error);
    195 	if (SCARG(uap, oldlenp))
    196 		error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen));
    197 	return (error);
    198 }
    199 
    200 /*
    201  * Attributes stored in the kernel.
    202  */
    203 char hostname[MAXHOSTNAMELEN];
    204 int hostnamelen;
    205 char domainname[MAXHOSTNAMELEN];
    206 int domainnamelen;
    207 long hostid;
    208 #ifdef INSECURE
    209 int securelevel = -1;
    210 #else
    211 int securelevel = 0;
    212 #endif
    213 #ifdef SHORTCORENAME
    214 int shortcorename = 1;
    215 #else
    216 int shortcorename = 0;
    217 #endif
    218 
    219 /*
    220  * kernel related system variables.
    221  */
    222 int
    223 kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    224 	int *name;
    225 	u_int namelen;
    226 	void *oldp;
    227 	size_t *oldlenp;
    228 	void *newp;
    229 	size_t newlen;
    230 	struct proc *p;
    231 {
    232 	int error, level, inthostid;
    233 	int old_autonicetime;
    234 	int old_vnodes;
    235 	int old_shortcorename;
    236 	extern char ostype[], osrelease[], version[];
    237 
    238 	/* All sysctl names at this level, except for a few, are terminal. */
    239 	switch (name[0]) {
    240 	case KERN_PROC:
    241 	case KERN_PROF:
    242 	case KERN_MBUF:
    243 		/* Not terminal. */
    244 		break;
    245 	default:
    246 		if (namelen != 1)
    247 			return (ENOTDIR);	/* overloaded */
    248 	}
    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 	case KERN_SYNCHRONIZED_IO:
    394 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    395 	case KERN_IOV_MAX:
    396 		return (sysctl_rdint(oldp, oldlenp, newp, IOV_MAX));
    397 	case KERN_MBUF:
    398 		return (sysctl_dombuf(name + 1, namelen - 1, oldp, oldlenp,
    399 		    newp, newlen));
    400 	default:
    401 		return (EOPNOTSUPP);
    402 	}
    403 	/* NOTREACHED */
    404 }
    405 
    406 /*
    407  * hardware related system variables.
    408  */
    409 int
    410 hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    411 	int *name;
    412 	u_int namelen;
    413 	void *oldp;
    414 	size_t *oldlenp;
    415 	void *newp;
    416 	size_t newlen;
    417 	struct proc *p;
    418 {
    419 	extern char machine[], machine_arch[], cpu_model[];
    420 
    421 	/* all sysctl names at this level are terminal */
    422 	if (namelen != 1)
    423 		return (ENOTDIR);		/* overloaded */
    424 
    425 	switch (name[0]) {
    426 	case HW_MACHINE:
    427 		return (sysctl_rdstring(oldp, oldlenp, newp, machine));
    428 	case HW_MACHINE_ARCH:
    429 		return (sysctl_rdstring(oldp, oldlenp, newp, machine_arch));
    430 	case HW_MODEL:
    431 		return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
    432 	case HW_NCPU:
    433 		return (sysctl_rdint(oldp, oldlenp, newp, 1));	/* XXX */
    434 	case HW_BYTEORDER:
    435 		return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
    436 	case HW_PHYSMEM:
    437 		return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
    438 	case HW_USERMEM:
    439 		return (sysctl_rdint(oldp, oldlenp, newp,
    440 		    ctob(physmem - uvmexp.wired)));
    441 	case HW_PAGESIZE:
    442 		return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
    443 	default:
    444 		return (EOPNOTSUPP);
    445 	}
    446 	/* NOTREACHED */
    447 }
    448 
    449 #ifdef DEBUG
    450 /*
    451  * Debugging related system variables.
    452  */
    453 struct ctldebug debug0, debug1, debug2, debug3, debug4;
    454 struct ctldebug debug5, debug6, debug7, debug8, debug9;
    455 struct ctldebug debug10, debug11, debug12, debug13, debug14;
    456 struct ctldebug debug15, debug16, debug17, debug18, debug19;
    457 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
    458 	&debug0, &debug1, &debug2, &debug3, &debug4,
    459 	&debug5, &debug6, &debug7, &debug8, &debug9,
    460 	&debug10, &debug11, &debug12, &debug13, &debug14,
    461 	&debug15, &debug16, &debug17, &debug18, &debug19,
    462 };
    463 int
    464 debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    465 	int *name;
    466 	u_int namelen;
    467 	void *oldp;
    468 	size_t *oldlenp;
    469 	void *newp;
    470 	size_t newlen;
    471 	struct proc *p;
    472 {
    473 	struct ctldebug *cdp;
    474 
    475 	/* all sysctl names at this level are name and field */
    476 	if (namelen != 2)
    477 		return (ENOTDIR);		/* overloaded */
    478 	cdp = debugvars[name[0]];
    479 	if (name[0] >= CTL_DEBUG_MAXID || cdp->debugname == 0)
    480 		return (EOPNOTSUPP);
    481 	switch (name[1]) {
    482 	case CTL_DEBUG_NAME:
    483 		return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
    484 	case CTL_DEBUG_VALUE:
    485 		return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
    486 	default:
    487 		return (EOPNOTSUPP);
    488 	}
    489 	/* NOTREACHED */
    490 }
    491 #endif /* DEBUG */
    492 
    493 /*
    494  * Validate parameters and get old / set new parameters
    495  * for an integer-valued sysctl function.
    496  */
    497 int
    498 sysctl_int(oldp, oldlenp, newp, newlen, valp)
    499 	void *oldp;
    500 	size_t *oldlenp;
    501 	void *newp;
    502 	size_t newlen;
    503 	int *valp;
    504 {
    505 	int error = 0;
    506 
    507 	if (oldp && *oldlenp < sizeof(int))
    508 		return (ENOMEM);
    509 	if (newp && newlen != sizeof(int))
    510 		return (EINVAL);
    511 	*oldlenp = sizeof(int);
    512 	if (oldp)
    513 		error = copyout(valp, oldp, sizeof(int));
    514 	if (error == 0 && newp)
    515 		error = copyin(newp, valp, sizeof(int));
    516 	return (error);
    517 }
    518 
    519 /*
    520  * As above, but read-only.
    521  */
    522 int
    523 sysctl_rdint(oldp, oldlenp, newp, val)
    524 	void *oldp;
    525 	size_t *oldlenp;
    526 	void *newp;
    527 	int val;
    528 {
    529 	int error = 0;
    530 
    531 	if (oldp && *oldlenp < sizeof(int))
    532 		return (ENOMEM);
    533 	if (newp)
    534 		return (EPERM);
    535 	*oldlenp = sizeof(int);
    536 	if (oldp)
    537 		error = copyout((caddr_t)&val, oldp, sizeof(int));
    538 	return (error);
    539 }
    540 
    541 /*
    542  * Validate parameters and get old / set new parameters
    543  * for a string-valued sysctl function.
    544  */
    545 int
    546 sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen)
    547 	void *oldp;
    548 	size_t *oldlenp;
    549 	void *newp;
    550 	size_t newlen;
    551 	char *str;
    552 	int maxlen;
    553 {
    554 	int len, error = 0;
    555 
    556 	len = strlen(str) + 1;
    557 	if (oldp && *oldlenp < len)
    558 		return (ENOMEM);
    559 	if (newp && newlen >= maxlen)
    560 		return (EINVAL);
    561 	if (oldp) {
    562 		*oldlenp = len;
    563 		error = copyout(str, oldp, len);
    564 	}
    565 	if (error == 0 && newp) {
    566 		error = copyin(newp, str, newlen);
    567 		str[newlen] = 0;
    568 	}
    569 	return (error);
    570 }
    571 
    572 /*
    573  * As above, but read-only.
    574  */
    575 int
    576 sysctl_rdstring(oldp, oldlenp, newp, str)
    577 	void *oldp;
    578 	size_t *oldlenp;
    579 	void *newp;
    580 	char *str;
    581 {
    582 	int len, error = 0;
    583 
    584 	len = strlen(str) + 1;
    585 	if (oldp && *oldlenp < len)
    586 		return (ENOMEM);
    587 	if (newp)
    588 		return (EPERM);
    589 	*oldlenp = len;
    590 	if (oldp)
    591 		error = copyout(str, oldp, len);
    592 	return (error);
    593 }
    594 
    595 /*
    596  * Validate parameters and get old / set new parameters
    597  * for a structure oriented sysctl function.
    598  */
    599 int
    600 sysctl_struct(oldp, oldlenp, newp, newlen, sp, len)
    601 	void *oldp;
    602 	size_t *oldlenp;
    603 	void *newp;
    604 	size_t newlen;
    605 	void *sp;
    606 	int len;
    607 {
    608 	int error = 0;
    609 
    610 	if (oldp && *oldlenp < len)
    611 		return (ENOMEM);
    612 	if (newp && newlen > len)
    613 		return (EINVAL);
    614 	if (oldp) {
    615 		*oldlenp = len;
    616 		error = copyout(sp, oldp, len);
    617 	}
    618 	if (error == 0 && newp)
    619 		error = copyin(newp, sp, len);
    620 	return (error);
    621 }
    622 
    623 /*
    624  * Validate parameters and get old parameters
    625  * for a structure oriented sysctl function.
    626  */
    627 int
    628 sysctl_rdstruct(oldp, oldlenp, newp, sp, len)
    629 	void *oldp;
    630 	size_t *oldlenp;
    631 	void *newp, *sp;
    632 	int len;
    633 {
    634 	int error = 0;
    635 
    636 	if (oldp && *oldlenp < len)
    637 		return (ENOMEM);
    638 	if (newp)
    639 		return (EPERM);
    640 	*oldlenp = len;
    641 	if (oldp)
    642 		error = copyout(sp, oldp, len);
    643 	return (error);
    644 }
    645 
    646 /*
    647  * Get file structures.
    648  */
    649 int
    650 sysctl_file(where, sizep)
    651 	char *where;
    652 	size_t *sizep;
    653 {
    654 	int buflen, error;
    655 	struct file *fp;
    656 	char *start = where;
    657 
    658 	buflen = *sizep;
    659 	if (where == NULL) {
    660 		/*
    661 		 * overestimate by 10 files
    662 		 */
    663 		*sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
    664 		return (0);
    665 	}
    666 
    667 	/*
    668 	 * first copyout filehead
    669 	 */
    670 	if (buflen < sizeof(filehead)) {
    671 		*sizep = 0;
    672 		return (0);
    673 	}
    674 	error = copyout((caddr_t)&filehead, where, sizeof(filehead));
    675 	if (error)
    676 		return (error);
    677 	buflen -= sizeof(filehead);
    678 	where += sizeof(filehead);
    679 
    680 	/*
    681 	 * followed by an array of file structures
    682 	 */
    683 	for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
    684 		if (buflen < sizeof(struct file)) {
    685 			*sizep = where - start;
    686 			return (ENOMEM);
    687 		}
    688 		error = copyout((caddr_t)fp, where, sizeof(struct file));
    689 		if (error)
    690 			return (error);
    691 		buflen -= sizeof(struct file);
    692 		where += sizeof(struct file);
    693 	}
    694 	*sizep = where - start;
    695 	return (0);
    696 }
    697 
    698 /*
    699  * try over estimating by 5 procs
    700  */
    701 #define KERN_PROCSLOP	(5 * sizeof(struct kinfo_proc))
    702 
    703 int
    704 sysctl_doproc(name, namelen, where, sizep)
    705 	int *name;
    706 	u_int namelen;
    707 	char *where;
    708 	size_t *sizep;
    709 {
    710 	register struct proc *p;
    711 	register struct kinfo_proc *dp = (struct kinfo_proc *)where;
    712 	register int needed = 0;
    713 	int buflen = where != NULL ? *sizep : 0;
    714 	const struct proclist_desc *pd;
    715 	struct eproc eproc;
    716 	int error = 0;
    717 
    718 	if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
    719 		return (EINVAL);
    720 
    721 	pd = proclists;
    722 again:
    723 	for (p = LIST_FIRST(pd->pd_list); p != NULL;
    724 	     p = LIST_NEXT(p, p_list)) {
    725 		/*
    726 		 * Skip embryonic processes.
    727 		 */
    728 		if (p->p_stat == SIDL)
    729 			continue;
    730 		/*
    731 		 * TODO - make more efficient (see notes below).
    732 		 * do by session.
    733 		 */
    734 		switch (name[0]) {
    735 
    736 		case KERN_PROC_PID:
    737 			/* could do this with just a lookup */
    738 			if (p->p_pid != (pid_t)name[1])
    739 				continue;
    740 			break;
    741 
    742 		case KERN_PROC_PGRP:
    743 			/* could do this by traversing pgrp */
    744 			if (p->p_pgrp->pg_id != (pid_t)name[1])
    745 				continue;
    746 			break;
    747 
    748 		case KERN_PROC_TTY:
    749 			if ((p->p_flag & P_CONTROLT) == 0 ||
    750 			    p->p_session->s_ttyp == NULL ||
    751 			    p->p_session->s_ttyp->t_dev != (dev_t)name[1])
    752 				continue;
    753 			break;
    754 
    755 		case KERN_PROC_UID:
    756 			if (p->p_ucred->cr_uid != (uid_t)name[1])
    757 				continue;
    758 			break;
    759 
    760 		case KERN_PROC_RUID:
    761 			if (p->p_cred->p_ruid != (uid_t)name[1])
    762 				continue;
    763 			break;
    764 		}
    765 		if (buflen >= sizeof(struct kinfo_proc)) {
    766 			fill_eproc(p, &eproc);
    767 			error = copyout((caddr_t)p, &dp->kp_proc,
    768 					sizeof(struct proc));
    769 			if (error)
    770 				return (error);
    771 			error = copyout((caddr_t)&eproc, &dp->kp_eproc,
    772 					sizeof(eproc));
    773 			if (error)
    774 				return (error);
    775 			dp++;
    776 			buflen -= sizeof(struct kinfo_proc);
    777 		}
    778 		needed += sizeof(struct kinfo_proc);
    779 	}
    780 	pd++;
    781 	if (pd->pd_list != NULL)
    782 		goto again;
    783 
    784 	if (where != NULL) {
    785 		*sizep = (caddr_t)dp - where;
    786 		if (needed > *sizep)
    787 			return (ENOMEM);
    788 	} else {
    789 		needed += KERN_PROCSLOP;
    790 		*sizep = needed;
    791 	}
    792 	return (0);
    793 }
    794 
    795 /*
    796  * Fill in an eproc structure for the specified process.
    797  */
    798 void
    799 fill_eproc(p, ep)
    800 	register struct proc *p;
    801 	register struct eproc *ep;
    802 {
    803 	register struct tty *tp;
    804 
    805 	ep->e_paddr = p;
    806 	ep->e_sess = p->p_pgrp->pg_session;
    807 	ep->e_pcred = *p->p_cred;
    808 	ep->e_ucred = *p->p_ucred;
    809 	if (p->p_stat == SIDL || p->p_stat == SZOMB) {
    810 		ep->e_vm.vm_rssize = 0;
    811 		ep->e_vm.vm_tsize = 0;
    812 		ep->e_vm.vm_dsize = 0;
    813 		ep->e_vm.vm_ssize = 0;
    814 		/* ep->e_vm.vm_pmap = XXX; */
    815 	} else {
    816 		register struct vmspace *vm = p->p_vmspace;
    817 
    818 		ep->e_vm.vm_rssize = vm_resident_count(vm);
    819 		ep->e_vm.vm_tsize = vm->vm_tsize;
    820 		ep->e_vm.vm_dsize = vm->vm_dsize;
    821 		ep->e_vm.vm_ssize = vm->vm_ssize;
    822 	}
    823 	if (p->p_pptr)
    824 		ep->e_ppid = p->p_pptr->p_pid;
    825 	else
    826 		ep->e_ppid = 0;
    827 	ep->e_pgid = p->p_pgrp->pg_id;
    828 	ep->e_sid = ep->e_sess->s_sid;
    829 	ep->e_jobc = p->p_pgrp->pg_jobc;
    830 	if ((p->p_flag & P_CONTROLT) &&
    831 	     (tp = ep->e_sess->s_ttyp)) {
    832 		ep->e_tdev = tp->t_dev;
    833 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
    834 		ep->e_tsess = tp->t_session;
    835 	} else
    836 		ep->e_tdev = NODEV;
    837 	if (p->p_wmesg)
    838 		strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
    839 	ep->e_xsize = ep->e_xrssize = 0;
    840 	ep->e_xccount = ep->e_xswrss = 0;
    841 	ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
    842 	if (SESS_LEADER(p))
    843 		ep->e_flag |= EPROC_SLEADER;
    844 	strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
    845 }
    846