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