Home | History | Annotate | Line # | Download | only in kern
kern_sysctl.c revision 1.86.2.27
      1  1.86.2.27   thorpej /*	$NetBSD: kern_sysctl.c,v 1.86.2.27 2002/12/19 00:50:43 thorpej Exp $	*/
      2        1.2       cgd 
      3        1.1       cgd /*-
      4        1.1       cgd  * Copyright (c) 1982, 1986, 1989, 1993
      5        1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6        1.1       cgd  *
      7        1.1       cgd  * This code is derived from software contributed to Berkeley by
      8        1.1       cgd  * Mike Karels at Berkeley Software Design, Inc.
      9        1.1       cgd  *
     10        1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11        1.1       cgd  * modification, are permitted provided that the following conditions
     12        1.1       cgd  * are met:
     13        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18        1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19        1.1       cgd  *    must display the following acknowledgement:
     20        1.1       cgd  *	This product includes software developed by the University of
     21        1.1       cgd  *	California, Berkeley and its contributors.
     22        1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23        1.1       cgd  *    may be used to endorse or promote products derived from this software
     24        1.1       cgd  *    without specific prior written permission.
     25        1.1       cgd  *
     26        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36        1.1       cgd  * SUCH DAMAGE.
     37        1.1       cgd  *
     38       1.34      fvdl  *	@(#)kern_sysctl.c	8.9 (Berkeley) 5/20/95
     39        1.1       cgd  */
     40        1.1       cgd 
     41        1.1       cgd /*
     42        1.1       cgd  * sysctl system call.
     43        1.1       cgd  */
     44       1.30   thorpej 
     45  1.86.2.10   nathanw #include <sys/cdefs.h>
     46  1.86.2.27   thorpej __KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.86.2.27 2002/12/19 00:50:43 thorpej Exp $");
     47  1.86.2.10   nathanw 
     48       1.38  jonathan #include "opt_ddb.h"
     49       1.30   thorpej #include "opt_insecure.h"
     50       1.52    bouyer #include "opt_defcorename.h"
     51  1.86.2.12   nathanw #include "opt_pipe.h"
     52       1.42      tron #include "opt_sysv.h"
     53       1.77  jdolecek #include "pty.h"
     54  1.86.2.21   nathanw #include "rnd.h"
     55        1.1       cgd 
     56        1.1       cgd #include <sys/param.h>
     57        1.1       cgd #include <sys/systm.h>
     58        1.1       cgd #include <sys/kernel.h>
     59       1.62    simonb #include <sys/buf.h>
     60       1.62    simonb #include <sys/device.h>
     61       1.62    simonb #include <sys/disklabel.h>
     62       1.62    simonb #include <sys/dkstat.h>
     63       1.62    simonb #include <sys/exec.h>
     64       1.62    simonb #include <sys/file.h>
     65       1.62    simonb #include <sys/ioctl.h>
     66        1.1       cgd #include <sys/malloc.h>
     67       1.62    simonb #include <sys/mount.h>
     68       1.62    simonb #include <sys/msgbuf.h>
     69       1.52    bouyer #include <sys/pool.h>
     70        1.1       cgd #include <sys/proc.h>
     71       1.62    simonb #include <sys/resource.h>
     72       1.62    simonb #include <sys/resourcevar.h>
     73  1.86.2.18   nathanw #include <sys/sa.h>
     74       1.62    simonb #include <sys/syscallargs.h>
     75       1.62    simonb #include <sys/tty.h>
     76       1.62    simonb #include <sys/unistd.h>
     77        1.1       cgd #include <sys/vnode.h>
     78   1.86.2.6   nathanw #include <sys/socketvar.h>
     79       1.75   thorpej #define	__SYSCTL_PRIVATE
     80        1.1       cgd #include <sys/sysctl.h>
     81       1.75   thorpej #include <sys/lock.h>
     82   1.86.2.9   nathanw #include <sys/namei.h>
     83       1.38  jonathan 
     84       1.69    simonb #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
     85       1.69    simonb #include <sys/ipc.h>
     86       1.69    simonb #endif
     87       1.69    simonb #ifdef SYSVMSG
     88       1.69    simonb #include <sys/msg.h>
     89       1.69    simonb #endif
     90       1.69    simonb #ifdef SYSVSEM
     91       1.69    simonb #include <sys/sem.h>
     92       1.69    simonb #endif
     93       1.69    simonb #ifdef SYSVSHM
     94       1.69    simonb #include <sys/shm.h>
     95       1.69    simonb #endif
     96       1.69    simonb 
     97       1.74    simonb #include <dev/cons.h>
     98       1.74    simonb 
     99       1.38  jonathan #if defined(DDB)
    100       1.38  jonathan #include <ddb/ddbvar.h>
    101       1.31       mrg #endif
    102       1.31       mrg 
    103  1.86.2.12   nathanw #ifndef PIPE_SOCKETPAIR
    104   1.86.2.6   nathanw #include <sys/pipe.h>
    105   1.86.2.6   nathanw #endif
    106   1.86.2.6   nathanw 
    107  1.86.2.21   nathanw #if NRND > 0
    108  1.86.2.21   nathanw #include <sys/rnd.h>
    109  1.86.2.21   nathanw #endif
    110  1.86.2.21   nathanw 
    111       1.62    simonb #define PTRTOINT64(foo)	((u_int64_t)(uintptr_t)(foo))
    112       1.62    simonb 
    113   1.86.2.6   nathanw static int sysctl_file(void *, size_t *);
    114       1.69    simonb #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
    115   1.86.2.6   nathanw static int sysctl_sysvipc(int *, u_int, void *, size_t *);
    116       1.69    simonb #endif
    117   1.86.2.6   nathanw static int sysctl_msgbuf(void *, size_t *);
    118   1.86.2.6   nathanw static int sysctl_doeproc(int *, u_int, void *, size_t *);
    119  1.86.2.15   nathanw static int sysctl_dolwp(int *, u_int, void *, size_t *);
    120  1.86.2.12   nathanw static int sysctl_dotkstat(int *, u_int, void *, size_t *, void *);
    121       1.83  sommerfe #ifdef MULTIPROCESSOR
    122   1.86.2.6   nathanw static int sysctl_docptime(void *, size_t *, void *);
    123   1.86.2.6   nathanw static int sysctl_ncpus(void);
    124       1.83  sommerfe #endif
    125   1.86.2.6   nathanw static void fill_kproc2(struct proc *, struct kinfo_proc2 *);
    126  1.86.2.15   nathanw static void fill_lwp(struct lwp *, struct kinfo_lwp *);
    127   1.86.2.6   nathanw static int sysctl_procargs(int *, u_int, void *, size_t *, struct proc *);
    128       1.80     bjh21 #if NPTY > 0
    129   1.86.2.6   nathanw static int sysctl_pty(void *, size_t *, void *, size_t);
    130       1.80     bjh21 #endif
    131       1.62    simonb 
    132       1.75   thorpej /*
    133       1.75   thorpej  * The `sysctl_memlock' is intended to keep too many processes from
    134       1.75   thorpej  * locking down memory by doing sysctls at once.  Whether or not this
    135       1.75   thorpej  * is really a good idea to worry about it probably a subject of some
    136       1.75   thorpej  * debate.
    137       1.75   thorpej  */
    138       1.75   thorpej struct lock sysctl_memlock;
    139       1.75   thorpej 
    140       1.75   thorpej void
    141       1.75   thorpej sysctl_init(void)
    142       1.75   thorpej {
    143       1.75   thorpej 
    144       1.75   thorpej 	lockinit(&sysctl_memlock, PRIBIO|PCATCH, "sysctl", 0, 0);
    145       1.75   thorpej }
    146       1.75   thorpej 
    147        1.1       cgd int
    148   1.86.2.6   nathanw sys___sysctl(struct lwp *l, void *v, register_t *retval)
    149       1.11   thorpej {
    150       1.60  augustss 	struct sys___sysctl_args /* {
    151        1.5       cgd 		syscallarg(int *) name;
    152        1.5       cgd 		syscallarg(u_int) namelen;
    153        1.5       cgd 		syscallarg(void *) old;
    154        1.5       cgd 		syscallarg(size_t *) oldlenp;
    155        1.5       cgd 		syscallarg(void *) new;
    156        1.5       cgd 		syscallarg(size_t) newlen;
    157       1.11   thorpej 	} */ *uap = v;
    158   1.86.2.1   nathanw 	struct proc *p = l->l_proc;
    159       1.75   thorpej 	int error;
    160       1.13  christos 	size_t savelen = 0, oldlen = 0;
    161        1.1       cgd 	sysctlfn *fn;
    162        1.1       cgd 	int name[CTL_MAXNAME];
    163       1.55        is 	size_t *oldlenp;
    164        1.1       cgd 
    165        1.1       cgd 	/*
    166        1.1       cgd 	 * all top-level sysctl names are non-terminal
    167        1.1       cgd 	 */
    168        1.5       cgd 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
    169        1.1       cgd 		return (EINVAL);
    170       1.13  christos 	error = copyin(SCARG(uap, name), &name,
    171       1.13  christos 		       SCARG(uap, namelen) * sizeof(int));
    172       1.13  christos 	if (error)
    173        1.1       cgd 		return (error);
    174        1.1       cgd 
    175       1.52    bouyer 	/*
    176       1.52    bouyer 	 * For all but CTL_PROC, must be root to change a value.
    177       1.52    bouyer 	 * For CTL_PROC, must be root, or owner of the proc (and not suid),
    178       1.52    bouyer 	 * this is checked in proc_sysctl() (once we know the targer proc).
    179       1.52    bouyer 	 */
    180       1.52    bouyer 	if (SCARG(uap, new) != NULL && name[0] != CTL_PROC &&
    181       1.52    bouyer 		    (error = suser(p->p_ucred, &p->p_acflag)))
    182       1.52    bouyer 			return error;
    183       1.52    bouyer 
    184        1.1       cgd 	switch (name[0]) {
    185        1.1       cgd 	case CTL_KERN:
    186        1.1       cgd 		fn = kern_sysctl;
    187        1.1       cgd 		break;
    188        1.1       cgd 	case CTL_HW:
    189        1.1       cgd 		fn = hw_sysctl;
    190        1.1       cgd 		break;
    191        1.1       cgd 	case CTL_VM:
    192       1.31       mrg 		fn = uvm_sysctl;
    193        1.1       cgd 		break;
    194        1.1       cgd 	case CTL_NET:
    195        1.1       cgd 		fn = net_sysctl;
    196        1.1       cgd 		break;
    197       1.34      fvdl 	case CTL_VFS:
    198       1.34      fvdl 		fn = vfs_sysctl;
    199        1.1       cgd 		break;
    200        1.1       cgd 	case CTL_MACHDEP:
    201        1.1       cgd 		fn = cpu_sysctl;
    202        1.1       cgd 		break;
    203        1.1       cgd #ifdef DEBUG
    204        1.1       cgd 	case CTL_DEBUG:
    205        1.1       cgd 		fn = debug_sysctl;
    206       1.20   thorpej 		break;
    207       1.20   thorpej #endif
    208       1.20   thorpej #ifdef DDB
    209       1.20   thorpej 	case CTL_DDB:
    210       1.20   thorpej 		fn = ddb_sysctl;
    211        1.1       cgd 		break;
    212        1.1       cgd #endif
    213       1.52    bouyer 	case CTL_PROC:
    214       1.52    bouyer 		fn = proc_sysctl;
    215       1.52    bouyer 		break;
    216  1.86.2.13   nathanw 
    217  1.86.2.13   nathanw 	case CTL_EMUL:
    218  1.86.2.13   nathanw 		fn = emul_sysctl;
    219  1.86.2.13   nathanw 		break;
    220        1.1       cgd 	default:
    221        1.1       cgd 		return (EOPNOTSUPP);
    222        1.1       cgd 	}
    223        1.1       cgd 
    224       1.75   thorpej 	/*
    225       1.75   thorpej 	 * XXX Hey, we wire `old', but what about `new'?
    226       1.75   thorpej 	 */
    227       1.75   thorpej 
    228       1.55        is 	oldlenp = SCARG(uap, oldlenp);
    229       1.55        is 	if (oldlenp) {
    230       1.55        is 		if ((error = copyin(oldlenp, &oldlen, sizeof(oldlen))))
    231       1.55        is 			return (error);
    232       1.55        is 		oldlenp = &oldlen;
    233       1.55        is 	}
    234        1.5       cgd 	if (SCARG(uap, old) != NULL) {
    235       1.75   thorpej 		error = lockmgr(&sysctl_memlock, LK_EXCLUSIVE, NULL);
    236       1.75   thorpej 		if (error)
    237       1.75   thorpej 			return (error);
    238  1.86.2.12   nathanw 		error = uvm_vslock(p, SCARG(uap, old), oldlen, VM_PROT_WRITE);
    239   1.86.2.3   nathanw 		if (error) {
    240       1.75   thorpej 			(void) lockmgr(&sysctl_memlock, LK_RELEASE, NULL);
    241   1.86.2.3   nathanw 			return error;
    242       1.45   thorpej 		}
    243        1.1       cgd 		savelen = oldlen;
    244        1.1       cgd 	}
    245        1.5       cgd 	error = (*fn)(name + 1, SCARG(uap, namelen) - 1, SCARG(uap, old),
    246       1.55        is 	    oldlenp, SCARG(uap, new), SCARG(uap, newlen), p);
    247        1.5       cgd 	if (SCARG(uap, old) != NULL) {
    248       1.75   thorpej 		uvm_vsunlock(p, SCARG(uap, old), savelen);
    249       1.75   thorpej 		(void) lockmgr(&sysctl_memlock, LK_RELEASE, NULL);
    250        1.1       cgd 	}
    251        1.1       cgd 	if (error)
    252        1.1       cgd 		return (error);
    253        1.5       cgd 	if (SCARG(uap, oldlenp))
    254        1.5       cgd 		error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen));
    255       1.16   thorpej 	return (error);
    256        1.1       cgd }
    257        1.1       cgd 
    258        1.1       cgd /*
    259        1.1       cgd  * Attributes stored in the kernel.
    260        1.1       cgd  */
    261        1.1       cgd char hostname[MAXHOSTNAMELEN];
    262        1.1       cgd int hostnamelen;
    263       1.75   thorpej 
    264        1.1       cgd char domainname[MAXHOSTNAMELEN];
    265        1.1       cgd int domainnamelen;
    266       1.75   thorpej 
    267        1.1       cgd long hostid;
    268       1.75   thorpej 
    269        1.8       cgd #ifdef INSECURE
    270        1.8       cgd int securelevel = -1;
    271        1.8       cgd #else
    272       1.17       mrg int securelevel = 0;
    273        1.8       cgd #endif
    274       1.75   thorpej 
    275       1.75   thorpej #ifndef DEFCORENAME
    276       1.75   thorpej #define	DEFCORENAME	"%n.core"
    277       1.75   thorpej #endif
    278       1.52    bouyer char defcorename[MAXPATHLEN] = DEFCORENAME;
    279       1.52    bouyer int defcorenamelen = sizeof(DEFCORENAME);
    280       1.75   thorpej 
    281       1.57      fair extern	int	kern_logsigexit;
    282       1.62    simonb extern	fixpt_t	ccpu;
    283  1.86.2.27   thorpej extern  int	forkfsleep;
    284        1.1       cgd 
    285       1.84  sommerfe #ifndef MULTIPROCESSOR
    286       1.84  sommerfe #define sysctl_ncpus() 1
    287       1.84  sommerfe #endif
    288       1.84  sommerfe 
    289       1.83  sommerfe #ifdef MULTIPROCESSOR
    290       1.83  sommerfe 
    291       1.83  sommerfe #ifndef CPU_INFO_FOREACH
    292       1.83  sommerfe #define CPU_INFO_ITERATOR int
    293       1.83  sommerfe #define CPU_INFO_FOREACH(cii, ci) cii = 0, ci = curcpu(); ci != NULL; ci = NULL
    294       1.83  sommerfe #endif
    295       1.83  sommerfe 
    296       1.83  sommerfe static int
    297   1.86.2.6   nathanw sysctl_docptime(void *oldp, size_t *oldlenp, void *newp)
    298       1.83  sommerfe {
    299       1.83  sommerfe 	u_int64_t cp_time[CPUSTATES];
    300       1.83  sommerfe 	int i;
    301       1.83  sommerfe 	struct cpu_info *ci;
    302       1.83  sommerfe 	CPU_INFO_ITERATOR cii;
    303       1.83  sommerfe 
    304  1.86.2.25   thorpej 	for (i = 0; i < CPUSTATES; i++)
    305       1.83  sommerfe 		cp_time[i] = 0;
    306       1.83  sommerfe 
    307       1.83  sommerfe 	for (CPU_INFO_FOREACH(cii, ci)) {
    308  1.86.2.25   thorpej 		for (i = 0; i < CPUSTATES; i++)
    309       1.83  sommerfe 			cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
    310       1.83  sommerfe 	}
    311       1.83  sommerfe 	return (sysctl_rdstruct(oldp, oldlenp, newp,
    312       1.83  sommerfe 	    cp_time, sizeof(cp_time)));
    313       1.83  sommerfe }
    314       1.84  sommerfe 
    315       1.84  sommerfe static int
    316       1.84  sommerfe sysctl_ncpus(void)
    317       1.84  sommerfe {
    318       1.84  sommerfe 	struct cpu_info *ci;
    319       1.84  sommerfe 	CPU_INFO_ITERATOR cii;
    320       1.84  sommerfe 
    321       1.84  sommerfe 	int ncpus = 0;
    322       1.84  sommerfe 	for (CPU_INFO_FOREACH(cii, ci))
    323       1.84  sommerfe 		ncpus++;
    324       1.84  sommerfe 	return ncpus;
    325       1.84  sommerfe }
    326       1.84  sommerfe 
    327       1.83  sommerfe #endif
    328       1.83  sommerfe 
    329        1.1       cgd /*
    330        1.1       cgd  * kernel related system variables.
    331        1.1       cgd  */
    332       1.13  christos int
    333   1.86.2.6   nathanw kern_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    334   1.86.2.6   nathanw     void *newp, size_t newlen, struct proc *p)
    335        1.1       cgd {
    336        1.1       cgd 	int error, level, inthostid;
    337       1.18  explorer 	int old_autonicetime;
    338       1.22       tls 	int old_vnodes;
    339       1.74    simonb 	dev_t consdev;
    340  1.86.2.21   nathanw #if NRND > 0
    341  1.86.2.21   nathanw 	int v;
    342  1.86.2.21   nathanw #endif
    343        1.1       cgd 
    344       1.44   thorpej 	/* All sysctl names at this level, except for a few, are terminal. */
    345       1.44   thorpej 	switch (name[0]) {
    346       1.44   thorpej 	case KERN_PROC:
    347       1.62    simonb 	case KERN_PROC2:
    348  1.86.2.15   nathanw 	case KERN_LWP:
    349       1.44   thorpej 	case KERN_PROF:
    350       1.44   thorpej 	case KERN_MBUF:
    351       1.62    simonb 	case KERN_PROC_ARGS:
    352       1.69    simonb 	case KERN_SYSVIPC_INFO:
    353   1.86.2.5   nathanw 	case KERN_PIPE:
    354  1.86.2.12   nathanw 	case KERN_TKSTAT:
    355       1.44   thorpej 		/* Not terminal. */
    356       1.44   thorpej 		break;
    357       1.44   thorpej 	default:
    358       1.44   thorpej 		if (namelen != 1)
    359       1.44   thorpej 			return (ENOTDIR);	/* overloaded */
    360       1.44   thorpej 	}
    361        1.1       cgd 
    362        1.1       cgd 	switch (name[0]) {
    363        1.1       cgd 	case KERN_OSTYPE:
    364        1.1       cgd 		return (sysctl_rdstring(oldp, oldlenp, newp, ostype));
    365        1.1       cgd 	case KERN_OSRELEASE:
    366        1.1       cgd 		return (sysctl_rdstring(oldp, oldlenp, newp, osrelease));
    367        1.1       cgd 	case KERN_OSREV:
    368       1.75   thorpej 		return (sysctl_rdint(oldp, oldlenp, newp, __NetBSD_Version__));
    369        1.1       cgd 	case KERN_VERSION:
    370        1.1       cgd 		return (sysctl_rdstring(oldp, oldlenp, newp, version));
    371        1.1       cgd 	case KERN_MAXVNODES:
    372       1.22       tls 		old_vnodes = desiredvnodes;
    373       1.29  sommerfe 		error = sysctl_int(oldp, oldlenp, newp, newlen, &desiredvnodes);
    374  1.86.2.12   nathanw 		if (newp && !error) {
    375  1.86.2.12   nathanw 			if (old_vnodes > desiredvnodes) {
    376  1.86.2.12   nathanw 				desiredvnodes = old_vnodes;
    377  1.86.2.12   nathanw 				return (EINVAL);
    378  1.86.2.12   nathanw 			}
    379   1.86.2.8   nathanw 			vfs_reinit();
    380   1.86.2.9   nathanw 			nchreinit();
    381       1.29  sommerfe 		}
    382       1.22       tls 		return (error);
    383        1.1       cgd 	case KERN_MAXPROC:
    384  1.86.2.27   thorpej 	    {
    385  1.86.2.27   thorpej 		int nmaxproc = maxproc;
    386  1.86.2.27   thorpej 
    387  1.86.2.27   thorpej 		error = (sysctl_int(oldp, oldlenp, newp, newlen, &nmaxproc));
    388  1.86.2.27   thorpej 
    389  1.86.2.27   thorpej 		if (!error && newp) {
    390  1.86.2.27   thorpej 			if (nmaxproc < 0 || nmaxproc >= PID_MAX - PID_SKIP)
    391  1.86.2.27   thorpej 				return (EINVAL);
    392  1.86.2.27   thorpej 
    393  1.86.2.27   thorpej #ifdef __HAVE_CPU_MAXPROC
    394  1.86.2.27   thorpej 			if (nmaxproc > cpu_maxproc())
    395  1.86.2.27   thorpej 				return (EINVAL);
    396  1.86.2.27   thorpej #endif
    397  1.86.2.27   thorpej 			maxproc = nmaxproc;
    398  1.86.2.27   thorpej 		}
    399  1.86.2.27   thorpej 
    400  1.86.2.27   thorpej 		return (error);
    401  1.86.2.27   thorpej 	    }
    402        1.1       cgd 	case KERN_MAXFILES:
    403        1.1       cgd 		return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles));
    404        1.1       cgd 	case KERN_ARGMAX:
    405        1.1       cgd 		return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX));
    406        1.1       cgd 	case KERN_SECURELVL:
    407        1.1       cgd 		level = securelevel;
    408        1.1       cgd 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) ||
    409        1.1       cgd 		    newp == NULL)
    410        1.1       cgd 			return (error);
    411        1.1       cgd 		if (level < securelevel && p->p_pid != 1)
    412        1.1       cgd 			return (EPERM);
    413        1.1       cgd 		securelevel = level;
    414        1.1       cgd 		return (0);
    415        1.1       cgd 	case KERN_HOSTNAME:
    416        1.1       cgd 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    417        1.1       cgd 		    hostname, sizeof(hostname));
    418        1.1       cgd 		if (newp && !error)
    419        1.1       cgd 			hostnamelen = newlen;
    420        1.1       cgd 		return (error);
    421        1.1       cgd 	case KERN_DOMAINNAME:
    422        1.1       cgd 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    423        1.1       cgd 		    domainname, sizeof(domainname));
    424        1.1       cgd 		if (newp && !error)
    425        1.1       cgd 			domainnamelen = newlen;
    426        1.1       cgd 		return (error);
    427        1.1       cgd 	case KERN_HOSTID:
    428        1.1       cgd 		inthostid = hostid;  /* XXX assumes sizeof long <= sizeof int */
    429        1.1       cgd 		error =  sysctl_int(oldp, oldlenp, newp, newlen, &inthostid);
    430  1.86.2.12   nathanw 		if (newp && !error)
    431  1.86.2.12   nathanw 			hostid = inthostid;
    432        1.1       cgd 		return (error);
    433        1.1       cgd 	case KERN_CLOCKRATE:
    434        1.1       cgd 		return (sysctl_clockrate(oldp, oldlenp));
    435        1.1       cgd 	case KERN_BOOTTIME:
    436        1.1       cgd 		return (sysctl_rdstruct(oldp, oldlenp, newp, &boottime,
    437        1.1       cgd 		    sizeof(struct timeval)));
    438        1.1       cgd 	case KERN_VNODE:
    439       1.34      fvdl 		return (sysctl_vnode(oldp, oldlenp, p));
    440        1.1       cgd 	case KERN_PROC:
    441       1.62    simonb 	case KERN_PROC2:
    442       1.62    simonb 		return (sysctl_doeproc(name, namelen, oldp, oldlenp));
    443  1.86.2.15   nathanw 	case KERN_LWP:
    444  1.86.2.15   nathanw 		return (sysctl_dolwp(name, namelen, oldp, oldlenp));
    445       1.62    simonb 	case KERN_PROC_ARGS:
    446       1.62    simonb 		return (sysctl_procargs(name + 1, namelen - 1,
    447       1.62    simonb 		    oldp, oldlenp, p));
    448        1.1       cgd 	case KERN_FILE:
    449        1.1       cgd 		return (sysctl_file(oldp, oldlenp));
    450        1.1       cgd #ifdef GPROF
    451        1.1       cgd 	case KERN_PROF:
    452        1.1       cgd 		return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
    453        1.1       cgd 		    newp, newlen));
    454        1.1       cgd #endif
    455        1.1       cgd 	case KERN_POSIX1:
    456        1.1       cgd 		return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION));
    457        1.1       cgd 	case KERN_NGROUPS:
    458        1.1       cgd 		return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX));
    459        1.1       cgd 	case KERN_JOB_CONTROL:
    460        1.1       cgd 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    461        1.1       cgd 	case KERN_SAVED_IDS:
    462        1.1       cgd #ifdef _POSIX_SAVED_IDS
    463        1.1       cgd 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    464        1.1       cgd #else
    465        1.1       cgd 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    466        1.1       cgd #endif
    467        1.7       cgd 	case KERN_MAXPARTITIONS:
    468        1.7       cgd 		return (sysctl_rdint(oldp, oldlenp, newp, MAXPARTITIONS));
    469       1.10   thorpej 	case KERN_RAWPARTITION:
    470       1.10   thorpej 		return (sysctl_rdint(oldp, oldlenp, newp, RAW_PART));
    471       1.19   thorpej #ifdef NTP
    472       1.15  jonathan 	case KERN_NTPTIME:
    473       1.15  jonathan 		return (sysctl_ntptime(oldp, oldlenp));
    474       1.19   thorpej #endif
    475       1.18  explorer 	case KERN_AUTONICETIME:
    476       1.18  explorer 	        old_autonicetime = autonicetime;
    477       1.18  explorer 	        error = sysctl_int(oldp, oldlenp, newp, newlen, &autonicetime);
    478       1.18  explorer 		if (autonicetime < 0)
    479       1.18  explorer  		        autonicetime = old_autonicetime;
    480       1.18  explorer 		return (error);
    481       1.18  explorer 	case KERN_AUTONICEVAL:
    482       1.18  explorer 		error = sysctl_int(oldp, oldlenp, newp, newlen, &autoniceval);
    483       1.18  explorer 		if (autoniceval < PRIO_MIN)
    484       1.18  explorer 			autoniceval = PRIO_MIN;
    485       1.18  explorer 		if (autoniceval > PRIO_MAX)
    486       1.18  explorer 			autoniceval = PRIO_MAX;
    487       1.18  explorer 		return (error);
    488       1.21     perry 	case KERN_RTC_OFFSET:
    489       1.21     perry 		return (sysctl_rdint(oldp, oldlenp, newp, rtc_offset));
    490       1.23   thorpej 	case KERN_ROOT_DEVICE:
    491       1.23   thorpej 		return (sysctl_rdstring(oldp, oldlenp, newp,
    492       1.23   thorpej 		    root_device->dv_xname));
    493       1.28       leo 	case KERN_MSGBUFSIZE:
    494       1.28       leo 		/*
    495       1.28       leo 		 * deal with cases where the message buffer has
    496       1.28       leo 		 * become corrupted.
    497       1.28       leo 		 */
    498       1.28       leo 		if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
    499       1.28       leo 			msgbufenabled = 0;
    500       1.28       leo 			return (ENXIO);
    501       1.28       leo 		}
    502       1.28       leo 		return (sysctl_rdint(oldp, oldlenp, newp, msgbufp->msg_bufs));
    503       1.36    kleink 	case KERN_FSYNC:
    504       1.36    kleink 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    505       1.36    kleink 	case KERN_SYSVMSG:
    506       1.36    kleink #ifdef SYSVMSG
    507       1.36    kleink 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    508       1.36    kleink #else
    509       1.36    kleink 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    510       1.36    kleink #endif
    511       1.36    kleink 	case KERN_SYSVSEM:
    512       1.36    kleink #ifdef SYSVSEM
    513       1.36    kleink 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    514       1.36    kleink #else
    515       1.36    kleink 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    516       1.36    kleink #endif
    517       1.36    kleink 	case KERN_SYSVSHM:
    518       1.36    kleink #ifdef SYSVSHM
    519       1.36    kleink 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    520       1.36    kleink #else
    521       1.36    kleink 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
    522       1.36    kleink #endif
    523       1.52    bouyer  	case KERN_DEFCORENAME:
    524       1.52    bouyer 		if (newp && newlen < 1)
    525       1.52    bouyer 			return (EINVAL);
    526       1.52    bouyer 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    527       1.52    bouyer 		    defcorename, sizeof(defcorename));
    528       1.52    bouyer 		if (newp && !error)
    529       1.52    bouyer 			defcorenamelen = newlen;
    530       1.52    bouyer 		return (error);
    531       1.40    kleink 	case KERN_SYNCHRONIZED_IO:
    532       1.40    kleink 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    533       1.40    kleink 	case KERN_IOV_MAX:
    534       1.40    kleink 		return (sysctl_rdint(oldp, oldlenp, newp, IOV_MAX));
    535       1.44   thorpej 	case KERN_MBUF:
    536       1.44   thorpej 		return (sysctl_dombuf(name + 1, namelen - 1, oldp, oldlenp,
    537       1.44   thorpej 		    newp, newlen));
    538       1.47    kleink 	case KERN_MAPPED_FILES:
    539       1.47    kleink 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    540       1.47    kleink 	case KERN_MEMLOCK:
    541       1.47    kleink 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    542       1.47    kleink 	case KERN_MEMLOCK_RANGE:
    543       1.47    kleink 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    544       1.47    kleink 	case KERN_MEMORY_PROTECTION:
    545       1.47    kleink 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
    546       1.51    kleink 	case KERN_LOGIN_NAME_MAX:
    547       1.51    kleink 		return (sysctl_rdint(oldp, oldlenp, newp, LOGIN_NAME_MAX));
    548       1.57      fair 	case KERN_LOGSIGEXIT:
    549       1.62    simonb 		return (sysctl_int(oldp, oldlenp, newp, newlen,
    550       1.62    simonb 		    &kern_logsigexit));
    551       1.62    simonb 	case KERN_FSCALE:
    552       1.62    simonb 		return (sysctl_rdint(oldp, oldlenp, newp, FSCALE));
    553       1.62    simonb 	case KERN_CCPU:
    554       1.62    simonb 		return (sysctl_rdint(oldp, oldlenp, newp, ccpu));
    555       1.62    simonb 	case KERN_CP_TIME:
    556       1.83  sommerfe #ifndef MULTIPROCESSOR
    557       1.70   thorpej 		return (sysctl_rdstruct(oldp, oldlenp, newp,
    558       1.70   thorpej 		    curcpu()->ci_schedstate.spc_cp_time,
    559       1.70   thorpej 		    sizeof(curcpu()->ci_schedstate.spc_cp_time)));
    560       1.83  sommerfe #else
    561       1.83  sommerfe 		return (sysctl_docptime(oldp, oldlenp, newp));
    562       1.83  sommerfe #endif
    563       1.69    simonb #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
    564       1.69    simonb 	case KERN_SYSVIPC_INFO:
    565       1.69    simonb 		return (sysctl_sysvipc(name + 1, namelen - 1, oldp, oldlenp));
    566       1.69    simonb #endif
    567       1.72    simonb 	case KERN_MSGBUF:
    568       1.72    simonb 		return (sysctl_msgbuf(oldp, oldlenp));
    569       1.74    simonb 	case KERN_CONSDEV:
    570       1.74    simonb 		if (cn_tab != NULL)
    571       1.74    simonb 			consdev = cn_tab->cn_dev;
    572       1.74    simonb 		else
    573       1.74    simonb 			consdev = NODEV;
    574       1.74    simonb 		return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
    575       1.74    simonb 		    sizeof consdev));
    576       1.77  jdolecek #if NPTY > 0
    577       1.77  jdolecek 	case KERN_MAXPTYS:
    578       1.78  jdolecek 		return sysctl_pty(oldp, oldlenp, newp, newlen);
    579       1.77  jdolecek #endif
    580  1.86.2.12   nathanw #ifndef PIPE_SOCKETPAIR
    581   1.86.2.5   nathanw 	case KERN_PIPE:
    582   1.86.2.5   nathanw 		return (sysctl_dopipe(name + 1, namelen - 1, oldp, oldlenp,
    583   1.86.2.5   nathanw 		    newp, newlen));
    584   1.86.2.5   nathanw #endif
    585   1.86.2.6   nathanw 	case KERN_MAXPHYS:
    586   1.86.2.6   nathanw 		return (sysctl_rdint(oldp, oldlenp, newp, MAXPHYS));
    587   1.86.2.6   nathanw 	case KERN_SBMAX:
    588   1.86.2.6   nathanw 	    {
    589   1.86.2.6   nathanw 		int new_sbmax = sb_max;
    590   1.86.2.6   nathanw 
    591   1.86.2.6   nathanw 		error = sysctl_int(oldp, oldlenp, newp, newlen, &new_sbmax);
    592  1.86.2.12   nathanw 		if (newp && !error) {
    593   1.86.2.6   nathanw 			if (new_sbmax < (16 * 1024)) /* sanity */
    594   1.86.2.6   nathanw 				return (EINVAL);
    595   1.86.2.6   nathanw 			sb_max = new_sbmax;
    596   1.86.2.6   nathanw 		}
    597   1.86.2.6   nathanw 		return (error);
    598   1.86.2.6   nathanw 	    }
    599  1.86.2.12   nathanw 	case KERN_TKSTAT:
    600  1.86.2.12   nathanw 		return (sysctl_dotkstat(name + 1, namelen - 1, oldp, oldlenp,
    601  1.86.2.12   nathanw 		    newp));
    602  1.86.2.12   nathanw 	case KERN_MONOTONIC_CLOCK:	/* XXX _POSIX_VERSION */
    603  1.86.2.12   nathanw 		return (sysctl_rdint(oldp, oldlenp, newp, 200112));
    604  1.86.2.21   nathanw 	case KERN_URND:
    605  1.86.2.21   nathanw #if NRND > 0
    606  1.86.2.21   nathanw 		if (rnd_extract_data(&v, sizeof(v), RND_EXTRACT_ANY) ==
    607  1.86.2.21   nathanw 		    sizeof(v))
    608  1.86.2.21   nathanw 			return (sysctl_rdint(oldp, oldlenp, newp, v));
    609  1.86.2.21   nathanw 		else
    610  1.86.2.21   nathanw 			return (EIO);	/*XXX*/
    611  1.86.2.21   nathanw #else
    612  1.86.2.21   nathanw 		return (EOPNOTSUPP);
    613  1.86.2.21   nathanw #endif
    614  1.86.2.26   thorpej 	case KERN_LABELSECTOR:
    615  1.86.2.26   thorpej 		return (sysctl_rdint(oldp, oldlenp, newp, LABELSECTOR));
    616  1.86.2.26   thorpej 	case KERN_LABELOFFSET:
    617  1.86.2.26   thorpej 		return (sysctl_rdint(oldp, oldlenp, newp, LABELOFFSET));
    618  1.86.2.27   thorpej 	case KERN_FORKFSLEEP:
    619  1.86.2.27   thorpej 	    {
    620  1.86.2.27   thorpej 		/* userland sees value in ms, internally is in ticks */
    621  1.86.2.27   thorpej 		int timo, lsleep = forkfsleep * 1000 / hz;
    622  1.86.2.27   thorpej 
    623  1.86.2.27   thorpej 		error = (sysctl_int(oldp, oldlenp, newp, newlen, &lsleep));
    624  1.86.2.27   thorpej 		if (newp && !error) {
    625  1.86.2.27   thorpej 			/* refuse negative values, and overly 'long time' */
    626  1.86.2.27   thorpej 			if (lsleep < 0 || lsleep > MAXSLP * 1000)
    627  1.86.2.27   thorpej 				return (EINVAL);
    628  1.86.2.27   thorpej 
    629  1.86.2.27   thorpej 			timo = mstohz(lsleep);
    630  1.86.2.27   thorpej 
    631  1.86.2.27   thorpej 			/* if the interval is >0 ms && <1 tick, use 1 tick */
    632  1.86.2.27   thorpej 			if (lsleep != 0 && timo == 0)
    633  1.86.2.27   thorpej 				forkfsleep = 1;
    634  1.86.2.27   thorpej 			else
    635  1.86.2.27   thorpej 				forkfsleep = timo;
    636  1.86.2.27   thorpej 		}
    637  1.86.2.27   thorpej 		return (error);
    638  1.86.2.27   thorpej 	    }
    639  1.86.2.27   thorpej 
    640        1.1       cgd 	default:
    641        1.1       cgd 		return (EOPNOTSUPP);
    642        1.1       cgd 	}
    643        1.1       cgd 	/* NOTREACHED */
    644        1.1       cgd }
    645        1.1       cgd 
    646        1.1       cgd /*
    647        1.1       cgd  * hardware related system variables.
    648        1.1       cgd  */
    649       1.13  christos int
    650   1.86.2.6   nathanw hw_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    651   1.86.2.6   nathanw     void *newp, size_t newlen, struct proc *p)
    652        1.1       cgd {
    653        1.1       cgd 
    654  1.86.2.12   nathanw 	/* All sysctl names at this level, except for a few, are terminal. */
    655  1.86.2.12   nathanw 	switch (name[0]) {
    656  1.86.2.12   nathanw 	case HW_DISKSTATS:
    657  1.86.2.12   nathanw 		/* Not terminal. */
    658  1.86.2.12   nathanw 		break;
    659  1.86.2.12   nathanw 	default:
    660  1.86.2.12   nathanw 		if (namelen != 1)
    661  1.86.2.12   nathanw 			return (ENOTDIR);	/* overloaded */
    662  1.86.2.12   nathanw 	}
    663        1.1       cgd 
    664        1.1       cgd 	switch (name[0]) {
    665        1.1       cgd 	case HW_MACHINE:
    666        1.1       cgd 		return (sysctl_rdstring(oldp, oldlenp, newp, machine));
    667       1.27     veego 	case HW_MACHINE_ARCH:
    668       1.27     veego 		return (sysctl_rdstring(oldp, oldlenp, newp, machine_arch));
    669        1.1       cgd 	case HW_MODEL:
    670        1.1       cgd 		return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
    671        1.1       cgd 	case HW_NCPU:
    672       1.84  sommerfe 		return (sysctl_rdint(oldp, oldlenp, newp, sysctl_ncpus()));
    673        1.1       cgd 	case HW_BYTEORDER:
    674        1.1       cgd 		return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
    675        1.1       cgd 	case HW_PHYSMEM:
    676        1.1       cgd 		return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
    677        1.1       cgd 	case HW_USERMEM:
    678       1.31       mrg 		return (sysctl_rdint(oldp, oldlenp, newp,
    679       1.31       mrg 		    ctob(physmem - uvmexp.wired)));
    680        1.1       cgd 	case HW_PAGESIZE:
    681        1.1       cgd 		return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
    682       1.58    itojun 	case HW_ALIGNBYTES:
    683       1.58    itojun 		return (sysctl_rdint(oldp, oldlenp, newp, ALIGNBYTES));
    684  1.86.2.12   nathanw 	case HW_DISKNAMES:
    685  1.86.2.12   nathanw 		return (sysctl_disknames(oldp, oldlenp));
    686  1.86.2.12   nathanw 	case HW_DISKSTATS:
    687  1.86.2.12   nathanw 		return (sysctl_diskstats(name + 1, namelen - 1, oldp, oldlenp));
    688       1.82       eeh 	case HW_CNMAGIC: {
    689       1.82       eeh 		char magic[CNS_LEN];
    690       1.82       eeh 		int error;
    691       1.82       eeh 
    692       1.82       eeh 		if (oldp)
    693       1.82       eeh 			cn_get_magic(magic, CNS_LEN);
    694       1.82       eeh 		error = sysctl_string(oldp, oldlenp, newp, newlen,
    695       1.82       eeh 		    magic, sizeof(magic));
    696       1.82       eeh 		if (newp && !error) {
    697       1.82       eeh 			error = cn_set_magic(magic);
    698       1.82       eeh 		}
    699       1.82       eeh 		return (error);
    700       1.82       eeh 	}
    701        1.1       cgd 	default:
    702        1.1       cgd 		return (EOPNOTSUPP);
    703        1.1       cgd 	}
    704        1.1       cgd 	/* NOTREACHED */
    705        1.1       cgd }
    706        1.1       cgd 
    707        1.1       cgd #ifdef DEBUG
    708        1.1       cgd /*
    709        1.1       cgd  * Debugging related system variables.
    710        1.1       cgd  */
    711  1.86.2.19   nathanw struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
    712        1.1       cgd struct ctldebug debug5, debug6, debug7, debug8, debug9;
    713        1.1       cgd struct ctldebug debug10, debug11, debug12, debug13, debug14;
    714        1.1       cgd struct ctldebug debug15, debug16, debug17, debug18, debug19;
    715        1.1       cgd static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
    716        1.1       cgd 	&debug0, &debug1, &debug2, &debug3, &debug4,
    717        1.1       cgd 	&debug5, &debug6, &debug7, &debug8, &debug9,
    718        1.1       cgd 	&debug10, &debug11, &debug12, &debug13, &debug14,
    719        1.1       cgd 	&debug15, &debug16, &debug17, &debug18, &debug19,
    720        1.1       cgd };
    721   1.86.2.6   nathanw 
    722        1.1       cgd int
    723   1.86.2.6   nathanw debug_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    724   1.86.2.6   nathanw     void *newp, size_t newlen, struct proc *p)
    725        1.1       cgd {
    726        1.1       cgd 	struct ctldebug *cdp;
    727        1.1       cgd 
    728        1.1       cgd 	/* all sysctl names at this level are name and field */
    729        1.1       cgd 	if (namelen != 2)
    730        1.1       cgd 		return (ENOTDIR);		/* overloaded */
    731  1.86.2.13   nathanw 	if (name[0] >= CTL_DEBUG_MAXID)
    732  1.86.2.13   nathanw 		return (EOPNOTSUPP);
    733        1.1       cgd 	cdp = debugvars[name[0]];
    734  1.86.2.13   nathanw 	if (cdp->debugname == 0)
    735        1.1       cgd 		return (EOPNOTSUPP);
    736        1.1       cgd 	switch (name[1]) {
    737        1.1       cgd 	case CTL_DEBUG_NAME:
    738        1.1       cgd 		return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
    739        1.1       cgd 	case CTL_DEBUG_VALUE:
    740        1.1       cgd 		return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
    741        1.1       cgd 	default:
    742        1.1       cgd 		return (EOPNOTSUPP);
    743        1.1       cgd 	}
    744        1.1       cgd 	/* NOTREACHED */
    745        1.1       cgd }
    746        1.1       cgd #endif /* DEBUG */
    747        1.1       cgd 
    748       1.52    bouyer int
    749   1.86.2.6   nathanw proc_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    750   1.86.2.6   nathanw     void *newp, size_t newlen, struct proc *p)
    751       1.52    bouyer {
    752       1.62    simonb 	struct proc *ptmp = NULL;
    753       1.52    bouyer 	const struct proclist_desc *pd;
    754       1.52    bouyer 	int error = 0;
    755       1.52    bouyer 	struct rlimit alim;
    756       1.52    bouyer 	struct plimit *newplim;
    757       1.52    bouyer 	char *tmps = NULL;
    758  1.86.2.22   nathanw 	size_t len, curlen;
    759  1.86.2.22   nathanw 	u_int i;
    760       1.52    bouyer 
    761       1.52    bouyer 	if (namelen < 2)
    762       1.52    bouyer 		return EINVAL;
    763       1.52    bouyer 
    764       1.52    bouyer 	if (name[0] == PROC_CURPROC) {
    765       1.52    bouyer 		ptmp = p;
    766       1.52    bouyer 	} else {
    767       1.52    bouyer 		proclist_lock_read();
    768       1.52    bouyer 		for (pd = proclists; pd->pd_list != NULL; pd++) {
    769       1.52    bouyer 			for (ptmp = LIST_FIRST(pd->pd_list); ptmp != NULL;
    770       1.52    bouyer 			    ptmp = LIST_NEXT(ptmp, p_list)) {
    771       1.52    bouyer 				/* Skip embryonic processes. */
    772       1.52    bouyer 				if (ptmp->p_stat == SIDL)
    773       1.52    bouyer 					continue;
    774       1.52    bouyer 				if (ptmp->p_pid == (pid_t)name[0])
    775       1.52    bouyer 					break;
    776       1.52    bouyer 			}
    777       1.52    bouyer 			if (ptmp != NULL)
    778       1.52    bouyer 				break;
    779       1.52    bouyer 		}
    780       1.52    bouyer 		proclist_unlock_read();
    781       1.52    bouyer 		if (ptmp == NULL)
    782       1.52    bouyer 			return(ESRCH);
    783       1.52    bouyer 		if (p->p_ucred->cr_uid != 0) {
    784       1.52    bouyer 			if(p->p_cred->p_ruid != ptmp->p_cred->p_ruid ||
    785       1.52    bouyer 			    p->p_cred->p_ruid != ptmp->p_cred->p_svuid)
    786       1.52    bouyer 				return EPERM;
    787       1.52    bouyer 			if (ptmp->p_cred->p_rgid != ptmp->p_cred->p_svgid)
    788       1.52    bouyer 				return EPERM; /* sgid proc */
    789       1.52    bouyer 			for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
    790       1.52    bouyer 				if (p->p_ucred->cr_groups[i] ==
    791       1.52    bouyer 				    ptmp->p_cred->p_rgid)
    792       1.52    bouyer 					break;
    793       1.52    bouyer 			}
    794       1.52    bouyer 			if (i == p->p_ucred->cr_ngroups)
    795       1.52    bouyer 				return EPERM;
    796       1.52    bouyer 		}
    797       1.52    bouyer 	}
    798  1.86.2.24   nathanw 	switch(name[1]) {
    799  1.86.2.24   nathanw 	case PROC_PID_STOPFORK:
    800  1.86.2.24   nathanw 		if (namelen != 2)
    801  1.86.2.24   nathanw 			return EINVAL;
    802  1.86.2.24   nathanw 		i = ((ptmp->p_flag & P_STOPFORK) != 0);
    803  1.86.2.24   nathanw 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &i)) != 0)
    804  1.86.2.24   nathanw 			return error;
    805  1.86.2.24   nathanw 		if (i != 0)
    806  1.86.2.24   nathanw 			ptmp->p_flag |= P_STOPFORK;
    807  1.86.2.24   nathanw 		else
    808  1.86.2.24   nathanw 			ptmp->p_flag &= ~P_STOPFORK;
    809  1.86.2.24   nathanw 		return 0;
    810  1.86.2.24   nathanw 		break;
    811  1.86.2.24   nathanw 
    812  1.86.2.24   nathanw 	case PROC_PID_STOPEXEC:
    813  1.86.2.24   nathanw 		if (namelen != 2)
    814  1.86.2.24   nathanw 			return EINVAL;
    815  1.86.2.24   nathanw 		i = ((ptmp->p_flag & P_STOPEXEC) != 0);
    816  1.86.2.24   nathanw 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &i)) != 0)
    817  1.86.2.24   nathanw 			return error;
    818  1.86.2.24   nathanw 		if (i != 0)
    819  1.86.2.24   nathanw 			ptmp->p_flag |= P_STOPEXEC;
    820  1.86.2.24   nathanw 		else
    821  1.86.2.24   nathanw 			ptmp->p_flag &= ~P_STOPEXEC;
    822  1.86.2.24   nathanw 		return 0;
    823  1.86.2.24   nathanw 		break;
    824  1.86.2.24   nathanw 
    825  1.86.2.24   nathanw 	case PROC_PID_CORENAME:
    826       1.52    bouyer 		if (namelen != 2)
    827       1.52    bouyer 			return EINVAL;
    828       1.52    bouyer 		/*
    829       1.52    bouyer 		 * Can't use sysctl_string() here because we may malloc a new
    830       1.52    bouyer 		 * area during the process, so we have to do it by hand.
    831       1.52    bouyer 		 */
    832       1.52    bouyer 		curlen = strlen(ptmp->p_limit->pl_corename) + 1;
    833       1.55        is 		if (oldlenp  && *oldlenp < curlen) {
    834       1.55        is 			if (!oldp)
    835       1.55        is 				*oldlenp = curlen;
    836       1.52    bouyer 			return (ENOMEM);
    837       1.55        is 		}
    838       1.52    bouyer 		if (newp) {
    839       1.52    bouyer 			if (securelevel > 2)
    840       1.52    bouyer 				return EPERM;
    841       1.52    bouyer 			if (newlen > MAXPATHLEN)
    842       1.52    bouyer 				return ENAMETOOLONG;
    843       1.52    bouyer 			tmps = malloc(newlen + 1, M_TEMP, M_WAITOK);
    844       1.52    bouyer 			if (tmps == NULL)
    845       1.52    bouyer 				return ENOMEM;
    846       1.52    bouyer 			error = copyin(newp, tmps, newlen + 1);
    847       1.52    bouyer 			tmps[newlen] = '\0';
    848       1.52    bouyer 			if (error)
    849       1.52    bouyer 				goto cleanup;
    850       1.52    bouyer 			/* Enforce to be either 'core' for end with '.core' */
    851       1.52    bouyer 			if (newlen < 4)  { /* c.o.r.e */
    852       1.52    bouyer 				error = EINVAL;
    853       1.52    bouyer 				goto cleanup;
    854       1.52    bouyer 			}
    855       1.52    bouyer 			len = newlen - 4;
    856       1.52    bouyer 			if (len > 0) {
    857       1.52    bouyer 				if (tmps[len - 1] != '.' &&
    858       1.52    bouyer 				    tmps[len - 1] != '/') {
    859       1.52    bouyer 					error = EINVAL;
    860       1.52    bouyer 					goto cleanup;
    861       1.52    bouyer 				}
    862       1.52    bouyer 			}
    863       1.52    bouyer 			if (strcmp(&tmps[len], "core") != 0) {
    864       1.52    bouyer 				error = EINVAL;
    865       1.52    bouyer 				goto cleanup;
    866       1.52    bouyer 			}
    867       1.52    bouyer 		}
    868       1.55        is 		if (oldp && oldlenp) {
    869       1.52    bouyer 			*oldlenp = curlen;
    870       1.52    bouyer 			error = copyout(ptmp->p_limit->pl_corename, oldp,
    871       1.52    bouyer 			    curlen);
    872       1.52    bouyer 		}
    873       1.52    bouyer 		if (newp && error == 0) {
    874       1.52    bouyer 			/* if the 2 strings are identical, don't limcopy() */
    875       1.52    bouyer 			if (strcmp(tmps, ptmp->p_limit->pl_corename) == 0) {
    876       1.52    bouyer 				error = 0;
    877       1.52    bouyer 				goto cleanup;
    878       1.52    bouyer 			}
    879       1.52    bouyer 			if (ptmp->p_limit->p_refcnt > 1 &&
    880       1.52    bouyer 			    (ptmp->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    881       1.52    bouyer 				newplim = limcopy(ptmp->p_limit);
    882       1.52    bouyer 				limfree(ptmp->p_limit);
    883       1.52    bouyer 				ptmp->p_limit = newplim;
    884  1.86.2.10   nathanw 			}
    885  1.86.2.10   nathanw 			if (ptmp->p_limit->pl_corename != defcorename) {
    886       1.52    bouyer 				free(ptmp->p_limit->pl_corename, M_TEMP);
    887       1.52    bouyer 			}
    888       1.52    bouyer 			ptmp->p_limit->pl_corename = tmps;
    889       1.52    bouyer 			return (0);
    890       1.52    bouyer 		}
    891       1.52    bouyer cleanup:
    892       1.52    bouyer 		if (tmps)
    893       1.52    bouyer 			free(tmps, M_TEMP);
    894       1.52    bouyer 		return (error);
    895  1.86.2.24   nathanw 		break;
    896  1.86.2.24   nathanw 
    897  1.86.2.24   nathanw 	case PROC_PID_LIMIT:
    898       1.52    bouyer 		if (namelen != 4 || name[2] >= PROC_PID_LIMIT_MAXID)
    899       1.52    bouyer 			return EINVAL;
    900       1.52    bouyer 		memcpy(&alim, &ptmp->p_rlimit[name[2] - 1], sizeof(alim));
    901       1.52    bouyer 		if (name[3] == PROC_PID_LIMIT_TYPE_HARD)
    902       1.52    bouyer 			error = sysctl_quad(oldp, oldlenp, newp, newlen,
    903       1.52    bouyer 			    &alim.rlim_max);
    904       1.52    bouyer 		else if (name[3] == PROC_PID_LIMIT_TYPE_SOFT)
    905       1.52    bouyer 			error = sysctl_quad(oldp, oldlenp, newp, newlen,
    906       1.52    bouyer 			    &alim.rlim_cur);
    907       1.52    bouyer 		else
    908       1.52    bouyer 			error = EINVAL;
    909       1.52    bouyer 
    910       1.52    bouyer 		if (error)
    911       1.52    bouyer 			return error;
    912       1.52    bouyer 
    913       1.52    bouyer 		if (newp)
    914       1.52    bouyer 			error = dosetrlimit(ptmp, p->p_cred,
    915       1.52    bouyer 			    name[2] - 1, &alim);
    916       1.52    bouyer 		return error;
    917  1.86.2.24   nathanw 		break;
    918  1.86.2.24   nathanw 
    919  1.86.2.24   nathanw 	default:
    920  1.86.2.24   nathanw 		return (EINVAL);
    921  1.86.2.24   nathanw 		break;
    922       1.52    bouyer 	}
    923  1.86.2.24   nathanw 	/* NOTREACHED */
    924       1.52    bouyer 	return (EINVAL);
    925       1.52    bouyer }
    926       1.52    bouyer 
    927  1.86.2.13   nathanw int
    928  1.86.2.13   nathanw emul_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    929  1.86.2.13   nathanw     void *newp, size_t newlen, struct proc *p)
    930  1.86.2.13   nathanw {
    931  1.86.2.13   nathanw 	static struct {
    932  1.86.2.13   nathanw 		const char *name;
    933  1.86.2.13   nathanw 		int  type;
    934  1.86.2.13   nathanw 	} emulations[] = CTL_EMUL_NAMES;
    935  1.86.2.14   nathanw 	const struct emul *e;
    936  1.86.2.13   nathanw 	const char *ename;
    937  1.86.2.14   nathanw #ifdef LKM
    938  1.86.2.14   nathanw 	extern struct lock exec_lock;	/* XXX */
    939  1.86.2.14   nathanw 	int error;
    940  1.86.2.14   nathanw #else
    941  1.86.2.14   nathanw 	extern int nexecs_builtin;
    942  1.86.2.14   nathanw 	extern const struct execsw execsw_builtin[];
    943  1.86.2.13   nathanw 	int i;
    944  1.86.2.14   nathanw #endif
    945  1.86.2.13   nathanw 
    946  1.86.2.13   nathanw 	/* all sysctl names at this level are name and field */
    947  1.86.2.13   nathanw 	if (namelen < 2)
    948  1.86.2.13   nathanw 		return (ENOTDIR);		/* overloaded */
    949  1.86.2.13   nathanw 
    950  1.86.2.14   nathanw 	if ((u_int) name[0] >= EMUL_MAXID || name[0] == 0)
    951  1.86.2.13   nathanw 		return (EOPNOTSUPP);
    952  1.86.2.13   nathanw 
    953  1.86.2.13   nathanw 	ename = emulations[name[0]].name;
    954  1.86.2.13   nathanw 
    955  1.86.2.14   nathanw #ifdef LKM
    956  1.86.2.14   nathanw 	lockmgr(&exec_lock, LK_SHARED, NULL);
    957  1.86.2.14   nathanw 	if ((e = emul_search(ename))) {
    958  1.86.2.14   nathanw 		error = (*e->e_sysctl)(name + 1, namelen - 1, oldp, oldlenp,
    959  1.86.2.14   nathanw 				newp, newlen, p);
    960  1.86.2.14   nathanw 	} else
    961  1.86.2.14   nathanw 		error = EOPNOTSUPP;
    962  1.86.2.14   nathanw 	lockmgr(&exec_lock, LK_RELEASE, NULL);
    963  1.86.2.14   nathanw 
    964  1.86.2.14   nathanw 	return (error);
    965  1.86.2.14   nathanw #else
    966  1.86.2.13   nathanw 	for (i = 0; i < nexecs_builtin; i++) {
    967  1.86.2.14   nathanw 	    e = execsw_builtin[i].es_emul;
    968  1.86.2.24   nathanw 	    /*
    969  1.86.2.24   nathanw 	     * In order to match e.g. e->e_name "irix o32" with ename "irix",
    970  1.86.2.24   nathanw 	     * we limit the comparison to the length of ename.
    971  1.86.2.24   nathanw 	     */
    972  1.86.2.24   nathanw 	    if (e == NULL || strncmp(ename, e->e_name, strlen(ename)) != 0 ||
    973  1.86.2.24   nathanw 		e->e_sysctl == NULL)
    974  1.86.2.14   nathanw 		continue;
    975  1.86.2.14   nathanw 
    976  1.86.2.14   nathanw 	    return (*e->e_sysctl)(name + 1, namelen - 1, oldp, oldlenp,
    977  1.86.2.14   nathanw 					newp, newlen, p);
    978  1.86.2.13   nathanw 	}
    979  1.86.2.13   nathanw 
    980  1.86.2.13   nathanw 	return (EOPNOTSUPP);
    981  1.86.2.14   nathanw #endif
    982  1.86.2.13   nathanw }
    983        1.1       cgd /*
    984       1.55        is  * Convenience macros.
    985       1.55        is  */
    986       1.55        is 
    987       1.55        is #define SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, len) 		\
    988       1.55        is 	if (oldlenp) {							\
    989       1.55        is 		if (!oldp)						\
    990       1.55        is 			*oldlenp = len;					\
    991       1.55        is 		else {							\
    992       1.55        is 			if (*oldlenp < len)				\
    993       1.55        is 				return(ENOMEM);				\
    994       1.55        is 			*oldlenp = len;					\
    995       1.55        is 			error = copyout((caddr_t)valp, oldp, len);	\
    996       1.55        is 		}							\
    997       1.55        is 	}
    998       1.55        is 
    999       1.55        is #define SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, typ) \
   1000       1.55        is 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, sizeof(typ))
   1001       1.55        is 
   1002       1.55        is #define SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len)	\
   1003       1.55        is 	if (newp && newlen != len)			\
   1004       1.55        is 		return (EINVAL);
   1005       1.55        is 
   1006       1.55        is #define SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, typ)	\
   1007       1.55        is 	SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, sizeof(typ))
   1008       1.55        is 
   1009       1.55        is #define SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, len)	\
   1010       1.55        is 	if (error == 0 && newp)				\
   1011       1.55        is 		error = copyin(newp, valp, len);
   1012       1.55        is 
   1013       1.55        is #define SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, typ)      \
   1014       1.55        is 	SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, sizeof(typ))
   1015       1.55        is 
   1016       1.55        is #define SYSCTL_STRING_CORE(oldp, oldlenp, str)		\
   1017       1.55        is 	if (oldlenp) {					\
   1018       1.55        is 		len = strlen(str) + 1;			\
   1019       1.55        is 		if (!oldp)				\
   1020       1.55        is 			*oldlenp = len;			\
   1021       1.55        is 		else {					\
   1022       1.55        is 			if (*oldlenp < len) {		\
   1023       1.55        is 				err2 = ENOMEM;		\
   1024       1.55        is 				len = *oldlenp;		\
   1025       1.55        is 			} else				\
   1026       1.55        is 				*oldlenp = len;		\
   1027       1.55        is 			error = copyout(str, oldp, len);\
   1028       1.55        is 			if (error == 0)			\
   1029       1.55        is 				error = err2;		\
   1030       1.55        is 		}					\
   1031       1.55        is 	}
   1032       1.55        is 
   1033       1.55        is /*
   1034        1.1       cgd  * Validate parameters and get old / set new parameters
   1035        1.1       cgd  * for an integer-valued sysctl function.
   1036        1.1       cgd  */
   1037       1.13  christos int
   1038   1.86.2.6   nathanw sysctl_int(void *oldp, size_t *oldlenp, void *newp, size_t newlen, int *valp)
   1039        1.1       cgd {
   1040        1.1       cgd 	int error = 0;
   1041        1.1       cgd 
   1042       1.55        is 	SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
   1043       1.55        is 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, int)
   1044       1.55        is 	SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, int)
   1045       1.55        is 
   1046        1.1       cgd 	return (error);
   1047        1.1       cgd }
   1048        1.1       cgd 
   1049       1.55        is 
   1050        1.1       cgd /*
   1051        1.1       cgd  * As above, but read-only.
   1052        1.1       cgd  */
   1053       1.13  christos int
   1054   1.86.2.6   nathanw sysctl_rdint(void *oldp, size_t *oldlenp, void *newp, int val)
   1055        1.1       cgd {
   1056        1.1       cgd 	int error = 0;
   1057        1.1       cgd 
   1058        1.1       cgd 	if (newp)
   1059        1.1       cgd 		return (EPERM);
   1060       1.55        is 
   1061       1.55        is 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, int)
   1062       1.55        is 
   1063        1.1       cgd 	return (error);
   1064        1.1       cgd }
   1065        1.1       cgd 
   1066        1.1       cgd /*
   1067        1.1       cgd  * Validate parameters and get old / set new parameters
   1068       1.52    bouyer  * for an quad-valued sysctl function.
   1069       1.52    bouyer  */
   1070       1.52    bouyer int
   1071   1.86.2.6   nathanw sysctl_quad(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
   1072   1.86.2.6   nathanw     quad_t *valp)
   1073       1.52    bouyer {
   1074       1.52    bouyer 	int error = 0;
   1075       1.52    bouyer 
   1076       1.55        is 	SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, quad_t)
   1077       1.55        is 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, quad_t)
   1078       1.55        is 	SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, quad_t)
   1079       1.55        is 
   1080       1.52    bouyer 	return (error);
   1081       1.52    bouyer }
   1082       1.52    bouyer 
   1083       1.52    bouyer /*
   1084       1.52    bouyer  * As above, but read-only.
   1085       1.52    bouyer  */
   1086       1.52    bouyer int
   1087   1.86.2.6   nathanw sysctl_rdquad(void *oldp, size_t *oldlenp, void *newp, quad_t val)
   1088       1.52    bouyer {
   1089       1.52    bouyer 	int error = 0;
   1090       1.52    bouyer 
   1091       1.52    bouyer 	if (newp)
   1092       1.52    bouyer 		return (EPERM);
   1093       1.55        is 
   1094       1.55        is 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, quad_t)
   1095       1.55        is 
   1096       1.52    bouyer 	return (error);
   1097       1.52    bouyer }
   1098       1.52    bouyer 
   1099       1.52    bouyer /*
   1100       1.52    bouyer  * Validate parameters and get old / set new parameters
   1101        1.1       cgd  * for a string-valued sysctl function.
   1102        1.1       cgd  */
   1103       1.13  christos int
   1104   1.86.2.6   nathanw sysctl_string(void *oldp, size_t *oldlenp, void *newp, size_t newlen, char *str,
   1105  1.86.2.22   nathanw     size_t maxlen)
   1106        1.1       cgd {
   1107  1.86.2.22   nathanw 	int error = 0, err2 = 0;
   1108  1.86.2.22   nathanw 	size_t len;
   1109        1.1       cgd 
   1110        1.1       cgd 	if (newp && newlen >= maxlen)
   1111        1.1       cgd 		return (EINVAL);
   1112       1.55        is 
   1113       1.55        is 	SYSCTL_STRING_CORE(oldp, oldlenp, str);
   1114       1.55        is 
   1115        1.1       cgd 	if (error == 0 && newp) {
   1116        1.1       cgd 		error = copyin(newp, str, newlen);
   1117        1.1       cgd 		str[newlen] = 0;
   1118        1.1       cgd 	}
   1119        1.1       cgd 	return (error);
   1120        1.1       cgd }
   1121        1.1       cgd 
   1122        1.1       cgd /*
   1123        1.1       cgd  * As above, but read-only.
   1124        1.1       cgd  */
   1125       1.13  christos int
   1126   1.86.2.6   nathanw sysctl_rdstring(void *oldp, size_t *oldlenp, void *newp, const char *str)
   1127        1.1       cgd {
   1128  1.86.2.22   nathanw 	int error = 0, err2 = 0;
   1129  1.86.2.22   nathanw 	size_t len;
   1130        1.1       cgd 
   1131        1.1       cgd 	if (newp)
   1132        1.1       cgd 		return (EPERM);
   1133       1.55        is 
   1134       1.55        is 	SYSCTL_STRING_CORE(oldp, oldlenp, str);
   1135       1.55        is 
   1136        1.1       cgd 	return (error);
   1137        1.1       cgd }
   1138        1.1       cgd 
   1139        1.1       cgd /*
   1140        1.1       cgd  * Validate parameters and get old / set new parameters
   1141        1.1       cgd  * for a structure oriented sysctl function.
   1142        1.1       cgd  */
   1143       1.13  christos int
   1144   1.86.2.6   nathanw sysctl_struct(void *oldp, size_t *oldlenp, void *newp, size_t newlen, void *sp,
   1145  1.86.2.22   nathanw     size_t len)
   1146        1.1       cgd {
   1147        1.1       cgd 	int error = 0;
   1148        1.1       cgd 
   1149       1.55        is 	SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len)
   1150       1.55        is 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
   1151       1.55        is 	SYSCTL_SCALAR_NEWPCOP_LEN(newp, sp, len)
   1152       1.55        is 
   1153        1.1       cgd 	return (error);
   1154        1.1       cgd }
   1155        1.1       cgd 
   1156        1.1       cgd /*
   1157        1.1       cgd  * Validate parameters and get old parameters
   1158        1.1       cgd  * for a structure oriented sysctl function.
   1159        1.1       cgd  */
   1160       1.13  christos int
   1161   1.86.2.6   nathanw sysctl_rdstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
   1162  1.86.2.22   nathanw     size_t len)
   1163        1.1       cgd {
   1164        1.1       cgd 	int error = 0;
   1165        1.1       cgd 
   1166        1.1       cgd 	if (newp)
   1167        1.1       cgd 		return (EPERM);
   1168       1.55        is 
   1169       1.85    simonb 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
   1170       1.85    simonb 
   1171       1.85    simonb 	return (error);
   1172       1.85    simonb }
   1173       1.85    simonb 
   1174       1.85    simonb /*
   1175       1.85    simonb  * As above, but can return a truncated result.
   1176       1.85    simonb  */
   1177       1.85    simonb int
   1178   1.86.2.6   nathanw sysctl_rdminstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
   1179  1.86.2.22   nathanw     size_t len)
   1180       1.85    simonb {
   1181       1.85    simonb 	int error = 0;
   1182       1.85    simonb 
   1183       1.85    simonb 	if (newp)
   1184       1.85    simonb 		return (EPERM);
   1185       1.85    simonb 
   1186       1.85    simonb 	len = min(*oldlenp, len);
   1187       1.55        is 	SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
   1188       1.55        is 
   1189        1.1       cgd 	return (error);
   1190        1.1       cgd }
   1191        1.1       cgd 
   1192        1.1       cgd /*
   1193        1.1       cgd  * Get file structures.
   1194        1.1       cgd  */
   1195       1.69    simonb static int
   1196   1.86.2.6   nathanw sysctl_file(void *vwhere, size_t *sizep)
   1197        1.1       cgd {
   1198  1.86.2.22   nathanw 	int error;
   1199  1.86.2.22   nathanw 	size_t buflen;
   1200        1.1       cgd 	struct file *fp;
   1201       1.69    simonb 	char *start, *where;
   1202        1.1       cgd 
   1203       1.69    simonb 	start = where = vwhere;
   1204        1.1       cgd 	buflen = *sizep;
   1205        1.1       cgd 	if (where == NULL) {
   1206        1.1       cgd 		/*
   1207        1.1       cgd 		 * overestimate by 10 files
   1208        1.1       cgd 		 */
   1209        1.1       cgd 		*sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
   1210        1.1       cgd 		return (0);
   1211        1.1       cgd 	}
   1212        1.1       cgd 
   1213        1.1       cgd 	/*
   1214        1.1       cgd 	 * first copyout filehead
   1215        1.1       cgd 	 */
   1216        1.1       cgd 	if (buflen < sizeof(filehead)) {
   1217        1.1       cgd 		*sizep = 0;
   1218        1.1       cgd 		return (0);
   1219        1.1       cgd 	}
   1220       1.13  christos 	error = copyout((caddr_t)&filehead, where, sizeof(filehead));
   1221       1.13  christos 	if (error)
   1222        1.1       cgd 		return (error);
   1223        1.1       cgd 	buflen -= sizeof(filehead);
   1224        1.1       cgd 	where += sizeof(filehead);
   1225        1.1       cgd 
   1226        1.1       cgd 	/*
   1227        1.1       cgd 	 * followed by an array of file structures
   1228        1.1       cgd 	 */
   1229  1.86.2.23   nathanw 	LIST_FOREACH(fp, &filehead, f_list) {
   1230        1.1       cgd 		if (buflen < sizeof(struct file)) {
   1231        1.1       cgd 			*sizep = where - start;
   1232        1.1       cgd 			return (ENOMEM);
   1233        1.1       cgd 		}
   1234       1.39     perry 		error = copyout((caddr_t)fp, where, sizeof(struct file));
   1235       1.13  christos 		if (error)
   1236        1.1       cgd 			return (error);
   1237        1.1       cgd 		buflen -= sizeof(struct file);
   1238        1.1       cgd 		where += sizeof(struct file);
   1239        1.1       cgd 	}
   1240        1.1       cgd 	*sizep = where - start;
   1241        1.1       cgd 	return (0);
   1242        1.1       cgd }
   1243        1.1       cgd 
   1244       1.69    simonb #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
   1245       1.69    simonb #define	FILL_PERM(src, dst) do { \
   1246       1.69    simonb 		(dst)._key = (src)._key; \
   1247       1.69    simonb 		(dst).uid = (src).uid; \
   1248       1.69    simonb 		(dst).gid = (src).gid; \
   1249       1.69    simonb 		(dst).cuid = (src).cuid; \
   1250       1.69    simonb 		(dst).cgid = (src).cgid; \
   1251       1.69    simonb 		(dst).mode = (src).mode; \
   1252       1.69    simonb 		(dst)._seq = (src)._seq; \
   1253  1.86.2.24   nathanw 	} while (/*CONSTCOND*/ 0);
   1254       1.69    simonb #define	FILL_MSG(src, dst) do { \
   1255       1.69    simonb 	FILL_PERM((src).msg_perm, (dst).msg_perm); \
   1256       1.69    simonb 	(dst).msg_qnum = (src).msg_qnum; \
   1257       1.69    simonb 	(dst).msg_qbytes = (src).msg_qbytes; \
   1258       1.69    simonb 	(dst)._msg_cbytes = (src)._msg_cbytes; \
   1259       1.69    simonb 	(dst).msg_lspid = (src).msg_lspid; \
   1260       1.69    simonb 	(dst).msg_lrpid = (src).msg_lrpid; \
   1261       1.69    simonb 	(dst).msg_stime = (src).msg_stime; \
   1262       1.69    simonb 	(dst).msg_rtime = (src).msg_rtime; \
   1263       1.69    simonb 	(dst).msg_ctime = (src).msg_ctime; \
   1264  1.86.2.24   nathanw 	} while (/*CONSTCOND*/ 0)
   1265       1.69    simonb #define	FILL_SEM(src, dst) do { \
   1266       1.69    simonb 	FILL_PERM((src).sem_perm, (dst).sem_perm); \
   1267       1.69    simonb 	(dst).sem_nsems = (src).sem_nsems; \
   1268       1.69    simonb 	(dst).sem_otime = (src).sem_otime; \
   1269       1.69    simonb 	(dst).sem_ctime = (src).sem_ctime; \
   1270  1.86.2.24   nathanw 	} while (/*CONSTCOND*/ 0)
   1271       1.69    simonb #define	FILL_SHM(src, dst) do { \
   1272       1.69    simonb 	FILL_PERM((src).shm_perm, (dst).shm_perm); \
   1273       1.69    simonb 	(dst).shm_segsz = (src).shm_segsz; \
   1274       1.69    simonb 	(dst).shm_lpid = (src).shm_lpid; \
   1275       1.69    simonb 	(dst).shm_cpid = (src).shm_cpid; \
   1276       1.69    simonb 	(dst).shm_atime = (src).shm_atime; \
   1277       1.69    simonb 	(dst).shm_dtime = (src).shm_dtime; \
   1278       1.69    simonb 	(dst).shm_ctime = (src).shm_ctime; \
   1279       1.69    simonb 	(dst).shm_nattch = (src).shm_nattch; \
   1280  1.86.2.24   nathanw 	} while (/*CONSTCOND*/ 0)
   1281       1.69    simonb 
   1282       1.69    simonb static int
   1283   1.86.2.6   nathanw sysctl_sysvipc(int *name, u_int namelen, void *where, size_t *sizep)
   1284       1.69    simonb {
   1285       1.73    simonb #ifdef SYSVMSG
   1286  1.86.2.25   thorpej 	struct msg_sysctl_info *msgsi = NULL;
   1287       1.73    simonb #endif
   1288       1.73    simonb #ifdef SYSVSEM
   1289  1.86.2.25   thorpej 	struct sem_sysctl_info *semsi = NULL;
   1290       1.73    simonb #endif
   1291       1.73    simonb #ifdef SYSVSHM
   1292  1.86.2.25   thorpej 	struct shm_sysctl_info *shmsi = NULL;
   1293       1.73    simonb #endif
   1294       1.69    simonb 	size_t infosize, dssize, tsize, buflen;
   1295  1.86.2.14   nathanw 	void *buf = NULL;
   1296       1.69    simonb 	char *start;
   1297       1.69    simonb 	int32_t nds;
   1298       1.69    simonb 	int i, error, ret;
   1299       1.69    simonb 
   1300       1.69    simonb 	if (namelen != 1)
   1301       1.69    simonb 		return (EINVAL);
   1302       1.69    simonb 
   1303       1.69    simonb 	start = where;
   1304       1.69    simonb 	buflen = *sizep;
   1305       1.69    simonb 
   1306       1.69    simonb 	switch (*name) {
   1307       1.69    simonb 	case KERN_SYSVIPC_MSG_INFO:
   1308       1.69    simonb #ifdef SYSVMSG
   1309       1.69    simonb 		infosize = sizeof(msgsi->msginfo);
   1310       1.69    simonb 		nds = msginfo.msgmni;
   1311       1.69    simonb 		dssize = sizeof(msgsi->msgids[0]);
   1312       1.69    simonb 		break;
   1313       1.69    simonb #else
   1314       1.69    simonb 		return (EINVAL);
   1315       1.69    simonb #endif
   1316       1.69    simonb 	case KERN_SYSVIPC_SEM_INFO:
   1317       1.69    simonb #ifdef SYSVSEM
   1318       1.69    simonb 		infosize = sizeof(semsi->seminfo);
   1319       1.69    simonb 		nds = seminfo.semmni;
   1320       1.69    simonb 		dssize = sizeof(semsi->semids[0]);
   1321       1.69    simonb 		break;
   1322       1.69    simonb #else
   1323       1.69    simonb 		return (EINVAL);
   1324       1.69    simonb #endif
   1325       1.69    simonb 	case KERN_SYSVIPC_SHM_INFO:
   1326       1.69    simonb #ifdef SYSVSHM
   1327       1.69    simonb 		infosize = sizeof(shmsi->shminfo);
   1328       1.69    simonb 		nds = shminfo.shmmni;
   1329       1.69    simonb 		dssize = sizeof(shmsi->shmids[0]);
   1330       1.69    simonb 		break;
   1331       1.69    simonb #else
   1332       1.69    simonb 		return (EINVAL);
   1333       1.69    simonb #endif
   1334       1.69    simonb 	default:
   1335       1.69    simonb 		return (EINVAL);
   1336       1.69    simonb 	}
   1337       1.69    simonb 	/*
   1338       1.69    simonb 	 * Round infosize to 64 bit boundary if requesting more than just
   1339       1.69    simonb 	 * the info structure or getting the total data size.
   1340       1.69    simonb 	 */
   1341       1.69    simonb 	if (where == NULL || *sizep > infosize)
   1342       1.69    simonb 		infosize = ((infosize + 7) / 8) * 8;
   1343       1.69    simonb 	tsize = infosize + nds * dssize;
   1344       1.69    simonb 
   1345       1.69    simonb 	/* Return just the total size required. */
   1346       1.69    simonb 	if (where == NULL) {
   1347       1.69    simonb 		*sizep = tsize;
   1348       1.69    simonb 		return (0);
   1349       1.69    simonb 	}
   1350       1.69    simonb 
   1351       1.69    simonb 	/* Not enough room for even the info struct. */
   1352       1.69    simonb 	if (buflen < infosize) {
   1353       1.69    simonb 		*sizep = 0;
   1354       1.69    simonb 		return (ENOMEM);
   1355       1.69    simonb 	}
   1356       1.69    simonb 	buf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK);
   1357       1.69    simonb 	memset(buf, 0, min(tsize, buflen));
   1358       1.69    simonb 
   1359       1.69    simonb 	switch (*name) {
   1360       1.73    simonb #ifdef SYSVMSG
   1361       1.69    simonb 	case KERN_SYSVIPC_MSG_INFO:
   1362       1.69    simonb 		msgsi = (struct msg_sysctl_info *)buf;
   1363       1.69    simonb 		msgsi->msginfo = msginfo;
   1364       1.69    simonb 		break;
   1365       1.73    simonb #endif
   1366       1.73    simonb #ifdef SYSVSEM
   1367       1.69    simonb 	case KERN_SYSVIPC_SEM_INFO:
   1368       1.69    simonb 		semsi = (struct sem_sysctl_info *)buf;
   1369       1.69    simonb 		semsi->seminfo = seminfo;
   1370       1.69    simonb 		break;
   1371       1.73    simonb #endif
   1372       1.73    simonb #ifdef SYSVSHM
   1373       1.69    simonb 	case KERN_SYSVIPC_SHM_INFO:
   1374       1.69    simonb 		shmsi = (struct shm_sysctl_info *)buf;
   1375       1.69    simonb 		shmsi->shminfo = shminfo;
   1376       1.69    simonb 		break;
   1377       1.73    simonb #endif
   1378       1.69    simonb 	}
   1379       1.69    simonb 	buflen -= infosize;
   1380       1.69    simonb 
   1381       1.69    simonb 	ret = 0;
   1382       1.69    simonb 	if (buflen > 0) {
   1383       1.69    simonb 		/* Fill in the IPC data structures.  */
   1384       1.69    simonb 		for (i = 0; i < nds; i++) {
   1385       1.69    simonb 			if (buflen < dssize) {
   1386       1.69    simonb 				ret = ENOMEM;
   1387       1.69    simonb 				break;
   1388       1.69    simonb 			}
   1389       1.69    simonb 			switch (*name) {
   1390       1.73    simonb #ifdef SYSVMSG
   1391       1.69    simonb 			case KERN_SYSVIPC_MSG_INFO:
   1392       1.69    simonb 				FILL_MSG(msqids[i], msgsi->msgids[i]);
   1393       1.69    simonb 				break;
   1394       1.73    simonb #endif
   1395       1.73    simonb #ifdef SYSVSEM
   1396       1.69    simonb 			case KERN_SYSVIPC_SEM_INFO:
   1397       1.69    simonb 				FILL_SEM(sema[i], semsi->semids[i]);
   1398       1.69    simonb 				break;
   1399       1.73    simonb #endif
   1400       1.73    simonb #ifdef SYSVSHM
   1401       1.69    simonb 			case KERN_SYSVIPC_SHM_INFO:
   1402       1.69    simonb 				FILL_SHM(shmsegs[i], shmsi->shmids[i]);
   1403       1.69    simonb 				break;
   1404       1.73    simonb #endif
   1405       1.69    simonb 			}
   1406       1.69    simonb 			buflen -= dssize;
   1407       1.69    simonb 		}
   1408       1.69    simonb 	}
   1409       1.69    simonb 	*sizep -= buflen;
   1410       1.69    simonb 	error = copyout(buf, start, *sizep);
   1411       1.69    simonb 	/* If copyout succeeded, use return code set earlier. */
   1412       1.69    simonb 	if (error == 0)
   1413       1.69    simonb 		error = ret;
   1414       1.69    simonb 	if (buf)
   1415       1.69    simonb 		free(buf, M_TEMP);
   1416       1.69    simonb 	return (error);
   1417       1.69    simonb }
   1418       1.69    simonb #endif /* SYSVMSG || SYSVSEM || SYSVSHM */
   1419       1.72    simonb 
   1420       1.72    simonb static int
   1421   1.86.2.6   nathanw sysctl_msgbuf(void *vwhere, size_t *sizep)
   1422       1.72    simonb {
   1423       1.72    simonb 	char *where = vwhere;
   1424       1.72    simonb 	size_t len, maxlen = *sizep;
   1425   1.86.2.5   nathanw 	long beg, end;
   1426       1.72    simonb 	int error;
   1427       1.72    simonb 
   1428       1.72    simonb 	/*
   1429       1.72    simonb 	 * deal with cases where the message buffer has
   1430       1.72    simonb 	 * become corrupted.
   1431       1.72    simonb 	 */
   1432       1.72    simonb 	if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
   1433       1.72    simonb 		msgbufenabled = 0;
   1434       1.72    simonb 		return (ENXIO);
   1435       1.72    simonb 	}
   1436       1.72    simonb 
   1437       1.72    simonb 	if (where == NULL) {
   1438       1.72    simonb 		/* always return full buffer size */
   1439       1.72    simonb 		*sizep = msgbufp->msg_bufs;
   1440       1.72    simonb 		return (0);
   1441       1.72    simonb 	}
   1442       1.72    simonb 
   1443       1.72    simonb 	error = 0;
   1444       1.72    simonb 	maxlen = min(msgbufp->msg_bufs, maxlen);
   1445   1.86.2.5   nathanw 
   1446   1.86.2.5   nathanw 	/*
   1447   1.86.2.5   nathanw 	 * First, copy from the write pointer to the end of
   1448   1.86.2.5   nathanw 	 * message buffer.
   1449   1.86.2.5   nathanw 	 */
   1450   1.86.2.5   nathanw 	beg = msgbufp->msg_bufx;
   1451   1.86.2.5   nathanw 	end = msgbufp->msg_bufs;
   1452       1.72    simonb 	while (maxlen > 0) {
   1453   1.86.2.5   nathanw 		len = min(end - beg, maxlen);
   1454       1.72    simonb 		if (len == 0)
   1455       1.72    simonb 			break;
   1456   1.86.2.5   nathanw 		error = copyout(&msgbufp->msg_bufc[beg], where, len);
   1457       1.72    simonb 		if (error)
   1458       1.72    simonb 			break;
   1459       1.72    simonb 		where += len;
   1460       1.72    simonb 		maxlen -= len;
   1461   1.86.2.5   nathanw 
   1462   1.86.2.5   nathanw 		/*
   1463   1.86.2.5   nathanw 		 * ... then, copy from the beginning of message buffer to
   1464   1.86.2.5   nathanw 		 * the write pointer.
   1465   1.86.2.5   nathanw 		 */
   1466   1.86.2.5   nathanw 		beg = 0;
   1467   1.86.2.5   nathanw 		end = msgbufp->msg_bufx;
   1468       1.72    simonb 	}
   1469       1.72    simonb 	return (error);
   1470       1.72    simonb }
   1471       1.69    simonb 
   1472        1.1       cgd /*
   1473        1.1       cgd  * try over estimating by 5 procs
   1474        1.1       cgd  */
   1475       1.39     perry #define KERN_PROCSLOP	(5 * sizeof(struct kinfo_proc))
   1476        1.1       cgd 
   1477       1.69    simonb static int
   1478   1.86.2.6   nathanw sysctl_doeproc(int *name, u_int namelen, void *vwhere, size_t *sizep)
   1479        1.1       cgd {
   1480       1.62    simonb 	struct eproc eproc;
   1481       1.62    simonb 	struct kinfo_proc2 kproc2;
   1482       1.69    simonb 	struct kinfo_proc *dp;
   1483       1.60  augustss 	struct proc *p;
   1484       1.41   thorpej 	const struct proclist_desc *pd;
   1485       1.69    simonb 	char *where, *dp2;
   1486  1.86.2.22   nathanw 	int type, op, arg;
   1487  1.86.2.22   nathanw 	u_int elem_size, elem_count;
   1488  1.86.2.22   nathanw 	size_t buflen, needed;
   1489  1.86.2.22   nathanw 	int error;
   1490       1.62    simonb 
   1491       1.69    simonb 	dp = vwhere;
   1492       1.69    simonb 	dp2 = where = vwhere;
   1493       1.62    simonb 	buflen = where != NULL ? *sizep : 0;
   1494  1.86.2.22   nathanw 	error = 0;
   1495  1.86.2.22   nathanw 	needed = 0;
   1496       1.62    simonb 	type = name[0];
   1497        1.1       cgd 
   1498       1.62    simonb 	if (type == KERN_PROC) {
   1499       1.62    simonb 		if (namelen != 3 && !(namelen == 2 && name[1] == KERN_PROC_ALL))
   1500       1.62    simonb 			return (EINVAL);
   1501       1.62    simonb 		op = name[1];
   1502       1.62    simonb 		if (op != KERN_PROC_ALL)
   1503       1.62    simonb 			arg = name[2];
   1504  1.86.2.25   thorpej 		else
   1505  1.86.2.25   thorpej 			arg = 0;		/* Quell compiler warning */
   1506  1.86.2.25   thorpej 		elem_size = elem_count = 0;	/* Ditto */
   1507       1.62    simonb 	} else {
   1508       1.62    simonb 		if (namelen != 5)
   1509       1.62    simonb 			return (EINVAL);
   1510       1.62    simonb 		op = name[1];
   1511       1.62    simonb 		arg = name[2];
   1512       1.62    simonb 		elem_size = name[3];
   1513       1.62    simonb 		elem_count = name[4];
   1514       1.62    simonb 	}
   1515       1.41   thorpej 
   1516       1.50   thorpej 	proclist_lock_read();
   1517       1.49   thorpej 
   1518       1.41   thorpej 	pd = proclists;
   1519        1.1       cgd again:
   1520       1.62    simonb 	for (p = LIST_FIRST(pd->pd_list); p != NULL; p = LIST_NEXT(p, p_list)) {
   1521        1.1       cgd 		/*
   1522        1.1       cgd 		 * Skip embryonic processes.
   1523        1.1       cgd 		 */
   1524        1.1       cgd 		if (p->p_stat == SIDL)
   1525        1.1       cgd 			continue;
   1526        1.1       cgd 		/*
   1527        1.1       cgd 		 * TODO - make more efficient (see notes below).
   1528        1.1       cgd 		 * do by session.
   1529        1.1       cgd 		 */
   1530       1.62    simonb 		switch (op) {
   1531        1.1       cgd 
   1532        1.1       cgd 		case KERN_PROC_PID:
   1533        1.1       cgd 			/* could do this with just a lookup */
   1534       1.62    simonb 			if (p->p_pid != (pid_t)arg)
   1535        1.1       cgd 				continue;
   1536        1.1       cgd 			break;
   1537        1.1       cgd 
   1538        1.1       cgd 		case KERN_PROC_PGRP:
   1539        1.1       cgd 			/* could do this by traversing pgrp */
   1540       1.62    simonb 			if (p->p_pgrp->pg_id != (pid_t)arg)
   1541        1.1       cgd 				continue;
   1542        1.1       cgd 			break;
   1543        1.1       cgd 
   1544       1.71    simonb 		case KERN_PROC_SESSION:
   1545       1.71    simonb 			if (p->p_session->s_sid != (pid_t)arg)
   1546       1.71    simonb 				continue;
   1547       1.71    simonb 			break;
   1548       1.71    simonb 
   1549        1.1       cgd 		case KERN_PROC_TTY:
   1550  1.86.2.22   nathanw 			if (arg == (int) KERN_PROC_TTY_REVOKE) {
   1551       1.61    simonb 				if ((p->p_flag & P_CONTROLT) == 0 ||
   1552       1.61    simonb 				    p->p_session->s_ttyp == NULL ||
   1553       1.61    simonb 				    p->p_session->s_ttyvp != NULL)
   1554       1.61    simonb 					continue;
   1555       1.61    simonb 			} else if ((p->p_flag & P_CONTROLT) == 0 ||
   1556       1.61    simonb 			    p->p_session->s_ttyp == NULL) {
   1557       1.62    simonb 				if ((dev_t)arg != KERN_PROC_TTY_NODEV)
   1558       1.61    simonb 					continue;
   1559       1.62    simonb 			} else if (p->p_session->s_ttyp->t_dev != (dev_t)arg)
   1560        1.1       cgd 				continue;
   1561        1.1       cgd 			break;
   1562        1.1       cgd 
   1563        1.1       cgd 		case KERN_PROC_UID:
   1564       1.62    simonb 			if (p->p_ucred->cr_uid != (uid_t)arg)
   1565        1.1       cgd 				continue;
   1566        1.1       cgd 			break;
   1567        1.1       cgd 
   1568        1.1       cgd 		case KERN_PROC_RUID:
   1569       1.62    simonb 			if (p->p_cred->p_ruid != (uid_t)arg)
   1570        1.1       cgd 				continue;
   1571        1.1       cgd 			break;
   1572       1.71    simonb 
   1573       1.71    simonb 		case KERN_PROC_GID:
   1574       1.71    simonb 			if (p->p_ucred->cr_gid != (uid_t)arg)
   1575       1.71    simonb 				continue;
   1576       1.71    simonb 			break;
   1577       1.71    simonb 
   1578       1.71    simonb 		case KERN_PROC_RGID:
   1579       1.71    simonb 			if (p->p_cred->p_rgid != (uid_t)arg)
   1580       1.71    simonb 				continue;
   1581       1.71    simonb 			break;
   1582       1.71    simonb 
   1583       1.71    simonb 		case KERN_PROC_ALL:
   1584       1.71    simonb 			/* allow everything */
   1585       1.71    simonb 			break;
   1586       1.71    simonb 
   1587       1.71    simonb 		default:
   1588       1.71    simonb 			error = EINVAL;
   1589       1.71    simonb 			goto cleanup;
   1590        1.1       cgd 		}
   1591       1.62    simonb 		if (type == KERN_PROC) {
   1592       1.62    simonb 			if (buflen >= sizeof(struct kinfo_proc)) {
   1593       1.62    simonb 				fill_eproc(p, &eproc);
   1594       1.62    simonb 				error = copyout((caddr_t)p, &dp->kp_proc,
   1595       1.62    simonb 						sizeof(struct proc));
   1596       1.62    simonb 				if (error)
   1597       1.62    simonb 					goto cleanup;
   1598       1.62    simonb 				error = copyout((caddr_t)&eproc, &dp->kp_eproc,
   1599       1.62    simonb 						sizeof(eproc));
   1600       1.62    simonb 				if (error)
   1601       1.62    simonb 					goto cleanup;
   1602       1.62    simonb 				dp++;
   1603       1.62    simonb 				buflen -= sizeof(struct kinfo_proc);
   1604       1.62    simonb 			}
   1605       1.62    simonb 			needed += sizeof(struct kinfo_proc);
   1606       1.62    simonb 		} else { /* KERN_PROC2 */
   1607       1.64    simonb 			if (buflen >= elem_size && elem_count > 0) {
   1608       1.62    simonb 				fill_kproc2(p, &kproc2);
   1609       1.62    simonb 				/*
   1610       1.62    simonb 				 * Copy out elem_size, but not larger than
   1611       1.62    simonb 				 * the size of a struct kinfo_proc2.
   1612       1.62    simonb 				 */
   1613       1.62    simonb 				error = copyout(&kproc2, dp2,
   1614       1.62    simonb 				    min(sizeof(kproc2), elem_size));
   1615       1.62    simonb 				if (error)
   1616       1.62    simonb 					goto cleanup;
   1617       1.62    simonb 				dp2 += elem_size;
   1618       1.62    simonb 				buflen -= elem_size;
   1619       1.64    simonb 				elem_count--;
   1620       1.62    simonb 			}
   1621       1.62    simonb 			needed += elem_size;
   1622        1.1       cgd 		}
   1623        1.1       cgd 	}
   1624       1.41   thorpej 	pd++;
   1625       1.41   thorpej 	if (pd->pd_list != NULL)
   1626        1.1       cgd 		goto again;
   1627       1.49   thorpej 	proclist_unlock_read();
   1628       1.41   thorpej 
   1629        1.1       cgd 	if (where != NULL) {
   1630       1.62    simonb 		if (type == KERN_PROC)
   1631       1.62    simonb 			*sizep = (caddr_t)dp - where;
   1632       1.62    simonb 		else
   1633       1.62    simonb 			*sizep = dp2 - where;
   1634        1.1       cgd 		if (needed > *sizep)
   1635        1.1       cgd 			return (ENOMEM);
   1636        1.1       cgd 	} else {
   1637        1.1       cgd 		needed += KERN_PROCSLOP;
   1638        1.1       cgd 		*sizep = needed;
   1639        1.1       cgd 	}
   1640        1.1       cgd 	return (0);
   1641       1.56     assar  cleanup:
   1642       1.56     assar 	proclist_unlock_read();
   1643       1.56     assar 	return (error);
   1644        1.1       cgd }
   1645        1.1       cgd 
   1646  1.86.2.15   nathanw 
   1647  1.86.2.15   nathanw /*
   1648  1.86.2.15   nathanw  * try over estimating by 5 LWPs
   1649  1.86.2.15   nathanw  */
   1650  1.86.2.15   nathanw #define KERN_LWPSLOP	(5 * sizeof(struct kinfo_lwp))
   1651  1.86.2.15   nathanw 
   1652  1.86.2.15   nathanw static int
   1653  1.86.2.15   nathanw sysctl_dolwp(int *name, u_int namelen, void *vwhere, size_t *sizep)
   1654  1.86.2.15   nathanw {
   1655  1.86.2.15   nathanw 	struct kinfo_lwp klwp;
   1656  1.86.2.15   nathanw 	struct proc *p;
   1657  1.86.2.15   nathanw 	struct lwp *l;
   1658  1.86.2.15   nathanw 	char *where, *dp;
   1659  1.86.2.15   nathanw 	int type, pid, elem_size, elem_count;
   1660  1.86.2.15   nathanw 	int buflen, needed, error;
   1661  1.86.2.15   nathanw 
   1662  1.86.2.15   nathanw 	dp = where = vwhere;
   1663  1.86.2.15   nathanw 	buflen = where != NULL ? *sizep : 0;
   1664  1.86.2.15   nathanw 	error = needed = 0;
   1665  1.86.2.15   nathanw 	type = name[0];
   1666  1.86.2.15   nathanw 
   1667  1.86.2.15   nathanw 	if (namelen != 4)
   1668  1.86.2.15   nathanw 		return (EINVAL);
   1669  1.86.2.15   nathanw 	pid = name[1];
   1670  1.86.2.15   nathanw 	elem_size = name[2];
   1671  1.86.2.15   nathanw 	elem_count = name[3];
   1672  1.86.2.15   nathanw 
   1673  1.86.2.15   nathanw 	p = pfind(pid);
   1674  1.86.2.15   nathanw 	if (p == NULL)
   1675  1.86.2.15   nathanw 		return (ESRCH);
   1676  1.86.2.15   nathanw 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1677  1.86.2.15   nathanw 		if (buflen >= elem_size && elem_count > 0) {
   1678  1.86.2.15   nathanw 			fill_lwp(l, &klwp);
   1679  1.86.2.15   nathanw 			/*
   1680  1.86.2.15   nathanw 			 * Copy out elem_size, but not larger than
   1681  1.86.2.15   nathanw 			 * the size of a struct kinfo_proc2.
   1682  1.86.2.15   nathanw 			 */
   1683  1.86.2.15   nathanw 			error = copyout(&klwp, dp,
   1684  1.86.2.15   nathanw 			    min(sizeof(klwp), elem_size));
   1685  1.86.2.15   nathanw 			if (error)
   1686  1.86.2.15   nathanw 				goto cleanup;
   1687  1.86.2.15   nathanw 			dp += elem_size;
   1688  1.86.2.15   nathanw 			buflen -= elem_size;
   1689  1.86.2.15   nathanw 			elem_count--;
   1690  1.86.2.15   nathanw 		}
   1691  1.86.2.15   nathanw 		needed += elem_size;
   1692  1.86.2.15   nathanw 	}
   1693  1.86.2.15   nathanw 
   1694  1.86.2.15   nathanw 	if (where != NULL) {
   1695  1.86.2.15   nathanw 		*sizep = dp - where;
   1696  1.86.2.15   nathanw 		if (needed > *sizep)
   1697  1.86.2.15   nathanw 			return (ENOMEM);
   1698  1.86.2.15   nathanw 	} else {
   1699  1.86.2.15   nathanw 		needed += KERN_PROCSLOP;
   1700  1.86.2.15   nathanw 		*sizep = needed;
   1701  1.86.2.15   nathanw 	}
   1702  1.86.2.15   nathanw 	return (0);
   1703  1.86.2.15   nathanw  cleanup:
   1704  1.86.2.15   nathanw 	return (error);
   1705  1.86.2.15   nathanw }
   1706  1.86.2.15   nathanw 
   1707        1.1       cgd /*
   1708        1.1       cgd  * Fill in an eproc structure for the specified process.
   1709        1.1       cgd  */
   1710        1.1       cgd void
   1711   1.86.2.6   nathanw fill_eproc(struct proc *p, struct eproc *ep)
   1712        1.1       cgd {
   1713       1.60  augustss 	struct tty *tp;
   1714   1.86.2.1   nathanw 	struct lwp *l;
   1715   1.86.2.1   nathanw 
   1716        1.1       cgd 	ep->e_paddr = p;
   1717       1.61    simonb 	ep->e_sess = p->p_session;
   1718        1.1       cgd 	ep->e_pcred = *p->p_cred;
   1719        1.1       cgd 	ep->e_ucred = *p->p_ucred;
   1720       1.48   thorpej 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   1721        1.1       cgd 		ep->e_vm.vm_rssize = 0;
   1722        1.1       cgd 		ep->e_vm.vm_tsize = 0;
   1723        1.1       cgd 		ep->e_vm.vm_dsize = 0;
   1724        1.1       cgd 		ep->e_vm.vm_ssize = 0;
   1725        1.1       cgd 		/* ep->e_vm.vm_pmap = XXX; */
   1726        1.1       cgd 	} else {
   1727       1.60  augustss 		struct vmspace *vm = p->p_vmspace;
   1728        1.1       cgd 
   1729       1.26       gwr 		ep->e_vm.vm_rssize = vm_resident_count(vm);
   1730        1.1       cgd 		ep->e_vm.vm_tsize = vm->vm_tsize;
   1731        1.1       cgd 		ep->e_vm.vm_dsize = vm->vm_dsize;
   1732        1.1       cgd 		ep->e_vm.vm_ssize = vm->vm_ssize;
   1733   1.86.2.2   nathanw 
   1734   1.86.2.2   nathanw 		/* Pick a "representative" LWP */
   1735   1.86.2.2   nathanw 		l = proc_representative_lwp(p);
   1736   1.86.2.2   nathanw 
   1737   1.86.2.2   nathanw 		if (l->l_wmesg)
   1738   1.86.2.2   nathanw 			strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
   1739        1.1       cgd 	}
   1740        1.1       cgd 	if (p->p_pptr)
   1741        1.1       cgd 		ep->e_ppid = p->p_pptr->p_pid;
   1742        1.1       cgd 	else
   1743        1.1       cgd 		ep->e_ppid = 0;
   1744        1.1       cgd 	ep->e_pgid = p->p_pgrp->pg_id;
   1745       1.33   thorpej 	ep->e_sid = ep->e_sess->s_sid;
   1746        1.1       cgd 	ep->e_jobc = p->p_pgrp->pg_jobc;
   1747        1.1       cgd 	if ((p->p_flag & P_CONTROLT) &&
   1748        1.1       cgd 	     (tp = ep->e_sess->s_ttyp)) {
   1749        1.1       cgd 		ep->e_tdev = tp->t_dev;
   1750        1.1       cgd 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
   1751        1.1       cgd 		ep->e_tsess = tp->t_session;
   1752        1.1       cgd 	} else
   1753        1.1       cgd 		ep->e_tdev = NODEV;
   1754   1.86.2.1   nathanw 
   1755        1.1       cgd 	ep->e_xsize = ep->e_xrssize = 0;
   1756        1.1       cgd 	ep->e_xccount = ep->e_xswrss = 0;
   1757       1.24   mycroft 	ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
   1758       1.24   mycroft 	if (SESS_LEADER(p))
   1759       1.24   mycroft 		ep->e_flag |= EPROC_SLEADER;
   1760       1.24   mycroft 	strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
   1761       1.62    simonb }
   1762       1.62    simonb 
   1763       1.62    simonb /*
   1764       1.62    simonb  * Fill in an eproc structure for the specified process.
   1765       1.62    simonb  */
   1766       1.62    simonb static void
   1767   1.86.2.6   nathanw fill_kproc2(struct proc *p, struct kinfo_proc2 *ki)
   1768       1.62    simonb {
   1769       1.62    simonb 	struct tty *tp;
   1770   1.86.2.1   nathanw 	struct lwp *l;
   1771       1.62    simonb 	memset(ki, 0, sizeof(*ki));
   1772       1.62    simonb 
   1773       1.62    simonb 	ki->p_paddr = PTRTOINT64(p);
   1774       1.62    simonb 	ki->p_fd = PTRTOINT64(p->p_fd);
   1775       1.62    simonb 	ki->p_cwdi = PTRTOINT64(p->p_cwdi);
   1776       1.62    simonb 	ki->p_stats = PTRTOINT64(p->p_stats);
   1777       1.62    simonb 	ki->p_limit = PTRTOINT64(p->p_limit);
   1778       1.62    simonb 	ki->p_vmspace = PTRTOINT64(p->p_vmspace);
   1779       1.62    simonb 	ki->p_sigacts = PTRTOINT64(p->p_sigacts);
   1780       1.62    simonb 	ki->p_sess = PTRTOINT64(p->p_session);
   1781       1.62    simonb 	ki->p_tsess = 0;	/* may be changed if controlling tty below */
   1782       1.62    simonb 	ki->p_ru = PTRTOINT64(p->p_ru);
   1783       1.62    simonb 
   1784       1.62    simonb 	ki->p_eflag = 0;
   1785       1.62    simonb 	ki->p_exitsig = p->p_exitsig;
   1786       1.62    simonb 	ki->p_flag = p->p_flag;
   1787       1.62    simonb 
   1788       1.62    simonb 	ki->p_pid = p->p_pid;
   1789       1.62    simonb 	if (p->p_pptr)
   1790       1.62    simonb 		ki->p_ppid = p->p_pptr->p_pid;
   1791       1.62    simonb 	else
   1792       1.62    simonb 		ki->p_ppid = 0;
   1793       1.62    simonb 	ki->p_sid = p->p_session->s_sid;
   1794       1.62    simonb 	ki->p__pgid = p->p_pgrp->pg_id;
   1795       1.62    simonb 
   1796       1.62    simonb 	ki->p_tpgid = NO_PID;	/* may be changed if controlling tty below */
   1797       1.62    simonb 
   1798       1.62    simonb 	ki->p_uid = p->p_ucred->cr_uid;
   1799       1.62    simonb 	ki->p_ruid = p->p_cred->p_ruid;
   1800       1.62    simonb 	ki->p_gid = p->p_ucred->cr_gid;
   1801       1.62    simonb 	ki->p_rgid = p->p_cred->p_rgid;
   1802       1.62    simonb 
   1803       1.62    simonb 	memcpy(ki->p_groups, p->p_cred->pc_ucred->cr_groups,
   1804       1.62    simonb 	    min(sizeof(ki->p_groups), sizeof(p->p_cred->pc_ucred->cr_groups)));
   1805       1.62    simonb 	ki->p_ngroups = p->p_cred->pc_ucred->cr_ngroups;
   1806       1.62    simonb 
   1807       1.62    simonb 	ki->p_jobc = p->p_pgrp->pg_jobc;
   1808       1.62    simonb 	if ((p->p_flag & P_CONTROLT) && (tp = p->p_session->s_ttyp)) {
   1809       1.62    simonb 		ki->p_tdev = tp->t_dev;
   1810       1.62    simonb 		ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
   1811       1.62    simonb 		ki->p_tsess = PTRTOINT64(tp->t_session);
   1812       1.62    simonb 	} else {
   1813       1.62    simonb 		ki->p_tdev = NODEV;
   1814       1.62    simonb 	}
   1815       1.62    simonb 
   1816       1.62    simonb 	ki->p_estcpu = p->p_estcpu;
   1817       1.62    simonb 	ki->p_rtime_sec = p->p_rtime.tv_sec;
   1818       1.62    simonb 	ki->p_rtime_usec = p->p_rtime.tv_usec;
   1819       1.62    simonb 	ki->p_cpticks = p->p_cpticks;
   1820       1.62    simonb 	ki->p_pctcpu = p->p_pctcpu;
   1821       1.62    simonb 
   1822       1.62    simonb 	ki->p_uticks = p->p_uticks;
   1823       1.62    simonb 	ki->p_sticks = p->p_sticks;
   1824       1.62    simonb 	ki->p_iticks = p->p_iticks;
   1825       1.62    simonb 
   1826       1.62    simonb 	ki->p_tracep = PTRTOINT64(p->p_tracep);
   1827       1.62    simonb 	ki->p_traceflag = p->p_traceflag;
   1828       1.62    simonb 
   1829       1.62    simonb 
   1830       1.86  jdolecek 	memcpy(&ki->p_siglist, &p->p_sigctx.ps_siglist, sizeof(ki_sigset_t));
   1831       1.86  jdolecek 	memcpy(&ki->p_sigmask, &p->p_sigctx.ps_sigmask, sizeof(ki_sigset_t));
   1832       1.86  jdolecek 	memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
   1833       1.86  jdolecek 	memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
   1834       1.62    simonb 
   1835  1.86.2.16   nathanw 	ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
   1836  1.86.2.16   nathanw 	ki->p_realstat = p->p_stat;
   1837       1.62    simonb 	ki->p_nice = p->p_nice;
   1838       1.62    simonb 
   1839       1.62    simonb 	ki->p_xstat = p->p_xstat;
   1840       1.62    simonb 	ki->p_acflag = p->p_acflag;
   1841       1.62    simonb 
   1842       1.62    simonb 	strncpy(ki->p_comm, p->p_comm,
   1843       1.62    simonb 	    min(sizeof(ki->p_comm), sizeof(p->p_comm)));
   1844       1.62    simonb 
   1845       1.62    simonb 	strncpy(ki->p_login, p->p_session->s_login, sizeof(ki->p_login));
   1846       1.62    simonb 
   1847  1.86.2.15   nathanw 	ki->p_nlwps = p->p_nlwps;
   1848  1.86.2.15   nathanw 	ki->p_nrlwps = p->p_nrlwps;
   1849  1.86.2.15   nathanw 	ki->p_realflag = p->p_flag;
   1850  1.86.2.15   nathanw 
   1851       1.62    simonb 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   1852       1.62    simonb 		ki->p_vm_rssize = 0;
   1853       1.62    simonb 		ki->p_vm_tsize = 0;
   1854       1.62    simonb 		ki->p_vm_dsize = 0;
   1855       1.62    simonb 		ki->p_vm_ssize = 0;
   1856  1.86.2.15   nathanw 		l = NULL;
   1857       1.62    simonb 	} else {
   1858       1.62    simonb 		struct vmspace *vm = p->p_vmspace;
   1859       1.62    simonb 
   1860       1.62    simonb 		ki->p_vm_rssize = vm_resident_count(vm);
   1861       1.62    simonb 		ki->p_vm_tsize = vm->vm_tsize;
   1862       1.62    simonb 		ki->p_vm_dsize = vm->vm_dsize;
   1863       1.62    simonb 		ki->p_vm_ssize = vm->vm_ssize;
   1864   1.86.2.2   nathanw 
   1865   1.86.2.2   nathanw 		/* Pick a "representative" LWP */
   1866   1.86.2.2   nathanw 		l = proc_representative_lwp(p);
   1867   1.86.2.2   nathanw 		ki->p_forw = PTRTOINT64(l->l_forw);
   1868   1.86.2.2   nathanw 		ki->p_back = PTRTOINT64(l->l_back);
   1869   1.86.2.2   nathanw 		ki->p_addr = PTRTOINT64(l->l_addr);
   1870   1.86.2.7   nathanw 		ki->p_stat = l->l_stat;
   1871   1.86.2.7   nathanw 		ki->p_flag |= l->l_flag;
   1872   1.86.2.2   nathanw 		ki->p_swtime = l->l_swtime;
   1873   1.86.2.2   nathanw 		ki->p_slptime = l->l_slptime;
   1874   1.86.2.2   nathanw 		if (l->l_stat == LSONPROC) {
   1875   1.86.2.2   nathanw 			KDASSERT(l->l_cpu != NULL);
   1876   1.86.2.2   nathanw 			ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   1877   1.86.2.2   nathanw 		} else
   1878   1.86.2.2   nathanw 			ki->p_schedflags = 0;
   1879   1.86.2.2   nathanw 		ki->p_holdcnt = l->l_holdcnt;
   1880   1.86.2.2   nathanw 		ki->p_priority = l->l_priority;
   1881   1.86.2.2   nathanw 		ki->p_usrpri = l->l_usrpri;
   1882   1.86.2.2   nathanw 		if (l->l_wmesg)
   1883   1.86.2.2   nathanw 			strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
   1884   1.86.2.2   nathanw 		ki->p_wchan = PTRTOINT64(l->l_wchan);
   1885   1.86.2.2   nathanw 
   1886       1.62    simonb 	}
   1887       1.62    simonb 
   1888       1.62    simonb 	if (p->p_session->s_ttyvp)
   1889       1.62    simonb 		ki->p_eflag |= EPROC_CTTY;
   1890       1.62    simonb 	if (SESS_LEADER(p))
   1891       1.62    simonb 		ki->p_eflag |= EPROC_SLEADER;
   1892       1.62    simonb 
   1893       1.62    simonb 	/* XXX Is this double check necessary? */
   1894   1.86.2.2   nathanw 	if (P_ZOMBIE(p)) {
   1895       1.62    simonb 		ki->p_uvalid = 0;
   1896       1.62    simonb 	} else {
   1897       1.62    simonb 		ki->p_uvalid = 1;
   1898       1.62    simonb 
   1899       1.62    simonb 		ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
   1900       1.62    simonb 		ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
   1901       1.62    simonb 
   1902       1.62    simonb 		ki->p_uutime_sec = p->p_stats->p_ru.ru_utime.tv_sec;
   1903       1.62    simonb 		ki->p_uutime_usec = p->p_stats->p_ru.ru_utime.tv_usec;
   1904       1.62    simonb 		ki->p_ustime_sec = p->p_stats->p_ru.ru_stime.tv_sec;
   1905       1.62    simonb 		ki->p_ustime_usec = p->p_stats->p_ru.ru_stime.tv_usec;
   1906       1.62    simonb 
   1907       1.62    simonb 		ki->p_uru_maxrss = p->p_stats->p_ru.ru_maxrss;
   1908       1.62    simonb 		ki->p_uru_ixrss = p->p_stats->p_ru.ru_ixrss;
   1909       1.62    simonb 		ki->p_uru_idrss = p->p_stats->p_ru.ru_idrss;
   1910       1.62    simonb 		ki->p_uru_isrss = p->p_stats->p_ru.ru_isrss;
   1911       1.62    simonb 		ki->p_uru_minflt = p->p_stats->p_ru.ru_minflt;
   1912       1.62    simonb 		ki->p_uru_majflt = p->p_stats->p_ru.ru_majflt;
   1913       1.62    simonb 		ki->p_uru_nswap = p->p_stats->p_ru.ru_nswap;
   1914       1.62    simonb 		ki->p_uru_inblock = p->p_stats->p_ru.ru_inblock;
   1915       1.62    simonb 		ki->p_uru_oublock = p->p_stats->p_ru.ru_oublock;
   1916       1.62    simonb 		ki->p_uru_msgsnd = p->p_stats->p_ru.ru_msgsnd;
   1917       1.62    simonb 		ki->p_uru_msgrcv = p->p_stats->p_ru.ru_msgrcv;
   1918       1.62    simonb 		ki->p_uru_nsignals = p->p_stats->p_ru.ru_nsignals;
   1919       1.62    simonb 		ki->p_uru_nvcsw = p->p_stats->p_ru.ru_nvcsw;
   1920       1.62    simonb 		ki->p_uru_nivcsw = p->p_stats->p_ru.ru_nivcsw;
   1921       1.62    simonb 
   1922       1.62    simonb 		ki->p_uctime_sec = p->p_stats->p_cru.ru_utime.tv_sec +
   1923       1.62    simonb 		    p->p_stats->p_cru.ru_stime.tv_sec;
   1924       1.62    simonb 		ki->p_uctime_usec = p->p_stats->p_cru.ru_utime.tv_usec +
   1925       1.62    simonb 		    p->p_stats->p_cru.ru_stime.tv_usec;
   1926       1.62    simonb 	}
   1927       1.83  sommerfe #ifdef MULTIPROCESSOR
   1928  1.86.2.15   nathanw 	if (l && l->l_cpu != NULL)
   1929  1.86.2.15   nathanw 		ki->p_cpuid = l->l_cpu->ci_cpuid;
   1930       1.83  sommerfe 	else
   1931       1.83  sommerfe #endif
   1932       1.83  sommerfe 		ki->p_cpuid = KI_NOCPU;
   1933   1.86.2.1   nathanw 
   1934   1.86.2.1   nathanw }
   1935   1.86.2.1   nathanw 
   1936  1.86.2.15   nathanw /*
   1937  1.86.2.15   nathanw  * Fill in a kinfo_lwp structure for the specified lwp.
   1938  1.86.2.15   nathanw  */
   1939  1.86.2.15   nathanw static void
   1940  1.86.2.15   nathanw fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
   1941  1.86.2.15   nathanw {
   1942  1.86.2.15   nathanw 	kl->l_forw = PTRTOINT64(l->l_forw);
   1943  1.86.2.15   nathanw 	kl->l_back = PTRTOINT64(l->l_back);
   1944  1.86.2.16   nathanw 	kl->l_laddr = PTRTOINT64(l);
   1945  1.86.2.15   nathanw 	kl->l_addr = PTRTOINT64(l->l_addr);
   1946  1.86.2.15   nathanw 	kl->l_stat = l->l_stat;
   1947  1.86.2.15   nathanw 	kl->l_lid = l->l_lid;
   1948  1.86.2.17   nathanw 	kl->l_flag = l->l_flag;
   1949  1.86.2.15   nathanw 
   1950  1.86.2.15   nathanw 	kl->l_swtime = l->l_swtime;
   1951  1.86.2.15   nathanw 	kl->l_slptime = l->l_slptime;
   1952  1.86.2.15   nathanw 	if (l->l_stat == LSONPROC) {
   1953  1.86.2.15   nathanw 		KDASSERT(l->l_cpu != NULL);
   1954  1.86.2.15   nathanw 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   1955  1.86.2.15   nathanw 	} else
   1956  1.86.2.15   nathanw 		kl->l_schedflags = 0;
   1957  1.86.2.15   nathanw 	kl->l_holdcnt = l->l_holdcnt;
   1958  1.86.2.15   nathanw 	kl->l_priority = l->l_priority;
   1959  1.86.2.15   nathanw 	kl->l_usrpri = l->l_usrpri;
   1960  1.86.2.15   nathanw 	if (l->l_wmesg)
   1961  1.86.2.15   nathanw 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
   1962  1.86.2.15   nathanw 	kl->l_wchan = PTRTOINT64(l->l_wchan);
   1963  1.86.2.15   nathanw #ifdef MULTIPROCESSOR
   1964  1.86.2.15   nathanw 	if (l->l_cpu != NULL)
   1965  1.86.2.15   nathanw 		kl->l_cpuid = l->l_cpu->ci_cpuid;
   1966  1.86.2.15   nathanw 	else
   1967  1.86.2.15   nathanw #endif
   1968  1.86.2.15   nathanw 		kl->l_cpuid = KI_NOCPU;
   1969  1.86.2.15   nathanw }
   1970   1.86.2.1   nathanw 
   1971       1.62    simonb int
   1972   1.86.2.6   nathanw sysctl_procargs(int *name, u_int namelen, void *where, size_t *sizep,
   1973   1.86.2.6   nathanw     struct proc *up)
   1974       1.62    simonb {
   1975       1.62    simonb 	struct ps_strings pss;
   1976       1.62    simonb 	struct proc *p;
   1977  1.86.2.22   nathanw 	size_t len, upper_bound, xlen, i;
   1978       1.62    simonb 	struct uio auio;
   1979       1.62    simonb 	struct iovec aiov;
   1980       1.62    simonb 	vaddr_t argv;
   1981       1.62    simonb 	pid_t pid;
   1982  1.86.2.22   nathanw 	int nargv, type, error;
   1983       1.62    simonb 	char *arg;
   1984       1.68        pk 	char *tmp;
   1985       1.62    simonb 
   1986       1.62    simonb 	if (namelen != 2)
   1987       1.62    simonb 		return (EINVAL);
   1988       1.62    simonb 	pid = name[0];
   1989       1.62    simonb 	type = name[1];
   1990       1.62    simonb 
   1991       1.62    simonb 	switch (type) {
   1992  1.86.2.22   nathanw 	case KERN_PROC_ARGV:
   1993  1.86.2.22   nathanw 	case KERN_PROC_NARGV:
   1994  1.86.2.22   nathanw 	case KERN_PROC_ENV:
   1995  1.86.2.22   nathanw 	case KERN_PROC_NENV:
   1996       1.62    simonb 		/* ok */
   1997       1.62    simonb 		break;
   1998  1.86.2.22   nathanw 	default:
   1999       1.62    simonb 		return (EINVAL);
   2000       1.62    simonb 	}
   2001       1.62    simonb 
   2002       1.62    simonb 	/* check pid */
   2003       1.62    simonb 	if ((p = pfind(pid)) == NULL)
   2004       1.62    simonb 		return (EINVAL);
   2005       1.62    simonb 
   2006       1.62    simonb 	/* only root or same user change look at the environment */
   2007       1.62    simonb 	if (type == KERN_PROC_ENV || type == KERN_PROC_NENV) {
   2008       1.62    simonb 		if (up->p_ucred->cr_uid != 0) {
   2009       1.62    simonb 			if (up->p_cred->p_ruid != p->p_cred->p_ruid ||
   2010       1.62    simonb 			    up->p_cred->p_ruid != p->p_cred->p_svuid)
   2011       1.62    simonb 				return (EPERM);
   2012       1.62    simonb 		}
   2013       1.62    simonb 	}
   2014       1.62    simonb 
   2015       1.62    simonb 	if (sizep != NULL && where == NULL) {
   2016       1.62    simonb 		if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
   2017       1.62    simonb 			*sizep = sizeof (int);
   2018       1.62    simonb 		else
   2019       1.62    simonb 			*sizep = ARG_MAX;	/* XXX XXX XXX */
   2020       1.62    simonb 		return (0);
   2021       1.62    simonb 	}
   2022       1.62    simonb 	if (where == NULL || sizep == NULL)
   2023       1.62    simonb 		return (EINVAL);
   2024       1.62    simonb 
   2025       1.62    simonb 	/*
   2026       1.62    simonb 	 * Zombies don't have a stack, so we can't read their psstrings.
   2027       1.62    simonb 	 * System processes also don't have a user stack.
   2028       1.62    simonb 	 */
   2029       1.62    simonb 	if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0)
   2030       1.62    simonb 		return (EINVAL);
   2031       1.62    simonb 
   2032       1.62    simonb 	/*
   2033       1.62    simonb 	 * Lock the process down in memory.
   2034       1.62    simonb 	 */
   2035       1.62    simonb 	/* XXXCDC: how should locking work here? */
   2036       1.62    simonb 	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
   2037       1.62    simonb 		return (EFAULT);
   2038   1.86.2.1   nathanw 
   2039       1.62    simonb 	p->p_vmspace->vm_refcnt++;	/* XXX */
   2040       1.67    simonb 
   2041       1.67    simonb 	/*
   2042       1.67    simonb 	 * Allocate a temporary buffer to hold the arguments.
   2043       1.67    simonb 	 */
   2044       1.67    simonb 	arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
   2045       1.62    simonb 
   2046       1.62    simonb 	/*
   2047       1.62    simonb 	 * Read in the ps_strings structure.
   2048       1.62    simonb 	 */
   2049       1.62    simonb 	aiov.iov_base = &pss;
   2050       1.62    simonb 	aiov.iov_len = sizeof(pss);
   2051       1.62    simonb 	auio.uio_iov = &aiov;
   2052       1.62    simonb 	auio.uio_iovcnt = 1;
   2053       1.62    simonb 	auio.uio_offset = (vaddr_t)p->p_psstr;
   2054       1.62    simonb 	auio.uio_resid = sizeof(pss);
   2055       1.62    simonb 	auio.uio_segflg = UIO_SYSSPACE;
   2056       1.62    simonb 	auio.uio_rw = UIO_READ;
   2057       1.62    simonb 	auio.uio_procp = NULL;
   2058       1.62    simonb 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
   2059       1.62    simonb 	if (error)
   2060       1.62    simonb 		goto done;
   2061       1.62    simonb 
   2062       1.62    simonb 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
   2063       1.62    simonb 		memcpy(&nargv, (char *)&pss + p->p_psnargv, sizeof(nargv));
   2064       1.62    simonb 	else
   2065       1.62    simonb 		memcpy(&nargv, (char *)&pss + p->p_psnenv, sizeof(nargv));
   2066       1.62    simonb 	if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
   2067       1.62    simonb 		error = copyout(&nargv, where, sizeof(nargv));
   2068       1.64    simonb 		*sizep = sizeof(nargv);
   2069       1.62    simonb 		goto done;
   2070       1.62    simonb 	}
   2071       1.62    simonb 	/*
   2072       1.62    simonb 	 * Now read the address of the argument vector.
   2073       1.62    simonb 	 */
   2074       1.62    simonb 	switch (type) {
   2075       1.68        pk 	case KERN_PROC_ARGV:
   2076       1.62    simonb 		/* XXX compat32 stuff here */
   2077       1.68        pk 		memcpy(&tmp, (char *)&pss + p->p_psargv, sizeof(tmp));
   2078       1.62    simonb 		break;
   2079       1.68        pk 	case KERN_PROC_ENV:
   2080       1.68        pk 		memcpy(&tmp, (char *)&pss + p->p_psenv, sizeof(tmp));
   2081       1.62    simonb 		break;
   2082       1.68        pk 	default:
   2083       1.68        pk 		return (EINVAL);
   2084       1.62    simonb 	}
   2085       1.68        pk 	auio.uio_offset = (off_t)(long)tmp;
   2086       1.62    simonb 	aiov.iov_base = &argv;
   2087       1.62    simonb 	aiov.iov_len = sizeof(argv);
   2088       1.62    simonb 	auio.uio_iov = &aiov;
   2089       1.62    simonb 	auio.uio_iovcnt = 1;
   2090       1.62    simonb 	auio.uio_resid = sizeof(argv);
   2091       1.62    simonb 	auio.uio_segflg = UIO_SYSSPACE;
   2092       1.62    simonb 	auio.uio_rw = UIO_READ;
   2093       1.62    simonb 	auio.uio_procp = NULL;
   2094       1.62    simonb 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
   2095       1.62    simonb 	if (error)
   2096       1.62    simonb 		goto done;
   2097       1.62    simonb 
   2098       1.62    simonb 	/*
   2099       1.62    simonb 	 * Now copy in the actual argument vector, one page at a time,
   2100       1.62    simonb 	 * since we don't know how long the vector is (though, we do
   2101       1.62    simonb 	 * know how many NUL-terminated strings are in the vector).
   2102       1.62    simonb 	 */
   2103       1.62    simonb 	len = 0;
   2104       1.62    simonb 	upper_bound = *sizep;
   2105       1.62    simonb 	for (; nargv != 0 && len < upper_bound; len += xlen) {
   2106       1.62    simonb 		aiov.iov_base = arg;
   2107       1.62    simonb 		aiov.iov_len = PAGE_SIZE;
   2108       1.62    simonb 		auio.uio_iov = &aiov;
   2109       1.62    simonb 		auio.uio_iovcnt = 1;
   2110       1.62    simonb 		auio.uio_offset = argv + len;
   2111       1.62    simonb 		xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
   2112       1.62    simonb 		auio.uio_resid = xlen;
   2113       1.62    simonb 		auio.uio_segflg = UIO_SYSSPACE;
   2114       1.62    simonb 		auio.uio_rw = UIO_READ;
   2115       1.62    simonb 		auio.uio_procp = NULL;
   2116       1.62    simonb 		error = uvm_io(&p->p_vmspace->vm_map, &auio);
   2117       1.62    simonb 		if (error)
   2118       1.62    simonb 			goto done;
   2119       1.62    simonb 
   2120       1.62    simonb 		for (i = 0; i < xlen && nargv != 0; i++) {
   2121       1.62    simonb 			if (arg[i] == '\0')
   2122       1.62    simonb 				nargv--;	/* one full string */
   2123       1.62    simonb 		}
   2124       1.62    simonb 
   2125  1.86.2.22   nathanw 		/*
   2126  1.86.2.22   nathanw 		 * Make sure we don't copyout past the end of the user's
   2127  1.86.2.22   nathanw 		 * buffer.
   2128  1.86.2.22   nathanw 		 */
   2129       1.62    simonb 		if (len + i > upper_bound)
   2130       1.62    simonb 			i = upper_bound - len;
   2131       1.62    simonb 
   2132       1.62    simonb 		error = copyout(arg, (char *)where + len, i);
   2133       1.62    simonb 		if (error)
   2134       1.62    simonb 			break;
   2135       1.62    simonb 
   2136       1.62    simonb 		if (nargv == 0) {
   2137       1.62    simonb 			len += i;
   2138       1.62    simonb 			break;
   2139       1.62    simonb 		}
   2140       1.62    simonb 	}
   2141       1.62    simonb 	*sizep = len;
   2142       1.62    simonb 
   2143       1.62    simonb done:
   2144       1.62    simonb 	uvmspace_free(p->p_vmspace);
   2145       1.62    simonb 
   2146       1.62    simonb 	free(arg, M_TEMP);
   2147       1.62    simonb 	return (error);
   2148        1.1       cgd }
   2149       1.78  jdolecek 
   2150       1.78  jdolecek #if NPTY > 0
   2151   1.86.2.6   nathanw int pty_maxptys(int, int);		/* defined in kern/tty_pty.c */
   2152       1.78  jdolecek 
   2153       1.78  jdolecek /*
   2154       1.78  jdolecek  * Validate parameters and get old / set new parameters
   2155       1.78  jdolecek  * for pty sysctl function.
   2156       1.78  jdolecek  */
   2157       1.78  jdolecek static int
   2158   1.86.2.6   nathanw sysctl_pty(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
   2159       1.78  jdolecek {
   2160       1.78  jdolecek 	int error = 0;
   2161       1.78  jdolecek 	int oldmax = 0, newmax = 0;
   2162       1.78  jdolecek 
   2163       1.78  jdolecek 	/* get current value of maxptys */
   2164       1.79   thorpej 	oldmax = pty_maxptys(0, 0);
   2165       1.78  jdolecek 
   2166       1.78  jdolecek 	SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &oldmax, int)
   2167       1.78  jdolecek 
   2168       1.78  jdolecek 	if (!error && newp) {
   2169       1.78  jdolecek 		SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
   2170       1.78  jdolecek 		SYSCTL_SCALAR_NEWPCOP_TYP(newp, &newmax, int)
   2171       1.78  jdolecek 
   2172       1.78  jdolecek 		if (newmax != pty_maxptys(newmax, (newp != NULL)))
   2173       1.78  jdolecek 			return (EINVAL);
   2174       1.78  jdolecek 
   2175       1.78  jdolecek 	}
   2176       1.78  jdolecek 
   2177       1.78  jdolecek 	return (error);
   2178       1.78  jdolecek }
   2179       1.78  jdolecek #endif /* NPTY > 0 */
   2180  1.86.2.12   nathanw 
   2181  1.86.2.12   nathanw static int
   2182  1.86.2.12   nathanw sysctl_dotkstat(name, namelen, where, sizep, newp)
   2183  1.86.2.12   nathanw 	int *name;
   2184  1.86.2.12   nathanw 	u_int namelen;
   2185  1.86.2.12   nathanw 	void *where;
   2186  1.86.2.12   nathanw 	size_t *sizep;
   2187  1.86.2.12   nathanw 	void *newp;
   2188  1.86.2.12   nathanw {
   2189  1.86.2.12   nathanw 	/* all sysctl names at this level are terminal */
   2190  1.86.2.12   nathanw 	if (namelen != 1)
   2191  1.86.2.12   nathanw 		return (ENOTDIR);		/* overloaded */
   2192  1.86.2.12   nathanw 
   2193  1.86.2.12   nathanw 	switch (name[0]) {
   2194  1.86.2.12   nathanw 	case KERN_TKSTAT_NIN:
   2195  1.86.2.12   nathanw 		return (sysctl_rdquad(where, sizep, newp, tk_nin));
   2196  1.86.2.12   nathanw 	case KERN_TKSTAT_NOUT:
   2197  1.86.2.12   nathanw 		return (sysctl_rdquad(where, sizep, newp, tk_nout));
   2198  1.86.2.12   nathanw 	case KERN_TKSTAT_CANCC:
   2199  1.86.2.12   nathanw 		return (sysctl_rdquad(where, sizep, newp, tk_cancc));
   2200  1.86.2.12   nathanw 	case KERN_TKSTAT_RAWCC:
   2201  1.86.2.12   nathanw 		return (sysctl_rdquad(where, sizep, newp, tk_rawcc));
   2202  1.86.2.12   nathanw 	default:
   2203  1.86.2.12   nathanw 		return (EOPNOTSUPP);
   2204  1.86.2.12   nathanw 	}
   2205  1.86.2.12   nathanw }
   2206