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