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