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