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