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