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