xref: /netbsd-src/sys/kern/init_sysctl.c (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /*	$NetBSD: init_sysctl.c,v 1.188 2012/03/10 21:51:59 joerg 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.188 2012/03/10 21:51:59 joerg 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_sbmax(SYSCTLFN_PROTO);
160 static int sysctl_kern_urnd(SYSCTLFN_PROTO);
161 static int sysctl_kern_arnd(SYSCTLFN_PROTO);
162 static int sysctl_kern_lwp(SYSCTLFN_PROTO);
163 static int sysctl_kern_forkfsleep(SYSCTLFN_PROTO);
164 static int sysctl_kern_root_partition(SYSCTLFN_PROTO);
165 static int sysctl_kern_drivers(SYSCTLFN_PROTO);
166 static int sysctl_security_setidcore(SYSCTLFN_PROTO);
167 static int sysctl_security_setidcorename(SYSCTLFN_PROTO);
168 static int sysctl_kern_cpid(SYSCTLFN_PROTO);
169 static int sysctl_hw_usermem(SYSCTLFN_PROTO);
170 static int sysctl_hw_cnmagic(SYSCTLFN_PROTO);
171 
172 static void fill_lwp(struct lwp *l, struct kinfo_lwp *kl);
173 
174 /*
175  * ********************************************************************
176  * section 1: setup routines
177  * ********************************************************************
178  * These functions are stuffed into a link set for sysctl setup
179  * functions. They're never called or referenced from anywhere else.
180  * ********************************************************************
181  */
182 
183 /*
184  * this setup routine is a replacement for kern_sysctl()
185  */
186 SYSCTL_SETUP(sysctl_kern_setup, "sysctl kern subtree setup")
187 {
188 	extern int kern_logsigexit;	/* defined in kern/kern_sig.c */
189 	extern fixpt_t ccpu;		/* defined in kern/kern_synch.c */
190 	extern int dumponpanic;		/* defined in kern/subr_prf.c */
191 	const struct sysctlnode *rnode;
192 
193 	sysctl_createv(clog, 0, NULL, NULL,
194 		       CTLFLAG_PERMANENT,
195 		       CTLTYPE_NODE, "kern", NULL,
196 		       NULL, 0, NULL, 0,
197 		       CTL_KERN, CTL_EOL);
198 
199 	sysctl_createv(clog, 0, NULL, NULL,
200 		       CTLFLAG_PERMANENT,
201 		       CTLTYPE_STRING, "ostype",
202 		       SYSCTL_DESCR("Operating system type"),
203 		       NULL, 0, __UNCONST(&ostype), 0,
204 		       CTL_KERN, KERN_OSTYPE, CTL_EOL);
205 	sysctl_createv(clog, 0, NULL, NULL,
206 		       CTLFLAG_PERMANENT,
207 		       CTLTYPE_STRING, "osrelease",
208 		       SYSCTL_DESCR("Operating system release"),
209 		       NULL, 0, __UNCONST(&osrelease), 0,
210 		       CTL_KERN, KERN_OSRELEASE, CTL_EOL);
211 	sysctl_createv(clog, 0, NULL, NULL,
212 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
213 		       CTLTYPE_INT, "osrevision",
214 		       SYSCTL_DESCR("Operating system revision"),
215 		       NULL, __NetBSD_Version__, NULL, 0,
216 		       CTL_KERN, KERN_OSREV, CTL_EOL);
217 	sysctl_createv(clog, 0, NULL, NULL,
218 		       CTLFLAG_PERMANENT,
219 		       CTLTYPE_STRING, "version",
220 		       SYSCTL_DESCR("Kernel version"),
221 		       NULL, 0, __UNCONST(&version), 0,
222 		       CTL_KERN, KERN_VERSION, CTL_EOL);
223 	sysctl_createv(clog, 0, NULL, NULL,
224 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
225 		       CTLTYPE_INT, "maxvnodes",
226 		       SYSCTL_DESCR("Maximum number of vnodes"),
227 		       sysctl_kern_maxvnodes, 0, NULL, 0,
228 		       CTL_KERN, KERN_MAXVNODES, CTL_EOL);
229 	sysctl_createv(clog, 0, NULL, NULL,
230 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
231 		       CTLTYPE_INT, "maxproc",
232 		       SYSCTL_DESCR("Maximum number of simultaneous processes"),
233 		       sysctl_kern_maxproc, 0, NULL, 0,
234 		       CTL_KERN, KERN_MAXPROC, CTL_EOL);
235 	sysctl_createv(clog, 0, NULL, NULL,
236 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
237 		       CTLTYPE_INT, "maxfiles",
238 		       SYSCTL_DESCR("Maximum number of open files"),
239 		       NULL, 0, &maxfiles, 0,
240 		       CTL_KERN, KERN_MAXFILES, CTL_EOL);
241 	sysctl_createv(clog, 0, NULL, NULL,
242 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
243 		       CTLTYPE_INT, "argmax",
244 		       SYSCTL_DESCR("Maximum number of bytes of arguments to "
245 				    "execve(2)"),
246 		       NULL, ARG_MAX, NULL, 0,
247 		       CTL_KERN, KERN_ARGMAX, CTL_EOL);
248 	sysctl_createv(clog, 0, NULL, NULL,
249 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
250 		       CTLTYPE_STRING, "hostname",
251 		       SYSCTL_DESCR("System hostname"),
252 		       sysctl_setlen, 0, &hostname, MAXHOSTNAMELEN,
253 		       CTL_KERN, KERN_HOSTNAME, CTL_EOL);
254 	sysctl_createv(clog, 0, NULL, NULL,
255 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
256 		       CTLTYPE_INT, "hostid",
257 		       SYSCTL_DESCR("System host ID number"),
258 		       sysctl_kern_hostid, 0, NULL, 0,
259 		       CTL_KERN, KERN_HOSTID, CTL_EOL);
260 	sysctl_createv(clog, 0, NULL, NULL,
261 		       CTLFLAG_PERMANENT,
262 		       CTLTYPE_STRUCT, "clockrate",
263 		       SYSCTL_DESCR("Kernel clock rates"),
264 		       sysctl_kern_clockrate, 0, NULL,
265 		       sizeof(struct clockinfo),
266 		       CTL_KERN, KERN_CLOCKRATE, CTL_EOL);
267 	sysctl_createv(clog, 0, NULL, NULL,
268 		       CTLFLAG_PERMANENT,
269 		       CTLTYPE_INT, "hardclock_ticks",
270 		       SYSCTL_DESCR("Number of hardclock ticks"),
271 		       NULL, 0, &hardclock_ticks, sizeof(hardclock_ticks),
272 		       CTL_KERN, KERN_HARDCLOCK_TICKS, CTL_EOL);
273 	sysctl_createv(clog, 0, NULL, NULL,
274 		       CTLFLAG_PERMANENT,
275 		       CTLTYPE_STRUCT, "vnode",
276 		       SYSCTL_DESCR("System vnode table"),
277 		       sysctl_kern_vnode, 0, NULL, 0,
278 		       CTL_KERN, KERN_VNODE, CTL_EOL);
279 #ifndef GPROF
280 	sysctl_createv(clog, 0, NULL, NULL,
281 		       CTLFLAG_PERMANENT,
282 		       CTLTYPE_NODE, "profiling",
283 		       SYSCTL_DESCR("Profiling information (not available)"),
284 		       sysctl_notavail, 0, NULL, 0,
285 		       CTL_KERN, KERN_PROF, CTL_EOL);
286 #endif
287 	sysctl_createv(clog, 0, NULL, NULL,
288 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
289 		       CTLTYPE_INT, "posix1version",
290 		       SYSCTL_DESCR("Version of ISO/IEC 9945 (POSIX 1003.1) "
291 				    "with which the operating system attempts "
292 				    "to comply"),
293 		       NULL, _POSIX_VERSION, NULL, 0,
294 		       CTL_KERN, KERN_POSIX1, CTL_EOL);
295 	sysctl_createv(clog, 0, NULL, NULL,
296 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
297 		       CTLTYPE_INT, "ngroups",
298 		       SYSCTL_DESCR("Maximum number of supplemental groups"),
299 		       NULL, NGROUPS_MAX, NULL, 0,
300 		       CTL_KERN, KERN_NGROUPS, CTL_EOL);
301 	sysctl_createv(clog, 0, NULL, NULL,
302 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
303 		       CTLTYPE_INT, "job_control",
304 		       SYSCTL_DESCR("Whether job control is available"),
305 		       NULL, 1, NULL, 0,
306 		       CTL_KERN, KERN_JOB_CONTROL, CTL_EOL);
307 	sysctl_createv(clog, 0, NULL, NULL,
308 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
309 		       CTLTYPE_INT, "saved_ids",
310 		       SYSCTL_DESCR("Whether POSIX saved set-group/user ID is "
311 				    "available"), NULL,
312 #ifdef _POSIX_SAVED_IDS
313 		       1,
314 #else /* _POSIX_SAVED_IDS */
315 		       0,
316 #endif /* _POSIX_SAVED_IDS */
317 		       NULL, 0, CTL_KERN, KERN_SAVED_IDS, CTL_EOL);
318 	sysctl_createv(clog, 0, NULL, NULL,
319 		       CTLFLAG_PERMANENT|CTLFLAG_HEX,
320 		       CTLTYPE_INT, "boothowto",
321 		       SYSCTL_DESCR("Flags from boot loader"),
322 		       NULL, 0, &boothowto, sizeof(boothowto),
323 		       CTL_KERN, CTL_CREATE, CTL_EOL);
324 	sysctl_createv(clog, 0, NULL, NULL,
325 		       CTLFLAG_PERMANENT,
326 		       CTLTYPE_STRUCT, "boottime",
327 		       SYSCTL_DESCR("System boot time"),
328 		       NULL, 0, &boottime, sizeof(boottime),
329 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
330 #ifdef COMPAT_50
331 	{
332 		extern struct timeval50 boottime50;
333 		sysctl_createv(clog, 0, NULL, NULL,
334 			       CTLFLAG_PERMANENT,
335 			       CTLTYPE_STRUCT, "oboottime",
336 			       SYSCTL_DESCR("System boot time"),
337 			       NULL, 0, &boottime50, sizeof(boottime50),
338 			       CTL_KERN, KERN_OBOOTTIME, CTL_EOL);
339 	}
340 #endif
341 	sysctl_createv(clog, 0, NULL, NULL,
342 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
343 		       CTLTYPE_STRING, "domainname",
344 		       SYSCTL_DESCR("YP domain name"),
345 		       sysctl_setlen, 0, &domainname, MAXHOSTNAMELEN,
346 		       CTL_KERN, KERN_DOMAINNAME, CTL_EOL);
347 	sysctl_createv(clog, 0, NULL, NULL,
348 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
349 		       CTLTYPE_INT, "maxpartitions",
350 		       SYSCTL_DESCR("Maximum number of partitions allowed per "
351 				    "disk"),
352 		       NULL, MAXPARTITIONS, NULL, 0,
353 		       CTL_KERN, KERN_MAXPARTITIONS, CTL_EOL);
354 	sysctl_createv(clog, 0, NULL, NULL,
355 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
356 		       CTLTYPE_INT, "rawpartition",
357 		       SYSCTL_DESCR("Raw partition of a disk"),
358 		       NULL, RAW_PART, NULL, 0,
359 		       CTL_KERN, KERN_RAWPARTITION, CTL_EOL);
360 	sysctl_createv(clog, 0, NULL, NULL,
361 		       CTLFLAG_PERMANENT,
362 		       CTLTYPE_STRUCT, "timex", NULL,
363 		       sysctl_notavail, 0, NULL, 0,
364 		       CTL_KERN, KERN_TIMEX, CTL_EOL);
365 	sysctl_createv(clog, 0, NULL, NULL,
366 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
367 		       CTLTYPE_INT, "rtc_offset",
368 		       SYSCTL_DESCR("Offset of real time clock from UTC in "
369 				    "minutes"),
370 		       sysctl_kern_rtc_offset, 0, &rtc_offset, 0,
371 		       CTL_KERN, KERN_RTC_OFFSET, CTL_EOL);
372 	sysctl_createv(clog, 0, NULL, NULL,
373 		       CTLFLAG_PERMANENT,
374 		       CTLTYPE_STRING, "root_device",
375 		       SYSCTL_DESCR("Name of the root device"),
376 		       sysctl_root_device, 0, NULL, 0,
377 		       CTL_KERN, KERN_ROOT_DEVICE, CTL_EOL);
378 	sysctl_createv(clog, 0, NULL, NULL,
379 		       CTLFLAG_PERMANENT,
380 		       CTLTYPE_INT, "msgbufsize",
381 		       SYSCTL_DESCR("Size of the kernel message buffer"),
382 		       sysctl_msgbuf, 0, NULL, 0,
383 		       CTL_KERN, KERN_MSGBUFSIZE, CTL_EOL);
384 	sysctl_createv(clog, 0, NULL, NULL,
385 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
386 		       CTLTYPE_INT, "fsync",
387 		       SYSCTL_DESCR("Whether the POSIX 1003.1b File "
388 				    "Synchronization Option is available on "
389 				    "this system"),
390 		       NULL, 1, NULL, 0,
391 		       CTL_KERN, KERN_FSYNC, CTL_EOL);
392 	sysctl_createv(clog, 0, NULL, NULL,
393 		       CTLFLAG_PERMANENT,
394 		       CTLTYPE_NODE, "ipc",
395 		       SYSCTL_DESCR("SysV IPC options"),
396 		       NULL, 0, NULL, 0,
397 		       CTL_KERN, KERN_SYSVIPC, CTL_EOL);
398 	sysctl_createv(clog, 0, NULL, NULL,
399 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
400 		       CTLTYPE_INT, "sysvmsg",
401 		       SYSCTL_DESCR("System V style message support available"),
402 		       NULL,
403 #ifdef SYSVMSG
404 		       1,
405 #else /* SYSVMSG */
406 		       0,
407 #endif /* SYSVMSG */
408 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_MSG, CTL_EOL);
409 	sysctl_createv(clog, 0, NULL, NULL,
410 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
411 		       CTLTYPE_INT, "sysvsem",
412 		       SYSCTL_DESCR("System V style semaphore support "
413 				    "available"), NULL,
414 #ifdef SYSVSEM
415 		       1,
416 #else /* SYSVSEM */
417 		       0,
418 #endif /* SYSVSEM */
419 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SEM, CTL_EOL);
420 	sysctl_createv(clog, 0, NULL, NULL,
421 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
422 		       CTLTYPE_INT, "sysvshm",
423 		       SYSCTL_DESCR("System V style shared memory support "
424 				    "available"), NULL,
425 #ifdef SYSVSHM
426 		       1,
427 #else /* SYSVSHM */
428 		       0,
429 #endif /* SYSVSHM */
430 		       NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHM, CTL_EOL);
431 	sysctl_createv(clog, 0, NULL, NULL,
432 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
433 		       CTLTYPE_INT, "synchronized_io",
434 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Synchronized "
435 				    "I/O Option is available on this system"),
436 		       NULL, 1, NULL, 0,
437 		       CTL_KERN, KERN_SYNCHRONIZED_IO, CTL_EOL);
438 	sysctl_createv(clog, 0, NULL, NULL,
439 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
440 		       CTLTYPE_INT, "iov_max",
441 		       SYSCTL_DESCR("Maximum number of iovec structures per "
442 				    "process"),
443 		       NULL, IOV_MAX, NULL, 0,
444 		       CTL_KERN, KERN_IOV_MAX, CTL_EOL);
445 	sysctl_createv(clog, 0, NULL, NULL,
446 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
447 		       CTLTYPE_INT, "mapped_files",
448 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory Mapped "
449 				    "Files Option is available on this system"),
450 		       NULL, 1, NULL, 0,
451 		       CTL_KERN, KERN_MAPPED_FILES, CTL_EOL);
452 	sysctl_createv(clog, 0, NULL, NULL,
453 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
454 		       CTLTYPE_INT, "memlock",
455 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Process Memory "
456 				    "Locking Option is available on this "
457 				    "system"),
458 		       NULL, 1, NULL, 0,
459 		       CTL_KERN, KERN_MEMLOCK, CTL_EOL);
460 	sysctl_createv(clog, 0, NULL, NULL,
461 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
462 		       CTLTYPE_INT, "memlock_range",
463 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Range Memory "
464 				    "Locking Option is available on this "
465 				    "system"),
466 		       NULL, 1, NULL, 0,
467 		       CTL_KERN, KERN_MEMLOCK_RANGE, CTL_EOL);
468 	sysctl_createv(clog, 0, NULL, NULL,
469 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
470 		       CTLTYPE_INT, "memory_protection",
471 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory "
472 				    "Protection Option is available on this "
473 				    "system"),
474 		       NULL, 1, NULL, 0,
475 		       CTL_KERN, KERN_MEMORY_PROTECTION, CTL_EOL);
476 	sysctl_createv(clog, 0, NULL, NULL,
477 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
478 		       CTLTYPE_INT, "login_name_max",
479 		       SYSCTL_DESCR("Maximum login name length"),
480 		       NULL, LOGIN_NAME_MAX, NULL, 0,
481 		       CTL_KERN, KERN_LOGIN_NAME_MAX, CTL_EOL);
482 	sysctl_createv(clog, 0, NULL, NULL,
483 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
484 		       CTLTYPE_STRING, "defcorename",
485 		       SYSCTL_DESCR("Default core file name"),
486 		       sysctl_kern_defcorename, 0, defcorename, MAXPATHLEN,
487 		       CTL_KERN, KERN_DEFCORENAME, CTL_EOL);
488 	sysctl_createv(clog, 0, NULL, NULL,
489 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
490 		       CTLTYPE_INT, "logsigexit",
491 		       SYSCTL_DESCR("Log process exit when caused by signals"),
492 		       NULL, 0, &kern_logsigexit, 0,
493 		       CTL_KERN, KERN_LOGSIGEXIT, CTL_EOL);
494 	sysctl_createv(clog, 0, NULL, NULL,
495 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
496 		       CTLTYPE_INT, "fscale",
497 		       SYSCTL_DESCR("Kernel fixed-point scale factor"),
498 		       NULL, FSCALE, NULL, 0,
499 		       CTL_KERN, KERN_FSCALE, CTL_EOL);
500 	sysctl_createv(clog, 0, NULL, NULL,
501 		       CTLFLAG_PERMANENT,
502 		       CTLTYPE_INT, "ccpu",
503 		       SYSCTL_DESCR("Scheduler exponential decay value"),
504 		       NULL, 0, &ccpu, 0,
505 		       CTL_KERN, KERN_CCPU, CTL_EOL);
506 	sysctl_createv(clog, 0, NULL, NULL,
507 		       CTLFLAG_PERMANENT,
508 		       CTLTYPE_STRUCT, "cp_time",
509 		       SYSCTL_DESCR("Clock ticks spent in different CPU states"),
510 		       sysctl_kern_cptime, 0, NULL, 0,
511 		       CTL_KERN, KERN_CP_TIME, CTL_EOL);
512 	sysctl_createv(clog, 0, NULL, NULL,
513 		       CTLFLAG_PERMANENT,
514 		       CTLTYPE_INT, "msgbuf",
515 		       SYSCTL_DESCR("Kernel message buffer"),
516 		       sysctl_msgbuf, 0, NULL, 0,
517 		       CTL_KERN, KERN_MSGBUF, CTL_EOL);
518 	sysctl_createv(clog, 0, NULL, NULL,
519 		       CTLFLAG_PERMANENT,
520 		       CTLTYPE_STRUCT, "consdev",
521 		       SYSCTL_DESCR("Console device"),
522 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
523 		       CTL_KERN, KERN_CONSDEV, CTL_EOL);
524 #if NPTY > 0
525 	sysctl_createv(clog, 0, NULL, NULL,
526 		       CTLFLAG_PERMANENT,
527 		       CTLTYPE_INT, "maxptys",
528 		       SYSCTL_DESCR("Maximum number of pseudo-ttys"),
529 		       sysctl_kern_maxptys, 0, NULL, 0,
530 		       CTL_KERN, KERN_MAXPTYS, CTL_EOL);
531 #endif /* NPTY > 0 */
532 	sysctl_createv(clog, 0, NULL, NULL,
533 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
534 		       CTLTYPE_INT, "maxphys",
535 		       SYSCTL_DESCR("Maximum raw I/O transfer size"),
536 		       NULL, MAXPHYS, NULL, 0,
537 		       CTL_KERN, KERN_MAXPHYS, CTL_EOL);
538 	sysctl_createv(clog, 0, NULL, NULL,
539 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
540 		       CTLTYPE_INT, "sbmax",
541 		       SYSCTL_DESCR("Maximum socket buffer size"),
542 		       sysctl_kern_sbmax, 0, NULL, 0,
543 		       CTL_KERN, KERN_SBMAX, CTL_EOL);
544 	sysctl_createv(clog, 0, NULL, NULL,
545 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
546 		       CTLTYPE_INT, "monotonic_clock",
547 		       SYSCTL_DESCR("Implementation version of the POSIX "
548 				    "1003.1b Monotonic Clock Option"),
549 		       /* XXX _POSIX_VERSION */
550 		       NULL, _POSIX_MONOTONIC_CLOCK, NULL, 0,
551 		       CTL_KERN, KERN_MONOTONIC_CLOCK, CTL_EOL);
552 	sysctl_createv(clog, 0, NULL, NULL,
553 		       CTLFLAG_PERMANENT,
554 		       CTLTYPE_INT, "urandom",
555 		       SYSCTL_DESCR("Random integer value"),
556 		       sysctl_kern_urnd, 0, NULL, 0,
557 		       CTL_KERN, KERN_URND, CTL_EOL);
558 	sysctl_createv(clog, 0, NULL, NULL,
559 		       CTLFLAG_PERMANENT,
560 		       CTLTYPE_INT, "arandom",
561 		       SYSCTL_DESCR("n bytes of random data"),
562 		       sysctl_kern_arnd, 0, NULL, 0,
563 		       CTL_KERN, KERN_ARND, CTL_EOL);
564 	sysctl_createv(clog, 0, NULL, NULL,
565 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
566 		       CTLTYPE_INT, "labelsector",
567 		       SYSCTL_DESCR("Sector number containing the disklabel"),
568 		       NULL, LABELSECTOR, NULL, 0,
569 		       CTL_KERN, KERN_LABELSECTOR, CTL_EOL);
570 	sysctl_createv(clog, 0, NULL, NULL,
571 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
572 		       CTLTYPE_INT, "labeloffset",
573 		       SYSCTL_DESCR("Offset of the disklabel within the "
574 				    "sector"),
575 		       NULL, LABELOFFSET, NULL, 0,
576 		       CTL_KERN, KERN_LABELOFFSET, CTL_EOL);
577 	sysctl_createv(clog, 0, NULL, NULL,
578 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
579 		       CTLTYPE_INT, "labelusesmbr",
580 		       SYSCTL_DESCR("disklabel is inside MBR partition"),
581 		       NULL, LABELUSESMBR, NULL, 0,
582 		       CTL_KERN, CTL_CREATE, CTL_EOL);
583 	sysctl_createv(clog, 0, NULL, NULL,
584 		       CTLFLAG_PERMANENT,
585 		       CTLTYPE_NODE, "lwp",
586 		       SYSCTL_DESCR("System-wide LWP information"),
587 		       sysctl_kern_lwp, 0, NULL, 0,
588 		       CTL_KERN, KERN_LWP, CTL_EOL);
589 	sysctl_createv(clog, 0, NULL, NULL,
590 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
591 		       CTLTYPE_INT, "forkfsleep",
592 		       SYSCTL_DESCR("Milliseconds to sleep on fork failure due "
593 				    "to process limits"),
594 		       sysctl_kern_forkfsleep, 0, NULL, 0,
595 		       CTL_KERN, KERN_FORKFSLEEP, CTL_EOL);
596 	sysctl_createv(clog, 0, NULL, NULL,
597 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
598 		       CTLTYPE_INT, "posix_threads",
599 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
600 				    "Threads option to which the system "
601 				    "attempts to conform"),
602 		       /* XXX _POSIX_VERSION */
603 		       NULL, _POSIX_THREADS, NULL, 0,
604 		       CTL_KERN, KERN_POSIX_THREADS, CTL_EOL);
605 	sysctl_createv(clog, 0, NULL, NULL,
606 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
607 		       CTLTYPE_INT, "posix_semaphores",
608 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
609 				    "Semaphores option to which the system "
610 				    "attempts to conform"), NULL,
611 		       200112, NULL, 0,
612 		       CTL_KERN, KERN_POSIX_SEMAPHORES, CTL_EOL);
613 	sysctl_createv(clog, 0, NULL, NULL,
614 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
615 		       CTLTYPE_INT, "posix_barriers",
616 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
617 				    "Barriers option to which the system "
618 				    "attempts to conform"),
619 		       /* XXX _POSIX_VERSION */
620 		       NULL, _POSIX_BARRIERS, NULL, 0,
621 		       CTL_KERN, KERN_POSIX_BARRIERS, CTL_EOL);
622 	sysctl_createv(clog, 0, NULL, NULL,
623 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
624 		       CTLTYPE_INT, "posix_timers",
625 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
626 				    "Timers option to which the system "
627 				    "attempts to conform"),
628 		       /* XXX _POSIX_VERSION */
629 		       NULL, _POSIX_TIMERS, NULL, 0,
630 		       CTL_KERN, KERN_POSIX_TIMERS, CTL_EOL);
631 	sysctl_createv(clog, 0, NULL, NULL,
632 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
633 		       CTLTYPE_INT, "posix_spin_locks",
634 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its Spin "
635 				    "Locks option to which the system attempts "
636 				    "to conform"),
637 		       /* XXX _POSIX_VERSION */
638 		       NULL, _POSIX_SPIN_LOCKS, NULL, 0,
639 		       CTL_KERN, KERN_POSIX_SPIN_LOCKS, CTL_EOL);
640 	sysctl_createv(clog, 0, NULL, NULL,
641 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
642 		       CTLTYPE_INT, "posix_reader_writer_locks",
643 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
644 				    "Read-Write Locks option to which the "
645 				    "system attempts to conform"),
646 		       /* XXX _POSIX_VERSION */
647 		       NULL, _POSIX_READER_WRITER_LOCKS, NULL, 0,
648 		       CTL_KERN, KERN_POSIX_READER_WRITER_LOCKS, CTL_EOL);
649 	sysctl_createv(clog, 0, NULL, NULL,
650 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
651 		       CTLTYPE_INT, "dump_on_panic",
652 		       SYSCTL_DESCR("Perform a crash dump on system panic"),
653 		       NULL, 0, &dumponpanic, 0,
654 		       CTL_KERN, KERN_DUMP_ON_PANIC, CTL_EOL);
655 #ifdef DIAGNOSTIC
656 	sysctl_createv(clog, 0, NULL, NULL,
657 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
658 		       CTLTYPE_INT, "panic_now",
659 		       SYSCTL_DESCR("Trigger a panic"),
660 		       sysctl_kern_trigger_panic, 0, NULL, 0,
661 		       CTL_KERN, CTL_CREATE, CTL_EOL);
662 #endif
663 	sysctl_createv(clog, 0, NULL, NULL,
664 		       CTLFLAG_PERMANENT,
665 		       CTLTYPE_INT, "root_partition",
666 		       SYSCTL_DESCR("Root partition on the root device"),
667 		       sysctl_kern_root_partition, 0, NULL, 0,
668 		       CTL_KERN, KERN_ROOT_PARTITION, CTL_EOL);
669 	sysctl_createv(clog, 0, NULL, NULL,
670 		       CTLFLAG_PERMANENT,
671 		       CTLTYPE_STRUCT, "drivers",
672 		       SYSCTL_DESCR("List of all drivers with block and "
673 				    "character device numbers"),
674 		       sysctl_kern_drivers, 0, NULL, 0,
675 		       CTL_KERN, KERN_DRIVERS, CTL_EOL);
676 	sysctl_createv(clog, 0, NULL, NULL,
677 		       CTLFLAG_PERMANENT,
678 		       CTLTYPE_STRUCT, "cp_id",
679 		       SYSCTL_DESCR("Mapping of CPU number to CPU id"),
680 		       sysctl_kern_cpid, 0, NULL, 0,
681 		       CTL_KERN, KERN_CP_ID, CTL_EOL);
682 	sysctl_createv(clog, 0, NULL, &rnode,
683 		       CTLFLAG_PERMANENT,
684 		       CTLTYPE_NODE, "coredump",
685 		       SYSCTL_DESCR("Coredump settings."),
686 		       NULL, 0, NULL, 0,
687 		       CTL_KERN, CTL_CREATE, CTL_EOL);
688 	sysctl_createv(clog, 0, &rnode, &rnode,
689 		       CTLFLAG_PERMANENT,
690 		       CTLTYPE_NODE, "setid",
691 		       SYSCTL_DESCR("Set-id processes' coredump settings."),
692 		       NULL, 0, NULL, 0,
693 		       CTL_CREATE, CTL_EOL);
694 	sysctl_createv(clog, 0, &rnode, NULL,
695 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
696 		       CTLTYPE_INT, "dump",
697 		       SYSCTL_DESCR("Allow set-id processes to dump core."),
698 		       sysctl_security_setidcore, 0, &security_setidcore_dump,
699 		       sizeof(security_setidcore_dump),
700 		       CTL_CREATE, CTL_EOL);
701 	sysctl_createv(clog, 0, &rnode, NULL,
702 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
703 		       CTLTYPE_STRING, "path",
704 		       SYSCTL_DESCR("Path pattern for set-id coredumps."),
705 		       sysctl_security_setidcorename, 0,
706 		       &security_setidcore_path,
707 		       sizeof(security_setidcore_path),
708 		       CTL_CREATE, CTL_EOL);
709 	sysctl_createv(clog, 0, &rnode, NULL,
710 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
711 		       CTLTYPE_INT, "owner",
712 		       SYSCTL_DESCR("Owner id for set-id processes' cores."),
713 		       sysctl_security_setidcore, 0, &security_setidcore_owner,
714 		       0,
715 		       CTL_CREATE, CTL_EOL);
716 	sysctl_createv(clog, 0, &rnode, NULL,
717 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
718 		       CTLTYPE_INT, "group",
719 		       SYSCTL_DESCR("Group id for set-id processes' cores."),
720 		       sysctl_security_setidcore, 0, &security_setidcore_group,
721 		       0,
722 		       CTL_CREATE, CTL_EOL);
723 	sysctl_createv(clog, 0, &rnode, NULL,
724 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
725 		       CTLTYPE_INT, "mode",
726 		       SYSCTL_DESCR("Mode for set-id processes' cores."),
727 		       sysctl_security_setidcore, 0, &security_setidcore_mode,
728 		       0,
729 		       CTL_CREATE, CTL_EOL);
730 	sysctl_createv(clog, 0, NULL, NULL,
731 		       CTLFLAG_IMMEDIATE|CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
732 		       CTLTYPE_INT, "no_sa_support",
733 		       SYSCTL_DESCR("0 if the kernel supports SA, otherwise "
734 		       "it doesn't"),
735 		       NULL, 1, NULL, 0,
736 		       CTL_KERN, CTL_CREATE, CTL_EOL);
737 	/* kern.posix. */
738 	sysctl_createv(clog, 0, NULL, &rnode,
739 			CTLFLAG_PERMANENT,
740 			CTLTYPE_NODE, "posix",
741 			SYSCTL_DESCR("POSIX options"),
742 			NULL, 0, NULL, 0,
743 			CTL_KERN, CTL_CREATE, CTL_EOL);
744 	sysctl_createv(clog, 0, &rnode, NULL,
745 			CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
746 			CTLTYPE_INT, "semmax",
747 			SYSCTL_DESCR("Maximal number of semaphores"),
748 			NULL, 0, &ksem_max, 0,
749 			CTL_CREATE, CTL_EOL);
750 }
751 
752 SYSCTL_SETUP(sysctl_hw_setup, "sysctl hw subtree setup")
753 {
754 	u_int u;
755 	u_quad_t q;
756 
757 	sysctl_createv(clog, 0, NULL, NULL,
758 		       CTLFLAG_PERMANENT,
759 		       CTLTYPE_NODE, "hw", NULL,
760 		       NULL, 0, NULL, 0,
761 		       CTL_HW, CTL_EOL);
762 
763 	sysctl_createv(clog, 0, NULL, NULL,
764 		       CTLFLAG_PERMANENT,
765 		       CTLTYPE_STRING, "machine",
766 		       SYSCTL_DESCR("Machine class"),
767 		       NULL, 0, machine, 0,
768 		       CTL_HW, HW_MACHINE, CTL_EOL);
769 	sysctl_createv(clog, 0, NULL, NULL,
770 		       CTLFLAG_PERMANENT,
771 		       CTLTYPE_STRING, "model",
772 		       SYSCTL_DESCR("Machine model"),
773 		       NULL, 0, cpu_model, 0,
774 		       CTL_HW, HW_MODEL, CTL_EOL);
775 	sysctl_createv(clog, 0, NULL, NULL,
776 		       CTLFLAG_PERMANENT,
777 		       CTLTYPE_INT, "ncpu",
778 		       SYSCTL_DESCR("Number of CPUs configured"),
779 		       NULL, 0, &ncpu, 0,
780 		       CTL_HW, HW_NCPU, CTL_EOL);
781 	sysctl_createv(clog, 0, NULL, NULL,
782 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
783 		       CTLTYPE_INT, "byteorder",
784 		       SYSCTL_DESCR("System byte order"),
785 		       NULL, BYTE_ORDER, NULL, 0,
786 		       CTL_HW, HW_BYTEORDER, CTL_EOL);
787 	u = ((u_int)physmem > (UINT_MAX / PAGE_SIZE)) ?
788 		UINT_MAX : physmem * PAGE_SIZE;
789 	sysctl_createv(clog, 0, NULL, NULL,
790 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
791 		       CTLTYPE_INT, "physmem",
792 		       SYSCTL_DESCR("Bytes of physical memory"),
793 		       NULL, u, NULL, 0,
794 		       CTL_HW, HW_PHYSMEM, CTL_EOL);
795 	sysctl_createv(clog, 0, NULL, NULL,
796 		       CTLFLAG_PERMANENT,
797 		       CTLTYPE_INT, "usermem",
798 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
799 		       sysctl_hw_usermem, 0, NULL, 0,
800 		       CTL_HW, HW_USERMEM, CTL_EOL);
801 	sysctl_createv(clog, 0, NULL, NULL,
802 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
803 		       CTLTYPE_INT, "pagesize",
804 		       SYSCTL_DESCR("Software page size"),
805 		       NULL, PAGE_SIZE, NULL, 0,
806 		       CTL_HW, HW_PAGESIZE, CTL_EOL);
807 	sysctl_createv(clog, 0, NULL, NULL,
808 		       CTLFLAG_PERMANENT,
809 		       CTLTYPE_STRING, "machine_arch",
810 		       SYSCTL_DESCR("Machine CPU class"),
811 		       NULL, 0, machine_arch, 0,
812 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
813 	sysctl_createv(clog, 0, NULL, NULL,
814 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
815 		       CTLTYPE_INT, "alignbytes",
816 		       SYSCTL_DESCR("Alignment constraint for all possible "
817 				    "data types"),
818 		       NULL, ALIGNBYTES, NULL, 0,
819 		       CTL_HW, HW_ALIGNBYTES, CTL_EOL);
820 	sysctl_createv(clog, 0, NULL, NULL,
821 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
822 		       CTLTYPE_STRING, "cnmagic",
823 		       SYSCTL_DESCR("Console magic key sequence"),
824 		       sysctl_hw_cnmagic, 0, NULL, CNS_LEN,
825 		       CTL_HW, HW_CNMAGIC, CTL_EOL);
826 	q = (u_quad_t)physmem * PAGE_SIZE;
827 	sysctl_createv(clog, 0, NULL, NULL,
828 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
829 		       CTLTYPE_QUAD, "physmem64",
830 		       SYSCTL_DESCR("Bytes of physical memory"),
831 		       NULL, q, NULL, 0,
832 		       CTL_HW, HW_PHYSMEM64, CTL_EOL);
833 	sysctl_createv(clog, 0, NULL, NULL,
834 		       CTLFLAG_PERMANENT,
835 		       CTLTYPE_QUAD, "usermem64",
836 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
837 		       sysctl_hw_usermem, 0, NULL, 0,
838 		       CTL_HW, HW_USERMEM64, CTL_EOL);
839 	sysctl_createv(clog, 0, NULL, NULL,
840 		       CTLFLAG_PERMANENT,
841 		       CTLTYPE_INT, "ncpuonline",
842 		       SYSCTL_DESCR("Number of CPUs online"),
843 		       NULL, 0, &ncpuonline, 0,
844 		       CTL_HW, HW_NCPUONLINE, CTL_EOL);
845 }
846 
847 #ifdef DEBUG
848 /*
849  * Debugging related system variables.
850  */
851 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
852 struct ctldebug debug5, debug6, debug7, debug8, debug9;
853 struct ctldebug debug10, debug11, debug12, debug13, debug14;
854 struct ctldebug debug15, debug16, debug17, debug18, debug19;
855 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
856 	&debug0, &debug1, &debug2, &debug3, &debug4,
857 	&debug5, &debug6, &debug7, &debug8, &debug9,
858 	&debug10, &debug11, &debug12, &debug13, &debug14,
859 	&debug15, &debug16, &debug17, &debug18, &debug19,
860 };
861 
862 /*
863  * this setup routine is a replacement for debug_sysctl()
864  *
865  * note that it creates several nodes per defined debug variable
866  */
867 SYSCTL_SETUP(sysctl_debug_setup, "sysctl debug subtree setup")
868 {
869 	struct ctldebug *cdp;
870 	char nodename[20];
871 	int i;
872 
873 	/*
874 	 * two ways here:
875 	 *
876 	 * the "old" way (debug.name -> value) which was emulated by
877 	 * the sysctl(8) binary
878 	 *
879 	 * the new way, which the sysctl(8) binary was actually using
880 
881 	 node	debug
882 	 node	debug.0
883 	 string debug.0.name
884 	 int	debug.0.value
885 	 int	debug.name
886 
887 	 */
888 
889 	sysctl_createv(clog, 0, NULL, NULL,
890 		       CTLFLAG_PERMANENT,
891 		       CTLTYPE_NODE, "debug", NULL,
892 		       NULL, 0, NULL, 0,
893 		       CTL_DEBUG, CTL_EOL);
894 
895 	for (i = 0; i < CTL_DEBUG_MAXID; i++) {
896 		cdp = debugvars[i];
897 		if (cdp->debugname == NULL || cdp->debugvar == NULL)
898 			continue;
899 
900 		snprintf(nodename, sizeof(nodename), "debug%d", i);
901 		sysctl_createv(clog, 0, NULL, NULL,
902 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
903 			       CTLTYPE_NODE, nodename, NULL,
904 			       NULL, 0, NULL, 0,
905 			       CTL_DEBUG, i, CTL_EOL);
906 		sysctl_createv(clog, 0, NULL, NULL,
907 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
908 			       CTLTYPE_STRING, "name", NULL,
909 			       /*XXXUNCONST*/
910 			       NULL, 0, __UNCONST(cdp->debugname), 0,
911 			       CTL_DEBUG, i, CTL_DEBUG_NAME, CTL_EOL);
912 		sysctl_createv(clog, 0, NULL, NULL,
913 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
914 			       CTLTYPE_INT, "value", NULL,
915 			       NULL, 0, cdp->debugvar, 0,
916 			       CTL_DEBUG, i, CTL_DEBUG_VALUE, CTL_EOL);
917 		sysctl_createv(clog, 0, NULL, NULL,
918 			       CTLFLAG_PERMANENT,
919 			       CTLTYPE_INT, cdp->debugname, NULL,
920 			       NULL, 0, cdp->debugvar, 0,
921 			       CTL_DEBUG, CTL_CREATE, CTL_EOL);
922 	}
923 }
924 #endif /* DEBUG */
925 
926 /*
927  * ********************************************************************
928  * section 2: private node-specific helper routines.
929  * ********************************************************************
930  */
931 
932 #ifdef DIAGNOSTIC
933 static int
934 sysctl_kern_trigger_panic(SYSCTLFN_ARGS)
935 {
936 	int newtrig, error;
937 	struct sysctlnode node;
938 
939 	newtrig = 0;
940 	node = *rnode;
941 	node.sysctl_data = &newtrig;
942 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
943 	if (error || newp == NULL)
944 		return (error);
945 
946 	if (newtrig != 0)
947 		panic("Panic triggered");
948 
949 	return (error);
950 }
951 #endif
952 
953 /*
954  * sysctl helper routine for kern.maxvnodes.  Drain vnodes if
955  * new value is lower than desiredvnodes and then calls reinit
956  * routines that needs to adjust to the new value.
957  */
958 static int
959 sysctl_kern_maxvnodes(SYSCTLFN_ARGS)
960 {
961 	int error, new_vnodes, old_vnodes, new_max;
962 	struct sysctlnode node;
963 
964 	new_vnodes = desiredvnodes;
965 	node = *rnode;
966 	node.sysctl_data = &new_vnodes;
967 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
968 	if (error || newp == NULL)
969 		return (error);
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.sbmax. Basically just ensures that
1347  * any new value is not too small.
1348  */
1349 static int
1350 sysctl_kern_sbmax(SYSCTLFN_ARGS)
1351 {
1352 	int error, new_sbmax;
1353 	struct sysctlnode node;
1354 
1355 	new_sbmax = sb_max;
1356 	node = *rnode;
1357 	node.sysctl_data = &new_sbmax;
1358 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1359 	if (error || newp == NULL)
1360 		return (error);
1361 
1362 	KERNEL_LOCK(1, NULL);
1363 	error = sb_max_set(new_sbmax);
1364 	KERNEL_UNLOCK_ONE(NULL);
1365 
1366 	return (error);
1367 }
1368 
1369 /*
1370  * sysctl helper routine for kern.urandom node. Picks a random number
1371  * for you.
1372  */
1373 static int
1374 sysctl_kern_urnd(SYSCTLFN_ARGS)
1375 {
1376 	int v, rv;
1377 
1378 	rv = cprng_strong(sysctl_prng, &v, sizeof(v), 0);
1379 	if (rv == sizeof(v)) {
1380 		struct sysctlnode node = *rnode;
1381 		node.sysctl_data = &v;
1382 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1383 	}
1384 	else
1385 		return (EIO);	/*XXX*/
1386 }
1387 
1388 /*
1389  * sysctl helper routine for kern.arandom node. Picks a random number
1390  * for you.
1391  */
1392 static int
1393 sysctl_kern_arnd(SYSCTLFN_ARGS)
1394 {
1395 	int error;
1396 	void *v;
1397 	struct sysctlnode node = *rnode;
1398 
1399 	if (*oldlenp == 0)
1400 		return 0;
1401 	/*
1402 	 * This code used to allow sucking 8192 bytes at a time out
1403 	 * of the kernel arc4random generator.  Evidently there is some
1404 	 * very old OpenBSD application code that may try to do this.
1405 	 *
1406 	 * Note that this node is documented as type "INT" -- 4 or 8
1407 	 * bytes, not 8192.
1408 	 *
1409 	 * We continue to support this abuse of the "len" pointer here
1410 	 * but only 256 bytes at a time, as, anecdotally, the actual
1411 	 * application use here was to generate RC4 keys in userspace.
1412 	 *
1413 	 * Support for such large requests will probably be removed
1414 	 * entirely in the future.
1415 	 */
1416 	if (*oldlenp > 256)
1417 		return E2BIG;
1418 
1419 	v = kmem_alloc(*oldlenp, KM_SLEEP);
1420 	cprng_fast(v, *oldlenp);
1421 	node.sysctl_data = v;
1422 	node.sysctl_size = *oldlenp;
1423 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1424 	kmem_free(v, *oldlenp);
1425 	return error;
1426 }
1427 /*
1428  * sysctl helper routine to do kern.lwp.* work.
1429  */
1430 static int
1431 sysctl_kern_lwp(SYSCTLFN_ARGS)
1432 {
1433 	struct kinfo_lwp klwp;
1434 	struct proc *p;
1435 	struct lwp *l2, *l3;
1436 	char *where, *dp;
1437 	int pid, elem_size, elem_count;
1438 	int buflen, needed, error;
1439 	bool gotit;
1440 
1441 	if (namelen == 1 && name[0] == CTL_QUERY)
1442 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
1443 
1444 	dp = where = oldp;
1445 	buflen = where != NULL ? *oldlenp : 0;
1446 	error = needed = 0;
1447 
1448 	if (newp != NULL || namelen != 3)
1449 		return (EINVAL);
1450 	pid = name[0];
1451 	elem_size = name[1];
1452 	elem_count = name[2];
1453 
1454 	sysctl_unlock();
1455 	if (pid == -1) {
1456 		mutex_enter(proc_lock);
1457 		PROCLIST_FOREACH(p, &allproc) {
1458 			/* Grab a hold on the process. */
1459 			if (!rw_tryenter(&p->p_reflock, RW_READER)) {
1460 				continue;
1461 			}
1462 			mutex_exit(proc_lock);
1463 
1464 			mutex_enter(p->p_lock);
1465 			LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1466 				if (buflen >= elem_size && elem_count > 0) {
1467 					lwp_lock(l2);
1468 					fill_lwp(l2, &klwp);
1469 					lwp_unlock(l2);
1470 					mutex_exit(p->p_lock);
1471 
1472 					/*
1473 					 * Copy out elem_size, but not
1474 					 * larger than the size of a
1475 					 * struct kinfo_proc2.
1476 					 */
1477 					error = dcopyout(l, &klwp, dp,
1478 					    min(sizeof(klwp), elem_size));
1479 					if (error) {
1480 						rw_exit(&p->p_reflock);
1481 						goto cleanup;
1482 					}
1483 					mutex_enter(p->p_lock);
1484 					LIST_FOREACH(l3, &p->p_lwps,
1485 					    l_sibling) {
1486 						if (l2 == l3)
1487 							break;
1488 					}
1489 					if (l3 == NULL) {
1490 						mutex_exit(p->p_lock);
1491 						rw_exit(&p->p_reflock);
1492 						error = EAGAIN;
1493 						goto cleanup;
1494 					}
1495 					dp += elem_size;
1496 					buflen -= elem_size;
1497 					elem_count--;
1498 				}
1499 				needed += elem_size;
1500 			}
1501 			mutex_exit(p->p_lock);
1502 
1503 			/* Drop reference to process. */
1504 			mutex_enter(proc_lock);
1505 			rw_exit(&p->p_reflock);
1506 		}
1507 		mutex_exit(proc_lock);
1508 	} else {
1509 		mutex_enter(proc_lock);
1510 		p = proc_find(pid);
1511 		if (p == NULL) {
1512 			error = ESRCH;
1513 			mutex_exit(proc_lock);
1514 			goto cleanup;
1515 		}
1516 		/* Grab a hold on the process. */
1517 		gotit = rw_tryenter(&p->p_reflock, RW_READER);
1518 		mutex_exit(proc_lock);
1519 		if (!gotit) {
1520 			error = ESRCH;
1521 			goto cleanup;
1522 		}
1523 
1524 		mutex_enter(p->p_lock);
1525 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1526 			if (buflen >= elem_size && elem_count > 0) {
1527 				lwp_lock(l2);
1528 				fill_lwp(l2, &klwp);
1529 				lwp_unlock(l2);
1530 				mutex_exit(p->p_lock);
1531 				/*
1532 				 * Copy out elem_size, but not larger than
1533 				 * the size of a struct kinfo_proc2.
1534 				 */
1535 				error = dcopyout(l, &klwp, dp,
1536 				    min(sizeof(klwp), elem_size));
1537 				if (error) {
1538 					rw_exit(&p->p_reflock);
1539 					goto cleanup;
1540 				}
1541 				mutex_enter(p->p_lock);
1542 				LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
1543 					if (l2 == l3)
1544 						break;
1545 				}
1546 				if (l3 == NULL) {
1547 					mutex_exit(p->p_lock);
1548 					rw_exit(&p->p_reflock);
1549 					error = EAGAIN;
1550 					goto cleanup;
1551 				}
1552 				dp += elem_size;
1553 				buflen -= elem_size;
1554 				elem_count--;
1555 			}
1556 			needed += elem_size;
1557 		}
1558 		mutex_exit(p->p_lock);
1559 
1560 		/* Drop reference to process. */
1561 		rw_exit(&p->p_reflock);
1562 	}
1563 
1564 	if (where != NULL) {
1565 		*oldlenp = dp - where;
1566 		if (needed > *oldlenp) {
1567 			sysctl_relock();
1568 			return (ENOMEM);
1569 		}
1570 	} else {
1571 		needed += KERN_LWPSLOP;
1572 		*oldlenp = needed;
1573 	}
1574 	error = 0;
1575  cleanup:
1576 	sysctl_relock();
1577 	return (error);
1578 }
1579 
1580 /*
1581  * sysctl helper routine for kern.forkfsleep node. Ensures that the
1582  * given value is not too large or two small, and is at least one
1583  * timer tick if not zero.
1584  */
1585 static int
1586 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
1587 {
1588 	/* userland sees value in ms, internally is in ticks */
1589 	extern int forkfsleep;		/* defined in kern/kern_fork.c */
1590 	int error, timo, lsleep;
1591 	struct sysctlnode node;
1592 
1593 	lsleep = forkfsleep * 1000 / hz;
1594 	node = *rnode;
1595 	node.sysctl_data = &lsleep;
1596 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1597 	if (error || newp == NULL)
1598 		return (error);
1599 
1600 	/* refuse negative values, and overly 'long time' */
1601 	if (lsleep < 0 || lsleep > MAXSLP * 1000)
1602 		return (EINVAL);
1603 
1604 	timo = mstohz(lsleep);
1605 
1606 	/* if the interval is >0 ms && <1 tick, use 1 tick */
1607 	if (lsleep != 0 && timo == 0)
1608 		forkfsleep = 1;
1609 	else
1610 		forkfsleep = timo;
1611 
1612 	return (0);
1613 }
1614 
1615 /*
1616  * sysctl helper routine for kern.root_partition
1617  */
1618 static int
1619 sysctl_kern_root_partition(SYSCTLFN_ARGS)
1620 {
1621 	int rootpart = DISKPART(rootdev);
1622 	struct sysctlnode node = *rnode;
1623 
1624 	node.sysctl_data = &rootpart;
1625 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1626 }
1627 
1628 /*
1629  * sysctl helper function for kern.drivers
1630  */
1631 static int
1632 sysctl_kern_drivers(SYSCTLFN_ARGS)
1633 {
1634 	int error;
1635 	size_t buflen;
1636 	struct kinfo_drivers kd;
1637 	char *start, *where;
1638 	const char *dname;
1639 	int i;
1640 	extern struct devsw_conv *devsw_conv;
1641 	extern int max_devsw_convs;
1642 
1643 	if (newp != NULL || namelen != 0)
1644 		return (EINVAL);
1645 
1646 	start = where = oldp;
1647 	buflen = *oldlenp;
1648 	if (where == NULL) {
1649 		*oldlenp = max_devsw_convs * sizeof kd;
1650 		return 0;
1651 	}
1652 
1653 	/*
1654 	 * An array of kinfo_drivers structures
1655 	 */
1656 	error = 0;
1657 	sysctl_unlock();
1658 	mutex_enter(&device_lock);
1659 	for (i = 0; i < max_devsw_convs; i++) {
1660 		dname = devsw_conv[i].d_name;
1661 		if (dname == NULL)
1662 			continue;
1663 		if (buflen < sizeof kd) {
1664 			error = ENOMEM;
1665 			break;
1666 		}
1667 		memset(&kd, 0, sizeof(kd));
1668 		kd.d_bmajor = devsw_conv[i].d_bmajor;
1669 		kd.d_cmajor = devsw_conv[i].d_cmajor;
1670 		strlcpy(kd.d_name, dname, sizeof kd.d_name);
1671 		mutex_exit(&device_lock);
1672 		error = dcopyout(l, &kd, where, sizeof kd);
1673 		mutex_enter(&device_lock);
1674 		if (error != 0)
1675 			break;
1676 		buflen -= sizeof kd;
1677 		where += sizeof kd;
1678 	}
1679 	mutex_exit(&device_lock);
1680 	sysctl_relock();
1681 	*oldlenp = where - start;
1682 	return error;
1683 }
1684 
1685 static int
1686 sysctl_security_setidcore(SYSCTLFN_ARGS)
1687 {
1688 	int newsize, error;
1689 	struct sysctlnode node;
1690 
1691 	node = *rnode;
1692 	node.sysctl_data = &newsize;
1693 	newsize = *(int *)rnode->sysctl_data;
1694 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1695 	if (error || newp == NULL)
1696 		return error;
1697 
1698 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
1699 	    0, NULL, NULL, NULL))
1700 		return (EPERM);
1701 
1702 	*(int *)rnode->sysctl_data = newsize;
1703 
1704 	return 0;
1705 }
1706 
1707 static int
1708 sysctl_security_setidcorename(SYSCTLFN_ARGS)
1709 {
1710 	int error;
1711 	char *newsetidcorename;
1712 	struct sysctlnode node;
1713 
1714 	newsetidcorename = PNBUF_GET();
1715 	node = *rnode;
1716 	node.sysctl_data = newsetidcorename;
1717 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
1718 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1719 	if (error || newp == NULL) {
1720 		goto out;
1721 	}
1722 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
1723 	    0, NULL, NULL, NULL)) {
1724 		error = EPERM;
1725 		goto out;
1726 	}
1727 	if (strlen(newsetidcorename) == 0) {
1728 		error = EINVAL;
1729 		goto out;
1730 	}
1731 	memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
1732 out:
1733 	PNBUF_PUT(newsetidcorename);
1734 	return error;
1735 }
1736 
1737 /*
1738  * sysctl helper routine for kern.cp_id node. Maps cpus to their
1739  * cpuids.
1740  */
1741 static int
1742 sysctl_kern_cpid(SYSCTLFN_ARGS)
1743 {
1744 	struct sysctlnode node = *rnode;
1745 	uint64_t *cp_id = NULL;
1746 	int error, n = ncpu;
1747 	struct cpu_info *ci;
1748 	CPU_INFO_ITERATOR cii;
1749 
1750 	/*
1751 	 * Here you may either retrieve a single cpu id or the whole
1752 	 * set. The size you get back when probing depends on what
1753 	 * you ask for.
1754 	 */
1755 	switch (namelen) {
1756 	case 0:
1757 		node.sysctl_size = n * sizeof(uint64_t);
1758 		n = -2; /* ALL */
1759 		break;
1760 	case 1:
1761 		if (name[0] < 0 || name[0] >= n)
1762 			return (ENOENT); /* ENOSUCHPROCESSOR */
1763 		node.sysctl_size = sizeof(uint64_t);
1764 		n = name[0];
1765 		/*
1766 		 * adjust these so that sysctl_lookup() will be happy
1767 		 */
1768 		name++;
1769 		namelen--;
1770 		break;
1771 	default:
1772 		return (EINVAL);
1773 	}
1774 
1775 	cp_id = kmem_alloc(node.sysctl_size, KM_SLEEP);
1776 	if (cp_id == NULL)
1777 		return (ENOMEM);
1778 	node.sysctl_data = cp_id;
1779 	memset(cp_id, 0, node.sysctl_size);
1780 
1781 	for (CPU_INFO_FOREACH(cii, ci)) {
1782 		if (n <= 0)
1783 			cp_id[0] = cpu_index(ci);
1784 		/*
1785 		 * if a specific processor was requested and we just
1786 		 * did it, we're done here
1787 		 */
1788 		if (n == 0)
1789 			break;
1790 		/*
1791 		 * if doing "all", skip to next cp_id slot for next processor
1792 		 */
1793 		if (n == -2)
1794 			cp_id++;
1795 		/*
1796 		 * if we're doing a specific processor, we're one
1797 		 * processor closer
1798 		 */
1799 		if (n > 0)
1800 			n--;
1801 	}
1802 
1803 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1804 	kmem_free(node.sysctl_data, node.sysctl_size);
1805 	return (error);
1806 }
1807 
1808 /*
1809  * sysctl helper routine for hw.usermem and hw.usermem64. Values are
1810  * calculate on the fly taking into account integer overflow and the
1811  * current wired count.
1812  */
1813 static int
1814 sysctl_hw_usermem(SYSCTLFN_ARGS)
1815 {
1816 	u_int ui;
1817 	u_quad_t uq;
1818 	struct sysctlnode node;
1819 
1820 	node = *rnode;
1821 	switch (rnode->sysctl_num) {
1822 	case HW_USERMEM:
1823 		if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
1824 			ui = UINT_MAX;
1825 		else
1826 			ui *= PAGE_SIZE;
1827 		node.sysctl_data = &ui;
1828 		break;
1829 	case HW_USERMEM64:
1830 		uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
1831 		node.sysctl_data = &uq;
1832 		break;
1833 	default:
1834 		return (EINVAL);
1835 	}
1836 
1837 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1838 }
1839 
1840 /*
1841  * sysctl helper routine for kern.cnmagic node. Pulls the old value
1842  * out, encoded, and stuffs the new value in for decoding.
1843  */
1844 static int
1845 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
1846 {
1847 	char magic[CNS_LEN];
1848 	int error;
1849 	struct sysctlnode node;
1850 
1851 	if (oldp)
1852 		cn_get_magic(magic, CNS_LEN);
1853 	node = *rnode;
1854 	node.sysctl_data = &magic[0];
1855 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1856 	if (error || newp == NULL)
1857 		return (error);
1858 
1859 	return (cn_set_magic(magic));
1860 }
1861 
1862 /*
1863  * ********************************************************************
1864  * section 3: public helper routines that are used for more than one
1865  * node
1866  * ********************************************************************
1867  */
1868 
1869 /*
1870  * sysctl helper routine for the kern.root_device node and some ports'
1871  * machdep.root_device nodes.
1872  */
1873 int
1874 sysctl_root_device(SYSCTLFN_ARGS)
1875 {
1876 	struct sysctlnode node;
1877 
1878 	node = *rnode;
1879 	node.sysctl_data = root_device->dv_xname;
1880 	node.sysctl_size = strlen(device_xname(root_device)) + 1;
1881 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1882 }
1883 
1884 /*
1885  * sysctl helper routine for kern.consdev, dependent on the current
1886  * state of the console. Also used for machdep.console_device on some
1887  * ports.
1888  */
1889 int
1890 sysctl_consdev(SYSCTLFN_ARGS)
1891 {
1892 	dev_t consdev;
1893 	uint32_t oconsdev;
1894 	struct sysctlnode node;
1895 
1896 	if (cn_tab != NULL)
1897 		consdev = cn_tab->cn_dev;
1898 	else
1899 		consdev = NODEV;
1900 	node = *rnode;
1901 	switch (*oldlenp) {
1902 	case sizeof(consdev):
1903 		node.sysctl_data = &consdev;
1904 		node.sysctl_size = sizeof(consdev);
1905 		break;
1906 	case sizeof(oconsdev):
1907 		oconsdev = (uint32_t)consdev;
1908 		node.sysctl_data = &oconsdev;
1909 		node.sysctl_size = sizeof(oconsdev);
1910 		break;
1911 	default:
1912 		return EINVAL;
1913 	}
1914 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1915 }
1916 
1917 /*
1918  * ********************************************************************
1919  * section 4: support for some helpers
1920  * ********************************************************************
1921  */
1922 
1923 
1924 /*
1925  * Fill in a kinfo_lwp structure for the specified lwp.
1926  */
1927 static void
1928 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
1929 {
1930 	struct proc *p = l->l_proc;
1931 	struct timeval tv;
1932 
1933 	KASSERT(lwp_locked(l, NULL));
1934 
1935 	memset(kl, 0, sizeof(*kl));
1936 
1937 	kl->l_forw = 0;
1938 	kl->l_back = 0;
1939 	kl->l_laddr = PTRTOUINT64(l);
1940 	kl->l_addr = PTRTOUINT64(l->l_addr);
1941 	kl->l_stat = l->l_stat;
1942 	kl->l_lid = l->l_lid;
1943 	kl->l_flag = L_INMEM;
1944 	kl->l_flag |= sysctl_map_flags(sysctl_lwpprflagmap, l->l_prflag);
1945 	kl->l_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
1946 
1947 	kl->l_swtime = l->l_swtime;
1948 	kl->l_slptime = l->l_slptime;
1949 	if (l->l_stat == LSONPROC)
1950 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
1951 	else
1952 		kl->l_schedflags = 0;
1953 	kl->l_priority = lwp_eprio(l);
1954 	kl->l_usrpri = l->l_priority;
1955 	if (l->l_wchan)
1956 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
1957 	kl->l_wchan = PTRTOUINT64(l->l_wchan);
1958 	kl->l_cpuid = cpu_index(l->l_cpu);
1959 	bintime2timeval(&l->l_rtime, &tv);
1960 	kl->l_rtime_sec = tv.tv_sec;
1961 	kl->l_rtime_usec = tv.tv_usec;
1962 	kl->l_cpticks = l->l_cpticks;
1963 	kl->l_pctcpu = l->l_pctcpu;
1964 	kl->l_pid = p->p_pid;
1965 	if (l->l_name == NULL)
1966 		kl->l_name[0] = '\0';
1967 	else
1968 		strlcpy(kl->l_name, l->l_name, sizeof(kl->l_name));
1969 }
1970