Home | History | Annotate | Line # | Download | only in kern
kern_sysctl.c revision 1.43
      1 /*	$NetBSD: kern_sysctl.c,v 1.43 1999/03/24 05:51:25 mrg 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 			uvm_vslock(p, SCARG(uap, old), oldlen);
    167 		savelen = oldlen;
    168 	}
    169 	error = (*fn)(name + 1, SCARG(uap, namelen) - 1, SCARG(uap, old),
    170 	    &oldlen, SCARG(uap, new), SCARG(uap, newlen), p);
    171 	if (SCARG(uap, old) != NULL) {
    172 		if (dolock)
    173 			uvm_vsunlock(p, SCARG(uap, old), savelen);
    174 		memlock.sl_lock = 0;
    175 		if (memlock.sl_want) {
    176 			memlock.sl_want = 0;
    177 			wakeup((caddr_t)&memlock);
    178 		}
    179 	}
    180 	if (error)
    181 		return (error);
    182 	if (SCARG(uap, oldlenp))
    183 		error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen));
    184 	return (error);
    185 }
    186 
    187 /*
    188  * Attributes stored in the kernel.
    189  */
    190 char hostname[MAXHOSTNAMELEN];
    191 int hostnamelen;
    192 char domainname[MAXHOSTNAMELEN];
    193 int domainnamelen;
    194 long hostid;
    195 #ifdef INSECURE
    196 int securelevel = -1;
    197 #else
    198 int securelevel = 0;
    199 #endif
    200 #ifdef SHORTCORENAME
    201 int shortcorename = 1;
    202 #else
    203 int shortcorename = 0;
    204 #endif
    205 
    206 /*
    207  * kernel related system variables.
    208  */
    209 int
    210 kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    211 	int *name;
    212 	u_int namelen;
    213 	void *oldp;
    214 	size_t *oldlenp;
    215 	void *newp;
    216 	size_t newlen;
    217 	struct proc *p;
    218 {
    219 	int error, level, inthostid;
    220 	int old_autonicetime;
    221 	int old_vnodes;
    222 	int old_shortcorename;
    223 	extern char ostype[], osrelease[], version[];
    224 
    225 	/* all sysctl names at this level are terminal */
    226 	if (namelen != 1 && !(name[0] == KERN_PROC || name[0] == KERN_PROF))
    227 		return (ENOTDIR);		/* overloaded */
    228 
    229 	switch (name[0]) {
    230 	case KERN_OSTYPE:
    231 		return (sysctl_rdstring(oldp, oldlenp, newp, ostype));
    232 	case KERN_OSRELEASE:
    233 		return (sysctl_rdstring(oldp, oldlenp, newp, osrelease));
    234 	case KERN_OSREV:
    235 		return (sysctl_rdint(oldp, oldlenp, newp, NetBSD));
    236 	case KERN_VERSION:
    237 		return (sysctl_rdstring(oldp, oldlenp, newp, version));
    238 	case KERN_MAXVNODES:
    239 		old_vnodes = desiredvnodes;
    240 		error = sysctl_int(oldp, oldlenp, newp, newlen, &desiredvnodes);
    241 		if (old_vnodes > desiredvnodes) {
    242 		        desiredvnodes = old_vnodes;
    243 			return (EINVAL);
    244 		}
    245 		return (error);
    246 	case KERN_MAXPROC:
    247 		return (sysctl_int(oldp, oldlenp, newp, newlen, &maxproc));
    248 	case KERN_MAXFILES:
    249 		return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles));
    250 	case KERN_ARGMAX:
    251 		return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX));
    252 	case KERN_SECURELVL:
    253 		level = securelevel;
    254 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) ||
    255 		    newp == NULL)
    256 			return (error);
    257 		if (level < securelevel && p->p_pid != 1)
    258 			return (EPERM);
    259 		securelevel = level;
    260 		return (0);
    261 	case KERN_HOSTNAME:
    262 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    263 		    hostname, sizeof(hostname));
    264 		if (newp && !error)
    265 			hostnamelen = newlen;
    266 		return (error);
    267 	case KERN_DOMAINNAME:
    268 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    269 		    domainname, sizeof(domainname));
    270 		if (newp && !error)
    271 			domainnamelen = newlen;
    272 		return (error);
    273 	case KERN_HOSTID:
    274 		inthostid = hostid;  /* XXX assumes sizeof long <= sizeof int */
    275 		error =  sysctl_int(oldp, oldlenp, newp, newlen, &inthostid);
    276 		hostid = inthostid;
    277 		return (error);
    278 	case KERN_CLOCKRATE:
    279 		return (sysctl_clockrate(oldp, oldlenp));
    280 	case KERN_BOOTTIME:
    281 		return (sysctl_rdstruct(oldp, oldlenp, newp, &boottime,
    282 		    sizeof(struct timeval)));
    283 	case KERN_VNODE:
    284 		return (sysctl_vnode(oldp, oldlenp, p));
    285 	case KERN_PROC:
    286 		return (sysctl_doproc(name + 1, namelen - 1, oldp, oldlenp));
    287 	case KERN_FILE:
    288 		return (sysctl_file(oldp, oldlenp));
    289 #ifdef GPROF
    290 	case KERN_PROF:
    291 		return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
    292 		    newp, newlen));
    293 #endif
    294 	case KERN_POSIX1:
    295 		return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION));
    296 	case KERN_NGROUPS:
    297 		return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX));
    298 	case KERN_JOB_CONTROL:
    299 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    300 	case KERN_SAVED_IDS:
    301 #ifdef _POSIX_SAVED_IDS
    302 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    303 #else
    304 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    305 #endif
    306 	case KERN_MAXPARTITIONS:
    307 		return (sysctl_rdint(oldp, oldlenp, newp, MAXPARTITIONS));
    308 	case KERN_RAWPARTITION:
    309 		return (sysctl_rdint(oldp, oldlenp, newp, RAW_PART));
    310 #ifdef NTP
    311 	case KERN_NTPTIME:
    312 		return (sysctl_ntptime(oldp, oldlenp));
    313 #endif
    314 	case KERN_AUTONICETIME:
    315 	        old_autonicetime = autonicetime;
    316 	        error = sysctl_int(oldp, oldlenp, newp, newlen, &autonicetime);
    317 		if (autonicetime < 0)
    318  		        autonicetime = old_autonicetime;
    319 		return (error);
    320 	case KERN_AUTONICEVAL:
    321 		error = sysctl_int(oldp, oldlenp, newp, newlen, &autoniceval);
    322 		if (autoniceval < PRIO_MIN)
    323 			autoniceval = PRIO_MIN;
    324 		if (autoniceval > PRIO_MAX)
    325 			autoniceval = PRIO_MAX;
    326 		return (error);
    327 	case KERN_RTC_OFFSET:
    328 		return (sysctl_rdint(oldp, oldlenp, newp, rtc_offset));
    329 	case KERN_ROOT_DEVICE:
    330 		return (sysctl_rdstring(oldp, oldlenp, newp,
    331 		    root_device->dv_xname));
    332 	case KERN_MSGBUFSIZE:
    333 		/*
    334 		 * deal with cases where the message buffer has
    335 		 * become corrupted.
    336 		 */
    337 		if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
    338 			msgbufenabled = 0;
    339 			return (ENXIO);
    340 		}
    341 		return (sysctl_rdint(oldp, oldlenp, newp, msgbufp->msg_bufs));
    342 	case KERN_FSYNC:
    343 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    344 	case KERN_SYSVMSG:
    345 #ifdef SYSVMSG
    346 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    347 #else
    348 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    349 #endif
    350 	case KERN_SYSVSEM:
    351 #ifdef SYSVSEM
    352 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    353 #else
    354 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    355 #endif
    356 	case KERN_SYSVSHM:
    357 #ifdef SYSVSHM
    358 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    359 #else
    360 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    361 #endif
    362  	case KERN_SHORTCORENAME:
    363  		/* Only allow values of zero or one. */
    364  		old_shortcorename = shortcorename;
    365  		error = sysctl_int(oldp, oldlenp, newp, newlen,
    366  		    &shortcorename);
    367  		if (shortcorename != 0 && shortcorename != 1) {
    368  			shortcorename = old_shortcorename;
    369  			return (EINVAL);
    370  		}
    371  		return (error);
    372 	case KERN_SYNCHRONIZED_IO:
    373 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    374 	case KERN_IOV_MAX:
    375 		return (sysctl_rdint(oldp, oldlenp, newp, IOV_MAX));
    376 	default:
    377 		return (EOPNOTSUPP);
    378 	}
    379 	/* NOTREACHED */
    380 }
    381 
    382 /*
    383  * hardware related system variables.
    384  */
    385 int
    386 hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    387 	int *name;
    388 	u_int namelen;
    389 	void *oldp;
    390 	size_t *oldlenp;
    391 	void *newp;
    392 	size_t newlen;
    393 	struct proc *p;
    394 {
    395 	extern char machine[], machine_arch[], cpu_model[];
    396 
    397 	/* all sysctl names at this level are terminal */
    398 	if (namelen != 1)
    399 		return (ENOTDIR);		/* overloaded */
    400 
    401 	switch (name[0]) {
    402 	case HW_MACHINE:
    403 		return (sysctl_rdstring(oldp, oldlenp, newp, machine));
    404 	case HW_MACHINE_ARCH:
    405 		return (sysctl_rdstring(oldp, oldlenp, newp, machine_arch));
    406 	case HW_MODEL:
    407 		return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
    408 	case HW_NCPU:
    409 		return (sysctl_rdint(oldp, oldlenp, newp, 1));	/* XXX */
    410 	case HW_BYTEORDER:
    411 		return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
    412 	case HW_PHYSMEM:
    413 		return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
    414 	case HW_USERMEM:
    415 		return (sysctl_rdint(oldp, oldlenp, newp,
    416 		    ctob(physmem - uvmexp.wired)));
    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 	const struct proclist_desc *pd;
    691 	struct eproc eproc;
    692 	int error = 0;
    693 
    694 	if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
    695 		return (EINVAL);
    696 
    697 	pd = proclists;
    698 again:
    699 	for (p = LIST_FIRST(pd->pd_list); p != NULL;
    700 	     p = LIST_NEXT(p, p_list)) {
    701 		/*
    702 		 * Skip embryonic processes.
    703 		 */
    704 		if (p->p_stat == SIDL)
    705 			continue;
    706 		/*
    707 		 * TODO - make more efficient (see notes below).
    708 		 * do by session.
    709 		 */
    710 		switch (name[0]) {
    711 
    712 		case KERN_PROC_PID:
    713 			/* could do this with just a lookup */
    714 			if (p->p_pid != (pid_t)name[1])
    715 				continue;
    716 			break;
    717 
    718 		case KERN_PROC_PGRP:
    719 			/* could do this by traversing pgrp */
    720 			if (p->p_pgrp->pg_id != (pid_t)name[1])
    721 				continue;
    722 			break;
    723 
    724 		case KERN_PROC_TTY:
    725 			if ((p->p_flag & P_CONTROLT) == 0 ||
    726 			    p->p_session->s_ttyp == NULL ||
    727 			    p->p_session->s_ttyp->t_dev != (dev_t)name[1])
    728 				continue;
    729 			break;
    730 
    731 		case KERN_PROC_UID:
    732 			if (p->p_ucred->cr_uid != (uid_t)name[1])
    733 				continue;
    734 			break;
    735 
    736 		case KERN_PROC_RUID:
    737 			if (p->p_cred->p_ruid != (uid_t)name[1])
    738 				continue;
    739 			break;
    740 		}
    741 		if (buflen >= sizeof(struct kinfo_proc)) {
    742 			fill_eproc(p, &eproc);
    743 			error = copyout((caddr_t)p, &dp->kp_proc,
    744 					sizeof(struct proc));
    745 			if (error)
    746 				return (error);
    747 			error = copyout((caddr_t)&eproc, &dp->kp_eproc,
    748 					sizeof(eproc));
    749 			if (error)
    750 				return (error);
    751 			dp++;
    752 			buflen -= sizeof(struct kinfo_proc);
    753 		}
    754 		needed += sizeof(struct kinfo_proc);
    755 	}
    756 	pd++;
    757 	if (pd->pd_list != NULL)
    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