Home | History | Annotate | Line # | Download | only in kern
init_sysctl.c revision 1.60
      1 /*	$NetBSD: init_sysctl.c,v 1.60 2006/01/27 03:14:56 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.60 2006/01/27 03:14:56 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 = 0;
     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 	const struct sysctlnode *rnode = NULL;
   1025 
   1026 	sysctl_createv(clog, 0, NULL, &rnode,
   1027 		       CTLFLAG_PERMANENT,
   1028 		       CTLTYPE_NODE, "security", NULL,
   1029 		       NULL, 0, NULL, 0,
   1030 		       CTL_SECURITY, CTL_EOL);
   1031 
   1032 	sysctl_createv(clog, 0, &rnode, NULL,
   1033 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1034 		       CTLTYPE_INT, "curtain",
   1035 		       SYSCTL_DESCR("Curtain information about objects"
   1036 				    " to users not owning them."),
   1037 		       NULL, 0, &security_curtain, 0,
   1038 		       CTL_CREATE, CTL_EOL);
   1039 }
   1040 
   1041 /*
   1042  * ********************************************************************
   1043  * section 2: private node-specific helper routines.
   1044  * ********************************************************************
   1045  */
   1046 
   1047 /*
   1048  * sysctl helper routine for kern.maxvnodes.  drain vnodes if
   1049  * new value is lower than desiredvnodes and then calls reinit
   1050  * routines that needs to adjust to the new value.
   1051  */
   1052 static int
   1053 sysctl_kern_maxvnodes(SYSCTLFN_ARGS)
   1054 {
   1055 	int error, new_vnodes, old_vnodes;
   1056 	struct sysctlnode node;
   1057 
   1058 	new_vnodes = desiredvnodes;
   1059 	node = *rnode;
   1060 	node.sysctl_data = &new_vnodes;
   1061 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1062 	if (error || newp == NULL)
   1063 		return (error);
   1064 
   1065 	old_vnodes = desiredvnodes;
   1066 	desiredvnodes = new_vnodes;
   1067 	if (new_vnodes < old_vnodes) {
   1068 		error = vfs_drainvnodes(new_vnodes, l);
   1069 		if (error) {
   1070 			desiredvnodes = old_vnodes;
   1071 			return (error);
   1072 		}
   1073 	}
   1074 	vfs_reinit();
   1075 	nchreinit();
   1076 
   1077 	return (0);
   1078 }
   1079 
   1080 /*
   1081  * sysctl helper routine for rtc_offset - set time after changes
   1082  */
   1083 static int
   1084 sysctl_kern_rtc_offset(SYSCTLFN_ARGS)
   1085 {
   1086 	struct timespec ts, delta;
   1087 	int error, new_rtc_offset;
   1088 	struct sysctlnode node;
   1089 
   1090 	new_rtc_offset = rtc_offset;
   1091 	node = *rnode;
   1092 	node.sysctl_data = &new_rtc_offset;
   1093 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1094 	if (error || newp == NULL)
   1095 		return (error);
   1096 
   1097 	if (securelevel > 0)
   1098 		return (EPERM);
   1099 	if (rtc_offset == new_rtc_offset)
   1100 		return (0);
   1101 
   1102 	/* if we change the offset, adjust the time */
   1103 	nanotime(&ts);
   1104 	delta.tv_sec = 60 * (new_rtc_offset - rtc_offset);
   1105 	delta.tv_nsec = 0;
   1106 	timespecadd(&ts, &delta, &ts);
   1107 	rtc_offset = new_rtc_offset;
   1108 	settime(l->l_proc, &ts);
   1109 
   1110 	return (0);
   1111 }
   1112 
   1113 /*
   1114  * sysctl helper routine for kern.maxproc.  ensures that the new
   1115  * values are not too low or too high.
   1116  */
   1117 static int
   1118 sysctl_kern_maxproc(SYSCTLFN_ARGS)
   1119 {
   1120 	int error, nmaxproc;
   1121 	struct sysctlnode node;
   1122 
   1123 	nmaxproc = maxproc;
   1124 	node = *rnode;
   1125 	node.sysctl_data = &nmaxproc;
   1126 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1127 	if (error || newp == NULL)
   1128 		return (error);
   1129 
   1130 	if (nmaxproc < 0 || nmaxproc >= PID_MAX)
   1131 		return (EINVAL);
   1132 #ifdef __HAVE_CPU_MAXPROC
   1133 	if (nmaxproc > cpu_maxproc())
   1134 		return (EINVAL);
   1135 #endif
   1136 	maxproc = nmaxproc;
   1137 
   1138 	return (0);
   1139 }
   1140 
   1141 /*
   1142  * sysctl helper routine for kern.securelevel.  ensures that the value
   1143  * only rises unless the caller has pid 1 (assumed to be init).
   1144  */
   1145 static int
   1146 sysctl_kern_securelevel(SYSCTLFN_ARGS)
   1147 {
   1148 	int newsecurelevel, error;
   1149 	struct sysctlnode node;
   1150 
   1151 	newsecurelevel = securelevel;
   1152 	node = *rnode;
   1153 	node.sysctl_data = &newsecurelevel;
   1154 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1155 	if (error || newp == NULL)
   1156 		return (error);
   1157 
   1158 	if (newsecurelevel < securelevel && l && l->l_proc->p_pid != 1)
   1159 		return (EPERM);
   1160 	securelevel = newsecurelevel;
   1161 
   1162 	return (error);
   1163 }
   1164 
   1165 /*
   1166  * sysctl helper function for kern.hostid.  the hostid is a long, but
   1167  * we export it as an int, so we need to give it a little help.
   1168  */
   1169 static int
   1170 sysctl_kern_hostid(SYSCTLFN_ARGS)
   1171 {
   1172 	int error, inthostid;
   1173 	struct sysctlnode node;
   1174 
   1175 	inthostid = hostid;  /* XXX assumes sizeof int <= sizeof long */
   1176 	node = *rnode;
   1177 	node.sysctl_data = &inthostid;
   1178 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1179 	if (error || newp == NULL)
   1180 		return (error);
   1181 
   1182 	hostid = (unsigned)inthostid;
   1183 
   1184 	return (0);
   1185 }
   1186 
   1187 /*
   1188  * sysctl helper function for kern.hostname and kern.domainnname.
   1189  * resets the relevant recorded length when the underlying name is
   1190  * changed.
   1191  */
   1192 static int
   1193 sysctl_setlen(SYSCTLFN_ARGS)
   1194 {
   1195 	int error;
   1196 
   1197 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
   1198 	if (error || newp == NULL)
   1199 		return (error);
   1200 
   1201 	switch (rnode->sysctl_num) {
   1202 	case KERN_HOSTNAME:
   1203 		hostnamelen = strlen((const char*)rnode->sysctl_data);
   1204 		break;
   1205 	case KERN_DOMAINNAME:
   1206 		domainnamelen = strlen((const char*)rnode->sysctl_data);
   1207 		break;
   1208 	}
   1209 
   1210 	return (0);
   1211 }
   1212 
   1213 /*
   1214  * sysctl helper routine for kern.clockrate.  assembles a struct on
   1215  * the fly to be returned to the caller.
   1216  */
   1217 static int
   1218 sysctl_kern_clockrate(SYSCTLFN_ARGS)
   1219 {
   1220 	struct clockinfo clkinfo;
   1221 	struct sysctlnode node;
   1222 
   1223 	clkinfo.tick = tick;
   1224 	clkinfo.tickadj = tickadj;
   1225 	clkinfo.hz = hz;
   1226 	clkinfo.profhz = profhz;
   1227 	clkinfo.stathz = stathz ? stathz : hz;
   1228 
   1229 	node = *rnode;
   1230 	node.sysctl_data = &clkinfo;
   1231 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1232 }
   1233 
   1234 
   1235 /*
   1236  * sysctl helper routine for kern.file pseudo-subtree.
   1237  */
   1238 static int
   1239 sysctl_kern_file(SYSCTLFN_ARGS)
   1240 {
   1241 	int error;
   1242 	size_t buflen;
   1243 	struct file *fp;
   1244 	char *start, *where;
   1245 
   1246 	start = where = oldp;
   1247 	buflen = *oldlenp;
   1248 	if (where == NULL) {
   1249 		/*
   1250 		 * overestimate by 10 files
   1251 		 */
   1252 		*oldlenp = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
   1253 		return (0);
   1254 	}
   1255 
   1256 	/*
   1257 	 * first copyout filehead
   1258 	 */
   1259 	if (buflen < sizeof(filehead)) {
   1260 		*oldlenp = 0;
   1261 		return (0);
   1262 	}
   1263 	error = copyout(&filehead, where, sizeof(filehead));
   1264 	if (error)
   1265 		return (error);
   1266 	buflen -= sizeof(filehead);
   1267 	where += sizeof(filehead);
   1268 
   1269 	/*
   1270 	 * followed by an array of file structures
   1271 	 */
   1272 	LIST_FOREACH(fp, &filehead, f_list) {
   1273 		if (CURTAIN(l->l_proc->p_ucred->cr_uid, fp->f_cred->cr_uid))
   1274 			continue;
   1275 		if (buflen < sizeof(struct file)) {
   1276 			*oldlenp = where - start;
   1277 			return (ENOMEM);
   1278 		}
   1279 		error = copyout(fp, where, sizeof(struct file));
   1280 		if (error)
   1281 			return (error);
   1282 		buflen -= sizeof(struct file);
   1283 		where += sizeof(struct file);
   1284 	}
   1285 	*oldlenp = where - start;
   1286 	return (0);
   1287 }
   1288 
   1289 /*
   1290  * sysctl helper routine for kern.autonicetime and kern.autoniceval.
   1291  * asserts that the assigned value is in the correct range.
   1292  */
   1293 static int
   1294 sysctl_kern_autonice(SYSCTLFN_ARGS)
   1295 {
   1296 	int error, t = 0;
   1297 	struct sysctlnode node;
   1298 
   1299 	node = *rnode;
   1300 	t = *(int*)node.sysctl_data;
   1301 	node.sysctl_data = &t;
   1302 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1303 	if (error || newp == NULL)
   1304 		return (error);
   1305 
   1306 	switch (node.sysctl_num) {
   1307 	case KERN_AUTONICETIME:
   1308 		if (t >= 0)
   1309 			autonicetime = t;
   1310 		break;
   1311 	case KERN_AUTONICEVAL:
   1312 		if (t < PRIO_MIN)
   1313 			t = PRIO_MIN;
   1314 		else if (t > PRIO_MAX)
   1315 			t = PRIO_MAX;
   1316 		autoniceval = t;
   1317 		break;
   1318 	}
   1319 
   1320 	return (0);
   1321 }
   1322 
   1323 /*
   1324  * sysctl helper routine for kern.msgbufsize and kern.msgbuf.  for the
   1325  * former it merely checks the message buffer is set up.  for the latter,
   1326  * it also copies out the data if necessary.
   1327  */
   1328 static int
   1329 sysctl_msgbuf(SYSCTLFN_ARGS)
   1330 {
   1331 	char *where = oldp;
   1332 	size_t len, maxlen;
   1333 	long beg, end;
   1334 	int error;
   1335 
   1336 	if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
   1337 		msgbufenabled = 0;
   1338 		return (ENXIO);
   1339 	}
   1340 
   1341 	switch (rnode->sysctl_num) {
   1342 	case KERN_MSGBUFSIZE: {
   1343 		struct sysctlnode node = *rnode;
   1344 		int msg_bufs = (int)msgbufp->msg_bufs;
   1345 		node.sysctl_data = &msg_bufs;
   1346 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1347 	}
   1348 	case KERN_MSGBUF:
   1349 		break;
   1350 	default:
   1351 		return (EOPNOTSUPP);
   1352 	}
   1353 
   1354 	if (newp != NULL)
   1355 		return (EPERM);
   1356 
   1357         if (oldp == NULL) {
   1358 		/* always return full buffer size */
   1359 		*oldlenp = msgbufp->msg_bufs;
   1360 		return (0);
   1361         }
   1362 
   1363 	error = 0;
   1364 	maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
   1365 
   1366 	/*
   1367 	 * First, copy from the write pointer to the end of
   1368 	 * message buffer.
   1369 	 */
   1370 	beg = msgbufp->msg_bufx;
   1371 	end = msgbufp->msg_bufs;
   1372 	while (maxlen > 0) {
   1373 		len = MIN(end - beg, maxlen);
   1374 		if (len == 0)
   1375 			break;
   1376 		error = copyout(&msgbufp->msg_bufc[beg], where, len);
   1377 		if (error)
   1378 			break;
   1379 		where += len;
   1380 		maxlen -= len;
   1381 
   1382 		/*
   1383 		 * ... then, copy from the beginning of message buffer to
   1384 		 * the write pointer.
   1385 		 */
   1386 		beg = 0;
   1387 		end = msgbufp->msg_bufx;
   1388 	}
   1389 
   1390 	return (error);
   1391 }
   1392 
   1393 /*
   1394  * sysctl helper routine for kern.defcorename.  in the case of a new
   1395  * string being assigned, check that it's not a zero-length string.
   1396  * (XXX the check in -current doesn't work, but do we really care?)
   1397  */
   1398 static int
   1399 sysctl_kern_defcorename(SYSCTLFN_ARGS)
   1400 {
   1401 	int error;
   1402 	char newcorename[MAXPATHLEN];
   1403 	struct sysctlnode node;
   1404 
   1405 	node = *rnode;
   1406 	node.sysctl_data = &newcorename[0];
   1407 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
   1408 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1409 	if (error || newp == NULL)
   1410 		return (error);
   1411 
   1412 	/*
   1413 	 * when sysctl_lookup() deals with a string, it's guaranteed
   1414 	 * to come back nul terminated.  so there.  :)
   1415 	 */
   1416 	if (strlen(newcorename) == 0)
   1417 		return (EINVAL);
   1418 
   1419 	memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
   1420 
   1421 	return (0);
   1422 }
   1423 
   1424 /*
   1425  * sysctl helper routine for kern.cp_time node.  adds up cpu time
   1426  * across all cpus.
   1427  */
   1428 static int
   1429 sysctl_kern_cptime(SYSCTLFN_ARGS)
   1430 {
   1431 	struct sysctlnode node = *rnode;
   1432 
   1433 #ifndef MULTIPROCESSOR
   1434 
   1435 	if (namelen == 1) {
   1436 		if (name[0] != 0)
   1437 			return (ENOENT);
   1438 		/*
   1439 		 * you're allowed to ask for the zero'th processor
   1440 		 */
   1441 		name++;
   1442 		namelen--;
   1443 	}
   1444 	node.sysctl_data = curcpu()->ci_schedstate.spc_cp_time;
   1445 	node.sysctl_size = sizeof(curcpu()->ci_schedstate.spc_cp_time);
   1446 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1447 
   1448 #else /* MULTIPROCESSOR */
   1449 
   1450 	uint64_t *cp_time = NULL;
   1451 	int error, n = sysctl_ncpus(), i;
   1452 	struct cpu_info *ci;
   1453 	CPU_INFO_ITERATOR cii;
   1454 
   1455 	/*
   1456 	 * if you specifically pass a buffer that is the size of the
   1457 	 * sum, or if you are probing for the size, you get the "sum"
   1458 	 * of cp_time (and the size thereof) across all processors.
   1459 	 *
   1460 	 * alternately, you can pass an additional mib number and get
   1461 	 * cp_time for that particular processor.
   1462 	 */
   1463 	switch (namelen) {
   1464 	case 0:
   1465 	    	if (*oldlenp == sizeof(uint64_t) * CPUSTATES || oldp == NULL) {
   1466 			node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
   1467 			n = -1; /* SUM */
   1468 		}
   1469 		else {
   1470 			node.sysctl_size = n * sizeof(uint64_t) * CPUSTATES;
   1471 			n = -2; /* ALL */
   1472 		}
   1473 		break;
   1474 	case 1:
   1475 		if (name[0] < 0 || name[0] >= n)
   1476 			return (ENOENT); /* ENOSUCHPROCESSOR */
   1477 		node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
   1478 		n = name[0];
   1479 		/*
   1480 		 * adjust these so that sysctl_lookup() will be happy
   1481 		 */
   1482 		name++;
   1483 		namelen--;
   1484 		break;
   1485 	default:
   1486 		return (EINVAL);
   1487 	}
   1488 
   1489 	cp_time = malloc(node.sysctl_size, M_TEMP, M_WAITOK|M_CANFAIL);
   1490 	if (cp_time == NULL)
   1491 		return (ENOMEM);
   1492 	node.sysctl_data = cp_time;
   1493 	memset(cp_time, 0, node.sysctl_size);
   1494 
   1495 	for (CPU_INFO_FOREACH(cii, ci)) {
   1496 		if (n <= 0)
   1497 			for (i = 0; i < CPUSTATES; i++)
   1498 				cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
   1499 		/*
   1500 		 * if a specific processor was requested and we just
   1501 		 * did it, we're done here
   1502 		 */
   1503 		if (n == 0)
   1504 			break;
   1505 		/*
   1506 		 * if doing "all", skip to next cp_time set for next processor
   1507 		 */
   1508 		if (n == -2)
   1509 			cp_time += CPUSTATES;
   1510 		/*
   1511 		 * if we're doing a specific processor, we're one
   1512 		 * processor closer
   1513 		 */
   1514 		if (n > 0)
   1515 			n--;
   1516 	}
   1517 
   1518 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1519 	free(node.sysctl_data, M_TEMP);
   1520 	return (error);
   1521 
   1522 #endif /* MULTIPROCESSOR */
   1523 }
   1524 
   1525 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
   1526 /*
   1527  * sysctl helper routine for kern.sysvipc_info subtree.
   1528  */
   1529 
   1530 #define	FILL_PERM(src, dst) do { \
   1531 	(dst)._key = (src)._key; \
   1532 	(dst).uid = (src).uid; \
   1533 	(dst).gid = (src).gid; \
   1534 	(dst).cuid = (src).cuid; \
   1535 	(dst).cgid = (src).cgid; \
   1536 	(dst).mode = (src).mode; \
   1537 	(dst)._seq = (src)._seq; \
   1538 } while (/*CONSTCOND*/ 0);
   1539 #define	FILL_MSG(src, dst) do { \
   1540 	FILL_PERM((src).msg_perm, (dst).msg_perm); \
   1541 	(dst).msg_qnum = (src).msg_qnum; \
   1542 	(dst).msg_qbytes = (src).msg_qbytes; \
   1543 	(dst)._msg_cbytes = (src)._msg_cbytes; \
   1544 	(dst).msg_lspid = (src).msg_lspid; \
   1545 	(dst).msg_lrpid = (src).msg_lrpid; \
   1546 	(dst).msg_stime = (src).msg_stime; \
   1547 	(dst).msg_rtime = (src).msg_rtime; \
   1548 	(dst).msg_ctime = (src).msg_ctime; \
   1549 } while (/*CONSTCOND*/ 0)
   1550 #define	FILL_SEM(src, dst) do { \
   1551 	FILL_PERM((src).sem_perm, (dst).sem_perm); \
   1552 	(dst).sem_nsems = (src).sem_nsems; \
   1553 	(dst).sem_otime = (src).sem_otime; \
   1554 	(dst).sem_ctime = (src).sem_ctime; \
   1555 } while (/*CONSTCOND*/ 0)
   1556 #define	FILL_SHM(src, dst) do { \
   1557 	FILL_PERM((src).shm_perm, (dst).shm_perm); \
   1558 	(dst).shm_segsz = (src).shm_segsz; \
   1559 	(dst).shm_lpid = (src).shm_lpid; \
   1560 	(dst).shm_cpid = (src).shm_cpid; \
   1561 	(dst).shm_atime = (src).shm_atime; \
   1562 	(dst).shm_dtime = (src).shm_dtime; \
   1563 	(dst).shm_ctime = (src).shm_ctime; \
   1564 	(dst).shm_nattch = (src).shm_nattch; \
   1565 } while (/*CONSTCOND*/ 0)
   1566 
   1567 static int
   1568 sysctl_kern_sysvipc(SYSCTLFN_ARGS)
   1569 {
   1570 	void *where = oldp;
   1571 	size_t *sizep = oldlenp;
   1572 #ifdef SYSVMSG
   1573 	struct msg_sysctl_info *msgsi = NULL;
   1574 #endif
   1575 #ifdef SYSVSEM
   1576 	struct sem_sysctl_info *semsi = NULL;
   1577 #endif
   1578 #ifdef SYSVSHM
   1579 	struct shm_sysctl_info *shmsi = NULL;
   1580 #endif
   1581 	size_t infosize, dssize, tsize, buflen;
   1582 	void *bf = NULL;
   1583 	char *start;
   1584 	int32_t nds;
   1585 	int i, error, ret;
   1586 
   1587 	if (namelen != 1)
   1588 		return (EINVAL);
   1589 
   1590 	start = where;
   1591 	buflen = *sizep;
   1592 
   1593 	switch (*name) {
   1594 	case KERN_SYSVIPC_MSG_INFO:
   1595 #ifdef SYSVMSG
   1596 		infosize = sizeof(msgsi->msginfo);
   1597 		nds = msginfo.msgmni;
   1598 		dssize = sizeof(msgsi->msgids[0]);
   1599 		break;
   1600 #else
   1601 		return (EINVAL);
   1602 #endif
   1603 	case KERN_SYSVIPC_SEM_INFO:
   1604 #ifdef SYSVSEM
   1605 		infosize = sizeof(semsi->seminfo);
   1606 		nds = seminfo.semmni;
   1607 		dssize = sizeof(semsi->semids[0]);
   1608 		break;
   1609 #else
   1610 		return (EINVAL);
   1611 #endif
   1612 	case KERN_SYSVIPC_SHM_INFO:
   1613 #ifdef SYSVSHM
   1614 		infosize = sizeof(shmsi->shminfo);
   1615 		nds = shminfo.shmmni;
   1616 		dssize = sizeof(shmsi->shmids[0]);
   1617 		break;
   1618 #else
   1619 		return (EINVAL);
   1620 #endif
   1621 	default:
   1622 		return (EINVAL);
   1623 	}
   1624 	/*
   1625 	 * Round infosize to 64 bit boundary if requesting more than just
   1626 	 * the info structure or getting the total data size.
   1627 	 */
   1628 	if (where == NULL || *sizep > infosize)
   1629 		infosize = ((infosize + 7) / 8) * 8;
   1630 	tsize = infosize + nds * dssize;
   1631 
   1632 	/* Return just the total size required. */
   1633 	if (where == NULL) {
   1634 		*sizep = tsize;
   1635 		return (0);
   1636 	}
   1637 
   1638 	/* Not enough room for even the info struct. */
   1639 	if (buflen < infosize) {
   1640 		*sizep = 0;
   1641 		return (ENOMEM);
   1642 	}
   1643 	bf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK);
   1644 	memset(bf, 0, min(tsize, buflen));
   1645 
   1646 	switch (*name) {
   1647 #ifdef SYSVMSG
   1648 	case KERN_SYSVIPC_MSG_INFO:
   1649 		msgsi = (struct msg_sysctl_info *)bf;
   1650 		msgsi->msginfo = msginfo;
   1651 		break;
   1652 #endif
   1653 #ifdef SYSVSEM
   1654 	case KERN_SYSVIPC_SEM_INFO:
   1655 		semsi = (struct sem_sysctl_info *)bf;
   1656 		semsi->seminfo = seminfo;
   1657 		break;
   1658 #endif
   1659 #ifdef SYSVSHM
   1660 	case KERN_SYSVIPC_SHM_INFO:
   1661 		shmsi = (struct shm_sysctl_info *)bf;
   1662 		shmsi->shminfo = shminfo;
   1663 		break;
   1664 #endif
   1665 	}
   1666 	buflen -= infosize;
   1667 
   1668 	ret = 0;
   1669 	if (buflen > 0) {
   1670 		/* Fill in the IPC data structures.  */
   1671 		for (i = 0; i < nds; i++) {
   1672 			if (buflen < dssize) {
   1673 				ret = ENOMEM;
   1674 				break;
   1675 			}
   1676 			switch (*name) {
   1677 #ifdef SYSVMSG
   1678 			case KERN_SYSVIPC_MSG_INFO:
   1679 				FILL_MSG(msqids[i], msgsi->msgids[i]);
   1680 				break;
   1681 #endif
   1682 #ifdef SYSVSEM
   1683 			case KERN_SYSVIPC_SEM_INFO:
   1684 				FILL_SEM(sema[i], semsi->semids[i]);
   1685 				break;
   1686 #endif
   1687 #ifdef SYSVSHM
   1688 			case KERN_SYSVIPC_SHM_INFO:
   1689 				FILL_SHM(shmsegs[i], shmsi->shmids[i]);
   1690 				break;
   1691 #endif
   1692 			}
   1693 			buflen -= dssize;
   1694 		}
   1695 	}
   1696 	*sizep -= buflen;
   1697 	error = copyout(bf, start, *sizep);
   1698 	/* If copyout succeeded, use return code set earlier. */
   1699 	if (error == 0)
   1700 		error = ret;
   1701 	if (bf)
   1702 		free(bf, M_TEMP);
   1703 	return (error);
   1704 }
   1705 
   1706 #undef FILL_PERM
   1707 #undef FILL_MSG
   1708 #undef FILL_SEM
   1709 #undef FILL_SHM
   1710 
   1711 #endif /* defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM) */
   1712 
   1713 #if NPTY > 0
   1714 /*
   1715  * sysctl helper routine for kern.maxptys.  ensures that any new value
   1716  * is acceptable to the pty subsystem.
   1717  */
   1718 static int
   1719 sysctl_kern_maxptys(SYSCTLFN_ARGS)
   1720 {
   1721 	int pty_maxptys(int, int);		/* defined in kern/tty_pty.c */
   1722 	int error, xmax;
   1723 	struct sysctlnode node;
   1724 
   1725 	/* get current value of maxptys */
   1726 	xmax = pty_maxptys(0, 0);
   1727 
   1728 	node = *rnode;
   1729 	node.sysctl_data = &xmax;
   1730 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1731 	if (error || newp == NULL)
   1732 		return (error);
   1733 
   1734 	if (xmax != pty_maxptys(xmax, 1))
   1735 		return (EINVAL);
   1736 
   1737 	return (0);
   1738 }
   1739 #endif /* NPTY > 0 */
   1740 
   1741 /*
   1742  * sysctl helper routine for kern.sbmax.  basically just ensures that
   1743  * any new value is not too small.
   1744  */
   1745 static int
   1746 sysctl_kern_sbmax(SYSCTLFN_ARGS)
   1747 {
   1748 	int error, new_sbmax;
   1749 	struct sysctlnode node;
   1750 
   1751 	new_sbmax = sb_max;
   1752 	node = *rnode;
   1753 	node.sysctl_data = &new_sbmax;
   1754 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1755 	if (error || newp == NULL)
   1756 		return (error);
   1757 
   1758 	error = sb_max_set(new_sbmax);
   1759 
   1760 	return (error);
   1761 }
   1762 
   1763 /*
   1764  * sysctl helper routine for kern.urandom node.  picks a random number
   1765  * for you.
   1766  */
   1767 static int
   1768 sysctl_kern_urnd(SYSCTLFN_ARGS)
   1769 {
   1770 #if NRND > 0
   1771 	int v;
   1772 
   1773 	if (rnd_extract_data(&v, sizeof(v), RND_EXTRACT_ANY) == sizeof(v)) {
   1774 		struct sysctlnode node = *rnode;
   1775 		node.sysctl_data = &v;
   1776 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1777 	}
   1778 	else
   1779 		return (EIO);	/*XXX*/
   1780 #else
   1781 	return (EOPNOTSUPP);
   1782 #endif
   1783 }
   1784 
   1785 /*
   1786  * sysctl helper routine to do kern.lwp.* work.
   1787  */
   1788 static int
   1789 sysctl_kern_lwp(SYSCTLFN_ARGS)
   1790 {
   1791 	struct kinfo_lwp klwp;
   1792 	struct proc *p;
   1793 	struct lwp *l2;
   1794 	char *where, *dp;
   1795 	int pid, elem_size, elem_count;
   1796 	int buflen, needed, error;
   1797 
   1798 	if (namelen == 1 && name[0] == CTL_QUERY)
   1799 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1800 
   1801 	dp = where = oldp;
   1802 	buflen = where != NULL ? *oldlenp : 0;
   1803 	error = needed = 0;
   1804 
   1805 	if (newp != NULL || namelen != 3)
   1806 		return (EINVAL);
   1807 	pid = name[0];
   1808 	elem_size = name[1];
   1809 	elem_count = name[2];
   1810 
   1811 	p = pfind(pid);
   1812 	if (p == NULL)
   1813 		return (ESRCH);
   1814 	LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1815 		if (buflen >= elem_size && elem_count > 0) {
   1816 			fill_lwp(l2, &klwp);
   1817 			/*
   1818 			 * Copy out elem_size, but not larger than
   1819 			 * the size of a struct kinfo_proc2.
   1820 			 */
   1821 			error = copyout(&klwp, dp,
   1822 			    min(sizeof(klwp), elem_size));
   1823 			if (error)
   1824 				goto cleanup;
   1825 			dp += elem_size;
   1826 			buflen -= elem_size;
   1827 			elem_count--;
   1828 		}
   1829 		needed += elem_size;
   1830 	}
   1831 
   1832 	if (where != NULL) {
   1833 		*oldlenp = dp - where;
   1834 		if (needed > *oldlenp)
   1835 			return (ENOMEM);
   1836 	} else {
   1837 		needed += KERN_LWPSLOP;
   1838 		*oldlenp = needed;
   1839 	}
   1840 	return (0);
   1841  cleanup:
   1842 	return (error);
   1843 }
   1844 
   1845 /*
   1846  * sysctl helper routine for kern.forkfsleep node.  ensures that the
   1847  * given value is not too large or two small, and is at least one
   1848  * timer tick if not zero.
   1849  */
   1850 static int
   1851 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
   1852 {
   1853 	/* userland sees value in ms, internally is in ticks */
   1854 	extern int forkfsleep;		/* defined in kern/kern_fork.c */
   1855 	int error, timo, lsleep;
   1856 	struct sysctlnode node;
   1857 
   1858 	lsleep = forkfsleep * 1000 / hz;
   1859 	node = *rnode;
   1860 	node.sysctl_data = &lsleep;
   1861 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1862 	if (error || newp == NULL)
   1863 		return (error);
   1864 
   1865 	/* refuse negative values, and overly 'long time' */
   1866 	if (lsleep < 0 || lsleep > MAXSLP * 1000)
   1867 		return (EINVAL);
   1868 
   1869 	timo = mstohz(lsleep);
   1870 
   1871 	/* if the interval is >0 ms && <1 tick, use 1 tick */
   1872 	if (lsleep != 0 && timo == 0)
   1873 		forkfsleep = 1;
   1874 	else
   1875 		forkfsleep = timo;
   1876 
   1877 	return (0);
   1878 }
   1879 
   1880 /*
   1881  * sysctl helper routine for kern.root_partition
   1882  */
   1883 static int
   1884 sysctl_kern_root_partition(SYSCTLFN_ARGS)
   1885 {
   1886 	int rootpart = DISKPART(rootdev);
   1887 	struct sysctlnode node = *rnode;
   1888 
   1889 	node.sysctl_data = &rootpart;
   1890 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1891 }
   1892 
   1893 /*
   1894  * sysctl helper function for kern.drivers
   1895  */
   1896 static int
   1897 sysctl_kern_drivers(SYSCTLFN_ARGS)
   1898 {
   1899 	int error;
   1900 	size_t buflen;
   1901 	struct kinfo_drivers kd;
   1902 	char *start, *where;
   1903 	const char *dname;
   1904 	int i;
   1905 	extern struct devsw_conv *devsw_conv;
   1906 	extern int max_devsw_convs;
   1907 
   1908 	if (newp != NULL || namelen != 0)
   1909 		return (EINVAL);
   1910 
   1911 	start = where = oldp;
   1912 	buflen = *oldlenp;
   1913 	if (where == NULL) {
   1914 		*oldlenp = max_devsw_convs * sizeof kd;
   1915 		return 0;
   1916 	}
   1917 
   1918 	/*
   1919 	 * An array of kinfo_drivers structures
   1920 	 */
   1921 	error = 0;
   1922 	for (i = 0; i < max_devsw_convs; i++) {
   1923 		dname = devsw_conv[i].d_name;
   1924 		if (dname == NULL)
   1925 			continue;
   1926 		if (buflen < sizeof kd) {
   1927 			error = ENOMEM;
   1928 			break;
   1929 		}
   1930 		memset(&kd, 0, sizeof(kd));
   1931 		kd.d_bmajor = devsw_conv[i].d_bmajor;
   1932 		kd.d_cmajor = devsw_conv[i].d_cmajor;
   1933 		strlcpy(kd.d_name, dname, sizeof kd.d_name);
   1934 		error = copyout(&kd, where, sizeof kd);
   1935 		if (error != 0)
   1936 			break;
   1937 		buflen -= sizeof kd;
   1938 		where += sizeof kd;
   1939 	}
   1940 	*oldlenp = where - start;
   1941 	return error;
   1942 }
   1943 
   1944 /*
   1945  * sysctl helper function for kern.file2
   1946  */
   1947 static int
   1948 sysctl_kern_file2(SYSCTLFN_ARGS)
   1949 {
   1950 	struct proc *p;
   1951 	struct file *fp;
   1952 	struct filedesc *fd;
   1953 	struct kinfo_file kf;
   1954 	char *dp;
   1955 	u_int i, op;
   1956 	size_t len, needed, elem_size, out_size;
   1957 	int error, arg, elem_count;
   1958 
   1959 	if (namelen == 1 && name[0] == CTL_QUERY)
   1960 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1961 
   1962 	if (namelen != 4)
   1963 		return (EINVAL);
   1964 
   1965 	error = 0;
   1966 	dp = oldp;
   1967 	len = (oldp != NULL) ? *oldlenp : 0;
   1968 	op = name[0];
   1969 	arg = name[1];
   1970 	elem_size = name[2];
   1971 	elem_count = name[3];
   1972 	out_size = MIN(sizeof(kf), elem_size);
   1973 	needed = 0;
   1974 
   1975 	if (elem_size < 1 || elem_count < 0)
   1976 		return (EINVAL);
   1977 
   1978 	switch (op) {
   1979 	case KERN_FILE_BYFILE:
   1980 		/*
   1981 		 * doesn't use arg so it must be zero
   1982 		 */
   1983 		if (arg != 0)
   1984 			return (EINVAL);
   1985 		LIST_FOREACH(fp, &filehead, f_list) {
   1986 			if (CURTAIN(l->l_proc->p_ucred->cr_uid,
   1987 				    fp->f_cred->cr_uid))
   1988 				continue;
   1989 			if (len >= elem_size && elem_count > 0) {
   1990 				fill_file(&kf, fp, NULL, 0);
   1991 				error = copyout(&kf, dp, out_size);
   1992 				if (error)
   1993 					break;
   1994 				dp += elem_size;
   1995 				len -= elem_size;
   1996 			}
   1997 			if (elem_count > 0) {
   1998 				needed += elem_size;
   1999 				if (elem_count != INT_MAX)
   2000 					elem_count--;
   2001 			}
   2002 		}
   2003 		break;
   2004 	    case KERN_FILE_BYPID:
   2005 		if (arg < -1)
   2006 			/* -1 means all processes */
   2007 			return (EINVAL);
   2008 		proclist_lock_read();
   2009 		PROCLIST_FOREACH(p, &allproc) {
   2010 			if (p->p_stat == SIDL)
   2011 				/* skip embryonic processes */
   2012 				continue;
   2013 			if (CURTAIN(l->l_proc->p_ucred->cr_uid,
   2014 				    p->p_ucred->cr_uid))
   2015 				continue;
   2016 			if (arg > 0 && p->p_pid != arg)
   2017 				/* pick only the one we want */
   2018 				/* XXX want 0 to mean "kernel files" */
   2019 				continue;
   2020 			fd = p->p_fd;
   2021 			for (i = 0; i < fd->fd_nfiles; i++) {
   2022 				fp = fd->fd_ofiles[i];
   2023 				if (fp == NULL || !FILE_IS_USABLE(fp))
   2024 					continue;
   2025 				if (len >= elem_size && elem_count > 0) {
   2026 					fill_file(&kf, fd->fd_ofiles[i],
   2027 						  p, i);
   2028 					error = copyout(&kf, dp, out_size);
   2029 					if (error)
   2030 						break;
   2031 					dp += elem_size;
   2032 					len -= elem_size;
   2033 				}
   2034 				if (elem_count > 0) {
   2035 					needed += elem_size;
   2036 					if (elem_count != INT_MAX)
   2037 						elem_count--;
   2038 				}
   2039 			}
   2040 		}
   2041 		proclist_unlock_read();
   2042 		break;
   2043 	default:
   2044 		return (EINVAL);
   2045 	}
   2046 
   2047 	if (oldp == NULL)
   2048 		needed += KERN_FILESLOP * elem_size;
   2049 	*oldlenp = needed;
   2050 
   2051 	return (error);
   2052 }
   2053 
   2054 static void
   2055 fill_file(struct kinfo_file *kp, const struct file *fp, struct proc *p, int i)
   2056 {
   2057 
   2058 	memset(kp, 0, sizeof(*kp));
   2059 
   2060 	kp->ki_fileaddr =	PTRTOUINT64(fp);
   2061 	kp->ki_flag =		fp->f_flag;
   2062 	kp->ki_iflags =		fp->f_iflags;
   2063 	kp->ki_ftype =		fp->f_type;
   2064 	kp->ki_count =		fp->f_count;
   2065 	kp->ki_msgcount =	fp->f_msgcount;
   2066 	kp->ki_usecount =	fp->f_usecount;
   2067 	kp->ki_fucred =		PTRTOUINT64(fp->f_cred);
   2068 	kp->ki_fuid =		fp->f_cred->cr_uid;
   2069 	kp->ki_fgid =		fp->f_cred->cr_gid;
   2070 	kp->ki_fops =		PTRTOUINT64(fp->f_ops);
   2071 	kp->ki_foffset =	fp->f_offset;
   2072 	kp->ki_fdata =		PTRTOUINT64(fp->f_data);
   2073 
   2074 	/* vnode information to glue this file to something */
   2075 	if (fp->f_type == DTYPE_VNODE) {
   2076 		struct vnode *vp = (struct vnode *)fp->f_data;
   2077 
   2078 		kp->ki_vun =	PTRTOUINT64(vp->v_un.vu_socket);
   2079 		kp->ki_vsize =	vp->v_size;
   2080 		kp->ki_vtype =	vp->v_type;
   2081 		kp->ki_vtag =	vp->v_tag;
   2082 		kp->ki_vdata =	PTRTOUINT64(vp->v_data);
   2083 	}
   2084 
   2085         /* process information when retrieved via KERN_FILE_BYPID */
   2086 	if (p) {
   2087 		kp->ki_pid =		p->p_pid;
   2088 		kp->ki_fd =		i;
   2089 		kp->ki_ofileflags =	p->p_fd->fd_ofileflags[i];
   2090 	}
   2091 }
   2092 
   2093 static int
   2094 sysctl_doeproc(SYSCTLFN_ARGS)
   2095 {
   2096 	struct eproc eproc;
   2097 	struct kinfo_proc2 kproc2;
   2098 	struct kinfo_proc *dp;
   2099 	struct proc *p;
   2100 	const struct proclist_desc *pd;
   2101 	char *where, *dp2;
   2102 	int type, op, arg;
   2103 	u_int elem_size, elem_count;
   2104 	size_t buflen, needed;
   2105 	int error;
   2106 
   2107 	if (namelen == 1 && name[0] == CTL_QUERY)
   2108 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   2109 
   2110 	dp = oldp;
   2111 	dp2 = where = oldp;
   2112 	buflen = where != NULL ? *oldlenp : 0;
   2113 	error = 0;
   2114 	needed = 0;
   2115 	type = rnode->sysctl_num;
   2116 
   2117 	if (type == KERN_PROC) {
   2118 		if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
   2119 			return (EINVAL);
   2120 		op = name[0];
   2121 		if (op != KERN_PROC_ALL)
   2122 			arg = name[1];
   2123 		else
   2124 			arg = 0;		/* Quell compiler warning */
   2125 		elem_size = elem_count = 0;	/* Ditto */
   2126 	} else {
   2127 		if (namelen != 4)
   2128 			return (EINVAL);
   2129 		op = name[0];
   2130 		arg = name[1];
   2131 		elem_size = name[2];
   2132 		elem_count = name[3];
   2133 	}
   2134 
   2135 	proclist_lock_read();
   2136 
   2137 	pd = proclists;
   2138 again:
   2139 	PROCLIST_FOREACH(p, pd->pd_list) {
   2140 		/*
   2141 		 * Skip embryonic processes.
   2142 		 */
   2143 		if (p->p_stat == SIDL)
   2144 			continue;
   2145 
   2146 		if (CURTAIN(l->l_proc->p_ucred->cr_uid, p->p_ucred->cr_uid))
   2147 			continue;
   2148 
   2149 		/*
   2150 		 * TODO - make more efficient (see notes below).
   2151 		 * do by session.
   2152 		 */
   2153 		switch (op) {
   2154 
   2155 		case KERN_PROC_PID:
   2156 			/* could do this with just a lookup */
   2157 			if (p->p_pid != (pid_t)arg)
   2158 				continue;
   2159 			break;
   2160 
   2161 		case KERN_PROC_PGRP:
   2162 			/* could do this by traversing pgrp */
   2163 			if (p->p_pgrp->pg_id != (pid_t)arg)
   2164 				continue;
   2165 			break;
   2166 
   2167 		case KERN_PROC_SESSION:
   2168 			if (p->p_session->s_sid != (pid_t)arg)
   2169 				continue;
   2170 			break;
   2171 
   2172 		case KERN_PROC_TTY:
   2173 			if (arg == (int) KERN_PROC_TTY_REVOKE) {
   2174 				if ((p->p_flag & P_CONTROLT) == 0 ||
   2175 				    p->p_session->s_ttyp == NULL ||
   2176 				    p->p_session->s_ttyvp != NULL)
   2177 					continue;
   2178 			} else if ((p->p_flag & P_CONTROLT) == 0 ||
   2179 			    p->p_session->s_ttyp == NULL) {
   2180 				if ((dev_t)arg != KERN_PROC_TTY_NODEV)
   2181 					continue;
   2182 			} else if (p->p_session->s_ttyp->t_dev != (dev_t)arg)
   2183 				continue;
   2184 			break;
   2185 
   2186 		case KERN_PROC_UID:
   2187 			if (p->p_ucred->cr_uid != (uid_t)arg)
   2188 				continue;
   2189 			break;
   2190 
   2191 		case KERN_PROC_RUID:
   2192 			if (p->p_cred->p_ruid != (uid_t)arg)
   2193 				continue;
   2194 			break;
   2195 
   2196 		case KERN_PROC_GID:
   2197 			if (p->p_ucred->cr_gid != (uid_t)arg)
   2198 				continue;
   2199 			break;
   2200 
   2201 		case KERN_PROC_RGID:
   2202 			if (p->p_cred->p_rgid != (uid_t)arg)
   2203 				continue;
   2204 			break;
   2205 
   2206 		case KERN_PROC_ALL:
   2207 			/* allow everything */
   2208 			break;
   2209 
   2210 		default:
   2211 			error = EINVAL;
   2212 			goto cleanup;
   2213 		}
   2214 		if (type == KERN_PROC) {
   2215 			if (buflen >= sizeof(struct kinfo_proc)) {
   2216 				fill_eproc(p, &eproc);
   2217 				error = copyout(p, &dp->kp_proc,
   2218 				    sizeof(struct proc));
   2219 				if (error)
   2220 					goto cleanup;
   2221 				error = copyout(&eproc, &dp->kp_eproc,
   2222 				    sizeof(eproc));
   2223 				if (error)
   2224 					goto cleanup;
   2225 				dp++;
   2226 				buflen -= sizeof(struct kinfo_proc);
   2227 			}
   2228 			needed += sizeof(struct kinfo_proc);
   2229 		} else { /* KERN_PROC2 */
   2230 			if (buflen >= elem_size && elem_count > 0) {
   2231 				fill_kproc2(p, &kproc2);
   2232 				/*
   2233 				 * Copy out elem_size, but not larger than
   2234 				 * the size of a struct kinfo_proc2.
   2235 				 */
   2236 				error = copyout(&kproc2, dp2,
   2237 				    min(sizeof(kproc2), elem_size));
   2238 				if (error)
   2239 					goto cleanup;
   2240 				dp2 += elem_size;
   2241 				buflen -= elem_size;
   2242 				elem_count--;
   2243 			}
   2244 			needed += elem_size;
   2245 		}
   2246 	}
   2247 	pd++;
   2248 	if (pd->pd_list != NULL)
   2249 		goto again;
   2250 	proclist_unlock_read();
   2251 
   2252 	if (where != NULL) {
   2253 		if (type == KERN_PROC)
   2254 			*oldlenp = (char *)dp - where;
   2255 		else
   2256 			*oldlenp = dp2 - where;
   2257 		if (needed > *oldlenp)
   2258 			return (ENOMEM);
   2259 	} else {
   2260 		needed += KERN_PROCSLOP;
   2261 		*oldlenp = needed;
   2262 	}
   2263 	return (0);
   2264  cleanup:
   2265 	proclist_unlock_read();
   2266 	return (error);
   2267 }
   2268 
   2269 /*
   2270  * sysctl helper routine for kern.proc_args pseudo-subtree.
   2271  */
   2272 static int
   2273 sysctl_kern_proc_args(SYSCTLFN_ARGS)
   2274 {
   2275 	struct ps_strings pss;
   2276 	struct proc *p, *up = l->l_proc;
   2277 	size_t len, upper_bound, xlen, i;
   2278 	struct uio auio;
   2279 	struct iovec aiov;
   2280 	vaddr_t argv;
   2281 	pid_t pid;
   2282 	int nargv, type, error;
   2283 	char *arg;
   2284 	char *tmp;
   2285 	struct vmspace *vmspace;
   2286 	vaddr_t psstr_addr;
   2287 	vaddr_t offsetn;
   2288 	vaddr_t offsetv;
   2289 
   2290 	if (namelen == 1 && name[0] == CTL_QUERY)
   2291 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   2292 
   2293 	if (newp != NULL || namelen != 2)
   2294 		return (EINVAL);
   2295 	pid = name[0];
   2296 	type = name[1];
   2297 
   2298 	switch (type) {
   2299 	case KERN_PROC_ARGV:
   2300 	case KERN_PROC_NARGV:
   2301 	case KERN_PROC_ENV:
   2302 	case KERN_PROC_NENV:
   2303 		/* ok */
   2304 		break;
   2305 	default:
   2306 		return (EINVAL);
   2307 	}
   2308 
   2309 	proclist_lock_read();
   2310 
   2311 	/* check pid */
   2312 	if ((p = p_find(pid, PFIND_LOCKED)) == NULL) {
   2313 		error = EINVAL;
   2314 		goto out_locked;
   2315 	}
   2316 
   2317 	if (CURTAIN(l->l_proc->p_ucred->cr_uid, p->p_ucred->cr_uid)) {
   2318 		error = EPERM;
   2319 		goto out_locked;
   2320 	}
   2321 
   2322 	/* only root or same user change look at the environment */
   2323 	if (type == KERN_PROC_ENV || type == KERN_PROC_NENV) {
   2324 		if (up->p_ucred->cr_uid != 0) {
   2325 			if (up->p_cred->p_ruid != p->p_cred->p_ruid ||
   2326 			    up->p_cred->p_ruid != p->p_cred->p_svuid) {
   2327 				error = EPERM;
   2328 				goto out_locked;
   2329 			}
   2330 		}
   2331 	}
   2332 
   2333 	if (oldp == NULL) {
   2334 		if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
   2335 			*oldlenp = sizeof (int);
   2336 		else
   2337 			*oldlenp = ARG_MAX;	/* XXX XXX XXX */
   2338 		error = 0;
   2339 		goto out_locked;
   2340 	}
   2341 
   2342 	/*
   2343 	 * Zombies don't have a stack, so we can't read their psstrings.
   2344 	 * System processes also don't have a user stack.
   2345 	 */
   2346 	if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0) {
   2347 		error = EINVAL;
   2348 		goto out_locked;
   2349 	}
   2350 
   2351 	/*
   2352 	 * Lock the process down in memory.
   2353 	 */
   2354 	/* XXXCDC: how should locking work here? */
   2355 	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1)) {
   2356 		error = EFAULT;
   2357 		goto out_locked;
   2358 	}
   2359 
   2360 	psstr_addr = (vaddr_t)p->p_psstr;
   2361 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV) {
   2362 		offsetn = p->p_psnargv;
   2363 		offsetv = p->p_psargv;
   2364 	} else {
   2365 		offsetn = p->p_psnenv;
   2366 		offsetv = p->p_psenv;
   2367 	}
   2368 	vmspace = p->p_vmspace;
   2369 	vmspace->vm_refcnt++;	/* XXX */
   2370 
   2371 	proclist_unlock_read();
   2372 
   2373 	/*
   2374 	 * Allocate a temporary buffer to hold the arguments.
   2375 	 */
   2376 	arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
   2377 
   2378 	/*
   2379 	 * Read in the ps_strings structure.
   2380 	 */
   2381 	aiov.iov_base = &pss;
   2382 	aiov.iov_len = sizeof(pss);
   2383 	auio.uio_iov = &aiov;
   2384 	auio.uio_iovcnt = 1;
   2385 	auio.uio_offset = psstr_addr;
   2386 	auio.uio_resid = sizeof(pss);
   2387 	auio.uio_segflg = UIO_SYSSPACE;
   2388 	auio.uio_rw = UIO_READ;
   2389 	auio.uio_lwp = NULL;
   2390 	error = uvm_io(&vmspace->vm_map, &auio);
   2391 	if (error)
   2392 		goto done;
   2393 
   2394 	memcpy(&nargv, (char *)&pss + offsetn, sizeof(nargv));
   2395 	if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
   2396 		error = copyout(&nargv, oldp, sizeof(nargv));
   2397 		*oldlenp = sizeof(nargv);
   2398 		goto done;
   2399 	}
   2400 	/*
   2401 	 * Now read the address of the argument vector.
   2402 	 */
   2403 	switch (type) {
   2404 	case KERN_PROC_ARGV:
   2405 		/* XXX compat32 stuff here */
   2406 		/* FALLTHROUGH */
   2407 	case KERN_PROC_ENV:
   2408 		memcpy(&tmp, (char *)&pss + offsetv, sizeof(tmp));
   2409 		break;
   2410 	default:
   2411 		return (EINVAL);
   2412 	}
   2413 	auio.uio_offset = (off_t)(unsigned long)tmp;
   2414 	aiov.iov_base = &argv;
   2415 	aiov.iov_len = sizeof(argv);
   2416 	auio.uio_iov = &aiov;
   2417 	auio.uio_iovcnt = 1;
   2418 	auio.uio_resid = sizeof(argv);
   2419 	auio.uio_segflg = UIO_SYSSPACE;
   2420 	auio.uio_rw = UIO_READ;
   2421 	auio.uio_lwp = NULL;
   2422 	error = uvm_io(&vmspace->vm_map, &auio);
   2423 	if (error)
   2424 		goto done;
   2425 
   2426 	/*
   2427 	 * Now copy in the actual argument vector, one page at a time,
   2428 	 * since we don't know how long the vector is (though, we do
   2429 	 * know how many NUL-terminated strings are in the vector).
   2430 	 */
   2431 	len = 0;
   2432 	upper_bound = *oldlenp;
   2433 	for (; nargv != 0 && len < upper_bound; len += xlen) {
   2434 		aiov.iov_base = arg;
   2435 		aiov.iov_len = PAGE_SIZE;
   2436 		auio.uio_iov = &aiov;
   2437 		auio.uio_iovcnt = 1;
   2438 		auio.uio_offset = argv + len;
   2439 		xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
   2440 		auio.uio_resid = xlen;
   2441 		auio.uio_segflg = UIO_SYSSPACE;
   2442 		auio.uio_rw = UIO_READ;
   2443 		auio.uio_lwp = NULL;
   2444 		error = uvm_io(&vmspace->vm_map, &auio);
   2445 		if (error)
   2446 			goto done;
   2447 
   2448 		for (i = 0; i < xlen && nargv != 0; i++) {
   2449 			if (arg[i] == '\0')
   2450 				nargv--;	/* one full string */
   2451 		}
   2452 
   2453 		/*
   2454 		 * Make sure we don't copyout past the end of the user's
   2455 		 * buffer.
   2456 		 */
   2457 		if (len + i > upper_bound)
   2458 			i = upper_bound - len;
   2459 
   2460 		error = copyout(arg, (char *)oldp + len, i);
   2461 		if (error)
   2462 			break;
   2463 
   2464 		if (nargv == 0) {
   2465 			len += i;
   2466 			break;
   2467 		}
   2468 	}
   2469 	*oldlenp = len;
   2470 
   2471 done:
   2472 	uvmspace_free(vmspace);
   2473 
   2474 	free(arg, M_TEMP);
   2475 	return error;
   2476 
   2477 out_locked:
   2478 	proclist_unlock_read();
   2479 	return error;
   2480 }
   2481 
   2482 /*
   2483  * Sysctl helper routine for Verified Exec.
   2484  */
   2485 #ifdef VERIFIED_EXEC
   2486 static int
   2487 sysctl_kern_veriexec(SYSCTLFN_ARGS)
   2488 {
   2489 	int newval, error;
   2490 	int *var = NULL, raise_only = 0;
   2491 	struct sysctlnode node;
   2492 
   2493 	node = *rnode;
   2494 
   2495 	switch (rnode->sysctl_num) {
   2496 	case VERIEXEC_STRICT:
   2497 		raise_only = 1;
   2498 		var = &veriexec_strict;
   2499 		break;
   2500 	case VERIEXEC_ALGORITHMS:
   2501 		node.sysctl_data = veriexec_fp_names;
   2502 		node.sysctl_size = strlen(veriexec_fp_names) + 1;
   2503 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2504 	default:
   2505 		return (EINVAL);
   2506 	}
   2507 
   2508 	newval = *var;
   2509 
   2510 	node.sysctl_data = &newval;
   2511 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2512 	if (error || newp == NULL) {
   2513 		return (error);
   2514 	}
   2515 
   2516 	if (raise_only && (newval < *var))
   2517 		return (EPERM);
   2518 
   2519 	*var = newval;
   2520 
   2521 	return (error);
   2522 }
   2523 #endif /* VERIFIED_EXEC */
   2524 
   2525 /*
   2526  * sysctl helper routine for kern.cp_id node.  maps cpus to their
   2527  * cpuids.
   2528  */
   2529 static int
   2530 sysctl_kern_cpid(SYSCTLFN_ARGS)
   2531 {
   2532 	struct sysctlnode node = *rnode;
   2533 
   2534 #ifndef MULTIPROCESSOR
   2535 	uint64_t id;
   2536 
   2537 	if (namelen == 1) {
   2538 		if (name[0] != 0)
   2539 			return (ENOENT);
   2540 		/*
   2541 		 * you're allowed to ask for the zero'th processor
   2542 		 */
   2543 		name++;
   2544 		namelen--;
   2545 	}
   2546 	node.sysctl_data = &id;
   2547 	node.sysctl_size = sizeof(id);
   2548 	id = cpu_number();
   2549 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2550 
   2551 #else /* MULTIPROCESSOR */
   2552 	uint64_t *cp_id = NULL;
   2553 	int error, n = sysctl_ncpus();
   2554 	struct cpu_info *ci;
   2555 	CPU_INFO_ITERATOR cii;
   2556 
   2557 	/*
   2558 	 * here you may either retrieve a single cpu id or the whole
   2559 	 * set.  the size you get back when probing depends on what
   2560 	 * you ask for.
   2561 	 */
   2562 	switch (namelen) {
   2563 	case 0:
   2564 		node.sysctl_size = n * sizeof(uint64_t);
   2565 		n = -2; /* ALL */
   2566 		break;
   2567 	case 1:
   2568 		if (name[0] < 0 || name[0] >= n)
   2569 			return (ENOENT); /* ENOSUCHPROCESSOR */
   2570 		node.sysctl_size = sizeof(uint64_t);
   2571 		n = name[0];
   2572 		/*
   2573 		 * adjust these so that sysctl_lookup() will be happy
   2574 		 */
   2575 		name++;
   2576 		namelen--;
   2577 		break;
   2578 	default:
   2579 		return (EINVAL);
   2580 	}
   2581 
   2582 	cp_id = malloc(node.sysctl_size, M_TEMP, M_WAITOK|M_CANFAIL);
   2583 	if (cp_id == NULL)
   2584 		return (ENOMEM);
   2585 	node.sysctl_data = cp_id;
   2586 	memset(cp_id, 0, node.sysctl_size);
   2587 
   2588 	for (CPU_INFO_FOREACH(cii, ci)) {
   2589 		if (n <= 0)
   2590 			cp_id[0] = ci->ci_cpuid;
   2591 		/*
   2592 		 * if a specific processor was requested and we just
   2593 		 * did it, we're done here
   2594 		 */
   2595 		if (n == 0)
   2596 			break;
   2597 		/*
   2598 		 * if doing "all", skip to next cp_id slot for next processor
   2599 		 */
   2600 		if (n == -2)
   2601 			cp_id++;
   2602 		/*
   2603 		 * if we're doing a specific processor, we're one
   2604 		 * processor closer
   2605 		 */
   2606 		if (n > 0)
   2607 			n--;
   2608 	}
   2609 
   2610 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2611 	free(node.sysctl_data, M_TEMP);
   2612 	return (error);
   2613 
   2614 #endif /* MULTIPROCESSOR */
   2615 }
   2616 
   2617 /*
   2618  * sysctl helper routine for hw.usermem and hw.usermem64.  values are
   2619  * calculate on the fly taking into account integer overflow and the
   2620  * current wired count.
   2621  */
   2622 static int
   2623 sysctl_hw_usermem(SYSCTLFN_ARGS)
   2624 {
   2625 	u_int ui;
   2626 	u_quad_t uq;
   2627 	struct sysctlnode node;
   2628 
   2629 	node = *rnode;
   2630 	switch (rnode->sysctl_num) {
   2631 	    case HW_USERMEM:
   2632 		if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
   2633 			ui = UINT_MAX;
   2634 		else
   2635 			ui *= PAGE_SIZE;
   2636 		node.sysctl_data = &ui;
   2637 		break;
   2638 	case HW_USERMEM64:
   2639 		uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
   2640 		node.sysctl_data = &uq;
   2641 		break;
   2642 	default:
   2643 		return (EINVAL);
   2644 	}
   2645 
   2646 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2647 }
   2648 
   2649 /*
   2650  * sysctl helper routine for kern.cnmagic node.  pulls the old value
   2651  * out, encoded, and stuffs the new value in for decoding.
   2652  */
   2653 static int
   2654 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
   2655 {
   2656 	char magic[CNS_LEN];
   2657 	int error;
   2658 	struct sysctlnode node;
   2659 
   2660 	if (oldp)
   2661 		cn_get_magic(magic, CNS_LEN);
   2662 	node = *rnode;
   2663 	node.sysctl_data = &magic[0];
   2664 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2665 	if (error || newp == NULL)
   2666 		return (error);
   2667 
   2668 	return (cn_set_magic(magic));
   2669 }
   2670 
   2671 static int
   2672 sysctl_hw_ncpu(SYSCTLFN_ARGS)
   2673 {
   2674 	int ncpu;
   2675 	struct sysctlnode node;
   2676 
   2677 	ncpu = sysctl_ncpus();
   2678 	node = *rnode;
   2679 	node.sysctl_data = &ncpu;
   2680 
   2681 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2682 }
   2683 
   2684 
   2685 /*
   2686  * ********************************************************************
   2687  * section 3: public helper routines that are used for more than one
   2688  * node
   2689  * ********************************************************************
   2690  */
   2691 
   2692 /*
   2693  * sysctl helper routine for the kern.root_device node and some ports'
   2694  * machdep.root_device nodes.
   2695  */
   2696 int
   2697 sysctl_root_device(SYSCTLFN_ARGS)
   2698 {
   2699 	struct sysctlnode node;
   2700 
   2701 	node = *rnode;
   2702 	node.sysctl_data = root_device->dv_xname;
   2703 	node.sysctl_size = strlen(root_device->dv_xname) + 1;
   2704 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2705 }
   2706 
   2707 /*
   2708  * sysctl helper routine for kern.consdev, dependent on the current
   2709  * state of the console.  also used for machdep.console_device on some
   2710  * ports.
   2711  */
   2712 int
   2713 sysctl_consdev(SYSCTLFN_ARGS)
   2714 {
   2715 	dev_t consdev;
   2716 	struct sysctlnode node;
   2717 
   2718 	if (cn_tab != NULL)
   2719 		consdev = cn_tab->cn_dev;
   2720 	else
   2721 		consdev = NODEV;
   2722 	node = *rnode;
   2723 	node.sysctl_data = &consdev;
   2724 	node.sysctl_size = sizeof(consdev);
   2725 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2726 }
   2727 
   2728 /*
   2729  * ********************************************************************
   2730  * section 4: support for some helpers
   2731  * ********************************************************************
   2732  */
   2733 
   2734 /*
   2735  * Fill in a kinfo_proc2 structure for the specified process.
   2736  */
   2737 static void
   2738 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki)
   2739 {
   2740 	struct tty *tp;
   2741 	struct lwp *l;
   2742 	struct timeval ut, st;
   2743 
   2744 	memset(ki, 0, sizeof(*ki));
   2745 
   2746 	ki->p_paddr = PTRTOUINT64(p);
   2747 	ki->p_fd = PTRTOUINT64(p->p_fd);
   2748 	ki->p_cwdi = PTRTOUINT64(p->p_cwdi);
   2749 	ki->p_stats = PTRTOUINT64(p->p_stats);
   2750 	ki->p_limit = PTRTOUINT64(p->p_limit);
   2751 	ki->p_vmspace = PTRTOUINT64(p->p_vmspace);
   2752 	ki->p_sigacts = PTRTOUINT64(p->p_sigacts);
   2753 	ki->p_sess = PTRTOUINT64(p->p_session);
   2754 	ki->p_tsess = 0;	/* may be changed if controlling tty below */
   2755 	ki->p_ru = PTRTOUINT64(p->p_ru);
   2756 
   2757 	ki->p_eflag = 0;
   2758 	ki->p_exitsig = p->p_exitsig;
   2759 	ki->p_flag = p->p_flag;
   2760 
   2761 	ki->p_pid = p->p_pid;
   2762 	if (p->p_pptr)
   2763 		ki->p_ppid = p->p_pptr->p_pid;
   2764 	else
   2765 		ki->p_ppid = 0;
   2766 	ki->p_sid = p->p_session->s_sid;
   2767 	ki->p__pgid = p->p_pgrp->pg_id;
   2768 
   2769 	ki->p_tpgid = NO_PGID;	/* may be changed if controlling tty below */
   2770 
   2771 	ki->p_uid = p->p_ucred->cr_uid;
   2772 	ki->p_ruid = p->p_cred->p_ruid;
   2773 	ki->p_gid = p->p_ucred->cr_gid;
   2774 	ki->p_rgid = p->p_cred->p_rgid;
   2775 	ki->p_svuid = p->p_cred->p_svuid;
   2776 	ki->p_svgid = p->p_cred->p_svgid;
   2777 
   2778 	memcpy(ki->p_groups, p->p_cred->pc_ucred->cr_groups,
   2779 	    min(sizeof(ki->p_groups), sizeof(p->p_cred->pc_ucred->cr_groups)));
   2780 	ki->p_ngroups = p->p_cred->pc_ucred->cr_ngroups;
   2781 
   2782 	ki->p_jobc = p->p_pgrp->pg_jobc;
   2783 	if ((p->p_flag & P_CONTROLT) && (tp = p->p_session->s_ttyp)) {
   2784 		ki->p_tdev = tp->t_dev;
   2785 		ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   2786 		ki->p_tsess = PTRTOUINT64(tp->t_session);
   2787 	} else {
   2788 		ki->p_tdev = NODEV;
   2789 	}
   2790 
   2791 	ki->p_estcpu = p->p_estcpu;
   2792 	ki->p_rtime_sec = p->p_rtime.tv_sec;
   2793 	ki->p_rtime_usec = p->p_rtime.tv_usec;
   2794 	ki->p_cpticks = p->p_cpticks;
   2795 	ki->p_pctcpu = p->p_pctcpu;
   2796 
   2797 	ki->p_uticks = p->p_uticks;
   2798 	ki->p_sticks = p->p_sticks;
   2799 	ki->p_iticks = p->p_iticks;
   2800 
   2801 	ki->p_tracep = PTRTOUINT64(p->p_tracep);
   2802 	ki->p_traceflag = p->p_traceflag;
   2803 
   2804 
   2805 	memcpy(&ki->p_siglist, &p->p_sigctx.ps_siglist, sizeof(ki_sigset_t));
   2806 	memcpy(&ki->p_sigmask, &p->p_sigctx.ps_sigmask, sizeof(ki_sigset_t));
   2807 	memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
   2808 	memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
   2809 
   2810 	ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
   2811 	ki->p_realstat = p->p_stat;
   2812 	ki->p_nice = p->p_nice;
   2813 
   2814 	ki->p_xstat = p->p_xstat;
   2815 	ki->p_acflag = p->p_acflag;
   2816 
   2817 	strncpy(ki->p_comm, p->p_comm,
   2818 	    min(sizeof(ki->p_comm), sizeof(p->p_comm)));
   2819 
   2820 	strncpy(ki->p_login, p->p_session->s_login,
   2821 	    min(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
   2822 
   2823 	ki->p_nlwps = p->p_nlwps;
   2824 	ki->p_nrlwps = p->p_nrlwps;
   2825 	ki->p_realflag = p->p_flag;
   2826 
   2827 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   2828 		ki->p_vm_rssize = 0;
   2829 		ki->p_vm_tsize = 0;
   2830 		ki->p_vm_dsize = 0;
   2831 		ki->p_vm_ssize = 0;
   2832 		l = NULL;
   2833 	} else {
   2834 		struct vmspace *vm = p->p_vmspace;
   2835 
   2836 		ki->p_vm_rssize = vm_resident_count(vm);
   2837 		ki->p_vm_tsize = vm->vm_tsize;
   2838 		ki->p_vm_dsize = vm->vm_dsize;
   2839 		ki->p_vm_ssize = vm->vm_ssize;
   2840 
   2841 		/* Pick a "representative" LWP */
   2842 		l = proc_representative_lwp(p);
   2843 		ki->p_forw = PTRTOUINT64(l->l_forw);
   2844 		ki->p_back = PTRTOUINT64(l->l_back);
   2845 		ki->p_addr = PTRTOUINT64(l->l_addr);
   2846 		ki->p_stat = l->l_stat;
   2847 		ki->p_flag |= l->l_flag;
   2848 		ki->p_swtime = l->l_swtime;
   2849 		ki->p_slptime = l->l_slptime;
   2850 		if (l->l_stat == LSONPROC) {
   2851 			KDASSERT(l->l_cpu != NULL);
   2852 			ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   2853 		} else
   2854 			ki->p_schedflags = 0;
   2855 		ki->p_holdcnt = l->l_holdcnt;
   2856 		ki->p_priority = l->l_priority;
   2857 		ki->p_usrpri = l->l_usrpri;
   2858 		if (l->l_wmesg)
   2859 			strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
   2860 		ki->p_wchan = PTRTOUINT64(l->l_wchan);
   2861 
   2862 	}
   2863 
   2864 	if (p->p_session->s_ttyvp)
   2865 		ki->p_eflag |= EPROC_CTTY;
   2866 	if (SESS_LEADER(p))
   2867 		ki->p_eflag |= EPROC_SLEADER;
   2868 
   2869 	/* XXX Is this double check necessary? */
   2870 	if (P_ZOMBIE(p)) {
   2871 		ki->p_uvalid = 0;
   2872 	} else {
   2873 		ki->p_uvalid = 1;
   2874 
   2875 		ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
   2876 		ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
   2877 
   2878 		calcru(p, &ut, &st, 0);
   2879 		ki->p_uutime_sec = ut.tv_sec;
   2880 		ki->p_uutime_usec = ut.tv_usec;
   2881 		ki->p_ustime_sec = st.tv_sec;
   2882 		ki->p_ustime_usec = st.tv_usec;
   2883 
   2884 		ki->p_uru_maxrss = p->p_stats->p_ru.ru_maxrss;
   2885 		ki->p_uru_ixrss = p->p_stats->p_ru.ru_ixrss;
   2886 		ki->p_uru_idrss = p->p_stats->p_ru.ru_idrss;
   2887 		ki->p_uru_isrss = p->p_stats->p_ru.ru_isrss;
   2888 		ki->p_uru_minflt = p->p_stats->p_ru.ru_minflt;
   2889 		ki->p_uru_majflt = p->p_stats->p_ru.ru_majflt;
   2890 		ki->p_uru_nswap = p->p_stats->p_ru.ru_nswap;
   2891 		ki->p_uru_inblock = p->p_stats->p_ru.ru_inblock;
   2892 		ki->p_uru_oublock = p->p_stats->p_ru.ru_oublock;
   2893 		ki->p_uru_msgsnd = p->p_stats->p_ru.ru_msgsnd;
   2894 		ki->p_uru_msgrcv = p->p_stats->p_ru.ru_msgrcv;
   2895 		ki->p_uru_nsignals = p->p_stats->p_ru.ru_nsignals;
   2896 		ki->p_uru_nvcsw = p->p_stats->p_ru.ru_nvcsw;
   2897 		ki->p_uru_nivcsw = p->p_stats->p_ru.ru_nivcsw;
   2898 
   2899 		timeradd(&p->p_stats->p_cru.ru_utime,
   2900 			 &p->p_stats->p_cru.ru_stime, &ut);
   2901 		ki->p_uctime_sec = ut.tv_sec;
   2902 		ki->p_uctime_usec = ut.tv_usec;
   2903 	}
   2904 #ifdef MULTIPROCESSOR
   2905 	if (l && l->l_cpu != NULL)
   2906 		ki->p_cpuid = l->l_cpu->ci_cpuid;
   2907 	else
   2908 #endif
   2909 		ki->p_cpuid = KI_NOCPU;
   2910 }
   2911 
   2912 /*
   2913  * Fill in a kinfo_lwp structure for the specified lwp.
   2914  */
   2915 static void
   2916 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
   2917 {
   2918 
   2919 	kl->l_forw = PTRTOUINT64(l->l_forw);
   2920 	kl->l_back = PTRTOUINT64(l->l_back);
   2921 	kl->l_laddr = PTRTOUINT64(l);
   2922 	kl->l_addr = PTRTOUINT64(l->l_addr);
   2923 	kl->l_stat = l->l_stat;
   2924 	kl->l_lid = l->l_lid;
   2925 	kl->l_flag = l->l_flag;
   2926 
   2927 	kl->l_swtime = l->l_swtime;
   2928 	kl->l_slptime = l->l_slptime;
   2929 	if (l->l_stat == LSONPROC) {
   2930 		KDASSERT(l->l_cpu != NULL);
   2931 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   2932 	} else
   2933 		kl->l_schedflags = 0;
   2934 	kl->l_holdcnt = l->l_holdcnt;
   2935 	kl->l_priority = l->l_priority;
   2936 	kl->l_usrpri = l->l_usrpri;
   2937 	if (l->l_wmesg)
   2938 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
   2939 	kl->l_wchan = PTRTOUINT64(l->l_wchan);
   2940 #ifdef MULTIPROCESSOR
   2941 	if (l->l_cpu != NULL)
   2942 		kl->l_cpuid = l->l_cpu->ci_cpuid;
   2943 	else
   2944 #endif
   2945 		kl->l_cpuid = KI_NOCPU;
   2946 }
   2947 
   2948 /*
   2949  * Fill in an eproc structure for the specified process.
   2950  */
   2951 void
   2952 fill_eproc(struct proc *p, struct eproc *ep)
   2953 {
   2954 	struct tty *tp;
   2955 	struct lwp *l;
   2956 
   2957 	ep->e_paddr = p;
   2958 	ep->e_sess = p->p_session;
   2959 	ep->e_pcred = *p->p_cred;
   2960 	ep->e_ucred = *p->p_ucred;
   2961 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   2962 		ep->e_vm.vm_rssize = 0;
   2963 		ep->e_vm.vm_tsize = 0;
   2964 		ep->e_vm.vm_dsize = 0;
   2965 		ep->e_vm.vm_ssize = 0;
   2966 		/* ep->e_vm.vm_pmap = XXX; */
   2967 	} else {
   2968 		struct vmspace *vm = p->p_vmspace;
   2969 
   2970 		ep->e_vm.vm_rssize = vm_resident_count(vm);
   2971 		ep->e_vm.vm_tsize = vm->vm_tsize;
   2972 		ep->e_vm.vm_dsize = vm->vm_dsize;
   2973 		ep->e_vm.vm_ssize = vm->vm_ssize;
   2974 
   2975 		/* Pick a "representative" LWP */
   2976 		l = proc_representative_lwp(p);
   2977 
   2978 		if (l->l_wmesg)
   2979 			strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
   2980 	}
   2981 	if (p->p_pptr)
   2982 		ep->e_ppid = p->p_pptr->p_pid;
   2983 	else
   2984 		ep->e_ppid = 0;
   2985 	ep->e_pgid = p->p_pgrp->pg_id;
   2986 	ep->e_sid = ep->e_sess->s_sid;
   2987 	ep->e_jobc = p->p_pgrp->pg_jobc;
   2988 	if ((p->p_flag & P_CONTROLT) &&
   2989 	    (tp = ep->e_sess->s_ttyp)) {
   2990 		ep->e_tdev = tp->t_dev;
   2991 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   2992 		ep->e_tsess = tp->t_session;
   2993 	} else
   2994 		ep->e_tdev = NODEV;
   2995 
   2996 	ep->e_xsize = ep->e_xrssize = 0;
   2997 	ep->e_xccount = ep->e_xswrss = 0;
   2998 	ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
   2999 	if (SESS_LEADER(p))
   3000 		ep->e_flag |= EPROC_SLEADER;
   3001 	strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
   3002 }
   3003