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