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