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