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