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