Home | History | Annotate | Line # | Download | only in kern
init_sysctl.c revision 1.186.2.1
      1 /*	$NetBSD: init_sysctl.c,v 1.186.2.1 2013/03/14 16:33:09 riz Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2003, 2007, 2008, 2009 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, and by Andrew Doran.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: init_sysctl.c,v 1.186.2.1 2013/03/14 16:33:09 riz Exp $");
     34 
     35 #include "opt_sysv.h"
     36 #include "opt_compat_netbsd.h"
     37 #include "opt_modular.h"
     38 #include "opt_sa.h"
     39 #include "opt_posix.h"
     40 #include "pty.h"
     41 
     42 #include <sys/types.h>
     43 #include <sys/param.h>
     44 #include <sys/sysctl.h>
     45 #include <sys/cpu.h>
     46 #include <sys/errno.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/unistd.h>
     50 #include <sys/disklabel.h>
     51 #include <sys/cprng.h>
     52 #include <sys/vnode.h>
     53 #include <sys/mount.h>
     54 #include <sys/namei.h>
     55 #include <sys/msgbuf.h>
     56 #include <dev/cons.h>
     57 #include <sys/socketvar.h>
     58 #include <sys/file.h>
     59 #include <sys/filedesc.h>
     60 #include <sys/tty.h>
     61 #include <sys/kmem.h>
     62 #include <sys/resource.h>
     63 #include <sys/resourcevar.h>
     64 #include <sys/exec.h>
     65 #include <sys/conf.h>
     66 #include <sys/device.h>
     67 #include <sys/stat.h>
     68 #include <sys/kauth.h>
     69 #include <sys/ktrace.h>
     70 #include <sys/ksem.h>
     71 
     72 #ifdef KERN_SA
     73 #include <sys/sa.h>
     74 #endif
     75 
     76 #include <sys/cpu.h>
     77 
     78 #if defined(MODULAR) || defined(P1003_1B_SEMAPHORE)
     79 int posix_semaphores = 200112;
     80 #else
     81 int posix_semaphores;
     82 #endif
     83 
     84 int security_setidcore_dump;
     85 char security_setidcore_path[MAXPATHLEN] = "/var/crash/%n.core";
     86 uid_t security_setidcore_owner = 0;
     87 gid_t security_setidcore_group = 0;
     88 mode_t security_setidcore_mode = (S_IRUSR|S_IWUSR);
     89 
     90 static const u_int sysctl_flagmap[] = {
     91 	PK_ADVLOCK, P_ADVLOCK,
     92 	PK_EXEC, P_EXEC,
     93 	PK_NOCLDWAIT, P_NOCLDWAIT,
     94 	PK_32, P_32,
     95 	PK_CLDSIGIGN, P_CLDSIGIGN,
     96 	PK_SUGID, P_SUGID,
     97 	0
     98 };
     99 
    100 static const u_int sysctl_sflagmap[] = {
    101 	PS_NOCLDSTOP, P_NOCLDSTOP,
    102 	PS_WEXIT, P_WEXIT,
    103 	PS_STOPFORK, P_STOPFORK,
    104 	PS_STOPEXEC, P_STOPEXEC,
    105 	PS_STOPEXIT, P_STOPEXIT,
    106 	0
    107 };
    108 
    109 static const u_int sysctl_slflagmap[] = {
    110 	PSL_TRACED, P_TRACED,
    111 	PSL_FSTRACE, P_FSTRACE,
    112 	PSL_CHTRACED, P_CHTRACED,
    113 	PSL_SYSCALL, P_SYSCALL,
    114 	0
    115 };
    116 
    117 static const u_int sysctl_lflagmap[] = {
    118 	PL_CONTROLT, P_CONTROLT,
    119 	PL_PPWAIT, P_PPWAIT,
    120 	0
    121 };
    122 
    123 static const u_int sysctl_stflagmap[] = {
    124 	PST_PROFIL, P_PROFIL,
    125 	0
    126 
    127 };
    128 
    129 static const u_int sysctl_lwpprflagmap[] = {
    130 	LPR_DETACHED, L_DETACHED,
    131 	0
    132 };
    133 
    134 /*
    135  * try over estimating by 5 procs/lwps
    136  */
    137 #define KERN_LWPSLOP	(5 * sizeof(struct kinfo_lwp))
    138 
    139 static int dcopyout(struct lwp *, const void *, void *, size_t);
    140 
    141 static int
    142 dcopyout(struct lwp *l, const void *kaddr, void *uaddr, size_t len)
    143 {
    144 	int error;
    145 
    146 	error = copyout(kaddr, uaddr, len);
    147 	ktrmibio(-1, UIO_READ, uaddr, len, error);
    148 
    149 	return error;
    150 }
    151 
    152 #ifdef DIAGNOSTIC
    153 static int sysctl_kern_trigger_panic(SYSCTLFN_PROTO);
    154 #endif
    155 static int sysctl_kern_maxvnodes(SYSCTLFN_PROTO);
    156 static int sysctl_kern_rtc_offset(SYSCTLFN_PROTO);
    157 static int sysctl_kern_maxproc(SYSCTLFN_PROTO);
    158 static int sysctl_kern_hostid(SYSCTLFN_PROTO);
    159 static int sysctl_setlen(SYSCTLFN_PROTO);
    160 static int sysctl_kern_clockrate(SYSCTLFN_PROTO);
    161 static int sysctl_msgbuf(SYSCTLFN_PROTO);
    162 static int sysctl_kern_defcorename(SYSCTLFN_PROTO);
    163 static int sysctl_kern_cptime(SYSCTLFN_PROTO);
    164 #if NPTY > 0
    165 static int sysctl_kern_maxptys(SYSCTLFN_PROTO);
    166 #endif /* NPTY > 0 */
    167 static int sysctl_kern_sbmax(SYSCTLFN_PROTO);
    168 static int sysctl_kern_urnd(SYSCTLFN_PROTO);
    169 static int sysctl_kern_arnd(SYSCTLFN_PROTO);
    170 static int sysctl_kern_lwp(SYSCTLFN_PROTO);
    171 static int sysctl_kern_forkfsleep(SYSCTLFN_PROTO);
    172 static int sysctl_kern_root_partition(SYSCTLFN_PROTO);
    173 static int sysctl_kern_drivers(SYSCTLFN_PROTO);
    174 static int sysctl_security_setidcore(SYSCTLFN_PROTO);
    175 static int sysctl_security_setidcorename(SYSCTLFN_PROTO);
    176 static int sysctl_kern_cpid(SYSCTLFN_PROTO);
    177 static int sysctl_hw_usermem(SYSCTLFN_PROTO);
    178 static int sysctl_hw_cnmagic(SYSCTLFN_PROTO);
    179 
    180 static void fill_lwp(struct lwp *l, struct kinfo_lwp *kl);
    181 
    182 /*
    183  * ********************************************************************
    184  * section 1: setup routines
    185  * ********************************************************************
    186  * These functions are stuffed into a link set for sysctl setup
    187  * functions. They're never called or referenced from anywhere else.
    188  * ********************************************************************
    189  */
    190 
    191 /*
    192  * this setup routine is a replacement for kern_sysctl()
    193  */
    194 SYSCTL_SETUP(sysctl_kern_setup, "sysctl kern subtree setup")
    195 {
    196 	extern int kern_logsigexit;	/* defined in kern/kern_sig.c */
    197 	extern fixpt_t ccpu;		/* defined in kern/kern_synch.c */
    198 	extern int dumponpanic;		/* defined in kern/subr_prf.c */
    199 	const struct sysctlnode *rnode;
    200 
    201 	sysctl_createv(clog, 0, NULL, NULL,
    202 		       CTLFLAG_PERMANENT,
    203 		       CTLTYPE_NODE, "kern", NULL,
    204 		       NULL, 0, NULL, 0,
    205 		       CTL_KERN, CTL_EOL);
    206 
    207 	sysctl_createv(clog, 0, NULL, NULL,
    208 		       CTLFLAG_PERMANENT,
    209 		       CTLTYPE_STRING, "ostype",
    210 		       SYSCTL_DESCR("Operating system type"),
    211 		       NULL, 0, __UNCONST(&ostype), 0,
    212 		       CTL_KERN, KERN_OSTYPE, CTL_EOL);
    213 	sysctl_createv(clog, 0, NULL, NULL,
    214 		       CTLFLAG_PERMANENT,
    215 		       CTLTYPE_STRING, "osrelease",
    216 		       SYSCTL_DESCR("Operating system release"),
    217 		       NULL, 0, __UNCONST(&osrelease), 0,
    218 		       CTL_KERN, KERN_OSRELEASE, CTL_EOL);
    219 	sysctl_createv(clog, 0, NULL, NULL,
    220 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    221 		       CTLTYPE_INT, "osrevision",
    222 		       SYSCTL_DESCR("Operating system revision"),
    223 		       NULL, __NetBSD_Version__, NULL, 0,
    224 		       CTL_KERN, KERN_OSREV, CTL_EOL);
    225 	sysctl_createv(clog, 0, NULL, NULL,
    226 		       CTLFLAG_PERMANENT,
    227 		       CTLTYPE_STRING, "version",
    228 		       SYSCTL_DESCR("Kernel version"),
    229 		       NULL, 0, __UNCONST(&version), 0,
    230 		       CTL_KERN, KERN_VERSION, CTL_EOL);
    231 	sysctl_createv(clog, 0, NULL, NULL,
    232 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    233 		       CTLTYPE_INT, "maxvnodes",
    234 		       SYSCTL_DESCR("Maximum number of vnodes"),
    235 		       sysctl_kern_maxvnodes, 0, NULL, 0,
    236 		       CTL_KERN, KERN_MAXVNODES, CTL_EOL);
    237 	sysctl_createv(clog, 0, NULL, NULL,
    238 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    239 		       CTLTYPE_INT, "maxproc",
    240 		       SYSCTL_DESCR("Maximum number of simultaneous processes"),
    241 		       sysctl_kern_maxproc, 0, NULL, 0,
    242 		       CTL_KERN, KERN_MAXPROC, CTL_EOL);
    243 	sysctl_createv(clog, 0, NULL, NULL,
    244 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    245 		       CTLTYPE_INT, "maxfiles",
    246 		       SYSCTL_DESCR("Maximum number of open files"),
    247 		       NULL, 0, &maxfiles, 0,
    248 		       CTL_KERN, KERN_MAXFILES, CTL_EOL);
    249 	sysctl_createv(clog, 0, NULL, NULL,
    250 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    251 		       CTLTYPE_INT, "argmax",
    252 		       SYSCTL_DESCR("Maximum number of bytes of arguments to "
    253 				    "execve(2)"),
    254 		       NULL, ARG_MAX, NULL, 0,
    255 		       CTL_KERN, KERN_ARGMAX, CTL_EOL);
    256 	sysctl_createv(clog, 0, NULL, NULL,
    257 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    258 		       CTLTYPE_STRING, "hostname",
    259 		       SYSCTL_DESCR("System hostname"),
    260 		       sysctl_setlen, 0, &hostname, MAXHOSTNAMELEN,
    261 		       CTL_KERN, KERN_HOSTNAME, CTL_EOL);
    262 	sysctl_createv(clog, 0, NULL, NULL,
    263 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
    264 		       CTLTYPE_INT, "hostid",
    265 		       SYSCTL_DESCR("System host ID number"),
    266 		       sysctl_kern_hostid, 0, NULL, 0,
    267 		       CTL_KERN, KERN_HOSTID, CTL_EOL);
    268 	sysctl_createv(clog, 0, NULL, NULL,
    269 		       CTLFLAG_PERMANENT,
    270 		       CTLTYPE_STRUCT, "clockrate",
    271 		       SYSCTL_DESCR("Kernel clock rates"),
    272 		       sysctl_kern_clockrate, 0, NULL,
    273 		       sizeof(struct clockinfo),
    274 		       CTL_KERN, KERN_CLOCKRATE, CTL_EOL);
    275 	sysctl_createv(clog, 0, NULL, NULL,
    276 		       CTLFLAG_PERMANENT,
    277 		       CTLTYPE_INT, "hardclock_ticks",
    278 		       SYSCTL_DESCR("Number of hardclock ticks"),
    279 		       NULL, 0, &hardclock_ticks, sizeof(hardclock_ticks),
    280 		       CTL_KERN, KERN_HARDCLOCK_TICKS, CTL_EOL);
    281 	sysctl_createv(clog, 0, NULL, NULL,
    282 		       CTLFLAG_PERMANENT,
    283 		       CTLTYPE_STRUCT, "vnode",
    284 		       SYSCTL_DESCR("System vnode table"),
    285 		       sysctl_kern_vnode, 0, NULL, 0,
    286 		       CTL_KERN, KERN_VNODE, CTL_EOL);
    287 #ifndef GPROF
    288 	sysctl_createv(clog, 0, NULL, NULL,
    289 		       CTLFLAG_PERMANENT,
    290 		       CTLTYPE_NODE, "profiling",
    291 		       SYSCTL_DESCR("Profiling information (not available)"),
    292 		       sysctl_notavail, 0, NULL, 0,
    293 		       CTL_KERN, KERN_PROF, CTL_EOL);
    294 #endif
    295 	sysctl_createv(clog, 0, NULL, NULL,
    296 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    297 		       CTLTYPE_INT, "posix1version",
    298 		       SYSCTL_DESCR("Version of ISO/IEC 9945 (POSIX 1003.1) "
    299 				    "with which the operating system attempts "
    300 				    "to comply"),
    301 		       NULL, _POSIX_VERSION, NULL, 0,
    302 		       CTL_KERN, KERN_POSIX1, CTL_EOL);
    303 	sysctl_createv(clog, 0, NULL, NULL,
    304 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    305 		       CTLTYPE_INT, "ngroups",
    306 		       SYSCTL_DESCR("Maximum number of supplemental groups"),
    307 		       NULL, NGROUPS_MAX, NULL, 0,
    308 		       CTL_KERN, KERN_NGROUPS, CTL_EOL);
    309 	sysctl_createv(clog, 0, NULL, NULL,
    310 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    311 		       CTLTYPE_INT, "job_control",
    312 		       SYSCTL_DESCR("Whether job control is available"),
    313 		       NULL, 1, NULL, 0,
    314 		       CTL_KERN, KERN_JOB_CONTROL, CTL_EOL);
    315 	sysctl_createv(clog, 0, NULL, NULL,
    316 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    317 		       CTLTYPE_INT, "saved_ids",
    318 		       SYSCTL_DESCR("Whether POSIX saved set-group/user ID is "
    319 				    "available"), NULL,
    320 #ifdef _POSIX_SAVED_IDS
    321 		       1,
    322 #else /* _POSIX_SAVED_IDS */
    323 		       0,
    324 #endif /* _POSIX_SAVED_IDS */
    325 		       NULL, 0, CTL_KERN, KERN_SAVED_IDS, CTL_EOL);
    326 	sysctl_createv(clog, 0, NULL, NULL,
    327 		       CTLFLAG_PERMANENT|CTLFLAG_HEX,
    328 		       CTLTYPE_INT, "boothowto",
    329 		       SYSCTL_DESCR("Flags from boot loader"),
    330 		       NULL, 0, &boothowto, sizeof(boothowto),
    331 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    332 	sysctl_createv(clog, 0, NULL, NULL,
    333 		       CTLFLAG_PERMANENT,
    334 		       CTLTYPE_STRUCT, "boottime",
    335 		       SYSCTL_DESCR("System boot time"),
    336 		       NULL, 0, &boottime, sizeof(boottime),
    337 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
    338 	sysctl_createv(clog, 0, NULL, NULL,
    339 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    340 		       CTLTYPE_STRING, "domainname",
    341 		       SYSCTL_DESCR("YP domain name"),
    342 		       sysctl_setlen, 0, &domainname, MAXHOSTNAMELEN,
    343 		       CTL_KERN, KERN_DOMAINNAME, CTL_EOL);
    344 	sysctl_createv(clog, 0, NULL, NULL,
    345 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    346 		       CTLTYPE_INT, "maxpartitions",
    347 		       SYSCTL_DESCR("Maximum number of partitions allowed per "
    348 				    "disk"),
    349 		       NULL, MAXPARTITIONS, NULL, 0,
    350 		       CTL_KERN, KERN_MAXPARTITIONS, CTL_EOL);
    351 	sysctl_createv(clog, 0, NULL, NULL,
    352 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    353 		       CTLTYPE_INT, "rawpartition",
    354 		       SYSCTL_DESCR("Raw partition of a disk"),
    355 		       NULL, RAW_PART, NULL, 0,
    356 		       CTL_KERN, KERN_RAWPARTITION, CTL_EOL);
    357 	sysctl_createv(clog, 0, NULL, NULL,
    358 		       CTLFLAG_PERMANENT,
    359 		       CTLTYPE_STRUCT, "timex", NULL,
    360 		       sysctl_notavail, 0, NULL, 0,
    361 		       CTL_KERN, KERN_TIMEX, CTL_EOL);
    362 	sysctl_createv(clog, 0, NULL, NULL,
    363 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    364 		       CTLTYPE_INT, "rtc_offset",
    365 		       SYSCTL_DESCR("Offset of real time clock from UTC in "
    366 				    "minutes"),
    367 		       sysctl_kern_rtc_offset, 0, &rtc_offset, 0,
    368 		       CTL_KERN, KERN_RTC_OFFSET, CTL_EOL);
    369 	sysctl_createv(clog, 0, NULL, NULL,
    370 		       CTLFLAG_PERMANENT,
    371 		       CTLTYPE_STRING, "root_device",
    372 		       SYSCTL_DESCR("Name of the root device"),
    373 		       sysctl_root_device, 0, NULL, 0,
    374 		       CTL_KERN, KERN_ROOT_DEVICE, CTL_EOL);
    375 	sysctl_createv(clog, 0, NULL, NULL,
    376 		       CTLFLAG_PERMANENT,
    377 		       CTLTYPE_INT, "msgbufsize",
    378 		       SYSCTL_DESCR("Size of the kernel message buffer"),
    379 		       sysctl_msgbuf, 0, NULL, 0,
    380 		       CTL_KERN, KERN_MSGBUFSIZE, CTL_EOL);
    381 	sysctl_createv(clog, 0, NULL, NULL,
    382 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    383 		       CTLTYPE_INT, "fsync",
    384 		       SYSCTL_DESCR("Whether the POSIX 1003.1b File "
    385 				    "Synchronization Option is available on "
    386 				    "this system"),
    387 		       NULL, 1, NULL, 0,
    388 		       CTL_KERN, KERN_FSYNC, CTL_EOL);
    389 	sysctl_createv(clog, 0, NULL, NULL,
    390 		       CTLFLAG_PERMANENT,
    391 		       CTLTYPE_NODE, "ipc",
    392 		       SYSCTL_DESCR("SysV IPC options"),
    393 		       NULL, 0, NULL, 0,
    394 		       CTL_KERN, KERN_SYSVIPC, CTL_EOL);
    395 	sysctl_createv(clog, 0, NULL, NULL,
    396 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    397 		       CTLTYPE_INT, "sysvmsg",
    398 		       SYSCTL_DESCR("System V style message support available"),
    399 		       NULL,
    400 #ifdef SYSVMSG
    401 		       1,
    402 #else /* SYSVMSG */
    403 		       0,
    404 #endif /* SYSVMSG */
    405 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_MSG, CTL_EOL);
    406 	sysctl_createv(clog, 0, NULL, NULL,
    407 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    408 		       CTLTYPE_INT, "sysvsem",
    409 		       SYSCTL_DESCR("System V style semaphore support "
    410 				    "available"), NULL,
    411 #ifdef SYSVSEM
    412 		       1,
    413 #else /* SYSVSEM */
    414 		       0,
    415 #endif /* SYSVSEM */
    416 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SEM, CTL_EOL);
    417 	sysctl_createv(clog, 0, NULL, NULL,
    418 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    419 		       CTLTYPE_INT, "sysvshm",
    420 		       SYSCTL_DESCR("System V style shared memory support "
    421 				    "available"), NULL,
    422 #ifdef SYSVSHM
    423 		       1,
    424 #else /* SYSVSHM */
    425 		       0,
    426 #endif /* SYSVSHM */
    427 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHM, CTL_EOL);
    428 	sysctl_createv(clog, 0, NULL, NULL,
    429 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    430 		       CTLTYPE_INT, "synchronized_io",
    431 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Synchronized "
    432 				    "I/O Option is available on this system"),
    433 		       NULL, 1, NULL, 0,
    434 		       CTL_KERN, KERN_SYNCHRONIZED_IO, CTL_EOL);
    435 	sysctl_createv(clog, 0, NULL, NULL,
    436 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    437 		       CTLTYPE_INT, "iov_max",
    438 		       SYSCTL_DESCR("Maximum number of iovec structures per "
    439 				    "process"),
    440 		       NULL, IOV_MAX, NULL, 0,
    441 		       CTL_KERN, KERN_IOV_MAX, CTL_EOL);
    442 	sysctl_createv(clog, 0, NULL, NULL,
    443 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    444 		       CTLTYPE_INT, "mapped_files",
    445 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory Mapped "
    446 				    "Files Option is available on this system"),
    447 		       NULL, 1, NULL, 0,
    448 		       CTL_KERN, KERN_MAPPED_FILES, CTL_EOL);
    449 	sysctl_createv(clog, 0, NULL, NULL,
    450 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    451 		       CTLTYPE_INT, "memlock",
    452 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Process Memory "
    453 				    "Locking Option is available on this "
    454 				    "system"),
    455 		       NULL, 1, NULL, 0,
    456 		       CTL_KERN, KERN_MEMLOCK, CTL_EOL);
    457 	sysctl_createv(clog, 0, NULL, NULL,
    458 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    459 		       CTLTYPE_INT, "memlock_range",
    460 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Range Memory "
    461 				    "Locking Option is available on this "
    462 				    "system"),
    463 		       NULL, 1, NULL, 0,
    464 		       CTL_KERN, KERN_MEMLOCK_RANGE, CTL_EOL);
    465 	sysctl_createv(clog, 0, NULL, NULL,
    466 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    467 		       CTLTYPE_INT, "memory_protection",
    468 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory "
    469 				    "Protection Option is available on this "
    470 				    "system"),
    471 		       NULL, 1, NULL, 0,
    472 		       CTL_KERN, KERN_MEMORY_PROTECTION, CTL_EOL);
    473 	sysctl_createv(clog, 0, NULL, NULL,
    474 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    475 		       CTLTYPE_INT, "login_name_max",
    476 		       SYSCTL_DESCR("Maximum login name length"),
    477 		       NULL, LOGIN_NAME_MAX, NULL, 0,
    478 		       CTL_KERN, KERN_LOGIN_NAME_MAX, CTL_EOL);
    479 	sysctl_createv(clog, 0, NULL, NULL,
    480 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    481 		       CTLTYPE_STRING, "defcorename",
    482 		       SYSCTL_DESCR("Default core file name"),
    483 		       sysctl_kern_defcorename, 0, defcorename, MAXPATHLEN,
    484 		       CTL_KERN, KERN_DEFCORENAME, CTL_EOL);
    485 	sysctl_createv(clog, 0, NULL, NULL,
    486 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    487 		       CTLTYPE_INT, "logsigexit",
    488 		       SYSCTL_DESCR("Log process exit when caused by signals"),
    489 		       NULL, 0, &kern_logsigexit, 0,
    490 		       CTL_KERN, KERN_LOGSIGEXIT, CTL_EOL);
    491 	sysctl_createv(clog, 0, NULL, NULL,
    492 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    493 		       CTLTYPE_INT, "fscale",
    494 		       SYSCTL_DESCR("Kernel fixed-point scale factor"),
    495 		       NULL, FSCALE, NULL, 0,
    496 		       CTL_KERN, KERN_FSCALE, CTL_EOL);
    497 	sysctl_createv(clog, 0, NULL, NULL,
    498 		       CTLFLAG_PERMANENT,
    499 		       CTLTYPE_INT, "ccpu",
    500 		       SYSCTL_DESCR("Scheduler exponential decay value"),
    501 		       NULL, 0, &ccpu, 0,
    502 		       CTL_KERN, KERN_CCPU, CTL_EOL);
    503 	sysctl_createv(clog, 0, NULL, NULL,
    504 		       CTLFLAG_PERMANENT,
    505 		       CTLTYPE_STRUCT, "cp_time",
    506 		       SYSCTL_DESCR("Clock ticks spent in different CPU states"),
    507 		       sysctl_kern_cptime, 0, NULL, 0,
    508 		       CTL_KERN, KERN_CP_TIME, CTL_EOL);
    509 	sysctl_createv(clog, 0, NULL, NULL,
    510 		       CTLFLAG_PERMANENT,
    511 		       CTLTYPE_INT, "msgbuf",
    512 		       SYSCTL_DESCR("Kernel message buffer"),
    513 		       sysctl_msgbuf, 0, NULL, 0,
    514 		       CTL_KERN, KERN_MSGBUF, CTL_EOL);
    515 	sysctl_createv(clog, 0, NULL, NULL,
    516 		       CTLFLAG_PERMANENT,
    517 		       CTLTYPE_STRUCT, "consdev",
    518 		       SYSCTL_DESCR("Console device"),
    519 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
    520 		       CTL_KERN, KERN_CONSDEV, CTL_EOL);
    521 #if NPTY > 0
    522 	sysctl_createv(clog, 0, NULL, NULL,
    523 		       CTLFLAG_PERMANENT,
    524 		       CTLTYPE_INT, "maxptys",
    525 		       SYSCTL_DESCR("Maximum number of pseudo-ttys"),
    526 		       sysctl_kern_maxptys, 0, NULL, 0,
    527 		       CTL_KERN, KERN_MAXPTYS, CTL_EOL);
    528 #endif /* NPTY > 0 */
    529 	sysctl_createv(clog, 0, NULL, NULL,
    530 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    531 		       CTLTYPE_INT, "maxphys",
    532 		       SYSCTL_DESCR("Maximum raw I/O transfer size"),
    533 		       NULL, MAXPHYS, NULL, 0,
    534 		       CTL_KERN, KERN_MAXPHYS, CTL_EOL);
    535 	sysctl_createv(clog, 0, NULL, NULL,
    536 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    537 		       CTLTYPE_INT, "sbmax",
    538 		       SYSCTL_DESCR("Maximum socket buffer size"),
    539 		       sysctl_kern_sbmax, 0, NULL, 0,
    540 		       CTL_KERN, KERN_SBMAX, CTL_EOL);
    541 	sysctl_createv(clog, 0, NULL, NULL,
    542 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    543 		       CTLTYPE_INT, "monotonic_clock",
    544 		       SYSCTL_DESCR("Implementation version of the POSIX "
    545 				    "1003.1b Monotonic Clock Option"),
    546 		       /* XXX _POSIX_VERSION */
    547 		       NULL, _POSIX_MONOTONIC_CLOCK, NULL, 0,
    548 		       CTL_KERN, KERN_MONOTONIC_CLOCK, CTL_EOL);
    549 	sysctl_createv(clog, 0, NULL, NULL,
    550 		       CTLFLAG_PERMANENT,
    551 		       CTLTYPE_INT, "urandom",
    552 		       SYSCTL_DESCR("Random integer value"),
    553 		       sysctl_kern_urnd, 0, NULL, 0,
    554 		       CTL_KERN, KERN_URND, CTL_EOL);
    555 	sysctl_createv(clog, 0, NULL, NULL,
    556 		       CTLFLAG_PERMANENT,
    557 		       CTLTYPE_INT, "arandom",
    558 		       SYSCTL_DESCR("n bytes of random data"),
    559 		       sysctl_kern_arnd, 0, NULL, 0,
    560 		       CTL_KERN, KERN_ARND, CTL_EOL);
    561 	sysctl_createv(clog, 0, NULL, NULL,
    562 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    563 		       CTLTYPE_INT, "labelsector",
    564 		       SYSCTL_DESCR("Sector number containing the disklabel"),
    565 		       NULL, LABELSECTOR, NULL, 0,
    566 		       CTL_KERN, KERN_LABELSECTOR, CTL_EOL);
    567 	sysctl_createv(clog, 0, NULL, NULL,
    568 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    569 		       CTLTYPE_INT, "labeloffset",
    570 		       SYSCTL_DESCR("Offset of the disklabel within the "
    571 				    "sector"),
    572 		       NULL, LABELOFFSET, NULL, 0,
    573 		       CTL_KERN, KERN_LABELOFFSET, CTL_EOL);
    574 	sysctl_createv(clog, 0, NULL, NULL,
    575 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    576 		       CTLTYPE_INT, "labelusesmbr",
    577 		       SYSCTL_DESCR("disklabel is inside MBR partition"),
    578 		       NULL, LABELUSESMBR, NULL, 0,
    579 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    580 	sysctl_createv(clog, 0, NULL, NULL,
    581 		       CTLFLAG_PERMANENT,
    582 		       CTLTYPE_NODE, "lwp",
    583 		       SYSCTL_DESCR("System-wide LWP information"),
    584 		       sysctl_kern_lwp, 0, NULL, 0,
    585 		       CTL_KERN, KERN_LWP, CTL_EOL);
    586 	sysctl_createv(clog, 0, NULL, NULL,
    587 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    588 		       CTLTYPE_INT, "forkfsleep",
    589 		       SYSCTL_DESCR("Milliseconds to sleep on fork failure due "
    590 				    "to process limits"),
    591 		       sysctl_kern_forkfsleep, 0, NULL, 0,
    592 		       CTL_KERN, KERN_FORKFSLEEP, CTL_EOL);
    593 	sysctl_createv(clog, 0, NULL, NULL,
    594 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    595 		       CTLTYPE_INT, "posix_threads",
    596 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    597 				    "Threads option to which the system "
    598 				    "attempts to conform"),
    599 		       /* XXX _POSIX_VERSION */
    600 		       NULL, _POSIX_THREADS, NULL, 0,
    601 		       CTL_KERN, KERN_POSIX_THREADS, CTL_EOL);
    602 	sysctl_createv(clog, 0, NULL, NULL,
    603 		       CTLFLAG_PERMANENT,
    604 		       CTLTYPE_INT, "posix_semaphores",
    605 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    606 				    "Semaphores option to which the system "
    607 				    "attempts to conform"), NULL,
    608 		       0, &posix_semaphores,
    609 		       0, CTL_KERN, KERN_POSIX_SEMAPHORES, CTL_EOL);
    610 	sysctl_createv(clog, 0, NULL, NULL,
    611 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    612 		       CTLTYPE_INT, "posix_barriers",
    613 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    614 				    "Barriers option to which the system "
    615 				    "attempts to conform"),
    616 		       /* XXX _POSIX_VERSION */
    617 		       NULL, _POSIX_BARRIERS, NULL, 0,
    618 		       CTL_KERN, KERN_POSIX_BARRIERS, CTL_EOL);
    619 	sysctl_createv(clog, 0, NULL, NULL,
    620 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    621 		       CTLTYPE_INT, "posix_timers",
    622 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    623 				    "Timers option to which the system "
    624 				    "attempts to conform"),
    625 		       /* XXX _POSIX_VERSION */
    626 		       NULL, _POSIX_TIMERS, NULL, 0,
    627 		       CTL_KERN, KERN_POSIX_TIMERS, CTL_EOL);
    628 	sysctl_createv(clog, 0, NULL, NULL,
    629 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    630 		       CTLTYPE_INT, "posix_spin_locks",
    631 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its Spin "
    632 				    "Locks option to which the system attempts "
    633 				    "to conform"),
    634 		       /* XXX _POSIX_VERSION */
    635 		       NULL, _POSIX_SPIN_LOCKS, NULL, 0,
    636 		       CTL_KERN, KERN_POSIX_SPIN_LOCKS, CTL_EOL);
    637 	sysctl_createv(clog, 0, NULL, NULL,
    638 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    639 		       CTLTYPE_INT, "posix_reader_writer_locks",
    640 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    641 				    "Read-Write Locks option to which the "
    642 				    "system attempts to conform"),
    643 		       /* XXX _POSIX_VERSION */
    644 		       NULL, _POSIX_READER_WRITER_LOCKS, NULL, 0,
    645 		       CTL_KERN, KERN_POSIX_READER_WRITER_LOCKS, CTL_EOL);
    646 	sysctl_createv(clog, 0, NULL, NULL,
    647 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    648 		       CTLTYPE_INT, "dump_on_panic",
    649 		       SYSCTL_DESCR("Perform a crash dump on system panic"),
    650 		       NULL, 0, &dumponpanic, 0,
    651 		       CTL_KERN, KERN_DUMP_ON_PANIC, CTL_EOL);
    652 #ifdef DIAGNOSTIC
    653 	sysctl_createv(clog, 0, NULL, NULL,
    654 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    655 		       CTLTYPE_INT, "panic_now",
    656 		       SYSCTL_DESCR("Trigger a panic"),
    657 		       sysctl_kern_trigger_panic, 0, NULL, 0,
    658 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    659 #endif
    660 	sysctl_createv(clog, 0, NULL, NULL,
    661 		       CTLFLAG_PERMANENT,
    662 		       CTLTYPE_INT, "root_partition",
    663 		       SYSCTL_DESCR("Root partition on the root device"),
    664 		       sysctl_kern_root_partition, 0, NULL, 0,
    665 		       CTL_KERN, KERN_ROOT_PARTITION, CTL_EOL);
    666 	sysctl_createv(clog, 0, NULL, NULL,
    667 		       CTLFLAG_PERMANENT,
    668 		       CTLTYPE_STRUCT, "drivers",
    669 		       SYSCTL_DESCR("List of all drivers with block and "
    670 				    "character device numbers"),
    671 		       sysctl_kern_drivers, 0, NULL, 0,
    672 		       CTL_KERN, KERN_DRIVERS, CTL_EOL);
    673 	sysctl_createv(clog, 0, NULL, NULL,
    674 		       CTLFLAG_PERMANENT,
    675 		       CTLTYPE_STRUCT, "cp_id",
    676 		       SYSCTL_DESCR("Mapping of CPU number to CPU id"),
    677 		       sysctl_kern_cpid, 0, NULL, 0,
    678 		       CTL_KERN, KERN_CP_ID, CTL_EOL);
    679 	sysctl_createv(clog, 0, NULL, &rnode,
    680 		       CTLFLAG_PERMANENT,
    681 		       CTLTYPE_NODE, "coredump",
    682 		       SYSCTL_DESCR("Coredump settings."),
    683 		       NULL, 0, NULL, 0,
    684 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    685 	sysctl_createv(clog, 0, &rnode, &rnode,
    686 		       CTLFLAG_PERMANENT,
    687 		       CTLTYPE_NODE, "setid",
    688 		       SYSCTL_DESCR("Set-id processes' coredump settings."),
    689 		       NULL, 0, NULL, 0,
    690 		       CTL_CREATE, CTL_EOL);
    691 	sysctl_createv(clog, 0, &rnode, NULL,
    692 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    693 		       CTLTYPE_INT, "dump",
    694 		       SYSCTL_DESCR("Allow set-id processes to dump core."),
    695 		       sysctl_security_setidcore, 0, &security_setidcore_dump,
    696 		       sizeof(security_setidcore_dump),
    697 		       CTL_CREATE, CTL_EOL);
    698 	sysctl_createv(clog, 0, &rnode, NULL,
    699 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    700 		       CTLTYPE_STRING, "path",
    701 		       SYSCTL_DESCR("Path pattern for set-id coredumps."),
    702 		       sysctl_security_setidcorename, 0,
    703 		       &security_setidcore_path,
    704 		       sizeof(security_setidcore_path),
    705 		       CTL_CREATE, CTL_EOL);
    706 	sysctl_createv(clog, 0, &rnode, NULL,
    707 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    708 		       CTLTYPE_INT, "owner",
    709 		       SYSCTL_DESCR("Owner id for set-id processes' cores."),
    710 		       sysctl_security_setidcore, 0, &security_setidcore_owner,
    711 		       0,
    712 		       CTL_CREATE, CTL_EOL);
    713 	sysctl_createv(clog, 0, &rnode, NULL,
    714 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    715 		       CTLTYPE_INT, "group",
    716 		       SYSCTL_DESCR("Group id for set-id processes' cores."),
    717 		       sysctl_security_setidcore, 0, &security_setidcore_group,
    718 		       0,
    719 		       CTL_CREATE, CTL_EOL);
    720 	sysctl_createv(clog, 0, &rnode, NULL,
    721 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    722 		       CTLTYPE_INT, "mode",
    723 		       SYSCTL_DESCR("Mode for set-id processes' cores."),
    724 		       sysctl_security_setidcore, 0, &security_setidcore_mode,
    725 		       0,
    726 		       CTL_CREATE, CTL_EOL);
    727 	sysctl_createv(clog, 0, NULL, NULL,
    728 #ifndef KERN_SA
    729 		       CTLFLAG_IMMEDIATE|
    730 #endif
    731 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    732 		       CTLTYPE_INT, "no_sa_support",
    733 		       SYSCTL_DESCR("0 if the kernel supports SA, otherwise "
    734 		       "it doesn't"),
    735 		       NULL,
    736 #ifndef KERN_SA
    737 		       1, NULL,
    738 #else
    739 		       0, &sa_system_disabled,
    740 #endif
    741 		       0,
    742 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    743 	/* kern.posix. */
    744 	sysctl_createv(clog, 0, NULL, &rnode,
    745 			CTLFLAG_PERMANENT,
    746 			CTLTYPE_NODE, "posix",
    747 			SYSCTL_DESCR("POSIX options"),
    748 			NULL, 0, NULL, 0,
    749 			CTL_KERN, CTL_CREATE, CTL_EOL);
    750 	sysctl_createv(clog, 0, &rnode, NULL,
    751 			CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    752 			CTLTYPE_INT, "semmax",
    753 			SYSCTL_DESCR("Maximal number of semaphores"),
    754 			NULL, 0, &ksem_max, 0,
    755 			CTL_CREATE, CTL_EOL);
    756 }
    757 
    758 SYSCTL_SETUP(sysctl_hw_setup, "sysctl hw subtree setup")
    759 {
    760 	u_int u;
    761 	u_quad_t q;
    762 
    763 	sysctl_createv(clog, 0, NULL, NULL,
    764 		       CTLFLAG_PERMANENT,
    765 		       CTLTYPE_NODE, "hw", NULL,
    766 		       NULL, 0, NULL, 0,
    767 		       CTL_HW, CTL_EOL);
    768 
    769 	sysctl_createv(clog, 0, NULL, NULL,
    770 		       CTLFLAG_PERMANENT,
    771 		       CTLTYPE_STRING, "machine",
    772 		       SYSCTL_DESCR("Machine class"),
    773 		       NULL, 0, machine, 0,
    774 		       CTL_HW, HW_MACHINE, CTL_EOL);
    775 	sysctl_createv(clog, 0, NULL, NULL,
    776 		       CTLFLAG_PERMANENT,
    777 		       CTLTYPE_STRING, "model",
    778 		       SYSCTL_DESCR("Machine model"),
    779 		       NULL, 0, cpu_model, 0,
    780 		       CTL_HW, HW_MODEL, CTL_EOL);
    781 	sysctl_createv(clog, 0, NULL, NULL,
    782 		       CTLFLAG_PERMANENT,
    783 		       CTLTYPE_INT, "ncpu",
    784 		       SYSCTL_DESCR("Number of CPUs configured"),
    785 		       NULL, 0, &ncpu, 0,
    786 		       CTL_HW, HW_NCPU, CTL_EOL);
    787 	sysctl_createv(clog, 0, NULL, NULL,
    788 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    789 		       CTLTYPE_INT, "byteorder",
    790 		       SYSCTL_DESCR("System byte order"),
    791 		       NULL, BYTE_ORDER, NULL, 0,
    792 		       CTL_HW, HW_BYTEORDER, CTL_EOL);
    793 	u = ((u_int)physmem > (UINT_MAX / PAGE_SIZE)) ?
    794 		UINT_MAX : physmem * PAGE_SIZE;
    795 	sysctl_createv(clog, 0, NULL, NULL,
    796 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    797 		       CTLTYPE_INT, "physmem",
    798 		       SYSCTL_DESCR("Bytes of physical memory"),
    799 		       NULL, u, NULL, 0,
    800 		       CTL_HW, HW_PHYSMEM, CTL_EOL);
    801 	sysctl_createv(clog, 0, NULL, NULL,
    802 		       CTLFLAG_PERMANENT,
    803 		       CTLTYPE_INT, "usermem",
    804 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
    805 		       sysctl_hw_usermem, 0, NULL, 0,
    806 		       CTL_HW, HW_USERMEM, CTL_EOL);
    807 	sysctl_createv(clog, 0, NULL, NULL,
    808 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    809 		       CTLTYPE_INT, "pagesize",
    810 		       SYSCTL_DESCR("Software page size"),
    811 		       NULL, PAGE_SIZE, NULL, 0,
    812 		       CTL_HW, HW_PAGESIZE, CTL_EOL);
    813 	sysctl_createv(clog, 0, NULL, NULL,
    814 		       CTLFLAG_PERMANENT,
    815 		       CTLTYPE_STRING, "machine_arch",
    816 		       SYSCTL_DESCR("Machine CPU class"),
    817 		       NULL, 0, machine_arch, 0,
    818 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
    819 	sysctl_createv(clog, 0, NULL, NULL,
    820 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    821 		       CTLTYPE_INT, "alignbytes",
    822 		       SYSCTL_DESCR("Alignment constraint for all possible "
    823 				    "data types"),
    824 		       NULL, ALIGNBYTES, NULL, 0,
    825 		       CTL_HW, HW_ALIGNBYTES, CTL_EOL);
    826 	sysctl_createv(clog, 0, NULL, NULL,
    827 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
    828 		       CTLTYPE_STRING, "cnmagic",
    829 		       SYSCTL_DESCR("Console magic key sequence"),
    830 		       sysctl_hw_cnmagic, 0, NULL, CNS_LEN,
    831 		       CTL_HW, HW_CNMAGIC, CTL_EOL);
    832 	q = (u_quad_t)physmem * PAGE_SIZE;
    833 	sysctl_createv(clog, 0, NULL, NULL,
    834 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    835 		       CTLTYPE_QUAD, "physmem64",
    836 		       SYSCTL_DESCR("Bytes of physical memory"),
    837 		       NULL, q, NULL, 0,
    838 		       CTL_HW, HW_PHYSMEM64, CTL_EOL);
    839 	sysctl_createv(clog, 0, NULL, NULL,
    840 		       CTLFLAG_PERMANENT,
    841 		       CTLTYPE_QUAD, "usermem64",
    842 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
    843 		       sysctl_hw_usermem, 0, NULL, 0,
    844 		       CTL_HW, HW_USERMEM64, CTL_EOL);
    845 	sysctl_createv(clog, 0, NULL, NULL,
    846 		       CTLFLAG_PERMANENT,
    847 		       CTLTYPE_INT, "ncpuonline",
    848 		       SYSCTL_DESCR("Number of CPUs online"),
    849 		       NULL, 0, &ncpuonline, 0,
    850 		       CTL_HW, HW_NCPUONLINE, CTL_EOL);
    851 }
    852 
    853 #ifdef DEBUG
    854 /*
    855  * Debugging related system variables.
    856  */
    857 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
    858 struct ctldebug debug5, debug6, debug7, debug8, debug9;
    859 struct ctldebug debug10, debug11, debug12, debug13, debug14;
    860 struct ctldebug debug15, debug16, debug17, debug18, debug19;
    861 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
    862 	&debug0, &debug1, &debug2, &debug3, &debug4,
    863 	&debug5, &debug6, &debug7, &debug8, &debug9,
    864 	&debug10, &debug11, &debug12, &debug13, &debug14,
    865 	&debug15, &debug16, &debug17, &debug18, &debug19,
    866 };
    867 
    868 /*
    869  * this setup routine is a replacement for debug_sysctl()
    870  *
    871  * note that it creates several nodes per defined debug variable
    872  */
    873 SYSCTL_SETUP(sysctl_debug_setup, "sysctl debug subtree setup")
    874 {
    875 	struct ctldebug *cdp;
    876 	char nodename[20];
    877 	int i;
    878 
    879 	/*
    880 	 * two ways here:
    881 	 *
    882 	 * the "old" way (debug.name -> value) which was emulated by
    883 	 * the sysctl(8) binary
    884 	 *
    885 	 * the new way, which the sysctl(8) binary was actually using
    886 
    887 	 node	debug
    888 	 node	debug.0
    889 	 string debug.0.name
    890 	 int	debug.0.value
    891 	 int	debug.name
    892 
    893 	 */
    894 
    895 	sysctl_createv(clog, 0, NULL, NULL,
    896 		       CTLFLAG_PERMANENT,
    897 		       CTLTYPE_NODE, "debug", NULL,
    898 		       NULL, 0, NULL, 0,
    899 		       CTL_DEBUG, CTL_EOL);
    900 
    901 	for (i = 0; i < CTL_DEBUG_MAXID; i++) {
    902 		cdp = debugvars[i];
    903 		if (cdp->debugname == NULL || cdp->debugvar == NULL)
    904 			continue;
    905 
    906 		snprintf(nodename, sizeof(nodename), "debug%d", i);
    907 		sysctl_createv(clog, 0, NULL, NULL,
    908 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
    909 			       CTLTYPE_NODE, nodename, NULL,
    910 			       NULL, 0, NULL, 0,
    911 			       CTL_DEBUG, i, CTL_EOL);
    912 		sysctl_createv(clog, 0, NULL, NULL,
    913 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
    914 			       CTLTYPE_STRING, "name", NULL,
    915 			       /*XXXUNCONST*/
    916 			       NULL, 0, __UNCONST(cdp->debugname), 0,
    917 			       CTL_DEBUG, i, CTL_DEBUG_NAME, CTL_EOL);
    918 		sysctl_createv(clog, 0, NULL, NULL,
    919 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
    920 			       CTLTYPE_INT, "value", NULL,
    921 			       NULL, 0, cdp->debugvar, 0,
    922 			       CTL_DEBUG, i, CTL_DEBUG_VALUE, CTL_EOL);
    923 		sysctl_createv(clog, 0, NULL, NULL,
    924 			       CTLFLAG_PERMANENT,
    925 			       CTLTYPE_INT, cdp->debugname, NULL,
    926 			       NULL, 0, cdp->debugvar, 0,
    927 			       CTL_DEBUG, CTL_CREATE, CTL_EOL);
    928 	}
    929 }
    930 #endif /* DEBUG */
    931 
    932 /*
    933  * ********************************************************************
    934  * section 2: private node-specific helper routines.
    935  * ********************************************************************
    936  */
    937 
    938 #ifdef DIAGNOSTIC
    939 static int
    940 sysctl_kern_trigger_panic(SYSCTLFN_ARGS)
    941 {
    942 	int newtrig, error;
    943 	struct sysctlnode node;
    944 
    945 	newtrig = 0;
    946 	node = *rnode;
    947 	node.sysctl_data = &newtrig;
    948 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    949 	if (error || newp == NULL)
    950 		return (error);
    951 
    952 	if (newtrig != 0)
    953 		panic("Panic triggered");
    954 
    955 	return (error);
    956 }
    957 #endif
    958 
    959 /*
    960  * sysctl helper routine for kern.maxvnodes.  Drain vnodes if
    961  * new value is lower than desiredvnodes and then calls reinit
    962  * routines that needs to adjust to the new value.
    963  */
    964 static int
    965 sysctl_kern_maxvnodes(SYSCTLFN_ARGS)
    966 {
    967 	int error, new_vnodes, old_vnodes, new_max;
    968 	struct sysctlnode node;
    969 
    970 	new_vnodes = desiredvnodes;
    971 	node = *rnode;
    972 	node.sysctl_data = &new_vnodes;
    973 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    974 	if (error || newp == NULL)
    975 		return (error);
    976 
    977 	/* Limits: 75% of KVA and physical memory. */
    978 	new_max = calc_cache_size(kernel_map, 75, 75) / VNODE_COST;
    979 	if (new_vnodes > new_max)
    980 		new_vnodes = new_max;
    981 
    982 	old_vnodes = desiredvnodes;
    983 	desiredvnodes = new_vnodes;
    984 	if (new_vnodes < old_vnodes) {
    985 		error = vfs_drainvnodes(new_vnodes);
    986 		if (error) {
    987 			desiredvnodes = old_vnodes;
    988 			return (error);
    989 		}
    990 	}
    991 	vfs_reinit();
    992 	nchreinit();
    993 
    994 	return (0);
    995 }
    996 
    997 /*
    998  * sysctl helper routine for rtc_offset - set time after changes
    999  */
   1000 static int
   1001 sysctl_kern_rtc_offset(SYSCTLFN_ARGS)
   1002 {
   1003 	struct timespec ts, delta;
   1004 	int error, new_rtc_offset;
   1005 	struct sysctlnode node;
   1006 
   1007 	new_rtc_offset = rtc_offset;
   1008 	node = *rnode;
   1009 	node.sysctl_data = &new_rtc_offset;
   1010 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1011 	if (error || newp == NULL)
   1012 		return (error);
   1013 
   1014 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
   1015 	    KAUTH_REQ_SYSTEM_TIME_RTCOFFSET,
   1016 	    KAUTH_ARG(new_rtc_offset), NULL, NULL))
   1017 		return (EPERM);
   1018 	if (rtc_offset == new_rtc_offset)
   1019 		return (0);
   1020 
   1021 	/* if we change the offset, adjust the time */
   1022 	nanotime(&ts);
   1023 	delta.tv_sec = 60 * (new_rtc_offset - rtc_offset);
   1024 	delta.tv_nsec = 0;
   1025 	timespecadd(&ts, &delta, &ts);
   1026 	rtc_offset = new_rtc_offset;
   1027 	return (settime(l->l_proc, &ts));
   1028 }
   1029 
   1030 /*
   1031  * sysctl helper routine for kern.maxproc. Ensures that the new
   1032  * values are not too low or too high.
   1033  */
   1034 static int
   1035 sysctl_kern_maxproc(SYSCTLFN_ARGS)
   1036 {
   1037 	int error, nmaxproc;
   1038 	struct sysctlnode node;
   1039 
   1040 	nmaxproc = maxproc;
   1041 	node = *rnode;
   1042 	node.sysctl_data = &nmaxproc;
   1043 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1044 	if (error || newp == NULL)
   1045 		return (error);
   1046 
   1047 	if (nmaxproc < 0 || nmaxproc >= PID_MAX)
   1048 		return (EINVAL);
   1049 #ifdef __HAVE_CPU_MAXPROC
   1050 	if (nmaxproc > cpu_maxproc())
   1051 		return (EINVAL);
   1052 #endif
   1053 	maxproc = nmaxproc;
   1054 
   1055 	return (0);
   1056 }
   1057 
   1058 /*
   1059  * sysctl helper function for kern.hostid. The hostid is a long, but
   1060  * we export it as an int, so we need to give it a little help.
   1061  */
   1062 static int
   1063 sysctl_kern_hostid(SYSCTLFN_ARGS)
   1064 {
   1065 	int error, inthostid;
   1066 	struct sysctlnode node;
   1067 
   1068 	inthostid = hostid;  /* XXX assumes sizeof int <= sizeof long */
   1069 	node = *rnode;
   1070 	node.sysctl_data = &inthostid;
   1071 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1072 	if (error || newp == NULL)
   1073 		return (error);
   1074 
   1075 	hostid = (unsigned)inthostid;
   1076 
   1077 	return (0);
   1078 }
   1079 
   1080 /*
   1081  * sysctl helper function for kern.hostname and kern.domainnname.
   1082  * resets the relevant recorded length when the underlying name is
   1083  * changed.
   1084  */
   1085 static int
   1086 sysctl_setlen(SYSCTLFN_ARGS)
   1087 {
   1088 	int error;
   1089 
   1090 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
   1091 	if (error || newp == NULL)
   1092 		return (error);
   1093 
   1094 	switch (rnode->sysctl_num) {
   1095 	case KERN_HOSTNAME:
   1096 		hostnamelen = strlen((const char*)rnode->sysctl_data);
   1097 		break;
   1098 	case KERN_DOMAINNAME:
   1099 		domainnamelen = strlen((const char*)rnode->sysctl_data);
   1100 		break;
   1101 	}
   1102 
   1103 	return (0);
   1104 }
   1105 
   1106 /*
   1107  * sysctl helper routine for kern.clockrate. Assembles a struct on
   1108  * the fly to be returned to the caller.
   1109  */
   1110 static int
   1111 sysctl_kern_clockrate(SYSCTLFN_ARGS)
   1112 {
   1113 	struct clockinfo clkinfo;
   1114 	struct sysctlnode node;
   1115 
   1116 	clkinfo.tick = tick;
   1117 	clkinfo.tickadj = tickadj;
   1118 	clkinfo.hz = hz;
   1119 	clkinfo.profhz = profhz;
   1120 	clkinfo.stathz = stathz ? stathz : hz;
   1121 
   1122 	node = *rnode;
   1123 	node.sysctl_data = &clkinfo;
   1124 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1125 }
   1126 
   1127 /*
   1128  * sysctl helper routine for kern.msgbufsize and kern.msgbuf. For the
   1129  * former it merely checks the message buffer is set up. For the latter,
   1130  * it also copies out the data if necessary.
   1131  */
   1132 static int
   1133 sysctl_msgbuf(SYSCTLFN_ARGS)
   1134 {
   1135 	char *where = oldp;
   1136 	size_t len, maxlen;
   1137 	long beg, end;
   1138 	extern kmutex_t log_lock;
   1139 	int error;
   1140 
   1141 	if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
   1142 		msgbufenabled = 0;
   1143 		return (ENXIO);
   1144 	}
   1145 
   1146 	switch (rnode->sysctl_num) {
   1147 	case KERN_MSGBUFSIZE: {
   1148 		struct sysctlnode node = *rnode;
   1149 		int msg_bufs = (int)msgbufp->msg_bufs;
   1150 		node.sysctl_data = &msg_bufs;
   1151 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1152 	}
   1153 	case KERN_MSGBUF:
   1154 		break;
   1155 	default:
   1156 		return (EOPNOTSUPP);
   1157 	}
   1158 
   1159 	if (newp != NULL)
   1160 		return (EPERM);
   1161 
   1162 	if (oldp == NULL) {
   1163 		/* always return full buffer size */
   1164 		*oldlenp = msgbufp->msg_bufs;
   1165 		return (0);
   1166 	}
   1167 
   1168 	sysctl_unlock();
   1169 
   1170 	/*
   1171 	 * First, copy from the write pointer to the end of
   1172 	 * message buffer.
   1173 	 */
   1174 	error = 0;
   1175 	mutex_spin_enter(&log_lock);
   1176 	maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
   1177 	beg = msgbufp->msg_bufx;
   1178 	end = msgbufp->msg_bufs;
   1179 	mutex_spin_exit(&log_lock);
   1180 
   1181 	while (maxlen > 0) {
   1182 		len = MIN(end - beg, maxlen);
   1183 		if (len == 0)
   1184 			break;
   1185 		/* XXX unlocked, but hardly matters. */
   1186 		error = dcopyout(l, &msgbufp->msg_bufc[beg], where, len);
   1187 		if (error)
   1188 			break;
   1189 		where += len;
   1190 		maxlen -= len;
   1191 
   1192 		/*
   1193 		 * ... then, copy from the beginning of message buffer to
   1194 		 * the write pointer.
   1195 		 */
   1196 		beg = 0;
   1197 		end = msgbufp->msg_bufx;
   1198 	}
   1199 
   1200 	sysctl_relock();
   1201 	return (error);
   1202 }
   1203 
   1204 /*
   1205  * sysctl helper routine for kern.defcorename. In the case of a new
   1206  * string being assigned, check that it's not a zero-length string.
   1207  * (XXX the check in -current doesn't work, but do we really care?)
   1208  */
   1209 static int
   1210 sysctl_kern_defcorename(SYSCTLFN_ARGS)
   1211 {
   1212 	int error;
   1213 	char *newcorename;
   1214 	struct sysctlnode node;
   1215 
   1216 	newcorename = PNBUF_GET();
   1217 	node = *rnode;
   1218 	node.sysctl_data = &newcorename[0];
   1219 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
   1220 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1221 	if (error || newp == NULL) {
   1222 		goto done;
   1223 	}
   1224 
   1225 	/*
   1226 	 * when sysctl_lookup() deals with a string, it's guaranteed
   1227 	 * to come back nul terminated. So there.  :)
   1228 	 */
   1229 	if (strlen(newcorename) == 0) {
   1230 		error = EINVAL;
   1231 	} else {
   1232 		memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
   1233 		error = 0;
   1234 	}
   1235 done:
   1236 	PNBUF_PUT(newcorename);
   1237 	return error;
   1238 }
   1239 
   1240 /*
   1241  * sysctl helper routine for kern.cp_time node. Adds up cpu time
   1242  * across all cpus.
   1243  */
   1244 static int
   1245 sysctl_kern_cptime(SYSCTLFN_ARGS)
   1246 {
   1247 	struct sysctlnode node = *rnode;
   1248 	uint64_t *cp_time = NULL;
   1249 	int error, n = ncpu, i;
   1250 	struct cpu_info *ci;
   1251 	CPU_INFO_ITERATOR cii;
   1252 
   1253 	/*
   1254 	 * if you specifically pass a buffer that is the size of the
   1255 	 * sum, or if you are probing for the size, you get the "sum"
   1256 	 * of cp_time (and the size thereof) across all processors.
   1257 	 *
   1258 	 * alternately, you can pass an additional mib number and get
   1259 	 * cp_time for that particular processor.
   1260 	 */
   1261 	switch (namelen) {
   1262 	case 0:
   1263 		if (*oldlenp == sizeof(uint64_t) * CPUSTATES || oldp == NULL) {
   1264 			node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
   1265 			n = -1; /* SUM */
   1266 		}
   1267 		else {
   1268 			node.sysctl_size = n * sizeof(uint64_t) * CPUSTATES;
   1269 			n = -2; /* ALL */
   1270 		}
   1271 		break;
   1272 	case 1:
   1273 		if (name[0] < 0 || name[0] >= n)
   1274 			return (ENOENT); /* ENOSUCHPROCESSOR */
   1275 		node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
   1276 		n = name[0];
   1277 		/*
   1278 		 * adjust these so that sysctl_lookup() will be happy
   1279 		 */
   1280 		name++;
   1281 		namelen--;
   1282 		break;
   1283 	default:
   1284 		return (EINVAL);
   1285 	}
   1286 
   1287 	cp_time = kmem_alloc(node.sysctl_size, KM_SLEEP);
   1288 	if (cp_time == NULL)
   1289 		return (ENOMEM);
   1290 	node.sysctl_data = cp_time;
   1291 	memset(cp_time, 0, node.sysctl_size);
   1292 
   1293 	for (CPU_INFO_FOREACH(cii, ci)) {
   1294 		if (n <= 0) {
   1295 			for (i = 0; i < CPUSTATES; i++) {
   1296 				cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
   1297 			}
   1298 		}
   1299 		/*
   1300 		 * if a specific processor was requested and we just
   1301 		 * did it, we're done here
   1302 		 */
   1303 		if (n == 0)
   1304 			break;
   1305 		/*
   1306 		 * if doing "all", skip to next cp_time set for next processor
   1307 		 */
   1308 		if (n == -2)
   1309 			cp_time += CPUSTATES;
   1310 		/*
   1311 		 * if we're doing a specific processor, we're one
   1312 		 * processor closer
   1313 		 */
   1314 		if (n > 0)
   1315 			n--;
   1316 	}
   1317 
   1318 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1319 	kmem_free(node.sysctl_data, node.sysctl_size);
   1320 	return (error);
   1321 }
   1322 
   1323 #if NPTY > 0
   1324 /*
   1325  * sysctl helper routine for kern.maxptys. Ensures that any new value
   1326  * is acceptable to the pty subsystem.
   1327  */
   1328 static int
   1329 sysctl_kern_maxptys(SYSCTLFN_ARGS)
   1330 {
   1331 	int pty_maxptys(int, int);		/* defined in kern/tty_pty.c */
   1332 	int error, xmax;
   1333 	struct sysctlnode node;
   1334 
   1335 	/* get current value of maxptys */
   1336 	xmax = pty_maxptys(0, 0);
   1337 
   1338 	node = *rnode;
   1339 	node.sysctl_data = &xmax;
   1340 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1341 	if (error || newp == NULL)
   1342 		return (error);
   1343 
   1344 	if (xmax != pty_maxptys(xmax, 1))
   1345 		return (EINVAL);
   1346 
   1347 	return (0);
   1348 }
   1349 #endif /* NPTY > 0 */
   1350 
   1351 /*
   1352  * sysctl helper routine for kern.sbmax. Basically just ensures that
   1353  * any new value is not too small.
   1354  */
   1355 static int
   1356 sysctl_kern_sbmax(SYSCTLFN_ARGS)
   1357 {
   1358 	int error, new_sbmax;
   1359 	struct sysctlnode node;
   1360 
   1361 	new_sbmax = sb_max;
   1362 	node = *rnode;
   1363 	node.sysctl_data = &new_sbmax;
   1364 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1365 	if (error || newp == NULL)
   1366 		return (error);
   1367 
   1368 	KERNEL_LOCK(1, NULL);
   1369 	error = sb_max_set(new_sbmax);
   1370 	KERNEL_UNLOCK_ONE(NULL);
   1371 
   1372 	return (error);
   1373 }
   1374 
   1375 /*
   1376  * sysctl helper routine for kern.urandom node. Picks a random number
   1377  * for you.
   1378  */
   1379 static int
   1380 sysctl_kern_urnd(SYSCTLFN_ARGS)
   1381 {
   1382 	int v, rv;
   1383 
   1384 	rv = cprng_strong(sysctl_prng, &v, sizeof(v), 0);
   1385 	if (rv == sizeof(v)) {
   1386 		struct sysctlnode node = *rnode;
   1387 		node.sysctl_data = &v;
   1388 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1389 	}
   1390 	else
   1391 		return (EIO);	/*XXX*/
   1392 }
   1393 
   1394 /*
   1395  * sysctl helper routine for kern.arandom node. Picks a random number
   1396  * for you.
   1397  */
   1398 static int
   1399 sysctl_kern_arnd(SYSCTLFN_ARGS)
   1400 {
   1401 	int error;
   1402 	void *v;
   1403 	struct sysctlnode node = *rnode;
   1404 
   1405 	if (*oldlenp == 0)
   1406 		return 0;
   1407 	/*
   1408 	 * This code used to allow sucking 8192 bytes at a time out
   1409 	 * of the kernel arc4random generator.  Evidently there is some
   1410 	 * very old OpenBSD application code that may try to do this.
   1411 	 *
   1412 	 * Note that this node is documented as type "INT" -- 4 or 8
   1413 	 * bytes, not 8192.
   1414 	 *
   1415 	 * We continue to support this abuse of the "len" pointer here
   1416 	 * but only 256 bytes at a time, as, anecdotally, the actual
   1417 	 * application use here was to generate RC4 keys in userspace.
   1418 	 *
   1419 	 * Support for such large requests will probably be removed
   1420 	 * entirely in the future.
   1421 	 */
   1422 	if (*oldlenp > 256)
   1423 		return E2BIG;
   1424 
   1425 	v = kmem_alloc(*oldlenp, KM_SLEEP);
   1426 	cprng_fast(v, *oldlenp);
   1427 	node.sysctl_data = v;
   1428 	node.sysctl_size = *oldlenp;
   1429 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1430 	kmem_free(v, *oldlenp);
   1431 	return error;
   1432 }
   1433 /*
   1434  * sysctl helper routine to do kern.lwp.* work.
   1435  */
   1436 static int
   1437 sysctl_kern_lwp(SYSCTLFN_ARGS)
   1438 {
   1439 	struct kinfo_lwp klwp;
   1440 	struct proc *p;
   1441 	struct lwp *l2, *l3;
   1442 	char *where, *dp;
   1443 	int pid, elem_size, elem_count;
   1444 	int buflen, needed, error;
   1445 	bool gotit;
   1446 
   1447 	if (namelen == 1 && name[0] == CTL_QUERY)
   1448 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1449 
   1450 	dp = where = oldp;
   1451 	buflen = where != NULL ? *oldlenp : 0;
   1452 	error = needed = 0;
   1453 
   1454 	if (newp != NULL || namelen != 3)
   1455 		return (EINVAL);
   1456 	pid = name[0];
   1457 	elem_size = name[1];
   1458 	elem_count = name[2];
   1459 
   1460 	sysctl_unlock();
   1461 	if (pid == -1) {
   1462 		mutex_enter(proc_lock);
   1463 		PROCLIST_FOREACH(p, &allproc) {
   1464 			/* Grab a hold on the process. */
   1465 			if (!rw_tryenter(&p->p_reflock, RW_READER)) {
   1466 				continue;
   1467 			}
   1468 			mutex_exit(proc_lock);
   1469 
   1470 			mutex_enter(p->p_lock);
   1471 			LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1472 				if (buflen >= elem_size && elem_count > 0) {
   1473 					lwp_lock(l2);
   1474 					fill_lwp(l2, &klwp);
   1475 					lwp_unlock(l2);
   1476 					mutex_exit(p->p_lock);
   1477 
   1478 					/*
   1479 					 * Copy out elem_size, but not
   1480 					 * larger than the size of a
   1481 					 * struct kinfo_proc2.
   1482 					 */
   1483 					error = dcopyout(l, &klwp, dp,
   1484 					    min(sizeof(klwp), elem_size));
   1485 					if (error) {
   1486 						rw_exit(&p->p_reflock);
   1487 						goto cleanup;
   1488 					}
   1489 					mutex_enter(p->p_lock);
   1490 					LIST_FOREACH(l3, &p->p_lwps,
   1491 					    l_sibling) {
   1492 						if (l2 == l3)
   1493 							break;
   1494 					}
   1495 					if (l3 == NULL) {
   1496 						mutex_exit(p->p_lock);
   1497 						rw_exit(&p->p_reflock);
   1498 						error = EAGAIN;
   1499 						goto cleanup;
   1500 					}
   1501 					dp += elem_size;
   1502 					buflen -= elem_size;
   1503 					elem_count--;
   1504 				}
   1505 				needed += elem_size;
   1506 			}
   1507 			mutex_exit(p->p_lock);
   1508 
   1509 			/* Drop reference to process. */
   1510 			mutex_enter(proc_lock);
   1511 			rw_exit(&p->p_reflock);
   1512 		}
   1513 		mutex_exit(proc_lock);
   1514 	} else {
   1515 		mutex_enter(proc_lock);
   1516 		p = proc_find(pid);
   1517 		if (p == NULL) {
   1518 			error = ESRCH;
   1519 			mutex_exit(proc_lock);
   1520 			goto cleanup;
   1521 		}
   1522 		/* Grab a hold on the process. */
   1523 		gotit = rw_tryenter(&p->p_reflock, RW_READER);
   1524 		mutex_exit(proc_lock);
   1525 		if (!gotit) {
   1526 			error = ESRCH;
   1527 			goto cleanup;
   1528 		}
   1529 
   1530 		mutex_enter(p->p_lock);
   1531 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1532 			if (buflen >= elem_size && elem_count > 0) {
   1533 				lwp_lock(l2);
   1534 				fill_lwp(l2, &klwp);
   1535 				lwp_unlock(l2);
   1536 				mutex_exit(p->p_lock);
   1537 				/*
   1538 				 * Copy out elem_size, but not larger than
   1539 				 * the size of a struct kinfo_proc2.
   1540 				 */
   1541 				error = dcopyout(l, &klwp, dp,
   1542 				    min(sizeof(klwp), elem_size));
   1543 				if (error) {
   1544 					rw_exit(&p->p_reflock);
   1545 					goto cleanup;
   1546 				}
   1547 				mutex_enter(p->p_lock);
   1548 				LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
   1549 					if (l2 == l3)
   1550 						break;
   1551 				}
   1552 				if (l3 == NULL) {
   1553 					mutex_exit(p->p_lock);
   1554 					rw_exit(&p->p_reflock);
   1555 					error = EAGAIN;
   1556 					goto cleanup;
   1557 				}
   1558 				dp += elem_size;
   1559 				buflen -= elem_size;
   1560 				elem_count--;
   1561 			}
   1562 			needed += elem_size;
   1563 		}
   1564 		mutex_exit(p->p_lock);
   1565 
   1566 		/* Drop reference to process. */
   1567 		rw_exit(&p->p_reflock);
   1568 	}
   1569 
   1570 	if (where != NULL) {
   1571 		*oldlenp = dp - where;
   1572 		if (needed > *oldlenp) {
   1573 			sysctl_relock();
   1574 			return (ENOMEM);
   1575 		}
   1576 	} else {
   1577 		needed += KERN_LWPSLOP;
   1578 		*oldlenp = needed;
   1579 	}
   1580 	error = 0;
   1581  cleanup:
   1582 	sysctl_relock();
   1583 	return (error);
   1584 }
   1585 
   1586 /*
   1587  * sysctl helper routine for kern.forkfsleep node. Ensures that the
   1588  * given value is not too large or two small, and is at least one
   1589  * timer tick if not zero.
   1590  */
   1591 static int
   1592 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
   1593 {
   1594 	/* userland sees value in ms, internally is in ticks */
   1595 	extern int forkfsleep;		/* defined in kern/kern_fork.c */
   1596 	int error, timo, lsleep;
   1597 	struct sysctlnode node;
   1598 
   1599 	lsleep = forkfsleep * 1000 / hz;
   1600 	node = *rnode;
   1601 	node.sysctl_data = &lsleep;
   1602 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1603 	if (error || newp == NULL)
   1604 		return (error);
   1605 
   1606 	/* refuse negative values, and overly 'long time' */
   1607 	if (lsleep < 0 || lsleep > MAXSLP * 1000)
   1608 		return (EINVAL);
   1609 
   1610 	timo = mstohz(lsleep);
   1611 
   1612 	/* if the interval is >0 ms && <1 tick, use 1 tick */
   1613 	if (lsleep != 0 && timo == 0)
   1614 		forkfsleep = 1;
   1615 	else
   1616 		forkfsleep = timo;
   1617 
   1618 	return (0);
   1619 }
   1620 
   1621 /*
   1622  * sysctl helper routine for kern.root_partition
   1623  */
   1624 static int
   1625 sysctl_kern_root_partition(SYSCTLFN_ARGS)
   1626 {
   1627 	int rootpart = DISKPART(rootdev);
   1628 	struct sysctlnode node = *rnode;
   1629 
   1630 	node.sysctl_data = &rootpart;
   1631 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1632 }
   1633 
   1634 /*
   1635  * sysctl helper function for kern.drivers
   1636  */
   1637 static int
   1638 sysctl_kern_drivers(SYSCTLFN_ARGS)
   1639 {
   1640 	int error;
   1641 	size_t buflen;
   1642 	struct kinfo_drivers kd;
   1643 	char *start, *where;
   1644 	const char *dname;
   1645 	int i;
   1646 	extern struct devsw_conv *devsw_conv;
   1647 	extern int max_devsw_convs;
   1648 
   1649 	if (newp != NULL || namelen != 0)
   1650 		return (EINVAL);
   1651 
   1652 	start = where = oldp;
   1653 	buflen = *oldlenp;
   1654 	if (where == NULL) {
   1655 		*oldlenp = max_devsw_convs * sizeof kd;
   1656 		return 0;
   1657 	}
   1658 
   1659 	/*
   1660 	 * An array of kinfo_drivers structures
   1661 	 */
   1662 	error = 0;
   1663 	sysctl_unlock();
   1664 	mutex_enter(&device_lock);
   1665 	for (i = 0; i < max_devsw_convs; i++) {
   1666 		dname = devsw_conv[i].d_name;
   1667 		if (dname == NULL)
   1668 			continue;
   1669 		if (buflen < sizeof kd) {
   1670 			error = ENOMEM;
   1671 			break;
   1672 		}
   1673 		memset(&kd, 0, sizeof(kd));
   1674 		kd.d_bmajor = devsw_conv[i].d_bmajor;
   1675 		kd.d_cmajor = devsw_conv[i].d_cmajor;
   1676 		strlcpy(kd.d_name, dname, sizeof kd.d_name);
   1677 		mutex_exit(&device_lock);
   1678 		error = dcopyout(l, &kd, where, sizeof kd);
   1679 		mutex_enter(&device_lock);
   1680 		if (error != 0)
   1681 			break;
   1682 		buflen -= sizeof kd;
   1683 		where += sizeof kd;
   1684 	}
   1685 	mutex_exit(&device_lock);
   1686 	sysctl_relock();
   1687 	*oldlenp = where - start;
   1688 	return error;
   1689 }
   1690 
   1691 static int
   1692 sysctl_security_setidcore(SYSCTLFN_ARGS)
   1693 {
   1694 	int newsize, error;
   1695 	struct sysctlnode node;
   1696 
   1697 	node = *rnode;
   1698 	node.sysctl_data = &newsize;
   1699 	newsize = *(int *)rnode->sysctl_data;
   1700 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1701 	if (error || newp == NULL)
   1702 		return error;
   1703 
   1704 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
   1705 	    0, NULL, NULL, NULL))
   1706 		return (EPERM);
   1707 
   1708 	*(int *)rnode->sysctl_data = newsize;
   1709 
   1710 	return 0;
   1711 }
   1712 
   1713 static int
   1714 sysctl_security_setidcorename(SYSCTLFN_ARGS)
   1715 {
   1716 	int error;
   1717 	char *newsetidcorename;
   1718 	struct sysctlnode node;
   1719 
   1720 	newsetidcorename = PNBUF_GET();
   1721 	node = *rnode;
   1722 	node.sysctl_data = newsetidcorename;
   1723 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
   1724 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1725 	if (error || newp == NULL) {
   1726 		goto out;
   1727 	}
   1728 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
   1729 	    0, NULL, NULL, NULL)) {
   1730 		error = EPERM;
   1731 		goto out;
   1732 	}
   1733 	if (strlen(newsetidcorename) == 0) {
   1734 		error = EINVAL;
   1735 		goto out;
   1736 	}
   1737 	memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
   1738 out:
   1739 	PNBUF_PUT(newsetidcorename);
   1740 	return error;
   1741 }
   1742 
   1743 /*
   1744  * sysctl helper routine for kern.cp_id node. Maps cpus to their
   1745  * cpuids.
   1746  */
   1747 static int
   1748 sysctl_kern_cpid(SYSCTLFN_ARGS)
   1749 {
   1750 	struct sysctlnode node = *rnode;
   1751 	uint64_t *cp_id = NULL;
   1752 	int error, n = ncpu;
   1753 	struct cpu_info *ci;
   1754 	CPU_INFO_ITERATOR cii;
   1755 
   1756 	/*
   1757 	 * Here you may either retrieve a single cpu id or the whole
   1758 	 * set. The size you get back when probing depends on what
   1759 	 * you ask for.
   1760 	 */
   1761 	switch (namelen) {
   1762 	case 0:
   1763 		node.sysctl_size = n * sizeof(uint64_t);
   1764 		n = -2; /* ALL */
   1765 		break;
   1766 	case 1:
   1767 		if (name[0] < 0 || name[0] >= n)
   1768 			return (ENOENT); /* ENOSUCHPROCESSOR */
   1769 		node.sysctl_size = sizeof(uint64_t);
   1770 		n = name[0];
   1771 		/*
   1772 		 * adjust these so that sysctl_lookup() will be happy
   1773 		 */
   1774 		name++;
   1775 		namelen--;
   1776 		break;
   1777 	default:
   1778 		return (EINVAL);
   1779 	}
   1780 
   1781 	cp_id = kmem_alloc(node.sysctl_size, KM_SLEEP);
   1782 	if (cp_id == NULL)
   1783 		return (ENOMEM);
   1784 	node.sysctl_data = cp_id;
   1785 	memset(cp_id, 0, node.sysctl_size);
   1786 
   1787 	for (CPU_INFO_FOREACH(cii, ci)) {
   1788 		if (n <= 0)
   1789 			cp_id[0] = cpu_index(ci);
   1790 		/*
   1791 		 * if a specific processor was requested and we just
   1792 		 * did it, we're done here
   1793 		 */
   1794 		if (n == 0)
   1795 			break;
   1796 		/*
   1797 		 * if doing "all", skip to next cp_id slot for next processor
   1798 		 */
   1799 		if (n == -2)
   1800 			cp_id++;
   1801 		/*
   1802 		 * if we're doing a specific processor, we're one
   1803 		 * processor closer
   1804 		 */
   1805 		if (n > 0)
   1806 			n--;
   1807 	}
   1808 
   1809 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1810 	kmem_free(node.sysctl_data, node.sysctl_size);
   1811 	return (error);
   1812 }
   1813 
   1814 /*
   1815  * sysctl helper routine for hw.usermem and hw.usermem64. Values are
   1816  * calculate on the fly taking into account integer overflow and the
   1817  * current wired count.
   1818  */
   1819 static int
   1820 sysctl_hw_usermem(SYSCTLFN_ARGS)
   1821 {
   1822 	u_int ui;
   1823 	u_quad_t uq;
   1824 	struct sysctlnode node;
   1825 
   1826 	node = *rnode;
   1827 	switch (rnode->sysctl_num) {
   1828 	case HW_USERMEM:
   1829 		if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
   1830 			ui = UINT_MAX;
   1831 		else
   1832 			ui *= PAGE_SIZE;
   1833 		node.sysctl_data = &ui;
   1834 		break;
   1835 	case HW_USERMEM64:
   1836 		uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
   1837 		node.sysctl_data = &uq;
   1838 		break;
   1839 	default:
   1840 		return (EINVAL);
   1841 	}
   1842 
   1843 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1844 }
   1845 
   1846 /*
   1847  * sysctl helper routine for kern.cnmagic node. Pulls the old value
   1848  * out, encoded, and stuffs the new value in for decoding.
   1849  */
   1850 static int
   1851 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
   1852 {
   1853 	char magic[CNS_LEN];
   1854 	int error;
   1855 	struct sysctlnode node;
   1856 
   1857 	if (oldp)
   1858 		cn_get_magic(magic, CNS_LEN);
   1859 	node = *rnode;
   1860 	node.sysctl_data = &magic[0];
   1861 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1862 	if (error || newp == NULL)
   1863 		return (error);
   1864 
   1865 	return (cn_set_magic(magic));
   1866 }
   1867 
   1868 /*
   1869  * ********************************************************************
   1870  * section 3: public helper routines that are used for more than one
   1871  * node
   1872  * ********************************************************************
   1873  */
   1874 
   1875 /*
   1876  * sysctl helper routine for the kern.root_device node and some ports'
   1877  * machdep.root_device nodes.
   1878  */
   1879 int
   1880 sysctl_root_device(SYSCTLFN_ARGS)
   1881 {
   1882 	struct sysctlnode node;
   1883 
   1884 	node = *rnode;
   1885 	node.sysctl_data = root_device->dv_xname;
   1886 	node.sysctl_size = strlen(device_xname(root_device)) + 1;
   1887 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1888 }
   1889 
   1890 /*
   1891  * sysctl helper routine for kern.consdev, dependent on the current
   1892  * state of the console. Also used for machdep.console_device on some
   1893  * ports.
   1894  */
   1895 int
   1896 sysctl_consdev(SYSCTLFN_ARGS)
   1897 {
   1898 	dev_t consdev;
   1899 	uint32_t oconsdev;
   1900 	struct sysctlnode node;
   1901 
   1902 	if (cn_tab != NULL)
   1903 		consdev = cn_tab->cn_dev;
   1904 	else
   1905 		consdev = NODEV;
   1906 	node = *rnode;
   1907 	switch (*oldlenp) {
   1908 	case sizeof(consdev):
   1909 		node.sysctl_data = &consdev;
   1910 		node.sysctl_size = sizeof(consdev);
   1911 		break;
   1912 	case sizeof(oconsdev):
   1913 		oconsdev = (uint32_t)consdev;
   1914 		node.sysctl_data = &oconsdev;
   1915 		node.sysctl_size = sizeof(oconsdev);
   1916 		break;
   1917 	default:
   1918 		return EINVAL;
   1919 	}
   1920 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1921 }
   1922 
   1923 /*
   1924  * ********************************************************************
   1925  * section 4: support for some helpers
   1926  * ********************************************************************
   1927  */
   1928 
   1929 
   1930 /*
   1931  * Fill in a kinfo_lwp structure for the specified lwp.
   1932  */
   1933 static void
   1934 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
   1935 {
   1936 	struct proc *p = l->l_proc;
   1937 	struct timeval tv;
   1938 
   1939 	KASSERT(lwp_locked(l, NULL));
   1940 
   1941 	memset(kl, 0, sizeof(*kl));
   1942 
   1943 	kl->l_forw = 0;
   1944 	kl->l_back = 0;
   1945 	kl->l_laddr = PTRTOUINT64(l);
   1946 	kl->l_addr = PTRTOUINT64(l->l_addr);
   1947 	kl->l_stat = l->l_stat;
   1948 	kl->l_lid = l->l_lid;
   1949 	kl->l_flag = L_INMEM;
   1950 	kl->l_flag |= sysctl_map_flags(sysctl_lwpprflagmap, l->l_prflag);
   1951 	kl->l_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
   1952 
   1953 	kl->l_swtime = l->l_swtime;
   1954 	kl->l_slptime = l->l_slptime;
   1955 	if (l->l_stat == LSONPROC)
   1956 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   1957 	else
   1958 		kl->l_schedflags = 0;
   1959 	kl->l_priority = lwp_eprio(l);
   1960 	kl->l_usrpri = l->l_priority;
   1961 	if (l->l_wchan)
   1962 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
   1963 	kl->l_wchan = PTRTOUINT64(l->l_wchan);
   1964 	kl->l_cpuid = cpu_index(l->l_cpu);
   1965 	bintime2timeval(&l->l_rtime, &tv);
   1966 	kl->l_rtime_sec = tv.tv_sec;
   1967 	kl->l_rtime_usec = tv.tv_usec;
   1968 	kl->l_cpticks = l->l_cpticks;
   1969 	kl->l_pctcpu = l->l_pctcpu;
   1970 	kl->l_pid = p->p_pid;
   1971 	if (l->l_name == NULL)
   1972 		kl->l_name[0] = '\0';
   1973 	else
   1974 		strlcpy(kl->l_name, l->l_name, sizeof(kl->l_name));
   1975 }
   1976