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