Home | History | Annotate | Line # | Download | only in kern
init_sysctl.c revision 1.92
      1 /*	$NetBSD: init_sysctl.c,v 1.92 2006/11/25 21:40:05 christos 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.92 2006/11/25 21:40:05 christos 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 "veriexec.h"
     48 #include "pty.h"
     49 #include "rnd.h"
     50 
     51 #include <sys/types.h>
     52 #include <sys/param.h>
     53 #include <sys/sysctl.h>
     54 #include <sys/errno.h>
     55 #include <sys/systm.h>
     56 #include <sys/kernel.h>
     57 #include <sys/unistd.h>
     58 #include <sys/disklabel.h>
     59 #include <sys/rnd.h>
     60 #include <sys/vnode.h>
     61 #include <sys/mount.h>
     62 #include <sys/namei.h>
     63 #include <sys/msgbuf.h>
     64 #include <dev/cons.h>
     65 #include <sys/socketvar.h>
     66 #include <sys/file.h>
     67 #include <sys/filedesc.h>
     68 #include <sys/tty.h>
     69 #include <sys/malloc.h>
     70 #include <sys/resource.h>
     71 #include <sys/resourcevar.h>
     72 #include <sys/exec.h>
     73 #include <sys/conf.h>
     74 #include <sys/device.h>
     75 #if NVERIEXEC > 0
     76 #define	VERIEXEC_NEED_NODE
     77 #include <sys/verified_exec.h>
     78 #endif /* NVERIEXEC > 0 */
     79 #include <sys/stat.h>
     80 #include <sys/kauth.h>
     81 #ifdef KTRACE
     82 #include <sys/ktrace.h>
     83 #endif
     84 
     85 #ifdef COMPAT_NETBSD32
     86 #include <compat/netbsd32/netbsd32.h>
     87 #endif
     88 
     89 #include <machine/cpu.h>
     90 
     91 /* XXX this should not be here */
     92 int security_setidcore_dump;
     93 char security_setidcore_path[MAXPATHLEN] = "/var/crash/%n.core";
     94 uid_t security_setidcore_owner = 0;
     95 gid_t security_setidcore_group = 0;
     96 mode_t security_setidcore_mode = (S_IRUSR|S_IWUSR);
     97 
     98 /*
     99  * try over estimating by 5 procs/lwps
    100  */
    101 #define KERN_PROCSLOP	(5 * sizeof(struct kinfo_proc))
    102 #define KERN_LWPSLOP	(5 * sizeof(struct kinfo_lwp))
    103 
    104 #ifdef KTRACE
    105 int dcopyout(struct lwp *, const void *, void *, size_t);
    106 
    107 int
    108 dcopyout(l, kaddr, uaddr, len)
    109 	struct lwp *l;
    110 	const void *kaddr;
    111 	void *uaddr;
    112 	size_t len;
    113 {
    114 	int error;
    115 
    116 	error = copyout(kaddr, uaddr, len);
    117 	if (!error && KTRPOINT(l->l_proc, KTR_MIB)) {
    118 		struct iovec iov;
    119 
    120 		iov.iov_base = uaddr;
    121 		iov.iov_len = len;
    122 		ktrgenio(l, -1, UIO_READ, &iov, len, 0);
    123 	}
    124 	return error;
    125 }
    126 #else /* !KTRACE */
    127 #define dcopyout(l, kaddr, uaddr, len) copyout(kaddr, uaddr, len)
    128 #endif /* KTRACE */
    129 #ifndef MULTIPROCESSOR
    130 #define	sysctl_ncpus()	(1)
    131 #else /* MULTIPROCESSOR */
    132 #ifndef CPU_INFO_FOREACH
    133 #define CPU_INFO_ITERATOR int
    134 #define CPU_INFO_FOREACH(cii, ci) cii = 0, ci = curcpu(); ci != NULL; ci = NULL
    135 #endif
    136 static int
    137 sysctl_ncpus(void)
    138 {
    139 	struct cpu_info *ci;
    140 	CPU_INFO_ITERATOR cii;
    141 
    142 	int ncpus = 0;
    143 	for (CPU_INFO_FOREACH(cii, ci))
    144 		ncpus++;
    145 	return (ncpus);
    146 }
    147 #endif /* MULTIPROCESSOR */
    148 
    149 #ifdef DIAGNOSTIC
    150 static int sysctl_kern_trigger_panic(SYSCTLFN_PROTO);
    151 #endif
    152 static int sysctl_kern_maxvnodes(SYSCTLFN_PROTO);
    153 static int sysctl_kern_rtc_offset(SYSCTLFN_PROTO);
    154 static int sysctl_kern_maxproc(SYSCTLFN_PROTO);
    155 static int sysctl_kern_hostid(SYSCTLFN_PROTO);
    156 static int sysctl_setlen(SYSCTLFN_PROTO);
    157 static int sysctl_kern_clockrate(SYSCTLFN_PROTO);
    158 static int sysctl_kern_file(SYSCTLFN_PROTO);
    159 static int sysctl_kern_autonice(SYSCTLFN_PROTO);
    160 static int sysctl_msgbuf(SYSCTLFN_PROTO);
    161 static int sysctl_kern_defcorename(SYSCTLFN_PROTO);
    162 static int sysctl_kern_cptime(SYSCTLFN_PROTO);
    163 #if NPTY > 0
    164 static int sysctl_kern_maxptys(SYSCTLFN_PROTO);
    165 #endif /* NPTY > 0 */
    166 static int sysctl_kern_sbmax(SYSCTLFN_PROTO);
    167 static int sysctl_kern_urnd(SYSCTLFN_PROTO);
    168 static int sysctl_kern_arnd(SYSCTLFN_PROTO);
    169 static int sysctl_kern_lwp(SYSCTLFN_PROTO);
    170 static int sysctl_kern_forkfsleep(SYSCTLFN_PROTO);
    171 static int sysctl_kern_root_partition(SYSCTLFN_PROTO);
    172 static int sysctl_kern_drivers(SYSCTLFN_PROTO);
    173 static int sysctl_kern_file2(SYSCTLFN_PROTO);
    174 #if NVERIEXEC > 0
    175 static int sysctl_kern_veriexec(SYSCTLFN_PROTO);
    176 #endif /* NVERIEXEC > 0 */
    177 static int sysctl_security_setidcore(SYSCTLFN_PROTO);
    178 static int sysctl_security_setidcorename(SYSCTLFN_PROTO);
    179 static int sysctl_kern_cpid(SYSCTLFN_PROTO);
    180 static int sysctl_doeproc(SYSCTLFN_PROTO);
    181 static int sysctl_kern_proc_args(SYSCTLFN_PROTO);
    182 static int sysctl_hw_usermem(SYSCTLFN_PROTO);
    183 static int sysctl_hw_cnmagic(SYSCTLFN_PROTO);
    184 static int sysctl_hw_ncpu(SYSCTLFN_PROTO);
    185 
    186 static void fill_kproc2(struct proc *, struct kinfo_proc2 *);
    187 static void fill_lwp(struct lwp *l, struct kinfo_lwp *kl);
    188 static void fill_file(struct kinfo_file *, const struct file *, struct proc *,
    189 		      int);
    190 
    191 /*
    192  * ********************************************************************
    193  * section 1: setup routines
    194  * ********************************************************************
    195  * these functions are stuffed into a link set for sysctl setup
    196  * functions.  they're never called or referenced from anywhere else.
    197  * ********************************************************************
    198  */
    199 
    200 /*
    201  * sets up the base nodes...
    202  */
    203 SYSCTL_SETUP(sysctl_root_setup, "sysctl base setup")
    204 {
    205 
    206 	sysctl_createv(clog, 0, NULL, NULL,
    207 		       CTLFLAG_PERMANENT,
    208 		       CTLTYPE_NODE, "kern",
    209 		       SYSCTL_DESCR("High kernel"),
    210 		       NULL, 0, NULL, 0,
    211 		       CTL_KERN, CTL_EOL);
    212 	sysctl_createv(clog, 0, NULL, NULL,
    213 		       CTLFLAG_PERMANENT,
    214 		       CTLTYPE_NODE, "vm",
    215 		       SYSCTL_DESCR("Virtual memory"),
    216 		       NULL, 0, NULL, 0,
    217 		       CTL_VM, CTL_EOL);
    218 	sysctl_createv(clog, 0, NULL, NULL,
    219 		       CTLFLAG_PERMANENT,
    220 		       CTLTYPE_NODE, "vfs",
    221 		       SYSCTL_DESCR("Filesystem"),
    222 		       NULL, 0, NULL, 0,
    223 		       CTL_VFS, CTL_EOL);
    224 	sysctl_createv(clog, 0, NULL, NULL,
    225 		       CTLFLAG_PERMANENT,
    226 		       CTLTYPE_NODE, "net",
    227 		       SYSCTL_DESCR("Networking"),
    228 		       NULL, 0, NULL, 0,
    229 		       CTL_NET, CTL_EOL);
    230 	sysctl_createv(clog, 0, NULL, NULL,
    231 		       CTLFLAG_PERMANENT,
    232 		       CTLTYPE_NODE, "debug",
    233 		       SYSCTL_DESCR("Debugging"),
    234 		       NULL, 0, NULL, 0,
    235 		       CTL_DEBUG, CTL_EOL);
    236 	sysctl_createv(clog, 0, NULL, NULL,
    237 		       CTLFLAG_PERMANENT,
    238 		       CTLTYPE_NODE, "hw",
    239 		       SYSCTL_DESCR("Generic CPU, I/O"),
    240 		       NULL, 0, NULL, 0,
    241 		       CTL_HW, CTL_EOL);
    242 	sysctl_createv(clog, 0, NULL, NULL,
    243 		       CTLFLAG_PERMANENT,
    244 		       CTLTYPE_NODE, "machdep",
    245 		       SYSCTL_DESCR("Machine dependent"),
    246 		       NULL, 0, NULL, 0,
    247 		       CTL_MACHDEP, CTL_EOL);
    248 	/*
    249 	 * this node is inserted so that the sysctl nodes in libc can
    250 	 * operate.
    251 	 */
    252 	sysctl_createv(clog, 0, NULL, NULL,
    253 		       CTLFLAG_PERMANENT,
    254 		       CTLTYPE_NODE, "user",
    255 		       SYSCTL_DESCR("User-level"),
    256 		       NULL, 0, NULL, 0,
    257 		       CTL_USER, CTL_EOL);
    258 	sysctl_createv(clog, 0, NULL, NULL,
    259 		       CTLFLAG_PERMANENT,
    260 		       CTLTYPE_NODE, "ddb",
    261 		       SYSCTL_DESCR("In-kernel debugger"),
    262 		       NULL, 0, NULL, 0,
    263 		       CTL_DDB, CTL_EOL);
    264 	sysctl_createv(clog, 0, NULL, NULL,
    265 		       CTLFLAG_PERMANENT,
    266 		       CTLTYPE_NODE, "proc",
    267 		       SYSCTL_DESCR("Per-process"),
    268 		       NULL, 0, NULL, 0,
    269 		       CTL_PROC, CTL_EOL);
    270 	sysctl_createv(clog, 0, NULL, NULL,
    271 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    272 		       CTLTYPE_NODE, "vendor",
    273 		       SYSCTL_DESCR("Vendor specific"),
    274 		       NULL, 0, NULL, 0,
    275 		       CTL_VENDOR, CTL_EOL);
    276 	sysctl_createv(clog, 0, NULL, NULL,
    277 		       CTLFLAG_PERMANENT,
    278 		       CTLTYPE_NODE, "emul",
    279 		       SYSCTL_DESCR("Emulation settings"),
    280 		       NULL, 0, NULL, 0,
    281 		       CTL_EMUL, CTL_EOL);
    282 	sysctl_createv(clog, 0, NULL, NULL,
    283 		       CTLFLAG_PERMANENT,
    284 		       CTLTYPE_NODE, "security",
    285 		       SYSCTL_DESCR("Security"),
    286 		       NULL, 0, NULL, 0,
    287 		       CTL_SECURITY, CTL_EOL);
    288 }
    289 
    290 /*
    291  * this setup routine is a replacement for kern_sysctl()
    292  */
    293 SYSCTL_SETUP(sysctl_kern_setup, "sysctl kern subtree setup")
    294 {
    295 	extern int kern_logsigexit;	/* defined in kern/kern_sig.c */
    296 	extern fixpt_t ccpu;		/* defined in kern/kern_synch.c */
    297 	extern int dumponpanic;		/* defined in kern/subr_prf.c */
    298 	const struct sysctlnode *rnode;
    299 
    300 	sysctl_createv(clog, 0, NULL, NULL,
    301 		       CTLFLAG_PERMANENT,
    302 		       CTLTYPE_NODE, "kern", NULL,
    303 		       NULL, 0, NULL, 0,
    304 		       CTL_KERN, CTL_EOL);
    305 
    306 	sysctl_createv(clog, 0, NULL, NULL,
    307 		       CTLFLAG_PERMANENT,
    308 		       CTLTYPE_STRING, "ostype",
    309 		       SYSCTL_DESCR("Operating system type"),
    310 		       NULL, 0, &ostype, 0,
    311 		       CTL_KERN, KERN_OSTYPE, CTL_EOL);
    312 	sysctl_createv(clog, 0, NULL, NULL,
    313 		       CTLFLAG_PERMANENT,
    314 		       CTLTYPE_STRING, "osrelease",
    315 		       SYSCTL_DESCR("Operating system release"),
    316 		       NULL, 0, &osrelease, 0,
    317 		       CTL_KERN, KERN_OSRELEASE, CTL_EOL);
    318 	sysctl_createv(clog, 0, NULL, NULL,
    319 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    320 		       CTLTYPE_INT, "osrevision",
    321 		       SYSCTL_DESCR("Operating system revision"),
    322 		       NULL, __NetBSD_Version__, NULL, 0,
    323 		       CTL_KERN, KERN_OSREV, CTL_EOL);
    324 	sysctl_createv(clog, 0, NULL, NULL,
    325 		       CTLFLAG_PERMANENT,
    326 		       CTLTYPE_STRING, "version",
    327 		       SYSCTL_DESCR("Kernel version"),
    328 		       NULL, 0, &version, 0,
    329 		       CTL_KERN, KERN_VERSION, CTL_EOL);
    330 	sysctl_createv(clog, 0, NULL, NULL,
    331 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    332 		       CTLTYPE_INT, "maxvnodes",
    333 		       SYSCTL_DESCR("Maximum number of vnodes"),
    334 		       sysctl_kern_maxvnodes, 0, NULL, 0,
    335 		       CTL_KERN, KERN_MAXVNODES, CTL_EOL);
    336 	sysctl_createv(clog, 0, NULL, NULL,
    337 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    338 		       CTLTYPE_INT, "maxproc",
    339 		       SYSCTL_DESCR("Maximum number of simultaneous processes"),
    340 		       sysctl_kern_maxproc, 0, NULL, 0,
    341 		       CTL_KERN, KERN_MAXPROC, CTL_EOL);
    342 	sysctl_createv(clog, 0, NULL, NULL,
    343 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    344 		       CTLTYPE_INT, "maxfiles",
    345 		       SYSCTL_DESCR("Maximum number of open files"),
    346 		       NULL, 0, &maxfiles, 0,
    347 		       CTL_KERN, KERN_MAXFILES, CTL_EOL);
    348 	sysctl_createv(clog, 0, NULL, NULL,
    349 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    350 		       CTLTYPE_INT, "argmax",
    351 		       SYSCTL_DESCR("Maximum number of bytes of arguments to "
    352 				    "execve(2)"),
    353 		       NULL, ARG_MAX, NULL, 0,
    354 		       CTL_KERN, KERN_ARGMAX, CTL_EOL);
    355 	sysctl_createv(clog, 0, NULL, NULL,
    356 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    357 		       CTLTYPE_STRING, "hostname",
    358 		       SYSCTL_DESCR("System hostname"),
    359 		       sysctl_setlen, 0, &hostname, MAXHOSTNAMELEN,
    360 		       CTL_KERN, KERN_HOSTNAME, CTL_EOL);
    361 	sysctl_createv(clog, 0, NULL, NULL,
    362 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
    363 		       CTLTYPE_INT, "hostid",
    364 		       SYSCTL_DESCR("System host ID number"),
    365 		       sysctl_kern_hostid, 0, NULL, 0,
    366 		       CTL_KERN, KERN_HOSTID, CTL_EOL);
    367 	sysctl_createv(clog, 0, NULL, NULL,
    368 		       CTLFLAG_PERMANENT,
    369 		       CTLTYPE_STRUCT, "clockrate",
    370 		       SYSCTL_DESCR("Kernel clock rates"),
    371 		       sysctl_kern_clockrate, 0, NULL,
    372 		       sizeof(struct clockinfo),
    373 		       CTL_KERN, KERN_CLOCKRATE, CTL_EOL);
    374 	sysctl_createv(clog, 0, NULL, NULL,
    375 		       CTLFLAG_PERMANENT,
    376 		       CTLTYPE_INT, "hardclock_ticks",
    377 		       SYSCTL_DESCR("Number of hardclock ticks"),
    378 		       NULL, 0, &hardclock_ticks, sizeof(hardclock_ticks),
    379 		       CTL_KERN, KERN_HARDCLOCK_TICKS, CTL_EOL);
    380 	sysctl_createv(clog, 0, NULL, NULL,
    381 		       CTLFLAG_PERMANENT,
    382 		       CTLTYPE_STRUCT, "vnode",
    383 		       SYSCTL_DESCR("System vnode table"),
    384 		       sysctl_kern_vnode, 0, NULL, 0,
    385 		       CTL_KERN, KERN_VNODE, CTL_EOL);
    386 	sysctl_createv(clog, 0, NULL, NULL,
    387 		       CTLFLAG_PERMANENT,
    388 		       CTLTYPE_STRUCT, "file",
    389 		       SYSCTL_DESCR("System open file table"),
    390 		       sysctl_kern_file, 0, NULL, 0,
    391 		       CTL_KERN, KERN_FILE, CTL_EOL);
    392 #ifndef GPROF
    393 	sysctl_createv(clog, 0, NULL, NULL,
    394 		       CTLFLAG_PERMANENT,
    395 		       CTLTYPE_NODE, "profiling",
    396 		       SYSCTL_DESCR("Profiling information (not available)"),
    397 		       sysctl_notavail, 0, NULL, 0,
    398 		       CTL_KERN, KERN_PROF, CTL_EOL);
    399 #endif
    400 	sysctl_createv(clog, 0, NULL, NULL,
    401 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    402 		       CTLTYPE_INT, "posix1version",
    403 		       SYSCTL_DESCR("Version of ISO/IEC 9945 (POSIX 1003.1) "
    404 				    "with which the operating system attempts "
    405 				    "to comply"),
    406 		       NULL, _POSIX_VERSION, NULL, 0,
    407 		       CTL_KERN, KERN_POSIX1, CTL_EOL);
    408 	sysctl_createv(clog, 0, NULL, NULL,
    409 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    410 		       CTLTYPE_INT, "ngroups",
    411 		       SYSCTL_DESCR("Maximum number of supplemental groups"),
    412 		       NULL, NGROUPS_MAX, NULL, 0,
    413 		       CTL_KERN, KERN_NGROUPS, CTL_EOL);
    414 	sysctl_createv(clog, 0, NULL, NULL,
    415 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    416 		       CTLTYPE_INT, "job_control",
    417 		       SYSCTL_DESCR("Whether job control is available"),
    418 		       NULL, 1, NULL, 0,
    419 		       CTL_KERN, KERN_JOB_CONTROL, CTL_EOL);
    420 	sysctl_createv(clog, 0, NULL, NULL,
    421 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    422 		       CTLTYPE_INT, "saved_ids",
    423 		       SYSCTL_DESCR("Whether POSIX saved set-group/user ID is "
    424 				    "available"), NULL,
    425 #ifdef _POSIX_SAVED_IDS
    426 		       1,
    427 #else /* _POSIX_SAVED_IDS */
    428 		       0,
    429 #endif /* _POSIX_SAVED_IDS */
    430 		       NULL, 0, CTL_KERN, KERN_SAVED_IDS, CTL_EOL);
    431 	sysctl_createv(clog, 0, NULL, NULL,
    432 		       CTLFLAG_PERMANENT,
    433 		       CTLTYPE_STRUCT, "boottime",
    434 		       SYSCTL_DESCR("System boot time"),
    435 		       NULL, 0, &boottime, sizeof(boottime),
    436 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
    437 	sysctl_createv(clog, 0, NULL, NULL,
    438 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    439 		       CTLTYPE_STRING, "domainname",
    440 		       SYSCTL_DESCR("YP domain name"),
    441 		       sysctl_setlen, 0, &domainname, MAXHOSTNAMELEN,
    442 		       CTL_KERN, KERN_DOMAINNAME, CTL_EOL);
    443 	sysctl_createv(clog, 0, NULL, NULL,
    444 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    445 		       CTLTYPE_INT, "maxpartitions",
    446 		       SYSCTL_DESCR("Maximum number of partitions allowed per "
    447 				    "disk"),
    448 		       NULL, MAXPARTITIONS, NULL, 0,
    449 		       CTL_KERN, KERN_MAXPARTITIONS, CTL_EOL);
    450 	sysctl_createv(clog, 0, NULL, NULL,
    451 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    452 		       CTLTYPE_INT, "rawpartition",
    453 		       SYSCTL_DESCR("Raw partition of a disk"),
    454 		       NULL, RAW_PART, NULL, 0,
    455 		       CTL_KERN, KERN_RAWPARTITION, CTL_EOL);
    456 	sysctl_createv(clog, 0, NULL, NULL,
    457 		       CTLFLAG_PERMANENT,
    458 		       CTLTYPE_STRUCT, "timex", NULL,
    459 		       sysctl_notavail, 0, NULL, 0,
    460 		       CTL_KERN, KERN_TIMEX, CTL_EOL);
    461 	sysctl_createv(clog, 0, NULL, NULL,
    462 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    463 		       CTLTYPE_INT, "autonicetime",
    464 		       SYSCTL_DESCR("CPU clock seconds before non-root "
    465 				    "process priority is lowered"),
    466 		       sysctl_kern_autonice, 0, &autonicetime, 0,
    467 		       CTL_KERN, KERN_AUTONICETIME, CTL_EOL);
    468 	sysctl_createv(clog, 0, NULL, NULL,
    469 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    470 		       CTLTYPE_INT, "autoniceval",
    471 		       SYSCTL_DESCR("Automatic reniced non-root process "
    472 				    "priority"),
    473 		       sysctl_kern_autonice, 0, &autoniceval, 0,
    474 		       CTL_KERN, KERN_AUTONICEVAL, CTL_EOL);
    475 	sysctl_createv(clog, 0, NULL, NULL,
    476 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    477 		       CTLTYPE_INT, "rtc_offset",
    478 		       SYSCTL_DESCR("Offset of real time clock from UTC in "
    479 				    "minutes"),
    480 		       sysctl_kern_rtc_offset, 0, &rtc_offset, 0,
    481 		       CTL_KERN, KERN_RTC_OFFSET, CTL_EOL);
    482 	sysctl_createv(clog, 0, NULL, NULL,
    483 		       CTLFLAG_PERMANENT,
    484 		       CTLTYPE_STRING, "root_device",
    485 		       SYSCTL_DESCR("Name of the root device"),
    486 		       sysctl_root_device, 0, NULL, 0,
    487 		       CTL_KERN, KERN_ROOT_DEVICE, CTL_EOL);
    488 	sysctl_createv(clog, 0, NULL, NULL,
    489 		       CTLFLAG_PERMANENT,
    490 		       CTLTYPE_INT, "msgbufsize",
    491 		       SYSCTL_DESCR("Size of the kernel message buffer"),
    492 		       sysctl_msgbuf, 0, NULL, 0,
    493 		       CTL_KERN, KERN_MSGBUFSIZE, CTL_EOL);
    494 	sysctl_createv(clog, 0, NULL, NULL,
    495 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    496 		       CTLTYPE_INT, "fsync",
    497 		       SYSCTL_DESCR("Whether the POSIX 1003.1b File "
    498 				    "Synchronization Option is available on "
    499 				    "this system"),
    500 		       NULL, 1, NULL, 0,
    501 		       CTL_KERN, KERN_FSYNC, CTL_EOL);
    502 	sysctl_createv(clog, 0, NULL, NULL,
    503 		       CTLFLAG_PERMANENT,
    504 		       CTLTYPE_NODE, "ipc",
    505 		       SYSCTL_DESCR("SysV IPC options"),
    506 		       NULL, 0, NULL, 0,
    507 		       CTL_KERN, KERN_SYSVIPC, CTL_EOL);
    508 	sysctl_createv(clog, 0, NULL, NULL,
    509 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    510 		       CTLTYPE_INT, "sysvmsg",
    511 		       SYSCTL_DESCR("System V style message support available"),
    512 		       NULL,
    513 #ifdef SYSVMSG
    514 		       1,
    515 #else /* SYSVMSG */
    516 		       0,
    517 #endif /* SYSVMSG */
    518 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_MSG, CTL_EOL);
    519 	sysctl_createv(clog, 0, NULL, NULL,
    520 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    521 		       CTLTYPE_INT, "sysvsem",
    522 		       SYSCTL_DESCR("System V style semaphore support "
    523 				    "available"), NULL,
    524 #ifdef SYSVSEM
    525 		       1,
    526 #else /* SYSVSEM */
    527 		       0,
    528 #endif /* SYSVSEM */
    529 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SEM, CTL_EOL);
    530 	sysctl_createv(clog, 0, NULL, NULL,
    531 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    532 		       CTLTYPE_INT, "sysvshm",
    533 		       SYSCTL_DESCR("System V style shared memory support "
    534 				    "available"), NULL,
    535 #ifdef SYSVSHM
    536 		       1,
    537 #else /* SYSVSHM */
    538 		       0,
    539 #endif /* SYSVSHM */
    540 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHM, CTL_EOL);
    541 	sysctl_createv(clog, 0, NULL, NULL,
    542 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    543 		       CTLTYPE_INT, "synchronized_io",
    544 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Synchronized "
    545 				    "I/O Option is available on this system"),
    546 		       NULL, 1, NULL, 0,
    547 		       CTL_KERN, KERN_SYNCHRONIZED_IO, CTL_EOL);
    548 	sysctl_createv(clog, 0, NULL, NULL,
    549 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    550 		       CTLTYPE_INT, "iov_max",
    551 		       SYSCTL_DESCR("Maximum number of iovec structures per "
    552 				    "process"),
    553 		       NULL, IOV_MAX, NULL, 0,
    554 		       CTL_KERN, KERN_IOV_MAX, CTL_EOL);
    555 	sysctl_createv(clog, 0, NULL, NULL,
    556 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    557 		       CTLTYPE_INT, "mapped_files",
    558 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory Mapped "
    559 				    "Files Option is available on this system"),
    560 		       NULL, 1, NULL, 0,
    561 		       CTL_KERN, KERN_MAPPED_FILES, CTL_EOL);
    562 	sysctl_createv(clog, 0, NULL, NULL,
    563 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    564 		       CTLTYPE_INT, "memlock",
    565 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Process Memory "
    566 				    "Locking Option is available on this "
    567 				    "system"),
    568 		       NULL, 1, NULL, 0,
    569 		       CTL_KERN, KERN_MEMLOCK, CTL_EOL);
    570 	sysctl_createv(clog, 0, NULL, NULL,
    571 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    572 		       CTLTYPE_INT, "memlock_range",
    573 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Range Memory "
    574 				    "Locking Option is available on this "
    575 				    "system"),
    576 		       NULL, 1, NULL, 0,
    577 		       CTL_KERN, KERN_MEMLOCK_RANGE, CTL_EOL);
    578 	sysctl_createv(clog, 0, NULL, NULL,
    579 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    580 		       CTLTYPE_INT, "memory_protection",
    581 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory "
    582 				    "Protection Option is available on this "
    583 				    "system"),
    584 		       NULL, 1, NULL, 0,
    585 		       CTL_KERN, KERN_MEMORY_PROTECTION, CTL_EOL);
    586 	sysctl_createv(clog, 0, NULL, NULL,
    587 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    588 		       CTLTYPE_INT, "login_name_max",
    589 		       SYSCTL_DESCR("Maximum login name length"),
    590 		       NULL, LOGIN_NAME_MAX, NULL, 0,
    591 		       CTL_KERN, KERN_LOGIN_NAME_MAX, CTL_EOL);
    592 	sysctl_createv(clog, 0, NULL, NULL,
    593 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    594 		       CTLTYPE_STRING, "defcorename",
    595 		       SYSCTL_DESCR("Default core file name"),
    596 		       sysctl_kern_defcorename, 0, defcorename, MAXPATHLEN,
    597 		       CTL_KERN, KERN_DEFCORENAME, CTL_EOL);
    598 	sysctl_createv(clog, 0, NULL, NULL,
    599 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    600 		       CTLTYPE_INT, "logsigexit",
    601 		       SYSCTL_DESCR("Log process exit when caused by signals"),
    602 		       NULL, 0, &kern_logsigexit, 0,
    603 		       CTL_KERN, KERN_LOGSIGEXIT, CTL_EOL);
    604 	sysctl_createv(clog, 0, NULL, NULL,
    605 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    606 		       CTLTYPE_INT, "fscale",
    607 		       SYSCTL_DESCR("Kernel fixed-point scale factor"),
    608 		       NULL, FSCALE, NULL, 0,
    609 		       CTL_KERN, KERN_FSCALE, CTL_EOL);
    610 	sysctl_createv(clog, 0, NULL, NULL,
    611 		       CTLFLAG_PERMANENT,
    612 		       CTLTYPE_INT, "ccpu",
    613 		       SYSCTL_DESCR("Scheduler exponential decay value"),
    614 		       NULL, 0, &ccpu, 0,
    615 		       CTL_KERN, KERN_CCPU, CTL_EOL);
    616 	sysctl_createv(clog, 0, NULL, NULL,
    617 		       CTLFLAG_PERMANENT,
    618 		       CTLTYPE_STRUCT, "cp_time",
    619 		       SYSCTL_DESCR("Clock ticks spent in different CPU states"),
    620 		       sysctl_kern_cptime, 0, NULL, 0,
    621 		       CTL_KERN, KERN_CP_TIME, CTL_EOL);
    622 	sysctl_createv(clog, 0, NULL, NULL,
    623 		       CTLFLAG_PERMANENT,
    624 		       CTLTYPE_INT, "msgbuf",
    625 		       SYSCTL_DESCR("Kernel message buffer"),
    626 		       sysctl_msgbuf, 0, NULL, 0,
    627 		       CTL_KERN, KERN_MSGBUF, CTL_EOL);
    628 	sysctl_createv(clog, 0, NULL, NULL,
    629 		       CTLFLAG_PERMANENT,
    630 		       CTLTYPE_STRUCT, "consdev",
    631 		       SYSCTL_DESCR("Console device"),
    632 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
    633 		       CTL_KERN, KERN_CONSDEV, CTL_EOL);
    634 #if NPTY > 0
    635 	sysctl_createv(clog, 0, NULL, NULL,
    636 		       CTLFLAG_PERMANENT,
    637 		       CTLTYPE_INT, "maxptys",
    638 		       SYSCTL_DESCR("Maximum number of pseudo-ttys"),
    639 		       sysctl_kern_maxptys, 0, NULL, 0,
    640 		       CTL_KERN, KERN_MAXPTYS, CTL_EOL);
    641 #endif /* NPTY > 0 */
    642 	sysctl_createv(clog, 0, NULL, NULL,
    643 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    644 		       CTLTYPE_INT, "maxphys",
    645 		       SYSCTL_DESCR("Maximum raw I/O transfer size"),
    646 		       NULL, MAXPHYS, NULL, 0,
    647 		       CTL_KERN, KERN_MAXPHYS, CTL_EOL);
    648 	sysctl_createv(clog, 0, NULL, NULL,
    649 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    650 		       CTLTYPE_INT, "sbmax",
    651 		       SYSCTL_DESCR("Maximum socket buffer size"),
    652 		       sysctl_kern_sbmax, 0, NULL, 0,
    653 		       CTL_KERN, KERN_SBMAX, CTL_EOL);
    654 	sysctl_createv(clog, 0, NULL, NULL,
    655 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    656 		       CTLTYPE_INT, "monotonic_clock",
    657 		       SYSCTL_DESCR("Implementation version of the POSIX "
    658 				    "1003.1b Monotonic Clock Option"),
    659 		       /* XXX _POSIX_VERSION */
    660 		       NULL, _POSIX_MONOTONIC_CLOCK, NULL, 0,
    661 		       CTL_KERN, KERN_MONOTONIC_CLOCK, CTL_EOL);
    662 	sysctl_createv(clog, 0, NULL, NULL,
    663 		       CTLFLAG_PERMANENT,
    664 		       CTLTYPE_INT, "urandom",
    665 		       SYSCTL_DESCR("Random integer value"),
    666 		       sysctl_kern_urnd, 0, NULL, 0,
    667 		       CTL_KERN, KERN_URND, CTL_EOL);
    668 	sysctl_createv(clog, 0, NULL, NULL,
    669 		       CTLFLAG_PERMANENT,
    670 		       CTLTYPE_INT, "arandom",
    671 		       SYSCTL_DESCR("n bytes of random data"),
    672 		       sysctl_kern_arnd, 0, NULL, 0,
    673 		       CTL_KERN, KERN_ARND, CTL_EOL);
    674 	sysctl_createv(clog, 0, NULL, NULL,
    675 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    676 		       CTLTYPE_INT, "labelsector",
    677 		       SYSCTL_DESCR("Sector number containing the disklabel"),
    678 		       NULL, LABELSECTOR, NULL, 0,
    679 		       CTL_KERN, KERN_LABELSECTOR, CTL_EOL);
    680 	sysctl_createv(clog, 0, NULL, NULL,
    681 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    682 		       CTLTYPE_INT, "labeloffset",
    683 		       SYSCTL_DESCR("Offset of the disklabel within the "
    684 				    "sector"),
    685 		       NULL, LABELOFFSET, NULL, 0,
    686 		       CTL_KERN, KERN_LABELOFFSET, CTL_EOL);
    687 	sysctl_createv(clog, 0, NULL, NULL,
    688 		       CTLFLAG_PERMANENT,
    689 		       CTLTYPE_NODE, "lwp",
    690 		       SYSCTL_DESCR("System-wide LWP information"),
    691 		       sysctl_kern_lwp, 0, NULL, 0,
    692 		       CTL_KERN, KERN_LWP, CTL_EOL);
    693 	sysctl_createv(clog, 0, NULL, NULL,
    694 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    695 		       CTLTYPE_INT, "forkfsleep",
    696 		       SYSCTL_DESCR("Milliseconds to sleep on fork failure due "
    697 				    "to process limits"),
    698 		       sysctl_kern_forkfsleep, 0, NULL, 0,
    699 		       CTL_KERN, KERN_FORKFSLEEP, CTL_EOL);
    700 	sysctl_createv(clog, 0, NULL, NULL,
    701 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    702 		       CTLTYPE_INT, "posix_threads",
    703 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    704 				    "Threads option to which the system "
    705 				    "attempts to conform"),
    706 		       /* XXX _POSIX_VERSION */
    707 		       NULL, _POSIX_THREADS, NULL, 0,
    708 		       CTL_KERN, KERN_POSIX_THREADS, CTL_EOL);
    709 	sysctl_createv(clog, 0, NULL, NULL,
    710 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    711 		       CTLTYPE_INT, "posix_semaphores",
    712 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    713 				    "Semaphores option to which the system "
    714 				    "attempts to conform"), NULL,
    715 #ifdef P1003_1B_SEMAPHORE
    716 		       200112,
    717 #else /* P1003_1B_SEMAPHORE */
    718 		       0,
    719 #endif /* P1003_1B_SEMAPHORE */
    720 		       NULL, 0, CTL_KERN, KERN_POSIX_SEMAPHORES, CTL_EOL);
    721 	sysctl_createv(clog, 0, NULL, NULL,
    722 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    723 		       CTLTYPE_INT, "posix_barriers",
    724 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    725 				    "Barriers option to which the system "
    726 				    "attempts to conform"),
    727 		       /* XXX _POSIX_VERSION */
    728 		       NULL, _POSIX_BARRIERS, NULL, 0,
    729 		       CTL_KERN, KERN_POSIX_BARRIERS, CTL_EOL);
    730 	sysctl_createv(clog, 0, NULL, NULL,
    731 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    732 		       CTLTYPE_INT, "posix_timers",
    733 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    734 				    "Timers option to which the system "
    735 				    "attempts to conform"),
    736 		       /* XXX _POSIX_VERSION */
    737 		       NULL, _POSIX_TIMERS, NULL, 0,
    738 		       CTL_KERN, KERN_POSIX_TIMERS, CTL_EOL);
    739 	sysctl_createv(clog, 0, NULL, NULL,
    740 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    741 		       CTLTYPE_INT, "posix_spin_locks",
    742 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its Spin "
    743 				    "Locks option to which the system attempts "
    744 				    "to conform"),
    745 		       /* XXX _POSIX_VERSION */
    746 		       NULL, _POSIX_SPIN_LOCKS, NULL, 0,
    747 		       CTL_KERN, KERN_POSIX_SPIN_LOCKS, CTL_EOL);
    748 	sysctl_createv(clog, 0, NULL, NULL,
    749 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    750 		       CTLTYPE_INT, "posix_reader_writer_locks",
    751 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    752 				    "Read-Write Locks option to which the "
    753 				    "system attempts to conform"),
    754 		       /* XXX _POSIX_VERSION */
    755 		       NULL, _POSIX_READER_WRITER_LOCKS, NULL, 0,
    756 		       CTL_KERN, KERN_POSIX_READER_WRITER_LOCKS, CTL_EOL);
    757 	sysctl_createv(clog, 0, NULL, NULL,
    758 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    759 		       CTLTYPE_INT, "dump_on_panic",
    760 		       SYSCTL_DESCR("Perform a crash dump on system panic"),
    761 		       NULL, 0, &dumponpanic, 0,
    762 		       CTL_KERN, KERN_DUMP_ON_PANIC, CTL_EOL);
    763 #ifdef DIAGNOSTIC
    764 	sysctl_createv(clog, 0, NULL, NULL,
    765 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    766 		       CTLTYPE_INT, "panic_now",
    767 		       SYSCTL_DESCR("Trigger a panic"),
    768 		       sysctl_kern_trigger_panic, 0, NULL, 0,
    769 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    770 #endif
    771 	sysctl_createv(clog, 0, NULL, NULL,
    772 		       CTLFLAG_PERMANENT,
    773 		       CTLTYPE_INT, "root_partition",
    774 		       SYSCTL_DESCR("Root partition on the root device"),
    775 		       sysctl_kern_root_partition, 0, NULL, 0,
    776 		       CTL_KERN, KERN_ROOT_PARTITION, CTL_EOL);
    777 	sysctl_createv(clog, 0, NULL, NULL,
    778 		       CTLFLAG_PERMANENT,
    779 		       CTLTYPE_STRUCT, "drivers",
    780 		       SYSCTL_DESCR("List of all drivers with block and "
    781 				    "character device numbers"),
    782 		       sysctl_kern_drivers, 0, NULL, 0,
    783 		       CTL_KERN, KERN_DRIVERS, CTL_EOL);
    784 	sysctl_createv(clog, 0, NULL, NULL,
    785 		       CTLFLAG_PERMANENT,
    786 		       CTLTYPE_STRUCT, "file2",
    787 		       SYSCTL_DESCR("System open file table"),
    788 		       sysctl_kern_file2, 0, NULL, 0,
    789 		       CTL_KERN, KERN_FILE2, CTL_EOL);
    790 #if NVERIEXEC > 0
    791 	sysctl_createv(clog, 0, NULL, NULL,
    792 		       CTLFLAG_PERMANENT,
    793 		       CTLTYPE_NODE, "veriexec",
    794 		       SYSCTL_DESCR("Verified Exec"),
    795 		       NULL, 0, NULL, 0,
    796 		       CTL_KERN, KERN_VERIEXEC, CTL_EOL);
    797 	sysctl_createv(clog, 0, NULL, NULL,
    798 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    799 		       CTLTYPE_INT, "verbose",
    800 		       SYSCTL_DESCR("Verified Exec verbose level"),
    801 		       NULL, 0, &veriexec_verbose, 0,
    802 		       CTL_KERN, KERN_VERIEXEC, VERIEXEC_VERBOSE,
    803 		       CTL_EOL);
    804 	sysctl_createv(clog, 0, NULL, NULL,
    805 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    806 		       CTLTYPE_INT, "strict",
    807 		       SYSCTL_DESCR("Verified Exec strict level"),
    808 		       sysctl_kern_veriexec, 0, NULL, 0,
    809 		       CTL_KERN, KERN_VERIEXEC, VERIEXEC_STRICT, CTL_EOL);
    810 	sysctl_createv(clog, 0, NULL, NULL,
    811 		       CTLFLAG_PERMANENT,
    812 		       CTLTYPE_STRING, "algorithms",
    813 		       SYSCTL_DESCR("Verified Exec supported hashing "
    814 				    "algorithms"),
    815 		       sysctl_kern_veriexec, 0, NULL, 0,
    816 		       CTL_KERN, KERN_VERIEXEC, VERIEXEC_ALGORITHMS, CTL_EOL);
    817 	sysctl_createv(clog, 0, NULL, &veriexec_count_node,
    818 		       CTLFLAG_PERMANENT,
    819 		       CTLTYPE_NODE, "count",
    820 		       SYSCTL_DESCR("Number of fingerprints on device(s)"),
    821 		       NULL, 0, NULL, 0,
    822 		       CTL_KERN, KERN_VERIEXEC, VERIEXEC_COUNT, CTL_EOL);
    823 #endif /* NVERIEXEC > 0 */
    824 	sysctl_createv(clog, 0, NULL, NULL,
    825 		       CTLFLAG_PERMANENT,
    826 		       CTLTYPE_STRUCT, "cp_id",
    827 		       SYSCTL_DESCR("Mapping of CPU number to CPU id"),
    828 		       sysctl_kern_cpid, 0, NULL, 0,
    829 		       CTL_KERN, KERN_CP_ID, CTL_EOL);
    830 
    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 = sysctl_ncpus(), 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 	p = pfind(pid);
   1712 	if (p == NULL)
   1713 		return (ESRCH);
   1714 	LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1715 		if (buflen >= elem_size && elem_count > 0) {
   1716 			fill_lwp(l2, &klwp);
   1717 			/*
   1718 			 * Copy out elem_size, but not larger than
   1719 			 * the size of a struct kinfo_proc2.
   1720 			 */
   1721 			error = dcopyout(l, &klwp, dp,
   1722 			    min(sizeof(klwp), elem_size));
   1723 			if (error)
   1724 				goto cleanup;
   1725 			dp += elem_size;
   1726 			buflen -= elem_size;
   1727 			elem_count--;
   1728 		}
   1729 		needed += elem_size;
   1730 	}
   1731 
   1732 	if (where != NULL) {
   1733 		*oldlenp = dp - where;
   1734 		if (needed > *oldlenp)
   1735 			return (ENOMEM);
   1736 	} else {
   1737 		needed += KERN_LWPSLOP;
   1738 		*oldlenp = needed;
   1739 	}
   1740 	return (0);
   1741  cleanup:
   1742 	return (error);
   1743 }
   1744 
   1745 /*
   1746  * sysctl helper routine for kern.forkfsleep node.  ensures that the
   1747  * given value is not too large or two small, and is at least one
   1748  * timer tick if not zero.
   1749  */
   1750 static int
   1751 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
   1752 {
   1753 	/* userland sees value in ms, internally is in ticks */
   1754 	extern int forkfsleep;		/* defined in kern/kern_fork.c */
   1755 	int error, timo, lsleep;
   1756 	struct sysctlnode node;
   1757 
   1758 	lsleep = forkfsleep * 1000 / hz;
   1759 	node = *rnode;
   1760 	node.sysctl_data = &lsleep;
   1761 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1762 	if (error || newp == NULL)
   1763 		return (error);
   1764 
   1765 	/* refuse negative values, and overly 'long time' */
   1766 	if (lsleep < 0 || lsleep > MAXSLP * 1000)
   1767 		return (EINVAL);
   1768 
   1769 	timo = mstohz(lsleep);
   1770 
   1771 	/* if the interval is >0 ms && <1 tick, use 1 tick */
   1772 	if (lsleep != 0 && timo == 0)
   1773 		forkfsleep = 1;
   1774 	else
   1775 		forkfsleep = timo;
   1776 
   1777 	return (0);
   1778 }
   1779 
   1780 /*
   1781  * sysctl helper routine for kern.root_partition
   1782  */
   1783 static int
   1784 sysctl_kern_root_partition(SYSCTLFN_ARGS)
   1785 {
   1786 	int rootpart = DISKPART(rootdev);
   1787 	struct sysctlnode node = *rnode;
   1788 
   1789 	node.sysctl_data = &rootpart;
   1790 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1791 }
   1792 
   1793 /*
   1794  * sysctl helper function for kern.drivers
   1795  */
   1796 static int
   1797 sysctl_kern_drivers(SYSCTLFN_ARGS)
   1798 {
   1799 	int error;
   1800 	size_t buflen;
   1801 	struct kinfo_drivers kd;
   1802 	char *start, *where;
   1803 	const char *dname;
   1804 	int i;
   1805 	extern struct devsw_conv *devsw_conv;
   1806 	extern int max_devsw_convs;
   1807 
   1808 	if (newp != NULL || namelen != 0)
   1809 		return (EINVAL);
   1810 
   1811 	start = where = oldp;
   1812 	buflen = *oldlenp;
   1813 	if (where == NULL) {
   1814 		*oldlenp = max_devsw_convs * sizeof kd;
   1815 		return 0;
   1816 	}
   1817 
   1818 	/*
   1819 	 * An array of kinfo_drivers structures
   1820 	 */
   1821 	error = 0;
   1822 	for (i = 0; i < max_devsw_convs; i++) {
   1823 		dname = devsw_conv[i].d_name;
   1824 		if (dname == NULL)
   1825 			continue;
   1826 		if (buflen < sizeof kd) {
   1827 			error = ENOMEM;
   1828 			break;
   1829 		}
   1830 		memset(&kd, 0, sizeof(kd));
   1831 		kd.d_bmajor = devsw_conv[i].d_bmajor;
   1832 		kd.d_cmajor = devsw_conv[i].d_cmajor;
   1833 		strlcpy(kd.d_name, dname, sizeof kd.d_name);
   1834 		error = dcopyout(l, &kd, where, sizeof kd);
   1835 		if (error != 0)
   1836 			break;
   1837 		buflen -= sizeof kd;
   1838 		where += sizeof kd;
   1839 	}
   1840 	*oldlenp = where - start;
   1841 	return error;
   1842 }
   1843 
   1844 /*
   1845  * sysctl helper function for kern.file2
   1846  */
   1847 static int
   1848 sysctl_kern_file2(SYSCTLFN_ARGS)
   1849 {
   1850 	struct proc *p;
   1851 	struct file *fp;
   1852 	struct filedesc *fd;
   1853 	struct kinfo_file kf;
   1854 	char *dp;
   1855 	u_int i, op;
   1856 	size_t len, needed, elem_size, out_size;
   1857 	int error, arg, elem_count;
   1858 
   1859 	if (namelen == 1 && name[0] == CTL_QUERY)
   1860 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1861 
   1862 	if (namelen != 4)
   1863 		return (EINVAL);
   1864 
   1865 	error = 0;
   1866 	dp = oldp;
   1867 	len = (oldp != NULL) ? *oldlenp : 0;
   1868 	op = name[0];
   1869 	arg = name[1];
   1870 	elem_size = name[2];
   1871 	elem_count = name[3];
   1872 	out_size = MIN(sizeof(kf), elem_size);
   1873 	needed = 0;
   1874 
   1875 	if (elem_size < 1 || elem_count < 0)
   1876 		return (EINVAL);
   1877 
   1878 	switch (op) {
   1879 	case KERN_FILE_BYFILE:
   1880 		/*
   1881 		 * doesn't use arg so it must be zero
   1882 		 */
   1883 		if (arg != 0)
   1884 			return (EINVAL);
   1885 		LIST_FOREACH(fp, &filehead, f_list) {
   1886 			if (kauth_authorize_generic(l->l_cred,
   1887 			    KAUTH_GENERIC_CANSEE, fp->f_cred) != 0)
   1888 				continue;
   1889 			if (len >= elem_size && elem_count > 0) {
   1890 				fill_file(&kf, fp, NULL, 0);
   1891 				error = dcopyout(l, &kf, dp, out_size);
   1892 				if (error)
   1893 					break;
   1894 				dp += elem_size;
   1895 				len -= elem_size;
   1896 			}
   1897 			if (elem_count > 0) {
   1898 				needed += elem_size;
   1899 				if (elem_count != INT_MAX)
   1900 					elem_count--;
   1901 			}
   1902 		}
   1903 		break;
   1904 	case KERN_FILE_BYPID:
   1905 		if (arg < -1)
   1906 			/* -1 means all processes */
   1907 			return (EINVAL);
   1908 		proclist_lock_read();
   1909 		PROCLIST_FOREACH(p, &allproc) {
   1910 			if (p->p_stat == SIDL)
   1911 				/* skip embryonic processes */
   1912 				continue;
   1913 			if (kauth_authorize_process(l->l_cred,
   1914 			    KAUTH_PROCESS_CANSEE, p, NULL, NULL, NULL) != 0)
   1915 				continue;
   1916 			if (arg > 0 && p->p_pid != arg)
   1917 				/* pick only the one we want */
   1918 				/* XXX want 0 to mean "kernel files" */
   1919 				continue;
   1920 			fd = p->p_fd;
   1921 			for (i = 0; i < fd->fd_nfiles; i++) {
   1922 				fp = fd->fd_ofiles[i];
   1923 				if (fp == NULL || !FILE_IS_USABLE(fp))
   1924 					continue;
   1925 				if (len >= elem_size && elem_count > 0) {
   1926 					fill_file(&kf, fd->fd_ofiles[i],
   1927 						  p, i);
   1928 					error = dcopyout(l, &kf, dp, out_size);
   1929 					if (error)
   1930 						break;
   1931 					dp += elem_size;
   1932 					len -= elem_size;
   1933 				}
   1934 				if (elem_count > 0) {
   1935 					needed += elem_size;
   1936 					if (elem_count != INT_MAX)
   1937 						elem_count--;
   1938 				}
   1939 			}
   1940 		}
   1941 		proclist_unlock_read();
   1942 		break;
   1943 	default:
   1944 		return (EINVAL);
   1945 	}
   1946 
   1947 	if (oldp == NULL)
   1948 		needed += KERN_FILESLOP * elem_size;
   1949 	*oldlenp = needed;
   1950 
   1951 	return (error);
   1952 }
   1953 
   1954 static void
   1955 fill_file(struct kinfo_file *kp, const struct file *fp, struct proc *p, int i)
   1956 {
   1957 
   1958 	memset(kp, 0, sizeof(*kp));
   1959 
   1960 	kp->ki_fileaddr =	PTRTOUINT64(fp);
   1961 	kp->ki_flag =		fp->f_flag;
   1962 	kp->ki_iflags =		fp->f_iflags;
   1963 	kp->ki_ftype =		fp->f_type;
   1964 	kp->ki_count =		fp->f_count;
   1965 	kp->ki_msgcount =	fp->f_msgcount;
   1966 	kp->ki_usecount =	fp->f_usecount;
   1967 	kp->ki_fucred =		PTRTOUINT64(fp->f_cred);
   1968 	kp->ki_fuid =		kauth_cred_geteuid(fp->f_cred);
   1969 	kp->ki_fgid =		kauth_cred_getegid(fp->f_cred);
   1970 	kp->ki_fops =		PTRTOUINT64(fp->f_ops);
   1971 	kp->ki_foffset =	fp->f_offset;
   1972 	kp->ki_fdata =		PTRTOUINT64(fp->f_data);
   1973 
   1974 	/* vnode information to glue this file to something */
   1975 	if (fp->f_type == DTYPE_VNODE) {
   1976 		struct vnode *vp = (struct vnode *)fp->f_data;
   1977 
   1978 		kp->ki_vun =	PTRTOUINT64(vp->v_un.vu_socket);
   1979 		kp->ki_vsize =	vp->v_size;
   1980 		kp->ki_vtype =	vp->v_type;
   1981 		kp->ki_vtag =	vp->v_tag;
   1982 		kp->ki_vdata =	PTRTOUINT64(vp->v_data);
   1983 	}
   1984 
   1985         /* process information when retrieved via KERN_FILE_BYPID */
   1986 	if (p) {
   1987 		kp->ki_pid =		p->p_pid;
   1988 		kp->ki_fd =		i;
   1989 		kp->ki_ofileflags =	p->p_fd->fd_ofileflags[i];
   1990 	}
   1991 }
   1992 
   1993 static int
   1994 sysctl_doeproc(SYSCTLFN_ARGS)
   1995 {
   1996 	struct eproc *eproc;
   1997 	struct kinfo_proc2 *kproc2;
   1998 	struct kinfo_proc *dp;
   1999 	struct proc *p;
   2000 	const struct proclist_desc *pd;
   2001 	char *where, *dp2;
   2002 	int type, op, arg;
   2003 	u_int elem_size, elem_count;
   2004 	size_t buflen, needed;
   2005 	int error;
   2006 
   2007 	if (namelen == 1 && name[0] == CTL_QUERY)
   2008 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   2009 
   2010 	dp = oldp;
   2011 	dp2 = where = oldp;
   2012 	buflen = where != NULL ? *oldlenp : 0;
   2013 	error = 0;
   2014 	needed = 0;
   2015 	type = rnode->sysctl_num;
   2016 
   2017 	if (type == KERN_PROC) {
   2018 		if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
   2019 			return (EINVAL);
   2020 		op = name[0];
   2021 		if (op != KERN_PROC_ALL)
   2022 			arg = name[1];
   2023 		else
   2024 			arg = 0;		/* Quell compiler warning */
   2025 		elem_size = elem_count = 0;	/* Ditto */
   2026 	} else {
   2027 		if (namelen != 4)
   2028 			return (EINVAL);
   2029 		op = name[0];
   2030 		arg = name[1];
   2031 		elem_size = name[2];
   2032 		elem_count = name[3];
   2033 	}
   2034 
   2035 	if (type == KERN_PROC) {
   2036 		eproc = malloc(sizeof(*eproc), M_TEMP, M_WAITOK);
   2037 		kproc2 = NULL;
   2038 	} else {
   2039 		eproc = NULL;
   2040 		kproc2 = malloc(sizeof(*kproc2), M_TEMP, M_WAITOK);
   2041 	}
   2042 	proclist_lock_read();
   2043 
   2044 	pd = proclists;
   2045 again:
   2046 	PROCLIST_FOREACH(p, pd->pd_list) {
   2047 		/*
   2048 		 * Skip embryonic processes.
   2049 		 */
   2050 		if (p->p_stat == SIDL)
   2051 			continue;
   2052 
   2053 		if (kauth_authorize_process(l->l_cred,
   2054 		    KAUTH_PROCESS_CANSEE, p, NULL, NULL, NULL) != 0)
   2055 			continue;
   2056 
   2057 		/*
   2058 		 * TODO - make more efficient (see notes below).
   2059 		 * do by session.
   2060 		 */
   2061 		switch (op) {
   2062 
   2063 		case KERN_PROC_PID:
   2064 			/* could do this with just a lookup */
   2065 			if (p->p_pid != (pid_t)arg)
   2066 				continue;
   2067 			break;
   2068 
   2069 		case KERN_PROC_PGRP:
   2070 			/* could do this by traversing pgrp */
   2071 			if (p->p_pgrp->pg_id != (pid_t)arg)
   2072 				continue;
   2073 			break;
   2074 
   2075 		case KERN_PROC_SESSION:
   2076 			if (p->p_session->s_sid != (pid_t)arg)
   2077 				continue;
   2078 			break;
   2079 
   2080 		case KERN_PROC_TTY:
   2081 			if (arg == (int) KERN_PROC_TTY_REVOKE) {
   2082 				if ((p->p_flag & P_CONTROLT) == 0 ||
   2083 				    p->p_session->s_ttyp == NULL ||
   2084 				    p->p_session->s_ttyvp != NULL)
   2085 					continue;
   2086 			} else if ((p->p_flag & P_CONTROLT) == 0 ||
   2087 			    p->p_session->s_ttyp == NULL) {
   2088 				if ((dev_t)arg != KERN_PROC_TTY_NODEV)
   2089 					continue;
   2090 			} else if (p->p_session->s_ttyp->t_dev != (dev_t)arg)
   2091 				continue;
   2092 			break;
   2093 
   2094 		case KERN_PROC_UID:
   2095 			if (kauth_cred_geteuid(p->p_cred) != (uid_t)arg)
   2096 				continue;
   2097 			break;
   2098 
   2099 		case KERN_PROC_RUID:
   2100 			if (kauth_cred_getuid(p->p_cred) != (uid_t)arg)
   2101 				continue;
   2102 			break;
   2103 
   2104 		case KERN_PROC_GID:
   2105 			if (kauth_cred_getegid(p->p_cred) != (uid_t)arg)
   2106 				continue;
   2107 			break;
   2108 
   2109 		case KERN_PROC_RGID:
   2110 			if (kauth_cred_getgid(p->p_cred) != (uid_t)arg)
   2111 				continue;
   2112 			break;
   2113 
   2114 		case KERN_PROC_ALL:
   2115 			/* allow everything */
   2116 			break;
   2117 
   2118 		default:
   2119 			error = EINVAL;
   2120 			goto cleanup;
   2121 		}
   2122 		if (type == KERN_PROC) {
   2123 			if (buflen >= sizeof(struct kinfo_proc)) {
   2124 				fill_eproc(p, eproc);
   2125 				error = dcopyout(l, p, &dp->kp_proc,
   2126 				    sizeof(struct proc));
   2127 				if (error)
   2128 					goto cleanup;
   2129 				error = dcopyout(l, eproc, &dp->kp_eproc,
   2130 				    sizeof(*eproc));
   2131 				if (error)
   2132 					goto cleanup;
   2133 				dp++;
   2134 				buflen -= sizeof(struct kinfo_proc);
   2135 			}
   2136 			needed += sizeof(struct kinfo_proc);
   2137 		} else { /* KERN_PROC2 */
   2138 			if (buflen >= elem_size && elem_count > 0) {
   2139 				fill_kproc2(p, kproc2);
   2140 				/*
   2141 				 * Copy out elem_size, but not larger than
   2142 				 * the size of a struct kinfo_proc2.
   2143 				 */
   2144 				error = dcopyout(l, kproc2, dp2,
   2145 				    min(sizeof(*kproc2), elem_size));
   2146 				if (error)
   2147 					goto cleanup;
   2148 				dp2 += elem_size;
   2149 				buflen -= elem_size;
   2150 				elem_count--;
   2151 			}
   2152 			needed += elem_size;
   2153 		}
   2154 	}
   2155 	pd++;
   2156 	if (pd->pd_list != NULL)
   2157 		goto again;
   2158 	proclist_unlock_read();
   2159 
   2160 	if (where != NULL) {
   2161 		if (type == KERN_PROC)
   2162 			*oldlenp = (char *)dp - where;
   2163 		else
   2164 			*oldlenp = dp2 - where;
   2165 		if (needed > *oldlenp) {
   2166 			error = ENOMEM;
   2167 			goto out;
   2168 		}
   2169 	} else {
   2170 		needed += KERN_PROCSLOP;
   2171 		*oldlenp = needed;
   2172 	}
   2173 	if (kproc2)
   2174 		free(kproc2, M_TEMP);
   2175 	if (eproc)
   2176 		free(eproc, M_TEMP);
   2177 	return 0;
   2178  cleanup:
   2179 	proclist_unlock_read();
   2180  out:
   2181 	if (kproc2)
   2182 		free(kproc2, M_TEMP);
   2183 	if (eproc)
   2184 		free(eproc, M_TEMP);
   2185 	return error;
   2186 }
   2187 
   2188 /*
   2189  * sysctl helper routine for kern.proc_args pseudo-subtree.
   2190  */
   2191 static int
   2192 sysctl_kern_proc_args(SYSCTLFN_ARGS)
   2193 {
   2194 	struct ps_strings pss;
   2195 	struct proc *p;
   2196 	size_t len, i;
   2197 	struct uio auio;
   2198 	struct iovec aiov;
   2199 	pid_t pid;
   2200 	int nargv, type, error;
   2201 	char *arg;
   2202 	char **argv = NULL;
   2203 	char *tmp;
   2204 	struct vmspace *vmspace;
   2205 	vaddr_t psstr_addr;
   2206 	vaddr_t offsetn;
   2207 	vaddr_t offsetv;
   2208 
   2209 	if (namelen == 1 && name[0] == CTL_QUERY)
   2210 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   2211 
   2212 	if (newp != NULL || namelen != 2)
   2213 		return (EINVAL);
   2214 	pid = name[0];
   2215 	type = name[1];
   2216 
   2217 	switch (type) {
   2218 	case KERN_PROC_ARGV:
   2219 	case KERN_PROC_NARGV:
   2220 	case KERN_PROC_ENV:
   2221 	case KERN_PROC_NENV:
   2222 		/* ok */
   2223 		break;
   2224 	default:
   2225 		return (EINVAL);
   2226 	}
   2227 
   2228 	proclist_lock_read();
   2229 
   2230 	/* check pid */
   2231 	if ((p = p_find(pid, PFIND_LOCKED)) == NULL) {
   2232 		error = EINVAL;
   2233 		goto out_locked;
   2234 	}
   2235 
   2236 	error = kauth_authorize_process(l->l_cred,
   2237 	    KAUTH_PROCESS_CANSEE, p, NULL, NULL, NULL);
   2238 	if (error) {
   2239 		goto out_locked;
   2240 	}
   2241 
   2242 	/* only root or same user change look at the environment */
   2243 	if (type == KERN_PROC_ENV || type == KERN_PROC_NENV) {
   2244 		if (kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
   2245 		     p, NULL, NULL, NULL)) {
   2246 				error = EPERM;
   2247 				goto out_locked;
   2248 		}
   2249 	}
   2250 
   2251 	if (oldp == NULL) {
   2252 		if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
   2253 			*oldlenp = sizeof (int);
   2254 		else
   2255 			*oldlenp = ARG_MAX;	/* XXX XXX XXX */
   2256 		error = 0;
   2257 		goto out_locked;
   2258 	}
   2259 
   2260 	/*
   2261 	 * Zombies don't have a stack, so we can't read their psstrings.
   2262 	 * System processes also don't have a user stack.
   2263 	 */
   2264 	if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0) {
   2265 		error = EINVAL;
   2266 		goto out_locked;
   2267 	}
   2268 
   2269 	/*
   2270 	 * Lock the process down in memory.
   2271 	 */
   2272 	/* XXXCDC: how should locking work here? */
   2273 	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1)) {
   2274 		error = EFAULT;
   2275 		goto out_locked;
   2276 	}
   2277 
   2278 	psstr_addr = (vaddr_t)p->p_psstr;
   2279 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV) {
   2280 		offsetn = p->p_psnargv;
   2281 		offsetv = p->p_psargv;
   2282 	} else {
   2283 		offsetn = p->p_psnenv;
   2284 		offsetv = p->p_psenv;
   2285 	}
   2286 	vmspace = p->p_vmspace;
   2287 	vmspace->vm_refcnt++;	/* XXX */
   2288 
   2289 	proclist_unlock_read();
   2290 
   2291 	/*
   2292 	 * Allocate a temporary buffer to hold the arguments.
   2293 	 */
   2294 	arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
   2295 
   2296 	/*
   2297 	 * Read in the ps_strings structure.
   2298 	 */
   2299 	aiov.iov_base = &pss;
   2300 	aiov.iov_len = sizeof(pss);
   2301 	auio.uio_iov = &aiov;
   2302 	auio.uio_iovcnt = 1;
   2303 	auio.uio_offset = psstr_addr;
   2304 	auio.uio_resid = sizeof(pss);
   2305 	auio.uio_rw = UIO_READ;
   2306 	UIO_SETUP_SYSSPACE(&auio);
   2307 	error = uvm_io(&vmspace->vm_map, &auio);
   2308 	if (error)
   2309 		goto done;
   2310 
   2311 	memcpy(&nargv, (char *)&pss + offsetn, sizeof(nargv));
   2312 	if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
   2313 		error = dcopyout(l, &nargv, oldp, sizeof(nargv));
   2314 		*oldlenp = sizeof(nargv);
   2315 		goto done;
   2316 	}
   2317 	/*
   2318 	 * Now read the address of the argument vector.
   2319 	 */
   2320 	switch (type) {
   2321 	case KERN_PROC_ARGV:
   2322 		/* FALLTHROUGH */
   2323 	case KERN_PROC_ENV:
   2324 		memcpy(&tmp, (char *)&pss + offsetv, sizeof(tmp));
   2325 		break;
   2326 	default:
   2327 		return (EINVAL);
   2328 	}
   2329 
   2330 #ifdef COMPAT_NETBSD32
   2331 	if (p->p_flag & P_32)
   2332 		len = sizeof(netbsd32_charp) * nargv;
   2333 	else
   2334 #endif
   2335 		len = sizeof(char *) * nargv;
   2336 
   2337 	argv = malloc(len, M_TEMP, M_WAITOK);
   2338 
   2339 	aiov.iov_base = argv;
   2340 	aiov.iov_len = len;
   2341 	auio.uio_iov = &aiov;
   2342 	auio.uio_iovcnt = 1;
   2343 	auio.uio_offset = (off_t)(unsigned long)tmp;
   2344 	auio.uio_resid = len;
   2345 	auio.uio_rw = UIO_READ;
   2346 	UIO_SETUP_SYSSPACE(&auio);
   2347 	error = uvm_io(&vmspace->vm_map, &auio);
   2348 	if (error)
   2349 		goto done;
   2350 
   2351 	/*
   2352 	 * Now copy each string.
   2353 	 */
   2354 	len = 0; /* bytes written to user buffer */
   2355 	for (i = 0; i < nargv; i++) {
   2356 		int finished = 0;
   2357 		vaddr_t base;
   2358 		size_t xlen;
   2359 		int j;
   2360 
   2361 #ifdef COMPAT_NETBSD32
   2362 		if (p->p_flag & P_32) {
   2363 			netbsd32_charp *argv32;
   2364 
   2365 			argv32 = (netbsd32_charp *)argv;
   2366 
   2367 			base = (vaddr_t)NETBSD32PTR64(argv32[i]);
   2368 		} else
   2369 #endif
   2370 			base = (vaddr_t)argv[i];
   2371 
   2372 		while (!finished) {
   2373 			xlen = PAGE_SIZE - (base & PAGE_MASK);
   2374 
   2375 			aiov.iov_base = arg;
   2376 			aiov.iov_len = PAGE_SIZE;
   2377 			auio.uio_iov = &aiov;
   2378 			auio.uio_iovcnt = 1;
   2379 			auio.uio_offset = base;
   2380 			auio.uio_resid = xlen;
   2381 			auio.uio_rw = UIO_READ;
   2382 			UIO_SETUP_SYSSPACE(&auio);
   2383 			error = uvm_io(&vmspace->vm_map, &auio);
   2384 			if (error)
   2385 				goto done;
   2386 
   2387 			/* Look for the end of the string */
   2388 			for (j = 0; j < xlen; j++) {
   2389 				if (arg[j] == '\0') {
   2390 					xlen = j + 1;
   2391 					finished = 1;
   2392 					break;
   2393 				}
   2394 			}
   2395 
   2396 			/* Check for user buffer overflow */
   2397 			if (len + xlen > *oldlenp) {
   2398 				finished = 1;
   2399 				if (len > *oldlenp)
   2400 					xlen = 0;
   2401 				else
   2402 					xlen = *oldlenp - len;
   2403 			}
   2404 
   2405 			/* Copyout the page */
   2406 			error = dcopyout(l, arg, (char *)oldp + len, xlen);
   2407 			if (error)
   2408 				goto done;
   2409 
   2410 			len += xlen;
   2411 			base += xlen;
   2412 		}
   2413 	}
   2414 	*oldlenp = len;
   2415 
   2416 done:
   2417 	if (argv != NULL)
   2418 		free(argv, M_TEMP);
   2419 
   2420 	uvmspace_free(vmspace);
   2421 
   2422 	free(arg, M_TEMP);
   2423 	return error;
   2424 
   2425 out_locked:
   2426 	proclist_unlock_read();
   2427 	return error;
   2428 }
   2429 
   2430 /*
   2431  * Sysctl helper routine for Verified Exec.
   2432  */
   2433 #if NVERIEXEC > 0
   2434 static int
   2435 sysctl_kern_veriexec(SYSCTLFN_ARGS)
   2436 {
   2437 	int newval, error;
   2438 	int *var = NULL, raise_only = 0;
   2439 	struct sysctlnode node;
   2440 
   2441 	node = *rnode;
   2442 
   2443 	switch (rnode->sysctl_num) {
   2444 	case VERIEXEC_STRICT:
   2445 		raise_only = 1;
   2446 		var = &veriexec_strict;
   2447 		break;
   2448 	case VERIEXEC_ALGORITHMS:
   2449 		node.sysctl_data = veriexec_fp_names;
   2450 		node.sysctl_size = strlen(veriexec_fp_names) + 1;
   2451 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2452 	default:
   2453 		return (EINVAL);
   2454 	}
   2455 
   2456 	newval = *var;
   2457 
   2458 	node.sysctl_data = &newval;
   2459 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2460 	if (error || newp == NULL) {
   2461 		return (error);
   2462 	}
   2463 
   2464 	if (raise_only && (newval < *var))
   2465 		return (EPERM);
   2466 
   2467 	*var = newval;
   2468 
   2469 	return (error);
   2470 }
   2471 #endif /* NVERIEXEC > 0 */
   2472 
   2473 static int
   2474 sysctl_security_setidcore(SYSCTLFN_ARGS)
   2475 {
   2476 	int newsize, error;
   2477 	struct sysctlnode node;
   2478 
   2479 	node = *rnode;
   2480 	node.sysctl_data = &newsize;
   2481 	newsize = *(int *)rnode->sysctl_data;
   2482 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2483 	if (error || newp == NULL)
   2484 		return error;
   2485 
   2486 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
   2487 	    0, NULL, NULL, NULL))
   2488 		return (EPERM);
   2489 
   2490 	*(int *)rnode->sysctl_data = newsize;
   2491 
   2492 	return 0;
   2493 }
   2494 
   2495 static int
   2496 sysctl_security_setidcorename(SYSCTLFN_ARGS)
   2497 {
   2498 	int error;
   2499 	char *newsetidcorename;
   2500 	struct sysctlnode node;
   2501 
   2502 	newsetidcorename = PNBUF_GET();
   2503 	node = *rnode;
   2504 	node.sysctl_data = newsetidcorename;
   2505 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
   2506 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2507 	if (error || newp == NULL) {
   2508 		goto out;
   2509 	}
   2510 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
   2511 	    0, NULL, NULL, NULL)) {
   2512 		error = EPERM;
   2513 		goto out;
   2514 	}
   2515 	if (strlen(newsetidcorename) == 0) {
   2516 		error = EINVAL;
   2517 		goto out;
   2518 	}
   2519 	memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
   2520 out:
   2521 	PNBUF_PUT(newsetidcorename);
   2522 	return error;
   2523 }
   2524 
   2525 /*
   2526  * sysctl helper routine for kern.cp_id node.  maps cpus to their
   2527  * cpuids.
   2528  */
   2529 static int
   2530 sysctl_kern_cpid(SYSCTLFN_ARGS)
   2531 {
   2532 	struct sysctlnode node = *rnode;
   2533 
   2534 #ifndef MULTIPROCESSOR
   2535 	uint64_t id;
   2536 
   2537 	if (namelen == 1) {
   2538 		if (name[0] != 0)
   2539 			return (ENOENT);
   2540 		/*
   2541 		 * you're allowed to ask for the zero'th processor
   2542 		 */
   2543 		name++;
   2544 		namelen--;
   2545 	}
   2546 	node.sysctl_data = &id;
   2547 	node.sysctl_size = sizeof(id);
   2548 	id = cpu_number();
   2549 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2550 
   2551 #else /* MULTIPROCESSOR */
   2552 	uint64_t *cp_id = NULL;
   2553 	int error, n = sysctl_ncpus();
   2554 	struct cpu_info *ci;
   2555 	CPU_INFO_ITERATOR cii;
   2556 
   2557 	/*
   2558 	 * here you may either retrieve a single cpu id or the whole
   2559 	 * set.  the size you get back when probing depends on what
   2560 	 * you ask for.
   2561 	 */
   2562 	switch (namelen) {
   2563 	case 0:
   2564 		node.sysctl_size = n * sizeof(uint64_t);
   2565 		n = -2; /* ALL */
   2566 		break;
   2567 	case 1:
   2568 		if (name[0] < 0 || name[0] >= n)
   2569 			return (ENOENT); /* ENOSUCHPROCESSOR */
   2570 		node.sysctl_size = sizeof(uint64_t);
   2571 		n = name[0];
   2572 		/*
   2573 		 * adjust these so that sysctl_lookup() will be happy
   2574 		 */
   2575 		name++;
   2576 		namelen--;
   2577 		break;
   2578 	default:
   2579 		return (EINVAL);
   2580 	}
   2581 
   2582 	cp_id = malloc(node.sysctl_size, M_TEMP, M_WAITOK|M_CANFAIL);
   2583 	if (cp_id == NULL)
   2584 		return (ENOMEM);
   2585 	node.sysctl_data = cp_id;
   2586 	memset(cp_id, 0, node.sysctl_size);
   2587 
   2588 	for (CPU_INFO_FOREACH(cii, ci)) {
   2589 		if (n <= 0)
   2590 			cp_id[0] = ci->ci_cpuid;
   2591 		/*
   2592 		 * if a specific processor was requested and we just
   2593 		 * did it, we're done here
   2594 		 */
   2595 		if (n == 0)
   2596 			break;
   2597 		/*
   2598 		 * if doing "all", skip to next cp_id slot for next processor
   2599 		 */
   2600 		if (n == -2)
   2601 			cp_id++;
   2602 		/*
   2603 		 * if we're doing a specific processor, we're one
   2604 		 * processor closer
   2605 		 */
   2606 		if (n > 0)
   2607 			n--;
   2608 	}
   2609 
   2610 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2611 	free(node.sysctl_data, M_TEMP);
   2612 	return (error);
   2613 
   2614 #endif /* MULTIPROCESSOR */
   2615 }
   2616 
   2617 /*
   2618  * sysctl helper routine for hw.usermem and hw.usermem64.  values are
   2619  * calculate on the fly taking into account integer overflow and the
   2620  * current wired count.
   2621  */
   2622 static int
   2623 sysctl_hw_usermem(SYSCTLFN_ARGS)
   2624 {
   2625 	u_int ui;
   2626 	u_quad_t uq;
   2627 	struct sysctlnode node;
   2628 
   2629 	node = *rnode;
   2630 	switch (rnode->sysctl_num) {
   2631 	    case HW_USERMEM:
   2632 		if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
   2633 			ui = UINT_MAX;
   2634 		else
   2635 			ui *= PAGE_SIZE;
   2636 		node.sysctl_data = &ui;
   2637 		break;
   2638 	case HW_USERMEM64:
   2639 		uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
   2640 		node.sysctl_data = &uq;
   2641 		break;
   2642 	default:
   2643 		return (EINVAL);
   2644 	}
   2645 
   2646 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2647 }
   2648 
   2649 /*
   2650  * sysctl helper routine for kern.cnmagic node.  pulls the old value
   2651  * out, encoded, and stuffs the new value in for decoding.
   2652  */
   2653 static int
   2654 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
   2655 {
   2656 	char magic[CNS_LEN];
   2657 	int error;
   2658 	struct sysctlnode node;
   2659 
   2660 	if (oldp)
   2661 		cn_get_magic(magic, CNS_LEN);
   2662 	node = *rnode;
   2663 	node.sysctl_data = &magic[0];
   2664 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2665 	if (error || newp == NULL)
   2666 		return (error);
   2667 
   2668 	return (cn_set_magic(magic));
   2669 }
   2670 
   2671 static int
   2672 sysctl_hw_ncpu(SYSCTLFN_ARGS)
   2673 {
   2674 	int ncpu;
   2675 	struct sysctlnode node;
   2676 
   2677 	ncpu = sysctl_ncpus();
   2678 	node = *rnode;
   2679 	node.sysctl_data = &ncpu;
   2680 
   2681 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2682 }
   2683 
   2684 
   2685 /*
   2686  * ********************************************************************
   2687  * section 3: public helper routines that are used for more than one
   2688  * node
   2689  * ********************************************************************
   2690  */
   2691 
   2692 /*
   2693  * sysctl helper routine for the kern.root_device node and some ports'
   2694  * machdep.root_device nodes.
   2695  */
   2696 int
   2697 sysctl_root_device(SYSCTLFN_ARGS)
   2698 {
   2699 	struct sysctlnode node;
   2700 
   2701 	node = *rnode;
   2702 	node.sysctl_data = root_device->dv_xname;
   2703 	node.sysctl_size = strlen(root_device->dv_xname) + 1;
   2704 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2705 }
   2706 
   2707 /*
   2708  * sysctl helper routine for kern.consdev, dependent on the current
   2709  * state of the console.  also used for machdep.console_device on some
   2710  * ports.
   2711  */
   2712 int
   2713 sysctl_consdev(SYSCTLFN_ARGS)
   2714 {
   2715 	dev_t consdev;
   2716 	struct sysctlnode node;
   2717 
   2718 	if (cn_tab != NULL)
   2719 		consdev = cn_tab->cn_dev;
   2720 	else
   2721 		consdev = NODEV;
   2722 	node = *rnode;
   2723 	node.sysctl_data = &consdev;
   2724 	node.sysctl_size = sizeof(consdev);
   2725 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2726 }
   2727 
   2728 /*
   2729  * ********************************************************************
   2730  * section 4: support for some helpers
   2731  * ********************************************************************
   2732  */
   2733 
   2734 /*
   2735  * Fill in a kinfo_proc2 structure for the specified process.
   2736  */
   2737 static void
   2738 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki)
   2739 {
   2740 	struct tty *tp;
   2741 	struct lwp *l;
   2742 	struct timeval ut, st;
   2743 
   2744 	memset(ki, 0, sizeof(*ki));
   2745 
   2746 	ki->p_paddr = PTRTOUINT64(p);
   2747 	ki->p_fd = PTRTOUINT64(p->p_fd);
   2748 	ki->p_cwdi = PTRTOUINT64(p->p_cwdi);
   2749 	ki->p_stats = PTRTOUINT64(p->p_stats);
   2750 	ki->p_limit = PTRTOUINT64(p->p_limit);
   2751 	ki->p_vmspace = PTRTOUINT64(p->p_vmspace);
   2752 	ki->p_sigacts = PTRTOUINT64(p->p_sigacts);
   2753 	ki->p_sess = PTRTOUINT64(p->p_session);
   2754 	ki->p_tsess = 0;	/* may be changed if controlling tty below */
   2755 	ki->p_ru = PTRTOUINT64(p->p_ru);
   2756 
   2757 	ki->p_eflag = 0;
   2758 	ki->p_exitsig = p->p_exitsig;
   2759 	ki->p_flag = p->p_flag;
   2760 
   2761 	ki->p_pid = p->p_pid;
   2762 	if (p->p_pptr)
   2763 		ki->p_ppid = p->p_pptr->p_pid;
   2764 	else
   2765 		ki->p_ppid = 0;
   2766 	ki->p_sid = p->p_session->s_sid;
   2767 	ki->p__pgid = p->p_pgrp->pg_id;
   2768 
   2769 	ki->p_tpgid = NO_PGID;	/* may be changed if controlling tty below */
   2770 
   2771 	ki->p_uid = kauth_cred_geteuid(p->p_cred);
   2772 	ki->p_ruid = kauth_cred_getuid(p->p_cred);
   2773 	ki->p_gid = kauth_cred_getegid(p->p_cred);
   2774 	ki->p_rgid = kauth_cred_getgid(p->p_cred);
   2775 	ki->p_svuid = kauth_cred_getsvuid(p->p_cred);
   2776 	ki->p_svgid = kauth_cred_getsvgid(p->p_cred);
   2777 
   2778 	ki->p_ngroups = kauth_cred_ngroups(p->p_cred);
   2779 	kauth_cred_getgroups(p->p_cred, ki->p_groups,
   2780 	    min(ki->p_ngroups, sizeof(ki->p_groups) / sizeof(ki->p_groups[0])));
   2781 
   2782 	ki->p_jobc = p->p_pgrp->pg_jobc;
   2783 	if ((p->p_flag & P_CONTROLT) && (tp = p->p_session->s_ttyp)) {
   2784 		ki->p_tdev = tp->t_dev;
   2785 		ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   2786 		ki->p_tsess = PTRTOUINT64(tp->t_session);
   2787 	} else {
   2788 		ki->p_tdev = NODEV;
   2789 	}
   2790 
   2791 	ki->p_estcpu = p->p_estcpu;
   2792 	ki->p_rtime_sec = p->p_rtime.tv_sec;
   2793 	ki->p_rtime_usec = p->p_rtime.tv_usec;
   2794 	ki->p_cpticks = p->p_cpticks;
   2795 	ki->p_pctcpu = p->p_pctcpu;
   2796 
   2797 	ki->p_uticks = p->p_uticks;
   2798 	ki->p_sticks = p->p_sticks;
   2799 	ki->p_iticks = p->p_iticks;
   2800 
   2801 	ki->p_tracep = PTRTOUINT64(p->p_tracep);
   2802 	ki->p_traceflag = p->p_traceflag;
   2803 
   2804 
   2805 	memcpy(&ki->p_siglist, &p->p_sigctx.ps_siglist, sizeof(ki_sigset_t));
   2806 	memcpy(&ki->p_sigmask, &p->p_sigctx.ps_sigmask, sizeof(ki_sigset_t));
   2807 	memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
   2808 	memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
   2809 
   2810 	ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
   2811 	ki->p_realstat = p->p_stat;
   2812 	ki->p_nice = p->p_nice;
   2813 
   2814 	ki->p_xstat = p->p_xstat;
   2815 	ki->p_acflag = p->p_acflag;
   2816 
   2817 	strncpy(ki->p_comm, p->p_comm,
   2818 	    min(sizeof(ki->p_comm), sizeof(p->p_comm)));
   2819 
   2820 	strncpy(ki->p_login, p->p_session->s_login,
   2821 	    min(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
   2822 
   2823 	ki->p_nlwps = p->p_nlwps;
   2824 	ki->p_nrlwps = p->p_nrlwps;
   2825 	ki->p_realflag = p->p_flag;
   2826 
   2827 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   2828 		ki->p_vm_rssize = 0;
   2829 		ki->p_vm_tsize = 0;
   2830 		ki->p_vm_dsize = 0;
   2831 		ki->p_vm_ssize = 0;
   2832 		l = NULL;
   2833 	} else {
   2834 		struct vmspace *vm = p->p_vmspace;
   2835 
   2836 		ki->p_vm_rssize = vm_resident_count(vm);
   2837 		ki->p_vm_tsize = vm->vm_tsize;
   2838 		ki->p_vm_dsize = vm->vm_dsize;
   2839 		ki->p_vm_ssize = vm->vm_ssize;
   2840 
   2841 		/* Pick a "representative" LWP */
   2842 		l = proc_representative_lwp(p);
   2843 		ki->p_forw = PTRTOUINT64(l->l_forw);
   2844 		ki->p_back = PTRTOUINT64(l->l_back);
   2845 		ki->p_addr = PTRTOUINT64(l->l_addr);
   2846 		ki->p_stat = l->l_stat;
   2847 		ki->p_flag |= l->l_flag & P_SHARED;
   2848 		ki->p_swtime = l->l_swtime;
   2849 		ki->p_slptime = l->l_slptime;
   2850 		if (l->l_stat == LSONPROC) {
   2851 			KDASSERT(l->l_cpu != NULL);
   2852 			ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   2853 		} else
   2854 			ki->p_schedflags = 0;
   2855 		ki->p_holdcnt = l->l_holdcnt;
   2856 		ki->p_priority = l->l_priority;
   2857 		ki->p_usrpri = l->l_usrpri;
   2858 		if (l->l_wmesg)
   2859 			strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
   2860 		ki->p_wchan = PTRTOUINT64(l->l_wchan);
   2861 
   2862 	}
   2863 
   2864 	if (p->p_session->s_ttyvp)
   2865 		ki->p_eflag |= EPROC_CTTY;
   2866 	if (SESS_LEADER(p))
   2867 		ki->p_eflag |= EPROC_SLEADER;
   2868 
   2869 	/* XXX Is this double check necessary? */
   2870 	if (P_ZOMBIE(p)) {
   2871 		ki->p_uvalid = 0;
   2872 	} else {
   2873 		ki->p_uvalid = 1;
   2874 
   2875 		ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
   2876 		ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
   2877 
   2878 		calcru(p, &ut, &st, 0);
   2879 		ki->p_uutime_sec = ut.tv_sec;
   2880 		ki->p_uutime_usec = ut.tv_usec;
   2881 		ki->p_ustime_sec = st.tv_sec;
   2882 		ki->p_ustime_usec = st.tv_usec;
   2883 
   2884 		ki->p_uru_maxrss = p->p_stats->p_ru.ru_maxrss;
   2885 		ki->p_uru_ixrss = p->p_stats->p_ru.ru_ixrss;
   2886 		ki->p_uru_idrss = p->p_stats->p_ru.ru_idrss;
   2887 		ki->p_uru_isrss = p->p_stats->p_ru.ru_isrss;
   2888 		ki->p_uru_minflt = p->p_stats->p_ru.ru_minflt;
   2889 		ki->p_uru_majflt = p->p_stats->p_ru.ru_majflt;
   2890 		ki->p_uru_nswap = p->p_stats->p_ru.ru_nswap;
   2891 		ki->p_uru_inblock = p->p_stats->p_ru.ru_inblock;
   2892 		ki->p_uru_oublock = p->p_stats->p_ru.ru_oublock;
   2893 		ki->p_uru_msgsnd = p->p_stats->p_ru.ru_msgsnd;
   2894 		ki->p_uru_msgrcv = p->p_stats->p_ru.ru_msgrcv;
   2895 		ki->p_uru_nsignals = p->p_stats->p_ru.ru_nsignals;
   2896 		ki->p_uru_nvcsw = p->p_stats->p_ru.ru_nvcsw;
   2897 		ki->p_uru_nivcsw = p->p_stats->p_ru.ru_nivcsw;
   2898 
   2899 		timeradd(&p->p_stats->p_cru.ru_utime,
   2900 			 &p->p_stats->p_cru.ru_stime, &ut);
   2901 		ki->p_uctime_sec = ut.tv_sec;
   2902 		ki->p_uctime_usec = ut.tv_usec;
   2903 	}
   2904 #ifdef MULTIPROCESSOR
   2905 	if (l && l->l_cpu != NULL)
   2906 		ki->p_cpuid = l->l_cpu->ci_cpuid;
   2907 	else
   2908 #endif
   2909 		ki->p_cpuid = KI_NOCPU;
   2910 }
   2911 
   2912 /*
   2913  * Fill in a kinfo_lwp structure for the specified lwp.
   2914  */
   2915 static void
   2916 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
   2917 {
   2918 
   2919 	kl->l_forw = PTRTOUINT64(l->l_forw);
   2920 	kl->l_back = PTRTOUINT64(l->l_back);
   2921 	kl->l_laddr = PTRTOUINT64(l);
   2922 	kl->l_addr = PTRTOUINT64(l->l_addr);
   2923 	kl->l_stat = l->l_stat;
   2924 	kl->l_lid = l->l_lid;
   2925 	kl->l_flag = l->l_flag;
   2926 
   2927 	kl->l_swtime = l->l_swtime;
   2928 	kl->l_slptime = l->l_slptime;
   2929 	if (l->l_stat == LSONPROC) {
   2930 		KDASSERT(l->l_cpu != NULL);
   2931 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   2932 	} else
   2933 		kl->l_schedflags = 0;
   2934 	kl->l_holdcnt = l->l_holdcnt;
   2935 	kl->l_priority = l->l_priority;
   2936 	kl->l_usrpri = l->l_usrpri;
   2937 	if (l->l_wmesg)
   2938 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
   2939 	kl->l_wchan = PTRTOUINT64(l->l_wchan);
   2940 #ifdef MULTIPROCESSOR
   2941 	if (l->l_cpu != NULL)
   2942 		kl->l_cpuid = l->l_cpu->ci_cpuid;
   2943 	else
   2944 #endif
   2945 		kl->l_cpuid = KI_NOCPU;
   2946 }
   2947 
   2948 /*
   2949  * Fill in an eproc structure for the specified process.
   2950  */
   2951 void
   2952 fill_eproc(struct proc *p, struct eproc *ep)
   2953 {
   2954 	struct tty *tp;
   2955 	struct lwp *l;
   2956 
   2957 	ep->e_paddr = p;
   2958 	ep->e_sess = p->p_session;
   2959 	kauth_cred_topcred(p->p_cred, &ep->e_pcred);
   2960 	kauth_cred_toucred(p->p_cred, &ep->e_ucred);
   2961 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   2962 		ep->e_vm.vm_rssize = 0;
   2963 		ep->e_vm.vm_tsize = 0;
   2964 		ep->e_vm.vm_dsize = 0;
   2965 		ep->e_vm.vm_ssize = 0;
   2966 		/* ep->e_vm.vm_pmap = XXX; */
   2967 	} else {
   2968 		struct vmspace *vm = p->p_vmspace;
   2969 
   2970 		ep->e_vm.vm_rssize = vm_resident_count(vm);
   2971 		ep->e_vm.vm_tsize = vm->vm_tsize;
   2972 		ep->e_vm.vm_dsize = vm->vm_dsize;
   2973 		ep->e_vm.vm_ssize = vm->vm_ssize;
   2974 
   2975 		/* Pick a "representative" LWP */
   2976 		l = proc_representative_lwp(p);
   2977 
   2978 		if (l->l_wmesg)
   2979 			strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
   2980 	}
   2981 	if (p->p_pptr)
   2982 		ep->e_ppid = p->p_pptr->p_pid;
   2983 	else
   2984 		ep->e_ppid = 0;
   2985 	ep->e_pgid = p->p_pgrp->pg_id;
   2986 	ep->e_sid = ep->e_sess->s_sid;
   2987 	ep->e_jobc = p->p_pgrp->pg_jobc;
   2988 	if ((p->p_flag & P_CONTROLT) &&
   2989 	    (tp = ep->e_sess->s_ttyp)) {
   2990 		ep->e_tdev = tp->t_dev;
   2991 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   2992 		ep->e_tsess = tp->t_session;
   2993 	} else
   2994 		ep->e_tdev = NODEV;
   2995 
   2996 	ep->e_xsize = ep->e_xrssize = 0;
   2997 	ep->e_xccount = ep->e_xswrss = 0;
   2998 	ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
   2999 	if (SESS_LEADER(p))
   3000 		ep->e_flag |= EPROC_SLEADER;
   3001 	strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
   3002 }
   3003