Home | History | Annotate | Line # | Download | only in kern
init_sysctl.c revision 1.96
      1 /*	$NetBSD: init_sysctl.c,v 1.96 2007/02/15 20:32:48 ad Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Brown.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by the NetBSD
     21  *      Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: init_sysctl.c,v 1.96 2007/02/15 20:32:48 ad Exp $");
     41 
     42 #include "opt_sysv.h"
     43 #include "opt_multiprocessor.h"
     44 #include "opt_posix.h"
     45 #include "opt_compat_netbsd32.h"
     46 #include "opt_ktrace.h"
     47 #include "pty.h"
     48 #include "rnd.h"
     49 
     50 #include <sys/types.h>
     51 #include <sys/param.h>
     52 #include <sys/sysctl.h>
     53 #include <sys/errno.h>
     54 #include <sys/systm.h>
     55 #include <sys/kernel.h>
     56 #include <sys/unistd.h>
     57 #include <sys/disklabel.h>
     58 #include <sys/rnd.h>
     59 #include <sys/vnode.h>
     60 #include <sys/mount.h>
     61 #include <sys/namei.h>
     62 #include <sys/msgbuf.h>
     63 #include <dev/cons.h>
     64 #include <sys/socketvar.h>
     65 #include <sys/file.h>
     66 #include <sys/filedesc.h>
     67 #include <sys/tty.h>
     68 #include <sys/malloc.h>
     69 #include <sys/resource.h>
     70 #include <sys/resourcevar.h>
     71 #include <sys/exec.h>
     72 #include <sys/conf.h>
     73 #include <sys/device.h>
     74 #include <sys/stat.h>
     75 #include <sys/kauth.h>
     76 #ifdef KTRACE
     77 #include <sys/ktrace.h>
     78 #endif
     79 
     80 #ifdef COMPAT_NETBSD32
     81 #include <compat/netbsd32/netbsd32.h>
     82 #endif
     83 
     84 #include <machine/cpu.h>
     85 
     86 /* XXX this should not be here */
     87 int security_setidcore_dump;
     88 char security_setidcore_path[MAXPATHLEN] = "/var/crash/%n.core";
     89 uid_t security_setidcore_owner = 0;
     90 gid_t security_setidcore_group = 0;
     91 mode_t security_setidcore_mode = (S_IRUSR|S_IWUSR);
     92 
     93 static const u_int sysctl_flagmap[] = {
     94 	P_ADVLOCK, KP_ADVLOCK,
     95 	P_EXEC, KP_EXEC,
     96 	P_NOCLDWAIT, KP_NOCLDWAIT,
     97 	P_32, KP_32,
     98 	P_CLDSIGIGN, KP_CLDSIGIGN,
     99 	P_PAXMPROTECT, KP_PAXMPROTECT,
    100 	P_PAXNOMPROTECT, KP_PAXNOMPROTECT,
    101 	P_SYSTRACE, KP_SYSTRACE,
    102 	P_SUGID, KP_SUGID,
    103 	0
    104 };
    105 
    106 static const u_int sysctl_sflagmap[] = {
    107 	PS_NOCLDSTOP, KP_NOCLDSTOP,
    108 	PS_PPWAIT, KP_PPWAIT,
    109 	PS_WEXIT, KP_WEXIT,
    110 	PS_STOPFORK, KP_STOPFORK,
    111 	PS_STOPEXEC, KP_STOPEXEC,
    112 	PS_STOPEXIT, KP_STOPEXIT,
    113 	0
    114 };
    115 
    116 static const u_int sysctl_slflagmap[] = {
    117 	PSL_TRACED, KP_TRACED,
    118 	PSL_FSTRACE, KP_FSTRACE,
    119 	PSL_CHTRACED, KP_CHTRACED,
    120 	PSL_SYSCALL, KP_SYSCALL,
    121 	0
    122 };
    123 
    124 static const u_int sysctl_lflagmap[] = {
    125 	PL_CONTROLT, KP_CONTROLT,
    126 	0
    127 };
    128 
    129 static const u_int sysctl_stflagmap[] = {
    130 	PST_PROFIL, KP_PROFIL,
    131 	0
    132 
    133 };
    134 
    135 static const u_int sysctl_lwpflagmap[] = {
    136 	L_INMEM, KP_INMEM,
    137 	L_SELECT, KP_SELECT,
    138 	L_SINTR, KP_SINTR,
    139 	L_SYSTEM, KP_SYSTEM,
    140 	0
    141 };
    142 
    143 static const u_int sysctl_lwpprflagmap[] = {
    144 	LPR_DETACHED, KL_DETACHED,
    145 	0
    146 };
    147 
    148 
    149 /*
    150  * try over estimating by 5 procs/lwps
    151  */
    152 #define KERN_PROCSLOP	(5 * sizeof(struct kinfo_proc))
    153 #define KERN_LWPSLOP	(5 * sizeof(struct kinfo_lwp))
    154 
    155 #ifdef KTRACE
    156 int dcopyout(struct lwp *, const void *, void *, size_t);
    157 
    158 int
    159 dcopyout(l, kaddr, uaddr, len)
    160 	struct lwp *l;
    161 	const void *kaddr;
    162 	void *uaddr;
    163 	size_t len;
    164 {
    165 	int error;
    166 
    167 	error = copyout(kaddr, uaddr, len);
    168 	if (!error && KTRPOINT(l->l_proc, KTR_MIB)) {
    169 		struct iovec iov;
    170 
    171 		iov.iov_base = uaddr;
    172 		iov.iov_len = len;
    173 		ktrgenio(l, -1, UIO_READ, &iov, len, 0);
    174 	}
    175 	return error;
    176 }
    177 #else /* !KTRACE */
    178 #define dcopyout(l, kaddr, uaddr, len) copyout(kaddr, uaddr, len)
    179 #endif /* KTRACE */
    180 
    181 #ifndef CPU_INFO_FOREACH
    182 #define CPU_INFO_ITERATOR int
    183 #define CPU_INFO_FOREACH(cii, ci) cii = 0, ci = curcpu(); ci != NULL; ci = NULL
    184 #endif
    185 
    186 #ifdef DIAGNOSTIC
    187 static int sysctl_kern_trigger_panic(SYSCTLFN_PROTO);
    188 #endif
    189 static int sysctl_kern_maxvnodes(SYSCTLFN_PROTO);
    190 static int sysctl_kern_rtc_offset(SYSCTLFN_PROTO);
    191 static int sysctl_kern_maxproc(SYSCTLFN_PROTO);
    192 static int sysctl_kern_hostid(SYSCTLFN_PROTO);
    193 static int sysctl_setlen(SYSCTLFN_PROTO);
    194 static int sysctl_kern_clockrate(SYSCTLFN_PROTO);
    195 static int sysctl_kern_file(SYSCTLFN_PROTO);
    196 static int sysctl_kern_autonice(SYSCTLFN_PROTO);
    197 static int sysctl_msgbuf(SYSCTLFN_PROTO);
    198 static int sysctl_kern_defcorename(SYSCTLFN_PROTO);
    199 static int sysctl_kern_cptime(SYSCTLFN_PROTO);
    200 #if NPTY > 0
    201 static int sysctl_kern_maxptys(SYSCTLFN_PROTO);
    202 #endif /* NPTY > 0 */
    203 static int sysctl_kern_sbmax(SYSCTLFN_PROTO);
    204 static int sysctl_kern_urnd(SYSCTLFN_PROTO);
    205 static int sysctl_kern_arnd(SYSCTLFN_PROTO);
    206 static int sysctl_kern_lwp(SYSCTLFN_PROTO);
    207 static int sysctl_kern_forkfsleep(SYSCTLFN_PROTO);
    208 static int sysctl_kern_root_partition(SYSCTLFN_PROTO);
    209 static int sysctl_kern_drivers(SYSCTLFN_PROTO);
    210 static int sysctl_kern_file2(SYSCTLFN_PROTO);
    211 static int sysctl_security_setidcore(SYSCTLFN_PROTO);
    212 static int sysctl_security_setidcorename(SYSCTLFN_PROTO);
    213 static int sysctl_kern_cpid(SYSCTLFN_PROTO);
    214 static int sysctl_doeproc(SYSCTLFN_PROTO);
    215 static int sysctl_kern_proc_args(SYSCTLFN_PROTO);
    216 static int sysctl_hw_usermem(SYSCTLFN_PROTO);
    217 static int sysctl_hw_cnmagic(SYSCTLFN_PROTO);
    218 static int sysctl_hw_ncpu(SYSCTLFN_PROTO);
    219 
    220 static u_int sysctl_map_flags(const u_int *, u_int);
    221 static void fill_kproc2(struct proc *, struct kinfo_proc2 *);
    222 static void fill_lwp(struct lwp *l, struct kinfo_lwp *kl);
    223 static void fill_file(struct kinfo_file *, const struct file *, struct proc *,
    224 		      int);
    225 
    226 /*
    227  * ********************************************************************
    228  * section 1: setup routines
    229  * ********************************************************************
    230  * these functions are stuffed into a link set for sysctl setup
    231  * functions.  they're never called or referenced from anywhere else.
    232  * ********************************************************************
    233  */
    234 
    235 /*
    236  * sets up the base nodes...
    237  */
    238 SYSCTL_SETUP(sysctl_root_setup, "sysctl base setup")
    239 {
    240 
    241 	sysctl_createv(clog, 0, NULL, NULL,
    242 		       CTLFLAG_PERMANENT,
    243 		       CTLTYPE_NODE, "kern",
    244 		       SYSCTL_DESCR("High kernel"),
    245 		       NULL, 0, NULL, 0,
    246 		       CTL_KERN, CTL_EOL);
    247 	sysctl_createv(clog, 0, NULL, NULL,
    248 		       CTLFLAG_PERMANENT,
    249 		       CTLTYPE_NODE, "vm",
    250 		       SYSCTL_DESCR("Virtual memory"),
    251 		       NULL, 0, NULL, 0,
    252 		       CTL_VM, CTL_EOL);
    253 	sysctl_createv(clog, 0, NULL, NULL,
    254 		       CTLFLAG_PERMANENT,
    255 		       CTLTYPE_NODE, "vfs",
    256 		       SYSCTL_DESCR("Filesystem"),
    257 		       NULL, 0, NULL, 0,
    258 		       CTL_VFS, CTL_EOL);
    259 	sysctl_createv(clog, 0, NULL, NULL,
    260 		       CTLFLAG_PERMANENT,
    261 		       CTLTYPE_NODE, "net",
    262 		       SYSCTL_DESCR("Networking"),
    263 		       NULL, 0, NULL, 0,
    264 		       CTL_NET, CTL_EOL);
    265 	sysctl_createv(clog, 0, NULL, NULL,
    266 		       CTLFLAG_PERMANENT,
    267 		       CTLTYPE_NODE, "debug",
    268 		       SYSCTL_DESCR("Debugging"),
    269 		       NULL, 0, NULL, 0,
    270 		       CTL_DEBUG, CTL_EOL);
    271 	sysctl_createv(clog, 0, NULL, NULL,
    272 		       CTLFLAG_PERMANENT,
    273 		       CTLTYPE_NODE, "hw",
    274 		       SYSCTL_DESCR("Generic CPU, I/O"),
    275 		       NULL, 0, NULL, 0,
    276 		       CTL_HW, CTL_EOL);
    277 	sysctl_createv(clog, 0, NULL, NULL,
    278 		       CTLFLAG_PERMANENT,
    279 		       CTLTYPE_NODE, "machdep",
    280 		       SYSCTL_DESCR("Machine dependent"),
    281 		       NULL, 0, NULL, 0,
    282 		       CTL_MACHDEP, CTL_EOL);
    283 	/*
    284 	 * this node is inserted so that the sysctl nodes in libc can
    285 	 * operate.
    286 	 */
    287 	sysctl_createv(clog, 0, NULL, NULL,
    288 		       CTLFLAG_PERMANENT,
    289 		       CTLTYPE_NODE, "user",
    290 		       SYSCTL_DESCR("User-level"),
    291 		       NULL, 0, NULL, 0,
    292 		       CTL_USER, CTL_EOL);
    293 	sysctl_createv(clog, 0, NULL, NULL,
    294 		       CTLFLAG_PERMANENT,
    295 		       CTLTYPE_NODE, "ddb",
    296 		       SYSCTL_DESCR("In-kernel debugger"),
    297 		       NULL, 0, NULL, 0,
    298 		       CTL_DDB, CTL_EOL);
    299 	sysctl_createv(clog, 0, NULL, NULL,
    300 		       CTLFLAG_PERMANENT,
    301 		       CTLTYPE_NODE, "proc",
    302 		       SYSCTL_DESCR("Per-process"),
    303 		       NULL, 0, NULL, 0,
    304 		       CTL_PROC, CTL_EOL);
    305 	sysctl_createv(clog, 0, NULL, NULL,
    306 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    307 		       CTLTYPE_NODE, "vendor",
    308 		       SYSCTL_DESCR("Vendor specific"),
    309 		       NULL, 0, NULL, 0,
    310 		       CTL_VENDOR, CTL_EOL);
    311 	sysctl_createv(clog, 0, NULL, NULL,
    312 		       CTLFLAG_PERMANENT,
    313 		       CTLTYPE_NODE, "emul",
    314 		       SYSCTL_DESCR("Emulation settings"),
    315 		       NULL, 0, NULL, 0,
    316 		       CTL_EMUL, CTL_EOL);
    317 	sysctl_createv(clog, 0, NULL, NULL,
    318 		       CTLFLAG_PERMANENT,
    319 		       CTLTYPE_NODE, "security",
    320 		       SYSCTL_DESCR("Security"),
    321 		       NULL, 0, NULL, 0,
    322 		       CTL_SECURITY, CTL_EOL);
    323 }
    324 
    325 /*
    326  * this setup routine is a replacement for kern_sysctl()
    327  */
    328 SYSCTL_SETUP(sysctl_kern_setup, "sysctl kern subtree setup")
    329 {
    330 	extern int kern_logsigexit;	/* defined in kern/kern_sig.c */
    331 	extern fixpt_t ccpu;		/* defined in kern/kern_synch.c */
    332 	extern int dumponpanic;		/* defined in kern/subr_prf.c */
    333 	const struct sysctlnode *rnode;
    334 
    335 	sysctl_createv(clog, 0, NULL, NULL,
    336 		       CTLFLAG_PERMANENT,
    337 		       CTLTYPE_NODE, "kern", NULL,
    338 		       NULL, 0, NULL, 0,
    339 		       CTL_KERN, CTL_EOL);
    340 
    341 	sysctl_createv(clog, 0, NULL, NULL,
    342 		       CTLFLAG_PERMANENT,
    343 		       CTLTYPE_STRING, "ostype",
    344 		       SYSCTL_DESCR("Operating system type"),
    345 		       NULL, 0, &ostype, 0,
    346 		       CTL_KERN, KERN_OSTYPE, CTL_EOL);
    347 	sysctl_createv(clog, 0, NULL, NULL,
    348 		       CTLFLAG_PERMANENT,
    349 		       CTLTYPE_STRING, "osrelease",
    350 		       SYSCTL_DESCR("Operating system release"),
    351 		       NULL, 0, &osrelease, 0,
    352 		       CTL_KERN, KERN_OSRELEASE, CTL_EOL);
    353 	sysctl_createv(clog, 0, NULL, NULL,
    354 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    355 		       CTLTYPE_INT, "osrevision",
    356 		       SYSCTL_DESCR("Operating system revision"),
    357 		       NULL, __NetBSD_Version__, NULL, 0,
    358 		       CTL_KERN, KERN_OSREV, CTL_EOL);
    359 	sysctl_createv(clog, 0, NULL, NULL,
    360 		       CTLFLAG_PERMANENT,
    361 		       CTLTYPE_STRING, "version",
    362 		       SYSCTL_DESCR("Kernel version"),
    363 		       NULL, 0, &version, 0,
    364 		       CTL_KERN, KERN_VERSION, CTL_EOL);
    365 	sysctl_createv(clog, 0, NULL, NULL,
    366 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    367 		       CTLTYPE_INT, "maxvnodes",
    368 		       SYSCTL_DESCR("Maximum number of vnodes"),
    369 		       sysctl_kern_maxvnodes, 0, NULL, 0,
    370 		       CTL_KERN, KERN_MAXVNODES, CTL_EOL);
    371 	sysctl_createv(clog, 0, NULL, NULL,
    372 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    373 		       CTLTYPE_INT, "maxproc",
    374 		       SYSCTL_DESCR("Maximum number of simultaneous processes"),
    375 		       sysctl_kern_maxproc, 0, NULL, 0,
    376 		       CTL_KERN, KERN_MAXPROC, CTL_EOL);
    377 	sysctl_createv(clog, 0, NULL, NULL,
    378 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    379 		       CTLTYPE_INT, "maxfiles",
    380 		       SYSCTL_DESCR("Maximum number of open files"),
    381 		       NULL, 0, &maxfiles, 0,
    382 		       CTL_KERN, KERN_MAXFILES, CTL_EOL);
    383 	sysctl_createv(clog, 0, NULL, NULL,
    384 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    385 		       CTLTYPE_INT, "argmax",
    386 		       SYSCTL_DESCR("Maximum number of bytes of arguments to "
    387 				    "execve(2)"),
    388 		       NULL, ARG_MAX, NULL, 0,
    389 		       CTL_KERN, KERN_ARGMAX, CTL_EOL);
    390 	sysctl_createv(clog, 0, NULL, NULL,
    391 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    392 		       CTLTYPE_STRING, "hostname",
    393 		       SYSCTL_DESCR("System hostname"),
    394 		       sysctl_setlen, 0, &hostname, MAXHOSTNAMELEN,
    395 		       CTL_KERN, KERN_HOSTNAME, CTL_EOL);
    396 	sysctl_createv(clog, 0, NULL, NULL,
    397 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
    398 		       CTLTYPE_INT, "hostid",
    399 		       SYSCTL_DESCR("System host ID number"),
    400 		       sysctl_kern_hostid, 0, NULL, 0,
    401 		       CTL_KERN, KERN_HOSTID, CTL_EOL);
    402 	sysctl_createv(clog, 0, NULL, NULL,
    403 		       CTLFLAG_PERMANENT,
    404 		       CTLTYPE_STRUCT, "clockrate",
    405 		       SYSCTL_DESCR("Kernel clock rates"),
    406 		       sysctl_kern_clockrate, 0, NULL,
    407 		       sizeof(struct clockinfo),
    408 		       CTL_KERN, KERN_CLOCKRATE, CTL_EOL);
    409 	sysctl_createv(clog, 0, NULL, NULL,
    410 		       CTLFLAG_PERMANENT,
    411 		       CTLTYPE_INT, "hardclock_ticks",
    412 		       SYSCTL_DESCR("Number of hardclock ticks"),
    413 		       NULL, 0, &hardclock_ticks, sizeof(hardclock_ticks),
    414 		       CTL_KERN, KERN_HARDCLOCK_TICKS, CTL_EOL);
    415 	sysctl_createv(clog, 0, NULL, NULL,
    416 		       CTLFLAG_PERMANENT,
    417 		       CTLTYPE_STRUCT, "vnode",
    418 		       SYSCTL_DESCR("System vnode table"),
    419 		       sysctl_kern_vnode, 0, NULL, 0,
    420 		       CTL_KERN, KERN_VNODE, CTL_EOL);
    421 	sysctl_createv(clog, 0, NULL, NULL,
    422 		       CTLFLAG_PERMANENT,
    423 		       CTLTYPE_STRUCT, "file",
    424 		       SYSCTL_DESCR("System open file table"),
    425 		       sysctl_kern_file, 0, NULL, 0,
    426 		       CTL_KERN, KERN_FILE, CTL_EOL);
    427 #ifndef GPROF
    428 	sysctl_createv(clog, 0, NULL, NULL,
    429 		       CTLFLAG_PERMANENT,
    430 		       CTLTYPE_NODE, "profiling",
    431 		       SYSCTL_DESCR("Profiling information (not available)"),
    432 		       sysctl_notavail, 0, NULL, 0,
    433 		       CTL_KERN, KERN_PROF, CTL_EOL);
    434 #endif
    435 	sysctl_createv(clog, 0, NULL, NULL,
    436 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    437 		       CTLTYPE_INT, "posix1version",
    438 		       SYSCTL_DESCR("Version of ISO/IEC 9945 (POSIX 1003.1) "
    439 				    "with which the operating system attempts "
    440 				    "to comply"),
    441 		       NULL, _POSIX_VERSION, NULL, 0,
    442 		       CTL_KERN, KERN_POSIX1, CTL_EOL);
    443 	sysctl_createv(clog, 0, NULL, NULL,
    444 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    445 		       CTLTYPE_INT, "ngroups",
    446 		       SYSCTL_DESCR("Maximum number of supplemental groups"),
    447 		       NULL, NGROUPS_MAX, NULL, 0,
    448 		       CTL_KERN, KERN_NGROUPS, CTL_EOL);
    449 	sysctl_createv(clog, 0, NULL, NULL,
    450 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    451 		       CTLTYPE_INT, "job_control",
    452 		       SYSCTL_DESCR("Whether job control is available"),
    453 		       NULL, 1, NULL, 0,
    454 		       CTL_KERN, KERN_JOB_CONTROL, CTL_EOL);
    455 	sysctl_createv(clog, 0, NULL, NULL,
    456 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    457 		       CTLTYPE_INT, "saved_ids",
    458 		       SYSCTL_DESCR("Whether POSIX saved set-group/user ID is "
    459 				    "available"), NULL,
    460 #ifdef _POSIX_SAVED_IDS
    461 		       1,
    462 #else /* _POSIX_SAVED_IDS */
    463 		       0,
    464 #endif /* _POSIX_SAVED_IDS */
    465 		       NULL, 0, CTL_KERN, KERN_SAVED_IDS, CTL_EOL);
    466 	sysctl_createv(clog, 0, NULL, NULL,
    467 		       CTLFLAG_PERMANENT,
    468 		       CTLTYPE_STRUCT, "boottime",
    469 		       SYSCTL_DESCR("System boot time"),
    470 		       NULL, 0, &boottime, sizeof(boottime),
    471 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
    472 	sysctl_createv(clog, 0, NULL, NULL,
    473 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    474 		       CTLTYPE_STRING, "domainname",
    475 		       SYSCTL_DESCR("YP domain name"),
    476 		       sysctl_setlen, 0, &domainname, MAXHOSTNAMELEN,
    477 		       CTL_KERN, KERN_DOMAINNAME, CTL_EOL);
    478 	sysctl_createv(clog, 0, NULL, NULL,
    479 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    480 		       CTLTYPE_INT, "maxpartitions",
    481 		       SYSCTL_DESCR("Maximum number of partitions allowed per "
    482 				    "disk"),
    483 		       NULL, MAXPARTITIONS, NULL, 0,
    484 		       CTL_KERN, KERN_MAXPARTITIONS, CTL_EOL);
    485 	sysctl_createv(clog, 0, NULL, NULL,
    486 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    487 		       CTLTYPE_INT, "rawpartition",
    488 		       SYSCTL_DESCR("Raw partition of a disk"),
    489 		       NULL, RAW_PART, NULL, 0,
    490 		       CTL_KERN, KERN_RAWPARTITION, CTL_EOL);
    491 	sysctl_createv(clog, 0, NULL, NULL,
    492 		       CTLFLAG_PERMANENT,
    493 		       CTLTYPE_STRUCT, "timex", NULL,
    494 		       sysctl_notavail, 0, NULL, 0,
    495 		       CTL_KERN, KERN_TIMEX, CTL_EOL);
    496 	sysctl_createv(clog, 0, NULL, NULL,
    497 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    498 		       CTLTYPE_INT, "autonicetime",
    499 		       SYSCTL_DESCR("CPU clock seconds before non-root "
    500 				    "process priority is lowered"),
    501 		       sysctl_kern_autonice, 0, &autonicetime, 0,
    502 		       CTL_KERN, KERN_AUTONICETIME, CTL_EOL);
    503 	sysctl_createv(clog, 0, NULL, NULL,
    504 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    505 		       CTLTYPE_INT, "autoniceval",
    506 		       SYSCTL_DESCR("Automatic reniced non-root process "
    507 				    "priority"),
    508 		       sysctl_kern_autonice, 0, &autoniceval, 0,
    509 		       CTL_KERN, KERN_AUTONICEVAL, CTL_EOL);
    510 	sysctl_createv(clog, 0, NULL, NULL,
    511 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    512 		       CTLTYPE_INT, "rtc_offset",
    513 		       SYSCTL_DESCR("Offset of real time clock from UTC in "
    514 				    "minutes"),
    515 		       sysctl_kern_rtc_offset, 0, &rtc_offset, 0,
    516 		       CTL_KERN, KERN_RTC_OFFSET, CTL_EOL);
    517 	sysctl_createv(clog, 0, NULL, NULL,
    518 		       CTLFLAG_PERMANENT,
    519 		       CTLTYPE_STRING, "root_device",
    520 		       SYSCTL_DESCR("Name of the root device"),
    521 		       sysctl_root_device, 0, NULL, 0,
    522 		       CTL_KERN, KERN_ROOT_DEVICE, CTL_EOL);
    523 	sysctl_createv(clog, 0, NULL, NULL,
    524 		       CTLFLAG_PERMANENT,
    525 		       CTLTYPE_INT, "msgbufsize",
    526 		       SYSCTL_DESCR("Size of the kernel message buffer"),
    527 		       sysctl_msgbuf, 0, NULL, 0,
    528 		       CTL_KERN, KERN_MSGBUFSIZE, CTL_EOL);
    529 	sysctl_createv(clog, 0, NULL, NULL,
    530 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    531 		       CTLTYPE_INT, "fsync",
    532 		       SYSCTL_DESCR("Whether the POSIX 1003.1b File "
    533 				    "Synchronization Option is available on "
    534 				    "this system"),
    535 		       NULL, 1, NULL, 0,
    536 		       CTL_KERN, KERN_FSYNC, CTL_EOL);
    537 	sysctl_createv(clog, 0, NULL, NULL,
    538 		       CTLFLAG_PERMANENT,
    539 		       CTLTYPE_NODE, "ipc",
    540 		       SYSCTL_DESCR("SysV IPC options"),
    541 		       NULL, 0, NULL, 0,
    542 		       CTL_KERN, KERN_SYSVIPC, CTL_EOL);
    543 	sysctl_createv(clog, 0, NULL, NULL,
    544 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    545 		       CTLTYPE_INT, "sysvmsg",
    546 		       SYSCTL_DESCR("System V style message support available"),
    547 		       NULL,
    548 #ifdef SYSVMSG
    549 		       1,
    550 #else /* SYSVMSG */
    551 		       0,
    552 #endif /* SYSVMSG */
    553 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_MSG, CTL_EOL);
    554 	sysctl_createv(clog, 0, NULL, NULL,
    555 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    556 		       CTLTYPE_INT, "sysvsem",
    557 		       SYSCTL_DESCR("System V style semaphore support "
    558 				    "available"), NULL,
    559 #ifdef SYSVSEM
    560 		       1,
    561 #else /* SYSVSEM */
    562 		       0,
    563 #endif /* SYSVSEM */
    564 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SEM, CTL_EOL);
    565 	sysctl_createv(clog, 0, NULL, NULL,
    566 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    567 		       CTLTYPE_INT, "sysvshm",
    568 		       SYSCTL_DESCR("System V style shared memory support "
    569 				    "available"), NULL,
    570 #ifdef SYSVSHM
    571 		       1,
    572 #else /* SYSVSHM */
    573 		       0,
    574 #endif /* SYSVSHM */
    575 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHM, CTL_EOL);
    576 	sysctl_createv(clog, 0, NULL, NULL,
    577 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    578 		       CTLTYPE_INT, "synchronized_io",
    579 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Synchronized "
    580 				    "I/O Option is available on this system"),
    581 		       NULL, 1, NULL, 0,
    582 		       CTL_KERN, KERN_SYNCHRONIZED_IO, CTL_EOL);
    583 	sysctl_createv(clog, 0, NULL, NULL,
    584 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    585 		       CTLTYPE_INT, "iov_max",
    586 		       SYSCTL_DESCR("Maximum number of iovec structures per "
    587 				    "process"),
    588 		       NULL, IOV_MAX, NULL, 0,
    589 		       CTL_KERN, KERN_IOV_MAX, CTL_EOL);
    590 	sysctl_createv(clog, 0, NULL, NULL,
    591 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    592 		       CTLTYPE_INT, "mapped_files",
    593 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory Mapped "
    594 				    "Files Option is available on this system"),
    595 		       NULL, 1, NULL, 0,
    596 		       CTL_KERN, KERN_MAPPED_FILES, CTL_EOL);
    597 	sysctl_createv(clog, 0, NULL, NULL,
    598 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    599 		       CTLTYPE_INT, "memlock",
    600 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Process Memory "
    601 				    "Locking Option is available on this "
    602 				    "system"),
    603 		       NULL, 1, NULL, 0,
    604 		       CTL_KERN, KERN_MEMLOCK, CTL_EOL);
    605 	sysctl_createv(clog, 0, NULL, NULL,
    606 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    607 		       CTLTYPE_INT, "memlock_range",
    608 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Range Memory "
    609 				    "Locking Option is available on this "
    610 				    "system"),
    611 		       NULL, 1, NULL, 0,
    612 		       CTL_KERN, KERN_MEMLOCK_RANGE, CTL_EOL);
    613 	sysctl_createv(clog, 0, NULL, NULL,
    614 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    615 		       CTLTYPE_INT, "memory_protection",
    616 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory "
    617 				    "Protection Option is available on this "
    618 				    "system"),
    619 		       NULL, 1, NULL, 0,
    620 		       CTL_KERN, KERN_MEMORY_PROTECTION, CTL_EOL);
    621 	sysctl_createv(clog, 0, NULL, NULL,
    622 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    623 		       CTLTYPE_INT, "login_name_max",
    624 		       SYSCTL_DESCR("Maximum login name length"),
    625 		       NULL, LOGIN_NAME_MAX, NULL, 0,
    626 		       CTL_KERN, KERN_LOGIN_NAME_MAX, CTL_EOL);
    627 	sysctl_createv(clog, 0, NULL, NULL,
    628 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    629 		       CTLTYPE_STRING, "defcorename",
    630 		       SYSCTL_DESCR("Default core file name"),
    631 		       sysctl_kern_defcorename, 0, defcorename, MAXPATHLEN,
    632 		       CTL_KERN, KERN_DEFCORENAME, CTL_EOL);
    633 	sysctl_createv(clog, 0, NULL, NULL,
    634 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    635 		       CTLTYPE_INT, "logsigexit",
    636 		       SYSCTL_DESCR("Log process exit when caused by signals"),
    637 		       NULL, 0, &kern_logsigexit, 0,
    638 		       CTL_KERN, KERN_LOGSIGEXIT, CTL_EOL);
    639 	sysctl_createv(clog, 0, NULL, NULL,
    640 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    641 		       CTLTYPE_INT, "fscale",
    642 		       SYSCTL_DESCR("Kernel fixed-point scale factor"),
    643 		       NULL, FSCALE, NULL, 0,
    644 		       CTL_KERN, KERN_FSCALE, CTL_EOL);
    645 	sysctl_createv(clog, 0, NULL, NULL,
    646 		       CTLFLAG_PERMANENT,
    647 		       CTLTYPE_INT, "ccpu",
    648 		       SYSCTL_DESCR("Scheduler exponential decay value"),
    649 		       NULL, 0, &ccpu, 0,
    650 		       CTL_KERN, KERN_CCPU, CTL_EOL);
    651 	sysctl_createv(clog, 0, NULL, NULL,
    652 		       CTLFLAG_PERMANENT,
    653 		       CTLTYPE_STRUCT, "cp_time",
    654 		       SYSCTL_DESCR("Clock ticks spent in different CPU states"),
    655 		       sysctl_kern_cptime, 0, NULL, 0,
    656 		       CTL_KERN, KERN_CP_TIME, CTL_EOL);
    657 	sysctl_createv(clog, 0, NULL, NULL,
    658 		       CTLFLAG_PERMANENT,
    659 		       CTLTYPE_INT, "msgbuf",
    660 		       SYSCTL_DESCR("Kernel message buffer"),
    661 		       sysctl_msgbuf, 0, NULL, 0,
    662 		       CTL_KERN, KERN_MSGBUF, CTL_EOL);
    663 	sysctl_createv(clog, 0, NULL, NULL,
    664 		       CTLFLAG_PERMANENT,
    665 		       CTLTYPE_STRUCT, "consdev",
    666 		       SYSCTL_DESCR("Console device"),
    667 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
    668 		       CTL_KERN, KERN_CONSDEV, CTL_EOL);
    669 #if NPTY > 0
    670 	sysctl_createv(clog, 0, NULL, NULL,
    671 		       CTLFLAG_PERMANENT,
    672 		       CTLTYPE_INT, "maxptys",
    673 		       SYSCTL_DESCR("Maximum number of pseudo-ttys"),
    674 		       sysctl_kern_maxptys, 0, NULL, 0,
    675 		       CTL_KERN, KERN_MAXPTYS, CTL_EOL);
    676 #endif /* NPTY > 0 */
    677 	sysctl_createv(clog, 0, NULL, NULL,
    678 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    679 		       CTLTYPE_INT, "maxphys",
    680 		       SYSCTL_DESCR("Maximum raw I/O transfer size"),
    681 		       NULL, MAXPHYS, NULL, 0,
    682 		       CTL_KERN, KERN_MAXPHYS, CTL_EOL);
    683 	sysctl_createv(clog, 0, NULL, NULL,
    684 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    685 		       CTLTYPE_INT, "sbmax",
    686 		       SYSCTL_DESCR("Maximum socket buffer size"),
    687 		       sysctl_kern_sbmax, 0, NULL, 0,
    688 		       CTL_KERN, KERN_SBMAX, CTL_EOL);
    689 	sysctl_createv(clog, 0, NULL, NULL,
    690 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    691 		       CTLTYPE_INT, "monotonic_clock",
    692 		       SYSCTL_DESCR("Implementation version of the POSIX "
    693 				    "1003.1b Monotonic Clock Option"),
    694 		       /* XXX _POSIX_VERSION */
    695 		       NULL, _POSIX_MONOTONIC_CLOCK, NULL, 0,
    696 		       CTL_KERN, KERN_MONOTONIC_CLOCK, CTL_EOL);
    697 	sysctl_createv(clog, 0, NULL, NULL,
    698 		       CTLFLAG_PERMANENT,
    699 		       CTLTYPE_INT, "urandom",
    700 		       SYSCTL_DESCR("Random integer value"),
    701 		       sysctl_kern_urnd, 0, NULL, 0,
    702 		       CTL_KERN, KERN_URND, CTL_EOL);
    703 	sysctl_createv(clog, 0, NULL, NULL,
    704 		       CTLFLAG_PERMANENT,
    705 		       CTLTYPE_INT, "arandom",
    706 		       SYSCTL_DESCR("n bytes of random data"),
    707 		       sysctl_kern_arnd, 0, NULL, 0,
    708 		       CTL_KERN, KERN_ARND, CTL_EOL);
    709 	sysctl_createv(clog, 0, NULL, NULL,
    710 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    711 		       CTLTYPE_INT, "labelsector",
    712 		       SYSCTL_DESCR("Sector number containing the disklabel"),
    713 		       NULL, LABELSECTOR, NULL, 0,
    714 		       CTL_KERN, KERN_LABELSECTOR, CTL_EOL);
    715 	sysctl_createv(clog, 0, NULL, NULL,
    716 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    717 		       CTLTYPE_INT, "labeloffset",
    718 		       SYSCTL_DESCR("Offset of the disklabel within the "
    719 				    "sector"),
    720 		       NULL, LABELOFFSET, NULL, 0,
    721 		       CTL_KERN, KERN_LABELOFFSET, CTL_EOL);
    722 	sysctl_createv(clog, 0, NULL, NULL,
    723 		       CTLFLAG_PERMANENT,
    724 		       CTLTYPE_NODE, "lwp",
    725 		       SYSCTL_DESCR("System-wide LWP information"),
    726 		       sysctl_kern_lwp, 0, NULL, 0,
    727 		       CTL_KERN, KERN_LWP, CTL_EOL);
    728 	sysctl_createv(clog, 0, NULL, NULL,
    729 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    730 		       CTLTYPE_INT, "forkfsleep",
    731 		       SYSCTL_DESCR("Milliseconds to sleep on fork failure due "
    732 				    "to process limits"),
    733 		       sysctl_kern_forkfsleep, 0, NULL, 0,
    734 		       CTL_KERN, KERN_FORKFSLEEP, CTL_EOL);
    735 	sysctl_createv(clog, 0, NULL, NULL,
    736 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    737 		       CTLTYPE_INT, "posix_threads",
    738 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    739 				    "Threads option to which the system "
    740 				    "attempts to conform"),
    741 		       /* XXX _POSIX_VERSION */
    742 		       NULL, _POSIX_THREADS, NULL, 0,
    743 		       CTL_KERN, KERN_POSIX_THREADS, CTL_EOL);
    744 	sysctl_createv(clog, 0, NULL, NULL,
    745 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    746 		       CTLTYPE_INT, "posix_semaphores",
    747 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    748 				    "Semaphores option to which the system "
    749 				    "attempts to conform"), NULL,
    750 #ifdef P1003_1B_SEMAPHORE
    751 		       200112,
    752 #else /* P1003_1B_SEMAPHORE */
    753 		       0,
    754 #endif /* P1003_1B_SEMAPHORE */
    755 		       NULL, 0, CTL_KERN, KERN_POSIX_SEMAPHORES, CTL_EOL);
    756 	sysctl_createv(clog, 0, NULL, NULL,
    757 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    758 		       CTLTYPE_INT, "posix_barriers",
    759 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    760 				    "Barriers option to which the system "
    761 				    "attempts to conform"),
    762 		       /* XXX _POSIX_VERSION */
    763 		       NULL, _POSIX_BARRIERS, NULL, 0,
    764 		       CTL_KERN, KERN_POSIX_BARRIERS, CTL_EOL);
    765 	sysctl_createv(clog, 0, NULL, NULL,
    766 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    767 		       CTLTYPE_INT, "posix_timers",
    768 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    769 				    "Timers option to which the system "
    770 				    "attempts to conform"),
    771 		       /* XXX _POSIX_VERSION */
    772 		       NULL, _POSIX_TIMERS, NULL, 0,
    773 		       CTL_KERN, KERN_POSIX_TIMERS, CTL_EOL);
    774 	sysctl_createv(clog, 0, NULL, NULL,
    775 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    776 		       CTLTYPE_INT, "posix_spin_locks",
    777 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its Spin "
    778 				    "Locks option to which the system attempts "
    779 				    "to conform"),
    780 		       /* XXX _POSIX_VERSION */
    781 		       NULL, _POSIX_SPIN_LOCKS, NULL, 0,
    782 		       CTL_KERN, KERN_POSIX_SPIN_LOCKS, CTL_EOL);
    783 	sysctl_createv(clog, 0, NULL, NULL,
    784 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    785 		       CTLTYPE_INT, "posix_reader_writer_locks",
    786 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    787 				    "Read-Write Locks option to which the "
    788 				    "system attempts to conform"),
    789 		       /* XXX _POSIX_VERSION */
    790 		       NULL, _POSIX_READER_WRITER_LOCKS, NULL, 0,
    791 		       CTL_KERN, KERN_POSIX_READER_WRITER_LOCKS, CTL_EOL);
    792 	sysctl_createv(clog, 0, NULL, NULL,
    793 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    794 		       CTLTYPE_INT, "dump_on_panic",
    795 		       SYSCTL_DESCR("Perform a crash dump on system panic"),
    796 		       NULL, 0, &dumponpanic, 0,
    797 		       CTL_KERN, KERN_DUMP_ON_PANIC, CTL_EOL);
    798 #ifdef DIAGNOSTIC
    799 	sysctl_createv(clog, 0, NULL, NULL,
    800 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    801 		       CTLTYPE_INT, "panic_now",
    802 		       SYSCTL_DESCR("Trigger a panic"),
    803 		       sysctl_kern_trigger_panic, 0, NULL, 0,
    804 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    805 #endif
    806 	sysctl_createv(clog, 0, NULL, NULL,
    807 		       CTLFLAG_PERMANENT,
    808 		       CTLTYPE_INT, "root_partition",
    809 		       SYSCTL_DESCR("Root partition on the root device"),
    810 		       sysctl_kern_root_partition, 0, NULL, 0,
    811 		       CTL_KERN, KERN_ROOT_PARTITION, CTL_EOL);
    812 	sysctl_createv(clog, 0, NULL, NULL,
    813 		       CTLFLAG_PERMANENT,
    814 		       CTLTYPE_STRUCT, "drivers",
    815 		       SYSCTL_DESCR("List of all drivers with block and "
    816 				    "character device numbers"),
    817 		       sysctl_kern_drivers, 0, NULL, 0,
    818 		       CTL_KERN, KERN_DRIVERS, CTL_EOL);
    819 	sysctl_createv(clog, 0, NULL, NULL,
    820 		       CTLFLAG_PERMANENT,
    821 		       CTLTYPE_STRUCT, "file2",
    822 		       SYSCTL_DESCR("System open file table"),
    823 		       sysctl_kern_file2, 0, NULL, 0,
    824 		       CTL_KERN, KERN_FILE2, CTL_EOL);
    825 	sysctl_createv(clog, 0, NULL, NULL,
    826 		       CTLFLAG_PERMANENT,
    827 		       CTLTYPE_STRUCT, "cp_id",
    828 		       SYSCTL_DESCR("Mapping of CPU number to CPU id"),
    829 		       sysctl_kern_cpid, 0, NULL, 0,
    830 		       CTL_KERN, KERN_CP_ID, CTL_EOL);
    831 	sysctl_createv(clog, 0, NULL, &rnode,
    832 		       CTLFLAG_PERMANENT,
    833 		       CTLTYPE_NODE, "coredump",
    834 		       SYSCTL_DESCR("Coredump settings."),
    835 		       NULL, 0, NULL, 0,
    836 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    837 	sysctl_createv(clog, 0, &rnode, &rnode,
    838 		       CTLFLAG_PERMANENT,
    839 		       CTLTYPE_NODE, "setid",
    840 		       SYSCTL_DESCR("Set-id processes' coredump settings."),
    841 		       NULL, 0, NULL, 0,
    842 		       CTL_CREATE, CTL_EOL);
    843 	sysctl_createv(clog, 0, &rnode, NULL,
    844 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    845 		       CTLTYPE_INT, "dump",
    846 		       SYSCTL_DESCR("Allow set-id processes to dump core."),
    847 		       sysctl_security_setidcore, 0, &security_setidcore_dump,
    848 		       sizeof(security_setidcore_dump),
    849 		       CTL_CREATE, CTL_EOL);
    850 	sysctl_createv(clog, 0, &rnode, NULL,
    851 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    852 		       CTLTYPE_STRING, "path",
    853 		       SYSCTL_DESCR("Path pattern for set-id coredumps."),
    854 		       sysctl_security_setidcorename, 0,
    855 		       &security_setidcore_path,
    856 		       sizeof(security_setidcore_path),
    857 		       CTL_CREATE, CTL_EOL);
    858 	sysctl_createv(clog, 0, &rnode, NULL,
    859 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    860 		       CTLTYPE_INT, "owner",
    861 		       SYSCTL_DESCR("Owner id for set-id processes' cores."),
    862 		       sysctl_security_setidcore, 0, &security_setidcore_owner,
    863 		       0,
    864 		       CTL_CREATE, CTL_EOL);
    865 	sysctl_createv(clog, 0, &rnode, NULL,
    866 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    867 		       CTLTYPE_INT, "group",
    868 		       SYSCTL_DESCR("Group id for set-id processes' cores."),
    869 		       sysctl_security_setidcore, 0, &security_setidcore_group,
    870 		       0,
    871 		       CTL_CREATE, CTL_EOL);
    872 	sysctl_createv(clog, 0, &rnode, NULL,
    873 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    874 		       CTLTYPE_INT, "mode",
    875 		       SYSCTL_DESCR("Mode for set-id processes' cores."),
    876 		       sysctl_security_setidcore, 0, &security_setidcore_mode,
    877 		       0,
    878 		       CTL_CREATE, CTL_EOL);
    879 }
    880 
    881 SYSCTL_SETUP(sysctl_kern_proc_setup,
    882 	     "sysctl kern.proc/proc2/proc_args subtree setup")
    883 {
    884 
    885 	sysctl_createv(clog, 0, NULL, NULL,
    886 		       CTLFLAG_PERMANENT,
    887 		       CTLTYPE_NODE, "kern", NULL,
    888 		       NULL, 0, NULL, 0,
    889 		       CTL_KERN, CTL_EOL);
    890 
    891 	sysctl_createv(clog, 0, NULL, NULL,
    892 		       CTLFLAG_PERMANENT,
    893 		       CTLTYPE_NODE, "proc",
    894 		       SYSCTL_DESCR("System-wide process information"),
    895 		       sysctl_doeproc, 0, NULL, 0,
    896 		       CTL_KERN, KERN_PROC, CTL_EOL);
    897 	sysctl_createv(clog, 0, NULL, NULL,
    898 		       CTLFLAG_PERMANENT,
    899 		       CTLTYPE_NODE, "proc2",
    900 		       SYSCTL_DESCR("Machine-independent process information"),
    901 		       sysctl_doeproc, 0, NULL, 0,
    902 		       CTL_KERN, KERN_PROC2, CTL_EOL);
    903 	sysctl_createv(clog, 0, NULL, NULL,
    904 		       CTLFLAG_PERMANENT,
    905 		       CTLTYPE_NODE, "proc_args",
    906 		       SYSCTL_DESCR("Process argument information"),
    907 		       sysctl_kern_proc_args, 0, NULL, 0,
    908 		       CTL_KERN, KERN_PROC_ARGS, CTL_EOL);
    909 
    910 	/*
    911 	  "nodes" under these:
    912 
    913 	  KERN_PROC_ALL
    914 	  KERN_PROC_PID pid
    915 	  KERN_PROC_PGRP pgrp
    916 	  KERN_PROC_SESSION sess
    917 	  KERN_PROC_TTY tty
    918 	  KERN_PROC_UID uid
    919 	  KERN_PROC_RUID uid
    920 	  KERN_PROC_GID gid
    921 	  KERN_PROC_RGID gid
    922 
    923 	  all in all, probably not worth the effort...
    924 	*/
    925 }
    926 
    927 SYSCTL_SETUP(sysctl_hw_setup, "sysctl hw subtree setup")
    928 {
    929 	u_int u;
    930 	u_quad_t q;
    931 
    932 	sysctl_createv(clog, 0, NULL, NULL,
    933 		       CTLFLAG_PERMANENT,
    934 		       CTLTYPE_NODE, "hw", NULL,
    935 		       NULL, 0, NULL, 0,
    936 		       CTL_HW, CTL_EOL);
    937 
    938 	sysctl_createv(clog, 0, NULL, NULL,
    939 		       CTLFLAG_PERMANENT,
    940 		       CTLTYPE_STRING, "machine",
    941 		       SYSCTL_DESCR("Machine class"),
    942 		       NULL, 0, machine, 0,
    943 		       CTL_HW, HW_MACHINE, CTL_EOL);
    944 	sysctl_createv(clog, 0, NULL, NULL,
    945 		       CTLFLAG_PERMANENT,
    946 		       CTLTYPE_STRING, "model",
    947 		       SYSCTL_DESCR("Machine model"),
    948 		       NULL, 0, cpu_model, 0,
    949 		       CTL_HW, HW_MODEL, CTL_EOL);
    950 	sysctl_createv(clog, 0, NULL, NULL,
    951 		       CTLFLAG_PERMANENT,
    952 		       CTLTYPE_INT, "ncpu",
    953 		       SYSCTL_DESCR("Number of active CPUs"),
    954 		       sysctl_hw_ncpu, 0, NULL, 0,
    955 		       CTL_HW, HW_NCPU, CTL_EOL);
    956 	sysctl_createv(clog, 0, NULL, NULL,
    957 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    958 		       CTLTYPE_INT, "byteorder",
    959 		       SYSCTL_DESCR("System byte order"),
    960 		       NULL, BYTE_ORDER, NULL, 0,
    961 		       CTL_HW, HW_BYTEORDER, CTL_EOL);
    962 	u = ((u_int)physmem > (UINT_MAX / PAGE_SIZE)) ?
    963 		UINT_MAX : physmem * PAGE_SIZE;
    964 	sysctl_createv(clog, 0, NULL, NULL,
    965 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    966 		       CTLTYPE_INT, "physmem",
    967 		       SYSCTL_DESCR("Bytes of physical memory"),
    968 		       NULL, u, NULL, 0,
    969 		       CTL_HW, HW_PHYSMEM, CTL_EOL);
    970 	sysctl_createv(clog, 0, NULL, NULL,
    971 		       CTLFLAG_PERMANENT,
    972 		       CTLTYPE_INT, "usermem",
    973 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
    974 		       sysctl_hw_usermem, 0, NULL, 0,
    975 		       CTL_HW, HW_USERMEM, CTL_EOL);
    976 	sysctl_createv(clog, 0, NULL, NULL,
    977 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    978 		       CTLTYPE_INT, "pagesize",
    979 		       SYSCTL_DESCR("Software page size"),
    980 		       NULL, PAGE_SIZE, NULL, 0,
    981 		       CTL_HW, HW_PAGESIZE, CTL_EOL);
    982 	sysctl_createv(clog, 0, NULL, NULL,
    983 		       CTLFLAG_PERMANENT,
    984 		       CTLTYPE_STRING, "machine_arch",
    985 		       SYSCTL_DESCR("Machine CPU class"),
    986 		       NULL, 0, machine_arch, 0,
    987 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
    988 	sysctl_createv(clog, 0, NULL, NULL,
    989 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    990 		       CTLTYPE_INT, "alignbytes",
    991 		       SYSCTL_DESCR("Alignment constraint for all possible "
    992 				    "data types"),
    993 		       NULL, ALIGNBYTES, NULL, 0,
    994 		       CTL_HW, HW_ALIGNBYTES, CTL_EOL);
    995 	sysctl_createv(clog, 0, NULL, NULL,
    996 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
    997 		       CTLTYPE_STRING, "cnmagic",
    998 		       SYSCTL_DESCR("Console magic key sequence"),
    999 		       sysctl_hw_cnmagic, 0, NULL, CNS_LEN,
   1000 		       CTL_HW, HW_CNMAGIC, CTL_EOL);
   1001 	q = (u_quad_t)physmem * PAGE_SIZE;
   1002 	sysctl_createv(clog, 0, NULL, NULL,
   1003 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
   1004 		       CTLTYPE_QUAD, "physmem64",
   1005 		       SYSCTL_DESCR("Bytes of physical memory"),
   1006 		       NULL, q, NULL, 0,
   1007 		       CTL_HW, HW_PHYSMEM64, CTL_EOL);
   1008 	sysctl_createv(clog, 0, NULL, NULL,
   1009 		       CTLFLAG_PERMANENT,
   1010 		       CTLTYPE_QUAD, "usermem64",
   1011 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
   1012 		       sysctl_hw_usermem, 0, NULL, 0,
   1013 		       CTL_HW, HW_USERMEM64, CTL_EOL);
   1014 }
   1015 
   1016 #ifdef DEBUG
   1017 /*
   1018  * Debugging related system variables.
   1019  */
   1020 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
   1021 struct ctldebug debug5, debug6, debug7, debug8, debug9;
   1022 struct ctldebug debug10, debug11, debug12, debug13, debug14;
   1023 struct ctldebug debug15, debug16, debug17, debug18, debug19;
   1024 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
   1025 	&debug0, &debug1, &debug2, &debug3, &debug4,
   1026 	&debug5, &debug6, &debug7, &debug8, &debug9,
   1027 	&debug10, &debug11, &debug12, &debug13, &debug14,
   1028 	&debug15, &debug16, &debug17, &debug18, &debug19,
   1029 };
   1030 
   1031 /*
   1032  * this setup routine is a replacement for debug_sysctl()
   1033  *
   1034  * note that it creates several nodes per defined debug variable
   1035  */
   1036 SYSCTL_SETUP(sysctl_debug_setup, "sysctl debug subtree setup")
   1037 {
   1038 	struct ctldebug *cdp;
   1039 	char nodename[20];
   1040 	int i;
   1041 
   1042 	/*
   1043 	 * two ways here:
   1044 	 *
   1045 	 * the "old" way (debug.name -> value) which was emulated by
   1046 	 * the sysctl(8) binary
   1047 	 *
   1048 	 * the new way, which the sysctl(8) binary was actually using
   1049 
   1050 	 node	debug
   1051 	 node	debug.0
   1052 	 string	debug.0.name
   1053 	 int	debug.0.value
   1054 	 int	debug.name
   1055 
   1056 	 */
   1057 
   1058 	sysctl_createv(clog, 0, NULL, NULL,
   1059 		       CTLFLAG_PERMANENT,
   1060 		       CTLTYPE_NODE, "debug", NULL,
   1061 		       NULL, 0, NULL, 0,
   1062 		       CTL_DEBUG, CTL_EOL);
   1063 
   1064 	for (i = 0; i < CTL_DEBUG_MAXID; i++) {
   1065 		cdp = debugvars[i];
   1066 		if (cdp->debugname == NULL || cdp->debugvar == NULL)
   1067 			continue;
   1068 
   1069 		snprintf(nodename, sizeof(nodename), "debug%d", i);
   1070 		sysctl_createv(clog, 0, NULL, NULL,
   1071 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
   1072 			       CTLTYPE_NODE, nodename, NULL,
   1073 			       NULL, 0, NULL, 0,
   1074 			       CTL_DEBUG, i, CTL_EOL);
   1075 		sysctl_createv(clog, 0, NULL, NULL,
   1076 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
   1077 			       CTLTYPE_STRING, "name", NULL,
   1078 			       /*XXXUNCONST*/
   1079 			       NULL, 0, __UNCONST(cdp->debugname), 0,
   1080 			       CTL_DEBUG, i, CTL_DEBUG_NAME, CTL_EOL);
   1081 		sysctl_createv(clog, 0, NULL, NULL,
   1082 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
   1083 			       CTLTYPE_INT, "value", NULL,
   1084 			       NULL, 0, cdp->debugvar, 0,
   1085 			       CTL_DEBUG, i, CTL_DEBUG_VALUE, CTL_EOL);
   1086 		sysctl_createv(clog, 0, NULL, NULL,
   1087 			       CTLFLAG_PERMANENT,
   1088 			       CTLTYPE_INT, cdp->debugname, NULL,
   1089 			       NULL, 0, cdp->debugvar, 0,
   1090 			       CTL_DEBUG, CTL_CREATE, CTL_EOL);
   1091 	}
   1092 }
   1093 #endif /* DEBUG */
   1094 
   1095 /*
   1096  * ********************************************************************
   1097  * section 2: private node-specific helper routines.
   1098  * ********************************************************************
   1099  */
   1100 
   1101 #ifdef DIAGNOSTIC
   1102 static int
   1103 sysctl_kern_trigger_panic(SYSCTLFN_ARGS)
   1104 {
   1105 	int newtrig, error;
   1106 	struct sysctlnode node;
   1107 
   1108 	newtrig = 0;
   1109 	node = *rnode;
   1110 	node.sysctl_data = &newtrig;
   1111 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1112 	if (error || newp == NULL)
   1113 		return (error);
   1114 
   1115 	if (newtrig != 0)
   1116 		panic("Panic triggered");
   1117 
   1118 	return (error);
   1119 }
   1120 #endif
   1121 
   1122 /*
   1123  * sysctl helper routine for kern.maxvnodes.  drain vnodes if
   1124  * new value is lower than desiredvnodes and then calls reinit
   1125  * routines that needs to adjust to the new value.
   1126  */
   1127 static int
   1128 sysctl_kern_maxvnodes(SYSCTLFN_ARGS)
   1129 {
   1130 	int error, new_vnodes, old_vnodes;
   1131 	struct sysctlnode node;
   1132 
   1133 	new_vnodes = desiredvnodes;
   1134 	node = *rnode;
   1135 	node.sysctl_data = &new_vnodes;
   1136 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1137 	if (error || newp == NULL)
   1138 		return (error);
   1139 
   1140 	old_vnodes = desiredvnodes;
   1141 	desiredvnodes = new_vnodes;
   1142 	if (new_vnodes < old_vnodes) {
   1143 		error = vfs_drainvnodes(new_vnodes, l);
   1144 		if (error) {
   1145 			desiredvnodes = old_vnodes;
   1146 			return (error);
   1147 		}
   1148 	}
   1149 	vfs_reinit();
   1150 	nchreinit();
   1151 
   1152 	return (0);
   1153 }
   1154 
   1155 /*
   1156  * sysctl helper routine for rtc_offset - set time after changes
   1157  */
   1158 static int
   1159 sysctl_kern_rtc_offset(SYSCTLFN_ARGS)
   1160 {
   1161 	struct timespec ts, delta;
   1162 	int error, new_rtc_offset;
   1163 	struct sysctlnode node;
   1164 
   1165 	new_rtc_offset = rtc_offset;
   1166 	node = *rnode;
   1167 	node.sysctl_data = &new_rtc_offset;
   1168 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1169 	if (error || newp == NULL)
   1170 		return (error);
   1171 
   1172 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
   1173 	    KAUTH_REQ_SYSTEM_TIME_RTCOFFSET,
   1174 	    (void *)(u_long)new_rtc_offset, NULL, NULL))
   1175 		return (EPERM);
   1176 	if (rtc_offset == new_rtc_offset)
   1177 		return (0);
   1178 
   1179 	/* if we change the offset, adjust the time */
   1180 	nanotime(&ts);
   1181 	delta.tv_sec = 60 * (new_rtc_offset - rtc_offset);
   1182 	delta.tv_nsec = 0;
   1183 	timespecadd(&ts, &delta, &ts);
   1184 	rtc_offset = new_rtc_offset;
   1185 	settime(l->l_proc, &ts);
   1186 
   1187 	return (0);
   1188 }
   1189 
   1190 /*
   1191  * sysctl helper routine for kern.maxproc.  ensures that the new
   1192  * values are not too low or too high.
   1193  */
   1194 static int
   1195 sysctl_kern_maxproc(SYSCTLFN_ARGS)
   1196 {
   1197 	int error, nmaxproc;
   1198 	struct sysctlnode node;
   1199 
   1200 	nmaxproc = maxproc;
   1201 	node = *rnode;
   1202 	node.sysctl_data = &nmaxproc;
   1203 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1204 	if (error || newp == NULL)
   1205 		return (error);
   1206 
   1207 	if (nmaxproc < 0 || nmaxproc >= PID_MAX)
   1208 		return (EINVAL);
   1209 #ifdef __HAVE_CPU_MAXPROC
   1210 	if (nmaxproc > cpu_maxproc())
   1211 		return (EINVAL);
   1212 #endif
   1213 	maxproc = nmaxproc;
   1214 
   1215 	return (0);
   1216 }
   1217 
   1218 /*
   1219  * sysctl helper function for kern.hostid.  the hostid is a long, but
   1220  * we export it as an int, so we need to give it a little help.
   1221  */
   1222 static int
   1223 sysctl_kern_hostid(SYSCTLFN_ARGS)
   1224 {
   1225 	int error, inthostid;
   1226 	struct sysctlnode node;
   1227 
   1228 	inthostid = hostid;  /* XXX assumes sizeof int <= sizeof long */
   1229 	node = *rnode;
   1230 	node.sysctl_data = &inthostid;
   1231 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1232 	if (error || newp == NULL)
   1233 		return (error);
   1234 
   1235 	hostid = (unsigned)inthostid;
   1236 
   1237 	return (0);
   1238 }
   1239 
   1240 /*
   1241  * sysctl helper function for kern.hostname and kern.domainnname.
   1242  * resets the relevant recorded length when the underlying name is
   1243  * changed.
   1244  */
   1245 static int
   1246 sysctl_setlen(SYSCTLFN_ARGS)
   1247 {
   1248 	int error;
   1249 
   1250 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
   1251 	if (error || newp == NULL)
   1252 		return (error);
   1253 
   1254 	switch (rnode->sysctl_num) {
   1255 	case KERN_HOSTNAME:
   1256 		hostnamelen = strlen((const char*)rnode->sysctl_data);
   1257 		break;
   1258 	case KERN_DOMAINNAME:
   1259 		domainnamelen = strlen((const char*)rnode->sysctl_data);
   1260 		break;
   1261 	}
   1262 
   1263 	return (0);
   1264 }
   1265 
   1266 /*
   1267  * sysctl helper routine for kern.clockrate.  assembles a struct on
   1268  * the fly to be returned to the caller.
   1269  */
   1270 static int
   1271 sysctl_kern_clockrate(SYSCTLFN_ARGS)
   1272 {
   1273 	struct clockinfo clkinfo;
   1274 	struct sysctlnode node;
   1275 
   1276 	clkinfo.tick = tick;
   1277 	clkinfo.tickadj = tickadj;
   1278 	clkinfo.hz = hz;
   1279 	clkinfo.profhz = profhz;
   1280 	clkinfo.stathz = stathz ? stathz : hz;
   1281 
   1282 	node = *rnode;
   1283 	node.sysctl_data = &clkinfo;
   1284 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1285 }
   1286 
   1287 
   1288 /*
   1289  * sysctl helper routine for kern.file pseudo-subtree.
   1290  */
   1291 static int
   1292 sysctl_kern_file(SYSCTLFN_ARGS)
   1293 {
   1294 	int error;
   1295 	size_t buflen;
   1296 	struct file *fp;
   1297 	char *start, *where;
   1298 
   1299 	start = where = oldp;
   1300 	buflen = *oldlenp;
   1301 	if (where == NULL) {
   1302 		/*
   1303 		 * overestimate by 10 files
   1304 		 */
   1305 		*oldlenp = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
   1306 		return (0);
   1307 	}
   1308 
   1309 	/*
   1310 	 * first dcopyout filehead
   1311 	 */
   1312 	if (buflen < sizeof(filehead)) {
   1313 		*oldlenp = 0;
   1314 		return (0);
   1315 	}
   1316 	error = dcopyout(l, &filehead, where, sizeof(filehead));
   1317 	if (error)
   1318 		return (error);
   1319 	buflen -= sizeof(filehead);
   1320 	where += sizeof(filehead);
   1321 
   1322 	/*
   1323 	 * followed by an array of file structures
   1324 	 */
   1325 	LIST_FOREACH(fp, &filehead, f_list) {
   1326 		if (kauth_authorize_generic(l->l_cred,
   1327 		    KAUTH_GENERIC_CANSEE, fp->f_cred) != 0)
   1328 			continue;
   1329 		if (buflen < sizeof(struct file)) {
   1330 			*oldlenp = where - start;
   1331 			return (ENOMEM);
   1332 		}
   1333 		error = dcopyout(l, fp, where, sizeof(struct file));
   1334 		if (error)
   1335 			return (error);
   1336 		buflen -= sizeof(struct file);
   1337 		where += sizeof(struct file);
   1338 	}
   1339 	*oldlenp = where - start;
   1340 	return (0);
   1341 }
   1342 
   1343 /*
   1344  * sysctl helper routine for kern.autonicetime and kern.autoniceval.
   1345  * asserts that the assigned value is in the correct range.
   1346  */
   1347 static int
   1348 sysctl_kern_autonice(SYSCTLFN_ARGS)
   1349 {
   1350 	int error, t = 0;
   1351 	struct sysctlnode node;
   1352 
   1353 	node = *rnode;
   1354 	t = *(int*)node.sysctl_data;
   1355 	node.sysctl_data = &t;
   1356 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1357 	if (error || newp == NULL)
   1358 		return (error);
   1359 
   1360 	switch (node.sysctl_num) {
   1361 	case KERN_AUTONICETIME:
   1362 		if (t >= 0)
   1363 			autonicetime = t;
   1364 		break;
   1365 	case KERN_AUTONICEVAL:
   1366 		if (t < PRIO_MIN)
   1367 			t = PRIO_MIN;
   1368 		else if (t > PRIO_MAX)
   1369 			t = PRIO_MAX;
   1370 		autoniceval = t;
   1371 		break;
   1372 	}
   1373 
   1374 	return (0);
   1375 }
   1376 
   1377 /*
   1378  * sysctl helper routine for kern.msgbufsize and kern.msgbuf.  for the
   1379  * former it merely checks the message buffer is set up.  for the latter,
   1380  * it also copies out the data if necessary.
   1381  */
   1382 static int
   1383 sysctl_msgbuf(SYSCTLFN_ARGS)
   1384 {
   1385 	char *where = oldp;
   1386 	size_t len, maxlen;
   1387 	long beg, end;
   1388 	int error;
   1389 
   1390 	if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
   1391 		msgbufenabled = 0;
   1392 		return (ENXIO);
   1393 	}
   1394 
   1395 	switch (rnode->sysctl_num) {
   1396 	case KERN_MSGBUFSIZE: {
   1397 		struct sysctlnode node = *rnode;
   1398 		int msg_bufs = (int)msgbufp->msg_bufs;
   1399 		node.sysctl_data = &msg_bufs;
   1400 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1401 	}
   1402 	case KERN_MSGBUF:
   1403 		break;
   1404 	default:
   1405 		return (EOPNOTSUPP);
   1406 	}
   1407 
   1408 	if (newp != NULL)
   1409 		return (EPERM);
   1410 
   1411         if (oldp == NULL) {
   1412 		/* always return full buffer size */
   1413 		*oldlenp = msgbufp->msg_bufs;
   1414 		return (0);
   1415         }
   1416 
   1417 	error = 0;
   1418 	maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
   1419 
   1420 	/*
   1421 	 * First, copy from the write pointer to the end of
   1422 	 * message buffer.
   1423 	 */
   1424 	beg = msgbufp->msg_bufx;
   1425 	end = msgbufp->msg_bufs;
   1426 	while (maxlen > 0) {
   1427 		len = MIN(end - beg, maxlen);
   1428 		if (len == 0)
   1429 			break;
   1430 		error = dcopyout(l, &msgbufp->msg_bufc[beg], where, len);
   1431 		if (error)
   1432 			break;
   1433 		where += len;
   1434 		maxlen -= len;
   1435 
   1436 		/*
   1437 		 * ... then, copy from the beginning of message buffer to
   1438 		 * the write pointer.
   1439 		 */
   1440 		beg = 0;
   1441 		end = msgbufp->msg_bufx;
   1442 	}
   1443 
   1444 	return (error);
   1445 }
   1446 
   1447 /*
   1448  * sysctl helper routine for kern.defcorename.  in the case of a new
   1449  * string being assigned, check that it's not a zero-length string.
   1450  * (XXX the check in -current doesn't work, but do we really care?)
   1451  */
   1452 static int
   1453 sysctl_kern_defcorename(SYSCTLFN_ARGS)
   1454 {
   1455 	int error;
   1456 	char *newcorename;
   1457 	struct sysctlnode node;
   1458 
   1459 	newcorename = PNBUF_GET();
   1460 	node = *rnode;
   1461 	node.sysctl_data = &newcorename[0];
   1462 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
   1463 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1464 	if (error || newp == NULL) {
   1465 		goto done;
   1466 	}
   1467 
   1468 	/*
   1469 	 * when sysctl_lookup() deals with a string, it's guaranteed
   1470 	 * to come back nul terminated.  so there.  :)
   1471 	 */
   1472 	if (strlen(newcorename) == 0) {
   1473 		error = EINVAL;
   1474 	} else {
   1475 		memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
   1476 		error = 0;
   1477 	}
   1478 done:
   1479 	PNBUF_PUT(newcorename);
   1480 	return error;
   1481 }
   1482 
   1483 /*
   1484  * sysctl helper routine for kern.cp_time node.  adds up cpu time
   1485  * across all cpus.
   1486  */
   1487 static int
   1488 sysctl_kern_cptime(SYSCTLFN_ARGS)
   1489 {
   1490 	struct sysctlnode node = *rnode;
   1491 
   1492 #ifndef MULTIPROCESSOR
   1493 
   1494 	if (namelen == 1) {
   1495 		if (name[0] != 0)
   1496 			return (ENOENT);
   1497 		/*
   1498 		 * you're allowed to ask for the zero'th processor
   1499 		 */
   1500 		name++;
   1501 		namelen--;
   1502 	}
   1503 	node.sysctl_data = curcpu()->ci_schedstate.spc_cp_time;
   1504 	node.sysctl_size = sizeof(curcpu()->ci_schedstate.spc_cp_time);
   1505 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1506 
   1507 #else /* MULTIPROCESSOR */
   1508 
   1509 	uint64_t *cp_time = NULL;
   1510 	int error, n = ncpu, i;
   1511 	struct cpu_info *ci;
   1512 	CPU_INFO_ITERATOR cii;
   1513 
   1514 	/*
   1515 	 * if you specifically pass a buffer that is the size of the
   1516 	 * sum, or if you are probing for the size, you get the "sum"
   1517 	 * of cp_time (and the size thereof) across all processors.
   1518 	 *
   1519 	 * alternately, you can pass an additional mib number and get
   1520 	 * cp_time for that particular processor.
   1521 	 */
   1522 	switch (namelen) {
   1523 	case 0:
   1524 	    	if (*oldlenp == sizeof(uint64_t) * CPUSTATES || oldp == NULL) {
   1525 			node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
   1526 			n = -1; /* SUM */
   1527 		}
   1528 		else {
   1529 			node.sysctl_size = n * sizeof(uint64_t) * CPUSTATES;
   1530 			n = -2; /* ALL */
   1531 		}
   1532 		break;
   1533 	case 1:
   1534 		if (name[0] < 0 || name[0] >= n)
   1535 			return (ENOENT); /* ENOSUCHPROCESSOR */
   1536 		node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
   1537 		n = name[0];
   1538 		/*
   1539 		 * adjust these so that sysctl_lookup() will be happy
   1540 		 */
   1541 		name++;
   1542 		namelen--;
   1543 		break;
   1544 	default:
   1545 		return (EINVAL);
   1546 	}
   1547 
   1548 	cp_time = malloc(node.sysctl_size, M_TEMP, M_WAITOK|M_CANFAIL);
   1549 	if (cp_time == NULL)
   1550 		return (ENOMEM);
   1551 	node.sysctl_data = cp_time;
   1552 	memset(cp_time, 0, node.sysctl_size);
   1553 
   1554 	for (CPU_INFO_FOREACH(cii, ci)) {
   1555 		if (n <= 0)
   1556 			for (i = 0; i < CPUSTATES; i++)
   1557 				cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
   1558 		/*
   1559 		 * if a specific processor was requested and we just
   1560 		 * did it, we're done here
   1561 		 */
   1562 		if (n == 0)
   1563 			break;
   1564 		/*
   1565 		 * if doing "all", skip to next cp_time set for next processor
   1566 		 */
   1567 		if (n == -2)
   1568 			cp_time += CPUSTATES;
   1569 		/*
   1570 		 * if we're doing a specific processor, we're one
   1571 		 * processor closer
   1572 		 */
   1573 		if (n > 0)
   1574 			n--;
   1575 	}
   1576 
   1577 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1578 	free(node.sysctl_data, M_TEMP);
   1579 	return (error);
   1580 
   1581 #endif /* MULTIPROCESSOR */
   1582 }
   1583 
   1584 #if NPTY > 0
   1585 /*
   1586  * sysctl helper routine for kern.maxptys.  ensures that any new value
   1587  * is acceptable to the pty subsystem.
   1588  */
   1589 static int
   1590 sysctl_kern_maxptys(SYSCTLFN_ARGS)
   1591 {
   1592 	int pty_maxptys(int, int);		/* defined in kern/tty_pty.c */
   1593 	int error, xmax;
   1594 	struct sysctlnode node;
   1595 
   1596 	/* get current value of maxptys */
   1597 	xmax = pty_maxptys(0, 0);
   1598 
   1599 	node = *rnode;
   1600 	node.sysctl_data = &xmax;
   1601 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1602 	if (error || newp == NULL)
   1603 		return (error);
   1604 
   1605 	if (xmax != pty_maxptys(xmax, 1))
   1606 		return (EINVAL);
   1607 
   1608 	return (0);
   1609 }
   1610 #endif /* NPTY > 0 */
   1611 
   1612 /*
   1613  * sysctl helper routine for kern.sbmax.  basically just ensures that
   1614  * any new value is not too small.
   1615  */
   1616 static int
   1617 sysctl_kern_sbmax(SYSCTLFN_ARGS)
   1618 {
   1619 	int error, new_sbmax;
   1620 	struct sysctlnode node;
   1621 
   1622 	new_sbmax = sb_max;
   1623 	node = *rnode;
   1624 	node.sysctl_data = &new_sbmax;
   1625 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1626 	if (error || newp == NULL)
   1627 		return (error);
   1628 
   1629 	error = sb_max_set(new_sbmax);
   1630 
   1631 	return (error);
   1632 }
   1633 
   1634 /*
   1635  * sysctl helper routine for kern.urandom node.  picks a random number
   1636  * for you.
   1637  */
   1638 static int
   1639 sysctl_kern_urnd(SYSCTLFN_ARGS)
   1640 {
   1641 #if NRND > 0
   1642 	int v;
   1643 
   1644 	if (rnd_extract_data(&v, sizeof(v), RND_EXTRACT_ANY) == sizeof(v)) {
   1645 		struct sysctlnode node = *rnode;
   1646 		node.sysctl_data = &v;
   1647 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1648 	}
   1649 	else
   1650 		return (EIO);	/*XXX*/
   1651 #else
   1652 	return (EOPNOTSUPP);
   1653 #endif
   1654 }
   1655 
   1656 /*
   1657  * sysctl helper routine for kern.arandom node.  picks a random number
   1658  * for you.
   1659  */
   1660 static int
   1661 sysctl_kern_arnd(SYSCTLFN_ARGS)
   1662 {
   1663 #if NRND > 0
   1664 	int error;
   1665 	void *v;
   1666 	struct sysctlnode node = *rnode;
   1667 
   1668 	if (*oldlenp == 0)
   1669 		return 0;
   1670 	if (*oldlenp > 8192)
   1671 		return E2BIG;
   1672 
   1673 	v = malloc(*oldlenp, M_TEMP, M_WAITOK);
   1674 
   1675 	arc4randbytes(v, *oldlenp);
   1676 	node.sysctl_data = v;
   1677 	node.sysctl_size = *oldlenp;
   1678 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1679 	free(v, M_TEMP);
   1680 	return error;
   1681 #else
   1682 	return (EOPNOTSUPP);
   1683 #endif
   1684 }
   1685 /*
   1686  * sysctl helper routine to do kern.lwp.* work.
   1687  */
   1688 static int
   1689 sysctl_kern_lwp(SYSCTLFN_ARGS)
   1690 {
   1691 	struct kinfo_lwp klwp;
   1692 	struct proc *p;
   1693 	struct lwp *l2;
   1694 	char *where, *dp;
   1695 	int pid, elem_size, elem_count;
   1696 	int buflen, needed, error;
   1697 
   1698 	if (namelen == 1 && name[0] == CTL_QUERY)
   1699 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1700 
   1701 	dp = where = oldp;
   1702 	buflen = where != NULL ? *oldlenp : 0;
   1703 	error = needed = 0;
   1704 
   1705 	if (newp != NULL || namelen != 3)
   1706 		return (EINVAL);
   1707 	pid = name[0];
   1708 	elem_size = name[1];
   1709 	elem_count = name[2];
   1710 
   1711 	rw_enter(&proclist_lock, RW_READER);
   1712 	p = p_find(pid, PFIND_LOCKED);
   1713 	if (p == NULL) {
   1714 		rw_exit(&proclist_lock);
   1715 		return (ESRCH);
   1716 	}
   1717 	mutex_enter(&p->p_smutex);
   1718 	LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1719 		if (buflen >= elem_size && elem_count > 0) {
   1720 			lwp_lock(l2);
   1721 			fill_lwp(l2, &klwp);
   1722 			lwp_unlock(l2);
   1723 
   1724 			/*
   1725 			 * Copy out elem_size, but not larger than
   1726 			 * the size of a struct kinfo_proc2.
   1727 			 *
   1728 			 * XXX We should not be holding p_smutex, but
   1729 			 * for now, the buffer is wired.  Fix later.
   1730 			 */
   1731 			error = dcopyout(l, &klwp, dp,
   1732 			    min(sizeof(klwp), elem_size));
   1733 			if (error)
   1734 				goto cleanup;
   1735 			dp += elem_size;
   1736 			buflen -= elem_size;
   1737 			elem_count--;
   1738 		}
   1739 		needed += elem_size;
   1740 	}
   1741 	mutex_exit(&p->p_smutex);
   1742 	rw_exit(&proclist_lock);
   1743 
   1744 	if (where != NULL) {
   1745 		*oldlenp = dp - where;
   1746 		if (needed > *oldlenp)
   1747 			return (ENOMEM);
   1748 	} else {
   1749 		needed += KERN_LWPSLOP;
   1750 		*oldlenp = needed;
   1751 	}
   1752 	return (0);
   1753  cleanup:
   1754 	return (error);
   1755 }
   1756 
   1757 /*
   1758  * sysctl helper routine for kern.forkfsleep node.  ensures that the
   1759  * given value is not too large or two small, and is at least one
   1760  * timer tick if not zero.
   1761  */
   1762 static int
   1763 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
   1764 {
   1765 	/* userland sees value in ms, internally is in ticks */
   1766 	extern int forkfsleep;		/* defined in kern/kern_fork.c */
   1767 	int error, timo, lsleep;
   1768 	struct sysctlnode node;
   1769 
   1770 	lsleep = forkfsleep * 1000 / hz;
   1771 	node = *rnode;
   1772 	node.sysctl_data = &lsleep;
   1773 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1774 	if (error || newp == NULL)
   1775 		return (error);
   1776 
   1777 	/* refuse negative values, and overly 'long time' */
   1778 	if (lsleep < 0 || lsleep > MAXSLP * 1000)
   1779 		return (EINVAL);
   1780 
   1781 	timo = mstohz(lsleep);
   1782 
   1783 	/* if the interval is >0 ms && <1 tick, use 1 tick */
   1784 	if (lsleep != 0 && timo == 0)
   1785 		forkfsleep = 1;
   1786 	else
   1787 		forkfsleep = timo;
   1788 
   1789 	return (0);
   1790 }
   1791 
   1792 /*
   1793  * sysctl helper routine for kern.root_partition
   1794  */
   1795 static int
   1796 sysctl_kern_root_partition(SYSCTLFN_ARGS)
   1797 {
   1798 	int rootpart = DISKPART(rootdev);
   1799 	struct sysctlnode node = *rnode;
   1800 
   1801 	node.sysctl_data = &rootpart;
   1802 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1803 }
   1804 
   1805 /*
   1806  * sysctl helper function for kern.drivers
   1807  */
   1808 static int
   1809 sysctl_kern_drivers(SYSCTLFN_ARGS)
   1810 {
   1811 	int error;
   1812 	size_t buflen;
   1813 	struct kinfo_drivers kd;
   1814 	char *start, *where;
   1815 	const char *dname;
   1816 	int i;
   1817 	extern struct devsw_conv *devsw_conv;
   1818 	extern int max_devsw_convs;
   1819 
   1820 	if (newp != NULL || namelen != 0)
   1821 		return (EINVAL);
   1822 
   1823 	start = where = oldp;
   1824 	buflen = *oldlenp;
   1825 	if (where == NULL) {
   1826 		*oldlenp = max_devsw_convs * sizeof kd;
   1827 		return 0;
   1828 	}
   1829 
   1830 	/*
   1831 	 * An array of kinfo_drivers structures
   1832 	 */
   1833 	error = 0;
   1834 	for (i = 0; i < max_devsw_convs; i++) {
   1835 		dname = devsw_conv[i].d_name;
   1836 		if (dname == NULL)
   1837 			continue;
   1838 		if (buflen < sizeof kd) {
   1839 			error = ENOMEM;
   1840 			break;
   1841 		}
   1842 		memset(&kd, 0, sizeof(kd));
   1843 		kd.d_bmajor = devsw_conv[i].d_bmajor;
   1844 		kd.d_cmajor = devsw_conv[i].d_cmajor;
   1845 		strlcpy(kd.d_name, dname, sizeof kd.d_name);
   1846 		error = dcopyout(l, &kd, where, sizeof kd);
   1847 		if (error != 0)
   1848 			break;
   1849 		buflen -= sizeof kd;
   1850 		where += sizeof kd;
   1851 	}
   1852 	*oldlenp = where - start;
   1853 	return error;
   1854 }
   1855 
   1856 /*
   1857  * sysctl helper function for kern.file2
   1858  */
   1859 static int
   1860 sysctl_kern_file2(SYSCTLFN_ARGS)
   1861 {
   1862 	struct proc *p;
   1863 	struct file *fp;
   1864 	struct filedesc *fd;
   1865 	struct kinfo_file kf;
   1866 	char *dp;
   1867 	u_int i, op;
   1868 	size_t len, needed, elem_size, out_size;
   1869 	int error, arg, elem_count;
   1870 
   1871 	if (namelen == 1 && name[0] == CTL_QUERY)
   1872 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1873 
   1874 	if (namelen != 4)
   1875 		return (EINVAL);
   1876 
   1877 	error = 0;
   1878 	dp = oldp;
   1879 	len = (oldp != NULL) ? *oldlenp : 0;
   1880 	op = name[0];
   1881 	arg = name[1];
   1882 	elem_size = name[2];
   1883 	elem_count = name[3];
   1884 	out_size = MIN(sizeof(kf), elem_size);
   1885 	needed = 0;
   1886 
   1887 	if (elem_size < 1 || elem_count < 0)
   1888 		return (EINVAL);
   1889 
   1890 	switch (op) {
   1891 	case KERN_FILE_BYFILE:
   1892 		/*
   1893 		 * doesn't use arg so it must be zero
   1894 		 */
   1895 		if (arg != 0)
   1896 			return (EINVAL);
   1897 		LIST_FOREACH(fp, &filehead, f_list) {
   1898 			if (kauth_authorize_generic(l->l_cred,
   1899 			    KAUTH_GENERIC_CANSEE, fp->f_cred) != 0)
   1900 				continue;
   1901 			if (len >= elem_size && elem_count > 0) {
   1902 				fill_file(&kf, fp, NULL, 0);
   1903 				error = dcopyout(l, &kf, dp, out_size);
   1904 				if (error)
   1905 					break;
   1906 				dp += elem_size;
   1907 				len -= elem_size;
   1908 			}
   1909 			if (elem_count > 0) {
   1910 				needed += elem_size;
   1911 				if (elem_count != INT_MAX)
   1912 					elem_count--;
   1913 			}
   1914 		}
   1915 		break;
   1916 	case KERN_FILE_BYPID:
   1917 		if (arg < -1)
   1918 			/* -1 means all processes */
   1919 			return (EINVAL);
   1920 		rw_enter(&proclist_lock, RW_READER);
   1921 		PROCLIST_FOREACH(p, &allproc) {
   1922 			if (p->p_stat == SIDL)
   1923 				/* skip embryonic processes */
   1924 				continue;
   1925 			if (kauth_authorize_process(l->l_cred,
   1926 			    KAUTH_PROCESS_CANSEE, p, NULL, NULL, NULL) != 0)
   1927 				continue;
   1928 			if (arg > 0 && p->p_pid != arg)
   1929 				/* pick only the one we want */
   1930 				/* XXX want 0 to mean "kernel files" */
   1931 				continue;
   1932 			fd = p->p_fd;
   1933 			for (i = 0; i < fd->fd_nfiles; i++) {
   1934 				fp = fd->fd_ofiles[i];
   1935 				if (fp == NULL || !FILE_IS_USABLE(fp))
   1936 					continue;
   1937 				if (len >= elem_size && elem_count > 0) {
   1938 					fill_file(&kf, fd->fd_ofiles[i],
   1939 						  p, i);
   1940 					error = dcopyout(l, &kf, dp, out_size);
   1941 					if (error)
   1942 						break;
   1943 					dp += elem_size;
   1944 					len -= elem_size;
   1945 				}
   1946 				if (elem_count > 0) {
   1947 					needed += elem_size;
   1948 					if (elem_count != INT_MAX)
   1949 						elem_count--;
   1950 				}
   1951 			}
   1952 		}
   1953 		rw_exit(&proclist_lock);
   1954 		break;
   1955 	default:
   1956 		return (EINVAL);
   1957 	}
   1958 
   1959 	if (oldp == NULL)
   1960 		needed += KERN_FILESLOP * elem_size;
   1961 	*oldlenp = needed;
   1962 
   1963 	return (error);
   1964 }
   1965 
   1966 static void
   1967 fill_file(struct kinfo_file *kp, const struct file *fp, struct proc *p, int i)
   1968 {
   1969 
   1970 	memset(kp, 0, sizeof(*kp));
   1971 
   1972 	kp->ki_fileaddr =	PTRTOUINT64(fp);
   1973 	kp->ki_flag =		fp->f_flag;
   1974 	kp->ki_iflags =		fp->f_iflags;
   1975 	kp->ki_ftype =		fp->f_type;
   1976 	kp->ki_count =		fp->f_count;
   1977 	kp->ki_msgcount =	fp->f_msgcount;
   1978 	kp->ki_usecount =	fp->f_usecount;
   1979 	kp->ki_fucred =		PTRTOUINT64(fp->f_cred);
   1980 	kp->ki_fuid =		kauth_cred_geteuid(fp->f_cred);
   1981 	kp->ki_fgid =		kauth_cred_getegid(fp->f_cred);
   1982 	kp->ki_fops =		PTRTOUINT64(fp->f_ops);
   1983 	kp->ki_foffset =	fp->f_offset;
   1984 	kp->ki_fdata =		PTRTOUINT64(fp->f_data);
   1985 
   1986 	/* vnode information to glue this file to something */
   1987 	if (fp->f_type == DTYPE_VNODE) {
   1988 		struct vnode *vp = (struct vnode *)fp->f_data;
   1989 
   1990 		kp->ki_vun =	PTRTOUINT64(vp->v_un.vu_socket);
   1991 		kp->ki_vsize =	vp->v_size;
   1992 		kp->ki_vtype =	vp->v_type;
   1993 		kp->ki_vtag =	vp->v_tag;
   1994 		kp->ki_vdata =	PTRTOUINT64(vp->v_data);
   1995 	}
   1996 
   1997         /* process information when retrieved via KERN_FILE_BYPID */
   1998 	if (p) {
   1999 		kp->ki_pid =		p->p_pid;
   2000 		kp->ki_fd =		i;
   2001 		kp->ki_ofileflags =	p->p_fd->fd_ofileflags[i];
   2002 	}
   2003 }
   2004 
   2005 static int
   2006 sysctl_doeproc(SYSCTLFN_ARGS)
   2007 {
   2008 	struct eproc *eproc;
   2009 	struct kinfo_proc2 *kproc2;
   2010 	struct kinfo_proc *dp;
   2011 	struct proc *p;
   2012 	const struct proclist_desc *pd;
   2013 	char *where, *dp2;
   2014 	int type, op, arg;
   2015 	u_int elem_size, elem_count;
   2016 	size_t buflen, needed;
   2017 	int error;
   2018 
   2019 	if (namelen == 1 && name[0] == CTL_QUERY)
   2020 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   2021 
   2022 	dp = oldp;
   2023 	dp2 = where = oldp;
   2024 	buflen = where != NULL ? *oldlenp : 0;
   2025 	error = 0;
   2026 	needed = 0;
   2027 	type = rnode->sysctl_num;
   2028 
   2029 	if (type == KERN_PROC) {
   2030 		if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
   2031 			return (EINVAL);
   2032 		op = name[0];
   2033 		if (op != KERN_PROC_ALL)
   2034 			arg = name[1];
   2035 		else
   2036 			arg = 0;		/* Quell compiler warning */
   2037 		elem_size = elem_count = 0;	/* Ditto */
   2038 	} else {
   2039 		if (namelen != 4)
   2040 			return (EINVAL);
   2041 		op = name[0];
   2042 		arg = name[1];
   2043 		elem_size = name[2];
   2044 		elem_count = name[3];
   2045 	}
   2046 
   2047 	if (type == KERN_PROC) {
   2048 		eproc = malloc(sizeof(*eproc), M_TEMP, M_WAITOK);
   2049 		kproc2 = NULL;
   2050 	} else {
   2051 		eproc = NULL;
   2052 		kproc2 = malloc(sizeof(*kproc2), M_TEMP, M_WAITOK);
   2053 	}
   2054 	rw_enter(&proclist_lock, RW_READER);
   2055 
   2056 	pd = proclists;
   2057 again:
   2058 	PROCLIST_FOREACH(p, pd->pd_list) {
   2059 		/*
   2060 		 * Skip embryonic processes.
   2061 		 */
   2062 		if (p->p_stat == SIDL)
   2063 			continue;
   2064 
   2065 		if (kauth_authorize_process(l->l_cred,
   2066 		    KAUTH_PROCESS_CANSEE, p, NULL, NULL, NULL) != 0)
   2067 			continue;
   2068 
   2069 		/*
   2070 		 * TODO - make more efficient (see notes below).
   2071 		 * do by session.
   2072 		 */
   2073 		switch (op) {
   2074 
   2075 		case KERN_PROC_PID:
   2076 			/* could do this with just a lookup */
   2077 			if (p->p_pid != (pid_t)arg)
   2078 				continue;
   2079 			break;
   2080 
   2081 		case KERN_PROC_PGRP:
   2082 			/* could do this by traversing pgrp */
   2083 			if (p->p_pgrp->pg_id != (pid_t)arg)
   2084 				continue;
   2085 			break;
   2086 
   2087 		case KERN_PROC_SESSION:
   2088 			if (p->p_session->s_sid != (pid_t)arg)
   2089 				continue;
   2090 			break;
   2091 
   2092 		case KERN_PROC_TTY:
   2093 			if (arg == (int) KERN_PROC_TTY_REVOKE) {
   2094 				if ((p->p_lflag & PL_CONTROLT) == 0 ||
   2095 				    p->p_session->s_ttyp == NULL ||
   2096 				    p->p_session->s_ttyvp != NULL)
   2097 					continue;
   2098 			} else if ((p->p_lflag & PL_CONTROLT) == 0 ||
   2099 			    p->p_session->s_ttyp == NULL) {
   2100 				if ((dev_t)arg != KERN_PROC_TTY_NODEV)
   2101 					continue;
   2102 			} else if (p->p_session->s_ttyp->t_dev != (dev_t)arg)
   2103 				continue;
   2104 			break;
   2105 
   2106 		case KERN_PROC_UID:
   2107 			if (kauth_cred_geteuid(p->p_cred) != (uid_t)arg)
   2108 				continue;
   2109 			break;
   2110 
   2111 		case KERN_PROC_RUID:
   2112 			if (kauth_cred_getuid(p->p_cred) != (uid_t)arg)
   2113 				continue;
   2114 			break;
   2115 
   2116 		case KERN_PROC_GID:
   2117 			if (kauth_cred_getegid(p->p_cred) != (uid_t)arg)
   2118 				continue;
   2119 			break;
   2120 
   2121 		case KERN_PROC_RGID:
   2122 			if (kauth_cred_getgid(p->p_cred) != (uid_t)arg)
   2123 				continue;
   2124 			break;
   2125 
   2126 		case KERN_PROC_ALL:
   2127 			/* allow everything */
   2128 			break;
   2129 
   2130 		default:
   2131 			error = EINVAL;
   2132 			goto cleanup;
   2133 		}
   2134 		if (type == KERN_PROC) {
   2135 			if (buflen >= sizeof(struct kinfo_proc)) {
   2136 				fill_eproc(p, eproc);
   2137 				error = dcopyout(l, p, &dp->kp_proc,
   2138 				    sizeof(struct proc));
   2139 				if (error)
   2140 					goto cleanup;
   2141 				error = dcopyout(l, eproc, &dp->kp_eproc,
   2142 				    sizeof(*eproc));
   2143 				if (error)
   2144 					goto cleanup;
   2145 				dp++;
   2146 				buflen -= sizeof(struct kinfo_proc);
   2147 			}
   2148 			needed += sizeof(struct kinfo_proc);
   2149 		} else { /* KERN_PROC2 */
   2150 			if (buflen >= elem_size && elem_count > 0) {
   2151 				fill_kproc2(p, kproc2);
   2152 				/*
   2153 				 * Copy out elem_size, but not larger than
   2154 				 * the size of a struct kinfo_proc2.
   2155 				 */
   2156 				error = dcopyout(l, kproc2, dp2,
   2157 				    min(sizeof(*kproc2), elem_size));
   2158 				if (error)
   2159 					goto cleanup;
   2160 				dp2 += elem_size;
   2161 				buflen -= elem_size;
   2162 				elem_count--;
   2163 			}
   2164 			needed += elem_size;
   2165 		}
   2166 	}
   2167 	pd++;
   2168 	if (pd->pd_list != NULL)
   2169 		goto again;
   2170 	rw_exit(&proclist_lock);
   2171 
   2172 	if (where != NULL) {
   2173 		if (type == KERN_PROC)
   2174 			*oldlenp = (char *)dp - where;
   2175 		else
   2176 			*oldlenp = dp2 - where;
   2177 		if (needed > *oldlenp) {
   2178 			error = ENOMEM;
   2179 			goto out;
   2180 		}
   2181 	} else {
   2182 		needed += KERN_PROCSLOP;
   2183 		*oldlenp = needed;
   2184 	}
   2185 	if (kproc2)
   2186 		free(kproc2, M_TEMP);
   2187 	if (eproc)
   2188 		free(eproc, M_TEMP);
   2189 	return 0;
   2190  cleanup:
   2191 	rw_exit(&proclist_lock);
   2192  out:
   2193 	if (kproc2)
   2194 		free(kproc2, M_TEMP);
   2195 	if (eproc)
   2196 		free(eproc, M_TEMP);
   2197 	return error;
   2198 }
   2199 
   2200 /*
   2201  * sysctl helper routine for kern.proc_args pseudo-subtree.
   2202  */
   2203 static int
   2204 sysctl_kern_proc_args(SYSCTLFN_ARGS)
   2205 {
   2206 	struct ps_strings pss;
   2207 	struct proc *p;
   2208 	size_t len, i;
   2209 	struct uio auio;
   2210 	struct iovec aiov;
   2211 	pid_t pid;
   2212 	int nargv, type, error;
   2213 	char *arg;
   2214 	char **argv = NULL;
   2215 	char *tmp;
   2216 	struct vmspace *vmspace;
   2217 	vaddr_t psstr_addr;
   2218 	vaddr_t offsetn;
   2219 	vaddr_t offsetv;
   2220 
   2221 	if (namelen == 1 && name[0] == CTL_QUERY)
   2222 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   2223 
   2224 	if (newp != NULL || namelen != 2)
   2225 		return (EINVAL);
   2226 	pid = name[0];
   2227 	type = name[1];
   2228 
   2229 	switch (type) {
   2230 	case KERN_PROC_ARGV:
   2231 	case KERN_PROC_NARGV:
   2232 	case KERN_PROC_ENV:
   2233 	case KERN_PROC_NENV:
   2234 		/* ok */
   2235 		break;
   2236 	default:
   2237 		return (EINVAL);
   2238 	}
   2239 
   2240 	rw_enter(&proclist_lock, RW_READER);
   2241 
   2242 	/* check pid */
   2243 	if ((p = p_find(pid, PFIND_LOCKED)) == NULL) {
   2244 		error = EINVAL;
   2245 		goto out_locked;
   2246 	}
   2247 
   2248 	error = kauth_authorize_process(l->l_cred,
   2249 	    KAUTH_PROCESS_CANSEE, p, NULL, NULL, NULL);
   2250 	if (error) {
   2251 		goto out_locked;
   2252 	}
   2253 
   2254 	/* only root or same user change look at the environment */
   2255 	if (type == KERN_PROC_ENV || type == KERN_PROC_NENV) {
   2256 		if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
   2257 		    NULL) != 0) {
   2258 			if (kauth_cred_getuid(l->l_cred) !=
   2259 			    kauth_cred_getuid(p->p_cred) ||
   2260 			    kauth_cred_getuid(l->l_cred) !=
   2261 			    kauth_cred_getsvuid(p->p_cred)) {
   2262 				error = EPERM;
   2263 				goto out_locked;
   2264 			}
   2265 		}
   2266 	}
   2267 
   2268 	if (oldp == NULL) {
   2269 		if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
   2270 			*oldlenp = sizeof (int);
   2271 		else
   2272 			*oldlenp = ARG_MAX;	/* XXX XXX XXX */
   2273 		error = 0;
   2274 		goto out_locked;
   2275 	}
   2276 
   2277 	/*
   2278 	 * Zombies don't have a stack, so we can't read their psstrings.
   2279 	 * System processes also don't have a user stack.
   2280 	 */
   2281 	if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0) {
   2282 		error = EINVAL;
   2283 		goto out_locked;
   2284 	}
   2285 
   2286 	/*
   2287 	 * Lock the process down in memory.
   2288 	 */
   2289 	/* XXXCDC: how should locking work here? */
   2290 	if ((l->l_flag & L_WEXIT) || (p->p_vmspace->vm_refcnt < 1)) {
   2291 		error = EFAULT;
   2292 		goto out_locked;
   2293 	}
   2294 
   2295 	psstr_addr = (vaddr_t)p->p_psstr;
   2296 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV) {
   2297 		offsetn = p->p_psnargv;
   2298 		offsetv = p->p_psargv;
   2299 	} else {
   2300 		offsetn = p->p_psnenv;
   2301 		offsetv = p->p_psenv;
   2302 	}
   2303 	vmspace = p->p_vmspace;
   2304 	vmspace->vm_refcnt++;	/* XXX */
   2305 
   2306 	rw_exit(&proclist_lock);
   2307 
   2308 	/*
   2309 	 * Allocate a temporary buffer to hold the arguments.
   2310 	 */
   2311 	arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
   2312 
   2313 	/*
   2314 	 * Read in the ps_strings structure.
   2315 	 */
   2316 	aiov.iov_base = &pss;
   2317 	aiov.iov_len = sizeof(pss);
   2318 	auio.uio_iov = &aiov;
   2319 	auio.uio_iovcnt = 1;
   2320 	auio.uio_offset = psstr_addr;
   2321 	auio.uio_resid = sizeof(pss);
   2322 	auio.uio_rw = UIO_READ;
   2323 	UIO_SETUP_SYSSPACE(&auio);
   2324 	error = uvm_io(&vmspace->vm_map, &auio);
   2325 	if (error)
   2326 		goto done;
   2327 
   2328 	memcpy(&nargv, (char *)&pss + offsetn, sizeof(nargv));
   2329 	if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
   2330 		error = dcopyout(l, &nargv, oldp, sizeof(nargv));
   2331 		*oldlenp = sizeof(nargv);
   2332 		goto done;
   2333 	}
   2334 	/*
   2335 	 * Now read the address of the argument vector.
   2336 	 */
   2337 	switch (type) {
   2338 	case KERN_PROC_ARGV:
   2339 		/* FALLTHROUGH */
   2340 	case KERN_PROC_ENV:
   2341 		memcpy(&tmp, (char *)&pss + offsetv, sizeof(tmp));
   2342 		break;
   2343 	default:
   2344 		return (EINVAL);
   2345 	}
   2346 
   2347 #ifdef COMPAT_NETBSD32
   2348 	if (p->p_flag & P_32)
   2349 		len = sizeof(netbsd32_charp) * nargv;
   2350 	else
   2351 #endif
   2352 		len = sizeof(char *) * nargv;
   2353 
   2354 	argv = malloc(len, M_TEMP, M_WAITOK);
   2355 
   2356 	aiov.iov_base = argv;
   2357 	aiov.iov_len = len;
   2358 	auio.uio_iov = &aiov;
   2359 	auio.uio_iovcnt = 1;
   2360 	auio.uio_offset = (off_t)(unsigned long)tmp;
   2361 	auio.uio_resid = len;
   2362 	auio.uio_rw = UIO_READ;
   2363 	UIO_SETUP_SYSSPACE(&auio);
   2364 	error = uvm_io(&vmspace->vm_map, &auio);
   2365 	if (error)
   2366 		goto done;
   2367 
   2368 	/*
   2369 	 * Now copy each string.
   2370 	 */
   2371 	len = 0; /* bytes written to user buffer */
   2372 	for (i = 0; i < nargv; i++) {
   2373 		int finished = 0;
   2374 		vaddr_t base;
   2375 		size_t xlen;
   2376 		int j;
   2377 
   2378 #ifdef COMPAT_NETBSD32
   2379 		if (p->p_flag & P_32) {
   2380 			netbsd32_charp *argv32;
   2381 
   2382 			argv32 = (netbsd32_charp *)argv;
   2383 
   2384 			base = (vaddr_t)NETBSD32PTR64(argv32[i]);
   2385 		} else
   2386 #endif
   2387 			base = (vaddr_t)argv[i];
   2388 
   2389 		while (!finished) {
   2390 			xlen = PAGE_SIZE - (base & PAGE_MASK);
   2391 
   2392 			aiov.iov_base = arg;
   2393 			aiov.iov_len = PAGE_SIZE;
   2394 			auio.uio_iov = &aiov;
   2395 			auio.uio_iovcnt = 1;
   2396 			auio.uio_offset = base;
   2397 			auio.uio_resid = xlen;
   2398 			auio.uio_rw = UIO_READ;
   2399 			UIO_SETUP_SYSSPACE(&auio);
   2400 			error = uvm_io(&vmspace->vm_map, &auio);
   2401 			if (error)
   2402 				goto done;
   2403 
   2404 			/* Look for the end of the string */
   2405 			for (j = 0; j < xlen; j++) {
   2406 				if (arg[j] == '\0') {
   2407 					xlen = j + 1;
   2408 					finished = 1;
   2409 					break;
   2410 				}
   2411 			}
   2412 
   2413 			/* Check for user buffer overflow */
   2414 			if (len + xlen > *oldlenp) {
   2415 				finished = 1;
   2416 				if (len > *oldlenp)
   2417 					xlen = 0;
   2418 				else
   2419 					xlen = *oldlenp - len;
   2420 			}
   2421 
   2422 			/* Copyout the page */
   2423 			error = dcopyout(l, arg, (char *)oldp + len, xlen);
   2424 			if (error)
   2425 				goto done;
   2426 
   2427 			len += xlen;
   2428 			base += xlen;
   2429 		}
   2430 	}
   2431 	*oldlenp = len;
   2432 
   2433 done:
   2434 	if (argv != NULL)
   2435 		free(argv, M_TEMP);
   2436 
   2437 	uvmspace_free(vmspace);
   2438 
   2439 	free(arg, M_TEMP);
   2440 	return error;
   2441 
   2442 out_locked:
   2443 	rw_exit(&proclist_lock);
   2444 	return error;
   2445 }
   2446 
   2447 static int
   2448 sysctl_security_setidcore(SYSCTLFN_ARGS)
   2449 {
   2450 	int newsize, error;
   2451 	struct sysctlnode node;
   2452 
   2453 	node = *rnode;
   2454 	node.sysctl_data = &newsize;
   2455 	newsize = *(int *)rnode->sysctl_data;
   2456 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2457 	if (error || newp == NULL)
   2458 		return error;
   2459 
   2460 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
   2461 	    0, NULL, NULL, NULL))
   2462 		return (EPERM);
   2463 
   2464 	*(int *)rnode->sysctl_data = newsize;
   2465 
   2466 	return 0;
   2467 }
   2468 
   2469 static int
   2470 sysctl_security_setidcorename(SYSCTLFN_ARGS)
   2471 {
   2472 	int error;
   2473 	char *newsetidcorename;
   2474 	struct sysctlnode node;
   2475 
   2476 	newsetidcorename = PNBUF_GET();
   2477 	node = *rnode;
   2478 	node.sysctl_data = newsetidcorename;
   2479 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
   2480 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2481 	if (error || newp == NULL) {
   2482 		goto out;
   2483 	}
   2484 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
   2485 	    0, NULL, NULL, NULL)) {
   2486 		error = EPERM;
   2487 		goto out;
   2488 	}
   2489 	if (strlen(newsetidcorename) == 0) {
   2490 		error = EINVAL;
   2491 		goto out;
   2492 	}
   2493 	memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
   2494 out:
   2495 	PNBUF_PUT(newsetidcorename);
   2496 	return error;
   2497 }
   2498 
   2499 /*
   2500  * sysctl helper routine for kern.cp_id node.  maps cpus to their
   2501  * cpuids.
   2502  */
   2503 static int
   2504 sysctl_kern_cpid(SYSCTLFN_ARGS)
   2505 {
   2506 	struct sysctlnode node = *rnode;
   2507 
   2508 #ifndef MULTIPROCESSOR
   2509 	uint64_t id;
   2510 
   2511 	if (namelen == 1) {
   2512 		if (name[0] != 0)
   2513 			return (ENOENT);
   2514 		/*
   2515 		 * you're allowed to ask for the zero'th processor
   2516 		 */
   2517 		name++;
   2518 		namelen--;
   2519 	}
   2520 	node.sysctl_data = &id;
   2521 	node.sysctl_size = sizeof(id);
   2522 	id = cpu_number();
   2523 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2524 
   2525 #else /* MULTIPROCESSOR */
   2526 	uint64_t *cp_id = NULL;
   2527 	int error, n = ncpu;
   2528 	struct cpu_info *ci;
   2529 	CPU_INFO_ITERATOR cii;
   2530 
   2531 	/*
   2532 	 * here you may either retrieve a single cpu id or the whole
   2533 	 * set.  the size you get back when probing depends on what
   2534 	 * you ask for.
   2535 	 */
   2536 	switch (namelen) {
   2537 	case 0:
   2538 		node.sysctl_size = n * sizeof(uint64_t);
   2539 		n = -2; /* ALL */
   2540 		break;
   2541 	case 1:
   2542 		if (name[0] < 0 || name[0] >= n)
   2543 			return (ENOENT); /* ENOSUCHPROCESSOR */
   2544 		node.sysctl_size = sizeof(uint64_t);
   2545 		n = name[0];
   2546 		/*
   2547 		 * adjust these so that sysctl_lookup() will be happy
   2548 		 */
   2549 		name++;
   2550 		namelen--;
   2551 		break;
   2552 	default:
   2553 		return (EINVAL);
   2554 	}
   2555 
   2556 	cp_id = malloc(node.sysctl_size, M_TEMP, M_WAITOK|M_CANFAIL);
   2557 	if (cp_id == NULL)
   2558 		return (ENOMEM);
   2559 	node.sysctl_data = cp_id;
   2560 	memset(cp_id, 0, node.sysctl_size);
   2561 
   2562 	for (CPU_INFO_FOREACH(cii, ci)) {
   2563 		if (n <= 0)
   2564 			cp_id[0] = ci->ci_cpuid;
   2565 		/*
   2566 		 * if a specific processor was requested and we just
   2567 		 * did it, we're done here
   2568 		 */
   2569 		if (n == 0)
   2570 			break;
   2571 		/*
   2572 		 * if doing "all", skip to next cp_id slot for next processor
   2573 		 */
   2574 		if (n == -2)
   2575 			cp_id++;
   2576 		/*
   2577 		 * if we're doing a specific processor, we're one
   2578 		 * processor closer
   2579 		 */
   2580 		if (n > 0)
   2581 			n--;
   2582 	}
   2583 
   2584 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2585 	free(node.sysctl_data, M_TEMP);
   2586 	return (error);
   2587 
   2588 #endif /* MULTIPROCESSOR */
   2589 }
   2590 
   2591 /*
   2592  * sysctl helper routine for hw.usermem and hw.usermem64.  values are
   2593  * calculate on the fly taking into account integer overflow and the
   2594  * current wired count.
   2595  */
   2596 static int
   2597 sysctl_hw_usermem(SYSCTLFN_ARGS)
   2598 {
   2599 	u_int ui;
   2600 	u_quad_t uq;
   2601 	struct sysctlnode node;
   2602 
   2603 	node = *rnode;
   2604 	switch (rnode->sysctl_num) {
   2605 	    case HW_USERMEM:
   2606 		if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
   2607 			ui = UINT_MAX;
   2608 		else
   2609 			ui *= PAGE_SIZE;
   2610 		node.sysctl_data = &ui;
   2611 		break;
   2612 	case HW_USERMEM64:
   2613 		uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
   2614 		node.sysctl_data = &uq;
   2615 		break;
   2616 	default:
   2617 		return (EINVAL);
   2618 	}
   2619 
   2620 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2621 }
   2622 
   2623 /*
   2624  * sysctl helper routine for kern.cnmagic node.  pulls the old value
   2625  * out, encoded, and stuffs the new value in for decoding.
   2626  */
   2627 static int
   2628 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
   2629 {
   2630 	char magic[CNS_LEN];
   2631 	int error;
   2632 	struct sysctlnode node;
   2633 
   2634 	if (oldp)
   2635 		cn_get_magic(magic, CNS_LEN);
   2636 	node = *rnode;
   2637 	node.sysctl_data = &magic[0];
   2638 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2639 	if (error || newp == NULL)
   2640 		return (error);
   2641 
   2642 	return (cn_set_magic(magic));
   2643 }
   2644 
   2645 static int
   2646 sysctl_hw_ncpu(SYSCTLFN_ARGS)
   2647 {
   2648 	struct sysctlnode node;
   2649 
   2650 	node = *rnode;
   2651 	node.sysctl_data = &ncpu;
   2652 
   2653 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2654 }
   2655 
   2656 
   2657 /*
   2658  * ********************************************************************
   2659  * section 3: public helper routines that are used for more than one
   2660  * node
   2661  * ********************************************************************
   2662  */
   2663 
   2664 /*
   2665  * sysctl helper routine for the kern.root_device node and some ports'
   2666  * machdep.root_device nodes.
   2667  */
   2668 int
   2669 sysctl_root_device(SYSCTLFN_ARGS)
   2670 {
   2671 	struct sysctlnode node;
   2672 
   2673 	node = *rnode;
   2674 	node.sysctl_data = root_device->dv_xname;
   2675 	node.sysctl_size = strlen(root_device->dv_xname) + 1;
   2676 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2677 }
   2678 
   2679 /*
   2680  * sysctl helper routine for kern.consdev, dependent on the current
   2681  * state of the console.  also used for machdep.console_device on some
   2682  * ports.
   2683  */
   2684 int
   2685 sysctl_consdev(SYSCTLFN_ARGS)
   2686 {
   2687 	dev_t consdev;
   2688 	struct sysctlnode node;
   2689 
   2690 	if (cn_tab != NULL)
   2691 		consdev = cn_tab->cn_dev;
   2692 	else
   2693 		consdev = NODEV;
   2694 	node = *rnode;
   2695 	node.sysctl_data = &consdev;
   2696 	node.sysctl_size = sizeof(consdev);
   2697 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2698 }
   2699 
   2700 /*
   2701  * ********************************************************************
   2702  * section 4: support for some helpers
   2703  * ********************************************************************
   2704  */
   2705 
   2706 /*
   2707  * Fill in a kinfo_proc2 structure for the specified process.
   2708  */
   2709 static void
   2710 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki)
   2711 {
   2712 	struct tty *tp;
   2713 	struct lwp *l, *l2;
   2714 	struct timeval ut, st, rt;
   2715 	sigset_t ss1, ss2;
   2716 
   2717 	memset(ki, 0, sizeof(*ki));
   2718 
   2719 	ki->p_paddr = PTRTOUINT64(p);
   2720 	ki->p_fd = PTRTOUINT64(p->p_fd);
   2721 	ki->p_cwdi = PTRTOUINT64(p->p_cwdi);
   2722 	ki->p_stats = PTRTOUINT64(p->p_stats);
   2723 	ki->p_limit = PTRTOUINT64(p->p_limit);
   2724 	ki->p_vmspace = PTRTOUINT64(p->p_vmspace);
   2725 	ki->p_sigacts = PTRTOUINT64(p->p_sigacts);
   2726 	ki->p_sess = PTRTOUINT64(p->p_session);
   2727 	ki->p_tsess = 0;	/* may be changed if controlling tty below */
   2728 	ki->p_ru = PTRTOUINT64(p->p_ru);
   2729 
   2730 	ki->p_eflag = 0;
   2731 	ki->p_exitsig = p->p_exitsig;
   2732 
   2733 	ki->p_flag = sysctl_map_flags(sysctl_flagmap, p->p_flag);
   2734 	ki->p_flag |= sysctl_map_flags(sysctl_sflagmap, p->p_sflag);
   2735 	ki->p_flag |= sysctl_map_flags(sysctl_slflagmap, p->p_slflag);
   2736 	ki->p_flag |= sysctl_map_flags(sysctl_lflagmap, p->p_lflag);
   2737 	ki->p_flag |= sysctl_map_flags(sysctl_stflagmap, p->p_stflag);
   2738 
   2739 	ki->p_pid = p->p_pid;
   2740 	if (p->p_pptr)
   2741 		ki->p_ppid = p->p_pptr->p_pid;
   2742 	else
   2743 		ki->p_ppid = 0;
   2744 	ki->p_sid = p->p_session->s_sid;
   2745 	ki->p__pgid = p->p_pgrp->pg_id;
   2746 
   2747 	ki->p_tpgid = NO_PGID;	/* may be changed if controlling tty below */
   2748 
   2749 	ki->p_uid = kauth_cred_geteuid(p->p_cred);
   2750 	ki->p_ruid = kauth_cred_getuid(p->p_cred);
   2751 	ki->p_gid = kauth_cred_getegid(p->p_cred);
   2752 	ki->p_rgid = kauth_cred_getgid(p->p_cred);
   2753 	ki->p_svuid = kauth_cred_getsvuid(p->p_cred);
   2754 	ki->p_svgid = kauth_cred_getsvgid(p->p_cred);
   2755 
   2756 	ki->p_ngroups = kauth_cred_ngroups(p->p_cred);
   2757 	kauth_cred_getgroups(p->p_cred, ki->p_groups,
   2758 	    min(ki->p_ngroups, sizeof(ki->p_groups) / sizeof(ki->p_groups[0])));
   2759 
   2760 	ki->p_jobc = p->p_pgrp->pg_jobc;
   2761 	if ((p->p_lflag & PL_CONTROLT) && (tp = p->p_session->s_ttyp)) {
   2762 		ki->p_tdev = tp->t_dev;
   2763 		ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   2764 		ki->p_tsess = PTRTOUINT64(tp->t_session);
   2765 	} else {
   2766 		ki->p_tdev = NODEV;
   2767 	}
   2768 
   2769 	ki->p_estcpu = p->p_estcpu;
   2770 	ki->p_cpticks = p->p_cpticks;
   2771 	ki->p_pctcpu = p->p_pctcpu;
   2772 
   2773 	ki->p_uticks = p->p_uticks;
   2774 	ki->p_sticks = p->p_sticks;
   2775 	ki->p_iticks = p->p_iticks;
   2776 
   2777 	ki->p_tracep = PTRTOUINT64(p->p_tracep);
   2778 	ki->p_traceflag = p->p_traceflag;
   2779 
   2780 	mutex_enter(&p->p_smutex);
   2781 
   2782 	memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
   2783 	memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
   2784 
   2785 	ss1 = p->p_sigpend.sp_set;
   2786 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   2787 		/* This is hardly correct, but... */
   2788 		sigplusset(&l->l_sigpend.sp_set, &ss1);
   2789 		sigplusset(&l->l_sigmask, &ss2);
   2790 	}
   2791 	memcpy(&ki->p_siglist, &ss1, sizeof(ki_sigset_t));
   2792 	memcpy(&ki->p_sigmask, &ss2, sizeof(ki_sigset_t));
   2793 
   2794 	ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
   2795 	ki->p_realstat = p->p_stat;
   2796 	ki->p_nice = p->p_nice;
   2797 
   2798 	ki->p_xstat = p->p_xstat;
   2799 	ki->p_acflag = p->p_acflag;
   2800 
   2801 	strncpy(ki->p_comm, p->p_comm,
   2802 	    min(sizeof(ki->p_comm), sizeof(p->p_comm)));
   2803 
   2804 	strncpy(ki->p_login, p->p_session->s_login,
   2805 	    min(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
   2806 
   2807 	ki->p_nlwps = p->p_nlwps;
   2808 	ki->p_realflag = ki->p_flag;
   2809 
   2810 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   2811 		ki->p_vm_rssize = 0;
   2812 		ki->p_vm_tsize = 0;
   2813 		ki->p_vm_dsize = 0;
   2814 		ki->p_vm_ssize = 0;
   2815 		ki->p_nrlwps = 0;
   2816 		l = NULL;
   2817 	} else {
   2818 		struct vmspace *vm = p->p_vmspace;
   2819 		int tmp;
   2820 
   2821 		ki->p_vm_rssize = vm_resident_count(vm);
   2822 		ki->p_vm_tsize = vm->vm_tsize;
   2823 		ki->p_vm_dsize = vm->vm_dsize;
   2824 		ki->p_vm_ssize = vm->vm_ssize;
   2825 
   2826 		/* Pick a "representative" LWP */
   2827 		l = proc_representative_lwp(p, &tmp, 1);
   2828 		lwp_lock(l);
   2829 		ki->p_nrlwps = tmp;
   2830 		ki->p_forw = PTRTOUINT64(l->l_forw);
   2831 		ki->p_back = PTRTOUINT64(l->l_back);
   2832 		ki->p_addr = PTRTOUINT64(l->l_addr);
   2833 		ki->p_stat = l->l_stat;
   2834 		ki->p_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
   2835 		ki->p_swtime = l->l_swtime;
   2836 		ki->p_slptime = l->l_slptime;
   2837 		if (l->l_stat == LSONPROC)
   2838 			ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   2839 		else
   2840 			ki->p_schedflags = 0;
   2841 		ki->p_holdcnt = l->l_holdcnt;
   2842 		ki->p_priority = l->l_priority;
   2843 		ki->p_usrpri = l->l_usrpri;
   2844 		if (l->l_wmesg)
   2845 			strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
   2846 		ki->p_wchan = PTRTOUINT64(l->l_wchan);
   2847 		lwp_unlock(l);
   2848 	}
   2849 	if (p->p_session->s_ttyvp)
   2850 		ki->p_eflag |= EPROC_CTTY;
   2851 	if (SESS_LEADER(p))
   2852 		ki->p_eflag |= EPROC_SLEADER;
   2853 
   2854 	/* XXX Is this double check necessary? */
   2855 	if (P_ZOMBIE(p)) {
   2856 		ki->p_uvalid = 0;
   2857 		ki->p_rtime_sec = 0;
   2858 		ki->p_rtime_usec = 0;
   2859 	} else {
   2860 		ki->p_uvalid = 1;
   2861 
   2862 		ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
   2863 		ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
   2864 
   2865 		calcru(p, &ut, &st, NULL, &rt);
   2866 		ki->p_rtime_sec = rt.tv_sec;
   2867 		ki->p_rtime_usec = rt.tv_usec;
   2868 		ki->p_uutime_sec = ut.tv_sec;
   2869 		ki->p_uutime_usec = ut.tv_usec;
   2870 		ki->p_ustime_sec = st.tv_sec;
   2871 		ki->p_ustime_usec = st.tv_usec;
   2872 
   2873 		ki->p_uru_maxrss = p->p_stats->p_ru.ru_maxrss;
   2874 		ki->p_uru_ixrss = p->p_stats->p_ru.ru_ixrss;
   2875 		ki->p_uru_idrss = p->p_stats->p_ru.ru_idrss;
   2876 		ki->p_uru_isrss = p->p_stats->p_ru.ru_isrss;
   2877 		ki->p_uru_minflt = p->p_stats->p_ru.ru_minflt;
   2878 		ki->p_uru_majflt = p->p_stats->p_ru.ru_majflt;
   2879 		ki->p_uru_nswap = p->p_stats->p_ru.ru_nswap;
   2880 		ki->p_uru_inblock = p->p_stats->p_ru.ru_inblock;
   2881 		ki->p_uru_oublock = p->p_stats->p_ru.ru_oublock;
   2882 		ki->p_uru_msgsnd = p->p_stats->p_ru.ru_msgsnd;
   2883 		ki->p_uru_msgrcv = p->p_stats->p_ru.ru_msgrcv;
   2884 		ki->p_uru_nsignals = p->p_stats->p_ru.ru_nsignals;
   2885 
   2886 		ki->p_uru_nvcsw = 0;
   2887 		ki->p_uru_nivcsw = 0;
   2888 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   2889 			ki->p_uru_nvcsw += l->l_nvcsw;
   2890 			ki->p_uru_nivcsw += l->l_nivcsw;
   2891 		}
   2892 
   2893 		timeradd(&p->p_stats->p_cru.ru_utime,
   2894 			 &p->p_stats->p_cru.ru_stime, &ut);
   2895 		ki->p_uctime_sec = ut.tv_sec;
   2896 		ki->p_uctime_usec = ut.tv_usec;
   2897 	}
   2898 #ifdef MULTIPROCESSOR
   2899 	if (l != NULL)
   2900 		ki->p_cpuid = l->l_cpu->ci_cpuid;
   2901 	else
   2902 #endif
   2903 		ki->p_cpuid = KI_NOCPU;
   2904 
   2905 	mutex_exit(&p->p_smutex);
   2906 }
   2907 
   2908 /*
   2909  * Fill in a kinfo_lwp structure for the specified lwp.
   2910  */
   2911 static void
   2912 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
   2913 {
   2914 
   2915 	kl->l_forw = PTRTOUINT64(l->l_forw);
   2916 	kl->l_back = PTRTOUINT64(l->l_back);
   2917 	kl->l_laddr = PTRTOUINT64(l);
   2918 	kl->l_addr = PTRTOUINT64(l->l_addr);
   2919 	kl->l_stat = l->l_stat;
   2920 	kl->l_lid = l->l_lid;
   2921 	kl->l_flag = sysctl_map_flags(sysctl_lwpprflagmap, l->l_prflag);
   2922 
   2923 	kl->l_swtime = l->l_swtime;
   2924 	kl->l_slptime = l->l_slptime;
   2925 	if (l->l_stat == LSONPROC)
   2926 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   2927 	else
   2928 		kl->l_schedflags = 0;
   2929 	kl->l_holdcnt = l->l_holdcnt;
   2930 	kl->l_priority = l->l_priority;
   2931 	kl->l_usrpri = l->l_usrpri;
   2932 	if (l->l_wmesg)
   2933 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
   2934 	kl->l_wchan = PTRTOUINT64(l->l_wchan);
   2935 #ifdef MULTIPROCESSOR
   2936 	kl->l_cpuid = l->l_cpu->ci_cpuid;
   2937 #else
   2938 	kl->l_cpuid = KI_NOCPU;
   2939 #endif
   2940 }
   2941 
   2942 /*
   2943  * Fill in an eproc structure for the specified process.
   2944  */
   2945 void
   2946 fill_eproc(struct proc *p, struct eproc *ep)
   2947 {
   2948 	struct tty *tp;
   2949 	struct lwp *l;
   2950 
   2951 	ep->e_paddr = p;
   2952 	ep->e_sess = p->p_session;
   2953 	kauth_cred_topcred(p->p_cred, &ep->e_pcred);
   2954 	kauth_cred_toucred(p->p_cred, &ep->e_ucred);
   2955 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   2956 		ep->e_vm.vm_rssize = 0;
   2957 		ep->e_vm.vm_tsize = 0;
   2958 		ep->e_vm.vm_dsize = 0;
   2959 		ep->e_vm.vm_ssize = 0;
   2960 		/* ep->e_vm.vm_pmap = XXX; */
   2961 	} else {
   2962 		struct vmspace *vm = p->p_vmspace;
   2963 
   2964 		ep->e_vm.vm_rssize = vm_resident_count(vm);
   2965 		ep->e_vm.vm_tsize = vm->vm_tsize;
   2966 		ep->e_vm.vm_dsize = vm->vm_dsize;
   2967 		ep->e_vm.vm_ssize = vm->vm_ssize;
   2968 
   2969 		/* Pick a "representative" LWP */
   2970 		mutex_enter(&p->p_smutex);
   2971 		l = proc_representative_lwp(p, NULL, 1);
   2972 		lwp_lock(l);
   2973 		if (l->l_wmesg)
   2974 			strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
   2975 		lwp_unlock(l);
   2976 		mutex_exit(&p->p_smutex);
   2977 	}
   2978 	if (p->p_pptr)
   2979 		ep->e_ppid = p->p_pptr->p_pid;
   2980 	else
   2981 		ep->e_ppid = 0;
   2982 	ep->e_pgid = p->p_pgrp->pg_id;
   2983 	ep->e_sid = ep->e_sess->s_sid;
   2984 	ep->e_jobc = p->p_pgrp->pg_jobc;
   2985 	if ((p->p_lflag & PL_CONTROLT) &&
   2986 	    (tp = ep->e_sess->s_ttyp)) {
   2987 		ep->e_tdev = tp->t_dev;
   2988 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   2989 		ep->e_tsess = tp->t_session;
   2990 	} else
   2991 		ep->e_tdev = NODEV;
   2992 
   2993 	ep->e_xsize = ep->e_xrssize = 0;
   2994 	ep->e_xccount = ep->e_xswrss = 0;
   2995 	ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
   2996 	if (SESS_LEADER(p))
   2997 		ep->e_flag |= EPROC_SLEADER;
   2998 	strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
   2999 }
   3000 
   3001 u_int
   3002 sysctl_map_flags(const u_int *map, u_int word)
   3003 {
   3004 	u_int rv;
   3005 
   3006 	for (rv = 0; *map != 0; map += 2)
   3007 		if ((word & map[0]) != 0)
   3008 			rv |= map[1];
   3009 
   3010 	return rv;
   3011 }
   3012