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