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