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