xref: /netbsd-src/sys/kern/init_sysctl.c (revision 9aa0541bdf64142d9a27c2cf274394d60182818f)
1 /*	$NetBSD: init_sysctl.c,v 1.183 2011/08/30 12:39:59 bouyer Exp $ */
2 
3 /*-
4  * Copyright (c) 2003, 2007, 2008, 2009 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, and by Andrew Doran.
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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: init_sysctl.c,v 1.183 2011/08/30 12:39:59 bouyer Exp $");
34 
35 #include "opt_sysv.h"
36 #include "opt_compat_netbsd.h"
37 #include "opt_modular.h"
38 #include "opt_sa.h"
39 #include "opt_posix.h"
40 #include "pty.h"
41 #include "rnd.h"
42 
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/sysctl.h>
46 #include <sys/cpu.h>
47 #include <sys/errno.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/unistd.h>
51 #include <sys/disklabel.h>
52 #include <sys/rnd.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
56 #include <sys/msgbuf.h>
57 #include <dev/cons.h>
58 #include <sys/socketvar.h>
59 #include <sys/file.h>
60 #include <sys/filedesc.h>
61 #include <sys/tty.h>
62 #include <sys/kmem.h>
63 #include <sys/resource.h>
64 #include <sys/resourcevar.h>
65 #include <sys/exec.h>
66 #include <sys/conf.h>
67 #include <sys/device.h>
68 #include <sys/stat.h>
69 #include <sys/kauth.h>
70 #include <sys/ktrace.h>
71 #include <sys/ksem.h>
72 
73 #ifdef COMPAT_50
74 #include <compat/sys/time.h>
75 #endif
76 
77 #ifdef KERN_SA
78 #include <sys/sa.h>
79 #endif
80 
81 #include <sys/cpu.h>
82 
83 #if defined(MODULAR) || defined(P1003_1B_SEMAPHORE)
84 int posix_semaphores = 200112;
85 #else
86 int posix_semaphores;
87 #endif
88 
89 int security_setidcore_dump;
90 char security_setidcore_path[MAXPATHLEN] = "/var/crash/%n.core";
91 uid_t security_setidcore_owner = 0;
92 gid_t security_setidcore_group = 0;
93 mode_t security_setidcore_mode = (S_IRUSR|S_IWUSR);
94 
95 static const u_int sysctl_flagmap[] = {
96 	PK_ADVLOCK, P_ADVLOCK,
97 	PK_EXEC, P_EXEC,
98 	PK_NOCLDWAIT, P_NOCLDWAIT,
99 	PK_32, P_32,
100 	PK_CLDSIGIGN, P_CLDSIGIGN,
101 	PK_SUGID, P_SUGID,
102 	0
103 };
104 
105 static const u_int sysctl_sflagmap[] = {
106 	PS_NOCLDSTOP, P_NOCLDSTOP,
107 	PS_WEXIT, P_WEXIT,
108 	PS_STOPFORK, P_STOPFORK,
109 	PS_STOPEXEC, P_STOPEXEC,
110 	PS_STOPEXIT, P_STOPEXIT,
111 	0
112 };
113 
114 static const u_int sysctl_slflagmap[] = {
115 	PSL_TRACED, P_TRACED,
116 	PSL_FSTRACE, P_FSTRACE,
117 	PSL_CHTRACED, P_CHTRACED,
118 	PSL_SYSCALL, P_SYSCALL,
119 	0
120 };
121 
122 static const u_int sysctl_lflagmap[] = {
123 	PL_CONTROLT, P_CONTROLT,
124 	PL_PPWAIT, P_PPWAIT,
125 	0
126 };
127 
128 static const u_int sysctl_stflagmap[] = {
129 	PST_PROFIL, P_PROFIL,
130 	0
131 
132 };
133 
134 static const u_int sysctl_lwpprflagmap[] = {
135 	LPR_DETACHED, L_DETACHED,
136 	0
137 };
138 
139 /*
140  * try over estimating by 5 procs/lwps
141  */
142 #define KERN_LWPSLOP	(5 * sizeof(struct kinfo_lwp))
143 
144 static int dcopyout(struct lwp *, const void *, void *, size_t);
145 
146 static int
147 dcopyout(struct lwp *l, const void *kaddr, void *uaddr, size_t len)
148 {
149 	int error;
150 
151 	error = copyout(kaddr, uaddr, len);
152 	ktrmibio(-1, UIO_READ, uaddr, len, error);
153 
154 	return error;
155 }
156 
157 #ifdef DIAGNOSTIC
158 static int sysctl_kern_trigger_panic(SYSCTLFN_PROTO);
159 #endif
160 static int sysctl_kern_maxvnodes(SYSCTLFN_PROTO);
161 static int sysctl_kern_rtc_offset(SYSCTLFN_PROTO);
162 static int sysctl_kern_maxproc(SYSCTLFN_PROTO);
163 static int sysctl_kern_hostid(SYSCTLFN_PROTO);
164 static int sysctl_setlen(SYSCTLFN_PROTO);
165 static int sysctl_kern_clockrate(SYSCTLFN_PROTO);
166 static int sysctl_msgbuf(SYSCTLFN_PROTO);
167 static int sysctl_kern_defcorename(SYSCTLFN_PROTO);
168 static int sysctl_kern_cptime(SYSCTLFN_PROTO);
169 #if NPTY > 0
170 static int sysctl_kern_maxptys(SYSCTLFN_PROTO);
171 #endif /* NPTY > 0 */
172 static int sysctl_kern_sbmax(SYSCTLFN_PROTO);
173 static int sysctl_kern_urnd(SYSCTLFN_PROTO);
174 static int sysctl_kern_arnd(SYSCTLFN_PROTO);
175 static int sysctl_kern_lwp(SYSCTLFN_PROTO);
176 static int sysctl_kern_forkfsleep(SYSCTLFN_PROTO);
177 static int sysctl_kern_root_partition(SYSCTLFN_PROTO);
178 static int sysctl_kern_drivers(SYSCTLFN_PROTO);
179 static int sysctl_security_setidcore(SYSCTLFN_PROTO);
180 static int sysctl_security_setidcorename(SYSCTLFN_PROTO);
181 static int sysctl_kern_cpid(SYSCTLFN_PROTO);
182 static int sysctl_hw_usermem(SYSCTLFN_PROTO);
183 static int sysctl_hw_cnmagic(SYSCTLFN_PROTO);
184 
185 static void fill_lwp(struct lwp *l, struct kinfo_lwp *kl);
186 
187 /*
188  * ********************************************************************
189  * section 1: setup routines
190  * ********************************************************************
191  * These functions are stuffed into a link set for sysctl setup
192  * functions. They're never called or referenced from anywhere else.
193  * ********************************************************************
194  */
195 
196 /*
197  * this setup routine is a replacement for kern_sysctl()
198  */
199 SYSCTL_SETUP(sysctl_kern_setup, "sysctl kern subtree setup")
200 {
201 	extern int kern_logsigexit;	/* defined in kern/kern_sig.c */
202 	extern fixpt_t ccpu;		/* defined in kern/kern_synch.c */
203 	extern int dumponpanic;		/* defined in kern/subr_prf.c */
204 	const struct sysctlnode *rnode;
205 
206 	sysctl_createv(clog, 0, NULL, NULL,
207 		       CTLFLAG_PERMANENT,
208 		       CTLTYPE_NODE, "kern", NULL,
209 		       NULL, 0, NULL, 0,
210 		       CTL_KERN, CTL_EOL);
211 
212 	sysctl_createv(clog, 0, NULL, NULL,
213 		       CTLFLAG_PERMANENT,
214 		       CTLTYPE_STRING, "ostype",
215 		       SYSCTL_DESCR("Operating system type"),
216 		       NULL, 0, __UNCONST(&ostype), 0,
217 		       CTL_KERN, KERN_OSTYPE, CTL_EOL);
218 	sysctl_createv(clog, 0, NULL, NULL,
219 		       CTLFLAG_PERMANENT,
220 		       CTLTYPE_STRING, "osrelease",
221 		       SYSCTL_DESCR("Operating system release"),
222 		       NULL, 0, __UNCONST(&osrelease), 0,
223 		       CTL_KERN, KERN_OSRELEASE, CTL_EOL);
224 	sysctl_createv(clog, 0, NULL, NULL,
225 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
226 		       CTLTYPE_INT, "osrevision",
227 		       SYSCTL_DESCR("Operating system revision"),
228 		       NULL, __NetBSD_Version__, NULL, 0,
229 		       CTL_KERN, KERN_OSREV, CTL_EOL);
230 	sysctl_createv(clog, 0, NULL, NULL,
231 		       CTLFLAG_PERMANENT,
232 		       CTLTYPE_STRING, "version",
233 		       SYSCTL_DESCR("Kernel version"),
234 		       NULL, 0, __UNCONST(&version), 0,
235 		       CTL_KERN, KERN_VERSION, CTL_EOL);
236 	sysctl_createv(clog, 0, NULL, NULL,
237 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
238 		       CTLTYPE_INT, "maxvnodes",
239 		       SYSCTL_DESCR("Maximum number of vnodes"),
240 		       sysctl_kern_maxvnodes, 0, NULL, 0,
241 		       CTL_KERN, KERN_MAXVNODES, CTL_EOL);
242 	sysctl_createv(clog, 0, NULL, NULL,
243 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
244 		       CTLTYPE_INT, "maxproc",
245 		       SYSCTL_DESCR("Maximum number of simultaneous processes"),
246 		       sysctl_kern_maxproc, 0, NULL, 0,
247 		       CTL_KERN, KERN_MAXPROC, CTL_EOL);
248 	sysctl_createv(clog, 0, NULL, NULL,
249 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
250 		       CTLTYPE_INT, "maxfiles",
251 		       SYSCTL_DESCR("Maximum number of open files"),
252 		       NULL, 0, &maxfiles, 0,
253 		       CTL_KERN, KERN_MAXFILES, CTL_EOL);
254 	sysctl_createv(clog, 0, NULL, NULL,
255 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
256 		       CTLTYPE_INT, "argmax",
257 		       SYSCTL_DESCR("Maximum number of bytes of arguments to "
258 				    "execve(2)"),
259 		       NULL, ARG_MAX, NULL, 0,
260 		       CTL_KERN, KERN_ARGMAX, CTL_EOL);
261 	sysctl_createv(clog, 0, NULL, NULL,
262 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
263 		       CTLTYPE_STRING, "hostname",
264 		       SYSCTL_DESCR("System hostname"),
265 		       sysctl_setlen, 0, &hostname, MAXHOSTNAMELEN,
266 		       CTL_KERN, KERN_HOSTNAME, CTL_EOL);
267 	sysctl_createv(clog, 0, NULL, NULL,
268 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
269 		       CTLTYPE_INT, "hostid",
270 		       SYSCTL_DESCR("System host ID number"),
271 		       sysctl_kern_hostid, 0, NULL, 0,
272 		       CTL_KERN, KERN_HOSTID, CTL_EOL);
273 	sysctl_createv(clog, 0, NULL, NULL,
274 		       CTLFLAG_PERMANENT,
275 		       CTLTYPE_STRUCT, "clockrate",
276 		       SYSCTL_DESCR("Kernel clock rates"),
277 		       sysctl_kern_clockrate, 0, NULL,
278 		       sizeof(struct clockinfo),
279 		       CTL_KERN, KERN_CLOCKRATE, CTL_EOL);
280 	sysctl_createv(clog, 0, NULL, NULL,
281 		       CTLFLAG_PERMANENT,
282 		       CTLTYPE_INT, "hardclock_ticks",
283 		       SYSCTL_DESCR("Number of hardclock ticks"),
284 		       NULL, 0, &hardclock_ticks, sizeof(hardclock_ticks),
285 		       CTL_KERN, KERN_HARDCLOCK_TICKS, CTL_EOL);
286 	sysctl_createv(clog, 0, NULL, NULL,
287 		       CTLFLAG_PERMANENT,
288 		       CTLTYPE_STRUCT, "vnode",
289 		       SYSCTL_DESCR("System vnode table"),
290 		       sysctl_kern_vnode, 0, NULL, 0,
291 		       CTL_KERN, KERN_VNODE, CTL_EOL);
292 #ifndef GPROF
293 	sysctl_createv(clog, 0, NULL, NULL,
294 		       CTLFLAG_PERMANENT,
295 		       CTLTYPE_NODE, "profiling",
296 		       SYSCTL_DESCR("Profiling information (not available)"),
297 		       sysctl_notavail, 0, NULL, 0,
298 		       CTL_KERN, KERN_PROF, CTL_EOL);
299 #endif
300 	sysctl_createv(clog, 0, NULL, NULL,
301 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
302 		       CTLTYPE_INT, "posix1version",
303 		       SYSCTL_DESCR("Version of ISO/IEC 9945 (POSIX 1003.1) "
304 				    "with which the operating system attempts "
305 				    "to comply"),
306 		       NULL, _POSIX_VERSION, NULL, 0,
307 		       CTL_KERN, KERN_POSIX1, CTL_EOL);
308 	sysctl_createv(clog, 0, NULL, NULL,
309 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
310 		       CTLTYPE_INT, "ngroups",
311 		       SYSCTL_DESCR("Maximum number of supplemental groups"),
312 		       NULL, NGROUPS_MAX, NULL, 0,
313 		       CTL_KERN, KERN_NGROUPS, CTL_EOL);
314 	sysctl_createv(clog, 0, NULL, NULL,
315 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
316 		       CTLTYPE_INT, "job_control",
317 		       SYSCTL_DESCR("Whether job control is available"),
318 		       NULL, 1, NULL, 0,
319 		       CTL_KERN, KERN_JOB_CONTROL, CTL_EOL);
320 	sysctl_createv(clog, 0, NULL, NULL,
321 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
322 		       CTLTYPE_INT, "saved_ids",
323 		       SYSCTL_DESCR("Whether POSIX saved set-group/user ID is "
324 				    "available"), NULL,
325 #ifdef _POSIX_SAVED_IDS
326 		       1,
327 #else /* _POSIX_SAVED_IDS */
328 		       0,
329 #endif /* _POSIX_SAVED_IDS */
330 		       NULL, 0, CTL_KERN, KERN_SAVED_IDS, CTL_EOL);
331 	sysctl_createv(clog, 0, NULL, NULL,
332 		       CTLFLAG_PERMANENT|CTLFLAG_HEX,
333 		       CTLTYPE_INT, "boothowto",
334 		       SYSCTL_DESCR("Flags from boot loader"),
335 		       NULL, 0, &boothowto, sizeof(boothowto),
336 		       CTL_KERN, CTL_CREATE, CTL_EOL);
337 	sysctl_createv(clog, 0, NULL, NULL,
338 		       CTLFLAG_PERMANENT,
339 		       CTLTYPE_STRUCT, "boottime",
340 		       SYSCTL_DESCR("System boot time"),
341 		       NULL, 0, &boottime, sizeof(boottime),
342 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
343 #ifdef COMPAT_50
344 	{
345 		extern struct timeval50 boottime50;
346 		sysctl_createv(clog, 0, NULL, NULL,
347 			       CTLFLAG_PERMANENT,
348 			       CTLTYPE_STRUCT, "oboottime",
349 			       SYSCTL_DESCR("System boot time"),
350 			       NULL, 0, &boottime50, sizeof(boottime50),
351 			       CTL_KERN, KERN_OBOOTTIME, CTL_EOL);
352 	}
353 #endif
354 	sysctl_createv(clog, 0, NULL, NULL,
355 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
356 		       CTLTYPE_STRING, "domainname",
357 		       SYSCTL_DESCR("YP domain name"),
358 		       sysctl_setlen, 0, &domainname, MAXHOSTNAMELEN,
359 		       CTL_KERN, KERN_DOMAINNAME, CTL_EOL);
360 	sysctl_createv(clog, 0, NULL, NULL,
361 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
362 		       CTLTYPE_INT, "maxpartitions",
363 		       SYSCTL_DESCR("Maximum number of partitions allowed per "
364 				    "disk"),
365 		       NULL, MAXPARTITIONS, NULL, 0,
366 		       CTL_KERN, KERN_MAXPARTITIONS, CTL_EOL);
367 	sysctl_createv(clog, 0, NULL, NULL,
368 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
369 		       CTLTYPE_INT, "rawpartition",
370 		       SYSCTL_DESCR("Raw partition of a disk"),
371 		       NULL, RAW_PART, NULL, 0,
372 		       CTL_KERN, KERN_RAWPARTITION, CTL_EOL);
373 	sysctl_createv(clog, 0, NULL, NULL,
374 		       CTLFLAG_PERMANENT,
375 		       CTLTYPE_STRUCT, "timex", NULL,
376 		       sysctl_notavail, 0, NULL, 0,
377 		       CTL_KERN, KERN_TIMEX, CTL_EOL);
378 	sysctl_createv(clog, 0, NULL, NULL,
379 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
380 		       CTLTYPE_INT, "rtc_offset",
381 		       SYSCTL_DESCR("Offset of real time clock from UTC in "
382 				    "minutes"),
383 		       sysctl_kern_rtc_offset, 0, &rtc_offset, 0,
384 		       CTL_KERN, KERN_RTC_OFFSET, CTL_EOL);
385 	sysctl_createv(clog, 0, NULL, NULL,
386 		       CTLFLAG_PERMANENT,
387 		       CTLTYPE_STRING, "root_device",
388 		       SYSCTL_DESCR("Name of the root device"),
389 		       sysctl_root_device, 0, NULL, 0,
390 		       CTL_KERN, KERN_ROOT_DEVICE, CTL_EOL);
391 	sysctl_createv(clog, 0, NULL, NULL,
392 		       CTLFLAG_PERMANENT,
393 		       CTLTYPE_INT, "msgbufsize",
394 		       SYSCTL_DESCR("Size of the kernel message buffer"),
395 		       sysctl_msgbuf, 0, NULL, 0,
396 		       CTL_KERN, KERN_MSGBUFSIZE, CTL_EOL);
397 	sysctl_createv(clog, 0, NULL, NULL,
398 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
399 		       CTLTYPE_INT, "fsync",
400 		       SYSCTL_DESCR("Whether the POSIX 1003.1b File "
401 				    "Synchronization Option is available on "
402 				    "this system"),
403 		       NULL, 1, NULL, 0,
404 		       CTL_KERN, KERN_FSYNC, CTL_EOL);
405 	sysctl_createv(clog, 0, NULL, NULL,
406 		       CTLFLAG_PERMANENT,
407 		       CTLTYPE_NODE, "ipc",
408 		       SYSCTL_DESCR("SysV IPC options"),
409 		       NULL, 0, NULL, 0,
410 		       CTL_KERN, KERN_SYSVIPC, CTL_EOL);
411 	sysctl_createv(clog, 0, NULL, NULL,
412 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
413 		       CTLTYPE_INT, "sysvmsg",
414 		       SYSCTL_DESCR("System V style message support available"),
415 		       NULL,
416 #ifdef SYSVMSG
417 		       1,
418 #else /* SYSVMSG */
419 		       0,
420 #endif /* SYSVMSG */
421 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_MSG, CTL_EOL);
422 	sysctl_createv(clog, 0, NULL, NULL,
423 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
424 		       CTLTYPE_INT, "sysvsem",
425 		       SYSCTL_DESCR("System V style semaphore support "
426 				    "available"), NULL,
427 #ifdef SYSVSEM
428 		       1,
429 #else /* SYSVSEM */
430 		       0,
431 #endif /* SYSVSEM */
432 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SEM, CTL_EOL);
433 	sysctl_createv(clog, 0, NULL, NULL,
434 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
435 		       CTLTYPE_INT, "sysvshm",
436 		       SYSCTL_DESCR("System V style shared memory support "
437 				    "available"), NULL,
438 #ifdef SYSVSHM
439 		       1,
440 #else /* SYSVSHM */
441 		       0,
442 #endif /* SYSVSHM */
443 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHM, CTL_EOL);
444 	sysctl_createv(clog, 0, NULL, NULL,
445 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
446 		       CTLTYPE_INT, "synchronized_io",
447 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Synchronized "
448 				    "I/O Option is available on this system"),
449 		       NULL, 1, NULL, 0,
450 		       CTL_KERN, KERN_SYNCHRONIZED_IO, CTL_EOL);
451 	sysctl_createv(clog, 0, NULL, NULL,
452 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
453 		       CTLTYPE_INT, "iov_max",
454 		       SYSCTL_DESCR("Maximum number of iovec structures per "
455 				    "process"),
456 		       NULL, IOV_MAX, NULL, 0,
457 		       CTL_KERN, KERN_IOV_MAX, CTL_EOL);
458 	sysctl_createv(clog, 0, NULL, NULL,
459 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
460 		       CTLTYPE_INT, "mapped_files",
461 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory Mapped "
462 				    "Files Option is available on this system"),
463 		       NULL, 1, NULL, 0,
464 		       CTL_KERN, KERN_MAPPED_FILES, CTL_EOL);
465 	sysctl_createv(clog, 0, NULL, NULL,
466 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
467 		       CTLTYPE_INT, "memlock",
468 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Process Memory "
469 				    "Locking Option is available on this "
470 				    "system"),
471 		       NULL, 1, NULL, 0,
472 		       CTL_KERN, KERN_MEMLOCK, CTL_EOL);
473 	sysctl_createv(clog, 0, NULL, NULL,
474 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
475 		       CTLTYPE_INT, "memlock_range",
476 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Range Memory "
477 				    "Locking Option is available on this "
478 				    "system"),
479 		       NULL, 1, NULL, 0,
480 		       CTL_KERN, KERN_MEMLOCK_RANGE, CTL_EOL);
481 	sysctl_createv(clog, 0, NULL, NULL,
482 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
483 		       CTLTYPE_INT, "memory_protection",
484 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory "
485 				    "Protection Option is available on this "
486 				    "system"),
487 		       NULL, 1, NULL, 0,
488 		       CTL_KERN, KERN_MEMORY_PROTECTION, CTL_EOL);
489 	sysctl_createv(clog, 0, NULL, NULL,
490 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
491 		       CTLTYPE_INT, "login_name_max",
492 		       SYSCTL_DESCR("Maximum login name length"),
493 		       NULL, LOGIN_NAME_MAX, NULL, 0,
494 		       CTL_KERN, KERN_LOGIN_NAME_MAX, CTL_EOL);
495 	sysctl_createv(clog, 0, NULL, NULL,
496 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
497 		       CTLTYPE_STRING, "defcorename",
498 		       SYSCTL_DESCR("Default core file name"),
499 		       sysctl_kern_defcorename, 0, defcorename, MAXPATHLEN,
500 		       CTL_KERN, KERN_DEFCORENAME, CTL_EOL);
501 	sysctl_createv(clog, 0, NULL, NULL,
502 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
503 		       CTLTYPE_INT, "logsigexit",
504 		       SYSCTL_DESCR("Log process exit when caused by signals"),
505 		       NULL, 0, &kern_logsigexit, 0,
506 		       CTL_KERN, KERN_LOGSIGEXIT, CTL_EOL);
507 	sysctl_createv(clog, 0, NULL, NULL,
508 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
509 		       CTLTYPE_INT, "fscale",
510 		       SYSCTL_DESCR("Kernel fixed-point scale factor"),
511 		       NULL, FSCALE, NULL, 0,
512 		       CTL_KERN, KERN_FSCALE, CTL_EOL);
513 	sysctl_createv(clog, 0, NULL, NULL,
514 		       CTLFLAG_PERMANENT,
515 		       CTLTYPE_INT, "ccpu",
516 		       SYSCTL_DESCR("Scheduler exponential decay value"),
517 		       NULL, 0, &ccpu, 0,
518 		       CTL_KERN, KERN_CCPU, CTL_EOL);
519 	sysctl_createv(clog, 0, NULL, NULL,
520 		       CTLFLAG_PERMANENT,
521 		       CTLTYPE_STRUCT, "cp_time",
522 		       SYSCTL_DESCR("Clock ticks spent in different CPU states"),
523 		       sysctl_kern_cptime, 0, NULL, 0,
524 		       CTL_KERN, KERN_CP_TIME, CTL_EOL);
525 	sysctl_createv(clog, 0, NULL, NULL,
526 		       CTLFLAG_PERMANENT,
527 		       CTLTYPE_INT, "msgbuf",
528 		       SYSCTL_DESCR("Kernel message buffer"),
529 		       sysctl_msgbuf, 0, NULL, 0,
530 		       CTL_KERN, KERN_MSGBUF, CTL_EOL);
531 	sysctl_createv(clog, 0, NULL, NULL,
532 		       CTLFLAG_PERMANENT,
533 		       CTLTYPE_STRUCT, "consdev",
534 		       SYSCTL_DESCR("Console device"),
535 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
536 		       CTL_KERN, KERN_CONSDEV, CTL_EOL);
537 #if NPTY > 0
538 	sysctl_createv(clog, 0, NULL, NULL,
539 		       CTLFLAG_PERMANENT,
540 		       CTLTYPE_INT, "maxptys",
541 		       SYSCTL_DESCR("Maximum number of pseudo-ttys"),
542 		       sysctl_kern_maxptys, 0, NULL, 0,
543 		       CTL_KERN, KERN_MAXPTYS, CTL_EOL);
544 #endif /* NPTY > 0 */
545 	sysctl_createv(clog, 0, NULL, NULL,
546 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
547 		       CTLTYPE_INT, "maxphys",
548 		       SYSCTL_DESCR("Maximum raw I/O transfer size"),
549 		       NULL, MAXPHYS, NULL, 0,
550 		       CTL_KERN, KERN_MAXPHYS, CTL_EOL);
551 	sysctl_createv(clog, 0, NULL, NULL,
552 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
553 		       CTLTYPE_INT, "sbmax",
554 		       SYSCTL_DESCR("Maximum socket buffer size"),
555 		       sysctl_kern_sbmax, 0, NULL, 0,
556 		       CTL_KERN, KERN_SBMAX, CTL_EOL);
557 	sysctl_createv(clog, 0, NULL, NULL,
558 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
559 		       CTLTYPE_INT, "monotonic_clock",
560 		       SYSCTL_DESCR("Implementation version of the POSIX "
561 				    "1003.1b Monotonic Clock Option"),
562 		       /* XXX _POSIX_VERSION */
563 		       NULL, _POSIX_MONOTONIC_CLOCK, NULL, 0,
564 		       CTL_KERN, KERN_MONOTONIC_CLOCK, CTL_EOL);
565 	sysctl_createv(clog, 0, NULL, NULL,
566 		       CTLFLAG_PERMANENT,
567 		       CTLTYPE_INT, "urandom",
568 		       SYSCTL_DESCR("Random integer value"),
569 		       sysctl_kern_urnd, 0, NULL, 0,
570 		       CTL_KERN, KERN_URND, CTL_EOL);
571 	sysctl_createv(clog, 0, NULL, NULL,
572 		       CTLFLAG_PERMANENT,
573 		       CTLTYPE_INT, "arandom",
574 		       SYSCTL_DESCR("n bytes of random data"),
575 		       sysctl_kern_arnd, 0, NULL, 0,
576 		       CTL_KERN, KERN_ARND, CTL_EOL);
577 	sysctl_createv(clog, 0, NULL, NULL,
578 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
579 		       CTLTYPE_INT, "labelsector",
580 		       SYSCTL_DESCR("Sector number containing the disklabel"),
581 		       NULL, LABELSECTOR, NULL, 0,
582 		       CTL_KERN, KERN_LABELSECTOR, CTL_EOL);
583 	sysctl_createv(clog, 0, NULL, NULL,
584 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
585 		       CTLTYPE_INT, "labeloffset",
586 		       SYSCTL_DESCR("Offset of the disklabel within the "
587 				    "sector"),
588 		       NULL, LABELOFFSET, NULL, 0,
589 		       CTL_KERN, KERN_LABELOFFSET, CTL_EOL);
590 	sysctl_createv(clog, 0, NULL, NULL,
591 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
592 		       CTLTYPE_INT, "labelusesmbr",
593 		       SYSCTL_DESCR("disklabel is inside MBR partition"),
594 		       NULL, LABELUSESMBR, NULL, 0,
595 		       CTL_KERN, CTL_CREATE, CTL_EOL);
596 	sysctl_createv(clog, 0, NULL, NULL,
597 		       CTLFLAG_PERMANENT,
598 		       CTLTYPE_NODE, "lwp",
599 		       SYSCTL_DESCR("System-wide LWP information"),
600 		       sysctl_kern_lwp, 0, NULL, 0,
601 		       CTL_KERN, KERN_LWP, CTL_EOL);
602 	sysctl_createv(clog, 0, NULL, NULL,
603 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
604 		       CTLTYPE_INT, "forkfsleep",
605 		       SYSCTL_DESCR("Milliseconds to sleep on fork failure due "
606 				    "to process limits"),
607 		       sysctl_kern_forkfsleep, 0, NULL, 0,
608 		       CTL_KERN, KERN_FORKFSLEEP, CTL_EOL);
609 	sysctl_createv(clog, 0, NULL, NULL,
610 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
611 		       CTLTYPE_INT, "posix_threads",
612 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
613 				    "Threads option to which the system "
614 				    "attempts to conform"),
615 		       /* XXX _POSIX_VERSION */
616 		       NULL, _POSIX_THREADS, NULL, 0,
617 		       CTL_KERN, KERN_POSIX_THREADS, CTL_EOL);
618 	sysctl_createv(clog, 0, NULL, NULL,
619 		       CTLFLAG_PERMANENT,
620 		       CTLTYPE_INT, "posix_semaphores",
621 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
622 				    "Semaphores option to which the system "
623 				    "attempts to conform"), NULL,
624 		       0, &posix_semaphores,
625 		       0, CTL_KERN, KERN_POSIX_SEMAPHORES, CTL_EOL);
626 	sysctl_createv(clog, 0, NULL, NULL,
627 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
628 		       CTLTYPE_INT, "posix_barriers",
629 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
630 				    "Barriers option to which the system "
631 				    "attempts to conform"),
632 		       /* XXX _POSIX_VERSION */
633 		       NULL, _POSIX_BARRIERS, NULL, 0,
634 		       CTL_KERN, KERN_POSIX_BARRIERS, CTL_EOL);
635 	sysctl_createv(clog, 0, NULL, NULL,
636 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
637 		       CTLTYPE_INT, "posix_timers",
638 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
639 				    "Timers option to which the system "
640 				    "attempts to conform"),
641 		       /* XXX _POSIX_VERSION */
642 		       NULL, _POSIX_TIMERS, NULL, 0,
643 		       CTL_KERN, KERN_POSIX_TIMERS, CTL_EOL);
644 	sysctl_createv(clog, 0, NULL, NULL,
645 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
646 		       CTLTYPE_INT, "posix_spin_locks",
647 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its Spin "
648 				    "Locks option to which the system attempts "
649 				    "to conform"),
650 		       /* XXX _POSIX_VERSION */
651 		       NULL, _POSIX_SPIN_LOCKS, NULL, 0,
652 		       CTL_KERN, KERN_POSIX_SPIN_LOCKS, CTL_EOL);
653 	sysctl_createv(clog, 0, NULL, NULL,
654 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
655 		       CTLTYPE_INT, "posix_reader_writer_locks",
656 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
657 				    "Read-Write Locks option to which the "
658 				    "system attempts to conform"),
659 		       /* XXX _POSIX_VERSION */
660 		       NULL, _POSIX_READER_WRITER_LOCKS, NULL, 0,
661 		       CTL_KERN, KERN_POSIX_READER_WRITER_LOCKS, CTL_EOL);
662 	sysctl_createv(clog, 0, NULL, NULL,
663 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
664 		       CTLTYPE_INT, "dump_on_panic",
665 		       SYSCTL_DESCR("Perform a crash dump on system panic"),
666 		       NULL, 0, &dumponpanic, 0,
667 		       CTL_KERN, KERN_DUMP_ON_PANIC, CTL_EOL);
668 #ifdef DIAGNOSTIC
669 	sysctl_createv(clog, 0, NULL, NULL,
670 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
671 		       CTLTYPE_INT, "panic_now",
672 		       SYSCTL_DESCR("Trigger a panic"),
673 		       sysctl_kern_trigger_panic, 0, NULL, 0,
674 		       CTL_KERN, CTL_CREATE, CTL_EOL);
675 #endif
676 	sysctl_createv(clog, 0, NULL, NULL,
677 		       CTLFLAG_PERMANENT,
678 		       CTLTYPE_INT, "root_partition",
679 		       SYSCTL_DESCR("Root partition on the root device"),
680 		       sysctl_kern_root_partition, 0, NULL, 0,
681 		       CTL_KERN, KERN_ROOT_PARTITION, CTL_EOL);
682 	sysctl_createv(clog, 0, NULL, NULL,
683 		       CTLFLAG_PERMANENT,
684 		       CTLTYPE_STRUCT, "drivers",
685 		       SYSCTL_DESCR("List of all drivers with block and "
686 				    "character device numbers"),
687 		       sysctl_kern_drivers, 0, NULL, 0,
688 		       CTL_KERN, KERN_DRIVERS, CTL_EOL);
689 	sysctl_createv(clog, 0, NULL, NULL,
690 		       CTLFLAG_PERMANENT,
691 		       CTLTYPE_STRUCT, "cp_id",
692 		       SYSCTL_DESCR("Mapping of CPU number to CPU id"),
693 		       sysctl_kern_cpid, 0, NULL, 0,
694 		       CTL_KERN, KERN_CP_ID, CTL_EOL);
695 	sysctl_createv(clog, 0, NULL, &rnode,
696 		       CTLFLAG_PERMANENT,
697 		       CTLTYPE_NODE, "coredump",
698 		       SYSCTL_DESCR("Coredump settings."),
699 		       NULL, 0, NULL, 0,
700 		       CTL_KERN, CTL_CREATE, CTL_EOL);
701 	sysctl_createv(clog, 0, &rnode, &rnode,
702 		       CTLFLAG_PERMANENT,
703 		       CTLTYPE_NODE, "setid",
704 		       SYSCTL_DESCR("Set-id processes' coredump settings."),
705 		       NULL, 0, NULL, 0,
706 		       CTL_CREATE, CTL_EOL);
707 	sysctl_createv(clog, 0, &rnode, NULL,
708 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
709 		       CTLTYPE_INT, "dump",
710 		       SYSCTL_DESCR("Allow set-id processes to dump core."),
711 		       sysctl_security_setidcore, 0, &security_setidcore_dump,
712 		       sizeof(security_setidcore_dump),
713 		       CTL_CREATE, CTL_EOL);
714 	sysctl_createv(clog, 0, &rnode, NULL,
715 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
716 		       CTLTYPE_STRING, "path",
717 		       SYSCTL_DESCR("Path pattern for set-id coredumps."),
718 		       sysctl_security_setidcorename, 0,
719 		       &security_setidcore_path,
720 		       sizeof(security_setidcore_path),
721 		       CTL_CREATE, CTL_EOL);
722 	sysctl_createv(clog, 0, &rnode, NULL,
723 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
724 		       CTLTYPE_INT, "owner",
725 		       SYSCTL_DESCR("Owner id for set-id processes' cores."),
726 		       sysctl_security_setidcore, 0, &security_setidcore_owner,
727 		       0,
728 		       CTL_CREATE, CTL_EOL);
729 	sysctl_createv(clog, 0, &rnode, NULL,
730 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
731 		       CTLTYPE_INT, "group",
732 		       SYSCTL_DESCR("Group id for set-id processes' cores."),
733 		       sysctl_security_setidcore, 0, &security_setidcore_group,
734 		       0,
735 		       CTL_CREATE, CTL_EOL);
736 	sysctl_createv(clog, 0, &rnode, NULL,
737 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
738 		       CTLTYPE_INT, "mode",
739 		       SYSCTL_DESCR("Mode for set-id processes' cores."),
740 		       sysctl_security_setidcore, 0, &security_setidcore_mode,
741 		       0,
742 		       CTL_CREATE, CTL_EOL);
743 	sysctl_createv(clog, 0, NULL, NULL,
744 #ifndef KERN_SA
745 		       CTLFLAG_IMMEDIATE|
746 #endif
747 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
748 		       CTLTYPE_INT, "no_sa_support",
749 		       SYSCTL_DESCR("0 if the kernel supports SA, otherwise "
750 		       "it doesn't"),
751 		       NULL,
752 #ifndef KERN_SA
753 		       1, NULL,
754 #else
755 		       0, &sa_system_disabled,
756 #endif
757 		       0,
758 		       CTL_KERN, CTL_CREATE, CTL_EOL);
759 	/* kern.posix. */
760 	sysctl_createv(clog, 0, NULL, &rnode,
761 			CTLFLAG_PERMANENT,
762 			CTLTYPE_NODE, "posix",
763 			SYSCTL_DESCR("POSIX options"),
764 			NULL, 0, NULL, 0,
765 			CTL_KERN, CTL_CREATE, CTL_EOL);
766 	sysctl_createv(clog, 0, &rnode, NULL,
767 			CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
768 			CTLTYPE_INT, "semmax",
769 			SYSCTL_DESCR("Maximal number of semaphores"),
770 			NULL, 0, &ksem_max, 0,
771 			CTL_CREATE, CTL_EOL);
772 }
773 
774 SYSCTL_SETUP(sysctl_hw_setup, "sysctl hw subtree setup")
775 {
776 	u_int u;
777 	u_quad_t q;
778 
779 	sysctl_createv(clog, 0, NULL, NULL,
780 		       CTLFLAG_PERMANENT,
781 		       CTLTYPE_NODE, "hw", NULL,
782 		       NULL, 0, NULL, 0,
783 		       CTL_HW, CTL_EOL);
784 
785 	sysctl_createv(clog, 0, NULL, NULL,
786 		       CTLFLAG_PERMANENT,
787 		       CTLTYPE_STRING, "machine",
788 		       SYSCTL_DESCR("Machine class"),
789 		       NULL, 0, machine, 0,
790 		       CTL_HW, HW_MACHINE, CTL_EOL);
791 	sysctl_createv(clog, 0, NULL, NULL,
792 		       CTLFLAG_PERMANENT,
793 		       CTLTYPE_STRING, "model",
794 		       SYSCTL_DESCR("Machine model"),
795 		       NULL, 0, cpu_model, 0,
796 		       CTL_HW, HW_MODEL, CTL_EOL);
797 	sysctl_createv(clog, 0, NULL, NULL,
798 		       CTLFLAG_PERMANENT,
799 		       CTLTYPE_INT, "ncpu",
800 		       SYSCTL_DESCR("Number of CPUs configured"),
801 		       NULL, 0, &ncpu, 0,
802 		       CTL_HW, HW_NCPU, CTL_EOL);
803 	sysctl_createv(clog, 0, NULL, NULL,
804 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
805 		       CTLTYPE_INT, "byteorder",
806 		       SYSCTL_DESCR("System byte order"),
807 		       NULL, BYTE_ORDER, NULL, 0,
808 		       CTL_HW, HW_BYTEORDER, CTL_EOL);
809 	u = ((u_int)physmem > (UINT_MAX / PAGE_SIZE)) ?
810 		UINT_MAX : physmem * PAGE_SIZE;
811 	sysctl_createv(clog, 0, NULL, NULL,
812 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
813 		       CTLTYPE_INT, "physmem",
814 		       SYSCTL_DESCR("Bytes of physical memory"),
815 		       NULL, u, NULL, 0,
816 		       CTL_HW, HW_PHYSMEM, CTL_EOL);
817 	sysctl_createv(clog, 0, NULL, NULL,
818 		       CTLFLAG_PERMANENT,
819 		       CTLTYPE_INT, "usermem",
820 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
821 		       sysctl_hw_usermem, 0, NULL, 0,
822 		       CTL_HW, HW_USERMEM, CTL_EOL);
823 	sysctl_createv(clog, 0, NULL, NULL,
824 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
825 		       CTLTYPE_INT, "pagesize",
826 		       SYSCTL_DESCR("Software page size"),
827 		       NULL, PAGE_SIZE, NULL, 0,
828 		       CTL_HW, HW_PAGESIZE, CTL_EOL);
829 	sysctl_createv(clog, 0, NULL, NULL,
830 		       CTLFLAG_PERMANENT,
831 		       CTLTYPE_STRING, "machine_arch",
832 		       SYSCTL_DESCR("Machine CPU class"),
833 		       NULL, 0, machine_arch, 0,
834 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
835 	sysctl_createv(clog, 0, NULL, NULL,
836 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
837 		       CTLTYPE_INT, "alignbytes",
838 		       SYSCTL_DESCR("Alignment constraint for all possible "
839 				    "data types"),
840 		       NULL, ALIGNBYTES, NULL, 0,
841 		       CTL_HW, HW_ALIGNBYTES, CTL_EOL);
842 	sysctl_createv(clog, 0, NULL, NULL,
843 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
844 		       CTLTYPE_STRING, "cnmagic",
845 		       SYSCTL_DESCR("Console magic key sequence"),
846 		       sysctl_hw_cnmagic, 0, NULL, CNS_LEN,
847 		       CTL_HW, HW_CNMAGIC, CTL_EOL);
848 	q = (u_quad_t)physmem * PAGE_SIZE;
849 	sysctl_createv(clog, 0, NULL, NULL,
850 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
851 		       CTLTYPE_QUAD, "physmem64",
852 		       SYSCTL_DESCR("Bytes of physical memory"),
853 		       NULL, q, NULL, 0,
854 		       CTL_HW, HW_PHYSMEM64, CTL_EOL);
855 	sysctl_createv(clog, 0, NULL, NULL,
856 		       CTLFLAG_PERMANENT,
857 		       CTLTYPE_QUAD, "usermem64",
858 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
859 		       sysctl_hw_usermem, 0, NULL, 0,
860 		       CTL_HW, HW_USERMEM64, CTL_EOL);
861 	sysctl_createv(clog, 0, NULL, NULL,
862 		       CTLFLAG_PERMANENT,
863 		       CTLTYPE_INT, "ncpuonline",
864 		       SYSCTL_DESCR("Number of CPUs online"),
865 		       NULL, 0, &ncpuonline, 0,
866 		       CTL_HW, HW_NCPUONLINE, CTL_EOL);
867 }
868 
869 #ifdef DEBUG
870 /*
871  * Debugging related system variables.
872  */
873 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
874 struct ctldebug debug5, debug6, debug7, debug8, debug9;
875 struct ctldebug debug10, debug11, debug12, debug13, debug14;
876 struct ctldebug debug15, debug16, debug17, debug18, debug19;
877 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
878 	&debug0, &debug1, &debug2, &debug3, &debug4,
879 	&debug5, &debug6, &debug7, &debug8, &debug9,
880 	&debug10, &debug11, &debug12, &debug13, &debug14,
881 	&debug15, &debug16, &debug17, &debug18, &debug19,
882 };
883 
884 /*
885  * this setup routine is a replacement for debug_sysctl()
886  *
887  * note that it creates several nodes per defined debug variable
888  */
889 SYSCTL_SETUP(sysctl_debug_setup, "sysctl debug subtree setup")
890 {
891 	struct ctldebug *cdp;
892 	char nodename[20];
893 	int i;
894 
895 	/*
896 	 * two ways here:
897 	 *
898 	 * the "old" way (debug.name -> value) which was emulated by
899 	 * the sysctl(8) binary
900 	 *
901 	 * the new way, which the sysctl(8) binary was actually using
902 
903 	 node	debug
904 	 node	debug.0
905 	 string debug.0.name
906 	 int	debug.0.value
907 	 int	debug.name
908 
909 	 */
910 
911 	sysctl_createv(clog, 0, NULL, NULL,
912 		       CTLFLAG_PERMANENT,
913 		       CTLTYPE_NODE, "debug", NULL,
914 		       NULL, 0, NULL, 0,
915 		       CTL_DEBUG, CTL_EOL);
916 
917 	for (i = 0; i < CTL_DEBUG_MAXID; i++) {
918 		cdp = debugvars[i];
919 		if (cdp->debugname == NULL || cdp->debugvar == NULL)
920 			continue;
921 
922 		snprintf(nodename, sizeof(nodename), "debug%d", i);
923 		sysctl_createv(clog, 0, NULL, NULL,
924 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
925 			       CTLTYPE_NODE, nodename, NULL,
926 			       NULL, 0, NULL, 0,
927 			       CTL_DEBUG, i, CTL_EOL);
928 		sysctl_createv(clog, 0, NULL, NULL,
929 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
930 			       CTLTYPE_STRING, "name", NULL,
931 			       /*XXXUNCONST*/
932 			       NULL, 0, __UNCONST(cdp->debugname), 0,
933 			       CTL_DEBUG, i, CTL_DEBUG_NAME, CTL_EOL);
934 		sysctl_createv(clog, 0, NULL, NULL,
935 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
936 			       CTLTYPE_INT, "value", NULL,
937 			       NULL, 0, cdp->debugvar, 0,
938 			       CTL_DEBUG, i, CTL_DEBUG_VALUE, CTL_EOL);
939 		sysctl_createv(clog, 0, NULL, NULL,
940 			       CTLFLAG_PERMANENT,
941 			       CTLTYPE_INT, cdp->debugname, NULL,
942 			       NULL, 0, cdp->debugvar, 0,
943 			       CTL_DEBUG, CTL_CREATE, CTL_EOL);
944 	}
945 }
946 #endif /* DEBUG */
947 
948 /*
949  * ********************************************************************
950  * section 2: private node-specific helper routines.
951  * ********************************************************************
952  */
953 
954 #ifdef DIAGNOSTIC
955 static int
956 sysctl_kern_trigger_panic(SYSCTLFN_ARGS)
957 {
958 	int newtrig, error;
959 	struct sysctlnode node;
960 
961 	newtrig = 0;
962 	node = *rnode;
963 	node.sysctl_data = &newtrig;
964 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
965 	if (error || newp == NULL)
966 		return (error);
967 
968 	if (newtrig != 0)
969 		panic("Panic triggered");
970 
971 	return (error);
972 }
973 #endif
974 
975 /*
976  * sysctl helper routine for kern.maxvnodes.  Drain vnodes if
977  * new value is lower than desiredvnodes and then calls reinit
978  * routines that needs to adjust to the new value.
979  */
980 static int
981 sysctl_kern_maxvnodes(SYSCTLFN_ARGS)
982 {
983 	int error, new_vnodes, old_vnodes, new_max;
984 	struct sysctlnode node;
985 
986 	new_vnodes = desiredvnodes;
987 	node = *rnode;
988 	node.sysctl_data = &new_vnodes;
989 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
990 	if (error || newp == NULL)
991 		return (error);
992 
993 	/* Limits: 75% of KVA and physical memory. */
994 	new_max = calc_cache_size(kernel_map, 75, 75) / VNODE_COST;
995 	if (new_vnodes > new_max)
996 		new_vnodes = new_max;
997 
998 	old_vnodes = desiredvnodes;
999 	desiredvnodes = new_vnodes;
1000 	if (new_vnodes < old_vnodes) {
1001 		error = vfs_drainvnodes(new_vnodes);
1002 		if (error) {
1003 			desiredvnodes = old_vnodes;
1004 			return (error);
1005 		}
1006 	}
1007 	vfs_reinit();
1008 	nchreinit();
1009 
1010 	return (0);
1011 }
1012 
1013 /*
1014  * sysctl helper routine for rtc_offset - set time after changes
1015  */
1016 static int
1017 sysctl_kern_rtc_offset(SYSCTLFN_ARGS)
1018 {
1019 	struct timespec ts, delta;
1020 	int error, new_rtc_offset;
1021 	struct sysctlnode node;
1022 
1023 	new_rtc_offset = rtc_offset;
1024 	node = *rnode;
1025 	node.sysctl_data = &new_rtc_offset;
1026 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1027 	if (error || newp == NULL)
1028 		return (error);
1029 
1030 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
1031 	    KAUTH_REQ_SYSTEM_TIME_RTCOFFSET,
1032 	    KAUTH_ARG(new_rtc_offset), NULL, NULL))
1033 		return (EPERM);
1034 	if (rtc_offset == new_rtc_offset)
1035 		return (0);
1036 
1037 	/* if we change the offset, adjust the time */
1038 	nanotime(&ts);
1039 	delta.tv_sec = 60 * (new_rtc_offset - rtc_offset);
1040 	delta.tv_nsec = 0;
1041 	timespecadd(&ts, &delta, &ts);
1042 	rtc_offset = new_rtc_offset;
1043 	return (settime(l->l_proc, &ts));
1044 }
1045 
1046 /*
1047  * sysctl helper routine for kern.maxproc. Ensures that the new
1048  * values are not too low or too high.
1049  */
1050 static int
1051 sysctl_kern_maxproc(SYSCTLFN_ARGS)
1052 {
1053 	int error, nmaxproc;
1054 	struct sysctlnode node;
1055 
1056 	nmaxproc = maxproc;
1057 	node = *rnode;
1058 	node.sysctl_data = &nmaxproc;
1059 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1060 	if (error || newp == NULL)
1061 		return (error);
1062 
1063 	if (nmaxproc < 0 || nmaxproc >= PID_MAX)
1064 		return (EINVAL);
1065 #ifdef __HAVE_CPU_MAXPROC
1066 	if (nmaxproc > cpu_maxproc())
1067 		return (EINVAL);
1068 #endif
1069 	maxproc = nmaxproc;
1070 
1071 	return (0);
1072 }
1073 
1074 /*
1075  * sysctl helper function for kern.hostid. The hostid is a long, but
1076  * we export it as an int, so we need to give it a little help.
1077  */
1078 static int
1079 sysctl_kern_hostid(SYSCTLFN_ARGS)
1080 {
1081 	int error, inthostid;
1082 	struct sysctlnode node;
1083 
1084 	inthostid = hostid;  /* XXX assumes sizeof int <= sizeof long */
1085 	node = *rnode;
1086 	node.sysctl_data = &inthostid;
1087 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1088 	if (error || newp == NULL)
1089 		return (error);
1090 
1091 	hostid = (unsigned)inthostid;
1092 
1093 	return (0);
1094 }
1095 
1096 /*
1097  * sysctl helper function for kern.hostname and kern.domainnname.
1098  * resets the relevant recorded length when the underlying name is
1099  * changed.
1100  */
1101 static int
1102 sysctl_setlen(SYSCTLFN_ARGS)
1103 {
1104 	int error;
1105 
1106 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
1107 	if (error || newp == NULL)
1108 		return (error);
1109 
1110 	switch (rnode->sysctl_num) {
1111 	case KERN_HOSTNAME:
1112 		hostnamelen = strlen((const char*)rnode->sysctl_data);
1113 		break;
1114 	case KERN_DOMAINNAME:
1115 		domainnamelen = strlen((const char*)rnode->sysctl_data);
1116 		break;
1117 	}
1118 
1119 	return (0);
1120 }
1121 
1122 /*
1123  * sysctl helper routine for kern.clockrate. Assembles a struct on
1124  * the fly to be returned to the caller.
1125  */
1126 static int
1127 sysctl_kern_clockrate(SYSCTLFN_ARGS)
1128 {
1129 	struct clockinfo clkinfo;
1130 	struct sysctlnode node;
1131 
1132 	clkinfo.tick = tick;
1133 	clkinfo.tickadj = tickadj;
1134 	clkinfo.hz = hz;
1135 	clkinfo.profhz = profhz;
1136 	clkinfo.stathz = stathz ? stathz : hz;
1137 
1138 	node = *rnode;
1139 	node.sysctl_data = &clkinfo;
1140 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1141 }
1142 
1143 /*
1144  * sysctl helper routine for kern.msgbufsize and kern.msgbuf. For the
1145  * former it merely checks the message buffer is set up. For the latter,
1146  * it also copies out the data if necessary.
1147  */
1148 static int
1149 sysctl_msgbuf(SYSCTLFN_ARGS)
1150 {
1151 	char *where = oldp;
1152 	size_t len, maxlen;
1153 	long beg, end;
1154 	extern kmutex_t log_lock;
1155 	int error;
1156 
1157 	if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
1158 		msgbufenabled = 0;
1159 		return (ENXIO);
1160 	}
1161 
1162 	switch (rnode->sysctl_num) {
1163 	case KERN_MSGBUFSIZE: {
1164 		struct sysctlnode node = *rnode;
1165 		int msg_bufs = (int)msgbufp->msg_bufs;
1166 		node.sysctl_data = &msg_bufs;
1167 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1168 	}
1169 	case KERN_MSGBUF:
1170 		break;
1171 	default:
1172 		return (EOPNOTSUPP);
1173 	}
1174 
1175 	if (newp != NULL)
1176 		return (EPERM);
1177 
1178 	if (oldp == NULL) {
1179 		/* always return full buffer size */
1180 		*oldlenp = msgbufp->msg_bufs;
1181 		return (0);
1182 	}
1183 
1184 	sysctl_unlock();
1185 
1186 	/*
1187 	 * First, copy from the write pointer to the end of
1188 	 * message buffer.
1189 	 */
1190 	error = 0;
1191 	mutex_spin_enter(&log_lock);
1192 	maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
1193 	beg = msgbufp->msg_bufx;
1194 	end = msgbufp->msg_bufs;
1195 	mutex_spin_exit(&log_lock);
1196 
1197 	while (maxlen > 0) {
1198 		len = MIN(end - beg, maxlen);
1199 		if (len == 0)
1200 			break;
1201 		/* XXX unlocked, but hardly matters. */
1202 		error = dcopyout(l, &msgbufp->msg_bufc[beg], where, len);
1203 		if (error)
1204 			break;
1205 		where += len;
1206 		maxlen -= len;
1207 
1208 		/*
1209 		 * ... then, copy from the beginning of message buffer to
1210 		 * the write pointer.
1211 		 */
1212 		beg = 0;
1213 		end = msgbufp->msg_bufx;
1214 	}
1215 
1216 	sysctl_relock();
1217 	return (error);
1218 }
1219 
1220 /*
1221  * sysctl helper routine for kern.defcorename. In the case of a new
1222  * string being assigned, check that it's not a zero-length string.
1223  * (XXX the check in -current doesn't work, but do we really care?)
1224  */
1225 static int
1226 sysctl_kern_defcorename(SYSCTLFN_ARGS)
1227 {
1228 	int error;
1229 	char *newcorename;
1230 	struct sysctlnode node;
1231 
1232 	newcorename = PNBUF_GET();
1233 	node = *rnode;
1234 	node.sysctl_data = &newcorename[0];
1235 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
1236 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1237 	if (error || newp == NULL) {
1238 		goto done;
1239 	}
1240 
1241 	/*
1242 	 * when sysctl_lookup() deals with a string, it's guaranteed
1243 	 * to come back nul terminated. So there.  :)
1244 	 */
1245 	if (strlen(newcorename) == 0) {
1246 		error = EINVAL;
1247 	} else {
1248 		memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
1249 		error = 0;
1250 	}
1251 done:
1252 	PNBUF_PUT(newcorename);
1253 	return error;
1254 }
1255 
1256 /*
1257  * sysctl helper routine for kern.cp_time node. Adds up cpu time
1258  * across all cpus.
1259  */
1260 static int
1261 sysctl_kern_cptime(SYSCTLFN_ARGS)
1262 {
1263 	struct sysctlnode node = *rnode;
1264 	uint64_t *cp_time = NULL;
1265 	int error, n = ncpu, i;
1266 	struct cpu_info *ci;
1267 	CPU_INFO_ITERATOR cii;
1268 
1269 	/*
1270 	 * if you specifically pass a buffer that is the size of the
1271 	 * sum, or if you are probing for the size, you get the "sum"
1272 	 * of cp_time (and the size thereof) across all processors.
1273 	 *
1274 	 * alternately, you can pass an additional mib number and get
1275 	 * cp_time for that particular processor.
1276 	 */
1277 	switch (namelen) {
1278 	case 0:
1279 		if (*oldlenp == sizeof(uint64_t) * CPUSTATES || oldp == NULL) {
1280 			node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
1281 			n = -1; /* SUM */
1282 		}
1283 		else {
1284 			node.sysctl_size = n * sizeof(uint64_t) * CPUSTATES;
1285 			n = -2; /* ALL */
1286 		}
1287 		break;
1288 	case 1:
1289 		if (name[0] < 0 || name[0] >= n)
1290 			return (ENOENT); /* ENOSUCHPROCESSOR */
1291 		node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
1292 		n = name[0];
1293 		/*
1294 		 * adjust these so that sysctl_lookup() will be happy
1295 		 */
1296 		name++;
1297 		namelen--;
1298 		break;
1299 	default:
1300 		return (EINVAL);
1301 	}
1302 
1303 	cp_time = kmem_alloc(node.sysctl_size, KM_SLEEP);
1304 	if (cp_time == NULL)
1305 		return (ENOMEM);
1306 	node.sysctl_data = cp_time;
1307 	memset(cp_time, 0, node.sysctl_size);
1308 
1309 	for (CPU_INFO_FOREACH(cii, ci)) {
1310 		if (n <= 0) {
1311 			for (i = 0; i < CPUSTATES; i++) {
1312 				cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
1313 			}
1314 		}
1315 		/*
1316 		 * if a specific processor was requested and we just
1317 		 * did it, we're done here
1318 		 */
1319 		if (n == 0)
1320 			break;
1321 		/*
1322 		 * if doing "all", skip to next cp_time set for next processor
1323 		 */
1324 		if (n == -2)
1325 			cp_time += CPUSTATES;
1326 		/*
1327 		 * if we're doing a specific processor, we're one
1328 		 * processor closer
1329 		 */
1330 		if (n > 0)
1331 			n--;
1332 	}
1333 
1334 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1335 	kmem_free(node.sysctl_data, node.sysctl_size);
1336 	return (error);
1337 }
1338 
1339 #if NPTY > 0
1340 /*
1341  * sysctl helper routine for kern.maxptys. Ensures that any new value
1342  * is acceptable to the pty subsystem.
1343  */
1344 static int
1345 sysctl_kern_maxptys(SYSCTLFN_ARGS)
1346 {
1347 	int pty_maxptys(int, int);		/* defined in kern/tty_pty.c */
1348 	int error, xmax;
1349 	struct sysctlnode node;
1350 
1351 	/* get current value of maxptys */
1352 	xmax = pty_maxptys(0, 0);
1353 
1354 	node = *rnode;
1355 	node.sysctl_data = &xmax;
1356 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1357 	if (error || newp == NULL)
1358 		return (error);
1359 
1360 	if (xmax != pty_maxptys(xmax, 1))
1361 		return (EINVAL);
1362 
1363 	return (0);
1364 }
1365 #endif /* NPTY > 0 */
1366 
1367 /*
1368  * sysctl helper routine for kern.sbmax. Basically just ensures that
1369  * any new value is not too small.
1370  */
1371 static int
1372 sysctl_kern_sbmax(SYSCTLFN_ARGS)
1373 {
1374 	int error, new_sbmax;
1375 	struct sysctlnode node;
1376 
1377 	new_sbmax = sb_max;
1378 	node = *rnode;
1379 	node.sysctl_data = &new_sbmax;
1380 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1381 	if (error || newp == NULL)
1382 		return (error);
1383 
1384 	KERNEL_LOCK(1, NULL);
1385 	error = sb_max_set(new_sbmax);
1386 	KERNEL_UNLOCK_ONE(NULL);
1387 
1388 	return (error);
1389 }
1390 
1391 /*
1392  * sysctl helper routine for kern.urandom node. Picks a random number
1393  * for you.
1394  */
1395 static int
1396 sysctl_kern_urnd(SYSCTLFN_ARGS)
1397 {
1398 #if NRND > 0
1399 	int v, rv;
1400 
1401 	KERNEL_LOCK(1, NULL);
1402 	rv = rnd_extract_data(&v, sizeof(v), RND_EXTRACT_ANY);
1403 	KERNEL_UNLOCK_ONE(NULL);
1404 	if (rv == sizeof(v)) {
1405 		struct sysctlnode node = *rnode;
1406 		node.sysctl_data = &v;
1407 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1408 	}
1409 	else
1410 		return (EIO);	/*XXX*/
1411 #else
1412 	return (EOPNOTSUPP);
1413 #endif
1414 }
1415 
1416 /*
1417  * sysctl helper routine for kern.arandom node. Picks a random number
1418  * for you.
1419  */
1420 static int
1421 sysctl_kern_arnd(SYSCTLFN_ARGS)
1422 {
1423 #if NRND > 0
1424 	int error;
1425 	void *v;
1426 	struct sysctlnode node = *rnode;
1427 
1428 	if (*oldlenp == 0)
1429 		return 0;
1430 	if (*oldlenp > 8192)
1431 		return E2BIG;
1432 
1433 	v = kmem_alloc(*oldlenp, KM_SLEEP);
1434 	arc4randbytes(v, *oldlenp);
1435 	node.sysctl_data = v;
1436 	node.sysctl_size = *oldlenp;
1437 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1438 	kmem_free(v, *oldlenp);
1439 	return error;
1440 #else
1441 	return (EOPNOTSUPP);
1442 #endif
1443 }
1444 /*
1445  * sysctl helper routine to do kern.lwp.* work.
1446  */
1447 static int
1448 sysctl_kern_lwp(SYSCTLFN_ARGS)
1449 {
1450 	struct kinfo_lwp klwp;
1451 	struct proc *p;
1452 	struct lwp *l2, *l3;
1453 	char *where, *dp;
1454 	int pid, elem_size, elem_count;
1455 	int buflen, needed, error;
1456 	bool gotit;
1457 
1458 	if (namelen == 1 && name[0] == CTL_QUERY)
1459 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
1460 
1461 	dp = where = oldp;
1462 	buflen = where != NULL ? *oldlenp : 0;
1463 	error = needed = 0;
1464 
1465 	if (newp != NULL || namelen != 3)
1466 		return (EINVAL);
1467 	pid = name[0];
1468 	elem_size = name[1];
1469 	elem_count = name[2];
1470 
1471 	sysctl_unlock();
1472 	if (pid == -1) {
1473 		mutex_enter(proc_lock);
1474 		PROCLIST_FOREACH(p, &allproc) {
1475 			/* Grab a hold on the process. */
1476 			if (!rw_tryenter(&p->p_reflock, RW_READER)) {
1477 				continue;
1478 			}
1479 			mutex_exit(proc_lock);
1480 
1481 			mutex_enter(p->p_lock);
1482 			LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1483 				if (buflen >= elem_size && elem_count > 0) {
1484 					lwp_lock(l2);
1485 					fill_lwp(l2, &klwp);
1486 					lwp_unlock(l2);
1487 					mutex_exit(p->p_lock);
1488 
1489 					/*
1490 					 * Copy out elem_size, but not
1491 					 * larger than the size of a
1492 					 * struct kinfo_proc2.
1493 					 */
1494 					error = dcopyout(l, &klwp, dp,
1495 					    min(sizeof(klwp), elem_size));
1496 					if (error) {
1497 						rw_exit(&p->p_reflock);
1498 						goto cleanup;
1499 					}
1500 					mutex_enter(p->p_lock);
1501 					LIST_FOREACH(l3, &p->p_lwps,
1502 					    l_sibling) {
1503 						if (l2 == l3)
1504 							break;
1505 					}
1506 					if (l3 == NULL) {
1507 						mutex_exit(p->p_lock);
1508 						rw_exit(&p->p_reflock);
1509 						error = EAGAIN;
1510 						goto cleanup;
1511 					}
1512 					dp += elem_size;
1513 					buflen -= elem_size;
1514 					elem_count--;
1515 				}
1516 				needed += elem_size;
1517 			}
1518 			mutex_exit(p->p_lock);
1519 
1520 			/* Drop reference to process. */
1521 			mutex_enter(proc_lock);
1522 			rw_exit(&p->p_reflock);
1523 		}
1524 		mutex_exit(proc_lock);
1525 	} else {
1526 		mutex_enter(proc_lock);
1527 		p = proc_find(pid);
1528 		if (p == NULL) {
1529 			error = ESRCH;
1530 			mutex_exit(proc_lock);
1531 			goto cleanup;
1532 		}
1533 		/* Grab a hold on the process. */
1534 		gotit = rw_tryenter(&p->p_reflock, RW_READER);
1535 		mutex_exit(proc_lock);
1536 		if (!gotit) {
1537 			error = ESRCH;
1538 			goto cleanup;
1539 		}
1540 
1541 		mutex_enter(p->p_lock);
1542 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1543 			if (buflen >= elem_size && elem_count > 0) {
1544 				lwp_lock(l2);
1545 				fill_lwp(l2, &klwp);
1546 				lwp_unlock(l2);
1547 				mutex_exit(p->p_lock);
1548 				/*
1549 				 * Copy out elem_size, but not larger than
1550 				 * the size of a struct kinfo_proc2.
1551 				 */
1552 				error = dcopyout(l, &klwp, dp,
1553 				    min(sizeof(klwp), elem_size));
1554 				if (error) {
1555 					rw_exit(&p->p_reflock);
1556 					goto cleanup;
1557 				}
1558 				mutex_enter(p->p_lock);
1559 				LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
1560 					if (l2 == l3)
1561 						break;
1562 				}
1563 				if (l3 == NULL) {
1564 					mutex_exit(p->p_lock);
1565 					rw_exit(&p->p_reflock);
1566 					error = EAGAIN;
1567 					goto cleanup;
1568 				}
1569 				dp += elem_size;
1570 				buflen -= elem_size;
1571 				elem_count--;
1572 			}
1573 			needed += elem_size;
1574 		}
1575 		mutex_exit(p->p_lock);
1576 
1577 		/* Drop reference to process. */
1578 		rw_exit(&p->p_reflock);
1579 	}
1580 
1581 	if (where != NULL) {
1582 		*oldlenp = dp - where;
1583 		if (needed > *oldlenp) {
1584 			sysctl_relock();
1585 			return (ENOMEM);
1586 		}
1587 	} else {
1588 		needed += KERN_LWPSLOP;
1589 		*oldlenp = needed;
1590 	}
1591 	error = 0;
1592  cleanup:
1593 	sysctl_relock();
1594 	return (error);
1595 }
1596 
1597 /*
1598  * sysctl helper routine for kern.forkfsleep node. Ensures that the
1599  * given value is not too large or two small, and is at least one
1600  * timer tick if not zero.
1601  */
1602 static int
1603 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
1604 {
1605 	/* userland sees value in ms, internally is in ticks */
1606 	extern int forkfsleep;		/* defined in kern/kern_fork.c */
1607 	int error, timo, lsleep;
1608 	struct sysctlnode node;
1609 
1610 	lsleep = forkfsleep * 1000 / hz;
1611 	node = *rnode;
1612 	node.sysctl_data = &lsleep;
1613 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1614 	if (error || newp == NULL)
1615 		return (error);
1616 
1617 	/* refuse negative values, and overly 'long time' */
1618 	if (lsleep < 0 || lsleep > MAXSLP * 1000)
1619 		return (EINVAL);
1620 
1621 	timo = mstohz(lsleep);
1622 
1623 	/* if the interval is >0 ms && <1 tick, use 1 tick */
1624 	if (lsleep != 0 && timo == 0)
1625 		forkfsleep = 1;
1626 	else
1627 		forkfsleep = timo;
1628 
1629 	return (0);
1630 }
1631 
1632 /*
1633  * sysctl helper routine for kern.root_partition
1634  */
1635 static int
1636 sysctl_kern_root_partition(SYSCTLFN_ARGS)
1637 {
1638 	int rootpart = DISKPART(rootdev);
1639 	struct sysctlnode node = *rnode;
1640 
1641 	node.sysctl_data = &rootpart;
1642 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1643 }
1644 
1645 /*
1646  * sysctl helper function for kern.drivers
1647  */
1648 static int
1649 sysctl_kern_drivers(SYSCTLFN_ARGS)
1650 {
1651 	int error;
1652 	size_t buflen;
1653 	struct kinfo_drivers kd;
1654 	char *start, *where;
1655 	const char *dname;
1656 	int i;
1657 	extern struct devsw_conv *devsw_conv;
1658 	extern int max_devsw_convs;
1659 
1660 	if (newp != NULL || namelen != 0)
1661 		return (EINVAL);
1662 
1663 	start = where = oldp;
1664 	buflen = *oldlenp;
1665 	if (where == NULL) {
1666 		*oldlenp = max_devsw_convs * sizeof kd;
1667 		return 0;
1668 	}
1669 
1670 	/*
1671 	 * An array of kinfo_drivers structures
1672 	 */
1673 	error = 0;
1674 	sysctl_unlock();
1675 	mutex_enter(&device_lock);
1676 	for (i = 0; i < max_devsw_convs; i++) {
1677 		dname = devsw_conv[i].d_name;
1678 		if (dname == NULL)
1679 			continue;
1680 		if (buflen < sizeof kd) {
1681 			error = ENOMEM;
1682 			break;
1683 		}
1684 		memset(&kd, 0, sizeof(kd));
1685 		kd.d_bmajor = devsw_conv[i].d_bmajor;
1686 		kd.d_cmajor = devsw_conv[i].d_cmajor;
1687 		strlcpy(kd.d_name, dname, sizeof kd.d_name);
1688 		mutex_exit(&device_lock);
1689 		error = dcopyout(l, &kd, where, sizeof kd);
1690 		mutex_enter(&device_lock);
1691 		if (error != 0)
1692 			break;
1693 		buflen -= sizeof kd;
1694 		where += sizeof kd;
1695 	}
1696 	mutex_exit(&device_lock);
1697 	sysctl_relock();
1698 	*oldlenp = where - start;
1699 	return error;
1700 }
1701 
1702 static int
1703 sysctl_security_setidcore(SYSCTLFN_ARGS)
1704 {
1705 	int newsize, error;
1706 	struct sysctlnode node;
1707 
1708 	node = *rnode;
1709 	node.sysctl_data = &newsize;
1710 	newsize = *(int *)rnode->sysctl_data;
1711 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1712 	if (error || newp == NULL)
1713 		return error;
1714 
1715 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
1716 	    0, NULL, NULL, NULL))
1717 		return (EPERM);
1718 
1719 	*(int *)rnode->sysctl_data = newsize;
1720 
1721 	return 0;
1722 }
1723 
1724 static int
1725 sysctl_security_setidcorename(SYSCTLFN_ARGS)
1726 {
1727 	int error;
1728 	char *newsetidcorename;
1729 	struct sysctlnode node;
1730 
1731 	newsetidcorename = PNBUF_GET();
1732 	node = *rnode;
1733 	node.sysctl_data = newsetidcorename;
1734 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
1735 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1736 	if (error || newp == NULL) {
1737 		goto out;
1738 	}
1739 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
1740 	    0, NULL, NULL, NULL)) {
1741 		error = EPERM;
1742 		goto out;
1743 	}
1744 	if (strlen(newsetidcorename) == 0) {
1745 		error = EINVAL;
1746 		goto out;
1747 	}
1748 	memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
1749 out:
1750 	PNBUF_PUT(newsetidcorename);
1751 	return error;
1752 }
1753 
1754 /*
1755  * sysctl helper routine for kern.cp_id node. Maps cpus to their
1756  * cpuids.
1757  */
1758 static int
1759 sysctl_kern_cpid(SYSCTLFN_ARGS)
1760 {
1761 	struct sysctlnode node = *rnode;
1762 	uint64_t *cp_id = NULL;
1763 	int error, n = ncpu;
1764 	struct cpu_info *ci;
1765 	CPU_INFO_ITERATOR cii;
1766 
1767 	/*
1768 	 * Here you may either retrieve a single cpu id or the whole
1769 	 * set. The size you get back when probing depends on what
1770 	 * you ask for.
1771 	 */
1772 	switch (namelen) {
1773 	case 0:
1774 		node.sysctl_size = n * sizeof(uint64_t);
1775 		n = -2; /* ALL */
1776 		break;
1777 	case 1:
1778 		if (name[0] < 0 || name[0] >= n)
1779 			return (ENOENT); /* ENOSUCHPROCESSOR */
1780 		node.sysctl_size = sizeof(uint64_t);
1781 		n = name[0];
1782 		/*
1783 		 * adjust these so that sysctl_lookup() will be happy
1784 		 */
1785 		name++;
1786 		namelen--;
1787 		break;
1788 	default:
1789 		return (EINVAL);
1790 	}
1791 
1792 	cp_id = kmem_alloc(node.sysctl_size, KM_SLEEP);
1793 	if (cp_id == NULL)
1794 		return (ENOMEM);
1795 	node.sysctl_data = cp_id;
1796 	memset(cp_id, 0, node.sysctl_size);
1797 
1798 	for (CPU_INFO_FOREACH(cii, ci)) {
1799 		if (n <= 0)
1800 			cp_id[0] = cpu_index(ci);
1801 		/*
1802 		 * if a specific processor was requested and we just
1803 		 * did it, we're done here
1804 		 */
1805 		if (n == 0)
1806 			break;
1807 		/*
1808 		 * if doing "all", skip to next cp_id slot for next processor
1809 		 */
1810 		if (n == -2)
1811 			cp_id++;
1812 		/*
1813 		 * if we're doing a specific processor, we're one
1814 		 * processor closer
1815 		 */
1816 		if (n > 0)
1817 			n--;
1818 	}
1819 
1820 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1821 	kmem_free(node.sysctl_data, node.sysctl_size);
1822 	return (error);
1823 }
1824 
1825 /*
1826  * sysctl helper routine for hw.usermem and hw.usermem64. Values are
1827  * calculate on the fly taking into account integer overflow and the
1828  * current wired count.
1829  */
1830 static int
1831 sysctl_hw_usermem(SYSCTLFN_ARGS)
1832 {
1833 	u_int ui;
1834 	u_quad_t uq;
1835 	struct sysctlnode node;
1836 
1837 	node = *rnode;
1838 	switch (rnode->sysctl_num) {
1839 	case HW_USERMEM:
1840 		if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
1841 			ui = UINT_MAX;
1842 		else
1843 			ui *= PAGE_SIZE;
1844 		node.sysctl_data = &ui;
1845 		break;
1846 	case HW_USERMEM64:
1847 		uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
1848 		node.sysctl_data = &uq;
1849 		break;
1850 	default:
1851 		return (EINVAL);
1852 	}
1853 
1854 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1855 }
1856 
1857 /*
1858  * sysctl helper routine for kern.cnmagic node. Pulls the old value
1859  * out, encoded, and stuffs the new value in for decoding.
1860  */
1861 static int
1862 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
1863 {
1864 	char magic[CNS_LEN];
1865 	int error;
1866 	struct sysctlnode node;
1867 
1868 	if (oldp)
1869 		cn_get_magic(magic, CNS_LEN);
1870 	node = *rnode;
1871 	node.sysctl_data = &magic[0];
1872 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1873 	if (error || newp == NULL)
1874 		return (error);
1875 
1876 	return (cn_set_magic(magic));
1877 }
1878 
1879 /*
1880  * ********************************************************************
1881  * section 3: public helper routines that are used for more than one
1882  * node
1883  * ********************************************************************
1884  */
1885 
1886 /*
1887  * sysctl helper routine for the kern.root_device node and some ports'
1888  * machdep.root_device nodes.
1889  */
1890 int
1891 sysctl_root_device(SYSCTLFN_ARGS)
1892 {
1893 	struct sysctlnode node;
1894 
1895 	node = *rnode;
1896 	node.sysctl_data = root_device->dv_xname;
1897 	node.sysctl_size = strlen(device_xname(root_device)) + 1;
1898 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1899 }
1900 
1901 /*
1902  * sysctl helper routine for kern.consdev, dependent on the current
1903  * state of the console. Also used for machdep.console_device on some
1904  * ports.
1905  */
1906 int
1907 sysctl_consdev(SYSCTLFN_ARGS)
1908 {
1909 	dev_t consdev;
1910 	uint32_t oconsdev;
1911 	struct sysctlnode node;
1912 
1913 	if (cn_tab != NULL)
1914 		consdev = cn_tab->cn_dev;
1915 	else
1916 		consdev = NODEV;
1917 	node = *rnode;
1918 	switch (*oldlenp) {
1919 	case sizeof(consdev):
1920 		node.sysctl_data = &consdev;
1921 		node.sysctl_size = sizeof(consdev);
1922 		break;
1923 	case sizeof(oconsdev):
1924 		oconsdev = (uint32_t)consdev;
1925 		node.sysctl_data = &oconsdev;
1926 		node.sysctl_size = sizeof(oconsdev);
1927 		break;
1928 	default:
1929 		return EINVAL;
1930 	}
1931 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1932 }
1933 
1934 /*
1935  * ********************************************************************
1936  * section 4: support for some helpers
1937  * ********************************************************************
1938  */
1939 
1940 
1941 /*
1942  * Fill in a kinfo_lwp structure for the specified lwp.
1943  */
1944 static void
1945 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
1946 {
1947 	struct proc *p = l->l_proc;
1948 	struct timeval tv;
1949 
1950 	KASSERT(lwp_locked(l, NULL));
1951 
1952 	memset(kl, 0, sizeof(*kl));
1953 
1954 	kl->l_forw = 0;
1955 	kl->l_back = 0;
1956 	kl->l_laddr = PTRTOUINT64(l);
1957 	kl->l_addr = PTRTOUINT64(l->l_addr);
1958 	kl->l_stat = l->l_stat;
1959 	kl->l_lid = l->l_lid;
1960 	kl->l_flag = L_INMEM;
1961 	kl->l_flag |= sysctl_map_flags(sysctl_lwpprflagmap, l->l_prflag);
1962 	kl->l_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
1963 
1964 	kl->l_swtime = l->l_swtime;
1965 	kl->l_slptime = l->l_slptime;
1966 	if (l->l_stat == LSONPROC)
1967 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
1968 	else
1969 		kl->l_schedflags = 0;
1970 	kl->l_priority = lwp_eprio(l);
1971 	kl->l_usrpri = l->l_priority;
1972 	if (l->l_wchan)
1973 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
1974 	kl->l_wchan = PTRTOUINT64(l->l_wchan);
1975 	kl->l_cpuid = cpu_index(l->l_cpu);
1976 	bintime2timeval(&l->l_rtime, &tv);
1977 	kl->l_rtime_sec = tv.tv_sec;
1978 	kl->l_rtime_usec = tv.tv_usec;
1979 	kl->l_cpticks = l->l_cpticks;
1980 	kl->l_pctcpu = l->l_pctcpu;
1981 	kl->l_pid = p->p_pid;
1982 	if (l->l_name == NULL)
1983 		kl->l_name[0] = '\0';
1984 	else
1985 		strlcpy(kl->l_name, l->l_name, sizeof(kl->l_name));
1986 }
1987