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