Home | History | Annotate | Line # | Download | only in kern
kern_sysctl.c revision 1.61
      1 /*	$NetBSD: kern_sysctl.c,v 1.61 2000/04/15 04:38:07 simonb Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Mike Karels at Berkeley Software Design, Inc.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)kern_sysctl.c	8.9 (Berkeley) 5/20/95
     39  */
     40 
     41 /*
     42  * sysctl system call.
     43  */
     44 
     45 #include "opt_ddb.h"
     46 #include "opt_insecure.h"
     47 #include "opt_defcorename.h"
     48 #include "opt_sysv.h"
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/kernel.h>
     53 #include <sys/malloc.h>
     54 #include <sys/pool.h>
     55 #include <sys/proc.h>
     56 #include <sys/file.h>
     57 #include <sys/vnode.h>
     58 #include <sys/unistd.h>
     59 #include <sys/buf.h>
     60 #include <sys/ioctl.h>
     61 #include <sys/tty.h>
     62 #include <sys/disklabel.h>
     63 #include <sys/device.h>
     64 #include <vm/vm.h>
     65 #include <sys/sysctl.h>
     66 #include <sys/msgbuf.h>
     67 
     68 #include <uvm/uvm_extern.h>
     69 
     70 #include <sys/mount.h>
     71 #include <sys/syscallargs.h>
     72 #include <sys/resource.h>
     73 #include <sys/resourcevar.h>
     74 
     75 
     76 #if defined(DDB)
     77 #include <ddb/ddbvar.h>
     78 #endif
     79 
     80 /*
     81  * Locking and stats
     82  */
     83 static struct sysctl_lock {
     84 	int	sl_lock;
     85 	int	sl_want;
     86 	int	sl_locked;
     87 } memlock;
     88 
     89 int
     90 sys___sysctl(p, v, retval)
     91 	struct proc *p;
     92 	void *v;
     93 	register_t *retval;
     94 {
     95 	struct sys___sysctl_args /* {
     96 		syscallarg(int *) name;
     97 		syscallarg(u_int) namelen;
     98 		syscallarg(void *) old;
     99 		syscallarg(size_t *) oldlenp;
    100 		syscallarg(void *) new;
    101 		syscallarg(size_t) newlen;
    102 	} */ *uap = v;
    103 	int error, dolock = 1;
    104 	size_t savelen = 0, oldlen = 0;
    105 	sysctlfn *fn;
    106 	int name[CTL_MAXNAME];
    107 	size_t *oldlenp;
    108 
    109 	/*
    110 	 * all top-level sysctl names are non-terminal
    111 	 */
    112 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
    113 		return (EINVAL);
    114 	error = copyin(SCARG(uap, name), &name,
    115 		       SCARG(uap, namelen) * sizeof(int));
    116 	if (error)
    117 		return (error);
    118 
    119 	/*
    120 	 * For all but CTL_PROC, must be root to change a value.
    121 	 * For CTL_PROC, must be root, or owner of the proc (and not suid),
    122 	 * this is checked in proc_sysctl() (once we know the targer proc).
    123 	 */
    124 	if (SCARG(uap, new) != NULL && name[0] != CTL_PROC &&
    125 		    (error = suser(p->p_ucred, &p->p_acflag)))
    126 			return error;
    127 
    128 	switch (name[0]) {
    129 	case CTL_KERN:
    130 		fn = kern_sysctl;
    131 		if (name[2] != KERN_VNODE)	/* XXX */
    132 			dolock = 0;
    133 		break;
    134 	case CTL_HW:
    135 		fn = hw_sysctl;
    136 		break;
    137 	case CTL_VM:
    138 		fn = uvm_sysctl;
    139 		break;
    140 	case CTL_NET:
    141 		fn = net_sysctl;
    142 		break;
    143 	case CTL_VFS:
    144 		fn = vfs_sysctl;
    145 		break;
    146 	case CTL_MACHDEP:
    147 		fn = cpu_sysctl;
    148 		break;
    149 #ifdef DEBUG
    150 	case CTL_DEBUG:
    151 		fn = debug_sysctl;
    152 		break;
    153 #endif
    154 #ifdef DDB
    155 	case CTL_DDB:
    156 		fn = ddb_sysctl;
    157 		break;
    158 #endif
    159 	case CTL_PROC:
    160 		fn = proc_sysctl;
    161 		break;
    162 	default:
    163 		return (EOPNOTSUPP);
    164 	}
    165 
    166 	oldlenp = SCARG(uap, oldlenp);
    167 	if (oldlenp) {
    168 		if ((error = copyin(oldlenp, &oldlen, sizeof(oldlen))))
    169 			return (error);
    170 		oldlenp = &oldlen;
    171 	}
    172 	if (SCARG(uap, old) != NULL) {
    173 		if (!uvm_useracc(SCARG(uap, old), oldlen, B_WRITE))
    174 			return (EFAULT);
    175 		while (memlock.sl_lock) {
    176 			memlock.sl_want = 1;
    177 			sleep((caddr_t)&memlock, PRIBIO+1);
    178 			memlock.sl_locked++;
    179 		}
    180 		memlock.sl_lock = 1;
    181 		if (dolock) {
    182 			/*
    183 			 * XXX Um, this is kind of evil.  What should we
    184 			 * XXX be passing here?
    185 			 */
    186 			if (uvm_vslock(p, SCARG(uap, old), oldlen,
    187 			    VM_PROT_NONE) != KERN_SUCCESS) {
    188 				memlock.sl_lock = 0;
    189 				if (memlock.sl_want) {
    190 					memlock.sl_want = 0;
    191 					wakeup((caddr_t)&memlock);
    192 					return (EFAULT);
    193 				}
    194 			}
    195 		}
    196 		savelen = oldlen;
    197 	}
    198 	error = (*fn)(name + 1, SCARG(uap, namelen) - 1, SCARG(uap, old),
    199 	    oldlenp, SCARG(uap, new), SCARG(uap, newlen), p);
    200 	if (SCARG(uap, old) != NULL) {
    201 		if (dolock)
    202 			uvm_vsunlock(p, SCARG(uap, old), savelen);
    203 		memlock.sl_lock = 0;
    204 		if (memlock.sl_want) {
    205 			memlock.sl_want = 0;
    206 			wakeup((caddr_t)&memlock);
    207 		}
    208 	}
    209 	if (error)
    210 		return (error);
    211 	if (SCARG(uap, oldlenp))
    212 		error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen));
    213 	return (error);
    214 }
    215 
    216 /*
    217  * Attributes stored in the kernel.
    218  */
    219 char hostname[MAXHOSTNAMELEN];
    220 int hostnamelen;
    221 char domainname[MAXHOSTNAMELEN];
    222 int domainnamelen;
    223 long hostid;
    224 #ifdef INSECURE
    225 int securelevel = -1;
    226 #else
    227 int securelevel = 0;
    228 #endif
    229 #ifdef DEFCORENAME
    230 char defcorename[MAXPATHLEN] = DEFCORENAME;
    231 int defcorenamelen = sizeof(DEFCORENAME);
    232 #else
    233 char defcorename[MAXPATHLEN] = "%n.core";
    234 int defcorenamelen = sizeof("%n.core");
    235 #endif
    236 extern	int	kern_logsigexit;
    237 
    238 /*
    239  * kernel related system variables.
    240  */
    241 int
    242 kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    243 	int *name;
    244 	u_int namelen;
    245 	void *oldp;
    246 	size_t *oldlenp;
    247 	void *newp;
    248 	size_t newlen;
    249 	struct proc *p;
    250 {
    251 	int error, level, inthostid;
    252 	int old_autonicetime;
    253 	int old_vnodes;
    254 
    255 	/* All sysctl names at this level, except for a few, are terminal. */
    256 	switch (name[0]) {
    257 	case KERN_PROC:
    258 	case KERN_PROF:
    259 	case KERN_MBUF:
    260 		/* Not terminal. */
    261 		break;
    262 	default:
    263 		if (namelen != 1)
    264 			return (ENOTDIR);	/* overloaded */
    265 	}
    266 
    267 	switch (name[0]) {
    268 	case KERN_OSTYPE:
    269 		return (sysctl_rdstring(oldp, oldlenp, newp, ostype));
    270 	case KERN_OSRELEASE:
    271 		return (sysctl_rdstring(oldp, oldlenp, newp, osrelease));
    272 	case KERN_OSREV:
    273 		return (sysctl_rdint(oldp, oldlenp, newp, NetBSD));
    274 	case KERN_VERSION:
    275 		return (sysctl_rdstring(oldp, oldlenp, newp, version));
    276 	case KERN_MAXVNODES:
    277 		old_vnodes = desiredvnodes;
    278 		error = sysctl_int(oldp, oldlenp, newp, newlen, &desiredvnodes);
    279 		if (old_vnodes > desiredvnodes) {
    280 		        desiredvnodes = old_vnodes;
    281 			return (EINVAL);
    282 		}
    283 		return (error);
    284 	case KERN_MAXPROC:
    285 		return (sysctl_int(oldp, oldlenp, newp, newlen, &maxproc));
    286 	case KERN_MAXFILES:
    287 		return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles));
    288 	case KERN_ARGMAX:
    289 		return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX));
    290 	case KERN_SECURELVL:
    291 		level = securelevel;
    292 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) ||
    293 		    newp == NULL)
    294 			return (error);
    295 		if (level < securelevel && p->p_pid != 1)
    296 			return (EPERM);
    297 		securelevel = level;
    298 		return (0);
    299 	case KERN_HOSTNAME:
    300 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    301 		    hostname, sizeof(hostname));
    302 		if (newp && !error)
    303 			hostnamelen = newlen;
    304 		return (error);
    305 	case KERN_DOMAINNAME:
    306 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    307 		    domainname, sizeof(domainname));
    308 		if (newp && !error)
    309 			domainnamelen = newlen;
    310 		return (error);
    311 	case KERN_HOSTID:
    312 		inthostid = hostid;  /* XXX assumes sizeof long <= sizeof int */
    313 		error =  sysctl_int(oldp, oldlenp, newp, newlen, &inthostid);
    314 		hostid = inthostid;
    315 		return (error);
    316 	case KERN_CLOCKRATE:
    317 		return (sysctl_clockrate(oldp, oldlenp));
    318 	case KERN_BOOTTIME:
    319 		return (sysctl_rdstruct(oldp, oldlenp, newp, &boottime,
    320 		    sizeof(struct timeval)));
    321 	case KERN_VNODE:
    322 		return (sysctl_vnode(oldp, oldlenp, p));
    323 	case KERN_PROC:
    324 		return (sysctl_doeproc(name + 1, namelen - 1, oldp, oldlenp));
    325 	case KERN_FILE:
    326 		return (sysctl_file(oldp, oldlenp));
    327 #ifdef GPROF
    328 	case KERN_PROF:
    329 		return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
    330 		    newp, newlen));
    331 #endif
    332 	case KERN_POSIX1:
    333 		return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION));
    334 	case KERN_NGROUPS:
    335 		return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX));
    336 	case KERN_JOB_CONTROL:
    337 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    338 	case KERN_SAVED_IDS:
    339 #ifdef _POSIX_SAVED_IDS
    340 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    341 #else
    342 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    343 #endif
    344 	case KERN_MAXPARTITIONS:
    345 		return (sysctl_rdint(oldp, oldlenp, newp, MAXPARTITIONS));
    346 	case KERN_RAWPARTITION:
    347 		return (sysctl_rdint(oldp, oldlenp, newp, RAW_PART));
    348 #ifdef NTP
    349 	case KERN_NTPTIME:
    350 		return (sysctl_ntptime(oldp, oldlenp));
    351 #endif
    352 	case KERN_AUTONICETIME:
    353 	        old_autonicetime = autonicetime;
    354 	        error = sysctl_int(oldp, oldlenp, newp, newlen, &autonicetime);
    355 		if (autonicetime < 0)
    356  		        autonicetime = old_autonicetime;
    357 		return (error);
    358 	case KERN_AUTONICEVAL:
    359 		error = sysctl_int(oldp, oldlenp, newp, newlen, &autoniceval);
    360 		if (autoniceval < PRIO_MIN)
    361 			autoniceval = PRIO_MIN;
    362 		if (autoniceval > PRIO_MAX)
    363 			autoniceval = PRIO_MAX;
    364 		return (error);
    365 	case KERN_RTC_OFFSET:
    366 		return (sysctl_rdint(oldp, oldlenp, newp, rtc_offset));
    367 	case KERN_ROOT_DEVICE:
    368 		return (sysctl_rdstring(oldp, oldlenp, newp,
    369 		    root_device->dv_xname));
    370 	case KERN_MSGBUFSIZE:
    371 		/*
    372 		 * deal with cases where the message buffer has
    373 		 * become corrupted.
    374 		 */
    375 		if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
    376 			msgbufenabled = 0;
    377 			return (ENXIO);
    378 		}
    379 		return (sysctl_rdint(oldp, oldlenp, newp, msgbufp->msg_bufs));
    380 	case KERN_FSYNC:
    381 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    382 	case KERN_SYSVMSG:
    383 #ifdef SYSVMSG
    384 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    385 #else
    386 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    387 #endif
    388 	case KERN_SYSVSEM:
    389 #ifdef SYSVSEM
    390 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    391 #else
    392 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    393 #endif
    394 	case KERN_SYSVSHM:
    395 #ifdef SYSVSHM
    396 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    397 #else
    398 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    399 #endif
    400  	case KERN_DEFCORENAME:
    401 		if (newp && newlen < 1)
    402 			return (EINVAL);
    403 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    404 		    defcorename, sizeof(defcorename));
    405 		if (newp && !error)
    406 			defcorenamelen = newlen;
    407 		return (error);
    408 	case KERN_SYNCHRONIZED_IO:
    409 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    410 	case KERN_IOV_MAX:
    411 		return (sysctl_rdint(oldp, oldlenp, newp, IOV_MAX));
    412 	case KERN_MBUF:
    413 		return (sysctl_dombuf(name + 1, namelen - 1, oldp, oldlenp,
    414 		    newp, newlen));
    415 	case KERN_MAPPED_FILES:
    416 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    417 	case KERN_MEMLOCK:
    418 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    419 	case KERN_MEMLOCK_RANGE:
    420 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    421 	case KERN_MEMORY_PROTECTION:
    422 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    423 	case KERN_LOGIN_NAME_MAX:
    424 		return (sysctl_rdint(oldp, oldlenp, newp, LOGIN_NAME_MAX));
    425 	case KERN_LOGSIGEXIT:
    426 		return (sysctl_int(oldp, oldlenp, newp, newlen, &kern_logsigexit));
    427 	default:
    428 		return (EOPNOTSUPP);
    429 	}
    430 	/* NOTREACHED */
    431 }
    432 
    433 /*
    434  * hardware related system variables.
    435  */
    436 int
    437 hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    438 	int *name;
    439 	u_int namelen;
    440 	void *oldp;
    441 	size_t *oldlenp;
    442 	void *newp;
    443 	size_t newlen;
    444 	struct proc *p;
    445 {
    446 
    447 	/* all sysctl names at this level are terminal */
    448 	if (namelen != 1)
    449 		return (ENOTDIR);		/* overloaded */
    450 
    451 	switch (name[0]) {
    452 	case HW_MACHINE:
    453 		return (sysctl_rdstring(oldp, oldlenp, newp, machine));
    454 	case HW_MACHINE_ARCH:
    455 		return (sysctl_rdstring(oldp, oldlenp, newp, machine_arch));
    456 	case HW_MODEL:
    457 		return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
    458 	case HW_NCPU:
    459 		return (sysctl_rdint(oldp, oldlenp, newp, 1));	/* XXX */
    460 	case HW_BYTEORDER:
    461 		return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
    462 	case HW_PHYSMEM:
    463 		return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
    464 	case HW_USERMEM:
    465 		return (sysctl_rdint(oldp, oldlenp, newp,
    466 		    ctob(physmem - uvmexp.wired)));
    467 	case HW_PAGESIZE:
    468 		return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
    469 	case HW_ALIGNBYTES:
    470 		return (sysctl_rdint(oldp, oldlenp, newp, ALIGNBYTES));
    471 	default:
    472 		return (EOPNOTSUPP);
    473 	}
    474 	/* NOTREACHED */
    475 }
    476 
    477 #ifdef DEBUG
    478 /*
    479  * Debugging related system variables.
    480  */
    481 struct ctldebug debug0, debug1, debug2, debug3, debug4;
    482 struct ctldebug debug5, debug6, debug7, debug8, debug9;
    483 struct ctldebug debug10, debug11, debug12, debug13, debug14;
    484 struct ctldebug debug15, debug16, debug17, debug18, debug19;
    485 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
    486 	&debug0, &debug1, &debug2, &debug3, &debug4,
    487 	&debug5, &debug6, &debug7, &debug8, &debug9,
    488 	&debug10, &debug11, &debug12, &debug13, &debug14,
    489 	&debug15, &debug16, &debug17, &debug18, &debug19,
    490 };
    491 int
    492 debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    493 	int *name;
    494 	u_int namelen;
    495 	void *oldp;
    496 	size_t *oldlenp;
    497 	void *newp;
    498 	size_t newlen;
    499 	struct proc *p;
    500 {
    501 	struct ctldebug *cdp;
    502 
    503 	/* all sysctl names at this level are name and field */
    504 	if (namelen != 2)
    505 		return (ENOTDIR);		/* overloaded */
    506 	cdp = debugvars[name[0]];
    507 	if (name[0] >= CTL_DEBUG_MAXID || cdp->debugname == 0)
    508 		return (EOPNOTSUPP);
    509 	switch (name[1]) {
    510 	case CTL_DEBUG_NAME:
    511 		return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
    512 	case CTL_DEBUG_VALUE:
    513 		return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
    514 	default:
    515 		return (EOPNOTSUPP);
    516 	}
    517 	/* NOTREACHED */
    518 }
    519 #endif /* DEBUG */
    520 
    521 int
    522 proc_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    523 	int *name;
    524 	u_int namelen;
    525 	void *oldp;
    526 	size_t *oldlenp;
    527 	void *newp;
    528 	size_t newlen;
    529 	struct proc *p;
    530 {
    531 	struct proc *ptmp=NULL;
    532 	const struct proclist_desc *pd;
    533 	int error = 0;
    534 	struct rlimit alim;
    535 	struct plimit *newplim;
    536 	char *tmps = NULL;
    537 	int i, curlen, len;
    538 
    539 	if (namelen < 2)
    540 		return EINVAL;
    541 
    542 	if (name[0] == PROC_CURPROC) {
    543 		ptmp = p;
    544 	} else {
    545 		proclist_lock_read();
    546 		for (pd = proclists; pd->pd_list != NULL; pd++) {
    547 			for (ptmp = LIST_FIRST(pd->pd_list); ptmp != NULL;
    548 			    ptmp = LIST_NEXT(ptmp, p_list)) {
    549 				/* Skip embryonic processes. */
    550 				if (ptmp->p_stat == SIDL)
    551 					continue;
    552 				if (ptmp->p_pid == (pid_t)name[0])
    553 					break;
    554 			}
    555 			if (ptmp != NULL)
    556 				break;
    557 		}
    558 		proclist_unlock_read();
    559 		if (ptmp == NULL)
    560 			return(ESRCH);
    561 		if (p->p_ucred->cr_uid != 0) {
    562 			if(p->p_cred->p_ruid != ptmp->p_cred->p_ruid ||
    563 			    p->p_cred->p_ruid != ptmp->p_cred->p_svuid)
    564 				return EPERM;
    565 			if (ptmp->p_cred->p_rgid != ptmp->p_cred->p_svgid)
    566 				return EPERM; /* sgid proc */
    567 			for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
    568 				if (p->p_ucred->cr_groups[i] ==
    569 				    ptmp->p_cred->p_rgid)
    570 					break;
    571 			}
    572 			if (i == p->p_ucred->cr_ngroups)
    573 				return EPERM;
    574 		}
    575 	}
    576 	if (name[1] == PROC_PID_CORENAME) {
    577 		if (namelen != 2)
    578 			return EINVAL;
    579 		/*
    580 		 * Can't use sysctl_string() here because we may malloc a new
    581 		 * area during the process, so we have to do it by hand.
    582 		 */
    583 		curlen = strlen(ptmp->p_limit->pl_corename) + 1;
    584 		if (oldlenp  && *oldlenp < curlen) {
    585 			if (!oldp)
    586 				*oldlenp = curlen;
    587 			return (ENOMEM);
    588 		}
    589 		if (newp) {
    590 			if (securelevel > 2)
    591 				return EPERM;
    592 			if (newlen > MAXPATHLEN)
    593 				return ENAMETOOLONG;
    594 			tmps = malloc(newlen + 1, M_TEMP, M_WAITOK);
    595 			if (tmps == NULL)
    596 				return ENOMEM;
    597 			error = copyin(newp, tmps, newlen + 1);
    598 			tmps[newlen] = '\0';
    599 			if (error)
    600 				goto cleanup;
    601 			/* Enforce to be either 'core' for end with '.core' */
    602 			if (newlen < 4)  { /* c.o.r.e */
    603 				error = EINVAL;
    604 				goto cleanup;
    605 			}
    606 			len = newlen - 4;
    607 			if (len > 0) {
    608 				if (tmps[len - 1] != '.' &&
    609 				    tmps[len - 1] != '/') {
    610 					error = EINVAL;
    611 					goto cleanup;
    612 				}
    613 			}
    614 			if (strcmp(&tmps[len], "core") != 0) {
    615 				error = EINVAL;
    616 				goto cleanup;
    617 			}
    618 		}
    619 		if (oldp && oldlenp) {
    620 			*oldlenp = curlen;
    621 			error = copyout(ptmp->p_limit->pl_corename, oldp,
    622 			    curlen);
    623 		}
    624 		if (newp && error == 0) {
    625 			/* if the 2 strings are identical, don't limcopy() */
    626 			if (strcmp(tmps, ptmp->p_limit->pl_corename) == 0) {
    627 				error = 0;
    628 				goto cleanup;
    629 			}
    630 			if (ptmp->p_limit->p_refcnt > 1 &&
    631 			    (ptmp->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    632 				newplim = limcopy(ptmp->p_limit);
    633 				limfree(ptmp->p_limit);
    634 				ptmp->p_limit = newplim;
    635 			} else if (ptmp->p_limit->pl_corename != defcorename) {
    636 				free(ptmp->p_limit->pl_corename, M_TEMP);
    637 			}
    638 			ptmp->p_limit->pl_corename = tmps;
    639 			return (0);
    640 		}
    641 cleanup:
    642 		if (tmps)
    643 			free(tmps, M_TEMP);
    644 		return (error);
    645 	}
    646 	if (name[1] == PROC_PID_LIMIT) {
    647 		if (namelen != 4 || name[2] >= PROC_PID_LIMIT_MAXID)
    648 			return EINVAL;
    649 		memcpy(&alim, &ptmp->p_rlimit[name[2] - 1], sizeof(alim));
    650 		if (name[3] == PROC_PID_LIMIT_TYPE_HARD)
    651 			error = sysctl_quad(oldp, oldlenp, newp, newlen,
    652 			    &alim.rlim_max);
    653 		else if (name[3] == PROC_PID_LIMIT_TYPE_SOFT)
    654 			error = sysctl_quad(oldp, oldlenp, newp, newlen,
    655 			    &alim.rlim_cur);
    656 		else
    657 			error = EINVAL;
    658 
    659 		if (error)
    660 			return error;
    661 
    662 		if (newp)
    663 			error = dosetrlimit(ptmp, p->p_cred,
    664 			    name[2] - 1, &alim);
    665 		return error;
    666 	}
    667 	return (EINVAL);
    668 }
    669 
    670 /*
    671  * Convenience macros.
    672  */
    673 
    674 #define SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, len) 		\
    675 	if (oldlenp) {							\
    676 		if (!oldp)						\
    677 			*oldlenp = len;					\
    678 		else {							\
    679 			if (*oldlenp < len)				\
    680 				return(ENOMEM);				\
    681 			*oldlenp = len;					\
    682 			error = copyout((caddr_t)valp, oldp, len);	\
    683 		}							\
    684 	}
    685 
    686 #define SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, typ) \
    687 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, sizeof(typ))
    688 
    689 #define SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len)	\
    690 	if (newp && newlen != len)			\
    691 		return (EINVAL);
    692 
    693 #define SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, typ)	\
    694 	SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, sizeof(typ))
    695 
    696 #define SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, len)	\
    697 	if (error == 0 && newp)				\
    698 		error = copyin(newp, valp, len);
    699 
    700 #define SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, typ)      \
    701 	SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, sizeof(typ))
    702 
    703 #define SYSCTL_STRING_CORE(oldp, oldlenp, str)		\
    704 	if (oldlenp) {					\
    705 		len = strlen(str) + 1;			\
    706 		if (!oldp)				\
    707 			*oldlenp = len;			\
    708 		else {					\
    709 			if (*oldlenp < len) {		\
    710 				err2 = ENOMEM;		\
    711 				len = *oldlenp;		\
    712 			} else				\
    713 				*oldlenp = len;		\
    714 			error = copyout(str, oldp, len);\
    715 			if (error == 0)			\
    716 				error = err2;		\
    717 		}					\
    718 	}
    719 
    720 /*
    721  * Validate parameters and get old / set new parameters
    722  * for an integer-valued sysctl function.
    723  */
    724 int
    725 sysctl_int(oldp, oldlenp, newp, newlen, valp)
    726 	void *oldp;
    727 	size_t *oldlenp;
    728 	void *newp;
    729 	size_t newlen;
    730 	int *valp;
    731 {
    732 	int error = 0;
    733 
    734 	SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
    735 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, int)
    736 	SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, int)
    737 
    738 	return (error);
    739 }
    740 
    741 
    742 /*
    743  * As above, but read-only.
    744  */
    745 int
    746 sysctl_rdint(oldp, oldlenp, newp, val)
    747 	void *oldp;
    748 	size_t *oldlenp;
    749 	void *newp;
    750 	int val;
    751 {
    752 	int error = 0;
    753 
    754 	if (newp)
    755 		return (EPERM);
    756 
    757 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, int)
    758 
    759 	return (error);
    760 }
    761 
    762 /*
    763  * Validate parameters and get old / set new parameters
    764  * for an quad-valued sysctl function.
    765  */
    766 int
    767 sysctl_quad(oldp, oldlenp, newp, newlen, valp)
    768 	void *oldp;
    769 	size_t *oldlenp;
    770 	void *newp;
    771 	size_t newlen;
    772 	quad_t *valp;
    773 {
    774 	int error = 0;
    775 
    776 	SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, quad_t)
    777 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, quad_t)
    778 	SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, quad_t)
    779 
    780 	return (error);
    781 }
    782 
    783 /*
    784  * As above, but read-only.
    785  */
    786 int
    787 sysctl_rdquad(oldp, oldlenp, newp, val)
    788 	void *oldp;
    789 	size_t *oldlenp;
    790 	void *newp;
    791 	quad_t val;
    792 {
    793 	int error = 0;
    794 
    795 	if (newp)
    796 		return (EPERM);
    797 
    798 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, quad_t)
    799 
    800 	return (error);
    801 }
    802 
    803 /*
    804  * Validate parameters and get old / set new parameters
    805  * for a string-valued sysctl function.
    806  */
    807 int
    808 sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen)
    809 	void *oldp;
    810 	size_t *oldlenp;
    811 	void *newp;
    812 	size_t newlen;
    813 	char *str;
    814 	int maxlen;
    815 {
    816 	int len, error = 0, err2 = 0;
    817 
    818 	if (newp && newlen >= maxlen)
    819 		return (EINVAL);
    820 
    821 	SYSCTL_STRING_CORE(oldp, oldlenp, str);
    822 
    823 	if (error == 0 && newp) {
    824 		error = copyin(newp, str, newlen);
    825 		str[newlen] = 0;
    826 	}
    827 	return (error);
    828 }
    829 
    830 /*
    831  * As above, but read-only.
    832  */
    833 int
    834 sysctl_rdstring(oldp, oldlenp, newp, str)
    835 	void *oldp;
    836 	size_t *oldlenp;
    837 	void *newp;
    838 	char *str;
    839 {
    840 	int len, error = 0, err2 = 0;
    841 
    842 	if (newp)
    843 		return (EPERM);
    844 
    845 	SYSCTL_STRING_CORE(oldp, oldlenp, str);
    846 
    847 	return (error);
    848 }
    849 
    850 /*
    851  * Validate parameters and get old / set new parameters
    852  * for a structure oriented sysctl function.
    853  */
    854 int
    855 sysctl_struct(oldp, oldlenp, newp, newlen, sp, len)
    856 	void *oldp;
    857 	size_t *oldlenp;
    858 	void *newp;
    859 	size_t newlen;
    860 	void *sp;
    861 	int len;
    862 {
    863 	int error = 0;
    864 
    865 	SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len)
    866 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
    867 	SYSCTL_SCALAR_NEWPCOP_LEN(newp, sp, len)
    868 
    869 	return (error);
    870 }
    871 
    872 /*
    873  * Validate parameters and get old parameters
    874  * for a structure oriented sysctl function.
    875  */
    876 int
    877 sysctl_rdstruct(oldp, oldlenp, newp, sp, len)
    878 	void *oldp;
    879 	size_t *oldlenp;
    880 	void *newp, *sp;
    881 	int len;
    882 {
    883 	int error = 0;
    884 
    885 	if (newp)
    886 		return (EPERM);
    887 
    888 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
    889 
    890 	return (error);
    891 }
    892 
    893 /*
    894  * Get file structures.
    895  */
    896 int
    897 sysctl_file(where, sizep)
    898 	char *where;
    899 	size_t *sizep;
    900 {
    901 	int buflen, error;
    902 	struct file *fp;
    903 	char *start = where;
    904 
    905 	buflen = *sizep;
    906 	if (where == NULL) {
    907 		/*
    908 		 * overestimate by 10 files
    909 		 */
    910 		*sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
    911 		return (0);
    912 	}
    913 
    914 	/*
    915 	 * first copyout filehead
    916 	 */
    917 	if (buflen < sizeof(filehead)) {
    918 		*sizep = 0;
    919 		return (0);
    920 	}
    921 	error = copyout((caddr_t)&filehead, where, sizeof(filehead));
    922 	if (error)
    923 		return (error);
    924 	buflen -= sizeof(filehead);
    925 	where += sizeof(filehead);
    926 
    927 	/*
    928 	 * followed by an array of file structures
    929 	 */
    930 	for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
    931 		if (buflen < sizeof(struct file)) {
    932 			*sizep = where - start;
    933 			return (ENOMEM);
    934 		}
    935 		error = copyout((caddr_t)fp, where, sizeof(struct file));
    936 		if (error)
    937 			return (error);
    938 		buflen -= sizeof(struct file);
    939 		where += sizeof(struct file);
    940 	}
    941 	*sizep = where - start;
    942 	return (0);
    943 }
    944 
    945 /*
    946  * try over estimating by 5 procs
    947  */
    948 #define KERN_PROCSLOP	(5 * sizeof(struct kinfo_proc))
    949 
    950 int
    951 sysctl_doeproc(name, namelen, where, sizep)
    952 	int *name;
    953 	u_int namelen;
    954 	char *where;
    955 	size_t *sizep;
    956 {
    957 	struct proc *p;
    958 	struct kinfo_proc *dp = (struct kinfo_proc *)where;
    959 	int needed = 0;
    960 	int buflen = where != NULL ? *sizep : 0;
    961 	const struct proclist_desc *pd;
    962 	struct eproc eproc;
    963 	int error = 0;
    964 
    965 	if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
    966 		return (EINVAL);
    967 
    968 	proclist_lock_read();
    969 
    970 	pd = proclists;
    971 again:
    972 	for (p = LIST_FIRST(pd->pd_list); p != NULL;
    973 	     p = LIST_NEXT(p, p_list)) {
    974 		/*
    975 		 * Skip embryonic processes.
    976 		 */
    977 		if (p->p_stat == SIDL)
    978 			continue;
    979 		/*
    980 		 * TODO - make more efficient (see notes below).
    981 		 * do by session.
    982 		 */
    983 		switch (name[0]) {
    984 
    985 		case KERN_PROC_PID:
    986 			/* could do this with just a lookup */
    987 			if (p->p_pid != (pid_t)name[1])
    988 				continue;
    989 			break;
    990 
    991 		case KERN_PROC_PGRP:
    992 			/* could do this by traversing pgrp */
    993 			if (p->p_pgrp->pg_id != (pid_t)name[1])
    994 				continue;
    995 			break;
    996 
    997 		case KERN_PROC_TTY:
    998 			if (name[1] == KERN_PROC_TTY_REVOKE) {
    999 				if ((p->p_flag & P_CONTROLT) == 0 ||
   1000 				    p->p_session->s_ttyp == NULL ||
   1001 				    p->p_session->s_ttyvp != NULL)
   1002 					continue;
   1003 			} else if ((p->p_flag & P_CONTROLT) == 0 ||
   1004 			    p->p_session->s_ttyp == NULL) {
   1005 				if ((dev_t)name[1] != KERN_PROC_TTY_NODEV)
   1006 					continue;
   1007 			} else if (p->p_session->s_ttyp->t_dev !=
   1008 			    (dev_t)name[1])
   1009 				continue;
   1010 			break;
   1011 
   1012 		case KERN_PROC_UID:
   1013 			if (p->p_ucred->cr_uid != (uid_t)name[1])
   1014 				continue;
   1015 			break;
   1016 
   1017 		case KERN_PROC_RUID:
   1018 			if (p->p_cred->p_ruid != (uid_t)name[1])
   1019 				continue;
   1020 			break;
   1021 		}
   1022 		if (buflen >= sizeof(struct kinfo_proc)) {
   1023 			fill_eproc(p, &eproc);
   1024 			error = copyout((caddr_t)p, &dp->kp_proc,
   1025 					sizeof(struct proc));
   1026 			if (error)
   1027 				goto cleanup;
   1028 			error = copyout((caddr_t)&eproc, &dp->kp_eproc,
   1029 					sizeof(eproc));
   1030 			if (error)
   1031 				goto cleanup;
   1032 			dp++;
   1033 			buflen -= sizeof(struct kinfo_proc);
   1034 		}
   1035 		needed += sizeof(struct kinfo_proc);
   1036 	}
   1037 	pd++;
   1038 	if (pd->pd_list != NULL)
   1039 		goto again;
   1040 	proclist_unlock_read();
   1041 
   1042 	if (where != NULL) {
   1043 		*sizep = (caddr_t)dp - where;
   1044 		if (needed > *sizep)
   1045 			return (ENOMEM);
   1046 	} else {
   1047 		needed += KERN_PROCSLOP;
   1048 		*sizep = needed;
   1049 	}
   1050 	return (0);
   1051  cleanup:
   1052 	proclist_unlock_read();
   1053 	return (error);
   1054 }
   1055 
   1056 /*
   1057  * Fill in an eproc structure for the specified process.
   1058  */
   1059 void
   1060 fill_eproc(p, ep)
   1061 	struct proc *p;
   1062 	struct eproc *ep;
   1063 {
   1064 	struct tty *tp;
   1065 
   1066 	ep->e_paddr = p;
   1067 	ep->e_sess = p->p_session;
   1068 	ep->e_pcred = *p->p_cred;
   1069 	ep->e_ucred = *p->p_ucred;
   1070 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   1071 		ep->e_vm.vm_rssize = 0;
   1072 		ep->e_vm.vm_tsize = 0;
   1073 		ep->e_vm.vm_dsize = 0;
   1074 		ep->e_vm.vm_ssize = 0;
   1075 		/* ep->e_vm.vm_pmap = XXX; */
   1076 	} else {
   1077 		struct vmspace *vm = p->p_vmspace;
   1078 
   1079 		ep->e_vm.vm_rssize = vm_resident_count(vm);
   1080 		ep->e_vm.vm_tsize = vm->vm_tsize;
   1081 		ep->e_vm.vm_dsize = vm->vm_dsize;
   1082 		ep->e_vm.vm_ssize = vm->vm_ssize;
   1083 	}
   1084 	if (p->p_pptr)
   1085 		ep->e_ppid = p->p_pptr->p_pid;
   1086 	else
   1087 		ep->e_ppid = 0;
   1088 	ep->e_pgid = p->p_pgrp->pg_id;
   1089 	ep->e_sid = ep->e_sess->s_sid;
   1090 	ep->e_jobc = p->p_pgrp->pg_jobc;
   1091 	if ((p->p_flag & P_CONTROLT) &&
   1092 	     (tp = ep->e_sess->s_ttyp)) {
   1093 		ep->e_tdev = tp->t_dev;
   1094 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
   1095 		ep->e_tsess = tp->t_session;
   1096 	} else
   1097 		ep->e_tdev = NODEV;
   1098 	if (p->p_wmesg)
   1099 		strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
   1100 	ep->e_xsize = ep->e_xrssize = 0;
   1101 	ep->e_xccount = ep->e_xswrss = 0;
   1102 	ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
   1103 	if (SESS_LEADER(p))
   1104 		ep->e_flag |= EPROC_SLEADER;
   1105 	strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
   1106 }
   1107