Home | History | Annotate | Line # | Download | only in kern
kern_sysctl.c revision 1.111
      1 /*	$NetBSD: kern_sysctl.c,v 1.111 2002/08/25 22:51:07 thorpej 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.111 2002/08/25 22:51:07 thorpej 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 	size_t len, curlen;
    719 	u_int i;
    720 
    721 	if (namelen < 2)
    722 		return EINVAL;
    723 
    724 	if (name[0] == PROC_CURPROC) {
    725 		ptmp = p;
    726 	} else {
    727 		proclist_lock_read();
    728 		for (pd = proclists; pd->pd_list != NULL; pd++) {
    729 			for (ptmp = LIST_FIRST(pd->pd_list); ptmp != NULL;
    730 			    ptmp = LIST_NEXT(ptmp, p_list)) {
    731 				/* Skip embryonic processes. */
    732 				if (ptmp->p_stat == SIDL)
    733 					continue;
    734 				if (ptmp->p_pid == (pid_t)name[0])
    735 					break;
    736 			}
    737 			if (ptmp != NULL)
    738 				break;
    739 		}
    740 		proclist_unlock_read();
    741 		if (ptmp == NULL)
    742 			return(ESRCH);
    743 		if (p->p_ucred->cr_uid != 0) {
    744 			if(p->p_cred->p_ruid != ptmp->p_cred->p_ruid ||
    745 			    p->p_cred->p_ruid != ptmp->p_cred->p_svuid)
    746 				return EPERM;
    747 			if (ptmp->p_cred->p_rgid != ptmp->p_cred->p_svgid)
    748 				return EPERM; /* sgid proc */
    749 			for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
    750 				if (p->p_ucred->cr_groups[i] ==
    751 				    ptmp->p_cred->p_rgid)
    752 					break;
    753 			}
    754 			if (i == p->p_ucred->cr_ngroups)
    755 				return EPERM;
    756 		}
    757 	}
    758 	if (name[1] == PROC_PID_CORENAME) {
    759 		if (namelen != 2)
    760 			return EINVAL;
    761 		/*
    762 		 * Can't use sysctl_string() here because we may malloc a new
    763 		 * area during the process, so we have to do it by hand.
    764 		 */
    765 		curlen = strlen(ptmp->p_limit->pl_corename) + 1;
    766 		if (oldlenp  && *oldlenp < curlen) {
    767 			if (!oldp)
    768 				*oldlenp = curlen;
    769 			return (ENOMEM);
    770 		}
    771 		if (newp) {
    772 			if (securelevel > 2)
    773 				return EPERM;
    774 			if (newlen > MAXPATHLEN)
    775 				return ENAMETOOLONG;
    776 			tmps = malloc(newlen + 1, M_TEMP, M_WAITOK);
    777 			if (tmps == NULL)
    778 				return ENOMEM;
    779 			error = copyin(newp, tmps, newlen + 1);
    780 			tmps[newlen] = '\0';
    781 			if (error)
    782 				goto cleanup;
    783 			/* Enforce to be either 'core' for end with '.core' */
    784 			if (newlen < 4)  { /* c.o.r.e */
    785 				error = EINVAL;
    786 				goto cleanup;
    787 			}
    788 			len = newlen - 4;
    789 			if (len > 0) {
    790 				if (tmps[len - 1] != '.' &&
    791 				    tmps[len - 1] != '/') {
    792 					error = EINVAL;
    793 					goto cleanup;
    794 				}
    795 			}
    796 			if (strcmp(&tmps[len], "core") != 0) {
    797 				error = EINVAL;
    798 				goto cleanup;
    799 			}
    800 		}
    801 		if (oldp && oldlenp) {
    802 			*oldlenp = curlen;
    803 			error = copyout(ptmp->p_limit->pl_corename, oldp,
    804 			    curlen);
    805 		}
    806 		if (newp && error == 0) {
    807 			/* if the 2 strings are identical, don't limcopy() */
    808 			if (strcmp(tmps, ptmp->p_limit->pl_corename) == 0) {
    809 				error = 0;
    810 				goto cleanup;
    811 			}
    812 			if (ptmp->p_limit->p_refcnt > 1 &&
    813 			    (ptmp->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    814 				newplim = limcopy(ptmp->p_limit);
    815 				limfree(ptmp->p_limit);
    816 				ptmp->p_limit = newplim;
    817 			}
    818 			if (ptmp->p_limit->pl_corename != defcorename) {
    819 				free(ptmp->p_limit->pl_corename, M_TEMP);
    820 			}
    821 			ptmp->p_limit->pl_corename = tmps;
    822 			return (0);
    823 		}
    824 cleanup:
    825 		if (tmps)
    826 			free(tmps, M_TEMP);
    827 		return (error);
    828 	}
    829 	if (name[1] == PROC_PID_LIMIT) {
    830 		if (namelen != 4 || name[2] >= PROC_PID_LIMIT_MAXID)
    831 			return EINVAL;
    832 		memcpy(&alim, &ptmp->p_rlimit[name[2] - 1], sizeof(alim));
    833 		if (name[3] == PROC_PID_LIMIT_TYPE_HARD)
    834 			error = sysctl_quad(oldp, oldlenp, newp, newlen,
    835 			    &alim.rlim_max);
    836 		else if (name[3] == PROC_PID_LIMIT_TYPE_SOFT)
    837 			error = sysctl_quad(oldp, oldlenp, newp, newlen,
    838 			    &alim.rlim_cur);
    839 		else
    840 			error = EINVAL;
    841 
    842 		if (error)
    843 			return error;
    844 
    845 		if (newp)
    846 			error = dosetrlimit(ptmp, p->p_cred,
    847 			    name[2] - 1, &alim);
    848 		return error;
    849 	}
    850 	return (EINVAL);
    851 }
    852 
    853 int
    854 emul_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    855     void *newp, size_t newlen, struct proc *p)
    856 {
    857 	static struct {
    858 		const char *name;
    859 		int  type;
    860 	} emulations[] = CTL_EMUL_NAMES;
    861 	const struct emul *e;
    862 	const char *ename;
    863 #ifdef LKM
    864 	extern struct lock exec_lock;	/* XXX */
    865 	int error;
    866 #else
    867 	extern int nexecs_builtin;
    868 	extern const struct execsw execsw_builtin[];
    869 	int i;
    870 #endif
    871 
    872 	/* all sysctl names at this level are name and field */
    873 	if (namelen < 2)
    874 		return (ENOTDIR);		/* overloaded */
    875 
    876 	if ((u_int) name[0] >= EMUL_MAXID || name[0] == 0)
    877 		return (EOPNOTSUPP);
    878 
    879 	ename = emulations[name[0]].name;
    880 
    881 #ifdef LKM
    882 	lockmgr(&exec_lock, LK_SHARED, NULL);
    883 	if ((e = emul_search(ename))) {
    884 		error = (*e->e_sysctl)(name + 1, namelen - 1, oldp, oldlenp,
    885 				newp, newlen, p);
    886 	} else
    887 		error = EOPNOTSUPP;
    888 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    889 
    890 	return (error);
    891 #else
    892 	for (i = 0; i < nexecs_builtin; i++) {
    893 	    e = execsw_builtin[i].es_emul;
    894 	    if (e == NULL || strcmp(ename, e->e_name) != 0 ||
    895 		e->e_sysctl != NULL)
    896 		continue;
    897 
    898 	    return (*e->e_sysctl)(name + 1, namelen - 1, oldp, oldlenp,
    899 					newp, newlen, p);
    900 	}
    901 
    902 	return (EOPNOTSUPP);
    903 #endif
    904 }
    905 /*
    906  * Convenience macros.
    907  */
    908 
    909 #define SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, len) 		\
    910 	if (oldlenp) {							\
    911 		if (!oldp)						\
    912 			*oldlenp = len;					\
    913 		else {							\
    914 			if (*oldlenp < len)				\
    915 				return(ENOMEM);				\
    916 			*oldlenp = len;					\
    917 			error = copyout((caddr_t)valp, oldp, len);	\
    918 		}							\
    919 	}
    920 
    921 #define SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, typ) \
    922 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, sizeof(typ))
    923 
    924 #define SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len)	\
    925 	if (newp && newlen != len)			\
    926 		return (EINVAL);
    927 
    928 #define SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, typ)	\
    929 	SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, sizeof(typ))
    930 
    931 #define SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, len)	\
    932 	if (error == 0 && newp)				\
    933 		error = copyin(newp, valp, len);
    934 
    935 #define SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, typ)      \
    936 	SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, sizeof(typ))
    937 
    938 #define SYSCTL_STRING_CORE(oldp, oldlenp, str)		\
    939 	if (oldlenp) {					\
    940 		len = strlen(str) + 1;			\
    941 		if (!oldp)				\
    942 			*oldlenp = len;			\
    943 		else {					\
    944 			if (*oldlenp < len) {		\
    945 				err2 = ENOMEM;		\
    946 				len = *oldlenp;		\
    947 			} else				\
    948 				*oldlenp = len;		\
    949 			error = copyout(str, oldp, len);\
    950 			if (error == 0)			\
    951 				error = err2;		\
    952 		}					\
    953 	}
    954 
    955 /*
    956  * Validate parameters and get old / set new parameters
    957  * for an integer-valued sysctl function.
    958  */
    959 int
    960 sysctl_int(void *oldp, size_t *oldlenp, void *newp, size_t newlen, int *valp)
    961 {
    962 	int error = 0;
    963 
    964 	SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
    965 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, int)
    966 	SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, int)
    967 
    968 	return (error);
    969 }
    970 
    971 
    972 /*
    973  * As above, but read-only.
    974  */
    975 int
    976 sysctl_rdint(void *oldp, size_t *oldlenp, void *newp, int val)
    977 {
    978 	int error = 0;
    979 
    980 	if (newp)
    981 		return (EPERM);
    982 
    983 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, int)
    984 
    985 	return (error);
    986 }
    987 
    988 /*
    989  * Validate parameters and get old / set new parameters
    990  * for an quad-valued sysctl function.
    991  */
    992 int
    993 sysctl_quad(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
    994     quad_t *valp)
    995 {
    996 	int error = 0;
    997 
    998 	SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, quad_t)
    999 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, quad_t)
   1000 	SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, quad_t)
   1001 
   1002 	return (error);
   1003 }
   1004 
   1005 /*
   1006  * As above, but read-only.
   1007  */
   1008 int
   1009 sysctl_rdquad(void *oldp, size_t *oldlenp, void *newp, quad_t val)
   1010 {
   1011 	int error = 0;
   1012 
   1013 	if (newp)
   1014 		return (EPERM);
   1015 
   1016 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, quad_t)
   1017 
   1018 	return (error);
   1019 }
   1020 
   1021 /*
   1022  * Validate parameters and get old / set new parameters
   1023  * for a string-valued sysctl function.
   1024  */
   1025 int
   1026 sysctl_string(void *oldp, size_t *oldlenp, void *newp, size_t newlen, char *str,
   1027     size_t maxlen)
   1028 {
   1029 	int error = 0, err2 = 0;
   1030 	size_t len;
   1031 
   1032 	if (newp && newlen >= maxlen)
   1033 		return (EINVAL);
   1034 
   1035 	SYSCTL_STRING_CORE(oldp, oldlenp, str);
   1036 
   1037 	if (error == 0 && newp) {
   1038 		error = copyin(newp, str, newlen);
   1039 		str[newlen] = 0;
   1040 	}
   1041 	return (error);
   1042 }
   1043 
   1044 /*
   1045  * As above, but read-only.
   1046  */
   1047 int
   1048 sysctl_rdstring(void *oldp, size_t *oldlenp, void *newp, const char *str)
   1049 {
   1050 	int error = 0, err2 = 0;
   1051 	size_t len;
   1052 
   1053 	if (newp)
   1054 		return (EPERM);
   1055 
   1056 	SYSCTL_STRING_CORE(oldp, oldlenp, str);
   1057 
   1058 	return (error);
   1059 }
   1060 
   1061 /*
   1062  * Validate parameters and get old / set new parameters
   1063  * for a structure oriented sysctl function.
   1064  */
   1065 int
   1066 sysctl_struct(void *oldp, size_t *oldlenp, void *newp, size_t newlen, void *sp,
   1067     size_t len)
   1068 {
   1069 	int error = 0;
   1070 
   1071 	SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len)
   1072 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
   1073 	SYSCTL_SCALAR_NEWPCOP_LEN(newp, sp, len)
   1074 
   1075 	return (error);
   1076 }
   1077 
   1078 /*
   1079  * Validate parameters and get old parameters
   1080  * for a structure oriented sysctl function.
   1081  */
   1082 int
   1083 sysctl_rdstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
   1084     size_t len)
   1085 {
   1086 	int error = 0;
   1087 
   1088 	if (newp)
   1089 		return (EPERM);
   1090 
   1091 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
   1092 
   1093 	return (error);
   1094 }
   1095 
   1096 /*
   1097  * As above, but can return a truncated result.
   1098  */
   1099 int
   1100 sysctl_rdminstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
   1101     size_t len)
   1102 {
   1103 	int error = 0;
   1104 
   1105 	if (newp)
   1106 		return (EPERM);
   1107 
   1108 	len = min(*oldlenp, len);
   1109 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
   1110 
   1111 	return (error);
   1112 }
   1113 
   1114 /*
   1115  * Get file structures.
   1116  */
   1117 static int
   1118 sysctl_file(void *vwhere, size_t *sizep)
   1119 {
   1120 	int error;
   1121 	size_t buflen;
   1122 	struct file *fp;
   1123 	char *start, *where;
   1124 
   1125 	start = where = vwhere;
   1126 	buflen = *sizep;
   1127 	if (where == NULL) {
   1128 		/*
   1129 		 * overestimate by 10 files
   1130 		 */
   1131 		*sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
   1132 		return (0);
   1133 	}
   1134 
   1135 	/*
   1136 	 * first copyout filehead
   1137 	 */
   1138 	if (buflen < sizeof(filehead)) {
   1139 		*sizep = 0;
   1140 		return (0);
   1141 	}
   1142 	error = copyout((caddr_t)&filehead, where, sizeof(filehead));
   1143 	if (error)
   1144 		return (error);
   1145 	buflen -= sizeof(filehead);
   1146 	where += sizeof(filehead);
   1147 
   1148 	/*
   1149 	 * followed by an array of file structures
   1150 	 */
   1151 	for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
   1152 		if (buflen < sizeof(struct file)) {
   1153 			*sizep = where - start;
   1154 			return (ENOMEM);
   1155 		}
   1156 		error = copyout((caddr_t)fp, where, sizeof(struct file));
   1157 		if (error)
   1158 			return (error);
   1159 		buflen -= sizeof(struct file);
   1160 		where += sizeof(struct file);
   1161 	}
   1162 	*sizep = where - start;
   1163 	return (0);
   1164 }
   1165 
   1166 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
   1167 #define	FILL_PERM(src, dst) do { \
   1168 		(dst)._key = (src)._key; \
   1169 		(dst).uid = (src).uid; \
   1170 		(dst).gid = (src).gid; \
   1171 		(dst).cuid = (src).cuid; \
   1172 		(dst).cgid = (src).cgid; \
   1173 		(dst).mode = (src).mode; \
   1174 		(dst)._seq = (src)._seq; \
   1175 	} while (0);
   1176 #define	FILL_MSG(src, dst) do { \
   1177 	FILL_PERM((src).msg_perm, (dst).msg_perm); \
   1178 	(dst).msg_qnum = (src).msg_qnum; \
   1179 	(dst).msg_qbytes = (src).msg_qbytes; \
   1180 	(dst)._msg_cbytes = (src)._msg_cbytes; \
   1181 	(dst).msg_lspid = (src).msg_lspid; \
   1182 	(dst).msg_lrpid = (src).msg_lrpid; \
   1183 	(dst).msg_stime = (src).msg_stime; \
   1184 	(dst).msg_rtime = (src).msg_rtime; \
   1185 	(dst).msg_ctime = (src).msg_ctime; \
   1186 	} while (0)
   1187 #define	FILL_SEM(src, dst) do { \
   1188 	FILL_PERM((src).sem_perm, (dst).sem_perm); \
   1189 	(dst).sem_nsems = (src).sem_nsems; \
   1190 	(dst).sem_otime = (src).sem_otime; \
   1191 	(dst).sem_ctime = (src).sem_ctime; \
   1192 	} while (0)
   1193 #define	FILL_SHM(src, dst) do { \
   1194 	FILL_PERM((src).shm_perm, (dst).shm_perm); \
   1195 	(dst).shm_segsz = (src).shm_segsz; \
   1196 	(dst).shm_lpid = (src).shm_lpid; \
   1197 	(dst).shm_cpid = (src).shm_cpid; \
   1198 	(dst).shm_atime = (src).shm_atime; \
   1199 	(dst).shm_dtime = (src).shm_dtime; \
   1200 	(dst).shm_ctime = (src).shm_ctime; \
   1201 	(dst).shm_nattch = (src).shm_nattch; \
   1202 	} while (0)
   1203 
   1204 static int
   1205 sysctl_sysvipc(int *name, u_int namelen, void *where, size_t *sizep)
   1206 {
   1207 #ifdef SYSVMSG
   1208 	struct msg_sysctl_info *msgsi;
   1209 #endif
   1210 #ifdef SYSVSEM
   1211 	struct sem_sysctl_info *semsi;
   1212 #endif
   1213 #ifdef SYSVSHM
   1214 	struct shm_sysctl_info *shmsi;
   1215 #endif
   1216 	size_t infosize, dssize, tsize, buflen;
   1217 	void *buf = NULL;
   1218 	char *start;
   1219 	int32_t nds;
   1220 	int i, error, ret;
   1221 
   1222 	if (namelen != 1)
   1223 		return (EINVAL);
   1224 
   1225 	start = where;
   1226 	buflen = *sizep;
   1227 
   1228 	switch (*name) {
   1229 	case KERN_SYSVIPC_MSG_INFO:
   1230 #ifdef SYSVMSG
   1231 		infosize = sizeof(msgsi->msginfo);
   1232 		nds = msginfo.msgmni;
   1233 		dssize = sizeof(msgsi->msgids[0]);
   1234 		break;
   1235 #else
   1236 		return (EINVAL);
   1237 #endif
   1238 	case KERN_SYSVIPC_SEM_INFO:
   1239 #ifdef SYSVSEM
   1240 		infosize = sizeof(semsi->seminfo);
   1241 		nds = seminfo.semmni;
   1242 		dssize = sizeof(semsi->semids[0]);
   1243 		break;
   1244 #else
   1245 		return (EINVAL);
   1246 #endif
   1247 	case KERN_SYSVIPC_SHM_INFO:
   1248 #ifdef SYSVSHM
   1249 		infosize = sizeof(shmsi->shminfo);
   1250 		nds = shminfo.shmmni;
   1251 		dssize = sizeof(shmsi->shmids[0]);
   1252 		break;
   1253 #else
   1254 		return (EINVAL);
   1255 #endif
   1256 	default:
   1257 		return (EINVAL);
   1258 	}
   1259 	/*
   1260 	 * Round infosize to 64 bit boundary if requesting more than just
   1261 	 * the info structure or getting the total data size.
   1262 	 */
   1263 	if (where == NULL || *sizep > infosize)
   1264 		infosize = ((infosize + 7) / 8) * 8;
   1265 	tsize = infosize + nds * dssize;
   1266 
   1267 	/* Return just the total size required. */
   1268 	if (where == NULL) {
   1269 		*sizep = tsize;
   1270 		return (0);
   1271 	}
   1272 
   1273 	/* Not enough room for even the info struct. */
   1274 	if (buflen < infosize) {
   1275 		*sizep = 0;
   1276 		return (ENOMEM);
   1277 	}
   1278 	buf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK);
   1279 	memset(buf, 0, min(tsize, buflen));
   1280 
   1281 	switch (*name) {
   1282 #ifdef SYSVMSG
   1283 	case KERN_SYSVIPC_MSG_INFO:
   1284 		msgsi = (struct msg_sysctl_info *)buf;
   1285 		msgsi->msginfo = msginfo;
   1286 		break;
   1287 #endif
   1288 #ifdef SYSVSEM
   1289 	case KERN_SYSVIPC_SEM_INFO:
   1290 		semsi = (struct sem_sysctl_info *)buf;
   1291 		semsi->seminfo = seminfo;
   1292 		break;
   1293 #endif
   1294 #ifdef SYSVSHM
   1295 	case KERN_SYSVIPC_SHM_INFO:
   1296 		shmsi = (struct shm_sysctl_info *)buf;
   1297 		shmsi->shminfo = shminfo;
   1298 		break;
   1299 #endif
   1300 	}
   1301 	buflen -= infosize;
   1302 
   1303 	ret = 0;
   1304 	if (buflen > 0) {
   1305 		/* Fill in the IPC data structures.  */
   1306 		for (i = 0; i < nds; i++) {
   1307 			if (buflen < dssize) {
   1308 				ret = ENOMEM;
   1309 				break;
   1310 			}
   1311 			switch (*name) {
   1312 #ifdef SYSVMSG
   1313 			case KERN_SYSVIPC_MSG_INFO:
   1314 				FILL_MSG(msqids[i], msgsi->msgids[i]);
   1315 				break;
   1316 #endif
   1317 #ifdef SYSVSEM
   1318 			case KERN_SYSVIPC_SEM_INFO:
   1319 				FILL_SEM(sema[i], semsi->semids[i]);
   1320 				break;
   1321 #endif
   1322 #ifdef SYSVSHM
   1323 			case KERN_SYSVIPC_SHM_INFO:
   1324 				FILL_SHM(shmsegs[i], shmsi->shmids[i]);
   1325 				break;
   1326 #endif
   1327 			}
   1328 			buflen -= dssize;
   1329 		}
   1330 	}
   1331 	*sizep -= buflen;
   1332 	error = copyout(buf, start, *sizep);
   1333 	/* If copyout succeeded, use return code set earlier. */
   1334 	if (error == 0)
   1335 		error = ret;
   1336 	if (buf)
   1337 		free(buf, M_TEMP);
   1338 	return (error);
   1339 }
   1340 #endif /* SYSVMSG || SYSVSEM || SYSVSHM */
   1341 
   1342 static int
   1343 sysctl_msgbuf(void *vwhere, size_t *sizep)
   1344 {
   1345 	char *where = vwhere;
   1346 	size_t len, maxlen = *sizep;
   1347 	long beg, end;
   1348 	int error;
   1349 
   1350 	/*
   1351 	 * deal with cases where the message buffer has
   1352 	 * become corrupted.
   1353 	 */
   1354 	if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
   1355 		msgbufenabled = 0;
   1356 		return (ENXIO);
   1357 	}
   1358 
   1359 	if (where == NULL) {
   1360 		/* always return full buffer size */
   1361 		*sizep = msgbufp->msg_bufs;
   1362 		return (0);
   1363 	}
   1364 
   1365 	error = 0;
   1366 	maxlen = min(msgbufp->msg_bufs, maxlen);
   1367 
   1368 	/*
   1369 	 * First, copy from the write pointer to the end of
   1370 	 * message buffer.
   1371 	 */
   1372 	beg = msgbufp->msg_bufx;
   1373 	end = msgbufp->msg_bufs;
   1374 	while (maxlen > 0) {
   1375 		len = min(end - beg, maxlen);
   1376 		if (len == 0)
   1377 			break;
   1378 		error = copyout(&msgbufp->msg_bufc[beg], where, len);
   1379 		if (error)
   1380 			break;
   1381 		where += len;
   1382 		maxlen -= len;
   1383 
   1384 		/*
   1385 		 * ... then, copy from the beginning of message buffer to
   1386 		 * the write pointer.
   1387 		 */
   1388 		beg = 0;
   1389 		end = msgbufp->msg_bufx;
   1390 	}
   1391 	return (error);
   1392 }
   1393 
   1394 /*
   1395  * try over estimating by 5 procs
   1396  */
   1397 #define KERN_PROCSLOP	(5 * sizeof(struct kinfo_proc))
   1398 
   1399 static int
   1400 sysctl_doeproc(int *name, u_int namelen, void *vwhere, size_t *sizep)
   1401 {
   1402 	struct eproc eproc;
   1403 	struct kinfo_proc2 kproc2;
   1404 	struct kinfo_proc *dp;
   1405 	struct proc *p;
   1406 	const struct proclist_desc *pd;
   1407 	char *where, *dp2;
   1408 	int type, op, arg;
   1409 	u_int elem_size, elem_count;
   1410 	size_t buflen, needed;
   1411 	int error;
   1412 
   1413 	dp = vwhere;
   1414 	dp2 = where = vwhere;
   1415 	buflen = where != NULL ? *sizep : 0;
   1416 	error = 0;
   1417 	needed = 0;
   1418 	type = name[0];
   1419 
   1420 	if (type == KERN_PROC) {
   1421 		if (namelen != 3 && !(namelen == 2 && name[1] == KERN_PROC_ALL))
   1422 			return (EINVAL);
   1423 		op = name[1];
   1424 		if (op != KERN_PROC_ALL)
   1425 			arg = name[2];
   1426 	} else {
   1427 		if (namelen != 5)
   1428 			return (EINVAL);
   1429 		op = name[1];
   1430 		arg = name[2];
   1431 		elem_size = name[3];
   1432 		elem_count = name[4];
   1433 	}
   1434 
   1435 	proclist_lock_read();
   1436 
   1437 	pd = proclists;
   1438 again:
   1439 	for (p = LIST_FIRST(pd->pd_list); p != NULL; p = LIST_NEXT(p, p_list)) {
   1440 		/*
   1441 		 * Skip embryonic processes.
   1442 		 */
   1443 		if (p->p_stat == SIDL)
   1444 			continue;
   1445 		/*
   1446 		 * TODO - make more efficient (see notes below).
   1447 		 * do by session.
   1448 		 */
   1449 		switch (op) {
   1450 
   1451 		case KERN_PROC_PID:
   1452 			/* could do this with just a lookup */
   1453 			if (p->p_pid != (pid_t)arg)
   1454 				continue;
   1455 			break;
   1456 
   1457 		case KERN_PROC_PGRP:
   1458 			/* could do this by traversing pgrp */
   1459 			if (p->p_pgrp->pg_id != (pid_t)arg)
   1460 				continue;
   1461 			break;
   1462 
   1463 		case KERN_PROC_SESSION:
   1464 			if (p->p_session->s_sid != (pid_t)arg)
   1465 				continue;
   1466 			break;
   1467 
   1468 		case KERN_PROC_TTY:
   1469 			if (arg == (int) KERN_PROC_TTY_REVOKE) {
   1470 				if ((p->p_flag & P_CONTROLT) == 0 ||
   1471 				    p->p_session->s_ttyp == NULL ||
   1472 				    p->p_session->s_ttyvp != NULL)
   1473 					continue;
   1474 			} else if ((p->p_flag & P_CONTROLT) == 0 ||
   1475 			    p->p_session->s_ttyp == NULL) {
   1476 				if ((dev_t)arg != KERN_PROC_TTY_NODEV)
   1477 					continue;
   1478 			} else if (p->p_session->s_ttyp->t_dev != (dev_t)arg)
   1479 				continue;
   1480 			break;
   1481 
   1482 		case KERN_PROC_UID:
   1483 			if (p->p_ucred->cr_uid != (uid_t)arg)
   1484 				continue;
   1485 			break;
   1486 
   1487 		case KERN_PROC_RUID:
   1488 			if (p->p_cred->p_ruid != (uid_t)arg)
   1489 				continue;
   1490 			break;
   1491 
   1492 		case KERN_PROC_GID:
   1493 			if (p->p_ucred->cr_gid != (uid_t)arg)
   1494 				continue;
   1495 			break;
   1496 
   1497 		case KERN_PROC_RGID:
   1498 			if (p->p_cred->p_rgid != (uid_t)arg)
   1499 				continue;
   1500 			break;
   1501 
   1502 		case KERN_PROC_ALL:
   1503 			/* allow everything */
   1504 			break;
   1505 
   1506 		default:
   1507 			error = EINVAL;
   1508 			goto cleanup;
   1509 		}
   1510 		if (type == KERN_PROC) {
   1511 			if (buflen >= sizeof(struct kinfo_proc)) {
   1512 				fill_eproc(p, &eproc);
   1513 				error = copyout((caddr_t)p, &dp->kp_proc,
   1514 						sizeof(struct proc));
   1515 				if (error)
   1516 					goto cleanup;
   1517 				error = copyout((caddr_t)&eproc, &dp->kp_eproc,
   1518 						sizeof(eproc));
   1519 				if (error)
   1520 					goto cleanup;
   1521 				dp++;
   1522 				buflen -= sizeof(struct kinfo_proc);
   1523 			}
   1524 			needed += sizeof(struct kinfo_proc);
   1525 		} else { /* KERN_PROC2 */
   1526 			if (buflen >= elem_size && elem_count > 0) {
   1527 				fill_kproc2(p, &kproc2);
   1528 				/*
   1529 				 * Copy out elem_size, but not larger than
   1530 				 * the size of a struct kinfo_proc2.
   1531 				 */
   1532 				error = copyout(&kproc2, dp2,
   1533 				    min(sizeof(kproc2), elem_size));
   1534 				if (error)
   1535 					goto cleanup;
   1536 				dp2 += elem_size;
   1537 				buflen -= elem_size;
   1538 				elem_count--;
   1539 			}
   1540 			needed += elem_size;
   1541 		}
   1542 	}
   1543 	pd++;
   1544 	if (pd->pd_list != NULL)
   1545 		goto again;
   1546 	proclist_unlock_read();
   1547 
   1548 	if (where != NULL) {
   1549 		if (type == KERN_PROC)
   1550 			*sizep = (caddr_t)dp - where;
   1551 		else
   1552 			*sizep = dp2 - where;
   1553 		if (needed > *sizep)
   1554 			return (ENOMEM);
   1555 	} else {
   1556 		needed += KERN_PROCSLOP;
   1557 		*sizep = needed;
   1558 	}
   1559 	return (0);
   1560  cleanup:
   1561 	proclist_unlock_read();
   1562 	return (error);
   1563 }
   1564 
   1565 /*
   1566  * Fill in an eproc structure for the specified process.
   1567  */
   1568 void
   1569 fill_eproc(struct proc *p, struct eproc *ep)
   1570 {
   1571 	struct tty *tp;
   1572 
   1573 	ep->e_paddr = p;
   1574 	ep->e_sess = p->p_session;
   1575 	ep->e_pcred = *p->p_cred;
   1576 	ep->e_ucred = *p->p_ucred;
   1577 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   1578 		ep->e_vm.vm_rssize = 0;
   1579 		ep->e_vm.vm_tsize = 0;
   1580 		ep->e_vm.vm_dsize = 0;
   1581 		ep->e_vm.vm_ssize = 0;
   1582 		/* ep->e_vm.vm_pmap = XXX; */
   1583 	} else {
   1584 		struct vmspace *vm = p->p_vmspace;
   1585 
   1586 		ep->e_vm.vm_rssize = vm_resident_count(vm);
   1587 		ep->e_vm.vm_tsize = vm->vm_tsize;
   1588 		ep->e_vm.vm_dsize = vm->vm_dsize;
   1589 		ep->e_vm.vm_ssize = vm->vm_ssize;
   1590 	}
   1591 	if (p->p_pptr)
   1592 		ep->e_ppid = p->p_pptr->p_pid;
   1593 	else
   1594 		ep->e_ppid = 0;
   1595 	ep->e_pgid = p->p_pgrp->pg_id;
   1596 	ep->e_sid = ep->e_sess->s_sid;
   1597 	ep->e_jobc = p->p_pgrp->pg_jobc;
   1598 	if ((p->p_flag & P_CONTROLT) &&
   1599 	     (tp = ep->e_sess->s_ttyp)) {
   1600 		ep->e_tdev = tp->t_dev;
   1601 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
   1602 		ep->e_tsess = tp->t_session;
   1603 	} else
   1604 		ep->e_tdev = NODEV;
   1605 	if (p->p_wmesg)
   1606 		strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
   1607 	ep->e_xsize = ep->e_xrssize = 0;
   1608 	ep->e_xccount = ep->e_xswrss = 0;
   1609 	ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
   1610 	if (SESS_LEADER(p))
   1611 		ep->e_flag |= EPROC_SLEADER;
   1612 	strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
   1613 }
   1614 
   1615 /*
   1616  * Fill in an eproc structure for the specified process.
   1617  */
   1618 static void
   1619 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki)
   1620 {
   1621 	struct tty *tp;
   1622 
   1623 	memset(ki, 0, sizeof(*ki));
   1624 
   1625 	ki->p_forw = PTRTOINT64(p->p_forw);
   1626 	ki->p_back = PTRTOINT64(p->p_back);
   1627 	ki->p_paddr = PTRTOINT64(p);
   1628 
   1629 	ki->p_addr = PTRTOINT64(p->p_addr);
   1630 	ki->p_fd = PTRTOINT64(p->p_fd);
   1631 	ki->p_cwdi = PTRTOINT64(p->p_cwdi);
   1632 	ki->p_stats = PTRTOINT64(p->p_stats);
   1633 	ki->p_limit = PTRTOINT64(p->p_limit);
   1634 	ki->p_vmspace = PTRTOINT64(p->p_vmspace);
   1635 	ki->p_sigacts = PTRTOINT64(p->p_sigacts);
   1636 	ki->p_sess = PTRTOINT64(p->p_session);
   1637 	ki->p_tsess = 0;	/* may be changed if controlling tty below */
   1638 	ki->p_ru = PTRTOINT64(p->p_ru);
   1639 
   1640 	ki->p_eflag = 0;
   1641 	ki->p_exitsig = p->p_exitsig;
   1642 	ki->p_flag = p->p_flag;
   1643 
   1644 	ki->p_pid = p->p_pid;
   1645 	if (p->p_pptr)
   1646 		ki->p_ppid = p->p_pptr->p_pid;
   1647 	else
   1648 		ki->p_ppid = 0;
   1649 	ki->p_sid = p->p_session->s_sid;
   1650 	ki->p__pgid = p->p_pgrp->pg_id;
   1651 
   1652 	ki->p_tpgid = NO_PID;	/* may be changed if controlling tty below */
   1653 
   1654 	ki->p_uid = p->p_ucred->cr_uid;
   1655 	ki->p_ruid = p->p_cred->p_ruid;
   1656 	ki->p_gid = p->p_ucred->cr_gid;
   1657 	ki->p_rgid = p->p_cred->p_rgid;
   1658 
   1659 	memcpy(ki->p_groups, p->p_cred->pc_ucred->cr_groups,
   1660 	    min(sizeof(ki->p_groups), sizeof(p->p_cred->pc_ucred->cr_groups)));
   1661 	ki->p_ngroups = p->p_cred->pc_ucred->cr_ngroups;
   1662 
   1663 	ki->p_jobc = p->p_pgrp->pg_jobc;
   1664 	if ((p->p_flag & P_CONTROLT) && (tp = p->p_session->s_ttyp)) {
   1665 		ki->p_tdev = tp->t_dev;
   1666 		ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
   1667 		ki->p_tsess = PTRTOINT64(tp->t_session);
   1668 	} else {
   1669 		ki->p_tdev = NODEV;
   1670 	}
   1671 
   1672 	ki->p_estcpu = p->p_estcpu;
   1673 	ki->p_rtime_sec = p->p_rtime.tv_sec;
   1674 	ki->p_rtime_usec = p->p_rtime.tv_usec;
   1675 	ki->p_cpticks = p->p_cpticks;
   1676 	ki->p_pctcpu = p->p_pctcpu;
   1677 	ki->p_swtime = p->p_swtime;
   1678 	ki->p_slptime = p->p_slptime;
   1679 	if (p->p_stat == SONPROC) {
   1680 		KDASSERT(p->p_cpu != NULL);
   1681 		ki->p_schedflags = p->p_cpu->ci_schedstate.spc_flags;
   1682 	} else
   1683 		ki->p_schedflags = 0;
   1684 
   1685 	ki->p_uticks = p->p_uticks;
   1686 	ki->p_sticks = p->p_sticks;
   1687 	ki->p_iticks = p->p_iticks;
   1688 
   1689 	ki->p_tracep = PTRTOINT64(p->p_tracep);
   1690 	ki->p_traceflag = p->p_traceflag;
   1691 
   1692 	ki->p_holdcnt = p->p_holdcnt;
   1693 
   1694 	memcpy(&ki->p_siglist, &p->p_sigctx.ps_siglist, sizeof(ki_sigset_t));
   1695 	memcpy(&ki->p_sigmask, &p->p_sigctx.ps_sigmask, sizeof(ki_sigset_t));
   1696 	memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
   1697 	memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
   1698 
   1699 	ki->p_stat = p->p_stat;
   1700 	ki->p_priority = p->p_priority;
   1701 	ki->p_usrpri = p->p_usrpri;
   1702 	ki->p_nice = p->p_nice;
   1703 
   1704 	ki->p_xstat = p->p_xstat;
   1705 	ki->p_acflag = p->p_acflag;
   1706 
   1707 	strncpy(ki->p_comm, p->p_comm,
   1708 	    min(sizeof(ki->p_comm), sizeof(p->p_comm)));
   1709 
   1710 	if (p->p_wmesg)
   1711 		strncpy(ki->p_wmesg, p->p_wmesg, sizeof(ki->p_wmesg));
   1712 	ki->p_wchan = PTRTOINT64(p->p_wchan);
   1713 
   1714 	strncpy(ki->p_login, p->p_session->s_login, sizeof(ki->p_login));
   1715 
   1716 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   1717 		ki->p_vm_rssize = 0;
   1718 		ki->p_vm_tsize = 0;
   1719 		ki->p_vm_dsize = 0;
   1720 		ki->p_vm_ssize = 0;
   1721 	} else {
   1722 		struct vmspace *vm = p->p_vmspace;
   1723 
   1724 		ki->p_vm_rssize = vm_resident_count(vm);
   1725 		ki->p_vm_tsize = vm->vm_tsize;
   1726 		ki->p_vm_dsize = vm->vm_dsize;
   1727 		ki->p_vm_ssize = vm->vm_ssize;
   1728 	}
   1729 
   1730 	if (p->p_session->s_ttyvp)
   1731 		ki->p_eflag |= EPROC_CTTY;
   1732 	if (SESS_LEADER(p))
   1733 		ki->p_eflag |= EPROC_SLEADER;
   1734 
   1735 	/* XXX Is this double check necessary? */
   1736 	if ((p->p_flag & P_INMEM) == 0 || P_ZOMBIE(p)) {
   1737 		ki->p_uvalid = 0;
   1738 	} else {
   1739 		ki->p_uvalid = 1;
   1740 
   1741 		ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
   1742 		ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
   1743 
   1744 		ki->p_uutime_sec = p->p_stats->p_ru.ru_utime.tv_sec;
   1745 		ki->p_uutime_usec = p->p_stats->p_ru.ru_utime.tv_usec;
   1746 		ki->p_ustime_sec = p->p_stats->p_ru.ru_stime.tv_sec;
   1747 		ki->p_ustime_usec = p->p_stats->p_ru.ru_stime.tv_usec;
   1748 
   1749 		ki->p_uru_maxrss = p->p_stats->p_ru.ru_maxrss;
   1750 		ki->p_uru_ixrss = p->p_stats->p_ru.ru_ixrss;
   1751 		ki->p_uru_idrss = p->p_stats->p_ru.ru_idrss;
   1752 		ki->p_uru_isrss = p->p_stats->p_ru.ru_isrss;
   1753 		ki->p_uru_minflt = p->p_stats->p_ru.ru_minflt;
   1754 		ki->p_uru_majflt = p->p_stats->p_ru.ru_majflt;
   1755 		ki->p_uru_nswap = p->p_stats->p_ru.ru_nswap;
   1756 		ki->p_uru_inblock = p->p_stats->p_ru.ru_inblock;
   1757 		ki->p_uru_oublock = p->p_stats->p_ru.ru_oublock;
   1758 		ki->p_uru_msgsnd = p->p_stats->p_ru.ru_msgsnd;
   1759 		ki->p_uru_msgrcv = p->p_stats->p_ru.ru_msgrcv;
   1760 		ki->p_uru_nsignals = p->p_stats->p_ru.ru_nsignals;
   1761 		ki->p_uru_nvcsw = p->p_stats->p_ru.ru_nvcsw;
   1762 		ki->p_uru_nivcsw = p->p_stats->p_ru.ru_nivcsw;
   1763 
   1764 		ki->p_uctime_sec = p->p_stats->p_cru.ru_utime.tv_sec +
   1765 		    p->p_stats->p_cru.ru_stime.tv_sec;
   1766 		ki->p_uctime_usec = p->p_stats->p_cru.ru_utime.tv_usec +
   1767 		    p->p_stats->p_cru.ru_stime.tv_usec;
   1768 	}
   1769 #ifdef MULTIPROCESSOR
   1770 	if (p->p_cpu != NULL)
   1771 		ki->p_cpuid = p->p_cpu->ci_cpuid;
   1772 	else
   1773 #endif
   1774 		ki->p_cpuid = KI_NOCPU;
   1775 }
   1776 
   1777 int
   1778 sysctl_procargs(int *name, u_int namelen, void *where, size_t *sizep,
   1779     struct proc *up)
   1780 {
   1781 	struct ps_strings pss;
   1782 	struct proc *p;
   1783 	size_t len, upper_bound, xlen, i;
   1784 	struct uio auio;
   1785 	struct iovec aiov;
   1786 	vaddr_t argv;
   1787 	pid_t pid;
   1788 	int nargv, type, error;
   1789 	char *arg;
   1790 	char *tmp;
   1791 
   1792 	if (namelen != 2)
   1793 		return (EINVAL);
   1794 	pid = name[0];
   1795 	type = name[1];
   1796 
   1797 	switch (type) {
   1798 	case KERN_PROC_ARGV:
   1799 	case KERN_PROC_NARGV:
   1800 	case KERN_PROC_ENV:
   1801 	case KERN_PROC_NENV:
   1802 		/* ok */
   1803 		break;
   1804 	default:
   1805 		return (EINVAL);
   1806 	}
   1807 
   1808 	/* check pid */
   1809 	if ((p = pfind(pid)) == NULL)
   1810 		return (EINVAL);
   1811 
   1812 	/* only root or same user change look at the environment */
   1813 	if (type == KERN_PROC_ENV || type == KERN_PROC_NENV) {
   1814 		if (up->p_ucred->cr_uid != 0) {
   1815 			if (up->p_cred->p_ruid != p->p_cred->p_ruid ||
   1816 			    up->p_cred->p_ruid != p->p_cred->p_svuid)
   1817 				return (EPERM);
   1818 		}
   1819 	}
   1820 
   1821 	if (sizep != NULL && where == NULL) {
   1822 		if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
   1823 			*sizep = sizeof (int);
   1824 		else
   1825 			*sizep = ARG_MAX;	/* XXX XXX XXX */
   1826 		return (0);
   1827 	}
   1828 	if (where == NULL || sizep == NULL)
   1829 		return (EINVAL);
   1830 
   1831 	/*
   1832 	 * Zombies don't have a stack, so we can't read their psstrings.
   1833 	 * System processes also don't have a user stack.
   1834 	 */
   1835 	if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0)
   1836 		return (EINVAL);
   1837 
   1838 	/*
   1839 	 * Lock the process down in memory.
   1840 	 */
   1841 	/* XXXCDC: how should locking work here? */
   1842 	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
   1843 		return (EFAULT);
   1844 	p->p_vmspace->vm_refcnt++;	/* XXX */
   1845 
   1846 	/*
   1847 	 * Allocate a temporary buffer to hold the arguments.
   1848 	 */
   1849 	arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
   1850 
   1851 	/*
   1852 	 * Read in the ps_strings structure.
   1853 	 */
   1854 	aiov.iov_base = &pss;
   1855 	aiov.iov_len = sizeof(pss);
   1856 	auio.uio_iov = &aiov;
   1857 	auio.uio_iovcnt = 1;
   1858 	auio.uio_offset = (vaddr_t)p->p_psstr;
   1859 	auio.uio_resid = sizeof(pss);
   1860 	auio.uio_segflg = UIO_SYSSPACE;
   1861 	auio.uio_rw = UIO_READ;
   1862 	auio.uio_procp = NULL;
   1863 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
   1864 	if (error)
   1865 		goto done;
   1866 
   1867 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
   1868 		memcpy(&nargv, (char *)&pss + p->p_psnargv, sizeof(nargv));
   1869 	else
   1870 		memcpy(&nargv, (char *)&pss + p->p_psnenv, sizeof(nargv));
   1871 	if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
   1872 		error = copyout(&nargv, where, sizeof(nargv));
   1873 		*sizep = sizeof(nargv);
   1874 		goto done;
   1875 	}
   1876 	/*
   1877 	 * Now read the address of the argument vector.
   1878 	 */
   1879 	switch (type) {
   1880 	case KERN_PROC_ARGV:
   1881 		/* XXX compat32 stuff here */
   1882 		memcpy(&tmp, (char *)&pss + p->p_psargv, sizeof(tmp));
   1883 		break;
   1884 	case KERN_PROC_ENV:
   1885 		memcpy(&tmp, (char *)&pss + p->p_psenv, sizeof(tmp));
   1886 		break;
   1887 	default:
   1888 		return (EINVAL);
   1889 	}
   1890 	auio.uio_offset = (off_t)(long)tmp;
   1891 	aiov.iov_base = &argv;
   1892 	aiov.iov_len = sizeof(argv);
   1893 	auio.uio_iov = &aiov;
   1894 	auio.uio_iovcnt = 1;
   1895 	auio.uio_resid = sizeof(argv);
   1896 	auio.uio_segflg = UIO_SYSSPACE;
   1897 	auio.uio_rw = UIO_READ;
   1898 	auio.uio_procp = NULL;
   1899 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
   1900 	if (error)
   1901 		goto done;
   1902 
   1903 	/*
   1904 	 * Now copy in the actual argument vector, one page at a time,
   1905 	 * since we don't know how long the vector is (though, we do
   1906 	 * know how many NUL-terminated strings are in the vector).
   1907 	 */
   1908 	len = 0;
   1909 	upper_bound = *sizep;
   1910 	for (; nargv != 0 && len < upper_bound; len += xlen) {
   1911 		aiov.iov_base = arg;
   1912 		aiov.iov_len = PAGE_SIZE;
   1913 		auio.uio_iov = &aiov;
   1914 		auio.uio_iovcnt = 1;
   1915 		auio.uio_offset = argv + len;
   1916 		xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
   1917 		auio.uio_resid = xlen;
   1918 		auio.uio_segflg = UIO_SYSSPACE;
   1919 		auio.uio_rw = UIO_READ;
   1920 		auio.uio_procp = NULL;
   1921 		error = uvm_io(&p->p_vmspace->vm_map, &auio);
   1922 		if (error)
   1923 			goto done;
   1924 
   1925 		for (i = 0; i < xlen && nargv != 0; i++) {
   1926 			if (arg[i] == '\0')
   1927 				nargv--;	/* one full string */
   1928 		}
   1929 
   1930 		/*
   1931 		 * Make sure we don't copyout past the end of the user's
   1932 		 * buffer.
   1933 		 */
   1934 		if (len + i > upper_bound)
   1935 			i = upper_bound - len;
   1936 
   1937 		error = copyout(arg, (char *)where + len, i);
   1938 		if (error)
   1939 			break;
   1940 
   1941 		if (nargv == 0) {
   1942 			len += i;
   1943 			break;
   1944 		}
   1945 	}
   1946 	*sizep = len;
   1947 
   1948 done:
   1949 	uvmspace_free(p->p_vmspace);
   1950 
   1951 	free(arg, M_TEMP);
   1952 	return (error);
   1953 }
   1954 
   1955 #if NPTY > 0
   1956 int pty_maxptys(int, int);		/* defined in kern/tty_pty.c */
   1957 
   1958 /*
   1959  * Validate parameters and get old / set new parameters
   1960  * for pty sysctl function.
   1961  */
   1962 static int
   1963 sysctl_pty(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
   1964 {
   1965 	int error = 0;
   1966 	int oldmax = 0, newmax = 0;
   1967 
   1968 	/* get current value of maxptys */
   1969 	oldmax = pty_maxptys(0, 0);
   1970 
   1971 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &oldmax, int)
   1972 
   1973 	if (!error && newp) {
   1974 		SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
   1975 		SYSCTL_SCALAR_NEWPCOP_TYP(newp, &newmax, int)
   1976 
   1977 		if (newmax != pty_maxptys(newmax, (newp != NULL)))
   1978 			return (EINVAL);
   1979 
   1980 	}
   1981 
   1982 	return (error);
   1983 }
   1984 #endif /* NPTY > 0 */
   1985 
   1986 static int
   1987 sysctl_dotkstat(name, namelen, where, sizep, newp)
   1988 	int *name;
   1989 	u_int namelen;
   1990 	void *where;
   1991 	size_t *sizep;
   1992 	void *newp;
   1993 {
   1994 	/* all sysctl names at this level are terminal */
   1995 	if (namelen != 1)
   1996 		return (ENOTDIR);		/* overloaded */
   1997 
   1998 	switch (name[0]) {
   1999 	case KERN_TKSTAT_NIN:
   2000 		return (sysctl_rdquad(where, sizep, newp, tk_nin));
   2001 	case KERN_TKSTAT_NOUT:
   2002 		return (sysctl_rdquad(where, sizep, newp, tk_nout));
   2003 	case KERN_TKSTAT_CANCC:
   2004 		return (sysctl_rdquad(where, sizep, newp, tk_cancc));
   2005 	case KERN_TKSTAT_RAWCC:
   2006 		return (sysctl_rdquad(where, sizep, newp, tk_rawcc));
   2007 	default:
   2008 		return (EOPNOTSUPP);
   2009 	}
   2010 }
   2011 
   2012 static int
   2013 sysctl_dodev(name, namelen, oldp, oldlenp, newp, newlen, p)
   2014 	int *name;
   2015 	u_int namelen;
   2016 	void *oldp;
   2017 	size_t *oldlenp;
   2018 	void *newp;
   2019 	size_t newlen;
   2020 	struct proc *p;
   2021 {
   2022 	/* no sysctl names at this level are terminal */
   2023 	if (namelen < 1)
   2024 		return (ENOTDIR);		/* overloaded */
   2025 
   2026 	switch (name[0]) {
   2027 #if NUBTBCMFW
   2028 	case HW_DEV_UBTBCMFW:
   2029 		return (hw_dev_ubtbcmfw_sysctl(name + 1, namelen - 1,
   2030 		    oldp, oldlenp, newp, newlen, p));
   2031 #endif
   2032 	default:
   2033 		return (EOPNOTSUPP);
   2034 	}
   2035 }
   2036