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