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