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