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