Home | History | Annotate | Line # | Download | only in kern
init_sysctl.c revision 1.195
      1 /*	$NetBSD: init_sysctl.c,v 1.195 2013/02/21 01:39:55 pgoyette 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.195 2013/02/21 01:39:55 pgoyette 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 }
    731 
    732 SYSCTL_SETUP(sysctl_hw_setup, "sysctl hw subtree setup")
    733 {
    734 	u_int u;
    735 	u_quad_t q;
    736 
    737 	sysctl_createv(clog, 0, NULL, NULL,
    738 		       CTLFLAG_PERMANENT,
    739 		       CTLTYPE_NODE, "hw", NULL,
    740 		       NULL, 0, NULL, 0,
    741 		       CTL_HW, CTL_EOL);
    742 
    743 	sysctl_createv(clog, 0, NULL, NULL,
    744 		       CTLFLAG_PERMANENT,
    745 		       CTLTYPE_STRING, "machine",
    746 		       SYSCTL_DESCR("Machine class"),
    747 		       NULL, 0, machine, 0,
    748 		       CTL_HW, HW_MACHINE, CTL_EOL);
    749 	sysctl_createv(clog, 0, NULL, NULL,
    750 		       CTLFLAG_PERMANENT,
    751 		       CTLTYPE_STRING, "model",
    752 		       SYSCTL_DESCR("Machine model"),
    753 		       NULL, 0, cpu_model, 0,
    754 		       CTL_HW, HW_MODEL, CTL_EOL);
    755 	sysctl_createv(clog, 0, NULL, NULL,
    756 		       CTLFLAG_PERMANENT,
    757 		       CTLTYPE_INT, "ncpu",
    758 		       SYSCTL_DESCR("Number of CPUs configured"),
    759 		       NULL, 0, &ncpu, 0,
    760 		       CTL_HW, HW_NCPU, CTL_EOL);
    761 	sysctl_createv(clog, 0, NULL, NULL,
    762 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    763 		       CTLTYPE_INT, "byteorder",
    764 		       SYSCTL_DESCR("System byte order"),
    765 		       NULL, BYTE_ORDER, NULL, 0,
    766 		       CTL_HW, HW_BYTEORDER, CTL_EOL);
    767 	u = ((u_int)physmem > (UINT_MAX / PAGE_SIZE)) ?
    768 		UINT_MAX : physmem * PAGE_SIZE;
    769 	sysctl_createv(clog, 0, NULL, NULL,
    770 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    771 		       CTLTYPE_INT, "physmem",
    772 		       SYSCTL_DESCR("Bytes of physical memory"),
    773 		       NULL, u, NULL, 0,
    774 		       CTL_HW, HW_PHYSMEM, CTL_EOL);
    775 	sysctl_createv(clog, 0, NULL, NULL,
    776 		       CTLFLAG_PERMANENT,
    777 		       CTLTYPE_INT, "usermem",
    778 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
    779 		       sysctl_hw_usermem, 0, NULL, 0,
    780 		       CTL_HW, HW_USERMEM, CTL_EOL);
    781 	sysctl_createv(clog, 0, NULL, NULL,
    782 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    783 		       CTLTYPE_INT, "pagesize",
    784 		       SYSCTL_DESCR("Software page size"),
    785 		       NULL, PAGE_SIZE, NULL, 0,
    786 		       CTL_HW, HW_PAGESIZE, CTL_EOL);
    787 	sysctl_createv(clog, 0, NULL, NULL,
    788 		       CTLFLAG_PERMANENT,
    789 		       CTLTYPE_STRING, "machine_arch",
    790 		       SYSCTL_DESCR("Machine CPU class"),
    791 		       NULL, 0, machine_arch, 0,
    792 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
    793 	sysctl_createv(clog, 0, NULL, NULL,
    794 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    795 		       CTLTYPE_INT, "alignbytes",
    796 		       SYSCTL_DESCR("Alignment constraint for all possible "
    797 				    "data types"),
    798 		       NULL, ALIGNBYTES, NULL, 0,
    799 		       CTL_HW, HW_ALIGNBYTES, CTL_EOL);
    800 	sysctl_createv(clog, 0, NULL, NULL,
    801 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
    802 		       CTLTYPE_STRING, "cnmagic",
    803 		       SYSCTL_DESCR("Console magic key sequence"),
    804 		       sysctl_hw_cnmagic, 0, NULL, CNS_LEN,
    805 		       CTL_HW, HW_CNMAGIC, CTL_EOL);
    806 	q = (u_quad_t)physmem * PAGE_SIZE;
    807 	sysctl_createv(clog, 0, NULL, NULL,
    808 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    809 		       CTLTYPE_QUAD, "physmem64",
    810 		       SYSCTL_DESCR("Bytes of physical memory"),
    811 		       NULL, q, NULL, 0,
    812 		       CTL_HW, HW_PHYSMEM64, CTL_EOL);
    813 	sysctl_createv(clog, 0, NULL, NULL,
    814 		       CTLFLAG_PERMANENT,
    815 		       CTLTYPE_QUAD, "usermem64",
    816 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
    817 		       sysctl_hw_usermem, 0, NULL, 0,
    818 		       CTL_HW, HW_USERMEM64, CTL_EOL);
    819 	sysctl_createv(clog, 0, NULL, NULL,
    820 		       CTLFLAG_PERMANENT,
    821 		       CTLTYPE_INT, "ncpuonline",
    822 		       SYSCTL_DESCR("Number of CPUs online"),
    823 		       NULL, 0, &ncpuonline, 0,
    824 		       CTL_HW, HW_NCPUONLINE, CTL_EOL);
    825 }
    826 
    827 #ifdef DEBUG
    828 /*
    829  * Debugging related system variables.
    830  */
    831 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
    832 struct ctldebug debug5, debug6, debug7, debug8, debug9;
    833 struct ctldebug debug10, debug11, debug12, debug13, debug14;
    834 struct ctldebug debug15, debug16, debug17, debug18, debug19;
    835 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
    836 	&debug0, &debug1, &debug2, &debug3, &debug4,
    837 	&debug5, &debug6, &debug7, &debug8, &debug9,
    838 	&debug10, &debug11, &debug12, &debug13, &debug14,
    839 	&debug15, &debug16, &debug17, &debug18, &debug19,
    840 };
    841 
    842 /*
    843  * this setup routine is a replacement for debug_sysctl()
    844  *
    845  * note that it creates several nodes per defined debug variable
    846  */
    847 SYSCTL_SETUP(sysctl_debug_setup, "sysctl debug subtree setup")
    848 {
    849 	struct ctldebug *cdp;
    850 	char nodename[20];
    851 	int i;
    852 
    853 	/*
    854 	 * two ways here:
    855 	 *
    856 	 * the "old" way (debug.name -> value) which was emulated by
    857 	 * the sysctl(8) binary
    858 	 *
    859 	 * the new way, which the sysctl(8) binary was actually using
    860 
    861 	 node	debug
    862 	 node	debug.0
    863 	 string debug.0.name
    864 	 int	debug.0.value
    865 	 int	debug.name
    866 
    867 	 */
    868 
    869 	sysctl_createv(clog, 0, NULL, NULL,
    870 		       CTLFLAG_PERMANENT,
    871 		       CTLTYPE_NODE, "debug", NULL,
    872 		       NULL, 0, NULL, 0,
    873 		       CTL_DEBUG, CTL_EOL);
    874 
    875 	for (i = 0; i < CTL_DEBUG_MAXID; i++) {
    876 		cdp = debugvars[i];
    877 		if (cdp->debugname == NULL || cdp->debugvar == NULL)
    878 			continue;
    879 
    880 		snprintf(nodename, sizeof(nodename), "debug%d", i);
    881 		sysctl_createv(clog, 0, NULL, NULL,
    882 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
    883 			       CTLTYPE_NODE, nodename, NULL,
    884 			       NULL, 0, NULL, 0,
    885 			       CTL_DEBUG, i, CTL_EOL);
    886 		sysctl_createv(clog, 0, NULL, NULL,
    887 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
    888 			       CTLTYPE_STRING, "name", NULL,
    889 			       /*XXXUNCONST*/
    890 			       NULL, 0, __UNCONST(cdp->debugname), 0,
    891 			       CTL_DEBUG, i, CTL_DEBUG_NAME, CTL_EOL);
    892 		sysctl_createv(clog, 0, NULL, NULL,
    893 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
    894 			       CTLTYPE_INT, "value", NULL,
    895 			       NULL, 0, cdp->debugvar, 0,
    896 			       CTL_DEBUG, i, CTL_DEBUG_VALUE, CTL_EOL);
    897 		sysctl_createv(clog, 0, NULL, NULL,
    898 			       CTLFLAG_PERMANENT,
    899 			       CTLTYPE_INT, cdp->debugname, NULL,
    900 			       NULL, 0, cdp->debugvar, 0,
    901 			       CTL_DEBUG, CTL_CREATE, CTL_EOL);
    902 	}
    903 }
    904 #endif /* DEBUG */
    905 
    906 /*
    907  * ********************************************************************
    908  * section 2: private node-specific helper routines.
    909  * ********************************************************************
    910  */
    911 
    912 #ifdef DIAGNOSTIC
    913 static int
    914 sysctl_kern_trigger_panic(SYSCTLFN_ARGS)
    915 {
    916 	int newtrig, error;
    917 	struct sysctlnode node;
    918 
    919 	newtrig = 0;
    920 	node = *rnode;
    921 	node.sysctl_data = &newtrig;
    922 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    923 	if (error || newp == NULL)
    924 		return (error);
    925 
    926 	if (newtrig != 0)
    927 		panic("Panic triggered");
    928 
    929 	return (error);
    930 }
    931 #endif
    932 
    933 /*
    934  * sysctl helper routine for kern.maxvnodes.  Drain vnodes if
    935  * new value is lower than desiredvnodes and then calls reinit
    936  * routines that needs to adjust to the new value.
    937  */
    938 static int
    939 sysctl_kern_maxvnodes(SYSCTLFN_ARGS)
    940 {
    941 	int error, new_vnodes, old_vnodes, new_max;
    942 	struct sysctlnode node;
    943 
    944 	new_vnodes = desiredvnodes;
    945 	node = *rnode;
    946 	node.sysctl_data = &new_vnodes;
    947 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    948 	if (error || newp == NULL)
    949 		return (error);
    950 
    951 	/*
    952 	 * sysctl passes down unsigned values, require them
    953 	 * to be positive
    954 	 */
    955 	if (new_vnodes <= 0)
    956 		return (EINVAL);
    957 
    958 	/* Limits: 75% of KVA and physical memory. */
    959 	new_max = calc_cache_size(kernel_map, 75, 75) / VNODE_COST;
    960 	if (new_vnodes > new_max)
    961 		new_vnodes = new_max;
    962 
    963 	old_vnodes = desiredvnodes;
    964 	desiredvnodes = new_vnodes;
    965 	if (new_vnodes < old_vnodes) {
    966 		error = vfs_drainvnodes(new_vnodes);
    967 		if (error) {
    968 			desiredvnodes = old_vnodes;
    969 			return (error);
    970 		}
    971 	}
    972 	vfs_reinit();
    973 	nchreinit();
    974 
    975 	return (0);
    976 }
    977 
    978 /*
    979  * sysctl helper routine for rtc_offset - set time after changes
    980  */
    981 static int
    982 sysctl_kern_rtc_offset(SYSCTLFN_ARGS)
    983 {
    984 	struct timespec ts, delta;
    985 	int error, new_rtc_offset;
    986 	struct sysctlnode node;
    987 
    988 	new_rtc_offset = rtc_offset;
    989 	node = *rnode;
    990 	node.sysctl_data = &new_rtc_offset;
    991 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    992 	if (error || newp == NULL)
    993 		return (error);
    994 
    995 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
    996 	    KAUTH_REQ_SYSTEM_TIME_RTCOFFSET,
    997 	    KAUTH_ARG(new_rtc_offset), NULL, NULL))
    998 		return (EPERM);
    999 	if (rtc_offset == new_rtc_offset)
   1000 		return (0);
   1001 
   1002 	/* if we change the offset, adjust the time */
   1003 	nanotime(&ts);
   1004 	delta.tv_sec = 60 * (new_rtc_offset - rtc_offset);
   1005 	delta.tv_nsec = 0;
   1006 	timespecadd(&ts, &delta, &ts);
   1007 	rtc_offset = new_rtc_offset;
   1008 	return (settime(l->l_proc, &ts));
   1009 }
   1010 
   1011 /*
   1012  * sysctl helper routine for kern.maxproc. Ensures that the new
   1013  * values are not too low or too high.
   1014  */
   1015 static int
   1016 sysctl_kern_maxproc(SYSCTLFN_ARGS)
   1017 {
   1018 	int error, nmaxproc;
   1019 	struct sysctlnode node;
   1020 
   1021 	nmaxproc = maxproc;
   1022 	node = *rnode;
   1023 	node.sysctl_data = &nmaxproc;
   1024 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1025 	if (error || newp == NULL)
   1026 		return (error);
   1027 
   1028 	if (nmaxproc < 0 || nmaxproc >= PID_MAX)
   1029 		return (EINVAL);
   1030 #ifdef __HAVE_CPU_MAXPROC
   1031 	if (nmaxproc > cpu_maxproc())
   1032 		return (EINVAL);
   1033 #endif
   1034 	maxproc = nmaxproc;
   1035 
   1036 	return (0);
   1037 }
   1038 
   1039 /*
   1040  * sysctl helper function for kern.hostid. The hostid is a long, but
   1041  * we export it as an int, so we need to give it a little help.
   1042  */
   1043 static int
   1044 sysctl_kern_hostid(SYSCTLFN_ARGS)
   1045 {
   1046 	int error, inthostid;
   1047 	struct sysctlnode node;
   1048 
   1049 	inthostid = hostid;  /* XXX assumes sizeof int <= sizeof long */
   1050 	node = *rnode;
   1051 	node.sysctl_data = &inthostid;
   1052 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1053 	if (error || newp == NULL)
   1054 		return (error);
   1055 
   1056 	hostid = (unsigned)inthostid;
   1057 
   1058 	return (0);
   1059 }
   1060 
   1061 /*
   1062  * sysctl helper function for kern.hostname and kern.domainnname.
   1063  * resets the relevant recorded length when the underlying name is
   1064  * changed.
   1065  */
   1066 static int
   1067 sysctl_setlen(SYSCTLFN_ARGS)
   1068 {
   1069 	int error;
   1070 
   1071 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
   1072 	if (error || newp == NULL)
   1073 		return (error);
   1074 
   1075 	switch (rnode->sysctl_num) {
   1076 	case KERN_HOSTNAME:
   1077 		hostnamelen = strlen((const char*)rnode->sysctl_data);
   1078 		break;
   1079 	case KERN_DOMAINNAME:
   1080 		domainnamelen = strlen((const char*)rnode->sysctl_data);
   1081 		break;
   1082 	}
   1083 
   1084 	return (0);
   1085 }
   1086 
   1087 /*
   1088  * sysctl helper routine for kern.clockrate. Assembles a struct on
   1089  * the fly to be returned to the caller.
   1090  */
   1091 static int
   1092 sysctl_kern_clockrate(SYSCTLFN_ARGS)
   1093 {
   1094 	struct clockinfo clkinfo;
   1095 	struct sysctlnode node;
   1096 
   1097 	clkinfo.tick = tick;
   1098 	clkinfo.tickadj = tickadj;
   1099 	clkinfo.hz = hz;
   1100 	clkinfo.profhz = profhz;
   1101 	clkinfo.stathz = stathz ? stathz : hz;
   1102 
   1103 	node = *rnode;
   1104 	node.sysctl_data = &clkinfo;
   1105 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1106 }
   1107 
   1108 /*
   1109  * sysctl helper routine for kern.msgbufsize and kern.msgbuf. For the
   1110  * former it merely checks the message buffer is set up. For the latter,
   1111  * it also copies out the data if necessary.
   1112  */
   1113 static int
   1114 sysctl_msgbuf(SYSCTLFN_ARGS)
   1115 {
   1116 	char *where = oldp;
   1117 	size_t len, maxlen;
   1118 	long beg, end;
   1119 	extern kmutex_t log_lock;
   1120 	int error;
   1121 
   1122 	if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
   1123 		msgbufenabled = 0;
   1124 		return (ENXIO);
   1125 	}
   1126 
   1127 	switch (rnode->sysctl_num) {
   1128 	case KERN_MSGBUFSIZE: {
   1129 		struct sysctlnode node = *rnode;
   1130 		int msg_bufs = (int)msgbufp->msg_bufs;
   1131 		node.sysctl_data = &msg_bufs;
   1132 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1133 	}
   1134 	case KERN_MSGBUF:
   1135 		break;
   1136 	default:
   1137 		return (EOPNOTSUPP);
   1138 	}
   1139 
   1140 	if (newp != NULL)
   1141 		return (EPERM);
   1142 
   1143 	if (oldp == NULL) {
   1144 		/* always return full buffer size */
   1145 		*oldlenp = msgbufp->msg_bufs;
   1146 		return (0);
   1147 	}
   1148 
   1149 	sysctl_unlock();
   1150 
   1151 	/*
   1152 	 * First, copy from the write pointer to the end of
   1153 	 * message buffer.
   1154 	 */
   1155 	error = 0;
   1156 	mutex_spin_enter(&log_lock);
   1157 	maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
   1158 	beg = msgbufp->msg_bufx;
   1159 	end = msgbufp->msg_bufs;
   1160 	mutex_spin_exit(&log_lock);
   1161 
   1162 	while (maxlen > 0) {
   1163 		len = MIN(end - beg, maxlen);
   1164 		if (len == 0)
   1165 			break;
   1166 		/* XXX unlocked, but hardly matters. */
   1167 		error = dcopyout(l, &msgbufp->msg_bufc[beg], where, len);
   1168 		if (error)
   1169 			break;
   1170 		where += len;
   1171 		maxlen -= len;
   1172 
   1173 		/*
   1174 		 * ... then, copy from the beginning of message buffer to
   1175 		 * the write pointer.
   1176 		 */
   1177 		beg = 0;
   1178 		end = msgbufp->msg_bufx;
   1179 	}
   1180 
   1181 	sysctl_relock();
   1182 	return (error);
   1183 }
   1184 
   1185 /*
   1186  * sysctl helper routine for kern.defcorename. In the case of a new
   1187  * string being assigned, check that it's not a zero-length string.
   1188  * (XXX the check in -current doesn't work, but do we really care?)
   1189  */
   1190 static int
   1191 sysctl_kern_defcorename(SYSCTLFN_ARGS)
   1192 {
   1193 	int error;
   1194 	char *newcorename;
   1195 	struct sysctlnode node;
   1196 
   1197 	newcorename = PNBUF_GET();
   1198 	node = *rnode;
   1199 	node.sysctl_data = &newcorename[0];
   1200 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
   1201 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1202 	if (error || newp == NULL) {
   1203 		goto done;
   1204 	}
   1205 
   1206 	/*
   1207 	 * when sysctl_lookup() deals with a string, it's guaranteed
   1208 	 * to come back nul terminated. So there.  :)
   1209 	 */
   1210 	if (strlen(newcorename) == 0) {
   1211 		error = EINVAL;
   1212 	} else {
   1213 		memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
   1214 		error = 0;
   1215 	}
   1216 done:
   1217 	PNBUF_PUT(newcorename);
   1218 	return error;
   1219 }
   1220 
   1221 /*
   1222  * sysctl helper routine for kern.cp_time node. Adds up cpu time
   1223  * across all cpus.
   1224  */
   1225 static int
   1226 sysctl_kern_cptime(SYSCTLFN_ARGS)
   1227 {
   1228 	struct sysctlnode node = *rnode;
   1229 	uint64_t *cp_time = NULL;
   1230 	int error, n = ncpu, i;
   1231 	struct cpu_info *ci;
   1232 	CPU_INFO_ITERATOR cii;
   1233 
   1234 	/*
   1235 	 * if you specifically pass a buffer that is the size of the
   1236 	 * sum, or if you are probing for the size, you get the "sum"
   1237 	 * of cp_time (and the size thereof) across all processors.
   1238 	 *
   1239 	 * alternately, you can pass an additional mib number and get
   1240 	 * cp_time for that particular processor.
   1241 	 */
   1242 	switch (namelen) {
   1243 	case 0:
   1244 		if (*oldlenp == sizeof(uint64_t) * CPUSTATES || oldp == NULL) {
   1245 			node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
   1246 			n = -1; /* SUM */
   1247 		}
   1248 		else {
   1249 			node.sysctl_size = n * sizeof(uint64_t) * CPUSTATES;
   1250 			n = -2; /* ALL */
   1251 		}
   1252 		break;
   1253 	case 1:
   1254 		if (name[0] < 0 || name[0] >= n)
   1255 			return (ENOENT); /* ENOSUCHPROCESSOR */
   1256 		node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
   1257 		n = name[0];
   1258 		/*
   1259 		 * adjust these so that sysctl_lookup() will be happy
   1260 		 */
   1261 		name++;
   1262 		namelen--;
   1263 		break;
   1264 	default:
   1265 		return (EINVAL);
   1266 	}
   1267 
   1268 	cp_time = kmem_alloc(node.sysctl_size, KM_SLEEP);
   1269 	if (cp_time == NULL)
   1270 		return (ENOMEM);
   1271 	node.sysctl_data = cp_time;
   1272 	memset(cp_time, 0, node.sysctl_size);
   1273 
   1274 	for (CPU_INFO_FOREACH(cii, ci)) {
   1275 		if (n <= 0) {
   1276 			for (i = 0; i < CPUSTATES; i++) {
   1277 				cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
   1278 			}
   1279 		}
   1280 		/*
   1281 		 * if a specific processor was requested and we just
   1282 		 * did it, we're done here
   1283 		 */
   1284 		if (n == 0)
   1285 			break;
   1286 		/*
   1287 		 * if doing "all", skip to next cp_time set for next processor
   1288 		 */
   1289 		if (n == -2)
   1290 			cp_time += CPUSTATES;
   1291 		/*
   1292 		 * if we're doing a specific processor, we're one
   1293 		 * processor closer
   1294 		 */
   1295 		if (n > 0)
   1296 			n--;
   1297 	}
   1298 
   1299 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1300 	kmem_free(node.sysctl_data, node.sysctl_size);
   1301 	return (error);
   1302 }
   1303 
   1304 #if NPTY > 0
   1305 /*
   1306  * sysctl helper routine for kern.maxptys. Ensures that any new value
   1307  * is acceptable to the pty subsystem.
   1308  */
   1309 static int
   1310 sysctl_kern_maxptys(SYSCTLFN_ARGS)
   1311 {
   1312 	int pty_maxptys(int, int);		/* defined in kern/tty_pty.c */
   1313 	int error, xmax;
   1314 	struct sysctlnode node;
   1315 
   1316 	/* get current value of maxptys */
   1317 	xmax = pty_maxptys(0, 0);
   1318 
   1319 	node = *rnode;
   1320 	node.sysctl_data = &xmax;
   1321 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1322 	if (error || newp == NULL)
   1323 		return (error);
   1324 
   1325 	if (xmax != pty_maxptys(xmax, 1))
   1326 		return (EINVAL);
   1327 
   1328 	return (0);
   1329 }
   1330 #endif /* NPTY > 0 */
   1331 
   1332 /*
   1333  * sysctl helper routine for kern.urandom node. Picks a random number
   1334  * for you.
   1335  */
   1336 static int
   1337 sysctl_kern_urnd(SYSCTLFN_ARGS)
   1338 {
   1339 	int v, rv;
   1340 
   1341 	rv = cprng_strong(sysctl_prng, &v, sizeof(v), 0);
   1342 	if (rv == sizeof(v)) {
   1343 		struct sysctlnode node = *rnode;
   1344 		node.sysctl_data = &v;
   1345 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1346 	}
   1347 	else
   1348 		return (EIO);	/*XXX*/
   1349 }
   1350 
   1351 /*
   1352  * sysctl helper routine for kern.arandom node. Picks a random number
   1353  * for you.
   1354  */
   1355 static int
   1356 sysctl_kern_arnd(SYSCTLFN_ARGS)
   1357 {
   1358 	int error;
   1359 	void *v;
   1360 	struct sysctlnode node = *rnode;
   1361 
   1362 	if (*oldlenp == 0)
   1363 		return 0;
   1364 	/*
   1365 	 * This code used to allow sucking 8192 bytes at a time out
   1366 	 * of the kernel arc4random generator.  Evidently there is some
   1367 	 * very old OpenBSD application code that may try to do this.
   1368 	 *
   1369 	 * Note that this node is documented as type "INT" -- 4 or 8
   1370 	 * bytes, not 8192.
   1371 	 *
   1372 	 * We continue to support this abuse of the "len" pointer here
   1373 	 * but only 256 bytes at a time, as, anecdotally, the actual
   1374 	 * application use here was to generate RC4 keys in userspace.
   1375 	 *
   1376 	 * Support for such large requests will probably be removed
   1377 	 * entirely in the future.
   1378 	 */
   1379 	if (*oldlenp > 256)
   1380 		return E2BIG;
   1381 
   1382 	v = kmem_alloc(*oldlenp, KM_SLEEP);
   1383 	cprng_fast(v, *oldlenp);
   1384 	node.sysctl_data = v;
   1385 	node.sysctl_size = *oldlenp;
   1386 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1387 	kmem_free(v, *oldlenp);
   1388 	return error;
   1389 }
   1390 /*
   1391  * sysctl helper routine to do kern.lwp.* work.
   1392  */
   1393 static int
   1394 sysctl_kern_lwp(SYSCTLFN_ARGS)
   1395 {
   1396 	struct kinfo_lwp klwp;
   1397 	struct proc *p;
   1398 	struct lwp *l2, *l3;
   1399 	char *where, *dp;
   1400 	int pid, elem_size, elem_count;
   1401 	int buflen, needed, error;
   1402 	bool gotit;
   1403 
   1404 	if (namelen == 1 && name[0] == CTL_QUERY)
   1405 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1406 
   1407 	dp = where = oldp;
   1408 	buflen = where != NULL ? *oldlenp : 0;
   1409 	error = needed = 0;
   1410 
   1411 	if (newp != NULL || namelen != 3)
   1412 		return (EINVAL);
   1413 	pid = name[0];
   1414 	elem_size = name[1];
   1415 	elem_count = name[2];
   1416 
   1417 	sysctl_unlock();
   1418 	if (pid == -1) {
   1419 		mutex_enter(proc_lock);
   1420 		PROCLIST_FOREACH(p, &allproc) {
   1421 			/* Grab a hold on the process. */
   1422 			if (!rw_tryenter(&p->p_reflock, RW_READER)) {
   1423 				continue;
   1424 			}
   1425 			mutex_exit(proc_lock);
   1426 
   1427 			mutex_enter(p->p_lock);
   1428 			LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1429 				if (buflen >= elem_size && elem_count > 0) {
   1430 					lwp_lock(l2);
   1431 					fill_lwp(l2, &klwp);
   1432 					lwp_unlock(l2);
   1433 					mutex_exit(p->p_lock);
   1434 
   1435 					/*
   1436 					 * Copy out elem_size, but not
   1437 					 * larger than the size of a
   1438 					 * struct kinfo_proc2.
   1439 					 */
   1440 					error = dcopyout(l, &klwp, dp,
   1441 					    min(sizeof(klwp), elem_size));
   1442 					if (error) {
   1443 						rw_exit(&p->p_reflock);
   1444 						goto cleanup;
   1445 					}
   1446 					mutex_enter(p->p_lock);
   1447 					LIST_FOREACH(l3, &p->p_lwps,
   1448 					    l_sibling) {
   1449 						if (l2 == l3)
   1450 							break;
   1451 					}
   1452 					if (l3 == NULL) {
   1453 						mutex_exit(p->p_lock);
   1454 						rw_exit(&p->p_reflock);
   1455 						error = EAGAIN;
   1456 						goto cleanup;
   1457 					}
   1458 					dp += elem_size;
   1459 					buflen -= elem_size;
   1460 					elem_count--;
   1461 				}
   1462 				needed += elem_size;
   1463 			}
   1464 			mutex_exit(p->p_lock);
   1465 
   1466 			/* Drop reference to process. */
   1467 			mutex_enter(proc_lock);
   1468 			rw_exit(&p->p_reflock);
   1469 		}
   1470 		mutex_exit(proc_lock);
   1471 	} else {
   1472 		mutex_enter(proc_lock);
   1473 		p = proc_find(pid);
   1474 		if (p == NULL) {
   1475 			error = ESRCH;
   1476 			mutex_exit(proc_lock);
   1477 			goto cleanup;
   1478 		}
   1479 		/* Grab a hold on the process. */
   1480 		gotit = rw_tryenter(&p->p_reflock, RW_READER);
   1481 		mutex_exit(proc_lock);
   1482 		if (!gotit) {
   1483 			error = ESRCH;
   1484 			goto cleanup;
   1485 		}
   1486 
   1487 		mutex_enter(p->p_lock);
   1488 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1489 			if (buflen >= elem_size && elem_count > 0) {
   1490 				lwp_lock(l2);
   1491 				fill_lwp(l2, &klwp);
   1492 				lwp_unlock(l2);
   1493 				mutex_exit(p->p_lock);
   1494 				/*
   1495 				 * Copy out elem_size, but not larger than
   1496 				 * the size of a struct kinfo_proc2.
   1497 				 */
   1498 				error = dcopyout(l, &klwp, dp,
   1499 				    min(sizeof(klwp), elem_size));
   1500 				if (error) {
   1501 					rw_exit(&p->p_reflock);
   1502 					goto cleanup;
   1503 				}
   1504 				mutex_enter(p->p_lock);
   1505 				LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
   1506 					if (l2 == l3)
   1507 						break;
   1508 				}
   1509 				if (l3 == NULL) {
   1510 					mutex_exit(p->p_lock);
   1511 					rw_exit(&p->p_reflock);
   1512 					error = EAGAIN;
   1513 					goto cleanup;
   1514 				}
   1515 				dp += elem_size;
   1516 				buflen -= elem_size;
   1517 				elem_count--;
   1518 			}
   1519 			needed += elem_size;
   1520 		}
   1521 		mutex_exit(p->p_lock);
   1522 
   1523 		/* Drop reference to process. */
   1524 		rw_exit(&p->p_reflock);
   1525 	}
   1526 
   1527 	if (where != NULL) {
   1528 		*oldlenp = dp - where;
   1529 		if (needed > *oldlenp) {
   1530 			sysctl_relock();
   1531 			return (ENOMEM);
   1532 		}
   1533 	} else {
   1534 		needed += KERN_LWPSLOP;
   1535 		*oldlenp = needed;
   1536 	}
   1537 	error = 0;
   1538  cleanup:
   1539 	sysctl_relock();
   1540 	return (error);
   1541 }
   1542 
   1543 /*
   1544  * sysctl helper routine for kern.forkfsleep node. Ensures that the
   1545  * given value is not too large or two small, and is at least one
   1546  * timer tick if not zero.
   1547  */
   1548 static int
   1549 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
   1550 {
   1551 	/* userland sees value in ms, internally is in ticks */
   1552 	extern int forkfsleep;		/* defined in kern/kern_fork.c */
   1553 	int error, timo, lsleep;
   1554 	struct sysctlnode node;
   1555 
   1556 	lsleep = forkfsleep * 1000 / hz;
   1557 	node = *rnode;
   1558 	node.sysctl_data = &lsleep;
   1559 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1560 	if (error || newp == NULL)
   1561 		return (error);
   1562 
   1563 	/* refuse negative values, and overly 'long time' */
   1564 	if (lsleep < 0 || lsleep > MAXSLP * 1000)
   1565 		return (EINVAL);
   1566 
   1567 	timo = mstohz(lsleep);
   1568 
   1569 	/* if the interval is >0 ms && <1 tick, use 1 tick */
   1570 	if (lsleep != 0 && timo == 0)
   1571 		forkfsleep = 1;
   1572 	else
   1573 		forkfsleep = timo;
   1574 
   1575 	return (0);
   1576 }
   1577 
   1578 /*
   1579  * sysctl helper routine for kern.root_partition
   1580  */
   1581 static int
   1582 sysctl_kern_root_partition(SYSCTLFN_ARGS)
   1583 {
   1584 	int rootpart = DISKPART(rootdev);
   1585 	struct sysctlnode node = *rnode;
   1586 
   1587 	node.sysctl_data = &rootpart;
   1588 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1589 }
   1590 
   1591 /*
   1592  * sysctl helper function for kern.drivers
   1593  */
   1594 static int
   1595 sysctl_kern_drivers(SYSCTLFN_ARGS)
   1596 {
   1597 	int error;
   1598 	size_t buflen;
   1599 	struct kinfo_drivers kd;
   1600 	char *start, *where;
   1601 	const char *dname;
   1602 	int i;
   1603 	extern struct devsw_conv *devsw_conv;
   1604 	extern int max_devsw_convs;
   1605 
   1606 	start = where = oldp;
   1607 	buflen = *oldlenp;
   1608 	if (where == NULL) {
   1609 		*oldlenp = max_devsw_convs * sizeof kd;
   1610 		return 0;
   1611 	}
   1612 
   1613 	/*
   1614 	 * An array of kinfo_drivers structures
   1615 	 */
   1616 	error = 0;
   1617 	sysctl_unlock();
   1618 	mutex_enter(&device_lock);
   1619 	for (i = 0; i < max_devsw_convs; i++) {
   1620 		dname = devsw_conv[i].d_name;
   1621 		if (dname == NULL)
   1622 			continue;
   1623 		if (buflen < sizeof kd) {
   1624 			error = ENOMEM;
   1625 			break;
   1626 		}
   1627 		memset(&kd, 0, sizeof(kd));
   1628 		kd.d_bmajor = devsw_conv[i].d_bmajor;
   1629 		kd.d_cmajor = devsw_conv[i].d_cmajor;
   1630 		strlcpy(kd.d_name, dname, sizeof kd.d_name);
   1631 		mutex_exit(&device_lock);
   1632 		error = dcopyout(l, &kd, where, sizeof kd);
   1633 		mutex_enter(&device_lock);
   1634 		if (error != 0)
   1635 			break;
   1636 		buflen -= sizeof kd;
   1637 		where += sizeof kd;
   1638 	}
   1639 	mutex_exit(&device_lock);
   1640 	sysctl_relock();
   1641 	*oldlenp = where - start;
   1642 	return error;
   1643 }
   1644 
   1645 static int
   1646 sysctl_security_setidcore(SYSCTLFN_ARGS)
   1647 {
   1648 	int newsize, error;
   1649 	struct sysctlnode node;
   1650 
   1651 	node = *rnode;
   1652 	node.sysctl_data = &newsize;
   1653 	newsize = *(int *)rnode->sysctl_data;
   1654 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1655 	if (error || newp == NULL)
   1656 		return error;
   1657 
   1658 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
   1659 	    0, NULL, NULL, NULL))
   1660 		return (EPERM);
   1661 
   1662 	*(int *)rnode->sysctl_data = newsize;
   1663 
   1664 	return 0;
   1665 }
   1666 
   1667 static int
   1668 sysctl_security_setidcorename(SYSCTLFN_ARGS)
   1669 {
   1670 	int error;
   1671 	char *newsetidcorename;
   1672 	struct sysctlnode node;
   1673 
   1674 	newsetidcorename = PNBUF_GET();
   1675 	node = *rnode;
   1676 	node.sysctl_data = newsetidcorename;
   1677 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
   1678 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1679 	if (error || newp == NULL) {
   1680 		goto out;
   1681 	}
   1682 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
   1683 	    0, NULL, NULL, NULL)) {
   1684 		error = EPERM;
   1685 		goto out;
   1686 	}
   1687 	if (strlen(newsetidcorename) == 0) {
   1688 		error = EINVAL;
   1689 		goto out;
   1690 	}
   1691 	memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
   1692 out:
   1693 	PNBUF_PUT(newsetidcorename);
   1694 	return error;
   1695 }
   1696 
   1697 /*
   1698  * sysctl helper routine for kern.cp_id node. Maps cpus to their
   1699  * cpuids.
   1700  */
   1701 static int
   1702 sysctl_kern_cpid(SYSCTLFN_ARGS)
   1703 {
   1704 	struct sysctlnode node = *rnode;
   1705 	uint64_t *cp_id = NULL;
   1706 	int error, n = ncpu;
   1707 	struct cpu_info *ci;
   1708 	CPU_INFO_ITERATOR cii;
   1709 
   1710 	/*
   1711 	 * Here you may either retrieve a single cpu id or the whole
   1712 	 * set. The size you get back when probing depends on what
   1713 	 * you ask for.
   1714 	 */
   1715 	switch (namelen) {
   1716 	case 0:
   1717 		node.sysctl_size = n * sizeof(uint64_t);
   1718 		n = -2; /* ALL */
   1719 		break;
   1720 	case 1:
   1721 		if (name[0] < 0 || name[0] >= n)
   1722 			return (ENOENT); /* ENOSUCHPROCESSOR */
   1723 		node.sysctl_size = sizeof(uint64_t);
   1724 		n = name[0];
   1725 		/*
   1726 		 * adjust these so that sysctl_lookup() will be happy
   1727 		 */
   1728 		name++;
   1729 		namelen--;
   1730 		break;
   1731 	default:
   1732 		return (EINVAL);
   1733 	}
   1734 
   1735 	cp_id = kmem_alloc(node.sysctl_size, KM_SLEEP);
   1736 	if (cp_id == NULL)
   1737 		return (ENOMEM);
   1738 	node.sysctl_data = cp_id;
   1739 	memset(cp_id, 0, node.sysctl_size);
   1740 
   1741 	for (CPU_INFO_FOREACH(cii, ci)) {
   1742 		if (n <= 0)
   1743 			cp_id[0] = cpu_index(ci);
   1744 		/*
   1745 		 * if a specific processor was requested and we just
   1746 		 * did it, we're done here
   1747 		 */
   1748 		if (n == 0)
   1749 			break;
   1750 		/*
   1751 		 * if doing "all", skip to next cp_id slot for next processor
   1752 		 */
   1753 		if (n == -2)
   1754 			cp_id++;
   1755 		/*
   1756 		 * if we're doing a specific processor, we're one
   1757 		 * processor closer
   1758 		 */
   1759 		if (n > 0)
   1760 			n--;
   1761 	}
   1762 
   1763 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1764 	kmem_free(node.sysctl_data, node.sysctl_size);
   1765 	return (error);
   1766 }
   1767 
   1768 /*
   1769  * sysctl helper routine for hw.usermem and hw.usermem64. Values are
   1770  * calculate on the fly taking into account integer overflow and the
   1771  * current wired count.
   1772  */
   1773 static int
   1774 sysctl_hw_usermem(SYSCTLFN_ARGS)
   1775 {
   1776 	u_int ui;
   1777 	u_quad_t uq;
   1778 	struct sysctlnode node;
   1779 
   1780 	node = *rnode;
   1781 	switch (rnode->sysctl_num) {
   1782 	case HW_USERMEM:
   1783 		if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
   1784 			ui = UINT_MAX;
   1785 		else
   1786 			ui *= PAGE_SIZE;
   1787 		node.sysctl_data = &ui;
   1788 		break;
   1789 	case HW_USERMEM64:
   1790 		uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
   1791 		node.sysctl_data = &uq;
   1792 		break;
   1793 	default:
   1794 		return (EINVAL);
   1795 	}
   1796 
   1797 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1798 }
   1799 
   1800 /*
   1801  * sysctl helper routine for kern.cnmagic node. Pulls the old value
   1802  * out, encoded, and stuffs the new value in for decoding.
   1803  */
   1804 static int
   1805 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
   1806 {
   1807 	char magic[CNS_LEN];
   1808 	int error;
   1809 	struct sysctlnode node;
   1810 
   1811 	if (oldp)
   1812 		cn_get_magic(magic, CNS_LEN);
   1813 	node = *rnode;
   1814 	node.sysctl_data = &magic[0];
   1815 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1816 	if (error || newp == NULL)
   1817 		return (error);
   1818 
   1819 	return (cn_set_magic(magic));
   1820 }
   1821 
   1822 /*
   1823  * ********************************************************************
   1824  * section 3: public helper routines that are used for more than one
   1825  * node
   1826  * ********************************************************************
   1827  */
   1828 
   1829 /*
   1830  * sysctl helper routine for the kern.root_device node and some ports'
   1831  * machdep.root_device nodes.
   1832  */
   1833 int
   1834 sysctl_root_device(SYSCTLFN_ARGS)
   1835 {
   1836 	struct sysctlnode node;
   1837 
   1838 	node = *rnode;
   1839 	node.sysctl_data = __UNCONST(device_xname(root_device));
   1840 	node.sysctl_size = strlen(device_xname(root_device)) + 1;
   1841 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1842 }
   1843 
   1844 /*
   1845  * sysctl helper routine for kern.consdev, dependent on the current
   1846  * state of the console. Also used for machdep.console_device on some
   1847  * ports.
   1848  */
   1849 int
   1850 sysctl_consdev(SYSCTLFN_ARGS)
   1851 {
   1852 	dev_t consdev;
   1853 	uint32_t oconsdev;
   1854 	struct sysctlnode node;
   1855 
   1856 	if (cn_tab != NULL)
   1857 		consdev = cn_tab->cn_dev;
   1858 	else
   1859 		consdev = NODEV;
   1860 	node = *rnode;
   1861 	switch (*oldlenp) {
   1862 	case sizeof(consdev):
   1863 		node.sysctl_data = &consdev;
   1864 		node.sysctl_size = sizeof(consdev);
   1865 		break;
   1866 	case sizeof(oconsdev):
   1867 		oconsdev = (uint32_t)consdev;
   1868 		node.sysctl_data = &oconsdev;
   1869 		node.sysctl_size = sizeof(oconsdev);
   1870 		break;
   1871 	default:
   1872 		return EINVAL;
   1873 	}
   1874 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1875 }
   1876 
   1877 /*
   1878  * ********************************************************************
   1879  * section 4: support for some helpers
   1880  * ********************************************************************
   1881  */
   1882 
   1883 
   1884 /*
   1885  * Fill in a kinfo_lwp structure for the specified lwp.
   1886  */
   1887 static void
   1888 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
   1889 {
   1890 	struct proc *p = l->l_proc;
   1891 	struct timeval tv;
   1892 
   1893 	KASSERT(lwp_locked(l, NULL));
   1894 
   1895 	memset(kl, 0, sizeof(*kl));
   1896 
   1897 	kl->l_forw = 0;
   1898 	kl->l_back = 0;
   1899 	kl->l_laddr = PTRTOUINT64(l);
   1900 	kl->l_addr = PTRTOUINT64(l->l_addr);
   1901 	kl->l_stat = l->l_stat;
   1902 	kl->l_lid = l->l_lid;
   1903 	kl->l_flag = L_INMEM;
   1904 	kl->l_flag |= sysctl_map_flags(sysctl_lwpprflagmap, l->l_prflag);
   1905 	kl->l_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
   1906 
   1907 	kl->l_swtime = l->l_swtime;
   1908 	kl->l_slptime = l->l_slptime;
   1909 	if (l->l_stat == LSONPROC)
   1910 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   1911 	else
   1912 		kl->l_schedflags = 0;
   1913 	kl->l_priority = lwp_eprio(l);
   1914 	kl->l_usrpri = l->l_priority;
   1915 	if (l->l_wchan)
   1916 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
   1917 	kl->l_wchan = PTRTOUINT64(l->l_wchan);
   1918 	kl->l_cpuid = cpu_index(l->l_cpu);
   1919 	bintime2timeval(&l->l_rtime, &tv);
   1920 	kl->l_rtime_sec = tv.tv_sec;
   1921 	kl->l_rtime_usec = tv.tv_usec;
   1922 	kl->l_cpticks = l->l_cpticks;
   1923 	kl->l_pctcpu = l->l_pctcpu;
   1924 	kl->l_pid = p->p_pid;
   1925 	if (l->l_name == NULL)
   1926 		kl->l_name[0] = '\0';
   1927 	else
   1928 		strlcpy(kl->l_name, l->l_name, sizeof(kl->l_name));
   1929 }
   1930