Home | History | Annotate | Line # | Download | only in kern
kern_sysctl.c revision 1.149
      1 /*	$NetBSD: kern_sysctl.c,v 1.149 2003/10/03 15:33:42 christos 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.149 2003/10/03 15:33:42 christos 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 		break;
    869 
    870 	case PROC_PID_STOPEXEC:
    871 		if (namelen != 2)
    872 			return (EINVAL);
    873 		si = ((ptmp->p_flag & P_STOPEXEC) != 0);
    874 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &si)) != 0)
    875 			return (error);
    876 		if (si != 0)
    877 			ptmp->p_flag |= P_STOPEXEC;
    878 		else
    879 			ptmp->p_flag &= ~P_STOPEXEC;
    880 		return (0);
    881 		break;
    882 
    883 	case PROC_PID_CORENAME:
    884 		if (namelen != 2)
    885 			return (EINVAL);
    886 		/*
    887 		 * Can't use sysctl_string() here because we may malloc a new
    888 		 * area during the process, so we have to do it by hand.
    889 		 */
    890 		curlen = strlen(ptmp->p_limit->pl_corename) + 1;
    891 		if (oldlenp && *oldlenp < curlen) {
    892 			if (!oldp)
    893 				*oldlenp = curlen;
    894 			return (ENOMEM);
    895 		}
    896 		if (newp) {
    897 			if (securelevel > 2)
    898 				return (EPERM);
    899 			if (newlen > MAXPATHLEN)
    900 				return (ENAMETOOLONG);
    901 			tmps = malloc(newlen + 1, M_TEMP, M_WAITOK);
    902 			if (tmps == NULL)
    903 				return (ENOMEM);
    904 			error = copyin(newp, tmps, newlen + 1);
    905 			tmps[newlen] = '\0';
    906 			if (error)
    907 				goto cleanup;
    908 			/* Enforce to be either 'core' for end with '.core' */
    909 			if (newlen < 4) {	/* c.o.r.e */
    910 				error = EINVAL;
    911 				goto cleanup;
    912 			}
    913 			len = newlen - 4;
    914 			if (len > 0) {
    915 				if (tmps[len - 1] != '.' &&
    916 				    tmps[len - 1] != '/') {
    917 					error = EINVAL;
    918 					goto cleanup;
    919 				}
    920 			}
    921 			if (strcmp(&tmps[len], "core") != 0) {
    922 				error = EINVAL;
    923 				goto cleanup;
    924 			}
    925 		}
    926 		if (oldp && oldlenp) {
    927 			*oldlenp = curlen;
    928 			error = copyout(ptmp->p_limit->pl_corename, oldp,
    929 			    curlen);
    930 		}
    931 		if (newp && error == 0) {
    932 			/* if the 2 strings are identical, don't limcopy() */
    933 			if (strcmp(tmps, ptmp->p_limit->pl_corename) == 0) {
    934 				error = 0;
    935 				goto cleanup;
    936 			}
    937 			if (ptmp->p_limit->p_refcnt > 1 &&
    938 			    (ptmp->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    939 				newplim = limcopy(ptmp->p_limit);
    940 				limfree(ptmp->p_limit);
    941 				ptmp->p_limit = newplim;
    942 			}
    943 			if (ptmp->p_limit->pl_corename != defcorename) {
    944 				free(ptmp->p_limit->pl_corename, M_TEMP);
    945 			}
    946 			ptmp->p_limit->pl_corename = tmps;
    947 			return (0);
    948 		}
    949 cleanup:
    950 		if (tmps)
    951 			free(tmps, M_TEMP);
    952 		return (error);
    953 		break;
    954 
    955 	case PROC_PID_LIMIT:
    956 		if (namelen != 4 || name[2] < 1 ||
    957 		    name[2] >= PROC_PID_LIMIT_MAXID)
    958 			return (EINVAL);
    959 		memcpy(&alim, &ptmp->p_rlimit[name[2] - 1], sizeof(alim));
    960 		if (name[3] == PROC_PID_LIMIT_TYPE_HARD)
    961 			error = sysctl_quad(oldp, oldlenp, newp, newlen,
    962 			    &alim.rlim_max);
    963 		else if (name[3] == PROC_PID_LIMIT_TYPE_SOFT)
    964 			error = sysctl_quad(oldp, oldlenp, newp, newlen,
    965 			    &alim.rlim_cur);
    966 		else
    967 			error = (EINVAL);
    968 
    969 		if (error)
    970 			return (error);
    971 
    972 		if (newp)
    973 			error = dosetrlimit(ptmp, p->p_cred,
    974 			    name[2] - 1, &alim);
    975 		return (error);
    976 		break;
    977 
    978 	default:
    979 		return (EINVAL);
    980 		break;
    981 	}
    982 	/* NOTREACHED */
    983 	return (EINVAL);
    984 }
    985 
    986 int
    987 emul_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    988     void *newp, size_t newlen, struct proc *p)
    989 {
    990 	static struct {
    991 		const char *name;
    992 		int type;
    993 	} emulations[] = CTL_EMUL_NAMES;
    994 	const struct emul *e;
    995 	const char *ename;
    996 #ifdef LKM
    997 	extern struct lock exec_lock;	/* XXX */
    998 	int error;
    999 #else
   1000 	extern int nexecs_builtin;
   1001 	extern const struct execsw execsw_builtin[];
   1002 	int i;
   1003 #endif
   1004 
   1005 	/* all sysctl names at this level are name and field */
   1006 	if (namelen < 2)
   1007 		return (ENOTDIR);		/* overloaded */
   1008 
   1009 	if ((u_int) name[0] >= EMUL_MAXID || name[0] == 0)
   1010 		return (EOPNOTSUPP);
   1011 
   1012 	ename = emulations[name[0]].name;
   1013 
   1014 #ifdef LKM
   1015 	lockmgr(&exec_lock, LK_SHARED, NULL);
   1016 	if ((e = emul_search(ename))) {
   1017 		error = (*e->e_sysctl)(name + 1, namelen - 1, oldp, oldlenp,
   1018 		    newp, newlen, p);
   1019 	} else
   1020 		error = EOPNOTSUPP;
   1021 	lockmgr(&exec_lock, LK_RELEASE, NULL);
   1022 
   1023 	return (error);
   1024 #else
   1025 	for (i = 0; i < nexecs_builtin; i++) {
   1026 		e = execsw_builtin[i].es_emul;
   1027 		/*
   1028 		 * In order to match e.g. e->e_name "irix o32"
   1029 		 * with ename "irix", we limit the comparison
   1030 		 * to the length of ename.
   1031 		 */
   1032 		if (e == NULL ||
   1033 		    strncmp(ename, e->e_name, strlen(ename)) != 0 ||
   1034 		    e->e_sysctl == NULL)
   1035 			continue;
   1036 
   1037 		return ((*e->e_sysctl)(name + 1, namelen - 1, oldp, oldlenp,
   1038 		    newp, newlen, p));
   1039 	}
   1040 
   1041 	return (EOPNOTSUPP);
   1042 #endif
   1043 }
   1044 /*
   1045  * Convenience macros.
   1046  */
   1047 
   1048 #define SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, len) 		\
   1049 	if (oldlenp) {							\
   1050 		if (!oldp)						\
   1051 			*oldlenp = len;					\
   1052 		else {							\
   1053 			if (*oldlenp < len)				\
   1054 				return (ENOMEM);			\
   1055 			*oldlenp = len;					\
   1056 			error = copyout(valp, oldp, len);		\
   1057 		}							\
   1058 	}
   1059 
   1060 #define SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, typ) \
   1061 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, sizeof(typ))
   1062 
   1063 #define SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len)	\
   1064 	if (newp && newlen != len)			\
   1065 		return (EINVAL);
   1066 
   1067 #define SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, typ)	\
   1068 	SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, sizeof(typ))
   1069 
   1070 #define SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, len)	\
   1071 	if (error == 0 && newp)				\
   1072 		error = copyin(newp, valp, len);
   1073 
   1074 #define SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, typ)	\
   1075 	SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, sizeof(typ))
   1076 
   1077 #define SYSCTL_STRING_CORE(oldp, oldlenp, str)		\
   1078 	if (oldlenp) {					\
   1079 		len = strlen(str) + 1;			\
   1080 		if (!oldp)				\
   1081 			*oldlenp = len;			\
   1082 		else {					\
   1083 			if (*oldlenp < len) {		\
   1084 				err2 = ENOMEM;		\
   1085 				len = *oldlenp;		\
   1086 			} else				\
   1087 				*oldlenp = len;		\
   1088 			error = copyout(str, oldp, len);\
   1089 			if (error == 0)			\
   1090 				error = err2;		\
   1091 		}					\
   1092 	}
   1093 
   1094 /*
   1095  * Validate parameters and get old / set new parameters
   1096  * for an integer-valued sysctl function.
   1097  */
   1098 int
   1099 sysctl_int(void *oldp, size_t *oldlenp, void *newp, size_t newlen, int *valp)
   1100 {
   1101 	int error = 0;
   1102 
   1103 	SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
   1104 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, int)
   1105 	SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, int)
   1106 
   1107 	return (error);
   1108 }
   1109 
   1110 
   1111 /*
   1112  * As above, but read-only.
   1113  */
   1114 int
   1115 sysctl_rdint(void *oldp, size_t *oldlenp, void *newp, int val)
   1116 {
   1117 	int error = 0;
   1118 
   1119 	if (newp)
   1120 		return (EPERM);
   1121 
   1122 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, int)
   1123 
   1124 	return (error);
   1125 }
   1126 
   1127 /*
   1128  * Validate parameters and get old / set new parameters
   1129  * for an quad-valued sysctl function.
   1130  */
   1131 int
   1132 sysctl_quad(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
   1133     quad_t *valp)
   1134 {
   1135 	int error = 0;
   1136 
   1137 	SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, quad_t)
   1138 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, quad_t)
   1139 	SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, quad_t)
   1140 
   1141 	return (error);
   1142 }
   1143 
   1144 /*
   1145  * As above, but read-only.
   1146  */
   1147 int
   1148 sysctl_rdquad(void *oldp, size_t *oldlenp, void *newp, quad_t val)
   1149 {
   1150 	int error = 0;
   1151 
   1152 	if (newp)
   1153 		return (EPERM);
   1154 
   1155 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, quad_t)
   1156 
   1157 	return (error);
   1158 }
   1159 
   1160 /*
   1161  * Validate parameters and get old / set new parameters
   1162  * for a string-valued sysctl function.
   1163  */
   1164 int
   1165 sysctl_string(void *oldp, size_t *oldlenp, void *newp, size_t newlen, char *str,
   1166     size_t maxlen)
   1167 {
   1168 	int error = 0, err2 = 0;
   1169 	size_t len;
   1170 
   1171 	if (newp && newlen >= maxlen)
   1172 		return (EINVAL);
   1173 
   1174 	SYSCTL_STRING_CORE(oldp, oldlenp, str);
   1175 
   1176 	if (error == 0 && newp) {
   1177 		error = copyin(newp, str, newlen);
   1178 		str[newlen] = 0;
   1179 	}
   1180 	return (error);
   1181 }
   1182 
   1183 /*
   1184  * As above, but read-only.
   1185  */
   1186 int
   1187 sysctl_rdstring(void *oldp, size_t *oldlenp, void *newp, const char *str)
   1188 {
   1189 	int error = 0, err2 = 0;
   1190 	size_t len;
   1191 
   1192 	if (newp)
   1193 		return (EPERM);
   1194 
   1195 	SYSCTL_STRING_CORE(oldp, oldlenp, str);
   1196 
   1197 	return (error);
   1198 }
   1199 
   1200 /*
   1201  * Validate parameters and get old / set new parameters
   1202  * for a structure oriented sysctl function.
   1203  */
   1204 int
   1205 sysctl_struct(void *oldp, size_t *oldlenp, void *newp, size_t newlen, void *sp,
   1206     size_t len)
   1207 {
   1208 	int error = 0;
   1209 
   1210 	SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len)
   1211 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
   1212 	SYSCTL_SCALAR_NEWPCOP_LEN(newp, sp, len)
   1213 
   1214 	return (error);
   1215 }
   1216 
   1217 /*
   1218  * Validate parameters and get old parameters
   1219  * for a structure oriented sysctl function.
   1220  */
   1221 int
   1222 sysctl_rdstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
   1223     size_t len)
   1224 {
   1225 	int error = 0;
   1226 
   1227 	if (newp)
   1228 		return (EPERM);
   1229 
   1230 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
   1231 
   1232 	return (error);
   1233 }
   1234 
   1235 /*
   1236  * As above, but can return a truncated result.
   1237  */
   1238 int
   1239 sysctl_rdminstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
   1240     size_t len)
   1241 {
   1242 	int error = 0;
   1243 
   1244 	if (newp)
   1245 		return (EPERM);
   1246 
   1247 	len = min(*oldlenp, len);
   1248 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
   1249 
   1250 	return (error);
   1251 }
   1252 
   1253 /*
   1254  * Get file structures.
   1255  */
   1256 static int
   1257 sysctl_file(void *vwhere, size_t *sizep)
   1258 {
   1259 	int error;
   1260 	size_t buflen;
   1261 	struct file *fp;
   1262 	char *start, *where;
   1263 
   1264 	start = where = vwhere;
   1265 	buflen = *sizep;
   1266 	if (where == NULL) {
   1267 		/*
   1268 		 * overestimate by 10 files
   1269 		 */
   1270 		*sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
   1271 		return (0);
   1272 	}
   1273 
   1274 	/*
   1275 	 * first copyout filehead
   1276 	 */
   1277 	if (buflen < sizeof(filehead)) {
   1278 		*sizep = 0;
   1279 		return (0);
   1280 	}
   1281 	error = copyout(&filehead, where, sizeof(filehead));
   1282 	if (error)
   1283 		return (error);
   1284 	buflen -= sizeof(filehead);
   1285 	where += sizeof(filehead);
   1286 
   1287 	/*
   1288 	 * followed by an array of file structures
   1289 	 */
   1290 	LIST_FOREACH(fp, &filehead, f_list) {
   1291 		if (buflen < sizeof(struct file)) {
   1292 			*sizep = where - start;
   1293 			return (ENOMEM);
   1294 		}
   1295 		error = copyout(fp, where, sizeof(struct file));
   1296 		if (error)
   1297 			return (error);
   1298 		buflen -= sizeof(struct file);
   1299 		where += sizeof(struct file);
   1300 	}
   1301 	*sizep = where - start;
   1302 	return (0);
   1303 }
   1304 
   1305 /*
   1306  * Get driver names and majors
   1307  */
   1308 static int
   1309 sysctl_drivers(void *vwhere, size_t *sizep)
   1310 {
   1311 	int error;
   1312 	size_t buflen;
   1313 	struct kinfo_drivers kd;
   1314 	char *start, *where;
   1315 	const char *name;
   1316 	int i;
   1317 	extern struct devsw_conv *devsw_conv;
   1318 	extern int max_devsw_convs;
   1319 
   1320 	start = where = vwhere;
   1321 	buflen = *sizep;
   1322 	if (where == NULL) {
   1323 		*sizep = max_devsw_convs * sizeof kd;
   1324 		return 0;
   1325 	}
   1326 
   1327 	/*
   1328 	 * An array of kinfo_drivers structures
   1329 	 */
   1330 	error = 0;
   1331 	for (i = 0; i < max_devsw_convs; i++) {
   1332 		name = devsw_conv[i].d_name;
   1333 		if (name == NULL)
   1334 			continue;
   1335 		if (buflen < sizeof kd) {
   1336 			error = ENOMEM;
   1337 			break;
   1338 		}
   1339 		kd.d_bmajor = devsw_conv[i].d_bmajor;
   1340 		kd.d_cmajor = devsw_conv[i].d_cmajor;
   1341 		strlcpy(kd.d_name, name, sizeof kd.d_name);
   1342 		error = copyout(&kd, where, sizeof kd);
   1343 		if (error != 0)
   1344 			break;
   1345 		buflen -= sizeof kd;
   1346 		where += sizeof kd;
   1347 	}
   1348 	*sizep = where - start;
   1349 	return error;
   1350 }
   1351 
   1352 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
   1353 #define	FILL_PERM(src, dst) do { \
   1354 	(dst)._key = (src)._key; \
   1355 	(dst).uid = (src).uid; \
   1356 	(dst).gid = (src).gid; \
   1357 	(dst).cuid = (src).cuid; \
   1358 	(dst).cgid = (src).cgid; \
   1359 	(dst).mode = (src).mode; \
   1360 	(dst)._seq = (src)._seq; \
   1361 } while (/*CONSTCOND*/ 0);
   1362 #define	FILL_MSG(src, dst) do { \
   1363 	FILL_PERM((src).msg_perm, (dst).msg_perm); \
   1364 	(dst).msg_qnum = (src).msg_qnum; \
   1365 	(dst).msg_qbytes = (src).msg_qbytes; \
   1366 	(dst)._msg_cbytes = (src)._msg_cbytes; \
   1367 	(dst).msg_lspid = (src).msg_lspid; \
   1368 	(dst).msg_lrpid = (src).msg_lrpid; \
   1369 	(dst).msg_stime = (src).msg_stime; \
   1370 	(dst).msg_rtime = (src).msg_rtime; \
   1371 	(dst).msg_ctime = (src).msg_ctime; \
   1372 } while (/*CONSTCOND*/ 0)
   1373 #define	FILL_SEM(src, dst) do { \
   1374 	FILL_PERM((src).sem_perm, (dst).sem_perm); \
   1375 	(dst).sem_nsems = (src).sem_nsems; \
   1376 	(dst).sem_otime = (src).sem_otime; \
   1377 	(dst).sem_ctime = (src).sem_ctime; \
   1378 } while (/*CONSTCOND*/ 0)
   1379 #define	FILL_SHM(src, dst) do { \
   1380 	FILL_PERM((src).shm_perm, (dst).shm_perm); \
   1381 	(dst).shm_segsz = (src).shm_segsz; \
   1382 	(dst).shm_lpid = (src).shm_lpid; \
   1383 	(dst).shm_cpid = (src).shm_cpid; \
   1384 	(dst).shm_atime = (src).shm_atime; \
   1385 	(dst).shm_dtime = (src).shm_dtime; \
   1386 	(dst).shm_ctime = (src).shm_ctime; \
   1387 	(dst).shm_nattch = (src).shm_nattch; \
   1388 } while (/*CONSTCOND*/ 0)
   1389 
   1390 static int
   1391 sysctl_sysvipc(int *name, u_int namelen, void *where, size_t *sizep)
   1392 {
   1393 #ifdef SYSVMSG
   1394 	struct msg_sysctl_info *msgsi = NULL;
   1395 #endif
   1396 #ifdef SYSVSEM
   1397 	struct sem_sysctl_info *semsi = NULL;
   1398 #endif
   1399 #ifdef SYSVSHM
   1400 	struct shm_sysctl_info *shmsi = NULL;
   1401 #endif
   1402 	size_t infosize, dssize, tsize, buflen;
   1403 	void *buf = NULL;
   1404 	char *start;
   1405 	int32_t nds;
   1406 	int i, error, ret;
   1407 
   1408 	if (namelen != 1)
   1409 		return (EINVAL);
   1410 
   1411 	start = where;
   1412 	buflen = *sizep;
   1413 
   1414 	switch (*name) {
   1415 	case KERN_SYSVIPC_MSG_INFO:
   1416 #ifdef SYSVMSG
   1417 		infosize = sizeof(msgsi->msginfo);
   1418 		nds = msginfo.msgmni;
   1419 		dssize = sizeof(msgsi->msgids[0]);
   1420 		break;
   1421 #else
   1422 		return (EINVAL);
   1423 #endif
   1424 	case KERN_SYSVIPC_SEM_INFO:
   1425 #ifdef SYSVSEM
   1426 		infosize = sizeof(semsi->seminfo);
   1427 		nds = seminfo.semmni;
   1428 		dssize = sizeof(semsi->semids[0]);
   1429 		break;
   1430 #else
   1431 		return (EINVAL);
   1432 #endif
   1433 	case KERN_SYSVIPC_SHM_INFO:
   1434 #ifdef SYSVSHM
   1435 		infosize = sizeof(shmsi->shminfo);
   1436 		nds = shminfo.shmmni;
   1437 		dssize = sizeof(shmsi->shmids[0]);
   1438 		break;
   1439 #else
   1440 		return (EINVAL);
   1441 #endif
   1442 	default:
   1443 		return (EINVAL);
   1444 	}
   1445 	/*
   1446 	 * Round infosize to 64 bit boundary if requesting more than just
   1447 	 * the info structure or getting the total data size.
   1448 	 */
   1449 	if (where == NULL || *sizep > infosize)
   1450 		infosize = ((infosize + 7) / 8) * 8;
   1451 	tsize = infosize + nds * dssize;
   1452 
   1453 	/* Return just the total size required. */
   1454 	if (where == NULL) {
   1455 		*sizep = tsize;
   1456 		return (0);
   1457 	}
   1458 
   1459 	/* Not enough room for even the info struct. */
   1460 	if (buflen < infosize) {
   1461 		*sizep = 0;
   1462 		return (ENOMEM);
   1463 	}
   1464 	buf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK);
   1465 	memset(buf, 0, min(tsize, buflen));
   1466 
   1467 	switch (*name) {
   1468 #ifdef SYSVMSG
   1469 	case KERN_SYSVIPC_MSG_INFO:
   1470 		msgsi = (struct msg_sysctl_info *)buf;
   1471 		msgsi->msginfo = msginfo;
   1472 		break;
   1473 #endif
   1474 #ifdef SYSVSEM
   1475 	case KERN_SYSVIPC_SEM_INFO:
   1476 		semsi = (struct sem_sysctl_info *)buf;
   1477 		semsi->seminfo = seminfo;
   1478 		break;
   1479 #endif
   1480 #ifdef SYSVSHM
   1481 	case KERN_SYSVIPC_SHM_INFO:
   1482 		shmsi = (struct shm_sysctl_info *)buf;
   1483 		shmsi->shminfo = shminfo;
   1484 		break;
   1485 #endif
   1486 	}
   1487 	buflen -= infosize;
   1488 
   1489 	ret = 0;
   1490 	if (buflen > 0) {
   1491 		/* Fill in the IPC data structures.  */
   1492 		for (i = 0; i < nds; i++) {
   1493 			if (buflen < dssize) {
   1494 				ret = ENOMEM;
   1495 				break;
   1496 			}
   1497 			switch (*name) {
   1498 #ifdef SYSVMSG
   1499 			case KERN_SYSVIPC_MSG_INFO:
   1500 				FILL_MSG(msqids[i], msgsi->msgids[i]);
   1501 				break;
   1502 #endif
   1503 #ifdef SYSVSEM
   1504 			case KERN_SYSVIPC_SEM_INFO:
   1505 				FILL_SEM(sema[i], semsi->semids[i]);
   1506 				break;
   1507 #endif
   1508 #ifdef SYSVSHM
   1509 			case KERN_SYSVIPC_SHM_INFO:
   1510 				FILL_SHM(shmsegs[i], shmsi->shmids[i]);
   1511 				break;
   1512 #endif
   1513 			}
   1514 			buflen -= dssize;
   1515 		}
   1516 	}
   1517 	*sizep -= buflen;
   1518 	error = copyout(buf, start, *sizep);
   1519 	/* If copyout succeeded, use return code set earlier. */
   1520 	if (error == 0)
   1521 		error = ret;
   1522 	if (buf)
   1523 		free(buf, M_TEMP);
   1524 	return (error);
   1525 }
   1526 #endif /* SYSVMSG || SYSVSEM || SYSVSHM */
   1527 
   1528 static int
   1529 sysctl_msgbuf(void *vwhere, size_t *sizep)
   1530 {
   1531 	char *where = vwhere;
   1532 	size_t len, maxlen = *sizep;
   1533 	long beg, end;
   1534 	int error;
   1535 
   1536 	/*
   1537 	 * deal with cases where the message buffer has
   1538 	 * become corrupted.
   1539 	 */
   1540 	if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
   1541 		msgbufenabled = 0;
   1542 		return (ENXIO);
   1543 	}
   1544 
   1545 	if (where == NULL) {
   1546 		/* always return full buffer size */
   1547 		*sizep = msgbufp->msg_bufs;
   1548 		return (0);
   1549 	}
   1550 
   1551 	error = 0;
   1552 	maxlen = min(msgbufp->msg_bufs, maxlen);
   1553 
   1554 	/*
   1555 	 * First, copy from the write pointer to the end of
   1556 	 * message buffer.
   1557 	 */
   1558 	beg = msgbufp->msg_bufx;
   1559 	end = msgbufp->msg_bufs;
   1560 	while (maxlen > 0) {
   1561 		len = min(end - beg, maxlen);
   1562 		if (len == 0)
   1563 			break;
   1564 		error = copyout(&msgbufp->msg_bufc[beg], where, len);
   1565 		if (error)
   1566 			break;
   1567 		where += len;
   1568 		maxlen -= len;
   1569 
   1570 		/*
   1571 		 * ... then, copy from the beginning of message buffer to
   1572 		 * the write pointer.
   1573 		 */
   1574 		beg = 0;
   1575 		end = msgbufp->msg_bufx;
   1576 	}
   1577 	return (error);
   1578 }
   1579 
   1580 /*
   1581  * try over estimating by 5 procs
   1582  */
   1583 #define KERN_PROCSLOP	(5 * sizeof(struct kinfo_proc))
   1584 
   1585 static int
   1586 sysctl_doeproc(int *name, u_int namelen, void *vwhere, size_t *sizep)
   1587 {
   1588 	struct eproc eproc;
   1589 	struct kinfo_proc2 kproc2;
   1590 	struct kinfo_proc *dp;
   1591 	struct proc *p;
   1592 	const struct proclist_desc *pd;
   1593 	char *where, *dp2;
   1594 	int type, op, arg;
   1595 	u_int elem_size, elem_count;
   1596 	size_t buflen, needed;
   1597 	int error;
   1598 
   1599 	dp = vwhere;
   1600 	dp2 = where = vwhere;
   1601 	buflen = where != NULL ? *sizep : 0;
   1602 	error = 0;
   1603 	needed = 0;
   1604 	type = name[0];
   1605 
   1606 	if (type == KERN_PROC) {
   1607 		if (namelen != 3 && !(namelen == 2 && name[1] == KERN_PROC_ALL))
   1608 			return (EINVAL);
   1609 		op = name[1];
   1610 		if (op != KERN_PROC_ALL)
   1611 			arg = name[2];
   1612 		else
   1613 			arg = 0;		/* Quell compiler warning */
   1614 		elem_size = elem_count = 0;	/* Ditto */
   1615 	} else {
   1616 		if (namelen != 5)
   1617 			return (EINVAL);
   1618 		op = name[1];
   1619 		arg = name[2];
   1620 		elem_size = name[3];
   1621 		elem_count = name[4];
   1622 	}
   1623 
   1624 	proclist_lock_read();
   1625 
   1626 	pd = proclists;
   1627 again:
   1628 	for (p = LIST_FIRST(pd->pd_list); p != NULL; p = LIST_NEXT(p, p_list)) {
   1629 		/*
   1630 		 * Skip embryonic processes.
   1631 		 */
   1632 		if (p->p_stat == SIDL)
   1633 			continue;
   1634 		/*
   1635 		 * TODO - make more efficient (see notes below).
   1636 		 * do by session.
   1637 		 */
   1638 		switch (op) {
   1639 
   1640 		case KERN_PROC_PID:
   1641 			/* could do this with just a lookup */
   1642 			if (p->p_pid != (pid_t)arg)
   1643 				continue;
   1644 			break;
   1645 
   1646 		case KERN_PROC_PGRP:
   1647 			/* could do this by traversing pgrp */
   1648 			if (p->p_pgrp->pg_id != (pid_t)arg)
   1649 				continue;
   1650 			break;
   1651 
   1652 		case KERN_PROC_SESSION:
   1653 			if (p->p_session->s_sid != (pid_t)arg)
   1654 				continue;
   1655 			break;
   1656 
   1657 		case KERN_PROC_TTY:
   1658 			if (arg == (int) KERN_PROC_TTY_REVOKE) {
   1659 				if ((p->p_flag & P_CONTROLT) == 0 ||
   1660 				    p->p_session->s_ttyp == NULL ||
   1661 				    p->p_session->s_ttyvp != NULL)
   1662 					continue;
   1663 			} else if ((p->p_flag & P_CONTROLT) == 0 ||
   1664 			    p->p_session->s_ttyp == NULL) {
   1665 				if ((dev_t)arg != KERN_PROC_TTY_NODEV)
   1666 					continue;
   1667 			} else if (p->p_session->s_ttyp->t_dev != (dev_t)arg)
   1668 				continue;
   1669 			break;
   1670 
   1671 		case KERN_PROC_UID:
   1672 			if (p->p_ucred->cr_uid != (uid_t)arg)
   1673 				continue;
   1674 			break;
   1675 
   1676 		case KERN_PROC_RUID:
   1677 			if (p->p_cred->p_ruid != (uid_t)arg)
   1678 				continue;
   1679 			break;
   1680 
   1681 		case KERN_PROC_GID:
   1682 			if (p->p_ucred->cr_gid != (uid_t)arg)
   1683 				continue;
   1684 			break;
   1685 
   1686 		case KERN_PROC_RGID:
   1687 			if (p->p_cred->p_rgid != (uid_t)arg)
   1688 				continue;
   1689 			break;
   1690 
   1691 		case KERN_PROC_ALL:
   1692 			/* allow everything */
   1693 			break;
   1694 
   1695 		default:
   1696 			error = EINVAL;
   1697 			goto cleanup;
   1698 		}
   1699 		if (type == KERN_PROC) {
   1700 			if (buflen >= sizeof(struct kinfo_proc)) {
   1701 				fill_eproc(p, &eproc);
   1702 				error = copyout(p, &dp->kp_proc,
   1703 				    sizeof(struct proc));
   1704 				if (error)
   1705 					goto cleanup;
   1706 				error = copyout(&eproc, &dp->kp_eproc,
   1707 				    sizeof(eproc));
   1708 				if (error)
   1709 					goto cleanup;
   1710 				dp++;
   1711 				buflen -= sizeof(struct kinfo_proc);
   1712 			}
   1713 			needed += sizeof(struct kinfo_proc);
   1714 		} else { /* KERN_PROC2 */
   1715 			if (buflen >= elem_size && elem_count > 0) {
   1716 				fill_kproc2(p, &kproc2);
   1717 				/*
   1718 				 * Copy out elem_size, but not larger than
   1719 				 * the size of a struct kinfo_proc2.
   1720 				 */
   1721 				error = copyout(&kproc2, dp2,
   1722 				    min(sizeof(kproc2), elem_size));
   1723 				if (error)
   1724 					goto cleanup;
   1725 				dp2 += elem_size;
   1726 				buflen -= elem_size;
   1727 				elem_count--;
   1728 			}
   1729 			needed += elem_size;
   1730 		}
   1731 	}
   1732 	pd++;
   1733 	if (pd->pd_list != NULL)
   1734 		goto again;
   1735 	proclist_unlock_read();
   1736 
   1737 	if (where != NULL) {
   1738 		if (type == KERN_PROC)
   1739 			*sizep = (char *)dp - where;
   1740 		else
   1741 			*sizep = dp2 - where;
   1742 		if (needed > *sizep)
   1743 			return (ENOMEM);
   1744 	} else {
   1745 		needed += KERN_PROCSLOP;
   1746 		*sizep = needed;
   1747 	}
   1748 	return (0);
   1749  cleanup:
   1750 	proclist_unlock_read();
   1751 	return (error);
   1752 }
   1753 
   1754 
   1755 /*
   1756  * try over estimating by 5 LWPs
   1757  */
   1758 #define KERN_LWPSLOP	(5 * sizeof(struct kinfo_lwp))
   1759 
   1760 static int
   1761 sysctl_dolwp(int *name, u_int namelen, void *vwhere, size_t *sizep)
   1762 {
   1763 	struct kinfo_lwp klwp;
   1764 	struct proc *p;
   1765 	struct lwp *l;
   1766 	char *where, *dp;
   1767 	int type, pid, elem_size, elem_count;
   1768 	int buflen, needed, error;
   1769 
   1770 	dp = where = vwhere;
   1771 	buflen = where != NULL ? *sizep : 0;
   1772 	error = needed = 0;
   1773 	type = name[0];
   1774 
   1775 	if (namelen != 4)
   1776 		return (EINVAL);
   1777 	pid = name[1];
   1778 	elem_size = name[2];
   1779 	elem_count = name[3];
   1780 
   1781 	p = pfind(pid);
   1782 	if (p == NULL)
   1783 		return (ESRCH);
   1784 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1785 		if (buflen >= elem_size && elem_count > 0) {
   1786 			fill_lwp(l, &klwp);
   1787 			/*
   1788 			 * Copy out elem_size, but not larger than
   1789 			 * the size of a struct kinfo_proc2.
   1790 			 */
   1791 			error = copyout(&klwp, dp,
   1792 			    min(sizeof(klwp), elem_size));
   1793 			if (error)
   1794 				goto cleanup;
   1795 			dp += elem_size;
   1796 			buflen -= elem_size;
   1797 			elem_count--;
   1798 		}
   1799 		needed += elem_size;
   1800 	}
   1801 
   1802 	if (where != NULL) {
   1803 		*sizep = dp - where;
   1804 		if (needed > *sizep)
   1805 			return (ENOMEM);
   1806 	} else {
   1807 		needed += KERN_PROCSLOP;
   1808 		*sizep = needed;
   1809 	}
   1810 	return (0);
   1811  cleanup:
   1812 	return (error);
   1813 }
   1814 
   1815 /*
   1816  * Fill in an eproc structure for the specified process.
   1817  */
   1818 void
   1819 fill_eproc(struct proc *p, struct eproc *ep)
   1820 {
   1821 	struct tty *tp;
   1822 	struct lwp *l;
   1823 
   1824 	ep->e_paddr = p;
   1825 	ep->e_sess = p->p_session;
   1826 	ep->e_pcred = *p->p_cred;
   1827 	ep->e_ucred = *p->p_ucred;
   1828 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   1829 		ep->e_vm.vm_rssize = 0;
   1830 		ep->e_vm.vm_tsize = 0;
   1831 		ep->e_vm.vm_dsize = 0;
   1832 		ep->e_vm.vm_ssize = 0;
   1833 		/* ep->e_vm.vm_pmap = XXX; */
   1834 	} else {
   1835 		struct vmspace *vm = p->p_vmspace;
   1836 
   1837 		ep->e_vm.vm_rssize = vm_resident_count(vm);
   1838 		ep->e_vm.vm_tsize = vm->vm_tsize;
   1839 		ep->e_vm.vm_dsize = vm->vm_dsize;
   1840 		ep->e_vm.vm_ssize = vm->vm_ssize;
   1841 
   1842 		/* Pick a "representative" LWP */
   1843 		l = proc_representative_lwp(p);
   1844 
   1845 		if (l->l_wmesg)
   1846 			strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
   1847 	}
   1848 	if (p->p_pptr)
   1849 		ep->e_ppid = p->p_pptr->p_pid;
   1850 	else
   1851 		ep->e_ppid = 0;
   1852 	ep->e_pgid = p->p_pgrp->pg_id;
   1853 	ep->e_sid = ep->e_sess->s_sid;
   1854 	ep->e_jobc = p->p_pgrp->pg_jobc;
   1855 	if ((p->p_flag & P_CONTROLT) &&
   1856 	    (tp = ep->e_sess->s_ttyp)) {
   1857 		ep->e_tdev = tp->t_dev;
   1858 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   1859 		ep->e_tsess = tp->t_session;
   1860 	} else
   1861 		ep->e_tdev = NODEV;
   1862 
   1863 	ep->e_xsize = ep->e_xrssize = 0;
   1864 	ep->e_xccount = ep->e_xswrss = 0;
   1865 	ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
   1866 	if (SESS_LEADER(p))
   1867 		ep->e_flag |= EPROC_SLEADER;
   1868 	strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
   1869 }
   1870 
   1871 /*
   1872  * Fill in an eproc structure for the specified process.
   1873  */
   1874 static void
   1875 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki)
   1876 {
   1877 	struct tty *tp;
   1878 	struct lwp *l;
   1879 	struct timeval ut, st;
   1880 
   1881 	memset(ki, 0, sizeof(*ki));
   1882 
   1883 	ki->p_paddr = PTRTOINT64(p);
   1884 	ki->p_fd = PTRTOINT64(p->p_fd);
   1885 	ki->p_cwdi = PTRTOINT64(p->p_cwdi);
   1886 	ki->p_stats = PTRTOINT64(p->p_stats);
   1887 	ki->p_limit = PTRTOINT64(p->p_limit);
   1888 	ki->p_vmspace = PTRTOINT64(p->p_vmspace);
   1889 	ki->p_sigacts = PTRTOINT64(p->p_sigacts);
   1890 	ki->p_sess = PTRTOINT64(p->p_session);
   1891 	ki->p_tsess = 0;	/* may be changed if controlling tty below */
   1892 	ki->p_ru = PTRTOINT64(p->p_ru);
   1893 
   1894 	ki->p_eflag = 0;
   1895 	ki->p_exitsig = p->p_exitsig;
   1896 	ki->p_flag = p->p_flag;
   1897 
   1898 	ki->p_pid = p->p_pid;
   1899 	if (p->p_pptr)
   1900 		ki->p_ppid = p->p_pptr->p_pid;
   1901 	else
   1902 		ki->p_ppid = 0;
   1903 	ki->p_sid = p->p_session->s_sid;
   1904 	ki->p__pgid = p->p_pgrp->pg_id;
   1905 
   1906 	ki->p_tpgid = NO_PGID;	/* may be changed if controlling tty below */
   1907 
   1908 	ki->p_uid = p->p_ucred->cr_uid;
   1909 	ki->p_ruid = p->p_cred->p_ruid;
   1910 	ki->p_gid = p->p_ucred->cr_gid;
   1911 	ki->p_rgid = p->p_cred->p_rgid;
   1912 	ki->p_svuid = p->p_cred->p_svuid;
   1913 	ki->p_svgid = p->p_cred->p_svgid;
   1914 
   1915 	memcpy(ki->p_groups, p->p_cred->pc_ucred->cr_groups,
   1916 	    min(sizeof(ki->p_groups), sizeof(p->p_cred->pc_ucred->cr_groups)));
   1917 	ki->p_ngroups = p->p_cred->pc_ucred->cr_ngroups;
   1918 
   1919 	ki->p_jobc = p->p_pgrp->pg_jobc;
   1920 	if ((p->p_flag & P_CONTROLT) && (tp = p->p_session->s_ttyp)) {
   1921 		ki->p_tdev = tp->t_dev;
   1922 		ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   1923 		ki->p_tsess = PTRTOINT64(tp->t_session);
   1924 	} else {
   1925 		ki->p_tdev = NODEV;
   1926 	}
   1927 
   1928 	ki->p_estcpu = p->p_estcpu;
   1929 	ki->p_rtime_sec = p->p_rtime.tv_sec;
   1930 	ki->p_rtime_usec = p->p_rtime.tv_usec;
   1931 	ki->p_cpticks = p->p_cpticks;
   1932 	ki->p_pctcpu = p->p_pctcpu;
   1933 
   1934 	ki->p_uticks = p->p_uticks;
   1935 	ki->p_sticks = p->p_sticks;
   1936 	ki->p_iticks = p->p_iticks;
   1937 
   1938 	ki->p_tracep = PTRTOINT64(p->p_tracep);
   1939 	ki->p_traceflag = p->p_traceflag;
   1940 
   1941 
   1942 	memcpy(&ki->p_siglist, &p->p_sigctx.ps_siglist, sizeof(ki_sigset_t));
   1943 	memcpy(&ki->p_sigmask, &p->p_sigctx.ps_sigmask, sizeof(ki_sigset_t));
   1944 	memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
   1945 	memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
   1946 
   1947 	ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
   1948 	ki->p_realstat = p->p_stat;
   1949 	ki->p_nice = p->p_nice;
   1950 
   1951 	ki->p_xstat = p->p_xstat;
   1952 	ki->p_acflag = p->p_acflag;
   1953 
   1954 	strncpy(ki->p_comm, p->p_comm,
   1955 	    min(sizeof(ki->p_comm), sizeof(p->p_comm)));
   1956 
   1957 	strncpy(ki->p_login, p->p_session->s_login,
   1958 	    min(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
   1959 
   1960 	ki->p_nlwps = p->p_nlwps;
   1961 	ki->p_nrlwps = p->p_nrlwps;
   1962 	ki->p_realflag = p->p_flag;
   1963 
   1964 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   1965 		ki->p_vm_rssize = 0;
   1966 		ki->p_vm_tsize = 0;
   1967 		ki->p_vm_dsize = 0;
   1968 		ki->p_vm_ssize = 0;
   1969 		l = NULL;
   1970 	} else {
   1971 		struct vmspace *vm = p->p_vmspace;
   1972 
   1973 		ki->p_vm_rssize = vm_resident_count(vm);
   1974 		ki->p_vm_tsize = vm->vm_tsize;
   1975 		ki->p_vm_dsize = vm->vm_dsize;
   1976 		ki->p_vm_ssize = vm->vm_ssize;
   1977 
   1978 		/* Pick a "representative" LWP */
   1979 		l = proc_representative_lwp(p);
   1980 		ki->p_forw = PTRTOINT64(l->l_forw);
   1981 		ki->p_back = PTRTOINT64(l->l_back);
   1982 		ki->p_addr = PTRTOINT64(l->l_addr);
   1983 		ki->p_stat = l->l_stat;
   1984 		ki->p_flag |= l->l_flag;
   1985 		ki->p_swtime = l->l_swtime;
   1986 		ki->p_slptime = l->l_slptime;
   1987 		if (l->l_stat == LSONPROC) {
   1988 			KDASSERT(l->l_cpu != NULL);
   1989 			ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   1990 		} else
   1991 			ki->p_schedflags = 0;
   1992 		ki->p_holdcnt = l->l_holdcnt;
   1993 		ki->p_priority = l->l_priority;
   1994 		ki->p_usrpri = l->l_usrpri;
   1995 		if (l->l_wmesg)
   1996 			strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
   1997 		ki->p_wchan = PTRTOINT64(l->l_wchan);
   1998 
   1999 	}
   2000 
   2001 	if (p->p_session->s_ttyvp)
   2002 		ki->p_eflag |= EPROC_CTTY;
   2003 	if (SESS_LEADER(p))
   2004 		ki->p_eflag |= EPROC_SLEADER;
   2005 
   2006 	/* XXX Is this double check necessary? */
   2007 	if (P_ZOMBIE(p)) {
   2008 		ki->p_uvalid = 0;
   2009 	} else {
   2010 		ki->p_uvalid = 1;
   2011 
   2012 		ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
   2013 		ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
   2014 
   2015 		calcru(p, &ut, &st, 0);
   2016 		ki->p_uutime_sec = ut.tv_sec;
   2017 		ki->p_uutime_usec = ut.tv_usec;
   2018 		ki->p_ustime_sec = st.tv_sec;
   2019 		ki->p_ustime_usec = st.tv_usec;
   2020 
   2021 		ki->p_uru_maxrss = p->p_stats->p_ru.ru_maxrss;
   2022 		ki->p_uru_ixrss = p->p_stats->p_ru.ru_ixrss;
   2023 		ki->p_uru_idrss = p->p_stats->p_ru.ru_idrss;
   2024 		ki->p_uru_isrss = p->p_stats->p_ru.ru_isrss;
   2025 		ki->p_uru_minflt = p->p_stats->p_ru.ru_minflt;
   2026 		ki->p_uru_majflt = p->p_stats->p_ru.ru_majflt;
   2027 		ki->p_uru_nswap = p->p_stats->p_ru.ru_nswap;
   2028 		ki->p_uru_inblock = p->p_stats->p_ru.ru_inblock;
   2029 		ki->p_uru_oublock = p->p_stats->p_ru.ru_oublock;
   2030 		ki->p_uru_msgsnd = p->p_stats->p_ru.ru_msgsnd;
   2031 		ki->p_uru_msgrcv = p->p_stats->p_ru.ru_msgrcv;
   2032 		ki->p_uru_nsignals = p->p_stats->p_ru.ru_nsignals;
   2033 		ki->p_uru_nvcsw = p->p_stats->p_ru.ru_nvcsw;
   2034 		ki->p_uru_nivcsw = p->p_stats->p_ru.ru_nivcsw;
   2035 
   2036 		timeradd(&p->p_stats->p_cru.ru_utime,
   2037 			 &p->p_stats->p_cru.ru_stime, &ut);
   2038 		ki->p_uctime_sec = ut.tv_sec;
   2039 		ki->p_uctime_usec = ut.tv_usec;
   2040 	}
   2041 #ifdef MULTIPROCESSOR
   2042 	if (l && l->l_cpu != NULL)
   2043 		ki->p_cpuid = l->l_cpu->ci_cpuid;
   2044 	else
   2045 #endif
   2046 		ki->p_cpuid = KI_NOCPU;
   2047 
   2048 }
   2049 
   2050 /*
   2051  * Fill in a kinfo_lwp structure for the specified lwp.
   2052  */
   2053 static void
   2054 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
   2055 {
   2056 
   2057 	kl->l_forw = PTRTOINT64(l->l_forw);
   2058 	kl->l_back = PTRTOINT64(l->l_back);
   2059 	kl->l_laddr = PTRTOINT64(l);
   2060 	kl->l_addr = PTRTOINT64(l->l_addr);
   2061 	kl->l_stat = l->l_stat;
   2062 	kl->l_lid = l->l_lid;
   2063 	kl->l_flag = l->l_flag;
   2064 
   2065 	kl->l_swtime = l->l_swtime;
   2066 	kl->l_slptime = l->l_slptime;
   2067 	if (l->l_stat == LSONPROC) {
   2068 		KDASSERT(l->l_cpu != NULL);
   2069 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   2070 	} else
   2071 		kl->l_schedflags = 0;
   2072 	kl->l_holdcnt = l->l_holdcnt;
   2073 	kl->l_priority = l->l_priority;
   2074 	kl->l_usrpri = l->l_usrpri;
   2075 	if (l->l_wmesg)
   2076 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
   2077 	kl->l_wchan = PTRTOINT64(l->l_wchan);
   2078 #ifdef MULTIPROCESSOR
   2079 	if (l->l_cpu != NULL)
   2080 		kl->l_cpuid = l->l_cpu->ci_cpuid;
   2081 	else
   2082 #endif
   2083 		kl->l_cpuid = KI_NOCPU;
   2084 }
   2085 
   2086 int
   2087 sysctl_procargs(int *name, u_int namelen, void *where, size_t *sizep,
   2088     struct proc *up)
   2089 {
   2090 	struct ps_strings pss;
   2091 	struct proc *p;
   2092 	size_t len, upper_bound, xlen, i;
   2093 	struct uio auio;
   2094 	struct iovec aiov;
   2095 	vaddr_t argv;
   2096 	pid_t pid;
   2097 	int nargv, type, error;
   2098 	char *arg;
   2099 	char *tmp;
   2100 
   2101 	if (namelen != 2)
   2102 		return (EINVAL);
   2103 	pid = name[0];
   2104 	type = name[1];
   2105 
   2106 	switch (type) {
   2107 	case KERN_PROC_ARGV:
   2108 	case KERN_PROC_NARGV:
   2109 	case KERN_PROC_ENV:
   2110 	case KERN_PROC_NENV:
   2111 		/* ok */
   2112 		break;
   2113 	default:
   2114 		return (EINVAL);
   2115 	}
   2116 
   2117 	/* check pid */
   2118 	if ((p = pfind(pid)) == NULL)
   2119 		return (EINVAL);
   2120 
   2121 	/* only root or same user change look at the environment */
   2122 	if (type == KERN_PROC_ENV || type == KERN_PROC_NENV) {
   2123 		if (up->p_ucred->cr_uid != 0) {
   2124 			if (up->p_cred->p_ruid != p->p_cred->p_ruid ||
   2125 			    up->p_cred->p_ruid != p->p_cred->p_svuid)
   2126 				return (EPERM);
   2127 		}
   2128 	}
   2129 
   2130 	if (sizep != NULL && where == NULL) {
   2131 		if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
   2132 			*sizep = sizeof (int);
   2133 		else
   2134 			*sizep = ARG_MAX;	/* XXX XXX XXX */
   2135 		return (0);
   2136 	}
   2137 	if (where == NULL || sizep == NULL)
   2138 		return (EINVAL);
   2139 
   2140 	/*
   2141 	 * Zombies don't have a stack, so we can't read their psstrings.
   2142 	 * System processes also don't have a user stack.
   2143 	 */
   2144 	if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0)
   2145 		return (EINVAL);
   2146 
   2147 	/*
   2148 	 * Lock the process down in memory.
   2149 	 */
   2150 	/* XXXCDC: how should locking work here? */
   2151 	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
   2152 		return (EFAULT);
   2153 
   2154 	p->p_vmspace->vm_refcnt++;	/* XXX */
   2155 
   2156 	/*
   2157 	 * Allocate a temporary buffer to hold the arguments.
   2158 	 */
   2159 	arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
   2160 
   2161 	/*
   2162 	 * Read in the ps_strings structure.
   2163 	 */
   2164 	aiov.iov_base = &pss;
   2165 	aiov.iov_len = sizeof(pss);
   2166 	auio.uio_iov = &aiov;
   2167 	auio.uio_iovcnt = 1;
   2168 	auio.uio_offset = (vaddr_t)p->p_psstr;
   2169 	auio.uio_resid = sizeof(pss);
   2170 	auio.uio_segflg = UIO_SYSSPACE;
   2171 	auio.uio_rw = UIO_READ;
   2172 	auio.uio_procp = NULL;
   2173 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
   2174 	if (error)
   2175 		goto done;
   2176 
   2177 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
   2178 		memcpy(&nargv, (char *)&pss + p->p_psnargv, sizeof(nargv));
   2179 	else
   2180 		memcpy(&nargv, (char *)&pss + p->p_psnenv, sizeof(nargv));
   2181 	if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
   2182 		error = copyout(&nargv, where, sizeof(nargv));
   2183 		*sizep = sizeof(nargv);
   2184 		goto done;
   2185 	}
   2186 	/*
   2187 	 * Now read the address of the argument vector.
   2188 	 */
   2189 	switch (type) {
   2190 	case KERN_PROC_ARGV:
   2191 		/* XXX compat32 stuff here */
   2192 		memcpy(&tmp, (char *)&pss + p->p_psargv, sizeof(tmp));
   2193 		break;
   2194 	case KERN_PROC_ENV:
   2195 		memcpy(&tmp, (char *)&pss + p->p_psenv, sizeof(tmp));
   2196 		break;
   2197 	default:
   2198 		return (EINVAL);
   2199 	}
   2200 	auio.uio_offset = (off_t)(long)tmp;
   2201 	aiov.iov_base = &argv;
   2202 	aiov.iov_len = sizeof(argv);
   2203 	auio.uio_iov = &aiov;
   2204 	auio.uio_iovcnt = 1;
   2205 	auio.uio_resid = sizeof(argv);
   2206 	auio.uio_segflg = UIO_SYSSPACE;
   2207 	auio.uio_rw = UIO_READ;
   2208 	auio.uio_procp = NULL;
   2209 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
   2210 	if (error)
   2211 		goto done;
   2212 
   2213 	/*
   2214 	 * Now copy in the actual argument vector, one page at a time,
   2215 	 * since we don't know how long the vector is (though, we do
   2216 	 * know how many NUL-terminated strings are in the vector).
   2217 	 */
   2218 	len = 0;
   2219 	upper_bound = *sizep;
   2220 	for (; nargv != 0 && len < upper_bound; len += xlen) {
   2221 		aiov.iov_base = arg;
   2222 		aiov.iov_len = PAGE_SIZE;
   2223 		auio.uio_iov = &aiov;
   2224 		auio.uio_iovcnt = 1;
   2225 		auio.uio_offset = argv + len;
   2226 		xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
   2227 		auio.uio_resid = xlen;
   2228 		auio.uio_segflg = UIO_SYSSPACE;
   2229 		auio.uio_rw = UIO_READ;
   2230 		auio.uio_procp = NULL;
   2231 		error = uvm_io(&p->p_vmspace->vm_map, &auio);
   2232 		if (error)
   2233 			goto done;
   2234 
   2235 		for (i = 0; i < xlen && nargv != 0; i++) {
   2236 			if (arg[i] == '\0')
   2237 				nargv--;	/* one full string */
   2238 		}
   2239 
   2240 		/*
   2241 		 * Make sure we don't copyout past the end of the user's
   2242 		 * buffer.
   2243 		 */
   2244 		if (len + i > upper_bound)
   2245 			i = upper_bound - len;
   2246 
   2247 		error = copyout(arg, (char *)where + len, i);
   2248 		if (error)
   2249 			break;
   2250 
   2251 		if (nargv == 0) {
   2252 			len += i;
   2253 			break;
   2254 		}
   2255 	}
   2256 	*sizep = len;
   2257 
   2258 done:
   2259 	uvmspace_free(p->p_vmspace);
   2260 
   2261 	free(arg, M_TEMP);
   2262 	return (error);
   2263 }
   2264 
   2265 #if NPTY > 0
   2266 int pty_maxptys(int, int);		/* defined in kern/tty_pty.c */
   2267 
   2268 /*
   2269  * Validate parameters and get old / set new parameters
   2270  * for pty sysctl function.
   2271  */
   2272 static int
   2273 sysctl_pty(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
   2274 {
   2275 	int error = 0;
   2276 	int oldmax = 0, newmax = 0;
   2277 
   2278 	/* get current value of maxptys */
   2279 	oldmax = pty_maxptys(0, 0);
   2280 
   2281 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &oldmax, int)
   2282 
   2283 	if (!error && newp) {
   2284 		SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
   2285 		SYSCTL_SCALAR_NEWPCOP_TYP(newp, &newmax, int)
   2286 
   2287 		if (newmax != pty_maxptys(newmax, (newp != NULL)))
   2288 			return (EINVAL);
   2289 
   2290 	}
   2291 
   2292 	return (error);
   2293 }
   2294 #endif /* NPTY > 0 */
   2295 
   2296 static int
   2297 sysctl_dotkstat(int *name, u_int namelen, void *where, size_t *sizep,
   2298     void *newp)
   2299 {
   2300 
   2301 	/* all sysctl names at this level are terminal */
   2302 	if (namelen != 1)
   2303 		return (ENOTDIR);		/* overloaded */
   2304 
   2305 	switch (name[0]) {
   2306 	case KERN_TKSTAT_NIN:
   2307 		return (sysctl_rdquad(where, sizep, newp, tk_nin));
   2308 	case KERN_TKSTAT_NOUT:
   2309 		return (sysctl_rdquad(where, sizep, newp, tk_nout));
   2310 	case KERN_TKSTAT_CANCC:
   2311 		return (sysctl_rdquad(where, sizep, newp, tk_cancc));
   2312 	case KERN_TKSTAT_RAWCC:
   2313 		return (sysctl_rdquad(where, sizep, newp, tk_rawcc));
   2314 	default:
   2315 		return (EOPNOTSUPP);
   2316 	}
   2317 }
   2318