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