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