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