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