xref: /netbsd-src/sys/kern/init_sysctl.c (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1 /*	$NetBSD: init_sysctl.c,v 1.222 2019/01/15 07:11:23 mrg 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.222 2019/01/15 07:11:23 mrg Exp $");
34 
35 #include "opt_sysv.h"
36 #include "opt_compat_netbsd.h"
37 #include "opt_modular.h"
38 #include "opt_gprof.h"
39 #include "pty.h"
40 
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/sysctl.h>
44 #include <sys/cpu.h>
45 #include <sys/errno.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/unistd.h>
49 #include <sys/disklabel.h>
50 #include <sys/cprng.h>
51 #include <sys/vnode_impl.h>	/* For vfs_drainvnodes(). */
52 #include <sys/mount.h>
53 #include <sys/namei.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/reboot.h>
61 #include <sys/resource.h>
62 #include <sys/resourcevar.h>
63 #include <sys/exec.h>
64 #include <sys/conf.h>
65 #include <sys/device.h>
66 #include <sys/stat.h>
67 #include <sys/kauth.h>
68 #include <sys/ktrace.h>
69 
70 #include <sys/cpu.h>
71 
72 int security_setidcore_dump;
73 char security_setidcore_path[MAXPATHLEN] = "/var/crash/%n.core";
74 uid_t security_setidcore_owner = 0;
75 gid_t security_setidcore_group = 0;
76 mode_t security_setidcore_mode = (S_IRUSR|S_IWUSR);
77 
78 /*
79  * Current status of SysV IPC capability.  Initially, these are
80  * 0 if the capability is not built-in to the kernel, but can
81  * be updated if the appropriate kernel module is (auto)loaded.
82  */
83 
84 int kern_has_sysvmsg = 0;
85 int kern_has_sysvshm = 0;
86 int kern_has_sysvsem = 0;
87 
88 static const u_int sysctl_lwpprflagmap[] = {
89 	LPR_DETACHED, L_DETACHED,
90 	0
91 };
92 
93 /*
94  * try over estimating by 5 procs/lwps
95  */
96 #define KERN_LWPSLOP	(5 * sizeof(struct kinfo_lwp))
97 
98 static int dcopyout(struct lwp *, const void *, void *, size_t);
99 
100 static int
101 dcopyout(struct lwp *l, const void *kaddr, void *uaddr, size_t len)
102 {
103 	int error;
104 
105 	error = copyout(kaddr, uaddr, len);
106 	ktrmibio(-1, UIO_READ, uaddr, len, error);
107 
108 	return error;
109 }
110 
111 static int sysctl_kern_maxvnodes(SYSCTLFN_PROTO);
112 static int sysctl_kern_messages(SYSCTLFN_PROTO);
113 static int sysctl_kern_rtc_offset(SYSCTLFN_PROTO);
114 static int sysctl_kern_maxproc(SYSCTLFN_PROTO);
115 static int sysctl_kern_hostid(SYSCTLFN_PROTO);
116 static int sysctl_kern_defcorename(SYSCTLFN_PROTO);
117 static int sysctl_kern_cptime(SYSCTLFN_PROTO);
118 #if NPTY > 0
119 static int sysctl_kern_maxptys(SYSCTLFN_PROTO);
120 #endif /* NPTY > 0 */
121 static int sysctl_kern_lwp(SYSCTLFN_PROTO);
122 static int sysctl_kern_forkfsleep(SYSCTLFN_PROTO);
123 static int sysctl_kern_root_partition(SYSCTLFN_PROTO);
124 static int sysctl_kern_drivers(SYSCTLFN_PROTO);
125 static int sysctl_security_setidcore(SYSCTLFN_PROTO);
126 static int sysctl_security_setidcorename(SYSCTLFN_PROTO);
127 static int sysctl_kern_cpid(SYSCTLFN_PROTO);
128 static int sysctl_hw_usermem(SYSCTLFN_PROTO);
129 static int sysctl_hw_cnmagic(SYSCTLFN_PROTO);
130 
131 static void fill_lwp(struct lwp *l, struct kinfo_lwp *kl);
132 
133 /*
134  * ********************************************************************
135  * section 1: setup routines
136  * ********************************************************************
137  * These functions are stuffed into a link set for sysctl setup
138  * functions. They're never called or referenced from anywhere else.
139  * ********************************************************************
140  */
141 
142 /*
143  * this setup routine is a replacement for kern_sysctl()
144  */
145 SYSCTL_SETUP(sysctl_kern_setup, "sysctl kern subtree setup")
146 {
147 	extern int kern_logsigexit;	/* defined in kern/kern_sig.c */
148 	extern fixpt_t ccpu;		/* defined in kern/kern_synch.c */
149 	extern int dumponpanic;		/* defined in kern/subr_prf.c */
150 	const struct sysctlnode *rnode;
151 
152 	sysctl_createv(clog, 0, NULL, NULL,
153 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
154 		       CTLTYPE_INT, "maxvnodes",
155 		       SYSCTL_DESCR("Maximum number of vnodes"),
156 		       sysctl_kern_maxvnodes, 0, NULL, 0,
157 		       CTL_KERN, KERN_MAXVNODES, CTL_EOL);
158 	sysctl_createv(clog, 0, NULL, NULL,
159 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
160 		       CTLTYPE_INT, "maxproc",
161 		       SYSCTL_DESCR("Maximum number of simultaneous processes"),
162 		       sysctl_kern_maxproc, 0, NULL, 0,
163 		       CTL_KERN, KERN_MAXPROC, CTL_EOL);
164 	sysctl_createv(clog, 0, NULL, NULL,
165 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
166 		       CTLTYPE_INT, "maxfiles",
167 		       SYSCTL_DESCR("Maximum number of open files"),
168 		       NULL, 0, &maxfiles, 0,
169 		       CTL_KERN, KERN_MAXFILES, CTL_EOL);
170 	sysctl_createv(clog, 0, NULL, NULL,
171 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
172 		       CTLTYPE_INT, "argmax",
173 		       SYSCTL_DESCR("Maximum number of bytes of arguments to "
174 				    "execve(2)"),
175 		       NULL, ARG_MAX, NULL, 0,
176 		       CTL_KERN, KERN_ARGMAX, CTL_EOL);
177 	sysctl_createv(clog, 0, NULL, NULL,
178 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
179 		       CTLTYPE_INT, "hostid",
180 		       SYSCTL_DESCR("System host ID number"),
181 		       sysctl_kern_hostid, 0, NULL, 0,
182 		       CTL_KERN, KERN_HOSTID, CTL_EOL);
183 	sysctl_createv(clog, 0, NULL, NULL,
184 		       CTLFLAG_PERMANENT,
185 		       CTLTYPE_STRUCT, "vnode",
186 		       SYSCTL_DESCR("System vnode table"),
187 		       sysctl_kern_vnode, 0, NULL, 0,
188 		       CTL_KERN, KERN_VNODE, CTL_EOL);
189 #ifndef GPROF
190 	sysctl_createv(clog, 0, NULL, NULL,
191 		       CTLFLAG_PERMANENT,
192 		       CTLTYPE_NODE, "profiling",
193 		       SYSCTL_DESCR("Profiling information (not available)"),
194 		       sysctl_notavail, 0, NULL, 0,
195 		       CTL_KERN, KERN_PROF, CTL_EOL);
196 #endif
197 	sysctl_createv(clog, 0, NULL, NULL,
198 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
199 		       CTLTYPE_INT, "posix1version",
200 		       SYSCTL_DESCR("Version of ISO/IEC 9945 (POSIX 1003.1) "
201 				    "with which the operating system attempts "
202 				    "to comply"),
203 		       NULL, _POSIX_VERSION, NULL, 0,
204 		       CTL_KERN, KERN_POSIX1, CTL_EOL);
205 	sysctl_createv(clog, 0, NULL, NULL,
206 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
207 		       CTLTYPE_INT, "ngroups",
208 		       SYSCTL_DESCR("Maximum number of supplemental groups"),
209 		       NULL, NGROUPS_MAX, NULL, 0,
210 		       CTL_KERN, KERN_NGROUPS, CTL_EOL);
211 	sysctl_createv(clog, 0, NULL, NULL,
212 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
213 		       CTLTYPE_INT, "job_control",
214 		       SYSCTL_DESCR("Whether job control is available"),
215 		       NULL, 1, NULL, 0,
216 		       CTL_KERN, KERN_JOB_CONTROL, CTL_EOL);
217 	sysctl_createv(clog, 0, NULL, NULL,
218 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
219 		       CTLTYPE_INT, "saved_ids",
220 		       SYSCTL_DESCR("Whether POSIX saved set-group/user ID is "
221 				    "available"), NULL,
222 #ifdef _POSIX_SAVED_IDS
223 		       1,
224 #else /* _POSIX_SAVED_IDS */
225 		       0,
226 #endif /* _POSIX_SAVED_IDS */
227 		       NULL, 0, CTL_KERN, KERN_SAVED_IDS, CTL_EOL);
228 	sysctl_createv(clog, 0, NULL, NULL,
229 		       CTLFLAG_PERMANENT|CTLFLAG_HEX,
230 		       CTLTYPE_INT, "boothowto",
231 		       SYSCTL_DESCR("Flags from boot loader"),
232 		       NULL, 0, &boothowto, sizeof(boothowto),
233 		       CTL_KERN, CTL_CREATE, CTL_EOL);
234 	sysctl_createv(clog, 0, NULL, NULL,
235 		       CTLFLAG_PERMANENT,
236 		       CTLTYPE_STRUCT, "boottime",
237 		       SYSCTL_DESCR("System boot time"),
238 		       NULL, 0, &boottime, sizeof(boottime),
239 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
240 	sysctl_createv(clog, 0, NULL, NULL,
241 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
242 		       CTLTYPE_INT, "maxpartitions",
243 		       SYSCTL_DESCR("Maximum number of partitions allowed per "
244 				    "disk"),
245 		       NULL, MAXPARTITIONS, NULL, 0,
246 		       CTL_KERN, KERN_MAXPARTITIONS, CTL_EOL);
247 	sysctl_createv(clog, 0, NULL, NULL,
248 		       CTLFLAG_PERMANENT,
249 		       CTLTYPE_STRUCT, "timex", NULL,
250 		       sysctl_notavail, 0, NULL, 0,
251 		       CTL_KERN, KERN_TIMEX, CTL_EOL);
252 	sysctl_createv(clog, 0, NULL, NULL,
253 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
254 		       CTLTYPE_INT, "rtc_offset",
255 		       SYSCTL_DESCR("Offset of real time clock from UTC in "
256 				    "minutes"),
257 		       sysctl_kern_rtc_offset, 0, &rtc_offset, 0,
258 		       CTL_KERN, KERN_RTC_OFFSET, CTL_EOL);
259 	sysctl_createv(clog, 0, NULL, NULL,
260 		       CTLFLAG_PERMANENT,
261 		       CTLTYPE_STRING, "root_device",
262 		       SYSCTL_DESCR("Name of the root device"),
263 		       sysctl_root_device, 0, NULL, 0,
264 		       CTL_KERN, KERN_ROOT_DEVICE, CTL_EOL);
265 	sysctl_createv(clog, 0, NULL, NULL,
266 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
267 		       CTLTYPE_INT, "fsync",
268 		       SYSCTL_DESCR("Whether the POSIX 1003.1b File "
269 				    "Synchronization Option is available on "
270 				    "this system"),
271 		       NULL, 1, NULL, 0,
272 		       CTL_KERN, KERN_FSYNC, CTL_EOL);
273 	sysctl_createv(clog, 0, NULL, NULL,
274 		       CTLFLAG_PERMANENT,
275 		       CTLTYPE_NODE, "ipc",
276 		       SYSCTL_DESCR("SysV IPC options"),
277 		       NULL, 0, NULL, 0,
278 		       CTL_KERN, KERN_SYSVIPC, CTL_EOL);
279 	sysctl_createv(clog, 0, NULL, NULL,
280 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
281 		       CTLTYPE_INT, "sysvmsg",
282 		       SYSCTL_DESCR("System V style message support available"),
283 		       NULL, 0, &kern_has_sysvmsg, sizeof(int),
284 		       CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_MSG, CTL_EOL);
285 	sysctl_createv(clog, 0, NULL, NULL,
286 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
287 		       CTLTYPE_INT, "sysvsem",
288 		       SYSCTL_DESCR("System V style semaphore support "
289 				    "available"),
290 		       NULL, 0, &kern_has_sysvsem, sizeof(int),
291 		       CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SEM, CTL_EOL);
292 	sysctl_createv(clog, 0, NULL, NULL,
293 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
294 		       CTLTYPE_INT, "sysvshm",
295 		       SYSCTL_DESCR("System V style shared memory support "
296 				    "available"),
297 		       NULL, 0, &kern_has_sysvshm, sizeof(int),
298 		       CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHM, CTL_EOL);
299 	sysctl_createv(clog, 0, NULL, NULL,
300 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
301 		       CTLTYPE_INT, "synchronized_io",
302 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Synchronized "
303 				    "I/O Option is available on this system"),
304 		       NULL, 1, NULL, 0,
305 		       CTL_KERN, KERN_SYNCHRONIZED_IO, CTL_EOL);
306 	sysctl_createv(clog, 0, NULL, NULL,
307 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
308 		       CTLTYPE_INT, "iov_max",
309 		       SYSCTL_DESCR("Maximum number of iovec structures per "
310 				    "process"),
311 		       NULL, IOV_MAX, NULL, 0,
312 		       CTL_KERN, KERN_IOV_MAX, CTL_EOL);
313 	sysctl_createv(clog, 0, NULL, NULL,
314 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
315 		       CTLTYPE_INT, "mapped_files",
316 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory Mapped "
317 				    "Files Option is available on this system"),
318 		       NULL, 1, NULL, 0,
319 		       CTL_KERN, KERN_MAPPED_FILES, CTL_EOL);
320 	sysctl_createv(clog, 0, NULL, NULL,
321 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
322 		       CTLTYPE_INT, "memlock",
323 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Process Memory "
324 				    "Locking Option is available on this "
325 				    "system"),
326 		       NULL, 1, NULL, 0,
327 		       CTL_KERN, KERN_MEMLOCK, CTL_EOL);
328 	sysctl_createv(clog, 0, NULL, NULL,
329 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
330 		       CTLTYPE_INT, "memlock_range",
331 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Range Memory "
332 				    "Locking Option is available on this "
333 				    "system"),
334 		       NULL, 1, NULL, 0,
335 		       CTL_KERN, KERN_MEMLOCK_RANGE, CTL_EOL);
336 	sysctl_createv(clog, 0, NULL, NULL,
337 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
338 		       CTLTYPE_INT, "memory_protection",
339 		       SYSCTL_DESCR("Whether the POSIX 1003.1b Memory "
340 				    "Protection Option is available on this "
341 				    "system"),
342 		       NULL, 1, NULL, 0,
343 		       CTL_KERN, KERN_MEMORY_PROTECTION, CTL_EOL);
344 	sysctl_createv(clog, 0, NULL, NULL,
345 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
346 		       CTLTYPE_INT, "login_name_max",
347 		       SYSCTL_DESCR("Maximum login name length"),
348 		       NULL, LOGIN_NAME_MAX, NULL, 0,
349 		       CTL_KERN, KERN_LOGIN_NAME_MAX, CTL_EOL);
350 	sysctl_createv(clog, 0, NULL, NULL,
351 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
352 		       CTLTYPE_STRING, "defcorename",
353 		       SYSCTL_DESCR("Default core file name"),
354 		       sysctl_kern_defcorename, 0, defcorename, MAXPATHLEN,
355 		       CTL_KERN, KERN_DEFCORENAME, CTL_EOL);
356 	sysctl_createv(clog, 0, NULL, NULL,
357 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
358 		       CTLTYPE_INT, "logsigexit",
359 		       SYSCTL_DESCR("Log process exit when caused by signals"),
360 		       NULL, 0, &kern_logsigexit, 0,
361 		       CTL_KERN, KERN_LOGSIGEXIT, CTL_EOL);
362 	sysctl_createv(clog, 0, NULL, NULL,
363 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
364 		       CTLTYPE_INT, "fscale",
365 		       SYSCTL_DESCR("Kernel fixed-point scale factor"),
366 		       NULL, FSCALE, NULL, 0,
367 		       CTL_KERN, KERN_FSCALE, CTL_EOL);
368 	sysctl_createv(clog, 0, NULL, NULL,
369 		       CTLFLAG_PERMANENT,
370 		       CTLTYPE_INT, "ccpu",
371 		       SYSCTL_DESCR("Scheduler exponential decay value"),
372 		       NULL, 0, &ccpu, 0,
373 		       CTL_KERN, KERN_CCPU, CTL_EOL);
374 	sysctl_createv(clog, 0, NULL, NULL,
375 		       CTLFLAG_PERMANENT,
376 		       CTLTYPE_STRUCT, "cp_time",
377 		       SYSCTL_DESCR("Clock ticks spent in different CPU states"),
378 		       sysctl_kern_cptime, 0, NULL, 0,
379 		       CTL_KERN, KERN_CP_TIME, CTL_EOL);
380 	sysctl_createv(clog, 0, NULL, NULL,
381 		       CTLFLAG_PERMANENT,
382 		       CTLTYPE_STRUCT, "consdev",
383 		       SYSCTL_DESCR("Console device"),
384 		       sysctl_consdev, 0, NULL, sizeof(dev_t),
385 		       CTL_KERN, KERN_CONSDEV, CTL_EOL);
386 #if NPTY > 0
387 	sysctl_createv(clog, 0, NULL, NULL,
388 		       CTLFLAG_PERMANENT,
389 		       CTLTYPE_INT, "maxptys",
390 		       SYSCTL_DESCR("Maximum number of pseudo-ttys"),
391 		       sysctl_kern_maxptys, 0, NULL, 0,
392 		       CTL_KERN, KERN_MAXPTYS, CTL_EOL);
393 #endif /* NPTY > 0 */
394 	sysctl_createv(clog, 0, NULL, NULL,
395 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
396 		       CTLTYPE_INT, "maxphys",
397 		       SYSCTL_DESCR("Maximum raw I/O transfer size"),
398 		       NULL, MAXPHYS, NULL, 0,
399 		       CTL_KERN, KERN_MAXPHYS, CTL_EOL);
400 	sysctl_createv(clog, 0, NULL, NULL,
401 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
402 		       CTLTYPE_INT, "monotonic_clock",
403 		       SYSCTL_DESCR("Implementation version of the POSIX "
404 				    "1003.1b Monotonic Clock Option"),
405 		       /* XXX _POSIX_VERSION */
406 		       NULL, _POSIX_MONOTONIC_CLOCK, NULL, 0,
407 		       CTL_KERN, KERN_MONOTONIC_CLOCK, CTL_EOL);
408 	sysctl_createv(clog, 0, NULL, NULL,
409 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
410 		       CTLTYPE_INT, "labelsector",
411 		       SYSCTL_DESCR("Sector number containing the disklabel"),
412 		       NULL, LABELSECTOR, NULL, 0,
413 		       CTL_KERN, KERN_LABELSECTOR, CTL_EOL);
414 	sysctl_createv(clog, 0, NULL, NULL,
415 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
416 		       CTLTYPE_INT, "labeloffset",
417 		       SYSCTL_DESCR("Offset of the disklabel within the "
418 				    "sector"),
419 		       NULL, LABELOFFSET, NULL, 0,
420 		       CTL_KERN, KERN_LABELOFFSET, CTL_EOL);
421 	sysctl_createv(clog, 0, NULL, NULL,
422 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
423 		       CTLTYPE_INT, "labelusesmbr",
424 		       SYSCTL_DESCR("disklabel is inside MBR partition"),
425 		       NULL, LABELUSESMBR, NULL, 0,
426 		       CTL_KERN, CTL_CREATE, CTL_EOL);
427 	sysctl_createv(clog, 0, NULL, NULL,
428 		       CTLFLAG_PERMANENT,
429 		       CTLTYPE_NODE, "lwp",
430 		       SYSCTL_DESCR("System-wide LWP information"),
431 		       sysctl_kern_lwp, 0, NULL, 0,
432 		       CTL_KERN, KERN_LWP, CTL_EOL);
433 	sysctl_createv(clog, 0, NULL, NULL,
434 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
435 		       CTLTYPE_INT, "forkfsleep",
436 		       SYSCTL_DESCR("Milliseconds to sleep on fork failure due "
437 				    "to process limits"),
438 		       sysctl_kern_forkfsleep, 0, NULL, 0,
439 		       CTL_KERN, KERN_FORKFSLEEP, CTL_EOL);
440 	sysctl_createv(clog, 0, NULL, NULL,
441 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
442 		       CTLTYPE_INT, "posix_threads",
443 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
444 				    "Threads option to which the system "
445 				    "attempts to conform"),
446 		       /* XXX _POSIX_VERSION */
447 		       NULL, _POSIX_THREADS, NULL, 0,
448 		       CTL_KERN, KERN_POSIX_THREADS, CTL_EOL);
449 	sysctl_createv(clog, 0, NULL, NULL,
450 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
451 		       CTLTYPE_INT, "posix_semaphores",
452 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
453 				    "Semaphores option to which the system "
454 				    "attempts to conform"), NULL,
455 		       200112, NULL, 0,
456 		       CTL_KERN, KERN_POSIX_SEMAPHORES, CTL_EOL);
457 	sysctl_createv(clog, 0, NULL, NULL,
458 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
459 		       CTLTYPE_INT, "posix_barriers",
460 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
461 				    "Barriers option to which the system "
462 				    "attempts to conform"),
463 		       /* XXX _POSIX_VERSION */
464 		       NULL, _POSIX_BARRIERS, NULL, 0,
465 		       CTL_KERN, KERN_POSIX_BARRIERS, CTL_EOL);
466 	sysctl_createv(clog, 0, NULL, NULL,
467 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
468 		       CTLTYPE_INT, "posix_timers",
469 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
470 				    "Timers option to which the system "
471 				    "attempts to conform"),
472 		       /* XXX _POSIX_VERSION */
473 		       NULL, _POSIX_TIMERS, NULL, 0,
474 		       CTL_KERN, KERN_POSIX_TIMERS, CTL_EOL);
475 	sysctl_createv(clog, 0, NULL, NULL,
476 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
477 		       CTLTYPE_INT, "posix_spin_locks",
478 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its Spin "
479 				    "Locks option to which the system attempts "
480 				    "to conform"),
481 		       /* XXX _POSIX_VERSION */
482 		       NULL, _POSIX_SPIN_LOCKS, NULL, 0,
483 		       CTL_KERN, KERN_POSIX_SPIN_LOCKS, CTL_EOL);
484 	sysctl_createv(clog, 0, NULL, NULL,
485 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
486 		       CTLTYPE_INT, "posix_reader_writer_locks",
487 		       SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
488 				    "Read-Write Locks option to which the "
489 				    "system attempts to conform"),
490 		       /* XXX _POSIX_VERSION */
491 		       NULL, _POSIX_READER_WRITER_LOCKS, NULL, 0,
492 		       CTL_KERN, KERN_POSIX_READER_WRITER_LOCKS, CTL_EOL);
493 	sysctl_createv(clog, 0, NULL, NULL,
494 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
495 		       CTLTYPE_INT, "dump_on_panic",
496 		       SYSCTL_DESCR("Perform a crash dump on system panic"),
497 		       NULL, 0, &dumponpanic, 0,
498 		       CTL_KERN, KERN_DUMP_ON_PANIC, CTL_EOL);
499 	sysctl_createv(clog, 0, NULL, NULL,
500 		       CTLFLAG_PERMANENT,
501 		       CTLTYPE_INT, "root_partition",
502 		       SYSCTL_DESCR("Root partition on the root device"),
503 		       sysctl_kern_root_partition, 0, NULL, 0,
504 		       CTL_KERN, KERN_ROOT_PARTITION, CTL_EOL);
505 	sysctl_createv(clog, 0, NULL, NULL,
506 		       CTLFLAG_PERMANENT,
507 		       CTLTYPE_STRUCT, "drivers",
508 		       SYSCTL_DESCR("List of all drivers with block and "
509 				    "character device numbers"),
510 		       sysctl_kern_drivers, 0, NULL, 0,
511 		       CTL_KERN, KERN_DRIVERS, CTL_EOL);
512 	sysctl_createv(clog, 0, NULL, NULL,
513 		       CTLFLAG_PERMANENT,
514 		       CTLTYPE_STRUCT, "cp_id",
515 		       SYSCTL_DESCR("Mapping of CPU number to CPU id"),
516 		       sysctl_kern_cpid, 0, NULL, 0,
517 		       CTL_KERN, KERN_CP_ID, CTL_EOL);
518 	sysctl_createv(clog, 0, NULL, &rnode,
519 		       CTLFLAG_PERMANENT,
520 		       CTLTYPE_NODE, "coredump",
521 		       SYSCTL_DESCR("Coredump settings."),
522 		       NULL, 0, NULL, 0,
523 		       CTL_KERN, CTL_CREATE, CTL_EOL);
524 	sysctl_createv(clog, 0, &rnode, &rnode,
525 		       CTLFLAG_PERMANENT,
526 		       CTLTYPE_NODE, "setid",
527 		       SYSCTL_DESCR("Set-id processes' coredump settings."),
528 		       NULL, 0, NULL, 0,
529 		       CTL_CREATE, CTL_EOL);
530 	sysctl_createv(clog, 0, &rnode, NULL,
531 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
532 		       CTLTYPE_INT, "dump",
533 		       SYSCTL_DESCR("Allow set-id processes to dump core."),
534 		       sysctl_security_setidcore, 0, &security_setidcore_dump,
535 		       sizeof(security_setidcore_dump),
536 		       CTL_CREATE, CTL_EOL);
537 	sysctl_createv(clog, 0, &rnode, NULL,
538 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
539 		       CTLTYPE_STRING, "path",
540 		       SYSCTL_DESCR("Path pattern for set-id coredumps."),
541 		       sysctl_security_setidcorename, 0,
542 		       security_setidcore_path,
543 		       sizeof(security_setidcore_path),
544 		       CTL_CREATE, CTL_EOL);
545 	sysctl_createv(clog, 0, &rnode, NULL,
546 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
547 		       CTLTYPE_INT, "owner",
548 		       SYSCTL_DESCR("Owner id for set-id processes' cores."),
549 		       sysctl_security_setidcore, 0, &security_setidcore_owner,
550 		       0,
551 		       CTL_CREATE, CTL_EOL);
552 	sysctl_createv(clog, 0, &rnode, NULL,
553 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
554 		       CTLTYPE_INT, "group",
555 		       SYSCTL_DESCR("Group id for set-id processes' cores."),
556 		       sysctl_security_setidcore, 0, &security_setidcore_group,
557 		       0,
558 		       CTL_CREATE, CTL_EOL);
559 	sysctl_createv(clog, 0, &rnode, NULL,
560 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
561 		       CTLTYPE_INT, "mode",
562 		       SYSCTL_DESCR("Mode for set-id processes' cores."),
563 		       sysctl_security_setidcore, 0, &security_setidcore_mode,
564 		       0,
565 		       CTL_CREATE, CTL_EOL);
566 	sysctl_createv(clog, 0, NULL, NULL,
567 		       CTLFLAG_IMMEDIATE|CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
568 		       CTLTYPE_INT, "no_sa_support",
569 		       SYSCTL_DESCR("0 if the kernel supports SA, otherwise "
570 		       "it doesn't"),
571 		       NULL, 1, NULL, 0,
572 		       CTL_KERN, CTL_CREATE, CTL_EOL);
573 	sysctl_createv(clog, 0, NULL, NULL,
574 			CTLFLAG_PERMANENT,
575 			CTLTYPE_STRING, "configname",
576 			SYSCTL_DESCR("Name of config file"),
577 			NULL, 0, __UNCONST(kernel_ident), 0,
578 			CTL_KERN, CTL_CREATE, CTL_EOL);
579 	sysctl_createv(clog, 0, NULL, NULL,
580 			CTLFLAG_PERMANENT,
581 			CTLTYPE_STRING, "buildinfo",
582 			SYSCTL_DESCR("Information from build environment"),
583 			NULL, 0, __UNCONST(buildinfo), 0,
584 			CTL_KERN, CTL_CREATE, CTL_EOL);
585 	sysctl_createv(clog, 0, NULL, NULL,
586 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
587 			CTLTYPE_INT, "messages",
588 			SYSCTL_DESCR("Kernel message verbosity"),
589 			sysctl_kern_messages, 0, NULL, 0,
590 			CTL_KERN, CTL_CREATE, CTL_EOL);
591 }
592 
593 SYSCTL_SETUP(sysctl_hw_misc_setup, "sysctl hw subtree misc setup")
594 {
595 
596 	sysctl_createv(clog, 0, NULL, NULL,
597 		       CTLFLAG_PERMANENT,
598 		       CTLTYPE_INT, "usermem",
599 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
600 		       sysctl_hw_usermem, 0, NULL, 0,
601 		       CTL_HW, HW_USERMEM, CTL_EOL);
602 	sysctl_createv(clog, 0, NULL, NULL,
603 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
604 		       CTLTYPE_STRING, "cnmagic",
605 		       SYSCTL_DESCR("Console magic key sequence"),
606 		       sysctl_hw_cnmagic, 0, NULL, CNS_LEN,
607 		       CTL_HW, HW_CNMAGIC, CTL_EOL);
608 	sysctl_createv(clog, 0, NULL, NULL,
609 		       CTLFLAG_PERMANENT,
610 		       CTLTYPE_QUAD, "usermem64",
611 		       SYSCTL_DESCR("Bytes of non-kernel memory"),
612 		       sysctl_hw_usermem, 0, NULL, 0,
613 		       CTL_HW, HW_USERMEM64, CTL_EOL);
614 }
615 
616 #ifdef DEBUG
617 /*
618  * Debugging related system variables.
619  */
620 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
621 struct ctldebug debug5, debug6, debug7, debug8, debug9;
622 struct ctldebug debug10, debug11, debug12, debug13, debug14;
623 struct ctldebug debug15, debug16, debug17, debug18, debug19;
624 static struct ctldebug *debugvars[] = {
625 	&debug0, &debug1, &debug2, &debug3, &debug4,
626 	&debug5, &debug6, &debug7, &debug8, &debug9,
627 	&debug10, &debug11, &debug12, &debug13, &debug14,
628 	&debug15, &debug16, &debug17, &debug18, &debug19,
629 };
630 
631 /*
632  * this setup routine is a replacement for debug_sysctl()
633  *
634  * note that it creates several nodes per defined debug variable
635  */
636 SYSCTL_SETUP(sysctl_debug_setup, "sysctl debug subtree setup")
637 {
638 	struct ctldebug *cdp;
639 	char nodename[20];
640 	int i;
641 
642 	/*
643 	 * two ways here:
644 	 *
645 	 * the "old" way (debug.name -> value) which was emulated by
646 	 * the sysctl(8) binary
647 	 *
648 	 * the new way, which the sysctl(8) binary was actually using
649 
650 	 node	debug
651 	 node	debug.0
652 	 string debug.0.name
653 	 int	debug.0.value
654 	 int	debug.name
655 
656 	 */
657 
658 	for (i = 0; i < __arraycount(debugvars); i++) {
659 		cdp = debugvars[i];
660 		if (cdp->debugname == NULL || cdp->debugvar == NULL)
661 			continue;
662 
663 		snprintf(nodename, sizeof(nodename), "debug%d", i);
664 		sysctl_createv(clog, 0, NULL, NULL,
665 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
666 			       CTLTYPE_NODE, nodename, NULL,
667 			       NULL, 0, NULL, 0,
668 			       CTL_DEBUG, i, CTL_EOL);
669 		sysctl_createv(clog, 0, NULL, NULL,
670 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
671 			       CTLTYPE_STRING, "name", NULL,
672 			       /*XXXUNCONST*/
673 			       NULL, 0, __UNCONST(cdp->debugname), 0,
674 			       CTL_DEBUG, i, CTL_DEBUG_NAME, CTL_EOL);
675 		sysctl_createv(clog, 0, NULL, NULL,
676 			       CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
677 			       CTLTYPE_INT, "value", NULL,
678 			       NULL, 0, cdp->debugvar, 0,
679 			       CTL_DEBUG, i, CTL_DEBUG_VALUE, CTL_EOL);
680 		sysctl_createv(clog, 0, NULL, NULL,
681 			       CTLFLAG_PERMANENT,
682 			       CTLTYPE_INT, cdp->debugname, NULL,
683 			       NULL, 0, cdp->debugvar, 0,
684 			       CTL_DEBUG, CTL_CREATE, CTL_EOL);
685 	}
686 }
687 #endif /* DEBUG */
688 
689 /*
690  * ********************************************************************
691  * section 2: private node-specific helper routines.
692  * ********************************************************************
693  */
694 
695 /*
696  * sysctl helper routine for kern.maxvnodes.  Drain vnodes if
697  * new value is lower than desiredvnodes and then calls reinit
698  * routines that needs to adjust to the new value.
699  */
700 static int
701 sysctl_kern_maxvnodes(SYSCTLFN_ARGS)
702 {
703 	int error, new_vnodes, old_vnodes, new_max;
704 	struct sysctlnode node;
705 
706 	new_vnodes = desiredvnodes;
707 	node = *rnode;
708 	node.sysctl_data = &new_vnodes;
709 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
710 	if (error || newp == NULL)
711 		return (error);
712 
713 	/*
714 	 * sysctl passes down unsigned values, require them
715 	 * to be positive
716 	 */
717 	if (new_vnodes <= 0)
718 		return (EINVAL);
719 
720 	/* Limits: 75% of kmem and physical memory. */
721 	new_max = calc_cache_size(vmem_size(kmem_arena, VMEM_FREE|VMEM_ALLOC),
722 	    75, 75) / VNODE_COST;
723 	if (new_vnodes > new_max)
724 		new_vnodes = new_max;
725 
726 	old_vnodes = desiredvnodes;
727 	desiredvnodes = new_vnodes;
728 	error = vfs_drainvnodes();
729 	if (error) {
730 		desiredvnodes = old_vnodes;
731 		return (error);
732 	}
733 	vfs_reinit();
734 	nchreinit();
735 
736 	return (0);
737 }
738 
739 /*
740  * sysctl helper routine for kern.messages.
741  * Alters boothowto to display kernel messages in increasing verbosity
742  * from 0 to 4.
743  */
744 
745 #define MAXMESSAGES            4
746 static int
747 sysctl_kern_messages(SYSCTLFN_ARGS)
748 {
749 	int error, messageverbose, messagemask, newboothowto;
750 	struct sysctlnode node;
751 
752 	messagemask = (AB_NORMAL|AB_QUIET|AB_SILENT|AB_VERBOSE|AB_DEBUG);
753 	switch (boothowto & messagemask) {
754 	case AB_SILENT:
755 		messageverbose = 0;
756 		break;
757 	case AB_QUIET:
758 		messageverbose = 1;
759 		break;
760 	case AB_VERBOSE:
761 		messageverbose = 3;
762 		break;
763 	case AB_DEBUG:
764 		messageverbose = 4;
765 		break;
766 	case AB_NORMAL:
767 	default:
768 		messageverbose = 2;
769 	}
770 
771 	node = *rnode;
772 	node.sysctl_data = &messageverbose;
773 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
774 	if (error || newp == NULL)
775 		return (error);
776 	if (messageverbose < 0 || messageverbose > MAXMESSAGES)
777 		return EINVAL;
778 
779 	/* Set boothowto */
780 	newboothowto = boothowto & ~messagemask;
781 
782 	switch (messageverbose) {
783 	case 0:
784 		newboothowto |= AB_SILENT;
785 		break;
786 	case 1:
787 		newboothowto |= AB_QUIET;
788 		break;
789 	case 3:
790 		newboothowto |= AB_VERBOSE;
791 		break;
792 	case 4:
793 		newboothowto |= AB_DEBUG;
794 		break;
795 	case 2:
796 	default:                /* Messages default to normal. */
797 		break;
798 	}
799 
800 	boothowto = newboothowto;
801 
802 	return (0);
803 }
804 
805 /*
806  * sysctl helper routine for rtc_offset - set time after changes
807  */
808 static int
809 sysctl_kern_rtc_offset(SYSCTLFN_ARGS)
810 {
811 	struct timespec ts, delta;
812 	int error, new_rtc_offset;
813 	struct sysctlnode node;
814 
815 	new_rtc_offset = rtc_offset;
816 	node = *rnode;
817 	node.sysctl_data = &new_rtc_offset;
818 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
819 	if (error || newp == NULL)
820 		return (error);
821 
822 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
823 	    KAUTH_REQ_SYSTEM_TIME_RTCOFFSET,
824 	    KAUTH_ARG(new_rtc_offset), NULL, NULL))
825 		return (EPERM);
826 	if (rtc_offset == new_rtc_offset)
827 		return (0);
828 
829 	/* if we change the offset, adjust the time */
830 	nanotime(&ts);
831 	delta.tv_sec = 60 * (new_rtc_offset - rtc_offset);
832 	delta.tv_nsec = 0;
833 	timespecadd(&ts, &delta, &ts);
834 	rtc_offset = new_rtc_offset;
835 	return (settime(l->l_proc, &ts));
836 }
837 
838 /*
839  * sysctl helper routine for kern.maxproc. Ensures that the new
840  * values are not too low or too high.
841  */
842 static int
843 sysctl_kern_maxproc(SYSCTLFN_ARGS)
844 {
845 	int error, nmaxproc;
846 	struct sysctlnode node;
847 
848 	nmaxproc = maxproc;
849 	node = *rnode;
850 	node.sysctl_data = &nmaxproc;
851 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
852 	if (error || newp == NULL)
853 		return (error);
854 
855 	if (nmaxproc < 0 || nmaxproc >= PID_MAX)
856 		return (EINVAL);
857 #ifdef __HAVE_CPU_MAXPROC
858 	if (nmaxproc > cpu_maxproc())
859 		return (EINVAL);
860 #endif
861 	maxproc = nmaxproc;
862 
863 	return (0);
864 }
865 
866 /*
867  * sysctl helper function for kern.hostid. The hostid is a long, but
868  * we export it as an int, so we need to give it a little help.
869  */
870 static int
871 sysctl_kern_hostid(SYSCTLFN_ARGS)
872 {
873 	int error, inthostid;
874 	struct sysctlnode node;
875 
876 	inthostid = hostid;  /* XXX assumes sizeof int <= sizeof long */
877 	node = *rnode;
878 	node.sysctl_data = &inthostid;
879 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
880 	if (error || newp == NULL)
881 		return (error);
882 
883 	hostid = (unsigned)inthostid;
884 
885 	return (0);
886 }
887 
888 /*
889  * sysctl helper routine for kern.defcorename. In the case of a new
890  * string being assigned, check that it's not a zero-length string.
891  * (XXX the check in -current doesn't work, but do we really care?)
892  */
893 static int
894 sysctl_kern_defcorename(SYSCTLFN_ARGS)
895 {
896 	int error;
897 	char *newcorename;
898 	struct sysctlnode node;
899 
900 	newcorename = PNBUF_GET();
901 	node = *rnode;
902 	node.sysctl_data = &newcorename[0];
903 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
904 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
905 	if (error || newp == NULL) {
906 		goto done;
907 	}
908 
909 	/*
910 	 * when sysctl_lookup() deals with a string, it's guaranteed
911 	 * to come back nul terminated. So there.  :)
912 	 */
913 	if (strlen(newcorename) == 0) {
914 		error = EINVAL;
915 	} else {
916 		memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
917 		error = 0;
918 	}
919 done:
920 	PNBUF_PUT(newcorename);
921 	return error;
922 }
923 
924 /*
925  * sysctl helper routine for kern.cp_time node. Adds up cpu time
926  * across all cpus.
927  */
928 static int
929 sysctl_kern_cptime(SYSCTLFN_ARGS)
930 {
931 	struct sysctlnode node = *rnode;
932 	uint64_t *cp_time = NULL;
933 	int error, n = ncpu, i;
934 	struct cpu_info *ci;
935 	CPU_INFO_ITERATOR cii;
936 
937 	/*
938 	 * if you specifically pass a buffer that is the size of the
939 	 * sum, or if you are probing for the size, you get the "sum"
940 	 * of cp_time (and the size thereof) across all processors.
941 	 *
942 	 * alternately, you can pass an additional mib number and get
943 	 * cp_time for that particular processor.
944 	 */
945 	switch (namelen) {
946 	case 0:
947 		if (*oldlenp == sizeof(uint64_t) * CPUSTATES || oldp == NULL) {
948 			node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
949 			n = -1; /* SUM */
950 		}
951 		else {
952 			node.sysctl_size = n * sizeof(uint64_t) * CPUSTATES;
953 			n = -2; /* ALL */
954 		}
955 		break;
956 	case 1:
957 		if (name[0] < 0 || name[0] >= n)
958 			return (ENOENT); /* ENOSUCHPROCESSOR */
959 		node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
960 		n = name[0];
961 		/*
962 		 * adjust these so that sysctl_lookup() will be happy
963 		 */
964 		name++;
965 		namelen--;
966 		break;
967 	default:
968 		return (EINVAL);
969 	}
970 
971 	cp_time = kmem_alloc(node.sysctl_size, KM_SLEEP);
972 	node.sysctl_data = cp_time;
973 	memset(cp_time, 0, node.sysctl_size);
974 
975 	for (CPU_INFO_FOREACH(cii, ci)) {
976 		if (n <= 0) {
977 			for (i = 0; i < CPUSTATES; i++) {
978 				cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
979 			}
980 		}
981 		/*
982 		 * if a specific processor was requested and we just
983 		 * did it, we're done here
984 		 */
985 		if (n == 0)
986 			break;
987 		/*
988 		 * if doing "all", skip to next cp_time set for next processor
989 		 */
990 		if (n == -2)
991 			cp_time += CPUSTATES;
992 		/*
993 		 * if we're doing a specific processor, we're one
994 		 * processor closer
995 		 */
996 		if (n > 0)
997 			n--;
998 	}
999 
1000 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1001 	kmem_free(node.sysctl_data, node.sysctl_size);
1002 	return (error);
1003 }
1004 
1005 #if NPTY > 0
1006 /*
1007  * sysctl helper routine for kern.maxptys. Ensures that any new value
1008  * is acceptable to the pty subsystem.
1009  */
1010 static int
1011 sysctl_kern_maxptys(SYSCTLFN_ARGS)
1012 {
1013 	int pty_maxptys(int, int);		/* defined in kern/tty_pty.c */
1014 	int error, xmax;
1015 	struct sysctlnode node;
1016 
1017 	/* get current value of maxptys */
1018 	xmax = pty_maxptys(0, 0);
1019 
1020 	node = *rnode;
1021 	node.sysctl_data = &xmax;
1022 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1023 	if (error || newp == NULL)
1024 		return (error);
1025 
1026 	if (xmax != pty_maxptys(xmax, 1))
1027 		return (EINVAL);
1028 
1029 	return (0);
1030 }
1031 #endif /* NPTY > 0 */
1032 
1033 /*
1034  * sysctl helper routine to do kern.lwp.* work.
1035  */
1036 static int
1037 sysctl_kern_lwp(SYSCTLFN_ARGS)
1038 {
1039 	struct kinfo_lwp klwp;
1040 	struct proc *p;
1041 	struct lwp *l2, *l3;
1042 	char *where, *dp;
1043 	int pid, elem_size, elem_count;
1044 	int buflen, needed, error;
1045 	bool gotit;
1046 
1047 	if (namelen == 1 && name[0] == CTL_QUERY)
1048 		return (sysctl_query(SYSCTLFN_CALL(rnode)));
1049 
1050 	dp = where = oldp;
1051 	buflen = where != NULL ? *oldlenp : 0;
1052 	error = needed = 0;
1053 
1054 	if (newp != NULL || namelen != 3)
1055 		return (EINVAL);
1056 	pid = name[0];
1057 	elem_size = name[1];
1058 	elem_count = name[2];
1059 
1060 	sysctl_unlock();
1061 	if (pid == -1) {
1062 		mutex_enter(proc_lock);
1063 		PROCLIST_FOREACH(p, &allproc) {
1064 			/* Grab a hold on the process. */
1065 			if (!rw_tryenter(&p->p_reflock, RW_READER)) {
1066 				continue;
1067 			}
1068 			mutex_exit(proc_lock);
1069 
1070 			mutex_enter(p->p_lock);
1071 			LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1072 				if (buflen >= elem_size && elem_count > 0) {
1073 					lwp_lock(l2);
1074 					fill_lwp(l2, &klwp);
1075 					lwp_unlock(l2);
1076 					mutex_exit(p->p_lock);
1077 
1078 					/*
1079 					 * Copy out elem_size, but not
1080 					 * larger than the size of a
1081 					 * struct kinfo_proc2.
1082 					 */
1083 					error = dcopyout(l, &klwp, dp,
1084 					    uimin(sizeof(klwp), elem_size));
1085 					if (error) {
1086 						rw_exit(&p->p_reflock);
1087 						goto cleanup;
1088 					}
1089 					mutex_enter(p->p_lock);
1090 					LIST_FOREACH(l3, &p->p_lwps,
1091 					    l_sibling) {
1092 						if (l2 == l3)
1093 							break;
1094 					}
1095 					if (l3 == NULL) {
1096 						mutex_exit(p->p_lock);
1097 						rw_exit(&p->p_reflock);
1098 						error = EAGAIN;
1099 						goto cleanup;
1100 					}
1101 					dp += elem_size;
1102 					buflen -= elem_size;
1103 					elem_count--;
1104 				}
1105 				needed += elem_size;
1106 			}
1107 			mutex_exit(p->p_lock);
1108 
1109 			/* Drop reference to process. */
1110 			mutex_enter(proc_lock);
1111 			rw_exit(&p->p_reflock);
1112 		}
1113 		mutex_exit(proc_lock);
1114 	} else {
1115 		mutex_enter(proc_lock);
1116 		p = proc_find(pid);
1117 		if (p == NULL) {
1118 			error = ESRCH;
1119 			mutex_exit(proc_lock);
1120 			goto cleanup;
1121 		}
1122 		/* Grab a hold on the process. */
1123 		gotit = rw_tryenter(&p->p_reflock, RW_READER);
1124 		mutex_exit(proc_lock);
1125 		if (!gotit) {
1126 			error = ESRCH;
1127 			goto cleanup;
1128 		}
1129 
1130 		mutex_enter(p->p_lock);
1131 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1132 			if (buflen >= elem_size && elem_count > 0) {
1133 				lwp_lock(l2);
1134 				fill_lwp(l2, &klwp);
1135 				lwp_unlock(l2);
1136 				mutex_exit(p->p_lock);
1137 				/*
1138 				 * Copy out elem_size, but not larger than
1139 				 * the size of a struct kinfo_proc2.
1140 				 */
1141 				error = dcopyout(l, &klwp, dp,
1142 				    uimin(sizeof(klwp), elem_size));
1143 				if (error) {
1144 					rw_exit(&p->p_reflock);
1145 					goto cleanup;
1146 				}
1147 				mutex_enter(p->p_lock);
1148 				LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
1149 					if (l2 == l3)
1150 						break;
1151 				}
1152 				if (l3 == NULL) {
1153 					mutex_exit(p->p_lock);
1154 					rw_exit(&p->p_reflock);
1155 					error = EAGAIN;
1156 					goto cleanup;
1157 				}
1158 				dp += elem_size;
1159 				buflen -= elem_size;
1160 				elem_count--;
1161 			}
1162 			needed += elem_size;
1163 		}
1164 		mutex_exit(p->p_lock);
1165 
1166 		/* Drop reference to process. */
1167 		rw_exit(&p->p_reflock);
1168 	}
1169 
1170 	if (where != NULL) {
1171 		*oldlenp = dp - where;
1172 		if (needed > *oldlenp) {
1173 			sysctl_relock();
1174 			return (ENOMEM);
1175 		}
1176 	} else {
1177 		needed += KERN_LWPSLOP;
1178 		*oldlenp = needed;
1179 	}
1180 	error = 0;
1181  cleanup:
1182 	sysctl_relock();
1183 	return (error);
1184 }
1185 
1186 /*
1187  * sysctl helper routine for kern.forkfsleep node. Ensures that the
1188  * given value is not too large or two small, and is at least one
1189  * timer tick if not zero.
1190  */
1191 static int
1192 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
1193 {
1194 	/* userland sees value in ms, internally is in ticks */
1195 	extern int forkfsleep;		/* defined in kern/kern_fork.c */
1196 	int error, timo, lsleep;
1197 	struct sysctlnode node;
1198 
1199 	lsleep = forkfsleep * 1000 / hz;
1200 	node = *rnode;
1201 	node.sysctl_data = &lsleep;
1202 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1203 	if (error || newp == NULL)
1204 		return (error);
1205 
1206 	/* refuse negative values, and overly 'long time' */
1207 	if (lsleep < 0 || lsleep > MAXSLP * 1000)
1208 		return (EINVAL);
1209 
1210 	timo = mstohz(lsleep);
1211 
1212 	/* if the interval is >0 ms && <1 tick, use 1 tick */
1213 	if (lsleep != 0 && timo == 0)
1214 		forkfsleep = 1;
1215 	else
1216 		forkfsleep = timo;
1217 
1218 	return (0);
1219 }
1220 
1221 /*
1222  * sysctl helper routine for kern.root_partition
1223  */
1224 static int
1225 sysctl_kern_root_partition(SYSCTLFN_ARGS)
1226 {
1227 	int rootpart = DISKPART(rootdev);
1228 	struct sysctlnode node = *rnode;
1229 
1230 	node.sysctl_data = &rootpart;
1231 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1232 }
1233 
1234 /*
1235  * sysctl helper function for kern.drivers
1236  */
1237 static int
1238 sysctl_kern_drivers(SYSCTLFN_ARGS)
1239 {
1240 	int error;
1241 	size_t buflen;
1242 	struct kinfo_drivers kd;
1243 	char *start, *where;
1244 	const char *dname;
1245 	int i;
1246 	extern struct devsw_conv *devsw_conv;
1247 	extern int max_devsw_convs;
1248 
1249 	start = where = oldp;
1250 	buflen = *oldlenp;
1251 	if (where == NULL) {
1252 		*oldlenp = max_devsw_convs * sizeof kd;
1253 		return 0;
1254 	}
1255 
1256 	/*
1257 	 * An array of kinfo_drivers structures
1258 	 */
1259 	error = 0;
1260 	sysctl_unlock();
1261 	mutex_enter(&device_lock);
1262 	for (i = 0; i < max_devsw_convs; i++) {
1263 		dname = devsw_conv[i].d_name;
1264 		if (dname == NULL)
1265 			continue;
1266 		if (buflen < sizeof kd) {
1267 			error = ENOMEM;
1268 			break;
1269 		}
1270 		memset(&kd, 0, sizeof(kd));
1271 		kd.d_bmajor = devsw_conv[i].d_bmajor;
1272 		kd.d_cmajor = devsw_conv[i].d_cmajor;
1273 		strlcpy(kd.d_name, dname, sizeof kd.d_name);
1274 		mutex_exit(&device_lock);
1275 		error = dcopyout(l, &kd, where, sizeof kd);
1276 		mutex_enter(&device_lock);
1277 		if (error != 0)
1278 			break;
1279 		buflen -= sizeof kd;
1280 		where += sizeof kd;
1281 	}
1282 	mutex_exit(&device_lock);
1283 	sysctl_relock();
1284 	*oldlenp = where - start;
1285 	return error;
1286 }
1287 
1288 static int
1289 sysctl_security_setidcore(SYSCTLFN_ARGS)
1290 {
1291 	int newsize, error;
1292 	struct sysctlnode node;
1293 
1294 	node = *rnode;
1295 	node.sysctl_data = &newsize;
1296 	newsize = *(int *)rnode->sysctl_data;
1297 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1298 	if (error || newp == NULL)
1299 		return error;
1300 
1301 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
1302 	    0, NULL, NULL, NULL))
1303 		return (EPERM);
1304 
1305 	*(int *)rnode->sysctl_data = newsize;
1306 
1307 	return 0;
1308 }
1309 
1310 static int
1311 sysctl_security_setidcorename(SYSCTLFN_ARGS)
1312 {
1313 	int error;
1314 	char *newsetidcorename;
1315 	struct sysctlnode node;
1316 
1317 	newsetidcorename = PNBUF_GET();
1318 	node = *rnode;
1319 	node.sysctl_data = newsetidcorename;
1320 	memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
1321 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1322 	if (error || newp == NULL) {
1323 		goto out;
1324 	}
1325 	if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
1326 	    0, NULL, NULL, NULL)) {
1327 		error = EPERM;
1328 		goto out;
1329 	}
1330 	if (strlen(newsetidcorename) == 0) {
1331 		error = EINVAL;
1332 		goto out;
1333 	}
1334 	memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
1335 out:
1336 	PNBUF_PUT(newsetidcorename);
1337 	return error;
1338 }
1339 
1340 /*
1341  * sysctl helper routine for kern.cp_id node. Maps cpus to their
1342  * cpuids.
1343  */
1344 static int
1345 sysctl_kern_cpid(SYSCTLFN_ARGS)
1346 {
1347 	struct sysctlnode node = *rnode;
1348 	uint64_t *cp_id = NULL;
1349 	int error, n = ncpu;
1350 	struct cpu_info *ci;
1351 	CPU_INFO_ITERATOR cii;
1352 
1353 	/*
1354 	 * Here you may either retrieve a single cpu id or the whole
1355 	 * set. The size you get back when probing depends on what
1356 	 * you ask for.
1357 	 */
1358 	switch (namelen) {
1359 	case 0:
1360 		node.sysctl_size = n * sizeof(uint64_t);
1361 		n = -2; /* ALL */
1362 		break;
1363 	case 1:
1364 		if (name[0] < 0 || name[0] >= n)
1365 			return (ENOENT); /* ENOSUCHPROCESSOR */
1366 		node.sysctl_size = sizeof(uint64_t);
1367 		n = name[0];
1368 		/*
1369 		 * adjust these so that sysctl_lookup() will be happy
1370 		 */
1371 		name++;
1372 		namelen--;
1373 		break;
1374 	default:
1375 		return (EINVAL);
1376 	}
1377 
1378 	cp_id = kmem_alloc(node.sysctl_size, KM_SLEEP);
1379 	node.sysctl_data = cp_id;
1380 	memset(cp_id, 0, node.sysctl_size);
1381 
1382 	for (CPU_INFO_FOREACH(cii, ci)) {
1383 		if (n <= 0)
1384 			cp_id[0] = cpu_index(ci);
1385 		/*
1386 		 * if a specific processor was requested and we just
1387 		 * did it, we're done here
1388 		 */
1389 		if (n == 0)
1390 			break;
1391 		/*
1392 		 * if doing "all", skip to next cp_id slot for next processor
1393 		 */
1394 		if (n == -2)
1395 			cp_id++;
1396 		/*
1397 		 * if we're doing a specific processor, we're one
1398 		 * processor closer
1399 		 */
1400 		if (n > 0)
1401 			n--;
1402 	}
1403 
1404 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1405 	kmem_free(node.sysctl_data, node.sysctl_size);
1406 	return (error);
1407 }
1408 
1409 /*
1410  * sysctl helper routine for hw.usermem and hw.usermem64. Values are
1411  * calculate on the fly taking into account integer overflow and the
1412  * current wired count.
1413  */
1414 static int
1415 sysctl_hw_usermem(SYSCTLFN_ARGS)
1416 {
1417 	u_int ui;
1418 	u_quad_t uq;
1419 	struct sysctlnode node;
1420 
1421 	node = *rnode;
1422 	switch (rnode->sysctl_num) {
1423 	case HW_USERMEM:
1424 		if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
1425 			ui = UINT_MAX;
1426 		else
1427 			ui *= PAGE_SIZE;
1428 		node.sysctl_data = &ui;
1429 		break;
1430 	case HW_USERMEM64:
1431 		uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
1432 		node.sysctl_data = &uq;
1433 		break;
1434 	default:
1435 		return (EINVAL);
1436 	}
1437 
1438 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1439 }
1440 
1441 /*
1442  * sysctl helper routine for kern.cnmagic node. Pulls the old value
1443  * out, encoded, and stuffs the new value in for decoding.
1444  */
1445 static int
1446 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
1447 {
1448 	char magic[CNS_LEN];
1449 	int error;
1450 	struct sysctlnode node;
1451 
1452 	if (oldp)
1453 		cn_get_magic(magic, CNS_LEN);
1454 	node = *rnode;
1455 	node.sysctl_data = &magic[0];
1456 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
1457 	if (error || newp == NULL)
1458 		return (error);
1459 
1460 	return (cn_set_magic(magic));
1461 }
1462 
1463 /*
1464  * ********************************************************************
1465  * section 3: public helper routines that are used for more than one
1466  * node
1467  * ********************************************************************
1468  */
1469 
1470 /*
1471  * sysctl helper routine for the kern.root_device node and some ports'
1472  * machdep.root_device nodes.
1473  */
1474 int
1475 sysctl_root_device(SYSCTLFN_ARGS)
1476 {
1477 	struct sysctlnode node;
1478 
1479 	node = *rnode;
1480 	node.sysctl_data = __UNCONST(device_xname(root_device));
1481 	node.sysctl_size = strlen(device_xname(root_device)) + 1;
1482 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1483 }
1484 
1485 /*
1486  * sysctl helper routine for kern.consdev, dependent on the current
1487  * state of the console. Also used for machdep.console_device on some
1488  * ports.
1489  */
1490 int
1491 sysctl_consdev(SYSCTLFN_ARGS)
1492 {
1493 	dev_t consdev;
1494 	uint32_t oconsdev;
1495 	struct sysctlnode node;
1496 
1497 	if (cn_tab != NULL)
1498 		consdev = cn_tab->cn_dev;
1499 	else
1500 		consdev = NODEV;
1501 	node = *rnode;
1502 	switch (*oldlenp) {
1503 	case sizeof(consdev):
1504 		node.sysctl_data = &consdev;
1505 		node.sysctl_size = sizeof(consdev);
1506 		break;
1507 	case sizeof(oconsdev):
1508 		oconsdev = (uint32_t)consdev;
1509 		node.sysctl_data = &oconsdev;
1510 		node.sysctl_size = sizeof(oconsdev);
1511 		break;
1512 	default:
1513 		return EINVAL;
1514 	}
1515 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1516 }
1517 
1518 /*
1519  * ********************************************************************
1520  * section 4: support for some helpers
1521  * ********************************************************************
1522  */
1523 
1524 
1525 /*
1526  * Fill in a kinfo_lwp structure for the specified lwp.
1527  */
1528 static void
1529 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
1530 {
1531 	const bool allowaddr = get_expose_address(curproc);
1532 	struct proc *p = l->l_proc;
1533 	struct timeval tv;
1534 
1535 	KASSERT(lwp_locked(l, NULL));
1536 
1537 	memset(kl, 0, sizeof(*kl));
1538 
1539 	kl->l_forw = 0;
1540 	kl->l_back = 0;
1541 	COND_SET_VALUE(kl->l_laddr, PTRTOUINT64(l), allowaddr);
1542 	COND_SET_VALUE(kl->l_addr, PTRTOUINT64(l->l_addr), allowaddr);
1543 	kl->l_stat = l->l_stat;
1544 	kl->l_lid = l->l_lid;
1545 	kl->l_flag = L_INMEM;
1546 	kl->l_flag |= sysctl_map_flags(sysctl_lwpprflagmap, l->l_prflag);
1547 	kl->l_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
1548 
1549 	kl->l_swtime = l->l_swtime;
1550 	kl->l_slptime = l->l_slptime;
1551 	if (l->l_stat == LSONPROC)
1552 		kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
1553 	else
1554 		kl->l_schedflags = 0;
1555 	kl->l_priority = lwp_eprio(l);
1556 	kl->l_usrpri = l->l_priority;
1557 	if (l->l_wchan)
1558 		strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
1559 	COND_SET_VALUE(kl->l_wchan, PTRTOUINT64(l->l_wchan), allowaddr);
1560 	kl->l_cpuid = cpu_index(l->l_cpu);
1561 	bintime2timeval(&l->l_rtime, &tv);
1562 	kl->l_rtime_sec = tv.tv_sec;
1563 	kl->l_rtime_usec = tv.tv_usec;
1564 	kl->l_cpticks = l->l_cpticks;
1565 	kl->l_pctcpu = l->l_pctcpu;
1566 	kl->l_pid = p->p_pid;
1567 	if (l->l_name == NULL)
1568 		kl->l_name[0] = '\0';
1569 	else
1570 		strlcpy(kl->l_name, l->l_name, sizeof(kl->l_name));
1571 }
1572