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