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