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