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