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