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