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