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