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