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