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