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