Home | History | Annotate | Line # | Download | only in kern
init_sysctl.c revision 1.25
      1 /*	$NetBSD: init_sysctl.c,v 1.25 2004/04/08 03:35:10 atatat 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.25 2004/04/08 03:35:10 atatat Exp $");
     41 
     42 #include "opt_sysv.h"
     43 #include "opt_multiprocessor.h"
     44 #include "opt_posix.h"
     45 #include "pty.h"
     46 #include "rnd.h"
     47 
     48 #include <sys/types.h>
     49 #include <sys/param.h>
     50 #include <sys/sysctl.h>
     51 #include <sys/errno.h>
     52 #include <sys/systm.h>
     53 #include <sys/kernel.h>
     54 #include <sys/unistd.h>
     55 #include <sys/disklabel.h>
     56 #include <sys/rnd.h>
     57 #include <sys/vnode.h>
     58 #include <sys/mount.h>
     59 #include <sys/namei.h>
     60 #include <sys/msgbuf.h>
     61 #include <dev/cons.h>
     62 #include <sys/socketvar.h>
     63 #include <sys/file.h>
     64 #include <sys/tty.h>
     65 #include <sys/malloc.h>
     66 #include <sys/resource.h>
     67 #include <sys/resourcevar.h>
     68 #include <sys/exec.h>
     69 #include <sys/conf.h>
     70 #include <sys/device.h>
     71 
     72 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
     73 #include <sys/ipc.h>
     74 #endif
     75 #ifdef SYSVMSG
     76 #include <sys/msg.h>
     77 #endif
     78 #ifdef SYSVSEM
     79 #include <sys/sem.h>
     80 #endif
     81 #ifdef SYSVSHM
     82 #include <sys/shm.h>
     83 #endif
     84 
     85 #include <machine/cpu.h>
     86 
     87 /*
     88  * try over estimating by 5 procs/lwps
     89  */
     90 #define KERN_PROCSLOP	(5 * sizeof(struct kinfo_proc))
     91 #define KERN_LWPSLOP	(5 * sizeof(struct kinfo_lwp))
     92 
     93 #ifndef MULTIPROCESSOR
     94 #define	sysctl_ncpus()	(1)
     95 #else /* MULTIPROCESSOR */
     96 #ifndef CPU_INFO_FOREACH
     97 #define CPU_INFO_ITERATOR int
     98 #define CPU_INFO_FOREACH(cii, ci) cii = 0, ci = curcpu(); ci != NULL; ci = NULL
     99 #endif
    100 static int
    101 sysctl_ncpus(void)
    102 {
    103 	struct cpu_info *ci;
    104 	CPU_INFO_ITERATOR cii;
    105 
    106 	int ncpus = 0;
    107 	for (CPU_INFO_FOREACH(cii, ci))
    108 		ncpus++;
    109 	return (ncpus);
    110 }
    111 #endif /* MULTIPROCESSOR */
    112 
    113 static int sysctl_kern_maxvnodes(SYSCTLFN_PROTO);
    114 static int sysctl_kern_rtc_offset(SYSCTLFN_PROTO);
    115 static int sysctl_kern_maxproc(SYSCTLFN_PROTO);
    116 static int sysctl_kern_securelevel(SYSCTLFN_PROTO);
    117 static int sysctl_kern_hostid(SYSCTLFN_PROTO);
    118 static int sysctl_setlen(SYSCTLFN_PROTO);
    119 static int sysctl_kern_clockrate(SYSCTLFN_PROTO);
    120 static int sysctl_kern_file(SYSCTLFN_PROTO);
    121 static int sysctl_kern_autonice(SYSCTLFN_PROTO);
    122 static int sysctl_msgbuf(SYSCTLFN_PROTO);
    123 static int sysctl_kern_defcorename(SYSCTLFN_PROTO);
    124 static int sysctl_kern_cptime(SYSCTLFN_PROTO);
    125 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
    126 static int sysctl_kern_sysvipc(SYSCTLFN_PROTO);
    127 #endif /* defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM) */
    128 #if NPTY > 0
    129 static int sysctl_kern_maxptys(SYSCTLFN_PROTO);
    130 #endif /* NPTY > 0 */
    131 static int sysctl_kern_sbmax(SYSCTLFN_PROTO);
    132 static int sysctl_kern_urnd(SYSCTLFN_PROTO);
    133 static int sysctl_kern_lwp(SYSCTLFN_PROTO);
    134 static int sysctl_kern_forkfsleep(SYSCTLFN_PROTO);
    135 static int sysctl_kern_root_partition(SYSCTLFN_PROTO);
    136 static int sysctl_kern_drivers(SYSCTLFN_PROTO);
    137 static int sysctl_doeproc(SYSCTLFN_PROTO);
    138 static int sysctl_kern_proc_args(SYSCTLFN_PROTO);
    139 static int sysctl_hw_usermem(SYSCTLFN_PROTO);
    140 static int sysctl_hw_cnmagic(SYSCTLFN_PROTO);
    141 static int sysctl_hw_ncpu(SYSCTLFN_PROTO);
    142 
    143 static void fill_kproc2(struct proc *, struct kinfo_proc2 *);
    144 static void fill_lwp(struct lwp *l, struct kinfo_lwp *kl);
    145 
    146 /*
    147  * ********************************************************************
    148  * section 1: setup routines
    149  * ********************************************************************
    150  * these functions are stuffed into a link set for sysctl setup
    151  * functions.  they're never called or referenced from anywhere else.
    152  * ********************************************************************
    153  */
    154 
    155 /*
    156  * sets up the base nodes...
    157  */
    158 SYSCTL_SETUP(sysctl_root_setup, "sysctl base setup")
    159 {
    160 
    161 	sysctl_createv(clog, 0, NULL, NULL,
    162 		       CTLFLAG_PERMANENT,
    163 		       CTLTYPE_NODE, "kern", NULL,
    164 		       NULL, 0, NULL, 0,
    165 		       CTL_KERN, CTL_EOL);
    166 	sysctl_createv(clog, 0, NULL, NULL,
    167 		       CTLFLAG_PERMANENT,
    168 		       CTLTYPE_NODE, "vm", NULL,
    169 		       NULL, 0, NULL, 0,
    170 		       CTL_VM, CTL_EOL);
    171 	sysctl_createv(clog, 0, NULL, NULL,
    172 		       CTLFLAG_PERMANENT,
    173 		       CTLTYPE_NODE, "vfs", NULL,
    174 		       NULL, 0, NULL, 0,
    175 		       CTL_VFS, CTL_EOL);
    176 	sysctl_createv(clog, 0, NULL, NULL,
    177 		       CTLFLAG_PERMANENT,
    178 		       CTLTYPE_NODE, "net", NULL,
    179 		       NULL, 0, NULL, 0,
    180 		       CTL_NET, CTL_EOL);
    181 	sysctl_createv(clog, 0, NULL, NULL,
    182 		       CTLFLAG_PERMANENT,
    183 		       CTLTYPE_NODE, "debug", NULL,
    184 		       NULL, 0, NULL, 0,
    185 		       CTL_DEBUG, CTL_EOL);
    186 	sysctl_createv(clog, 0, NULL, NULL,
    187 		       CTLFLAG_PERMANENT,
    188 		       CTLTYPE_NODE, "hw", NULL,
    189 		       NULL, 0, NULL, 0,
    190 		       CTL_HW, CTL_EOL);
    191 	sysctl_createv(clog, 0, NULL, NULL,
    192 		       CTLFLAG_PERMANENT,
    193 		       CTLTYPE_NODE, "machdep", NULL,
    194 		       NULL, 0, NULL, 0,
    195 		       CTL_MACHDEP, CTL_EOL);
    196 	/*
    197 	 * this node is inserted so that the sysctl nodes in libc can
    198 	 * operate.
    199 	 */
    200 	sysctl_createv(clog, 0, NULL, NULL,
    201 		       CTLFLAG_PERMANENT,
    202 		       CTLTYPE_NODE, "user", NULL,
    203 		       NULL, 0, NULL, 0,
    204 		       CTL_USER, CTL_EOL);
    205 	sysctl_createv(clog, 0, NULL, NULL,
    206 		       CTLFLAG_PERMANENT,
    207 		       CTLTYPE_NODE, "ddb", NULL,
    208 		       NULL, 0, NULL, 0,
    209 		       CTL_DDB, CTL_EOL);
    210 	sysctl_createv(clog, 0, NULL, NULL,
    211 		       CTLFLAG_PERMANENT,
    212 		       CTLTYPE_NODE, "proc", NULL,
    213 		       NULL, 0, NULL, 0,
    214 		       CTL_PROC, CTL_EOL);
    215 	sysctl_createv(clog, 0, NULL, NULL,
    216 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    217 		       CTLTYPE_NODE, "vendor", NULL,
    218 		       NULL, 0, NULL, 0,
    219 		       CTL_VENDOR, CTL_EOL);
    220 	sysctl_createv(clog, 0, NULL, NULL,
    221 		       CTLFLAG_PERMANENT,
    222 		       CTLTYPE_NODE, "emul", NULL,
    223 		       NULL, 0, NULL, 0,
    224 		       CTL_EMUL, CTL_EOL);
    225 }
    226 
    227 /*
    228  * this setup routine is a replacement for kern_sysctl()
    229  */
    230 SYSCTL_SETUP(sysctl_kern_setup, "sysctl kern subtree setup")
    231 {
    232 	extern int kern_logsigexit;	/* defined in kern/kern_sig.c */
    233 	extern fixpt_t ccpu;		/* defined in kern/kern_synch.c */
    234 	extern int dumponpanic;		/* defined in kern/subr_prf.c */
    235 
    236 	sysctl_createv(clog, 0, NULL, NULL,
    237 		       CTLFLAG_PERMANENT,
    238 		       CTLTYPE_NODE, "kern", NULL,
    239 		       NULL, 0, NULL, 0,
    240 		       CTL_KERN, CTL_EOL);
    241 
    242 	sysctl_createv(clog, 0, NULL, NULL,
    243 		       CTLFLAG_PERMANENT,
    244 		       CTLTYPE_STRING, "ostype", NULL,
    245 		       NULL, 0, &ostype, 0,
    246 		       CTL_KERN, KERN_OSTYPE, CTL_EOL);
    247 	sysctl_createv(clog, 0, NULL, NULL,
    248 		       CTLFLAG_PERMANENT,
    249 		       CTLTYPE_STRING, "osrelease", NULL,
    250 		       NULL, 0, &osrelease, 0,
    251 		       CTL_KERN, KERN_OSRELEASE, CTL_EOL);
    252 	sysctl_createv(clog, 0, NULL, NULL,
    253 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    254 		       CTLTYPE_INT, "osrevision", NULL,
    255 		       NULL, __NetBSD_Version__, NULL, 0,
    256 		       CTL_KERN, KERN_OSREV, CTL_EOL);
    257 	sysctl_createv(clog, 0, NULL, NULL,
    258 		       CTLFLAG_PERMANENT,
    259 		       CTLTYPE_STRING, "version", NULL,
    260 		       NULL, 0, &version, 0,
    261 		       CTL_KERN, KERN_VERSION, CTL_EOL);
    262 	sysctl_createv(clog, 0, NULL, NULL,
    263 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    264 		       CTLTYPE_INT, "maxvnodes", NULL,
    265 		       sysctl_kern_maxvnodes, 0, NULL, 0,
    266 		       CTL_KERN, KERN_MAXVNODES, CTL_EOL);
    267 	sysctl_createv(clog, 0, NULL, NULL,
    268 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    269 		       CTLTYPE_INT, "maxproc", NULL,
    270 		       sysctl_kern_maxproc, 0, NULL, 0,
    271 		       CTL_KERN, KERN_MAXPROC, CTL_EOL);
    272 	sysctl_createv(clog, 0, NULL, NULL,
    273 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    274 		       CTLTYPE_INT, "maxfiles", NULL,
    275 		       NULL, 0, &maxfiles, 0,
    276 		       CTL_KERN, KERN_MAXFILES, CTL_EOL);
    277 	sysctl_createv(clog, 0, NULL, NULL,
    278 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    279 		       CTLTYPE_INT, "argmax", NULL,
    280 		       NULL, ARG_MAX, NULL, 0,
    281 		       CTL_KERN, KERN_ARGMAX, CTL_EOL);
    282 	sysctl_createv(clog, 0, NULL, NULL,
    283 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    284 		       CTLTYPE_INT, "securelevel", NULL,
    285 		       sysctl_kern_securelevel, 0, &securelevel, 0,
    286 		       CTL_KERN, KERN_SECURELVL, CTL_EOL);
    287 	sysctl_createv(clog, 0, NULL, NULL,
    288 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    289 		       CTLTYPE_STRING, "hostname", NULL,
    290 		       sysctl_setlen, 0, &hostname, MAXHOSTNAMELEN,
    291 		       CTL_KERN, KERN_HOSTNAME, CTL_EOL);
    292 	sysctl_createv(clog, 0, NULL, NULL,
    293 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    294 		       CTLTYPE_INT, "hostid", NULL,
    295 		       sysctl_kern_hostid, 0, NULL, 0,
    296 		       CTL_KERN, KERN_HOSTID, CTL_EOL);
    297 	sysctl_createv(clog, 0, NULL, NULL,
    298 		       CTLFLAG_PERMANENT,
    299 		       CTLTYPE_STRUCT, "clockrate", NULL,
    300 		       sysctl_kern_clockrate, 0, NULL,
    301 		       sizeof(struct clockinfo),
    302 		       CTL_KERN, KERN_CLOCKRATE, CTL_EOL);
    303 	sysctl_createv(clog, 0, NULL, NULL,
    304 		       CTLFLAG_PERMANENT,
    305 		       CTLTYPE_STRUCT, "vnode", NULL,
    306 		       sysctl_kern_vnode, 0, NULL, 0,
    307 		       CTL_KERN, KERN_VNODE, CTL_EOL);
    308 	sysctl_createv(clog, 0, NULL, NULL,
    309 		       CTLFLAG_PERMANENT,
    310 		       CTLTYPE_STRUCT, "file", NULL,
    311 		       sysctl_kern_file, 0, NULL, 0,
    312 		       CTL_KERN, KERN_FILE, CTL_EOL);
    313 #ifndef GPROF
    314 	sysctl_createv(clog, 0, NULL, NULL,
    315 		       CTLFLAG_PERMANENT,
    316 		       CTLTYPE_NODE, "profiling", NULL,
    317 		       sysctl_notavail, 0, NULL, 0,
    318 		       CTL_KERN, KERN_PROF, CTL_EOL);
    319 #endif
    320 	sysctl_createv(clog, 0, NULL, NULL,
    321 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    322 		       CTLTYPE_INT, "posix1version", NULL,
    323 		       NULL, _POSIX_VERSION, NULL, 0,
    324 		       CTL_KERN, KERN_POSIX1, CTL_EOL);
    325 	sysctl_createv(clog, 0, NULL, NULL,
    326 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    327 		       CTLTYPE_INT, "ngroups", NULL,
    328 		       NULL, NGROUPS_MAX, NULL, 0,
    329 		       CTL_KERN, KERN_NGROUPS, CTL_EOL);
    330 	sysctl_createv(clog, 0, NULL, NULL,
    331 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    332 		       CTLTYPE_INT, "job_control", NULL,
    333 		       NULL, 1, NULL, 0,
    334 		       CTL_KERN, KERN_JOB_CONTROL, CTL_EOL);
    335 	sysctl_createv(clog, 0, NULL, NULL,
    336 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    337 		       CTLTYPE_INT, "saved_ids", NULL, NULL,
    338 #ifdef _POSIX_SAVED_IDS
    339 		       1,
    340 #else /* _POSIX_SAVED_IDS */
    341 		       0,
    342 #endif /* _POSIX_SAVED_IDS */
    343 		       NULL, 0, CTL_KERN, KERN_SAVED_IDS, CTL_EOL);
    344 	sysctl_createv(clog, 0, NULL, NULL,
    345 		       CTLFLAG_PERMANENT,
    346 		       CTLTYPE_STRUCT, "boottime", NULL,
    347 		       NULL, 0, &boottime, sizeof(boottime),
    348 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
    349 	sysctl_createv(clog, 0, NULL, NULL,
    350 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    351 		       CTLTYPE_STRING, "domainname", NULL,
    352 		       sysctl_setlen, 0, &domainname, MAXHOSTNAMELEN,
    353 		       CTL_KERN, KERN_DOMAINNAME, CTL_EOL);
    354 	sysctl_createv(clog, 0, NULL, NULL,
    355 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    356 		       CTLTYPE_INT, "maxpartitions", NULL,
    357 		       NULL, MAXPARTITIONS, NULL, 0,
    358 		       CTL_KERN, KERN_MAXPARTITIONS, CTL_EOL);
    359 	sysctl_createv(clog, 0, NULL, NULL,
    360 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    361 		       CTLTYPE_INT, "rawpartition", NULL,
    362 		       NULL, RAW_PART, NULL, 0,
    363 		       CTL_KERN, KERN_RAWPARTITION, CTL_EOL);
    364 	sysctl_createv(clog, 0, NULL, NULL,
    365 		       CTLFLAG_PERMANENT,
    366 		       CTLTYPE_STRUCT, "timex", NULL,
    367 		       sysctl_notavail, 0, NULL, 0,
    368 		       CTL_KERN, KERN_TIMEX, CTL_EOL);
    369 	sysctl_createv(clog, 0, NULL, NULL,
    370 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    371 		       CTLTYPE_INT, "autonicetime", NULL,
    372 		       sysctl_kern_autonice, 0, &autonicetime, 0,
    373 		       CTL_KERN, KERN_AUTONICETIME, CTL_EOL);
    374 	sysctl_createv(clog, 0, NULL, NULL,
    375 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    376 		       CTLTYPE_INT, "autoniceval", NULL,
    377 		       sysctl_kern_autonice, 0, &autoniceval, 0,
    378 		       CTL_KERN, KERN_AUTONICEVAL, CTL_EOL);
    379 	sysctl_createv(clog, 0, NULL, NULL,
    380 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    381 		       CTLTYPE_INT, "rtc_offset", NULL,
    382 		       sysctl_kern_rtc_offset, 0, &rtc_offset, 0,
    383 		       CTL_KERN, KERN_RTC_OFFSET, CTL_EOL);
    384 	sysctl_createv(clog, 0, NULL, NULL,
    385 		       CTLFLAG_PERMANENT,
    386 		       CTLTYPE_STRING, "root_device", NULL,
    387 		       sysctl_root_device, 0, NULL, 0,
    388 		       CTL_KERN, KERN_ROOT_DEVICE, CTL_EOL);
    389 	sysctl_createv(clog, 0, NULL, NULL,
    390 		       CTLFLAG_PERMANENT,
    391 		       CTLTYPE_INT, "msgbufsize", NULL,
    392 		       sysctl_msgbuf, 0, &msgbufp->msg_bufs, 0,
    393 		       CTL_KERN, KERN_MSGBUFSIZE, CTL_EOL);
    394 	sysctl_createv(clog, 0, NULL, NULL,
    395 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    396 		       CTLTYPE_INT, "fsync", NULL,
    397 		       NULL, 1, NULL, 0,
    398 		       CTL_KERN, KERN_FSYNC, CTL_EOL);
    399 	sysctl_createv(clog, 0, NULL, NULL,
    400 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    401 		       CTLTYPE_INT, "sysvmsg", NULL, NULL,
    402 #ifdef SYSVMSG
    403 		       1,
    404 #else /* SYSVMSG */
    405 		       0,
    406 #endif /* SYSVMSG */
    407 		       NULL, 0, CTL_KERN, KERN_SYSVMSG, CTL_EOL);
    408 	sysctl_createv(clog, 0, NULL, NULL,
    409 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    410 		       CTLTYPE_INT, "sysvsem", NULL, NULL,
    411 #ifdef SYSVSEM
    412 		       1,
    413 #else /* SYSVSEM */
    414 		       0,
    415 #endif /* SYSVSEM */
    416 		       NULL, 0, CTL_KERN, KERN_SYSVSEM, CTL_EOL);
    417 	sysctl_createv(clog, 0, NULL, NULL,
    418 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    419 		       CTLTYPE_INT, "sysvshm", NULL, NULL,
    420 #ifdef SYSVSHM
    421 		       1,
    422 #else /* SYSVSHM */
    423 		       0,
    424 #endif /* SYSVSHM */
    425 		       NULL, 0, CTL_KERN, KERN_SYSVSHM, CTL_EOL);
    426 	sysctl_createv(clog, 0, NULL, NULL,
    427 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    428 		       CTLTYPE_INT, "synchronized_io", NULL,
    429 		       NULL, 1, NULL, 0,
    430 		       CTL_KERN, KERN_SYNCHRONIZED_IO, CTL_EOL);
    431 	sysctl_createv(clog, 0, NULL, NULL,
    432 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    433 		       CTLTYPE_INT, "iov_max", NULL,
    434 		       NULL, IOV_MAX, NULL, 0,
    435 		       CTL_KERN, KERN_IOV_MAX, CTL_EOL);
    436 	sysctl_createv(clog, 0, NULL, NULL,
    437 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    438 		       CTLTYPE_INT, "mapped_files", NULL,
    439 		       NULL, 1, NULL, 0,
    440 		       CTL_KERN, KERN_MAPPED_FILES, CTL_EOL);
    441 	sysctl_createv(clog, 0, NULL, NULL,
    442 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    443 		       CTLTYPE_INT, "memlock", NULL,
    444 		       NULL, 1, NULL, 0,
    445 		       CTL_KERN, KERN_MEMLOCK, CTL_EOL);
    446 	sysctl_createv(clog, 0, NULL, NULL,
    447 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    448 		       CTLTYPE_INT, "memlock_range", NULL,
    449 		       NULL, 1, NULL, 0,
    450 		       CTL_KERN, KERN_MEMLOCK_RANGE, CTL_EOL);
    451 	sysctl_createv(clog, 0, NULL, NULL,
    452 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    453 		       CTLTYPE_INT, "memory_protection", NULL,
    454 		       NULL, 1, NULL, 0,
    455 		       CTL_KERN, KERN_MEMORY_PROTECTION, CTL_EOL);
    456 	sysctl_createv(clog, 0, NULL, NULL,
    457 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    458 		       CTLTYPE_INT, "login_name_max", NULL,
    459 		       NULL, LOGIN_NAME_MAX, NULL, 0,
    460 		       CTL_KERN, KERN_LOGIN_NAME_MAX, CTL_EOL);
    461 	sysctl_createv(clog, 0, NULL, NULL,
    462 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    463 		       CTLTYPE_STRING, "defcorename", NULL,
    464 		       sysctl_kern_defcorename, 0, defcorename, MAXPATHLEN,
    465 		       CTL_KERN, KERN_DEFCORENAME, CTL_EOL);
    466 	sysctl_createv(clog, 0, NULL, NULL,
    467 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    468 		       CTLTYPE_INT, "logsigexit", NULL,
    469 		       NULL, 0, &kern_logsigexit, 0,
    470 		       CTL_KERN, KERN_LOGSIGEXIT, CTL_EOL);
    471 	sysctl_createv(clog, 0, NULL, NULL,
    472 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    473 		       CTLTYPE_INT, "fscale", NULL,
    474 		       NULL, FSCALE, NULL, 0,
    475 		       CTL_KERN, KERN_FSCALE, CTL_EOL);
    476 	sysctl_createv(clog, 0, NULL, NULL,
    477 		       CTLFLAG_PERMANENT,
    478 		       CTLTYPE_INT, "ccpu", NULL,
    479 		       NULL, 0, &ccpu, 0,
    480 		       CTL_KERN, KERN_CCPU, CTL_EOL);
    481 	sysctl_createv(clog, 0, NULL, NULL,
    482 		       CTLFLAG_PERMANENT,
    483 		       CTLTYPE_STRUCT, "cp_time", NULL,
    484 		       sysctl_kern_cptime, 0, NULL, 0,
    485 		       CTL_KERN, KERN_CP_TIME, CTL_EOL);
    486 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
    487 	sysctl_createv(clog, 0, NULL, NULL,
    488 		       CTLFLAG_PERMANENT,
    489 		       CTLTYPE_STRUCT, "sysvipc_info", NULL,
    490 		       sysctl_kern_sysvipc, 0, NULL, 0,
    491 		       CTL_KERN, KERN_SYSVIPC_INFO, CTL_EOL);
    492 #endif /* SYSVMSG || SYSVSEM || SYSVSHM */
    493 	sysctl_createv(clog, 0, NULL, NULL,
    494 		       CTLFLAG_PERMANENT,
    495 		       CTLTYPE_INT, "msgbuf", NULL,
    496 		       sysctl_msgbuf, 0, NULL, 0,
    497 		       CTL_KERN, KERN_MSGBUF, CTL_EOL);
    498 	sysctl_createv(clog, 0, NULL, NULL,
    499 		       CTLFLAG_PERMANENT,
    500 		       CTLTYPE_STRUCT, "consdev", NULL,
    501 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
    502 		       CTL_KERN, KERN_CONSDEV, CTL_EOL);
    503 #if NPTY > 0
    504 	sysctl_createv(clog, 0, NULL, NULL,
    505 		       CTLFLAG_PERMANENT,
    506 		       CTLTYPE_INT, "maxptys", NULL,
    507 		       sysctl_kern_maxptys, 0, NULL, 0,
    508 		       CTL_KERN, KERN_MAXPTYS, CTL_EOL);
    509 #endif /* NPTY > 0 */
    510 	sysctl_createv(clog, 0, NULL, NULL,
    511 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    512 		       CTLTYPE_INT, "maxphys", NULL,
    513 		       NULL, MAXPHYS, NULL, 0,
    514 		       CTL_KERN, KERN_MAXPHYS, CTL_EOL);
    515 	sysctl_createv(clog, 0, NULL, NULL,
    516 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    517 		       CTLTYPE_INT, "sbmax", NULL,
    518 		       sysctl_kern_sbmax, 0, NULL, 0,
    519 		       CTL_KERN, KERN_SBMAX, CTL_EOL);
    520 	sysctl_createv(clog, 0, NULL, NULL,
    521 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    522 		       CTLTYPE_INT, "monotonic_clock", NULL,
    523 		       /* XXX _POSIX_VERSION */
    524 		       NULL, _POSIX_MONOTONIC_CLOCK, NULL, 0,
    525 		       CTL_KERN, KERN_MONOTONIC_CLOCK, CTL_EOL);
    526 	sysctl_createv(clog, 0, NULL, NULL,
    527 		       CTLFLAG_PERMANENT,
    528 		       CTLTYPE_INT, "urandom", NULL,
    529 		       sysctl_kern_urnd, 0, NULL, 0,
    530 		       CTL_KERN, KERN_URND, CTL_EOL);
    531 	sysctl_createv(clog, 0, NULL, NULL,
    532 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    533 		       CTLTYPE_INT, "labelsector", NULL,
    534 		       NULL, LABELSECTOR, NULL, 0,
    535 		       CTL_KERN, KERN_LABELSECTOR, CTL_EOL);
    536 	sysctl_createv(clog, 0, NULL, NULL,
    537 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    538 		       CTLTYPE_INT, "labeloffset", NULL,
    539 		       NULL, LABELOFFSET, NULL, 0,
    540 		       CTL_KERN, KERN_LABELOFFSET, CTL_EOL);
    541 	sysctl_createv(clog, 0, NULL, NULL,
    542 		       CTLFLAG_PERMANENT,
    543 		       CTLTYPE_NODE, "lwp", NULL,
    544 		       sysctl_kern_lwp, 0, NULL, 0,
    545 		       CTL_KERN, KERN_LWP, CTL_EOL);
    546 	sysctl_createv(clog, 0, NULL, NULL,
    547 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    548 		       CTLTYPE_INT, "forkfsleep", NULL,
    549 		       sysctl_kern_forkfsleep, 0, NULL, 0,
    550 		       CTL_KERN, KERN_FORKFSLEEP, CTL_EOL);
    551 	sysctl_createv(clog, 0, NULL, NULL,
    552 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    553 		       CTLTYPE_INT, "posix_threads", NULL,
    554 		       /* XXX _POSIX_VERSION */
    555 		       NULL, _POSIX_THREADS, NULL, 0,
    556 		       CTL_KERN, KERN_POSIX_THREADS, CTL_EOL);
    557 	sysctl_createv(clog, 0, NULL, NULL,
    558 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    559 		       CTLTYPE_INT, "posix_semaphores", NULL, NULL,
    560 #ifdef P1003_1B_SEMAPHORE
    561 		       200112,
    562 #else /* P1003_1B_SEMAPHORE */
    563 		       0,
    564 #endif /* P1003_1B_SEMAPHORE */
    565 		       NULL, 0, CTL_KERN, KERN_POSIX_SEMAPHORES, CTL_EOL);
    566 	sysctl_createv(clog, 0, NULL, NULL,
    567 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    568 		       CTLTYPE_INT, "posix_barriers", NULL,
    569 		       /* XXX _POSIX_VERSION */
    570 		       NULL, _POSIX_BARRIERS, NULL, 0,
    571 		       CTL_KERN, KERN_POSIX_BARRIERS, CTL_EOL);
    572 	sysctl_createv(clog, 0, NULL, NULL,
    573 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    574 		       CTLTYPE_INT, "posix_timers", NULL,
    575 		       /* XXX _POSIX_VERSION */
    576 		       NULL, _POSIX_TIMERS, NULL, 0,
    577 		       CTL_KERN, KERN_POSIX_TIMERS, CTL_EOL);
    578 	sysctl_createv(clog, 0, NULL, NULL,
    579 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    580 		       CTLTYPE_INT, "posix_spin_locks", NULL,
    581 		       /* XXX _POSIX_VERSION */
    582 		       NULL, _POSIX_SPIN_LOCKS, NULL, 0,
    583 		       CTL_KERN, KERN_POSIX_SPIN_LOCKS, CTL_EOL);
    584 	sysctl_createv(clog, 0, NULL, NULL,
    585 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    586 		       CTLTYPE_INT, "posix_reader_writer_locks", NULL,
    587 		       /* XXX _POSIX_VERSION */
    588 		       NULL, _POSIX_READER_WRITER_LOCKS, NULL, 0,
    589 		       CTL_KERN, KERN_POSIX_READER_WRITER_LOCKS, CTL_EOL);
    590 	sysctl_createv(clog, 0, NULL, NULL,
    591 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    592 		       CTLTYPE_INT, "dump_on_panic", NULL,
    593 		       NULL, 0, &dumponpanic, 0,
    594 		       CTL_KERN, KERN_DUMP_ON_PANIC, CTL_EOL);
    595 	sysctl_createv(clog, 0, NULL, NULL,
    596 		       CTLFLAG_PERMANENT,
    597 		       CTLTYPE_INT, "root_partition", NULL,
    598 		       sysctl_kern_root_partition, 0, NULL, 0,
    599 		       CTL_KERN, KERN_ROOT_PARTITION, CTL_EOL);
    600 	sysctl_createv(clog, 0, NULL, NULL,
    601 		       CTLFLAG_PERMANENT,
    602 		       CTLTYPE_STRUCT, "drivers", NULL,
    603 		       sysctl_kern_drivers, 0, NULL, 0,
    604 		       CTL_KERN, KERN_DRIVERS, CTL_EOL);
    605 }
    606 
    607 SYSCTL_SETUP(sysctl_kern_proc_setup,
    608 	     "sysctl kern.proc/proc2/proc_args subtree setup")
    609 {
    610 
    611 	sysctl_createv(clog, 0, NULL, NULL,
    612 		       CTLFLAG_PERMANENT,
    613 		       CTLTYPE_NODE, "kern", NULL,
    614 		       NULL, 0, NULL, 0,
    615 		       CTL_KERN, CTL_EOL);
    616 
    617 	sysctl_createv(clog, 0, NULL, NULL,
    618 		       CTLFLAG_PERMANENT,
    619 		       CTLTYPE_NODE, "proc", NULL,
    620 		       sysctl_doeproc, 0, NULL, 0,
    621 		       CTL_KERN, KERN_PROC, CTL_EOL);
    622 	sysctl_createv(clog, 0, NULL, NULL,
    623 		       CTLFLAG_PERMANENT,
    624 		       CTLTYPE_NODE, "proc2", NULL,
    625 		       sysctl_doeproc, 0, NULL, 0,
    626 		       CTL_KERN, KERN_PROC2, CTL_EOL);
    627 	sysctl_createv(clog, 0, NULL, NULL,
    628 		       CTLFLAG_PERMANENT,
    629 		       CTLTYPE_NODE, "proc_args", NULL,
    630 		       sysctl_kern_proc_args, 0, NULL, 0,
    631 		       CTL_KERN, KERN_PROC_ARGS, CTL_EOL);
    632 
    633 	/*
    634 	  "nodes" under these:
    635 
    636 	  KERN_PROC_ALL
    637 	  KERN_PROC_PID pid
    638 	  KERN_PROC_PGRP pgrp
    639 	  KERN_PROC_SESSION sess
    640 	  KERN_PROC_TTY tty
    641 	  KERN_PROC_UID uid
    642 	  KERN_PROC_RUID uid
    643 	  KERN_PROC_GID gid
    644 	  KERN_PROC_RGID gid
    645 
    646 	  all in all, probably not worth the effort...
    647 	*/
    648 }
    649 
    650 SYSCTL_SETUP(sysctl_hw_setup, "sysctl hw subtree setup")
    651 {
    652 	u_int u;
    653 	u_quad_t q;
    654 
    655 	sysctl_createv(clog, 0, NULL, NULL,
    656 		       CTLFLAG_PERMANENT,
    657 		       CTLTYPE_NODE, "hw", NULL,
    658 		       NULL, 0, NULL, 0,
    659 		       CTL_HW, CTL_EOL);
    660 
    661 	sysctl_createv(clog, 0, NULL, NULL,
    662 		       CTLFLAG_PERMANENT,
    663 		       CTLTYPE_STRING, "machine", NULL,
    664 		       NULL, 0, machine, 0,
    665 		       CTL_HW, HW_MACHINE, CTL_EOL);
    666 	sysctl_createv(clog, 0, NULL, NULL,
    667 		       CTLFLAG_PERMANENT,
    668 		       CTLTYPE_STRING, "model", NULL,
    669 		       NULL, 0, cpu_model, 0,
    670 		       CTL_HW, HW_MODEL, CTL_EOL);
    671 	sysctl_createv(clog, 0, NULL, NULL,
    672 		       CTLFLAG_PERMANENT,
    673 		       CTLTYPE_INT, "ncpu", NULL,
    674 		       sysctl_hw_ncpu, 0, NULL, 0,
    675 		       CTL_HW, HW_NCPU, CTL_EOL);
    676 	sysctl_createv(clog, 0, NULL, NULL,
    677 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    678 		       CTLTYPE_INT, "byteorder", NULL,
    679 		       NULL, BYTE_ORDER, NULL, 0,
    680 		       CTL_HW, HW_BYTEORDER, CTL_EOL);
    681 	u = ((u_int)physmem > (UINT_MAX / PAGE_SIZE)) ?
    682 		UINT_MAX : physmem * PAGE_SIZE;
    683 	sysctl_createv(clog, 0, NULL, NULL,
    684 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    685 		       CTLTYPE_INT, "physmem", NULL,
    686 		       NULL, u, NULL, 0,
    687 		       CTL_HW, HW_PHYSMEM, CTL_EOL);
    688 	sysctl_createv(clog, 0, NULL, NULL,
    689 		       CTLFLAG_PERMANENT,
    690 		       CTLTYPE_INT, "usermem", NULL,
    691 		       sysctl_hw_usermem, 0, NULL, 0,
    692 		       CTL_HW, HW_USERMEM, CTL_EOL);
    693 	sysctl_createv(clog, 0, NULL, NULL,
    694 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    695 		       CTLTYPE_INT, "pagesize", NULL,
    696 		       NULL, PAGE_SIZE, NULL, 0,
    697 		       CTL_HW, HW_PAGESIZE, CTL_EOL);
    698 	sysctl_createv(clog, 0, NULL, NULL,
    699 		       CTLFLAG_PERMANENT,
    700 		       CTLTYPE_STRING, "disknames", NULL,
    701 		       sysctl_hw_disknames, 0, NULL, 0,
    702 		       CTL_HW, HW_DISKNAMES, CTL_EOL);
    703 	sysctl_createv(clog, 0, NULL, NULL,
    704 		       CTLFLAG_PERMANENT,
    705 		       CTLTYPE_STRUCT, "diskstats", NULL,
    706 		       sysctl_hw_diskstats, 0, NULL, 0,
    707 		       CTL_HW, HW_DISKSTATS, CTL_EOL);
    708 	sysctl_createv(clog, 0, NULL, NULL,
    709 		       CTLFLAG_PERMANENT,
    710 		       CTLTYPE_STRING, "machine_arch", NULL,
    711 		       NULL, 0, machine_arch, 0,
    712 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
    713 	sysctl_createv(clog, 0, NULL, NULL,
    714 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    715 		       CTLTYPE_INT, "alignbytes", NULL,
    716 		       NULL, ALIGNBYTES, NULL, 0,
    717 		       CTL_HW, HW_ALIGNBYTES, CTL_EOL);
    718 	sysctl_createv(clog, 0, NULL, NULL,
    719 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
    720 		       CTLTYPE_STRING, "cnmagic", NULL,
    721 		       sysctl_hw_cnmagic, 0, NULL, CNS_LEN,
    722 		       CTL_HW, HW_CNMAGIC, CTL_EOL);
    723 	q = (u_quad_t)physmem * PAGE_SIZE;
    724 	sysctl_createv(clog, 0, NULL, NULL,
    725 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    726 		       CTLTYPE_QUAD, "physmem64", NULL,
    727 		       NULL, q, NULL, 0,
    728 		       CTL_HW, HW_PHYSMEM64, CTL_EOL);
    729 	sysctl_createv(clog, 0, NULL, NULL,
    730 		       CTLFLAG_PERMANENT,
    731 		       CTLTYPE_QUAD, "usermem64", NULL,
    732 		       sysctl_hw_usermem, 0, NULL, 0,
    733 		       CTL_HW, HW_USERMEM64, CTL_EOL);
    734 }
    735 
    736 #ifdef DEBUG
    737 /*
    738  * Debugging related system variables.
    739  */
    740 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
    741 struct ctldebug debug5, debug6, debug7, debug8, debug9;
    742 struct ctldebug debug10, debug11, debug12, debug13, debug14;
    743 struct ctldebug debug15, debug16, debug17, debug18, debug19;
    744 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
    745 	&debug0, &debug1, &debug2, &debug3, &debug4,
    746 	&debug5, &debug6, &debug7, &debug8, &debug9,
    747 	&debug10, &debug11, &debug12, &debug13, &debug14,
    748 	&debug15, &debug16, &debug17, &debug18, &debug19,
    749 };
    750 
    751 /*
    752  * this setup routine is a replacement for debug_sysctl()
    753  *
    754  * note that it creates several nodes per defined debug variable
    755  */
    756 SYSCTL_SETUP(sysctl_debug_setup, "sysctl debug subtree setup")
    757 {
    758 	struct ctldebug *cdp;
    759 	char nodename[20];
    760 	int i;
    761 
    762 	/*
    763 	 * two ways here:
    764 	 *
    765 	 * the "old" way (debug.name -> value) which was emulated by
    766 	 * the sysctl(8) binary
    767 	 *
    768 	 * the new way, which the sysctl(8) binary was actually using
    769 
    770 	 node	debug
    771 	 node	debug.0
    772 	 string	debug.0.name
    773 	 int	debug.0.value
    774 	 int	debug.name
    775 
    776 	 */
    777 
    778 	sysctl_createv(clog, 0, NULL, NULL,
    779 		       CTLFLAG_PERMANENT,
    780 		       CTLTYPE_NODE, "debug", NULL,
    781 		       NULL, 0, NULL, 0,
    782 		       CTL_DEBUG, CTL_EOL);
    783 
    784 	for (i = 0; i < CTL_DEBUG_MAXID; i++) {
    785 		cdp = debugvars[i];
    786 		if (cdp->debugname == NULL || cdp->debugvar == NULL)
    787 			continue;
    788 
    789 		snprintf(nodename, sizeof(nodename), "debug%d", i);
    790 		sysctl_createv(clog, 0, NULL, NULL,
    791 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
    792 			       CTLTYPE_NODE, nodename, NULL,
    793 			       NULL, 0, NULL, 0,
    794 			       CTL_DEBUG, i, CTL_EOL);
    795 		sysctl_createv(clog, 0, NULL, NULL,
    796 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
    797 			       CTLTYPE_STRING, "name", NULL,
    798 			       NULL, 0, cdp->debugname, 0,
    799 			       CTL_DEBUG, i, CTL_DEBUG_NAME, CTL_EOL);
    800 		sysctl_createv(clog, 0, NULL, NULL,
    801 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
    802 			       CTLTYPE_INT, "value", NULL,
    803 			       NULL, 0, cdp->debugvar, 0,
    804 			       CTL_DEBUG, i, CTL_DEBUG_VALUE, CTL_EOL);
    805 		sysctl_createv(clog, 0, NULL, NULL,
    806 			       CTLFLAG_PERMANENT,
    807 			       CTLTYPE_INT, cdp->debugname, NULL,
    808 			       NULL, 0, cdp->debugvar, 0,
    809 			       CTL_DEBUG, CTL_CREATE, CTL_EOL);
    810 	}
    811 }
    812 #endif /* DEBUG */
    813 
    814 /*
    815  * ********************************************************************
    816  * section 2: private node-specific helper routines.
    817  * ********************************************************************
    818  */
    819 
    820 /*
    821  * sysctl helper routine for kern.maxvnodes.  drain vnodes if
    822  * new value is lower than desiredvnodes and then calls reinit
    823  * routines that needs to adjust to the new value.
    824  */
    825 static int
    826 sysctl_kern_maxvnodes(SYSCTLFN_ARGS)
    827 {
    828 	int error, new_vnodes, old_vnodes;
    829 	struct sysctlnode node;
    830 
    831 	new_vnodes = desiredvnodes;
    832 	node = *rnode;
    833 	node.sysctl_data = &new_vnodes;
    834 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    835 	if (error || newp == NULL)
    836 		return (error);
    837 
    838 	old_vnodes = desiredvnodes;
    839 	desiredvnodes = new_vnodes;
    840 	if (new_vnodes < old_vnodes) {
    841 		error = vfs_drainvnodes(new_vnodes, l->l_proc);
    842 		if (error) {
    843 			desiredvnodes = old_vnodes;
    844 			return (error);
    845 		}
    846 	}
    847 	vfs_reinit();
    848 	nchreinit();
    849 
    850 	return (0);
    851 }
    852 
    853 /*
    854  * sysctl helper routine for rtc_offset - set time after changes
    855  */
    856 static int
    857 sysctl_kern_rtc_offset(SYSCTLFN_ARGS)
    858 {
    859 	struct timeval tv, delta;
    860 	int s, error, new_rtc_offset;
    861 	struct sysctlnode node;
    862 
    863 	new_rtc_offset = rtc_offset;
    864 	node = *rnode;
    865 	node.sysctl_data = &new_rtc_offset;
    866 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    867 	if (error || newp == NULL)
    868 		return (error);
    869 
    870 	if (securelevel > 0)
    871 		return (EPERM);
    872 	if (rtc_offset == new_rtc_offset)
    873 		return (0);
    874 
    875 	/* if we change the offset, adjust the time */
    876 	s = splclock();
    877 	tv = time;
    878 	splx(s);
    879 	delta.tv_sec = 60*(new_rtc_offset - rtc_offset);
    880 	delta.tv_usec = 0;
    881 	timeradd(&tv, &delta, &tv);
    882 	rtc_offset = new_rtc_offset;
    883 	settime(&tv);
    884 
    885 	return (0);
    886 }
    887 
    888 /*
    889  * sysctl helper routine for kern.maxvnodes.  ensures that the new
    890  * values are not too low or too high.
    891  */
    892 static int
    893 sysctl_kern_maxproc(SYSCTLFN_ARGS)
    894 {
    895 	int error, nmaxproc;
    896 	struct sysctlnode node;
    897 
    898 	nmaxproc = maxproc;
    899 	node = *rnode;
    900 	node.sysctl_data = &nmaxproc;
    901 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    902 	if (error || newp == NULL)
    903 		return (error);
    904 
    905 	if (nmaxproc < 0 || nmaxproc >= PID_MAX)
    906 		return (EINVAL);
    907 #ifdef __HAVE_CPU_MAXPROC
    908 	if (nmaxproc > cpu_maxproc())
    909 		return (EINVAL);
    910 #endif
    911 	maxproc = nmaxproc;
    912 
    913 	return (0);
    914 }
    915 
    916 /*
    917  * sysctl helper routine for kern.securelevel.  ensures that the value
    918  * only rises unless the caller has pid 1 (assumed to be init).
    919  */
    920 static int
    921 sysctl_kern_securelevel(SYSCTLFN_ARGS)
    922 {
    923 	int newsecurelevel, error;
    924 	struct sysctlnode node;
    925 
    926 	newsecurelevel = securelevel;
    927 	node = *rnode;
    928 	node.sysctl_data = &newsecurelevel;
    929 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    930 	if (error || newp == NULL)
    931 		return (error);
    932 
    933 	if (newsecurelevel < securelevel && l && l->l_proc->p_pid != 1)
    934 		return (EPERM);
    935 	securelevel = newsecurelevel;
    936 
    937 	return (error);
    938 }
    939 
    940 /*
    941  * sysctl helper function for kern.hostid.  the hostid is a long, but
    942  * we export it as an int, so we need to give it a little help.
    943  */
    944 static int
    945 sysctl_kern_hostid(SYSCTLFN_ARGS)
    946 {
    947 	int error, inthostid;
    948 	struct sysctlnode node;
    949 
    950 	inthostid = hostid;  /* XXX assumes sizeof int >= sizeof long */
    951 	node = *rnode;
    952 	node.sysctl_data = &inthostid;
    953 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    954 	if (error || newp == NULL)
    955 		return (error);
    956 
    957 	hostid = inthostid;
    958 
    959 	return (0);
    960 }
    961 
    962 /*
    963  * sysctl helper function for kern.hostname and kern.domainnname.
    964  * resets the relevant recorded length when the underlying name is
    965  * changed.
    966  */
    967 static int
    968 sysctl_setlen(SYSCTLFN_ARGS)
    969 {
    970 	int error;
    971 
    972 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
    973 	if (error || newp == NULL)
    974 		return (error);
    975 
    976 	switch (rnode->sysctl_num) {
    977 	case KERN_HOSTNAME:
    978 		hostnamelen = strlen((const char*)rnode->sysctl_data);
    979 		break;
    980 	case KERN_DOMAINNAME:
    981 		domainnamelen = strlen((const char*)rnode->sysctl_data);
    982 		break;
    983 	}
    984 
    985 	return (0);
    986 }
    987 
    988 /*
    989  * sysctl helper routine for kern.clockrate.  assembles a struct on
    990  * the fly to be returned to the caller.
    991  */
    992 static int
    993 sysctl_kern_clockrate(SYSCTLFN_ARGS)
    994 {
    995 	struct clockinfo clkinfo;
    996 	struct sysctlnode node;
    997 
    998 	clkinfo.tick = tick;
    999 	clkinfo.tickadj = tickadj;
   1000 	clkinfo.hz = hz;
   1001 	clkinfo.profhz = profhz;
   1002 	clkinfo.stathz = stathz ? stathz : hz;
   1003 
   1004 	node = *rnode;
   1005 	node.sysctl_data = &clkinfo;
   1006 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1007 }
   1008 
   1009 
   1010 /*
   1011  * sysctl helper routine for kern.file pseudo-subtree.
   1012  */
   1013 static int
   1014 sysctl_kern_file(SYSCTLFN_ARGS)
   1015 {
   1016 	int error;
   1017 	size_t buflen;
   1018 	struct file *fp;
   1019 	char *start, *where;
   1020 
   1021 	start = where = oldp;
   1022 	buflen = *oldlenp;
   1023 	if (where == NULL) {
   1024 		/*
   1025 		 * overestimate by 10 files
   1026 		 */
   1027 		*oldlenp = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
   1028 		return (0);
   1029 	}
   1030 
   1031 	/*
   1032 	 * first copyout filehead
   1033 	 */
   1034 	if (buflen < sizeof(filehead)) {
   1035 		*oldlenp = 0;
   1036 		return (0);
   1037 	}
   1038 	error = copyout(&filehead, where, sizeof(filehead));
   1039 	if (error)
   1040 		return (error);
   1041 	buflen -= sizeof(filehead);
   1042 	where += sizeof(filehead);
   1043 
   1044 	/*
   1045 	 * followed by an array of file structures
   1046 	 */
   1047 	LIST_FOREACH(fp, &filehead, f_list) {
   1048 		if (buflen < sizeof(struct file)) {
   1049 			*oldlenp = where - start;
   1050 			return (ENOMEM);
   1051 		}
   1052 		error = copyout(fp, where, sizeof(struct file));
   1053 		if (error)
   1054 			return (error);
   1055 		buflen -= sizeof(struct file);
   1056 		where += sizeof(struct file);
   1057 	}
   1058 	*oldlenp = where - start;
   1059 	return (0);
   1060 }
   1061 
   1062 /*
   1063  * sysctl helper routine for kern.autonicetime and kern.autoniceval.
   1064  * asserts that the assigned value is in the correct range.
   1065  */
   1066 static int
   1067 sysctl_kern_autonice(SYSCTLFN_ARGS)
   1068 {
   1069 	int error, t = 0;
   1070 	struct sysctlnode node;
   1071 
   1072 	node = *rnode;
   1073 	t = *(int*)node.sysctl_data;
   1074 	node.sysctl_data = &t;
   1075 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1076 	if (error || newp == NULL)
   1077 		return (error);
   1078 
   1079 	switch (node.sysctl_num) {
   1080 	case KERN_AUTONICETIME:
   1081 		if (t >= 0)
   1082 			autonicetime = t;
   1083 		break;
   1084 	case KERN_AUTONICEVAL:
   1085 		if (t < PRIO_MIN)
   1086 			t = PRIO_MIN;
   1087 		else if (t > PRIO_MAX)
   1088 			t = PRIO_MAX;
   1089 		autoniceval = t;
   1090 		break;
   1091 	}
   1092 
   1093 	return (0);
   1094 }
   1095 
   1096 /*
   1097  * sysctl helper routine for kern.msgbufsize and kern.msgbuf.  for the
   1098  * former it merely checks the the message buffer is set up.  for the
   1099  * latter, it also copies out the data if necessary.
   1100  */
   1101 static int
   1102 sysctl_msgbuf(SYSCTLFN_ARGS)
   1103 {
   1104 	char *where = oldp;
   1105 	size_t len, maxlen;
   1106 	long beg, end;
   1107 	int error;
   1108 
   1109 	if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
   1110 		msgbufenabled = 0;
   1111 		return (ENXIO);
   1112 	}
   1113 
   1114 	switch (rnode->sysctl_num) {
   1115 	case KERN_MSGBUFSIZE:
   1116 		return (sysctl_lookup(SYSCTLFN_CALL(rnode)));
   1117 	case KERN_MSGBUF:
   1118 		break;
   1119 	default:
   1120 		return (EOPNOTSUPP);
   1121 	}
   1122 
   1123 	if (newp != NULL)
   1124 		return (EPERM);
   1125 
   1126         if (oldp == NULL) {
   1127 		/* always return full buffer size */
   1128 		*oldlenp = msgbufp->msg_bufs;
   1129 		return (0);
   1130         }
   1131 
   1132 	error = 0;
   1133 	maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
   1134 
   1135 	/*
   1136 	 * First, copy from the write pointer to the end of
   1137 	 * message buffer.
   1138 	 */
   1139 	beg = msgbufp->msg_bufx;
   1140 	end = msgbufp->msg_bufs;
   1141 	while (maxlen > 0) {
   1142 		len = MIN(end - beg, maxlen);
   1143 		if (len == 0)
   1144 			break;
   1145 		error = copyout(&msgbufp->msg_bufc[beg], where, len);
   1146 		if (error)
   1147 			break;
   1148 		where += len;
   1149 		maxlen -= len;
   1150 
   1151 		/*
   1152 		 * ... then, copy from the beginning of message buffer to
   1153 		 * the write pointer.
   1154 		 */
   1155 		beg = 0;
   1156 		end = msgbufp->msg_bufx;
   1157 	}
   1158 
   1159 	return (error);
   1160 }
   1161 
   1162 /*
   1163  * sysctl helper routine for kern.defcorename.  in the case of a new
   1164  * string being assigned, check that it's not a zero-length string.
   1165  * (XXX the check in -current doesn't work, but do we really care?)
   1166  */
   1167 static int
   1168 sysctl_kern_defcorename(SYSCTLFN_ARGS)
   1169 {
   1170 	int error;
   1171 	char newcorename[MAXPATHLEN];
   1172 	struct sysctlnode node;
   1173 
   1174 	node = *rnode;
   1175 	node.sysctl_data = &newcorename[0];
   1176 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
   1177 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1178 	if (error || newp == NULL)
   1179 		return (error);
   1180 
   1181 	/*
   1182 	 * when sysctl_lookup() deals with a string, it's guaranteed
   1183 	 * to come back nul terminated.  so there.  :)
   1184 	 */
   1185 	if (strlen(newcorename) == 0)
   1186 		return (EINVAL);
   1187 
   1188 	memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
   1189 
   1190 	return (0);
   1191 }
   1192 
   1193 /*
   1194  * sysctl helper routine for kern.cp_time node.  adds up cpu time
   1195  * across all cpus.
   1196  */
   1197 static int
   1198 sysctl_kern_cptime(SYSCTLFN_ARGS)
   1199 {
   1200 	struct sysctlnode node = *rnode;
   1201 
   1202 #ifndef MULTIPROCESSOR
   1203 
   1204 	if (namelen == 1) {
   1205 		if (name[0] != 0)
   1206 			return (ENOENT);
   1207 		/*
   1208 		 * you're allowed to ask for the zero'th processor
   1209 		 */
   1210 		name++;
   1211 		namelen--;
   1212 	}
   1213 	node.sysctl_data = curcpu()->ci_schedstate.spc_cp_time;
   1214 	node.sysctl_size = sizeof(curcpu()->ci_schedstate.spc_cp_time);
   1215 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1216 
   1217 #else /* MULTIPROCESSOR */
   1218 
   1219 	u_int64_t *cp_time = NULL;
   1220 	int error, n = sysctl_ncpus(), i;
   1221 	struct cpu_info *ci;
   1222 	CPU_INFO_ITERATOR cii;
   1223 
   1224 	/*
   1225 	 * if you specifically pass a buffer that is the size of the
   1226 	 * sum, or if you are probing for the size, you get the "sum"
   1227 	 * of cp_time (and the size thereof) across all processors.
   1228 	 *
   1229 	 * alternately, you can pass an additional mib number and get
   1230 	 * cp_time for that particular processor.
   1231 	 */
   1232 	switch (namelen) {
   1233 	case 0:
   1234 	    	if (*oldlenp == sizeof(u_int64_t) * CPUSTATES || oldp == NULL) {
   1235 			node.sysctl_size = sizeof(u_int64_t) * CPUSTATES;
   1236 			n = -1; /* SUM */
   1237 		}
   1238 		else {
   1239 			node.sysctl_size = n * sizeof(u_int64_t) * CPUSTATES;
   1240 			n = -2; /* ALL */
   1241 		}
   1242 		break;
   1243 	case 1:
   1244 		if (name[0] < 0 || name[0] >= n)
   1245 			return (ENOENT); /* ENOSUCHPROCESSOR */
   1246 		node.sysctl_size = sizeof(u_int64_t) * CPUSTATES;
   1247 		n = name[0];
   1248 		/*
   1249 		 * adjust these so that sysctl_lookup() will be happy
   1250 		 */
   1251 		name++;
   1252 		namelen--;
   1253 		break;
   1254 	default:
   1255 		return (EINVAL);
   1256 	}
   1257 
   1258 	cp_time = malloc(node.sysctl_size, M_TEMP, M_WAITOK|M_CANFAIL);
   1259 	if (cp_time == NULL)
   1260 		return (ENOMEM);
   1261 	node.sysctl_data = cp_time;
   1262 	memset(cp_time, 0, node.sysctl_size);
   1263 
   1264 	for (CPU_INFO_FOREACH(cii, ci)) {
   1265 		if (n <= 0)
   1266 			for (i = 0; i < CPUSTATES; i++)
   1267 				cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
   1268 		/*
   1269 		 * if a specific processor was requested and we just
   1270 		 * did it, we're done here
   1271 		 */
   1272 		if (n == 0)
   1273 			break;
   1274 		/*
   1275 		 * if doing "all", skip to next cp_time set for next processor
   1276 		 */
   1277 		if (n == -2)
   1278 			cp_time += CPUSTATES;
   1279 		/*
   1280 		 * if we're doing a specific processor, we're one
   1281 		 * processor closer
   1282 		 */
   1283 		if (n > 0)
   1284 			n--;
   1285 	}
   1286 
   1287 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1288 	free(node.sysctl_data, M_TEMP);
   1289 	return (error);
   1290 
   1291 #endif /* MULTIPROCESSOR */
   1292 }
   1293 
   1294 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
   1295 /*
   1296  * sysctl helper routine for kern.sysvipc_info subtree.
   1297  */
   1298 
   1299 #define	FILL_PERM(src, dst) do { \
   1300 	(dst)._key = (src)._key; \
   1301 	(dst).uid = (src).uid; \
   1302 	(dst).gid = (src).gid; \
   1303 	(dst).cuid = (src).cuid; \
   1304 	(dst).cgid = (src).cgid; \
   1305 	(dst).mode = (src).mode; \
   1306 	(dst)._seq = (src)._seq; \
   1307 } while (/*CONSTCOND*/ 0);
   1308 #define	FILL_MSG(src, dst) do { \
   1309 	FILL_PERM((src).msg_perm, (dst).msg_perm); \
   1310 	(dst).msg_qnum = (src).msg_qnum; \
   1311 	(dst).msg_qbytes = (src).msg_qbytes; \
   1312 	(dst)._msg_cbytes = (src)._msg_cbytes; \
   1313 	(dst).msg_lspid = (src).msg_lspid; \
   1314 	(dst).msg_lrpid = (src).msg_lrpid; \
   1315 	(dst).msg_stime = (src).msg_stime; \
   1316 	(dst).msg_rtime = (src).msg_rtime; \
   1317 	(dst).msg_ctime = (src).msg_ctime; \
   1318 } while (/*CONSTCOND*/ 0)
   1319 #define	FILL_SEM(src, dst) do { \
   1320 	FILL_PERM((src).sem_perm, (dst).sem_perm); \
   1321 	(dst).sem_nsems = (src).sem_nsems; \
   1322 	(dst).sem_otime = (src).sem_otime; \
   1323 	(dst).sem_ctime = (src).sem_ctime; \
   1324 } while (/*CONSTCOND*/ 0)
   1325 #define	FILL_SHM(src, dst) do { \
   1326 	FILL_PERM((src).shm_perm, (dst).shm_perm); \
   1327 	(dst).shm_segsz = (src).shm_segsz; \
   1328 	(dst).shm_lpid = (src).shm_lpid; \
   1329 	(dst).shm_cpid = (src).shm_cpid; \
   1330 	(dst).shm_atime = (src).shm_atime; \
   1331 	(dst).shm_dtime = (src).shm_dtime; \
   1332 	(dst).shm_ctime = (src).shm_ctime; \
   1333 	(dst).shm_nattch = (src).shm_nattch; \
   1334 } while (/*CONSTCOND*/ 0)
   1335 
   1336 static int
   1337 sysctl_kern_sysvipc(SYSCTLFN_ARGS)
   1338 {
   1339 	void *where = oldp;
   1340 	size_t *sizep = oldlenp;
   1341 #ifdef SYSVMSG
   1342 	struct msg_sysctl_info *msgsi = NULL;
   1343 #endif
   1344 #ifdef SYSVSEM
   1345 	struct sem_sysctl_info *semsi = NULL;
   1346 #endif
   1347 #ifdef SYSVSHM
   1348 	struct shm_sysctl_info *shmsi = NULL;
   1349 #endif
   1350 	size_t infosize, dssize, tsize, buflen;
   1351 	void *buf = NULL;
   1352 	char *start;
   1353 	int32_t nds;
   1354 	int i, error, ret;
   1355 
   1356 	if (namelen != 1)
   1357 		return (EINVAL);
   1358 
   1359 	start = where;
   1360 	buflen = *sizep;
   1361 
   1362 	switch (*name) {
   1363 	case KERN_SYSVIPC_MSG_INFO:
   1364 #ifdef SYSVMSG
   1365 		infosize = sizeof(msgsi->msginfo);
   1366 		nds = msginfo.msgmni;
   1367 		dssize = sizeof(msgsi->msgids[0]);
   1368 		break;
   1369 #else
   1370 		return (EINVAL);
   1371 #endif
   1372 	case KERN_SYSVIPC_SEM_INFO:
   1373 #ifdef SYSVSEM
   1374 		infosize = sizeof(semsi->seminfo);
   1375 		nds = seminfo.semmni;
   1376 		dssize = sizeof(semsi->semids[0]);
   1377 		break;
   1378 #else
   1379 		return (EINVAL);
   1380 #endif
   1381 	case KERN_SYSVIPC_SHM_INFO:
   1382 #ifdef SYSVSHM
   1383 		infosize = sizeof(shmsi->shminfo);
   1384 		nds = shminfo.shmmni;
   1385 		dssize = sizeof(shmsi->shmids[0]);
   1386 		break;
   1387 #else
   1388 		return (EINVAL);
   1389 #endif
   1390 	default:
   1391 		return (EINVAL);
   1392 	}
   1393 	/*
   1394 	 * Round infosize to 64 bit boundary if requesting more than just
   1395 	 * the info structure or getting the total data size.
   1396 	 */
   1397 	if (where == NULL || *sizep > infosize)
   1398 		infosize = ((infosize + 7) / 8) * 8;
   1399 	tsize = infosize + nds * dssize;
   1400 
   1401 	/* Return just the total size required. */
   1402 	if (where == NULL) {
   1403 		*sizep = tsize;
   1404 		return (0);
   1405 	}
   1406 
   1407 	/* Not enough room for even the info struct. */
   1408 	if (buflen < infosize) {
   1409 		*sizep = 0;
   1410 		return (ENOMEM);
   1411 	}
   1412 	buf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK);
   1413 	memset(buf, 0, min(tsize, buflen));
   1414 
   1415 	switch (*name) {
   1416 #ifdef SYSVMSG
   1417 	case KERN_SYSVIPC_MSG_INFO:
   1418 		msgsi = (struct msg_sysctl_info *)buf;
   1419 		msgsi->msginfo = msginfo;
   1420 		break;
   1421 #endif
   1422 #ifdef SYSVSEM
   1423 	case KERN_SYSVIPC_SEM_INFO:
   1424 		semsi = (struct sem_sysctl_info *)buf;
   1425 		semsi->seminfo = seminfo;
   1426 		break;
   1427 #endif
   1428 #ifdef SYSVSHM
   1429 	case KERN_SYSVIPC_SHM_INFO:
   1430 		shmsi = (struct shm_sysctl_info *)buf;
   1431 		shmsi->shminfo = shminfo;
   1432 		break;
   1433 #endif
   1434 	}
   1435 	buflen -= infosize;
   1436 
   1437 	ret = 0;
   1438 	if (buflen > 0) {
   1439 		/* Fill in the IPC data structures.  */
   1440 		for (i = 0; i < nds; i++) {
   1441 			if (buflen < dssize) {
   1442 				ret = ENOMEM;
   1443 				break;
   1444 			}
   1445 			switch (*name) {
   1446 #ifdef SYSVMSG
   1447 			case KERN_SYSVIPC_MSG_INFO:
   1448 				FILL_MSG(msqids[i], msgsi->msgids[i]);
   1449 				break;
   1450 #endif
   1451 #ifdef SYSVSEM
   1452 			case KERN_SYSVIPC_SEM_INFO:
   1453 				FILL_SEM(sema[i], semsi->semids[i]);
   1454 				break;
   1455 #endif
   1456 #ifdef SYSVSHM
   1457 			case KERN_SYSVIPC_SHM_INFO:
   1458 				FILL_SHM(shmsegs[i], shmsi->shmids[i]);
   1459 				break;
   1460 #endif
   1461 			}
   1462 			buflen -= dssize;
   1463 		}
   1464 	}
   1465 	*sizep -= buflen;
   1466 	error = copyout(buf, start, *sizep);
   1467 	/* If copyout succeeded, use return code set earlier. */
   1468 	if (error == 0)
   1469 		error = ret;
   1470 	if (buf)
   1471 		free(buf, M_TEMP);
   1472 	return (error);
   1473 }
   1474 
   1475 #undef FILL_PERM
   1476 #undef FILL_MSG
   1477 #undef FILL_SEM
   1478 #undef FILL_SHM
   1479 
   1480 #endif /* defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM) */
   1481 
   1482 #if NPTY > 0
   1483 /*
   1484  * sysctl helper routine for kern.maxptys.  ensures that any new value
   1485  * is acceptable to the pty subsystem.
   1486  */
   1487 static int
   1488 sysctl_kern_maxptys(SYSCTLFN_ARGS)
   1489 {
   1490 	int pty_maxptys(int, int);		/* defined in kern/tty_pty.c */
   1491 	int error, max;
   1492 	struct sysctlnode node;
   1493 
   1494 	/* get current value of maxptys */
   1495 	max = pty_maxptys(0, 0);
   1496 
   1497 	node = *rnode;
   1498 	node.sysctl_data = &max;
   1499 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1500 	if (error || newp == NULL)
   1501 		return (error);
   1502 
   1503 	if (max != pty_maxptys(max, 1))
   1504 		return (EINVAL);
   1505 
   1506 	return (0);
   1507 }
   1508 #endif /* NPTY > 0 */
   1509 
   1510 /*
   1511  * sysctl helper routine for kern.sbmax.  basically just ensures that
   1512  * any new value is not too small.
   1513  */
   1514 static int
   1515 sysctl_kern_sbmax(SYSCTLFN_ARGS)
   1516 {
   1517 	int error, new_sbmax;
   1518 	struct sysctlnode node;
   1519 
   1520 	new_sbmax = sb_max;
   1521 	node = *rnode;
   1522 	node.sysctl_data = &new_sbmax;
   1523 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1524 	if (error || newp == NULL)
   1525 		return (error);
   1526 
   1527 	error = sb_max_set(new_sbmax);
   1528 
   1529 	return (error);
   1530 }
   1531 
   1532 /*
   1533  * sysctl helper routine for kern.urandom node.  picks a random number
   1534  * for you.
   1535  */
   1536 static int
   1537 sysctl_kern_urnd(SYSCTLFN_ARGS)
   1538 {
   1539 #if NRND > 0
   1540 	int v;
   1541 
   1542 	if (rnd_extract_data(&v, sizeof(v), RND_EXTRACT_ANY) == sizeof(v)) {
   1543 		struct sysctlnode node = *rnode;
   1544 		node.sysctl_data = &v;
   1545 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1546 	}
   1547 	else
   1548 		return (EIO);	/*XXX*/
   1549 #else
   1550 	return (EOPNOTSUPP);
   1551 #endif
   1552 }
   1553 
   1554 /*
   1555  * sysctl helper routine to do kern.lwp.* work.
   1556  */
   1557 static int
   1558 sysctl_kern_lwp(SYSCTLFN_ARGS)
   1559 {
   1560 	struct kinfo_lwp klwp;
   1561 	struct proc *p;
   1562 	struct lwp *l2;
   1563 	char *where, *dp;
   1564 	int pid, elem_size, elem_count;
   1565 	int buflen, needed, error;
   1566 
   1567 	if (namelen == 1 && name[0] == CTL_QUERY)
   1568 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1569 
   1570 	dp = where = oldp;
   1571 	buflen = where != NULL ? *oldlenp : 0;
   1572 	error = needed = 0;
   1573 
   1574 	if (newp != NULL || namelen != 3)
   1575 		return (EINVAL);
   1576 	pid = name[0];
   1577 	elem_size = name[1];
   1578 	elem_count = name[2];
   1579 
   1580 	p = pfind(pid);
   1581 	if (p == NULL)
   1582 		return (ESRCH);
   1583 	LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1584 		if (buflen >= elem_size && elem_count > 0) {
   1585 			fill_lwp(l2, &klwp);
   1586 			/*
   1587 			 * Copy out elem_size, but not larger than
   1588 			 * the size of a struct kinfo_proc2.
   1589 			 */
   1590 			error = copyout(&klwp, dp,
   1591 			    min(sizeof(klwp), elem_size));
   1592 			if (error)
   1593 				goto cleanup;
   1594 			dp += elem_size;
   1595 			buflen -= elem_size;
   1596 			elem_count--;
   1597 		}
   1598 		needed += elem_size;
   1599 	}
   1600 
   1601 	if (where != NULL) {
   1602 		*oldlenp = dp - where;
   1603 		if (needed > *oldlenp)
   1604 			return (ENOMEM);
   1605 	} else {
   1606 		needed += KERN_LWPSLOP;
   1607 		*oldlenp = needed;
   1608 	}
   1609 	return (0);
   1610  cleanup:
   1611 	return (error);
   1612 }
   1613 
   1614 /*
   1615  * sysctl helper routine for kern.forkfsleep node.  ensures that the
   1616  * given value is not too large or two small, and is at least one
   1617  * timer tick if not zero.
   1618  */
   1619 static int
   1620 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
   1621 {
   1622 	/* userland sees value in ms, internally is in ticks */
   1623 	extern int forkfsleep;		/* defined in kern/kern_fork.c */
   1624 	int error, timo, lsleep;
   1625 	struct sysctlnode node;
   1626 
   1627 	lsleep = forkfsleep * 1000 / hz;
   1628 	node = *rnode;
   1629 	node.sysctl_data = &lsleep;
   1630 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1631 	if (error || newp == NULL)
   1632 		return (error);
   1633 
   1634 	/* refuse negative values, and overly 'long time' */
   1635 	if (lsleep < 0 || lsleep > MAXSLP * 1000)
   1636 		return (EINVAL);
   1637 
   1638 	timo = mstohz(lsleep);
   1639 
   1640 	/* if the interval is >0 ms && <1 tick, use 1 tick */
   1641 	if (lsleep != 0 && timo == 0)
   1642 		forkfsleep = 1;
   1643 	else
   1644 		forkfsleep = timo;
   1645 
   1646 	return (0);
   1647 }
   1648 
   1649 /*
   1650  * sysctl helper routine for kern.root_partition
   1651  */
   1652 static int
   1653 sysctl_kern_root_partition(SYSCTLFN_ARGS)
   1654 {
   1655 	int rootpart = DISKPART(rootdev);
   1656 	struct sysctlnode node = *rnode;
   1657 
   1658 	node.sysctl_data = &rootpart;
   1659 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   1660 }
   1661 
   1662 /*
   1663  * sysctl helper function for kern.drivers
   1664  */
   1665 static int
   1666 sysctl_kern_drivers(SYSCTLFN_ARGS)
   1667 {
   1668 	int error;
   1669 	size_t buflen;
   1670 	struct kinfo_drivers kd;
   1671 	char *start, *where;
   1672 	const char *dname;
   1673 	int i;
   1674 	extern struct devsw_conv *devsw_conv;
   1675 	extern int max_devsw_convs;
   1676 
   1677 	if (newp != NULL || namelen != 0)
   1678 		return (EINVAL);
   1679 
   1680 	start = where = oldp;
   1681 	buflen = *oldlenp;
   1682 	if (where == NULL) {
   1683 		*oldlenp = max_devsw_convs * sizeof kd;
   1684 		return 0;
   1685 	}
   1686 
   1687 	/*
   1688 	 * An array of kinfo_drivers structures
   1689 	 */
   1690 	error = 0;
   1691 	for (i = 0; i < max_devsw_convs; i++) {
   1692 		dname = devsw_conv[i].d_name;
   1693 		if (dname == NULL)
   1694 			continue;
   1695 		if (buflen < sizeof kd) {
   1696 			error = ENOMEM;
   1697 			break;
   1698 		}
   1699 		memset(&kd, 0, sizeof(kd));
   1700 		kd.d_bmajor = devsw_conv[i].d_bmajor;
   1701 		kd.d_cmajor = devsw_conv[i].d_cmajor;
   1702 		strlcpy(kd.d_name, dname, sizeof kd.d_name);
   1703 		error = copyout(&kd, where, sizeof kd);
   1704 		if (error != 0)
   1705 			break;
   1706 		buflen -= sizeof kd;
   1707 		where += sizeof kd;
   1708 	}
   1709 	*oldlenp = where - start;
   1710 	return error;
   1711 }
   1712 
   1713 static int
   1714 sysctl_doeproc(SYSCTLFN_ARGS)
   1715 {
   1716 	struct eproc eproc;
   1717 	struct kinfo_proc2 kproc2;
   1718 	struct kinfo_proc *dp;
   1719 	struct proc *p;
   1720 	const struct proclist_desc *pd;
   1721 	char *where, *dp2;
   1722 	int type, op, arg;
   1723 	u_int elem_size, elem_count;
   1724 	size_t buflen, needed;
   1725 	int error;
   1726 
   1727 	if (namelen == 1 && name[0] == CTL_QUERY)
   1728 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1729 
   1730 	dp = oldp;
   1731 	dp2 = where = oldp;
   1732 	buflen = where != NULL ? *oldlenp : 0;
   1733 	error = 0;
   1734 	needed = 0;
   1735 	type = rnode->sysctl_num;
   1736 
   1737 	if (type == KERN_PROC) {
   1738 		if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
   1739 			return (EINVAL);
   1740 		op = name[0];
   1741 		if (op != KERN_PROC_ALL)
   1742 			arg = name[1];
   1743 		else
   1744 			arg = 0;		/* Quell compiler warning */
   1745 		elem_size = elem_count = 0;	/* Ditto */
   1746 	} else {
   1747 		if (namelen != 4)
   1748 			return (EINVAL);
   1749 		op = name[0];
   1750 		arg = name[1];
   1751 		elem_size = name[2];
   1752 		elem_count = name[3];
   1753 	}
   1754 
   1755 	proclist_lock_read();
   1756 
   1757 	pd = proclists;
   1758 again:
   1759 	for (p = LIST_FIRST(pd->pd_list); p != NULL; p = LIST_NEXT(p, p_list)) {
   1760 		/*
   1761 		 * Skip embryonic processes.
   1762 		 */
   1763 		if (p->p_stat == SIDL)
   1764 			continue;
   1765 		/*
   1766 		 * TODO - make more efficient (see notes below).
   1767 		 * do by session.
   1768 		 */
   1769 		switch (op) {
   1770 
   1771 		case KERN_PROC_PID:
   1772 			/* could do this with just a lookup */
   1773 			if (p->p_pid != (pid_t)arg)
   1774 				continue;
   1775 			break;
   1776 
   1777 		case KERN_PROC_PGRP:
   1778 			/* could do this by traversing pgrp */
   1779 			if (p->p_pgrp->pg_id != (pid_t)arg)
   1780 				continue;
   1781 			break;
   1782 
   1783 		case KERN_PROC_SESSION:
   1784 			if (p->p_session->s_sid != (pid_t)arg)
   1785 				continue;
   1786 			break;
   1787 
   1788 		case KERN_PROC_TTY:
   1789 			if (arg == (int) KERN_PROC_TTY_REVOKE) {
   1790 				if ((p->p_flag & P_CONTROLT) == 0 ||
   1791 				    p->p_session->s_ttyp == NULL ||
   1792 				    p->p_session->s_ttyvp != NULL)
   1793 					continue;
   1794 			} else if ((p->p_flag & P_CONTROLT) == 0 ||
   1795 			    p->p_session->s_ttyp == NULL) {
   1796 				if ((dev_t)arg != KERN_PROC_TTY_NODEV)
   1797 					continue;
   1798 			} else if (p->p_session->s_ttyp->t_dev != (dev_t)arg)
   1799 				continue;
   1800 			break;
   1801 
   1802 		case KERN_PROC_UID:
   1803 			if (p->p_ucred->cr_uid != (uid_t)arg)
   1804 				continue;
   1805 			break;
   1806 
   1807 		case KERN_PROC_RUID:
   1808 			if (p->p_cred->p_ruid != (uid_t)arg)
   1809 				continue;
   1810 			break;
   1811 
   1812 		case KERN_PROC_GID:
   1813 			if (p->p_ucred->cr_gid != (uid_t)arg)
   1814 				continue;
   1815 			break;
   1816 
   1817 		case KERN_PROC_RGID:
   1818 			if (p->p_cred->p_rgid != (uid_t)arg)
   1819 				continue;
   1820 			break;
   1821 
   1822 		case KERN_PROC_ALL:
   1823 			/* allow everything */
   1824 			break;
   1825 
   1826 		default:
   1827 			error = EINVAL;
   1828 			goto cleanup;
   1829 		}
   1830 		if (type == KERN_PROC) {
   1831 			if (buflen >= sizeof(struct kinfo_proc)) {
   1832 				fill_eproc(p, &eproc);
   1833 				error = copyout(p, &dp->kp_proc,
   1834 				    sizeof(struct proc));
   1835 				if (error)
   1836 					goto cleanup;
   1837 				error = copyout(&eproc, &dp->kp_eproc,
   1838 				    sizeof(eproc));
   1839 				if (error)
   1840 					goto cleanup;
   1841 				dp++;
   1842 				buflen -= sizeof(struct kinfo_proc);
   1843 			}
   1844 			needed += sizeof(struct kinfo_proc);
   1845 		} else { /* KERN_PROC2 */
   1846 			if (buflen >= elem_size && elem_count > 0) {
   1847 				fill_kproc2(p, &kproc2);
   1848 				/*
   1849 				 * Copy out elem_size, but not larger than
   1850 				 * the size of a struct kinfo_proc2.
   1851 				 */
   1852 				error = copyout(&kproc2, dp2,
   1853 				    min(sizeof(kproc2), elem_size));
   1854 				if (error)
   1855 					goto cleanup;
   1856 				dp2 += elem_size;
   1857 				buflen -= elem_size;
   1858 				elem_count--;
   1859 			}
   1860 			needed += elem_size;
   1861 		}
   1862 	}
   1863 	pd++;
   1864 	if (pd->pd_list != NULL)
   1865 		goto again;
   1866 	proclist_unlock_read();
   1867 
   1868 	if (where != NULL) {
   1869 		if (type == KERN_PROC)
   1870 			*oldlenp = (char *)dp - where;
   1871 		else
   1872 			*oldlenp = dp2 - where;
   1873 		if (needed > *oldlenp)
   1874 			return (ENOMEM);
   1875 	} else {
   1876 		needed += KERN_PROCSLOP;
   1877 		*oldlenp = needed;
   1878 	}
   1879 	return (0);
   1880  cleanup:
   1881 	proclist_unlock_read();
   1882 	return (error);
   1883 }
   1884 
   1885 /*
   1886  * sysctl helper routine for kern.proc_args pseudo-subtree.
   1887  */
   1888 static int
   1889 sysctl_kern_proc_args(SYSCTLFN_ARGS)
   1890 {
   1891 	struct ps_strings pss;
   1892 	struct proc *p, *up = l->l_proc;
   1893 	size_t len, upper_bound, xlen, i;
   1894 	struct uio auio;
   1895 	struct iovec aiov;
   1896 	vaddr_t argv;
   1897 	pid_t pid;
   1898 	int nargv, type, error;
   1899 	char *arg;
   1900 	char *tmp;
   1901 
   1902 	if (namelen == 1 && name[0] == CTL_QUERY)
   1903 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
   1904 
   1905 	if (newp != NULL || namelen != 2)
   1906 		return (EINVAL);
   1907 	pid = name[0];
   1908 	type = name[1];
   1909 
   1910 	switch (type) {
   1911 	case KERN_PROC_ARGV:
   1912 	case KERN_PROC_NARGV:
   1913 	case KERN_PROC_ENV:
   1914 	case KERN_PROC_NENV:
   1915 		/* ok */
   1916 		break;
   1917 	default:
   1918 		return (EINVAL);
   1919 	}
   1920 
   1921 	/* check pid */
   1922 	if ((p = pfind(pid)) == NULL)
   1923 		return (EINVAL);
   1924 
   1925 	/* only root or same user change look at the environment */
   1926 	if (type == KERN_PROC_ENV || type == KERN_PROC_NENV) {
   1927 		if (up->p_ucred->cr_uid != 0) {
   1928 			if (up->p_cred->p_ruid != p->p_cred->p_ruid ||
   1929 			    up->p_cred->p_ruid != p->p_cred->p_svuid)
   1930 				return (EPERM);
   1931 		}
   1932 	}
   1933 
   1934 	if (oldp == NULL) {
   1935 		if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
   1936 			*oldlenp = sizeof (int);
   1937 		else
   1938 			*oldlenp = ARG_MAX;	/* XXX XXX XXX */
   1939 		return (0);
   1940 	}
   1941 
   1942 	/*
   1943 	 * Zombies don't have a stack, so we can't read their psstrings.
   1944 	 * System processes also don't have a user stack.
   1945 	 */
   1946 	if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0)
   1947 		return (EINVAL);
   1948 
   1949 	/*
   1950 	 * Lock the process down in memory.
   1951 	 */
   1952 	/* XXXCDC: how should locking work here? */
   1953 	if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
   1954 		return (EFAULT);
   1955 
   1956 	p->p_vmspace->vm_refcnt++;	/* XXX */
   1957 
   1958 	/*
   1959 	 * Allocate a temporary buffer to hold the arguments.
   1960 	 */
   1961 	arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
   1962 
   1963 	/*
   1964 	 * Read in the ps_strings structure.
   1965 	 */
   1966 	aiov.iov_base = &pss;
   1967 	aiov.iov_len = sizeof(pss);
   1968 	auio.uio_iov = &aiov;
   1969 	auio.uio_iovcnt = 1;
   1970 	auio.uio_offset = (vaddr_t)p->p_psstr;
   1971 	auio.uio_resid = sizeof(pss);
   1972 	auio.uio_segflg = UIO_SYSSPACE;
   1973 	auio.uio_rw = UIO_READ;
   1974 	auio.uio_procp = NULL;
   1975 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
   1976 	if (error)
   1977 		goto done;
   1978 
   1979 	if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
   1980 		memcpy(&nargv, (char *)&pss + p->p_psnargv, sizeof(nargv));
   1981 	else
   1982 		memcpy(&nargv, (char *)&pss + p->p_psnenv, sizeof(nargv));
   1983 	if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
   1984 		error = copyout(&nargv, oldp, sizeof(nargv));
   1985 		*oldlenp = sizeof(nargv);
   1986 		goto done;
   1987 	}
   1988 	/*
   1989 	 * Now read the address of the argument vector.
   1990 	 */
   1991 	switch (type) {
   1992 	case KERN_PROC_ARGV:
   1993 		/* XXX compat32 stuff here */
   1994 		memcpy(&tmp, (char *)&pss + p->p_psargv, sizeof(tmp));
   1995 		break;
   1996 	case KERN_PROC_ENV:
   1997 		memcpy(&tmp, (char *)&pss + p->p_psenv, sizeof(tmp));
   1998 		break;
   1999 	default:
   2000 		return (EINVAL);
   2001 	}
   2002 	auio.uio_offset = (off_t)(long)tmp;
   2003 	aiov.iov_base = &argv;
   2004 	aiov.iov_len = sizeof(argv);
   2005 	auio.uio_iov = &aiov;
   2006 	auio.uio_iovcnt = 1;
   2007 	auio.uio_resid = sizeof(argv);
   2008 	auio.uio_segflg = UIO_SYSSPACE;
   2009 	auio.uio_rw = UIO_READ;
   2010 	auio.uio_procp = NULL;
   2011 	error = uvm_io(&p->p_vmspace->vm_map, &auio);
   2012 	if (error)
   2013 		goto done;
   2014 
   2015 	/*
   2016 	 * Now copy in the actual argument vector, one page at a time,
   2017 	 * since we don't know how long the vector is (though, we do
   2018 	 * know how many NUL-terminated strings are in the vector).
   2019 	 */
   2020 	len = 0;
   2021 	upper_bound = *oldlenp;
   2022 	for (; nargv != 0 && len < upper_bound; len += xlen) {
   2023 		aiov.iov_base = arg;
   2024 		aiov.iov_len = PAGE_SIZE;
   2025 		auio.uio_iov = &aiov;
   2026 		auio.uio_iovcnt = 1;
   2027 		auio.uio_offset = argv + len;
   2028 		xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
   2029 		auio.uio_resid = xlen;
   2030 		auio.uio_segflg = UIO_SYSSPACE;
   2031 		auio.uio_rw = UIO_READ;
   2032 		auio.uio_procp = NULL;
   2033 		error = uvm_io(&p->p_vmspace->vm_map, &auio);
   2034 		if (error)
   2035 			goto done;
   2036 
   2037 		for (i = 0; i < xlen && nargv != 0; i++) {
   2038 			if (arg[i] == '\0')
   2039 				nargv--;	/* one full string */
   2040 		}
   2041 
   2042 		/*
   2043 		 * Make sure we don't copyout past the end of the user's
   2044 		 * buffer.
   2045 		 */
   2046 		if (len + i > upper_bound)
   2047 			i = upper_bound - len;
   2048 
   2049 		error = copyout(arg, (char *)oldp + len, i);
   2050 		if (error)
   2051 			break;
   2052 
   2053 		if (nargv == 0) {
   2054 			len += i;
   2055 			break;
   2056 		}
   2057 	}
   2058 	*oldlenp = len;
   2059 
   2060 done:
   2061 	uvmspace_free(p->p_vmspace);
   2062 
   2063 	free(arg, M_TEMP);
   2064 	return (error);
   2065 }
   2066 
   2067 /*
   2068  * sysctl helper routine for hw.usermem and hw.usermem64.  values are
   2069  * calculate on the fly taking into account integer overflow and the
   2070  * current wired count.
   2071  */
   2072 static int
   2073 sysctl_hw_usermem(SYSCTLFN_ARGS)
   2074 {
   2075 	u_int ui;
   2076 	u_quad_t uq;
   2077 	struct sysctlnode node;
   2078 
   2079 	node = *rnode;
   2080 	switch (rnode->sysctl_num) {
   2081 	    case HW_USERMEM:
   2082 		if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
   2083 			ui = UINT_MAX;
   2084 		else
   2085 			ui *= PAGE_SIZE;
   2086 		node.sysctl_data = &ui;
   2087 		break;
   2088 	case HW_USERMEM64:
   2089 		uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
   2090 		node.sysctl_data = &uq;
   2091 		break;
   2092 	default:
   2093 		return (EINVAL);
   2094 	}
   2095 
   2096 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2097 }
   2098 
   2099 /*
   2100  * sysctl helper routine for kern.cnmagic node.  pulls the old value
   2101  * out, encoded, and stuffs the new value in for decoding.
   2102  */
   2103 static int
   2104 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
   2105 {
   2106 	char magic[CNS_LEN];
   2107 	int error;
   2108 	struct sysctlnode node;
   2109 
   2110 	if (oldp)
   2111 		cn_get_magic(magic, CNS_LEN);
   2112 	node = *rnode;
   2113 	node.sysctl_data = &magic[0];
   2114 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2115 	if (error || newp == NULL)
   2116 		return (error);
   2117 
   2118 	return (cn_set_magic(magic));
   2119 }
   2120 
   2121 static int
   2122 sysctl_hw_ncpu(SYSCTLFN_ARGS)
   2123 {
   2124 	int ncpu;
   2125 	struct sysctlnode node;
   2126 
   2127 	ncpu = sysctl_ncpus();
   2128 	node = *rnode;
   2129 	node.sysctl_data = &ncpu;
   2130 
   2131 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2132 }
   2133 
   2134 
   2135 /*
   2136  * ********************************************************************
   2137  * section 3: public helper routines that are used for more than one
   2138  * node
   2139  * ********************************************************************
   2140  */
   2141 
   2142 /*
   2143  * sysctl helper routine for the kern.root_device node and some ports'
   2144  * machdep.root_device nodes.
   2145  */
   2146 int
   2147 sysctl_root_device(SYSCTLFN_ARGS)
   2148 {
   2149 	struct sysctlnode node;
   2150 
   2151 	node = *rnode;
   2152 	node.sysctl_data = root_device->dv_xname;
   2153 	node.sysctl_size = strlen(root_device->dv_xname) + 1;
   2154 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2155 }
   2156 
   2157 /*
   2158  * sysctl helper routine for kern.consdev, dependent on the current
   2159  * state of the console.  also used for machdep.console_device on some
   2160  * ports.
   2161  */
   2162 int
   2163 sysctl_consdev(SYSCTLFN_ARGS)
   2164 {
   2165 	dev_t consdev;
   2166 	struct sysctlnode node;
   2167 
   2168 	if (cn_tab != NULL)
   2169 		consdev = cn_tab->cn_dev;
   2170 	else
   2171 		consdev = NODEV;
   2172 	node = *rnode;
   2173 	node.sysctl_data = &consdev;
   2174 	node.sysctl_size = sizeof(consdev);
   2175 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
   2176 }
   2177 
   2178 /*
   2179  * ********************************************************************
   2180  * section 4: support for some helpers
   2181  * ********************************************************************
   2182  */
   2183 
   2184 /*
   2185  * Fill in a kinfo_proc2 structure for the specified process.
   2186  */
   2187 static void
   2188 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki)
   2189 {
   2190 	struct tty *tp;
   2191 	struct lwp *l;
   2192 	struct timeval ut, st;
   2193 
   2194 	memset(ki, 0, sizeof(*ki));
   2195 
   2196 	ki->p_paddr = PTRTOUINT64(p);
   2197 	ki->p_fd = PTRTOUINT64(p->p_fd);
   2198 	ki->p_cwdi = PTRTOUINT64(p->p_cwdi);
   2199 	ki->p_stats = PTRTOUINT64(p->p_stats);
   2200 	ki->p_limit = PTRTOUINT64(p->p_limit);
   2201 	ki->p_vmspace = PTRTOUINT64(p->p_vmspace);
   2202 	ki->p_sigacts = PTRTOUINT64(p->p_sigacts);
   2203 	ki->p_sess = PTRTOUINT64(p->p_session);
   2204 	ki->p_tsess = 0;	/* may be changed if controlling tty below */
   2205 	ki->p_ru = PTRTOUINT64(p->p_ru);
   2206 
   2207 	ki->p_eflag = 0;
   2208 	ki->p_exitsig = p->p_exitsig;
   2209 	ki->p_flag = p->p_flag;
   2210 
   2211 	ki->p_pid = p->p_pid;
   2212 	if (p->p_pptr)
   2213 		ki->p_ppid = p->p_pptr->p_pid;
   2214 	else
   2215 		ki->p_ppid = 0;
   2216 	ki->p_sid = p->p_session->s_sid;
   2217 	ki->p__pgid = p->p_pgrp->pg_id;
   2218 
   2219 	ki->p_tpgid = NO_PGID;	/* may be changed if controlling tty below */
   2220 
   2221 	ki->p_uid = p->p_ucred->cr_uid;
   2222 	ki->p_ruid = p->p_cred->p_ruid;
   2223 	ki->p_gid = p->p_ucred->cr_gid;
   2224 	ki->p_rgid = p->p_cred->p_rgid;
   2225 	ki->p_svuid = p->p_cred->p_svuid;
   2226 	ki->p_svgid = p->p_cred->p_svgid;
   2227 
   2228 	memcpy(ki->p_groups, p->p_cred->pc_ucred->cr_groups,
   2229 	    min(sizeof(ki->p_groups), sizeof(p->p_cred->pc_ucred->cr_groups)));
   2230 	ki->p_ngroups = p->p_cred->pc_ucred->cr_ngroups;
   2231 
   2232 	ki->p_jobc = p->p_pgrp->pg_jobc;
   2233 	if ((p->p_flag & P_CONTROLT) && (tp = p->p_session->s_ttyp)) {
   2234 		ki->p_tdev = tp->t_dev;
   2235 		ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   2236 		ki->p_tsess = PTRTOUINT64(tp->t_session);
   2237 	} else {
   2238 		ki->p_tdev = NODEV;
   2239 	}
   2240 
   2241 	ki->p_estcpu = p->p_estcpu;
   2242 	ki->p_rtime_sec = p->p_rtime.tv_sec;
   2243 	ki->p_rtime_usec = p->p_rtime.tv_usec;
   2244 	ki->p_cpticks = p->p_cpticks;
   2245 	ki->p_pctcpu = p->p_pctcpu;
   2246 
   2247 	ki->p_uticks = p->p_uticks;
   2248 	ki->p_sticks = p->p_sticks;
   2249 	ki->p_iticks = p->p_iticks;
   2250 
   2251 	ki->p_tracep = PTRTOUINT64(p->p_tracep);
   2252 	ki->p_traceflag = p->p_traceflag;
   2253 
   2254 
   2255 	memcpy(&ki->p_siglist, &p->p_sigctx.ps_siglist, sizeof(ki_sigset_t));
   2256 	memcpy(&ki->p_sigmask, &p->p_sigctx.ps_sigmask, sizeof(ki_sigset_t));
   2257 	memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
   2258 	memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
   2259 
   2260 	ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
   2261 	ki->p_realstat = p->p_stat;
   2262 	ki->p_nice = p->p_nice;
   2263 
   2264 	ki->p_xstat = p->p_xstat;
   2265 	ki->p_acflag = p->p_acflag;
   2266 
   2267 	strncpy(ki->p_comm, p->p_comm,
   2268 	    min(sizeof(ki->p_comm), sizeof(p->p_comm)));
   2269 
   2270 	strncpy(ki->p_login, p->p_session->s_login,
   2271 	    min(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
   2272 
   2273 	ki->p_nlwps = p->p_nlwps;
   2274 	ki->p_nrlwps = p->p_nrlwps;
   2275 	ki->p_realflag = p->p_flag;
   2276 
   2277 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   2278 		ki->p_vm_rssize = 0;
   2279 		ki->p_vm_tsize = 0;
   2280 		ki->p_vm_dsize = 0;
   2281 		ki->p_vm_ssize = 0;
   2282 		l = NULL;
   2283 	} else {
   2284 		struct vmspace *vm = p->p_vmspace;
   2285 
   2286 		ki->p_vm_rssize = vm_resident_count(vm);
   2287 		ki->p_vm_tsize = vm->vm_tsize;
   2288 		ki->p_vm_dsize = vm->vm_dsize;
   2289 		ki->p_vm_ssize = vm->vm_ssize;
   2290 
   2291 		/* Pick a "representative" LWP */
   2292 		l = proc_representative_lwp(p);
   2293 		ki->p_forw = PTRTOUINT64(l->l_forw);
   2294 		ki->p_back = PTRTOUINT64(l->l_back);
   2295 		ki->p_addr = PTRTOUINT64(l->l_addr);
   2296 		ki->p_stat = l->l_stat;
   2297 		ki->p_flag |= l->l_flag;
   2298 		ki->p_swtime = l->l_swtime;
   2299 		ki->p_slptime = l->l_slptime;
   2300 		if (l->l_stat == LSONPROC) {
   2301 			KDASSERT(l->l_cpu != NULL);
   2302 			ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   2303 		} else
   2304 			ki->p_schedflags = 0;
   2305 		ki->p_holdcnt = l->l_holdcnt;
   2306 		ki->p_priority = l->l_priority;
   2307 		ki->p_usrpri = l->l_usrpri;
   2308 		if (l->l_wmesg)
   2309 			strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
   2310 		ki->p_wchan = PTRTOUINT64(l->l_wchan);
   2311 
   2312 	}
   2313 
   2314 	if (p->p_session->s_ttyvp)
   2315 		ki->p_eflag |= EPROC_CTTY;
   2316 	if (SESS_LEADER(p))
   2317 		ki->p_eflag |= EPROC_SLEADER;
   2318 
   2319 	/* XXX Is this double check necessary? */
   2320 	if (P_ZOMBIE(p)) {
   2321 		ki->p_uvalid = 0;
   2322 	} else {
   2323 		ki->p_uvalid = 1;
   2324 
   2325 		ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
   2326 		ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
   2327 
   2328 		calcru(p, &ut, &st, 0);
   2329 		ki->p_uutime_sec = ut.tv_sec;
   2330 		ki->p_uutime_usec = ut.tv_usec;
   2331 		ki->p_ustime_sec = st.tv_sec;
   2332 		ki->p_ustime_usec = st.tv_usec;
   2333 
   2334 		ki->p_uru_maxrss = p->p_stats->p_ru.ru_maxrss;
   2335 		ki->p_uru_ixrss = p->p_stats->p_ru.ru_ixrss;
   2336 		ki->p_uru_idrss = p->p_stats->p_ru.ru_idrss;
   2337 		ki->p_uru_isrss = p->p_stats->p_ru.ru_isrss;
   2338 		ki->p_uru_minflt = p->p_stats->p_ru.ru_minflt;
   2339 		ki->p_uru_majflt = p->p_stats->p_ru.ru_majflt;
   2340 		ki->p_uru_nswap = p->p_stats->p_ru.ru_nswap;
   2341 		ki->p_uru_inblock = p->p_stats->p_ru.ru_inblock;
   2342 		ki->p_uru_oublock = p->p_stats->p_ru.ru_oublock;
   2343 		ki->p_uru_msgsnd = p->p_stats->p_ru.ru_msgsnd;
   2344 		ki->p_uru_msgrcv = p->p_stats->p_ru.ru_msgrcv;
   2345 		ki->p_uru_nsignals = p->p_stats->p_ru.ru_nsignals;
   2346 		ki->p_uru_nvcsw = p->p_stats->p_ru.ru_nvcsw;
   2347 		ki->p_uru_nivcsw = p->p_stats->p_ru.ru_nivcsw;
   2348 
   2349 		timeradd(&p->p_stats->p_cru.ru_utime,
   2350 			 &p->p_stats->p_cru.ru_stime, &ut);
   2351 		ki->p_uctime_sec = ut.tv_sec;
   2352 		ki->p_uctime_usec = ut.tv_usec;
   2353 	}
   2354 #ifdef MULTIPROCESSOR
   2355 	if (l && l->l_cpu != NULL)
   2356 		ki->p_cpuid = l->l_cpu->ci_cpuid;
   2357 	else
   2358 #endif
   2359 		ki->p_cpuid = KI_NOCPU;
   2360 }
   2361 
   2362 /*
   2363  * Fill in a kinfo_lwp structure for the specified lwp.
   2364  */
   2365 static void
   2366 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
   2367 {
   2368 
   2369 	kl->l_forw = PTRTOUINT64(l->l_forw);
   2370 	kl->l_back = PTRTOUINT64(l->l_back);
   2371 	kl->l_laddr = PTRTOUINT64(l);
   2372 	kl->l_addr = PTRTOUINT64(l->l_addr);
   2373 	kl->l_stat = l->l_stat;
   2374 	kl->l_lid = l->l_lid;
   2375 	kl->l_flag = l->l_flag;
   2376 
   2377 	kl->l_swtime = l->l_swtime;
   2378 	kl->l_slptime = l->l_slptime;
   2379 	if (l->l_stat == LSONPROC) {
   2380 		KDASSERT(l->l_cpu != NULL);
   2381 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
   2382 	} else
   2383 		kl->l_schedflags = 0;
   2384 	kl->l_holdcnt = l->l_holdcnt;
   2385 	kl->l_priority = l->l_priority;
   2386 	kl->l_usrpri = l->l_usrpri;
   2387 	if (l->l_wmesg)
   2388 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
   2389 	kl->l_wchan = PTRTOUINT64(l->l_wchan);
   2390 #ifdef MULTIPROCESSOR
   2391 	if (l->l_cpu != NULL)
   2392 		kl->l_cpuid = l->l_cpu->ci_cpuid;
   2393 	else
   2394 #endif
   2395 		kl->l_cpuid = KI_NOCPU;
   2396 }
   2397 
   2398 /*
   2399  * Fill in an eproc structure for the specified process.
   2400  */
   2401 void
   2402 fill_eproc(struct proc *p, struct eproc *ep)
   2403 {
   2404 	struct tty *tp;
   2405 	struct lwp *l;
   2406 
   2407 	ep->e_paddr = p;
   2408 	ep->e_sess = p->p_session;
   2409 	ep->e_pcred = *p->p_cred;
   2410 	ep->e_ucred = *p->p_ucred;
   2411 	if (p->p_stat == SIDL || P_ZOMBIE(p)) {
   2412 		ep->e_vm.vm_rssize = 0;
   2413 		ep->e_vm.vm_tsize = 0;
   2414 		ep->e_vm.vm_dsize = 0;
   2415 		ep->e_vm.vm_ssize = 0;
   2416 		/* ep->e_vm.vm_pmap = XXX; */
   2417 	} else {
   2418 		struct vmspace *vm = p->p_vmspace;
   2419 
   2420 		ep->e_vm.vm_rssize = vm_resident_count(vm);
   2421 		ep->e_vm.vm_tsize = vm->vm_tsize;
   2422 		ep->e_vm.vm_dsize = vm->vm_dsize;
   2423 		ep->e_vm.vm_ssize = vm->vm_ssize;
   2424 
   2425 		/* Pick a "representative" LWP */
   2426 		l = proc_representative_lwp(p);
   2427 
   2428 		if (l->l_wmesg)
   2429 			strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
   2430 	}
   2431 	if (p->p_pptr)
   2432 		ep->e_ppid = p->p_pptr->p_pid;
   2433 	else
   2434 		ep->e_ppid = 0;
   2435 	ep->e_pgid = p->p_pgrp->pg_id;
   2436 	ep->e_sid = ep->e_sess->s_sid;
   2437 	ep->e_jobc = p->p_pgrp->pg_jobc;
   2438 	if ((p->p_flag & P_CONTROLT) &&
   2439 	    (tp = ep->e_sess->s_ttyp)) {
   2440 		ep->e_tdev = tp->t_dev;
   2441 		ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
   2442 		ep->e_tsess = tp->t_session;
   2443 	} else
   2444 		ep->e_tdev = NODEV;
   2445 
   2446 	ep->e_xsize = ep->e_xrssize = 0;
   2447 	ep->e_xccount = ep->e_xswrss = 0;
   2448 	ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
   2449 	if (SESS_LEADER(p))
   2450 		ep->e_flag |= EPROC_SLEADER;
   2451 	strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
   2452 }
   2453