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