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