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