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