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