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