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