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