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