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