Home | History | Annotate | Line # | Download | only in kern
init_sysctl.c revision 1.138
      1 /*	$NetBSD: init_sysctl.c,v 1.138 2008/05/12 14:28:22 ad Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2003, 2007, 2008 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.138 2008/05/12 14:28:22 ad Exp $");
     34 
     35 #include "opt_sysv.h"
     36 #include "opt_posix.h"
     37 #include "opt_compat_netbsd32.h"
     38 #include "pty.h"
     39 #include "rnd.h"
     40 
     41 #include <sys/types.h>
     42 #include <sys/param.h>
     43 #include <sys/sysctl.h>
     44 #include <sys/cpu.h>
     45 #include <sys/errno.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/unistd.h>
     49 #include <sys/disklabel.h>
     50 #include <sys/rnd.h>
     51 #include <sys/vnode.h>
     52 #include <sys/mount.h>
     53 #include <sys/namei.h>
     54 #include <sys/msgbuf.h>
     55 #include <dev/cons.h>
     56 #include <sys/socketvar.h>
     57 #include <sys/file.h>
     58 #include <sys/filedesc.h>
     59 #include <sys/tty.h>
     60 #include <sys/malloc.h>
     61 #include <sys/resource.h>
     62 #include <sys/resourcevar.h>
     63 #include <sys/exec.h>
     64 #include <sys/conf.h>
     65 #include <sys/device.h>
     66 #include <sys/stat.h>
     67 #include <sys/kauth.h>
     68 #include <sys/ktrace.h>
     69 
     70 #ifdef COMPAT_NETBSD32
     71 #include <compat/netbsd32/netbsd32.h>
     72 #endif
     73 
     74 #include <sys/cpu.h>
     75 
     76 /* XXX this should not be here */
     77 int security_setidcore_dump;
     78 char security_setidcore_path[MAXPATHLEN] = "/var/crash/%n.core";
     79 uid_t security_setidcore_owner = 0;
     80 gid_t security_setidcore_group = 0;
     81 mode_t security_setidcore_mode = (S_IRUSR|S_IWUSR);
     82 
     83 static const u_int sysctl_flagmap[] = {
     84 	PK_ADVLOCK, P_ADVLOCK,
     85 	PK_EXEC, P_EXEC,
     86 	PK_NOCLDWAIT, P_NOCLDWAIT,
     87 	PK_32, P_32,
     88 	PK_CLDSIGIGN, P_CLDSIGIGN,
     89 	PK_SUGID, P_SUGID,
     90 	0
     91 };
     92 
     93 static const u_int sysctl_sflagmap[] = {
     94 	PS_NOCLDSTOP, P_NOCLDSTOP,
     95 	PS_PPWAIT, P_PPWAIT,
     96 	PS_WEXIT, P_WEXIT,
     97 	PS_STOPFORK, P_STOPFORK,
     98 	PS_STOPEXEC, P_STOPEXEC,
     99 	PS_STOPEXIT, P_STOPEXIT,
    100 	0
    101 };
    102 
    103 static const u_int sysctl_slflagmap[] = {
    104 	PSL_TRACED, P_TRACED,
    105 	PSL_FSTRACE, P_FSTRACE,
    106 	PSL_CHTRACED, P_CHTRACED,
    107 	PSL_SYSCALL, P_SYSCALL,
    108 	0
    109 };
    110 
    111 static const u_int sysctl_lflagmap[] = {
    112 	PL_CONTROLT, P_CONTROLT,
    113 	0
    114 };
    115 
    116 static const u_int sysctl_stflagmap[] = {
    117 	PST_PROFIL, P_PROFIL,
    118 	0
    119 
    120 };
    121 
    122 static const u_int sysctl_lwpflagmap[] = {
    123 	LW_INMEM, P_INMEM,
    124 	LW_SINTR, P_SINTR,
    125 	LW_SYSTEM, P_SYSTEM,
    126 	0
    127 };
    128 
    129 static const u_int sysctl_lwpprflagmap[] = {
    130 	LPR_DETACHED, L_DETACHED,
    131 	0
    132 };
    133 
    134 /*
    135  * try over estimating by 5 procs/lwps
    136  */
    137 #define KERN_PROCSLOP	(5 * sizeof(struct kinfo_proc))
    138 #define KERN_LWPSLOP	(5 * sizeof(struct kinfo_lwp))
    139 
    140 static int dcopyout(struct lwp *, const void *, void *, size_t);
    141 
    142 static int
    143 dcopyout(struct lwp *l, const void *kaddr, void *uaddr, size_t len)
    144 {
    145 	int error;
    146 
    147 	error = copyout(kaddr, uaddr, len);
    148 	ktrmibio(-1, UIO_READ, uaddr, len, error);
    149 
    150 	return error;
    151 }
    152 
    153 #ifdef DIAGNOSTIC
    154 static int sysctl_kern_trigger_panic(SYSCTLFN_PROTO);
    155 #endif
    156 static int sysctl_kern_maxvnodes(SYSCTLFN_PROTO);
    157 static int sysctl_kern_rtc_offset(SYSCTLFN_PROTO);
    158 static int sysctl_kern_maxproc(SYSCTLFN_PROTO);
    159 static int sysctl_kern_hostid(SYSCTLFN_PROTO);
    160 static int sysctl_setlen(SYSCTLFN_PROTO);
    161 static int sysctl_kern_clockrate(SYSCTLFN_PROTO);
    162 static int sysctl_kern_file(SYSCTLFN_PROTO);
    163 static int sysctl_msgbuf(SYSCTLFN_PROTO);
    164 static int sysctl_kern_defcorename(SYSCTLFN_PROTO);
    165 static int sysctl_kern_cptime(SYSCTLFN_PROTO);
    166 #if NPTY > 0
    167 static int sysctl_kern_maxptys(SYSCTLFN_PROTO);
    168 #endif /* NPTY > 0 */
    169 static int sysctl_kern_sbmax(SYSCTLFN_PROTO);
    170 static int sysctl_kern_urnd(SYSCTLFN_PROTO);
    171 static int sysctl_kern_arnd(SYSCTLFN_PROTO);
    172 static int sysctl_kern_lwp(SYSCTLFN_PROTO);
    173 static int sysctl_kern_forkfsleep(SYSCTLFN_PROTO);
    174 static int sysctl_kern_root_partition(SYSCTLFN_PROTO);
    175 static int sysctl_kern_drivers(SYSCTLFN_PROTO);
    176 static int sysctl_kern_file2(SYSCTLFN_PROTO);
    177 static int sysctl_security_setidcore(SYSCTLFN_PROTO);
    178 static int sysctl_security_setidcorename(SYSCTLFN_PROTO);
    179 static int sysctl_kern_cpid(SYSCTLFN_PROTO);
    180 static int sysctl_doeproc(SYSCTLFN_PROTO);
    181 static int sysctl_kern_proc_args(SYSCTLFN_PROTO);
    182 static int sysctl_hw_usermem(SYSCTLFN_PROTO);
    183 static int sysctl_hw_cnmagic(SYSCTLFN_PROTO);
    184 
    185 static u_int sysctl_map_flags(const u_int *, u_int);
    186 static void fill_kproc2(struct proc *, struct kinfo_proc2 *, bool);
    187 static void fill_lwp(struct lwp *l, struct kinfo_lwp *kl);
    188 static void fill_file(struct kinfo_file *, const file_t *, const fdfile_t *,
    189 		      int, pid_t);
    190 
    191 /*
    192  * ********************************************************************
    193  * section 1: setup routines
    194  * ********************************************************************
    195  * These functions are stuffed into a link set for sysctl setup
    196  * functions. They're never called or referenced from anywhere else.
    197  * ********************************************************************
    198  */
    199 
    200 /*
    201  * sets up the base nodes...
    202  */
    203 SYSCTL_SETUP(sysctl_root_setup, "sysctl base setup")
    204 {
    205 
    206 	sysctl_createv(clog, 0, NULL, NULL,
    207 		       CTLFLAG_PERMANENT,
    208 		       CTLTYPE_NODE, "kern",
    209 		       SYSCTL_DESCR("High kernel"),
    210 		       NULL, 0, NULL, 0,
    211 		       CTL_KERN, CTL_EOL);
    212 	sysctl_createv(clog, 0, NULL, NULL,
    213 		       CTLFLAG_PERMANENT,
    214 		       CTLTYPE_NODE, "vm",
    215 		       SYSCTL_DESCR("Virtual memory"),
    216 		       NULL, 0, NULL, 0,
    217 		       CTL_VM, CTL_EOL);
    218 	sysctl_createv(clog, 0, NULL, NULL,
    219 		       CTLFLAG_PERMANENT,
    220 		       CTLTYPE_NODE, "vfs",
    221 		       SYSCTL_DESCR("Filesystem"),
    222 		       NULL, 0, NULL, 0,
    223 		       CTL_VFS, CTL_EOL);
    224 	sysctl_createv(clog, 0, NULL, NULL,
    225 		       CTLFLAG_PERMANENT,
    226 		       CTLTYPE_NODE, "net",
    227 		       SYSCTL_DESCR("Networking"),
    228 		       NULL, 0, NULL, 0,
    229 		       CTL_NET, CTL_EOL);
    230 	sysctl_createv(clog, 0, NULL, NULL,
    231 		       CTLFLAG_PERMANENT,
    232 		       CTLTYPE_NODE, "debug",
    233 		       SYSCTL_DESCR("Debugging"),
    234 		       NULL, 0, NULL, 0,
    235 		       CTL_DEBUG, CTL_EOL);
    236 	sysctl_createv(clog, 0, NULL, NULL,
    237 		       CTLFLAG_PERMANENT,
    238 		       CTLTYPE_NODE, "hw",
    239 		       SYSCTL_DESCR("Generic CPU, I/O"),
    240 		       NULL, 0, NULL, 0,
    241 		       CTL_HW, CTL_EOL);
    242 	sysctl_createv(clog, 0, NULL, NULL,
    243 		       CTLFLAG_PERMANENT,
    244 		       CTLTYPE_NODE, "machdep",
    245 		       SYSCTL_DESCR("Machine dependent"),
    246 		       NULL, 0, NULL, 0,
    247 		       CTL_MACHDEP, CTL_EOL);
    248 	/*
    249 	 * this node is inserted so that the sysctl nodes in libc can
    250 	 * operate.
    251 	 */
    252 	sysctl_createv(clog, 0, NULL, NULL,
    253 		       CTLFLAG_PERMANENT,
    254 		       CTLTYPE_NODE, "user",
    255 		       SYSCTL_DESCR("User-level"),
    256 		       NULL, 0, NULL, 0,
    257 		       CTL_USER, CTL_EOL);
    258 	sysctl_createv(clog, 0, NULL, NULL,
    259 		       CTLFLAG_PERMANENT,
    260 		       CTLTYPE_NODE, "ddb",
    261 		       SYSCTL_DESCR("In-kernel debugger"),
    262 		       NULL, 0, NULL, 0,
    263 		       CTL_DDB, CTL_EOL);
    264 	sysctl_createv(clog, 0, NULL, NULL,
    265 		       CTLFLAG_PERMANENT,
    266 		       CTLTYPE_NODE, "proc",
    267 		       SYSCTL_DESCR("Per-process"),
    268 		       NULL, 0, NULL, 0,
    269 		       CTL_PROC, CTL_EOL);
    270 	sysctl_createv(clog, 0, NULL, NULL,
    271 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    272 		       CTLTYPE_NODE, "vendor",
    273 		       SYSCTL_DESCR("Vendor specific"),
    274 		       NULL, 0, NULL, 0,
    275 		       CTL_VENDOR, CTL_EOL);
    276 	sysctl_createv(clog, 0, NULL, NULL,
    277 		       CTLFLAG_PERMANENT,
    278 		       CTLTYPE_NODE, "emul",
    279 		       SYSCTL_DESCR("Emulation settings"),
    280 		       NULL, 0, NULL, 0,
    281 		       CTL_EMUL, CTL_EOL);
    282 	sysctl_createv(clog, 0, NULL, NULL,
    283 		       CTLFLAG_PERMANENT,
    284 		       CTLTYPE_NODE, "security",
    285 		       SYSCTL_DESCR("Security"),
    286 		       NULL, 0, NULL, 0,
    287 		       CTL_SECURITY, CTL_EOL);
    288 }
    289 
    290 /*
    291  * this setup routine is a replacement for kern_sysctl()
    292  */
    293 SYSCTL_SETUP(sysctl_kern_setup, "sysctl kern subtree setup")
    294 {
    295 	extern int kern_logsigexit;	/* defined in kern/kern_sig.c */
    296 	extern fixpt_t ccpu;		/* defined in kern/kern_synch.c */
    297 	extern int dumponpanic;		/* defined in kern/subr_prf.c */
    298 	const struct sysctlnode *rnode;
    299 
    300 	sysctl_createv(clog, 0, NULL, NULL,
    301 		       CTLFLAG_PERMANENT,
    302 		       CTLTYPE_NODE, "kern", NULL,
    303 		       NULL, 0, NULL, 0,
    304 		       CTL_KERN, CTL_EOL);
    305 
    306 	sysctl_createv(clog, 0, NULL, NULL,
    307 		       CTLFLAG_PERMANENT,
    308 		       CTLTYPE_STRING, "ostype",
    309 		       SYSCTL_DESCR("Operating system type"),
    310 		       NULL, 0, &ostype, 0,
    311 		       CTL_KERN, KERN_OSTYPE, CTL_EOL);
    312 	sysctl_createv(clog, 0, NULL, NULL,
    313 		       CTLFLAG_PERMANENT,
    314 		       CTLTYPE_STRING, "osrelease",
    315 		       SYSCTL_DESCR("Operating system release"),
    316 		       NULL, 0, &osrelease, 0,
    317 		       CTL_KERN, KERN_OSRELEASE, CTL_EOL);
    318 	sysctl_createv(clog, 0, NULL, NULL,
    319 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    320 		       CTLTYPE_INT, "osrevision",
    321 		       SYSCTL_DESCR("Operating system revision"),
    322 		       NULL, __NetBSD_Version__, NULL, 0,
    323 		       CTL_KERN, KERN_OSREV, CTL_EOL);
    324 	sysctl_createv(clog, 0, NULL, NULL,
    325 		       CTLFLAG_PERMANENT,
    326 		       CTLTYPE_STRING, "version",
    327 		       SYSCTL_DESCR("Kernel version"),
    328 		       NULL, 0, &version, 0,
    329 		       CTL_KERN, KERN_VERSION, CTL_EOL);
    330 	sysctl_createv(clog, 0, NULL, NULL,
    331 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    332 		       CTLTYPE_INT, "maxvnodes",
    333 		       SYSCTL_DESCR("Maximum number of vnodes"),
    334 		       sysctl_kern_maxvnodes, 0, NULL, 0,
    335 		       CTL_KERN, KERN_MAXVNODES, CTL_EOL);
    336 	sysctl_createv(clog, 0, NULL, NULL,
    337 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    338 		       CTLTYPE_INT, "maxproc",
    339 		       SYSCTL_DESCR("Maximum number of simultaneous processes"),
    340 		       sysctl_kern_maxproc, 0, NULL, 0,
    341 		       CTL_KERN, KERN_MAXPROC, CTL_EOL);
    342 	sysctl_createv(clog, 0, NULL, NULL,
    343 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    344 		       CTLTYPE_INT, "maxfiles",
    345 		       SYSCTL_DESCR("Maximum number of open files"),
    346 		       NULL, 0, &maxfiles, 0,
    347 		       CTL_KERN, KERN_MAXFILES, CTL_EOL);
    348 	sysctl_createv(clog, 0, NULL, NULL,
    349 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    350 		       CTLTYPE_INT, "argmax",
    351 		       SYSCTL_DESCR("Maximum number of bytes of arguments to "
    352 				    "execve(2)"),
    353 		       NULL, ARG_MAX, NULL, 0,
    354 		       CTL_KERN, KERN_ARGMAX, CTL_EOL);
    355 	sysctl_createv(clog, 0, NULL, NULL,
    356 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    357 		       CTLTYPE_STRING, "hostname",
    358 		       SYSCTL_DESCR("System hostname"),
    359 		       sysctl_setlen, 0, &hostname, MAXHOSTNAMELEN,
    360 		       CTL_KERN, KERN_HOSTNAME, CTL_EOL);
    361 	sysctl_createv(clog, 0, NULL, NULL,
    362 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
    363 		       CTLTYPE_INT, "hostid",
    364 		       SYSCTL_DESCR("System host ID number"),
    365 		       sysctl_kern_hostid, 0, NULL, 0,
    366 		       CTL_KERN, KERN_HOSTID, CTL_EOL);
    367 	sysctl_createv(clog, 0, NULL, NULL,
    368 		       CTLFLAG_PERMANENT,
    369 		       CTLTYPE_STRUCT, "clockrate",
    370 		       SYSCTL_DESCR("Kernel clock rates"),
    371 		       sysctl_kern_clockrate, 0, NULL,
    372 		       sizeof(struct clockinfo),
    373 		       CTL_KERN, KERN_CLOCKRATE, CTL_EOL);
    374 	sysctl_createv(clog, 0, NULL, NULL,
    375 		       CTLFLAG_PERMANENT,
    376 		       CTLTYPE_INT, "hardclock_ticks",
    377 		       SYSCTL_DESCR("Number of hardclock ticks"),
    378 		       NULL, 0, &hardclock_ticks, sizeof(hardclock_ticks),
    379 		       CTL_KERN, KERN_HARDCLOCK_TICKS, CTL_EOL);
    380 	sysctl_createv(clog, 0, NULL, NULL,
    381 		       CTLFLAG_PERMANENT,
    382 		       CTLTYPE_STRUCT, "vnode",
    383 		       SYSCTL_DESCR("System vnode table"),
    384 		       sysctl_kern_vnode, 0, NULL, 0,
    385 		       CTL_KERN, KERN_VNODE, CTL_EOL);
    386 	sysctl_createv(clog, 0, NULL, NULL,
    387 		       CTLFLAG_PERMANENT,
    388 		       CTLTYPE_STRUCT, "file",
    389 		       SYSCTL_DESCR("System open file table"),
    390 		       sysctl_kern_file, 0, NULL, 0,
    391 		       CTL_KERN, KERN_FILE, CTL_EOL);
    392 #ifndef GPROF
    393 	sysctl_createv(clog, 0, NULL, NULL,
    394 		       CTLFLAG_PERMANENT,
    395 		       CTLTYPE_NODE, "profiling",
    396 		       SYSCTL_DESCR("Profiling information (not available)"),
    397 		       sysctl_notavail, 0, NULL, 0,
    398 		       CTL_KERN, KERN_PROF, CTL_EOL);
    399 #endif
    400 	sysctl_createv(clog, 0, NULL, NULL,
    401 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    402 		       CTLTYPE_INT, "posix1version",
    403 		       SYSCTL_DESCR("Version of ISO/IEC 9945 (POSIX 1003.1) "
    404 				    "with which the operating system attempts "
    405 				    "to comply"),
    406 		       NULL, _POSIX_VERSION, NULL, 0,
    407 		       CTL_KERN, KERN_POSIX1, CTL_EOL);
    408 	sysctl_createv(clog, 0, NULL, NULL,
    409 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    410 		       CTLTYPE_INT, "ngroups",
    411 		       SYSCTL_DESCR("Maximum number of supplemental groups"),
    412 		       NULL, NGROUPS_MAX, NULL, 0,
    413 		       CTL_KERN, KERN_NGROUPS, CTL_EOL);
    414 	sysctl_createv(clog, 0, NULL, NULL,
    415 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    416 		       CTLTYPE_INT, "job_control",
    417 		       SYSCTL_DESCR("Whether job control is available"),
    418 		       NULL, 1, NULL, 0,
    419 		       CTL_KERN, KERN_JOB_CONTROL, CTL_EOL);
    420 	sysctl_createv(clog, 0, NULL, NULL,
    421 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    422 		       CTLTYPE_INT, "saved_ids",
    423 		       SYSCTL_DESCR("Whether POSIX saved set-group/user ID is "
    424 				    "available"), NULL,
    425 #ifdef _POSIX_SAVED_IDS
    426 		       1,
    427 #else /* _POSIX_SAVED_IDS */
    428 		       0,
    429 #endif /* _POSIX_SAVED_IDS */
    430 		       NULL, 0, CTL_KERN, KERN_SAVED_IDS, CTL_EOL);
    431 	sysctl_createv(clog, 0, NULL, NULL,
    432 		       CTLFLAG_PERMANENT,
    433 		       CTLTYPE_STRUCT, "boottime",
    434 		       SYSCTL_DESCR("System boot time"),
    435 		       NULL, 0, &boottime, sizeof(boottime),
    436 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
    437 	sysctl_createv(clog, 0, NULL, NULL,
    438 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    439 		       CTLTYPE_STRING, "domainname",
    440 		       SYSCTL_DESCR("YP domain name"),
    441 		       sysctl_setlen, 0, &domainname, MAXHOSTNAMELEN,
    442 		       CTL_KERN, KERN_DOMAINNAME, CTL_EOL);
    443 	sysctl_createv(clog, 0, NULL, NULL,
    444 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    445 		       CTLTYPE_INT, "maxpartitions",
    446 		       SYSCTL_DESCR("Maximum number of partitions allowed per "
    447 				    "disk"),
    448 		       NULL, MAXPARTITIONS, NULL, 0,
    449 		       CTL_KERN, KERN_MAXPARTITIONS, CTL_EOL);
    450 	sysctl_createv(clog, 0, NULL, NULL,
    451 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    452 		       CTLTYPE_INT, "rawpartition",
    453 		       SYSCTL_DESCR("Raw partition of a disk"),
    454 		       NULL, RAW_PART, NULL, 0,
    455 		       CTL_KERN, KERN_RAWPARTITION, CTL_EOL);
    456 	sysctl_createv(clog, 0, NULL, NULL,
    457 		       CTLFLAG_PERMANENT,
    458 		       CTLTYPE_STRUCT, "timex", NULL,
    459 		       sysctl_notavail, 0, NULL, 0,
    460 		       CTL_KERN, KERN_TIMEX, CTL_EOL);
    461 	sysctl_createv(clog, 0, NULL, NULL,
    462 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    463 		       CTLTYPE_INT, "rtc_offset",
    464 		       SYSCTL_DESCR("Offset of real time clock from UTC in "
    465 				    "minutes"),
    466 		       sysctl_kern_rtc_offset, 0, &rtc_offset, 0,
    467 		       CTL_KERN, KERN_RTC_OFFSET, CTL_EOL);
    468 	sysctl_createv(clog, 0, NULL, NULL,
    469 		       CTLFLAG_PERMANENT,
    470 		       CTLTYPE_STRING, "root_device",
    471 		       SYSCTL_DESCR("Name of the root device"),
    472 		       sysctl_root_device, 0, NULL, 0,
    473 		       CTL_KERN, KERN_ROOT_DEVICE, CTL_EOL);
    474 	sysctl_createv(clog, 0, NULL, NULL,
    475 		       CTLFLAG_PERMANENT,
    476 		       CTLTYPE_INT, "msgbufsize",
    477 		       SYSCTL_DESCR("Size of the kernel message buffer"),
    478 		       sysctl_msgbuf, 0, NULL, 0,
    479 		       CTL_KERN, KERN_MSGBUFSIZE, CTL_EOL);
    480 	sysctl_createv(clog, 0, NULL, NULL,
    481 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    482 		       CTLTYPE_INT, "fsync",
    483 		       SYSCTL_DESCR("Whether the POSIX 1003.1b File "
    484 				    "Synchronization Option is available on "
    485 				    "this system"),
    486 		       NULL, 1, NULL, 0,
    487 		       CTL_KERN, KERN_FSYNC, CTL_EOL);
    488 	sysctl_createv(clog, 0, NULL, NULL,
    489 		       CTLFLAG_PERMANENT,
    490 		       CTLTYPE_NODE, "ipc",
    491 		       SYSCTL_DESCR("SysV IPC options"),
    492 		       NULL, 0, NULL, 0,
    493 		       CTL_KERN, KERN_SYSVIPC, CTL_EOL);
    494 	sysctl_createv(clog, 0, NULL, NULL,
    495 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    496 		       CTLTYPE_INT, "sysvmsg",
    497 		       SYSCTL_DESCR("System V style message support available"),
    498 		       NULL,
    499 #ifdef SYSVMSG
    500 		       1,
    501 #else /* SYSVMSG */
    502 		       0,
    503 #endif /* SYSVMSG */
    504 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_MSG, CTL_EOL);
    505 	sysctl_createv(clog, 0, NULL, NULL,
    506 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    507 		       CTLTYPE_INT, "sysvsem",
    508 		       SYSCTL_DESCR("System V style semaphore support "
    509 				    "available"), NULL,
    510 #ifdef SYSVSEM
    511 		       1,
    512 #else /* SYSVSEM */
    513 		       0,
    514 #endif /* SYSVSEM */
    515 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SEM, CTL_EOL);
    516 	sysctl_createv(clog, 0, NULL, NULL,
    517 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    518 		       CTLTYPE_INT, "sysvshm",
    519 		       SYSCTL_DESCR("System V style shared memory support "
    520 				    "available"), NULL,
    521 #ifdef SYSVSHM
    522 		       1,
    523 #else /* SYSVSHM */
    524 		       0,
    525 #endif /* SYSVSHM */
    526 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHM, CTL_EOL);
    527 	sysctl_createv(clog, 0, NULL, NULL,
    528 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    529 		       CTLTYPE_INT, "synchronized_io",
    530 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Synchronized "
    531 				    "I/O Option is available on this system"),
    532 		       NULL, 1, NULL, 0,
    533 		       CTL_KERN, KERN_SYNCHRONIZED_IO, CTL_EOL);
    534 	sysctl_createv(clog, 0, NULL, NULL,
    535 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    536 		       CTLTYPE_INT, "iov_max",
    537 		       SYSCTL_DESCR("Maximum number of iovec structures per "
    538 				    "process"),
    539 		       NULL, IOV_MAX, NULL, 0,
    540 		       CTL_KERN, KERN_IOV_MAX, CTL_EOL);
    541 	sysctl_createv(clog, 0, NULL, NULL,
    542 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    543 		       CTLTYPE_INT, "mapped_files",
    544 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory Mapped "
    545 				    "Files Option is available on this system"),
    546 		       NULL, 1, NULL, 0,
    547 		       CTL_KERN, KERN_MAPPED_FILES, CTL_EOL);
    548 	sysctl_createv(clog, 0, NULL, NULL,
    549 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    550 		       CTLTYPE_INT, "memlock",
    551 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Process Memory "
    552 				    "Locking Option is available on this "
    553 				    "system"),
    554 		       NULL, 1, NULL, 0,
    555 		       CTL_KERN, KERN_MEMLOCK, CTL_EOL);
    556 	sysctl_createv(clog, 0, NULL, NULL,
    557 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    558 		       CTLTYPE_INT, "memlock_range",
    559 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Range Memory "
    560 				    "Locking Option is available on this "
    561 				    "system"),
    562 		       NULL, 1, NULL, 0,
    563 		       CTL_KERN, KERN_MEMLOCK_RANGE, CTL_EOL);
    564 	sysctl_createv(clog, 0, NULL, NULL,
    565 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    566 		       CTLTYPE_INT, "memory_protection",
    567 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory "
    568 				    "Protection Option is available on this "
    569 				    "system"),
    570 		       NULL, 1, NULL, 0,
    571 		       CTL_KERN, KERN_MEMORY_PROTECTION, CTL_EOL);
    572 	sysctl_createv(clog, 0, NULL, NULL,
    573 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    574 		       CTLTYPE_INT, "login_name_max",
    575 		       SYSCTL_DESCR("Maximum login name length"),
    576 		       NULL, LOGIN_NAME_MAX, NULL, 0,
    577 		       CTL_KERN, KERN_LOGIN_NAME_MAX, CTL_EOL);
    578 	sysctl_createv(clog, 0, NULL, NULL,
    579 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    580 		       CTLTYPE_STRING, "defcorename",
    581 		       SYSCTL_DESCR("Default core file name"),
    582 		       sysctl_kern_defcorename, 0, defcorename, MAXPATHLEN,
    583 		       CTL_KERN, KERN_DEFCORENAME, CTL_EOL);
    584 	sysctl_createv(clog, 0, NULL, NULL,
    585 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    586 		       CTLTYPE_INT, "logsigexit",
    587 		       SYSCTL_DESCR("Log process exit when caused by signals"),
    588 		       NULL, 0, &kern_logsigexit, 0,
    589 		       CTL_KERN, KERN_LOGSIGEXIT, CTL_EOL);
    590 	sysctl_createv(clog, 0, NULL, NULL,
    591 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    592 		       CTLTYPE_INT, "fscale",
    593 		       SYSCTL_DESCR("Kernel fixed-point scale factor"),
    594 		       NULL, FSCALE, NULL, 0,
    595 		       CTL_KERN, KERN_FSCALE, CTL_EOL);
    596 	sysctl_createv(clog, 0, NULL, NULL,
    597 		       CTLFLAG_PERMANENT,
    598 		       CTLTYPE_INT, "ccpu",
    599 		       SYSCTL_DESCR("Scheduler exponential decay value"),
    600 		       NULL, 0, &ccpu, 0,
    601 		       CTL_KERN, KERN_CCPU, CTL_EOL);
    602 	sysctl_createv(clog, 0, NULL, NULL,
    603 		       CTLFLAG_PERMANENT,
    604 		       CTLTYPE_STRUCT, "cp_time",
    605 		       SYSCTL_DESCR("Clock ticks spent in different CPU states"),
    606 		       sysctl_kern_cptime, 0, NULL, 0,
    607 		       CTL_KERN, KERN_CP_TIME, CTL_EOL);
    608 	sysctl_createv(clog, 0, NULL, NULL,
    609 		       CTLFLAG_PERMANENT,
    610 		       CTLTYPE_INT, "msgbuf",
    611 		       SYSCTL_DESCR("Kernel message buffer"),
    612 		       sysctl_msgbuf, 0, NULL, 0,
    613 		       CTL_KERN, KERN_MSGBUF, CTL_EOL);
    614 	sysctl_createv(clog, 0, NULL, NULL,
    615 		       CTLFLAG_PERMANENT,
    616 		       CTLTYPE_STRUCT, "consdev",
    617 		       SYSCTL_DESCR("Console device"),
    618 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
    619 		       CTL_KERN, KERN_CONSDEV, CTL_EOL);
    620 #if NPTY > 0
    621 	sysctl_createv(clog, 0, NULL, NULL,
    622 		       CTLFLAG_PERMANENT,
    623 		       CTLTYPE_INT, "maxptys",
    624 		       SYSCTL_DESCR("Maximum number of pseudo-ttys"),
    625 		       sysctl_kern_maxptys, 0, NULL, 0,
    626 		       CTL_KERN, KERN_MAXPTYS, CTL_EOL);
    627 #endif /* NPTY > 0 */
    628 	sysctl_createv(clog, 0, NULL, NULL,
    629 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    630 		       CTLTYPE_INT, "maxphys",
    631 		       SYSCTL_DESCR("Maximum raw I/O transfer size"),
    632 		       NULL, MAXPHYS, NULL, 0,
    633 		       CTL_KERN, KERN_MAXPHYS, CTL_EOL);
    634 	sysctl_createv(clog, 0, NULL, NULL,
    635 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    636 		       CTLTYPE_INT, "sbmax",
    637 		       SYSCTL_DESCR("Maximum socket buffer size"),
    638 		       sysctl_kern_sbmax, 0, NULL, 0,
    639 		       CTL_KERN, KERN_SBMAX, CTL_EOL);
    640 	sysctl_createv(clog, 0, NULL, NULL,
    641 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    642 		       CTLTYPE_INT, "monotonic_clock",
    643 		       SYSCTL_DESCR("Implementation version of the POSIX "
    644 				    "1003.1b Monotonic Clock Option"),
    645 		       /* XXX _POSIX_VERSION */
    646 		       NULL, _POSIX_MONOTONIC_CLOCK, NULL, 0,
    647 		       CTL_KERN, KERN_MONOTONIC_CLOCK, CTL_EOL);
    648 	sysctl_createv(clog, 0, NULL, NULL,
    649 		       CTLFLAG_PERMANENT,
    650 		       CTLTYPE_INT, "urandom",
    651 		       SYSCTL_DESCR("Random integer value"),
    652 		       sysctl_kern_urnd, 0, NULL, 0,
    653 		       CTL_KERN, KERN_URND, CTL_EOL);
    654 	sysctl_createv(clog, 0, NULL, NULL,
    655 		       CTLFLAG_PERMANENT,
    656 		       CTLTYPE_INT, "arandom",
    657 		       SYSCTL_DESCR("n bytes of random data"),
    658 		       sysctl_kern_arnd, 0, NULL, 0,
    659 		       CTL_KERN, KERN_ARND, CTL_EOL);
    660 	sysctl_createv(clog, 0, NULL, NULL,
    661 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    662 		       CTLTYPE_INT, "labelsector",
    663 		       SYSCTL_DESCR("Sector number containing the disklabel"),
    664 		       NULL, LABELSECTOR, NULL, 0,
    665 		       CTL_KERN, KERN_LABELSECTOR, CTL_EOL);
    666 	sysctl_createv(clog, 0, NULL, NULL,
    667 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    668 		       CTLTYPE_INT, "labeloffset",
    669 		       SYSCTL_DESCR("Offset of the disklabel within the "
    670 				    "sector"),
    671 		       NULL, LABELOFFSET, NULL, 0,
    672 		       CTL_KERN, KERN_LABELOFFSET, CTL_EOL);
    673 	sysctl_createv(clog, 0, NULL, NULL,
    674 		       CTLFLAG_PERMANENT,
    675 		       CTLTYPE_NODE, "lwp",
    676 		       SYSCTL_DESCR("System-wide LWP information"),
    677 		       sysctl_kern_lwp, 0, NULL, 0,
    678 		       CTL_KERN, KERN_LWP, CTL_EOL);
    679 	sysctl_createv(clog, 0, NULL, NULL,
    680 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    681 		       CTLTYPE_INT, "forkfsleep",
    682 		       SYSCTL_DESCR("Milliseconds to sleep on fork failure due "
    683 				    "to process limits"),
    684 		       sysctl_kern_forkfsleep, 0, NULL, 0,
    685 		       CTL_KERN, KERN_FORKFSLEEP, CTL_EOL);
    686 	sysctl_createv(clog, 0, NULL, NULL,
    687 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    688 		       CTLTYPE_INT, "posix_threads",
    689 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    690 				    "Threads option to which the system "
    691 				    "attempts to conform"),
    692 		       /* XXX _POSIX_VERSION */
    693 		       NULL, _POSIX_THREADS, NULL, 0,
    694 		       CTL_KERN, KERN_POSIX_THREADS, CTL_EOL);
    695 	sysctl_createv(clog, 0, NULL, NULL,
    696 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    697 		       CTLTYPE_INT, "posix_semaphores",
    698 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    699 				    "Semaphores option to which the system "
    700 				    "attempts to conform"), NULL,
    701 #ifdef P1003_1B_SEMAPHORE
    702 		       200112,
    703 #else /* P1003_1B_SEMAPHORE */
    704 		       0,
    705 #endif /* P1003_1B_SEMAPHORE */
    706 		       NULL, 0, CTL_KERN, KERN_POSIX_SEMAPHORES, CTL_EOL);
    707 	sysctl_createv(clog, 0, NULL, NULL,
    708 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    709 		       CTLTYPE_INT, "posix_barriers",
    710 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    711 				    "Barriers option to which the system "
    712 				    "attempts to conform"),
    713 		       /* XXX _POSIX_VERSION */
    714 		       NULL, _POSIX_BARRIERS, NULL, 0,
    715 		       CTL_KERN, KERN_POSIX_BARRIERS, CTL_EOL);
    716 	sysctl_createv(clog, 0, NULL, NULL,
    717 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    718 		       CTLTYPE_INT, "posix_timers",
    719 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    720 				    "Timers option to which the system "
    721 				    "attempts to conform"),
    722 		       /* XXX _POSIX_VERSION */
    723 		       NULL, _POSIX_TIMERS, NULL, 0,
    724 		       CTL_KERN, KERN_POSIX_TIMERS, CTL_EOL);
    725 	sysctl_createv(clog, 0, NULL, NULL,
    726 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    727 		       CTLTYPE_INT, "posix_spin_locks",
    728 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its Spin "
    729 				    "Locks option to which the system attempts "
    730 				    "to conform"),
    731 		       /* XXX _POSIX_VERSION */
    732 		       NULL, _POSIX_SPIN_LOCKS, NULL, 0,
    733 		       CTL_KERN, KERN_POSIX_SPIN_LOCKS, CTL_EOL);
    734 	sysctl_createv(clog, 0, NULL, NULL,
    735 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    736 		       CTLTYPE_INT, "posix_reader_writer_locks",
    737 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
    738 				    "Read-Write Locks option to which the "
    739 				    "system attempts to conform"),
    740 		       /* XXX _POSIX_VERSION */
    741 		       NULL, _POSIX_READER_WRITER_LOCKS, NULL, 0,
    742 		       CTL_KERN, KERN_POSIX_READER_WRITER_LOCKS, CTL_EOL);
    743 	sysctl_createv(clog, 0, NULL, NULL,
    744 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    745 		       CTLTYPE_INT, "dump_on_panic",
    746 		       SYSCTL_DESCR("Perform a crash dump on system panic"),
    747 		       NULL, 0, &dumponpanic, 0,
    748 		       CTL_KERN, KERN_DUMP_ON_PANIC, CTL_EOL);
    749 #ifdef DIAGNOSTIC
    750 	sysctl_createv(clog, 0, NULL, NULL,
    751 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    752 		       CTLTYPE_INT, "panic_now",
    753 		       SYSCTL_DESCR("Trigger a panic"),
    754 		       sysctl_kern_trigger_panic, 0, NULL, 0,
    755 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    756 #endif
    757 	sysctl_createv(clog, 0, NULL, NULL,
    758 		       CTLFLAG_PERMANENT,
    759 		       CTLTYPE_INT, "root_partition",
    760 		       SYSCTL_DESCR("Root partition on the root device"),
    761 		       sysctl_kern_root_partition, 0, NULL, 0,
    762 		       CTL_KERN, KERN_ROOT_PARTITION, CTL_EOL);
    763 	sysctl_createv(clog, 0, NULL, NULL,
    764 		       CTLFLAG_PERMANENT,
    765 		       CTLTYPE_STRUCT, "drivers",
    766 		       SYSCTL_DESCR("List of all drivers with block and "
    767 				    "character device numbers"),
    768 		       sysctl_kern_drivers, 0, NULL, 0,
    769 		       CTL_KERN, KERN_DRIVERS, CTL_EOL);
    770 	sysctl_createv(clog, 0, NULL, NULL,
    771 		       CTLFLAG_PERMANENT,
    772 		       CTLTYPE_STRUCT, "file2",
    773 		       SYSCTL_DESCR("System open file table"),
    774 		       sysctl_kern_file2, 0, NULL, 0,
    775 		       CTL_KERN, KERN_FILE2, CTL_EOL);
    776 	sysctl_createv(clog, 0, NULL, NULL,
    777 		       CTLFLAG_PERMANENT,
    778 		       CTLTYPE_STRUCT, "cp_id",
    779 		       SYSCTL_DESCR("Mapping of CPU number to CPU id"),
    780 		       sysctl_kern_cpid, 0, NULL, 0,
    781 		       CTL_KERN, KERN_CP_ID, CTL_EOL);
    782 	sysctl_createv(clog, 0, NULL, &rnode,
    783 		       CTLFLAG_PERMANENT,
    784 		       CTLTYPE_NODE, "coredump",
    785 		       SYSCTL_DESCR("Coredump settings."),
    786 		       NULL, 0, NULL, 0,
    787 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    788 	sysctl_createv(clog, 0, &rnode, &rnode,
    789 		       CTLFLAG_PERMANENT,
    790 		       CTLTYPE_NODE, "setid",
    791 		       SYSCTL_DESCR("Set-id processes' coredump settings."),
    792 		       NULL, 0, NULL, 0,
    793 		       CTL_CREATE, CTL_EOL);
    794 	sysctl_createv(clog, 0, &rnode, NULL,
    795 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    796 		       CTLTYPE_INT, "dump",
    797 		       SYSCTL_DESCR("Allow set-id processes to dump core."),
    798 		       sysctl_security_setidcore, 0, &security_setidcore_dump,
    799 		       sizeof(security_setidcore_dump),
    800 		       CTL_CREATE, CTL_EOL);
    801 	sysctl_createv(clog, 0, &rnode, NULL,
    802 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    803 		       CTLTYPE_STRING, "path",
    804 		       SYSCTL_DESCR("Path pattern for set-id coredumps."),
    805 		       sysctl_security_setidcorename, 0,
    806 		       &security_setidcore_path,
    807 		       sizeof(security_setidcore_path),
    808 		       CTL_CREATE, CTL_EOL);
    809 	sysctl_createv(clog, 0, &rnode, NULL,
    810 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    811 		       CTLTYPE_INT, "owner",
    812 		       SYSCTL_DESCR("Owner id for set-id processes' cores."),
    813 		       sysctl_security_setidcore, 0, &security_setidcore_owner,
    814 		       0,
    815 		       CTL_CREATE, CTL_EOL);
    816 	sysctl_createv(clog, 0, &rnode, NULL,
    817 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    818 		       CTLTYPE_INT, "group",
    819 		       SYSCTL_DESCR("Group id for set-id processes' cores."),
    820 		       sysctl_security_setidcore, 0, &security_setidcore_group,
    821 		       0,
    822 		       CTL_CREATE, CTL_EOL);
    823 	sysctl_createv(clog, 0, &rnode, NULL,
    824 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    825 		       CTLTYPE_INT, "mode",
    826 		       SYSCTL_DESCR("Mode for set-id processes' cores."),
    827 		       sysctl_security_setidcore, 0, &security_setidcore_mode,
    828 		       0,
    829 		       CTL_CREATE, CTL_EOL);
    830 	sysctl_createv(clog, 0, NULL, NULL,
    831 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    832 		       CTLTYPE_INT, "no_sa_support",
    833 		       SYSCTL_DESCR("0 if the kernel supports SA, otherwise it doesn't"),
    834 		       NULL, 1, NULL, 0,
    835 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    836 }
    837 
    838 SYSCTL_SETUP(sysctl_kern_proc_setup,
    839 	     "sysctl kern.proc/proc2/proc_args subtree setup")
    840 {
    841 
    842 	sysctl_createv(clog, 0, NULL, NULL,
    843 		       CTLFLAG_PERMANENT,
    844 		       CTLTYPE_NODE, "kern", NULL,
    845 		       NULL, 0, NULL, 0,
    846 		       CTL_KERN, CTL_EOL);
    847 
    848 	sysctl_createv(clog, 0, NULL, NULL,
    849 		       CTLFLAG_PERMANENT,
    850 		       CTLTYPE_NODE, "proc",
    851 		       SYSCTL_DESCR("System-wide process information"),
    852 		       sysctl_doeproc, 0, NULL, 0,
    853 		       CTL_KERN, KERN_PROC, CTL_EOL);
    854 	sysctl_createv(clog, 0, NULL, NULL,
    855 		       CTLFLAG_PERMANENT,
    856 		       CTLTYPE_NODE, "proc2",
    857 		       SYSCTL_DESCR("Machine-independent process information"),
    858 		       sysctl_doeproc, 0, NULL, 0,
    859 		       CTL_KERN, KERN_PROC2, CTL_EOL);
    860 	sysctl_createv(clog, 0, NULL, NULL,
    861 		       CTLFLAG_PERMANENT,
    862 		       CTLTYPE_NODE, "proc_args",
    863 		       SYSCTL_DESCR("Process argument information"),
    864 		       sysctl_kern_proc_args, 0, NULL, 0,
    865 		       CTL_KERN, KERN_PROC_ARGS, CTL_EOL);
    866 
    867 	/*
    868 	  "nodes" under these:
    869 
    870 	  KERN_PROC_ALL
    871 	  KERN_PROC_PID pid
    872 	  KERN_PROC_PGRP pgrp
    873 	  KERN_PROC_SESSION sess
    874 	  KERN_PROC_TTY tty
    875 	  KERN_PROC_UID uid
    876 	  KERN_PROC_RUID uid
    877 	  KERN_PROC_GID gid
    878 	  KERN_PROC_RGID gid
    879 
    880 	  all in all, probably not worth the effort...
    881 	*/
    882 }
    883 
    884 SYSCTL_SETUP(sysctl_hw_setup, "sysctl hw subtree setup")
    885 {
    886 	u_int u;
    887 	u_quad_t q;
    888 
    889 	sysctl_createv(clog, 0, NULL, NULL,
    890 		       CTLFLAG_PERMANENT,
    891 		       CTLTYPE_NODE, "hw", NULL,
    892 		       NULL, 0, NULL, 0,
    893 		       CTL_HW, CTL_EOL);
    894 
    895 	sysctl_createv(clog, 0, NULL, NULL,
    896 		       CTLFLAG_PERMANENT,
    897 		       CTLTYPE_STRING, "machine",
    898 		       SYSCTL_DESCR("Machine class"),
    899 		       NULL, 0, machine, 0,
    900 		       CTL_HW, HW_MACHINE, CTL_EOL);
    901 	sysctl_createv(clog, 0, NULL, NULL,
    902 		       CTLFLAG_PERMANENT,
    903 		       CTLTYPE_STRING, "model",
    904 		       SYSCTL_DESCR("Machine model"),
    905 		       NULL, 0, cpu_model, 0,
    906 		       CTL_HW, HW_MODEL, CTL_EOL);
    907 	sysctl_createv(clog, 0, NULL, NULL,
    908 		       CTLFLAG_PERMANENT,
    909 		       CTLTYPE_INT, "ncpu",
    910 		       SYSCTL_DESCR("Number of CPUs configured"),
    911 		       NULL, 0, &ncpu, 0,
    912 		       CTL_HW, HW_NCPU, CTL_EOL);
    913 	sysctl_createv(clog, 0, NULL, NULL,
    914 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    915 		       CTLTYPE_INT, "byteorder",
    916 		       SYSCTL_DESCR("System byte order"),
    917 		       NULL, BYTE_ORDER, NULL, 0,
    918 		       CTL_HW, HW_BYTEORDER, CTL_EOL);
    919 	u = ((u_int)physmem > (UINT_MAX / PAGE_SIZE)) ?
    920 		UINT_MAX : physmem * PAGE_SIZE;
    921 	sysctl_createv(clog, 0, NULL, NULL,
    922 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    923 		       CTLTYPE_INT, "physmem",
    924 		       SYSCTL_DESCR("Bytes of physical memory"),
    925 		       NULL, u, NULL, 0,
    926 		       CTL_HW, HW_PHYSMEM, CTL_EOL);
    927 	sysctl_createv(clog, 0, NULL, NULL,
    928 		       CTLFLAG_PERMANENT,
    929 		       CTLTYPE_INT, "usermem",
    930 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
    931 		       sysctl_hw_usermem, 0, NULL, 0,
    932 		       CTL_HW, HW_USERMEM, CTL_EOL);
    933 	sysctl_createv(clog, 0, NULL, NULL,
    934 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    935 		       CTLTYPE_INT, "pagesize",
    936 		       SYSCTL_DESCR("Software page size"),
    937 		       NULL, PAGE_SIZE, NULL, 0,
    938 		       CTL_HW, HW_PAGESIZE, CTL_EOL);
    939 	sysctl_createv(clog, 0, NULL, NULL,
    940 		       CTLFLAG_PERMANENT,
    941 		       CTLTYPE_STRING, "machine_arch",
    942 		       SYSCTL_DESCR("Machine CPU class"),
    943 		       NULL, 0, machine_arch, 0,
    944 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
    945 	sysctl_createv(clog, 0, NULL, NULL,
    946 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    947 		       CTLTYPE_INT, "alignbytes",
    948 		       SYSCTL_DESCR("Alignment constraint for all possible "
    949 				    "data types"),
    950 		       NULL, ALIGNBYTES, NULL, 0,
    951 		       CTL_HW, HW_ALIGNBYTES, CTL_EOL);
    952 	sysctl_createv(clog, 0, NULL, NULL,
    953 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
    954 		       CTLTYPE_STRING, "cnmagic",
    955 		       SYSCTL_DESCR("Console magic key sequence"),
    956 		       sysctl_hw_cnmagic, 0, NULL, CNS_LEN,
    957 		       CTL_HW, HW_CNMAGIC, CTL_EOL);
    958 	q = (u_quad_t)physmem * PAGE_SIZE;
    959 	sysctl_createv(clog, 0, NULL, NULL,
    960 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    961 		       CTLTYPE_QUAD, "physmem64",
    962 		       SYSCTL_DESCR("Bytes of physical memory"),
    963 		       NULL, q, NULL, 0,
    964 		       CTL_HW, HW_PHYSMEM64, CTL_EOL);
    965 	sysctl_createv(clog, 0, NULL, NULL,
    966 		       CTLFLAG_PERMANENT,
    967 		       CTLTYPE_QUAD, "usermem64",
    968 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
    969 		       sysctl_hw_usermem, 0, NULL, 0,
    970 		       CTL_HW, HW_USERMEM64, CTL_EOL);
    971 	sysctl_createv(clog, 0, NULL, NULL,
    972 		       CTLFLAG_PERMANENT,
    973 		       CTLTYPE_INT, "ncpuonline",
    974 		       SYSCTL_DESCR("Number of CPUs online"),
    975 		       NULL, 0, &ncpuonline, 0,
    976 		       CTL_HW, HW_NCPUONLINE, CTL_EOL);
    977 }
    978 
    979 #ifdef DEBUG
    980 /*
    981  * Debugging related system variables.
    982  */
    983 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
    984 struct ctldebug debug5, debug6, debug7, debug8, debug9;
    985 struct ctldebug debug10, debug11, debug12, debug13, debug14;
    986 struct ctldebug debug15, debug16, debug17, debug18, debug19;
    987 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
    988 	&debug0, &debug1, &debug2, &debug3, &debug4,
    989 	&debug5, &debug6, &debug7, &debug8, &debug9,
    990 	&debug10, &debug11, &debug12, &debug13, &debug14,
    991 	&debug15, &debug16, &debug17, &debug18, &debug19,
    992 };
    993 
    994 /*
    995  * this setup routine is a replacement for debug_sysctl()
    996  *
    997  * note that it creates several nodes per defined debug variable
    998  */
    999 SYSCTL_SETUP(sysctl_debug_setup, "sysctl debug subtree setup")
   1000 {
   1001 	struct ctldebug *cdp;
   1002 	char nodename[20];
   1003 	int i;
   1004 
   1005 	/*
   1006 	 * two ways here:
   1007 	 *
   1008 	 * the "old" way (debug.name -> value) which was emulated by
   1009 	 * the sysctl(8) binary
   1010 	 *
   1011 	 * the new way, which the sysctl(8) binary was actually using
   1012 
   1013 	 node	debug
   1014 	 node	debug.0
   1015 	 string debug.0.name
   1016 	 int	debug.0.value
   1017 	 int	debug.name
   1018 
   1019 	 */
   1020 
   1021 	sysctl_createv(clog, 0, NULL, NULL,
   1022 		       CTLFLAG_PERMANENT,
   1023 		       CTLTYPE_NODE, "debug", NULL,
   1024 		       NULL, 0, NULL, 0,
   1025 		       CTL_DEBUG, CTL_EOL);
   1026 
   1027 	for (i = 0; i < CTL_DEBUG_MAXID; i++) {
   1028 		cdp = debugvars[i];
   1029 		if (cdp->debugname == NULL || cdp->debugvar == NULL)
   1030 			continue;
   1031 
   1032 		snprintf(nodename, sizeof(nodename), "debug%d", i);
   1033 		sysctl_createv(clog, 0, NULL, NULL,
   1034 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
   1035 			       CTLTYPE_NODE, nodename, NULL,
   1036 			       NULL, 0, NULL, 0,
   1037 			       CTL_DEBUG, i, CTL_EOL);
   1038 		sysctl_createv(clog, 0, NULL, NULL,
   1039 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
   1040 			       CTLTYPE_STRING, "name", NULL,
   1041 			       /*XXXUNCONST*/
   1042 			       NULL, 0, __UNCONST(cdp->debugname), 0,
   1043 			       CTL_DEBUG, i, CTL_DEBUG_NAME, CTL_EOL);
   1044 		sysctl_createv(clog, 0, NULL, NULL,
   1045 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
   1046 			       CTLTYPE_INT, "value", NULL,
   1047 			       NULL, 0, cdp->debugvar, 0,
   1048 			       CTL_DEBUG, i, CTL_DEBUG_VALUE, CTL_EOL);
   1049 		sysctl_createv(clog, 0, NULL, NULL,
   1050 			       CTLFLAG_PERMANENT,
   1051 			       CTLTYPE_INT, cdp->debugname, NULL,
   1052 			       NULL, 0, cdp->debugvar, 0,
   1053 			       CTL_DEBUG, CTL_CREATE, CTL_EOL);
   1054 	}
   1055 }
   1056 #endif /* DEBUG */
   1057 
   1058 /*
   1059  * ********************************************************************
   1060  * section 2: private node-specific helper routines.
   1061  * ********************************************************************
   1062  */
   1063 
   1064 #ifdef DIAGNOSTIC
   1065 static int
   1066 sysctl_kern_trigger_panic(SYSCTLFN_ARGS)
   1067 {
   1068 	int newtrig, error;
   1069 	struct sysctlnode node;
   1070 
   1071 	newtrig = 0;
   1072 	node = *rnode;
   1073 	node.sysctl_data = &newtrig;
   1074 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1075 	if (error || newp == NULL)
   1076 		return (error);
   1077 
   1078 	if (newtrig != 0)
   1079 		panic("Panic triggered");
   1080 
   1081 	return (error);
   1082 }
   1083 #endif
   1084 
   1085 /*
   1086  * sysctl helper routine for kern.maxvnodes.  Drain vnodes if
   1087  * new value is lower than desiredvnodes and then calls reinit
   1088  * routines that needs to adjust to the new value.
   1089  */
   1090 static int
   1091 sysctl_kern_maxvnodes(SYSCTLFN_ARGS)
   1092 {
   1093 	int error, new_vnodes, old_vnodes;
   1094 	struct sysctlnode node;
   1095 
   1096 	new_vnodes = desiredvnodes;
   1097 	node = *rnode;
   1098 	node.sysctl_data = &new_vnodes;
   1099 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1100 	if (error || newp == NULL)
   1101 		return (error);
   1102 
   1103 	old_vnodes = desiredvnodes;
   1104 	desiredvnodes = new_vnodes;
   1105 	if (new_vnodes < old_vnodes) {
   1106 		error = vfs_drainvnodes(new_vnodes, l);
   1107 		if (error) {
   1108 			desiredvnodes = old_vnodes;
   1109 			return (error);
   1110 		}
   1111 	}
   1112 	vfs_reinit();
   1113 	nchreinit();
   1114 
   1115 	return (0);
   1116 }
   1117 
   1118 /*
   1119  * sysctl helper routine for rtc_offset - set time after changes
   1120  */
   1121 static int
   1122 sysctl_kern_rtc_offset(SYSCTLFN_ARGS)
   1123 {
   1124 	struct timespec ts, delta;
   1125 	int error, new_rtc_offset;
   1126 	struct sysctlnode node;
   1127 
   1128 	new_rtc_offset = rtc_offset;
   1129 	node = *rnode;
   1130 	node.sysctl_data = &new_rtc_offset;
   1131 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1132 	if (error || newp == NULL)
   1133 		return (error);
   1134 
   1135 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
   1136 	    KAUTH_REQ_SYSTEM_TIME_RTCOFFSET,
   1137 	    KAUTH_ARG(new_rtc_offset), NULL, NULL))
   1138 		return (EPERM);
   1139 	if (rtc_offset == new_rtc_offset)
   1140 		return (0);
   1141 
   1142 	/* if we change the offset, adjust the time */
   1143 	nanotime(&ts);
   1144 	delta.tv_sec = 60 * (new_rtc_offset - rtc_offset);
   1145 	delta.tv_nsec = 0;
   1146 	timespecadd(&ts, &delta, &ts);
   1147 	rtc_offset = new_rtc_offset;
   1148 	return (settime(l->l_proc, &ts));
   1149 }
   1150 
   1151 /*
   1152  * sysctl helper routine for kern.maxproc. Ensures that the new
   1153  * values are not too low or too high.
   1154  */
   1155 static int
   1156 sysctl_kern_maxproc(SYSCTLFN_ARGS)
   1157 {
   1158 	int error, nmaxproc;
   1159 	struct sysctlnode node;
   1160 
   1161 	nmaxproc = maxproc;
   1162 	node = *rnode;
   1163 	node.sysctl_data = &nmaxproc;
   1164 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1165 	if (error || newp == NULL)
   1166 		return (error);
   1167 
   1168 	if (nmaxproc < 0 || nmaxproc >= PID_MAX)
   1169 		return (EINVAL);
   1170 #ifdef __HAVE_CPU_MAXPROC
   1171 	if (nmaxproc > cpu_maxproc())
   1172 		return (EINVAL);
   1173 #endif
   1174 	maxproc = nmaxproc;
   1175 
   1176 	return (0);
   1177 }
   1178 
   1179 /*
   1180  * sysctl helper function for kern.hostid. The hostid is a long, but
   1181  * we export it as an int, so we need to give it a little help.
   1182  */
   1183 static int
   1184 sysctl_kern_hostid(SYSCTLFN_ARGS)
   1185 {
   1186 	int error, inthostid;
   1187 	struct sysctlnode node;
   1188 
   1189 	inthostid = hostid;  /* XXX assumes sizeof int <= sizeof long */
   1190 	node = *rnode;
   1191 	node.sysctl_data = &inthostid;
   1192 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1193 	if (error || newp == NULL)
   1194 		return (error);
   1195 
   1196 	hostid = (unsigned)inthostid;
   1197 
   1198 	return (0);
   1199 }
   1200 
   1201 /*
   1202  * sysctl helper function for kern.hostname and kern.domainnname.
   1203  * resets the relevant recorded length when the underlying name is
   1204  * changed.
   1205  */
   1206 static int
   1207 sysctl_setlen(SYSCTLFN_ARGS)
   1208 {
   1209 	int error;
   1210 
   1211 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
   1212 	if (error || newp == NULL)
   1213 		return (error);
   1214 
   1215 	switch (rnode->sysctl_num) {
   1216 	case KERN_HOSTNAME:
   1217 		hostnamelen = strlen((const char*)rnode->sysctl_data);
   1218 		break;
   1219 	case KERN_DOMAINNAME:
   1220 		domainnamelen = strlen((const char*)rnode->sysctl_data);
   1221 		break;
   1222 	}
   1223 
   1224 	return (0);
   1225 }
   1226 
   1227 /*
   1228  * sysctl helper routine for kern.clockrate. Assembles a struct on
   1229  * the fly to be returned to the caller.
   1230  */
   1231 static int
   1232 sysctl_kern_clockrate(SYSCTLFN_ARGS)
   1233 {
   1234 	struct clockinfo clkinfo;
   1235 	struct sysctlnode node;
   1236 
   1237 	clkinfo.tick = tick;
   1238 	clkinfo.tickadj = tickadj;
   1239 	clkinfo.hz = hz;
   1240 	clkinfo.profhz = profhz;
   1241 	clkinfo.stathz = stathz ? stathz : hz;
   1242 
   1243 	node = *rnode;
   1244 	node.sysctl_data = &clkinfo;
   1245 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1246 }
   1247 
   1248 
   1249 /*
   1250  * sysctl helper routine for kern.file pseudo-subtree.
   1251  */
   1252 static int
   1253 sysctl_kern_file(SYSCTLFN_ARGS)
   1254 {
   1255 	int error;
   1256 	size_t buflen;
   1257 	struct file *fp, *dp, *np, fbuf;
   1258 	char *start, *where;
   1259 
   1260 	start = where = oldp;
   1261 	buflen = *oldlenp;
   1262 	dp = NULL;
   1263 
   1264 	if (where == NULL) {
   1265 		/*
   1266 		 * overestimate by 10 files
   1267 		 */
   1268 		*oldlenp = sizeof(filehead) + (nfiles + 10) *
   1269 		    sizeof(struct file);
   1270 		return (0);
   1271 	}
   1272 
   1273 	/*
   1274 	 * first dcopyout filehead
   1275 	 */
   1276 	if (buflen < sizeof(filehead)) {
   1277 		*oldlenp = 0;
   1278 		return (0);
   1279 	}
   1280 	sysctl_unlock();
   1281 	error = dcopyout(l, &filehead, where, sizeof(filehead));
   1282 	if (error) {
   1283 	 	sysctl_relock();
   1284 		return error;
   1285 	}
   1286 	buflen -= sizeof(filehead);
   1287 	where += sizeof(filehead);
   1288 
   1289 	/*
   1290 	 * allocate dummy file descriptor to make position in list
   1291 	 */
   1292 	if ((dp = fgetdummy()) == NULL) {
   1293 	 	sysctl_relock();
   1294 		return ENOMEM;
   1295 	}
   1296 
   1297 	/*
   1298 	 * followed by an array of file structures
   1299 	 */
   1300 	mutex_enter(&filelist_lock);
   1301 	for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) {
   1302 	    	np = LIST_NEXT(fp, f_list);
   1303 	    	mutex_enter(&fp->f_lock);
   1304 	    	if (fp->f_count == 0) {
   1305 		    	mutex_exit(&fp->f_lock);
   1306 	    		continue;
   1307 		}
   1308 		/*
   1309 		 * XXX Need to prevent that from being an alternative way
   1310 		 * XXX to getting process information.
   1311 		 */
   1312 		if (kauth_authorize_generic(l->l_cred,
   1313 		    KAUTH_GENERIC_CANSEE, fp->f_cred) != 0) {
   1314 		    	mutex_exit(&fp->f_lock);
   1315 			continue;
   1316 		}
   1317 		if (buflen < sizeof(struct file)) {
   1318 			*oldlenp = where - start;
   1319 		    	mutex_exit(&fp->f_lock);
   1320 			error = ENOMEM;
   1321 			break;
   1322 		}
   1323 		memcpy(&fbuf, fp, sizeof(fbuf));
   1324 		LIST_INSERT_AFTER(fp, dp, f_list);
   1325 	    	mutex_exit(&fp->f_lock);
   1326 		mutex_exit(&filelist_lock);
   1327 		error = dcopyout(l, &fbuf, where, sizeof(fbuf));
   1328 		if (error) {
   1329 			mutex_enter(&filelist_lock);
   1330 			LIST_REMOVE(dp, f_list);
   1331 			break;
   1332 		}
   1333 		buflen -= sizeof(struct file);
   1334 		where += sizeof(struct file);
   1335 		mutex_enter(&filelist_lock);
   1336 		np = LIST_NEXT(dp, f_list);
   1337 		LIST_REMOVE(dp, f_list);
   1338 	}
   1339 	mutex_exit(&filelist_lock);
   1340 	*oldlenp = where - start;
   1341  	if (dp != NULL)
   1342 		fputdummy(dp);
   1343  	sysctl_relock();
   1344 	return (error);
   1345 }
   1346 
   1347 /*
   1348  * sysctl helper routine for kern.msgbufsize and kern.msgbuf. For the
   1349  * former it merely checks the message buffer is set up. For the latter,
   1350  * it also copies out the data if necessary.
   1351  */
   1352 static int
   1353 sysctl_msgbuf(SYSCTLFN_ARGS)
   1354 {
   1355 	char *where = oldp;
   1356 	size_t len, maxlen;
   1357 	long beg, end;
   1358 	extern kmutex_t log_lock;
   1359 	int error;
   1360 
   1361 	if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
   1362 		msgbufenabled = 0;
   1363 		return (ENXIO);
   1364 	}
   1365 
   1366 	switch (rnode->sysctl_num) {
   1367 	case KERN_MSGBUFSIZE: {
   1368 		struct sysctlnode node = *rnode;
   1369 		int msg_bufs = (int)msgbufp->msg_bufs;
   1370 		node.sysctl_data = &msg_bufs;
   1371 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1372 	}
   1373 	case KERN_MSGBUF:
   1374 		break;
   1375 	default:
   1376 		return (EOPNOTSUPP);
   1377 	}
   1378 
   1379 	if (newp != NULL)
   1380 		return (EPERM);
   1381 
   1382 	if (oldp == NULL) {
   1383 		/* always return full buffer size */
   1384 		*oldlenp = msgbufp->msg_bufs;
   1385 		return (0);
   1386 	}
   1387 
   1388 	sysctl_unlock();
   1389 
   1390 	/*
   1391 	 * First, copy from the write pointer to the end of
   1392 	 * message buffer.
   1393 	 */
   1394 	error = 0;
   1395 	mutex_spin_enter(&log_lock);
   1396 	maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
   1397 	beg = msgbufp->msg_bufx;
   1398 	end = msgbufp->msg_bufs;
   1399 	mutex_spin_exit(&log_lock);
   1400 
   1401 	while (maxlen > 0) {
   1402 		len = MIN(end - beg, maxlen);
   1403 		if (len == 0)
   1404 			break;
   1405 		/* XXX unlocked, but hardly matters. */
   1406 		error = dcopyout(l, &msgbufp->msg_bufc[beg], where, len);
   1407 		if (error)
   1408 			break;
   1409 		where += len;
   1410 		maxlen -= len;
   1411 
   1412 		/*
   1413 		 * ... then, copy from the beginning of message buffer to
   1414 		 * the write pointer.
   1415 		 */
   1416 		beg = 0;
   1417 		end = msgbufp->msg_bufx;
   1418 	}
   1419 
   1420 	sysctl_relock();
   1421 	return (error);
   1422 }
   1423 
   1424 /*
   1425  * sysctl helper routine for kern.defcorename. In the case of a new
   1426  * string being assigned, check that it's not a zero-length string.
   1427  * (XXX the check in -current doesn't work, but do we really care?)
   1428  */
   1429 static int
   1430 sysctl_kern_defcorename(SYSCTLFN_ARGS)
   1431 {
   1432 	int error;
   1433 	char *newcorename;
   1434 	struct sysctlnode node;
   1435 
   1436 	newcorename = PNBUF_GET();
   1437 	node = *rnode;
   1438 	node.sysctl_data = &newcorename[0];
   1439 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
   1440 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1441 	if (error || newp == NULL) {
   1442 		goto done;
   1443 	}
   1444 
   1445 	/*
   1446 	 * when sysctl_lookup() deals with a string, it's guaranteed
   1447 	 * to come back nul terminated. So there.  :)
   1448 	 */
   1449 	if (strlen(newcorename) == 0) {
   1450 		error = EINVAL;
   1451 	} else {
   1452 		memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
   1453 		error = 0;
   1454 	}
   1455 done:
   1456 	PNBUF_PUT(newcorename);
   1457 	return error;
   1458 }
   1459 
   1460 /*
   1461  * sysctl helper routine for kern.cp_time node. Adds up cpu time
   1462  * across all cpus.
   1463  */
   1464 static int
   1465 sysctl_kern_cptime(SYSCTLFN_ARGS)
   1466 {
   1467 	struct sysctlnode node = *rnode;
   1468 	uint64_t *cp_time = NULL;
   1469 	int error, n = ncpu, i;
   1470 	struct cpu_info *ci;
   1471 	CPU_INFO_ITERATOR cii;
   1472 
   1473 	/*
   1474 	 * if you specifically pass a buffer that is the size of the
   1475 	 * sum, or if you are probing for the size, you get the "sum"
   1476 	 * of cp_time (and the size thereof) across all processors.
   1477 	 *
   1478 	 * alternately, you can pass an additional mib number and get
   1479 	 * cp_time for that particular processor.
   1480 	 */
   1481 	switch (namelen) {
   1482 	case 0:
   1483 		if (*oldlenp == sizeof(uint64_t) * CPUSTATES || oldp == NULL) {
   1484 			node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
   1485 			n = -1; /* SUM */
   1486 		}
   1487 		else {
   1488 			node.sysctl_size = n * sizeof(uint64_t) * CPUSTATES;
   1489 			n = -2; /* ALL */
   1490 		}
   1491 		break;
   1492 	case 1:
   1493 		if (name[0] < 0 || name[0] >= n)
   1494 			return (ENOENT); /* ENOSUCHPROCESSOR */
   1495 		node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
   1496 		n = name[0];
   1497 		/*
   1498 		 * adjust these so that sysctl_lookup() will be happy
   1499 		 */
   1500 		name++;
   1501 		namelen--;
   1502 		break;
   1503 	default:
   1504 		return (EINVAL);
   1505 	}
   1506 
   1507 	cp_time = kmem_alloc(node.sysctl_size, KM_SLEEP);
   1508 	if (cp_time == NULL)
   1509 		return (ENOMEM);
   1510 	node.sysctl_data = cp_time;
   1511 	memset(cp_time, 0, node.sysctl_size);
   1512 
   1513 	for (CPU_INFO_FOREACH(cii, ci)) {
   1514 		if (n <= 0) {
   1515 			for (i = 0; i < CPUSTATES; i++) {
   1516 				cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
   1517 			}
   1518 		}
   1519 		/*
   1520 		 * if a specific processor was requested and we just
   1521 		 * did it, we're done here
   1522 		 */
   1523 		if (n == 0)
   1524 			break;
   1525 		/*
   1526 		 * if doing "all", skip to next cp_time set for next processor
   1527 		 */
   1528 		if (n == -2)
   1529 			cp_time += CPUSTATES;
   1530 		/*
   1531 		 * if we're doing a specific processor, we're one
   1532 		 * processor closer
   1533 		 */
   1534 		if (n > 0)
   1535 			n--;
   1536 	}
   1537 
   1538 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1539 	kmem_free(node.sysctl_data, node.sysctl_size);
   1540 	return (error);
   1541 }
   1542 
   1543 #if NPTY > 0
   1544 /*
   1545  * sysctl helper routine for kern.maxptys. Ensures that any new value
   1546  * is acceptable to the pty subsystem.
   1547  */
   1548 static int
   1549 sysctl_kern_maxptys(SYSCTLFN_ARGS)
   1550 {
   1551 	int pty_maxptys(int, int);		/* defined in kern/tty_pty.c */
   1552 	int error, xmax;
   1553 	struct sysctlnode node;
   1554 
   1555 	/* get current value of maxptys */
   1556 	xmax = pty_maxptys(0, 0);
   1557 
   1558 	node = *rnode;
   1559 	node.sysctl_data = &xmax;
   1560 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1561 	if (error || newp == NULL)
   1562 		return (error);
   1563 
   1564 	if (xmax != pty_maxptys(xmax, 1))
   1565 		return (EINVAL);
   1566 
   1567 	return (0);
   1568 }
   1569 #endif /* NPTY > 0 */
   1570 
   1571 /*
   1572  * sysctl helper routine for kern.sbmax. Basically just ensures that
   1573  * any new value is not too small.
   1574  */
   1575 static int
   1576 sysctl_kern_sbmax(SYSCTLFN_ARGS)
   1577 {
   1578 	int error, new_sbmax;
   1579 	struct sysctlnode node;
   1580 
   1581 	new_sbmax = sb_max;
   1582 	node = *rnode;
   1583 	node.sysctl_data = &new_sbmax;
   1584 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1585 	if (error || newp == NULL)
   1586 		return (error);
   1587 
   1588 	KERNEL_LOCK(1, NULL);
   1589 	error = sb_max_set(new_sbmax);
   1590 	KERNEL_UNLOCK_ONE(NULL);
   1591 
   1592 	return (error);
   1593 }
   1594 
   1595 /*
   1596  * sysctl helper routine for kern.urandom node. Picks a random number
   1597  * for you.
   1598  */
   1599 static int
   1600 sysctl_kern_urnd(SYSCTLFN_ARGS)
   1601 {
   1602 #if NRND > 0
   1603 	int v, rv;
   1604 
   1605 	KERNEL_LOCK(1, NULL);
   1606 	rv = rnd_extract_data(&v, sizeof(v), RND_EXTRACT_ANY);
   1607 	KERNEL_UNLOCK_ONE(NULL);
   1608 	if (rv == sizeof(v)) {
   1609 		struct sysctlnode node = *rnode;
   1610 		node.sysctl_data = &v;
   1611 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1612 	}
   1613 	else
   1614 		return (EIO);	/*XXX*/
   1615 #else
   1616 	return (EOPNOTSUPP);
   1617 #endif
   1618 }
   1619 
   1620 /*
   1621  * sysctl helper routine for kern.arandom node. Picks a random number
   1622  * for you.
   1623  */
   1624 static int
   1625 sysctl_kern_arnd(SYSCTLFN_ARGS)
   1626 {
   1627 #if NRND > 0
   1628 	int error;
   1629 	void *v;
   1630 	struct sysctlnode node = *rnode;
   1631 
   1632 	if (*oldlenp == 0)
   1633 		return 0;
   1634 	if (*oldlenp > 8192)
   1635 		return E2BIG;
   1636 
   1637 	v = kmem_alloc(*oldlenp, KM_SLEEP);
   1638 	arc4randbytes(v, *oldlenp);
   1639 	node.sysctl_data = v;
   1640 	node.sysctl_size = *oldlenp;
   1641 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1642 	kmem_free(v, *oldlenp);
   1643 	return error;
   1644 #else
   1645 	return (EOPNOTSUPP);
   1646 #endif
   1647 }
   1648 /*
   1649  * sysctl helper routine to do kern.lwp.* work.
   1650  */
   1651 static int
   1652 sysctl_kern_lwp(SYSCTLFN_ARGS)
   1653 {
   1654 	struct kinfo_lwp klwp;
   1655 	struct proc *p;
   1656 	struct lwp *l2, *l3;
   1657 	char *where, *dp;
   1658 	int pid, elem_size, elem_count;
   1659 	int buflen, needed, error;
   1660 	bool gotit;
   1661 
   1662 	if (namelen == 1 && name[0] == CTL_QUERY)
   1663 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1664 
   1665 	dp = where = oldp;
   1666 	buflen = where != NULL ? *oldlenp : 0;
   1667 	error = needed = 0;
   1668 
   1669 	if (newp != NULL || namelen != 3)
   1670 		return (EINVAL);
   1671 	pid = name[0];
   1672 	elem_size = name[1];
   1673 	elem_count = name[2];
   1674 
   1675 	sysctl_unlock();
   1676 	if (pid == -1) {
   1677 		mutex_enter(proc_lock);
   1678 		LIST_FOREACH(p, &allproc, p_list) {
   1679 			/* Grab a hold on the process. */
   1680 			if (!rw_tryenter(&p->p_reflock, RW_READER)) {
   1681 				continue;
   1682 			}
   1683 			mutex_exit(proc_lock);
   1684 
   1685 			mutex_enter(p->p_lock);
   1686 			LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1687 				if (buflen >= elem_size && elem_count > 0) {
   1688 					lwp_lock(l2);
   1689 					fill_lwp(l2, &klwp);
   1690 					lwp_unlock(l2);
   1691 					mutex_exit(p->p_lock);
   1692 
   1693 					/*
   1694 					 * Copy out elem_size, but not
   1695 					 * larger than the size of a
   1696 					 * struct kinfo_proc2.
   1697 					 */
   1698 					error = dcopyout(l, &klwp, dp,
   1699 					    min(sizeof(klwp), elem_size));
   1700 					if (error) {
   1701 						rw_exit(&p->p_reflock);
   1702 						goto cleanup;
   1703 					}
   1704 					mutex_enter(p->p_lock);
   1705 					LIST_FOREACH(l3, &p->p_lwps,
   1706 					    l_sibling) {
   1707 						if (l2 == l3)
   1708 							break;
   1709 					}
   1710 					if (l3 == NULL) {
   1711 						mutex_exit(p->p_lock);
   1712 						rw_exit(&p->p_reflock);
   1713 						error = EAGAIN;
   1714 						goto cleanup;
   1715 					}
   1716 					dp += elem_size;
   1717 					buflen -= elem_size;
   1718 					elem_count--;
   1719 				}
   1720 				needed += elem_size;
   1721 			}
   1722 			mutex_exit(p->p_lock);
   1723 
   1724 			/* Drop reference to process. */
   1725 			mutex_enter(proc_lock);
   1726 			rw_exit(&p->p_reflock);
   1727 		}
   1728 		mutex_exit(proc_lock);
   1729 	} else {
   1730 		mutex_enter(proc_lock);
   1731 		p = p_find(pid, PFIND_LOCKED);
   1732 		if (p == NULL) {
   1733 			error = ESRCH;
   1734 			mutex_exit(proc_lock);
   1735 			goto cleanup;
   1736 		}
   1737 		/* Grab a hold on the process. */
   1738 		gotit = rw_tryenter(&p->p_reflock, RW_READER);
   1739 		mutex_exit(proc_lock);
   1740 		if (!gotit) {
   1741 			error = ESRCH;
   1742 			goto cleanup;
   1743 		}
   1744 
   1745 		mutex_enter(p->p_lock);
   1746 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1747 			if (buflen >= elem_size && elem_count > 0) {
   1748 				lwp_lock(l2);
   1749 				fill_lwp(l2, &klwp);
   1750 				lwp_unlock(l2);
   1751 				mutex_exit(p->p_lock);
   1752 				/*
   1753 				 * Copy out elem_size, but not larger than
   1754 				 * the size of a struct kinfo_proc2.
   1755 				 */
   1756 				error = dcopyout(l, &klwp, dp,
   1757 				    min(sizeof(klwp), elem_size));
   1758 				if (error) {
   1759 					rw_exit(&p->p_reflock);
   1760 					goto cleanup;
   1761 				}
   1762 				mutex_enter(p->p_lock);
   1763 				LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
   1764 					if (l2 == l3)
   1765 						break;
   1766 				}
   1767 				if (l3 == NULL) {
   1768 					mutex_exit(p->p_lock);
   1769 					rw_exit(&p->p_reflock);
   1770 					error = EAGAIN;
   1771 					goto cleanup;
   1772 				}
   1773 				dp += elem_size;
   1774 				buflen -= elem_size;
   1775 				elem_count--;
   1776 			}
   1777 			needed += elem_size;
   1778 		}
   1779 		mutex_exit(p->p_lock);
   1780 
   1781 		/* Drop reference to process. */
   1782 		rw_exit(&p->p_reflock);
   1783 	}
   1784 
   1785 	if (where != NULL) {
   1786 		*oldlenp = dp - where;
   1787 		if (needed > *oldlenp) {
   1788 			sysctl_relock();
   1789 			return (ENOMEM);
   1790 		}
   1791 	} else {
   1792 		needed += KERN_LWPSLOP;
   1793 		*oldlenp = needed;
   1794 	}
   1795 	error = 0;
   1796  cleanup:
   1797 	sysctl_relock();
   1798 	return (error);
   1799 }
   1800 
   1801 /*
   1802  * sysctl helper routine for kern.forkfsleep node. Ensures that the
   1803  * given value is not too large or two small, and is at least one
   1804  * timer tick if not zero.
   1805  */
   1806 static int
   1807 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
   1808 {
   1809 	/* userland sees value in ms, internally is in ticks */
   1810 	extern int forkfsleep;		/* defined in kern/kern_fork.c */
   1811 	int error, timo, lsleep;
   1812 	struct sysctlnode node;
   1813 
   1814 	lsleep = forkfsleep * 1000 / hz;
   1815 	node = *rnode;
   1816 	node.sysctl_data = &lsleep;
   1817 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1818 	if (error || newp == NULL)
   1819 		return (error);
   1820 
   1821 	/* refuse negative values, and overly 'long time' */
   1822 	if (lsleep < 0 || lsleep > MAXSLP * 1000)
   1823 		return (EINVAL);
   1824 
   1825 	timo = mstohz(lsleep);
   1826 
   1827 	/* if the interval is >0 ms && <1 tick, use 1 tick */
   1828 	if (lsleep != 0 && timo == 0)
   1829 		forkfsleep = 1;
   1830 	else
   1831 		forkfsleep = timo;
   1832 
   1833 	return (0);
   1834 }
   1835 
   1836 /*
   1837  * sysctl helper routine for kern.root_partition
   1838  */
   1839 static int
   1840 sysctl_kern_root_partition(SYSCTLFN_ARGS)
   1841 {
   1842 	int rootpart = DISKPART(rootdev);
   1843 	struct sysctlnode node = *rnode;
   1844 
   1845 	node.sysctl_data = &rootpart;
   1846 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1847 }
   1848 
   1849 /*
   1850  * sysctl helper function for kern.drivers
   1851  */
   1852 static int
   1853 sysctl_kern_drivers(SYSCTLFN_ARGS)
   1854 {
   1855 	int error;
   1856 	size_t buflen;
   1857 	struct kinfo_drivers kd;
   1858 	char *start, *where;
   1859 	const char *dname;
   1860 	int i;
   1861 	extern struct devsw_conv *devsw_conv;
   1862 	extern int max_devsw_convs;
   1863 	extern kmutex_t devsw_lock;
   1864 
   1865 	if (newp != NULL || namelen != 0)
   1866 		return (EINVAL);
   1867 
   1868 	start = where = oldp;
   1869 	buflen = *oldlenp;
   1870 	if (where == NULL) {
   1871 		*oldlenp = max_devsw_convs * sizeof kd;
   1872 		return 0;
   1873 	}
   1874 
   1875 	/*
   1876 	 * An array of kinfo_drivers structures
   1877 	 */
   1878 	error = 0;
   1879 	sysctl_unlock();
   1880 	mutex_enter(&devsw_lock);
   1881 	for (i = 0; i < max_devsw_convs; i++) {
   1882 		dname = devsw_conv[i].d_name;
   1883 		if (dname == NULL)
   1884 			continue;
   1885 		if (buflen < sizeof kd) {
   1886 			error = ENOMEM;
   1887 			break;
   1888 		}
   1889 		memset(&kd, 0, sizeof(kd));
   1890 		kd.d_bmajor = devsw_conv[i].d_bmajor;
   1891 		kd.d_cmajor = devsw_conv[i].d_cmajor;
   1892 		strlcpy(kd.d_name, dname, sizeof kd.d_name);
   1893 		mutex_exit(&devsw_lock);
   1894 		error = dcopyout(l, &kd, where, sizeof kd);
   1895 		mutex_enter(&devsw_lock);
   1896 		if (error != 0)
   1897 			break;
   1898 		buflen -= sizeof kd;
   1899 		where += sizeof kd;
   1900 	}
   1901 	mutex_exit(&devsw_lock);
   1902 	sysctl_relock();
   1903 	*oldlenp = where - start;
   1904 	return error;
   1905 }
   1906 
   1907 /*
   1908  * sysctl helper function for kern.file2
   1909  */
   1910 static int
   1911 sysctl_kern_file2(SYSCTLFN_ARGS)
   1912 {
   1913 	struct proc *p;
   1914 	struct file *fp, *tp, *np;
   1915 	struct filedesc *fd;
   1916 	struct kinfo_file kf;
   1917 	char *dp;
   1918 	u_int i, op;
   1919 	size_t len, needed, elem_size, out_size;
   1920 	int error, arg, elem_count;
   1921 	fdfile_t *ff;
   1922 
   1923 	if (namelen == 1 && name[0] == CTL_QUERY)
   1924 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1925 
   1926 	if (namelen != 4)
   1927 		return (EINVAL);
   1928 
   1929 	error = 0;
   1930 	dp = oldp;
   1931 	len = (oldp != NULL) ? *oldlenp : 0;
   1932 	op = name[0];
   1933 	arg = name[1];
   1934 	elem_size = name[2];
   1935 	elem_count = name[3];
   1936 	out_size = MIN(sizeof(kf), elem_size);
   1937 	needed = 0;
   1938 
   1939 	if (elem_size < 1 || elem_count < 0)
   1940 		return (EINVAL);
   1941 
   1942 	switch (op) {
   1943 	case KERN_FILE_BYFILE:
   1944 		/*
   1945 		 * doesn't use arg so it must be zero
   1946 		 */
   1947 		if (arg != 0)
   1948 			return (EINVAL);
   1949 		sysctl_unlock();
   1950 		/*
   1951 		 * allocate dummy file descriptor to make position in list
   1952 		 */
   1953 		if ((tp = fgetdummy()) == NULL) {
   1954 		 	sysctl_relock();
   1955 			return ENOMEM;
   1956 		}
   1957 		mutex_enter(&filelist_lock);
   1958 		for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) {
   1959 			np = LIST_NEXT(fp, f_list);
   1960 			mutex_enter(&fp->f_lock);
   1961 			if (fp->f_count == 0) {
   1962 				mutex_exit(&fp->f_lock);
   1963 				continue;
   1964 			}
   1965 			/*
   1966 			 * XXX Need to prevent that from being an alternative
   1967 			 * XXX way for getting process information.
   1968 			 */
   1969 			if (kauth_authorize_generic(l->l_cred,
   1970 			    KAUTH_GENERIC_CANSEE, fp->f_cred) != 0) {
   1971 				mutex_exit(&fp->f_lock);
   1972 				continue;
   1973 			}
   1974 			if (len >= elem_size && elem_count > 0) {
   1975 				fill_file(&kf, fp, NULL, 0, 0);
   1976 				LIST_INSERT_AFTER(fp, tp, f_list);
   1977 				mutex_exit(&fp->f_lock);
   1978 				mutex_exit(&filelist_lock);
   1979 				error = dcopyout(l, &kf, dp, out_size);
   1980 				mutex_enter(&filelist_lock);
   1981 				np = LIST_NEXT(tp, f_list);
   1982 				LIST_REMOVE(tp, f_list);
   1983 				if (error) {
   1984 					break;
   1985 				}
   1986 				dp += elem_size;
   1987 				len -= elem_size;
   1988 			} else {
   1989 				mutex_exit(&fp->f_lock);
   1990 			}
   1991 			if (elem_count > 0) {
   1992 				needed += elem_size;
   1993 				if (elem_count != INT_MAX)
   1994 					elem_count--;
   1995 			}
   1996 		}
   1997 		mutex_exit(&filelist_lock);
   1998 		fputdummy(tp);
   1999 		sysctl_relock();
   2000 		break;
   2001 	case KERN_FILE_BYPID:
   2002 		if (arg < -1)
   2003 			/* -1 means all processes */
   2004 			return (EINVAL);
   2005 		sysctl_unlock();
   2006 		mutex_enter(proc_lock);
   2007 		LIST_FOREACH(p, &allproc, p_list) {
   2008 			if (p->p_stat == SIDL) {
   2009 				/* skip embryonic processes */
   2010 				continue;
   2011 			}
   2012 			if (arg > 0 && p->p_pid != arg) {
   2013 				/* pick only the one we want */
   2014 				/* XXX want 0 to mean "kernel files" */
   2015 				continue;
   2016 			}
   2017 			mutex_enter(p->p_lock);
   2018 			error = kauth_authorize_process(l->l_cred,
   2019 			    KAUTH_PROCESS_CANSEE, p,
   2020 			    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_OPENFILES),
   2021 			    NULL, NULL);
   2022 			mutex_exit(p->p_lock);
   2023 			if (error != 0) {
   2024 				continue;
   2025 			}
   2026 
   2027 			/*
   2028 			 * Grab a hold on the process.
   2029 			 */
   2030 			if (!rw_tryenter(&p->p_reflock, RW_READER)) {
   2031 				continue;
   2032 			}
   2033 			mutex_exit(proc_lock);
   2034 
   2035 			/* XXX Do we need to check permission per file? */
   2036 			fd = p->p_fd;
   2037 			mutex_enter(&fd->fd_lock);
   2038 			for (i = 0; i < fd->fd_nfiles; i++) {
   2039 				if ((ff = fd->fd_ofiles[i]) == NULL) {
   2040 					continue;
   2041 				}
   2042 				mutex_enter(&ff->ff_lock);
   2043 				if ((fp = ff->ff_file) == NULL) {
   2044 					mutex_exit(&ff->ff_lock);
   2045 					continue;
   2046 				}
   2047 				if (len >= elem_size && elem_count > 0) {
   2048 					mutex_enter(&fp->f_lock);
   2049 					fill_file(&kf, fp, ff, i, p->p_pid);
   2050 					mutex_exit(&fp->f_lock);
   2051 					mutex_exit(&ff->ff_lock);
   2052 					mutex_exit(&fd->fd_lock);
   2053 					error = dcopyout(l, &kf, dp, out_size);
   2054 					mutex_enter(&fd->fd_lock);
   2055 					if (error)
   2056 						break;
   2057 					dp += elem_size;
   2058 					len -= elem_size;
   2059 				} else {
   2060 					mutex_exit(&ff->ff_lock);
   2061 				}
   2062 				if (elem_count > 0) {
   2063 					needed += elem_size;
   2064 					if (elem_count != INT_MAX)
   2065 						elem_count--;
   2066 				}
   2067 			}
   2068 			mutex_exit(&fd->fd_lock);
   2069 
   2070 			/*
   2071 			 * Release reference to process.
   2072 			 */
   2073 			mutex_enter(proc_lock);
   2074 			rw_exit(&p->p_reflock);
   2075 		}
   2076 		mutex_exit(proc_lock);
   2077 		sysctl_relock();
   2078 		break;
   2079 	default:
   2080 		return (EINVAL);
   2081 	}
   2082 
   2083 	if (oldp == NULL)
   2084 		needed += KERN_FILESLOP * elem_size;
   2085 	*oldlenp = needed;
   2086 
   2087 	return (error);
   2088 }
   2089 
   2090 static void
   2091 fill_file(struct kinfo_file *kp, const file_t *fp, const fdfile_t *ff,
   2092 	  int i, pid_t pid)
   2093 {
   2094 
   2095 	memset(kp, 0, sizeof(*kp));
   2096 
   2097 	kp->ki_fileaddr =	PTRTOUINT64(fp);
   2098 	kp->ki_flag =		fp->f_flag;
   2099 	kp->ki_iflags =		fp->f_iflags;
   2100 	kp->ki_ftype =		fp->f_type;
   2101 	kp->ki_count =		fp->f_count;
   2102 	kp->ki_msgcount =	fp->f_msgcount;
   2103 	kp->ki_fucred =		PTRTOUINT64(fp->f_cred);
   2104 	kp->ki_fuid =		kauth_cred_geteuid(fp->f_cred);
   2105 	kp->ki_fgid =		kauth_cred_getegid(fp->f_cred);
   2106 	kp->ki_fops =		PTRTOUINT64(fp->f_ops);
   2107 	kp->ki_foffset =	fp->f_offset;
   2108 	kp->ki_fdata =		PTRTOUINT64(fp->f_data);
   2109 
   2110 	/* vnode information to glue this file to something */
   2111 	if (fp->f_type == DTYPE_VNODE) {
   2112 		struct vnode *vp = (struct vnode *)fp->f_data;
   2113 
   2114 		kp->ki_vun =	PTRTOUINT64(vp->v_un.vu_socket);
   2115 		kp->ki_vsize =	vp->v_size;
   2116 		kp->ki_vtype =	vp->v_type;
   2117 		kp->ki_vtag =	vp->v_tag;
   2118 		kp->ki_vdata =	PTRTOUINT64(vp->v_data);
   2119 	}
   2120 
   2121 	/* process information when retrieved via KERN_FILE_BYPID */
   2122 	if (ff != NULL) {
   2123 		kp->ki_pid =		pid;
   2124 		kp->ki_fd =		i;
   2125 		kp->ki_ofileflags =	ff->ff_exclose;
   2126 		kp->ki_usecount =	ff->ff_refcnt;
   2127 	}
   2128 }
   2129 
   2130 static int
   2131 sysctl_doeproc(SYSCTLFN_ARGS)
   2132 {
   2133 	struct eproc *eproc;
   2134 	struct kinfo_proc2 *kproc2;
   2135 	struct kinfo_proc *dp;
   2136 	struct proc *p, *next, *marker;
   2137 	char *where, *dp2;
   2138 	int type, op, arg, error;
   2139 	u_int elem_size, elem_count;
   2140 	size_t buflen, needed;
   2141 	bool match, zombie, mmmbrains;
   2142 
   2143 	if (namelen == 1 && name[0] == CTL_QUERY)
   2144 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   2145 
   2146 	dp = oldp;
   2147 	dp2 = where = oldp;
   2148 	buflen = where != NULL ? *oldlenp : 0;
   2149 	error = 0;
   2150 	needed = 0;
   2151 	type = rnode->sysctl_num;
   2152 
   2153 	if (type == KERN_PROC) {
   2154 		if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
   2155 			return (EINVAL);
   2156 		op = name[0];
   2157 		if (op != KERN_PROC_ALL)
   2158 			arg = name[1];
   2159 		else
   2160 			arg = 0;		/* Quell compiler warning */
   2161 		elem_size = elem_count = 0;	/* Ditto */
   2162 	} else {
   2163 		if (namelen != 4)
   2164 			return (EINVAL);
   2165 		op = name[0];
   2166 		arg = name[1];
   2167 		elem_size = name[2];
   2168 		elem_count = name[3];
   2169 	}
   2170 
   2171 	sysctl_unlock();
   2172 
   2173 	if (type == KERN_PROC) {
   2174 		eproc = kmem_alloc(sizeof(*eproc), KM_SLEEP);
   2175 		kproc2 = NULL;
   2176 	} else {
   2177 		eproc = NULL;
   2178 		kproc2 = kmem_alloc(sizeof(*kproc2), KM_SLEEP);
   2179 	}
   2180 	marker = kmem_alloc(sizeof(*marker), KM_SLEEP);
   2181 
   2182 	mutex_enter(proc_lock);
   2183 	mmmbrains = false;
   2184 	for (p = LIST_FIRST(&allproc);; p = next) {
   2185 		if (p == NULL) {
   2186 			if (!mmmbrains) {
   2187 				p = LIST_FIRST(&zombproc);
   2188 				mmmbrains = true;
   2189 			}
   2190 			if (p == NULL)
   2191 				break;
   2192 		}
   2193 		next = LIST_NEXT(p, p_list);
   2194 
   2195 		/*
   2196 		 * Skip embryonic processes.
   2197 		 */
   2198 		if (p->p_stat == SIDL)
   2199 			continue;
   2200 
   2201 		mutex_enter(p->p_lock);
   2202 		error = kauth_authorize_process(l->l_cred,
   2203 		    KAUTH_PROCESS_CANSEE, p,
   2204 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
   2205 		if (error != 0) {
   2206 			mutex_exit(p->p_lock);
   2207 			continue;
   2208 		}
   2209 
   2210 		/*
   2211 		 * TODO - make more efficient (see notes below).
   2212 		 * do by session.
   2213 		 */
   2214 		switch (op) {
   2215 		case KERN_PROC_PID:
   2216 			/* could do this with just a lookup */
   2217 			match = (p->p_pid == (pid_t)arg);
   2218 			break;
   2219 
   2220 		case KERN_PROC_PGRP:
   2221 			/* could do this by traversing pgrp */
   2222 			match = (p->p_pgrp->pg_id == (pid_t)arg);
   2223 			break;
   2224 
   2225 		case KERN_PROC_SESSION:
   2226 			match = (p->p_session->s_sid == (pid_t)arg);
   2227 			break;
   2228 
   2229 		case KERN_PROC_TTY:
   2230 			match = true;
   2231 			if (arg == (int) KERN_PROC_TTY_REVOKE) {
   2232 				if ((p->p_lflag & PL_CONTROLT) == 0 ||
   2233 				    p->p_session->s_ttyp == NULL ||
   2234 				    p->p_session->s_ttyvp != NULL) {
   2235 				    	match = false;
   2236 				}
   2237 			} else if ((p->p_lflag & PL_CONTROLT) == 0 ||
   2238 			    p->p_session->s_ttyp == NULL) {
   2239 				if ((dev_t)arg != KERN_PROC_TTY_NODEV) {
   2240 					match = false;
   2241 				}
   2242 			} else if (p->p_session->s_ttyp->t_dev != (dev_t)arg) {
   2243 				match = false;
   2244 			}
   2245 			break;
   2246 
   2247 		case KERN_PROC_UID:
   2248 			match = (kauth_cred_geteuid(p->p_cred) == (uid_t)arg);
   2249 			break;
   2250 
   2251 		case KERN_PROC_RUID:
   2252 			match = (kauth_cred_getuid(p->p_cred) == (uid_t)arg);
   2253 			break;
   2254 
   2255 		case KERN_PROC_GID:
   2256 			match = (kauth_cred_getegid(p->p_cred) == (uid_t)arg);
   2257 			break;
   2258 
   2259 		case KERN_PROC_RGID:
   2260 			match = (kauth_cred_getgid(p->p_cred) == (uid_t)arg);
   2261 			break;
   2262 
   2263 		case KERN_PROC_ALL:
   2264 			match = true;
   2265 			/* allow everything */
   2266 			break;
   2267 
   2268 		default:
   2269 			error = EINVAL;
   2270 			mutex_exit(p->p_lock);
   2271 			goto cleanup;
   2272 		}
   2273 		if (!match) {
   2274 			mutex_exit(p->p_lock);
   2275 			continue;
   2276 		}
   2277 
   2278 		/*
   2279 		 * Grab a hold on the process.
   2280 		 */
   2281 		if (mmmbrains) {
   2282 			zombie = true;
   2283 		} else {
   2284 			zombie = !rw_tryenter(&p->p_reflock, RW_READER);
   2285 		}
   2286 		if (zombie) {
   2287 			LIST_INSERT_AFTER(p, marker, p_list);
   2288 		}
   2289 
   2290 		if (type == KERN_PROC) {
   2291 			if (buflen >= sizeof(struct kinfo_proc)) {
   2292 				fill_eproc(p, eproc, zombie);
   2293 				mutex_exit(p->p_lock);
   2294 				mutex_exit(proc_lock);
   2295 				error = dcopyout(l, p, &dp->kp_proc,
   2296 				    sizeof(struct proc));
   2297 				mutex_enter(proc_lock);
   2298 				if (error) {
   2299 					goto bah;
   2300 				}
   2301 				error = dcopyout(l, eproc, &dp->kp_eproc,
   2302 				    sizeof(*eproc));
   2303 				if (error) {
   2304 					goto bah;
   2305 				}
   2306 				dp++;
   2307 				buflen -= sizeof(struct kinfo_proc);
   2308 			} else {
   2309 				mutex_exit(p->p_lock);
   2310 			}
   2311 			needed += sizeof(struct kinfo_proc);
   2312 		} else { /* KERN_PROC2 */
   2313 			if (buflen >= elem_size && elem_count > 0) {
   2314 				fill_kproc2(p, kproc2, zombie);
   2315 				mutex_exit(p->p_lock);
   2316 				mutex_exit(proc_lock);
   2317 				/*
   2318 				 * Copy out elem_size, but not larger than
   2319 				 * the size of a struct kinfo_proc2.
   2320 				 */
   2321 				error = dcopyout(l, kproc2, dp2,
   2322 				    min(sizeof(*kproc2), elem_size));
   2323 				mutex_enter(proc_lock);
   2324 				if (error) {
   2325 					goto bah;
   2326 				}
   2327 				dp2 += elem_size;
   2328 				buflen -= elem_size;
   2329 				elem_count--;
   2330 			} else {
   2331 				mutex_exit(p->p_lock);
   2332 			}
   2333 			needed += elem_size;
   2334 		}
   2335 
   2336 		/*
   2337 		 * Release reference to process.
   2338 		 */
   2339 	 	if (zombie) {
   2340 			next = LIST_NEXT(marker, p_list);
   2341  			LIST_REMOVE(marker, p_list);
   2342 		} else {
   2343 			rw_exit(&p->p_reflock);
   2344 		}
   2345 	}
   2346 	mutex_exit(proc_lock);
   2347 
   2348 	if (where != NULL) {
   2349 		if (type == KERN_PROC)
   2350 			*oldlenp = (char *)dp - where;
   2351 		else
   2352 			*oldlenp = dp2 - where;
   2353 		if (needed > *oldlenp) {
   2354 			error = ENOMEM;
   2355 			goto out;
   2356 		}
   2357 	} else {
   2358 		needed += KERN_PROCSLOP;
   2359 		*oldlenp = needed;
   2360 	}
   2361 	if (kproc2)
   2362 		kmem_free(kproc2, sizeof(*kproc2));
   2363 	if (eproc)
   2364 		kmem_free(eproc, sizeof(*eproc));
   2365 	if (marker)
   2366 		kmem_free(marker, sizeof(*marker));
   2367 	sysctl_relock();
   2368 	return 0;
   2369  bah:
   2370  	if (zombie)
   2371  		LIST_REMOVE(marker, p_list);
   2372 	else
   2373 		rw_exit(&p->p_reflock);
   2374  cleanup:
   2375 	mutex_exit(proc_lock);
   2376  out:
   2377 	if (kproc2)
   2378 		kmem_free(kproc2, sizeof(*kproc2));
   2379 	if (eproc)
   2380 		kmem_free(eproc, sizeof(*eproc));
   2381 	if (marker)
   2382 		kmem_free(marker, sizeof(*marker));
   2383 	sysctl_relock();
   2384 	return error;
   2385 }
   2386 
   2387 /*
   2388  * sysctl helper routine for kern.proc_args pseudo-subtree.
   2389  */
   2390 static int
   2391 sysctl_kern_proc_args(SYSCTLFN_ARGS)
   2392 {
   2393 	struct ps_strings pss;
   2394 	struct proc *p;
   2395 	size_t len, i;
   2396 	struct uio auio;
   2397 	struct iovec aiov;
   2398 	pid_t pid;
   2399 	int nargv, type, error, argvlen;
   2400 	char *arg;
   2401 	char **argv = NULL;
   2402 	char *tmp;
   2403 	struct vmspace *vmspace;
   2404 	vaddr_t psstr_addr;
   2405 	vaddr_t offsetn;
   2406 	vaddr_t offsetv;
   2407 
   2408 	if (namelen == 1 && name[0] == CTL_QUERY)
   2409 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   2410 
   2411 	if (newp != NULL || namelen != 2)
   2412 		return (EINVAL);
   2413 	pid = name[0];
   2414 	type = name[1];
   2415 	argv = NULL;
   2416 	argvlen = 0;
   2417 
   2418 	switch (type) {
   2419 	case KERN_PROC_ARGV:
   2420 	case KERN_PROC_NARGV:
   2421 	case KERN_PROC_ENV:
   2422 	case KERN_PROC_NENV:
   2423 		/* ok */
   2424 		break;
   2425 	default:
   2426 		return (EINVAL);
   2427 	}
   2428 
   2429 	sysctl_unlock();
   2430 
   2431 	/* check pid */
   2432 	mutex_enter(proc_lock);
   2433 	if ((p = p_find(pid, PFIND_LOCKED)) == NULL) {
   2434 		error = EINVAL;
   2435 		goto out_locked;
   2436 	}
   2437 	mutex_enter(p->p_lock);
   2438 
   2439 	/* Check permission. */
   2440 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
   2441 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
   2442 		    p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ARGS), NULL, NULL);
   2443 	else if (type == KERN_PROC_ENV || type == KERN_PROC_NENV)
   2444 		error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
   2445 		    p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENV), NULL, NULL);
   2446 	else
   2447 		error = EINVAL; /* XXXGCC */
   2448 	if (error) {
   2449 		mutex_exit(p->p_lock);
   2450 		goto out_locked;
   2451 	}
   2452 
   2453 	if (oldp == NULL) {
   2454 		if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
   2455 			*oldlenp = sizeof (int);
   2456 		else
   2457 			*oldlenp = ARG_MAX;	/* XXX XXX XXX */
   2458 		error = 0;
   2459 		mutex_exit(p->p_lock);
   2460 		goto out_locked;
   2461 	}
   2462 
   2463 	/*
   2464 	 * Zombies don't have a stack, so we can't read their psstrings.
   2465 	 * System processes also don't have a user stack.
   2466 	 */
   2467 	if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) {
   2468 		error = EINVAL;
   2469 		mutex_exit(p->p_lock);
   2470 		goto out_locked;
   2471 	}
   2472 
   2473 	/*
   2474 	 * Lock the process down in memory.
   2475 	 */
   2476 	psstr_addr = (vaddr_t)p->p_psstr;
   2477 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV) {
   2478 		offsetn = p->p_psnargv;
   2479 		offsetv = p->p_psargv;
   2480 	} else {
   2481 		offsetn = p->p_psnenv;
   2482 		offsetv = p->p_psenv;
   2483 	}
   2484 	vmspace = p->p_vmspace;
   2485 	uvmspace_addref(vmspace);
   2486 	mutex_exit(p->p_lock);
   2487 	mutex_exit(proc_lock);
   2488 
   2489 	/*
   2490 	 * Allocate a temporary buffer to hold the arguments.
   2491 	 */
   2492 	arg = kmem_alloc(PAGE_SIZE, KM_SLEEP);
   2493 
   2494 	/*
   2495 	 * Read in the ps_strings structure.
   2496 	 */
   2497 	aiov.iov_base = &pss;
   2498 	aiov.iov_len = sizeof(pss);
   2499 	auio.uio_iov = &aiov;
   2500 	auio.uio_iovcnt = 1;
   2501 	auio.uio_offset = psstr_addr;
   2502 	auio.uio_resid = sizeof(pss);
   2503 	auio.uio_rw = UIO_READ;
   2504 	UIO_SETUP_SYSSPACE(&auio);
   2505 	error = uvm_io(&vmspace->vm_map, &auio);
   2506 	if (error)
   2507 		goto done;
   2508 
   2509 	memcpy(&nargv, (char *)&pss + offsetn, sizeof(nargv));
   2510 	if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
   2511 		error = dcopyout(l, &nargv, oldp, sizeof(nargv));
   2512 		*oldlenp = sizeof(nargv);
   2513 		goto done;
   2514 	}
   2515 	/*
   2516 	 * Now read the address of the argument vector.
   2517 	 */
   2518 	switch (type) {
   2519 	case KERN_PROC_ARGV:
   2520 		/* FALLTHROUGH */
   2521 	case KERN_PROC_ENV:
   2522 		memcpy(&tmp, (char *)&pss + offsetv, sizeof(tmp));
   2523 		break;
   2524 	default:
   2525 		error = EINVAL;
   2526 		goto done;
   2527 	}
   2528 
   2529 #ifdef COMPAT_NETBSD32
   2530 	if (p->p_flag & PK_32)
   2531 		len = sizeof(netbsd32_charp) * nargv;
   2532 	else
   2533 #endif
   2534 		len = sizeof(char *) * nargv;
   2535 
   2536 	if ((argvlen = len) != 0)
   2537 		argv = kmem_alloc(len, KM_SLEEP);
   2538 
   2539 	aiov.iov_base = argv;
   2540 	aiov.iov_len = len;
   2541 	auio.uio_iov = &aiov;
   2542 	auio.uio_iovcnt = 1;
   2543 	auio.uio_offset = (off_t)(unsigned long)tmp;
   2544 	auio.uio_resid = len;
   2545 	auio.uio_rw = UIO_READ;
   2546 	UIO_SETUP_SYSSPACE(&auio);
   2547 	error = uvm_io(&vmspace->vm_map, &auio);
   2548 	if (error)
   2549 		goto done;
   2550 
   2551 	/*
   2552 	 * Now copy each string.
   2553 	 */
   2554 	len = 0; /* bytes written to user buffer */
   2555 	for (i = 0; i < nargv; i++) {
   2556 		int finished = 0;
   2557 		vaddr_t base;
   2558 		size_t xlen;
   2559 		int j;
   2560 
   2561 #ifdef COMPAT_NETBSD32
   2562 		if (p->p_flag & PK_32) {
   2563 			netbsd32_charp *argv32;
   2564 
   2565 			argv32 = (netbsd32_charp *)argv;
   2566 			base = (vaddr_t)NETBSD32PTR64(argv32[i]);
   2567 		} else
   2568 #endif
   2569 			base = (vaddr_t)argv[i];
   2570 
   2571 		/*
   2572 		 * The program has messed around with its arguments,
   2573 		 * possibly deleting some, and replacing them with
   2574 		 * NULL's. Treat this as the last argument and not
   2575 		 * a failure.
   2576 		 */
   2577 		if (base == 0)
   2578 			break;
   2579 
   2580 		while (!finished) {
   2581 			xlen = PAGE_SIZE - (base & PAGE_MASK);
   2582 
   2583 			aiov.iov_base = arg;
   2584 			aiov.iov_len = PAGE_SIZE;
   2585 			auio.uio_iov = &aiov;
   2586 			auio.uio_iovcnt = 1;
   2587 			auio.uio_offset = base;
   2588 			auio.uio_resid = xlen;
   2589 			auio.uio_rw = UIO_READ;
   2590 			UIO_SETUP_SYSSPACE(&auio);
   2591 			error = uvm_io(&vmspace->vm_map, &auio);
   2592 			if (error)
   2593 				goto done;
   2594 
   2595 			/* Look for the end of the string */
   2596 			for (j = 0; j < xlen; j++) {
   2597 				if (arg[j] == '\0') {
   2598 					xlen = j + 1;
   2599 					finished = 1;
   2600 					break;
   2601 				}
   2602 			}
   2603 
   2604 			/* Check for user buffer overflow */
   2605 			if (len + xlen > *oldlenp) {
   2606 				finished = 1;
   2607 				if (len > *oldlenp)
   2608 					xlen = 0;
   2609 				else
   2610 					xlen = *oldlenp - len;
   2611 			}
   2612 
   2613 			/* Copyout the page */
   2614 			error = dcopyout(l, arg, (char *)oldp + len, xlen);
   2615 			if (error)
   2616 				goto done;
   2617 
   2618 			len += xlen;
   2619 			base += xlen;
   2620 		}
   2621 	}
   2622 	*oldlenp = len;
   2623 
   2624 done:
   2625 	if (argvlen != 0)
   2626 		kmem_free(argv, argvlen);
   2627 	uvmspace_free(vmspace);
   2628 	kmem_free(arg, PAGE_SIZE);
   2629 	sysctl_relock();
   2630 	return error;
   2631 
   2632 out_locked:
   2633 	mutex_exit(proc_lock);
   2634 	sysctl_relock();
   2635 	return error;
   2636 }
   2637 
   2638 static int
   2639 sysctl_security_setidcore(SYSCTLFN_ARGS)
   2640 {
   2641 	int newsize, error;
   2642 	struct sysctlnode node;
   2643 
   2644 	node = *rnode;
   2645 	node.sysctl_data = &newsize;
   2646 	newsize = *(int *)rnode->sysctl_data;
   2647 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2648 	if (error || newp == NULL)
   2649 		return error;
   2650 
   2651 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
   2652 	    0, NULL, NULL, NULL))
   2653 		return (EPERM);
   2654 
   2655 	*(int *)rnode->sysctl_data = newsize;
   2656 
   2657 	return 0;
   2658 }
   2659 
   2660 static int
   2661 sysctl_security_setidcorename(SYSCTLFN_ARGS)
   2662 {
   2663 	int error;
   2664 	char *newsetidcorename;
   2665 	struct sysctlnode node;
   2666 
   2667 	newsetidcorename = PNBUF_GET();
   2668 	node = *rnode;
   2669 	node.sysctl_data = newsetidcorename;
   2670 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
   2671 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2672 	if (error || newp == NULL) {
   2673 		goto out;
   2674 	}
   2675 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
   2676 	    0, NULL, NULL, NULL)) {
   2677 		error = EPERM;
   2678 		goto out;
   2679 	}
   2680 	if (strlen(newsetidcorename) == 0) {
   2681 		error = EINVAL;
   2682 		goto out;
   2683 	}
   2684 	memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
   2685 out:
   2686 	PNBUF_PUT(newsetidcorename);
   2687 	return error;
   2688 }
   2689 
   2690 /*
   2691  * sysctl helper routine for kern.cp_id node. Maps cpus to their
   2692  * cpuids.
   2693  */
   2694 static int
   2695 sysctl_kern_cpid(SYSCTLFN_ARGS)
   2696 {
   2697 	struct sysctlnode node = *rnode;
   2698 	uint64_t *cp_id = NULL;
   2699 	int error, n = ncpu;
   2700 	struct cpu_info *ci;
   2701 	CPU_INFO_ITERATOR cii;
   2702 
   2703 	/*
   2704 	 * Here you may either retrieve a single cpu id or the whole
   2705 	 * set. The size you get back when probing depends on what
   2706 	 * you ask for.
   2707 	 */
   2708 	switch (namelen) {
   2709 	case 0:
   2710 		node.sysctl_size = n * sizeof(uint64_t);
   2711 		n = -2; /* ALL */
   2712 		break;
   2713 	case 1:
   2714 		if (name[0] < 0 || name[0] >= n)
   2715 			return (ENOENT); /* ENOSUCHPROCESSOR */
   2716 		node.sysctl_size = sizeof(uint64_t);
   2717 		n = name[0];
   2718 		/*
   2719 		 * adjust these so that sysctl_lookup() will be happy
   2720 		 */
   2721 		name++;
   2722 		namelen--;
   2723 		break;
   2724 	default:
   2725 		return (EINVAL);
   2726 	}
   2727 
   2728 	cp_id = kmem_alloc(node.sysctl_size, KM_SLEEP);
   2729 	if (cp_id == NULL)
   2730 		return (ENOMEM);
   2731 	node.sysctl_data = cp_id;
   2732 	memset(cp_id, 0, node.sysctl_size);
   2733 
   2734 	for (CPU_INFO_FOREACH(cii, ci)) {
   2735 		if (n <= 0)
   2736 			cp_id[0] = cpu_index(ci);
   2737 		/*
   2738 		 * if a specific processor was requested and we just
   2739 		 * did it, we're done here
   2740 		 */
   2741 		if (n == 0)
   2742 			break;
   2743 		/*
   2744 		 * if doing "all", skip to next cp_id slot for next processor
   2745 		 */
   2746 		if (n == -2)
   2747 			cp_id++;
   2748 		/*
   2749 		 * if we're doing a specific processor, we're one
   2750 		 * processor closer
   2751 		 */
   2752 		if (n > 0)
   2753 			n--;
   2754 	}
   2755 
   2756 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2757 	kmem_free(node.sysctl_data, node.sysctl_size);
   2758 	return (error);
   2759 }
   2760 
   2761 /*
   2762  * sysctl helper routine for hw.usermem and hw.usermem64. Values are
   2763  * calculate on the fly taking into account integer overflow and the
   2764  * current wired count.
   2765  */
   2766 static int
   2767 sysctl_hw_usermem(SYSCTLFN_ARGS)
   2768 {
   2769 	u_int ui;
   2770 	u_quad_t uq;
   2771 	struct sysctlnode node;
   2772 
   2773 	node = *rnode;
   2774 	switch (rnode->sysctl_num) {
   2775 	    case HW_USERMEM:
   2776 		if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
   2777 			ui = UINT_MAX;
   2778 		else
   2779 			ui *= PAGE_SIZE;
   2780 		node.sysctl_data = &ui;
   2781 		break;
   2782 	case HW_USERMEM64:
   2783 		uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
   2784 		node.sysctl_data = &uq;
   2785 		break;
   2786 	default:
   2787 		return (EINVAL);
   2788 	}
   2789 
   2790 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2791 }
   2792 
   2793 /*
   2794  * sysctl helper routine for kern.cnmagic node. Pulls the old value
   2795  * out, encoded, and stuffs the new value in for decoding.
   2796  */
   2797 static int
   2798 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
   2799 {
   2800 	char magic[CNS_LEN];
   2801 	int error;
   2802 	struct sysctlnode node;
   2803 
   2804 	if (oldp)
   2805 		cn_get_magic(magic, CNS_LEN);
   2806 	node = *rnode;
   2807 	node.sysctl_data = &magic[0];
   2808 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2809 	if (error || newp == NULL)
   2810 		return (error);
   2811 
   2812 	return (cn_set_magic(magic));
   2813 }
   2814 
   2815 /*
   2816  * ********************************************************************
   2817  * section 3: public helper routines that are used for more than one
   2818  * node
   2819  * ********************************************************************
   2820  */
   2821 
   2822 /*
   2823  * sysctl helper routine for the kern.root_device node and some ports'
   2824  * machdep.root_device nodes.
   2825  */
   2826 int
   2827 sysctl_root_device(SYSCTLFN_ARGS)
   2828 {
   2829 	struct sysctlnode node;
   2830 
   2831 	node = *rnode;
   2832 	node.sysctl_data = root_device->dv_xname;
   2833 	node.sysctl_size = strlen(device_xname(root_device)) + 1;
   2834 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2835 }
   2836 
   2837 /*
   2838  * sysctl helper routine for kern.consdev, dependent on the current
   2839  * state of the console. Also used for machdep.console_device on some
   2840  * ports.
   2841  */
   2842 int
   2843 sysctl_consdev(SYSCTLFN_ARGS)
   2844 {
   2845 	dev_t consdev;
   2846 	struct sysctlnode node;
   2847 
   2848 	if (cn_tab != NULL)
   2849 		consdev = cn_tab->cn_dev;
   2850 	else
   2851 		consdev = NODEV;
   2852 	node = *rnode;
   2853 	node.sysctl_data = &consdev;
   2854 	node.sysctl_size = sizeof(consdev);
   2855 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2856 }
   2857 
   2858 /*
   2859  * ********************************************************************
   2860  * section 4: support for some helpers
   2861  * ********************************************************************
   2862  */
   2863 
   2864 /*
   2865  * Fill in a kinfo_proc2 structure for the specified process.
   2866  */
   2867 static void
   2868 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki, bool zombie)
   2869 {
   2870 	struct tty *tp;
   2871 	struct lwp *l, *l2;
   2872 	struct timeval ut, st, rt;
   2873 	sigset_t ss1, ss2;
   2874 	struct rusage ru;
   2875 	struct vmspace *vm;
   2876 	int tmp;
   2877 
   2878 	KASSERT(mutex_owned(proc_lock));
   2879 	KASSERT(mutex_owned(p->p_lock));
   2880 
   2881 	sigemptyset(&ss1);
   2882 	sigemptyset(&ss2);
   2883 	memset(ki, 0, sizeof(*ki));
   2884 
   2885 	ki->p_paddr = PTRTOUINT64(p);
   2886 	ki->p_fd = PTRTOUINT64(p->p_fd);
   2887 	ki->p_cwdi = PTRTOUINT64(p->p_cwdi);
   2888 	ki->p_stats = PTRTOUINT64(p->p_stats);
   2889 	ki->p_limit = PTRTOUINT64(p->p_limit);
   2890 	ki->p_vmspace = PTRTOUINT64(p->p_vmspace);
   2891 	ki->p_sigacts = PTRTOUINT64(p->p_sigacts);
   2892 	ki->p_sess = PTRTOUINT64(p->p_session);
   2893 	ki->p_tsess = 0;	/* may be changed if controlling tty below */
   2894 	ki->p_ru = PTRTOUINT64(&p->p_stats->p_ru);
   2895 	ki->p_eflag = 0;
   2896 	ki->p_exitsig = p->p_exitsig;
   2897 	ki->p_flag = sysctl_map_flags(sysctl_flagmap, p->p_flag);
   2898 	ki->p_flag |= sysctl_map_flags(sysctl_sflagmap, p->p_sflag);
   2899 	ki->p_flag |= sysctl_map_flags(sysctl_slflagmap, p->p_slflag);
   2900 	ki->p_flag |= sysctl_map_flags(sysctl_lflagmap, p->p_lflag);
   2901 	ki->p_flag |= sysctl_map_flags(sysctl_stflagmap, p->p_stflag);
   2902 	ki->p_pid = p->p_pid;
   2903 	if (p->p_pptr)
   2904 		ki->p_ppid = p->p_pptr->p_pid;
   2905 	else
   2906 		ki->p_ppid = 0;
   2907 	ki->p_uid = kauth_cred_geteuid(p->p_cred);
   2908 	ki->p_ruid = kauth_cred_getuid(p->p_cred);
   2909 	ki->p_gid = kauth_cred_getegid(p->p_cred);
   2910 	ki->p_rgid = kauth_cred_getgid(p->p_cred);
   2911 	ki->p_svuid = kauth_cred_getsvuid(p->p_cred);
   2912 	ki->p_svgid = kauth_cred_getsvgid(p->p_cred);
   2913 	ki->p_ngroups = kauth_cred_ngroups(p->p_cred);
   2914 	kauth_cred_getgroups(p->p_cred, ki->p_groups,
   2915 	    min(ki->p_ngroups, sizeof(ki->p_groups) / sizeof(ki->p_groups[0])),
   2916 	    UIO_SYSSPACE);
   2917 
   2918 	ki->p_uticks = p->p_uticks;
   2919 	ki->p_sticks = p->p_sticks;
   2920 	ki->p_iticks = p->p_iticks;
   2921 	ki->p_tpgid = NO_PGID;	/* may be changed if controlling tty below */
   2922 	ki->p_tracep = PTRTOUINT64(p->p_tracep);
   2923 	ki->p_traceflag = p->p_traceflag;
   2924 
   2925 	memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
   2926 	memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
   2927 
   2928 	ki->p_cpticks = 0;
   2929 	ki->p_pctcpu = p->p_pctcpu;
   2930 	ki->p_estcpu = 0;
   2931 	ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
   2932 	ki->p_realstat = p->p_stat;
   2933 	ki->p_nice = p->p_nice;
   2934 	ki->p_xstat = p->p_xstat;
   2935 	ki->p_acflag = p->p_acflag;
   2936 
   2937 	strncpy(ki->p_comm, p->p_comm,
   2938 	    min(sizeof(ki->p_comm), sizeof(p->p_comm)));
   2939 
   2940 	ki->p_nlwps = p->p_nlwps;
   2941 	ki->p_realflag = ki->p_flag;
   2942 
   2943 	if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
   2944 		vm = p->p_vmspace;
   2945 		ki->p_vm_rssize = vm_resident_count(vm);
   2946 		ki->p_vm_tsize = vm->vm_tsize;
   2947 		ki->p_vm_dsize = vm->vm_dsize;
   2948 		ki->p_vm_ssize = vm->vm_ssize;
   2949 
   2950 		/* Pick a "representative" LWP */
   2951 		l = proc_representative_lwp(p, &tmp, 1);
   2952 		lwp_lock(l);
   2953 		ki->p_nrlwps = tmp;
   2954 		ki->p_forw = 0;
   2955 		ki->p_back = 0;
   2956 		ki->p_addr = PTRTOUINT64(l->l_addr);
   2957 		ki->p_stat = l->l_stat;
   2958 		ki->p_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
   2959 		ki->p_swtime = l->l_swtime;
   2960 		ki->p_slptime = l->l_slptime;
   2961 		if (l->l_stat == LSONPROC)
   2962 			ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   2963 		else
   2964 			ki->p_schedflags = 0;
   2965 		ki->p_holdcnt = l->l_holdcnt;
   2966 		ki->p_priority = lwp_eprio(l);
   2967 		ki->p_usrpri = l->l_priority;
   2968 		if (l->l_wchan)
   2969 			strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
   2970 		ki->p_wchan = PTRTOUINT64(l->l_wchan);
   2971 		ki->p_cpuid = cpu_index(l->l_cpu);
   2972 		lwp_unlock(l);
   2973 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   2974 			/* This is hardly correct, but... */
   2975 			sigplusset(&l->l_sigpend.sp_set, &ss1);
   2976 			sigplusset(&l->l_sigmask, &ss2);
   2977 			ki->p_cpticks += l->l_cpticks;
   2978 			ki->p_pctcpu += l->l_pctcpu;
   2979 			ki->p_estcpu += l->l_estcpu;
   2980 		}
   2981 	}
   2982 	sigplusset(&p->p_sigpend.sp_set, &ss2);
   2983 	memcpy(&ki->p_siglist, &ss1, sizeof(ki_sigset_t));
   2984 	memcpy(&ki->p_sigmask, &ss2, sizeof(ki_sigset_t));
   2985 
   2986 	if (p->p_session != NULL) {
   2987 		ki->p_sid = p->p_session->s_sid;
   2988 		ki->p__pgid = p->p_pgrp->pg_id;
   2989 		if (p->p_session->s_ttyvp)
   2990 			ki->p_eflag |= EPROC_CTTY;
   2991 		if (SESS_LEADER(p))
   2992 			ki->p_eflag |= EPROC_SLEADER;
   2993 		strncpy(ki->p_login, p->p_session->s_login,
   2994 		    min(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
   2995 		ki->p_jobc = p->p_pgrp->pg_jobc;
   2996 		if ((p->p_lflag & PL_CONTROLT) && (tp = p->p_session->s_ttyp)) {
   2997 			ki->p_tdev = tp->t_dev;
   2998 			ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   2999 			ki->p_tsess = PTRTOUINT64(tp->t_session);
   3000 		} else {
   3001 			ki->p_tdev = NODEV;
   3002 		}
   3003 	}
   3004 
   3005 	if (!P_ZOMBIE(p) && !zombie) {
   3006 		ki->p_uvalid = 1;
   3007 		ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
   3008 		ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
   3009 
   3010 		calcru(p, &ut, &st, NULL, &rt);
   3011 		ki->p_rtime_sec = rt.tv_sec;
   3012 		ki->p_rtime_usec = rt.tv_usec;
   3013 		ki->p_uutime_sec = ut.tv_sec;
   3014 		ki->p_uutime_usec = ut.tv_usec;
   3015 		ki->p_ustime_sec = st.tv_sec;
   3016 		ki->p_ustime_usec = st.tv_usec;
   3017 
   3018 		memcpy(&ru, &p->p_stats->p_ru, sizeof(ru));
   3019 		ki->p_uru_nvcsw = 0;
   3020 		ki->p_uru_nivcsw = 0;
   3021 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   3022 			ki->p_uru_nvcsw += (l2->l_ncsw - l2->l_nivcsw);
   3023 			ki->p_uru_nivcsw += l2->l_nivcsw;
   3024 			ruadd(&ru, &l2->l_ru);
   3025 		}
   3026 		ki->p_uru_maxrss = ru.ru_maxrss;
   3027 		ki->p_uru_ixrss = ru.ru_ixrss;
   3028 		ki->p_uru_idrss = ru.ru_idrss;
   3029 		ki->p_uru_isrss = ru.ru_isrss;
   3030 		ki->p_uru_minflt = ru.ru_minflt;
   3031 		ki->p_uru_majflt = ru.ru_majflt;
   3032 		ki->p_uru_nswap = ru.ru_nswap;
   3033 		ki->p_uru_inblock = ru.ru_inblock;
   3034 		ki->p_uru_oublock = ru.ru_oublock;
   3035 		ki->p_uru_msgsnd = ru.ru_msgsnd;
   3036 		ki->p_uru_msgrcv = ru.ru_msgrcv;
   3037 		ki->p_uru_nsignals = ru.ru_nsignals;
   3038 
   3039 		timeradd(&p->p_stats->p_cru.ru_utime,
   3040 			 &p->p_stats->p_cru.ru_stime, &ut);
   3041 		ki->p_uctime_sec = ut.tv_sec;
   3042 		ki->p_uctime_usec = ut.tv_usec;
   3043 	}
   3044 }
   3045 
   3046 /*
   3047  * Fill in a kinfo_lwp structure for the specified lwp.
   3048  */
   3049 static void
   3050 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
   3051 {
   3052 	struct proc *p = l->l_proc;
   3053 	struct timeval tv;
   3054 
   3055 	KASSERT(lwp_locked(l, NULL));
   3056 
   3057 	kl->l_forw = 0;
   3058 	kl->l_back = 0;
   3059 	kl->l_laddr = PTRTOUINT64(l);
   3060 	kl->l_addr = PTRTOUINT64(l->l_addr);
   3061 	kl->l_stat = l->l_stat;
   3062 	kl->l_lid = l->l_lid;
   3063 	kl->l_flag = sysctl_map_flags(sysctl_lwpprflagmap, l->l_prflag);
   3064 
   3065 	kl->l_swtime = l->l_swtime;
   3066 	kl->l_slptime = l->l_slptime;
   3067 	if (l->l_stat == LSONPROC)
   3068 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   3069 	else
   3070 		kl->l_schedflags = 0;
   3071 	kl->l_holdcnt = l->l_holdcnt;
   3072 	kl->l_priority = lwp_eprio(l);
   3073 	kl->l_usrpri = l->l_priority;
   3074 	if (l->l_wchan)
   3075 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
   3076 	kl->l_wchan = PTRTOUINT64(l->l_wchan);
   3077 	kl->l_cpuid = cpu_index(l->l_cpu);
   3078 	bintime2timeval(&l->l_rtime, &tv);
   3079 	kl->l_rtime_sec = tv.tv_sec;
   3080 	kl->l_rtime_usec = tv.tv_usec;
   3081 	kl->l_cpticks = l->l_cpticks;
   3082 	kl->l_pctcpu = l->l_pctcpu;
   3083 	kl->l_pid = p->p_pid;
   3084 	if (l->l_name == NULL)
   3085 		kl->l_name[0] = '\0';
   3086 	else
   3087 		strlcpy(kl->l_name, l->l_name, sizeof(kl->l_name));
   3088 }
   3089 
   3090 /*
   3091  * Fill in an eproc structure for the specified process.
   3092  */
   3093 void
   3094 fill_eproc(struct proc *p, struct eproc *ep, bool zombie)
   3095 {
   3096 	struct tty *tp;
   3097 	struct lwp *l;
   3098 
   3099 	KASSERT(mutex_owned(proc_lock));
   3100 	KASSERT(mutex_owned(p->p_lock));
   3101 
   3102 	memset(ep, 0, sizeof(*ep));
   3103 
   3104 	ep->e_paddr = p;
   3105 	ep->e_sess = p->p_session;
   3106 	if (p->p_cred) {
   3107 		kauth_cred_topcred(p->p_cred, &ep->e_pcred);
   3108 		kauth_cred_toucred(p->p_cred, &ep->e_ucred);
   3109 	}
   3110 	if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
   3111 		struct vmspace *vm = p->p_vmspace;
   3112 
   3113 		ep->e_vm.vm_rssize = vm_resident_count(vm);
   3114 		ep->e_vm.vm_tsize = vm->vm_tsize;
   3115 		ep->e_vm.vm_dsize = vm->vm_dsize;
   3116 		ep->e_vm.vm_ssize = vm->vm_ssize;
   3117 
   3118 		/* Pick a "representative" LWP */
   3119 		l = proc_representative_lwp(p, NULL, 1);
   3120 		lwp_lock(l);
   3121 		if (l->l_wchan)
   3122 			strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
   3123 		lwp_unlock(l);
   3124 	}
   3125 	if (p->p_pptr)
   3126 		ep->e_ppid = p->p_pptr->p_pid;
   3127 	if (p->p_pgrp && p->p_session) {
   3128 		ep->e_pgid = p->p_pgrp->pg_id;
   3129 		ep->e_jobc = p->p_pgrp->pg_jobc;
   3130 		ep->e_sid = p->p_session->s_sid;
   3131 		if ((p->p_lflag & PL_CONTROLT) &&
   3132 		    (tp = ep->e_sess->s_ttyp)) {
   3133 			ep->e_tdev = tp->t_dev;
   3134 			ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   3135 			ep->e_tsess = tp->t_session;
   3136 		} else
   3137 			ep->e_tdev = NODEV;
   3138 		ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
   3139 		if (SESS_LEADER(p))
   3140 			ep->e_flag |= EPROC_SLEADER;
   3141 		strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
   3142 	}
   3143 	ep->e_xsize = ep->e_xrssize = 0;
   3144 	ep->e_xccount = ep->e_xswrss = 0;
   3145 }
   3146 
   3147 u_int
   3148 sysctl_map_flags(const u_int *map, u_int word)
   3149 {
   3150 	u_int rv;
   3151 
   3152 	for (rv = 0; *map != 0; map += 2)
   3153 		if ((word & map[0]) != 0)
   3154 			rv |= map[1];
   3155 
   3156 	return rv;
   3157 }
   3158