Home | History | Annotate | Line # | Download | only in kern
kern_sysctl.c revision 1.150
      1 /*	$NetBSD: kern_sysctl.c,v 1.150 2003/10/19 01:44:49 simonb 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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)kern_sysctl.c	8.9 (Berkeley) 5/20/95
     35  */
     36 
     37 /*
     38  * sysctl system call.
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.150 2003/10/19 01:44:49 simonb Exp $");
     43 
     44 #include "opt_ddb.h"
     45 #include "opt_insecure.h"
     46 #include "opt_defcorename.h"
     47 #include "opt_multiprocessor.h"
     48 #include "opt_pipe.h"
     49 #include "opt_posix.h"
     50 #include "opt_sysv.h"
     51 #include "pty.h"
     52 #include "rnd.h"
     53 
     54 #include <sys/param.h>
     55 #include <sys/systm.h>
     56 #include <sys/kernel.h>
     57 #include <sys/buf.h>
     58 #include <sys/device.h>
     59 #include <sys/disklabel.h>
     60 #include <sys/dkstat.h>
     61 #include <sys/exec.h>
     62 #include <sys/file.h>
     63 #include <sys/ioctl.h>
     64 #include <sys/malloc.h>
     65 #include <sys/mount.h>
     66 #include <sys/msgbuf.h>
     67 #include <sys/pool.h>
     68 #include <sys/proc.h>
     69 #include <sys/resource.h>
     70 #include <sys/resourcevar.h>
     71 #include <sys/sa.h>
     72 #include <sys/syscallargs.h>
     73 #include <sys/tty.h>
     74 #include <sys/unistd.h>
     75 #include <sys/vnode.h>
     76 #include <sys/socketvar.h>
     77 #define	__SYSCTL_PRIVATE
     78 #include <sys/sysctl.h>
     79 #include <sys/lock.h>
     80 #include <sys/namei.h>
     81 #include <sys/conf.h>
     82 
     83 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
     84 #include <sys/ipc.h>
     85 #endif
     86 #ifdef SYSVMSG
     87 #include <sys/msg.h>
     88 #endif
     89 #ifdef SYSVSEM
     90 #include <sys/sem.h>
     91 #endif
     92 #ifdef SYSVSHM
     93 #include <sys/shm.h>
     94 #endif
     95 
     96 #include <dev/cons.h>
     97 
     98 #if defined(DDB)
     99 #include <ddb/ddbvar.h>
    100 #endif
    101 
    102 #ifndef PIPE_SOCKETPAIR
    103 #include <sys/pipe.h>
    104 #endif
    105 
    106 #if NRND > 0
    107 #include <sys/rnd.h>
    108 #endif
    109 
    110 #define PTRTOINT64(foo)	((u_int64_t)(uintptr_t)(foo))
    111 
    112 static int sysctl_file(void *, size_t *);
    113 static int sysctl_drivers(void *, size_t *);
    114 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
    115 static int sysctl_sysvipc(int *, u_int, void *, size_t *);
    116 #endif
    117 static int sysctl_msgbuf(void *, size_t *);
    118 static int sysctl_doeproc(int *, u_int, void *, size_t *);
    119 static int sysctl_dolwp(int *, u_int, void *, size_t *);
    120 static int sysctl_dotkstat(int *, u_int, void *, size_t *, void *);
    121 #ifdef MULTIPROCESSOR
    122 static int sysctl_docptime(void *, size_t *, void *);
    123 static int sysctl_ncpus(void);
    124 #endif
    125 static void fill_kproc2(struct proc *, struct kinfo_proc2 *);
    126 static void fill_lwp(struct lwp *, struct kinfo_lwp *);
    127 static int sysctl_procargs(int *, u_int, void *, size_t *, struct proc *);
    128 #if NPTY > 0
    129 static int sysctl_pty(void *, size_t *, void *, size_t);
    130 #endif
    131 
    132 /*
    133  * The `sysctl_memlock' is intended to keep too many processes from
    134  * locking down memory by doing sysctls at once.  Whether or not this
    135  * is really a good idea to worry about it probably a subject of some
    136  * debate.
    137  */
    138 struct lock sysctl_memlock;
    139 
    140 void
    141 sysctl_init(void)
    142 {
    143 
    144 	lockinit(&sysctl_memlock, PRIBIO|PCATCH, "sysctl", 0, 0);
    145 }
    146 
    147 int
    148 sys___sysctl(struct lwp *l, void *v, register_t *retval)
    149 {
    150 	struct sys___sysctl_args /* {
    151 		syscallarg(int *) name;
    152 		syscallarg(u_int) namelen;
    153 		syscallarg(void *) old;
    154 		syscallarg(size_t *) oldlenp;
    155 		syscallarg(void *) new;
    156 		syscallarg(size_t) newlen;
    157 	} */ *uap = v;
    158 	struct proc *p = l->l_proc;
    159 	int error;
    160 	size_t savelen = 0, oldlen = 0;
    161 	sysctlfn *fn;
    162 	int name[CTL_MAXNAME];
    163 
    164 	/*
    165 	 * all top-level sysctl names are non-terminal
    166 	 */
    167 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
    168 		return (EINVAL);
    169 	error = copyin(SCARG(uap, name), &name,
    170 	    SCARG(uap, namelen) * sizeof(int));
    171 	if (error)
    172 		return (error);
    173 
    174 	/*
    175 	 * For all but CTL_PROC, must be root to change a value.
    176 	 * For CTL_PROC, must be root, or owner of the proc (and not suid),
    177 	 * this is checked in proc_sysctl() (once we know the targer proc).
    178 	 */
    179 	if (SCARG(uap, new) != NULL && name[0] != CTL_PROC &&
    180 	    (error = suser(p->p_ucred, &p->p_acflag)))
    181 		return (error);
    182 
    183 	switch (name[0]) {
    184 	case CTL_KERN:
    185 		fn = kern_sysctl;
    186 		break;
    187 	case CTL_HW:
    188 		fn = hw_sysctl;
    189 		break;
    190 	case CTL_VM:
    191 		fn = uvm_sysctl;
    192 		break;
    193 	case CTL_NET:
    194 		fn = net_sysctl;
    195 		break;
    196 	case CTL_VFS:
    197 		fn = vfs_sysctl;
    198 		break;
    199 	case CTL_MACHDEP:
    200 		fn = cpu_sysctl;
    201 		break;
    202 #ifdef DEBUG
    203 	case CTL_DEBUG:
    204 		fn = debug_sysctl;
    205 		break;
    206 #endif
    207 #ifdef DDB
    208 	case CTL_DDB:
    209 		fn = ddb_sysctl;
    210 		break;
    211 #endif
    212 	case CTL_PROC:
    213 		fn = proc_sysctl;
    214 		break;
    215 
    216 	case CTL_EMUL:
    217 		fn = emul_sysctl;
    218 		break;
    219 	default:
    220 		return (EOPNOTSUPP);
    221 	}
    222 
    223 	/*
    224 	 * XXX Hey, we wire `old', but what about `new'?
    225 	 */
    226 
    227 	if (SCARG(uap, oldlenp)) {
    228 		if ((error = copyin(SCARG(uap, oldlenp), &oldlen,
    229 		    sizeof(oldlen))))
    230 			return (error);
    231 	}
    232 	if (SCARG(uap, old) != NULL) {
    233 		error = lockmgr(&sysctl_memlock, LK_EXCLUSIVE, NULL);
    234 		if (error)
    235 			return (error);
    236 		error = uvm_vslock(p, SCARG(uap, old), oldlen, VM_PROT_WRITE);
    237 		if (error) {
    238 			(void) lockmgr(&sysctl_memlock, LK_RELEASE, NULL);
    239 			return (error);
    240 		}
    241 		savelen = oldlen;
    242 	}
    243 	error = (*fn)(name + 1, SCARG(uap, namelen) - 1, SCARG(uap, old),
    244 	    &oldlen, SCARG(uap, new), SCARG(uap, newlen), p);
    245 	if (SCARG(uap, old) != NULL) {
    246 		uvm_vsunlock(p, SCARG(uap, old), savelen);
    247 		(void) lockmgr(&sysctl_memlock, LK_RELEASE, NULL);
    248 	}
    249 	if (error)
    250 		return (error);
    251 	if (SCARG(uap, oldlenp))
    252 		error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen));
    253 	return (error);
    254 }
    255 
    256 /*
    257  * Attributes stored in the kernel.
    258  */
    259 char hostname[MAXHOSTNAMELEN];
    260 int hostnamelen;
    261 
    262 char domainname[MAXHOSTNAMELEN];
    263 int domainnamelen;
    264 
    265 long hostid;
    266 
    267 #ifdef INSECURE
    268 int securelevel = -1;
    269 #else
    270 int securelevel = 0;
    271 #endif
    272 
    273 #ifndef DEFCORENAME
    274 #define	DEFCORENAME	"%n.core"
    275 #endif
    276 char defcorename[MAXPATHLEN] = DEFCORENAME;
    277 
    278 extern	int	kern_logsigexit;
    279 extern	fixpt_t	ccpu;
    280 extern	int	forkfsleep;
    281 extern	int	dumponpanic;
    282 
    283 #ifndef MULTIPROCESSOR
    284 #define sysctl_ncpus() 1
    285 #endif
    286 
    287 #ifdef MULTIPROCESSOR
    288 
    289 #ifndef CPU_INFO_FOREACH
    290 #define CPU_INFO_ITERATOR int
    291 #define CPU_INFO_FOREACH(cii, ci) cii = 0, ci = curcpu(); ci != NULL; ci = NULL
    292 #endif
    293 
    294 static int
    295 sysctl_docptime(void *oldp, size_t *oldlenp, void *newp)
    296 {
    297 	u_int64_t cp_time[CPUSTATES];
    298 	struct cpu_info *ci;
    299 	CPU_INFO_ITERATOR cii;
    300 	int ncpus = sysctl_ncpus();
    301 
    302 	if (*oldlenp == sizeof(cp_time)) {
    303 		size_t i;
    304 
    305 		for (i = 0; i < CPUSTATES; i++)
    306 			cp_time[i] = 0;
    307 
    308 		for (CPU_INFO_FOREACH(cii, ci)) {
    309 			for (i = 0; i < CPUSTATES; i++)
    310 				cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
    311 		}
    312 		return sysctl_rdstruct(oldp, oldlenp, newp,
    313 		    cp_time, sizeof(cp_time));
    314 	} else if (*oldlenp == sizeof(cp_time) * ncpus) {
    315 		int error;
    316 
    317 		for (CPU_INFO_FOREACH(cii, ci)) {
    318 			if ((error = sysctl_rdstruct(oldp, oldlenp, newp,
    319 			    ci->ci_schedstate.spc_cp_time,
    320 			    sizeof(ci->ci_schedstate.spc_cp_time))) != 0)
    321 				return error;
    322 			oldp = ((char *)oldp) + sizeof(cp_time);
    323 		}
    324 		*oldlenp = ncpus * sizeof(cp_time);
    325 		return 0;
    326 	} else
    327 		return EINVAL;
    328 }
    329 
    330 static int
    331 sysctl_ncpus(void)
    332 {
    333 	struct cpu_info *ci;
    334 	CPU_INFO_ITERATOR cii;
    335 
    336 	int ncpus = 0;
    337 	for (CPU_INFO_FOREACH(cii, ci))
    338 		ncpus++;
    339 	return (ncpus);
    340 }
    341 
    342 #endif
    343 
    344 /*
    345  * kernel related system variables.
    346  */
    347 int
    348 kern_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    349     void *newp, size_t newlen, struct proc *p)
    350 {
    351 	int error, level, inthostid;
    352 	int old_autonicetime;
    353 	int old_vnodes;
    354 	dev_t consdev;
    355 #if NRND > 0
    356 	int v;
    357 #endif
    358 
    359 	/* All sysctl names at this level, except for a few, are terminal. */
    360 	switch (name[0]) {
    361 	case KERN_PROC:
    362 	case KERN_PROC2:
    363 	case KERN_LWP:
    364 	case KERN_PROF:
    365 	case KERN_MBUF:
    366 	case KERN_PROC_ARGS:
    367 	case KERN_SYSVIPC_INFO:
    368 	case KERN_PIPE:
    369 	case KERN_TKSTAT:
    370 		/* Not terminal. */
    371 		break;
    372 	default:
    373 		if (namelen != 1)
    374 			return (ENOTDIR);	/* overloaded */
    375 	}
    376 
    377 	switch (name[0]) {
    378 	case KERN_OSTYPE:
    379 		return (sysctl_rdstring(oldp, oldlenp, newp, ostype));
    380 	case KERN_OSRELEASE:
    381 		return (sysctl_rdstring(oldp, oldlenp, newp, osrelease));
    382 	case KERN_OSREV:
    383 		return (sysctl_rdint(oldp, oldlenp, newp, __NetBSD_Version__));
    384 	case KERN_VERSION:
    385 		return (sysctl_rdstring(oldp, oldlenp, newp, version));
    386 	case KERN_MAXVNODES:
    387 		old_vnodes = desiredvnodes;
    388 		error = sysctl_int(oldp, oldlenp, newp, newlen, &desiredvnodes);
    389 		if (newp && !error) {
    390 			if (old_vnodes > desiredvnodes) {
    391 				error = vfs_drainvnodes(desiredvnodes, p);
    392 				if (error) {
    393 					desiredvnodes = old_vnodes;
    394 					return error;
    395 				}
    396 			}
    397 			vfs_reinit();
    398 			nchreinit();
    399 		}
    400 		return (error);
    401 	case KERN_MAXPROC:
    402 	    {
    403 		int nmaxproc = maxproc;
    404 
    405 		error = sysctl_int(oldp, oldlenp, newp, newlen, &nmaxproc);
    406 
    407 		if (!error && newp) {
    408 			if (nmaxproc < 0 || nmaxproc >= PID_MAX)
    409 				return (EINVAL);
    410 
    411 #ifdef __HAVE_CPU_MAXPROC
    412 			if (nmaxproc > cpu_maxproc())
    413 				return (EINVAL);
    414 #endif
    415 			maxproc = nmaxproc;
    416 		}
    417 
    418 		return (error);
    419 	    }
    420 	case KERN_MAXFILES:
    421 		return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles));
    422 	case KERN_ARGMAX:
    423 		return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX));
    424 	case KERN_SECURELVL:
    425 		level = securelevel;
    426 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) ||
    427 		    newp == NULL)
    428 			return (error);
    429 		if (level < securelevel && p->p_pid != 1)
    430 			return (EPERM);
    431 		securelevel = level;
    432 		return (0);
    433 	case KERN_HOSTNAME:
    434 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    435 		    hostname, sizeof(hostname));
    436 		if (newp && !error)
    437 			hostnamelen = newlen;
    438 		return (error);
    439 	case KERN_DOMAINNAME:
    440 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    441 		    domainname, sizeof(domainname));
    442 		if (newp && !error)
    443 			domainnamelen = newlen;
    444 		return (error);
    445 	case KERN_HOSTID:
    446 		inthostid = hostid;  /* XXX assumes sizeof long <= sizeof int */
    447 		error = sysctl_int(oldp, oldlenp, newp, newlen, &inthostid);
    448 		if (newp && !error)
    449 			hostid = inthostid;
    450 		return (error);
    451 	case KERN_CLOCKRATE:
    452 		return (sysctl_clockrate(oldp, oldlenp));
    453 	case KERN_BOOTTIME:
    454 		return (sysctl_rdstruct(oldp, oldlenp, newp, &boottime,
    455 		    sizeof(struct timeval)));
    456 	case KERN_VNODE:
    457 		return (sysctl_vnode(oldp, oldlenp, p));
    458 	case KERN_PROC:
    459 	case KERN_PROC2:
    460 		return (sysctl_doeproc(name, namelen, oldp, oldlenp));
    461 	case KERN_LWP:
    462 		return (sysctl_dolwp(name, namelen, oldp, oldlenp));
    463 	case KERN_PROC_ARGS:
    464 		return (sysctl_procargs(name + 1, namelen - 1,
    465 		    oldp, oldlenp, p));
    466 	case KERN_FILE:
    467 		return (sysctl_file(oldp, oldlenp));
    468 #ifdef GPROF
    469 	case KERN_PROF:
    470 		return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
    471 		    newp, newlen));
    472 #endif
    473 	case KERN_POSIX1:
    474 		return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION));
    475 	case KERN_NGROUPS:
    476 		return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX));
    477 	case KERN_JOB_CONTROL:
    478 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    479 	case KERN_SAVED_IDS:
    480 #ifdef _POSIX_SAVED_IDS
    481 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    482 #else
    483 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    484 #endif
    485 	case KERN_MAXPARTITIONS:
    486 		return (sysctl_rdint(oldp, oldlenp, newp, MAXPARTITIONS));
    487 	case KERN_RAWPARTITION:
    488 		return (sysctl_rdint(oldp, oldlenp, newp, RAW_PART));
    489 #ifdef NTP
    490 	case KERN_NTPTIME:
    491 		return (sysctl_ntptime(oldp, oldlenp));
    492 #endif
    493 	case KERN_AUTONICETIME:
    494 		old_autonicetime = autonicetime;
    495 		error = sysctl_int(oldp, oldlenp, newp, newlen, &autonicetime);
    496 		if (autonicetime < 0)
    497  			autonicetime = old_autonicetime;
    498 		return (error);
    499 	case KERN_AUTONICEVAL:
    500 		error = sysctl_int(oldp, oldlenp, newp, newlen, &autoniceval);
    501 		if (autoniceval < PRIO_MIN)
    502 			autoniceval = PRIO_MIN;
    503 		if (autoniceval > PRIO_MAX)
    504 			autoniceval = PRIO_MAX;
    505 		return (error);
    506 	case KERN_RTC_OFFSET:
    507 		return (sysctl_rdint(oldp, oldlenp, newp, rtc_offset));
    508 	case KERN_ROOT_DEVICE:
    509 		return (sysctl_rdstring(oldp, oldlenp, newp,
    510 		    root_device->dv_xname));
    511 	case KERN_ROOT_PARTITION:
    512 		return (sysctl_rdint(oldp, oldlenp, newp, DISKPART(rootdev)));
    513 	case KERN_MSGBUFSIZE:
    514 		/*
    515 		 * deal with cases where the message buffer has
    516 		 * become corrupted.
    517 		 */
    518 		if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
    519 			msgbufenabled = 0;
    520 			return (ENXIO);
    521 		}
    522 		return (sysctl_rdint(oldp, oldlenp, newp, msgbufp->msg_bufs));
    523 	case KERN_FSYNC:
    524 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    525 	case KERN_SYSVMSG:
    526 #ifdef SYSVMSG
    527 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    528 #else
    529 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    530 #endif
    531 	case KERN_SYSVSEM:
    532 #ifdef SYSVSEM
    533 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    534 #else
    535 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    536 #endif
    537 	case KERN_SYSVSHM:
    538 #ifdef SYSVSHM
    539 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    540 #else
    541 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    542 #endif
    543  	case KERN_DEFCORENAME:
    544 		if (newp && newlen < 1)
    545 			return (EINVAL);
    546 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    547 		    defcorename, sizeof(defcorename));
    548 		return (error);
    549 	case KERN_SYNCHRONIZED_IO:
    550 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    551 	case KERN_IOV_MAX:
    552 		return (sysctl_rdint(oldp, oldlenp, newp, IOV_MAX));
    553 	case KERN_MBUF:
    554 		return (sysctl_dombuf(name + 1, namelen - 1, oldp, oldlenp,
    555 		    newp, newlen));
    556 	case KERN_MAPPED_FILES:
    557 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    558 	case KERN_MEMLOCK:
    559 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    560 	case KERN_MEMLOCK_RANGE:
    561 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    562 	case KERN_MEMORY_PROTECTION:
    563 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    564 	case KERN_LOGIN_NAME_MAX:
    565 		return (sysctl_rdint(oldp, oldlenp, newp, LOGIN_NAME_MAX));
    566 	case KERN_LOGSIGEXIT:
    567 		return (sysctl_int(oldp, oldlenp, newp, newlen,
    568 		    &kern_logsigexit));
    569 	case KERN_FSCALE:
    570 		return (sysctl_rdint(oldp, oldlenp, newp, FSCALE));
    571 	case KERN_CCPU:
    572 		return (sysctl_rdint(oldp, oldlenp, newp, ccpu));
    573 	case KERN_CP_TIME:
    574 #ifndef MULTIPROCESSOR
    575 		return (sysctl_rdstruct(oldp, oldlenp, newp,
    576 		    curcpu()->ci_schedstate.spc_cp_time,
    577 		    sizeof(curcpu()->ci_schedstate.spc_cp_time)));
    578 #else
    579 		return (sysctl_docptime(oldp, oldlenp, newp));
    580 #endif
    581 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
    582 	case KERN_SYSVIPC_INFO:
    583 		return (sysctl_sysvipc(name + 1, namelen - 1, oldp, oldlenp));
    584 #endif
    585 	case KERN_MSGBUF:
    586 		return (sysctl_msgbuf(oldp, oldlenp));
    587 	case KERN_CONSDEV:
    588 		if (cn_tab != NULL)
    589 			consdev = cn_tab->cn_dev;
    590 		else
    591 			consdev = NODEV;
    592 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
    593 		    sizeof consdev));
    594 #if NPTY > 0
    595 	case KERN_MAXPTYS:
    596 		return (sysctl_pty(oldp, oldlenp, newp, newlen));
    597 #endif
    598 #ifndef PIPE_SOCKETPAIR
    599 	case KERN_PIPE:
    600 		return (sysctl_dopipe(name + 1, namelen - 1, oldp, oldlenp,
    601 		    newp, newlen));
    602 #endif
    603 	case KERN_MAXPHYS:
    604 		return (sysctl_rdint(oldp, oldlenp, newp, MAXPHYS));
    605 	case KERN_SOMAXKVA:
    606 	    {
    607 		int new_somaxkva = somaxkva;
    608 
    609 		error = sysctl_int(oldp, oldlenp, newp, newlen, &new_somaxkva);
    610 		if (newp && !error) {
    611 			if (new_somaxkva < (16 * 1024 * 1024)) /* sanity */
    612 				return (EINVAL);
    613 			somaxkva = new_somaxkva;
    614 		}
    615 		return (error);
    616 	    }
    617 	case KERN_SBMAX:
    618 	    {
    619 		int new_sbmax = sb_max;
    620 
    621 		error = sysctl_int(oldp, oldlenp, newp, newlen, &new_sbmax);
    622 		if (newp && !error) {
    623 			if (new_sbmax < (16 * 1024)) /* sanity */
    624 				return (EINVAL);
    625 			sb_max = new_sbmax;
    626 		}
    627 		return (error);
    628 	    }
    629 	case KERN_TKSTAT:
    630 		return (sysctl_dotkstat(name + 1, namelen - 1, oldp, oldlenp,
    631 		    newp));
    632 	case KERN_MONOTONIC_CLOCK:	/* XXX _POSIX_VERSION */
    633 		return (sysctl_rdint(oldp, oldlenp, newp, 200112));
    634 	case KERN_URND:
    635 #if NRND > 0
    636 		if (rnd_extract_data(&v, sizeof(v), RND_EXTRACT_ANY) ==
    637 		    sizeof(v))
    638 			return (sysctl_rdint(oldp, oldlenp, newp, v));
    639 		else
    640 			return (EIO);	/*XXX*/
    641 #else
    642 		return (EOPNOTSUPP);
    643 #endif
    644 	case KERN_LABELSECTOR:
    645 		return (sysctl_rdint(oldp, oldlenp, newp, LABELSECTOR));
    646 	case KERN_LABELOFFSET:
    647 		return (sysctl_rdint(oldp, oldlenp, newp, LABELOFFSET));
    648 	case KERN_FORKFSLEEP:
    649 	    {
    650 		/* userland sees value in ms, internally is in ticks */
    651 		int timo, lsleep = forkfsleep * 1000 / hz;
    652 
    653 		error = sysctl_int(oldp, oldlenp, newp, newlen, &lsleep);
    654 		if (newp && !error) {
    655 			/* refuse negative values, and overly 'long time' */
    656 			if (lsleep < 0 || lsleep > MAXSLP * 1000)
    657 				return (EINVAL);
    658 
    659 			timo = mstohz(lsleep);
    660 
    661 			/* if the interval is >0 ms && <1 tick, use 1 tick */
    662 			if (lsleep != 0 && timo == 0)
    663 				forkfsleep = 1;
    664 			else
    665 				forkfsleep = timo;
    666 		}
    667 		return (error);
    668 	    }
    669 	case KERN_POSIX_THREADS:	/* XXX _POSIX_VERSION */
    670 		return (sysctl_rdint(oldp, oldlenp, newp, 200112));
    671 	case KERN_POSIX_SEMAPHORES:	/* XXX _POSIX_VERSION */
    672 #ifdef P1003_1B_SEMAPHORE
    673 		return (sysctl_rdint(oldp, oldlenp, newp, 200112));
    674 #else
    675 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    676 #endif
    677 	case KERN_POSIX_BARRIERS:	/* XXX _POSIX_VERSION */
    678 		return (sysctl_rdint(oldp, oldlenp, newp, 200112));
    679 	case KERN_POSIX_TIMERS:		/* XXX _POSIX_VERSION */
    680 		return (sysctl_rdint(oldp, oldlenp, newp, 200112));
    681 	case KERN_POSIX_SPIN_LOCKS:	/* XXX _POSIX_VERSION */
    682 		return (sysctl_rdint(oldp, oldlenp, newp, 200112));
    683 	case KERN_POSIX_READER_WRITER_LOCKS:	/* XXX _POSIX_VERSION */
    684 		return (sysctl_rdint(oldp, oldlenp, newp, 200112));
    685 	case KERN_DUMP_ON_PANIC:
    686 		return (sysctl_int(oldp, oldlenp, newp, newlen, &dumponpanic));
    687 
    688 	case KERN_DRIVERS:
    689 		return (sysctl_drivers(oldp, oldlenp));
    690 
    691 	default:
    692 		return (EOPNOTSUPP);
    693 	}
    694 	/* NOTREACHED */
    695 }
    696 
    697 /*
    698  * hardware related system variables.
    699  */
    700 int
    701 hw_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    702     void *newp, size_t newlen, struct proc *p)
    703 {
    704 
    705 	/* All sysctl names at this level, except for a few, are terminal. */
    706 	switch (name[0]) {
    707 	case HW_DISKSTATS:
    708 		/* Not terminal. */
    709 		break;
    710 	default:
    711 		if (namelen != 1)
    712 			return (ENOTDIR);	/* overloaded */
    713 	}
    714 
    715 	switch (name[0]) {
    716 	case HW_MACHINE:
    717 		return (sysctl_rdstring(oldp, oldlenp, newp, machine));
    718 	case HW_MACHINE_ARCH:
    719 		return (sysctl_rdstring(oldp, oldlenp, newp, machine_arch));
    720 	case HW_MODEL:
    721 		return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
    722 	case HW_NCPU:
    723 		return (sysctl_rdint(oldp, oldlenp, newp, sysctl_ncpus()));
    724 	case HW_BYTEORDER:
    725 		return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
    726 	case HW_PHYSMEM:
    727 	    {
    728 		u_int rval;
    729 
    730 		if ((u_int)physmem > (UINT_MAX / PAGE_SIZE))
    731 			rval = UINT_MAX;
    732 		else
    733 			rval = physmem * PAGE_SIZE;
    734 		return (sysctl_rdint(oldp, oldlenp, newp, rval));
    735 	    }
    736 	case HW_PHYSMEM64:
    737 		return (sysctl_rdquad(oldp, oldlenp, newp,
    738 		    (u_quad_t)physmem * PAGE_SIZE));
    739 	case HW_USERMEM:
    740 	    {
    741 		u_int rval;
    742 
    743 		if ((u_int)(physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
    744 			rval = UINT_MAX;
    745 		else
    746 			rval = (physmem - uvmexp.wired) * PAGE_SIZE;
    747 		return (sysctl_rdint(oldp, oldlenp, newp, rval));
    748 	    }
    749 	case HW_USERMEM64:
    750 		return (sysctl_rdquad(oldp, oldlenp, newp,
    751 		    (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE));
    752 	case HW_PAGESIZE:
    753 		return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
    754 	case HW_ALIGNBYTES:
    755 		return (sysctl_rdint(oldp, oldlenp, newp, ALIGNBYTES));
    756 	case HW_DISKNAMES:
    757 		return (sysctl_disknames(oldp, oldlenp));
    758 	case HW_DISKSTATS:
    759 		return (sysctl_diskstats(name + 1, namelen - 1, oldp, oldlenp));
    760 	case HW_CNMAGIC: {
    761 		char magic[CNS_LEN];
    762 		int error;
    763 
    764 		if (oldp)
    765 			cn_get_magic(magic, CNS_LEN);
    766 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    767 		    magic, sizeof(magic));
    768 		if (newp && !error) {
    769 			error = cn_set_magic(magic);
    770 		}
    771 		return (error);
    772 	}
    773 	default:
    774 		return (EOPNOTSUPP);
    775 	}
    776 	/* NOTREACHED */
    777 }
    778 
    779 #ifdef DEBUG
    780 /*
    781  * Debugging related system variables.
    782  */
    783 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
    784 struct ctldebug debug5, debug6, debug7, debug8, debug9;
    785 struct ctldebug debug10, debug11, debug12, debug13, debug14;
    786 struct ctldebug debug15, debug16, debug17, debug18, debug19;
    787 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
    788 	&debug0, &debug1, &debug2, &debug3, &debug4,
    789 	&debug5, &debug6, &debug7, &debug8, &debug9,
    790 	&debug10, &debug11, &debug12, &debug13, &debug14,
    791 	&debug15, &debug16, &debug17, &debug18, &debug19,
    792 };
    793 
    794 int
    795 debug_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    796     void *newp, size_t newlen, struct proc *p)
    797 {
    798 	struct ctldebug *cdp;
    799 
    800 	/* all sysctl names at this level are name and field */
    801 	if (namelen != 2)
    802 		return (ENOTDIR);		/* overloaded */
    803 	if (name[0] < 0 || name[0] >= CTL_DEBUG_MAXID)
    804 		return (EOPNOTSUPP);
    805 	cdp = debugvars[name[0]];
    806 	if (cdp->debugname == 0)
    807 		return (EOPNOTSUPP);
    808 	switch (name[1]) {
    809 	case CTL_DEBUG_NAME:
    810 		return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
    811 	case CTL_DEBUG_VALUE:
    812 		return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
    813 	default:
    814 		return (EOPNOTSUPP);
    815 	}
    816 	/* NOTREACHED */
    817 }
    818 #endif /* DEBUG */
    819 
    820 int
    821 proc_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    822     void *newp, size_t newlen, struct proc *p)
    823 {
    824 	struct proc *ptmp = NULL;
    825 	int error = 0;
    826 	struct rlimit alim;
    827 	struct plimit *newplim;
    828 	char *tmps = NULL;
    829 	size_t len, curlen;
    830 	u_int i;
    831 	int si;
    832 
    833 	if (namelen < 2)
    834 		return (EINVAL);
    835 
    836 	if (name[0] == PROC_CURPROC) {
    837 		ptmp = p;
    838 	} else if ((ptmp = pfind((pid_t)name[0])) == NULL) {
    839 		return (ESRCH);
    840 	} else {
    841 		if (p->p_ucred->cr_uid != 0) {
    842 			if (p->p_cred->p_ruid != ptmp->p_cred->p_ruid ||
    843 			    p->p_cred->p_ruid != ptmp->p_cred->p_svuid)
    844 				return (EPERM);
    845 			if (ptmp->p_cred->p_rgid != ptmp->p_cred->p_svgid)
    846 				return (EPERM); /* sgid proc */
    847 			for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
    848 				if (p->p_ucred->cr_groups[i] ==
    849 				    ptmp->p_cred->p_rgid)
    850 					break;
    851 			}
    852 			if (i == p->p_ucred->cr_ngroups)
    853 				return (EPERM);
    854 		}
    855 	}
    856 	switch (name[1]) {
    857 	case PROC_PID_STOPFORK:
    858 		if (namelen != 2)
    859 			return (EINVAL);
    860 		si = ((ptmp->p_flag & P_STOPFORK) != 0);
    861 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &si)) != 0)
    862 			return (error);
    863 		if (si != 0)
    864 			ptmp->p_flag |= P_STOPFORK;
    865 		else
    866 			ptmp->p_flag &= ~P_STOPFORK;
    867 		return (0);
    868 
    869 	case PROC_PID_STOPEXEC:
    870 		if (namelen != 2)
    871 			return (EINVAL);
    872 		si = ((ptmp->p_flag & P_STOPEXEC) != 0);
    873 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &si)) != 0)
    874 			return (error);
    875 		if (si != 0)
    876 			ptmp->p_flag |= P_STOPEXEC;
    877 		else
    878 			ptmp->p_flag &= ~P_STOPEXEC;
    879 		return (0);
    880 
    881 	case PROC_PID_CORENAME:
    882 		if (namelen != 2)
    883 			return (EINVAL);
    884 		/*
    885 		 * Can't use sysctl_string() here because we may malloc a new
    886 		 * area during the process, so we have to do it by hand.
    887 		 */
    888 		curlen = strlen(ptmp->p_limit->pl_corename) + 1;
    889 		if (oldlenp && *oldlenp < curlen) {
    890 			if (!oldp)
    891 				*oldlenp = curlen;
    892 			return (ENOMEM);
    893 		}
    894 		if (newp) {
    895 			if (securelevel > 2)
    896 				return (EPERM);
    897 			if (newlen > MAXPATHLEN)
    898 				return (ENAMETOOLONG);
    899 			tmps = malloc(newlen + 1, M_TEMP, M_WAITOK);
    900 			if (tmps == NULL)
    901 				return (ENOMEM);
    902 			error = copyin(newp, tmps, newlen + 1);
    903 			tmps[newlen] = '\0';
    904 			if (error)
    905 				goto cleanup;
    906 			/* Enforce to be either 'core' for end with '.core' */
    907 			if (newlen < 4) {	/* c.o.r.e */
    908 				error = EINVAL;
    909 				goto cleanup;
    910 			}
    911 			len = newlen - 4;
    912 			if (len > 0) {
    913 				if (tmps[len - 1] != '.' &&
    914 				    tmps[len - 1] != '/') {
    915 					error = EINVAL;
    916 					goto cleanup;
    917 				}
    918 			}
    919 			if (strcmp(&tmps[len], "core") != 0) {
    920 				error = EINVAL;
    921 				goto cleanup;
    922 			}
    923 		}
    924 		if (oldp && oldlenp) {
    925 			*oldlenp = curlen;
    926 			error = copyout(ptmp->p_limit->pl_corename, oldp,
    927 			    curlen);
    928 		}
    929 		if (newp && error == 0) {
    930 			/* if the 2 strings are identical, don't limcopy() */
    931 			if (strcmp(tmps, ptmp->p_limit->pl_corename) == 0) {
    932 				error = 0;
    933 				goto cleanup;
    934 			}
    935 			if (ptmp->p_limit->p_refcnt > 1 &&
    936 			    (ptmp->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    937 				newplim = limcopy(ptmp->p_limit);
    938 				limfree(ptmp->p_limit);
    939 				ptmp->p_limit = newplim;
    940 			}
    941 			if (ptmp->p_limit->pl_corename != defcorename) {
    942 				free(ptmp->p_limit->pl_corename, M_TEMP);
    943 			}
    944 			ptmp->p_limit->pl_corename = tmps;
    945 			return (0);
    946 		}
    947 cleanup:
    948 		if (tmps)
    949 			free(tmps, M_TEMP);
    950 		return (error);
    951 
    952 	case PROC_PID_LIMIT:
    953 		if (namelen != 4 || name[2] < 1 ||
    954 		    name[2] >= PROC_PID_LIMIT_MAXID)
    955 			return (EINVAL);
    956 		memcpy(&alim, &ptmp->p_rlimit[name[2] - 1], sizeof(alim));
    957 		if (name[3] == PROC_PID_LIMIT_TYPE_HARD)
    958 			error = sysctl_quad(oldp, oldlenp, newp, newlen,
    959 			    &alim.rlim_max);
    960 		else if (name[3] == PROC_PID_LIMIT_TYPE_SOFT)
    961 			error = sysctl_quad(oldp, oldlenp, newp, newlen,
    962 			    &alim.rlim_cur);
    963 		else
    964 			error = (EINVAL);
    965 
    966 		if (error)
    967 			return (error);
    968 
    969 		if (newp)
    970 			error = dosetrlimit(ptmp, p->p_cred,
    971 			    name[2] - 1, &alim);
    972 		return (error);
    973 
    974 	default:
    975 		return (EINVAL);
    976 	}
    977 	/* NOTREACHED */
    978 	return (EINVAL);
    979 }
    980 
    981 int
    982 emul_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    983     void *newp, size_t newlen, struct proc *p)
    984 {
    985 	static struct {
    986 		const char *name;
    987 		int type;
    988 	} emulations[] = CTL_EMUL_NAMES;
    989 	const struct emul *e;
    990 	const char *ename;
    991 #ifdef LKM
    992 	extern struct lock exec_lock;	/* XXX */
    993 	int error;
    994 #else
    995 	extern int nexecs_builtin;
    996 	extern const struct execsw execsw_builtin[];
    997 	int i;
    998 #endif
    999 
   1000 	/* all sysctl names at this level are name and field */
   1001 	if (namelen < 2)
   1002 		return (ENOTDIR);		/* overloaded */
   1003 
   1004 	if ((u_int) name[0] >= EMUL_MAXID || name[0] == 0)
   1005 		return (EOPNOTSUPP);
   1006 
   1007 	ename = emulations[name[0]].name;
   1008 
   1009 #ifdef LKM
   1010 	lockmgr(&exec_lock, LK_SHARED, NULL);
   1011 	if ((e = emul_search(ename))) {
   1012 		error = (*e->e_sysctl)(name + 1, namelen - 1, oldp, oldlenp,
   1013 		    newp, newlen, p);
   1014 	} else
   1015 		error = EOPNOTSUPP;
   1016 	lockmgr(&exec_lock, LK_RELEASE, NULL);
   1017 
   1018 	return (error);
   1019 #else
   1020 	for (i = 0; i < nexecs_builtin; i++) {
   1021 		e = execsw_builtin[i].es_emul;
   1022 		/*
   1023 		 * In order to match e.g. e->e_name "irix o32"
   1024 		 * with ename "irix", we limit the comparison
   1025 		 * to the length of ename.
   1026 		 */
   1027 		if (e == NULL ||
   1028 		    strncmp(ename, e->e_name, strlen(ename)) != 0 ||
   1029 		    e->e_sysctl == NULL)
   1030 			continue;
   1031 
   1032 		return ((*e->e_sysctl)(name + 1, namelen - 1, oldp, oldlenp,
   1033 		    newp, newlen, p));
   1034 	}
   1035 
   1036 	return (EOPNOTSUPP);
   1037 #endif
   1038 }
   1039 /*
   1040  * Convenience macros.
   1041  */
   1042 
   1043 #define SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, len) 		\
   1044 	if (oldlenp) {							\
   1045 		if (!oldp)						\
   1046 			*oldlenp = len;					\
   1047 		else {							\
   1048 			if (*oldlenp < len)				\
   1049 				return (ENOMEM);			\
   1050 			*oldlenp = len;					\
   1051 			error = copyout(valp, oldp, len);		\
   1052 		}							\
   1053 	}
   1054 
   1055 #define SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, typ) \
   1056 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, sizeof(typ))
   1057 
   1058 #define SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len)	\
   1059 	if (newp && newlen != len)			\
   1060 		return (EINVAL);
   1061 
   1062 #define SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, typ)	\
   1063 	SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, sizeof(typ))
   1064 
   1065 #define SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, len)	\
   1066 	if (error == 0 && newp)				\
   1067 		error = copyin(newp, valp, len);
   1068 
   1069 #define SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, typ)	\
   1070 	SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, sizeof(typ))
   1071 
   1072 #define SYSCTL_STRING_CORE(oldp, oldlenp, str)		\
   1073 	if (oldlenp) {					\
   1074 		len = strlen(str) + 1;			\
   1075 		if (!oldp)				\
   1076 			*oldlenp = len;			\
   1077 		else {					\
   1078 			if (*oldlenp < len) {		\
   1079 				err2 = ENOMEM;		\
   1080 				len = *oldlenp;		\
   1081 			} else				\
   1082 				*oldlenp = len;		\
   1083 			error = copyout(str, oldp, len);\
   1084 			if (error == 0)			\
   1085 				error = err2;		\
   1086 		}					\
   1087 	}
   1088 
   1089 /*
   1090  * Validate parameters and get old / set new parameters
   1091  * for an integer-valued sysctl function.
   1092  */
   1093 int
   1094 sysctl_int(void *oldp, size_t *oldlenp, void *newp, size_t newlen, int *valp)
   1095 {
   1096 	int error = 0;
   1097 
   1098 	SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
   1099 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, int)
   1100 	SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, int)
   1101 
   1102 	return (error);
   1103 }
   1104 
   1105 
   1106 /*
   1107  * As above, but read-only.
   1108  */
   1109 int
   1110 sysctl_rdint(void *oldp, size_t *oldlenp, void *newp, int val)
   1111 {
   1112 	int error = 0;
   1113 
   1114 	if (newp)
   1115 		return (EPERM);
   1116 
   1117 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, int)
   1118 
   1119 	return (error);
   1120 }
   1121 
   1122 /*
   1123  * Validate parameters and get old / set new parameters
   1124  * for an quad-valued sysctl function.
   1125  */
   1126 int
   1127 sysctl_quad(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
   1128     quad_t *valp)
   1129 {
   1130 	int error = 0;
   1131 
   1132 	SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, quad_t)
   1133 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, quad_t)
   1134 	SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, quad_t)
   1135 
   1136 	return (error);
   1137 }
   1138 
   1139 /*
   1140  * As above, but read-only.
   1141  */
   1142 int
   1143 sysctl_rdquad(void *oldp, size_t *oldlenp, void *newp, quad_t val)
   1144 {
   1145 	int error = 0;
   1146 
   1147 	if (newp)
   1148 		return (EPERM);
   1149 
   1150 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, quad_t)
   1151 
   1152 	return (error);
   1153 }
   1154 
   1155 /*
   1156  * Validate parameters and get old / set new parameters
   1157  * for a string-valued sysctl function.
   1158  */
   1159 int
   1160 sysctl_string(void *oldp, size_t *oldlenp, void *newp, size_t newlen, char *str,
   1161     size_t maxlen)
   1162 {
   1163 	int error = 0, err2 = 0;
   1164 	size_t len;
   1165 
   1166 	if (newp && newlen >= maxlen)
   1167 		return (EINVAL);
   1168 
   1169 	SYSCTL_STRING_CORE(oldp, oldlenp, str);
   1170 
   1171 	if (error == 0 && newp) {
   1172 		error = copyin(newp, str, newlen);
   1173 		str[newlen] = 0;
   1174 	}
   1175 	return (error);
   1176 }
   1177 
   1178 /*
   1179  * As above, but read-only.
   1180  */
   1181 int
   1182 sysctl_rdstring(void *oldp, size_t *oldlenp, void *newp, const char *str)
   1183 {
   1184 	int error = 0, err2 = 0;
   1185 	size_t len;
   1186 
   1187 	if (newp)
   1188 		return (EPERM);
   1189 
   1190 	SYSCTL_STRING_CORE(oldp, oldlenp, str);
   1191 
   1192 	return (error);
   1193 }
   1194 
   1195 /*
   1196  * Validate parameters and get old / set new parameters
   1197  * for a structure oriented sysctl function.
   1198  */
   1199 int
   1200 sysctl_struct(void *oldp, size_t *oldlenp, void *newp, size_t newlen, void *sp,
   1201     size_t len)
   1202 {
   1203 	int error = 0;
   1204 
   1205 	SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len)
   1206 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
   1207 	SYSCTL_SCALAR_NEWPCOP_LEN(newp, sp, len)
   1208 
   1209 	return (error);
   1210 }
   1211 
   1212 /*
   1213  * Validate parameters and get old parameters
   1214  * for a structure oriented sysctl function.
   1215  */
   1216 int
   1217 sysctl_rdstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
   1218     size_t len)
   1219 {
   1220 	int error = 0;
   1221 
   1222 	if (newp)
   1223 		return (EPERM);
   1224 
   1225 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
   1226 
   1227 	return (error);
   1228 }
   1229 
   1230 /*
   1231  * As above, but can return a truncated result.
   1232  */
   1233 int
   1234 sysctl_rdminstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
   1235     size_t len)
   1236 {
   1237 	int error = 0;
   1238 
   1239 	if (newp)
   1240 		return (EPERM);
   1241 
   1242 	len = min(*oldlenp, len);
   1243 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
   1244 
   1245 	return (error);
   1246 }
   1247 
   1248 /*
   1249  * Get file structures.
   1250  */
   1251 static int
   1252 sysctl_file(void *vwhere, size_t *sizep)
   1253 {
   1254 	int error;
   1255 	size_t buflen;
   1256 	struct file *fp;
   1257 	char *start, *where;
   1258 
   1259 	start = where = vwhere;
   1260 	buflen = *sizep;
   1261 	if (where == NULL) {
   1262 		/*
   1263 		 * overestimate by 10 files
   1264 		 */
   1265 		*sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
   1266 		return (0);
   1267 	}
   1268 
   1269 	/*
   1270 	 * first copyout filehead
   1271 	 */
   1272 	if (buflen < sizeof(filehead)) {
   1273 		*sizep = 0;
   1274 		return (0);
   1275 	}
   1276 	error = copyout(&filehead, where, sizeof(filehead));
   1277 	if (error)
   1278 		return (error);
   1279 	buflen -= sizeof(filehead);
   1280 	where += sizeof(filehead);
   1281 
   1282 	/*
   1283 	 * followed by an array of file structures
   1284 	 */
   1285 	LIST_FOREACH(fp, &filehead, f_list) {
   1286 		if (buflen < sizeof(struct file)) {
   1287 			*sizep = where - start;
   1288 			return (ENOMEM);
   1289 		}
   1290 		error = copyout(fp, where, sizeof(struct file));
   1291 		if (error)
   1292 			return (error);
   1293 		buflen -= sizeof(struct file);
   1294 		where += sizeof(struct file);
   1295 	}
   1296 	*sizep = where - start;
   1297 	return (0);
   1298 }
   1299 
   1300 /*
   1301  * Get driver names and majors
   1302  */
   1303 static int
   1304 sysctl_drivers(void *vwhere, size_t *sizep)
   1305 {
   1306 	int error;
   1307 	size_t buflen;
   1308 	struct kinfo_drivers kd;
   1309 	char *start, *where;
   1310 	const char *name;
   1311 	int i;
   1312 	extern struct devsw_conv *devsw_conv;
   1313 	extern int max_devsw_convs;
   1314 
   1315 	start = where = vwhere;
   1316 	buflen = *sizep;
   1317 	if (where == NULL) {
   1318 		*sizep = max_devsw_convs * sizeof kd;
   1319 		return 0;
   1320 	}
   1321 
   1322 	/*
   1323 	 * An array of kinfo_drivers structures
   1324 	 */
   1325 	error = 0;
   1326 	for (i = 0; i < max_devsw_convs; i++) {
   1327 		name = devsw_conv[i].d_name;
   1328 		if (name == NULL)
   1329 			continue;
   1330 		if (buflen < sizeof kd) {
   1331 			error = ENOMEM;
   1332 			break;
   1333 		}
   1334 		kd.d_bmajor = devsw_conv[i].d_bmajor;
   1335 		kd.d_cmajor = devsw_conv[i].d_cmajor;
   1336 		strlcpy(kd.d_name, name, sizeof kd.d_name);
   1337 		error = copyout(&kd, where, sizeof kd);
   1338 		if (error != 0)
   1339 			break;
   1340 		buflen -= sizeof kd;
   1341 		where += sizeof kd;
   1342 	}
   1343 	*sizep = where - start;
   1344 	return error;
   1345 }
   1346 
   1347 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
   1348 #define	FILL_PERM(src, dst) do { \
   1349 	(dst)._key = (src)._key; \
   1350 	(dst).uid = (src).uid; \
   1351 	(dst).gid = (src).gid; \
   1352 	(dst).cuid = (src).cuid; \
   1353 	(dst).cgid = (src).cgid; \
   1354 	(dst).mode = (src).mode; \
   1355 	(dst)._seq = (src)._seq; \
   1356 } while (/*CONSTCOND*/ 0);
   1357 #define	FILL_MSG(src, dst) do { \
   1358 	FILL_PERM((src).msg_perm, (dst).msg_perm); \
   1359 	(dst).msg_qnum = (src).msg_qnum; \
   1360 	(dst).msg_qbytes = (src).msg_qbytes; \
   1361 	(dst)._msg_cbytes = (src)._msg_cbytes; \
   1362 	(dst).msg_lspid = (src).msg_lspid; \
   1363 	(dst).msg_lrpid = (src).msg_lrpid; \
   1364 	(dst).msg_stime = (src).msg_stime; \
   1365 	(dst).msg_rtime = (src).msg_rtime; \
   1366 	(dst).msg_ctime = (src).msg_ctime; \
   1367 } while (/*CONSTCOND*/ 0)
   1368 #define	FILL_SEM(src, dst) do { \
   1369 	FILL_PERM((src).sem_perm, (dst).sem_perm); \
   1370 	(dst).sem_nsems = (src).sem_nsems; \
   1371 	(dst).sem_otime = (src).sem_otime; \
   1372 	(dst).sem_ctime = (src).sem_ctime; \
   1373 } while (/*CONSTCOND*/ 0)
   1374 #define	FILL_SHM(src, dst) do { \
   1375 	FILL_PERM((src).shm_perm, (dst).shm_perm); \
   1376 	(dst).shm_segsz = (src).shm_segsz; \
   1377 	(dst).shm_lpid = (src).shm_lpid; \
   1378 	(dst).shm_cpid = (src).shm_cpid; \
   1379 	(dst).shm_atime = (src).shm_atime; \
   1380 	(dst).shm_dtime = (src).shm_dtime; \
   1381 	(dst).shm_ctime = (src).shm_ctime; \
   1382 	(dst).shm_nattch = (src).shm_nattch; \
   1383 } while (/*CONSTCOND*/ 0)
   1384 
   1385 static int
   1386 sysctl_sysvipc(int *name, u_int namelen, void *where, size_t *sizep)
   1387 {
   1388 #ifdef SYSVMSG
   1389 	struct msg_sysctl_info *msgsi = NULL;
   1390 #endif
   1391 #ifdef SYSVSEM
   1392 	struct sem_sysctl_info *semsi = NULL;
   1393 #endif
   1394 #ifdef SYSVSHM
   1395 	struct shm_sysctl_info *shmsi = NULL;
   1396 #endif
   1397 	size_t infosize, dssize, tsize, buflen;
   1398 	void *buf = NULL;
   1399 	char *start;
   1400 	int32_t nds;
   1401 	int i, error, ret;
   1402 
   1403 	if (namelen != 1)
   1404 		return (EINVAL);
   1405 
   1406 	start = where;
   1407 	buflen = *sizep;
   1408 
   1409 	switch (*name) {
   1410 	case KERN_SYSVIPC_MSG_INFO:
   1411 #ifdef SYSVMSG
   1412 		infosize = sizeof(msgsi->msginfo);
   1413 		nds = msginfo.msgmni;
   1414 		dssize = sizeof(msgsi->msgids[0]);
   1415 		break;
   1416 #else
   1417 		return (EINVAL);
   1418 #endif
   1419 	case KERN_SYSVIPC_SEM_INFO:
   1420 #ifdef SYSVSEM
   1421 		infosize = sizeof(semsi->seminfo);
   1422 		nds = seminfo.semmni;
   1423 		dssize = sizeof(semsi->semids[0]);
   1424 		break;
   1425 #else
   1426 		return (EINVAL);
   1427 #endif
   1428 	case KERN_SYSVIPC_SHM_INFO:
   1429 #ifdef SYSVSHM
   1430 		infosize = sizeof(shmsi->shminfo);
   1431 		nds = shminfo.shmmni;
   1432 		dssize = sizeof(shmsi->shmids[0]);
   1433 		break;
   1434 #else
   1435 		return (EINVAL);
   1436 #endif
   1437 	default:
   1438 		return (EINVAL);
   1439 	}
   1440 	/*
   1441 	 * Round infosize to 64 bit boundary if requesting more than just
   1442 	 * the info structure or getting the total data size.
   1443 	 */
   1444 	if (where == NULL || *sizep > infosize)
   1445 		infosize = ((infosize + 7) / 8) * 8;
   1446 	tsize = infosize + nds * dssize;
   1447 
   1448 	/* Return just the total size required. */
   1449 	if (where == NULL) {
   1450 		*sizep = tsize;
   1451 		return (0);
   1452 	}
   1453 
   1454 	/* Not enough room for even the info struct. */
   1455 	if (buflen < infosize) {
   1456 		*sizep = 0;
   1457 		return (ENOMEM);
   1458 	}
   1459 	buf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK);
   1460 	memset(buf, 0, min(tsize, buflen));
   1461 
   1462 	switch (*name) {
   1463 #ifdef SYSVMSG
   1464 	case KERN_SYSVIPC_MSG_INFO:
   1465 		msgsi = (struct msg_sysctl_info *)buf;
   1466 		msgsi->msginfo = msginfo;
   1467 		break;
   1468 #endif
   1469 #ifdef SYSVSEM
   1470 	case KERN_SYSVIPC_SEM_INFO:
   1471 		semsi = (struct sem_sysctl_info *)buf;
   1472 		semsi->seminfo = seminfo;
   1473 		break;
   1474 #endif
   1475 #ifdef SYSVSHM
   1476 	case KERN_SYSVIPC_SHM_INFO:
   1477 		shmsi = (struct shm_sysctl_info *)buf;
   1478 		shmsi->shminfo = shminfo;
   1479 		break;
   1480 #endif
   1481 	}
   1482 	buflen -= infosize;
   1483 
   1484 	ret = 0;
   1485 	if (buflen > 0) {
   1486 		/* Fill in the IPC data structures.  */
   1487 		for (i = 0; i < nds; i++) {
   1488 			if (buflen < dssize) {
   1489 				ret = ENOMEM;
   1490 				break;
   1491 			}
   1492 			switch (*name) {
   1493 #ifdef SYSVMSG
   1494 			case KERN_SYSVIPC_MSG_INFO:
   1495 				FILL_MSG(msqids[i], msgsi->msgids[i]);
   1496 				break;
   1497 #endif
   1498 #ifdef SYSVSEM
   1499 			case KERN_SYSVIPC_SEM_INFO:
   1500 				FILL_SEM(sema[i], semsi->semids[i]);
   1501 				break;
   1502 #endif
   1503 #ifdef SYSVSHM
   1504 			case KERN_SYSVIPC_SHM_INFO:
   1505 				FILL_SHM(shmsegs[i], shmsi->shmids[i]);
   1506 				break;
   1507 #endif
   1508 			}
   1509 			buflen -= dssize;
   1510 		}
   1511 	}
   1512 	*sizep -= buflen;
   1513 	error = copyout(buf, start, *sizep);
   1514 	/* If copyout succeeded, use return code set earlier. */
   1515 	if (error == 0)
   1516 		error = ret;
   1517 	if (buf)
   1518 		free(buf, M_TEMP);
   1519 	return (error);
   1520 }
   1521 #endif /* SYSVMSG || SYSVSEM || SYSVSHM */
   1522 
   1523 static int
   1524 sysctl_msgbuf(void *vwhere, size_t *sizep)
   1525 {
   1526 	char *where = vwhere;
   1527 	size_t len, maxlen = *sizep;
   1528 	long beg, end;
   1529 	int error;
   1530 
   1531 	/*
   1532 	 * deal with cases where the message buffer has
   1533 	 * become corrupted.
   1534 	 */
   1535 	if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
   1536 		msgbufenabled = 0;
   1537 		return (ENXIO);
   1538 	}
   1539 
   1540 	if (where == NULL) {
   1541 		/* always return full buffer size */
   1542 		*sizep = msgbufp->msg_bufs;
   1543 		return (0);
   1544 	}
   1545 
   1546 	error = 0;
   1547 	maxlen = min(msgbufp->msg_bufs, maxlen);
   1548 
   1549 	/*
   1550 	 * First, copy from the write pointer to the end of
   1551 	 * message buffer.
   1552 	 */
   1553 	beg = msgbufp->msg_bufx;
   1554 	end = msgbufp->msg_bufs;
   1555 	while (maxlen > 0) {
   1556 		len = min(end - beg, maxlen);
   1557 		if (len == 0)
   1558 			break;
   1559 		error = copyout(&msgbufp->msg_bufc[beg], where, len);
   1560 		if (error)
   1561 			break;
   1562 		where += len;
   1563 		maxlen -= len;
   1564 
   1565 		/*
   1566 		 * ... then, copy from the beginning of message buffer to
   1567 		 * the write pointer.
   1568 		 */
   1569 		beg = 0;
   1570 		end = msgbufp->msg_bufx;
   1571 	}
   1572 	return (error);
   1573 }
   1574 
   1575 /*
   1576  * try over estimating by 5 procs
   1577  */
   1578 #define KERN_PROCSLOP	(5 * sizeof(struct kinfo_proc))
   1579 
   1580 static int
   1581 sysctl_doeproc(int *name, u_int namelen, void *vwhere, size_t *sizep)
   1582 {
   1583 	struct eproc eproc;
   1584 	struct kinfo_proc2 kproc2;
   1585 	struct kinfo_proc *dp;
   1586 	struct proc *p;
   1587 	const struct proclist_desc *pd;
   1588 	char *where, *dp2;
   1589 	int type, op, arg;
   1590 	u_int elem_size, elem_count;
   1591 	size_t buflen, needed;
   1592 	int error;
   1593 
   1594 	dp = vwhere;
   1595 	dp2 = where = vwhere;
   1596 	buflen = where != NULL ? *sizep : 0;
   1597 	error = 0;
   1598 	needed = 0;
   1599 	type = name[0];
   1600 
   1601 	if (type == KERN_PROC) {
   1602 		if (namelen != 3 && !(namelen == 2 && name[1] == KERN_PROC_ALL))
   1603 			return (EINVAL);
   1604 		op = name[1];
   1605 		if (op != KERN_PROC_ALL)
   1606 			arg = name[2];
   1607 		else
   1608 			arg = 0;		/* Quell compiler warning */
   1609 		elem_size = elem_count = 0;	/* Ditto */
   1610 	} else {
   1611 		if (namelen != 5)
   1612 			return (EINVAL);
   1613 		op = name[1];
   1614 		arg = name[2];
   1615 		elem_size = name[3];
   1616 		elem_count = name[4];
   1617 	}
   1618 
   1619 	proclist_lock_read();
   1620 
   1621 	pd = proclists;
   1622 again:
   1623 	for (p = LIST_FIRST(pd->pd_list); p != NULL; p = LIST_NEXT(p, p_list)) {
   1624 		/*
   1625 		 * Skip embryonic processes.
   1626 		 */
   1627 		if (p->p_stat == SIDL)
   1628 			continue;
   1629 		/*
   1630 		 * TODO - make more efficient (see notes below).
   1631 		 * do by session.
   1632 		 */
   1633 		switch (op) {
   1634 
   1635 		case KERN_PROC_PID:
   1636 			/* could do this with just a lookup */
   1637 			if (p->p_pid != (pid_t)arg)
   1638 				continue;
   1639 			break;
   1640 
   1641 		case KERN_PROC_PGRP:
   1642 			/* could do this by traversing pgrp */
   1643 			if (p->p_pgrp->pg_id != (pid_t)arg)
   1644 				continue;
   1645 			break;
   1646 
   1647 		case KERN_PROC_SESSION:
   1648 			if (p->p_session->s_sid != (pid_t)arg)
   1649 				continue;
   1650 			break;
   1651 
   1652 		case KERN_PROC_TTY:
   1653 			if (arg == (int) KERN_PROC_TTY_REVOKE) {
   1654 				if ((p->p_flag & P_CONTROLT) == 0 ||
   1655 				    p->p_session->s_ttyp == NULL ||
   1656 				    p->p_session->s_ttyvp != NULL)
   1657 					continue;
   1658 			} else if ((p->p_flag & P_CONTROLT) == 0 ||
   1659 			    p->p_session->s_ttyp == NULL) {
   1660 				if ((dev_t)arg != KERN_PROC_TTY_NODEV)
   1661 					continue;
   1662 			} else if (p->p_session->s_ttyp->t_dev != (dev_t)arg)
   1663 				continue;
   1664 			break;
   1665 
   1666 		case KERN_PROC_UID:
   1667 			if (p->p_ucred->cr_uid != (uid_t)arg)
   1668 				continue;
   1669 			break;
   1670 
   1671 		case KERN_PROC_RUID:
   1672 			if (p->p_cred->p_ruid != (uid_t)arg)
   1673 				continue;
   1674 			break;
   1675 
   1676 		case KERN_PROC_GID:
   1677 			if (p->p_ucred->cr_gid != (uid_t)arg)
   1678 				continue;
   1679 			break;
   1680 
   1681 		case KERN_PROC_RGID:
   1682 			if (p->p_cred->p_rgid != (uid_t)arg)
   1683 				continue;
   1684 			break;
   1685 
   1686 		case KERN_PROC_ALL:
   1687 			/* allow everything */
   1688 			break;
   1689 
   1690 		default:
   1691 			error = EINVAL;
   1692 			goto cleanup;
   1693 		}
   1694 		if (type == KERN_PROC) {
   1695 			if (buflen >= sizeof(struct kinfo_proc)) {
   1696 				fill_eproc(p, &eproc);
   1697 				error = copyout(p, &dp->kp_proc,
   1698 				    sizeof(struct proc));
   1699 				if (error)
   1700 					goto cleanup;
   1701 				error = copyout(&eproc, &dp->kp_eproc,
   1702 				    sizeof(eproc));
   1703 				if (error)
   1704 					goto cleanup;
   1705 				dp++;
   1706 				buflen -= sizeof(struct kinfo_proc);
   1707 			}
   1708 			needed += sizeof(struct kinfo_proc);
   1709 		} else { /* KERN_PROC2 */
   1710 			if (buflen >= elem_size && elem_count > 0) {
   1711 				fill_kproc2(p, &kproc2);
   1712 				/*
   1713 				 * Copy out elem_size, but not larger than
   1714 				 * the size of a struct kinfo_proc2.
   1715 				 */
   1716 				error = copyout(&kproc2, dp2,
   1717 				    min(sizeof(kproc2), elem_size));
   1718 				if (error)
   1719 					goto cleanup;
   1720 				dp2 += elem_size;
   1721 				buflen -= elem_size;
   1722 				elem_count--;
   1723 			}
   1724 			needed += elem_size;
   1725 		}
   1726 	}
   1727 	pd++;
   1728 	if (pd->pd_list != NULL)
   1729 		goto again;
   1730 	proclist_unlock_read();
   1731 
   1732 	if (where != NULL) {
   1733 		if (type == KERN_PROC)
   1734 			*sizep = (char *)dp - where;
   1735 		else
   1736 			*sizep = dp2 - where;
   1737 		if (needed > *sizep)
   1738 			return (ENOMEM);
   1739 	} else {
   1740 		needed += KERN_PROCSLOP;
   1741 		*sizep = needed;
   1742 	}
   1743 	return (0);
   1744  cleanup:
   1745 	proclist_unlock_read();
   1746 	return (error);
   1747 }
   1748 
   1749 
   1750 /*
   1751  * try over estimating by 5 LWPs
   1752  */
   1753 #define KERN_LWPSLOP	(5 * sizeof(struct kinfo_lwp))
   1754 
   1755 static int
   1756 sysctl_dolwp(int *name, u_int namelen, void *vwhere, size_t *sizep)
   1757 {
   1758 	struct kinfo_lwp klwp;
   1759 	struct proc *p;
   1760 	struct lwp *l;
   1761 	char *where, *dp;
   1762 	int type, pid, elem_size, elem_count;
   1763 	int buflen, needed, error;
   1764 
   1765 	dp = where = vwhere;
   1766 	buflen = where != NULL ? *sizep : 0;
   1767 	error = needed = 0;
   1768 	type = name[0];
   1769 
   1770 	if (namelen != 4)
   1771 		return (EINVAL);
   1772 	pid = name[1];
   1773 	elem_size = name[2];
   1774 	elem_count = name[3];
   1775 
   1776 	p = pfind(pid);
   1777 	if (p == NULL)
   1778 		return (ESRCH);
   1779 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1780 		if (buflen >= elem_size && elem_count > 0) {
   1781 			fill_lwp(l, &klwp);
   1782 			/*
   1783 			 * Copy out elem_size, but not larger than
   1784 			 * the size of a struct kinfo_proc2.
   1785 			 */
   1786 			error = copyout(&klwp, dp,
   1787 			    min(sizeof(klwp), elem_size));
   1788 			if (error)
   1789 				goto cleanup;
   1790 			dp += elem_size;
   1791 			buflen -= elem_size;
   1792 			elem_count--;
   1793 		}
   1794 		needed += elem_size;
   1795 	}
   1796 
   1797 	if (where != NULL) {
   1798 		*sizep = dp - where;
   1799 		if (needed > *sizep)
   1800 			return (ENOMEM);
   1801 	} else {
   1802 		needed += KERN_PROCSLOP;
   1803 		*sizep = needed;
   1804 	}
   1805 	return (0);
   1806  cleanup:
   1807 	return (error);
   1808 }
   1809 
   1810 /*
   1811  * Fill in an eproc structure for the specified process.
   1812  */
   1813 void
   1814 fill_eproc(struct proc *p, struct eproc *ep)
   1815 {
   1816 	struct tty *tp;
   1817 	struct lwp *l;
   1818 
   1819 	ep->e_paddr = p;
   1820 	ep->e_sess = p->p_session;
   1821 	ep->e_pcred = *p->p_cred;
   1822 	ep->e_ucred = *p->p_ucred;
   1823 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   1824 		ep->e_vm.vm_rssize = 0;
   1825 		ep->e_vm.vm_tsize = 0;
   1826 		ep->e_vm.vm_dsize = 0;
   1827 		ep->e_vm.vm_ssize = 0;
   1828 		/* ep->e_vm.vm_pmap = XXX; */
   1829 	} else {
   1830 		struct vmspace *vm = p->p_vmspace;
   1831 
   1832 		ep->e_vm.vm_rssize = vm_resident_count(vm);
   1833 		ep->e_vm.vm_tsize = vm->vm_tsize;
   1834 		ep->e_vm.vm_dsize = vm->vm_dsize;
   1835 		ep->e_vm.vm_ssize = vm->vm_ssize;
   1836 
   1837 		/* Pick a "representative" LWP */
   1838 		l = proc_representative_lwp(p);
   1839 
   1840 		if (l->l_wmesg)
   1841 			strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
   1842 	}
   1843 	if (p->p_pptr)
   1844 		ep->e_ppid = p->p_pptr->p_pid;
   1845 	else
   1846 		ep->e_ppid = 0;
   1847 	ep->e_pgid = p->p_pgrp->pg_id;
   1848 	ep->e_sid = ep->e_sess->s_sid;
   1849 	ep->e_jobc = p->p_pgrp->pg_jobc;
   1850 	if ((p->p_flag & P_CONTROLT) &&
   1851 	    (tp = ep->e_sess->s_ttyp)) {
   1852 		ep->e_tdev = tp->t_dev;
   1853 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   1854 		ep->e_tsess = tp->t_session;
   1855 	} else
   1856 		ep->e_tdev = NODEV;
   1857 
   1858 	ep->e_xsize = ep->e_xrssize = 0;
   1859 	ep->e_xccount = ep->e_xswrss = 0;
   1860 	ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
   1861 	if (SESS_LEADER(p))
   1862 		ep->e_flag |= EPROC_SLEADER;
   1863 	strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
   1864 }
   1865 
   1866 /*
   1867  * Fill in an eproc structure for the specified process.
   1868  */
   1869 static void
   1870 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki)
   1871 {
   1872 	struct tty *tp;
   1873 	struct lwp *l;
   1874 	struct timeval ut, st;
   1875 
   1876 	memset(ki, 0, sizeof(*ki));
   1877 
   1878 	ki->p_paddr = PTRTOINT64(p);
   1879 	ki->p_fd = PTRTOINT64(p->p_fd);
   1880 	ki->p_cwdi = PTRTOINT64(p->p_cwdi);
   1881 	ki->p_stats = PTRTOINT64(p->p_stats);
   1882 	ki->p_limit = PTRTOINT64(p->p_limit);
   1883 	ki->p_vmspace = PTRTOINT64(p->p_vmspace);
   1884 	ki->p_sigacts = PTRTOINT64(p->p_sigacts);
   1885 	ki->p_sess = PTRTOINT64(p->p_session);
   1886 	ki->p_tsess = 0;	/* may be changed if controlling tty below */
   1887 	ki->p_ru = PTRTOINT64(p->p_ru);
   1888 
   1889 	ki->p_eflag = 0;
   1890 	ki->p_exitsig = p->p_exitsig;
   1891 	ki->p_flag = p->p_flag;
   1892 
   1893 	ki->p_pid = p->p_pid;
   1894 	if (p->p_pptr)
   1895 		ki->p_ppid = p->p_pptr->p_pid;
   1896 	else
   1897 		ki->p_ppid = 0;
   1898 	ki->p_sid = p->p_session->s_sid;
   1899 	ki->p__pgid = p->p_pgrp->pg_id;
   1900 
   1901 	ki->p_tpgid = NO_PGID;	/* may be changed if controlling tty below */
   1902 
   1903 	ki->p_uid = p->p_ucred->cr_uid;
   1904 	ki->p_ruid = p->p_cred->p_ruid;
   1905 	ki->p_gid = p->p_ucred->cr_gid;
   1906 	ki->p_rgid = p->p_cred->p_rgid;
   1907 	ki->p_svuid = p->p_cred->p_svuid;
   1908 	ki->p_svgid = p->p_cred->p_svgid;
   1909 
   1910 	memcpy(ki->p_groups, p->p_cred->pc_ucred->cr_groups,
   1911 	    min(sizeof(ki->p_groups), sizeof(p->p_cred->pc_ucred->cr_groups)));
   1912 	ki->p_ngroups = p->p_cred->pc_ucred->cr_ngroups;
   1913 
   1914 	ki->p_jobc = p->p_pgrp->pg_jobc;
   1915 	if ((p->p_flag & P_CONTROLT) && (tp = p->p_session->s_ttyp)) {
   1916 		ki->p_tdev = tp->t_dev;
   1917 		ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   1918 		ki->p_tsess = PTRTOINT64(tp->t_session);
   1919 	} else {
   1920 		ki->p_tdev = NODEV;
   1921 	}
   1922 
   1923 	ki->p_estcpu = p->p_estcpu;
   1924 	ki->p_rtime_sec = p->p_rtime.tv_sec;
   1925 	ki->p_rtime_usec = p->p_rtime.tv_usec;
   1926 	ki->p_cpticks = p->p_cpticks;
   1927 	ki->p_pctcpu = p->p_pctcpu;
   1928 
   1929 	ki->p_uticks = p->p_uticks;
   1930 	ki->p_sticks = p->p_sticks;
   1931 	ki->p_iticks = p->p_iticks;
   1932 
   1933 	ki->p_tracep = PTRTOINT64(p->p_tracep);
   1934 	ki->p_traceflag = p->p_traceflag;
   1935 
   1936 
   1937 	memcpy(&ki->p_siglist, &p->p_sigctx.ps_siglist, sizeof(ki_sigset_t));
   1938 	memcpy(&ki->p_sigmask, &p->p_sigctx.ps_sigmask, sizeof(ki_sigset_t));
   1939 	memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
   1940 	memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
   1941 
   1942 	ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
   1943 	ki->p_realstat = p->p_stat;
   1944 	ki->p_nice = p->p_nice;
   1945 
   1946 	ki->p_xstat = p->p_xstat;
   1947 	ki->p_acflag = p->p_acflag;
   1948 
   1949 	strncpy(ki->p_comm, p->p_comm,
   1950 	    min(sizeof(ki->p_comm), sizeof(p->p_comm)));
   1951 
   1952 	strncpy(ki->p_login, p->p_session->s_login,
   1953 	    min(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
   1954 
   1955 	ki->p_nlwps = p->p_nlwps;
   1956 	ki->p_nrlwps = p->p_nrlwps;
   1957 	ki->p_realflag = p->p_flag;
   1958 
   1959 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   1960 		ki->p_vm_rssize = 0;
   1961 		ki->p_vm_tsize = 0;
   1962 		ki->p_vm_dsize = 0;
   1963 		ki->p_vm_ssize = 0;
   1964 		l = NULL;
   1965 	} else {
   1966 		struct vmspace *vm = p->p_vmspace;
   1967 
   1968 		ki->p_vm_rssize = vm_resident_count(vm);
   1969 		ki->p_vm_tsize = vm->vm_tsize;
   1970 		ki->p_vm_dsize = vm->vm_dsize;
   1971 		ki->p_vm_ssize = vm->vm_ssize;
   1972 
   1973 		/* Pick a "representative" LWP */
   1974 		l = proc_representative_lwp(p);
   1975 		ki->p_forw = PTRTOINT64(l->l_forw);
   1976 		ki->p_back = PTRTOINT64(l->l_back);
   1977 		ki->p_addr = PTRTOINT64(l->l_addr);
   1978 		ki->p_stat = l->l_stat;
   1979 		ki->p_flag |= l->l_flag;
   1980 		ki->p_swtime = l->l_swtime;
   1981 		ki->p_slptime = l->l_slptime;
   1982 		if (l->l_stat == LSONPROC) {
   1983 			KDASSERT(l->l_cpu != NULL);
   1984 			ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   1985 		} else
   1986 			ki->p_schedflags = 0;
   1987 		ki->p_holdcnt = l->l_holdcnt;
   1988 		ki->p_priority = l->l_priority;
   1989 		ki->p_usrpri = l->l_usrpri;
   1990 		if (l->l_wmesg)
   1991 			strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
   1992 		ki->p_wchan = PTRTOINT64(l->l_wchan);
   1993 
   1994 	}
   1995 
   1996 	if (p->p_session->s_ttyvp)
   1997 		ki->p_eflag |= EPROC_CTTY;
   1998 	if (SESS_LEADER(p))
   1999 		ki->p_eflag |= EPROC_SLEADER;
   2000 
   2001 	/* XXX Is this double check necessary? */
   2002 	if (P_ZOMBIE(p)) {
   2003 		ki->p_uvalid = 0;
   2004 	} else {
   2005 		ki->p_uvalid = 1;
   2006 
   2007 		ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
   2008 		ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
   2009 
   2010 		calcru(p, &ut, &st, 0);
   2011 		ki->p_uutime_sec = ut.tv_sec;
   2012 		ki->p_uutime_usec = ut.tv_usec;
   2013 		ki->p_ustime_sec = st.tv_sec;
   2014 		ki->p_ustime_usec = st.tv_usec;
   2015 
   2016 		ki->p_uru_maxrss = p->p_stats->p_ru.ru_maxrss;
   2017 		ki->p_uru_ixrss = p->p_stats->p_ru.ru_ixrss;
   2018 		ki->p_uru_idrss = p->p_stats->p_ru.ru_idrss;
   2019 		ki->p_uru_isrss = p->p_stats->p_ru.ru_isrss;
   2020 		ki->p_uru_minflt = p->p_stats->p_ru.ru_minflt;
   2021 		ki->p_uru_majflt = p->p_stats->p_ru.ru_majflt;
   2022 		ki->p_uru_nswap = p->p_stats->p_ru.ru_nswap;
   2023 		ki->p_uru_inblock = p->p_stats->p_ru.ru_inblock;
   2024 		ki->p_uru_oublock = p->p_stats->p_ru.ru_oublock;
   2025 		ki->p_uru_msgsnd = p->p_stats->p_ru.ru_msgsnd;
   2026 		ki->p_uru_msgrcv = p->p_stats->p_ru.ru_msgrcv;
   2027 		ki->p_uru_nsignals = p->p_stats->p_ru.ru_nsignals;
   2028 		ki->p_uru_nvcsw = p->p_stats->p_ru.ru_nvcsw;
   2029 		ki->p_uru_nivcsw = p->p_stats->p_ru.ru_nivcsw;
   2030 
   2031 		timeradd(&p->p_stats->p_cru.ru_utime,
   2032 			 &p->p_stats->p_cru.ru_stime, &ut);
   2033 		ki->p_uctime_sec = ut.tv_sec;
   2034 		ki->p_uctime_usec = ut.tv_usec;
   2035 	}
   2036 #ifdef MULTIPROCESSOR
   2037 	if (l && l->l_cpu != NULL)
   2038 		ki->p_cpuid = l->l_cpu->ci_cpuid;
   2039 	else
   2040 #endif
   2041 		ki->p_cpuid = KI_NOCPU;
   2042 
   2043 }
   2044 
   2045 /*
   2046  * Fill in a kinfo_lwp structure for the specified lwp.
   2047  */
   2048 static void
   2049 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
   2050 {
   2051 
   2052 	kl->l_forw = PTRTOINT64(l->l_forw);
   2053 	kl->l_back = PTRTOINT64(l->l_back);
   2054 	kl->l_laddr = PTRTOINT64(l);
   2055 	kl->l_addr = PTRTOINT64(l->l_addr);
   2056 	kl->l_stat = l->l_stat;
   2057 	kl->l_lid = l->l_lid;
   2058 	kl->l_flag = l->l_flag;
   2059 
   2060 	kl->l_swtime = l->l_swtime;
   2061 	kl->l_slptime = l->l_slptime;
   2062 	if (l->l_stat == LSONPROC) {
   2063 		KDASSERT(l->l_cpu != NULL);
   2064 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   2065 	} else
   2066 		kl->l_schedflags = 0;
   2067 	kl->l_holdcnt = l->l_holdcnt;
   2068 	kl->l_priority = l->l_priority;
   2069 	kl->l_usrpri = l->l_usrpri;
   2070 	if (l->l_wmesg)
   2071 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
   2072 	kl->l_wchan = PTRTOINT64(l->l_wchan);
   2073 #ifdef MULTIPROCESSOR
   2074 	if (l->l_cpu != NULL)
   2075 		kl->l_cpuid = l->l_cpu->ci_cpuid;
   2076 	else
   2077 #endif
   2078 		kl->l_cpuid = KI_NOCPU;
   2079 }
   2080 
   2081 int
   2082 sysctl_procargs(int *name, u_int namelen, void *where, size_t *sizep,
   2083     struct proc *up)
   2084 {
   2085 	struct ps_strings pss;
   2086 	struct proc *p;
   2087 	size_t len, upper_bound, xlen, i;
   2088 	struct uio auio;
   2089 	struct iovec aiov;
   2090 	vaddr_t argv;
   2091 	pid_t pid;
   2092 	int nargv, type, error;
   2093 	char *arg;
   2094 	char *tmp;
   2095 
   2096 	if (namelen != 2)
   2097 		return (EINVAL);
   2098 	pid = name[0];
   2099 	type = name[1];
   2100 
   2101 	switch (type) {
   2102 	case KERN_PROC_ARGV:
   2103 	case KERN_PROC_NARGV:
   2104 	case KERN_PROC_ENV:
   2105 	case KERN_PROC_NENV:
   2106 		/* ok */
   2107 		break;
   2108 	default:
   2109 		return (EINVAL);
   2110 	}
   2111 
   2112 	/* check pid */
   2113 	if ((p = pfind(pid)) == NULL)
   2114 		return (EINVAL);
   2115 
   2116 	/* only root or same user change look at the environment */
   2117 	if (type == KERN_PROC_ENV || type == KERN_PROC_NENV) {
   2118 		if (up->p_ucred->cr_uid != 0) {
   2119 			if (up->p_cred->p_ruid != p->p_cred->p_ruid ||
   2120 			    up->p_cred->p_ruid != p->p_cred->p_svuid)
   2121 				return (EPERM);
   2122 		}
   2123 	}
   2124 
   2125 	if (sizep != NULL && where == NULL) {
   2126 		if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
   2127 			*sizep = sizeof (int);
   2128 		else
   2129 			*sizep = ARG_MAX;	/* XXX XXX XXX */
   2130 		return (0);
   2131 	}
   2132 	if (where == NULL || sizep == NULL)
   2133 		return (EINVAL);
   2134 
   2135 	/*
   2136 	 * Zombies don't have a stack, so we can't read their psstrings.
   2137 	 * System processes also don't have a user stack.
   2138 	 */
   2139 	if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0)
   2140 		return (EINVAL);
   2141 
   2142 	/*
   2143 	 * Lock the process down in memory.
   2144 	 */
   2145 	/* XXXCDC: how should locking work here? */
   2146 	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
   2147 		return (EFAULT);
   2148 
   2149 	p->p_vmspace->vm_refcnt++;	/* XXX */
   2150 
   2151 	/*
   2152 	 * Allocate a temporary buffer to hold the arguments.
   2153 	 */
   2154 	arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
   2155 
   2156 	/*
   2157 	 * Read in the ps_strings structure.
   2158 	 */
   2159 	aiov.iov_base = &pss;
   2160 	aiov.iov_len = sizeof(pss);
   2161 	auio.uio_iov = &aiov;
   2162 	auio.uio_iovcnt = 1;
   2163 	auio.uio_offset = (vaddr_t)p->p_psstr;
   2164 	auio.uio_resid = sizeof(pss);
   2165 	auio.uio_segflg = UIO_SYSSPACE;
   2166 	auio.uio_rw = UIO_READ;
   2167 	auio.uio_procp = NULL;
   2168 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
   2169 	if (error)
   2170 		goto done;
   2171 
   2172 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
   2173 		memcpy(&nargv, (char *)&pss + p->p_psnargv, sizeof(nargv));
   2174 	else
   2175 		memcpy(&nargv, (char *)&pss + p->p_psnenv, sizeof(nargv));
   2176 	if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
   2177 		error = copyout(&nargv, where, sizeof(nargv));
   2178 		*sizep = sizeof(nargv);
   2179 		goto done;
   2180 	}
   2181 	/*
   2182 	 * Now read the address of the argument vector.
   2183 	 */
   2184 	switch (type) {
   2185 	case KERN_PROC_ARGV:
   2186 		/* XXX compat32 stuff here */
   2187 		memcpy(&tmp, (char *)&pss + p->p_psargv, sizeof(tmp));
   2188 		break;
   2189 	case KERN_PROC_ENV:
   2190 		memcpy(&tmp, (char *)&pss + p->p_psenv, sizeof(tmp));
   2191 		break;
   2192 	default:
   2193 		return (EINVAL);
   2194 	}
   2195 	auio.uio_offset = (off_t)(long)tmp;
   2196 	aiov.iov_base = &argv;
   2197 	aiov.iov_len = sizeof(argv);
   2198 	auio.uio_iov = &aiov;
   2199 	auio.uio_iovcnt = 1;
   2200 	auio.uio_resid = sizeof(argv);
   2201 	auio.uio_segflg = UIO_SYSSPACE;
   2202 	auio.uio_rw = UIO_READ;
   2203 	auio.uio_procp = NULL;
   2204 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
   2205 	if (error)
   2206 		goto done;
   2207 
   2208 	/*
   2209 	 * Now copy in the actual argument vector, one page at a time,
   2210 	 * since we don't know how long the vector is (though, we do
   2211 	 * know how many NUL-terminated strings are in the vector).
   2212 	 */
   2213 	len = 0;
   2214 	upper_bound = *sizep;
   2215 	for (; nargv != 0 && len < upper_bound; len += xlen) {
   2216 		aiov.iov_base = arg;
   2217 		aiov.iov_len = PAGE_SIZE;
   2218 		auio.uio_iov = &aiov;
   2219 		auio.uio_iovcnt = 1;
   2220 		auio.uio_offset = argv + len;
   2221 		xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
   2222 		auio.uio_resid = xlen;
   2223 		auio.uio_segflg = UIO_SYSSPACE;
   2224 		auio.uio_rw = UIO_READ;
   2225 		auio.uio_procp = NULL;
   2226 		error = uvm_io(&p->p_vmspace->vm_map, &auio);
   2227 		if (error)
   2228 			goto done;
   2229 
   2230 		for (i = 0; i < xlen && nargv != 0; i++) {
   2231 			if (arg[i] == '\0')
   2232 				nargv--;	/* one full string */
   2233 		}
   2234 
   2235 		/*
   2236 		 * Make sure we don't copyout past the end of the user's
   2237 		 * buffer.
   2238 		 */
   2239 		if (len + i > upper_bound)
   2240 			i = upper_bound - len;
   2241 
   2242 		error = copyout(arg, (char *)where + len, i);
   2243 		if (error)
   2244 			break;
   2245 
   2246 		if (nargv == 0) {
   2247 			len += i;
   2248 			break;
   2249 		}
   2250 	}
   2251 	*sizep = len;
   2252 
   2253 done:
   2254 	uvmspace_free(p->p_vmspace);
   2255 
   2256 	free(arg, M_TEMP);
   2257 	return (error);
   2258 }
   2259 
   2260 #if NPTY > 0
   2261 int pty_maxptys(int, int);		/* defined in kern/tty_pty.c */
   2262 
   2263 /*
   2264  * Validate parameters and get old / set new parameters
   2265  * for pty sysctl function.
   2266  */
   2267 static int
   2268 sysctl_pty(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
   2269 {
   2270 	int error = 0;
   2271 	int oldmax = 0, newmax = 0;
   2272 
   2273 	/* get current value of maxptys */
   2274 	oldmax = pty_maxptys(0, 0);
   2275 
   2276 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &oldmax, int)
   2277 
   2278 	if (!error && newp) {
   2279 		SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
   2280 		SYSCTL_SCALAR_NEWPCOP_TYP(newp, &newmax, int)
   2281 
   2282 		if (newmax != pty_maxptys(newmax, (newp != NULL)))
   2283 			return (EINVAL);
   2284 
   2285 	}
   2286 
   2287 	return (error);
   2288 }
   2289 #endif /* NPTY > 0 */
   2290 
   2291 static int
   2292 sysctl_dotkstat(int *name, u_int namelen, void *where, size_t *sizep,
   2293     void *newp)
   2294 {
   2295 
   2296 	/* all sysctl names at this level are terminal */
   2297 	if (namelen != 1)
   2298 		return (ENOTDIR);		/* overloaded */
   2299 
   2300 	switch (name[0]) {
   2301 	case KERN_TKSTAT_NIN:
   2302 		return (sysctl_rdquad(where, sizep, newp, tk_nin));
   2303 	case KERN_TKSTAT_NOUT:
   2304 		return (sysctl_rdquad(where, sizep, newp, tk_nout));
   2305 	case KERN_TKSTAT_CANCC:
   2306 		return (sysctl_rdquad(where, sizep, newp, tk_cancc));
   2307 	case KERN_TKSTAT_RAWCC:
   2308 		return (sysctl_rdquad(where, sizep, newp, tk_rawcc));
   2309 	default:
   2310 		return (EOPNOTSUPP);
   2311 	}
   2312 }
   2313