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