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